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