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