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