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