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