Home | History | Annotate | Line # | Download | only in obio
iwm_fd.c revision 1.13
      1 /*	$NetBSD: iwm_fd.c,v 1.13 2002/09/06 13:18:43 gehenna 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 #ifdef _LKM
     63 #include "iwm_mod.h"
     64 #endif
     65 
     66 /**
     67  **	Private functions
     68  **/
     69 static int map_iwm_base __P((vm_offset_t base));
     70 
     71 /* Autoconfig */
     72 int	iwm_match __P((struct device *, struct cfdata *, void *));
     73 void	iwm_attach __P((struct device *, struct device *, void *));
     74 int	iwm_print __P((void *, const char *));
     75 int	fd_match __P((struct device *, struct cfdata *, void *));
     76 void	fd_attach __P((struct device *, struct device *, void *));
     77 int	fd_print __P((void *, const char *));
     78 
     79 /* Disklabel stuff */
     80 static void fdGetDiskLabel __P((fd_softc_t *fd, dev_t dev));
     81 static void fdPrintDiskLabel __P((struct disklabel *lp));
     82 
     83 static fdInfo_t *getFDType __P((short unit));
     84 static fdInfo_t *fdDeviceToType __P((fd_softc_t *fd, dev_t dev));
     85 
     86 static void fdstart __P((fd_softc_t *fd));
     87 static void remap_geometry __P((daddr_t block, int heads, diskPosition_t *loc));
     88 static void motor_off __P((void *param));
     89 static int seek __P((fd_softc_t *fd, int style));
     90 static int checkTrack __P((diskPosition_t *loc, int debugFlag));
     91 static int initCylinderCache __P((fd_softc_t *fd));
     92 static void invalidateCylinderCache __P((fd_softc_t *fd));
     93 
     94 #ifdef _LKM
     95 static int probe_fd __P((void));
     96 int fd_mod_init __P((void));
     97 void fd_mod_free __P((void));
     98 #endif
     99 
    100 static int fdstart_Init __P((fd_softc_t *fd));
    101 static int fdstart_Seek __P((fd_softc_t *fd));
    102 static int fdstart_Read __P((fd_softc_t *fd));
    103 static int fdstart_Write __P((fd_softc_t *fd));
    104 static int fdstart_Flush __P((fd_softc_t *fd));
    105 static int fdstart_IOFinish __P((fd_softc_t *fd));
    106 static int fdstart_IOErr __P((fd_softc_t *fd));
    107 static int fdstart_Fault __P((fd_softc_t *fd));
    108 static int fdstart_Exit __P((fd_softc_t *fd));
    109 
    110 
    111 /**
    112  **	Driver debugging
    113  **/
    114 
    115 #ifdef DEBUG
    116 #define IWM_DEBUG
    117 #endif
    118 
    119 
    120 static void hexDump __P((u_char *buf, int len));
    121 
    122 /*
    123  * Stuff taken from Egan/Teixeira ch 8: 'if(TRACE_FOO)' debug output
    124  * statements don't break indentation, and when DEBUG is not defined,
    125  * the compiler code optimizer drops them as dead code.
    126  */
    127 #ifdef IWM_DEBUG
    128 #define M_TRACE_CONFIG	0x0001
    129 #define M_TRACE_OPEN	0x0002
    130 #define M_TRACE_CLOSE	0x0004
    131 #define M_TRACE_READ	0x0008
    132 #define M_TRACE_WRITE	0x0010
    133 #define M_TRACE_STRAT	(M_TRACE_READ | M_TRACE_WRITE)
    134 #define M_TRACE_IOCTL	0x0020
    135 #define M_TRACE_STEP	0x0040
    136 #define M_TRACE_ALL	0xFFFF
    137 
    138 #define TRACE_CONFIG	(iwmDebugging & M_TRACE_CONFIG)
    139 #define TRACE_OPEN	(iwmDebugging & M_TRACE_OPEN)
    140 #define TRACE_CLOSE	(iwmDebugging & M_TRACE_CLOSE)
    141 #define TRACE_READ	(iwmDebugging & M_TRACE_READ)
    142 #define TRACE_WRITE	(iwmDebugging & M_TRACE_WRITE)
    143 #define TRACE_STRAT	(iwmDebugging & M_TRACE_STRAT)
    144 #define TRACE_IOCTL	(iwmDebugging & M_TRACE_IOCTL)
    145 #define TRACE_STEP	(iwmDebugging & M_TRACE_STEP)
    146 #define TRACE_ALL	(iwmDebugging & M_TRACE_ALL)
    147 
    148  /* -1 = all active */
    149 int     iwmDebugging = 0 /* | M_TRACE_OPEN | M_TRACE_STRAT | M_TRACE_IOCTL */ ;
    150 
    151 #else
    152 #define TRACE_CONFIG	0
    153 #define TRACE_OPEN	0
    154 #define TRACE_CLOSE	0
    155 #define TRACE_READ	0
    156 #define TRACE_WRITE	0
    157 #define TRACE_STRAT	0
    158 #define TRACE_IOCTL	0
    159 #define TRACE_STEP	0
    160 #define TRACE_ALL	0
    161 #endif
    162 
    163 #define DISABLED 	0
    164 
    165 
    166 
    167 /**
    168  ** Module-global Variables
    169  **/
    170 
    171 /* The IWM base address */
    172 u_long IWMBase;
    173 
    174 /*
    175  * Table of supported disk types.
    176  * The table order seems to be pretty standardized across NetBSD ports, but
    177  * then, they are all MFM... So we roll our own for now.
    178  */
    179 static fdInfo_t fdTypes[] = {
    180 	{1, 80, 512, 10, 10,  800, 12, 2, IWM_GCR, "400K Sony"},
    181 	{2, 80, 512, 10, 20, 1600, 12, 2, IWM_GCR, "800K Sony"}
    182 };
    183 
    184 /* Table of GCR disk zones for one side (see IM II-211, The Disk Driver) */
    185 static diskZone_t diskZones[] = {
    186 	{16, 12,   0, 191},
    187 	{16, 11, 192, 367},
    188 	{16, 10, 368, 527},
    189 	{16,  9, 528, 671},
    190 	{16,  8, 672, 799}
    191 };
    192 
    193 /* Drive format codes/indexes */
    194 enum {
    195 	IWM_400K_GCR = 0,
    196 	IWM_800K_GCR = 1,
    197 	IWM_720K_MFM = 2,
    198 	IWM_1440K_MFM = 3
    199 };
    200 
    201 
    202 /**
    203  ** Autoconfiguration code
    204  **/
    205 
    206 /*
    207  * Autoconfig data structures
    208  *
    209  * These data structures (see <sys/device.h>) are referenced in
    210  * compile/$KERNEL/ioconf.c, which is generated by config(8).
    211  * Their names are formed like {device}_{ca,cd}.
    212  *
    213  * {device}_ca
    214  * is used for dynamically allocating driver data, probing and
    215  * attaching a device;
    216  *
    217  * {device}_cd
    218  * references all found devices of a type.
    219  */
    220 #ifdef _LKM
    221 
    222 struct cfdriver iwm_cd = {
    223 	NULL,			/* Ptr to array of devices found	 */
    224 	"iwm",			/* Device name string			 */
    225 	DV_DULL,		/* Device classification		 */
    226 	0			/* Number of devices found		 */
    227 };
    228 struct cfdriver fd_cd = {
    229 	NULL,
    230 	"fd",
    231 	DV_DISK,
    232 	0
    233 };
    234 
    235 #else /* defined _LKM */
    236 
    237 extern struct cfdriver iwm_cd;
    238 extern struct cfdriver fd_cd;
    239 
    240 #endif /* defined _LKM */
    241 
    242 /* IWM floppy disk controller */
    243 struct cfattach iwm_ca = {
    244 	sizeof(iwm_softc_t),	/* Size of device data for malloc()	 */
    245 	iwm_match,		/* Probe device and return match level	 */
    246 	iwm_attach		/* Initialize and attach device		 */
    247 };
    248 
    249 /* Attached floppy disk drives */
    250 struct cfattach fd_ca = {
    251 	sizeof(fd_softc_t),
    252 	fd_match,
    253 	fd_attach
    254 };
    255 
    256 dev_type_open(fdopen);
    257 dev_type_close(fdclose);
    258 dev_type_read(fdread);
    259 dev_type_write(fdwrite);
    260 dev_type_ioctl(fdioctl);
    261 dev_type_strategy(fdstrategy);
    262 
    263 const struct bdevsw fd_bdevsw = {
    264 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
    265 };
    266 
    267 const struct cdevsw fd_cdevsw = {
    268 	fdopen, fdclose, fdread, fdwrite, fdioctl,
    269 	nostop, notty, nopoll, nommap, D_DISK
    270 };
    271 
    272 /* disk(9) framework device switch */
    273 struct dkdriver fd_dkDriver = {
    274 	fdstrategy
    275 };
    276 
    277 /***  Configure the IWM controller  ***/
    278 
    279 /*
    280  * iwm_match
    281  *
    282  * Is the IWM chip present? Here, *auxp is a ptr to struct confargs
    283  * (see <mac68k/mac68k/autoconf.h>), which does not hold any information
    284  * to match against. After all, that's what the obio concept is
    285  * about: Onboard components that are present depending (only)
    286  * on machine type.
    287  */
    288 int
    289 iwm_match(parent, match, auxp)
    290 	struct device *parent;
    291 	struct cfdata *match;
    292 	void *auxp;
    293 {
    294 	int matched;
    295 #ifdef _LKM
    296 	int iwmErr;
    297 #endif
    298 	extern u_long IOBase;		/* from mac68k/machdep.c */
    299 	extern u_long IWMBase;
    300 
    301 	if (0 == map_iwm_base(IOBase)) {
    302 		/*
    303 		 * Unknown machine HW:
    304 		 * The SWIM II/III chips that are present in post-Q700
    305 		 * '040 Macs have dropped the IWM register structure.
    306 		 * We know next to nothing about the SWIM.
    307 		 */
    308 		matched = 0;
    309 		if (TRACE_CONFIG)
    310 			printf("IWM or SWIM not found: Unknown location (SWIM II?).\n");
    311 	} else {
    312 		matched = 1;
    313 		if (TRACE_CONFIG) {
    314 			printf("iwm: IWMBase mapped to 0x%lx in VM.\n",
    315 			    IWMBase);
    316 		}
    317 #ifdef _LKM
    318 		iwmErr = iwmInit();
    319 		if (TRACE_CONFIG)
    320 			printf("initIWM() says %d.\n", iwmErr);
    321 		matched = (iwmErr == 0) ? 1 : 0;
    322 #endif
    323 	}
    324 	return matched;
    325 }
    326 
    327 
    328 /*
    329  * iwm_attach
    330  *
    331  * The IWM is present, initialize it. Then look up the connected drives
    332  * and attach them.
    333  */
    334 void
    335 iwm_attach(parent, self, auxp)
    336 	struct device *parent;
    337 	struct device *self;
    338 	void *auxp;
    339 {
    340 	int iwmErr;
    341 	iwm_softc_t *iwm;
    342 	iwmAttachArgs_t ia;
    343 
    344 	printf(": Apple GCR floppy disk controller\n");
    345 	iwm = (iwm_softc_t *)self;
    346 
    347 	iwmErr = iwmInit();
    348 	if (TRACE_CONFIG)
    349 		printf("initIWM() says %d.\n", iwmErr);
    350 
    351 	if (0 == iwmErr) {
    352 		/* Set up the IWM softc */
    353 		iwm->maxRetries = 10;
    354 
    355 		/* Look for attached drives */
    356 		for (ia.unit = 0; ia.unit < IWM_MAX_DRIVE; ia.unit++) {
    357 			iwm->fd[ia.unit] = NULL;
    358 			ia.driveType = getFDType(ia.unit);
    359 			if (NULL != ia.driveType)
    360 				config_found_sm(self, (void *)&ia,
    361 				    fd_print, NULL);
    362 		}
    363 		if (TRACE_CONFIG)
    364 			printf("iwm: Initialization completed.\n");
    365 	} else {
    366 		printf("iwm: Chip revision not supported (%d)\n", iwmErr);
    367 	}
    368 }
    369 
    370 
    371 /*
    372  * iwm_print -- print device configuration.
    373  *
    374  * If the device is not configured 'controller' it is NULL and
    375  * we print a message in the *Attach routine; the return value
    376  * of *Print() is ignored.
    377  */
    378 int
    379 iwm_print(auxp, controller)
    380 	void *auxp;
    381 	const char *controller;
    382 {
    383 	return UNCONF;
    384 }
    385 
    386 
    387 /*
    388  * map_iwm_base
    389  *
    390  * Map physical IO address of IWM to VM address
    391  */
    392 static int
    393 map_iwm_base(base)
    394 	vm_offset_t base;
    395 {
    396 	int known;
    397 	extern u_long IWMBase;
    398 
    399 	switch (current_mac_model->class) {
    400 	case MACH_CLASSQ:
    401 	case MACH_CLASSQ2:
    402 	case MACH_CLASSP580:
    403 		IWMBase = base + 0x1E000;
    404 		known = 1;
    405 		break;
    406 	case MACH_CLASSII:
    407 	case MACH_CLASSPB:
    408 	case MACH_CLASSDUO:
    409 	case MACH_CLASSIIci:
    410 	case MACH_CLASSIIsi:
    411 	case MACH_CLASSIIvx:
    412 	case MACH_CLASSLC:
    413 		IWMBase = base + 0x16000;
    414 		known = 1;
    415 		break;
    416 	case MACH_CLASSIIfx:
    417 	case MACH_CLASSAV:
    418 	default:
    419 		/*
    420 		 * Neither IIfx/Q9[05]0 style IOP controllers nor
    421 		 * Q[68]40AV DMA based controllers are supported.
    422 		 */
    423 		if (TRACE_CONFIG)
    424 			printf("Unknown floppy controller chip.\n");
    425 		IWMBase = 0L;
    426 		known = 0;
    427 		break;
    428 	}
    429 	return known;
    430 }
    431 
    432 
    433 /***  Configure Sony disk drive(s)  ***/
    434 
    435 /*
    436  * fd_match
    437  */
    438 int
    439 fd_match(parent, match, auxp)
    440 	struct device *parent;
    441 	struct cfdata *match;
    442 	void *auxp;
    443 {
    444 	int matched, cfUnit;
    445 	struct cfdata *cfp;
    446 	iwmAttachArgs_t *fdParams;
    447 
    448 	cfp = match;
    449 	fdParams = (iwmAttachArgs_t *)auxp;
    450 	cfUnit = cfp->cf_loc[0];
    451 	matched = (cfUnit == fdParams->unit || cfUnit == -1) ? 1 : 0;
    452 	if (TRACE_CONFIG) {
    453 		printf("fdMatch() drive %d ? cfUnit = %d\n",
    454 		    fdParams->unit, cfp->cf_loc[0]);
    455 	}
    456 	return matched;
    457 }
    458 
    459 
    460 /*
    461  * fd_attach
    462  *
    463  * We have checked that the IWM is fine and the drive is present,
    464  * so we can attach it.
    465  */
    466 void
    467 fd_attach(parent, self, auxp)
    468 	struct device *parent;
    469 	struct device *self;
    470 	void *auxp;
    471 {
    472 	iwm_softc_t *iwm;
    473 	fd_softc_t *fd;
    474 	iwmAttachArgs_t *ia;
    475 	int driveInfo;
    476 
    477 	iwm = (iwm_softc_t *)parent;
    478 	fd = (fd_softc_t *)self;
    479 	ia = (iwmAttachArgs_t *)auxp;
    480 
    481 	driveInfo = iwmCheckDrive(ia->unit);
    482 
    483 	fd->currentType = ia->driveType;
    484 	fd->unit = ia->unit;
    485 	fd->defaultType = &fdTypes[IWM_800K_GCR];
    486 	fd->stepDirection = 0;
    487 
    488 	iwm->fd[ia->unit] = fd;		/* iwm has ptr to this drive */
    489 	iwm->drives++;
    490 
    491 	bufq_alloc(&fd->bufQueue, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
    492 	callout_init(&fd->motor_ch);
    493 
    494 	printf(" drive %d: ", fd->unit);
    495 
    496 	if (IWM_NO_DISK & driveInfo) {
    497 		printf("(drive empty)\n");
    498 	} else
    499 		if (!(IWM_DD_DISK & driveInfo)) {
    500 			printf("(HD disk -- not supported)\n");
    501 			iwmDiskEject(fd->unit);	/* XXX */
    502 		} else {
    503 			printf("%s %d cyl, %d head(s)\n",
    504 			    fd->currentType->description,
    505 			    fd->currentType->tracks,
    506 			    fd->currentType->heads);
    507 		}
    508 	if (TRACE_CONFIG) {
    509 		int reg, flags, spl;
    510 
    511 		/* List contents of drive status registers */
    512 		spl = spl6();
    513 		for (reg = 0; reg < 0x10; reg++) {
    514 			flags = iwmQueryDrvFlag(fd->unit, reg);
    515 			printf("iwm: Drive register 0x%x = 0x%x\n", reg, flags);
    516 		}
    517 		splx(spl);
    518 	}
    519 	fd->diskInfo.dk_name = fd->devInfo.dv_xname;
    520 	fd->diskInfo.dk_driver = &fd_dkDriver;
    521 	disk_attach(&fd->diskInfo);
    522 }
    523 
    524 
    525 /*
    526  * fdPrint -- print device configuration.
    527  *
    528  * If the device is not configured 'controller' refers to a name string
    529  * we print here.
    530  * Else it is NULL and we print a message in the *Attach routine; the
    531  * return value of *Print() is ignored.
    532  */
    533 int
    534 fd_print(auxp, controller)
    535 	void *auxp;
    536 	const char *controller;
    537 {
    538 	iwmAttachArgs_t *ia;
    539 
    540 	ia = (iwmAttachArgs_t *)auxp;
    541 	if (NULL != controller)
    542 		printf("fd%d at %s", ia->unit, controller);
    543 	return UNCONF;
    544 }
    545 
    546 
    547 #ifdef _LKM
    548 
    549 static iwm_softc_t *iwm;
    550 
    551 /*
    552  * fd_mod_init
    553  *
    554  * Any initializations necessary after loading the module happen here.
    555  */
    556 int
    557 fd_mod_init(void)
    558 {
    559 	int err;
    560 
    561 	iwm = (iwm_softc_t *)malloc(sizeof(iwm_softc_t), M_DEVBUF, M_WAITOK);
    562 
    563 	err = (1 == iwm_match(NULL, NULL, NULL)) ? 0 : EIO;
    564 	if (!err) {
    565 		memset(iwm, 0, sizeof(iwm_softc_t));
    566 		iwm->maxRetries = 10;
    567 		err = (0 == probe_fd()) ? 0 : EIO;
    568 	}
    569 	return err;
    570 }
    571 
    572 
    573 /*
    574  * fd_mod_free
    575  *
    576  * Necessary clean-up before unloading the module.
    577  */
    578 void
    579 fd_mod_free(void)
    580 {
    581 	int unit, spl;
    582 
    583 	spl = splbio();
    584 	/* Release any allocated memory */
    585 	for (unit = 0; unit < IWM_MAX_DRIVE; unit++)
    586 		if (iwm->fd[unit] != NULL) {
    587 			/*
    588 			 * Let's hope there is only one task per drive,
    589 			 * see callout(9).
    590 			 */
    591 			callout_stop(&iwm->fd[unit]->motor_ch);
    592 			disk_detach(&iwm->fd[unit]->diskInfo);
    593 			free(iwm->fd[unit], M_DEVBUF);
    594 			iwm->fd[unit] = NULL;
    595 		}
    596 	free(iwm, M_DEVBUF);
    597 	splx(spl);
    598 }
    599 
    600 
    601 /*
    602  * probe_fd
    603  *
    604  * See if there are any drives out there and configure them.
    605  * If we find a drive we allocate a softc structure for it and
    606  * insert its address into the iwm_softc.
    607  *
    608  * XXX Merge the remainder of probeFD() with the autoconfig framework.
    609  */
    610 static int
    611 probe_fd(void)
    612 {
    613 	fd_softc_t *fd;
    614 	iwmAttachArgs_t ia;
    615 	int err, unit;
    616 
    617 	err = 0;
    618 	for (ia.unit = 0; ia.unit < IWM_MAX_DRIVE; ia.unit++) {
    619 		ia.driveType = getFDType(ia.unit);
    620 		if (NULL == ia.driveType) {
    621 			iwm->fd[ia.unit] = NULL;
    622 			continue;
    623 		}
    624 		fd = (fd_softc_t *)malloc(sizeof(fd_softc_t),
    625 		    M_DEVBUF, M_WAITOK);
    626 		if (fd == NULL) {
    627 			err = ENOMEM;
    628 			break;
    629 		} else {
    630 			memset(fd, 0, sizeof(fd_softc_t));
    631 
    632 			/* This is usually set by the autoconfig framework */
    633 			sprintf(fd->devInfo.dv_xname, "fd%d%c", ia.unit, 'a');
    634 			fd_attach((struct device *)iwm, (struct device *)fd,
    635 			    &ia);
    636 		}
    637 	}
    638 	if (err) {
    639 		/* Release any allocated memory */
    640 		for (unit = 0; unit < IWM_MAX_DRIVE; unit++)
    641 			if (iwm->fd[unit] != NULL) {
    642 				free(iwm->fd[unit], M_DEVBUF);
    643 				iwm->fd[unit] = NULL;
    644 			}
    645 	}
    646 	return err;
    647 }
    648 
    649 #endif /* defined _LKM */
    650 
    651 
    652 /**
    653  ** Implementation section of driver interface
    654  **
    655  ** The prototypes for these functions are set up automagically
    656  ** by macros in mac68k/conf.c. Their names are generated from {fd}
    657  ** and {open,close,strategy,dump,size,read,write}. The driver entry
    658  ** points are then plugged into bdevsw[] and cdevsw[].
    659  **/
    660 
    661 
    662 /*
    663  * fdopen
    664  *
    665  * Open a floppy disk device.
    666  */
    667 int
    668 fdopen(dev, flags, devType, proc)
    669 	dev_t dev;
    670 	int flags;
    671 	int devType;
    672 	struct proc *proc;
    673 {
    674 	fd_softc_t *fd;
    675 	fdInfo_t *info;
    676 	int partitionMask;
    677 	int fdType, fdUnit;
    678 	int ierr, err;
    679 #ifndef _LKM
    680 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
    681 #endif
    682 	info = NULL;		/* XXX shut up egcs */
    683 
    684 	/*
    685 	 * See <device.h> for struct cfdriver, <disklabel.h> for
    686 	 * DISKUNIT() and <atari/atari/device.h> for getsoftc().
    687 	 */
    688 	fdType = minor(dev) % MAXPARTITIONS;
    689 	fdUnit = minor(dev) / MAXPARTITIONS;
    690 	if (TRACE_OPEN)
    691 		printf("iwm: Open drive %d", fdUnit);
    692 
    693 	/* Check if device # is valid */
    694 	err = (iwm->drives < fdUnit) ? ENXIO : 0;
    695 	if (!err) {
    696 		(void)iwmSelectDrive(fdUnit);
    697 		if (TRACE_OPEN)
    698 			printf(".\n Get softc");
    699 
    700 		/* Get fd state */
    701 		fd = iwm->fd[fdUnit];
    702 		err = (NULL == fd) ? ENXIO : 0;
    703 	}
    704 	if (!err) {
    705 		if (fd->state & IWM_FD_IS_OPEN) {
    706 			/*
    707 			 * Allow multiple open calls only if for identical
    708 			 * floppy format.
    709 			 */
    710 			if (TRACE_OPEN)
    711 				printf(".\n Drive already opened!\n");
    712 			err = (fd->partition == fdType) ? 0 : ENXIO;
    713 		} else {
    714 			if (TRACE_OPEN)
    715 				printf(".\n Get format info");
    716 
    717 			/* Get format type */
    718 			info = fdDeviceToType(fd, dev);
    719 			if (NULL == info) {
    720 				err = ENXIO;
    721 				if (TRACE_OPEN)
    722 					printf(".\n No such drive.\n");
    723 			}
    724 		}
    725 	}
    726 	if (!err && !(fd->state & IWM_FD_IS_OPEN)) {
    727 		if (TRACE_OPEN)
    728 			printf(".\n Set diskInfo flags.\n");
    729 
    730 		fd->writeLabel = 0;		/* XXX currently unused */
    731 		fd->partition = fdType;
    732 		fd->currentType = info;
    733 		fd->drvFlags = iwmCheckDrive(fd->unit);
    734 
    735 		if (fd->drvFlags & IWM_NO_DISK) {
    736 			err = EIO;
    737 #ifdef DIAGNOSTIC
    738 			printf(" Drive %d is empty.\n", fd->unit);
    739 #endif
    740 		} else {
    741 			if (!(fd->drvFlags & IWM_WRITEABLE) && (flags & FWRITE)) {
    742 
    743 				err = EPERM;
    744 #ifdef DIAGNOSTIC
    745 				printf(" Disk is write protected.\n");
    746 #endif
    747 			} else {
    748 				if (!(fd->drvFlags & IWM_DD_DISK)) {
    749 					err = ENXIO;
    750 #ifdef DIAGNOSTIC
    751 					printf(" HD format not supported.\n");
    752 #endif
    753 					(void)iwmDiskEject(fd->unit);
    754 				} else {
    755 					/* We're open now! */
    756 					fd->state |= IWM_FD_IS_OPEN;
    757 					err = initCylinderCache(fd);
    758 				}
    759 			}
    760 		}
    761 	}
    762 	if (!err) {
    763 		/*
    764 		 * Later, we might not want to recalibrate the drive when it
    765 		 * is already open. For now, it doesn't hurt.
    766 		 */
    767 		if (TRACE_OPEN)
    768 			printf(" Seek track 00 says");
    769 
    770 		memset(&fd->pos, 0, sizeof(diskPosition_t));
    771 		ierr = seek(fd, IWM_SEEK_RECAL);
    772 		if (TRACE_OPEN)
    773 			printf(" %d.\n", ierr);
    774 		err = (0 == ierr) ? 0 : EIO;
    775 	}
    776 	if (!err) {
    777 		/*
    778 		 * Update disklabel if we are not yet open.
    779 		 * (We shouldn't be: We are synchronous.)
    780 		 */
    781 		if (fd->diskInfo.dk_openmask == 0)
    782 			fdGetDiskLabel(fd, dev);
    783 
    784 		partitionMask = (1 << fdType);
    785 
    786 		switch (devType) {
    787 		case S_IFCHR:
    788 			fd->diskInfo.dk_copenmask |= partitionMask;
    789 			break;
    790 
    791 		case S_IFBLK:
    792 			fd->diskInfo.dk_bopenmask |= partitionMask;
    793 			break;
    794 		}
    795 		fd->diskInfo.dk_openmask =
    796 		    fd->diskInfo.dk_copenmask | fd->diskInfo.dk_bopenmask;
    797 	}
    798 	if (TRACE_OPEN)
    799 		printf("iwm: fdopen() says %d.\n", err);
    800 	return err;
    801 }
    802 
    803 
    804 /*
    805  * fdclose
    806  */
    807 int
    808 fdclose(dev, flags, devType, proc)
    809 	dev_t dev;
    810 	int flags;
    811 	int devType;
    812 	struct proc *proc;
    813 {
    814 	fd_softc_t *fd;
    815 	int partitionMask, fdUnit, fdType;
    816 #ifndef _LKM
    817 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
    818 #endif
    819 
    820 	if (TRACE_CLOSE)
    821 		printf("iwm: Closing driver.");
    822 	fdUnit = minor(dev) / MAXPARTITIONS;
    823 	fdType = minor(dev) % MAXPARTITIONS;
    824 	fd = iwm->fd[fdUnit];
    825 	/* release cylinder cache memory */
    826 	if (fd->cbuf != NULL)
    827 		     free(fd->cbuf, M_DEVBUF);
    828 
    829 	partitionMask = (1 << fdType);
    830 
    831 	/* Set state flag. */
    832 	fd->state &= ~IWM_FD_IS_OPEN;
    833 
    834 	switch (devType) {
    835 	case S_IFCHR:
    836 		fd->diskInfo.dk_copenmask &= ~partitionMask;
    837 		break;
    838 
    839 	case S_IFBLK:
    840 		fd->diskInfo.dk_bopenmask &= ~partitionMask;
    841 		break;
    842 	}
    843 	fd->diskInfo.dk_openmask =
    844 	    fd->diskInfo.dk_copenmask | fd->diskInfo.dk_bopenmask;
    845 	return 0;
    846 }
    847 
    848 
    849 /*
    850  * fdioctl
    851  *
    852  * We deal with all the disk-specific ioctls in <sys/dkio.h> here even if
    853  * we do not support them.
    854  */
    855 int
    856 fdioctl(dev, cmd, data, flags, proc)
    857 	dev_t dev;
    858 	u_long cmd;
    859 	caddr_t data;
    860 	int flags;
    861 	struct proc *proc;
    862 {
    863 	int result, fdUnit, fdType;
    864 	fd_softc_t *fd;
    865 #ifndef _LKM
    866 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
    867 #endif
    868 
    869 	if (TRACE_IOCTL)
    870 		printf("iwm: Execute ioctl... ");
    871 
    872 	/* Check if device # is valid and get its softc */
    873 	fdUnit = minor(dev) / MAXPARTITIONS;
    874 	fdType = minor(dev) % MAXPARTITIONS;
    875 	if (fdUnit >= iwm->drives) {
    876 		if (TRACE_IOCTL) {
    877 			printf("iwm: Wanted device no (%d) is >= %d.\n",
    878 			    fdUnit, iwm->drives);
    879 		}
    880 		return ENXIO;
    881 	}
    882 	fd = iwm->fd[fdUnit];
    883 	result = 0;
    884 
    885 	switch (cmd) {
    886 	case DIOCGDINFO:
    887 		if (TRACE_IOCTL)
    888 			printf(" DIOCGDINFO: Get in-core disklabel.\n");
    889 		*(struct disklabel *) data = *(fd->diskInfo.dk_label);
    890 		result = 0;
    891 		break;
    892 
    893 	case DIOCSDINFO:
    894 		if (TRACE_IOCTL)
    895 			printf(" DIOCSDINFO: Set in-core disklabel.\n");
    896 		result = ((flags & FWRITE) == 0) ? EBADF : 0;
    897 		if (result == 0)
    898 			result = setdisklabel(fd->diskInfo.dk_label,
    899 			    (struct disklabel *)data, 0,
    900 			    fd->diskInfo.dk_cpulabel);
    901 		break;
    902 
    903 	case DIOCWDINFO:
    904 		if (TRACE_IOCTL)
    905 			printf(" DIOCWDINFO: Set in-core disklabel "
    906 			    "& update disk.\n");
    907 		result = ((flags & FWRITE) == 0) ? EBADF : 0;
    908 
    909 		if (result == 0)
    910 			result = setdisklabel(fd->diskInfo.dk_label,
    911 			    (struct disklabel *)data, 0,
    912 			    fd->diskInfo.dk_cpulabel);
    913 		if (result == 0)
    914 			result = writedisklabel(dev, fdstrategy,
    915 			    fd->diskInfo.dk_label,
    916 			    fd->diskInfo.dk_cpulabel);
    917 		break;
    918 
    919 	case DIOCGPART:
    920 		if (TRACE_IOCTL)
    921 			printf(" DIOCGPART: Get disklabel & partition table.\n");
    922 		((struct partinfo *)data)->disklab = fd->diskInfo.dk_label;
    923 		((struct partinfo *)data)->part =
    924 		    &fd->diskInfo.dk_label->d_partitions[fdType];
    925 		result = 0;
    926 		break;
    927 
    928 	case DIOCRFORMAT:
    929 	case DIOCWFORMAT:
    930 		if (TRACE_IOCTL)
    931 			printf(" DIOC{R,W}FORMAT: No formatter support (yet?).\n");
    932 		result = EINVAL;
    933 		break;
    934 
    935 	case DIOCSSTEP:
    936 		if (TRACE_IOCTL)
    937 			printf(" DIOCSSTEP: IWM does step handshake.\n");
    938 		result = EINVAL;
    939 		break;
    940 
    941 	case DIOCSRETRIES:
    942 		if (TRACE_IOCTL)
    943 			printf(" DIOCSRETRIES: Set max. # of retries.\n");
    944 		if (*(int *)data < 0)
    945 			result = EINVAL;
    946 		else {
    947 			iwm->maxRetries = *(int *)data;
    948 			result = 0;
    949 		}
    950 		break;
    951 
    952 	case DIOCWLABEL:
    953 		if (TRACE_IOCTL)
    954 			printf(" DIOCWLABEL: Set write access to disklabel.\n");
    955 		result = ((flags & FWRITE) == 0) ? EBADF : 0;
    956 
    957 		if (result == 0)
    958 			fd->writeLabel = *(int *)data;
    959 		break;
    960 
    961 	case DIOCSBAD:
    962 		if (TRACE_IOCTL)
    963 			printf(" DIOCSBAD: No bad144-style handling.\n");
    964 		result = EINVAL;
    965 		break;
    966 
    967 	case ODIOCEJECT:
    968 	case DIOCEJECT:
    969 		/* XXX Eject disk only when unlocked */
    970 		if (TRACE_IOCTL)
    971 			printf(" DIOCEJECT: Eject disk from unit %d.\n",
    972 			    fd->unit);
    973 		result = iwmDiskEject(fd->unit);
    974 		break;
    975 
    976 	case DIOCLOCK:
    977 		/* XXX Use lock to prevent ejectimg a mounted disk */
    978 		if (TRACE_IOCTL)
    979 			printf(" DIOCLOCK: No need to (un)lock Sony drive.\n");
    980 		result = 0;
    981 		break;
    982 
    983 	default:
    984 		if (TRACE_IOCTL)
    985 			printf(" Not a disk related ioctl!\n");
    986 		result = ENOTTY;
    987 		break;
    988 	}
    989 	return result;
    990 }
    991 
    992 
    993 /*
    994  * fdread
    995  */
    996 int
    997 fdread(dev, uio, flags)
    998 	dev_t dev;
    999 	struct uio *uio;
   1000 	int flags;
   1001 {
   1002 	return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
   1003 }
   1004 
   1005 
   1006 /*
   1007  * fdwrite
   1008  */
   1009 int
   1010 fdwrite(dev, uio, flags)
   1011 	dev_t dev;
   1012 	struct uio *uio;
   1013 	int flags;
   1014 {
   1015 	return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
   1016 }
   1017 
   1018 
   1019 /*
   1020  * fdstrategy
   1021  *
   1022  * Entry point for read and write requests. The strategy routine usually
   1023  * queues io requests and kicks off the next transfer if the device is idle;
   1024  * but we get no interrupts from the IWM and have to do synchronous
   1025  * transfers - no queue.
   1026  */
   1027 void
   1028 fdstrategy(bp)
   1029 	struct buf *bp;
   1030 {
   1031 	int fdUnit, err, done, spl;
   1032 	int sectSize, transferSize;
   1033 	diskPosition_t physDiskLoc;
   1034 	fd_softc_t *fd;
   1035 #ifndef _LKM
   1036 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1037 #endif
   1038 
   1039 	err = 0;
   1040 	done = 0;
   1041 
   1042 	fdUnit = minor(bp->b_dev) / MAXPARTITIONS;
   1043 	if (TRACE_STRAT) {
   1044 		printf("iwm: fdstrategy()...\n");
   1045 		printf("     struct buf is at %p\n", bp);
   1046 		printf("     Allocated buffer size (b_bufsize): 0x0%lx\n",
   1047 		    bp->b_bufsize);
   1048 		printf("     Base address of buffer (b_data): %p\n",
   1049 		    bp->b_data);
   1050 		printf("     Bytes to be transferred (b_bcount): 0x0%lx\n",
   1051 		    bp->b_bcount);
   1052 		printf("     Remaining I/O (b_resid): 0x0%lx\n",
   1053 		    bp->b_resid);
   1054 	}
   1055 	/* Check for valid fd unit, controller and io request */
   1056 
   1057 	if (fdUnit >= iwm->drives) {
   1058 		if (TRACE_STRAT)
   1059 			printf(" No such unit (%d)\n", fdUnit);
   1060 		err = EINVAL;
   1061 	}
   1062 	if (!err) {
   1063 		fd = iwm->fd[fdUnit];
   1064 		err = (NULL == fd) ? EINVAL : 0;
   1065 	}
   1066 	if (!err) {
   1067 		sectSize = fd->currentType->sectorSize;
   1068 		if (bp->b_blkno < 0
   1069 		    || (bp->b_bcount % sectSize) != 0) {
   1070 			if (TRACE_STRAT)
   1071 				printf(" Illegal transfer size: "
   1072 				    "block %d, %ld bytes\n",
   1073 				    bp->b_blkno, bp->b_bcount);
   1074 			err = EINVAL;
   1075 		}
   1076 	}
   1077 	if (!err) {
   1078 		/* Null transfer: Return, nothing to do. */
   1079 		if (0 == bp->b_bcount) {
   1080 			if (TRACE_STRAT)
   1081 				printf(" Zero transfer length.\n");
   1082 			done = 1;
   1083 		}
   1084 	}
   1085 	if (!err && !done) {
   1086 		/* What to do if we touch the boundaries of the disk? */
   1087 		transferSize = (bp->b_bcount + (sectSize - 1)) / sectSize;
   1088 		if (bp->b_blkno + transferSize > fd->currentType->secPerDisk) {
   1089 			if (TRACE_STRAT) {
   1090 				printf("iwm: Transfer beyond end of disk!\n" \
   1091 				    " (Starting block %d, # of blocks %d," \
   1092 				    " last disk block %d).\n",
   1093 				    bp->b_blkno, transferSize,
   1094 				    fd->currentType->secPerDisk);
   1095 			}
   1096 			/* Return EOF if we are exactly at the end of the
   1097 			 * disk, EINVAL if we try to reach past the end; else
   1098 			 * truncate the request. */
   1099 			transferSize = fd->currentType->secPerDisk -
   1100 			    bp->b_blkno;
   1101 			if (0 == transferSize) {
   1102 				bp->b_resid = bp->b_bcount;
   1103 				done = 1;
   1104 			} else
   1105 				if (0 > transferSize)
   1106 					err = EINVAL;
   1107 				else
   1108 					bp->b_bcount = transferSize << DEV_BSHIFT;
   1109 		}
   1110 	}
   1111 	if (!err && !done) {
   1112 		/*
   1113 		 * Calculate cylinder # for disksort().
   1114 		 *
   1115 		 * XXX Shouldn't we use the (fake) logical cyl no here?
   1116 		 */
   1117 		remap_geometry(bp->b_blkno, fd->currentType->heads,
   1118 		    &physDiskLoc);
   1119 		bp->b_rawblkno = bp->b_blkno;
   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 		callout_stop(&fd->motor_ch);
   1130 		BUFQ_PUT(&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_PEEK(&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_data;
   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_GET(&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_data, bp->b_bcount);
   1660 	}
   1661 	if (DISABLED && TRACE_STRAT)
   1662 		printf(" Next buf (bufQueue first) at %p\n",
   1663 		    BUFQ_PEEK(&fd->bufQueue));
   1664 	disk_unbusy(&fd->diskInfo, bp->b_bcount - bp->b_resid);
   1665 	biodone(bp);
   1666 	/*
   1667 	 * Stop motor after 10s
   1668 	 *
   1669 	 * XXX Unloading the module while the timeout is still
   1670 	 *     running WILL crash the machine.
   1671 	 */
   1672 	callout_reset(&fd->motor_ch, 10 * hz, motor_off, fd);
   1673 
   1674 	return state_Done;
   1675 }
   1676 
   1677 
   1678 /*
   1679  * remap_geometry
   1680  *
   1681  * Remap the rigid UN*X view of a disk's cylinder/sector geometry
   1682  * to our zone recorded real Sony drive by splitting the disk
   1683  * into zones.
   1684  *
   1685  * Loop {
   1686  * 	Look if logical block number is in current zone
   1687  *	NO:	Add # of tracks for current zone to track counter
   1688  *		Process next zone
   1689  *
   1690  *	YES:	Subtract (number of first sector of current zone times heads)
   1691  *		from logical block number, then break up the difference
   1692  *		in tracks/side/sectors (spt is constant within a zone).
   1693  *		Done
   1694  * }
   1695  */
   1696 static void
   1697 remap_geometry(block, heads, loc)
   1698 	daddr_t block;
   1699 	int heads;
   1700 	diskPosition_t *loc;
   1701 {
   1702 	int zone, spt;
   1703 	extern diskZone_t diskZones[];
   1704 
   1705 	spt = 0;		/* XXX Shut up egcs warning */
   1706 	loc->oldTrack = loc->track;
   1707 	loc->track = 0;
   1708 
   1709 	for (zone = 0; zone < IWM_GCR_DISK_ZONES; zone++) {
   1710 		if (block >= heads * (diskZones[zone].lastBlock + 1)) {
   1711 			/* Process full zones */
   1712 			loc->track += diskZones[zone].tracks;
   1713 		} else {
   1714 			/* Process partial zone */
   1715 			spt = diskZones[zone].sectPerTrack;
   1716 			block -= heads * diskZones[zone].firstBlock;
   1717 			loc->track += block / (spt * heads);
   1718 			loc->sector = (block % spt);
   1719 			loc->side = (block % (spt * heads)) / spt;
   1720 			break;
   1721 		}
   1722 	}
   1723 	loc->maxSect = spt;
   1724 }
   1725 
   1726 
   1727 /*
   1728  * motor_off
   1729  *
   1730  * Callback for timeout()
   1731  */
   1732 static void
   1733 motor_off(param)
   1734 	void *param;
   1735 {
   1736 	int spl;
   1737 	fd_softc_t *fd;
   1738 
   1739 	fd = (fd_softc_t *)param;
   1740 	if (TRACE_STRAT)
   1741 		printf("iwm: Switching motor OFF (timeout).\n");
   1742 	spl = spl6();
   1743 	(void)iwmMotor(fd->unit, 0);
   1744 	fd->state &= ~IWM_FD_MOTOR_ON;
   1745 	splx(spl);
   1746 }
   1747 
   1748 
   1749 /*
   1750  * fdGetDiskLabel
   1751  *
   1752  * Set up disk label with parameters from current disk type.
   1753  * Then call the generic disklabel read routine which tries to
   1754  * read a label from disk and insert it. If it doesn't exist use
   1755  * our defaults.
   1756  */
   1757 static void
   1758 fdGetDiskLabel(fd, dev)
   1759 	fd_softc_t *fd;
   1760 	dev_t dev;
   1761 {
   1762 	char *msg;
   1763 	int fdType;
   1764 	struct disklabel *lp;
   1765 	struct cpu_disklabel *clp;
   1766 
   1767 	if (TRACE_IOCTL)
   1768 		printf("iwm: fdGetDiskLabel() for disk %d.\n",
   1769 		    minor(dev) / MAXPARTITIONS);
   1770 	fdType = minor(dev) % MAXPARTITIONS;
   1771 	lp = fd->diskInfo.dk_label;
   1772 	clp = fd->diskInfo.dk_cpulabel;
   1773 	memset(lp, 0, sizeof(struct disklabel));
   1774 	memset(clp, 0, sizeof(struct cpu_disklabel));
   1775 	/*
   1776 	 * How to describe a drive with a variable # of sectors per
   1777 	 * track (8..12) and variable rpm (300..550)? Apple came up
   1778 	 * with ZBR in 1983! Un*x drive management sucks.
   1779 	 */
   1780 	lp->d_type = DTYPE_FLOPPY;
   1781 	lp->d_rpm = 300;
   1782 	lp->d_secsize = fd->currentType->sectorSize;
   1783 	lp->d_ntracks = fd->currentType->heads;
   1784 	lp->d_ncylinders = fd->currentType->tracks;
   1785 	lp->d_nsectors = fd->currentType->secPerTrack;
   1786 	lp->d_secpercyl = fd->currentType->secPerCyl;
   1787 	lp->d_secperunit = fd->currentType->secPerDisk;
   1788 	lp->d_interleave = fd->currentType->interleave;
   1789 	lp->d_trkseek = fd->currentType->stepRate;
   1790 
   1791 	strcpy(lp->d_typename, dktypenames[DTYPE_FLOPPY]);
   1792 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1793 
   1794 	lp->d_npartitions = fdType + 1;
   1795 	lp->d_partitions[fdType].p_offset = 0;
   1796 	lp->d_partitions[fdType].p_size = lp->d_secperunit;
   1797 	lp->d_partitions[fdType].p_fstype = FS_BSDFFS;
   1798 	lp->d_partitions[fdType].p_fsize = 512;
   1799 	lp->d_partitions[fdType].p_frag = 8;
   1800 
   1801 	lp->d_magic = DISKMAGIC;
   1802 	lp->d_magic2 = DISKMAGIC;
   1803 	lp->d_checksum = dkcksum(lp);
   1804 	/*
   1805 	 * Call the generic disklabel extraction routine.  If we don't
   1806 	 * find a label on disk, keep our faked one.
   1807 	 */
   1808 	if (TRACE_OPEN)
   1809 		printf(" now calling readdisklabel()...\n");
   1810 
   1811 	msg = readdisklabel(dev, fdstrategy, lp, clp);
   1812 	if (msg == NULL) {
   1813 		strncpy(lp->d_packname, "default label",
   1814 		    sizeof(lp->d_packname));	/* XXX - ?? */
   1815 	}
   1816 #ifdef IWM_DEBUG
   1817 	else
   1818 		printf("iwm: %s.\n", msg);
   1819 #endif
   1820 	if (TRACE_OPEN)
   1821 		fdPrintDiskLabel(lp);
   1822 }
   1823 
   1824 
   1825 
   1826 /*
   1827  * initCylinderCache
   1828  *
   1829  * Allocate cylinder cache and set up pointers to sectors.
   1830  */
   1831 static int
   1832 initCylinderCache(fd)
   1833 	fd_softc_t *fd;
   1834 {
   1835 	int i;
   1836 	int err;
   1837 	int secsize;
   1838 
   1839 	err = 0;
   1840 	secsize = fd->currentType->sectorSize;
   1841 	fd->cachedSide = 0;
   1842 
   1843 	fd->cbuf = (unsigned char *) malloc(IWM_MAX_GCR_SECTORS
   1844 	    * secsize, M_DEVBUF, M_WAITOK);
   1845 	if (NULL == fd->cbuf)
   1846 		err = ENOMEM;
   1847 	else
   1848 		for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
   1849 			fd->w_slots[i].valid = 0;
   1850 			fd->w_slots[i].secbuf = NULL;
   1851 
   1852 			fd->r_slots[i].valid = 0;
   1853 			fd->r_slots[i].secbuf = fd->cbuf + i * secsize;
   1854 		}
   1855 	return err;
   1856 }
   1857 
   1858 
   1859 /*
   1860  * invalidateCylinderCache
   1861  *
   1862  * Switching cylinders (tracks?) invalidates the read cache.
   1863  */
   1864 static void
   1865 invalidateCylinderCache(fd)
   1866 	fd_softc_t *fd;
   1867 {
   1868 	int i;
   1869 
   1870 	fd->cachedSide = 0;
   1871 	for (i=0; i < IWM_MAX_GCR_SECTORS; i++) {
   1872 		fd->r_slots[i].valid = 0;
   1873 	}
   1874 }
   1875 
   1876 
   1877 /*
   1878  * getFDType
   1879  *
   1880  * return pointer to disk format description
   1881  */
   1882 static fdInfo_t *
   1883 getFDType(unit)
   1884 	short unit;
   1885 {
   1886 	int driveFlags;
   1887 	fdInfo_t *thisType;
   1888 	extern fdInfo_t fdTypes[];
   1889 
   1890 	driveFlags = iwmCheckDrive(unit);
   1891 /*
   1892  * Drive flags are: Bit  0 - 1 = Drive is double sided
   1893  *			 1 - 1 = No disk inserted
   1894  *			 2 - 1 = Motor is off
   1895  *			 3 - 1 = Disk is writeable
   1896  *			 4 - 1 = Disk is DD (800/720K)
   1897  *			31 - 1 = No drive / invalid drive #
   1898  */
   1899 	if (TRACE_CONFIG) {
   1900 		printf("iwm: Drive %d says 0x0%x (%d)\n",
   1901 		    unit, driveFlags, driveFlags);
   1902 	}
   1903 	if (driveFlags < 0)
   1904 		thisType = NULL;/* no such drive	 */
   1905 	else
   1906 		if (driveFlags & 0x01)
   1907 			thisType = &fdTypes[1];	/* double sided		 */
   1908 		else
   1909 			thisType = &fdTypes[0];	/* single sided		 */
   1910 
   1911 	return thisType;
   1912 }
   1913 
   1914 
   1915 /*
   1916  * fdDeviceToType
   1917  *
   1918  * maps the minor device number (elsewhere: partition type) to
   1919  * a corresponding disk format.
   1920  * This is currently:
   1921  * 	fdXa	default (800K GCR)
   1922  *	fdXb	400K GCR
   1923  *	fdXc	800K GCR
   1924  */
   1925 static fdInfo_t *
   1926 fdDeviceToType(fd, dev)
   1927 	fd_softc_t *fd;
   1928 	dev_t dev;
   1929 {
   1930 	int type;
   1931 	fdInfo_t *thisInfo;
   1932 	/* XXX This broke with egcs 1.0.2 */
   1933 	/* extern fdInfo_t	fdTypes[]; */
   1934 
   1935 	type = minor(dev) % MAXPARTITIONS;	/* 1,2,... */
   1936 	if (type > sizeof(fdTypes) / sizeof(fdTypes[0]))
   1937 		thisInfo = NULL;
   1938 	else
   1939 		thisInfo = (type == 0) ? fd->defaultType : &fdTypes[type - 1];
   1940 	return thisInfo;
   1941 }
   1942 
   1943 
   1944 /*
   1945  * seek
   1946  *
   1947  * Step to given track; optionally restore to track zero before
   1948  * and/or verify correct track.
   1949  * Note that any necessary retries are done here.
   1950  * We keep the current position on disk in a 'struct diskPosition'.
   1951  */
   1952 static int
   1953 seek(fd, style)
   1954 	fd_softc_t *fd;
   1955 	int style;
   1956 {
   1957 	int state, done;
   1958 	int err, ierr;
   1959 	int steps;
   1960 
   1961 	diskPosition_t *loc;
   1962 	sectorHdr_t hdr;
   1963 	char action[32];
   1964 #ifndef _LKM
   1965 	iwm_softc_t *iwm = iwm_cd.cd_devs[0];
   1966 #endif
   1967 
   1968 	char *stateDesc[] = {
   1969 		"Init",
   1970 		"Seek",
   1971 		"Recalibrate",
   1972 		"Verify",
   1973 		"Exit"
   1974 	};
   1975 	enum {
   1976 		state_Init = 0,
   1977 		state_Seek,
   1978 		state_Recalibrate,
   1979 		state_Verify,
   1980 		state_Exit
   1981 	};
   1982 	/* XXX egcs */
   1983 	done = err = ierr = 0;
   1984 	fd->seekRetries = 0;
   1985 	fd->verifyRetries = 0;
   1986 
   1987 	loc = &fd->pos;
   1988 
   1989 	state = state_Init;
   1990 	do {
   1991 		if (TRACE_STEP)
   1992 			printf(" seek state %d [%s].\n",
   1993 			    state, stateDesc[state]);
   1994 		switch (state) {
   1995 
   1996 		case state_Init:
   1997 			if (TRACE_STEP)
   1998 				printf("Current track is %d, new track %d.\n",
   1999 				    loc->oldTrack, loc->track);
   2000 			memset(&hdr, 0, sizeof(hdr));
   2001 			err = ierr = 0;
   2002 			fd->seekRetries = 0;
   2003 			fd->verifyRetries = 0;
   2004 			state = (style == IWM_SEEK_RECAL)
   2005 			    ? state_Recalibrate : state_Seek;
   2006 			done = 0;
   2007 			break;
   2008 
   2009 		case state_Recalibrate:
   2010 			ierr = iwmTrack00();
   2011 			if (ierr == 0) {
   2012 				loc->oldTrack = 0;
   2013 				state = state_Seek;
   2014 			} else {
   2015 				strncpy(action, "Recalibrate (track 0)",
   2016 				    sizeof(action));
   2017 				state = state_Exit;
   2018 			}
   2019 			break;
   2020 
   2021 		case state_Seek:
   2022 			ierr = 0;
   2023 			steps = loc->track - loc->oldTrack;
   2024 
   2025 			if (steps != 0)
   2026 				ierr = iwmSeek(steps);
   2027 			if (ierr == 0) {
   2028 				/* No error or nothing to do */
   2029 				state = (style == IWM_SEEK_VERIFY)
   2030 				    ? state_Verify : state_Exit;
   2031 			} else {
   2032 				if (fd->seekRetries++ < iwm->maxRetries)
   2033 					state = state_Recalibrate;
   2034 				else {
   2035 					strncpy(action, "Seek retries",
   2036 					    sizeof(action));
   2037 					state = state_Exit;
   2038 				}
   2039 			}
   2040 			break;
   2041 
   2042 		case state_Verify:
   2043 			ierr = checkTrack(loc, TRACE_STEP);
   2044 			if (ierr == 0 && loc->track == hdr.track)
   2045 				state = state_Exit;
   2046 			else {
   2047 				if (fd->verifyRetries++ < iwm->maxRetries)
   2048 					state = state_Recalibrate;
   2049 				else {
   2050 					strncpy(action, "Verify retries",
   2051 					    sizeof(action));
   2052 					state = state_Exit;
   2053 				}
   2054 			}
   2055 			break;
   2056 
   2057 		case state_Exit:
   2058 			if (ierr == 0) {
   2059 				loc->oldTrack = loc->track;
   2060 				err = 0;
   2061 				/* Give the head some time to settle down */
   2062 				delay(3000);
   2063 			} else {
   2064 #ifdef DIAGNOSTIC
   2065 				printf(" seek() action \"%s\", err = %d.\n",
   2066 				    action, ierr);
   2067 #endif
   2068 				err = EIO;
   2069 			}
   2070 			done = 1;
   2071 			break;
   2072 		}
   2073 	} while (!done);
   2074 	return err;
   2075 }
   2076 
   2077 
   2078 /*
   2079  * checkTrack
   2080  *
   2081  * After positioning, get a sector header for validation
   2082  */
   2083 static int
   2084 checkTrack(loc, debugFlag)
   2085 	diskPosition_t *loc;
   2086 	int debugFlag;
   2087 {
   2088 	int spl;
   2089 	int iwmErr;
   2090 	sectorHdr_t hdr;
   2091 
   2092 	spl = spl6();
   2093 	iwmSelectSide(loc->side);
   2094 	iwmErr = iwmReadSectHdr(&hdr);
   2095 	splx(spl);
   2096 	if (debugFlag) {
   2097 		printf("Seeked for %d, got at %d, Hdr read err %d.\n",
   2098 		    loc->track, hdr.track, iwmErr);
   2099 	}
   2100 	return iwmErr;
   2101 }
   2102 
   2103 
   2104 /* Debugging stuff */
   2105 
   2106 static void
   2107 hexDump(buf, len)
   2108 	u_char *buf;
   2109 	int len;
   2110 {
   2111 	int i, j;
   2112 	u_char ch;
   2113 
   2114 	printf("\nDump %d from %p:\n", len, buf);
   2115 	i = j = 0;
   2116 	if (NULL != buf) do {
   2117 		printf("%04x: ", i);
   2118 		for (j = 0; j < 8; j++)
   2119 			printf("%02x ", buf[i + j]);
   2120 		printf(" ");
   2121 		for (j = 8; j < 16; j++)
   2122 			printf("%02x ", buf[i + j]);
   2123 		printf(" ");
   2124 		for (j = 0; j < 16; j++) {
   2125 			ch = buf[i + j];
   2126 			if (ch > 31 && ch < 127)
   2127 				printf("%c", ch);
   2128 			else
   2129 				printf(".");
   2130 		}
   2131 		printf("\n");
   2132 		i += 16;
   2133 	} while (len > i);
   2134 }
   2135 
   2136 
   2137 static void
   2138 fdPrintDiskLabel(lp)
   2139 	struct disklabel *lp;
   2140 {
   2141 	int i;
   2142 
   2143 	printf("iwm: Disklabel entries of current floppy.\n");
   2144 	printf("\t d_type:\t%d (%s)\n", lp->d_type,
   2145 	    dktypenames[lp->d_type]);
   2146 	printf("\t d_typename:\t%s\n", lp->d_typename);
   2147 	printf("\t d_packname:\t%s\n", lp->d_packname);
   2148 
   2149 	printf("\t d_secsize:\t%d\n", lp->d_secsize);
   2150 	printf("\t d_nsectors:\t%d\n", lp->d_nsectors);
   2151 	printf("\t d_ntracks:\t%d\n", lp->d_ntracks);
   2152 	printf("\t d_ncylinders:\t%d\n", lp->d_ncylinders);
   2153 	printf("\t d_secpercyl:\t%d\n", lp->d_secpercyl);
   2154 	printf("\t d_secperunit:\t%d\n", lp->d_secperunit);
   2155 
   2156 	printf("\t d_rpm: \t%d\n", lp->d_rpm);
   2157 	printf("\t d_interleave:\t%d\n", lp->d_interleave);
   2158 	printf("\t d_trkseek:\t%d [ms]\n", lp->d_trkseek);
   2159 
   2160 	printf(" d_npartitions:\t%d\n", lp->d_npartitions);
   2161 	for (i = 0; i < lp->d_npartitions; i++) {
   2162 		printf("\t d_partitions[%d].p_offset:\t%d\n", i,
   2163 		    lp->d_partitions[i].p_offset);
   2164 		printf("\t d_partitions[%d].p_size:\t%d\n", i,
   2165 		    lp->d_partitions[i].p_size);
   2166 		printf("\t d_partitions[%d].p_fstype:\t%d (%s)\n", i,
   2167 		    lp->d_partitions[i].p_fstype,
   2168 		    fstypenames[lp->d_partitions[i].p_fstype]);
   2169 		printf("\t d_partitions[%d].p_frag:\t%d\n", i,
   2170 		    lp->d_partitions[i].p_frag);
   2171 		printf("\n");
   2172 	}
   2173 }
   2174