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