Bug Summary

File:dev/isa/fd.c
Warning:line 854, column 3
Value stored to 'read' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name fd.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -D CONFIG_DRM_AMD_DC_DCN3_0 -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /usr/obj/sys/arch/amd64/compile/GENERIC.MP/scan-build/2022-01-12-131800-47421-1 -x c /usr/src/sys/dev/isa/fd.c
1/* $OpenBSD: fd.c,v 1.107 2020/02/07 13:35:08 cheloha Exp $ */
2/* $NetBSD: fd.c,v 1.90 1996/05/12 23:12:03 mycroft Exp $ */
3
4/*-
5 * Copyright (c) 1993, 1994, 1995, 1996 Charles Hannum.
6 * Copyright (c) 1990 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Don Ahn.
11 *
12 * Portions Copyright (c) 1993, 1994 by
13 * jc@irbs.UUCP (John Capo)
14 * vak@zebub.msk.su (Serge Vakulenko)
15 * ache@astral.msk.su (Andrew A. Chernov)
16 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)fd.c 7.4 (Berkeley) 5/25/91
43 */
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/fcntl.h>
49#include <sys/ioctl.h>
50#include <sys/device.h>
51#include <sys/disklabel.h>
52#include <sys/disk.h>
53#include <sys/buf.h>
54#include <sys/malloc.h>
55#include <sys/uio.h>
56#include <sys/mtio.h>
57#include <sys/syslog.h>
58#include <sys/queue.h>
59#include <sys/stat.h>
60#include <sys/timeout.h>
61#include <sys/dkio.h>
62
63#include <machine/cpu.h>
64#include <machine/bus.h>
65#include <machine/intr.h>
66#include <machine/ioctl_fd.h>
67
68#include <dev/isa/isavar.h>
69#include <dev/isa/isadmavar.h>
70#include <dev/isa/fdreg.h>
71
72#if defined(__i386__) || defined(__amd64__1) /* XXX */
73#include <i386/isa/nvram.h>
74#endif
75
76#include <dev/isa/fdlink.h>
77
78/* XXX misuse a flag to identify format operation */
79#define B_FORMAT0x00040000 B_XXX0x00040000
80
81/* fd_type struct now in ioctl_fd.h */
82
83/* The order of entries in the following table is important -- BEWARE! */
84struct fd_type fd_types[] = {
85 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS0x00,"1.44MB" }, /* 1.44MB diskette */
86 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS0x00, "1.2MB" }, /* 1.2 MB AT-diskettes */
87 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS0x01, "360KB/AT" }, /* 360kB in 1.2MB drive */
88 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS0x02, "360KB/PC" }, /* 360kB PC diskettes */
89 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS0x02, "720KB" }, /* 3.5" 720kB diskette */
90 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS0x01, "720KB/x" }, /* 720kB in 1.2MB drive */
91 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS0x02, "360KB/x" }, /* 360kB in 720kB drive */
92 { 36,2,72,2,0xff,0xaf,0x1b,0x54,80,5760,1,FDC_500KBPS0x00,"2.88MB" }, /* 2.88MB diskette */
93 { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS0x00,"1.2MB/[1024bytes/sector]" } /* 1.2 MB japanese format */
94};
95
96/* software state, per disk (with up to 4 disks per ctlr) */
97struct fd_softc {
98 struct device sc_dev;
99 struct disk sc_dk;
100
101 struct fd_type *sc_deftype; /* default type descriptor */
102 struct fd_type *sc_type; /* current type descriptor */
103
104 daddr_t sc_blkno; /* starting block number */
105 int sc_bcount; /* byte count left */
106 int sc_opts; /* user-set options */
107 int sc_skip; /* bytes already transferred */
108 int sc_nblks; /* number of blocks currently transferring */
109 int sc_nbytes; /* number of bytes currently transferring */
110
111 int sc_drive; /* physical unit number */
112 int sc_flags;
113#define FD_OPEN0x01 0x01 /* it's open */
114#define FD_MOTOR0x02 0x02 /* motor should be on */
115#define FD_MOTOR_WAIT0x04 0x04 /* motor coming up */
116 int sc_cylin; /* where we think the head is */
117
118 TAILQ_ENTRY(fd_softc)struct { struct fd_softc *tqe_next; struct fd_softc **tqe_prev
; }
sc_drivechain;
119 int sc_ops; /* I/O ops since last switch */
120 struct bufq sc_bufq; /* pending I/O */
121 struct buf *sc_bp; /* the current I/O */
122 struct timeout fd_motor_on_to;
123 struct timeout fd_motor_off_to;
124 struct timeout fdtimeout_to;
125};
126
127/* floppy driver configuration */
128int fdprobe(struct device *, void *, void *);
129void fdattach(struct device *, struct device *, void *);
130int fdactivate(struct device *, int);
131
132struct cfattach fd_ca = {
133 sizeof(struct fd_softc), fdprobe, fdattach, NULL((void *)0), fdactivate
134};
135
136struct cfdriver fd_cd = {
137 NULL((void *)0), "fd", DV_DISK
138};
139
140int fdgetdisklabel(dev_t, struct fd_softc *, struct disklabel *, int);
141int fd_get_parms(struct fd_softc *);
142void fdstrategy(struct buf *);
143void fdstart(struct fd_softc *);
144int fdintr(struct fdc_softc *);
145
146void fd_set_motor(struct fdc_softc *fdc, int reset);
147void fd_motor_off(void *arg);
148void fd_motor_on(void *arg);
149void fdfinish(struct fd_softc *fd, struct buf *bp);
150int fdformat(dev_t, struct fd_formb *, struct proc *);
151static __inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
152void fdretry(struct fd_softc *);
153void fdtimeout(void *);
154
155int
156fdgetdisklabel(dev_t dev, struct fd_softc *fd, struct disklabel *lp,
157 int spoofonly)
158{
159 bzero(lp, sizeof(struct disklabel))__builtin_bzero((lp), (sizeof(struct disklabel)));
160
161 lp->d_type = DTYPE_FLOPPY10;
162 lp->d_secsize = FD_BSIZE(fd)(128 << fd->sc_type->secsize);
163 lp->d_secpercyl = fd->sc_type->seccyl;
164 lp->d_nsectors = fd->sc_type->sectrac;
165 lp->d_ncylinders = fd->sc_type->tracks;
166 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
167 DL_SETDSIZE(lp, fd->sc_type->size)do { u_int64_t __x = (fd->sc_type->size); (lp)->d_secperunith
= __x >> 32; (lp)->d_secperunit = __x; } while (0)
;
168
169 strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
170 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
171 lp->d_version = 1;
172
173 lp->d_magic = DISKMAGIC((u_int32_t)0x82564557);
174 lp->d_magic2 = DISKMAGIC((u_int32_t)0x82564557);
175 lp->d_checksum = dkcksum(lp);
176
177 /*
178 * Call the generic disklabel extraction routine. If there's
179 * not a label there, fake it.
180 */
181 return readdisklabel(DISKLABELDEV(dev)((((dev_t)((((((((unsigned)(dev) >> 8) & 0xff))) &
0xff) << 8) | (((((((((unsigned)((dev) & 0xff) | (
((dev) & 0xffff0000) >> 8)) / 16))) * 16) + ((2))))
& 0xff) | ((((((((((unsigned)((dev) & 0xff) | (((dev
) & 0xffff0000) >> 8)) / 16))) * 16) + ((2)))) &
0xffff00) << 8)))))
, fdstrategy, lp, spoofonly);
182}
183
184int
185fdprobe(struct device *parent, void *match, void *aux)
186{
187 struct fdc_softc *fdc = (void *)parent;
188 struct cfdata *cf = match;
189 struct fdc_attach_args *fa = aux;
190 int drive = fa->fa_drive;
191 bus_space_tag_t iot = fdc->sc_iot;
192 bus_space_handle_t ioh = fdc->sc_ioh;
193 int n;
194
195 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive)
196 return 0;
197 /*
198 * XXX
199 * This is to work around some odd interactions between this driver
200 * and SMC Ethernet cards.
201 */
202 if (cf->cf_loc[0] == -1 && drive >= 2)
203 return 0;
204
205 /*
206 * We want to keep the flags config gave us.
207 */
208 fa->fa_flags = cf->cf_flags;
209
210 /* select drive and turn on motor */
211 bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive))((iot)->write_1((ioh), (2), (drive | 0x04 | ((1 << drive
) * 0x10))))
;
212 /* wait for motor to spin up */
213 tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(250));
214 out_fdc(iot, ioh, NE7CMD_RECAL7);
215 out_fdc(iot, ioh, drive);
216 /* wait for recalibrate */
217 tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(2000));
218 out_fdc(iot, ioh, NE7CMD_SENSEI8);
219 n = fdcresult(fdc);
220#ifdef FD_DEBUG
221 {
222 int i;
223 printf("fdprobe: status");
224 for (i = 0; i < n; i++)
225 printf(" %x", fdc->sc_status[i]);
226 printf("\n");
227 }
228#endif
229
230 /* turn off motor */
231 tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(250));
232 bus_space_write_1(iot, ioh, fdout, FDO_FRST)((iot)->write_1((ioh), (2), (0x04)));
233
234 /* flags & 0x20 forces the drive to be found even if it won't probe */
235 if (!(fa->fa_flags & 0x20) && (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20))
236 return 0;
237
238 return 1;
239}
240
241/*
242 * Controller is working, and drive responded. Attach it.
243 */
244void
245fdattach(struct device *parent, struct device *self, void *aux)
246{
247 struct fdc_softc *fdc = (void *)parent;
248 struct fd_softc *fd = (void *)self;
249 struct fdc_attach_args *fa = aux;
250 struct fd_type *type = fa->fa_deftype;
251 int drive = fa->fa_drive;
252
253 if (!type || (fa->fa_flags & 0x10)) {
254 /* The config has overridden this. */
255 switch (fa->fa_flags & 0x07) {
256 case 1: /* 2.88MB */
257 type = &fd_types[7];
258 break;
259 case 2: /* 1.44MB */
260 type = &fd_types[0];
261 break;
262 case 3: /* 1.2MB */
263 type = &fd_types[1];
264 break;
265 case 4: /* 720K */
266 type = &fd_types[4];
267 break;
268 case 5: /* 360K */
269 type = &fd_types[3];
270 break;
271 case 6: /* 1.2 MB japanese format */
272 type = &fd_types[8];
273 break;
274#ifdef __alpha__
275 default:
276 /* 1.44MB, how to detect others?
277 * idea from NetBSD -- jay@rootaction.net
278 */
279 type = &fd_types[0];
280#endif
281 }
282 }
283
284 if (type)
285 printf(": %s %d cyl, %d head, %d sec\n", type->name,
286 type->tracks, type->heads, type->sectrac);
287 else
288 printf(": density unknown\n");
289
290 fd->sc_cylin = -1;
291 fd->sc_drive = drive;
292 fd->sc_deftype = type;
293 fdc->sc_type[drive] = FDC_TYPE_DISK;
294 fdc->sc_link.fdlink.sc_fd[drive] = fd;
295
296 /*
297 * Initialize and attach the disk structure.
298 */
299 fd->sc_dk.dk_flags = DKF_NOLABELREAD0x0004;
300 fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
301 bufq_init(&fd->sc_bufq, BUFQ_DEFAULT1);
302 disk_attach(&fd->sc_dev, &fd->sc_dk);
303
304 /* Setup timeout structures */
305 timeout_set(&fd->fd_motor_on_to, fd_motor_on, fd);
306 timeout_set(&fd->fd_motor_off_to, fd_motor_off, fd);
307 timeout_set(&fd->fdtimeout_to, fdtimeout, fd);
308}
309
310int
311fdactivate(struct device *self, int act)
312{
313 struct fd_softc *fd = (void *)self;
314 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
315 int rv = 0;
316
317 switch (act) {
318 case DVACT_SUSPEND3:
319 if (fdc->sc_state != DEVIDLE) {
320 timeout_del(&fd->fd_motor_on_to);
321 timeout_del(&fd->fd_motor_off_to);
322 timeout_del(&fd->fdtimeout_to);
323 fdc->sc_state = IOTIMEDOUT;
324 fdc->sc_errors = 4;
325 }
326 break;
327 case DVACT_POWERDOWN6:
328 fd_motor_off(self);
329 break;
330 }
331
332 return (rv);
333}
334
335/*
336 * Translate nvram type into internal data structure. Return NULL for
337 * none/unknown/unusable.
338 */
339struct fd_type *
340fd_nvtotype(char *fdc, int nvraminfo, int drive)
341{
342#ifdef __alpha__
343 /* Alpha: assume 1.44MB, idea from NetBSD sys/dev/isa/fd.c
344 * -- jay@rootaction.net
345 */
346 return &fd_types[0]; /* 1.44MB */
347#else
348 int type;
349
350 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
351 switch (type) {
352 case NVRAM_DISKETTE_NONE0:
353 return NULL((void *)0);
354 case NVRAM_DISKETTE_12M0x20:
355 return &fd_types[1];
356 case NVRAM_DISKETTE_TYPE50x50:
357 case NVRAM_DISKETTE_TYPE60x60:
358 return &fd_types[7];
359 case NVRAM_DISKETTE_144M0x40:
360 return &fd_types[0];
361 case NVRAM_DISKETTE_360K0x10:
362 return &fd_types[3];
363 case NVRAM_DISKETTE_720K0x30:
364 return &fd_types[4];
365 default:
366 printf("%s: drive %d: unknown device type 0x%x\n",
367 fdc, drive, type);
368 return NULL((void *)0);
369 }
370#endif
371}
372
373static __inline struct fd_type *
374fd_dev_to_type(struct fd_softc *fd, dev_t dev)
375{
376 int type = FDTYPE(dev)((((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) & 0x70) >> 4)
;
377
378 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
379 return NULL((void *)0);
380 return type ? &fd_types[type - 1] : fd->sc_deftype;
381}
382
383void
384fdstrategy(struct buf *bp)
385{
386 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(bp->b_dev)((bp->b_dev & 0x80) >> 7)];
387 int sz;
388 int s;
389 int fd_bsize = FD_BSIZE(fd)(128 << fd->sc_type->secsize);
390 int bf = fd_bsize / DEV_BSIZE(1 << 9);
391
392 /* Valid unit, controller, and request? */
393 if (bp->b_blkno < 0 ||
394 (((bp->b_blkno % bf) != 0 ||
395 (bp->b_bcount % fd_bsize) != 0) &&
396 (bp->b_flags & B_FORMAT0x00040000) == 0)) {
397 bp->b_error = EINVAL22;
398 goto bad;
399 }
400
401 /* If it's a null transfer, return immediately. */
402 if (bp->b_bcount == 0)
403 goto done;
404
405 sz = howmany(bp->b_bcount, DEV_BSIZE)(((bp->b_bcount) + (((1 << 9)) - 1)) / ((1 << 9
)))
;
406
407 if (bp->b_blkno + sz > fd->sc_type->size * bf) {
408 sz = fd->sc_type->size * bf - bp->b_blkno;
409 if (sz == 0)
410 /* If exactly at end of disk, return EOF. */
411 goto done;
412 if (sz < 0) {
413 /* If past end of disk, return EINVAL. */
414 bp->b_error = EINVAL22;
415 goto bad;
416 }
417 /* Otherwise, truncate request. */
418 bp->b_bcount = sz << DEV_BSHIFT9;
419 }
420
421 bp->b_resid = bp->b_bcount;
422
423#ifdef FD_DEBUG
424 printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld sz %d\n",
425 (long long)bp->b_blkno, bp->b_bcount,
426 (long long)fd->sc_blkno, sz);
427#endif
428
429 /* Queue I/O */
430 bufq_queue(&fd->sc_bufq, bp);
431
432 /* Queue transfer on drive, activate drive and controller if idle. */
433 s = splbio()splraise(0x6);
434 timeout_del(&fd->fd_motor_off_to); /* a good idea */
435 if (fd->sc_bp == NULL((void *)0))
436 fdstart(fd);
437#ifdef DIAGNOSTIC1
438 else {
439 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
440 if (fdc->sc_state == DEVIDLE) {
441 printf("fdstrategy: controller inactive\n");
442 fdcstart(fdc);
443 }
444 }
445#endif
446 splx(s)spllower(s);
447 return;
448
449bad:
450 bp->b_flags |= B_ERROR0x00000400;
451done:
452 /* Toss transfer; we're done early. */
453 bp->b_resid = bp->b_bcount;
454 s = splbio()splraise(0x6);
455 biodone(bp);
456 splx(s)spllower(s);
457}
458
459void
460fdstart(struct fd_softc *fd)
461{
462 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
463 int active = !TAILQ_EMPTY(&fdc->sc_link.fdlink.sc_drives)(((&fdc->sc_link.fdlink.sc_drives)->tqh_first) == (
(void *)0))
;
464
465 /* Link into controller queue. */
466 fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
467 TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain)do { (fd)->sc_drivechain.tqe_next = ((void *)0); (fd)->
sc_drivechain.tqe_prev = (&fdc->sc_link.fdlink.sc_drives
)->tqh_last; *(&fdc->sc_link.fdlink.sc_drives)->
tqh_last = (fd); (&fdc->sc_link.fdlink.sc_drives)->
tqh_last = &(fd)->sc_drivechain.tqe_next; } while (0)
;
468
469 /* If controller not already active, start it. */
470 if (!active)
471 fdcstart(fdc);
472}
473
474void
475fdfinish(struct fd_softc *fd, struct buf *bp)
476{
477 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
478
479 splassert(IPL_BIO)do { if (splassert_ctl > 0) { splassert_check(0x6, __func__
); } } while (0)
;
480
481 fd->sc_skip = 0;
482 fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
483
484 /*
485 * Move this drive to the end of the queue to give others a `fair'
486 * chance. We only force a switch if N operations are completed while
487 * another drive is waiting to be serviced, since there is a long motor
488 * startup delay whenever we switch.
489 */
490 if (TAILQ_NEXT(fd, sc_drivechain)((fd)->sc_drivechain.tqe_next) != NULL((void *)0) && ++fd->sc_ops >= 8) {
491 fd->sc_ops = 0;
492 TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain)do { if (((fd)->sc_drivechain.tqe_next) != ((void *)0)) (fd
)->sc_drivechain.tqe_next->sc_drivechain.tqe_prev = (fd
)->sc_drivechain.tqe_prev; else (&fdc->sc_link.fdlink
.sc_drives)->tqh_last = (fd)->sc_drivechain.tqe_prev; *
(fd)->sc_drivechain.tqe_prev = (fd)->sc_drivechain.tqe_next
; ((fd)->sc_drivechain.tqe_prev) = ((void *)-1); ((fd)->
sc_drivechain.tqe_next) = ((void *)-1); } while (0)
;
493 if (fd->sc_bp != NULL((void *)0)) {
494 TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd,do { (fd)->sc_drivechain.tqe_next = ((void *)0); (fd)->
sc_drivechain.tqe_prev = (&fdc->sc_link.fdlink.sc_drives
)->tqh_last; *(&fdc->sc_link.fdlink.sc_drives)->
tqh_last = (fd); (&fdc->sc_link.fdlink.sc_drives)->
tqh_last = &(fd)->sc_drivechain.tqe_next; } while (0)
495 sc_drivechain)do { (fd)->sc_drivechain.tqe_next = ((void *)0); (fd)->
sc_drivechain.tqe_prev = (&fdc->sc_link.fdlink.sc_drives
)->tqh_last; *(&fdc->sc_link.fdlink.sc_drives)->
tqh_last = (fd); (&fdc->sc_link.fdlink.sc_drives)->
tqh_last = &(fd)->sc_drivechain.tqe_next; } while (0)
;
496 }
497 }
498
499 biodone(bp);
500 /* turn off motor 5s from now */
501 timeout_add_sec(&fd->fd_motor_off_to, 5);
502 fdc->sc_state = DEVIDLE;
503}
504
505int
506fdread(dev_t dev, struct uio *uio, int flags)
507{
508 return (physio(fdstrategy, dev, B_READ0x00008000, minphys, uio));
509}
510
511int
512fdwrite(dev_t dev, struct uio *uio, int flags)
513{
514 return (physio(fdstrategy, dev, B_WRITE0x00000000, minphys, uio));
515}
516
517void
518fd_set_motor(struct fdc_softc *fdc, int reset)
519{
520 struct fd_softc *fd;
521 u_char status;
522 int n;
523
524 if ((fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives)((&fdc->sc_link.fdlink.sc_drives)->tqh_first)) != NULL((void *)0))
525 status = fd->sc_drive;
526 else
527 status = 0;
528 if (!reset)
529 status |= FDO_FRST0x04 | FDO_FDMAEN0x08;
530 for (n = 0; n < 4; n++)
531 if ((fd = fdc->sc_link.fdlink.sc_fd[n])
532 && (fd->sc_flags & FD_MOTOR0x02))
533 status |= FDO_MOEN(n)((1 << n) * 0x10);
534 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status)((fdc->sc_iot)->write_1((fdc->sc_ioh), (2), (status)
))
;
535}
536
537void
538fd_motor_off(void *arg)
539{
540 struct fd_softc *fd = arg;
541 int s;
542
543 s = splbio()splraise(0x6);
544 fd->sc_flags &= ~(FD_MOTOR0x02 | FD_MOTOR_WAIT0x04);
545 fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
546 splx(s)spllower(s);
547}
548
549void
550fd_motor_on(void *arg)
551{
552 struct fd_softc *fd = arg;
553 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
554 int s;
555
556 s = splbio()splraise(0x6);
557 fd->sc_flags &= ~FD_MOTOR_WAIT0x04;
558 if ((TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives)((&fdc->sc_link.fdlink.sc_drives)->tqh_first) == fd)
559 && (fdc->sc_state == MOTORWAIT))
560 (void) fdintr(fdc);
561 splx(s)spllower(s);
562}
563
564int
565fdopen(dev_t dev, int flags, int fmt, struct proc *p)
566{
567 int unit, pmask;
568 struct fd_softc *fd;
569 struct fd_type *type;
570
571 unit = FDUNIT(dev)((dev & 0x80) >> 7);
572 if (unit >= fd_cd.cd_ndevs)
573 return ENXIO6;
574 fd = fd_cd.cd_devs[unit];
575 if (fd == 0)
576 return ENXIO6;
577 type = fd_dev_to_type(fd, dev);
578 if (type == NULL((void *)0))
579 return ENXIO6;
580
581 if ((fd->sc_flags & FD_OPEN0x01) != 0 &&
582 fd->sc_type != type)
583 return EBUSY16;
584
585 fd->sc_type = type;
586 fd->sc_cylin = -1;
587 fd->sc_flags |= FD_OPEN0x01;
588
589 /*
590 * Only update the disklabel if we're not open anywhere else.
591 */
592 if (fd->sc_dk.dk_openmask == 0)
593 fdgetdisklabel(dev, fd, fd->sc_dk.dk_label, 0);
594
595 pmask = (1 << FDPART(dev)(((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) & 0x0f)
);
596
597 switch (fmt) {
598 case S_IFCHR0020000:
599 fd->sc_dk.dk_copenmask |= pmask;
600 break;
601
602 case S_IFBLK0060000:
603 fd->sc_dk.dk_bopenmask |= pmask;
604 break;
605 }
606 fd->sc_dk.dk_openmask =
607 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
608
609 return 0;
610}
611
612int
613fdclose(dev_t dev, int flags, int fmt, struct proc *p)
614{
615 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)((dev & 0x80) >> 7)];
616 int pmask = (1 << FDPART(dev)(((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) & 0x0f)
);
617
618 fd->sc_flags &= ~FD_OPEN0x01;
619 fd->sc_opts &= ~FDOPT_NORETRY0x0001;
620
621 switch (fmt) {
622 case S_IFCHR0020000:
623 fd->sc_dk.dk_copenmask &= ~pmask;
624 break;
625
626 case S_IFBLK0060000:
627 fd->sc_dk.dk_bopenmask &= ~pmask;
628 break;
629 }
630 fd->sc_dk.dk_openmask =
631 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
632
633 return (0);
634}
635
636daddr_t
637fdsize(dev_t dev)
638{
639 /* Swapping to floppies would not make sense. */
640 return -1;
641}
642
643int
644fddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
645{
646 /* Not implemented. */
647 return ENXIO6;
648}
649
650/*
651 * Called from the controller.
652 */
653int
654fdintr(struct fdc_softc *fdc)
655{
656#define st0 fdc->sc_status[0]
657#define cyl fdc->sc_status[1]
658 struct fd_softc *fd;
659 struct buf *bp;
660 bus_space_tag_t iot = fdc->sc_iot;
661 bus_space_handle_t ioh = fdc->sc_ioh;
662 bus_space_handle_t ioh_ctl = fdc->sc_ioh_ctl;
663 int read, head, sec, i, nblks, cylin;
664 struct fd_type *type;
665 struct fd_formb *finfo = NULL((void *)0);
666 int fd_bsize;
667
668loop:
669 /* Is there a transfer to this drive? If not, deactivate drive. */
670 fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives)((&fdc->sc_link.fdlink.sc_drives)->tqh_first);
671 if (fd == NULL((void *)0)) {
672 fdc->sc_state = DEVIDLE;
673 return 1;
674 }
675 fd_bsize = FD_BSIZE(fd)(128 << fd->sc_type->secsize);
676
677 bp = fd->sc_bp;
678 if (bp == NULL((void *)0)) {
679 fd->sc_ops = 0;
680 TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain)do { if (((fd)->sc_drivechain.tqe_next) != ((void *)0)) (fd
)->sc_drivechain.tqe_next->sc_drivechain.tqe_prev = (fd
)->sc_drivechain.tqe_prev; else (&fdc->sc_link.fdlink
.sc_drives)->tqh_last = (fd)->sc_drivechain.tqe_prev; *
(fd)->sc_drivechain.tqe_prev = (fd)->sc_drivechain.tqe_next
; ((fd)->sc_drivechain.tqe_prev) = ((void *)-1); ((fd)->
sc_drivechain.tqe_next) = ((void *)-1); } while (0)
;
681 goto loop;
682 }
683
684 if (bp->b_flags & B_FORMAT0x00040000)
685 finfo = (struct fd_formb *)bp->b_data;
686
687 cylin = ((bp->b_blkno * DEV_BSIZE(1 << 9)) + (bp->b_bcount - bp->b_resid)) /
688 (fd_bsize * fd->sc_type->seccyl);
689
690 switch (fdc->sc_state) {
691 case DEVIDLE:
692 fdc->sc_errors = 0;
693 fd->sc_skip = 0;
694 fd->sc_bcount = bp->b_bcount;
695 fd->sc_blkno = bp->b_blkno / (fd_bsize / DEV_BSIZE(1 << 9));
696 timeout_del(&fd->fd_motor_off_to);
697 if ((fd->sc_flags & FD_MOTOR_WAIT0x04) != 0) {
698 fdc->sc_state = MOTORWAIT;
699 return 1;
700 }
701 if ((fd->sc_flags & FD_MOTOR0x02) == 0) {
702 /* Turn on the motor, being careful about pairing. */
703 struct fd_softc *ofd =
704 fdc->sc_link.fdlink.sc_fd[fd->sc_drive ^ 1];
705 if (ofd && ofd->sc_flags & FD_MOTOR0x02) {
706 timeout_del(&ofd->fd_motor_off_to);
707 ofd->sc_flags &= ~(FD_MOTOR0x02 | FD_MOTOR_WAIT0x04);
708 }
709 fd->sc_flags |= FD_MOTOR0x02 | FD_MOTOR_WAIT0x04;
710 fd_set_motor(fdc, 0);
711 fdc->sc_state = MOTORWAIT;
712 /* Allow .25s for motor to stabilize. */
713 timeout_add_msec(&fd->fd_motor_on_to, 250);
714 return 1;
715 }
716 /* Make sure the right drive is selected. */
717 fd_set_motor(fdc, 0);
718
719 /* FALLTHROUGH */
720 case DOSEEK:
721 doseek:
722 if (fd->sc_cylin == cylin)
723 goto doio;
724
725 out_fdc(iot, ioh, NE7CMD_SPECIFY3);/* specify command */
726 out_fdc(iot, ioh, fd->sc_type->steprate);
727 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
728
729 out_fdc(iot, ioh, NE7CMD_SEEK0x0f); /* seek function */
730 out_fdc(iot, ioh, fd->sc_drive); /* drive number */
731 out_fdc(iot, ioh, cylin * fd->sc_type->step);
732
733 fd->sc_cylin = -1;
734 fdc->sc_state = SEEKWAIT;
735
736 fd->sc_dk.dk_seek++;
737 disk_busy(&fd->sc_dk);
738
739 timeout_add_sec(&fd->fdtimeout_to, 4);
740 return 1;
741
742 case DOIO:
743 doio:
744 type = fd->sc_type;
745 if (finfo)
746 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)format_info.structured.idfields[0].cylno) -
747 (char *)finfo;
748 sec = fd->sc_blkno % type->seccyl;
749 nblks = type->seccyl - sec;
750 nblks = min(nblks, fd->sc_bcount / fd_bsize);
751 nblks = min(nblks, FDC_MAXIOSIZE(1 << 12) / fd_bsize);
752 fd->sc_nblks = nblks;
753 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * fd_bsize;
754 head = sec / type->sectrac;
755 sec -= head * type->sectrac;
756#ifdef DIAGNOSTIC1
757 {int block;
758 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
759 if (block != fd->sc_blkno) {
760 panic("fdintr: block %d != blkno %llu", block, fd->sc_blkno);
761 }}
762#endif
763 read = bp->b_flags & B_READ0x00008000 ? DMAMODE_READ1 : DMAMODE_WRITE0;
764 isadma_start(bp->b_data + fd->sc_skip, fd->sc_nbytes,isa_dmastart(isa_dev, (fdc->sc_drqmap), (bp->b_data + fd
->sc_skip), (fd->sc_nbytes), 0, (read), 0x0000|0x0010)
765 fdc->sc_drq, read)isa_dmastart(isa_dev, (fdc->sc_drqmap), (bp->b_data + fd
->sc_skip), (fd->sc_nbytes), 0, (read), 0x0000|0x0010)
;
766 bus_space_write_1(iot, ioh_ctl, fdctl, type->rate)((iot)->write_1((ioh_ctl), (0), (type->rate)));
767#ifdef FD_DEBUG
768 printf("fdintr: %s drive %d track %d head %d sec %d nblks %d\n",
769 read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
770 sec, nblks);
771#endif
772 if (finfo) {
773 /* formatting */
774 if (out_fdc(iot, ioh, NE7CMD_FORMAT0x4d) < 0) {
775 fdc->sc_errors = 4;
776 fdretry(fd);
777 goto loop;
778 }
779 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
780 out_fdc(iot, ioh, finfo->fd_formb_secshiftformat_info.structured.secshift);
781 out_fdc(iot, ioh, finfo->fd_formb_nsecsformat_info.structured.nsecs);
782 out_fdc(iot, ioh, finfo->fd_formb_gaplenformat_info.structured.gaplen);
783 out_fdc(iot, ioh, finfo->fd_formb_fillbyteformat_info.structured.fillbyte);
784 } else {
785 if (read)
786 out_fdc(iot, ioh, NE7CMD_READ0xe6); /* READ */
787 else
788 out_fdc(iot, ioh, NE7CMD_WRITE0xc5);/* WRITE */
789 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
790 out_fdc(iot, ioh, fd->sc_cylin); /* track */
791 out_fdc(iot, ioh, head);
792 out_fdc(iot, ioh, sec + 1); /* sec +1 */
793 out_fdc(iot, ioh, type->secsize); /* sec size */
794 out_fdc(iot, ioh, type->sectrac); /* secs/track */
795 out_fdc(iot, ioh, type->gap1); /* gap1 size */
796 out_fdc(iot, ioh, type->datalen); /* data len */
797 }
798 fdc->sc_state = IOCOMPLETE;
799
800 disk_busy(&fd->sc_dk);
801
802 /* allow 2 seconds for operation */
803 timeout_add_sec(&fd->fdtimeout_to, 2);
804 return 1; /* will return later */
805
806 case SEEKWAIT:
807 timeout_del(&fd->fdtimeout_to);
808 fdc->sc_state = SEEKCOMPLETE;
809 /* allow 1/50 second for heads to settle */
810 timeout_add_msec(&fdc->fdcpseudointr_to, 20);
811 return 1;
812
813 case SEEKCOMPLETE:
814 disk_unbusy(&fd->sc_dk, 0, 0, 0); /* no data on seek */
815
816 /* Make sure seek really happened. */
817 out_fdc(iot, ioh, NE7CMD_SENSEI8);
818 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
819 cyl != cylin * fd->sc_type->step) {
820#ifdef FD_DEBUG
821 fdcstatus(&fd->sc_dev, 2, "seek failed");
822#endif
823 fdretry(fd);
824 goto loop;
825 }
826 fd->sc_cylin = cylin;
827 goto doio;
828
829 case IOTIMEDOUT:
830 isadma_abort(fdc->sc_drq)isa_dmaabort(isa_dev, (fdc->sc_drqmap));
831 case SEEKTIMEDOUT:
832 case RECALTIMEDOUT:
833 case RESETTIMEDOUT:
834 fdretry(fd);
835 goto loop;
836
837 case IOCOMPLETE: /* IO DONE, post-analyze */
838 timeout_del(&fd->fdtimeout_to);
839
840 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
841 fd->sc_blkno, (bp->b_flags & B_READ0x00008000));
842
843 if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
844 isadma_abort(fdc->sc_drq)isa_dmaabort(isa_dev, (fdc->sc_drqmap));
845#ifdef FD_DEBUG
846 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ0x00008000 ?
847 "read failed" : "write failed");
848 printf("blkno %lld nblks %d\n",
849 (long long)fd->sc_blkno, fd->sc_nblks);
850#endif
851 fdretry(fd);
852 goto loop;
853 }
854 read = bp->b_flags & B_READ0x00008000 ? DMAMODE_READ1 : DMAMODE_WRITE0;
Value stored to 'read' is never read
855 isadma_done(fdc->sc_drq)isa_dmadone(isa_dev, (fdc->sc_drqmap));
856 if (fdc->sc_errors) {
857 diskerr(bp, "fd", "soft error", LOG_PRINTF-1,
858 fd->sc_skip / fd_bsize, (struct disklabel *)NULL((void *)0));
859 printf("\n");
860 fdc->sc_errors = 0;
861 }
862
863 fd->sc_blkno += fd->sc_nblks;
864 fd->sc_skip += fd->sc_nbytes;
865 fd->sc_bcount -= fd->sc_nbytes;
866 bp->b_resid -= fd->sc_nbytes;
867 if (!finfo && fd->sc_bcount > 0) {
868 cylin = fd->sc_blkno / fd->sc_type->seccyl;
869 goto doseek;
870 }
871 fdfinish(fd, bp);
872 goto loop;
873
874 case DORESET:
875 /* try a reset, keep motor on */
876 fd_set_motor(fdc, 1);
877 delay(100)(*delay_func)(100);
878 fd_set_motor(fdc, 0);
879 fdc->sc_state = RESETCOMPLETE;
880 timeout_add_msec(&fd->fdtimeout_to, 500);
881 return 1; /* will return later */
882
883 case RESETCOMPLETE:
884 timeout_del(&fd->fdtimeout_to);
885 /* clear the controller output buffer */
886 for (i = 0; i < 4; i++) {
887 out_fdc(iot, ioh, NE7CMD_SENSEI8);
888 (void) fdcresult(fdc);
889 }
890
891 /* FALLTHROUGH */
892 case DORECAL:
893 out_fdc(iot, ioh, NE7CMD_RECAL7); /* recal function */
894 out_fdc(iot, ioh, fd->sc_drive);
895 fdc->sc_state = RECALWAIT;
896 timeout_add_sec(&fd->fdtimeout_to, 5);
897 return 1; /* will return later */
898
899 case RECALWAIT:
900 timeout_del(&fd->fdtimeout_to);
901 fdc->sc_state = RECALCOMPLETE;
902 /* allow 1/30 second for heads to settle */
903 timeout_add_msec(&fdc->fdcpseudointr_to, 1000 / 30);
904 return 1; /* will return later */
905
906 case RECALCOMPLETE:
907 out_fdc(iot, ioh, NE7CMD_SENSEI8);
908 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
909#ifdef FD_DEBUG
910 fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
911#endif
912 fdretry(fd);
913 goto loop;
914 }
915 fd->sc_cylin = 0;
916 goto doseek;
917
918 case MOTORWAIT:
919 if (fd->sc_flags & FD_MOTOR_WAIT0x04)
920 return 1; /* time's not up yet */
921 goto doseek;
922
923 default:
924 fdcstatus(&fd->sc_dev, 0, "stray interrupt");
925 return 1;
926 }
927#ifdef DIAGNOSTIC1
928 panic("fdintr: impossible");
929#endif
930#undef st0
931#undef cyl
932}
933
934void
935fdtimeout(void *arg)
936{
937 struct fd_softc *fd = arg;
938 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
939 int s;
940
941 s = splbio()splraise(0x6);
942#ifdef DEBUG
943 log(LOG_ERR3,"fdtimeout: state %d\n", fdc->sc_state);
944#endif
945 fdcstatus(&fd->sc_dev, 0, "timeout");
946
947 if (fd->sc_bp != NULL((void *)0))
948 fdc->sc_state++;
949 else
950 fdc->sc_state = DEVIDLE;
951
952 (void) fdintr(fdc);
953 splx(s)spllower(s);
954}
955
956void
957fdretry(struct fd_softc *fd)
958{
959 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
960 struct buf *bp = fd->sc_bp;
961
962 if (fd->sc_opts & FDOPT_NORETRY0x0001)
963 goto fail;
964 switch (fdc->sc_errors) {
965 case 0:
966 /* try again */
967 fdc->sc_state = DOSEEK;
968 break;
969
970 case 1: case 2: case 3:
971 /* didn't work; try recalibrating */
972 fdc->sc_state = DORECAL;
973 break;
974
975 case 4:
976 /* still no go; reset the bastard */
977 fdc->sc_state = DORESET;
978 break;
979
980 default:
981 fail:
982 diskerr(bp, "fd", "hard error", LOG_PRINTF-1,
983 fd->sc_skip / FD_BSIZE(fd)(128 << fd->sc_type->secsize), (struct disklabel *)NULL((void *)0));
984 printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
985 fdc->sc_status[0], NE7_ST0BITS"\020\010invld\007abnrml\006seek_cmplt\005equ_chck\004drive_notrdy\003top_head",
986 fdc->sc_status[1], NE7_ST1BITS"\020\010end_of_cyl\006bad_crc\005data_overrun\003sec_not_fnd\002write_protect\001no_am",
987 fdc->sc_status[2], NE7_ST2BITS"\020\007ctrl_mrk\006bad_crc\005wrong_cyl\004scn_eq\003scn_not_fnd\002bad_cyl\001no_dam",
988 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
989
990 bp->b_flags |= B_ERROR0x00000400;
991 bp->b_error = EIO5;
992 bp->b_resid = bp->b_bcount;
993 fdfinish(fd, bp);
994 }
995 fdc->sc_errors++;
996}
997
998int
999fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1000{
1001 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)((dev & 0x80) >> 7)];
1002 struct disklabel *lp;
1003 int error;
1004
1005 switch (cmd) {
1006 case MTIOCTOP((unsigned long)0x80000000 | ((sizeof(struct mtop) & 0x1fff
) << 16) | ((('m')) << 8) | ((1)))
:
1007 if (((struct mtop *)addr)->mt_op != MTOFFL6)
1008 return EIO5;
1009 return (0);
1010
1011 case DIOCRLDINFO((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('d')) << 8) | ((115)))
:
1012 lp = malloc(sizeof(*lp), M_TEMP127, M_WAITOK0x0001);
1013 fdgetdisklabel(dev, fd, lp, 0);
1014 bcopy(lp, fd->sc_dk.dk_label, sizeof(*lp));
1015 free(lp, M_TEMP127, sizeof(*lp));
1016 return 0;
1017
1018 case DIOCGPDINFO((unsigned long)0x40000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((114)))
:
1019 fdgetdisklabel(dev, fd, (struct disklabel *)addr, 1);
1020 return 0;
1021
1022 case DIOCGDINFO((unsigned long)0x40000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((101)))
:
1023 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1024 return 0;
1025
1026 case DIOCGPART((unsigned long)0x80000000 | ((sizeof(struct partinfo) & 0x1fff
) << 16) | ((('d')) << 8) | ((104)))
:
1027 ((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
1028 ((struct partinfo *)addr)->part =
1029 &fd->sc_dk.dk_label->d_partitions[FDPART(dev)(((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) & 0x0f)
];
1030 return 0;
1031
1032 case DIOCWDINFO((unsigned long)0x80000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((103)))
:
1033 case DIOCSDINFO((unsigned long)0x80000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((102)))
:
1034 if ((flag & FWRITE0x0002) == 0)
1035 return EBADF9;
1036
1037 error = setdisklabel(fd->sc_dk.dk_label,
1038 (struct disklabel *)addr, 0);
1039 if (error == 0) {
1040 if (cmd == DIOCWDINFO((unsigned long)0x80000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((103)))
)
1041 error = writedisklabel(DISKLABELDEV(dev)((((dev_t)((((((((unsigned)(dev) >> 8) & 0xff))) &
0xff) << 8) | (((((((((unsigned)((dev) & 0xff) | (
((dev) & 0xffff0000) >> 8)) / 16))) * 16) + ((2))))
& 0xff) | ((((((((((unsigned)((dev) & 0xff) | (((dev
) & 0xffff0000) >> 8)) / 16))) * 16) + ((2)))) &
0xffff00) << 8)))))
,
1042 fdstrategy, fd->sc_dk.dk_label);
1043 }
1044 return error;
1045
1046 case FD_FORM((unsigned long)0x80000000 | ((sizeof(struct fd_formb) & 0x1fff
) << 16) | ((('F')) << 8) | ((61)))
:
1047 if((flag & FWRITE0x0002) == 0)
1048 return EBADF9; /* must be opened for writing */
1049 else if(((struct fd_formb *)addr)->format_version !=
1050 FD_FORMAT_VERSION110)
1051 return EINVAL22; /* wrong version of formatting prog */
1052 else
1053 return fdformat(dev, (struct fd_formb *)addr, p);
1054 break;
1055
1056 case FD_GTYPE((unsigned long)0x40000000 | ((sizeof(struct fd_type) & 0x1fff
) << 16) | ((('F')) << 8) | ((62)))
: /* get drive type */
1057 *(struct fd_type *)addr = *fd->sc_type;
1058 return 0;
1059
1060 case FD_GOPTS((unsigned long)0x40000000 | ((sizeof(int) & 0x1fff) <<
16) | ((('F')) << 8) | ((64)))
: /* get drive options */
1061 *(int *)addr = fd->sc_opts;
1062 return 0;
1063
1064 case FD_SOPTS((unsigned long)0x80000000 | ((sizeof(int) & 0x1fff) <<
16) | ((('F')) << 8) | ((65)))
: /* set drive options */
1065 fd->sc_opts = *(int *)addr;
1066 return 0;
1067
1068 default:
1069 return ENOTTY25;
1070 }
1071
1072#ifdef DIAGNOSTIC1
1073 panic("fdioctl: impossible");
1074#endif
1075}
1076
1077int
1078fdformat(dev_t dev, struct fd_formb *finfo, struct proc *p)
1079{
1080 int rv = 0;
1081 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)((dev & 0x80) >> 7)];
1082 struct fd_type *type = fd->sc_type;
1083 struct buf *bp;
1084 int fd_bsize = FD_BSIZE(fd)(128 << fd->sc_type->secsize);
1085
1086 /* set up a buffer header for fdstrategy() */
1087 bp = malloc(sizeof(*bp), M_TEMP127, M_NOWAIT0x0002 | M_ZERO0x0008);
1088 if (bp == NULL((void *)0))
1089 return ENOBUFS55;
1090
1091 bp->b_flags = B_BUSY0x00000010 | B_PHYS0x00002000 | B_FORMAT0x00040000 | B_RAW0x00004000;
1092 bp->b_proc = p;
1093 bp->b_dev = dev;
1094
1095 /*
1096 * calculate a fake blkno, so fdstrategy() would initiate a
1097 * seek to the requested cylinder
1098 */
1099 bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1100 + finfo->head * type->sectrac) * fd_bsize / DEV_BSIZE(1 << 9);
1101
1102 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecsformat_info.structured.nsecs;
1103 bp->b_data = (caddr_t)finfo;
1104
1105#ifdef DEBUG
1106 printf("fdformat: blkno %llx count %lx\n", bp->b_blkno, bp->b_bcount);
1107#endif
1108
1109 /* now do the format */
1110 fdstrategy(bp);
1111
1112 /* ...and wait for it to complete */
1113 rv = biowait(bp);
1114 free(bp, M_TEMP127, sizeof(*bp));
1115 return (rv);
1116}