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