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