xref: /netbsd/sys/arch/mac68k/obio/iwm_fd.c (revision bf9ec67e)
1 /*	$NetBSD: iwm_fd.c,v 1.11 2000/05/27 10:25:15 jdolecek Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 1998 Hauke Fath.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * iwm_fd.c -- Sony (floppy disk) driver for m68k Macintoshes
31  *
32  * The present implementation supports the GCR format (800K) on
33  * non-{DMA,IOP} machines.
34  */
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/callout.h>
38 #include <sys/kernel.h>
39 #include <sys/file.h>
40 #include <sys/ioctl.h>
41 #include <sys/malloc.h>
42 #include <sys/device.h>
43 
44 #define FSTYPENAMES
45 #define DKTYPENAMES
46 #include <sys/disklabel.h>
47 
48 #include <sys/disk.h>
49 #include <sys/dkbad.h>
50 #include <sys/buf.h>
51 #include <sys/uio.h>
52 #include <sys/stat.h>
53 #include <sys/syslog.h>
54 #include <sys/conf.h>
55 
56 #include <machine/autoconf.h>
57 #include <machine/cpu.h>
58 
59 #include <mac68k/obio/iwmreg.h>
60 #include <mac68k/obio/iwm_fdvar.h>
61 
62 #ifdef _LKM
63 #include "iwm_mod.h"
64 #endif
65 
66 /**
67  **	Private functions
68  **/
69 static int map_iwm_base __P((vm_offset_t base));
70 
71 /* Autoconfig */
72 int	iwm_match __P((struct device *, struct cfdata *, void *));
73 void	iwm_attach __P((struct device *, struct device *, void *));
74 int	iwm_print __P((void *, const char *));
75 int	fd_match __P((struct device *, struct cfdata *, void *));
76 void	fd_attach __P((struct device *, struct device *, void *));
77 int	fd_print __P((void *, const char *));
78 
79 /* Disklabel stuff */
80 static void fdGetDiskLabel __P((fd_softc_t *fd, dev_t dev));
81 static void fdPrintDiskLabel __P((struct disklabel *lp));
82 
83 static fdInfo_t *getFDType __P((short unit));
84 static fdInfo_t *fdDeviceToType __P((fd_softc_t *fd, dev_t dev));
85 
86 static void fdstart __P((fd_softc_t *fd));
87 static void remap_geometry __P((daddr_t block, int heads, diskPosition_t *loc));
88 static void motor_off __P((void *param));
89 static int seek __P((fd_softc_t *fd, int style));
90 static int checkTrack __P((diskPosition_t *loc, int debugFlag));
91 static int initCylinderCache __P((fd_softc_t *fd));
92 static void invalidateCylinderCache __P((fd_softc_t *fd));
93 
94 #ifdef _LKM
95 static int probe_fd __P((void));
96 int fd_mod_init __P((void));
97 void fd_mod_free __P((void));
98 #endif
99 
100 static int fdstart_Init __P((fd_softc_t *fd));
101 static int fdstart_Seek __P((fd_softc_t *fd));
102 static int fdstart_Read __P((fd_softc_t *fd));
103 static int fdstart_Write __P((fd_softc_t *fd));
104 static int fdstart_Flush __P((fd_softc_t *fd));
105 static int fdstart_IOFinish __P((fd_softc_t *fd));
106 static int fdstart_IOErr __P((fd_softc_t *fd));
107 static int fdstart_Fault __P((fd_softc_t *fd));
108 static int fdstart_Exit __P((fd_softc_t *fd));
109 
110 
111 /**
112  **	Driver debugging
113  **/
114 
115 #ifdef DEBUG
116 #define IWM_DEBUG
117 #endif
118 
119 
120 static void hexDump __P((u_char *buf, int len));
121 
122 /*
123  * Stuff taken from Egan/Teixeira ch 8: 'if(TRACE_FOO)' debug output
124  * statements don't break indentation, and when DEBUG is not defined,
125  * the compiler code optimizer drops them as dead code.
126  */
127 #ifdef IWM_DEBUG
128 #define M_TRACE_CONFIG	0x0001
129 #define M_TRACE_OPEN	0x0002
130 #define M_TRACE_CLOSE	0x0004
131 #define M_TRACE_READ	0x0008
132 #define M_TRACE_WRITE	0x0010
133 #define M_TRACE_STRAT	(M_TRACE_READ | M_TRACE_WRITE)
134 #define M_TRACE_IOCTL	0x0020
135 #define M_TRACE_STEP	0x0040
136 #define M_TRACE_ALL	0xFFFF
137 
138 #define TRACE_CONFIG	(iwmDebugging & M_TRACE_CONFIG)
139 #define TRACE_OPEN	(iwmDebugging & M_TRACE_OPEN)
140 #define TRACE_CLOSE	(iwmDebugging & M_TRACE_CLOSE)
141 #define TRACE_READ	(iwmDebugging & M_TRACE_READ)
142 #define TRACE_WRITE	(iwmDebugging & M_TRACE_WRITE)
143 #define TRACE_STRAT	(iwmDebugging & M_TRACE_STRAT)
144 #define TRACE_IOCTL	(iwmDebugging & M_TRACE_IOCTL)
145 #define TRACE_STEP	(iwmDebugging & M_TRACE_STEP)
146 #define TRACE_ALL	(iwmDebugging & M_TRACE_ALL)
147 
148  /* -1 = all active */
149 int     iwmDebugging = 0 /* | M_TRACE_OPEN | M_TRACE_STRAT | M_TRACE_IOCTL */ ;
150 
151 #else
152 #define TRACE_CONFIG	0
153 #define TRACE_OPEN	0
154 #define TRACE_CLOSE	0
155 #define TRACE_READ	0
156 #define TRACE_WRITE	0
157 #define TRACE_STRAT	0
158 #define TRACE_IOCTL	0
159 #define TRACE_STEP	0
160 #define TRACE_ALL	0
161 #endif
162 
163 #define DISABLED 	0
164 
165 
166 
167 /**
168  ** Module-global Variables
169  **/
170 
171 /* The IWM base address */
172 u_long IWMBase;
173 
174 /*
175  * Table of supported disk types.
176  * The table order seems to be pretty standardized across NetBSD ports, but
177  * then, they are all MFM... So we roll our own for now.
178  */
179 static fdInfo_t fdTypes[] = {
180 	{1, 80, 512, 10, 10,  800, 12, 2, IWM_GCR, "400K Sony"},
181 	{2, 80, 512, 10, 20, 1600, 12, 2, IWM_GCR, "800K Sony"}
182 };
183 
184 /* Table of GCR disk zones for one side (see IM II-211, The Disk Driver) */
185 static diskZone_t diskZones[] = {
186 	{16, 12,   0, 191},
187 	{16, 11, 192, 367},
188 	{16, 10, 368, 527},
189 	{16,  9, 528, 671},
190 	{16,  8, 672, 799}
191 };
192 
193 /* disk(9) framework device switch */
194 struct dkdriver fd_dkDriver = {
195 	fdstrategy
196 };
197 
198 /* Drive format codes/indexes */
199 enum {
200 	IWM_400K_GCR = 0,
201 	IWM_800K_GCR = 1,
202 	IWM_720K_MFM = 2,
203 	IWM_1440K_MFM = 3
204 };
205 
206 
207 /**
208  ** Autoconfiguration code
209  **/
210 
211 /*
212  * Autoconfig data structures
213  *
214  * These data structures (see <sys/device.h>) are referenced in
215  * compile/$KERNEL/ioconf.c, which is generated by config(8).
216  * Their names are formed like {device}_{ca,cd}.
217  *
218  * {device}_ca
219  * is used for dynamically allocating driver data, probing and
220  * attaching a device;
221  *
222  * {device}_cd
223  * references all found devices of a type.
224  */
225 #ifdef _LKM
226 
227 struct cfdriver iwm_cd = {
228 	NULL,			/* Ptr to array of devices found	 */
229 	"iwm",			/* Device name string			 */
230 	DV_DULL,		/* Device classification		 */
231 	0			/* Number of devices found		 */
232 };
233 struct cfdriver fd_cd = {
234 	NULL,
235 	"fd",
236 	DV_DISK,
237 	0
238 };
239 
240 #else /* defined _LKM */
241 
242 extern struct cfdriver iwm_cd;
243 extern struct cfdriver fd_cd;
244 
245 #endif /* defined _LKM */
246 
247 /* IWM floppy disk controller */
248 struct cfattach iwm_ca = {
249 	sizeof(iwm_softc_t),	/* Size of device data for malloc()	 */
250 	iwm_match,		/* Probe device and return match level	 */
251 	iwm_attach		/* Initialize and attach device		 */
252 };
253 
254 /* Attached floppy disk drives */
255 struct cfattach fd_ca = {
256 	sizeof(fd_softc_t),
257 	fd_match,
258 	fd_attach
259 };
260 
261 
262 
263 /***  Configure the IWM controller  ***/
264 
265 /*
266  * iwm_match
267  *
268  * Is the IWM chip present? Here, *auxp is a ptr to struct confargs
269  * (see <mac68k/mac68k/autoconf.h>), which does not hold any information
270  * to match against. After all, that's what the obio concept is
271  * about: Onboard components that are present depending (only)
272  * on machine type.
273  */
274 int
275 iwm_match(parent, match, auxp)
276 	struct device *parent;
277 	struct cfdata *match;
278 	void *auxp;
279 {
280 	int matched;
281 #ifdef _LKM
282 	int iwmErr;
283 #endif
284 	extern u_long IOBase;		/* from mac68k/machdep.c */
285 	extern u_long IWMBase;
286 
287 	if (0 == map_iwm_base(IOBase)) {
288 		/*
289 		 * Unknown machine HW:
290 		 * The SWIM II/III chips that are present in post-Q700
291 		 * '040 Macs have dropped the IWM register structure.
292 		 * We know next to nothing about the SWIM.
293 		 */
294 		matched = 0;
295 		if (TRACE_CONFIG)
296 			printf("IWM or SWIM not found: Unknown location (SWIM II?).\n");
297 	} else {
298 		matched = 1;
299 		if (TRACE_CONFIG) {
300 			printf("iwm: IWMBase mapped to 0x%lx in VM.\n",
301 			    IWMBase);
302 		}
303 #ifdef _LKM
304 		iwmErr = iwmInit();
305 		if (TRACE_CONFIG)
306 			printf("initIWM() says %d.\n", iwmErr);
307 		matched = (iwmErr == 0) ? 1 : 0;
308 #endif
309 	}
310 	return matched;
311 }
312 
313 
314 /*
315  * iwm_attach
316  *
317  * The IWM is present, initialize it. Then look up the connected drives
318  * and attach them.
319  */
320 void
321 iwm_attach(parent, self, auxp)
322 	struct device *parent;
323 	struct device *self;
324 	void *auxp;
325 {
326 	int iwmErr;
327 	iwm_softc_t *iwm;
328 	iwmAttachArgs_t ia;
329 
330 	printf(": Apple GCR floppy disk controller\n");
331 	iwm = (iwm_softc_t *)self;
332 
333 	iwmErr = iwmInit();
334 	if (TRACE_CONFIG)
335 		printf("initIWM() says %d.\n", iwmErr);
336 
337 	if (0 == iwmErr) {
338 		/* Set up the IWM softc */
339 		iwm->maxRetries = 10;
340 
341 		/* Look for attached drives */
342 		for (ia.unit = 0; ia.unit < IWM_MAX_DRIVE; ia.unit++) {
343 			iwm->fd[ia.unit] = NULL;
344 			ia.driveType = getFDType(ia.unit);
345 			if (NULL != ia.driveType)
346 				config_found_sm(self, (void *)&ia,
347 				    fd_print, NULL);
348 		}
349 		if (TRACE_CONFIG)
350 			printf("iwm: Initialization completed.\n");
351 	} else {
352 		printf("iwm: Chip revision not supported (%d)\n", iwmErr);
353 	}
354 }
355 
356 
357 /*
358  * iwm_print -- print device configuration.
359  *
360  * If the device is not configured 'controller' it is NULL and
361  * we print a message in the *Attach routine; the return value
362  * of *Print() is ignored.
363  */
364 int
365 iwm_print(auxp, controller)
366 	void *auxp;
367 	const char *controller;
368 {
369 	return UNCONF;
370 }
371 
372 
373 /*
374  * map_iwm_base
375  *
376  * Map physical IO address of IWM to VM address
377  */
378 static int
379 map_iwm_base(base)
380 	vm_offset_t base;
381 {
382 	int known;
383 	extern u_long IWMBase;
384 
385 	switch (current_mac_model->class) {
386 	case MACH_CLASSQ:
387 	case MACH_CLASSQ2:
388 	case MACH_CLASSP580:
389 		IWMBase = base + 0x1E000;
390 		known = 1;
391 		break;
392 	case MACH_CLASSII:
393 	case MACH_CLASSPB:
394 	case MACH_CLASSDUO:
395 	case MACH_CLASSIIci:
396 	case MACH_CLASSIIsi:
397 	case MACH_CLASSIIvx:
398 	case MACH_CLASSLC:
399 		IWMBase = base + 0x16000;
400 		known = 1;
401 		break;
402 	case MACH_CLASSIIfx:
403 	case MACH_CLASSAV:
404 	default:
405 		/*
406 		 * Neither IIfx/Q9[05]0 style IOP controllers nor
407 		 * Q[68]40AV DMA based controllers are supported.
408 		 */
409 		if (TRACE_CONFIG)
410 			printf("Unknown floppy controller chip.\n");
411 		IWMBase = 0L;
412 		known = 0;
413 		break;
414 	}
415 	return known;
416 }
417 
418 
419 /***  Configure Sony disk drive(s)  ***/
420 
421 /*
422  * fd_match
423  */
424 int
425 fd_match(parent, match, auxp)
426 	struct device *parent;
427 	struct cfdata *match;
428 	void *auxp;
429 {
430 	int matched, cfUnit;
431 	struct cfdata *cfp;
432 	iwmAttachArgs_t *fdParams;
433 
434 	cfp = match;
435 	fdParams = (iwmAttachArgs_t *)auxp;
436 	cfUnit = cfp->cf_loc[0];
437 	matched = (cfUnit == fdParams->unit || cfUnit == -1) ? 1 : 0;
438 	if (TRACE_CONFIG) {
439 		printf("fdMatch() drive %d ? cfUnit = %d\n",
440 		    fdParams->unit, cfp->cf_loc[0]);
441 	}
442 	return matched;
443 }
444 
445 
446 /*
447  * fd_attach
448  *
449  * We have checked that the IWM is fine and the drive is present,
450  * so we can attach it.
451  */
452 void
453 fd_attach(parent, self, auxp)
454 	struct device *parent;
455 	struct device *self;
456 	void *auxp;
457 {
458 	iwm_softc_t *iwm;
459 	fd_softc_t *fd;
460 	iwmAttachArgs_t *ia;
461 	int driveInfo;
462 
463 	iwm = (iwm_softc_t *)parent;
464 	fd = (fd_softc_t *)self;
465 	ia = (iwmAttachArgs_t *)auxp;
466 
467 	driveInfo = iwmCheckDrive(ia->unit);
468 
469 	fd->currentType = ia->driveType;
470 	fd->unit = ia->unit;
471 	fd->defaultType = &fdTypes[IWM_800K_GCR];
472 	fd->stepDirection = 0;
473 
474 	iwm->fd[ia->unit] = fd;		/* iwm has ptr to this drive */
475 	iwm->drives++;
476 
477 	BUFQ_INIT(&fd->bufQueue);
478 	callout_init(&fd->motor_ch);
479 
480 	printf(" drive %d: ", fd->unit);
481 
482 	if (IWM_NO_DISK & driveInfo) {
483 		printf("(drive empty)\n");
484 	} else
485 		if (!(IWM_DD_DISK & driveInfo)) {
486 			printf("(HD disk -- not supported)\n");
487 			iwmDiskEject(fd->unit);	/* XXX */
488 		} else {
489 			printf("%s %d cyl, %d head(s)\n",
490 			    fd->currentType->description,
491 			    fd->currentType->tracks,
492 			    fd->currentType->heads);
493 		}
494 	if (TRACE_CONFIG) {
495 		int reg, flags, spl;
496 
497 		/* List contents of drive status registers */
498 		spl = spl6();
499 		for (reg = 0; reg < 0x10; reg++) {
500 			flags = iwmQueryDrvFlag(fd->unit, reg);
501 			printf("iwm: Drive register 0x%x = 0x%x\n", reg, flags);
502 		}
503 		splx(spl);
504 	}
505 	fd->diskInfo.dk_name = fd->devInfo.dv_xname;
506 	fd->diskInfo.dk_driver = &fd_dkDriver;
507 	disk_attach(&fd->diskInfo);
508 }
509 
510 
511 /*
512  * fdPrint -- print device configuration.
513  *
514  * If the device is not configured 'controller' refers to a name string
515  * we print here.
516  * Else it is NULL and we print a message in the *Attach routine; the
517  * return value of *Print() is ignored.
518  */
519 int
520 fd_print(auxp, controller)
521 	void *auxp;
522 	const char *controller;
523 {
524 	iwmAttachArgs_t *ia;
525 
526 	ia = (iwmAttachArgs_t *)auxp;
527 	if (NULL != controller)
528 		printf("fd%d at %s", ia->unit, controller);
529 	return UNCONF;
530 }
531 
532 
533 #ifdef _LKM
534 
535 static iwm_softc_t *iwm;
536 
537 /*
538  * fd_mod_init
539  *
540  * Any initializations necessary after loading the module happen here.
541  */
542 int
543 fd_mod_init(void)
544 {
545 	int err;
546 
547 	iwm = (iwm_softc_t *)malloc(sizeof(iwm_softc_t), M_DEVBUF, M_WAITOK);
548 
549 	err = (1 == iwm_match(NULL, NULL, NULL)) ? 0 : EIO;
550 	if (!err) {
551 		memset(iwm, 0, sizeof(iwm_softc_t));
552 		iwm->maxRetries = 10;
553 		err = (0 == probe_fd()) ? 0 : EIO;
554 	}
555 	return err;
556 }
557 
558 
559 /*
560  * fd_mod_free
561  *
562  * Necessary clean-up before unloading the module.
563  */
564 void
565 fd_mod_free(void)
566 {
567 	int unit, spl;
568 
569 	spl = splbio();
570 	/* Release any allocated memory */
571 	for (unit = 0; unit < IWM_MAX_DRIVE; unit++)
572 		if (iwm->fd[unit] != NULL) {
573 			/*
574 			 * Let's hope there is only one task per drive,
575 			 * see callout(9).
576 			 */
577 			callout_stop(&iwm->fd[unit]->motor_ch);
578 			disk_detach(&iwm->fd[unit]->diskInfo);
579 			free(iwm->fd[unit], M_DEVBUF);
580 			iwm->fd[unit] = NULL;
581 		}
582 	free(iwm, M_DEVBUF);
583 	splx(spl);
584 }
585 
586 
587 /*
588  * probe_fd
589  *
590  * See if there are any drives out there and configure them.
591  * If we find a drive we allocate a softc structure for it and
592  * insert its address into the iwm_softc.
593  *
594  * XXX Merge the remainder of probeFD() with the autoconfig framework.
595  */
596 static int
597 probe_fd(void)
598 {
599 	fd_softc_t *fd;
600 	iwmAttachArgs_t ia;
601 	int err, unit;
602 
603 	err = 0;
604 	for (ia.unit = 0; ia.unit < IWM_MAX_DRIVE; ia.unit++) {
605 		ia.driveType = getFDType(ia.unit);
606 		if (NULL == ia.driveType) {
607 			iwm->fd[ia.unit] = NULL;
608 			continue;
609 		}
610 		fd = (fd_softc_t *)malloc(sizeof(fd_softc_t),
611 		    M_DEVBUF, M_WAITOK);
612 		if (fd == NULL) {
613 			err = ENOMEM;
614 			break;
615 		} else {
616 			memset(fd, 0, sizeof(fd_softc_t));
617 
618 			/* This is usually set by the autoconfig framework */
619 			sprintf(fd->devInfo.dv_xname, "fd%d%c", ia.unit, 'a');
620 			fd_attach((struct device *)iwm, (struct device *)fd,
621 			    &ia);
622 		}
623 	}
624 	if (err) {
625 		/* Release any allocated memory */
626 		for (unit = 0; unit < IWM_MAX_DRIVE; unit++)
627 			if (iwm->fd[unit] != NULL) {
628 				free(iwm->fd[unit], M_DEVBUF);
629 				iwm->fd[unit] = NULL;
630 			}
631 	}
632 	return err;
633 }
634 
635 #endif /* defined _LKM */
636 
637 
638 /**
639  ** Implementation section of driver interface
640  **
641  ** The prototypes for these functions are set up automagically
642  ** by macros in mac68k/conf.c. Their names are generated from {fd}
643  ** and {open,close,strategy,dump,size,read,write}. The driver entry
644  ** points are then plugged into bdevsw[] and cdevsw[].
645  **/
646 
647 
648 /*
649  * fdopen
650  *
651  * Open a floppy disk device.
652  */
653 int
654 fdopen(dev, flags, devType, proc)
655 	dev_t dev;
656 	int flags;
657 	int devType;
658 	struct proc *proc;
659 {
660 	fd_softc_t *fd;
661 	fdInfo_t *info;
662 	int partitionMask;
663 	int fdType, fdUnit;
664 	int ierr, err;
665 #ifndef _LKM
666 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
667 #endif
668 	info = NULL;		/* XXX shut up egcs */
669 
670 	/*
671 	 * See <device.h> for struct cfdriver, <disklabel.h> for
672 	 * DISKUNIT() and <atari/atari/device.h> for getsoftc().
673 	 */
674 	fdType = minor(dev) % MAXPARTITIONS;
675 	fdUnit = minor(dev) / MAXPARTITIONS;
676 	if (TRACE_OPEN)
677 		printf("iwm: Open drive %d", fdUnit);
678 
679 	/* Check if device # is valid */
680 	err = (iwm->drives < fdUnit) ? ENXIO : 0;
681 	if (!err) {
682 		(void)iwmSelectDrive(fdUnit);
683 		if (TRACE_OPEN)
684 			printf(".\n Get softc");
685 
686 		/* Get fd state */
687 		fd = iwm->fd[fdUnit];
688 		err = (NULL == fd) ? ENXIO : 0;
689 	}
690 	if (!err) {
691 		if (fd->state & IWM_FD_IS_OPEN) {
692 			/*
693 			 * Allow multiple open calls only if for identical
694 			 * floppy format.
695 			 */
696 			if (TRACE_OPEN)
697 				printf(".\n Drive already opened!\n");
698 			err = (fd->partition == fdType) ? 0 : ENXIO;
699 		} else {
700 			if (TRACE_OPEN)
701 				printf(".\n Get format info");
702 
703 			/* Get format type */
704 			info = fdDeviceToType(fd, dev);
705 			if (NULL == info) {
706 				err = ENXIO;
707 				if (TRACE_OPEN)
708 					printf(".\n No such drive.\n");
709 			}
710 		}
711 	}
712 	if (!err && !(fd->state & IWM_FD_IS_OPEN)) {
713 		if (TRACE_OPEN)
714 			printf(".\n Set diskInfo flags.\n");
715 
716 		fd->writeLabel = 0;		/* XXX currently unused */
717 		fd->partition = fdType;
718 		fd->currentType = info;
719 		fd->drvFlags = iwmCheckDrive(fd->unit);
720 
721 		if (fd->drvFlags & IWM_NO_DISK) {
722 			err = EIO;
723 #ifdef DIAGNOSTIC
724 			printf(" Drive %d is empty.\n", fd->unit);
725 #endif
726 		} else {
727 			if (!(fd->drvFlags & IWM_WRITEABLE) && (flags & FWRITE)) {
728 
729 				err = EPERM;
730 #ifdef DIAGNOSTIC
731 				printf(" Disk is write protected.\n");
732 #endif
733 			} else {
734 				if (!(fd->drvFlags & IWM_DD_DISK)) {
735 					err = ENXIO;
736 #ifdef DIAGNOSTIC
737 					printf(" HD format not supported.\n");
738 #endif
739 					(void)iwmDiskEject(fd->unit);
740 				} else {
741 					/* We're open now! */
742 					fd->state |= IWM_FD_IS_OPEN;
743 					err = initCylinderCache(fd);
744 				}
745 			}
746 		}
747 	}
748 	if (!err) {
749 		/*
750 		 * Later, we might not want to recalibrate the drive when it
751 		 * is already open. For now, it doesn't hurt.
752 		 */
753 		if (TRACE_OPEN)
754 			printf(" Seek track 00 says");
755 
756 		memset(&fd->pos, 0, sizeof(diskPosition_t));
757 		ierr = seek(fd, IWM_SEEK_RECAL);
758 		if (TRACE_OPEN)
759 			printf(" %d.\n", ierr);
760 		err = (0 == ierr) ? 0 : EIO;
761 	}
762 	if (!err) {
763 		/*
764 		 * Update disklabel if we are not yet open.
765 		 * (We shouldn't be: We are synchronous.)
766 		 */
767 		if (fd->diskInfo.dk_openmask == 0)
768 			fdGetDiskLabel(fd, dev);
769 
770 		partitionMask = (1 << fdType);
771 
772 		switch (devType) {
773 		case S_IFCHR:
774 			fd->diskInfo.dk_copenmask |= partitionMask;
775 			break;
776 
777 		case S_IFBLK:
778 			fd->diskInfo.dk_bopenmask |= partitionMask;
779 			break;
780 		}
781 		fd->diskInfo.dk_openmask =
782 		    fd->diskInfo.dk_copenmask | fd->diskInfo.dk_bopenmask;
783 	}
784 	if (TRACE_OPEN)
785 		printf("iwm: fdopen() says %d.\n", err);
786 	return err;
787 }
788 
789 
790 /*
791  * fdclose
792  */
793 int
794 fdclose(dev, flags, devType, proc)
795 	dev_t dev;
796 	int flags;
797 	int devType;
798 	struct proc *proc;
799 {
800 	fd_softc_t *fd;
801 	int partitionMask, fdUnit, fdType;
802 #ifndef _LKM
803 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
804 #endif
805 
806 	if (TRACE_CLOSE)
807 		printf("iwm: Closing driver.");
808 	fdUnit = minor(dev) / MAXPARTITIONS;
809 	fdType = minor(dev) % MAXPARTITIONS;
810 	fd = iwm->fd[fdUnit];
811 	/* release cylinder cache memory */
812 	if (fd->cbuf != NULL)
813 		     free(fd->cbuf, M_DEVBUF);
814 
815 	partitionMask = (1 << fdType);
816 
817 	/* Set state flag. */
818 	fd->state &= ~IWM_FD_IS_OPEN;
819 
820 	switch (devType) {
821 	case S_IFCHR:
822 		fd->diskInfo.dk_copenmask &= ~partitionMask;
823 		break;
824 
825 	case S_IFBLK:
826 		fd->diskInfo.dk_bopenmask &= ~partitionMask;
827 		break;
828 	}
829 	fd->diskInfo.dk_openmask =
830 	    fd->diskInfo.dk_copenmask | fd->diskInfo.dk_bopenmask;
831 	return 0;
832 }
833 
834 
835 /*
836  * fdioctl
837  *
838  * We deal with all the disk-specific ioctls in <sys/dkio.h> here even if
839  * we do not support them.
840  */
841 int
842 fdioctl(dev, cmd, data, flags, proc)
843 	dev_t dev;
844 	u_long cmd;
845 	caddr_t data;
846 	int flags;
847 	struct proc *proc;
848 {
849 	int result, fdUnit, fdType;
850 	fd_softc_t *fd;
851 #ifndef _LKM
852 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
853 #endif
854 
855 	if (TRACE_IOCTL)
856 		printf("iwm: Execute ioctl... ");
857 
858 	/* Check if device # is valid and get its softc */
859 	fdUnit = minor(dev) / MAXPARTITIONS;
860 	fdType = minor(dev) % MAXPARTITIONS;
861 	if (fdUnit >= iwm->drives) {
862 		if (TRACE_IOCTL) {
863 			printf("iwm: Wanted device no (%d) is >= %d.\n",
864 			    fdUnit, iwm->drives);
865 		}
866 		return ENXIO;
867 	}
868 	fd = iwm->fd[fdUnit];
869 	result = 0;
870 
871 	switch (cmd) {
872 	case DIOCGDINFO:
873 		if (TRACE_IOCTL)
874 			printf(" DIOCGDINFO: Get in-core disklabel.\n");
875 		*(struct disklabel *) data = *(fd->diskInfo.dk_label);
876 		result = 0;
877 		break;
878 
879 	case DIOCSDINFO:
880 		if (TRACE_IOCTL)
881 			printf(" DIOCSDINFO: Set in-core disklabel.\n");
882 		result = ((flags & FWRITE) == 0) ? EBADF : 0;
883 		if (result == 0)
884 			result = setdisklabel(fd->diskInfo.dk_label,
885 			    (struct disklabel *)data, 0,
886 			    fd->diskInfo.dk_cpulabel);
887 		break;
888 
889 	case DIOCWDINFO:
890 		if (TRACE_IOCTL)
891 			printf(" DIOCWDINFO: Set in-core disklabel "
892 			    "& update disk.\n");
893 		result = ((flags & FWRITE) == 0) ? EBADF : 0;
894 
895 		if (result == 0)
896 			result = setdisklabel(fd->diskInfo.dk_label,
897 			    (struct disklabel *)data, 0,
898 			    fd->diskInfo.dk_cpulabel);
899 		if (result == 0)
900 			result = writedisklabel(dev, fdstrategy,
901 			    fd->diskInfo.dk_label,
902 			    fd->diskInfo.dk_cpulabel);
903 		break;
904 
905 	case DIOCGPART:
906 		if (TRACE_IOCTL)
907 			printf(" DIOCGPART: Get disklabel & partition table.\n");
908 		((struct partinfo *)data)->disklab = fd->diskInfo.dk_label;
909 		((struct partinfo *)data)->part =
910 		    &fd->diskInfo.dk_label->d_partitions[fdType];
911 		result = 0;
912 		break;
913 
914 	case DIOCRFORMAT:
915 	case DIOCWFORMAT:
916 		if (TRACE_IOCTL)
917 			printf(" DIOC{R,W}FORMAT: No formatter support (yet?).\n");
918 		result = EINVAL;
919 		break;
920 
921 	case DIOCSSTEP:
922 		if (TRACE_IOCTL)
923 			printf(" DIOCSSTEP: IWM does step handshake.\n");
924 		result = EINVAL;
925 		break;
926 
927 	case DIOCSRETRIES:
928 		if (TRACE_IOCTL)
929 			printf(" DIOCSRETRIES: Set max. # of retries.\n");
930 		if (*(int *)data < 0)
931 			result = EINVAL;
932 		else {
933 			iwm->maxRetries = *(int *)data;
934 			result = 0;
935 		}
936 		break;
937 
938 	case DIOCWLABEL:
939 		if (TRACE_IOCTL)
940 			printf(" DIOCWLABEL: Set write access to disklabel.\n");
941 		result = ((flags & FWRITE) == 0) ? EBADF : 0;
942 
943 		if (result == 0)
944 			fd->writeLabel = *(int *)data;
945 		break;
946 
947 	case DIOCSBAD:
948 		if (TRACE_IOCTL)
949 			printf(" DIOCSBAD: No bad144-style handling.\n");
950 		result = EINVAL;
951 		break;
952 
953 	case ODIOCEJECT:
954 	case DIOCEJECT:
955 		/* XXX Eject disk only when unlocked */
956 		if (TRACE_IOCTL)
957 			printf(" DIOCEJECT: Eject disk from unit %d.\n",
958 			    fd->unit);
959 		result = iwmDiskEject(fd->unit);
960 		break;
961 
962 	case DIOCLOCK:
963 		/* XXX Use lock to prevent ejectimg a mounted disk */
964 		if (TRACE_IOCTL)
965 			printf(" DIOCLOCK: No need to (un)lock Sony drive.\n");
966 		result = 0;
967 		break;
968 
969 	default:
970 		if (TRACE_IOCTL)
971 			printf(" Not a disk related ioctl!\n");
972 		result = ENOTTY;
973 		break;
974 	}
975 	return result;
976 }
977 
978 
979 /*
980  * fddump -- We don't dump to a floppy disk.
981  */
982 int
983 fddump(dev, blkno, va, size)
984 	dev_t dev;
985 	daddr_t blkno;
986 	caddr_t va;
987 	size_t size;
988 {
989 	return ENXIO;
990 }
991 
992 
993 /*
994  * fdsize -- We don't dump to a floppy disk.
995  */
996 int
997 fdsize(dev)
998 	dev_t dev;
999 {
1000 	return -1;
1001 }
1002 
1003 
1004 /*
1005  * fdread
1006  */
1007 int
1008 fdread(dev, uio, flags)
1009 	dev_t dev;
1010 	struct uio *uio;
1011 	int flags;
1012 {
1013 	return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
1014 }
1015 
1016 
1017 /*
1018  * fdwrite
1019  */
1020 int
1021 fdwrite(dev, uio, flags)
1022 	dev_t dev;
1023 	struct uio *uio;
1024 	int flags;
1025 {
1026 	return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
1027 }
1028 
1029 
1030 /*
1031  * fdstrategy
1032  *
1033  * Entry point for read and write requests. The strategy routine usually
1034  * queues io requests and kicks off the next transfer if the device is idle;
1035  * but we get no interrupts from the IWM and have to do synchronous
1036  * transfers - no queue.
1037  */
1038 void
1039 fdstrategy(bp)
1040 	struct buf *bp;
1041 {
1042 	int fdUnit, err, done, spl;
1043 	int sectSize, transferSize;
1044 	diskPosition_t physDiskLoc;
1045 	fd_softc_t *fd;
1046 #ifndef _LKM
1047 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
1048 #endif
1049 
1050 	err = 0;
1051 	done = 0;
1052 
1053 	fdUnit = minor(bp->b_dev) / MAXPARTITIONS;
1054 	if (TRACE_STRAT) {
1055 		printf("iwm: fdstrategy()...\n");
1056 		printf("     struct buf is at %p\n", bp);
1057 		printf("     Allocated buffer size (b_bufsize): 0x0%lx\n",
1058 		    bp->b_bufsize);
1059 		printf("     Base address of buffer (b_data): %p\n",
1060 		    bp->b_data);
1061 		printf("     Bytes to be transferred (b_bcount): 0x0%lx\n",
1062 		    bp->b_bcount);
1063 		printf("     Remaining I/O (b_resid): 0x0%lx\n",
1064 		    bp->b_resid);
1065 	}
1066 	/* Check for valid fd unit, controller and io request */
1067 
1068 	if (fdUnit >= iwm->drives) {
1069 		if (TRACE_STRAT)
1070 			printf(" No such unit (%d)\n", fdUnit);
1071 		err = EINVAL;
1072 	}
1073 	if (!err) {
1074 		fd = iwm->fd[fdUnit];
1075 		err = (NULL == fd) ? EINVAL : 0;
1076 	}
1077 	if (!err) {
1078 		sectSize = fd->currentType->sectorSize;
1079 		if (bp->b_blkno < 0
1080 		    || (bp->b_bcount % sectSize) != 0) {
1081 			if (TRACE_STRAT)
1082 				printf(" Illegal transfer size: "
1083 				    "block %d, %ld bytes\n",
1084 				    bp->b_blkno, bp->b_bcount);
1085 			err = EINVAL;
1086 		}
1087 	}
1088 	if (!err) {
1089 		/* Null transfer: Return, nothing to do. */
1090 		if (0 == bp->b_bcount) {
1091 			if (TRACE_STRAT)
1092 				printf(" Zero transfer length.\n");
1093 			done = 1;
1094 		}
1095 	}
1096 	if (!err && !done) {
1097 		/* What to do if we touch the boundaries of the disk? */
1098 		transferSize = (bp->b_bcount + (sectSize - 1)) / sectSize;
1099 		if (bp->b_blkno + transferSize > fd->currentType->secPerDisk) {
1100 			if (TRACE_STRAT) {
1101 				printf("iwm: Transfer beyond end of disk!\n" \
1102 				    " (Starting block %d, # of blocks %d," \
1103 				    " last disk block %d).\n",
1104 				    bp->b_blkno, transferSize,
1105 				    fd->currentType->secPerDisk);
1106 			}
1107 			/* Return EOF if we are exactly at the end of the
1108 			 * disk, EINVAL if we try to reach past the end; else
1109 			 * truncate the request. */
1110 			transferSize = fd->currentType->secPerDisk -
1111 			    bp->b_blkno;
1112 			if (0 == transferSize) {
1113 				bp->b_resid = bp->b_bcount;
1114 				done = 1;
1115 			} else
1116 				if (0 > transferSize)
1117 					err = EINVAL;
1118 				else
1119 					bp->b_bcount = transferSize << DEV_BSHIFT;
1120 		}
1121 	}
1122 	if (!err && !done) {
1123 		/*
1124 		 * Calculate cylinder # for disksort().
1125 		 *
1126 		 * XXX Shouldn't we use the (fake) logical cyl no here?
1127 		 */
1128 		remap_geometry(bp->b_blkno, fd->currentType->heads,
1129 		    &physDiskLoc);
1130 		bp->b_rawblkno = bp->b_blkno;
1131 		bp->b_cylinder = physDiskLoc.track;
1132 
1133 		if (TRACE_STRAT) {
1134 			printf(" This job starts at b_blkno %d; ",
1135 			    bp->b_blkno);
1136 			printf("it gets sorted for cylinder # %ld.\n",
1137 			    bp->b_cylinder);
1138 		}
1139 		spl = splbio();
1140 		callout_stop(&fd->motor_ch);
1141 		disksort_cylinder(&fd->bufQueue, bp);
1142 		if (fd->sc_active == 0)
1143 			fdstart(fd);
1144 		splx(spl);
1145 	}
1146 	/* Clean up, if necessary */
1147 	else {
1148 		if (TRACE_STRAT)
1149 			printf(" fdstrategy() finished early, err = %d.\n",
1150 			    err);
1151 		if (err) {
1152 			bp->b_error = err;
1153 			bp->b_flags |= B_ERROR;
1154 		}
1155 		bp->b_resid = bp->b_bcount;
1156 		biodone(bp);
1157 	}
1158 	/* Comment on results */
1159 	if (TRACE_STRAT) {
1160 		printf("iwm: fdstrategy() done.\n");
1161 		printf("     We have b_resid = %ld bytes left, " \
1162 		    "b_error is %d;\n", bp->b_resid, bp->b_error);
1163 		printf("     b_flags are 0x0%lx.\n", bp->b_flags);
1164 	}
1165 }
1166 
1167 
1168 
1169 /* ======================================================================== */
1170 
1171 
1172 /*
1173  * fdstart
1174  *
1175  * we are called from the strategy() routine to perform a data transfer.
1176  *
1177  * The disk(9) framework demands we run at splbio(); our caller
1178  * takes care of that.
1179  *
1180  * Wish we had pascalish local functions here...
1181  */
1182 
1183 /* fdstart FSM states */
1184 enum {
1185 	state_Init = 0,
1186 	state_Seek,
1187 	state_Read,
1188 	state_Write,
1189 	state_Flush,
1190 	state_IOFinish,
1191 	state_IOErr,
1192 	state_Fault,
1193 	state_Exit,
1194 	state_Done
1195 };
1196 
1197 static void
1198 fdstart(fd)
1199 	fd_softc_t *fd;
1200 {
1201 	int st;
1202 
1203 	static char *stateDesc[] = {
1204 		"Init",
1205 		"Seek",
1206 		"Read",
1207 		"Write",
1208 		"Flush",
1209 		"IOFinish",
1210 		"IOErr",
1211 		"Fault",
1212 		"Exit",
1213 		"Done"
1214 	};
1215 	int (*state[])(fd_softc_t *fd) = {
1216 		fdstart_Init,
1217 		fdstart_Seek,
1218 		fdstart_Read,
1219 		fdstart_Write,
1220 		fdstart_Flush,
1221 		fdstart_IOFinish,
1222 		fdstart_IOErr,
1223 		fdstart_Fault,
1224 		fdstart_Exit
1225 	};
1226 
1227 	st = state_Init;
1228 	do {
1229 		if (TRACE_STRAT)
1230 			printf(" fdstart state %d [%s] ",
1231 			    st, stateDesc[st]);
1232 
1233 		st = (*state[st])(fd);
1234 
1235 		if (TRACE_STRAT)
1236 			printf(".\n");
1237 	} while (st != state_Done);
1238 }
1239 
1240 
1241 /*
1242  * fdstart_Init
1243  *
1244  * Set up things
1245  */
1246 static int
1247 fdstart_Init(fd)
1248 	fd_softc_t *fd;
1249 {
1250 	struct buf *bp;
1251 
1252 	/*
1253 	 * Get the first entry from the queue. This is the buf we gave to
1254 	 * fdstrategy(); disksort() put it into our softc.
1255 	 */
1256 	bp = BUFQ_FIRST(&fd->bufQueue);
1257 	if (NULL == bp) {
1258 		if (TRACE_STRAT)
1259 			printf("Queue empty: Nothing to do");
1260 		return state_Done;
1261 	}
1262 	fd->ioDirection = bp->b_flags & B_READ;
1263 
1264 	disk_busy(&fd->diskInfo);
1265 	if (!(fd->state & IWM_FD_MOTOR_ON)) {
1266 		iwmMotor(fd->unit, 1);
1267 		fd->state |= IWM_FD_MOTOR_ON;
1268 	}
1269 	fd->current_buffer = bp->b_data;
1270 
1271 	/* XXX - assumes blocks of 512 bytes */
1272 	fd->startBlk = bp->b_blkno;
1273 
1274 	fd->iwmErr = 0;
1275 	fd->ioRetries = 0;		/* XXX */
1276 	fd->seekRetries = 0;
1277 	fd->bytesDone = 0;
1278 	fd->bytesLeft = bp->b_bcount;
1279 	return state_Seek;
1280 }
1281 
1282 
1283 /*
1284  * fdstart_Seek
1285  */
1286 static int
1287 fdstart_Seek(fd)
1288 	fd_softc_t *fd;
1289 {
1290 	int state;
1291 
1292 	/* Calculate the side/track/sector our block is at. */
1293 	if (TRACE_STRAT)
1294 		printf(" Remap block %d ", fd->startBlk);
1295 	remap_geometry(fd->startBlk,
1296 	    fd->currentType->heads, &fd->pos);
1297 	if (TRACE_STRAT)
1298 		printf("to c%d_h%d_s%d ", fd->pos.track,
1299 		    fd->pos.side, fd->pos.sector);
1300 
1301 	if (fd->cachedSide != fd->pos.side) {
1302 		if (TRACE_STRAT)
1303 			printf(" (invalidate cache) ");
1304 		invalidateCylinderCache(fd);
1305 		fd->cachedSide = fd->pos.side;
1306 	}
1307 
1308 	/*
1309 	 * If necessary, seek to wanted track. Note that
1310 	 * seek() performs any necessary retries.
1311 	 */
1312 	if (fd->pos.track != fd->pos.oldTrack &&
1313 	    0 != (fd->iwmErr = seek(fd, IWM_SEEK_VANILLA))) {
1314 		state = state_Fault;
1315 	} else {
1316 		state = (fd->ioDirection == IWM_WRITE)
1317 		    ? state_Write : state_Read;
1318 	}
1319 	return state;
1320 }
1321 
1322 
1323 /*
1324  * fdstart_Read
1325  *
1326  * Transfer a sector from disk. Get it from the track cache, if available;
1327  * otherwise, while we are at it, store in the cache all the sectors we find
1328  * on the way.
1329  *
1330  * Track buffering reads:
1331  * o  Look if the sector is already cached.
1332  * o  Else, read sectors into track cache until we meet the header of
1333  *    the sector we want.
1334  * o  Read that sector directly to fs buffer and return.
1335  */
1336 static int
1337 fdstart_Read(fd)
1338 	fd_softc_t *fd;
1339 {
1340 	int i;
1341 	diskPosition_t *pos;
1342 	sectorHdr_t *shdr;
1343 #ifndef _LKM
1344 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
1345 #endif
1346 
1347 	/* Initialize retry counters */
1348 	fd->seekRetries = 0;
1349 	fd->sectRetries = 0;
1350 	pos = &fd->pos;
1351 	shdr = &fd->sHdr;
1352 
1353 	if (TRACE_STRAT)
1354 		printf("<%s c%d_h%d_s%d> ",
1355 		    fd->ioDirection ? "Read" : "Write",
1356 		    pos->track, pos->side, pos->sector);
1357 
1358 	/* Sector already cached? */
1359 	i = pos->sector;
1360 	if (fd->r_slots[i].valid) {
1361 		if (TRACE_STRAT)
1362 			printf("(cached)");
1363 		memcpy(fd->current_buffer, fd->r_slots[i].secbuf,
1364 		    fd->currentType->sectorSize);
1365 		return state_IOFinish;
1366 	}
1367 
1368 	/* Get sector from disk */
1369 	shdr->side = pos->side;
1370 	shdr->sector = pos->sector;
1371 	shdr->track = pos->track;
1372 
1373 	(void)iwmSelectSide(pos->side);
1374 	fd->iwmErr = iwmReadSector(&fd->sHdr, fd->r_slots,
1375 	    fd->current_buffer);
1376 
1377 	/* Check possible error conditions */
1378 	if (TRACE_STRAT)
1379 		printf("c%d_h%d_s%d_err(%d)_sr%d ",
1380 		    shdr->track, shdr->side >> 3,
1381 		    shdr->sector, fd->iwmErr, fd->sectRetries);
1382 
1383 	/* IWM IO error? */
1384 	if (fd->iwmErr != 0)
1385 		return state_IOErr;
1386 
1387 	/* Bad seek? Retry */
1388 	if (shdr->track != pos->track) {
1389 		if (TRACE_STRAT) {
1390 			printf("Wanted track %d, got %d, %d seek retries.\n",
1391 			    pos->track, shdr->track, fd->seekRetries);
1392 		}
1393 		if (iwm->maxRetries > fd->seekRetries++) {
1394 			fd->iwmErr = seek(fd, IWM_SEEK_RECAL);
1395 			if (TRACE_STRAT) {
1396 				printf("[%d]", fd->seekRetries);
1397 				(void)checkTrack(&fd->pos, 1);
1398 			}
1399 		} else
1400 			fd->iwmErr = seekErr;
1401 		return (0 == fd->iwmErr) ? state_Read : state_Fault;
1402 	}
1403 
1404 	/* Sector not found? */
1405 	if (shdr->sector != pos->sector) {
1406 		if (TRACE_STRAT)
1407 			printf("c%d_h%d_s%d sect not found, %d retries ",
1408 			    shdr->track, shdr->side >> 3,
1409 			    shdr->sector, fd->sectRetries);
1410 		fd->iwmErr = noAdrMkErr;
1411 		return state_Fault;
1412 	}
1413 
1414 	/* Success */
1415 	return state_IOFinish;
1416 }
1417 
1418 
1419 /*
1420  * fdstart_Write
1421  *
1422  * Insert a sector into a write buffer slot and mark the slot dirty.
1423  */
1424 static int
1425 fdstart_Write(fd)
1426 	fd_softc_t *fd;
1427 {
1428 	int i;
1429 
1430 	/* XXX let's see... */
1431 	fd->sHdr.side = fd->pos.side;
1432 	fd->sHdr.sector = fd->pos.sector;
1433 	fd->sHdr.track = fd->pos.track;
1434 
1435 	i = fd->pos.sector;
1436 	fd->w_slots[i].secbuf = fd->current_buffer;
1437 	fd->w_slots[i].valid = 1;	/* "valid" is a dirty buffer here */
1438 
1439 	if (TRACE_STRAT)
1440 		printf("<%s c%d_h%d_s%d> (cached) ",
1441 		    fd->ioDirection ? "Read" : "Write",
1442 		    fd->pos.track, fd->pos.side, fd->pos.sector);
1443 	return state_IOFinish;
1444 }
1445 
1446 
1447 
1448 /*
1449  * fdstart_Flush
1450  *
1451  * Flush dirty buffers in the track cache to disk.
1452  */
1453 static int
1454 fdstart_Flush(fd)
1455 	fd_softc_t *fd;
1456 {
1457 	int state;
1458 	int i, dcnt;
1459 	diskPosition_t *pos;
1460 	sectorHdr_t *shdr;
1461 #ifndef _LKM
1462 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
1463 #endif
1464 	dcnt = 0;
1465 	pos = &fd->pos;
1466 	shdr = &fd->sHdr;
1467 
1468 	if (TRACE_STRAT) {
1469 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++)
1470 			if (fd->w_slots[i].valid) {
1471 				printf("|%d", i);
1472 				dcnt++;
1473 			}
1474 		printf("|\n");
1475 
1476 		printf(" <%s c%d_h%d_#s%d>\n",
1477 		    fd->ioDirection ? "Read" : "Write",
1478 		    pos->track, pos->side, dcnt);
1479 	}
1480 	(void)iwmSelectSide(pos->side);
1481 	fd->iwmErr = iwmWriteSector(&fd->sHdr, fd->w_slots);
1482 
1483 	switch (fd->iwmErr) {
1484 	case noErr:		/* Success */
1485 #ifdef DIAGNOSTIC
1486 		/* XXX Panic if buffer not clean? */
1487 		for (i=0; i<IWM_MAX_GCR_SECTORS; i++)
1488 			if (0 != fd->w_slots[i].valid)
1489 				printf("Oops! <c%d_h%d_s%d> not flushed.\n",
1490 				    fd->pos.track, fd->pos.side,
1491 				    fd->pos.sector);
1492 #endif
1493 		if (TRACE_STRAT)
1494 			printf("(Cache flushed, re-initialize) ");
1495 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
1496 			fd->w_slots[i].valid = 0;
1497 			fd->w_slots[i].secbuf = NULL;
1498 		}
1499 		fd->seekRetries = 0;
1500 		state = state_Exit;
1501 		break;
1502 
1503 	case seekErr:		/* Bad seek? Retry */
1504 		if (TRACE_STRAT) {
1505 			printf("Wanted track %d, got %d, %d seek retries.\n",
1506 			    pos->track, shdr->track, fd->seekRetries);
1507 		}
1508 		if (iwm->maxRetries > fd->seekRetries++) {
1509 			fd->iwmErr = seek(fd, IWM_SEEK_RECAL);
1510 			if (TRACE_STRAT) {
1511 				printf("[%d]", fd->seekRetries);
1512 			}
1513 		}
1514 		state = (0 == fd->iwmErr) ? state_Exit : state_Fault;
1515 		break;
1516 
1517 	default:		/* General IWM IO error? */
1518 		state = state_IOErr;
1519 	}
1520 	return state;
1521 }
1522 
1523 
1524 /*
1525  * fdstart_IOFinish
1526  *
1527  * Prepare for next block, if any is available
1528  */
1529 static int
1530 fdstart_IOFinish(fd)
1531 	fd_softc_t *fd;
1532 {
1533 	int state;
1534 
1535 	if (DISABLED && TRACE_STRAT)
1536 		printf("%s c%d_h%d_s%d ok ",
1537 		    fd->ioDirection ? "Read" : "Write",
1538 		    fd->sHdr.track, fd->sHdr.side >> 3, fd->sHdr.sector);
1539 
1540 	fd->bytesDone += fd->currentType->sectorSize;
1541 	fd->bytesLeft -= fd->currentType->sectorSize;
1542 	fd->current_buffer += fd->currentType->sectorSize;
1543 	/*
1544 	 * Instead of recalculating the chs mapping for
1545 	 * each and every sector, check for
1546 	 * 'current sector# <= max sector#' and recalculate
1547 	 * after overflow.
1548 	 */
1549 	fd->startBlk++;
1550 	if (fd->bytesLeft > 0) {
1551 		if (++fd->pos.sector < fd->pos.maxSect) {
1552 			if (TRACE_STRAT)
1553 				printf("continue");
1554 			state = (fd->ioDirection == IWM_WRITE)
1555 			    ? state_Write : state_Read;
1556 		}
1557 		else {
1558 			/*
1559 			 * Invalidate read cache when changing track;
1560 			 * flush write cache to disk.
1561 			 */
1562 			if (fd->ioDirection == IWM_WRITE) {
1563 				if (TRACE_STRAT)
1564 					printf("flush ");
1565 				state = (state_Exit == fdstart_Flush(fd))
1566 				    ? state_Seek : state_IOErr;
1567 			}
1568 			else {
1569 				if (TRACE_STRAT)
1570 					printf("step ");
1571 				invalidateCylinderCache(fd);
1572 				state = state_Seek;
1573 			}
1574 		}
1575 	} else {
1576 		state = (fd->ioDirection == IWM_WRITE)
1577 		    ? state_Flush : state_Exit;
1578 	}
1579 	return state;
1580 }
1581 
1582 
1583 /*
1584  * fdstart_IOErr
1585  *
1586  * Bad IO, repeat
1587  */
1588 static int
1589 fdstart_IOErr(fd)
1590 	fd_softc_t *fd;
1591 {
1592 	int state;
1593 #ifndef _LKM
1594 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
1595 #endif
1596 
1597 #ifdef DIAGNOSTIC
1598 	printf("iwm%sSector() err = %d, %d retries, on c%d_h%d_s%d.\n",
1599 	    fd->ioDirection ? "Read" : "Write",
1600 	    fd->iwmErr, fd->ioRetries, fd->pos.track,
1601 	    fd->pos.side, fd->pos.sector);
1602 #endif
1603 	/* XXX Do statistics */
1604 	if (fd->ioRetries++ < iwm->maxRetries)
1605 		state = (fd->ioDirection == IWM_WRITE)
1606 		    ? state_Flush : state_Read;
1607 	else
1608 		state = state_Fault;
1609 	return state;
1610 }
1611 
1612 
1613 /*
1614  * fdstart_Fault
1615  *
1616  * A non-recoverable error
1617  */
1618 static int
1619 fdstart_Fault(fd)
1620 	fd_softc_t *fd;
1621 {
1622 #ifdef DIAGNOSTIC
1623 	printf("Seek retries %d, IO retries %d, sect retries %d :\n" \
1624 	    "\t\t only found c%d_h%d_s%d \n",
1625 	    fd->seekRetries, fd->ioRetries, fd->sectRetries,
1626 	    fd->sHdr.track, fd->sHdr.side >> 3, fd->sHdr.sector);
1627 	printf("A non-recoverable error: %d ", fd->iwmErr);
1628 #else
1629 	/* ARGSUSED */
1630 #endif
1631 	return state_Exit;
1632 }
1633 
1634 
1635 /*
1636  * fdstart_Exit
1637  *
1638  * We are done, for good or bad
1639  */
1640 static int
1641 fdstart_Exit(fd)
1642 	fd_softc_t *fd;
1643 {
1644 	struct buf *bp;
1645 #ifdef DIAGNOSTIC
1646 	int i;
1647 #endif
1648 
1649 	invalidateCylinderCache(fd);
1650 
1651 #ifdef DIAGNOSTIC
1652 	/* XXX Panic if buffer not clean? */
1653 	for (i=0; i<IWM_MAX_GCR_SECTORS; i++)
1654 		if (0 != fd->w_slots[i].valid)
1655 			printf("Oops! <c%d_h%d_s%d> not flushed.\n",
1656 			    fd->pos.track, fd->pos.side, fd->pos.sector);
1657 #endif
1658 
1659 	bp = BUFQ_FIRST(&fd->bufQueue);
1660 
1661 	bp->b_resid = fd->bytesLeft;
1662 	bp->b_error = (0 == fd->iwmErr) ? 0 : EIO;
1663 	if (fd->iwmErr)
1664 		bp->b_flags |= B_ERROR;
1665 
1666 	if (TRACE_STRAT) {
1667 		printf(" fdstart() finished job; fd->iwmErr = %d, b_error = %d",
1668 		    fd->iwmErr, bp->b_error);
1669 		if (DISABLED)
1670 			hexDump(bp->b_data, bp->b_bcount);
1671 	}
1672 	/*
1673 	 * Remove requested buf from beginning of queue
1674 	 * and release it.
1675 	 */
1676 	BUFQ_REMOVE(&fd->bufQueue, bp);
1677 	if (DISABLED && TRACE_STRAT)
1678 		printf(" Next buf (bufQueue first) at %p\n",
1679 		    BUFQ_FIRST(&fd->bufQueue));
1680 	disk_unbusy(&fd->diskInfo, bp->b_bcount - bp->b_resid);
1681 	biodone(bp);
1682 	/*
1683 	 * Stop motor after 10s
1684 	 *
1685 	 * XXX Unloading the module while the timeout is still
1686 	 *     running WILL crash the machine.
1687 	 */
1688 	callout_reset(&fd->motor_ch, 10 * hz, motor_off, fd);
1689 
1690 	return state_Done;
1691 }
1692 
1693 
1694 /*
1695  * remap_geometry
1696  *
1697  * Remap the rigid UN*X view of a disk's cylinder/sector geometry
1698  * to our zone recorded real Sony drive by splitting the disk
1699  * into zones.
1700  *
1701  * Loop {
1702  * 	Look if logical block number is in current zone
1703  *	NO:	Add # of tracks for current zone to track counter
1704  *		Process next zone
1705  *
1706  *	YES:	Subtract (number of first sector of current zone times heads)
1707  *		from logical block number, then break up the difference
1708  *		in tracks/side/sectors (spt is constant within a zone).
1709  *		Done
1710  * }
1711  */
1712 static void
1713 remap_geometry(block, heads, loc)
1714 	daddr_t block;
1715 	int heads;
1716 	diskPosition_t *loc;
1717 {
1718 	int zone, spt;
1719 	extern diskZone_t diskZones[];
1720 
1721 	spt = 0;		/* XXX Shut up egcs warning */
1722 	loc->oldTrack = loc->track;
1723 	loc->track = 0;
1724 
1725 	for (zone = 0; zone < IWM_GCR_DISK_ZONES; zone++) {
1726 		if (block >= heads * (diskZones[zone].lastBlock + 1)) {
1727 			/* Process full zones */
1728 			loc->track += diskZones[zone].tracks;
1729 		} else {
1730 			/* Process partial zone */
1731 			spt = diskZones[zone].sectPerTrack;
1732 			block -= heads * diskZones[zone].firstBlock;
1733 			loc->track += block / (spt * heads);
1734 			loc->sector = (block % spt);
1735 			loc->side = (block % (spt * heads)) / spt;
1736 			break;
1737 		}
1738 	}
1739 	loc->maxSect = spt;
1740 }
1741 
1742 
1743 /*
1744  * motor_off
1745  *
1746  * Callback for timeout()
1747  */
1748 static void
1749 motor_off(param)
1750 	void *param;
1751 {
1752 	int spl;
1753 	fd_softc_t *fd;
1754 
1755 	fd = (fd_softc_t *)param;
1756 	if (TRACE_STRAT)
1757 		printf("iwm: Switching motor OFF (timeout).\n");
1758 	spl = spl6();
1759 	(void)iwmMotor(fd->unit, 0);
1760 	fd->state &= ~IWM_FD_MOTOR_ON;
1761 	splx(spl);
1762 }
1763 
1764 
1765 /*
1766  * fdGetDiskLabel
1767  *
1768  * Set up disk label with parameters from current disk type.
1769  * Then call the generic disklabel read routine which tries to
1770  * read a label from disk and insert it. If it doesn't exist use
1771  * our defaults.
1772  */
1773 static void
1774 fdGetDiskLabel(fd, dev)
1775 	fd_softc_t *fd;
1776 	dev_t dev;
1777 {
1778 	char *msg;
1779 	int fdType;
1780 	struct disklabel *lp;
1781 	struct cpu_disklabel *clp;
1782 
1783 	if (TRACE_IOCTL)
1784 		printf("iwm: fdGetDiskLabel() for disk %d.\n",
1785 		    minor(dev) / MAXPARTITIONS);
1786 	fdType = minor(dev) % MAXPARTITIONS;
1787 	lp = fd->diskInfo.dk_label;
1788 	clp = fd->diskInfo.dk_cpulabel;
1789 	memset(lp, 0, sizeof(struct disklabel));
1790 	memset(clp, 0, sizeof(struct cpu_disklabel));
1791 	/*
1792 	 * How to describe a drive with a variable # of sectors per
1793 	 * track (8..12) and variable rpm (300..550)? Apple came up
1794 	 * with ZBR in 1983! Un*x drive management sucks.
1795 	 */
1796 	lp->d_type = DTYPE_FLOPPY;
1797 	lp->d_rpm = 300;
1798 	lp->d_secsize = fd->currentType->sectorSize;
1799 	lp->d_ntracks = fd->currentType->heads;
1800 	lp->d_ncylinders = fd->currentType->tracks;
1801 	lp->d_nsectors = fd->currentType->secPerTrack;
1802 	lp->d_secpercyl = fd->currentType->secPerCyl;
1803 	lp->d_secperunit = fd->currentType->secPerDisk;
1804 	lp->d_interleave = fd->currentType->interleave;
1805 	lp->d_trkseek = fd->currentType->stepRate;
1806 
1807 	strcpy(lp->d_typename, dktypenames[DTYPE_FLOPPY]);
1808 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1809 
1810 	lp->d_npartitions = fdType + 1;
1811 	lp->d_partitions[fdType].p_offset = 0;
1812 	lp->d_partitions[fdType].p_size = lp->d_secperunit;
1813 	lp->d_partitions[fdType].p_fstype = FS_BSDFFS;
1814 	lp->d_partitions[fdType].p_fsize = 512;
1815 	lp->d_partitions[fdType].p_frag = 8;
1816 
1817 	lp->d_magic = DISKMAGIC;
1818 	lp->d_magic2 = DISKMAGIC;
1819 	lp->d_checksum = dkcksum(lp);
1820 	/*
1821 	 * Call the generic disklabel extraction routine.  If we don't
1822 	 * find a label on disk, keep our faked one.
1823 	 */
1824 	if (TRACE_OPEN)
1825 		printf(" now calling readdisklabel()...\n");
1826 
1827 	msg = readdisklabel(dev, fdstrategy, lp, clp);
1828 	if (msg == NULL) {
1829 		strncpy(lp->d_packname, "default label",
1830 		    sizeof(lp->d_packname));	/* XXX - ?? */
1831 	}
1832 #ifdef IWM_DEBUG
1833 	else
1834 		printf("iwm: %s.\n", msg);
1835 #endif
1836 	if (TRACE_OPEN)
1837 		fdPrintDiskLabel(lp);
1838 }
1839 
1840 
1841 
1842 /*
1843  * initCylinderCache
1844  *
1845  * Allocate cylinder cache and set up pointers to sectors.
1846  */
1847 static int
1848 initCylinderCache(fd)
1849 	fd_softc_t *fd;
1850 {
1851 	int i;
1852 	int err;
1853 	int secsize;
1854 
1855 	err = 0;
1856 	secsize = fd->currentType->sectorSize;
1857 	fd->cachedSide = 0;
1858 
1859 	fd->cbuf = (unsigned char *) malloc(IWM_MAX_GCR_SECTORS
1860 	    * secsize, M_DEVBUF, M_WAITOK);
1861 	if (NULL == fd->cbuf)
1862 		err = ENOMEM;
1863 	else
1864 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
1865 			fd->w_slots[i].valid = 0;
1866 			fd->w_slots[i].secbuf = NULL;
1867 
1868 			fd->r_slots[i].valid = 0;
1869 			fd->r_slots[i].secbuf = fd->cbuf + i * secsize;
1870 		}
1871 	return err;
1872 }
1873 
1874 
1875 /*
1876  * invalidateCylinderCache
1877  *
1878  * Switching cylinders (tracks?) invalidates the read cache.
1879  */
1880 static void
1881 invalidateCylinderCache(fd)
1882 	fd_softc_t *fd;
1883 {
1884 	int i;
1885 
1886 	fd->cachedSide = 0;
1887 	for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
1888 		fd->r_slots[i].valid = 0;
1889 	}
1890 }
1891 
1892 
1893 /*
1894  * getFDType
1895  *
1896  * return pointer to disk format description
1897  */
1898 static fdInfo_t *
1899 getFDType(unit)
1900 	short unit;
1901 {
1902 	int driveFlags;
1903 	fdInfo_t *thisType;
1904 	extern fdInfo_t fdTypes[];
1905 
1906 	driveFlags = iwmCheckDrive(unit);
1907 /*
1908  * Drive flags are: Bit  0 - 1 = Drive is double sided
1909  *			 1 - 1 = No disk inserted
1910  *			 2 - 1 = Motor is off
1911  *			 3 - 1 = Disk is writeable
1912  *			 4 - 1 = Disk is DD (800/720K)
1913  *			31 - 1 = No drive / invalid drive #
1914  */
1915 	if (TRACE_CONFIG) {
1916 		printf("iwm: Drive %d says 0x0%x (%d)\n",
1917 		    unit, driveFlags, driveFlags);
1918 	}
1919 	if (driveFlags < 0)
1920 		thisType = NULL;/* no such drive	 */
1921 	else
1922 		if (driveFlags & 0x01)
1923 			thisType = &fdTypes[1];	/* double sided		 */
1924 		else
1925 			thisType = &fdTypes[0];	/* single sided		 */
1926 
1927 	return thisType;
1928 }
1929 
1930 
1931 /*
1932  * fdDeviceToType
1933  *
1934  * maps the minor device number (elsewhere: partition type) to
1935  * a corresponding disk format.
1936  * This is currently:
1937  * 	fdXa	default (800K GCR)
1938  *	fdXb	400K GCR
1939  *	fdXc	800K GCR
1940  */
1941 static fdInfo_t *
1942 fdDeviceToType(fd, dev)
1943 	fd_softc_t *fd;
1944 	dev_t dev;
1945 {
1946 	int type;
1947 	fdInfo_t *thisInfo;
1948 	/* XXX This broke with egcs 1.0.2 */
1949 	/* extern fdInfo_t	fdTypes[]; */
1950 
1951 	type = minor(dev) % MAXPARTITIONS;	/* 1,2,... */
1952 	if (type > sizeof(fdTypes) / sizeof(fdTypes[0]))
1953 		thisInfo = NULL;
1954 	else
1955 		thisInfo = (type == 0) ? fd->defaultType : &fdTypes[type - 1];
1956 	return thisInfo;
1957 }
1958 
1959 
1960 /*
1961  * seek
1962  *
1963  * Step to given track; optionally restore to track zero before
1964  * and/or verify correct track.
1965  * Note that any necessary retries are done here.
1966  * We keep the current position on disk in a 'struct diskPosition'.
1967  */
1968 static int
1969 seek(fd, style)
1970 	fd_softc_t *fd;
1971 	int style;
1972 {
1973 	int state, done;
1974 	int err, ierr;
1975 	int steps;
1976 
1977 	diskPosition_t *loc;
1978 	sectorHdr_t hdr;
1979 	char action[32];
1980 #ifndef _LKM
1981 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
1982 #endif
1983 
1984 	char *stateDesc[] = {
1985 		"Init",
1986 		"Seek",
1987 		"Recalibrate",
1988 		"Verify",
1989 		"Exit"
1990 	};
1991 	enum {
1992 		state_Init = 0,
1993 		state_Seek,
1994 		state_Recalibrate,
1995 		state_Verify,
1996 		state_Exit
1997 	};
1998 	/* XXX egcs */
1999 	done = err = ierr = 0;
2000 	fd->seekRetries = 0;
2001 	fd->verifyRetries = 0;
2002 
2003 	loc = &fd->pos;
2004 
2005 	state = state_Init;
2006 	do {
2007 		if (TRACE_STEP)
2008 			printf(" seek state %d [%s].\n",
2009 			    state, stateDesc[state]);
2010 		switch (state) {
2011 
2012 		case state_Init:
2013 			if (TRACE_STEP)
2014 				printf("Current track is %d, new track %d.\n",
2015 				    loc->oldTrack, loc->track);
2016 			memset(&hdr, 0, sizeof(hdr));
2017 			err = ierr = 0;
2018 			fd->seekRetries = 0;
2019 			fd->verifyRetries = 0;
2020 			state = (style == IWM_SEEK_RECAL)
2021 			    ? state_Recalibrate : state_Seek;
2022 			done = 0;
2023 			break;
2024 
2025 		case state_Recalibrate:
2026 			ierr = iwmTrack00();
2027 			if (ierr == 0) {
2028 				loc->oldTrack = 0;
2029 				state = state_Seek;
2030 			} else {
2031 				strncpy(action, "Recalibrate (track 0)",
2032 				    sizeof(action));
2033 				state = state_Exit;
2034 			}
2035 			break;
2036 
2037 		case state_Seek:
2038 			ierr = 0;
2039 			steps = loc->track - loc->oldTrack;
2040 
2041 			if (steps != 0)
2042 				ierr = iwmSeek(steps);
2043 			if (ierr == 0) {
2044 				/* No error or nothing to do */
2045 				state = (style == IWM_SEEK_VERIFY)
2046 				    ? state_Verify : state_Exit;
2047 			} else {
2048 				if (fd->seekRetries++ < iwm->maxRetries)
2049 					state = state_Recalibrate;
2050 				else {
2051 					strncpy(action, "Seek retries",
2052 					    sizeof(action));
2053 					state = state_Exit;
2054 				}
2055 			}
2056 			break;
2057 
2058 		case state_Verify:
2059 			ierr = checkTrack(loc, TRACE_STEP);
2060 			if (ierr == 0 && loc->track == hdr.track)
2061 				state = state_Exit;
2062 			else {
2063 				if (fd->verifyRetries++ < iwm->maxRetries)
2064 					state = state_Recalibrate;
2065 				else {
2066 					strncpy(action, "Verify retries",
2067 					    sizeof(action));
2068 					state = state_Exit;
2069 				}
2070 			}
2071 			break;
2072 
2073 		case state_Exit:
2074 			if (ierr == 0) {
2075 				loc->oldTrack = loc->track;
2076 				err = 0;
2077 				/* Give the head some time to settle down */
2078 				delay(3000);
2079 			} else {
2080 #ifdef DIAGNOSTIC
2081 				printf(" seek() action \"%s\", err = %d.\n",
2082 				    action, ierr);
2083 #endif
2084 				err = EIO;
2085 			}
2086 			done = 1;
2087 			break;
2088 		}
2089 	} while (!done);
2090 	return err;
2091 }
2092 
2093 
2094 /*
2095  * checkTrack
2096  *
2097  * After positioning, get a sector header for validation
2098  */
2099 static int
2100 checkTrack(loc, debugFlag)
2101 	diskPosition_t *loc;
2102 	int debugFlag;
2103 {
2104 	int spl;
2105 	int iwmErr;
2106 	sectorHdr_t hdr;
2107 
2108 	spl = spl6();
2109 	iwmSelectSide(loc->side);
2110 	iwmErr = iwmReadSectHdr(&hdr);
2111 	splx(spl);
2112 	if (debugFlag) {
2113 		printf("Seeked for %d, got at %d, Hdr read err %d.\n",
2114 		    loc->track, hdr.track, iwmErr);
2115 	}
2116 	return iwmErr;
2117 }
2118 
2119 
2120 /* Debugging stuff */
2121 
2122 static void
2123 hexDump(buf, len)
2124 	u_char *buf;
2125 	int len;
2126 {
2127 	int i, j;
2128 	u_char ch;
2129 
2130 	printf("\nDump %d from %p:\n", len, buf);
2131 	i = j = 0;
2132 	if (NULL != buf) do {
2133 		printf("%04x: ", i);
2134 		for (j = 0; j < 8; j++)
2135 			printf("%02x ", buf[i + j]);
2136 		printf(" ");
2137 		for (j = 8; j < 16; j++)
2138 			printf("%02x ", buf[i + j]);
2139 		printf(" ");
2140 		for (j = 0; j < 16; j++) {
2141 			ch = buf[i + j];
2142 			if (ch > 31 && ch < 127)
2143 				printf("%c", ch);
2144 			else
2145 				printf(".");
2146 		}
2147 		printf("\n");
2148 		i += 16;
2149 	} while (len > i);
2150 }
2151 
2152 
2153 static void
2154 fdPrintDiskLabel(lp)
2155 	struct disklabel *lp;
2156 {
2157 	int i;
2158 
2159 	printf("iwm: Disklabel entries of current floppy.\n");
2160 	printf("\t d_type:\t%d (%s)\n", lp->d_type,
2161 	    dktypenames[lp->d_type]);
2162 	printf("\t d_typename:\t%s\n", lp->d_typename);
2163 	printf("\t d_packname:\t%s\n", lp->d_packname);
2164 
2165 	printf("\t d_secsize:\t%d\n", lp->d_secsize);
2166 	printf("\t d_nsectors:\t%d\n", lp->d_nsectors);
2167 	printf("\t d_ntracks:\t%d\n", lp->d_ntracks);
2168 	printf("\t d_ncylinders:\t%d\n", lp->d_ncylinders);
2169 	printf("\t d_secpercyl:\t%d\n", lp->d_secpercyl);
2170 	printf("\t d_secperunit:\t%d\n", lp->d_secperunit);
2171 
2172 	printf("\t d_rpm: \t%d\n", lp->d_rpm);
2173 	printf("\t d_interleave:\t%d\n", lp->d_interleave);
2174 	printf("\t d_trkseek:\t%d [ms]\n", lp->d_trkseek);
2175 
2176 	printf(" d_npartitions:\t%d\n", lp->d_npartitions);
2177 	for (i = 0; i < lp->d_npartitions; i++) {
2178 		printf("\t d_partitions[%d].p_offset:\t%d\n", i,
2179 		    lp->d_partitions[i].p_offset);
2180 		printf("\t d_partitions[%d].p_size:\t%d\n", i,
2181 		    lp->d_partitions[i].p_size);
2182 		printf("\t d_partitions[%d].p_fstype:\t%d (%s)\n", i,
2183 		    lp->d_partitions[i].p_fstype,
2184 		    fstypenames[lp->d_partitions[i].p_fstype]);
2185 		printf("\t d_partitions[%d].p_frag:\t%d\n", i,
2186 		    lp->d_partitions[i].p_frag);
2187 		printf("\n");
2188 	}
2189 }
2190