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