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