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