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