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