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