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