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