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