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