fd.c revision 1.48.4.8 1 1.48.4.8 thorpej /* $NetBSD: fd.c,v 1.48.4.8 2003/01/07 20:56:19 thorpej Exp $ */
2 1.48.4.2 nathanw
3 1.48.4.2 nathanw /*
4 1.48.4.2 nathanw * Copyright (c) 1994 Christian E. Hopps
5 1.48.4.2 nathanw * Copyright (c) 1996 Ezra Story
6 1.48.4.2 nathanw * All rights reserved.
7 1.48.4.2 nathanw *
8 1.48.4.2 nathanw * Redistribution and use in source and binary forms, with or without
9 1.48.4.2 nathanw * modification, are permitted provided that the following conditions
10 1.48.4.2 nathanw * are met:
11 1.48.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
12 1.48.4.2 nathanw * notice, this list of conditions and the following disclaimer.
13 1.48.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
14 1.48.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
15 1.48.4.2 nathanw * documentation and/or other materials provided with the distribution.
16 1.48.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
17 1.48.4.2 nathanw * must display the following acknowledgement:
18 1.48.4.2 nathanw * This product includes software developed by Christian E. Hopps.
19 1.48.4.2 nathanw * This product includes software developed by Ezra Story.
20 1.48.4.2 nathanw * 4. The name of the author may not be used to endorse or promote products
21 1.48.4.2 nathanw * derived from this software without specific prior written permission
22 1.48.4.2 nathanw *
23 1.48.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.48.4.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.48.4.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.48.4.2 nathanw * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.48.4.2 nathanw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.48.4.2 nathanw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.48.4.2 nathanw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.48.4.2 nathanw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.48.4.2 nathanw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.48.4.2 nathanw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.48.4.2 nathanw */
34 1.48.4.2 nathanw
35 1.48.4.2 nathanw #include <sys/cdefs.h>
36 1.48.4.8 thorpej __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.48.4.8 2003/01/07 20:56:19 thorpej Exp $");
37 1.48.4.2 nathanw
38 1.48.4.2 nathanw #include <sys/param.h>
39 1.48.4.2 nathanw #include <sys/systm.h>
40 1.48.4.2 nathanw #include <sys/callout.h>
41 1.48.4.2 nathanw #include <sys/kernel.h>
42 1.48.4.2 nathanw #include <sys/malloc.h>
43 1.48.4.2 nathanw #include <sys/buf.h>
44 1.48.4.2 nathanw #include <sys/device.h>
45 1.48.4.2 nathanw #include <sys/ioctl.h>
46 1.48.4.2 nathanw #include <sys/fcntl.h>
47 1.48.4.2 nathanw #include <sys/disklabel.h>
48 1.48.4.2 nathanw #include <sys/disk.h>
49 1.48.4.2 nathanw #include <sys/dkbad.h>
50 1.48.4.2 nathanw #include <sys/proc.h>
51 1.48.4.4 nathanw #include <sys/conf.h>
52 1.48.4.2 nathanw #include <machine/cpu.h>
53 1.48.4.2 nathanw #include <amiga/amiga/device.h>
54 1.48.4.2 nathanw #include <amiga/amiga/custom.h>
55 1.48.4.2 nathanw #include <amiga/amiga/cia.h>
56 1.48.4.2 nathanw #include <amiga/amiga/cc.h>
57 1.48.4.2 nathanw
58 1.48.4.2 nathanw #include "locators.h"
59 1.48.4.2 nathanw
60 1.48.4.2 nathanw enum fdc_bits { FDB_CHANGED = 2, FDB_PROTECT, FDB_CYLZERO, FDB_READY };
61 1.48.4.2 nathanw /*
62 1.48.4.2 nathanw * partitions in fd represent different format floppies
63 1.48.4.2 nathanw * partition a is 0 etc..
64 1.48.4.2 nathanw */
65 1.48.4.2 nathanw enum fd_parttypes {
66 1.48.4.2 nathanw FDAMIGAPART = 0,
67 1.48.4.2 nathanw FDMSDOSPART,
68 1.48.4.2 nathanw FDMAXPARTS
69 1.48.4.2 nathanw };
70 1.48.4.2 nathanw
71 1.48.4.2 nathanw #define FDBBSIZE (8192)
72 1.48.4.2 nathanw #define FDSBSIZE (8192)
73 1.48.4.2 nathanw
74 1.48.4.2 nathanw #define FDUNIT(dev) DISKUNIT(dev)
75 1.48.4.2 nathanw #define FDPART(dev) DISKPART(dev)
76 1.48.4.2 nathanw #define FDMAKEDEV(m, u, p) MAKEDISKDEV((m), (u), (p))
77 1.48.4.2 nathanw
78 1.48.4.2 nathanw /* that's nice, but we don't want to always use this as an amiga drive
79 1.48.4.2 nathanw bunghole :-) */
80 1.48.4.2 nathanw #define FDNHEADS (2) /* amiga drives always have 2 heads */
81 1.48.4.2 nathanw #define FDSECSIZE (512) /* amiga drives always have 512 byte sectors */
82 1.48.4.2 nathanw #define FDSECLWORDS (128)
83 1.48.4.2 nathanw
84 1.48.4.2 nathanw #define FDSETTLEDELAY (18000) /* usec delay after seeking after switch dir */
85 1.48.4.2 nathanw #define FDSTEPDELAY (3500) /* usec delay after steping */
86 1.48.4.2 nathanw #define FDPRESIDEDELAY (1000) /* usec delay before writing can occur */
87 1.48.4.2 nathanw #define FDWRITEDELAY (1300) /* usec delay after write */
88 1.48.4.2 nathanw
89 1.48.4.2 nathanw #define FDSTEPOUT (1) /* decrease track step */
90 1.48.4.2 nathanw #define FDSTEPIN (0) /* increase track step */
91 1.48.4.2 nathanw
92 1.48.4.2 nathanw #define FDCUNITMASK (0x78) /* mask for all units (bits 6-3) */
93 1.48.4.2 nathanw
94 1.48.4.2 nathanw #define FDRETRIES (2) /* default number of retries */
95 1.48.4.2 nathanw #define FDMAXUNITS (4) /* maximum number of supported units */
96 1.48.4.2 nathanw
97 1.48.4.2 nathanw #define DISKLEN_READ (0) /* fake mask for reading */
98 1.48.4.2 nathanw #define DISKLEN_WRITE (1 << 14) /* bit for writing */
99 1.48.4.2 nathanw #define DISKLEN_DMAEN (1 << 15) /* dma go */
100 1.48.4.2 nathanw #define DMABUFSZ ((DISKLEN_WRITE - 1) * 2) /* largest dma possible */
101 1.48.4.2 nathanw
102 1.48.4.2 nathanw #define FDMFMSYNC (0x4489)
103 1.48.4.2 nathanw #define FDMFMID (0x5554)
104 1.48.4.2 nathanw #define FDMFMDATA (0x5545)
105 1.48.4.2 nathanw #define FDMFMGAP1 (0x9254)
106 1.48.4.2 nathanw #define FDMFMGAP2 (0xAAAA)
107 1.48.4.2 nathanw #define FDMFMGAP3 (0x9254)
108 1.48.4.2 nathanw #define CRC16POLY (0x1021) /* (x^16) + x^12 + x^5 + x^0 */
109 1.48.4.2 nathanw
110 1.48.4.2 nathanw /*
111 1.48.4.2 nathanw * Msdos-type MFM encode/decode
112 1.48.4.2 nathanw */
113 1.48.4.2 nathanw static u_char msdecode[128];
114 1.48.4.2 nathanw static u_char msencode[16] =
115 1.48.4.2 nathanw {
116 1.48.4.2 nathanw 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
117 1.48.4.2 nathanw 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
118 1.48.4.2 nathanw };
119 1.48.4.2 nathanw static u_short mscrctab[256];
120 1.48.4.2 nathanw
121 1.48.4.2 nathanw /*
122 1.48.4.2 nathanw 5554 aaaa aaaa aaa5 2aa4 4452 aa51
123 1.48.4.2 nathanw 00 00 03 02 ac 0d
124 1.48.4.2 nathanw */
125 1.48.4.2 nathanw
126 1.48.4.2 nathanw /*
127 1.48.4.2 nathanw * floppy device type
128 1.48.4.2 nathanw */
129 1.48.4.2 nathanw struct fdtype {
130 1.48.4.2 nathanw u_int driveid; /* drive identification (from drive) */
131 1.48.4.2 nathanw u_int ncylinders; /* number of cylinders on drive */
132 1.48.4.2 nathanw u_int amiga_nsectors; /* number of sectors per amiga track */
133 1.48.4.2 nathanw u_int msdos_nsectors; /* number of sectors per msdos track */
134 1.48.4.2 nathanw u_int nreadw; /* number of words (short) read per track */
135 1.48.4.2 nathanw u_int nwritew; /* number of words (short) written per track */
136 1.48.4.2 nathanw u_int gap; /* track gap size in long words */
137 1.48.4.2 nathanw u_int precomp[2]; /* 1st and 2nd precomp values */
138 1.48.4.2 nathanw char *desc; /* description of drive type (useq) */
139 1.48.4.2 nathanw };
140 1.48.4.2 nathanw
141 1.48.4.2 nathanw /*
142 1.48.4.2 nathanw * floppy disk device data
143 1.48.4.2 nathanw */
144 1.48.4.2 nathanw struct fd_softc {
145 1.48.4.2 nathanw struct device sc_dv; /* generic device info; must come first */
146 1.48.4.2 nathanw struct disk dkdev; /* generic disk info */
147 1.48.4.3 nathanw struct bufq_state bufq; /* queue pending I/O operations */
148 1.48.4.2 nathanw struct buf curbuf; /* state of current I/O operation */
149 1.48.4.2 nathanw struct callout calibrate_ch;
150 1.48.4.2 nathanw struct callout motor_ch;
151 1.48.4.2 nathanw struct fdtype *type;
152 1.48.4.2 nathanw void *cachep; /* cached track data (write through) */
153 1.48.4.2 nathanw int cachetrk; /* cahced track -1 for none */
154 1.48.4.2 nathanw int hwunit; /* unit for amiga controlling hw */
155 1.48.4.2 nathanw int unitmask; /* mask for cia select deslect */
156 1.48.4.2 nathanw int pstepdir; /* previous step direction */
157 1.48.4.2 nathanw int curcyl; /* current curcyl head positioned on */
158 1.48.4.2 nathanw int flags; /* misc flags */
159 1.48.4.2 nathanw int wlabel;
160 1.48.4.2 nathanw int stepdelay; /* useq to delay after seek user setable */
161 1.48.4.2 nathanw int nsectors; /* number of sectors per track */
162 1.48.4.2 nathanw int openpart; /* which partition [ab] == [12] is open */
163 1.48.4.2 nathanw short retries; /* number of times to retry failed io */
164 1.48.4.2 nathanw short retried; /* number of times current io retried */
165 1.48.4.2 nathanw int bytespersec; /* number of bytes per sector */
166 1.48.4.2 nathanw };
167 1.48.4.2 nathanw
168 1.48.4.2 nathanw /* fd_softc->flags */
169 1.48.4.2 nathanw #define FDF_MOTORON (0x01) /* motor is running */
170 1.48.4.2 nathanw #define FDF_MOTOROFF (0x02) /* motor is waiting to be turned off */
171 1.48.4.2 nathanw #define FDF_WMOTOROFF (0x04) /* unit wants a wakeup after off */
172 1.48.4.2 nathanw #define FDF_DIRTY (0x08) /* track cache needs write */
173 1.48.4.2 nathanw #define FDF_WRITEWAIT (0x10) /* need to head select delay on next setpos */
174 1.48.4.2 nathanw #define FDF_HAVELABEL (0x20) /* label is valid */
175 1.48.4.2 nathanw #define FDF_JUSTFLUSH (0x40) /* don't bother caching track. */
176 1.48.4.2 nathanw #define FDF_NOTRACK0 (0x80) /* was not able to recalibrate drive */
177 1.48.4.2 nathanw
178 1.48.4.2 nathanw int fdc_wantwakeup;
179 1.48.4.2 nathanw int fdc_side;
180 1.48.4.2 nathanw void *fdc_dmap;
181 1.48.4.2 nathanw struct fd_softc *fdc_indma;
182 1.48.4.2 nathanw int fdc_dmalen;
183 1.48.4.2 nathanw int fdc_dmawrite;
184 1.48.4.2 nathanw
185 1.48.4.2 nathanw struct fdcargs {
186 1.48.4.2 nathanw struct fdtype *type;
187 1.48.4.2 nathanw int unit;
188 1.48.4.2 nathanw };
189 1.48.4.2 nathanw
190 1.48.4.2 nathanw int fdcmatch(struct device *, struct cfdata *, void *);
191 1.48.4.2 nathanw void fdcattach(struct device *, struct device *, void *);
192 1.48.4.2 nathanw int fdcprint(void *, const char *);
193 1.48.4.2 nathanw int fdmatch(struct device *, struct cfdata *, void *);
194 1.48.4.2 nathanw void fdattach(struct device *, struct device *, void *);
195 1.48.4.2 nathanw
196 1.48.4.2 nathanw void fdintr(int);
197 1.48.4.2 nathanw void fdidxintr(void);
198 1.48.4.2 nathanw int fdloaddisk(struct fd_softc *);
199 1.48.4.2 nathanw void fdgetdefaultlabel(struct fd_softc *, struct disklabel *, int);
200 1.48.4.2 nathanw int fdgetdisklabel(struct fd_softc *, dev_t);
201 1.48.4.2 nathanw int fdsetdisklabel(struct fd_softc *, struct disklabel *);
202 1.48.4.2 nathanw int fdputdisklabel(struct fd_softc *, dev_t);
203 1.48.4.2 nathanw struct fdtype * fdcgetfdtype(int);
204 1.48.4.2 nathanw void fdmotoroff(void *);
205 1.48.4.2 nathanw void fdsetpos(struct fd_softc *, int, int);
206 1.48.4.2 nathanw void fdselunit(struct fd_softc *);
207 1.48.4.2 nathanw void fdstart(struct fd_softc *);
208 1.48.4.2 nathanw void fdcont(struct fd_softc *);
209 1.48.4.2 nathanw void fddmastart(struct fd_softc *, int);
210 1.48.4.2 nathanw void fdcalibrate(void *);
211 1.48.4.2 nathanw void fddmadone(struct fd_softc *, int);
212 1.48.4.2 nathanw void fddone(struct fd_softc *);
213 1.48.4.2 nathanw void fdfindwork(int);
214 1.48.4.2 nathanw void fdminphys(struct buf *);
215 1.48.4.2 nathanw void fdcachetoraw(struct fd_softc *);
216 1.48.4.2 nathanw void amcachetoraw(struct fd_softc *);
217 1.48.4.2 nathanw int amrawtocache(struct fd_softc *);
218 1.48.4.2 nathanw u_long *fdfindsync(u_long *, u_long *);
219 1.48.4.2 nathanw int fdrawtocache(struct fd_softc *);
220 1.48.4.2 nathanw void mscachetoraw(struct fd_softc *);
221 1.48.4.2 nathanw int msrawtocache(struct fd_softc *);
222 1.48.4.2 nathanw u_long *mfmblkencode(u_long *, u_long *, u_long *, int);
223 1.48.4.2 nathanw u_long *mfmblkdecode(u_long *, u_long *, u_long *, int);
224 1.48.4.2 nathanw u_short *msblkdecode(u_short *, u_char *, int);
225 1.48.4.2 nathanw u_short *msblkencode(u_short *, u_char *, int, u_short *);
226 1.48.4.2 nathanw
227 1.48.4.2 nathanw /*
228 1.48.4.2 nathanw * read size is (nsectors + 1) * mfm secsize + gap bytes + 2 shorts
229 1.48.4.2 nathanw * write size is nsectors * mfm secsize + gap bytes + 3 shorts
230 1.48.4.2 nathanw * the extra shorts are to deal with a dma hw bug in the controller
231 1.48.4.2 nathanw * they are probably too much (I belive the bug is 1 short on write and
232 1.48.4.2 nathanw * 3 bits on read) but there is no need to be cheap here.
233 1.48.4.2 nathanw */
234 1.48.4.2 nathanw #define MAXTRKSZ (22 * FDSECSIZE)
235 1.48.4.2 nathanw struct fdtype fdtype[] = {
236 1.48.4.2 nathanw { 0x00000000, 80, 11, 9, 7358, 6815, 414, { 80, 161 }, "3.5dd" },
237 1.48.4.2 nathanw { 0x55555555, 40, 11, 9, 7358, 6815, 414, { 80, 161 }, "5.25dd" },
238 1.48.4.2 nathanw { 0xAAAAAAAA, 80, 22, 18, 14716, 13630, 828, { 80, 161 }, "3.5hd" }
239 1.48.4.2 nathanw };
240 1.48.4.2 nathanw int nfdtype = sizeof(fdtype) / sizeof(*fdtype);
241 1.48.4.2 nathanw
242 1.48.4.5 nathanw CFATTACH_DECL(fd, sizeof(struct fd_softc),
243 1.48.4.5 nathanw fdmatch, fdattach, NULL, NULL);
244 1.48.4.2 nathanw
245 1.48.4.2 nathanw extern struct cfdriver fd_cd;
246 1.48.4.2 nathanw
247 1.48.4.4 nathanw dev_type_open(fdopen);
248 1.48.4.4 nathanw dev_type_close(fdclose);
249 1.48.4.4 nathanw dev_type_read(fdread);
250 1.48.4.4 nathanw dev_type_write(fdwrite);
251 1.48.4.4 nathanw dev_type_ioctl(fdioctl);
252 1.48.4.4 nathanw dev_type_strategy(fdstrategy);
253 1.48.4.4 nathanw
254 1.48.4.4 nathanw const struct bdevsw fd_bdevsw = {
255 1.48.4.4 nathanw fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
256 1.48.4.4 nathanw };
257 1.48.4.4 nathanw
258 1.48.4.4 nathanw const struct cdevsw fd_cdevsw = {
259 1.48.4.4 nathanw fdopen, fdclose, fdread, fdwrite, fdioctl,
260 1.48.4.6 nathanw nostop, notty, nopoll, nommap, nokqfilter, D_DISK
261 1.48.4.4 nathanw };
262 1.48.4.4 nathanw
263 1.48.4.4 nathanw struct dkdriver fddkdriver = { fdstrategy };
264 1.48.4.4 nathanw
265 1.48.4.5 nathanw CFATTACH_DECL(fdc, sizeof(struct device),
266 1.48.4.5 nathanw fdcmatch, fdcattach, NULL, NULL);
267 1.48.4.2 nathanw
268 1.48.4.2 nathanw /*
269 1.48.4.2 nathanw * all hw access through macros, this helps to hide the active low
270 1.48.4.2 nathanw * properties
271 1.48.4.2 nathanw */
272 1.48.4.2 nathanw
273 1.48.4.2 nathanw #define FDUNITMASK(unit) (1 << (3 + (unit)))
274 1.48.4.2 nathanw
275 1.48.4.2 nathanw /*
276 1.48.4.2 nathanw * select units using mask
277 1.48.4.2 nathanw */
278 1.48.4.2 nathanw #define FDSELECT(um) do { ciab.prb &= ~(um); } while (0)
279 1.48.4.2 nathanw
280 1.48.4.2 nathanw /*
281 1.48.4.2 nathanw * deselect units using mask
282 1.48.4.2 nathanw */
283 1.48.4.2 nathanw #define FDDESELECT(um) do { ciab.prb |= (um); delay(1); } while (0)
284 1.48.4.2 nathanw
285 1.48.4.2 nathanw /*
286 1.48.4.2 nathanw * test hw condition bits
287 1.48.4.2 nathanw */
288 1.48.4.2 nathanw #define FDTESTC(bit) ((ciaa.pra & (1 << (bit))) == 0)
289 1.48.4.2 nathanw
290 1.48.4.2 nathanw /*
291 1.48.4.2 nathanw * set motor for select units, true motor on else off
292 1.48.4.2 nathanw */
293 1.48.4.2 nathanw #define FDSETMOTOR(on) do { \
294 1.48.4.2 nathanw if (on) ciab.prb &= ~CIAB_PRB_MTR; else ciab.prb |= CIAB_PRB_MTR; \
295 1.48.4.2 nathanw } while (0)
296 1.48.4.2 nathanw
297 1.48.4.2 nathanw /*
298 1.48.4.2 nathanw * set head for select units
299 1.48.4.2 nathanw */
300 1.48.4.2 nathanw #define FDSETHEAD(head) do { \
301 1.48.4.2 nathanw if (head) ciab.prb &= ~CIAB_PRB_SIDE; else ciab.prb |= CIAB_PRB_SIDE; \
302 1.48.4.2 nathanw delay(1); } while (0)
303 1.48.4.2 nathanw
304 1.48.4.2 nathanw /*
305 1.48.4.2 nathanw * select direction, true towards spindle else outwards
306 1.48.4.2 nathanw */
307 1.48.4.2 nathanw #define FDSETDIR(in) do { \
308 1.48.4.2 nathanw if (in) ciab.prb &= ~CIAB_PRB_DIR; else ciab.prb |= CIAB_PRB_DIR; \
309 1.48.4.2 nathanw delay(1); } while (0)
310 1.48.4.2 nathanw
311 1.48.4.2 nathanw /*
312 1.48.4.2 nathanw * step the selected units
313 1.48.4.2 nathanw */
314 1.48.4.2 nathanw #define FDSTEP do { \
315 1.48.4.2 nathanw ciab.prb &= ~CIAB_PRB_STEP; ciab.prb |= CIAB_PRB_STEP; \
316 1.48.4.2 nathanw } while (0)
317 1.48.4.2 nathanw
318 1.48.4.2 nathanw #define FDDMASTART(len, towrite) do { \
319 1.48.4.2 nathanw int dmasz = (len) | ((towrite) ? DISKLEN_WRITE : 0) | DISKLEN_DMAEN; \
320 1.48.4.2 nathanw custom.dsklen = dmasz; custom.dsklen = dmasz; } while (0)
321 1.48.4.2 nathanw
322 1.48.4.2 nathanw #define FDDMASTOP do { custom.dsklen = 0; } while (0)
323 1.48.4.2 nathanw
324 1.48.4.2 nathanw
325 1.48.4.2 nathanw int
326 1.48.4.2 nathanw fdcmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
327 1.48.4.2 nathanw {
328 1.48.4.2 nathanw static int fdc_matched = 0;
329 1.48.4.2 nathanw
330 1.48.4.2 nathanw /* Allow only once instance. */
331 1.48.4.2 nathanw if (matchname("fdc", auxp) == 0 || fdc_matched)
332 1.48.4.2 nathanw return(0);
333 1.48.4.2 nathanw if ((fdc_dmap = alloc_chipmem(DMABUFSZ)) == NULL) {
334 1.48.4.2 nathanw printf("fdc: unable to allocate dma buffer\n");
335 1.48.4.2 nathanw return(0);
336 1.48.4.2 nathanw }
337 1.48.4.2 nathanw
338 1.48.4.2 nathanw fdc_matched = 1;
339 1.48.4.2 nathanw return(1);
340 1.48.4.2 nathanw }
341 1.48.4.2 nathanw
342 1.48.4.2 nathanw void
343 1.48.4.2 nathanw fdcattach(struct device *pdp, struct device *dp, void *auxp)
344 1.48.4.2 nathanw {
345 1.48.4.2 nathanw struct fdcargs args;
346 1.48.4.2 nathanw
347 1.48.4.2 nathanw printf(": dmabuf pa 0x%x", kvtop(fdc_dmap));
348 1.48.4.2 nathanw printf(": dmabuf ka %p\n", fdc_dmap);
349 1.48.4.2 nathanw args.unit = 0;
350 1.48.4.2 nathanw args.type = fdcgetfdtype(args.unit);
351 1.48.4.2 nathanw
352 1.48.4.2 nathanw fdc_side = -1;
353 1.48.4.2 nathanw config_found(dp, &args, fdcprint);
354 1.48.4.2 nathanw for (args.unit++; args.unit < FDMAXUNITS; args.unit++) {
355 1.48.4.2 nathanw if ((args.type = fdcgetfdtype(args.unit)) == NULL)
356 1.48.4.2 nathanw continue;
357 1.48.4.2 nathanw config_found(dp, &args, fdcprint);
358 1.48.4.2 nathanw }
359 1.48.4.2 nathanw }
360 1.48.4.2 nathanw
361 1.48.4.2 nathanw int
362 1.48.4.2 nathanw fdcprint(void *auxp, const char *pnp)
363 1.48.4.2 nathanw {
364 1.48.4.2 nathanw struct fdcargs *fcp;
365 1.48.4.2 nathanw
366 1.48.4.2 nathanw fcp = auxp;
367 1.48.4.2 nathanw if (pnp)
368 1.48.4.7 thorpej aprint_normal("fd%d at %s unit %d:", fcp->unit, pnp,
369 1.48.4.2 nathanw fcp->type->driveid);
370 1.48.4.2 nathanw return(UNCONF);
371 1.48.4.2 nathanw }
372 1.48.4.2 nathanw
373 1.48.4.2 nathanw /*ARGSUSED*/
374 1.48.4.2 nathanw int
375 1.48.4.2 nathanw fdmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
376 1.48.4.2 nathanw {
377 1.48.4.2 nathanw struct fdcargs *fdap;
378 1.48.4.2 nathanw
379 1.48.4.2 nathanw fdap = auxp;
380 1.48.4.2 nathanw if (cfp->cf_loc[FDCCF_UNIT] == fdap->unit ||
381 1.48.4.2 nathanw cfp->cf_loc[FDCCF_UNIT] == FDCCF_UNIT_DEFAULT)
382 1.48.4.2 nathanw return(1);
383 1.48.4.2 nathanw
384 1.48.4.2 nathanw return(0);
385 1.48.4.2 nathanw }
386 1.48.4.2 nathanw
387 1.48.4.2 nathanw void
388 1.48.4.2 nathanw fdattach(struct device *pdp, struct device *dp, void *auxp)
389 1.48.4.2 nathanw {
390 1.48.4.2 nathanw struct fdcargs *ap;
391 1.48.4.2 nathanw struct fd_softc *sc;
392 1.48.4.2 nathanw int i;
393 1.48.4.2 nathanw
394 1.48.4.2 nathanw ap = auxp;
395 1.48.4.2 nathanw sc = (struct fd_softc *)dp;
396 1.48.4.2 nathanw
397 1.48.4.3 nathanw bufq_alloc(&sc->bufq, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
398 1.48.4.2 nathanw callout_init(&sc->calibrate_ch);
399 1.48.4.2 nathanw callout_init(&sc->motor_ch);
400 1.48.4.2 nathanw
401 1.48.4.2 nathanw sc->curcyl = sc->cachetrk = -1;
402 1.48.4.2 nathanw sc->openpart = -1;
403 1.48.4.2 nathanw sc->type = ap->type;
404 1.48.4.2 nathanw sc->hwunit = ap->unit;
405 1.48.4.2 nathanw sc->unitmask = 1 << (3 + ap->unit);
406 1.48.4.2 nathanw sc->retries = FDRETRIES;
407 1.48.4.2 nathanw sc->stepdelay = FDSTEPDELAY;
408 1.48.4.2 nathanw sc->bytespersec = 512;
409 1.48.4.2 nathanw printf(" unit %d: %s %d cyl, %d head, %d sec [%d sec], 512 bytes/sec\n",
410 1.48.4.2 nathanw sc->hwunit, sc->type->desc, sc->type->ncylinders, FDNHEADS,
411 1.48.4.2 nathanw sc->type->amiga_nsectors, sc->type->msdos_nsectors);
412 1.48.4.2 nathanw
413 1.48.4.2 nathanw /*
414 1.48.4.2 nathanw * Initialize and attach the disk structure.
415 1.48.4.2 nathanw */
416 1.48.4.2 nathanw sc->dkdev.dk_name = sc->sc_dv.dv_xname;
417 1.48.4.2 nathanw sc->dkdev.dk_driver = &fddkdriver;
418 1.48.4.2 nathanw disk_attach(&sc->dkdev);
419 1.48.4.2 nathanw
420 1.48.4.2 nathanw /*
421 1.48.4.2 nathanw * calibrate the drive
422 1.48.4.2 nathanw */
423 1.48.4.2 nathanw fdsetpos(sc, 0, 0);
424 1.48.4.2 nathanw fdsetpos(sc, sc->type->ncylinders, 0);
425 1.48.4.2 nathanw fdsetpos(sc, 0, 0);
426 1.48.4.2 nathanw fdmotoroff(sc);
427 1.48.4.2 nathanw
428 1.48.4.2 nathanw /*
429 1.48.4.2 nathanw * precalc msdos MFM and CRC
430 1.48.4.2 nathanw */
431 1.48.4.2 nathanw for (i = 0; i < 128; i++)
432 1.48.4.2 nathanw msdecode[i] = 0xff;
433 1.48.4.2 nathanw for (i = 0; i < 16; i++)
434 1.48.4.2 nathanw msdecode[msencode[i]] = i;
435 1.48.4.2 nathanw for (i = 0; i < 256; i++) {
436 1.48.4.2 nathanw mscrctab[i] = (0x1021 * (i & 0xf0)) ^ (0x1021 * (i & 0x0f)) ^
437 1.48.4.2 nathanw (0x1021 * (i >> 4));
438 1.48.4.2 nathanw }
439 1.48.4.2 nathanw
440 1.48.4.2 nathanw /*
441 1.48.4.2 nathanw * enable disk related interrupts
442 1.48.4.2 nathanw */
443 1.48.4.2 nathanw custom.dmacon = DMAF_SETCLR | DMAF_MASTER | DMAF_DISK;
444 1.48.4.2 nathanw custom.intena = INTF_SETCLR | INTF_DSKBLK;
445 1.48.4.2 nathanw ciab.icr = CIA_ICR_FLG;
446 1.48.4.2 nathanw }
447 1.48.4.2 nathanw
448 1.48.4.2 nathanw /*ARGSUSED*/
449 1.48.4.2 nathanw int
450 1.48.4.2 nathanw fdopen(dev_t dev, int flags, int devtype, struct proc *p)
451 1.48.4.2 nathanw {
452 1.48.4.2 nathanw struct fd_softc *sc;
453 1.48.4.2 nathanw int wasopen, fwork, error, s;
454 1.48.4.2 nathanw
455 1.48.4.2 nathanw error = 0;
456 1.48.4.2 nathanw
457 1.48.4.2 nathanw if (FDPART(dev) >= FDMAXPARTS)
458 1.48.4.2 nathanw return(ENXIO);
459 1.48.4.2 nathanw
460 1.48.4.2 nathanw if ((sc = getsoftc(fd_cd, FDUNIT(dev))) == NULL)
461 1.48.4.2 nathanw return(ENXIO);
462 1.48.4.2 nathanw if (sc->flags & FDF_NOTRACK0)
463 1.48.4.2 nathanw return(ENXIO);
464 1.48.4.2 nathanw if (sc->cachep == NULL)
465 1.48.4.2 nathanw sc->cachep = malloc(MAXTRKSZ, M_DEVBUF, M_WAITOK);
466 1.48.4.2 nathanw
467 1.48.4.2 nathanw s = splbio();
468 1.48.4.2 nathanw /*
469 1.48.4.2 nathanw * if we are sleeping in fdclose(); waiting for a chance to
470 1.48.4.2 nathanw * shut the motor off, do a sleep here also.
471 1.48.4.2 nathanw */
472 1.48.4.2 nathanw while (sc->flags & FDF_WMOTOROFF)
473 1.48.4.2 nathanw tsleep(fdmotoroff, PRIBIO, "fdopen", 0);
474 1.48.4.2 nathanw
475 1.48.4.2 nathanw fwork = 0;
476 1.48.4.2 nathanw /*
477 1.48.4.2 nathanw * if not open let user open request type, otherwise
478 1.48.4.2 nathanw * ensure they are trying to open same type.
479 1.48.4.2 nathanw */
480 1.48.4.2 nathanw if (sc->openpart == FDPART(dev))
481 1.48.4.2 nathanw wasopen = 1;
482 1.48.4.2 nathanw else if (sc->openpart == -1) {
483 1.48.4.2 nathanw sc->openpart = FDPART(dev);
484 1.48.4.2 nathanw wasopen = 0;
485 1.48.4.2 nathanw } else {
486 1.48.4.2 nathanw wasopen = 1;
487 1.48.4.2 nathanw error = EPERM;
488 1.48.4.2 nathanw goto done;
489 1.48.4.2 nathanw }
490 1.48.4.2 nathanw
491 1.48.4.2 nathanw /*
492 1.48.4.2 nathanw * wait for current io to complete if any
493 1.48.4.2 nathanw */
494 1.48.4.2 nathanw if (fdc_indma) {
495 1.48.4.2 nathanw fwork = 1;
496 1.48.4.2 nathanw fdc_wantwakeup++;
497 1.48.4.2 nathanw tsleep(fdopen, PRIBIO, "fdopen", 0);
498 1.48.4.2 nathanw }
499 1.48.4.2 nathanw if ((error = fdloaddisk(sc)) != 0)
500 1.48.4.2 nathanw goto done;
501 1.48.4.2 nathanw if ((error = fdgetdisklabel(sc, dev)) != 0)
502 1.48.4.2 nathanw goto done;
503 1.48.4.2 nathanw #ifdef FDDEBUG
504 1.48.4.2 nathanw printf(" open successful\n");
505 1.48.4.2 nathanw #endif
506 1.48.4.2 nathanw done:
507 1.48.4.2 nathanw /*
508 1.48.4.2 nathanw * if we requested that fddone()->fdfindwork() wake us, allow it to
509 1.48.4.2 nathanw * complete its job now
510 1.48.4.2 nathanw */
511 1.48.4.2 nathanw if (fwork)
512 1.48.4.2 nathanw fdfindwork(FDUNIT(dev));
513 1.48.4.2 nathanw splx(s);
514 1.48.4.2 nathanw
515 1.48.4.2 nathanw /*
516 1.48.4.2 nathanw * if we were not open and we marked us so reverse that.
517 1.48.4.2 nathanw */
518 1.48.4.2 nathanw if (error && wasopen == 0)
519 1.48.4.2 nathanw sc->openpart = -1;
520 1.48.4.2 nathanw return(error);
521 1.48.4.2 nathanw }
522 1.48.4.2 nathanw
523 1.48.4.2 nathanw /*ARGSUSED*/
524 1.48.4.2 nathanw int
525 1.48.4.2 nathanw fdclose(dev_t dev, int flags, int devtype, struct proc *p)
526 1.48.4.2 nathanw {
527 1.48.4.2 nathanw struct fd_softc *sc;
528 1.48.4.2 nathanw int s;
529 1.48.4.2 nathanw
530 1.48.4.2 nathanw #ifdef FDDEBUG
531 1.48.4.2 nathanw printf("fdclose()\n");
532 1.48.4.2 nathanw #endif
533 1.48.4.2 nathanw sc = getsoftc(fd_cd, FDUNIT(dev));
534 1.48.4.2 nathanw s = splbio();
535 1.48.4.2 nathanw if (sc->flags & FDF_MOTORON) {
536 1.48.4.2 nathanw sc->flags |= FDF_WMOTOROFF;
537 1.48.4.2 nathanw tsleep(fdmotoroff, PRIBIO, "fdclose", 0);
538 1.48.4.2 nathanw sc->flags &= ~FDF_WMOTOROFF;
539 1.48.4.2 nathanw wakeup(fdmotoroff);
540 1.48.4.2 nathanw }
541 1.48.4.2 nathanw sc->openpart = -1;
542 1.48.4.2 nathanw splx(s);
543 1.48.4.2 nathanw return(0);
544 1.48.4.2 nathanw }
545 1.48.4.2 nathanw
546 1.48.4.2 nathanw int
547 1.48.4.2 nathanw fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
548 1.48.4.2 nathanw {
549 1.48.4.2 nathanw struct fd_softc *sc;
550 1.48.4.2 nathanw int error, wlab;
551 1.48.4.2 nathanw
552 1.48.4.2 nathanw sc = getsoftc(fd_cd, FDUNIT(dev));
553 1.48.4.2 nathanw
554 1.48.4.2 nathanw if ((sc->flags & FDF_HAVELABEL) == 0)
555 1.48.4.2 nathanw return(EBADF);
556 1.48.4.2 nathanw
557 1.48.4.2 nathanw switch (cmd) {
558 1.48.4.2 nathanw case DIOCSBAD:
559 1.48.4.2 nathanw return(EINVAL);
560 1.48.4.2 nathanw case DIOCSRETRIES:
561 1.48.4.2 nathanw if (*(int *)addr < 0)
562 1.48.4.2 nathanw return(EINVAL);
563 1.48.4.2 nathanw sc->retries = *(int *)addr;
564 1.48.4.2 nathanw return(0);
565 1.48.4.2 nathanw case DIOCSSTEP:
566 1.48.4.2 nathanw if (*(int *)addr < FDSTEPDELAY)
567 1.48.4.2 nathanw return(EINVAL);
568 1.48.4.2 nathanw sc->dkdev.dk_label->d_trkseek = sc->stepdelay = *(int *)addr;
569 1.48.4.2 nathanw return(0);
570 1.48.4.2 nathanw case DIOCGDINFO:
571 1.48.4.2 nathanw *(struct disklabel *)addr = *(sc->dkdev.dk_label);
572 1.48.4.2 nathanw return(0);
573 1.48.4.2 nathanw case DIOCGPART:
574 1.48.4.2 nathanw ((struct partinfo *)addr)->disklab = sc->dkdev.dk_label;
575 1.48.4.2 nathanw ((struct partinfo *)addr)->part =
576 1.48.4.2 nathanw &sc->dkdev.dk_label->d_partitions[FDPART(dev)];
577 1.48.4.2 nathanw return(0);
578 1.48.4.2 nathanw case DIOCSDINFO:
579 1.48.4.2 nathanw if ((flag & FWRITE) == 0)
580 1.48.4.2 nathanw return(EBADF);
581 1.48.4.2 nathanw return(fdsetdisklabel(sc, (struct disklabel *)addr));
582 1.48.4.2 nathanw case DIOCWDINFO:
583 1.48.4.2 nathanw if ((flag & FWRITE) == 0)
584 1.48.4.2 nathanw return(EBADF);
585 1.48.4.2 nathanw if ((error = fdsetdisklabel(sc, (struct disklabel *)addr)) != 0)
586 1.48.4.2 nathanw return(error);
587 1.48.4.2 nathanw wlab = sc->wlabel;
588 1.48.4.2 nathanw sc->wlabel = 1;
589 1.48.4.2 nathanw error = fdputdisklabel(sc, dev);
590 1.48.4.2 nathanw sc->wlabel = wlab;
591 1.48.4.2 nathanw return(error);
592 1.48.4.2 nathanw case DIOCWLABEL:
593 1.48.4.2 nathanw if ((flag & FWRITE) == 0)
594 1.48.4.2 nathanw return(EBADF);
595 1.48.4.2 nathanw sc->wlabel = *(int *)addr;
596 1.48.4.2 nathanw return(0);
597 1.48.4.2 nathanw case DIOCGDEFLABEL:
598 1.48.4.2 nathanw fdgetdefaultlabel(sc, (struct disklabel *)addr, FDPART(dev));
599 1.48.4.2 nathanw return(0);
600 1.48.4.2 nathanw default:
601 1.48.4.2 nathanw return(ENOTTY);
602 1.48.4.2 nathanw }
603 1.48.4.2 nathanw }
604 1.48.4.2 nathanw
605 1.48.4.2 nathanw int
606 1.48.4.2 nathanw fdread(dev_t dev, struct uio *uio, int flags)
607 1.48.4.2 nathanw {
608 1.48.4.2 nathanw return (physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
609 1.48.4.2 nathanw }
610 1.48.4.2 nathanw
611 1.48.4.2 nathanw int
612 1.48.4.2 nathanw fdwrite(dev_t dev, struct uio *uio, int flags)
613 1.48.4.2 nathanw {
614 1.48.4.2 nathanw return (physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
615 1.48.4.2 nathanw }
616 1.48.4.2 nathanw
617 1.48.4.2 nathanw
618 1.48.4.2 nathanw void
619 1.48.4.2 nathanw fdintr(int flag)
620 1.48.4.2 nathanw {
621 1.48.4.2 nathanw int s;
622 1.48.4.2 nathanw
623 1.48.4.2 nathanw s = splbio();
624 1.48.4.2 nathanw if (fdc_indma)
625 1.48.4.2 nathanw fddmadone(fdc_indma, 0);
626 1.48.4.2 nathanw splx(s);
627 1.48.4.2 nathanw }
628 1.48.4.2 nathanw
629 1.48.4.2 nathanw void
630 1.48.4.2 nathanw fdidxintr(void)
631 1.48.4.2 nathanw {
632 1.48.4.2 nathanw if (fdc_indma && fdc_dmalen) {
633 1.48.4.2 nathanw /*
634 1.48.4.2 nathanw * turn off intr and start actual dma
635 1.48.4.2 nathanw */
636 1.48.4.2 nathanw ciab.icr = CIA_ICR_FLG;
637 1.48.4.2 nathanw FDDMASTART(fdc_dmalen, fdc_dmawrite);
638 1.48.4.2 nathanw fdc_dmalen = 0;
639 1.48.4.2 nathanw }
640 1.48.4.2 nathanw }
641 1.48.4.2 nathanw
642 1.48.4.2 nathanw void
643 1.48.4.2 nathanw fdstrategy(struct buf *bp)
644 1.48.4.2 nathanw {
645 1.48.4.2 nathanw struct disklabel *lp;
646 1.48.4.2 nathanw struct fd_softc *sc;
647 1.48.4.2 nathanw int unit, part, s;
648 1.48.4.2 nathanw
649 1.48.4.2 nathanw unit = FDUNIT(bp->b_dev);
650 1.48.4.2 nathanw part = FDPART(bp->b_dev);
651 1.48.4.2 nathanw sc = getsoftc(fd_cd, unit);
652 1.48.4.2 nathanw
653 1.48.4.2 nathanw #ifdef FDDEBUG
654 1.48.4.2 nathanw printf("fdstrategy: 0x%x\n", bp);
655 1.48.4.2 nathanw #endif
656 1.48.4.2 nathanw /*
657 1.48.4.2 nathanw * check for valid partition and bounds
658 1.48.4.2 nathanw */
659 1.48.4.2 nathanw lp = sc->dkdev.dk_label;
660 1.48.4.2 nathanw if ((sc->flags & FDF_HAVELABEL) == 0) {
661 1.48.4.2 nathanw bp->b_error = EIO;
662 1.48.4.2 nathanw goto bad;
663 1.48.4.2 nathanw }
664 1.48.4.2 nathanw if (bounds_check_with_label(bp, lp, sc->wlabel) <= 0)
665 1.48.4.2 nathanw goto done;
666 1.48.4.2 nathanw
667 1.48.4.2 nathanw /*
668 1.48.4.2 nathanw * trans count of zero or bounds check indicates io is done
669 1.48.4.2 nathanw * we are done.
670 1.48.4.2 nathanw */
671 1.48.4.2 nathanw if (bp->b_bcount == 0)
672 1.48.4.2 nathanw goto done;
673 1.48.4.2 nathanw
674 1.48.4.2 nathanw bp->b_rawblkno = bp->b_blkno;
675 1.48.4.2 nathanw
676 1.48.4.2 nathanw /*
677 1.48.4.2 nathanw * queue the buf and kick the low level code
678 1.48.4.2 nathanw */
679 1.48.4.2 nathanw s = splbio();
680 1.48.4.3 nathanw BUFQ_PUT(&sc->bufq, bp);
681 1.48.4.2 nathanw fdstart(sc);
682 1.48.4.2 nathanw splx(s);
683 1.48.4.2 nathanw return;
684 1.48.4.2 nathanw bad:
685 1.48.4.2 nathanw bp->b_flags |= B_ERROR;
686 1.48.4.2 nathanw done:
687 1.48.4.2 nathanw bp->b_resid = bp->b_bcount;
688 1.48.4.2 nathanw biodone(bp);
689 1.48.4.2 nathanw }
690 1.48.4.2 nathanw
691 1.48.4.2 nathanw /*
692 1.48.4.2 nathanw * make sure disk is loaded and label is up-to-date.
693 1.48.4.2 nathanw */
694 1.48.4.2 nathanw int
695 1.48.4.2 nathanw fdloaddisk(struct fd_softc *sc)
696 1.48.4.2 nathanw {
697 1.48.4.2 nathanw /*
698 1.48.4.2 nathanw * if diskchange is low step drive to 0 then up one then to zero.
699 1.48.4.2 nathanw */
700 1.48.4.2 nathanw fdselunit(sc); /* make sure the unit is selected */
701 1.48.4.2 nathanw if (FDTESTC(FDB_CHANGED)) {
702 1.48.4.2 nathanw fdsetpos(sc, 0, 0);
703 1.48.4.2 nathanw sc->cachetrk = -1; /* invalidate the cache */
704 1.48.4.2 nathanw sc->flags &= ~FDF_HAVELABEL;
705 1.48.4.2 nathanw fdsetpos(sc, FDNHEADS, 0);
706 1.48.4.2 nathanw fdsetpos(sc, 0, 0);
707 1.48.4.2 nathanw if (FDTESTC(FDB_CHANGED)) {
708 1.48.4.2 nathanw fdmotoroff(sc);
709 1.48.4.2 nathanw FDDESELECT(sc->unitmask);
710 1.48.4.2 nathanw return(ENXIO);
711 1.48.4.2 nathanw }
712 1.48.4.2 nathanw }
713 1.48.4.2 nathanw FDDESELECT(sc->unitmask);
714 1.48.4.2 nathanw fdmotoroff(sc);
715 1.48.4.2 nathanw sc->type = fdcgetfdtype(sc->hwunit);
716 1.48.4.2 nathanw if (sc->type == NULL)
717 1.48.4.2 nathanw return(ENXIO);
718 1.48.4.2 nathanw if (sc->openpart == FDMSDOSPART)
719 1.48.4.2 nathanw sc->nsectors = sc->type->msdos_nsectors;
720 1.48.4.2 nathanw else
721 1.48.4.2 nathanw sc->nsectors = sc->type->amiga_nsectors;
722 1.48.4.2 nathanw return(0);
723 1.48.4.2 nathanw }
724 1.48.4.2 nathanw
725 1.48.4.2 nathanw void
726 1.48.4.2 nathanw fdgetdefaultlabel(struct fd_softc *sc, struct disklabel *lp, int part)
727 1.48.4.2 nathanw /* (variable part) XXX ick */
728 1.48.4.2 nathanw {
729 1.48.4.2 nathanw
730 1.48.4.2 nathanw bzero(lp, sizeof(struct disklabel));
731 1.48.4.2 nathanw lp->d_secsize = FDSECSIZE;
732 1.48.4.2 nathanw lp->d_ntracks = FDNHEADS;
733 1.48.4.2 nathanw lp->d_ncylinders = sc->type->ncylinders;
734 1.48.4.2 nathanw lp->d_nsectors = sc->nsectors;
735 1.48.4.2 nathanw lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
736 1.48.4.2 nathanw lp->d_type = DTYPE_FLOPPY;
737 1.48.4.2 nathanw lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
738 1.48.4.2 nathanw lp->d_rpm = 300; /* good guess I suppose. */
739 1.48.4.2 nathanw lp->d_interleave = 1; /* should change when adding msdos */
740 1.48.4.2 nathanw sc->stepdelay = lp->d_trkseek = FDSTEPDELAY;
741 1.48.4.2 nathanw lp->d_bbsize = 0;
742 1.48.4.2 nathanw lp->d_sbsize = 0;
743 1.48.4.2 nathanw lp->d_partitions[part].p_size = lp->d_secperunit;
744 1.48.4.2 nathanw lp->d_partitions[part].p_fstype = FS_UNUSED;
745 1.48.4.2 nathanw lp->d_partitions[part].p_fsize = 1024;
746 1.48.4.2 nathanw lp->d_partitions[part].p_frag = 8;
747 1.48.4.2 nathanw lp->d_partitions[part].p_cpg = 2; /* adosfs: reserved blocks */
748 1.48.4.2 nathanw lp->d_npartitions = part + 1;
749 1.48.4.2 nathanw lp->d_magic = lp->d_magic2 = DISKMAGIC;
750 1.48.4.2 nathanw lp->d_checksum = dkcksum(lp);
751 1.48.4.2 nathanw }
752 1.48.4.2 nathanw
753 1.48.4.2 nathanw /*
754 1.48.4.2 nathanw * read disk label, if present otherwise create one
755 1.48.4.2 nathanw * return a new label if raw part and none found, otherwise err.
756 1.48.4.2 nathanw */
757 1.48.4.2 nathanw int
758 1.48.4.2 nathanw fdgetdisklabel(struct fd_softc *sc, dev_t dev)
759 1.48.4.2 nathanw {
760 1.48.4.2 nathanw struct disklabel *lp, *dlp;
761 1.48.4.2 nathanw struct cpu_disklabel *clp;
762 1.48.4.2 nathanw struct buf *bp;
763 1.48.4.2 nathanw int error, part;
764 1.48.4.2 nathanw
765 1.48.4.2 nathanw if (sc->flags & FDF_HAVELABEL &&
766 1.48.4.2 nathanw sc->dkdev.dk_label->d_npartitions == (FDPART(dev) + 1))
767 1.48.4.2 nathanw return(0);
768 1.48.4.2 nathanw #ifdef FDDEBUG
769 1.48.4.2 nathanw printf("fdgetdisklabel()\n");
770 1.48.4.2 nathanw #endif
771 1.48.4.2 nathanw part = FDPART(dev);
772 1.48.4.2 nathanw lp = sc->dkdev.dk_label;
773 1.48.4.2 nathanw clp = sc->dkdev.dk_cpulabel;
774 1.48.4.2 nathanw bzero(lp, sizeof(struct disklabel));
775 1.48.4.2 nathanw bzero(clp, sizeof(struct cpu_disklabel));
776 1.48.4.2 nathanw
777 1.48.4.2 nathanw lp->d_secsize = FDSECSIZE;
778 1.48.4.2 nathanw lp->d_ntracks = FDNHEADS;
779 1.48.4.2 nathanw lp->d_ncylinders = sc->type->ncylinders;
780 1.48.4.2 nathanw lp->d_nsectors = sc->nsectors;
781 1.48.4.2 nathanw lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
782 1.48.4.2 nathanw lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
783 1.48.4.2 nathanw lp->d_npartitions = part + 1;
784 1.48.4.2 nathanw lp->d_partitions[part].p_size = lp->d_secperunit;
785 1.48.4.2 nathanw lp->d_partitions[part].p_fstype = FS_UNUSED;
786 1.48.4.2 nathanw lp->d_partitions[part].p_fsize = 1024;
787 1.48.4.2 nathanw lp->d_partitions[part].p_frag = 8;
788 1.48.4.2 nathanw lp->d_partitions[part].p_cpg = 2; /* for adosfs: reserved blks */
789 1.48.4.2 nathanw
790 1.48.4.2 nathanw sc->flags |= FDF_HAVELABEL;
791 1.48.4.2 nathanw
792 1.48.4.2 nathanw bp = (void *)geteblk((int)lp->d_secsize);
793 1.48.4.2 nathanw bp->b_dev = dev;
794 1.48.4.2 nathanw bp->b_blkno = 0;
795 1.48.4.2 nathanw bp->b_cylinder = 0;
796 1.48.4.2 nathanw bp->b_bcount = FDSECSIZE;
797 1.48.4.2 nathanw bp->b_flags |= B_READ;
798 1.48.4.2 nathanw fdstrategy(bp);
799 1.48.4.2 nathanw if ((error = biowait(bp)) != 0)
800 1.48.4.2 nathanw goto nolabel;
801 1.48.4.2 nathanw dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
802 1.48.4.2 nathanw if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC ||
803 1.48.4.2 nathanw dkcksum(dlp)) {
804 1.48.4.2 nathanw error = EINVAL;
805 1.48.4.2 nathanw goto nolabel;
806 1.48.4.2 nathanw }
807 1.48.4.2 nathanw bcopy(dlp, lp, sizeof(struct disklabel));
808 1.48.4.2 nathanw if (lp->d_trkseek > FDSTEPDELAY)
809 1.48.4.2 nathanw sc->stepdelay = lp->d_trkseek;
810 1.48.4.2 nathanw brelse(bp);
811 1.48.4.2 nathanw return(0);
812 1.48.4.2 nathanw nolabel:
813 1.48.4.2 nathanw fdgetdefaultlabel(sc, lp, part);
814 1.48.4.2 nathanw brelse(bp);
815 1.48.4.2 nathanw return(0);
816 1.48.4.2 nathanw }
817 1.48.4.2 nathanw
818 1.48.4.2 nathanw /*
819 1.48.4.2 nathanw * set the incore copy of this units disklabel
820 1.48.4.2 nathanw */
821 1.48.4.2 nathanw int
822 1.48.4.2 nathanw fdsetdisklabel(struct fd_softc *sc, struct disklabel *lp)
823 1.48.4.2 nathanw {
824 1.48.4.2 nathanw struct disklabel *clp;
825 1.48.4.2 nathanw struct partition *pp;
826 1.48.4.2 nathanw
827 1.48.4.2 nathanw /*
828 1.48.4.2 nathanw * must have at least opened raw unit to fetch the
829 1.48.4.2 nathanw * raw_part stuff.
830 1.48.4.2 nathanw */
831 1.48.4.2 nathanw if ((sc->flags & FDF_HAVELABEL) == 0)
832 1.48.4.2 nathanw return(EINVAL);
833 1.48.4.2 nathanw clp = sc->dkdev.dk_label;
834 1.48.4.2 nathanw /*
835 1.48.4.2 nathanw * make sure things check out and we only have one valid
836 1.48.4.2 nathanw * partition
837 1.48.4.2 nathanw */
838 1.48.4.2 nathanw #ifdef FDDEBUG
839 1.48.4.2 nathanw printf("fdsetdisklabel\n");
840 1.48.4.2 nathanw #endif
841 1.48.4.2 nathanw if (lp->d_secsize != FDSECSIZE ||
842 1.48.4.2 nathanw lp->d_nsectors != clp->d_nsectors ||
843 1.48.4.2 nathanw lp->d_ntracks != FDNHEADS ||
844 1.48.4.2 nathanw lp->d_ncylinders != clp->d_ncylinders ||
845 1.48.4.2 nathanw lp->d_secpercyl != clp->d_secpercyl ||
846 1.48.4.2 nathanw lp->d_secperunit != clp->d_secperunit ||
847 1.48.4.2 nathanw lp->d_magic != DISKMAGIC ||
848 1.48.4.2 nathanw lp->d_magic2 != DISKMAGIC ||
849 1.48.4.2 nathanw lp->d_npartitions == 0 ||
850 1.48.4.2 nathanw lp->d_npartitions > FDMAXPARTS ||
851 1.48.4.2 nathanw (lp->d_partitions[0].p_offset && lp->d_partitions[1].p_offset) ||
852 1.48.4.2 nathanw dkcksum(lp))
853 1.48.4.2 nathanw return(EINVAL);
854 1.48.4.2 nathanw /*
855 1.48.4.2 nathanw * if any partitions are present make sure they
856 1.48.4.2 nathanw * represent the currently open type
857 1.48.4.2 nathanw */
858 1.48.4.2 nathanw if ((pp = &lp->d_partitions[0])->p_size) {
859 1.48.4.2 nathanw if ((pp = &lp->d_partitions[1])->p_size == 0)
860 1.48.4.2 nathanw goto done;
861 1.48.4.2 nathanw else if (sc->openpart != 1)
862 1.48.4.2 nathanw return(EINVAL);
863 1.48.4.2 nathanw } else if (sc->openpart != 0)
864 1.48.4.2 nathanw return(EINVAL);
865 1.48.4.2 nathanw /*
866 1.48.4.2 nathanw * make sure selected partition is within bounds
867 1.48.4.2 nathanw * XXX on the second check, its to handle a bug in
868 1.48.4.2 nathanw * XXX the cluster routines as they require mutliples
869 1.48.4.2 nathanw * XXX of NBPG currently
870 1.48.4.2 nathanw */
871 1.48.4.2 nathanw if ((pp->p_offset + pp->p_size >= lp->d_secperunit) ||
872 1.48.4.2 nathanw (pp->p_frag * pp->p_fsize % NBPG))
873 1.48.4.2 nathanw return(EINVAL);
874 1.48.4.2 nathanw done:
875 1.48.4.2 nathanw bcopy(lp, clp, sizeof(struct disklabel));
876 1.48.4.2 nathanw return(0);
877 1.48.4.2 nathanw }
878 1.48.4.2 nathanw
879 1.48.4.2 nathanw /*
880 1.48.4.2 nathanw * write out the incore copy of this units disklabel
881 1.48.4.2 nathanw */
882 1.48.4.2 nathanw int
883 1.48.4.2 nathanw fdputdisklabel(struct fd_softc *sc, dev_t dev)
884 1.48.4.2 nathanw {
885 1.48.4.2 nathanw struct disklabel *lp, *dlp;
886 1.48.4.2 nathanw struct buf *bp;
887 1.48.4.2 nathanw int error;
888 1.48.4.2 nathanw
889 1.48.4.2 nathanw if ((sc->flags & FDF_HAVELABEL) == 0)
890 1.48.4.2 nathanw return(EBADF);
891 1.48.4.2 nathanw #ifdef FDDEBUG
892 1.48.4.2 nathanw printf("fdputdisklabel\n");
893 1.48.4.2 nathanw #endif
894 1.48.4.2 nathanw /*
895 1.48.4.2 nathanw * get buf and read in sector 0
896 1.48.4.2 nathanw */
897 1.48.4.2 nathanw lp = sc->dkdev.dk_label;
898 1.48.4.2 nathanw bp = geteblk((int)lp->d_secsize);
899 1.48.4.2 nathanw bp->b_dev = FDMAKEDEV(major(dev), FDUNIT(dev), RAW_PART);
900 1.48.4.2 nathanw bp->b_blkno = 0;
901 1.48.4.2 nathanw bp->b_cylinder = 0;
902 1.48.4.2 nathanw bp->b_bcount = FDSECSIZE;
903 1.48.4.2 nathanw bp->b_flags |= B_READ;
904 1.48.4.2 nathanw fdstrategy(bp);
905 1.48.4.2 nathanw if ((error = biowait(bp)) != 0)
906 1.48.4.2 nathanw goto done;
907 1.48.4.2 nathanw /*
908 1.48.4.8 thorpej * copy disklabel to buf and write it out synchronous
909 1.48.4.2 nathanw */
910 1.48.4.2 nathanw dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
911 1.48.4.2 nathanw bcopy(lp, dlp, sizeof(struct disklabel));
912 1.48.4.2 nathanw bp->b_blkno = 0;
913 1.48.4.2 nathanw bp->b_cylinder = 0;
914 1.48.4.2 nathanw bp->b_flags &= ~(B_READ|B_DONE);
915 1.48.4.2 nathanw bp->b_flags |= B_WRITE;
916 1.48.4.2 nathanw fdstrategy(bp);
917 1.48.4.2 nathanw error = biowait(bp);
918 1.48.4.2 nathanw done:
919 1.48.4.2 nathanw brelse(bp);
920 1.48.4.2 nathanw return(error);
921 1.48.4.2 nathanw }
922 1.48.4.2 nathanw
923 1.48.4.2 nathanw /*
924 1.48.4.2 nathanw * figure out drive type or NULL if none.
925 1.48.4.2 nathanw */
926 1.48.4.2 nathanw struct fdtype *
927 1.48.4.2 nathanw fdcgetfdtype(int unit)
928 1.48.4.2 nathanw {
929 1.48.4.2 nathanw struct fdtype *ftp;
930 1.48.4.2 nathanw u_long id, idb;
931 1.48.4.2 nathanw int cnt, umask;
932 1.48.4.2 nathanw
933 1.48.4.2 nathanw id = 0;
934 1.48.4.2 nathanw umask = 1 << (3 + unit);
935 1.48.4.2 nathanw
936 1.48.4.2 nathanw FDDESELECT(FDCUNITMASK);
937 1.48.4.2 nathanw
938 1.48.4.2 nathanw FDSETMOTOR(1);
939 1.48.4.2 nathanw delay(1);
940 1.48.4.2 nathanw FDSELECT(umask);
941 1.48.4.2 nathanw delay(1);
942 1.48.4.2 nathanw FDDESELECT(umask);
943 1.48.4.2 nathanw
944 1.48.4.2 nathanw FDSETMOTOR(0);
945 1.48.4.2 nathanw delay(1);
946 1.48.4.2 nathanw FDSELECT(umask);
947 1.48.4.2 nathanw delay(1);
948 1.48.4.2 nathanw FDDESELECT(umask);
949 1.48.4.2 nathanw
950 1.48.4.2 nathanw for (idb = 0x80000000; idb; idb >>= 1) {
951 1.48.4.2 nathanw FDSELECT(umask);
952 1.48.4.2 nathanw delay(1);
953 1.48.4.2 nathanw if (FDTESTC(FDB_READY) == 0)
954 1.48.4.2 nathanw id |= idb;
955 1.48.4.2 nathanw FDDESELECT(umask);
956 1.48.4.2 nathanw delay(1);
957 1.48.4.2 nathanw }
958 1.48.4.2 nathanw #ifdef FDDEBUG
959 1.48.4.2 nathanw printf("fdcgettype unit %d id 0x%lx\n", unit, id);
960 1.48.4.2 nathanw #endif
961 1.48.4.2 nathanw
962 1.48.4.2 nathanw for (cnt = 0, ftp = fdtype; cnt < nfdtype; ftp++, cnt++)
963 1.48.4.2 nathanw if (ftp->driveid == id)
964 1.48.4.2 nathanw return(ftp);
965 1.48.4.2 nathanw /*
966 1.48.4.2 nathanw * 3.5dd's at unit 0 do not always return id.
967 1.48.4.2 nathanw */
968 1.48.4.2 nathanw if (unit == 0)
969 1.48.4.2 nathanw return(fdtype);
970 1.48.4.2 nathanw return(NULL);
971 1.48.4.2 nathanw }
972 1.48.4.2 nathanw
973 1.48.4.2 nathanw /*
974 1.48.4.2 nathanw * turn motor off if possible otherwise mark as needed and will be done
975 1.48.4.2 nathanw * later.
976 1.48.4.2 nathanw */
977 1.48.4.2 nathanw void
978 1.48.4.2 nathanw fdmotoroff(void *arg)
979 1.48.4.2 nathanw {
980 1.48.4.2 nathanw struct fd_softc *sc;
981 1.48.4.2 nathanw int s;
982 1.48.4.2 nathanw
983 1.48.4.2 nathanw sc = arg;
984 1.48.4.2 nathanw s = splbio();
985 1.48.4.2 nathanw
986 1.48.4.2 nathanw #ifdef FDDEBUG
987 1.48.4.2 nathanw printf("fdmotoroff: unit %d\n", sc->hwunit);
988 1.48.4.2 nathanw #endif
989 1.48.4.2 nathanw if ((sc->flags & FDF_MOTORON) == 0)
990 1.48.4.2 nathanw goto done;
991 1.48.4.2 nathanw /*
992 1.48.4.2 nathanw * if we have a timeout on a dma operation let fddmadone()
993 1.48.4.2 nathanw * deal with it.
994 1.48.4.2 nathanw */
995 1.48.4.2 nathanw if (fdc_indma == sc) {
996 1.48.4.2 nathanw fddmadone(sc, 1);
997 1.48.4.2 nathanw goto done;
998 1.48.4.2 nathanw }
999 1.48.4.2 nathanw #ifdef FDDEBUG
1000 1.48.4.2 nathanw printf(" motor was on, turning off\n");
1001 1.48.4.2 nathanw #endif
1002 1.48.4.2 nathanw
1003 1.48.4.2 nathanw /*
1004 1.48.4.2 nathanw * flush cache if needed
1005 1.48.4.2 nathanw */
1006 1.48.4.2 nathanw if (sc->flags & FDF_DIRTY) {
1007 1.48.4.2 nathanw sc->flags |= FDF_JUSTFLUSH | FDF_MOTOROFF;
1008 1.48.4.2 nathanw #ifdef FDDEBUG
1009 1.48.4.2 nathanw printf(" flushing dirty buffer first\n");
1010 1.48.4.2 nathanw #endif
1011 1.48.4.2 nathanw /*
1012 1.48.4.2 nathanw * if dma'ing done for now, fddone() will call us again
1013 1.48.4.2 nathanw */
1014 1.48.4.2 nathanw if (fdc_indma)
1015 1.48.4.2 nathanw goto done;
1016 1.48.4.2 nathanw fddmastart(sc, sc->cachetrk);
1017 1.48.4.2 nathanw goto done;
1018 1.48.4.2 nathanw }
1019 1.48.4.2 nathanw
1020 1.48.4.2 nathanw /*
1021 1.48.4.2 nathanw * if controller is busy just schedule us to be called back
1022 1.48.4.2 nathanw */
1023 1.48.4.2 nathanw if (fdc_indma) {
1024 1.48.4.2 nathanw /*
1025 1.48.4.2 nathanw * someone else has the controller now
1026 1.48.4.2 nathanw * just set flag and let fddone() call us again.
1027 1.48.4.2 nathanw */
1028 1.48.4.2 nathanw sc->flags |= FDF_MOTOROFF;
1029 1.48.4.2 nathanw goto done;
1030 1.48.4.2 nathanw }
1031 1.48.4.2 nathanw
1032 1.48.4.2 nathanw #ifdef FDDEBUG
1033 1.48.4.2 nathanw printf(" hw turning unit off\n");
1034 1.48.4.2 nathanw #endif
1035 1.48.4.2 nathanw
1036 1.48.4.2 nathanw sc->flags &= ~(FDF_MOTORON | FDF_MOTOROFF);
1037 1.48.4.2 nathanw FDDESELECT(FDCUNITMASK);
1038 1.48.4.2 nathanw FDSETMOTOR(0);
1039 1.48.4.2 nathanw delay(1);
1040 1.48.4.2 nathanw FDSELECT(sc->unitmask);
1041 1.48.4.2 nathanw delay(4);
1042 1.48.4.2 nathanw FDDESELECT(sc->unitmask);
1043 1.48.4.2 nathanw delay(1);
1044 1.48.4.2 nathanw if (sc->flags & FDF_WMOTOROFF)
1045 1.48.4.2 nathanw wakeup(fdmotoroff);
1046 1.48.4.2 nathanw done:
1047 1.48.4.2 nathanw splx(s);
1048 1.48.4.2 nathanw }
1049 1.48.4.2 nathanw
1050 1.48.4.2 nathanw /*
1051 1.48.4.2 nathanw * select drive seek to track exit with motor on.
1052 1.48.4.2 nathanw * fdsetpos(x, 0, 0) does calibrates the drive.
1053 1.48.4.2 nathanw */
1054 1.48.4.2 nathanw void
1055 1.48.4.2 nathanw fdsetpos(struct fd_softc *sc, int trk, int towrite)
1056 1.48.4.2 nathanw {
1057 1.48.4.2 nathanw int nstep, sdir, ondly, ncyl, nside;
1058 1.48.4.2 nathanw
1059 1.48.4.2 nathanw FDDESELECT(FDCUNITMASK);
1060 1.48.4.2 nathanw FDSETMOTOR(1);
1061 1.48.4.2 nathanw delay(1);
1062 1.48.4.2 nathanw FDSELECT(sc->unitmask);
1063 1.48.4.2 nathanw delay(1);
1064 1.48.4.2 nathanw if ((sc->flags & FDF_MOTORON) == 0) {
1065 1.48.4.2 nathanw ondly = 0;
1066 1.48.4.2 nathanw while (FDTESTC(FDB_READY) == 0) {
1067 1.48.4.2 nathanw delay(1000);
1068 1.48.4.2 nathanw if (++ondly >= 1000)
1069 1.48.4.2 nathanw break;
1070 1.48.4.2 nathanw }
1071 1.48.4.2 nathanw }
1072 1.48.4.2 nathanw sc->flags |= FDF_MOTORON;
1073 1.48.4.2 nathanw
1074 1.48.4.2 nathanw ncyl = trk / FDNHEADS;
1075 1.48.4.2 nathanw nside = trk % FDNHEADS;
1076 1.48.4.2 nathanw
1077 1.48.4.2 nathanw if (sc->curcyl == ncyl && fdc_side == nside)
1078 1.48.4.2 nathanw return;
1079 1.48.4.2 nathanw
1080 1.48.4.2 nathanw if (towrite)
1081 1.48.4.2 nathanw sc->flags |= FDF_WRITEWAIT;
1082 1.48.4.2 nathanw
1083 1.48.4.2 nathanw #ifdef FDDEBUG
1084 1.48.4.2 nathanw printf("fdsetpos: cyl %d head %d towrite %d\n", trk / FDNHEADS,
1085 1.48.4.2 nathanw trk % FDNHEADS, towrite);
1086 1.48.4.2 nathanw #endif
1087 1.48.4.2 nathanw nstep = ncyl - sc->curcyl;
1088 1.48.4.2 nathanw if (nstep) {
1089 1.48.4.2 nathanw /*
1090 1.48.4.2 nathanw * figure direction
1091 1.48.4.2 nathanw */
1092 1.48.4.2 nathanw if (nstep > 0 && ncyl != 0) {
1093 1.48.4.2 nathanw sdir = FDSTEPIN;
1094 1.48.4.2 nathanw FDSETDIR(1);
1095 1.48.4.2 nathanw } else {
1096 1.48.4.2 nathanw nstep = -nstep;
1097 1.48.4.2 nathanw sdir = FDSTEPOUT;
1098 1.48.4.2 nathanw FDSETDIR(0);
1099 1.48.4.2 nathanw }
1100 1.48.4.2 nathanw if (ncyl == 0) {
1101 1.48.4.2 nathanw /*
1102 1.48.4.2 nathanw * either just want cylinder 0 or doing
1103 1.48.4.2 nathanw * a calibrate.
1104 1.48.4.2 nathanw */
1105 1.48.4.2 nathanw nstep = 256;
1106 1.48.4.2 nathanw while (FDTESTC(FDB_CYLZERO) == 0 && nstep--) {
1107 1.48.4.2 nathanw FDSTEP;
1108 1.48.4.2 nathanw delay(sc->stepdelay);
1109 1.48.4.2 nathanw }
1110 1.48.4.2 nathanw if (nstep < 0)
1111 1.48.4.2 nathanw sc->flags |= FDF_NOTRACK0;
1112 1.48.4.2 nathanw } else {
1113 1.48.4.2 nathanw /*
1114 1.48.4.2 nathanw * step the needed amount amount.
1115 1.48.4.2 nathanw */
1116 1.48.4.2 nathanw while (nstep--) {
1117 1.48.4.2 nathanw FDSTEP;
1118 1.48.4.2 nathanw delay(sc->stepdelay);
1119 1.48.4.2 nathanw }
1120 1.48.4.2 nathanw }
1121 1.48.4.2 nathanw /*
1122 1.48.4.2 nathanw * if switched directions
1123 1.48.4.2 nathanw * allow drive to settle.
1124 1.48.4.2 nathanw */
1125 1.48.4.2 nathanw if (sc->pstepdir != sdir)
1126 1.48.4.2 nathanw delay(FDSETTLEDELAY);
1127 1.48.4.2 nathanw sc->pstepdir = sdir;
1128 1.48.4.2 nathanw sc->curcyl = ncyl;
1129 1.48.4.2 nathanw }
1130 1.48.4.2 nathanw if (nside == fdc_side)
1131 1.48.4.2 nathanw return;
1132 1.48.4.2 nathanw /*
1133 1.48.4.2 nathanw * select side
1134 1.48.4.2 nathanw */
1135 1.48.4.2 nathanw fdc_side = nside;
1136 1.48.4.2 nathanw FDSETHEAD(nside);
1137 1.48.4.2 nathanw delay(FDPRESIDEDELAY);
1138 1.48.4.2 nathanw }
1139 1.48.4.2 nathanw
1140 1.48.4.2 nathanw void
1141 1.48.4.2 nathanw fdselunit(struct fd_softc *sc)
1142 1.48.4.2 nathanw {
1143 1.48.4.2 nathanw FDDESELECT(FDCUNITMASK); /* deselect all */
1144 1.48.4.2 nathanw FDSETMOTOR(sc->flags & FDF_MOTORON); /* set motor to unit's state */
1145 1.48.4.2 nathanw delay(1);
1146 1.48.4.2 nathanw FDSELECT(sc->unitmask); /* select unit */
1147 1.48.4.2 nathanw delay(1);
1148 1.48.4.2 nathanw }
1149 1.48.4.2 nathanw
1150 1.48.4.2 nathanw /*
1151 1.48.4.2 nathanw * process next buf on device queue.
1152 1.48.4.2 nathanw * normall sequence of events:
1153 1.48.4.2 nathanw * fdstart() -> fddmastart();
1154 1.48.4.2 nathanw * fdidxintr();
1155 1.48.4.2 nathanw * fdintr() -> fddmadone() -> fddone();
1156 1.48.4.2 nathanw * if the track is in the cache then fdstart() will short-circuit
1157 1.48.4.2 nathanw * to fddone() else if the track cache is dirty it will flush. If
1158 1.48.4.2 nathanw * the buf is not an entire track it will cache the requested track.
1159 1.48.4.2 nathanw */
1160 1.48.4.2 nathanw void
1161 1.48.4.2 nathanw fdstart(struct fd_softc *sc)
1162 1.48.4.2 nathanw {
1163 1.48.4.2 nathanw int trk, error, write;
1164 1.48.4.2 nathanw struct buf *bp, *dp;
1165 1.48.4.2 nathanw int changed;
1166 1.48.4.2 nathanw
1167 1.48.4.2 nathanw #ifdef FDDEBUG
1168 1.48.4.2 nathanw printf("fdstart: unit %d\n", sc->hwunit);
1169 1.48.4.2 nathanw #endif
1170 1.48.4.2 nathanw
1171 1.48.4.2 nathanw /*
1172 1.48.4.2 nathanw * if dma'ing just return. we must have been called from fdstartegy.
1173 1.48.4.2 nathanw */
1174 1.48.4.2 nathanw if (fdc_indma)
1175 1.48.4.2 nathanw return;
1176 1.48.4.2 nathanw
1177 1.48.4.2 nathanw /*
1178 1.48.4.2 nathanw * get next buf if there.
1179 1.48.4.2 nathanw */
1180 1.48.4.2 nathanw dp = &sc->curbuf;
1181 1.48.4.3 nathanw if ((bp = BUFQ_PEEK(&sc->bufq)) == NULL) {
1182 1.48.4.2 nathanw #ifdef FDDEBUG
1183 1.48.4.2 nathanw printf(" nothing to do\n");
1184 1.48.4.2 nathanw #endif
1185 1.48.4.2 nathanw return;
1186 1.48.4.2 nathanw }
1187 1.48.4.2 nathanw
1188 1.48.4.2 nathanw /*
1189 1.48.4.2 nathanw * Mark us as busy now, in case fddone() gets called in one
1190 1.48.4.2 nathanw * of the cases below.
1191 1.48.4.2 nathanw */
1192 1.48.4.2 nathanw disk_busy(&sc->dkdev);
1193 1.48.4.2 nathanw
1194 1.48.4.2 nathanw /*
1195 1.48.4.2 nathanw * make sure same disk is loaded
1196 1.48.4.2 nathanw */
1197 1.48.4.2 nathanw fdselunit(sc);
1198 1.48.4.2 nathanw changed = FDTESTC(FDB_CHANGED);
1199 1.48.4.2 nathanw FDDESELECT(sc->unitmask);
1200 1.48.4.2 nathanw if (changed) {
1201 1.48.4.2 nathanw /*
1202 1.48.4.2 nathanw * disk missing, invalidate all future io on
1203 1.48.4.2 nathanw * this unit until re-open()'ed also invalidate
1204 1.48.4.2 nathanw * all current io
1205 1.48.4.2 nathanw */
1206 1.48.4.2 nathanw printf("fdstart: disk changed\n");
1207 1.48.4.2 nathanw #ifdef FDDEBUG
1208 1.48.4.2 nathanw printf(" disk was removed invalidating all io\n");
1209 1.48.4.2 nathanw #endif
1210 1.48.4.2 nathanw sc->flags &= ~FDF_HAVELABEL;
1211 1.48.4.2 nathanw for (;;) {
1212 1.48.4.3 nathanw bp = BUFQ_GET(&sc->bufq);
1213 1.48.4.2 nathanw bp->b_flags |= B_ERROR;
1214 1.48.4.2 nathanw bp->b_error = EIO;
1215 1.48.4.3 nathanw if (BUFQ_PEEK(&sc->bufq) == NULL)
1216 1.48.4.2 nathanw break;
1217 1.48.4.2 nathanw biodone(bp);
1218 1.48.4.2 nathanw }
1219 1.48.4.2 nathanw /*
1220 1.48.4.2 nathanw * do fddone() on last buf to allow other units to start.
1221 1.48.4.2 nathanw */
1222 1.48.4.3 nathanw BUFQ_PUT(&sc->bufq, bp);
1223 1.48.4.2 nathanw fddone(sc);
1224 1.48.4.2 nathanw return;
1225 1.48.4.2 nathanw }
1226 1.48.4.2 nathanw
1227 1.48.4.2 nathanw /*
1228 1.48.4.2 nathanw * we have a valid buf, setup our local version
1229 1.48.4.2 nathanw * we use this count to allow reading over multiple tracks.
1230 1.48.4.2 nathanw * into a single buffer
1231 1.48.4.2 nathanw */
1232 1.48.4.2 nathanw dp->b_bcount = bp->b_bcount;
1233 1.48.4.2 nathanw dp->b_blkno = bp->b_blkno;
1234 1.48.4.2 nathanw dp->b_data = bp->b_data;
1235 1.48.4.2 nathanw dp->b_flags = bp->b_flags;
1236 1.48.4.2 nathanw dp->b_resid = 0;
1237 1.48.4.2 nathanw
1238 1.48.4.2 nathanw if (bp->b_flags & B_READ)
1239 1.48.4.2 nathanw write = 0;
1240 1.48.4.2 nathanw else if (FDTESTC(FDB_PROTECT) == 0)
1241 1.48.4.2 nathanw write = 1;
1242 1.48.4.2 nathanw else {
1243 1.48.4.2 nathanw error = EPERM;
1244 1.48.4.2 nathanw goto bad;
1245 1.48.4.2 nathanw }
1246 1.48.4.2 nathanw
1247 1.48.4.2 nathanw /*
1248 1.48.4.2 nathanw * figure trk given blkno
1249 1.48.4.2 nathanw */
1250 1.48.4.2 nathanw trk = bp->b_blkno / sc->nsectors;
1251 1.48.4.2 nathanw
1252 1.48.4.2 nathanw /*
1253 1.48.4.2 nathanw * check to see if same as currently cached track
1254 1.48.4.2 nathanw * if so we need to do no dma read.
1255 1.48.4.2 nathanw */
1256 1.48.4.2 nathanw if (trk == sc->cachetrk) {
1257 1.48.4.2 nathanw fddone(sc);
1258 1.48.4.2 nathanw return;
1259 1.48.4.2 nathanw }
1260 1.48.4.2 nathanw
1261 1.48.4.2 nathanw /*
1262 1.48.4.2 nathanw * if we will be overwriting the entire cache, don't bother to
1263 1.48.4.2 nathanw * fetch it.
1264 1.48.4.2 nathanw */
1265 1.48.4.2 nathanw if (bp->b_bcount == (sc->nsectors * FDSECSIZE) && write &&
1266 1.48.4.2 nathanw bp->b_blkno % sc->nsectors == 0) {
1267 1.48.4.2 nathanw if (sc->flags & FDF_DIRTY)
1268 1.48.4.2 nathanw sc->flags |= FDF_JUSTFLUSH;
1269 1.48.4.2 nathanw else {
1270 1.48.4.2 nathanw sc->cachetrk = trk;
1271 1.48.4.2 nathanw fddone(sc);
1272 1.48.4.2 nathanw return;
1273 1.48.4.2 nathanw }
1274 1.48.4.2 nathanw }
1275 1.48.4.2 nathanw
1276 1.48.4.2 nathanw /*
1277 1.48.4.2 nathanw * start dma read of `trk'
1278 1.48.4.2 nathanw */
1279 1.48.4.2 nathanw fddmastart(sc, trk);
1280 1.48.4.2 nathanw return;
1281 1.48.4.2 nathanw bad:
1282 1.48.4.2 nathanw bp->b_flags |= B_ERROR;
1283 1.48.4.2 nathanw bp->b_error = error;
1284 1.48.4.2 nathanw fddone(sc);
1285 1.48.4.2 nathanw }
1286 1.48.4.2 nathanw
1287 1.48.4.2 nathanw /*
1288 1.48.4.2 nathanw * continue a started operation on next track. always begin at
1289 1.48.4.2 nathanw * sector 0 on the next track.
1290 1.48.4.2 nathanw */
1291 1.48.4.2 nathanw void
1292 1.48.4.2 nathanw fdcont(struct fd_softc *sc)
1293 1.48.4.2 nathanw {
1294 1.48.4.2 nathanw struct buf *dp, *bp;
1295 1.48.4.2 nathanw int trk, write;
1296 1.48.4.2 nathanw
1297 1.48.4.2 nathanw dp = &sc->curbuf;
1298 1.48.4.3 nathanw bp = BUFQ_PEEK(&sc->bufq);
1299 1.48.4.2 nathanw dp->b_data += (dp->b_bcount - bp->b_resid);
1300 1.48.4.2 nathanw dp->b_blkno += (dp->b_bcount - bp->b_resid) / FDSECSIZE;
1301 1.48.4.2 nathanw dp->b_bcount = bp->b_resid;
1302 1.48.4.2 nathanw
1303 1.48.4.2 nathanw /*
1304 1.48.4.2 nathanw * figure trk given blkno
1305 1.48.4.2 nathanw */
1306 1.48.4.2 nathanw trk = dp->b_blkno / sc->nsectors;
1307 1.48.4.2 nathanw #ifdef DEBUG
1308 1.48.4.2 nathanw if (trk != sc->cachetrk + 1 || dp->b_blkno % sc->nsectors != 0)
1309 1.48.4.2 nathanw panic("fdcont: confused");
1310 1.48.4.2 nathanw #endif
1311 1.48.4.2 nathanw if (dp->b_flags & B_READ)
1312 1.48.4.2 nathanw write = 0;
1313 1.48.4.2 nathanw else
1314 1.48.4.2 nathanw write = 1;
1315 1.48.4.2 nathanw /*
1316 1.48.4.2 nathanw * if we will be overwriting the entire cache, don't bother to
1317 1.48.4.2 nathanw * fetch it.
1318 1.48.4.2 nathanw */
1319 1.48.4.2 nathanw if (dp->b_bcount == (sc->nsectors * FDSECSIZE) && write) {
1320 1.48.4.2 nathanw if (sc->flags & FDF_DIRTY)
1321 1.48.4.2 nathanw sc->flags |= FDF_JUSTFLUSH;
1322 1.48.4.2 nathanw else {
1323 1.48.4.2 nathanw sc->cachetrk = trk;
1324 1.48.4.2 nathanw fddone(sc);
1325 1.48.4.2 nathanw return;
1326 1.48.4.2 nathanw }
1327 1.48.4.2 nathanw }
1328 1.48.4.2 nathanw /*
1329 1.48.4.2 nathanw * start dma read of `trk'
1330 1.48.4.2 nathanw */
1331 1.48.4.2 nathanw fddmastart(sc, trk);
1332 1.48.4.2 nathanw return;
1333 1.48.4.2 nathanw }
1334 1.48.4.2 nathanw
1335 1.48.4.2 nathanw void
1336 1.48.4.2 nathanw fddmastart(struct fd_softc *sc, int trk)
1337 1.48.4.2 nathanw {
1338 1.48.4.2 nathanw int adkmask, ndmaw, write, dmatrk;
1339 1.48.4.2 nathanw
1340 1.48.4.2 nathanw #ifdef FDDEBUG
1341 1.48.4.2 nathanw printf("fddmastart: unit %d cyl %d head %d", sc->hwunit,
1342 1.48.4.2 nathanw trk / FDNHEADS, trk % FDNHEADS);
1343 1.48.4.2 nathanw #endif
1344 1.48.4.2 nathanw /*
1345 1.48.4.2 nathanw * flush the cached track if dirty else read requested track.
1346 1.48.4.2 nathanw */
1347 1.48.4.2 nathanw if (sc->flags & FDF_DIRTY) {
1348 1.48.4.2 nathanw fdcachetoraw(sc);
1349 1.48.4.2 nathanw ndmaw = sc->type->nwritew;
1350 1.48.4.2 nathanw dmatrk = sc->cachetrk;
1351 1.48.4.2 nathanw write = 1;
1352 1.48.4.2 nathanw } else {
1353 1.48.4.2 nathanw ndmaw = sc->type->nreadw;
1354 1.48.4.2 nathanw dmatrk = trk;
1355 1.48.4.2 nathanw write = 0;
1356 1.48.4.2 nathanw }
1357 1.48.4.2 nathanw
1358 1.48.4.2 nathanw #ifdef FDDEBUG
1359 1.48.4.2 nathanw printf(" %s", write ? " flushing cache\n" : " loading cache\n");
1360 1.48.4.2 nathanw #endif
1361 1.48.4.2 nathanw sc->cachetrk = trk;
1362 1.48.4.2 nathanw fdc_indma = sc;
1363 1.48.4.2 nathanw fdsetpos(sc, dmatrk, write);
1364 1.48.4.2 nathanw
1365 1.48.4.2 nathanw /*
1366 1.48.4.2 nathanw * setup dma stuff
1367 1.48.4.2 nathanw */
1368 1.48.4.2 nathanw if (write == 0) {
1369 1.48.4.2 nathanw custom.adkcon = ADKF_MSBSYNC;
1370 1.48.4.2 nathanw custom.adkcon = ADKF_SETCLR | ADKF_WORDSYNC | ADKF_FAST;
1371 1.48.4.2 nathanw custom.dsksync = FDMFMSYNC;
1372 1.48.4.2 nathanw } else {
1373 1.48.4.2 nathanw custom.adkcon = ADKF_PRECOMP1 | ADKF_PRECOMP0 | ADKF_WORDSYNC |
1374 1.48.4.2 nathanw ADKF_MSBSYNC;
1375 1.48.4.2 nathanw adkmask = ADKF_SETCLR | ADKF_FAST | ADKF_MFMPREC;
1376 1.48.4.2 nathanw if (dmatrk >= sc->type->precomp[0])
1377 1.48.4.2 nathanw adkmask |= ADKF_PRECOMP0;
1378 1.48.4.2 nathanw if (dmatrk >= sc->type->precomp[1])
1379 1.48.4.2 nathanw adkmask |= ADKF_PRECOMP1;
1380 1.48.4.2 nathanw custom.adkcon = adkmask;
1381 1.48.4.2 nathanw }
1382 1.48.4.2 nathanw custom.dskpt = (u_char *)kvtop(fdc_dmap);
1383 1.48.4.2 nathanw
1384 1.48.4.2 nathanw /*
1385 1.48.4.2 nathanw * If writing an MSDOS track, activate disk index pulse
1386 1.48.4.2 nathanw * interrupt, dma will be started in the intr routine fdidxintr()
1387 1.48.4.2 nathanw * Otherwise, start the DMA here.
1388 1.48.4.2 nathanw */
1389 1.48.4.2 nathanw if (write && sc->openpart == FDMSDOSPART) {
1390 1.48.4.2 nathanw fdc_dmalen = ndmaw;
1391 1.48.4.2 nathanw fdc_dmawrite = write;
1392 1.48.4.2 nathanw ciab.icr = CIA_ICR_IR_SC | CIA_ICR_FLG;
1393 1.48.4.2 nathanw } else {
1394 1.48.4.2 nathanw FDDMASTART(ndmaw, write);
1395 1.48.4.2 nathanw fdc_dmalen = 0;
1396 1.48.4.2 nathanw }
1397 1.48.4.2 nathanw
1398 1.48.4.2 nathanw #ifdef FDDEBUG
1399 1.48.4.2 nathanw printf(" dma started\n");
1400 1.48.4.2 nathanw #endif
1401 1.48.4.2 nathanw }
1402 1.48.4.2 nathanw
1403 1.48.4.2 nathanw /*
1404 1.48.4.2 nathanw * recalibrate the drive
1405 1.48.4.2 nathanw */
1406 1.48.4.2 nathanw void
1407 1.48.4.2 nathanw fdcalibrate(void *arg)
1408 1.48.4.2 nathanw {
1409 1.48.4.2 nathanw struct fd_softc *sc;
1410 1.48.4.2 nathanw static int loopcnt;
1411 1.48.4.2 nathanw
1412 1.48.4.2 nathanw sc = arg;
1413 1.48.4.2 nathanw
1414 1.48.4.2 nathanw if (loopcnt == 0) {
1415 1.48.4.2 nathanw /*
1416 1.48.4.2 nathanw * seek cyl 0
1417 1.48.4.2 nathanw */
1418 1.48.4.2 nathanw fdc_indma = sc;
1419 1.48.4.2 nathanw sc->stepdelay += 900;
1420 1.48.4.2 nathanw if (sc->cachetrk > 1)
1421 1.48.4.2 nathanw fdsetpos(sc, sc->cachetrk % FDNHEADS, 0);
1422 1.48.4.2 nathanw sc->stepdelay -= 900;
1423 1.48.4.2 nathanw }
1424 1.48.4.2 nathanw if (loopcnt++ & 1)
1425 1.48.4.2 nathanw fdsetpos(sc, sc->cachetrk, 0);
1426 1.48.4.2 nathanw else
1427 1.48.4.2 nathanw fdsetpos(sc, sc->cachetrk + FDNHEADS, 0);
1428 1.48.4.2 nathanw /*
1429 1.48.4.2 nathanw * trk++, trk, trk++, trk, trk++, trk, trk++, trk and dma
1430 1.48.4.2 nathanw */
1431 1.48.4.2 nathanw if (loopcnt < 8)
1432 1.48.4.2 nathanw callout_reset(&sc->calibrate_ch, hz / 8, fdcalibrate, sc);
1433 1.48.4.2 nathanw else {
1434 1.48.4.2 nathanw loopcnt = 0;
1435 1.48.4.2 nathanw fdc_indma = NULL;
1436 1.48.4.2 nathanw callout_reset(&sc->motor_ch, 3 * hz / 2, fdmotoroff, sc);
1437 1.48.4.2 nathanw fddmastart(sc, sc->cachetrk);
1438 1.48.4.2 nathanw }
1439 1.48.4.2 nathanw }
1440 1.48.4.2 nathanw
1441 1.48.4.2 nathanw void
1442 1.48.4.2 nathanw fddmadone(struct fd_softc *sc, int timeo)
1443 1.48.4.2 nathanw {
1444 1.48.4.2 nathanw #ifdef FDDEBUG
1445 1.48.4.2 nathanw printf("fddmadone: unit %d, timeo %d\n", sc->hwunit, timeo);
1446 1.48.4.2 nathanw #endif
1447 1.48.4.2 nathanw fdc_indma = NULL;
1448 1.48.4.2 nathanw callout_stop(&sc->motor_ch);
1449 1.48.4.2 nathanw FDDMASTOP;
1450 1.48.4.2 nathanw
1451 1.48.4.2 nathanw /*
1452 1.48.4.2 nathanw * guarantee the drive has been at current head and cyl
1453 1.48.4.2 nathanw * for at least FDWRITEDELAY after a write.
1454 1.48.4.2 nathanw */
1455 1.48.4.2 nathanw if (sc->flags & FDF_WRITEWAIT) {
1456 1.48.4.2 nathanw delay(FDWRITEDELAY);
1457 1.48.4.2 nathanw sc->flags &= ~FDF_WRITEWAIT;
1458 1.48.4.2 nathanw }
1459 1.48.4.2 nathanw
1460 1.48.4.2 nathanw if ((sc->flags & FDF_MOTOROFF) == 0) {
1461 1.48.4.2 nathanw /*
1462 1.48.4.2 nathanw * motor runs for 1.5 seconds after last dma
1463 1.48.4.2 nathanw */
1464 1.48.4.2 nathanw callout_reset(&sc->motor_ch, 3 * hz / 2, fdmotoroff, sc);
1465 1.48.4.2 nathanw }
1466 1.48.4.2 nathanw if (sc->flags & FDF_DIRTY) {
1467 1.48.4.2 nathanw /*
1468 1.48.4.2 nathanw * if buffer dirty, the last dma cleaned it
1469 1.48.4.2 nathanw */
1470 1.48.4.2 nathanw sc->flags &= ~FDF_DIRTY;
1471 1.48.4.2 nathanw if (timeo)
1472 1.48.4.2 nathanw printf("%s: write of track cache timed out.\n",
1473 1.48.4.2 nathanw sc->sc_dv.dv_xname);
1474 1.48.4.2 nathanw if (sc->flags & FDF_JUSTFLUSH) {
1475 1.48.4.2 nathanw sc->flags &= ~FDF_JUSTFLUSH;
1476 1.48.4.2 nathanw /*
1477 1.48.4.2 nathanw * we are done dma'ing
1478 1.48.4.2 nathanw */
1479 1.48.4.2 nathanw fddone(sc);
1480 1.48.4.2 nathanw return;
1481 1.48.4.2 nathanw }
1482 1.48.4.2 nathanw /*
1483 1.48.4.2 nathanw * load the cache
1484 1.48.4.2 nathanw */
1485 1.48.4.2 nathanw fddmastart(sc, sc->cachetrk);
1486 1.48.4.2 nathanw return;
1487 1.48.4.2 nathanw }
1488 1.48.4.2 nathanw #ifdef FDDEBUG
1489 1.48.4.2 nathanw else if (sc->flags & FDF_MOTOROFF)
1490 1.48.4.2 nathanw panic("fddmadone: FDF_MOTOROFF with no FDF_DIRTY");
1491 1.48.4.2 nathanw #endif
1492 1.48.4.2 nathanw
1493 1.48.4.2 nathanw /*
1494 1.48.4.2 nathanw * cache loaded decode it into cache buffer
1495 1.48.4.2 nathanw */
1496 1.48.4.2 nathanw if (timeo == 0 && fdrawtocache(sc) == 0)
1497 1.48.4.2 nathanw sc->retried = 0;
1498 1.48.4.2 nathanw else {
1499 1.48.4.2 nathanw #ifdef FDDEBUG
1500 1.48.4.2 nathanw if (timeo)
1501 1.48.4.2 nathanw printf("%s: fddmadone: cache load timed out.\n",
1502 1.48.4.2 nathanw sc->sc_dv.dv_xname);
1503 1.48.4.2 nathanw #endif
1504 1.48.4.2 nathanw if (sc->retried >= sc->retries) {
1505 1.48.4.2 nathanw sc->retried = 0;
1506 1.48.4.2 nathanw sc->cachetrk = -1;
1507 1.48.4.2 nathanw } else {
1508 1.48.4.2 nathanw sc->retried++;
1509 1.48.4.2 nathanw /*
1510 1.48.4.2 nathanw * this will be restarted at end of calibrate loop.
1511 1.48.4.2 nathanw */
1512 1.48.4.2 nathanw callout_stop(&sc->motor_ch);
1513 1.48.4.2 nathanw fdcalibrate(sc);
1514 1.48.4.2 nathanw return;
1515 1.48.4.2 nathanw }
1516 1.48.4.2 nathanw }
1517 1.48.4.2 nathanw fddone(sc);
1518 1.48.4.2 nathanw }
1519 1.48.4.2 nathanw
1520 1.48.4.2 nathanw void
1521 1.48.4.2 nathanw fddone(struct fd_softc *sc)
1522 1.48.4.2 nathanw {
1523 1.48.4.2 nathanw struct buf *dp, *bp;
1524 1.48.4.2 nathanw char *data;
1525 1.48.4.2 nathanw int sz;
1526 1.48.4.2 nathanw
1527 1.48.4.2 nathanw #ifdef FDDEBUG
1528 1.48.4.2 nathanw printf("fddone: unit %d\n", sc->hwunit);
1529 1.48.4.2 nathanw #endif
1530 1.48.4.2 nathanw /*
1531 1.48.4.2 nathanw * check to see if unit is just flushing the cache,
1532 1.48.4.2 nathanw * that is we have no io queued.
1533 1.48.4.2 nathanw */
1534 1.48.4.2 nathanw if (sc->flags & FDF_MOTOROFF)
1535 1.48.4.2 nathanw goto nobuf;
1536 1.48.4.2 nathanw
1537 1.48.4.2 nathanw dp = &sc->curbuf;
1538 1.48.4.3 nathanw if ((bp = BUFQ_PEEK(&sc->bufq)) == NULL)
1539 1.48.4.2 nathanw panic ("fddone");
1540 1.48.4.2 nathanw /*
1541 1.48.4.2 nathanw * check for an error that may have occurred
1542 1.48.4.2 nathanw * while getting the track.
1543 1.48.4.2 nathanw */
1544 1.48.4.2 nathanw if (sc->cachetrk == -1) {
1545 1.48.4.2 nathanw sc->retried = 0;
1546 1.48.4.2 nathanw bp->b_flags |= B_ERROR;
1547 1.48.4.2 nathanw bp->b_error = EIO;
1548 1.48.4.2 nathanw } else if ((bp->b_flags & B_ERROR) == 0) {
1549 1.48.4.2 nathanw data = sc->cachep;
1550 1.48.4.2 nathanw /*
1551 1.48.4.2 nathanw * get offset of data in track cache and limit
1552 1.48.4.2 nathanw * the copy size to not exceed the cache's end.
1553 1.48.4.2 nathanw */
1554 1.48.4.2 nathanw data += (dp->b_blkno % sc->nsectors) * FDSECSIZE;
1555 1.48.4.2 nathanw sz = sc->nsectors - dp->b_blkno % sc->nsectors;
1556 1.48.4.2 nathanw sz *= FDSECSIZE;
1557 1.48.4.2 nathanw sz = min(dp->b_bcount, sz);
1558 1.48.4.2 nathanw if (bp->b_flags & B_READ)
1559 1.48.4.2 nathanw bcopy(data, dp->b_data, sz);
1560 1.48.4.2 nathanw else {
1561 1.48.4.2 nathanw bcopy(dp->b_data, data, sz);
1562 1.48.4.2 nathanw sc->flags |= FDF_DIRTY;
1563 1.48.4.2 nathanw }
1564 1.48.4.2 nathanw bp->b_resid = dp->b_bcount - sz;
1565 1.48.4.2 nathanw if (bp->b_resid == 0) {
1566 1.48.4.2 nathanw bp->b_error = 0;
1567 1.48.4.2 nathanw } else {
1568 1.48.4.2 nathanw /*
1569 1.48.4.2 nathanw * not done yet need to read next track
1570 1.48.4.2 nathanw */
1571 1.48.4.2 nathanw fdcont(sc);
1572 1.48.4.2 nathanw return;
1573 1.48.4.2 nathanw }
1574 1.48.4.2 nathanw }
1575 1.48.4.2 nathanw /*
1576 1.48.4.2 nathanw * remove from queue.
1577 1.48.4.2 nathanw */
1578 1.48.4.3 nathanw (void)BUFQ_GET(&sc->bufq);
1579 1.48.4.2 nathanw
1580 1.48.4.6 nathanw disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid),
1581 1.48.4.6 nathanw (bp->b_flags & B_READ));
1582 1.48.4.2 nathanw
1583 1.48.4.2 nathanw biodone(bp);
1584 1.48.4.2 nathanw nobuf:
1585 1.48.4.2 nathanw fdfindwork(sc->sc_dv.dv_unit);
1586 1.48.4.2 nathanw }
1587 1.48.4.2 nathanw
1588 1.48.4.2 nathanw void
1589 1.48.4.2 nathanw fdfindwork(int unit)
1590 1.48.4.2 nathanw {
1591 1.48.4.2 nathanw struct fd_softc *ssc, *sc;
1592 1.48.4.2 nathanw int i, last;
1593 1.48.4.2 nathanw
1594 1.48.4.2 nathanw /*
1595 1.48.4.2 nathanw * first see if we have any fdopen()'s waiting
1596 1.48.4.2 nathanw */
1597 1.48.4.2 nathanw if (fdc_wantwakeup) {
1598 1.48.4.2 nathanw wakeup(fdopen);
1599 1.48.4.2 nathanw fdc_wantwakeup--;
1600 1.48.4.2 nathanw return;
1601 1.48.4.2 nathanw }
1602 1.48.4.2 nathanw
1603 1.48.4.2 nathanw /*
1604 1.48.4.2 nathanw * start next available unit, linear search from the next unit
1605 1.48.4.2 nathanw * wrapping and finally this unit.
1606 1.48.4.2 nathanw */
1607 1.48.4.2 nathanw last = 0;
1608 1.48.4.2 nathanw ssc = NULL;
1609 1.48.4.2 nathanw for (i = unit + 1; last == 0; i++) {
1610 1.48.4.2 nathanw if (i == unit)
1611 1.48.4.2 nathanw last = 1;
1612 1.48.4.2 nathanw if (i >= fd_cd.cd_ndevs) {
1613 1.48.4.2 nathanw i = -1;
1614 1.48.4.2 nathanw continue;
1615 1.48.4.2 nathanw }
1616 1.48.4.2 nathanw if ((sc = fd_cd.cd_devs[i]) == NULL)
1617 1.48.4.2 nathanw continue;
1618 1.48.4.2 nathanw
1619 1.48.4.2 nathanw /*
1620 1.48.4.2 nathanw * if unit has requested to be turned off
1621 1.48.4.2 nathanw * and it has no buf's queued do it now
1622 1.48.4.2 nathanw */
1623 1.48.4.2 nathanw if (sc->flags & FDF_MOTOROFF) {
1624 1.48.4.3 nathanw if (BUFQ_PEEK(&sc->bufq) == NULL)
1625 1.48.4.2 nathanw fdmotoroff(sc);
1626 1.48.4.2 nathanw else {
1627 1.48.4.2 nathanw /*
1628 1.48.4.2 nathanw * we gained a buf request while
1629 1.48.4.2 nathanw * we waited, forget the motoroff
1630 1.48.4.2 nathanw */
1631 1.48.4.2 nathanw sc->flags &= ~FDF_MOTOROFF;
1632 1.48.4.2 nathanw }
1633 1.48.4.2 nathanw /*
1634 1.48.4.2 nathanw * if we now have dma unit must have needed
1635 1.48.4.2 nathanw * flushing, quit
1636 1.48.4.2 nathanw */
1637 1.48.4.2 nathanw if (fdc_indma)
1638 1.48.4.2 nathanw return;
1639 1.48.4.2 nathanw }
1640 1.48.4.2 nathanw /*
1641 1.48.4.2 nathanw * if we have no start unit and the current unit has
1642 1.48.4.2 nathanw * io waiting choose this unit to start.
1643 1.48.4.2 nathanw */
1644 1.48.4.3 nathanw if (ssc == NULL && BUFQ_PEEK(&sc->bufq) != NULL)
1645 1.48.4.2 nathanw ssc = sc;
1646 1.48.4.2 nathanw }
1647 1.48.4.2 nathanw if (ssc)
1648 1.48.4.2 nathanw fdstart(ssc);
1649 1.48.4.2 nathanw }
1650 1.48.4.2 nathanw
1651 1.48.4.2 nathanw /*
1652 1.48.4.2 nathanw * min byte count to whats left of the track in question
1653 1.48.4.2 nathanw */
1654 1.48.4.2 nathanw void
1655 1.48.4.2 nathanw fdminphys(struct buf *bp)
1656 1.48.4.2 nathanw {
1657 1.48.4.2 nathanw struct fd_softc *sc;
1658 1.48.4.2 nathanw int trk, sec, toff, tsz;
1659 1.48.4.2 nathanw
1660 1.48.4.2 nathanw if ((sc = getsoftc(fd_cd, FDUNIT(bp->b_dev))) == NULL)
1661 1.48.4.2 nathanw panic("fdminphys: couldn't get softc");
1662 1.48.4.2 nathanw
1663 1.48.4.2 nathanw trk = bp->b_blkno / sc->nsectors;
1664 1.48.4.2 nathanw sec = bp->b_blkno % sc->nsectors;
1665 1.48.4.2 nathanw
1666 1.48.4.2 nathanw toff = sec * FDSECSIZE;
1667 1.48.4.2 nathanw tsz = sc->nsectors * FDSECSIZE;
1668 1.48.4.2 nathanw #ifdef FDDEBUG
1669 1.48.4.2 nathanw printf("fdminphys: before %d", bp->b_bcount);
1670 1.48.4.2 nathanw #endif
1671 1.48.4.2 nathanw bp->b_bcount = min(bp->b_bcount, tsz - toff);
1672 1.48.4.2 nathanw #ifdef FDDEBUG
1673 1.48.4.2 nathanw printf(" after %d\n", bp->b_bcount);
1674 1.48.4.2 nathanw #endif
1675 1.48.4.2 nathanw minphys(bp);
1676 1.48.4.2 nathanw }
1677 1.48.4.2 nathanw
1678 1.48.4.2 nathanw /*
1679 1.48.4.2 nathanw * encode the track cache into raw MFM ready for dma
1680 1.48.4.2 nathanw * when we go to multiple disk formats, this will call type dependent
1681 1.48.4.2 nathanw * functions
1682 1.48.4.2 nathanw */
1683 1.48.4.2 nathanw void fdcachetoraw(struct fd_softc *sc)
1684 1.48.4.2 nathanw {
1685 1.48.4.2 nathanw if (sc->openpart == FDMSDOSPART)
1686 1.48.4.2 nathanw mscachetoraw(sc);
1687 1.48.4.2 nathanw else
1688 1.48.4.2 nathanw amcachetoraw(sc);
1689 1.48.4.2 nathanw }
1690 1.48.4.2 nathanw
1691 1.48.4.2 nathanw /*
1692 1.48.4.2 nathanw * decode raw MFM from dma into units track cache.
1693 1.48.4.2 nathanw * when we go to multiple disk formats, this will call type dependent
1694 1.48.4.2 nathanw * functions
1695 1.48.4.2 nathanw */
1696 1.48.4.2 nathanw int
1697 1.48.4.2 nathanw fdrawtocache(struct fd_softc *sc)
1698 1.48.4.2 nathanw {
1699 1.48.4.2 nathanw
1700 1.48.4.2 nathanw if (sc->openpart == FDMSDOSPART)
1701 1.48.4.2 nathanw return(msrawtocache(sc));
1702 1.48.4.2 nathanw else
1703 1.48.4.2 nathanw return(amrawtocache(sc));
1704 1.48.4.2 nathanw }
1705 1.48.4.2 nathanw
1706 1.48.4.2 nathanw void
1707 1.48.4.2 nathanw amcachetoraw(struct fd_softc *sc)
1708 1.48.4.2 nathanw {
1709 1.48.4.2 nathanw static u_long mfmnull[4];
1710 1.48.4.2 nathanw u_long *rp, *crp, *dp, hcksum, dcksum, info, zero;
1711 1.48.4.2 nathanw int sec, i;
1712 1.48.4.2 nathanw
1713 1.48.4.2 nathanw rp = fdc_dmap;
1714 1.48.4.2 nathanw
1715 1.48.4.2 nathanw /*
1716 1.48.4.2 nathanw * not yet one sector (- 1 long) gap.
1717 1.48.4.2 nathanw * for now use previous drivers values
1718 1.48.4.2 nathanw */
1719 1.48.4.2 nathanw for (i = 0; i < sc->type->gap; i++)
1720 1.48.4.2 nathanw *rp++ = 0xaaaaaaaa;
1721 1.48.4.2 nathanw /*
1722 1.48.4.2 nathanw * process sectors
1723 1.48.4.2 nathanw */
1724 1.48.4.2 nathanw dp = sc->cachep;
1725 1.48.4.2 nathanw zero = 0;
1726 1.48.4.2 nathanw info = 0xff000000 | (sc->cachetrk << 16) | sc->nsectors;
1727 1.48.4.2 nathanw for (sec = 0; sec < sc->nsectors; sec++, info += (1 << 8) - 1) {
1728 1.48.4.2 nathanw hcksum = dcksum = 0;
1729 1.48.4.2 nathanw /*
1730 1.48.4.2 nathanw * sector format
1731 1.48.4.2 nathanw * offset description
1732 1.48.4.2 nathanw *-----------------------------------
1733 1.48.4.2 nathanw * 0 null
1734 1.48.4.2 nathanw * 1 sync
1735 1.48.4.2 nathanw * oddbits evenbits
1736 1.48.4.2 nathanw *----------------------
1737 1.48.4.2 nathanw * 2 3 [0xff]b [trk]b [sec]b [togap]b
1738 1.48.4.2 nathanw * 4-7 8-11 null
1739 1.48.4.2 nathanw * 12 13 header cksum [2-11]
1740 1.48.4.2 nathanw * 14 15 data cksum [16-271]
1741 1.48.4.2 nathanw * 16-143 144-271 data
1742 1.48.4.2 nathanw */
1743 1.48.4.2 nathanw *rp = 0xaaaaaaaa;
1744 1.48.4.2 nathanw if (*(rp - 1) & 0x1)
1745 1.48.4.2 nathanw *rp &= 0x7fffffff; /* clock bit correction */
1746 1.48.4.2 nathanw rp++;
1747 1.48.4.2 nathanw *rp++ = (FDMFMSYNC << 16) | FDMFMSYNC;
1748 1.48.4.2 nathanw rp = mfmblkencode(&info, rp, &hcksum, 1);
1749 1.48.4.2 nathanw rp = mfmblkencode(mfmnull, rp, &hcksum, 4);
1750 1.48.4.2 nathanw rp = mfmblkencode(&hcksum, rp, NULL, 1);
1751 1.48.4.2 nathanw
1752 1.48.4.2 nathanw crp = rp;
1753 1.48.4.2 nathanw rp = mfmblkencode(dp, rp + 2, &dcksum, FDSECLWORDS);
1754 1.48.4.2 nathanw dp += FDSECLWORDS;
1755 1.48.4.2 nathanw crp = mfmblkencode(&dcksum, crp, NULL, 1);
1756 1.48.4.2 nathanw if (*(crp - 1) & 0x1)
1757 1.48.4.2 nathanw *crp &= 0x7fffffff; /* clock bit correction */
1758 1.48.4.2 nathanw else if ((*crp & 0x40000000) == 0)
1759 1.48.4.2 nathanw *crp |= 0x80000000;
1760 1.48.4.2 nathanw }
1761 1.48.4.2 nathanw *rp = 0xaaa80000;
1762 1.48.4.2 nathanw if (*(rp - 1) & 0x1)
1763 1.48.4.2 nathanw *rp &= 0x7fffffff;
1764 1.48.4.2 nathanw }
1765 1.48.4.2 nathanw
1766 1.48.4.2 nathanw u_long *
1767 1.48.4.2 nathanw fdfindsync(u_long *rp, u_long *ep)
1768 1.48.4.2 nathanw {
1769 1.48.4.2 nathanw u_short *sp;
1770 1.48.4.2 nathanw
1771 1.48.4.2 nathanw sp = (u_short *)rp;
1772 1.48.4.2 nathanw while ((u_long *)sp < ep && *sp != FDMFMSYNC)
1773 1.48.4.2 nathanw sp++;
1774 1.48.4.2 nathanw while ((u_long *)sp < ep && *sp == FDMFMSYNC)
1775 1.48.4.2 nathanw sp++;
1776 1.48.4.2 nathanw if ((u_long *)sp < ep)
1777 1.48.4.2 nathanw return((u_long *)sp);
1778 1.48.4.2 nathanw return(NULL);
1779 1.48.4.2 nathanw }
1780 1.48.4.2 nathanw
1781 1.48.4.2 nathanw int
1782 1.48.4.2 nathanw amrawtocache(struct fd_softc *sc)
1783 1.48.4.2 nathanw {
1784 1.48.4.2 nathanw u_long mfmnull[4];
1785 1.48.4.2 nathanw u_long *dp, *rp, *erp, *crp, *srp, hcksum, dcksum, info, cktmp;
1786 1.48.4.2 nathanw int cnt, doagain;
1787 1.48.4.2 nathanw
1788 1.48.4.2 nathanw doagain = 1;
1789 1.48.4.2 nathanw srp = rp = fdc_dmap;
1790 1.48.4.2 nathanw erp = (u_long *)((u_short *)rp + sc->type->nreadw);
1791 1.48.4.2 nathanw cnt = 0;
1792 1.48.4.2 nathanw again:
1793 1.48.4.2 nathanw if (doagain == 0 || (rp = srp = fdfindsync(srp, erp)) == NULL) {
1794 1.48.4.2 nathanw #ifdef DIAGNOSTIC
1795 1.48.4.2 nathanw printf("%s: corrupted track (%d) data.\n",
1796 1.48.4.2 nathanw sc->sc_dv.dv_xname, sc->cachetrk);
1797 1.48.4.2 nathanw #endif
1798 1.48.4.2 nathanw return(-1);
1799 1.48.4.2 nathanw }
1800 1.48.4.2 nathanw
1801 1.48.4.2 nathanw /*
1802 1.48.4.2 nathanw * process sectors
1803 1.48.4.2 nathanw */
1804 1.48.4.2 nathanw for (; cnt < sc->nsectors; cnt++) {
1805 1.48.4.2 nathanw hcksum = dcksum = 0;
1806 1.48.4.2 nathanw rp = mfmblkdecode(rp, &info, &hcksum, 1);
1807 1.48.4.2 nathanw rp = mfmblkdecode(rp, mfmnull, &hcksum, 4);
1808 1.48.4.2 nathanw rp = mfmblkdecode(rp, &cktmp, NULL, 1);
1809 1.48.4.2 nathanw if (cktmp != hcksum) {
1810 1.48.4.2 nathanw #ifdef FDDEBUG
1811 1.48.4.2 nathanw printf(" info 0x%x hchksum 0x%x trkhcksum 0x%x\n",
1812 1.48.4.2 nathanw info, hcksum, cktmp);
1813 1.48.4.2 nathanw #endif
1814 1.48.4.2 nathanw goto again;
1815 1.48.4.2 nathanw }
1816 1.48.4.2 nathanw if (((info >> 16) & 0xff) != sc->cachetrk) {
1817 1.48.4.2 nathanw #ifdef DEBUG
1818 1.48.4.2 nathanw printf("%s: incorrect track found: 0x%lx %d\n",
1819 1.48.4.2 nathanw sc->sc_dv.dv_xname, info, sc->cachetrk);
1820 1.48.4.2 nathanw #endif
1821 1.48.4.2 nathanw goto again;
1822 1.48.4.2 nathanw }
1823 1.48.4.2 nathanw #ifdef FDDEBUG
1824 1.48.4.2 nathanw printf(" info 0x%x\n", info);
1825 1.48.4.2 nathanw #endif
1826 1.48.4.2 nathanw
1827 1.48.4.2 nathanw rp = mfmblkdecode(rp, &cktmp, NULL, 1);
1828 1.48.4.2 nathanw dp = sc->cachep;
1829 1.48.4.2 nathanw dp += FDSECLWORDS * ((info >> 8) & 0xff);
1830 1.48.4.2 nathanw crp = mfmblkdecode(rp, dp, &dcksum, FDSECLWORDS);
1831 1.48.4.2 nathanw if (cktmp != dcksum) {
1832 1.48.4.2 nathanw #ifdef FDDEBUG
1833 1.48.4.2 nathanw printf(" info 0x%x dchksum 0x%x trkdcksum 0x%x\n",
1834 1.48.4.2 nathanw info, dcksum, cktmp);
1835 1.48.4.2 nathanw #endif
1836 1.48.4.2 nathanw goto again;
1837 1.48.4.2 nathanw }
1838 1.48.4.2 nathanw
1839 1.48.4.2 nathanw /*
1840 1.48.4.2 nathanw * if we are at gap then we can no longer be sure
1841 1.48.4.2 nathanw * of correct sync marks
1842 1.48.4.2 nathanw */
1843 1.48.4.2 nathanw if ((info && 0xff) == 1)
1844 1.48.4.2 nathanw doagain = 1;
1845 1.48.4.2 nathanw else
1846 1.48.4.2 nathanw doagain = 0;
1847 1.48.4.2 nathanw srp = rp = fdfindsync(crp, erp);
1848 1.48.4.2 nathanw }
1849 1.48.4.2 nathanw return(0);
1850 1.48.4.2 nathanw }
1851 1.48.4.2 nathanw
1852 1.48.4.2 nathanw void
1853 1.48.4.2 nathanw mscachetoraw(struct fd_softc *sc)
1854 1.48.4.2 nathanw {
1855 1.48.4.2 nathanw u_short *rp, *erp, crc;
1856 1.48.4.2 nathanw u_char *cp, tb[5];
1857 1.48.4.2 nathanw int sec, i;
1858 1.48.4.2 nathanw
1859 1.48.4.2 nathanw rp = (u_short *)fdc_dmap;
1860 1.48.4.2 nathanw erp = rp + sc->type->nwritew;
1861 1.48.4.2 nathanw cp = sc->cachep;
1862 1.48.4.2 nathanw
1863 1.48.4.2 nathanw /*
1864 1.48.4.2 nathanw * initial track filler (828 * GAP1)
1865 1.48.4.2 nathanw */
1866 1.48.4.2 nathanw for (i = 0; i < sc->type->gap; i++) {
1867 1.48.4.2 nathanw *rp++ = FDMFMGAP1;
1868 1.48.4.2 nathanw *rp++ = FDMFMGAP1;
1869 1.48.4.2 nathanw }
1870 1.48.4.2 nathanw
1871 1.48.4.2 nathanw for (sec = 0; sec < sc->nsectors; sec++) {
1872 1.48.4.2 nathanw
1873 1.48.4.2 nathanw /*
1874 1.48.4.2 nathanw * leading sector gap
1875 1.48.4.2 nathanw * (12 * GAP2) + (3 * SYNC)
1876 1.48.4.2 nathanw */
1877 1.48.4.2 nathanw for (i = 0; i < 12; i++)
1878 1.48.4.2 nathanw *rp++ = FDMFMGAP2;
1879 1.48.4.2 nathanw *rp++ = FDMFMSYNC;
1880 1.48.4.2 nathanw *rp++ = FDMFMSYNC;
1881 1.48.4.2 nathanw *rp++ = FDMFMSYNC;
1882 1.48.4.2 nathanw
1883 1.48.4.2 nathanw /*
1884 1.48.4.2 nathanw * sector information
1885 1.48.4.2 nathanw * (ID) + track + side + sector + sector size + CRC16
1886 1.48.4.2 nathanw */
1887 1.48.4.2 nathanw *rp++ = FDMFMID;
1888 1.48.4.2 nathanw tb[0] = sc->cachetrk / FDNHEADS;
1889 1.48.4.2 nathanw tb[1] = sc->cachetrk % FDNHEADS;
1890 1.48.4.2 nathanw tb[2] = sec + 1;
1891 1.48.4.2 nathanw i = sc->bytespersec;
1892 1.48.4.2 nathanw tb[3] = i < 256 ? 0 : (i < 512 ? 1 : (i < 1024 ? 2 : 3));
1893 1.48.4.2 nathanw rp = msblkencode(rp, tb, 4, &crc);
1894 1.48.4.2 nathanw tb[0] = crc >> 8;
1895 1.48.4.2 nathanw tb[1] = crc & 0xff;
1896 1.48.4.2 nathanw tb[2] = 0x4e; /* GAP1 decoded */
1897 1.48.4.2 nathanw rp = msblkencode(rp, tb, 3, 0);
1898 1.48.4.2 nathanw
1899 1.48.4.2 nathanw /*
1900 1.48.4.2 nathanw * sector info/data gap
1901 1.48.4.2 nathanw * (22 * GAP1) + (12 * GAP2) + (3 * SYNC)
1902 1.48.4.2 nathanw */
1903 1.48.4.2 nathanw for (i = 0; i < 21; i++)
1904 1.48.4.2 nathanw *rp++ = FDMFMGAP1;
1905 1.48.4.2 nathanw for (i = 0; i < 12; i++)
1906 1.48.4.2 nathanw *rp++ = FDMFMGAP2;
1907 1.48.4.2 nathanw *rp++ = FDMFMSYNC;
1908 1.48.4.2 nathanw *rp++ = FDMFMSYNC;
1909 1.48.4.2 nathanw *rp++ = FDMFMSYNC;
1910 1.48.4.2 nathanw
1911 1.48.4.2 nathanw /*
1912 1.48.4.2 nathanw * sector data
1913 1.48.4.2 nathanw * (DATA) + ...data... + CRC16
1914 1.48.4.2 nathanw */
1915 1.48.4.2 nathanw *rp++ = FDMFMDATA;
1916 1.48.4.2 nathanw rp = msblkencode(rp, cp, sc->bytespersec, &crc);
1917 1.48.4.2 nathanw cp += sc->bytespersec;
1918 1.48.4.2 nathanw tb[0] = crc >> 8;
1919 1.48.4.2 nathanw tb[1] = crc & 0xff;
1920 1.48.4.2 nathanw tb[2] = 0x4e; /* GAP3 decoded */
1921 1.48.4.2 nathanw rp = msblkencode(rp, tb, 3, 0);
1922 1.48.4.2 nathanw
1923 1.48.4.2 nathanw /*
1924 1.48.4.2 nathanw * trailing sector gap
1925 1.48.4.2 nathanw * (80 * GAP3)
1926 1.48.4.2 nathanw */
1927 1.48.4.2 nathanw for (i = 0; i < 79; i++)
1928 1.48.4.2 nathanw *rp++ = FDMFMGAP3;
1929 1.48.4.2 nathanw }
1930 1.48.4.2 nathanw
1931 1.48.4.2 nathanw /*
1932 1.48.4.2 nathanw * fill rest of track with GAP3
1933 1.48.4.2 nathanw */
1934 1.48.4.2 nathanw while (rp != erp)
1935 1.48.4.2 nathanw *rp++ = FDMFMGAP3;
1936 1.48.4.2 nathanw
1937 1.48.4.2 nathanw }
1938 1.48.4.2 nathanw
1939 1.48.4.2 nathanw int
1940 1.48.4.2 nathanw msrawtocache(struct fd_softc *sc)
1941 1.48.4.2 nathanw {
1942 1.48.4.2 nathanw u_short *rp, *srp, *erp;
1943 1.48.4.2 nathanw u_char tb[5], *cp;
1944 1.48.4.2 nathanw int ct, sec, retry;
1945 1.48.4.2 nathanw
1946 1.48.4.2 nathanw srp = rp = (u_short *)fdc_dmap;
1947 1.48.4.2 nathanw erp = rp + sc->type->nreadw;
1948 1.48.4.2 nathanw cp = sc->cachep;
1949 1.48.4.2 nathanw
1950 1.48.4.2 nathanw for (ct = 0; ct < sc->nsectors; ct++) {
1951 1.48.4.2 nathanw retry = 1;
1952 1.48.4.2 nathanw do {
1953 1.48.4.2 nathanw /*
1954 1.48.4.2 nathanw * skip leading gap to sync
1955 1.48.4.2 nathanw */
1956 1.48.4.2 nathanw if ((rp = (u_short *)fdfindsync((u_long *)rp, (u_long *)erp)) == NULL) {
1957 1.48.4.2 nathanw #ifdef DIAGNOSTIC
1958 1.48.4.2 nathanw printf("%s: corrupted track (%d) data.\n",
1959 1.48.4.2 nathanw sc->sc_dv.dv_xname, sc->cachetrk);
1960 1.48.4.2 nathanw #endif
1961 1.48.4.2 nathanw return(-1);
1962 1.48.4.2 nathanw }
1963 1.48.4.2 nathanw
1964 1.48.4.2 nathanw /*
1965 1.48.4.2 nathanw * Grab sector info
1966 1.48.4.2 nathanw */
1967 1.48.4.2 nathanw if (*rp++ != FDMFMID)
1968 1.48.4.2 nathanw continue;
1969 1.48.4.2 nathanw rp = msblkdecode(rp, tb, 4);
1970 1.48.4.2 nathanw #ifdef FDDEBUG
1971 1.48.4.2 nathanw printf("sector id: sector %d, track %d, side %d,"
1972 1.48.4.2 nathanw "bps %d\n", tb[2], tb[0], tb[1], 128 << tb[3]);
1973 1.48.4.2 nathanw #endif
1974 1.48.4.2 nathanw if ((tb[0] * FDNHEADS + tb[1]) != sc->cachetrk ||
1975 1.48.4.2 nathanw tb[2] > sc->nsectors)
1976 1.48.4.2 nathanw continue;
1977 1.48.4.2 nathanw
1978 1.48.4.2 nathanw sec = tb[2];
1979 1.48.4.2 nathanw sc->bytespersec = 128 << tb[3];
1980 1.48.4.2 nathanw rp += 2; /* skip CRC-16 */
1981 1.48.4.2 nathanw
1982 1.48.4.2 nathanw /*
1983 1.48.4.2 nathanw * skip gap and read in data
1984 1.48.4.2 nathanw */
1985 1.48.4.2 nathanw if ((rp = (u_short *)fdfindsync((u_long *)rp, (u_long *)erp)) == NULL)
1986 1.48.4.2 nathanw return(-1);
1987 1.48.4.2 nathanw if (*rp++ != FDMFMDATA)
1988 1.48.4.2 nathanw continue;
1989 1.48.4.2 nathanw rp = msblkdecode(rp, cp + ((sec-1) * sc->bytespersec),
1990 1.48.4.2 nathanw sc->bytespersec);
1991 1.48.4.2 nathanw rp += 2; /* skip CRC-16 */
1992 1.48.4.2 nathanw
1993 1.48.4.2 nathanw retry = 0;
1994 1.48.4.2 nathanw } while (retry);
1995 1.48.4.2 nathanw }
1996 1.48.4.2 nathanw return(0);
1997 1.48.4.2 nathanw }
1998 1.48.4.2 nathanw
1999 1.48.4.2 nathanw /*
2000 1.48.4.2 nathanw * encode len longwords of `dp' data in amiga mfm block format (`rp')
2001 1.48.4.2 nathanw * this format specified that the odd bits are at current pos and even
2002 1.48.4.2 nathanw * bits at len + current pos
2003 1.48.4.2 nathanw */
2004 1.48.4.2 nathanw u_long *
2005 1.48.4.2 nathanw mfmblkencode(u_long *dp, u_long *rp, u_long *cp, int len)
2006 1.48.4.2 nathanw {
2007 1.48.4.2 nathanw u_long *sdp, *edp, d, dtmp, correct;
2008 1.48.4.2 nathanw
2009 1.48.4.2 nathanw sdp = dp;
2010 1.48.4.2 nathanw edp = dp + len;
2011 1.48.4.2 nathanw
2012 1.48.4.2 nathanw if (*(rp - 1) & 0x1)
2013 1.48.4.2 nathanw correct = 1;
2014 1.48.4.2 nathanw else
2015 1.48.4.2 nathanw correct = 0;
2016 1.48.4.2 nathanw /*
2017 1.48.4.2 nathanw * do odd bits
2018 1.48.4.2 nathanw */
2019 1.48.4.2 nathanw while (dp < edp) {
2020 1.48.4.2 nathanw d = (*dp >> 1) & 0x55555555; /* remove clock bits */
2021 1.48.4.2 nathanw dtmp = d ^ 0x55555555;
2022 1.48.4.2 nathanw d |= ((dtmp >> 1) | 0x80000000) & (dtmp << 1);
2023 1.48.4.2 nathanw /*
2024 1.48.4.2 nathanw * correct upper clock bit if needed
2025 1.48.4.2 nathanw */
2026 1.48.4.2 nathanw if (correct)
2027 1.48.4.2 nathanw d &= 0x7fffffff;
2028 1.48.4.2 nathanw if (d & 0x1)
2029 1.48.4.2 nathanw correct = 1;
2030 1.48.4.2 nathanw else
2031 1.48.4.2 nathanw correct = 0;
2032 1.48.4.2 nathanw /*
2033 1.48.4.2 nathanw * do checksums and store in raw buffer
2034 1.48.4.2 nathanw */
2035 1.48.4.2 nathanw if (cp)
2036 1.48.4.2 nathanw *cp ^= d;
2037 1.48.4.2 nathanw *rp++ = d;
2038 1.48.4.2 nathanw dp++;
2039 1.48.4.2 nathanw }
2040 1.48.4.2 nathanw /*
2041 1.48.4.2 nathanw * do even bits
2042 1.48.4.2 nathanw */
2043 1.48.4.2 nathanw dp = sdp;
2044 1.48.4.2 nathanw while (dp < edp) {
2045 1.48.4.2 nathanw d = *dp & 0x55555555; /* remove clock bits */
2046 1.48.4.2 nathanw dtmp = d ^ 0x55555555;
2047 1.48.4.2 nathanw d |= ((dtmp >> 1) | 0x80000000) & (dtmp << 1);
2048 1.48.4.2 nathanw /*
2049 1.48.4.2 nathanw * correct upper clock bit if needed
2050 1.48.4.2 nathanw */
2051 1.48.4.2 nathanw if (correct)
2052 1.48.4.2 nathanw d &= 0x7fffffff;
2053 1.48.4.2 nathanw if (d & 0x1)
2054 1.48.4.2 nathanw correct = 1;
2055 1.48.4.2 nathanw else
2056 1.48.4.2 nathanw correct = 0;
2057 1.48.4.2 nathanw /*
2058 1.48.4.2 nathanw * do checksums and store in raw buffer
2059 1.48.4.2 nathanw */
2060 1.48.4.2 nathanw if (cp)
2061 1.48.4.2 nathanw *cp ^= d;
2062 1.48.4.2 nathanw *rp++ = d;
2063 1.48.4.2 nathanw dp++;
2064 1.48.4.2 nathanw }
2065 1.48.4.2 nathanw if (cp)
2066 1.48.4.2 nathanw *cp &= 0x55555555;
2067 1.48.4.2 nathanw return(rp);
2068 1.48.4.2 nathanw }
2069 1.48.4.2 nathanw
2070 1.48.4.2 nathanw /*
2071 1.48.4.2 nathanw * decode len longwords of `dp' data in amiga mfm block format (`rp')
2072 1.48.4.2 nathanw * this format specified that the odd bits are at current pos and even
2073 1.48.4.2 nathanw * bits at len + current pos
2074 1.48.4.2 nathanw */
2075 1.48.4.2 nathanw u_long *
2076 1.48.4.2 nathanw mfmblkdecode(u_long *rp, u_long *dp, u_long *cp, int len)
2077 1.48.4.2 nathanw {
2078 1.48.4.2 nathanw u_long o, e;
2079 1.48.4.2 nathanw int cnt;
2080 1.48.4.2 nathanw
2081 1.48.4.2 nathanw cnt = len;
2082 1.48.4.2 nathanw while (cnt--) {
2083 1.48.4.2 nathanw o = *rp;
2084 1.48.4.2 nathanw e = *(rp + len);
2085 1.48.4.2 nathanw if (cp) {
2086 1.48.4.2 nathanw *cp ^= o;
2087 1.48.4.2 nathanw *cp ^= e;
2088 1.48.4.2 nathanw }
2089 1.48.4.2 nathanw o &= 0x55555555;
2090 1.48.4.2 nathanw e &= 0x55555555;
2091 1.48.4.2 nathanw *dp++ = (o << 1) | e;
2092 1.48.4.2 nathanw rp++;
2093 1.48.4.2 nathanw }
2094 1.48.4.2 nathanw if (cp)
2095 1.48.4.2 nathanw *cp &= 0x55555555;
2096 1.48.4.2 nathanw return(rp + len);
2097 1.48.4.2 nathanw }
2098 1.48.4.2 nathanw
2099 1.48.4.2 nathanw /*
2100 1.48.4.2 nathanw * decode len words in standard MFM format to len bytes
2101 1.48.4.2 nathanw * of data.
2102 1.48.4.2 nathanw */
2103 1.48.4.2 nathanw u_short *
2104 1.48.4.2 nathanw msblkdecode(u_short *rp, u_char *cp, int len)
2105 1.48.4.2 nathanw {
2106 1.48.4.2 nathanw while (len--) {
2107 1.48.4.2 nathanw *cp++ = msdecode[*rp & 0x7f] |
2108 1.48.4.2 nathanw (msdecode[(*rp >> 8) & 0x7f] << 4);
2109 1.48.4.2 nathanw rp++;
2110 1.48.4.2 nathanw }
2111 1.48.4.2 nathanw
2112 1.48.4.2 nathanw return(rp);
2113 1.48.4.2 nathanw }
2114 1.48.4.2 nathanw
2115 1.48.4.2 nathanw /*
2116 1.48.4.2 nathanw * encode len bytes of data into len words in standard MFM format.
2117 1.48.4.2 nathanw * If a pointer is supplied for crc, calculate the CRC-16 of the data
2118 1.48.4.2 nathanw * as well.
2119 1.48.4.2 nathanw */
2120 1.48.4.2 nathanw u_short *
2121 1.48.4.2 nathanw msblkencode(u_short *rp, u_char *cp, int len, u_short *crc)
2122 1.48.4.2 nathanw {
2123 1.48.4.2 nathanw u_short td;
2124 1.48.4.2 nathanw u_short mycrc;
2125 1.48.4.2 nathanw
2126 1.48.4.2 nathanw /* preload crc for header (4 bytes)
2127 1.48.4.2 nathanw * or data (anything else)
2128 1.48.4.2 nathanw */
2129 1.48.4.2 nathanw mycrc = (len == 4) ? 0xb230 : 0xe295;
2130 1.48.4.2 nathanw
2131 1.48.4.2 nathanw while (len--) {
2132 1.48.4.2 nathanw td = (msencode[*cp >> 4] << 8) | msencode[*cp & 0x0f];
2133 1.48.4.2 nathanw
2134 1.48.4.2 nathanw /* Check for zeros in top bit of encode and bottom
2135 1.48.4.2 nathanw * bit of previous encode. if so, slap a one in betweem
2136 1.48.4.2 nathanw * them.
2137 1.48.4.2 nathanw */
2138 1.48.4.2 nathanw if ((td & 0x140) == 0)
2139 1.48.4.2 nathanw td |= 0x80;
2140 1.48.4.2 nathanw if ((td & 0x4000) == 0 && (rp[-1] & 1) == 0)
2141 1.48.4.2 nathanw td |= 0x8000;
2142 1.48.4.2 nathanw
2143 1.48.4.2 nathanw *rp++ = td;
2144 1.48.4.2 nathanw
2145 1.48.4.2 nathanw /*
2146 1.48.4.2 nathanw * calc crc if requested
2147 1.48.4.2 nathanw */
2148 1.48.4.2 nathanw if (crc)
2149 1.48.4.2 nathanw mycrc = (mycrc << 8) ^ mscrctab[*cp ^ (mycrc >> 8)];
2150 1.48.4.2 nathanw
2151 1.48.4.2 nathanw cp++;
2152 1.48.4.2 nathanw }
2153 1.48.4.2 nathanw
2154 1.48.4.2 nathanw if (crc)
2155 1.48.4.2 nathanw *crc = mycrc;
2156 1.48.4.2 nathanw
2157 1.48.4.2 nathanw return(rp);
2158 1.48.4.2 nathanw }
2159