Home | History | Annotate | Line # | Download | only in obio
iwm_fd.c revision 1.35
      1 /*	$NetBSD: iwm_fd.c,v 1.35 2005/12/11 12:18:03 christos 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.35 2005/12/11 12:18:03 christos 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);
    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, caddr_t 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_flags |= B_ERROR;
   1096 		}
   1097 		bp->b_resid = bp->b_bcount;
   1098 		biodone(bp);
   1099 	}
   1100 	/* Comment on results */
   1101 	if (TRACE_STRAT) {
   1102 		printf("iwm: fdstrategy() done.\n");
   1103 		printf("     We have b_resid = %d bytes left, " \
   1104 		    "b_error is %d;\n", bp->b_resid, bp->b_error);
   1105 		printf("     b_flags are 0x0%x.\n", bp->b_flags);
   1106 	}
   1107 }
   1108 
   1109 
   1110 
   1111 /* ======================================================================== */
   1112 
   1113 
   1114 /*
   1115  * fdstart
   1116  *
   1117  * we are called from the strategy() routine to perform a data transfer.
   1118  *
   1119  * The disk(9) framework demands we run at splbio(); our caller
   1120  * takes care of that.
   1121  *
   1122  * Wish we had pascalish local functions here...
   1123  */
   1124 
   1125 /* fdstart FSM states */
   1126 enum {
   1127 	state_Init = 0,
   1128 	state_Seek,
   1129 	state_Read,
   1130 	state_Write,
   1131 	state_Flush,
   1132 	state_IOFinish,
   1133 	state_IOErr,
   1134 	state_Fault,
   1135 	state_Exit,
   1136 	state_Done
   1137 };
   1138 
   1139 static void
   1140 fdstart(fd_softc_t *fd)
   1141 {
   1142 	int st;
   1143 
   1144 	static const char *stateDesc[] = {
   1145 		"Init",
   1146 		"Seek",
   1147 		"Read",
   1148 		"Write",
   1149 		"Flush",
   1150 		"IOFinish",
   1151 		"IOErr",
   1152 		"Fault",
   1153 		"Exit",
   1154 		"Done"
   1155 	};
   1156 	int (*state[])(fd_softc_t *) = {
   1157 		fdstart_Init,
   1158 		fdstart_Seek,
   1159 		fdstart_Read,
   1160 		fdstart_Write,
   1161 		fdstart_Flush,
   1162 		fdstart_IOFinish,
   1163 		fdstart_IOErr,
   1164 		fdstart_Fault,
   1165 		fdstart_Exit
   1166 	};
   1167 
   1168 	st = state_Init;
   1169 	do {
   1170 		if (TRACE_STRAT)
   1171 			printf(" fdstart state %d [%s] ",
   1172 			    st, stateDesc[st]);
   1173 
   1174 		st = (*state[st])(fd);
   1175 
   1176 		if (TRACE_STRAT)
   1177 			printf(".\n");
   1178 	} while (st != state_Done);
   1179 }
   1180 
   1181 
   1182 /*
   1183  * fdstart_Init
   1184  *
   1185  * Set up things
   1186  */
   1187 static int
   1188 fdstart_Init(fd_softc_t *fd)
   1189 {
   1190 	struct buf *bp;
   1191 
   1192 	/*
   1193 	 * Get the first entry from the queue. This is the buf we gave to
   1194 	 * fdstrategy(); disksort() put it into our softc.
   1195 	 */
   1196 	bp = BUFQ_PEEK(fd->bufQueue);
   1197 	if (NULL == bp) {
   1198 		if (TRACE_STRAT)
   1199 			printf("Queue empty: Nothing to do");
   1200 		return state_Done;
   1201 	}
   1202 	fd->ioDirection = bp->b_flags & B_READ;
   1203 
   1204 	disk_busy(&fd->diskInfo);
   1205 	if (!(fd->state & IWM_FD_MOTOR_ON)) {
   1206 		iwmMotor(fd->unit, 1);
   1207 		fd->state |= IWM_FD_MOTOR_ON;
   1208 	}
   1209 	fd->current_buffer = bp->b_data;
   1210 
   1211 	/* XXX - assumes blocks of 512 bytes */
   1212 	fd->startBlk = bp->b_blkno;
   1213 
   1214 	fd->iwmErr = 0;
   1215 	fd->ioRetries = 0;		/* XXX */
   1216 	fd->seekRetries = 0;
   1217 	fd->bytesDone = 0;
   1218 	fd->bytesLeft = bp->b_bcount;
   1219 	return state_Seek;
   1220 }
   1221 
   1222 
   1223 /*
   1224  * fdstart_Seek
   1225  */
   1226 static int
   1227 fdstart_Seek(fd_softc_t *fd)
   1228 {
   1229 	int state;
   1230 
   1231 	/* Calculate the side/track/sector our block is at. */
   1232 	if (TRACE_STRAT)
   1233 		printf(" Remap block %lld ", (long long) fd->startBlk);
   1234 	remap_geometry(fd->startBlk,
   1235 	    fd->currentType->heads, &fd->pos);
   1236 	if (TRACE_STRAT)
   1237 		printf("to c%d_h%d_s%d ", fd->pos.track,
   1238 		    fd->pos.side, fd->pos.sector);
   1239 
   1240 	if (fd->cachedSide != fd->pos.side) {
   1241 		if (TRACE_STRAT)
   1242 			printf(" (invalidate cache) ");
   1243 		invalidateCylinderCache(fd);
   1244 		fd->cachedSide = fd->pos.side;
   1245 	}
   1246 
   1247 	/*
   1248 	 * If necessary, seek to wanted track. Note that
   1249 	 * seek() performs any necessary retries.
   1250 	 */
   1251 	if (fd->pos.track != fd->pos.oldTrack &&
   1252 	    0 != (fd->iwmErr = seek(fd, IWM_SEEK_VANILLA))) {
   1253 		state = state_Fault;
   1254 	} else {
   1255 		state = (fd->ioDirection == IWM_WRITE)
   1256 		    ? state_Write : state_Read;
   1257 	}
   1258 	return state;
   1259 }
   1260 
   1261 
   1262 /*
   1263  * fdstart_Read
   1264  *
   1265  * Transfer a sector from disk. Get it from the track cache, if available;
   1266  * otherwise, while we are at it, store in the cache all the sectors we find
   1267  * on the way.
   1268  *
   1269  * Track buffering reads:
   1270  * o  Look if the sector is already cached.
   1271  * o  Else, read sectors into track cache until we meet the header of
   1272  *    the sector we want.
   1273  * o  Read that sector directly to fs buffer and return.
   1274  */
   1275 static int
   1276 fdstart_Read(fd_softc_t *fd)
   1277 {
   1278 	int i;
   1279 	diskPosition_t *pos;
   1280 	sectorHdr_t *shdr;
   1281 #ifndef _LKM
   1282 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1283 #endif
   1284 
   1285 	/* Initialize retry counters */
   1286 	fd->seekRetries = 0;
   1287 	fd->sectRetries = 0;
   1288 	pos = &fd->pos;
   1289 	shdr = &fd->sHdr;
   1290 
   1291 	if (TRACE_STRAT)
   1292 		printf("<%s c%d_h%d_s%d> ",
   1293 		    fd->ioDirection ? "Read" : "Write",
   1294 		    pos->track, pos->side, pos->sector);
   1295 
   1296 	/* Sector already cached? */
   1297 	i = pos->sector;
   1298 	if (fd->r_slots[i].valid) {
   1299 		if (TRACE_STRAT)
   1300 			printf("(cached)");
   1301 		memcpy(fd->current_buffer, fd->r_slots[i].secbuf,
   1302 		    fd->currentType->sectorSize);
   1303 		return state_IOFinish;
   1304 	}
   1305 
   1306 	/* Get sector from disk */
   1307 	shdr->side = pos->side;
   1308 	shdr->sector = pos->sector;
   1309 	shdr->track = pos->track;
   1310 
   1311 	(void)iwmSelectSide(pos->side);
   1312 	fd->iwmErr = iwmReadSector(&fd->sHdr, fd->r_slots,
   1313 	    fd->current_buffer);
   1314 
   1315 	/* Check possible error conditions */
   1316 	if (TRACE_STRAT)
   1317 		printf("c%d_h%d_s%d_err(%d)_sr%d ",
   1318 		    shdr->track, shdr->side >> 3,
   1319 		    shdr->sector, fd->iwmErr, fd->sectRetries);
   1320 
   1321 	/* IWM IO error? */
   1322 	if (fd->iwmErr != 0)
   1323 		return state_IOErr;
   1324 
   1325 	/* Bad seek? Retry */
   1326 	if (shdr->track != pos->track) {
   1327 		if (TRACE_STRAT) {
   1328 			printf("Wanted track %d, got %d, %d seek retries.\n",
   1329 			    pos->track, shdr->track, fd->seekRetries);
   1330 		}
   1331 		if (iwm->maxRetries > fd->seekRetries++) {
   1332 			fd->iwmErr = seek(fd, IWM_SEEK_RECAL);
   1333 			if (TRACE_STRAT) {
   1334 				printf("[%d]", fd->seekRetries);
   1335 				(void)checkTrack(&fd->pos, 1);
   1336 			}
   1337 		} else
   1338 			fd->iwmErr = seekErr;
   1339 		return (0 == fd->iwmErr) ? state_Read : state_Fault;
   1340 	}
   1341 
   1342 	/* Sector not found? */
   1343 	if (shdr->sector != pos->sector) {
   1344 		if (TRACE_STRAT)
   1345 			printf("c%d_h%d_s%d sect not found, %d retries ",
   1346 			    shdr->track, shdr->side >> 3,
   1347 			    shdr->sector, fd->sectRetries);
   1348 		fd->iwmErr = noAdrMkErr;
   1349 		return state_Fault;
   1350 	}
   1351 
   1352 	/* Success */
   1353 	return state_IOFinish;
   1354 }
   1355 
   1356 
   1357 /*
   1358  * fdstart_Write
   1359  *
   1360  * Insert a sector into a write buffer slot and mark the slot dirty.
   1361  */
   1362 static int
   1363 fdstart_Write(fd_softc_t *fd)
   1364 {
   1365 	int i;
   1366 
   1367 	/* XXX let's see... */
   1368 	fd->sHdr.side = fd->pos.side;
   1369 	fd->sHdr.sector = fd->pos.sector;
   1370 	fd->sHdr.track = fd->pos.track;
   1371 
   1372 	i = fd->pos.sector;
   1373 	fd->w_slots[i].secbuf = fd->current_buffer;
   1374 	fd->w_slots[i].valid = 1;	/* "valid" is a dirty buffer here */
   1375 
   1376 	if (TRACE_STRAT)
   1377 		printf("<%s c%d_h%d_s%d> (cached) ",
   1378 		    fd->ioDirection ? "Read" : "Write",
   1379 		    fd->pos.track, fd->pos.side, fd->pos.sector);
   1380 	return state_IOFinish;
   1381 }
   1382 
   1383 
   1384 
   1385 /*
   1386  * fdstart_Flush
   1387  *
   1388  * Flush dirty buffers in the track cache to disk.
   1389  */
   1390 static int
   1391 fdstart_Flush(fd_softc_t *fd)
   1392 {
   1393 	int state;
   1394 	int i, dcnt;
   1395 	diskPosition_t *pos;
   1396 	sectorHdr_t *shdr;
   1397 #ifndef _LKM
   1398 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1399 #endif
   1400 	dcnt = 0;
   1401 	pos = &fd->pos;
   1402 	shdr = &fd->sHdr;
   1403 
   1404 	if (TRACE_STRAT) {
   1405 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++)
   1406 			if (fd->w_slots[i].valid) {
   1407 				printf("|%d", i);
   1408 				dcnt++;
   1409 			}
   1410 		printf("|\n");
   1411 
   1412 		printf(" <%s c%d_h%d_#s%d>\n",
   1413 		    fd->ioDirection ? "Read" : "Write",
   1414 		    pos->track, pos->side, dcnt);
   1415 	}
   1416 	(void)iwmSelectSide(pos->side);
   1417 	fd->iwmErr = iwmWriteSector(&fd->sHdr, fd->w_slots);
   1418 
   1419 	switch (fd->iwmErr) {
   1420 	case noErr:		/* Success */
   1421 #ifdef DIAGNOSTIC
   1422 		/* XXX Panic if buffer not clean? */
   1423 		for (i=0; i<IWM_MAX_GCR_SECTORS; i++)
   1424 			if (0 != fd->w_slots[i].valid)
   1425 				printf("Oops! <c%d_h%d_s%d> not flushed.\n",
   1426 				    fd->pos.track, fd->pos.side,
   1427 				    fd->pos.sector);
   1428 #endif
   1429 		if (TRACE_STRAT)
   1430 			printf("(Cache flushed, re-initialize) ");
   1431 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
   1432 			fd->w_slots[i].valid = 0;
   1433 			fd->w_slots[i].secbuf = NULL;
   1434 		}
   1435 		fd->seekRetries = 0;
   1436 		state = state_Exit;
   1437 		break;
   1438 
   1439 	case seekErr:		/* Bad seek? Retry */
   1440 		if (TRACE_STRAT) {
   1441 			printf("Wanted track %d, got %d, %d seek retries.\n",
   1442 			    pos->track, shdr->track, fd->seekRetries);
   1443 		}
   1444 		if (iwm->maxRetries > fd->seekRetries++) {
   1445 			fd->iwmErr = seek(fd, IWM_SEEK_RECAL);
   1446 			if (TRACE_STRAT) {
   1447 				printf("[%d]", fd->seekRetries);
   1448 			}
   1449 		}
   1450 		state = (0 == fd->iwmErr) ? state_Exit : state_Fault;
   1451 		break;
   1452 
   1453 	default:		/* General IWM IO error? */
   1454 		state = state_IOErr;
   1455 	}
   1456 	return state;
   1457 }
   1458 
   1459 
   1460 /*
   1461  * fdstart_IOFinish
   1462  *
   1463  * Prepare for next block, if any is available
   1464  */
   1465 static int
   1466 fdstart_IOFinish(fd_softc_t *fd)
   1467 {
   1468 	int state;
   1469 
   1470 	if (DISABLED && TRACE_STRAT)
   1471 		printf("%s c%d_h%d_s%d ok ",
   1472 		    fd->ioDirection ? "Read" : "Write",
   1473 		    fd->sHdr.track, fd->sHdr.side >> 3, fd->sHdr.sector);
   1474 
   1475 	fd->bytesDone += fd->currentType->sectorSize;
   1476 	fd->bytesLeft -= fd->currentType->sectorSize;
   1477 	fd->current_buffer += fd->currentType->sectorSize;
   1478 	/*
   1479 	 * Instead of recalculating the chs mapping for
   1480 	 * each and every sector, check for
   1481 	 * 'current sector# <= max sector#' and recalculate
   1482 	 * after overflow.
   1483 	 */
   1484 	fd->startBlk++;
   1485 	if (fd->bytesLeft > 0) {
   1486 		if (++fd->pos.sector < fd->pos.maxSect) {
   1487 			if (TRACE_STRAT)
   1488 				printf("continue");
   1489 			state = (fd->ioDirection == IWM_WRITE)
   1490 			    ? state_Write : state_Read;
   1491 		}
   1492 		else {
   1493 			/*
   1494 			 * Invalidate read cache when changing track;
   1495 			 * flush write cache to disk.
   1496 			 */
   1497 			if (fd->ioDirection == IWM_WRITE) {
   1498 				if (TRACE_STRAT)
   1499 					printf("flush ");
   1500 				state = (state_Exit == fdstart_Flush(fd))
   1501 				    ? state_Seek : state_IOErr;
   1502 			}
   1503 			else {
   1504 				if (TRACE_STRAT)
   1505 					printf("step ");
   1506 				invalidateCylinderCache(fd);
   1507 				state = state_Seek;
   1508 			}
   1509 		}
   1510 	} else {
   1511 		state = (fd->ioDirection == IWM_WRITE)
   1512 		    ? state_Flush : state_Exit;
   1513 	}
   1514 	return state;
   1515 }
   1516 
   1517 
   1518 /*
   1519  * fdstart_IOErr
   1520  *
   1521  * Bad IO, repeat
   1522  */
   1523 static int
   1524 fdstart_IOErr(fd_softc_t *fd)
   1525 {
   1526 	int state;
   1527 #ifndef _LKM
   1528 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1529 #endif
   1530 
   1531 #ifdef DIAGNOSTIC
   1532 	printf("iwm%sSector() err = %d, %d retries, on c%d_h%d_s%d.\n",
   1533 	    fd->ioDirection ? "Read" : "Write",
   1534 	    fd->iwmErr, fd->ioRetries, fd->pos.track,
   1535 	    fd->pos.side, fd->pos.sector);
   1536 #endif
   1537 	/* XXX Do statistics */
   1538 	if (fd->ioRetries++ < iwm->maxRetries)
   1539 		state = (fd->ioDirection == IWM_WRITE)
   1540 		    ? state_Flush : state_Read;
   1541 	else
   1542 		state = state_Fault;
   1543 	return state;
   1544 }
   1545 
   1546 
   1547 /*
   1548  * fdstart_Fault
   1549  *
   1550  * A non-recoverable error
   1551  */
   1552 static int
   1553 fdstart_Fault(fd_softc_t *fd)
   1554 {
   1555 #ifdef DIAGNOSTIC
   1556 	printf("Seek retries %d, IO retries %d, sect retries %d :\n" \
   1557 	    "\t\t only found c%d_h%d_s%d \n",
   1558 	    fd->seekRetries, fd->ioRetries, fd->sectRetries,
   1559 	    fd->sHdr.track, fd->sHdr.side >> 3, fd->sHdr.sector);
   1560 	printf("A non-recoverable error: %d ", fd->iwmErr);
   1561 #else
   1562 	/* ARGSUSED */
   1563 #endif
   1564 	return state_Exit;
   1565 }
   1566 
   1567 
   1568 /*
   1569  * fdstart_Exit
   1570  *
   1571  * We are done, for good or bad
   1572  */
   1573 static int
   1574 fdstart_Exit(fd_softc_t *fd)
   1575 {
   1576 	struct buf *bp;
   1577 #ifdef DIAGNOSTIC
   1578 	int i;
   1579 #endif
   1580 
   1581 	invalidateCylinderCache(fd);
   1582 
   1583 #ifdef DIAGNOSTIC
   1584 	/* XXX Panic if buffer not clean? */
   1585 	for (i=0; i<IWM_MAX_GCR_SECTORS; i++)
   1586 		if (0 != fd->w_slots[i].valid)
   1587 			printf("Oops! <c%d_h%d_s%d> not flushed.\n",
   1588 			    fd->pos.track, fd->pos.side, fd->pos.sector);
   1589 #endif
   1590 
   1591 	bp = BUFQ_GET(fd->bufQueue);
   1592 
   1593 	bp->b_resid = fd->bytesLeft;
   1594 	bp->b_error = (0 == fd->iwmErr) ? 0 : EIO;
   1595 	if (fd->iwmErr)
   1596 		bp->b_flags |= B_ERROR;
   1597 
   1598 	if (TRACE_STRAT) {
   1599 		printf(" fdstart() finished job; fd->iwmErr = %d, b_error = %d",
   1600 		    fd->iwmErr, bp->b_error);
   1601 		if (DISABLED)
   1602 			hexDump(bp->b_data, bp->b_bcount);
   1603 	}
   1604 	if (DISABLED && TRACE_STRAT)
   1605 		printf(" Next buf (bufQueue first) at %p\n",
   1606 		    BUFQ_PEEK(fd->bufQueue));
   1607 	disk_unbusy(&fd->diskInfo, bp->b_bcount - bp->b_resid,
   1608 	    (bp->b_flags & B_READ));
   1609 	biodone(bp);
   1610 	/*
   1611 	 * Stop motor after 10s
   1612 	 *
   1613 	 * XXX Unloading the module while the timeout is still
   1614 	 *     running WILL crash the machine.
   1615 	 */
   1616 	callout_reset(&fd->motor_ch, 10 * hz, motor_off, fd);
   1617 
   1618 	return state_Done;
   1619 }
   1620 
   1621 
   1622 /*
   1623  * remap_geometry
   1624  *
   1625  * Remap the rigid UN*X view of a disk's cylinder/sector geometry
   1626  * to our zone recorded real Sony drive by splitting the disk
   1627  * into zones.
   1628  *
   1629  * Loop {
   1630  * 	Look if logical block number is in current zone
   1631  *	NO:	Add # of tracks for current zone to track counter
   1632  *		Process next zone
   1633  *
   1634  *	YES:	Subtract (number of first sector of current zone times heads)
   1635  *		from logical block number, then break up the difference
   1636  *		in tracks/side/sectors (spt is constant within a zone).
   1637  *		Done
   1638  * }
   1639  */
   1640 static void
   1641 remap_geometry(daddr_t block, int heads, diskPosition_t *loc)
   1642 {
   1643 	int zone, spt;
   1644 	extern diskZone_t diskZones[];
   1645 
   1646 	spt = 0;		/* XXX Shut up egcs warning */
   1647 	loc->oldTrack = loc->track;
   1648 	loc->track = 0;
   1649 
   1650 	for (zone = 0; zone < IWM_GCR_DISK_ZONES; zone++) {
   1651 		if (block >= heads * (diskZones[zone].lastBlock + 1)) {
   1652 			/* Process full zones */
   1653 			loc->track += diskZones[zone].tracks;
   1654 		} else {
   1655 			/* Process partial zone */
   1656 			spt = diskZones[zone].sectPerTrack;
   1657 			block -= heads * diskZones[zone].firstBlock;
   1658 			loc->track += block / (spt * heads);
   1659 			loc->sector = (block % spt);
   1660 			loc->side = (block % (spt * heads)) / spt;
   1661 			break;
   1662 		}
   1663 	}
   1664 	loc->maxSect = spt;
   1665 }
   1666 
   1667 
   1668 /*
   1669  * motor_off
   1670  *
   1671  * Callback for timeout()
   1672  */
   1673 static void
   1674 motor_off(void *param)
   1675 {
   1676 	int spl;
   1677 	fd_softc_t *fd;
   1678 
   1679 	fd = (fd_softc_t *)param;
   1680 	if (TRACE_STRAT)
   1681 		printf("iwm: Switching motor OFF (timeout).\n");
   1682 	spl = spl6();
   1683 	(void)iwmMotor(fd->unit, 0);
   1684 	fd->state &= ~IWM_FD_MOTOR_ON;
   1685 	splx(spl);
   1686 }
   1687 
   1688 
   1689 /*
   1690  * fdGetDiskLabel
   1691  *
   1692  * Set up disk label with parameters from current disk type.
   1693  * Then call the generic disklabel read routine which tries to
   1694  * read a label from disk and insert it. If it doesn't exist use
   1695  * our defaults.
   1696  */
   1697 static void
   1698 fdGetDiskLabel(fd_softc_t *fd, dev_t dev)
   1699 {
   1700 	const char *msg;
   1701 	int fdType;
   1702 	struct disklabel *lp;
   1703 	struct cpu_disklabel *clp;
   1704 
   1705 	if (TRACE_IOCTL)
   1706 		printf("iwm: fdGetDiskLabel() for disk %d.\n",
   1707 		    minor(dev) / MAXPARTITIONS);
   1708 	fdType = minor(dev) % MAXPARTITIONS;
   1709 	lp = fd->diskInfo.dk_label;
   1710 	clp = fd->diskInfo.dk_cpulabel;
   1711 	memset(lp, 0, sizeof(struct disklabel));
   1712 	memset(clp, 0, sizeof(struct cpu_disklabel));
   1713 	/*
   1714 	 * How to describe a drive with a variable # of sectors per
   1715 	 * track (8..12) and variable rpm (300..550)? Apple came up
   1716 	 * with ZBR in 1983! Un*x drive management sucks.
   1717 	 */
   1718 	lp->d_type = DTYPE_FLOPPY;
   1719 	lp->d_rpm = 300;
   1720 	lp->d_secsize = fd->currentType->sectorSize;
   1721 	lp->d_ntracks = fd->currentType->heads;
   1722 	lp->d_ncylinders = fd->currentType->tracks;
   1723 	lp->d_nsectors = fd->currentType->secPerTrack;
   1724 	lp->d_secpercyl = fd->currentType->secPerCyl;
   1725 	lp->d_secperunit = fd->currentType->secPerDisk;
   1726 	lp->d_interleave = fd->currentType->interleave;
   1727 	lp->d_trkseek = fd->currentType->stepRate;
   1728 
   1729 	strcpy(lp->d_typename, dktypenames[DTYPE_FLOPPY]);
   1730 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1731 
   1732 	lp->d_npartitions = fdType + 1;
   1733 	lp->d_partitions[fdType].p_offset = 0;
   1734 	lp->d_partitions[fdType].p_size = lp->d_secperunit;
   1735 	lp->d_partitions[fdType].p_fstype = FS_BSDFFS;
   1736 	lp->d_partitions[fdType].p_fsize = 512;
   1737 	lp->d_partitions[fdType].p_frag = 8;
   1738 
   1739 	lp->d_magic = DISKMAGIC;
   1740 	lp->d_magic2 = DISKMAGIC;
   1741 	lp->d_checksum = dkcksum(lp);
   1742 	/*
   1743 	 * Call the generic disklabel extraction routine.  If we don't
   1744 	 * find a label on disk, keep our faked one.
   1745 	 */
   1746 	if (TRACE_OPEN)
   1747 		printf(" now calling readdisklabel()...\n");
   1748 
   1749 	msg = readdisklabel(dev, fdstrategy, lp, clp);
   1750 	if (msg == NULL) {
   1751 		strncpy(lp->d_packname, "default label",
   1752 		    sizeof(lp->d_packname));	/* XXX - ?? */
   1753 	}
   1754 #ifdef IWM_DEBUG
   1755 	else
   1756 		printf("iwm: %s.\n", msg);
   1757 #endif
   1758 	if (TRACE_OPEN)
   1759 		fdPrintDiskLabel(lp);
   1760 }
   1761 
   1762 
   1763 
   1764 /*
   1765  * initCylinderCache
   1766  *
   1767  * Allocate cylinder cache and set up pointers to sectors.
   1768  */
   1769 static int
   1770 initCylinderCache(fd_softc_t *fd)
   1771 {
   1772 	int i;
   1773 	int err;
   1774 	int secsize;
   1775 
   1776 	err = 0;
   1777 	secsize = fd->currentType->sectorSize;
   1778 	fd->cachedSide = 0;
   1779 
   1780 	fd->cbuf = (unsigned char *) malloc(IWM_MAX_GCR_SECTORS
   1781 	    * secsize, M_DEVBUF, M_WAITOK);
   1782 	if (NULL == fd->cbuf)
   1783 		err = ENOMEM;
   1784 	else
   1785 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
   1786 			fd->w_slots[i].valid = 0;
   1787 			fd->w_slots[i].secbuf = NULL;
   1788 
   1789 			fd->r_slots[i].valid = 0;
   1790 			fd->r_slots[i].secbuf = fd->cbuf + i * secsize;
   1791 		}
   1792 	return err;
   1793 }
   1794 
   1795 
   1796 /*
   1797  * invalidateCylinderCache
   1798  *
   1799  * Switching cylinders (tracks?) invalidates the read cache.
   1800  */
   1801 static void
   1802 invalidateCylinderCache(fd_softc_t *fd)
   1803 {
   1804 	int i;
   1805 
   1806 	fd->cachedSide = 0;
   1807 	for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
   1808 		fd->r_slots[i].valid = 0;
   1809 	}
   1810 }
   1811 
   1812 
   1813 /*
   1814  * getFDType
   1815  *
   1816  * return pointer to disk format description
   1817  */
   1818 static fdInfo_t *
   1819 getFDType(short unit)
   1820 {
   1821 	int driveFlags;
   1822 	fdInfo_t *thisType;
   1823 	extern fdInfo_t fdTypes[];
   1824 
   1825 	driveFlags = iwmCheckDrive(unit);
   1826 /*
   1827  * Drive flags are: Bit  0 - 1 = Drive is double sided
   1828  *			 1 - 1 = No disk inserted
   1829  *			 2 - 1 = Motor is off
   1830  *			 3 - 1 = Disk is writable
   1831  *			 4 - 1 = Disk is DD (800/720K)
   1832  *			31 - 1 = No drive / invalid drive #
   1833  */
   1834 	if (TRACE_CONFIG) {
   1835 		printf("iwm: Drive %d says 0x0%x (%d)\n",
   1836 		    unit, driveFlags, driveFlags);
   1837 	}
   1838 	if (driveFlags < 0)
   1839 		thisType = NULL;/* no such drive	 */
   1840 	else
   1841 		if (driveFlags & 0x01)
   1842 			thisType = &fdTypes[1];	/* double sided		 */
   1843 		else
   1844 			thisType = &fdTypes[0];	/* single sided		 */
   1845 
   1846 	return thisType;
   1847 }
   1848 
   1849 
   1850 /*
   1851  * fdDeviceToType
   1852  *
   1853  * maps the minor device number (elsewhere: partition type) to
   1854  * a corresponding disk format.
   1855  * This is currently:
   1856  * 	fdXa	default (800K GCR)
   1857  *	fdXb	400K GCR
   1858  *	fdXc	800K GCR
   1859  */
   1860 static fdInfo_t *
   1861 fdDeviceToType(fd_softc_t *fd, dev_t dev)
   1862 {
   1863 	int type;
   1864 	fdInfo_t *thisInfo;
   1865 	/* XXX This broke with egcs 1.0.2 */
   1866 	/* extern fdInfo_t	fdTypes[]; */
   1867 
   1868 	type = minor(dev) % MAXPARTITIONS;	/* 1,2,... */
   1869 	if (type > sizeof(fdTypes) / sizeof(fdTypes[0]))
   1870 		thisInfo = NULL;
   1871 	else
   1872 		thisInfo = (type == 0) ? fd->defaultType : &fdTypes[type - 1];
   1873 	return thisInfo;
   1874 }
   1875 
   1876 
   1877 /*
   1878  * seek
   1879  *
   1880  * Step to given track; optionally restore to track zero before
   1881  * and/or verify correct track.
   1882  * Note that any necessary retries are done here.
   1883  * We keep the current position on disk in a 'struct diskPosition'.
   1884  */
   1885 static int
   1886 seek(fd_softc_t *fd, int style)
   1887 {
   1888 	int state, done;
   1889 	int err, ierr;
   1890 	int steps;
   1891 
   1892 	diskPosition_t *loc;
   1893 	sectorHdr_t hdr;
   1894 	char action[32];
   1895 #ifndef _LKM
   1896 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1897 #endif
   1898 
   1899 	const char *stateDesc[] = {
   1900 		"Init",
   1901 		"Seek",
   1902 		"Recalibrate",
   1903 		"Verify",
   1904 		"Exit"
   1905 	};
   1906 	enum {
   1907 		seek_state_Init = 0,
   1908 		seek_state_Seek,
   1909 		seek_state_Recalibrate,
   1910 		seek_state_Verify,
   1911 		seek_state_Exit
   1912 	};
   1913 	/* XXX egcs */
   1914 	done = err = ierr = 0;
   1915 	fd->seekRetries = 0;
   1916 	fd->verifyRetries = 0;
   1917 
   1918 	loc = &fd->pos;
   1919 
   1920 	state = seek_state_Init;
   1921 	do {
   1922 		if (TRACE_STEP)
   1923 			printf(" seek state %d [%s].\n",
   1924 			    state, stateDesc[state]);
   1925 		switch (state) {
   1926 
   1927 		case seek_state_Init:
   1928 			if (TRACE_STEP)
   1929 				printf("Current track is %d, new track %d.\n",
   1930 				    loc->oldTrack, loc->track);
   1931 			memset(&hdr, 0, sizeof(hdr));
   1932 			err = ierr = 0;
   1933 			fd->seekRetries = 0;
   1934 			fd->verifyRetries = 0;
   1935 			state = (style == IWM_SEEK_RECAL)
   1936 			    ? seek_state_Recalibrate : seek_state_Seek;
   1937 			done = 0;
   1938 			break;
   1939 
   1940 		case seek_state_Recalibrate:
   1941 			ierr = iwmTrack00();
   1942 			if (ierr == 0) {
   1943 				loc->oldTrack = 0;
   1944 				state = seek_state_Seek;
   1945 			} else {
   1946 				strncpy(action, "Recalibrate (track 0)",
   1947 				    sizeof(action));
   1948 				state = seek_state_Exit;
   1949 			}
   1950 			break;
   1951 
   1952 		case seek_state_Seek:
   1953 			ierr = 0;
   1954 			steps = loc->track - loc->oldTrack;
   1955 
   1956 			if (steps != 0)
   1957 				ierr = iwmSeek(steps);
   1958 			if (ierr == 0) {
   1959 				/* No error or nothing to do */
   1960 				state = (style == IWM_SEEK_VERIFY)
   1961 				    ? seek_state_Verify : seek_state_Exit;
   1962 			} else {
   1963 				if (fd->seekRetries++ < iwm->maxRetries)
   1964 					state = seek_state_Recalibrate;
   1965 				else {
   1966 					strncpy(action, "Seek retries",
   1967 					    sizeof(action));
   1968 					state = seek_state_Exit;
   1969 				}
   1970 			}
   1971 			break;
   1972 
   1973 		case seek_state_Verify:
   1974 			ierr = checkTrack(loc, TRACE_STEP);
   1975 			if (ierr == 0 && loc->track == hdr.track)
   1976 				state = seek_state_Exit;
   1977 			else {
   1978 				if (fd->verifyRetries++ < iwm->maxRetries)
   1979 					state = seek_state_Recalibrate;
   1980 				else {
   1981 					strncpy(action, "Verify retries",
   1982 					    sizeof(action));
   1983 					state = seek_state_Exit;
   1984 				}
   1985 			}
   1986 			break;
   1987 
   1988 		case seek_state_Exit:
   1989 			if (ierr == 0) {
   1990 				loc->oldTrack = loc->track;
   1991 				err = 0;
   1992 				/* Give the head some time to settle down */
   1993 				delay(3000);
   1994 			} else {
   1995 #ifdef DIAGNOSTIC
   1996 				printf(" seek() action \"%s\", err = %d.\n",
   1997 				    action, ierr);
   1998 #endif
   1999 				err = EIO;
   2000 			}
   2001 			done = 1;
   2002 			break;
   2003 		}
   2004 	} while (!done);
   2005 	return err;
   2006 }
   2007 
   2008 
   2009 /*
   2010  * checkTrack
   2011  *
   2012  * After positioning, get a sector header for validation
   2013  */
   2014 static int
   2015 checkTrack(diskPosition_t *loc, int debugFlag)
   2016 {
   2017 	int spl;
   2018 	int iwmErr;
   2019 	sectorHdr_t hdr;
   2020 
   2021 	spl = spl6();
   2022 	iwmSelectSide(loc->side);
   2023 	iwmErr = iwmReadSectHdr(&hdr);
   2024 	splx(spl);
   2025 	if (debugFlag) {
   2026 		printf("Seeked for %d, got at %d, Hdr read err %d.\n",
   2027 		    loc->track, hdr.track, iwmErr);
   2028 	}
   2029 	return iwmErr;
   2030 }
   2031 
   2032 
   2033 /* Debugging stuff */
   2034 
   2035 static void
   2036 hexDump(u_char *buf, int len)
   2037 {
   2038 	int i, j;
   2039 	u_char ch;
   2040 
   2041 	printf("\nDump %d from %p:\n", len, buf);
   2042 	i = j = 0;
   2043 	if (NULL != buf) do {
   2044 		printf("%04x: ", i);
   2045 		for (j = 0; j < 8; j++)
   2046 			printf("%02x ", buf[i + j]);
   2047 		printf(" ");
   2048 		for (j = 8; j < 16; j++)
   2049 			printf("%02x ", buf[i + j]);
   2050 		printf(" ");
   2051 		for (j = 0; j < 16; j++) {
   2052 			ch = buf[i + j];
   2053 			if (ch > 31 && ch < 127)
   2054 				printf("%c", ch);
   2055 			else
   2056 				printf(".");
   2057 		}
   2058 		printf("\n");
   2059 		i += 16;
   2060 	} while (len > i);
   2061 }
   2062 
   2063 
   2064 static void
   2065 fdPrintDiskLabel(struct disklabel *lp)
   2066 {
   2067 	int i;
   2068 
   2069 	printf("iwm: Disklabel entries of current floppy.\n");
   2070 	printf("\t d_type:\t%d (%s)\n", lp->d_type,
   2071 	    dktypenames[lp->d_type]);
   2072 	printf("\t d_typename:\t%s\n", lp->d_typename);
   2073 	printf("\t d_packname:\t%s\n", lp->d_packname);
   2074 
   2075 	printf("\t d_secsize:\t%d\n", lp->d_secsize);
   2076 	printf("\t d_nsectors:\t%d\n", lp->d_nsectors);
   2077 	printf("\t d_ntracks:\t%d\n", lp->d_ntracks);
   2078 	printf("\t d_ncylinders:\t%d\n", lp->d_ncylinders);
   2079 	printf("\t d_secpercyl:\t%d\n", lp->d_secpercyl);
   2080 	printf("\t d_secperunit:\t%d\n", lp->d_secperunit);
   2081 
   2082 	printf("\t d_rpm: \t%d\n", lp->d_rpm);
   2083 	printf("\t d_interleave:\t%d\n", lp->d_interleave);
   2084 	printf("\t d_trkseek:\t%d [ms]\n", lp->d_trkseek);
   2085 
   2086 	printf(" d_npartitions:\t%d\n", lp->d_npartitions);
   2087 	for (i = 0; i < lp->d_npartitions; i++) {
   2088 		printf("\t d_partitions[%d].p_offset:\t%d\n", i,
   2089 		    lp->d_partitions[i].p_offset);
   2090 		printf("\t d_partitions[%d].p_size:\t%d\n", i,
   2091 		    lp->d_partitions[i].p_size);
   2092 		printf("\t d_partitions[%d].p_fstype:\t%d (%s)\n", i,
   2093 		    lp->d_partitions[i].p_fstype,
   2094 		    fstypenames[lp->d_partitions[i].p_fstype]);
   2095 		printf("\t d_partitions[%d].p_frag:\t%d\n", i,
   2096 		    lp->d_partitions[i].p_frag);
   2097 		printf("\n");
   2098 	}
   2099 }
   2100