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