Home | History | Annotate | Line # | Download | only in obio
iwm_fd.c revision 1.25
      1 /*	$NetBSD: iwm_fd.c,v 1.25 2003/10/27 22:16:04 fredb 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.25 2003/10/27 22:16:04 fredb 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 	fd = NULL;		/* XXX shut up gcc3 */
    664 
    665 	/*
    666 	 * See <device.h> for struct cfdriver, <disklabel.h> for
    667 	 * DISKUNIT() and <atari/atari/device.h> for getsoftc().
    668 	 */
    669 	fdType = minor(dev) % MAXPARTITIONS;
    670 	fdUnit = minor(dev) / MAXPARTITIONS;
    671 	if (TRACE_OPEN)
    672 		printf("iwm: Open drive %d", fdUnit);
    673 
    674 	/* Check if device # is valid */
    675 	err = (iwm->drives < fdUnit) ? ENXIO : 0;
    676 	if (!err) {
    677 		(void)iwmSelectDrive(fdUnit);
    678 		if (TRACE_OPEN)
    679 			printf(".\n Get softc");
    680 
    681 		/* Get fd state */
    682 		fd = iwm->fd[fdUnit];
    683 		err = (NULL == fd) ? ENXIO : 0;
    684 	}
    685 	if (!err) {
    686 		if (fd->state & IWM_FD_IS_OPEN) {
    687 			/*
    688 			 * Allow multiple open calls only if for identical
    689 			 * floppy format.
    690 			 */
    691 			if (TRACE_OPEN)
    692 				printf(".\n Drive already opened!\n");
    693 			err = (fd->partition == fdType) ? 0 : ENXIO;
    694 		} else {
    695 			if (TRACE_OPEN)
    696 				printf(".\n Get format info");
    697 
    698 			/* Get format type */
    699 			info = fdDeviceToType(fd, dev);
    700 			if (NULL == info) {
    701 				err = ENXIO;
    702 				if (TRACE_OPEN)
    703 					printf(".\n No such drive.\n");
    704 			}
    705 		}
    706 	}
    707 	if (!err && !(fd->state & IWM_FD_IS_OPEN)) {
    708 		if (TRACE_OPEN)
    709 			printf(".\n Set diskInfo flags.\n");
    710 
    711 		fd->writeLabel = 0;		/* XXX currently unused */
    712 		fd->partition = fdType;
    713 		fd->currentType = info;
    714 		fd->drvFlags = iwmCheckDrive(fd->unit);
    715 
    716 		if (fd->drvFlags & IWM_NO_DISK) {
    717 			err = EIO;
    718 #ifdef DIAGNOSTIC
    719 			printf(" Drive %d is empty.\n", fd->unit);
    720 #endif
    721 		} else {
    722 			if (!(fd->drvFlags & IWM_WRITABLE) && (flags & FWRITE)) {
    723 
    724 				err = EPERM;
    725 #ifdef DIAGNOSTIC
    726 				printf(" Disk is write protected.\n");
    727 #endif
    728 			} else {
    729 				if (!(fd->drvFlags & IWM_DD_DISK)) {
    730 					err = ENXIO;
    731 #ifdef DIAGNOSTIC
    732 					printf(" HD format not supported.\n");
    733 #endif
    734 					(void)iwmDiskEject(fd->unit);
    735 				} else {
    736 					/* We're open now! */
    737 					fd->state |= IWM_FD_IS_OPEN;
    738 					err = initCylinderCache(fd);
    739 				}
    740 			}
    741 		}
    742 	}
    743 	if (!err) {
    744 		/*
    745 		 * Later, we might not want to recalibrate the drive when it
    746 		 * is already open. For now, it doesn't hurt.
    747 		 */
    748 		if (TRACE_OPEN)
    749 			printf(" Seek track 00 says");
    750 
    751 		memset(&fd->pos, 0, sizeof(diskPosition_t));
    752 		ierr = seek(fd, IWM_SEEK_RECAL);
    753 		if (TRACE_OPEN)
    754 			printf(" %d.\n", ierr);
    755 		err = (0 == ierr) ? 0 : EIO;
    756 	}
    757 	if (!err) {
    758 		/*
    759 		 * Update disklabel if we are not yet open.
    760 		 * (We shouldn't be: We are synchronous.)
    761 		 */
    762 		if (fd->diskInfo.dk_openmask == 0)
    763 			fdGetDiskLabel(fd, dev);
    764 
    765 		partitionMask = (1 << fdType);
    766 
    767 		switch (devType) {
    768 		case S_IFCHR:
    769 			fd->diskInfo.dk_copenmask |= partitionMask;
    770 			break;
    771 
    772 		case S_IFBLK:
    773 			fd->diskInfo.dk_bopenmask |= partitionMask;
    774 			break;
    775 		}
    776 		fd->diskInfo.dk_openmask =
    777 		    fd->diskInfo.dk_copenmask | fd->diskInfo.dk_bopenmask;
    778 	}
    779 	if (TRACE_OPEN)
    780 		printf("iwm: fdopen() says %d.\n", err);
    781 	return err;
    782 }
    783 
    784 
    785 /*
    786  * fdclose
    787  */
    788 int
    789 fdclose(dev, flags, devType, proc)
    790 	dev_t dev;
    791 	int flags;
    792 	int devType;
    793 	struct proc *proc;
    794 {
    795 	fd_softc_t *fd;
    796 	int partitionMask, fdUnit, fdType;
    797 #ifndef _LKM
    798 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
    799 #endif
    800 
    801 	if (TRACE_CLOSE)
    802 		printf("iwm: Closing driver.");
    803 	fdUnit = minor(dev) / MAXPARTITIONS;
    804 	fdType = minor(dev) % MAXPARTITIONS;
    805 	fd = iwm->fd[fdUnit];
    806 	/* release cylinder cache memory */
    807 	if (fd->cbuf != NULL)
    808 		     free(fd->cbuf, M_DEVBUF);
    809 
    810 	partitionMask = (1 << fdType);
    811 
    812 	/* Set state flag. */
    813 	fd->state &= ~IWM_FD_IS_OPEN;
    814 
    815 	switch (devType) {
    816 	case S_IFCHR:
    817 		fd->diskInfo.dk_copenmask &= ~partitionMask;
    818 		break;
    819 
    820 	case S_IFBLK:
    821 		fd->diskInfo.dk_bopenmask &= ~partitionMask;
    822 		break;
    823 	}
    824 	fd->diskInfo.dk_openmask =
    825 	    fd->diskInfo.dk_copenmask | fd->diskInfo.dk_bopenmask;
    826 	return 0;
    827 }
    828 
    829 
    830 /*
    831  * fdioctl
    832  *
    833  * We deal with all the disk-specific ioctls in <sys/dkio.h> here even if
    834  * we do not support them.
    835  */
    836 int
    837 fdioctl(dev, cmd, data, flags, proc)
    838 	dev_t dev;
    839 	u_long cmd;
    840 	caddr_t data;
    841 	int flags;
    842 	struct proc *proc;
    843 {
    844 	int result, fdUnit, fdType;
    845 	fd_softc_t *fd;
    846 #ifndef _LKM
    847 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
    848 #endif
    849 
    850 	if (TRACE_IOCTL)
    851 		printf("iwm: Execute ioctl... ");
    852 
    853 	/* Check if device # is valid and get its softc */
    854 	fdUnit = minor(dev) / MAXPARTITIONS;
    855 	fdType = minor(dev) % MAXPARTITIONS;
    856 	if (fdUnit >= iwm->drives) {
    857 		if (TRACE_IOCTL) {
    858 			printf("iwm: Wanted device no (%d) is >= %d.\n",
    859 			    fdUnit, iwm->drives);
    860 		}
    861 		return ENXIO;
    862 	}
    863 	fd = iwm->fd[fdUnit];
    864 	result = 0;
    865 
    866 	switch (cmd) {
    867 	case DIOCGDINFO:
    868 		if (TRACE_IOCTL)
    869 			printf(" DIOCGDINFO: Get in-core disklabel.\n");
    870 		*(struct disklabel *) data = *(fd->diskInfo.dk_label);
    871 		result = 0;
    872 		break;
    873 
    874 	case DIOCSDINFO:
    875 		if (TRACE_IOCTL)
    876 			printf(" DIOCSDINFO: Set in-core disklabel.\n");
    877 		result = ((flags & FWRITE) == 0) ? EBADF : 0;
    878 		if (result == 0)
    879 			result = setdisklabel(fd->diskInfo.dk_label,
    880 			    (struct disklabel *)data, 0,
    881 			    fd->diskInfo.dk_cpulabel);
    882 		break;
    883 
    884 	case DIOCWDINFO:
    885 		if (TRACE_IOCTL)
    886 			printf(" DIOCWDINFO: Set in-core disklabel "
    887 			    "& update disk.\n");
    888 		result = ((flags & FWRITE) == 0) ? EBADF : 0;
    889 
    890 		if (result == 0)
    891 			result = setdisklabel(fd->diskInfo.dk_label,
    892 			    (struct disklabel *)data, 0,
    893 			    fd->diskInfo.dk_cpulabel);
    894 		if (result == 0)
    895 			result = writedisklabel(dev, fdstrategy,
    896 			    fd->diskInfo.dk_label,
    897 			    fd->diskInfo.dk_cpulabel);
    898 		break;
    899 
    900 	case DIOCGPART:
    901 		if (TRACE_IOCTL)
    902 			printf(" DIOCGPART: Get disklabel & partition table.\n");
    903 		((struct partinfo *)data)->disklab = fd->diskInfo.dk_label;
    904 		((struct partinfo *)data)->part =
    905 		    &fd->diskInfo.dk_label->d_partitions[fdType];
    906 		result = 0;
    907 		break;
    908 
    909 	case DIOCRFORMAT:
    910 	case DIOCWFORMAT:
    911 		if (TRACE_IOCTL)
    912 			printf(" DIOC{R,W}FORMAT: No formatter support (yet?).\n");
    913 		result = EINVAL;
    914 		break;
    915 
    916 	case DIOCSSTEP:
    917 		if (TRACE_IOCTL)
    918 			printf(" DIOCSSTEP: IWM does step handshake.\n");
    919 		result = EINVAL;
    920 		break;
    921 
    922 	case DIOCSRETRIES:
    923 		if (TRACE_IOCTL)
    924 			printf(" DIOCSRETRIES: Set max. # of retries.\n");
    925 		if (*(int *)data < 0)
    926 			result = EINVAL;
    927 		else {
    928 			iwm->maxRetries = *(int *)data;
    929 			result = 0;
    930 		}
    931 		break;
    932 
    933 	case DIOCWLABEL:
    934 		if (TRACE_IOCTL)
    935 			printf(" DIOCWLABEL: Set write access to disklabel.\n");
    936 		result = ((flags & FWRITE) == 0) ? EBADF : 0;
    937 
    938 		if (result == 0)
    939 			fd->writeLabel = *(int *)data;
    940 		break;
    941 
    942 	case DIOCSBAD:
    943 		if (TRACE_IOCTL)
    944 			printf(" DIOCSBAD: No bad144-style handling.\n");
    945 		result = EINVAL;
    946 		break;
    947 
    948 	case ODIOCEJECT:
    949 	case DIOCEJECT:
    950 		/* XXX Eject disk only when unlocked */
    951 		if (TRACE_IOCTL)
    952 			printf(" DIOCEJECT: Eject disk from unit %d.\n",
    953 			    fd->unit);
    954 		result = iwmDiskEject(fd->unit);
    955 		break;
    956 
    957 	case DIOCLOCK:
    958 		/* XXX Use lock to prevent ejectimg a mounted disk */
    959 		if (TRACE_IOCTL)
    960 			printf(" DIOCLOCK: No need to (un)lock Sony drive.\n");
    961 		result = 0;
    962 		break;
    963 
    964 	default:
    965 		if (TRACE_IOCTL)
    966 			printf(" Not a disk related ioctl!\n");
    967 		result = ENOTTY;
    968 		break;
    969 	}
    970 	return result;
    971 }
    972 
    973 
    974 /*
    975  * fdread
    976  */
    977 int
    978 fdread(dev, uio, flags)
    979 	dev_t dev;
    980 	struct uio *uio;
    981 	int flags;
    982 {
    983 	return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
    984 }
    985 
    986 
    987 /*
    988  * fdwrite
    989  */
    990 int
    991 fdwrite(dev, uio, flags)
    992 	dev_t dev;
    993 	struct uio *uio;
    994 	int flags;
    995 {
    996 	return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
    997 }
    998 
    999 
   1000 /*
   1001  * fdstrategy
   1002  *
   1003  * Entry point for read and write requests. The strategy routine usually
   1004  * queues io requests and kicks off the next transfer if the device is idle;
   1005  * but we get no interrupts from the IWM and have to do synchronous
   1006  * transfers - no queue.
   1007  */
   1008 void
   1009 fdstrategy(bp)
   1010 	struct buf *bp;
   1011 {
   1012 	int fdUnit, err, done, spl;
   1013 	int sectSize, transferSize;
   1014 	diskPosition_t physDiskLoc;
   1015 	fd_softc_t *fd;
   1016 #ifndef _LKM
   1017 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1018 #endif
   1019 
   1020 	err = 0;
   1021 	done = 0;
   1022 	sectSize = 0;		/* XXX shut up gcc3 */
   1023 	fd = NULL;		/* XXX shut up gcc3 */
   1024 
   1025 	fdUnit = minor(bp->b_dev) / MAXPARTITIONS;
   1026 	if (TRACE_STRAT) {
   1027 		printf("iwm: fdstrategy()...\n");
   1028 		printf("     struct buf is at %p\n", bp);
   1029 		printf("     Allocated buffer size (b_bufsize): 0x0%lx\n",
   1030 		    bp->b_bufsize);
   1031 		printf("     Base address of buffer (b_data): %p\n",
   1032 		    bp->b_data);
   1033 		printf("     Bytes to be transferred (b_bcount): 0x0%lx\n",
   1034 		    bp->b_bcount);
   1035 		printf("     Remaining I/O (b_resid): 0x0%lx\n",
   1036 		    bp->b_resid);
   1037 	}
   1038 	/* Check for valid fd unit, controller and io request */
   1039 
   1040 	if (fdUnit >= iwm->drives) {
   1041 		if (TRACE_STRAT)
   1042 			printf(" No such unit (%d)\n", fdUnit);
   1043 		err = EINVAL;
   1044 	}
   1045 	if (!err) {
   1046 		fd = iwm->fd[fdUnit];
   1047 		err = (NULL == fd) ? EINVAL : 0;
   1048 	}
   1049 	if (!err) {
   1050 		sectSize = fd->currentType->sectorSize;
   1051 		if (bp->b_blkno < 0
   1052 		    || (bp->b_bcount % sectSize) != 0) {
   1053 			if (TRACE_STRAT)
   1054 				printf(" Illegal transfer size: "
   1055 				    "block %lld, %ld bytes\n",
   1056 				    (long long) bp->b_blkno, bp->b_bcount);
   1057 			err = EINVAL;
   1058 		}
   1059 	}
   1060 	if (!err) {
   1061 		/* Null transfer: Return, nothing to do. */
   1062 		if (0 == bp->b_bcount) {
   1063 			if (TRACE_STRAT)
   1064 				printf(" Zero transfer length.\n");
   1065 			done = 1;
   1066 		}
   1067 	}
   1068 	if (!err && !done) {
   1069 		/* What to do if we touch the boundaries of the disk? */
   1070 		transferSize = (bp->b_bcount + (sectSize - 1)) / sectSize;
   1071 		if (bp->b_blkno + transferSize > fd->currentType->secPerDisk) {
   1072 			if (TRACE_STRAT) {
   1073 				printf("iwm: Transfer beyond end of disk!\n" \
   1074 				    " (Starting block %lld, # of blocks %d," \
   1075 				    " last disk block %d).\n",
   1076 				    (long long) bp->b_blkno, transferSize,
   1077 				    fd->currentType->secPerDisk);
   1078 			}
   1079 			/* Return EOF if we are exactly at the end of the
   1080 			 * disk, EINVAL if we try to reach past the end; else
   1081 			 * truncate the request. */
   1082 			transferSize = fd->currentType->secPerDisk -
   1083 			    bp->b_blkno;
   1084 			if (0 == transferSize) {
   1085 				bp->b_resid = bp->b_bcount;
   1086 				done = 1;
   1087 			} else
   1088 				if (0 > transferSize)
   1089 					err = EINVAL;
   1090 				else
   1091 					bp->b_bcount = transferSize << DEV_BSHIFT;
   1092 		}
   1093 	}
   1094 	if (!err && !done) {
   1095 		/*
   1096 		 * Calculate cylinder # for disksort().
   1097 		 *
   1098 		 * XXX Shouldn't we use the (fake) logical cyl no here?
   1099 		 */
   1100 		remap_geometry(bp->b_blkno, fd->currentType->heads,
   1101 		    &physDiskLoc);
   1102 		bp->b_rawblkno = bp->b_blkno;
   1103 		bp->b_cylinder = physDiskLoc.track;
   1104 
   1105 		if (TRACE_STRAT) {
   1106 			printf(" This job starts at b_blkno %lld; ",
   1107 			    (long long) bp->b_blkno);
   1108 			printf("it gets sorted for cylinder # %ld.\n",
   1109 			    bp->b_cylinder);
   1110 		}
   1111 		spl = splbio();
   1112 		callout_stop(&fd->motor_ch);
   1113 		BUFQ_PUT(&fd->bufQueue, bp);
   1114 		if (fd->sc_active == 0)
   1115 			fdstart(fd);
   1116 		splx(spl);
   1117 	}
   1118 	/* Clean up, if necessary */
   1119 	else {
   1120 		if (TRACE_STRAT)
   1121 			printf(" fdstrategy() finished early, err = %d.\n",
   1122 			    err);
   1123 		if (err) {
   1124 			bp->b_error = err;
   1125 			bp->b_flags |= B_ERROR;
   1126 		}
   1127 		bp->b_resid = bp->b_bcount;
   1128 		biodone(bp);
   1129 	}
   1130 	/* Comment on results */
   1131 	if (TRACE_STRAT) {
   1132 		printf("iwm: fdstrategy() done.\n");
   1133 		printf("     We have b_resid = %ld bytes left, " \
   1134 		    "b_error is %d;\n", bp->b_resid, bp->b_error);
   1135 		printf("     b_flags are 0x0%lx.\n", bp->b_flags);
   1136 	}
   1137 }
   1138 
   1139 
   1140 
   1141 /* ======================================================================== */
   1142 
   1143 
   1144 /*
   1145  * fdstart
   1146  *
   1147  * we are called from the strategy() routine to perform a data transfer.
   1148  *
   1149  * The disk(9) framework demands we run at splbio(); our caller
   1150  * takes care of that.
   1151  *
   1152  * Wish we had pascalish local functions here...
   1153  */
   1154 
   1155 /* fdstart FSM states */
   1156 enum {
   1157 	state_Init = 0,
   1158 	state_Seek,
   1159 	state_Read,
   1160 	state_Write,
   1161 	state_Flush,
   1162 	state_IOFinish,
   1163 	state_IOErr,
   1164 	state_Fault,
   1165 	state_Exit,
   1166 	state_Done
   1167 };
   1168 
   1169 static void
   1170 fdstart(fd)
   1171 	fd_softc_t *fd;
   1172 {
   1173 	int st;
   1174 
   1175 	static char *stateDesc[] = {
   1176 		"Init",
   1177 		"Seek",
   1178 		"Read",
   1179 		"Write",
   1180 		"Flush",
   1181 		"IOFinish",
   1182 		"IOErr",
   1183 		"Fault",
   1184 		"Exit",
   1185 		"Done"
   1186 	};
   1187 	int (*state[])(fd_softc_t *fd) = {
   1188 		fdstart_Init,
   1189 		fdstart_Seek,
   1190 		fdstart_Read,
   1191 		fdstart_Write,
   1192 		fdstart_Flush,
   1193 		fdstart_IOFinish,
   1194 		fdstart_IOErr,
   1195 		fdstart_Fault,
   1196 		fdstart_Exit
   1197 	};
   1198 
   1199 	st = state_Init;
   1200 	do {
   1201 		if (TRACE_STRAT)
   1202 			printf(" fdstart state %d [%s] ",
   1203 			    st, stateDesc[st]);
   1204 
   1205 		st = (*state[st])(fd);
   1206 
   1207 		if (TRACE_STRAT)
   1208 			printf(".\n");
   1209 	} while (st != state_Done);
   1210 }
   1211 
   1212 
   1213 /*
   1214  * fdstart_Init
   1215  *
   1216  * Set up things
   1217  */
   1218 static int
   1219 fdstart_Init(fd)
   1220 	fd_softc_t *fd;
   1221 {
   1222 	struct buf *bp;
   1223 
   1224 	/*
   1225 	 * Get the first entry from the queue. This is the buf we gave to
   1226 	 * fdstrategy(); disksort() put it into our softc.
   1227 	 */
   1228 	bp = BUFQ_PEEK(&fd->bufQueue);
   1229 	if (NULL == bp) {
   1230 		if (TRACE_STRAT)
   1231 			printf("Queue empty: Nothing to do");
   1232 		return state_Done;
   1233 	}
   1234 	fd->ioDirection = bp->b_flags & B_READ;
   1235 
   1236 	disk_busy(&fd->diskInfo);
   1237 	if (!(fd->state & IWM_FD_MOTOR_ON)) {
   1238 		iwmMotor(fd->unit, 1);
   1239 		fd->state |= IWM_FD_MOTOR_ON;
   1240 	}
   1241 	fd->current_buffer = bp->b_data;
   1242 
   1243 	/* XXX - assumes blocks of 512 bytes */
   1244 	fd->startBlk = bp->b_blkno;
   1245 
   1246 	fd->iwmErr = 0;
   1247 	fd->ioRetries = 0;		/* XXX */
   1248 	fd->seekRetries = 0;
   1249 	fd->bytesDone = 0;
   1250 	fd->bytesLeft = bp->b_bcount;
   1251 	return state_Seek;
   1252 }
   1253 
   1254 
   1255 /*
   1256  * fdstart_Seek
   1257  */
   1258 static int
   1259 fdstart_Seek(fd)
   1260 	fd_softc_t *fd;
   1261 {
   1262 	int state;
   1263 
   1264 	/* Calculate the side/track/sector our block is at. */
   1265 	if (TRACE_STRAT)
   1266 		printf(" Remap block %lld ", (long long) fd->startBlk);
   1267 	remap_geometry(fd->startBlk,
   1268 	    fd->currentType->heads, &fd->pos);
   1269 	if (TRACE_STRAT)
   1270 		printf("to c%d_h%d_s%d ", fd->pos.track,
   1271 		    fd->pos.side, fd->pos.sector);
   1272 
   1273 	if (fd->cachedSide != fd->pos.side) {
   1274 		if (TRACE_STRAT)
   1275 			printf(" (invalidate cache) ");
   1276 		invalidateCylinderCache(fd);
   1277 		fd->cachedSide = fd->pos.side;
   1278 	}
   1279 
   1280 	/*
   1281 	 * If necessary, seek to wanted track. Note that
   1282 	 * seek() performs any necessary retries.
   1283 	 */
   1284 	if (fd->pos.track != fd->pos.oldTrack &&
   1285 	    0 != (fd->iwmErr = seek(fd, IWM_SEEK_VANILLA))) {
   1286 		state = state_Fault;
   1287 	} else {
   1288 		state = (fd->ioDirection == IWM_WRITE)
   1289 		    ? state_Write : state_Read;
   1290 	}
   1291 	return state;
   1292 }
   1293 
   1294 
   1295 /*
   1296  * fdstart_Read
   1297  *
   1298  * Transfer a sector from disk. Get it from the track cache, if available;
   1299  * otherwise, while we are at it, store in the cache all the sectors we find
   1300  * on the way.
   1301  *
   1302  * Track buffering reads:
   1303  * o  Look if the sector is already cached.
   1304  * o  Else, read sectors into track cache until we meet the header of
   1305  *    the sector we want.
   1306  * o  Read that sector directly to fs buffer and return.
   1307  */
   1308 static int
   1309 fdstart_Read(fd)
   1310 	fd_softc_t *fd;
   1311 {
   1312 	int i;
   1313 	diskPosition_t *pos;
   1314 	sectorHdr_t *shdr;
   1315 #ifndef _LKM
   1316 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1317 #endif
   1318 
   1319 	/* Initialize retry counters */
   1320 	fd->seekRetries = 0;
   1321 	fd->sectRetries = 0;
   1322 	pos = &fd->pos;
   1323 	shdr = &fd->sHdr;
   1324 
   1325 	if (TRACE_STRAT)
   1326 		printf("<%s c%d_h%d_s%d> ",
   1327 		    fd->ioDirection ? "Read" : "Write",
   1328 		    pos->track, pos->side, pos->sector);
   1329 
   1330 	/* Sector already cached? */
   1331 	i = pos->sector;
   1332 	if (fd->r_slots[i].valid) {
   1333 		if (TRACE_STRAT)
   1334 			printf("(cached)");
   1335 		memcpy(fd->current_buffer, fd->r_slots[i].secbuf,
   1336 		    fd->currentType->sectorSize);
   1337 		return state_IOFinish;
   1338 	}
   1339 
   1340 	/* Get sector from disk */
   1341 	shdr->side = pos->side;
   1342 	shdr->sector = pos->sector;
   1343 	shdr->track = pos->track;
   1344 
   1345 	(void)iwmSelectSide(pos->side);
   1346 	fd->iwmErr = iwmReadSector(&fd->sHdr, fd->r_slots,
   1347 	    fd->current_buffer);
   1348 
   1349 	/* Check possible error conditions */
   1350 	if (TRACE_STRAT)
   1351 		printf("c%d_h%d_s%d_err(%d)_sr%d ",
   1352 		    shdr->track, shdr->side >> 3,
   1353 		    shdr->sector, fd->iwmErr, fd->sectRetries);
   1354 
   1355 	/* IWM IO error? */
   1356 	if (fd->iwmErr != 0)
   1357 		return state_IOErr;
   1358 
   1359 	/* Bad seek? Retry */
   1360 	if (shdr->track != pos->track) {
   1361 		if (TRACE_STRAT) {
   1362 			printf("Wanted track %d, got %d, %d seek retries.\n",
   1363 			    pos->track, shdr->track, fd->seekRetries);
   1364 		}
   1365 		if (iwm->maxRetries > fd->seekRetries++) {
   1366 			fd->iwmErr = seek(fd, IWM_SEEK_RECAL);
   1367 			if (TRACE_STRAT) {
   1368 				printf("[%d]", fd->seekRetries);
   1369 				(void)checkTrack(&fd->pos, 1);
   1370 			}
   1371 		} else
   1372 			fd->iwmErr = seekErr;
   1373 		return (0 == fd->iwmErr) ? state_Read : state_Fault;
   1374 	}
   1375 
   1376 	/* Sector not found? */
   1377 	if (shdr->sector != pos->sector) {
   1378 		if (TRACE_STRAT)
   1379 			printf("c%d_h%d_s%d sect not found, %d retries ",
   1380 			    shdr->track, shdr->side >> 3,
   1381 			    shdr->sector, fd->sectRetries);
   1382 		fd->iwmErr = noAdrMkErr;
   1383 		return state_Fault;
   1384 	}
   1385 
   1386 	/* Success */
   1387 	return state_IOFinish;
   1388 }
   1389 
   1390 
   1391 /*
   1392  * fdstart_Write
   1393  *
   1394  * Insert a sector into a write buffer slot and mark the slot dirty.
   1395  */
   1396 static int
   1397 fdstart_Write(fd)
   1398 	fd_softc_t *fd;
   1399 {
   1400 	int i;
   1401 
   1402 	/* XXX let's see... */
   1403 	fd->sHdr.side = fd->pos.side;
   1404 	fd->sHdr.sector = fd->pos.sector;
   1405 	fd->sHdr.track = fd->pos.track;
   1406 
   1407 	i = fd->pos.sector;
   1408 	fd->w_slots[i].secbuf = fd->current_buffer;
   1409 	fd->w_slots[i].valid = 1;	/* "valid" is a dirty buffer here */
   1410 
   1411 	if (TRACE_STRAT)
   1412 		printf("<%s c%d_h%d_s%d> (cached) ",
   1413 		    fd->ioDirection ? "Read" : "Write",
   1414 		    fd->pos.track, fd->pos.side, fd->pos.sector);
   1415 	return state_IOFinish;
   1416 }
   1417 
   1418 
   1419 
   1420 /*
   1421  * fdstart_Flush
   1422  *
   1423  * Flush dirty buffers in the track cache to disk.
   1424  */
   1425 static int
   1426 fdstart_Flush(fd)
   1427 	fd_softc_t *fd;
   1428 {
   1429 	int state;
   1430 	int i, dcnt;
   1431 	diskPosition_t *pos;
   1432 	sectorHdr_t *shdr;
   1433 #ifndef _LKM
   1434 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1435 #endif
   1436 	dcnt = 0;
   1437 	pos = &fd->pos;
   1438 	shdr = &fd->sHdr;
   1439 
   1440 	if (TRACE_STRAT) {
   1441 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++)
   1442 			if (fd->w_slots[i].valid) {
   1443 				printf("|%d", i);
   1444 				dcnt++;
   1445 			}
   1446 		printf("|\n");
   1447 
   1448 		printf(" <%s c%d_h%d_#s%d>\n",
   1449 		    fd->ioDirection ? "Read" : "Write",
   1450 		    pos->track, pos->side, dcnt);
   1451 	}
   1452 	(void)iwmSelectSide(pos->side);
   1453 	fd->iwmErr = iwmWriteSector(&fd->sHdr, fd->w_slots);
   1454 
   1455 	switch (fd->iwmErr) {
   1456 	case noErr:		/* Success */
   1457 #ifdef DIAGNOSTIC
   1458 		/* XXX Panic if buffer not clean? */
   1459 		for (i=0; i<IWM_MAX_GCR_SECTORS; i++)
   1460 			if (0 != fd->w_slots[i].valid)
   1461 				printf("Oops! <c%d_h%d_s%d> not flushed.\n",
   1462 				    fd->pos.track, fd->pos.side,
   1463 				    fd->pos.sector);
   1464 #endif
   1465 		if (TRACE_STRAT)
   1466 			printf("(Cache flushed, re-initialize) ");
   1467 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
   1468 			fd->w_slots[i].valid = 0;
   1469 			fd->w_slots[i].secbuf = NULL;
   1470 		}
   1471 		fd->seekRetries = 0;
   1472 		state = state_Exit;
   1473 		break;
   1474 
   1475 	case seekErr:		/* Bad seek? Retry */
   1476 		if (TRACE_STRAT) {
   1477 			printf("Wanted track %d, got %d, %d seek retries.\n",
   1478 			    pos->track, shdr->track, fd->seekRetries);
   1479 		}
   1480 		if (iwm->maxRetries > fd->seekRetries++) {
   1481 			fd->iwmErr = seek(fd, IWM_SEEK_RECAL);
   1482 			if (TRACE_STRAT) {
   1483 				printf("[%d]", fd->seekRetries);
   1484 			}
   1485 		}
   1486 		state = (0 == fd->iwmErr) ? state_Exit : state_Fault;
   1487 		break;
   1488 
   1489 	default:		/* General IWM IO error? */
   1490 		state = state_IOErr;
   1491 	}
   1492 	return state;
   1493 }
   1494 
   1495 
   1496 /*
   1497  * fdstart_IOFinish
   1498  *
   1499  * Prepare for next block, if any is available
   1500  */
   1501 static int
   1502 fdstart_IOFinish(fd)
   1503 	fd_softc_t *fd;
   1504 {
   1505 	int state;
   1506 
   1507 	if (DISABLED && TRACE_STRAT)
   1508 		printf("%s c%d_h%d_s%d ok ",
   1509 		    fd->ioDirection ? "Read" : "Write",
   1510 		    fd->sHdr.track, fd->sHdr.side >> 3, fd->sHdr.sector);
   1511 
   1512 	fd->bytesDone += fd->currentType->sectorSize;
   1513 	fd->bytesLeft -= fd->currentType->sectorSize;
   1514 	fd->current_buffer += fd->currentType->sectorSize;
   1515 	/*
   1516 	 * Instead of recalculating the chs mapping for
   1517 	 * each and every sector, check for
   1518 	 * 'current sector# <= max sector#' and recalculate
   1519 	 * after overflow.
   1520 	 */
   1521 	fd->startBlk++;
   1522 	if (fd->bytesLeft > 0) {
   1523 		if (++fd->pos.sector < fd->pos.maxSect) {
   1524 			if (TRACE_STRAT)
   1525 				printf("continue");
   1526 			state = (fd->ioDirection == IWM_WRITE)
   1527 			    ? state_Write : state_Read;
   1528 		}
   1529 		else {
   1530 			/*
   1531 			 * Invalidate read cache when changing track;
   1532 			 * flush write cache to disk.
   1533 			 */
   1534 			if (fd->ioDirection == IWM_WRITE) {
   1535 				if (TRACE_STRAT)
   1536 					printf("flush ");
   1537 				state = (state_Exit == fdstart_Flush(fd))
   1538 				    ? state_Seek : state_IOErr;
   1539 			}
   1540 			else {
   1541 				if (TRACE_STRAT)
   1542 					printf("step ");
   1543 				invalidateCylinderCache(fd);
   1544 				state = state_Seek;
   1545 			}
   1546 		}
   1547 	} else {
   1548 		state = (fd->ioDirection == IWM_WRITE)
   1549 		    ? state_Flush : state_Exit;
   1550 	}
   1551 	return state;
   1552 }
   1553 
   1554 
   1555 /*
   1556  * fdstart_IOErr
   1557  *
   1558  * Bad IO, repeat
   1559  */
   1560 static int
   1561 fdstart_IOErr(fd)
   1562 	fd_softc_t *fd;
   1563 {
   1564 	int state;
   1565 #ifndef _LKM
   1566 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1567 #endif
   1568 
   1569 #ifdef DIAGNOSTIC
   1570 	printf("iwm%sSector() err = %d, %d retries, on c%d_h%d_s%d.\n",
   1571 	    fd->ioDirection ? "Read" : "Write",
   1572 	    fd->iwmErr, fd->ioRetries, fd->pos.track,
   1573 	    fd->pos.side, fd->pos.sector);
   1574 #endif
   1575 	/* XXX Do statistics */
   1576 	if (fd->ioRetries++ < iwm->maxRetries)
   1577 		state = (fd->ioDirection == IWM_WRITE)
   1578 		    ? state_Flush : state_Read;
   1579 	else
   1580 		state = state_Fault;
   1581 	return state;
   1582 }
   1583 
   1584 
   1585 /*
   1586  * fdstart_Fault
   1587  *
   1588  * A non-recoverable error
   1589  */
   1590 static int
   1591 fdstart_Fault(fd)
   1592 	fd_softc_t *fd;
   1593 {
   1594 #ifdef DIAGNOSTIC
   1595 	printf("Seek retries %d, IO retries %d, sect retries %d :\n" \
   1596 	    "\t\t only found c%d_h%d_s%d \n",
   1597 	    fd->seekRetries, fd->ioRetries, fd->sectRetries,
   1598 	    fd->sHdr.track, fd->sHdr.side >> 3, fd->sHdr.sector);
   1599 	printf("A non-recoverable error: %d ", fd->iwmErr);
   1600 #else
   1601 	/* ARGSUSED */
   1602 #endif
   1603 	return state_Exit;
   1604 }
   1605 
   1606 
   1607 /*
   1608  * fdstart_Exit
   1609  *
   1610  * We are done, for good or bad
   1611  */
   1612 static int
   1613 fdstart_Exit(fd)
   1614 	fd_softc_t *fd;
   1615 {
   1616 	struct buf *bp;
   1617 #ifdef DIAGNOSTIC
   1618 	int i;
   1619 #endif
   1620 
   1621 	invalidateCylinderCache(fd);
   1622 
   1623 #ifdef DIAGNOSTIC
   1624 	/* XXX Panic if buffer not clean? */
   1625 	for (i=0; i<IWM_MAX_GCR_SECTORS; i++)
   1626 		if (0 != fd->w_slots[i].valid)
   1627 			printf("Oops! <c%d_h%d_s%d> not flushed.\n",
   1628 			    fd->pos.track, fd->pos.side, fd->pos.sector);
   1629 #endif
   1630 
   1631 	bp = BUFQ_GET(&fd->bufQueue);
   1632 
   1633 	bp->b_resid = fd->bytesLeft;
   1634 	bp->b_error = (0 == fd->iwmErr) ? 0 : EIO;
   1635 	if (fd->iwmErr)
   1636 		bp->b_flags |= B_ERROR;
   1637 
   1638 	if (TRACE_STRAT) {
   1639 		printf(" fdstart() finished job; fd->iwmErr = %d, b_error = %d",
   1640 		    fd->iwmErr, bp->b_error);
   1641 		if (DISABLED)
   1642 			hexDump(bp->b_data, bp->b_bcount);
   1643 	}
   1644 	if (DISABLED && TRACE_STRAT)
   1645 		printf(" Next buf (bufQueue first) at %p\n",
   1646 		    BUFQ_PEEK(&fd->bufQueue));
   1647 	disk_unbusy(&fd->diskInfo, bp->b_bcount - bp->b_resid,
   1648 	    (bp->b_flags & B_READ));
   1649 	biodone(bp);
   1650 	/*
   1651 	 * Stop motor after 10s
   1652 	 *
   1653 	 * XXX Unloading the module while the timeout is still
   1654 	 *     running WILL crash the machine.
   1655 	 */
   1656 	callout_reset(&fd->motor_ch, 10 * hz, motor_off, fd);
   1657 
   1658 	return state_Done;
   1659 }
   1660 
   1661 
   1662 /*
   1663  * remap_geometry
   1664  *
   1665  * Remap the rigid UN*X view of a disk's cylinder/sector geometry
   1666  * to our zone recorded real Sony drive by splitting the disk
   1667  * into zones.
   1668  *
   1669  * Loop {
   1670  * 	Look if logical block number is in current zone
   1671  *	NO:	Add # of tracks for current zone to track counter
   1672  *		Process next zone
   1673  *
   1674  *	YES:	Subtract (number of first sector of current zone times heads)
   1675  *		from logical block number, then break up the difference
   1676  *		in tracks/side/sectors (spt is constant within a zone).
   1677  *		Done
   1678  * }
   1679  */
   1680 static void
   1681 remap_geometry(block, heads, loc)
   1682 	daddr_t block;
   1683 	int heads;
   1684 	diskPosition_t *loc;
   1685 {
   1686 	int zone, spt;
   1687 	extern diskZone_t diskZones[];
   1688 
   1689 	spt = 0;		/* XXX Shut up egcs warning */
   1690 	loc->oldTrack = loc->track;
   1691 	loc->track = 0;
   1692 
   1693 	for (zone = 0; zone < IWM_GCR_DISK_ZONES; zone++) {
   1694 		if (block >= heads * (diskZones[zone].lastBlock + 1)) {
   1695 			/* Process full zones */
   1696 			loc->track += diskZones[zone].tracks;
   1697 		} else {
   1698 			/* Process partial zone */
   1699 			spt = diskZones[zone].sectPerTrack;
   1700 			block -= heads * diskZones[zone].firstBlock;
   1701 			loc->track += block / (spt * heads);
   1702 			loc->sector = (block % spt);
   1703 			loc->side = (block % (spt * heads)) / spt;
   1704 			break;
   1705 		}
   1706 	}
   1707 	loc->maxSect = spt;
   1708 }
   1709 
   1710 
   1711 /*
   1712  * motor_off
   1713  *
   1714  * Callback for timeout()
   1715  */
   1716 static void
   1717 motor_off(param)
   1718 	void *param;
   1719 {
   1720 	int spl;
   1721 	fd_softc_t *fd;
   1722 
   1723 	fd = (fd_softc_t *)param;
   1724 	if (TRACE_STRAT)
   1725 		printf("iwm: Switching motor OFF (timeout).\n");
   1726 	spl = spl6();
   1727 	(void)iwmMotor(fd->unit, 0);
   1728 	fd->state &= ~IWM_FD_MOTOR_ON;
   1729 	splx(spl);
   1730 }
   1731 
   1732 
   1733 /*
   1734  * fdGetDiskLabel
   1735  *
   1736  * Set up disk label with parameters from current disk type.
   1737  * Then call the generic disklabel read routine which tries to
   1738  * read a label from disk and insert it. If it doesn't exist use
   1739  * our defaults.
   1740  */
   1741 static void
   1742 fdGetDiskLabel(fd, dev)
   1743 	fd_softc_t *fd;
   1744 	dev_t dev;
   1745 {
   1746 	const char *msg;
   1747 	int fdType;
   1748 	struct disklabel *lp;
   1749 	struct cpu_disklabel *clp;
   1750 
   1751 	if (TRACE_IOCTL)
   1752 		printf("iwm: fdGetDiskLabel() for disk %d.\n",
   1753 		    minor(dev) / MAXPARTITIONS);
   1754 	fdType = minor(dev) % MAXPARTITIONS;
   1755 	lp = fd->diskInfo.dk_label;
   1756 	clp = fd->diskInfo.dk_cpulabel;
   1757 	memset(lp, 0, sizeof(struct disklabel));
   1758 	memset(clp, 0, sizeof(struct cpu_disklabel));
   1759 	/*
   1760 	 * How to describe a drive with a variable # of sectors per
   1761 	 * track (8..12) and variable rpm (300..550)? Apple came up
   1762 	 * with ZBR in 1983! Un*x drive management sucks.
   1763 	 */
   1764 	lp->d_type = DTYPE_FLOPPY;
   1765 	lp->d_rpm = 300;
   1766 	lp->d_secsize = fd->currentType->sectorSize;
   1767 	lp->d_ntracks = fd->currentType->heads;
   1768 	lp->d_ncylinders = fd->currentType->tracks;
   1769 	lp->d_nsectors = fd->currentType->secPerTrack;
   1770 	lp->d_secpercyl = fd->currentType->secPerCyl;
   1771 	lp->d_secperunit = fd->currentType->secPerDisk;
   1772 	lp->d_interleave = fd->currentType->interleave;
   1773 	lp->d_trkseek = fd->currentType->stepRate;
   1774 
   1775 	strcpy(lp->d_typename, dktypenames[DTYPE_FLOPPY]);
   1776 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1777 
   1778 	lp->d_npartitions = fdType + 1;
   1779 	lp->d_partitions[fdType].p_offset = 0;
   1780 	lp->d_partitions[fdType].p_size = lp->d_secperunit;
   1781 	lp->d_partitions[fdType].p_fstype = FS_BSDFFS;
   1782 	lp->d_partitions[fdType].p_fsize = 512;
   1783 	lp->d_partitions[fdType].p_frag = 8;
   1784 
   1785 	lp->d_magic = DISKMAGIC;
   1786 	lp->d_magic2 = DISKMAGIC;
   1787 	lp->d_checksum = dkcksum(lp);
   1788 	/*
   1789 	 * Call the generic disklabel extraction routine.  If we don't
   1790 	 * find a label on disk, keep our faked one.
   1791 	 */
   1792 	if (TRACE_OPEN)
   1793 		printf(" now calling readdisklabel()...\n");
   1794 
   1795 	msg = readdisklabel(dev, fdstrategy, lp, clp);
   1796 	if (msg == NULL) {
   1797 		strncpy(lp->d_packname, "default label",
   1798 		    sizeof(lp->d_packname));	/* XXX - ?? */
   1799 	}
   1800 #ifdef IWM_DEBUG
   1801 	else
   1802 		printf("iwm: %s.\n", msg);
   1803 #endif
   1804 	if (TRACE_OPEN)
   1805 		fdPrintDiskLabel(lp);
   1806 }
   1807 
   1808 
   1809 
   1810 /*
   1811  * initCylinderCache
   1812  *
   1813  * Allocate cylinder cache and set up pointers to sectors.
   1814  */
   1815 static int
   1816 initCylinderCache(fd)
   1817 	fd_softc_t *fd;
   1818 {
   1819 	int i;
   1820 	int err;
   1821 	int secsize;
   1822 
   1823 	err = 0;
   1824 	secsize = fd->currentType->sectorSize;
   1825 	fd->cachedSide = 0;
   1826 
   1827 	fd->cbuf = (unsigned char *) malloc(IWM_MAX_GCR_SECTORS
   1828 	    * secsize, M_DEVBUF, M_WAITOK);
   1829 	if (NULL == fd->cbuf)
   1830 		err = ENOMEM;
   1831 	else
   1832 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
   1833 			fd->w_slots[i].valid = 0;
   1834 			fd->w_slots[i].secbuf = NULL;
   1835 
   1836 			fd->r_slots[i].valid = 0;
   1837 			fd->r_slots[i].secbuf = fd->cbuf + i * secsize;
   1838 		}
   1839 	return err;
   1840 }
   1841 
   1842 
   1843 /*
   1844  * invalidateCylinderCache
   1845  *
   1846  * Switching cylinders (tracks?) invalidates the read cache.
   1847  */
   1848 static void
   1849 invalidateCylinderCache(fd)
   1850 	fd_softc_t *fd;
   1851 {
   1852 	int i;
   1853 
   1854 	fd->cachedSide = 0;
   1855 	for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
   1856 		fd->r_slots[i].valid = 0;
   1857 	}
   1858 }
   1859 
   1860 
   1861 /*
   1862  * getFDType
   1863  *
   1864  * return pointer to disk format description
   1865  */
   1866 static fdInfo_t *
   1867 getFDType(unit)
   1868 	short unit;
   1869 {
   1870 	int driveFlags;
   1871 	fdInfo_t *thisType;
   1872 	extern fdInfo_t fdTypes[];
   1873 
   1874 	driveFlags = iwmCheckDrive(unit);
   1875 /*
   1876  * Drive flags are: Bit  0 - 1 = Drive is double sided
   1877  *			 1 - 1 = No disk inserted
   1878  *			 2 - 1 = Motor is off
   1879  *			 3 - 1 = Disk is writable
   1880  *			 4 - 1 = Disk is DD (800/720K)
   1881  *			31 - 1 = No drive / invalid drive #
   1882  */
   1883 	if (TRACE_CONFIG) {
   1884 		printf("iwm: Drive %d says 0x0%x (%d)\n",
   1885 		    unit, driveFlags, driveFlags);
   1886 	}
   1887 	if (driveFlags < 0)
   1888 		thisType = NULL;/* no such drive	 */
   1889 	else
   1890 		if (driveFlags & 0x01)
   1891 			thisType = &fdTypes[1];	/* double sided		 */
   1892 		else
   1893 			thisType = &fdTypes[0];	/* single sided		 */
   1894 
   1895 	return thisType;
   1896 }
   1897 
   1898 
   1899 /*
   1900  * fdDeviceToType
   1901  *
   1902  * maps the minor device number (elsewhere: partition type) to
   1903  * a corresponding disk format.
   1904  * This is currently:
   1905  * 	fdXa	default (800K GCR)
   1906  *	fdXb	400K GCR
   1907  *	fdXc	800K GCR
   1908  */
   1909 static fdInfo_t *
   1910 fdDeviceToType(fd, dev)
   1911 	fd_softc_t *fd;
   1912 	dev_t dev;
   1913 {
   1914 	int type;
   1915 	fdInfo_t *thisInfo;
   1916 	/* XXX This broke with egcs 1.0.2 */
   1917 	/* extern fdInfo_t	fdTypes[]; */
   1918 
   1919 	type = minor(dev) % MAXPARTITIONS;	/* 1,2,... */
   1920 	if (type > sizeof(fdTypes) / sizeof(fdTypes[0]))
   1921 		thisInfo = NULL;
   1922 	else
   1923 		thisInfo = (type == 0) ? fd->defaultType : &fdTypes[type - 1];
   1924 	return thisInfo;
   1925 }
   1926 
   1927 
   1928 /*
   1929  * seek
   1930  *
   1931  * Step to given track; optionally restore to track zero before
   1932  * and/or verify correct track.
   1933  * Note that any necessary retries are done here.
   1934  * We keep the current position on disk in a 'struct diskPosition'.
   1935  */
   1936 static int
   1937 seek(fd, style)
   1938 	fd_softc_t *fd;
   1939 	int style;
   1940 {
   1941 	int state, done;
   1942 	int err, ierr;
   1943 	int steps;
   1944 
   1945 	diskPosition_t *loc;
   1946 	sectorHdr_t hdr;
   1947 	char action[32];
   1948 #ifndef _LKM
   1949 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1950 #endif
   1951 
   1952 	char *stateDesc[] = {
   1953 		"Init",
   1954 		"Seek",
   1955 		"Recalibrate",
   1956 		"Verify",
   1957 		"Exit"
   1958 	};
   1959 	enum {
   1960 		state_Init = 0,
   1961 		state_Seek,
   1962 		state_Recalibrate,
   1963 		state_Verify,
   1964 		state_Exit
   1965 	};
   1966 	/* XXX egcs */
   1967 	done = err = ierr = 0;
   1968 	fd->seekRetries = 0;
   1969 	fd->verifyRetries = 0;
   1970 
   1971 	loc = &fd->pos;
   1972 
   1973 	state = state_Init;
   1974 	do {
   1975 		if (TRACE_STEP)
   1976 			printf(" seek state %d [%s].\n",
   1977 			    state, stateDesc[state]);
   1978 		switch (state) {
   1979 
   1980 		case state_Init:
   1981 			if (TRACE_STEP)
   1982 				printf("Current track is %d, new track %d.\n",
   1983 				    loc->oldTrack, loc->track);
   1984 			memset(&hdr, 0, sizeof(hdr));
   1985 			err = ierr = 0;
   1986 			fd->seekRetries = 0;
   1987 			fd->verifyRetries = 0;
   1988 			state = (style == IWM_SEEK_RECAL)
   1989 			    ? state_Recalibrate : state_Seek;
   1990 			done = 0;
   1991 			break;
   1992 
   1993 		case state_Recalibrate:
   1994 			ierr = iwmTrack00();
   1995 			if (ierr == 0) {
   1996 				loc->oldTrack = 0;
   1997 				state = state_Seek;
   1998 			} else {
   1999 				strncpy(action, "Recalibrate (track 0)",
   2000 				    sizeof(action));
   2001 				state = state_Exit;
   2002 			}
   2003 			break;
   2004 
   2005 		case state_Seek:
   2006 			ierr = 0;
   2007 			steps = loc->track - loc->oldTrack;
   2008 
   2009 			if (steps != 0)
   2010 				ierr = iwmSeek(steps);
   2011 			if (ierr == 0) {
   2012 				/* No error or nothing to do */
   2013 				state = (style == IWM_SEEK_VERIFY)
   2014 				    ? state_Verify : state_Exit;
   2015 			} else {
   2016 				if (fd->seekRetries++ < iwm->maxRetries)
   2017 					state = state_Recalibrate;
   2018 				else {
   2019 					strncpy(action, "Seek retries",
   2020 					    sizeof(action));
   2021 					state = state_Exit;
   2022 				}
   2023 			}
   2024 			break;
   2025 
   2026 		case state_Verify:
   2027 			ierr = checkTrack(loc, TRACE_STEP);
   2028 			if (ierr == 0 && loc->track == hdr.track)
   2029 				state = state_Exit;
   2030 			else {
   2031 				if (fd->verifyRetries++ < iwm->maxRetries)
   2032 					state = state_Recalibrate;
   2033 				else {
   2034 					strncpy(action, "Verify retries",
   2035 					    sizeof(action));
   2036 					state = state_Exit;
   2037 				}
   2038 			}
   2039 			break;
   2040 
   2041 		case state_Exit:
   2042 			if (ierr == 0) {
   2043 				loc->oldTrack = loc->track;
   2044 				err = 0;
   2045 				/* Give the head some time to settle down */
   2046 				delay(3000);
   2047 			} else {
   2048 #ifdef DIAGNOSTIC
   2049 				printf(" seek() action \"%s\", err = %d.\n",
   2050 				    action, ierr);
   2051 #endif
   2052 				err = EIO;
   2053 			}
   2054 			done = 1;
   2055 			break;
   2056 		}
   2057 	} while (!done);
   2058 	return err;
   2059 }
   2060 
   2061 
   2062 /*
   2063  * checkTrack
   2064  *
   2065  * After positioning, get a sector header for validation
   2066  */
   2067 static int
   2068 checkTrack(loc, debugFlag)
   2069 	diskPosition_t *loc;
   2070 	int debugFlag;
   2071 {
   2072 	int spl;
   2073 	int iwmErr;
   2074 	sectorHdr_t hdr;
   2075 
   2076 	spl = spl6();
   2077 	iwmSelectSide(loc->side);
   2078 	iwmErr = iwmReadSectHdr(&hdr);
   2079 	splx(spl);
   2080 	if (debugFlag) {
   2081 		printf("Seeked for %d, got at %d, Hdr read err %d.\n",
   2082 		    loc->track, hdr.track, iwmErr);
   2083 	}
   2084 	return iwmErr;
   2085 }
   2086 
   2087 
   2088 /* Debugging stuff */
   2089 
   2090 static void
   2091 hexDump(buf, len)
   2092 	u_char *buf;
   2093 	int len;
   2094 {
   2095 	int i, j;
   2096 	u_char ch;
   2097 
   2098 	printf("\nDump %d from %p:\n", len, buf);
   2099 	i = j = 0;
   2100 	if (NULL != buf) do {
   2101 		printf("%04x: ", i);
   2102 		for (j = 0; j < 8; j++)
   2103 			printf("%02x ", buf[i + j]);
   2104 		printf(" ");
   2105 		for (j = 8; j < 16; j++)
   2106 			printf("%02x ", buf[i + j]);
   2107 		printf(" ");
   2108 		for (j = 0; j < 16; j++) {
   2109 			ch = buf[i + j];
   2110 			if (ch > 31 && ch < 127)
   2111 				printf("%c", ch);
   2112 			else
   2113 				printf(".");
   2114 		}
   2115 		printf("\n");
   2116 		i += 16;
   2117 	} while (len > i);
   2118 }
   2119 
   2120 
   2121 static void
   2122 fdPrintDiskLabel(lp)
   2123 	struct disklabel *lp;
   2124 {
   2125 	int i;
   2126 
   2127 	printf("iwm: Disklabel entries of current floppy.\n");
   2128 	printf("\t d_type:\t%d (%s)\n", lp->d_type,
   2129 	    dktypenames[lp->d_type]);
   2130 	printf("\t d_typename:\t%s\n", lp->d_typename);
   2131 	printf("\t d_packname:\t%s\n", lp->d_packname);
   2132 
   2133 	printf("\t d_secsize:\t%d\n", lp->d_secsize);
   2134 	printf("\t d_nsectors:\t%d\n", lp->d_nsectors);
   2135 	printf("\t d_ntracks:\t%d\n", lp->d_ntracks);
   2136 	printf("\t d_ncylinders:\t%d\n", lp->d_ncylinders);
   2137 	printf("\t d_secpercyl:\t%d\n", lp->d_secpercyl);
   2138 	printf("\t d_secperunit:\t%d\n", lp->d_secperunit);
   2139 
   2140 	printf("\t d_rpm: \t%d\n", lp->d_rpm);
   2141 	printf("\t d_interleave:\t%d\n", lp->d_interleave);
   2142 	printf("\t d_trkseek:\t%d [ms]\n", lp->d_trkseek);
   2143 
   2144 	printf(" d_npartitions:\t%d\n", lp->d_npartitions);
   2145 	for (i = 0; i < lp->d_npartitions; i++) {
   2146 		printf("\t d_partitions[%d].p_offset:\t%d\n", i,
   2147 		    lp->d_partitions[i].p_offset);
   2148 		printf("\t d_partitions[%d].p_size:\t%d\n", i,
   2149 		    lp->d_partitions[i].p_size);
   2150 		printf("\t d_partitions[%d].p_fstype:\t%d (%s)\n", i,
   2151 		    lp->d_partitions[i].p_fstype,
   2152 		    fstypenames[lp->d_partitions[i].p_fstype]);
   2153 		printf("\t d_partitions[%d].p_frag:\t%d\n", i,
   2154 		    lp->d_partitions[i].p_frag);
   2155 		printf("\n");
   2156 	}
   2157 }
   2158