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