wd.c revision 1.113 1 1.113 mycroft /* $NetBSD: wd.c,v 1.113 1994/11/22 09:33:59 mycroft Exp $ */
2 1.100 cgd
3 1.11 deraadt /*
4 1.113 mycroft * Copyright (c) 1994 Charles Hannum. All rights reserved.
5 1.113 mycroft *
6 1.113 mycroft * Redistribution and use in source and binary forms, with or without
7 1.113 mycroft * modification, are permitted provided that the following conditions
8 1.113 mycroft * are met:
9 1.113 mycroft * 1. Redistributions of source code must retain the above copyright
10 1.113 mycroft * notice, this list of conditions and the following disclaimer.
11 1.113 mycroft * 2. Redistributions in binary form must reproduce the above copyright
12 1.113 mycroft * notice, this list of conditions and the following disclaimer in the
13 1.113 mycroft * documentation and/or other materials provided with the distribution.
14 1.113 mycroft * 3. All advertising materials mentioning features or use of this software
15 1.113 mycroft * must display the following acknowledgement:
16 1.113 mycroft * This product includes software developed by Charles Hannum.
17 1.113 mycroft * 4. The name of the author may not be used to endorse or promote products
18 1.113 mycroft * derived from this software without specific prior written permission.
19 1.113 mycroft *
20 1.1 cgd * Copyright (c) 1990 The Regents of the University of California.
21 1.1 cgd * All rights reserved.
22 1.1 cgd *
23 1.1 cgd * This code is derived from software contributed to Berkeley by
24 1.1 cgd * William Jolitz.
25 1.1 cgd *
26 1.1 cgd * Redistribution and use in source and binary forms, with or without
27 1.1 cgd * modification, are permitted provided that the following conditions
28 1.1 cgd * are met:
29 1.1 cgd * 1. Redistributions of source code must retain the above copyright
30 1.1 cgd * notice, this list of conditions and the following disclaimer.
31 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
32 1.1 cgd * notice, this list of conditions and the following disclaimer in the
33 1.1 cgd * documentation and/or other materials provided with the distribution.
34 1.1 cgd * 3. All advertising materials mentioning features or use of this software
35 1.1 cgd * must display the following acknowledgement:
36 1.1 cgd * This product includes software developed by the University of
37 1.1 cgd * California, Berkeley and its contributors.
38 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
39 1.1 cgd * may be used to endorse or promote products derived from this software
40 1.1 cgd * without specific prior written permission.
41 1.1 cgd *
42 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 1.1 cgd * SUCH DAMAGE.
53 1.1 cgd *
54 1.100 cgd * @(#)wd.c 7.2 (Berkeley) 5/9/91
55 1.1 cgd */
56 1.1 cgd
57 1.46 mycroft #define INSTRUMENT /* instrumentation stuff by Brad Parker */
58 1.1 cgd
59 1.29 mycroft #include <sys/param.h>
60 1.29 mycroft #include <sys/dkbad.h>
61 1.29 mycroft #include <sys/systm.h>
62 1.43 mycroft #include <sys/kernel.h>
63 1.29 mycroft #include <sys/conf.h>
64 1.29 mycroft #include <sys/file.h>
65 1.29 mycroft #include <sys/stat.h>
66 1.29 mycroft #include <sys/ioctl.h>
67 1.29 mycroft #include <sys/buf.h>
68 1.29 mycroft #include <sys/uio.h>
69 1.29 mycroft #include <sys/malloc.h>
70 1.64 mycroft #include <sys/device.h>
71 1.94 mycroft #include <sys/disklabel.h>
72 1.94 mycroft #include <sys/disk.h>
73 1.29 mycroft #include <sys/syslog.h>
74 1.3 deraadt #ifdef INSTRUMENT
75 1.29 mycroft #include <sys/dkstat.h>
76 1.3 deraadt #endif
77 1.29 mycroft
78 1.29 mycroft #include <vm/vm.h>
79 1.29 mycroft
80 1.29 mycroft #include <machine/cpu.h>
81 1.29 mycroft #include <machine/pio.h>
82 1.29 mycroft
83 1.74 mycroft #include <i386/isa/isavar.h>
84 1.29 mycroft #include <i386/isa/wdreg.h>
85 1.1 cgd
86 1.41 mycroft #define WDCNDELAY 100000 /* delay = 100us; so 10s for a controller state change */
87 1.41 mycroft #define WDCDELAY 100
88 1.98 mycroft
89 1.98 mycroft #define WAITTIME (4 * hz) /* time to wait for a completion */
90 1.98 mycroft #define RECOVERYTIME (hz / 2) /* time to recover from an error */
91 1.19 deraadt
92 1.89 mycroft #if 0
93 1.64 mycroft /* If you enable this, it will report any delays more than 100us * N long. */
94 1.69 mycroft #define WDCNDELAY_DEBUG 10
95 1.51 mycroft #endif
96 1.23 deraadt
97 1.21 deraadt #define WDIORETRIES 5 /* number of retries before giving up */
98 1.1 cgd
99 1.92 cgd #define WDUNIT(dev) DISKUNIT(dev)
100 1.92 cgd #define WDPART(dev) DISKPART(dev)
101 1.92 cgd #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
102 1.92 cgd
103 1.92 cgd #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
104 1.1 cgd
105 1.1 cgd #define b_cylin b_resid /* cylinder number for doing IO to */
106 1.1 cgd /* shares an entry in the buf struct */
107 1.1 cgd
108 1.1 cgd /*
109 1.37 mycroft * Drive status.
110 1.1 cgd */
111 1.64 mycroft struct wd_softc {
112 1.64 mycroft struct device sc_dev;
113 1.93 mycroft struct dkdevice sc_dk;
114 1.9 deraadt
115 1.64 mycroft long sc_bcount; /* byte count left */
116 1.64 mycroft short sc_skip; /* blocks already transferred */
117 1.74 mycroft char sc_drive; /* physical unit number */
118 1.64 mycroft char sc_state; /* control state */
119 1.113 mycroft #define RECAL 0 /* recalibrate */
120 1.113 mycroft #define RECAL_WAIT 1 /* done recalibrating */
121 1.113 mycroft #define GEOMETRY 2 /* upload geometry */
122 1.113 mycroft #define GEOMETRY_WAIT 3 /* done uploading geometry */
123 1.113 mycroft #define MULTIMODE 4 /* set multiple mode */
124 1.113 mycroft #define MULTIMODE_WAIT 5 /* done setting multiple mode */
125 1.113 mycroft #define OPEN 6 /* done with open */
126 1.113 mycroft char sc_mode; /* transfer mode */
127 1.113 mycroft #define WDM_PIOSINGLE 0 /* single-sector PIO */
128 1.113 mycroft #define WDM_PIOMULTI 1 /* multi-sector PIO */
129 1.113 mycroft #define WDM_DMA 2 /* DMA */
130 1.113 mycroft u_char sc_multiple; /* multiple for WDM_PIOMULTI */
131 1.93 mycroft u_char sc_flags; /* drive characteistics found */
132 1.108 mycroft #define WDF_LOCKED 0x01
133 1.108 mycroft #define WDF_WANTED 0x02
134 1.108 mycroft #define WDF_LOADED 0x04
135 1.113 mycroft #define WDF_BSDLABEL 0x08 /* has a BSD disk label */
136 1.113 mycroft #define WDF_WLABEL 0x10 /* label is writable */
137 1.78 mycroft TAILQ_ENTRY(wd_softc) sc_drivechain;
138 1.64 mycroft struct buf sc_q;
139 1.64 mycroft struct wdparams sc_params; /* ESDI/IDE drive/controller parameters */
140 1.64 mycroft long sc_badsect[127]; /* 126 plus trailing -1 marker */
141 1.74 mycroft };
142 1.64 mycroft
143 1.64 mycroft struct wdc_softc {
144 1.64 mycroft struct device sc_dev;
145 1.76 mycroft struct intrhand sc_ih;
146 1.64 mycroft
147 1.69 mycroft u_char sc_flags;
148 1.113 mycroft #define WDCF_ACTIVE 0x01 /* controller is active */
149 1.113 mycroft #define WDCF_SINGLE 0x02 /* sector at a time mode */
150 1.113 mycroft #define WDCF_ERROR 0x04 /* processing a disk error */
151 1.64 mycroft u_char sc_status; /* copy of status register */
152 1.64 mycroft u_char sc_error; /* copy of error register */
153 1.113 mycroft int sc_iobase; /* I/O port base */
154 1.113 mycroft int sc_drq; /* DMA channel */
155 1.78 mycroft int sc_errors; /* count of errors during current transfer */
156 1.113 mycroft int sc_nblks; /* number of blocks currently transferring */
157 1.78 mycroft TAILQ_HEAD(drivehead, wd_softc) sc_drives;
158 1.74 mycroft };
159 1.3 deraadt
160 1.102 mycroft int wdcprobe __P((struct device *, void *, void *));
161 1.102 mycroft void wdcattach __P((struct device *, struct device *, void *));
162 1.3 deraadt
163 1.74 mycroft struct cfdriver wdccd = {
164 1.105 mycroft NULL, "wdc", wdcprobe, wdcattach, DV_DULL, sizeof(struct wd_softc)
165 1.1 cgd };
166 1.1 cgd
167 1.102 mycroft int wdprobe __P((struct device *, void *, void *));
168 1.102 mycroft void wdattach __P((struct device *, struct device *, void *));
169 1.102 mycroft
170 1.74 mycroft struct cfdriver wdcd = {
171 1.74 mycroft NULL, "wd", wdprobe, wdattach, DV_DISK, sizeof(struct wd_softc)
172 1.65 mycroft };
173 1.108 mycroft
174 1.108 mycroft void wdgetdisklabel __P((struct wd_softc *));
175 1.108 mycroft int wd_get_parms __P((struct wd_softc *));
176 1.108 mycroft void wdstrategy __P((struct buf *));
177 1.108 mycroft void wdstart __P((struct wd_softc *));
178 1.108 mycroft
179 1.102 mycroft struct dkdriver wddkdriver = { wdstrategy };
180 1.65 mycroft
181 1.64 mycroft void wdfinish __P((struct wd_softc *, struct buf *));
182 1.103 mycroft int wdcintr __P((struct wdc_softc *));
183 1.64 mycroft static void wdcstart __P((struct wdc_softc *));
184 1.64 mycroft static int wdcommand __P((struct wd_softc *, int, int, int, int, int));
185 1.105 mycroft static int wdcommandshort __P((struct wdc_softc *, int, int));
186 1.69 mycroft static int wdcontrol __P((struct wd_softc *));
187 1.64 mycroft static int wdsetctlr __P((struct wd_softc *));
188 1.64 mycroft static void bad144intern __P((struct wd_softc *));
189 1.64 mycroft static int wdcreset __P((struct wdc_softc *));
190 1.81 cgd static void wdcrestart __P((void *arg));
191 1.64 mycroft static void wdcunwedge __P((struct wdc_softc *));
192 1.81 cgd static void wdctimeout __P((void *arg));
193 1.64 mycroft static void wderror __P((void *, struct buf *, char *));
194 1.64 mycroft int wdcwait __P((struct wdc_softc *, int));
195 1.87 mycroft /* ST506 spec says that if READY or SEEKCMPLT go off, then the read or write
196 1.87 mycroft command is aborted. */
197 1.110 mycroft #define wait_for_drq(d) wdcwait(d, WDCS_DRDY | WDCS_DSC | WDCS_DRQ)
198 1.110 mycroft #define wait_for_ready(d) wdcwait(d, WDCS_DRDY | WDCS_DSC)
199 1.64 mycroft #define wait_for_unbusy(d) wdcwait(d, 0)
200 1.1 cgd
201 1.1 cgd /*
202 1.1 cgd * Probe for controller.
203 1.1 cgd */
204 1.1 cgd int
205 1.102 mycroft wdcprobe(parent, match, aux)
206 1.102 mycroft struct device *parent;
207 1.102 mycroft void *match, *aux;
208 1.1 cgd {
209 1.102 mycroft struct wdc_softc *wdc = match;
210 1.74 mycroft struct isa_attach_args *ia = aux;
211 1.107 mycroft int iobase;
212 1.1 cgd
213 1.74 mycroft wdc->sc_iobase = iobase = ia->ia_iobase;
214 1.64 mycroft
215 1.64 mycroft /* Check if we have registers that work. */
216 1.64 mycroft outb(iobase+wd_error, 0x5a); /* Error register not writable. */
217 1.64 mycroft outb(iobase+wd_cyl_lo, 0xa5); /* But all of cyllo are implemented. */
218 1.64 mycroft if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo) != 0xa5)
219 1.64 mycroft return 0;
220 1.3 deraadt
221 1.64 mycroft if (wdcreset(wdc) != 0) {
222 1.64 mycroft delay(500000);
223 1.64 mycroft if (wdcreset(wdc) != 0)
224 1.64 mycroft return 0;
225 1.64 mycroft }
226 1.1 cgd
227 1.105 mycroft outb(iobase+wd_sdh, WDSD_IBM | 0);
228 1.105 mycroft
229 1.105 mycroft /* Wait for controller to become ready. */
230 1.105 mycroft if (wait_for_unbusy(wdc) < 0)
231 1.105 mycroft return 0;
232 1.105 mycroft
233 1.105 mycroft /* Send command. */
234 1.105 mycroft outb(iobase+wd_command, WDCC_DIAGNOSE);
235 1.105 mycroft
236 1.105 mycroft /* Wait for command to complete. */
237 1.105 mycroft if (wait_for_unbusy(wdc) != 0)
238 1.105 mycroft return 0;
239 1.3 deraadt
240 1.74 mycroft ia->ia_iosize = 8;
241 1.74 mycroft ia->ia_msize = 0;
242 1.74 mycroft return 1;
243 1.66 mycroft }
244 1.66 mycroft
245 1.74 mycroft struct wdc_attach_args {
246 1.74 mycroft int wa_drive;
247 1.74 mycroft };
248 1.74 mycroft
249 1.66 mycroft int
250 1.74 mycroft wdprint(aux, wdc)
251 1.74 mycroft void *aux;
252 1.74 mycroft char *wdc;
253 1.74 mycroft {
254 1.74 mycroft struct wdc_attach_args *wa = aux;
255 1.74 mycroft
256 1.74 mycroft if (!wdc)
257 1.74 mycroft printf(" drive %d", wa->wa_drive);
258 1.74 mycroft return QUIET;
259 1.66 mycroft }
260 1.66 mycroft
261 1.74 mycroft void
262 1.74 mycroft wdcattach(parent, self, aux)
263 1.74 mycroft struct device *parent, *self;
264 1.74 mycroft void *aux;
265 1.66 mycroft {
266 1.74 mycroft struct wdc_softc *wdc = (void *)self;
267 1.76 mycroft struct isa_attach_args *ia = aux;
268 1.74 mycroft struct wdc_attach_args wa;
269 1.67 mycroft
270 1.102 mycroft TAILQ_INIT(&wdc->sc_drives);
271 1.113 mycroft wdc->sc_drq = ia->ia_drq;
272 1.102 mycroft
273 1.74 mycroft printf("\n");
274 1.74 mycroft
275 1.76 mycroft wdc->sc_ih.ih_fun = wdcintr;
276 1.76 mycroft wdc->sc_ih.ih_arg = wdc;
277 1.76 mycroft wdc->sc_ih.ih_level = IPL_BIO;
278 1.76 mycroft intr_establish(ia->ia_irq, &wdc->sc_ih);
279 1.102 mycroft
280 1.102 mycroft for (wa.wa_drive = 0; wa.wa_drive < 2; wa.wa_drive++)
281 1.102 mycroft (void)config_found(self, (void *)&wa, wdprint);
282 1.1 cgd }
283 1.1 cgd
284 1.1 cgd int
285 1.102 mycroft wdprobe(parent, match, aux)
286 1.102 mycroft struct device *parent;
287 1.102 mycroft void *match, *aux;
288 1.74 mycroft {
289 1.105 mycroft struct wdc_softc *wdc = (void *)parent;
290 1.105 mycroft struct cfdata *cf = match;
291 1.74 mycroft struct wdc_attach_args *wa = aux;
292 1.74 mycroft int drive = wa->wa_drive;
293 1.3 deraadt
294 1.102 mycroft if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive)
295 1.11 deraadt return 0;
296 1.3 deraadt
297 1.110 mycroft if (wdcommandshort(wdc, drive, WDCC_RECAL) != 0 ||
298 1.105 mycroft wait_for_ready(wdc) != 0)
299 1.74 mycroft return 0;
300 1.64 mycroft
301 1.74 mycroft return 1;
302 1.74 mycroft }
303 1.64 mycroft
304 1.74 mycroft void
305 1.74 mycroft wdattach(parent, self, aux)
306 1.74 mycroft struct device *parent, *self;
307 1.74 mycroft void *aux;
308 1.74 mycroft {
309 1.74 mycroft struct wd_softc *wd = (void *)self;
310 1.113 mycroft struct wdc_softc *wdc = (void *)parent;
311 1.106 mycroft struct wdc_attach_args *wa = aux;
312 1.74 mycroft int i, blank;
313 1.56 mycroft
314 1.106 mycroft wd->sc_drive = wa->wa_drive;
315 1.106 mycroft
316 1.108 mycroft wd_get_parms(wd);
317 1.108 mycroft printf(": %dMB, %d cyl, %d head, %d sec, %d bytes/sec <",
318 1.110 mycroft wd->sc_params.wdp_cylinders *
319 1.108 mycroft (wd->sc_params.wdp_heads * wd->sc_params.wdp_sectors) /
320 1.108 mycroft (1048576 / DEV_BSIZE),
321 1.110 mycroft wd->sc_params.wdp_cylinders,
322 1.108 mycroft wd->sc_params.wdp_heads,
323 1.108 mycroft wd->sc_params.wdp_sectors,
324 1.108 mycroft DEV_BSIZE);
325 1.64 mycroft for (i = blank = 0; i < sizeof(wd->sc_params.wdp_model); i++) {
326 1.64 mycroft char c = wd->sc_params.wdp_model[i];
327 1.56 mycroft if (c == '\0')
328 1.56 mycroft break;
329 1.56 mycroft if (c != ' ') {
330 1.56 mycroft if (blank)
331 1.56 mycroft printf(" %c", c);
332 1.56 mycroft else
333 1.56 mycroft printf("%c", c);
334 1.56 mycroft blank = 0;
335 1.56 mycroft } else
336 1.56 mycroft blank = 1;
337 1.56 mycroft }
338 1.56 mycroft printf(">\n");
339 1.113 mycroft
340 1.113 mycroft if ((wd->sc_params.wdp_capabilities & WD_CAP_DMA) != 0 &&
341 1.113 mycroft wdc->sc_drq != DRQUNK) {
342 1.113 mycroft wd->sc_mode = WDM_DMA;
343 1.113 mycroft } else if (wd->sc_params.wdp_maxmulti > 1) {
344 1.113 mycroft wd->sc_mode = WDM_PIOMULTI;
345 1.113 mycroft wd->sc_multiple = min(wd->sc_params.wdp_maxmulti, 8);
346 1.113 mycroft } else {
347 1.113 mycroft wd->sc_mode = WDM_PIOSINGLE;
348 1.113 mycroft wd->sc_multiple = 1;
349 1.113 mycroft }
350 1.113 mycroft
351 1.113 mycroft if (wd->sc_mode == WDM_DMA)
352 1.113 mycroft printf("%s: using dma\n", wd->sc_dev.dv_xname);
353 1.113 mycroft else
354 1.113 mycroft printf("%s: using %d-sector pio\n", wd->sc_dev.dv_xname,
355 1.113 mycroft wd->sc_multiple);
356 1.113 mycroft
357 1.102 mycroft wd->sc_dk.dk_driver = &wddkdriver;
358 1.1 cgd }
359 1.1 cgd
360 1.64 mycroft /*
361 1.64 mycroft * Read/write routine for a buffer. Finds the proper unit, range checks
362 1.64 mycroft * arguments, and schedules the transfer. Does not wait for the transfer to
363 1.64 mycroft * complete. Multi-page transfers are supported. All I/O requests must be a
364 1.64 mycroft * multiple of a sector in length.
365 1.1 cgd */
366 1.64 mycroft void
367 1.55 mycroft wdstrategy(bp)
368 1.55 mycroft struct buf *bp;
369 1.1 cgd {
370 1.64 mycroft struct wd_softc *wd; /* disk unit to do the IO */
371 1.108 mycroft int unit = WDUNIT(bp->b_dev);
372 1.37 mycroft int s;
373 1.3 deraadt
374 1.64 mycroft /* Valid unit, controller, and request? */
375 1.108 mycroft if (unit >= wdcd.cd_ndevs ||
376 1.108 mycroft (wd = wdcd.cd_devs[unit]) == 0 ||
377 1.85 mycroft bp->b_blkno < 0 ||
378 1.85 mycroft (bp->b_bcount % DEV_BSIZE) != 0 ||
379 1.85 mycroft (bp->b_bcount / DEV_BSIZE) >= (1 << NBBY)) {
380 1.1 cgd bp->b_error = EINVAL;
381 1.93 mycroft goto bad;
382 1.1 cgd }
383 1.3 deraadt
384 1.108 mycroft #if 0
385 1.64 mycroft /* "Soft" write protect check. */
386 1.64 mycroft if ((wd->sc_flags & WDF_WRITEPROT) && (bp->b_flags & B_READ) == 0) {
387 1.1 cgd bp->b_error = EROFS;
388 1.93 mycroft goto bad;
389 1.1 cgd }
390 1.108 mycroft #endif
391 1.3 deraadt
392 1.93 mycroft /* If it's a null transfer, return immediately. */
393 1.93 mycroft if (bp->b_bcount == 0)
394 1.93 mycroft goto done;
395 1.93 mycroft
396 1.64 mycroft /* Have partitions and want to use them? */
397 1.93 mycroft if (WDPART(bp->b_dev) != RAW_PART) {
398 1.93 mycroft if ((wd->sc_flags & WDF_BSDLABEL) == 0) {
399 1.93 mycroft bp->b_error = EIO;
400 1.93 mycroft goto bad;
401 1.93 mycroft }
402 1.1 cgd /*
403 1.64 mycroft * Do bounds checking, adjust transfer. if error, process.
404 1.64 mycroft * If end of partition, just return.
405 1.1 cgd */
406 1.93 mycroft if (bounds_check_with_label(bp, &wd->sc_dk.dk_label,
407 1.93 mycroft (wd->sc_flags & WDF_WLABEL) != 0) <= 0)
408 1.1 cgd goto done;
409 1.64 mycroft /* Otherwise, process transfer request. */
410 1.1 cgd }
411 1.3 deraadt
412 1.64 mycroft /* Don't bother doing rotational optimization. */
413 1.38 mycroft bp->b_cylin = 0;
414 1.38 mycroft
415 1.64 mycroft /* Queue transfer on drive, activate drive and controller if idle. */
416 1.1 cgd s = splbio();
417 1.113 mycroft disksort(&wd->sc_q, bp);
418 1.78 mycroft if (!wd->sc_q.b_active)
419 1.64 mycroft wdstart(wd); /* Start drive. */
420 1.78 mycroft #ifdef DIAGNOSTIC
421 1.80 mycroft else {
422 1.80 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
423 1.80 mycroft if ((wdc->sc_flags & WDCF_ACTIVE) == 0) {
424 1.80 mycroft printf("wdstrategy: controller inactive\n");
425 1.80 mycroft wdcstart(wdc);
426 1.80 mycroft }
427 1.78 mycroft }
428 1.78 mycroft #endif
429 1.1 cgd splx(s);
430 1.64 mycroft return;
431 1.3 deraadt
432 1.93 mycroft bad:
433 1.93 mycroft bp->b_flags |= B_ERROR;
434 1.1 cgd done:
435 1.64 mycroft /* Toss transfer; we're done early. */
436 1.1 cgd biodone(bp);
437 1.1 cgd }
438 1.1 cgd
439 1.1 cgd /*
440 1.64 mycroft * Routine to queue a command to the controller. The unit's request is linked
441 1.64 mycroft * into the active list for the controller. If the controller is idle, the
442 1.64 mycroft * transfer is started.
443 1.1 cgd */
444 1.108 mycroft void
445 1.64 mycroft wdstart(wd)
446 1.64 mycroft struct wd_softc *wd;
447 1.1 cgd {
448 1.78 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
449 1.78 mycroft int active = wdc->sc_drives.tqh_first != 0;
450 1.64 mycroft
451 1.64 mycroft /* Link onto controller queue. */
452 1.78 mycroft wd->sc_q.b_active = 1;
453 1.78 mycroft TAILQ_INSERT_TAIL(&wdc->sc_drives, wd, sc_drivechain);
454 1.3 deraadt
455 1.78 mycroft /* If controller not already active, start it. */
456 1.78 mycroft if (!active)
457 1.78 mycroft wdcstart(wdc);
458 1.1 cgd }
459 1.1 cgd
460 1.64 mycroft void
461 1.64 mycroft wdfinish(wd, bp)
462 1.64 mycroft struct wd_softc *wd;
463 1.64 mycroft struct buf *bp;
464 1.64 mycroft {
465 1.64 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
466 1.64 mycroft
467 1.64 mycroft #ifdef INSTRUMENT
468 1.95 mycroft dk_busy &= ~(1 << wd->sc_dev.dv_unit);
469 1.64 mycroft #endif
470 1.69 mycroft wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR);
471 1.78 mycroft wdc->sc_errors = 0;
472 1.64 mycroft /*
473 1.113 mycroft * Move this drive to the end of the queue to give others a `fair'
474 1.113 mycroft * chance.
475 1.64 mycroft */
476 1.113 mycroft if (wd->sc_drivechain.tqe_next) {
477 1.113 mycroft TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
478 1.113 mycroft if (bp->b_actf) {
479 1.113 mycroft TAILQ_INSERT_TAIL(&wdc->sc_drives, wd,
480 1.113 mycroft sc_drivechain);
481 1.113 mycroft } else
482 1.113 mycroft wd->sc_q.b_active = 0;
483 1.64 mycroft }
484 1.64 mycroft bp->b_resid = wd->sc_bcount;
485 1.64 mycroft wd->sc_skip = 0;
486 1.64 mycroft wd->sc_q.b_actf = bp->b_actf;
487 1.64 mycroft biodone(bp);
488 1.64 mycroft }
489 1.64 mycroft
490 1.1 cgd /*
491 1.64 mycroft * Controller startup routine. This does the calculation, and starts a
492 1.64 mycroft * single-sector read or write operation. Called to start a transfer, or from
493 1.64 mycroft * the interrupt routine to continue a multi-sector transfer.
494 1.1 cgd * RESTRICTIONS:
495 1.1 cgd * 1. The transfer length must be an exact multiple of the sector size.
496 1.1 cgd */
497 1.1 cgd static void
498 1.64 mycroft wdcstart(wdc)
499 1.64 mycroft struct wdc_softc *wdc;
500 1.1 cgd {
501 1.64 mycroft struct wd_softc *wd; /* disk unit for IO */
502 1.64 mycroft struct buf *bp;
503 1.113 mycroft int nblks;
504 1.64 mycroft
505 1.1 cgd loop:
506 1.64 mycroft /* Is there a drive for the controller to do a transfer with? */
507 1.78 mycroft wd = wdc->sc_drives.tqh_first;
508 1.78 mycroft if (wd == NULL)
509 1.1 cgd return;
510 1.3 deraadt
511 1.64 mycroft /* Is there a transfer to this drive? If not, deactivate drive. */
512 1.78 mycroft bp = wd->sc_q.b_actf;
513 1.1 cgd if (bp == NULL) {
514 1.78 mycroft TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
515 1.78 mycroft wd->sc_q.b_active = 0;
516 1.1 cgd goto loop;
517 1.1 cgd }
518 1.3 deraadt
519 1.78 mycroft if (wdc->sc_errors >= WDIORETRIES) {
520 1.64 mycroft wderror(wd, bp, "hard error");
521 1.64 mycroft bp->b_error = EIO;
522 1.64 mycroft bp->b_flags |= B_ERROR;
523 1.64 mycroft wdfinish(wd, bp);
524 1.64 mycroft goto loop;
525 1.64 mycroft }
526 1.78 mycroft
527 1.78 mycroft /* Mark the controller active and set a timeout. */
528 1.99 mycroft if (wdc->sc_flags & WDCF_ACTIVE)
529 1.99 mycroft untimeout(wdctimeout, wdc);
530 1.99 mycroft else
531 1.99 mycroft wdc->sc_flags |= WDCF_ACTIVE;
532 1.98 mycroft timeout(wdctimeout, wdc, WAITTIME);
533 1.69 mycroft
534 1.69 mycroft /* Do control operations specially. */
535 1.64 mycroft if (wd->sc_state < OPEN) {
536 1.69 mycroft /*
537 1.69 mycroft * Actually, we want to be careful not to mess with the control
538 1.69 mycroft * state if the device is currently busy, but we can assume
539 1.69 mycroft * that we never get to this point if that's the case.
540 1.69 mycroft */
541 1.69 mycroft (void) wdcontrol(wd);
542 1.1 cgd return;
543 1.1 cgd }
544 1.64 mycroft
545 1.64 mycroft /*
546 1.76 mycroft * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
547 1.69 mycroft * encountered. If we are in multi-sector mode, then we switch to
548 1.69 mycroft * single-sector mode and retry the operation from the start.
549 1.64 mycroft */
550 1.69 mycroft if (wdc->sc_flags & WDCF_ERROR) {
551 1.69 mycroft wdc->sc_flags &= ~WDCF_ERROR;
552 1.69 mycroft if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
553 1.69 mycroft wdc->sc_flags |= WDCF_SINGLE;
554 1.113 mycroft wd->sc_skip = 0;
555 1.69 mycroft }
556 1.64 mycroft }
557 1.64 mycroft
558 1.95 mycroft if (wd->sc_skip == 0) {
559 1.44 mycroft #ifdef WDDEBUG
560 1.95 mycroft printf("\n%s: wdcstart %s %d@%d; map ", wd->sc_dev.dv_xname,
561 1.37 mycroft (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
562 1.95 mycroft bp->b_blkno);
563 1.95 mycroft #endif
564 1.95 mycroft wd->sc_bcount = bp->b_bcount;
565 1.95 mycroft #ifdef INSTRUMENT
566 1.95 mycroft dk_busy |= (1 << wd->sc_dev.dv_unit);
567 1.95 mycroft dk_wds[wd->sc_dev.dv_unit] += bp->b_bcount >> 6;
568 1.95 mycroft #endif
569 1.95 mycroft } else {
570 1.95 mycroft #ifdef WDDEBUG
571 1.64 mycroft printf(" %d)%x", wd->sc_skip, inb(wd->sc_iobase+wd_altsts));
572 1.1 cgd #endif
573 1.95 mycroft }
574 1.95 mycroft
575 1.95 mycroft /* If starting a multisector transfer, or doing single transfers. */
576 1.113 mycroft if (wd->sc_skip == 0 || (wdc->sc_flags & WDCF_SINGLE) != 0) {
577 1.95 mycroft struct disklabel *lp;
578 1.113 mycroft long blkno;
579 1.95 mycroft long cylin, head, sector;
580 1.95 mycroft int command;
581 1.95 mycroft
582 1.95 mycroft lp = &wd->sc_dk.dk_label;
583 1.95 mycroft blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE) + wd->sc_skip;
584 1.95 mycroft if (WDPART(bp->b_dev) != RAW_PART)
585 1.95 mycroft blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
586 1.95 mycroft if (wdc->sc_flags & WDCF_SINGLE)
587 1.95 mycroft nblks = 1;
588 1.95 mycroft else
589 1.113 mycroft nblks = howmany(wd->sc_bcount, lp->d_secsize);
590 1.113 mycroft if (wd->sc_mode == WDM_DMA && nblks > 8)
591 1.113 mycroft nblks = 8;
592 1.95 mycroft
593 1.95 mycroft /*
594 1.95 mycroft * Check for bad sectors if we have them, and not formatting. Only do
595 1.95 mycroft * this in single-sector mode, or when starting a multiple-sector
596 1.95 mycroft * transfer.
597 1.95 mycroft */
598 1.108 mycroft if ((wd->sc_dk.dk_label.d_flags & D_BADSECT) != 0
599 1.44 mycroft #ifdef B_FORMAT
600 1.95 mycroft && (bp->b_flags & B_FORMAT) == 0
601 1.3 deraadt #endif
602 1.95 mycroft ) {
603 1.95 mycroft long blkdiff;
604 1.95 mycroft int i;
605 1.95 mycroft
606 1.95 mycroft for (i = 0; (blkdiff = wd->sc_badsect[i]) != -1; i++) {
607 1.95 mycroft blkdiff -= blkno;
608 1.95 mycroft if (blkdiff < 0)
609 1.95 mycroft continue;
610 1.95 mycroft if (blkdiff == 0) {
611 1.95 mycroft /* Replace current block of transfer. */
612 1.95 mycroft blkno =
613 1.95 mycroft lp->d_secperunit - lp->d_nsectors - i - 1;
614 1.113 mycroft }
615 1.113 mycroft if (blkdiff < nblks) {
616 1.95 mycroft /* Bad block inside transfer. */
617 1.113 mycroft wdc->sc_flags |= WDCF_SINGLE;
618 1.113 mycroft nblks = 1;
619 1.95 mycroft }
620 1.113 mycroft break;
621 1.3 deraadt }
622 1.95 mycroft /* Tranfer is okay now. */
623 1.3 deraadt }
624 1.1 cgd
625 1.95 mycroft cylin = blkno / lp->d_secpercyl;
626 1.95 mycroft head = (blkno % lp->d_secpercyl) / lp->d_nsectors;
627 1.95 mycroft sector = blkno % lp->d_nsectors;
628 1.95 mycroft sector++; /* Sectors begin with 1, not 0. */
629 1.51 mycroft
630 1.64 mycroft #ifdef INSTRUMENT
631 1.74 mycroft ++dk_seek[wd->sc_dev.dv_unit];
632 1.74 mycroft ++dk_xfer[wd->sc_dev.dv_unit];
633 1.64 mycroft #endif
634 1.1 cgd
635 1.37 mycroft #ifdef B_FORMAT
636 1.1 cgd if (bp->b_flags & B_FORMAT) {
637 1.60 mycroft sector = lp->d_gap3;
638 1.95 mycroft nblks = lp->d_nsectors;
639 1.60 mycroft command = WDCC_FORMAT;
640 1.95 mycroft } else
641 1.95 mycroft #endif
642 1.113 mycroft switch (wd->sc_mode) {
643 1.113 mycroft case WDM_DMA:
644 1.113 mycroft command = (bp->b_flags & B_READ) ?
645 1.113 mycroft WDCC_READDMA : WDCC_WRITEDMA;
646 1.113 mycroft isa_dmastart(bp->b_flags & B_READ, bp->b_data,
647 1.113 mycroft nblks * DEV_BSIZE, wdc->sc_drq);
648 1.113 mycroft break;
649 1.113 mycroft case WDM_PIOMULTI:
650 1.113 mycroft command = (bp->b_flags & B_READ) ?
651 1.113 mycroft WDCC_READMULTI : WDCC_WRITEMULTI;
652 1.113 mycroft break;
653 1.113 mycroft case WDM_PIOSINGLE:
654 1.113 mycroft command = (bp->b_flags & B_READ) ?
655 1.113 mycroft WDCC_READ : WDCC_WRITE;
656 1.113 mycroft break;
657 1.113 mycroft }
658 1.3 deraadt
659 1.64 mycroft /* Initiate command! */
660 1.105 mycroft if (wdcommand(wd, command, cylin, head, sector, nblks) != 0) {
661 1.64 mycroft wderror(wd, NULL,
662 1.64 mycroft "wdcstart: timeout waiting for unbusy");
663 1.64 mycroft wdcunwedge(wdc);
664 1.64 mycroft return;
665 1.51 mycroft }
666 1.44 mycroft #ifdef WDDEBUG
667 1.37 mycroft printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
668 1.84 mycroft cylin, head, bp->b_data, inb(wd->sc_iobase+wd_altsts));
669 1.1 cgd #endif
670 1.1 cgd }
671 1.78 mycroft
672 1.113 mycroft if (wd->sc_mode == WDM_PIOSINGLE ||
673 1.113 mycroft (wdc->sc_flags & WDCF_SINGLE) != 0)
674 1.113 mycroft nblks = 1;
675 1.113 mycroft else if (wd->sc_mode == WDM_PIOMULTI)
676 1.113 mycroft nblks = min(wd->sc_bcount / DEV_BSIZE, wd->sc_multiple);
677 1.113 mycroft else
678 1.113 mycroft nblks = min(wd->sc_bcount / DEV_BSIZE, 8);
679 1.113 mycroft wdc->sc_nblks = nblks;
680 1.3 deraadt
681 1.113 mycroft /* If this was a write and not using DMA, push the data. */
682 1.113 mycroft if (wd->sc_mode != WDM_DMA &&
683 1.113 mycroft (bp->b_flags & B_READ) == 0) {
684 1.113 mycroft if (wait_for_drq(wdc) < 0) {
685 1.113 mycroft wderror(wd, NULL, "wdcstart: timeout waiting for drq");
686 1.113 mycroft wdcunwedge(wdc);
687 1.113 mycroft return;
688 1.113 mycroft }
689 1.113 mycroft
690 1.113 mycroft /* Then send it! */
691 1.113 mycroft outsw(wdc->sc_iobase+wd_data,
692 1.113 mycroft bp->b_data + wd->sc_skip * DEV_BSIZE,
693 1.113 mycroft nblks * DEV_BSIZE / sizeof(short));
694 1.19 deraadt }
695 1.1 cgd }
696 1.1 cgd
697 1.64 mycroft /*
698 1.64 mycroft * Interrupt routine for the controller. Acknowledge the interrupt, check for
699 1.64 mycroft * errors on the current operation, mark it done if necessary, and start the
700 1.64 mycroft * next request. Also check for a partially done transfer, and continue with
701 1.64 mycroft * the next chunk if so.
702 1.1 cgd */
703 1.64 mycroft int
704 1.103 mycroft wdcintr(wdc)
705 1.103 mycroft struct wdc_softc *wdc;
706 1.1 cgd {
707 1.64 mycroft struct wd_softc *wd;
708 1.64 mycroft struct buf *bp;
709 1.113 mycroft int nblks;
710 1.59 mycroft
711 1.87 mycroft if ((wdc->sc_flags & WDCF_ACTIVE) == 0) {
712 1.87 mycroft /* Clear the pending interrupt. */
713 1.87 mycroft (void) inb(wdc->sc_iobase+wd_status);
714 1.64 mycroft return 0;
715 1.87 mycroft }
716 1.78 mycroft
717 1.78 mycroft wd = wdc->sc_drives.tqh_first;
718 1.78 mycroft bp = wd->sc_q.b_actf;
719 1.78 mycroft
720 1.44 mycroft #ifdef WDDEBUG
721 1.3 deraadt printf("I%d ", ctrlr);
722 1.1 cgd #endif
723 1.13 deraadt
724 1.64 mycroft if (wait_for_unbusy(wdc) < 0) {
725 1.76 mycroft wderror(wd, NULL, "wdcintr: timeout waiting for unbusy");
726 1.64 mycroft wdc->sc_status |= WDCS_ERR; /* XXX */
727 1.27 cgd }
728 1.3 deraadt
729 1.64 mycroft /* Is it not a transfer, but a control operation? */
730 1.64 mycroft if (wd->sc_state < OPEN) {
731 1.69 mycroft if (wdcontrol(wd))
732 1.64 mycroft wdcstart(wdc);
733 1.64 mycroft return 1;
734 1.1 cgd }
735 1.78 mycroft
736 1.78 mycroft wdc->sc_flags &= ~WDCF_ACTIVE;
737 1.98 mycroft untimeout(wdctimeout, wdc);
738 1.113 mycroft nblks = wdc->sc_nblks;
739 1.3 deraadt
740 1.113 mycroft if (wd->sc_mode == WDM_DMA)
741 1.113 mycroft isa_dmadone(bp->b_flags & B_READ, bp->b_data,
742 1.113 mycroft nblks * DEV_BSIZE, wdc->sc_drq);
743 1.113 mycroft
744 1.64 mycroft /* Have we an error? */
745 1.96 mycroft if (wdc->sc_status & WDCS_ERR) {
746 1.51 mycroft lose:
747 1.44 mycroft #ifdef WDDEBUG
748 1.76 mycroft wderror(wd, NULL, "wdcintr");
749 1.1 cgd #endif
750 1.69 mycroft if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
751 1.69 mycroft wdc->sc_flags |= WDCF_ERROR;
752 1.64 mycroft goto restart;
753 1.1 cgd }
754 1.96 mycroft
755 1.1 cgd #ifdef B_FORMAT
756 1.96 mycroft if (bp->b_flags & B_FORMAT)
757 1.96 mycroft goto bad;
758 1.1 cgd #endif
759 1.3 deraadt
760 1.96 mycroft if (++wdc->sc_errors < WDIORETRIES)
761 1.96 mycroft goto restart;
762 1.96 mycroft wderror(wd, bp, "hard error");
763 1.96 mycroft
764 1.96 mycroft bad:
765 1.96 mycroft bp->b_error = EIO;
766 1.96 mycroft bp->b_flags |= B_ERROR;
767 1.96 mycroft goto done;
768 1.1 cgd }
769 1.96 mycroft
770 1.110 mycroft if (wdc->sc_status & WDCS_CORR)
771 1.96 mycroft wderror(wd, bp, "soft ecc");
772 1.3 deraadt
773 1.113 mycroft /* If this was a read and not using DMA, fetch the data. */
774 1.113 mycroft if (wd->sc_mode != WDM_DMA &&
775 1.113 mycroft (bp->b_flags & B_READ) != 0) {
776 1.110 mycroft if ((wdc->sc_status & (WDCS_DRDY | WDCS_DSC | WDCS_DRQ))
777 1.110 mycroft != (WDCS_DRDY | WDCS_DSC | WDCS_DRQ)) {
778 1.87 mycroft wderror(wd, NULL, "wdcintr: read intr before drq");
779 1.64 mycroft wdcunwedge(wdc);
780 1.64 mycroft return 1;
781 1.27 cgd }
782 1.27 cgd
783 1.64 mycroft /* Suck in data. */
784 1.64 mycroft insw(wdc->sc_iobase+wd_data,
785 1.84 mycroft bp->b_data + wd->sc_skip * DEV_BSIZE,
786 1.113 mycroft nblks * DEV_BSIZE / sizeof(short));
787 1.64 mycroft }
788 1.64 mycroft
789 1.64 mycroft /* If we encountered any abnormalities, flag it as a soft error. */
790 1.78 mycroft if (wdc->sc_errors) {
791 1.64 mycroft wderror(wd, bp, "soft error");
792 1.78 mycroft wdc->sc_errors = 0;
793 1.1 cgd }
794 1.3 deraadt
795 1.64 mycroft /* Ready for the next block, if any. */
796 1.113 mycroft wd->sc_skip += nblks;
797 1.113 mycroft wd->sc_bcount -= nblks * DEV_BSIZE;
798 1.64 mycroft
799 1.64 mycroft /* See if more to transfer. */
800 1.64 mycroft if (wd->sc_bcount > 0)
801 1.64 mycroft goto restart;
802 1.64 mycroft
803 1.64 mycroft done:
804 1.64 mycroft /* Done with this transfer, with or without error. */
805 1.64 mycroft wdfinish(wd, bp);
806 1.64 mycroft
807 1.64 mycroft restart:
808 1.64 mycroft /* Start the next transfer, if any. */
809 1.64 mycroft wdcstart(wdc);
810 1.1 cgd
811 1.64 mycroft return 1;
812 1.1 cgd }
813 1.1 cgd
814 1.1 cgd /*
815 1.1 cgd * Initialize a drive.
816 1.1 cgd */
817 1.1 cgd int
818 1.108 mycroft wdopen(dev, flag, fmt)
819 1.55 mycroft dev_t dev;
820 1.93 mycroft int flag, fmt;
821 1.1 cgd {
822 1.109 mycroft int error;
823 1.108 mycroft int unit, part;
824 1.64 mycroft struct wd_softc *wd;
825 1.3 deraadt
826 1.108 mycroft unit = WDUNIT(dev);
827 1.108 mycroft if (unit >= wdcd.cd_ndevs)
828 1.37 mycroft return ENXIO;
829 1.108 mycroft wd = wdcd.cd_devs[unit];
830 1.64 mycroft if (wd == 0)
831 1.37 mycroft return ENXIO;
832 1.3 deraadt
833 1.108 mycroft part = WDPART(dev);
834 1.1 cgd
835 1.108 mycroft while ((wd->sc_flags & WDF_LOCKED) != 0) {
836 1.108 mycroft wd->sc_flags |= WDF_WANTED;
837 1.109 mycroft if ((error = tsleep(wd, PRIBIO | PCATCH, "wdopn", 0)) != 0)
838 1.108 mycroft return error;
839 1.3 deraadt }
840 1.3 deraadt
841 1.109 mycroft if (wd->sc_dk.dk_openmask != 0) {
842 1.109 mycroft /*
843 1.109 mycroft * If any partition is open, but the disk has been invalidated,
844 1.109 mycroft * disallow further opens.
845 1.109 mycroft */
846 1.109 mycroft if ((wd->sc_flags & WDF_LOADED) == 0)
847 1.109 mycroft return ENXIO;
848 1.109 mycroft } else {
849 1.108 mycroft wd->sc_flags |= WDF_LOCKED;
850 1.108 mycroft
851 1.108 mycroft if ((wd->sc_flags & WDF_LOADED) == 0) {
852 1.108 mycroft wd->sc_flags &= ~WDF_BSDLABEL;
853 1.108 mycroft wd->sc_flags |= WDF_LOADED;
854 1.108 mycroft
855 1.108 mycroft /* Load the physical device parameters. */
856 1.108 mycroft if (wd_get_parms(wd) != 0) {
857 1.108 mycroft error = ENXIO;
858 1.108 mycroft goto bad2;
859 1.108 mycroft }
860 1.108 mycroft
861 1.108 mycroft /* Load the partition info if not already loaded. */
862 1.108 mycroft wdgetdisklabel(wd);
863 1.108 mycroft }
864 1.108 mycroft
865 1.108 mycroft wd->sc_flags &= ~WDF_LOCKED;
866 1.108 mycroft if ((wd->sc_flags & WDF_WANTED) != 0) {
867 1.108 mycroft wd->sc_flags &= ~WDF_WANTED;
868 1.108 mycroft wakeup(wd);
869 1.3 deraadt }
870 1.1 cgd }
871 1.108 mycroft
872 1.109 mycroft /* Check that the partition exists. */
873 1.93 mycroft if (part != RAW_PART &&
874 1.93 mycroft (part >= wd->sc_dk.dk_label.d_npartitions ||
875 1.93 mycroft wd->sc_dk.dk_label.d_partitions[part].p_fstype == FS_UNUSED)) {
876 1.93 mycroft error = ENXIO;
877 1.93 mycroft goto bad;
878 1.93 mycroft }
879 1.3 deraadt
880 1.64 mycroft /* Insure only one open at a time. */
881 1.3 deraadt switch (fmt) {
882 1.3 deraadt case S_IFCHR:
883 1.94 mycroft wd->sc_dk.dk_copenmask |= (1 << part);
884 1.3 deraadt break;
885 1.3 deraadt case S_IFBLK:
886 1.94 mycroft wd->sc_dk.dk_bopenmask |= (1 << part);
887 1.3 deraadt break;
888 1.3 deraadt }
889 1.94 mycroft wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
890 1.60 mycroft
891 1.37 mycroft return 0;
892 1.93 mycroft
893 1.108 mycroft bad2:
894 1.108 mycroft wd->sc_flags &= ~WDF_LOADED;
895 1.108 mycroft
896 1.93 mycroft bad:
897 1.108 mycroft wd->sc_flags &= ~WDF_LOCKED;
898 1.108 mycroft if ((wd->sc_flags & WDF_WANTED) != 0) {
899 1.108 mycroft wd->sc_flags &= ~WDF_WANTED;
900 1.108 mycroft wakeup(wd);
901 1.108 mycroft }
902 1.108 mycroft
903 1.93 mycroft return error;
904 1.1 cgd }
905 1.1 cgd
906 1.108 mycroft void
907 1.108 mycroft wdgetdisklabel(wd)
908 1.108 mycroft struct wd_softc *wd;
909 1.108 mycroft {
910 1.108 mycroft char *errstring;
911 1.108 mycroft
912 1.108 mycroft if ((wd->sc_flags & WDF_BSDLABEL) != 0)
913 1.108 mycroft return;
914 1.108 mycroft
915 1.108 mycroft bzero(&wd->sc_dk.dk_label, sizeof(struct disklabel));
916 1.108 mycroft bzero(&wd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
917 1.108 mycroft
918 1.108 mycroft wd->sc_dk.dk_label.d_secsize = DEV_BSIZE;
919 1.108 mycroft wd->sc_dk.dk_label.d_ntracks = wd->sc_params.wdp_heads;
920 1.108 mycroft wd->sc_dk.dk_label.d_nsectors = wd->sc_params.wdp_sectors;
921 1.110 mycroft wd->sc_dk.dk_label.d_ncylinders = wd->sc_params.wdp_cylinders;
922 1.108 mycroft wd->sc_dk.dk_label.d_secpercyl =
923 1.108 mycroft wd->sc_dk.dk_label.d_ntracks * wd->sc_dk.dk_label.d_nsectors;
924 1.108 mycroft
925 1.108 mycroft #if 0
926 1.108 mycroft strncpy(wd->sc_dk.dk_label.d_typename, "ST506 disk", 16);
927 1.108 mycroft wd->sc_dk.dk_label.d_type = DTYPE_ST506;
928 1.108 mycroft #endif
929 1.108 mycroft strncpy(wd->sc_dk.dk_label.d_packname, wd->sc_params.wdp_model, 16);
930 1.108 mycroft wd->sc_dk.dk_label.d_secperunit =
931 1.108 mycroft wd->sc_dk.dk_label.d_secpercyl * wd->sc_dk.dk_label.d_ncylinders;
932 1.108 mycroft wd->sc_dk.dk_label.d_rpm = 3600;
933 1.108 mycroft wd->sc_dk.dk_label.d_interleave = 1;
934 1.108 mycroft wd->sc_dk.dk_label.d_flags = 0;
935 1.108 mycroft
936 1.108 mycroft wd->sc_dk.dk_label.d_partitions[RAW_PART].p_offset = 0;
937 1.108 mycroft wd->sc_dk.dk_label.d_partitions[RAW_PART].p_size =
938 1.108 mycroft wd->sc_dk.dk_label.d_secperunit *
939 1.108 mycroft (wd->sc_dk.dk_label.d_secsize / DEV_BSIZE);
940 1.108 mycroft wd->sc_dk.dk_label.d_partitions[RAW_PART].p_fstype = FS_UNUSED;
941 1.108 mycroft wd->sc_dk.dk_label.d_npartitions = RAW_PART + 1;
942 1.108 mycroft
943 1.108 mycroft wd->sc_dk.dk_label.d_magic = DISKMAGIC;
944 1.108 mycroft wd->sc_dk.dk_label.d_magic2 = DISKMAGIC;
945 1.108 mycroft wd->sc_dk.dk_label.d_checksum = dkcksum(&wd->sc_dk.dk_label);
946 1.108 mycroft
947 1.113 mycroft wd->sc_badsect[0] = -1;
948 1.113 mycroft
949 1.108 mycroft if (wd->sc_state > RECAL)
950 1.108 mycroft wd->sc_state = RECAL;
951 1.108 mycroft errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
952 1.108 mycroft wdstrategy, &wd->sc_dk.dk_label, &wd->sc_dk.dk_cpulabel);
953 1.108 mycroft if (errstring) {
954 1.108 mycroft /*
955 1.108 mycroft * This probably happened because the drive's default
956 1.108 mycroft * geometry doesn't match the DOS geometry. We
957 1.108 mycroft * assume the DOS geometry is now in the label and try
958 1.108 mycroft * again. XXX This is a kluge.
959 1.108 mycroft */
960 1.108 mycroft if (wd->sc_state > GEOMETRY)
961 1.108 mycroft wd->sc_state = GEOMETRY;
962 1.108 mycroft errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
963 1.108 mycroft wdstrategy, &wd->sc_dk.dk_label, &wd->sc_dk.dk_cpulabel);
964 1.108 mycroft }
965 1.108 mycroft if (errstring) {
966 1.108 mycroft printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
967 1.108 mycroft return;
968 1.108 mycroft }
969 1.108 mycroft
970 1.108 mycroft if (wd->sc_state > GEOMETRY)
971 1.108 mycroft wd->sc_state = GEOMETRY;
972 1.108 mycroft if ((wd->sc_dk.dk_label.d_flags & D_BADSECT) != 0)
973 1.108 mycroft bad144intern(wd);
974 1.108 mycroft
975 1.108 mycroft wd->sc_flags |= WDF_BSDLABEL;
976 1.108 mycroft }
977 1.108 mycroft
978 1.1 cgd /*
979 1.1 cgd * Implement operations other than read/write.
980 1.76 mycroft * Called from wdcstart or wdcintr during opens and formats.
981 1.1 cgd * Uses finite-state-machine to track progress of operation in progress.
982 1.1 cgd * Returns 0 if operation still in progress, 1 if completed.
983 1.1 cgd */
984 1.1 cgd static int
985 1.69 mycroft wdcontrol(wd)
986 1.69 mycroft struct wd_softc *wd;
987 1.1 cgd {
988 1.64 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
989 1.3 deraadt
990 1.64 mycroft switch (wd->sc_state) {
991 1.110 mycroft case RECAL: /* Set SDH, step rate, do recal. */
992 1.110 mycroft if (wdcommandshort(wdc, wd->sc_drive, WDCC_RECAL) != 0) {
993 1.113 mycroft wderror(wd, NULL, "wdcontrol: recal failed (1)");
994 1.113 mycroft goto bad;
995 1.21 deraadt }
996 1.69 mycroft wd->sc_state = RECAL_WAIT;
997 1.37 mycroft return 0;
998 1.55 mycroft
999 1.69 mycroft case RECAL_WAIT:
1000 1.69 mycroft if (wdc->sc_status & WDCS_ERR) {
1001 1.113 mycroft wderror(wd, NULL, "wdcontrol: recal failed (2)");
1002 1.113 mycroft goto bad;
1003 1.1 cgd }
1004 1.69 mycroft /* fall through */
1005 1.69 mycroft case GEOMETRY:
1006 1.69 mycroft if (wdsetctlr(wd) != 0) {
1007 1.69 mycroft /* Already printed a message. */
1008 1.113 mycroft goto bad;
1009 1.69 mycroft }
1010 1.69 mycroft wd->sc_state = GEOMETRY_WAIT;
1011 1.69 mycroft return 0;
1012 1.69 mycroft
1013 1.69 mycroft case GEOMETRY_WAIT:
1014 1.69 mycroft if (wdc->sc_status & WDCS_ERR) {
1015 1.69 mycroft wderror(wd, NULL, "wdcontrol: geometry failed");
1016 1.113 mycroft goto bad;
1017 1.113 mycroft }
1018 1.113 mycroft /* fall through */
1019 1.113 mycroft case MULTIMODE:
1020 1.113 mycroft if (wd->sc_mode != WDM_PIOMULTI)
1021 1.113 mycroft goto open;
1022 1.113 mycroft outb(wdc->sc_iobase+wd_seccnt, wd->sc_multiple);
1023 1.113 mycroft if (wdcommandshort(wdc, wd->sc_drive, WDCC_SETMULTI) != 0) {
1024 1.113 mycroft wderror(wd, NULL, "wdcontrol: setmulti failed (1)");
1025 1.113 mycroft goto bad;
1026 1.69 mycroft }
1027 1.113 mycroft wd->sc_state = MULTIMODE_WAIT;
1028 1.113 mycroft return 0;
1029 1.69 mycroft
1030 1.113 mycroft case MULTIMODE_WAIT:
1031 1.113 mycroft if (wdc->sc_status & WDCS_ERR) {
1032 1.113 mycroft wderror(wd, NULL, "wdcontrol: setmulti failed (2)");
1033 1.113 mycroft goto bad;
1034 1.113 mycroft }
1035 1.113 mycroft /* fall through */
1036 1.113 mycroft open:
1037 1.78 mycroft wdc->sc_errors = 0;
1038 1.64 mycroft wd->sc_state = OPEN;
1039 1.1 cgd /*
1040 1.63 mycroft * The rest of the initialization can be done by normal means.
1041 1.1 cgd */
1042 1.37 mycroft return 1;
1043 1.113 mycroft
1044 1.113 mycroft bad:
1045 1.113 mycroft wdcunwedge(wdc);
1046 1.113 mycroft return 0;
1047 1.1 cgd }
1048 1.1 cgd
1049 1.60 mycroft #ifdef DIAGNOSTIC
1050 1.60 mycroft panic("wdcontrol: impossible");
1051 1.60 mycroft #endif
1052 1.1 cgd }
1053 1.1 cgd
1054 1.1 cgd /*
1055 1.64 mycroft * Send a command and wait uninterruptibly until controller is finished.
1056 1.64 mycroft * Return -1 if controller busy for too long, otherwise return non-zero if
1057 1.64 mycroft * error. Intended for brief controller commands at critical points.
1058 1.64 mycroft * Assumes interrupts are blocked.
1059 1.1 cgd */
1060 1.1 cgd static int
1061 1.105 mycroft wdcommand(wd, command, cylin, head, sector, count)
1062 1.64 mycroft struct wd_softc *wd;
1063 1.105 mycroft int command;
1064 1.60 mycroft int cylin, head, sector, count;
1065 1.3 deraadt {
1066 1.64 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1067 1.107 mycroft int iobase = wdc->sc_iobase;
1068 1.60 mycroft int stat;
1069 1.3 deraadt
1070 1.64 mycroft /* Select drive. */
1071 1.74 mycroft outb(iobase+wd_sdh, WDSD_IBM | (wd->sc_drive << 4) | head);
1072 1.90 mycroft
1073 1.90 mycroft /* Wait for it to become ready to accept a command. */
1074 1.110 mycroft if (command == WDCC_IDP)
1075 1.64 mycroft stat = wait_for_unbusy(wdc);
1076 1.60 mycroft else
1077 1.110 mycroft stat = wdcwait(wdc, WDCS_DRDY);
1078 1.60 mycroft if (stat < 0)
1079 1.60 mycroft return -1;
1080 1.3 deraadt
1081 1.64 mycroft /* Load parameters. */
1082 1.110 mycroft if (wd->sc_dk.dk_label.d_type == DTYPE_ST506)
1083 1.110 mycroft outb(iobase+wd_precomp, wd->sc_dk.dk_label.d_precompcyl / 4);
1084 1.110 mycroft else
1085 1.110 mycroft outb(iobase+wd_features, 0);
1086 1.64 mycroft outb(iobase+wd_cyl_lo, cylin);
1087 1.64 mycroft outb(iobase+wd_cyl_hi, cylin >> 8);
1088 1.64 mycroft outb(iobase+wd_sector, sector);
1089 1.64 mycroft outb(iobase+wd_seccnt, count);
1090 1.60 mycroft
1091 1.105 mycroft /* Send command. */
1092 1.105 mycroft outb(iobase+wd_command, command);
1093 1.105 mycroft
1094 1.105 mycroft return 0;
1095 1.105 mycroft }
1096 1.105 mycroft
1097 1.105 mycroft int
1098 1.105 mycroft wdcommandshort(wdc, drive, command)
1099 1.105 mycroft struct wdc_softc *wdc;
1100 1.105 mycroft int drive;
1101 1.105 mycroft int command;
1102 1.105 mycroft {
1103 1.107 mycroft int iobase = wdc->sc_iobase;
1104 1.105 mycroft
1105 1.105 mycroft /* Select drive. */
1106 1.105 mycroft outb(iobase+wd_sdh, WDSD_IBM | (drive << 4));
1107 1.105 mycroft
1108 1.110 mycroft if (wdcwait(wdc, WDCS_DRDY) < 0)
1109 1.105 mycroft return -1;
1110 1.105 mycroft
1111 1.105 mycroft outb(iobase+wd_command, command);
1112 1.40 mycroft
1113 1.64 mycroft return 0;
1114 1.1 cgd }
1115 1.1 cgd
1116 1.1 cgd /*
1117 1.110 mycroft * Issue IDP to drive to tell it just what geometry it is to be.
1118 1.1 cgd */
1119 1.1 cgd static int
1120 1.64 mycroft wdsetctlr(wd)
1121 1.64 mycroft struct wd_softc *wd;
1122 1.3 deraadt {
1123 1.64 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1124 1.63 mycroft
1125 1.44 mycroft #ifdef WDDEBUG
1126 1.112 mycroft printf("wd(%d,%d) C%dH%dS%d\n", wd->sc_dev.dv_unit, wd->sc_drive,
1127 1.93 mycroft wd->sc_dk.dk_label.d_ncylinders, wd->sc_dk.dk_label.d_ntracks,
1128 1.93 mycroft wd->sc_dk.dk_label.d_nsectors);
1129 1.44 mycroft #endif
1130 1.3 deraadt
1131 1.110 mycroft if (wdcommand(wd, WDCC_IDP, wd->sc_dk.dk_label.d_ncylinders,
1132 1.105 mycroft wd->sc_dk.dk_label.d_ntracks - 1, 0, wd->sc_dk.dk_label.d_nsectors)
1133 1.105 mycroft != 0) {
1134 1.105 mycroft wderror(wd, NULL, "wdsetctlr: geometry upload failed");
1135 1.51 mycroft return -1;
1136 1.57 mycroft }
1137 1.69 mycroft
1138 1.60 mycroft return 0;
1139 1.1 cgd }
1140 1.1 cgd
1141 1.1 cgd /*
1142 1.110 mycroft * Issue IDENTIFY to drive to ask it what it is.
1143 1.1 cgd */
1144 1.108 mycroft int
1145 1.108 mycroft wd_get_parms(wd)
1146 1.64 mycroft struct wd_softc *wd;
1147 1.3 deraadt {
1148 1.64 mycroft struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1149 1.63 mycroft int i;
1150 1.1 cgd char tb[DEV_BSIZE];
1151 1.3 deraadt
1152 1.110 mycroft if (wdcommandshort(wdc, wd->sc_drive, WDCC_IDENTIFY) != 0 ||
1153 1.64 mycroft wait_for_drq(wdc) != 0) {
1154 1.60 mycroft /*
1155 1.105 mycroft * We `know' there's a drive here; just assume it's old.
1156 1.60 mycroft */
1157 1.93 mycroft strncpy(wd->sc_dk.dk_label.d_typename, "ST506",
1158 1.93 mycroft sizeof wd->sc_dk.dk_label.d_typename);
1159 1.108 mycroft wd->sc_dk.dk_label.d_type = DTYPE_ST506;
1160 1.108 mycroft
1161 1.108 mycroft strncpy(wd->sc_params.wdp_model, "unknown",
1162 1.64 mycroft sizeof wd->sc_params.wdp_model);
1163 1.110 mycroft wd->sc_params.wdp_config = WD_CFG_FIXED;
1164 1.110 mycroft wd->sc_params.wdp_cylinders = 1024;
1165 1.108 mycroft wd->sc_params.wdp_heads = 8;
1166 1.108 mycroft wd->sc_params.wdp_sectors = 17;
1167 1.111 mycroft wd->sc_params.wdp_maxmulti = 0;
1168 1.111 mycroft wd->sc_params.wdp_usedmovsd = 0;
1169 1.110 mycroft wd->sc_params.wdp_capabilities = 0;
1170 1.60 mycroft } else {
1171 1.64 mycroft /* Obtain parameters. */
1172 1.64 mycroft insw(wdc->sc_iobase+wd_data, tb, sizeof(tb) / sizeof(short));
1173 1.108 mycroft bcopy(tb, &wd->sc_params, sizeof(struct wdparams));
1174 1.13 deraadt
1175 1.64 mycroft /* Shuffle string byte order. */
1176 1.108 mycroft for (i = 0; i < sizeof(wd->sc_params.wdp_model); i += 2) {
1177 1.13 deraadt u_short *p;
1178 1.108 mycroft p = (u_short *)(wd->sc_params.wdp_model + i);
1179 1.13 deraadt *p = ntohs(*p);
1180 1.13 deraadt }
1181 1.13 deraadt
1182 1.93 mycroft strncpy(wd->sc_dk.dk_label.d_typename, "ESDI/IDE",
1183 1.93 mycroft sizeof wd->sc_dk.dk_label.d_typename);
1184 1.93 mycroft wd->sc_dk.dk_label.d_type = DTYPE_ESDI;
1185 1.8 deraadt }
1186 1.13 deraadt
1187 1.13 deraadt #if 0
1188 1.37 mycroft printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1189 1.110 mycroft wp->wdp_config, wp->wdp_cylinders, wp->wdp_heads, wp->wdp_sectors,
1190 1.110 mycroft wp->wdp_buftype, wp->wdp_bufsize, wp->wdp_model);
1191 1.13 deraadt #endif
1192 1.13 deraadt
1193 1.1 cgd /* XXX sometimes possibly needed */
1194 1.64 mycroft (void) inb(wdc->sc_iobase+wd_status);
1195 1.108 mycroft
1196 1.108 mycroft return 0;
1197 1.1 cgd }
1198 1.1 cgd
1199 1.1 cgd int
1200 1.55 mycroft wdclose(dev, flag, fmt)
1201 1.55 mycroft dev_t dev;
1202 1.93 mycroft int flag, fmt;
1203 1.1 cgd {
1204 1.74 mycroft struct wd_softc *wd = wdcd.cd_devs[WDUNIT(dev)];
1205 1.93 mycroft int part = WDPART(dev);
1206 1.109 mycroft int s;
1207 1.3 deraadt
1208 1.3 deraadt switch (fmt) {
1209 1.3 deraadt case S_IFCHR:
1210 1.94 mycroft wd->sc_dk.dk_copenmask &= ~(1 << part);
1211 1.3 deraadt break;
1212 1.3 deraadt case S_IFBLK:
1213 1.94 mycroft wd->sc_dk.dk_bopenmask &= ~(1 << part);
1214 1.3 deraadt break;
1215 1.3 deraadt }
1216 1.94 mycroft wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1217 1.109 mycroft
1218 1.109 mycroft if (wd->sc_dk.dk_openmask == 0) {
1219 1.109 mycroft wd->sc_flags |= WDF_LOCKED;
1220 1.109 mycroft
1221 1.113 mycroft #if 0
1222 1.109 mycroft s = splbio();
1223 1.109 mycroft while (...) {
1224 1.109 mycroft wd->sc_flags |= WDF_WAITING;
1225 1.109 mycroft if ((error = tsleep(wd, PRIBIO | PCATCH, "wdcls", 0)) != 0)
1226 1.109 mycroft return error;
1227 1.109 mycroft }
1228 1.109 mycroft splx(s);
1229 1.113 mycroft #endif
1230 1.109 mycroft
1231 1.109 mycroft wd->sc_flags &= ~WDF_LOCKED;
1232 1.109 mycroft if ((wd->sc_flags & WDF_WANTED) != 0) {
1233 1.109 mycroft wd->sc_flags &= WDF_WANTED;
1234 1.109 mycroft wakeup(wd);
1235 1.109 mycroft }
1236 1.109 mycroft }
1237 1.60 mycroft
1238 1.37 mycroft return 0;
1239 1.1 cgd }
1240 1.1 cgd
1241 1.1 cgd int
1242 1.105 mycroft wdioctl(dev, command, addr, flag, p)
1243 1.55 mycroft dev_t dev;
1244 1.105 mycroft u_long command;
1245 1.55 mycroft caddr_t addr;
1246 1.55 mycroft int flag;
1247 1.55 mycroft struct proc *p;
1248 1.1 cgd {
1249 1.108 mycroft struct wd_softc *wd = wdcd.cd_devs[WDUNIT(dev)];
1250 1.54 mycroft int error;
1251 1.3 deraadt
1252 1.108 mycroft if ((wd->sc_flags & WDF_LOADED) == 0)
1253 1.108 mycroft return EIO;
1254 1.108 mycroft
1255 1.105 mycroft switch (command) {
1256 1.1 cgd case DIOCSBAD:
1257 1.3 deraadt if ((flag & FWRITE) == 0)
1258 1.54 mycroft return EBADF;
1259 1.93 mycroft wd->sc_dk.dk_cpulabel.bad = *(struct dkbad *)addr;
1260 1.108 mycroft wd->sc_dk.dk_label.d_flags |= D_BADSECT;
1261 1.64 mycroft bad144intern(wd);
1262 1.54 mycroft return 0;
1263 1.1 cgd
1264 1.1 cgd case DIOCGDINFO:
1265 1.93 mycroft *(struct disklabel *)addr = wd->sc_dk.dk_label;
1266 1.54 mycroft return 0;
1267 1.3 deraadt
1268 1.3 deraadt case DIOCGPART:
1269 1.93 mycroft ((struct partinfo *)addr)->disklab = &wd->sc_dk.dk_label;
1270 1.3 deraadt ((struct partinfo *)addr)->part =
1271 1.93 mycroft &wd->sc_dk.dk_label.d_partitions[WDPART(dev)];
1272 1.54 mycroft return 0;
1273 1.3 deraadt
1274 1.3 deraadt case DIOCSDINFO:
1275 1.3 deraadt if ((flag & FWRITE) == 0)
1276 1.54 mycroft return EBADF;
1277 1.93 mycroft error = setdisklabel(&wd->sc_dk.dk_label,
1278 1.54 mycroft (struct disklabel *)addr,
1279 1.94 mycroft /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_dk.dk_openmask : */0,
1280 1.93 mycroft &wd->sc_dk.dk_cpulabel);
1281 1.3 deraadt if (error == 0) {
1282 1.64 mycroft wd->sc_flags |= WDF_BSDLABEL;
1283 1.69 mycroft if (wd->sc_state > GEOMETRY)
1284 1.69 mycroft wd->sc_state = GEOMETRY;
1285 1.1 cgd }
1286 1.54 mycroft return error;
1287 1.3 deraadt
1288 1.44 mycroft case DIOCWLABEL:
1289 1.3 deraadt if ((flag & FWRITE) == 0)
1290 1.54 mycroft return EBADF;
1291 1.93 mycroft if (*(int *)addr)
1292 1.93 mycroft wd->sc_flags |= WDF_WLABEL;
1293 1.93 mycroft else
1294 1.93 mycroft wd->sc_flags &= ~WDF_WLABEL;
1295 1.54 mycroft return 0;
1296 1.3 deraadt
1297 1.44 mycroft case DIOCWDINFO:
1298 1.3 deraadt if ((flag & FWRITE) == 0)
1299 1.54 mycroft return EBADF;
1300 1.93 mycroft error = setdisklabel(&wd->sc_dk.dk_label,
1301 1.37 mycroft (struct disklabel *)addr,
1302 1.94 mycroft /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_dk.dk_openmask : */0,
1303 1.93 mycroft &wd->sc_dk.dk_cpulabel);
1304 1.54 mycroft if (error == 0) {
1305 1.64 mycroft wd->sc_flags |= WDF_BSDLABEL;
1306 1.69 mycroft if (wd->sc_state > GEOMETRY)
1307 1.69 mycroft wd->sc_state = GEOMETRY;
1308 1.3 deraadt
1309 1.64 mycroft /* Simulate opening partition 0 so write succeeds. */
1310 1.94 mycroft wd->sc_dk.dk_openmask |= (1 << 0); /* XXX */
1311 1.86 mycroft error = writedisklabel(WDLABELDEV(dev), wdstrategy,
1312 1.93 mycroft &wd->sc_dk.dk_label, &wd->sc_dk.dk_cpulabel);
1313 1.94 mycroft wd->sc_dk.dk_openmask =
1314 1.94 mycroft wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1315 1.3 deraadt }
1316 1.54 mycroft return error;
1317 1.3 deraadt
1318 1.1 cgd #ifdef notyet
1319 1.1 cgd case DIOCGDINFOP:
1320 1.93 mycroft *(struct disklabel **)addr = &wd->sc_dk.dk_label;
1321 1.54 mycroft return 0;
1322 1.3 deraadt
1323 1.1 cgd case DIOCWFORMAT:
1324 1.1 cgd if ((flag & FWRITE) == 0)
1325 1.54 mycroft return EBADF;
1326 1.54 mycroft {
1327 1.54 mycroft register struct format_op *fop;
1328 1.54 mycroft struct iovec aiov;
1329 1.54 mycroft struct uio auio;
1330 1.3 deraadt
1331 1.54 mycroft fop = (struct format_op *)addr;
1332 1.54 mycroft aiov.iov_base = fop->df_buf;
1333 1.54 mycroft aiov.iov_len = fop->df_count;
1334 1.54 mycroft auio.uio_iov = &aiov;
1335 1.54 mycroft auio.uio_iovcnt = 1;
1336 1.54 mycroft auio.uio_resid = fop->df_count;
1337 1.54 mycroft auio.uio_segflg = 0;
1338 1.54 mycroft auio.uio_offset =
1339 1.93 mycroft fop->df_startblk * wd->sc_dk.dk_label.d_secsize;
1340 1.83 pk auio.uio_procp = p;
1341 1.64 mycroft error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1342 1.64 mycroft &auio);
1343 1.54 mycroft fop->df_count -= auio.uio_resid;
1344 1.64 mycroft fop->df_reg[0] = wdc->sc_status;
1345 1.64 mycroft fop->df_reg[1] = wdc->sc_error;
1346 1.54 mycroft return error;
1347 1.54 mycroft }
1348 1.1 cgd #endif
1349 1.3 deraadt
1350 1.1 cgd default:
1351 1.54 mycroft return ENOTTY;
1352 1.1 cgd }
1353 1.54 mycroft
1354 1.54 mycroft #ifdef DIAGNOSTIC
1355 1.54 mycroft panic("wdioctl: impossible");
1356 1.54 mycroft #endif
1357 1.1 cgd }
1358 1.1 cgd
1359 1.44 mycroft #ifdef B_FORMAT
1360 1.1 cgd int
1361 1.1 cgd wdformat(struct buf *bp)
1362 1.1 cgd {
1363 1.44 mycroft
1364 1.1 cgd bp->b_flags |= B_FORMAT;
1365 1.37 mycroft return wdstrategy(bp);
1366 1.1 cgd }
1367 1.1 cgd #endif
1368 1.1 cgd
1369 1.1 cgd int
1370 1.55 mycroft wdsize(dev)
1371 1.55 mycroft dev_t dev;
1372 1.1 cgd {
1373 1.64 mycroft struct wd_softc *wd;
1374 1.108 mycroft int part;
1375 1.108 mycroft int size;
1376 1.3 deraadt
1377 1.108 mycroft if (wdopen(dev, 0, S_IFBLK) != 0)
1378 1.37 mycroft return -1;
1379 1.108 mycroft wd = wdcd.cd_devs[WDUNIT(dev)];
1380 1.108 mycroft part = WDPART(dev);
1381 1.108 mycroft if ((wd->sc_flags & WDF_BSDLABEL) == 0 ||
1382 1.108 mycroft wd->sc_dk.dk_label.d_partitions[part].p_fstype != FS_SWAP)
1383 1.108 mycroft size = -1;
1384 1.108 mycroft else
1385 1.108 mycroft size = wd->sc_dk.dk_label.d_partitions[part].p_size;
1386 1.108 mycroft if (wdclose(dev, 0, S_IFBLK) != 0)
1387 1.37 mycroft return -1;
1388 1.108 mycroft return size;
1389 1.1 cgd }
1390 1.1 cgd
1391 1.64 mycroft /*
1392 1.64 mycroft * Dump core after a system crash.
1393 1.64 mycroft */
1394 1.1 cgd int
1395 1.55 mycroft wddump(dev)
1396 1.55 mycroft dev_t dev;
1397 1.1 cgd {
1398 1.64 mycroft struct wd_softc *wd; /* disk unit to do the IO */
1399 1.64 mycroft struct wdc_softc *wdc;
1400 1.64 mycroft long num; /* number of sectors to write */
1401 1.108 mycroft int unit, part;
1402 1.93 mycroft long blkoff, blkno;
1403 1.63 mycroft long cylin, head, sector;
1404 1.60 mycroft long secpertrk, secpercyl, nblocks;
1405 1.1 cgd char *addr;
1406 1.46 mycroft static wddoingadump = 0;
1407 1.1 cgd extern caddr_t CADDR1;
1408 1.88 mycroft extern pt_entry_t *CMAP1;
1409 1.30 ws
1410 1.64 mycroft addr = (char *)0; /* starting address */
1411 1.3 deraadt
1412 1.16 deraadt #if DO_NOT_KNOW_HOW
1413 1.64 mycroft /* Toss any characters present prior to dump, ie. non-blocking getc. */
1414 1.16 deraadt while (cngetc())
1415 1.1 cgd ;
1416 1.16 deraadt #endif
1417 1.60 mycroft
1418 1.108 mycroft unit = WDUNIT(dev);
1419 1.64 mycroft /* Check for acceptable drive number. */
1420 1.108 mycroft if (unit >= wdcd.cd_ndevs)
1421 1.37 mycroft return ENXIO;
1422 1.108 mycroft wd = wdcd.cd_devs[unit];
1423 1.64 mycroft /* Was it ever initialized? */
1424 1.108 mycroft if (wd == 0 || wd->sc_state < OPEN)
1425 1.37 mycroft return ENXIO;
1426 1.46 mycroft
1427 1.64 mycroft wdc = (void *)wd->sc_dev.dv_parent;
1428 1.64 mycroft
1429 1.64 mycroft /* Convert to disk sectors. */
1430 1.93 mycroft num = ctob(physmem) / wd->sc_dk.dk_label.d_secsize;
1431 1.3 deraadt
1432 1.93 mycroft secpertrk = wd->sc_dk.dk_label.d_nsectors;
1433 1.93 mycroft secpercyl = wd->sc_dk.dk_label.d_secpercyl;
1434 1.64 mycroft part = WDPART(dev);
1435 1.93 mycroft nblocks = wd->sc_dk.dk_label.d_partitions[part].p_size;
1436 1.93 mycroft blkoff = wd->sc_dk.dk_label.d_partitions[part].p_offset;
1437 1.3 deraadt
1438 1.60 mycroft /*printf("part %x, nblocks %d, dumplo %d, num %d\n", part, nblocks,
1439 1.51 mycroft dumplo, num);*/
1440 1.60 mycroft
1441 1.64 mycroft /* Check transfer bounds against partition size. */
1442 1.46 mycroft if (dumplo < 0 || dumplo + num > nblocks)
1443 1.37 mycroft return EINVAL;
1444 1.60 mycroft
1445 1.60 mycroft if (wddoingadump)
1446 1.60 mycroft return EFAULT;
1447 1.3 deraadt wddoingadump = 1;
1448 1.60 mycroft
1449 1.60 mycroft /* Recalibrate. */
1450 1.110 mycroft if (wdcommandshort(wdc, wd->sc_drive, WDCC_RECAL) != 0 ||
1451 1.69 mycroft wait_for_ready(wdc) != 0 || wdsetctlr(wd) != 0 ||
1452 1.69 mycroft wait_for_ready(wdc) != 0) {
1453 1.64 mycroft wderror(wd, NULL, "wddump: recal failed");
1454 1.51 mycroft return EIO;
1455 1.63 mycroft }
1456 1.3 deraadt
1457 1.93 mycroft blkno = dumplo + blkoff;
1458 1.1 cgd while (num > 0) {
1459 1.64 mycroft /* Compute disk address. */
1460 1.93 mycroft cylin = blkno / secpercyl;
1461 1.93 mycroft head = (blkno % secpercyl) / secpertrk;
1462 1.93 mycroft sector = blkno % secpertrk;
1463 1.3 deraadt
1464 1.108 mycroft if ((wd->sc_dk.dk_label.d_flags & D_BADSECT) != 0) {
1465 1.3 deraadt long newblk;
1466 1.3 deraadt int i;
1467 1.3 deraadt
1468 1.64 mycroft for (i = 0; wd->sc_badsect[i] != -1; i++) {
1469 1.93 mycroft if (blkno < wd->sc_badsect[i]) {
1470 1.64 mycroft /* Sorted list, passed our block by. */
1471 1.44 mycroft break;
1472 1.93 mycroft } else if (blkno == wd->sc_badsect[i]) {
1473 1.93 mycroft newblk =
1474 1.93 mycroft wd->sc_dk.dk_label.d_secperunit -
1475 1.93 mycroft wd->sc_dk.dk_label.d_nsectors -
1476 1.93 mycroft i - 1;
1477 1.3 deraadt cylin = newblk / secpercyl;
1478 1.3 deraadt head = (newblk % secpercyl) / secpertrk;
1479 1.3 deraadt sector = newblk % secpertrk;
1480 1.64 mycroft /* Found and replaced; done. */
1481 1.3 deraadt break;
1482 1.3 deraadt }
1483 1.1 cgd }
1484 1.3 deraadt }
1485 1.1 cgd sector++; /* origin 1 */
1486 1.3 deraadt
1487 1.1 cgd #ifdef notdef
1488 1.64 mycroft /* Let's just talk about this first. */
1489 1.60 mycroft printf("cylin %d, head %d, sector %d, addr 0x%x", cylin, head,
1490 1.60 mycroft sector, addr);
1491 1.1 cgd #endif
1492 1.105 mycroft if (wdcommand(wd, WDCC_WRITE, cylin, head, sector, 1) != 0 ||
1493 1.105 mycroft wait_for_drq(wdc) != 0) {
1494 1.105 mycroft wderror(wd, NULL, "wddump: write failed");
1495 1.51 mycroft return EIO;
1496 1.63 mycroft }
1497 1.3 deraadt
1498 1.64 mycroft #ifdef notdef /* Cannot use this since this address was mapped differently. */
1499 1.46 mycroft pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
1500 1.46 mycroft #else
1501 1.88 mycroft *CMAP1 = PG_V | PG_KW | ctob((long)addr);
1502 1.46 mycroft tlbflush();
1503 1.46 mycroft #endif
1504 1.46 mycroft
1505 1.64 mycroft outsw(wdc->sc_iobase+wd_data, CADDR1 + ((int)addr & PGOFSET),
1506 1.51 mycroft DEV_BSIZE / sizeof(short));
1507 1.3 deraadt
1508 1.13 deraadt /* Check data request (should be done). */
1509 1.64 mycroft if (wait_for_ready(wdc) != 0) {
1510 1.64 mycroft wderror(wd, NULL, "wddump: timeout waiting for ready");
1511 1.63 mycroft return EIO;
1512 1.63 mycroft }
1513 1.64 mycroft if (wdc->sc_status & WDCS_DRQ) {
1514 1.64 mycroft wderror(wd, NULL, "wddump: extra drq");
1515 1.37 mycroft return EIO;
1516 1.63 mycroft }
1517 1.3 deraadt
1518 1.46 mycroft if ((unsigned)addr % 1048576 == 0)
1519 1.46 mycroft printf("%d ", num / (1048576 / DEV_BSIZE));
1520 1.1 cgd
1521 1.64 mycroft /* Update block count. */
1522 1.1 cgd num--;
1523 1.93 mycroft blkno++;
1524 1.46 mycroft (int)addr += DEV_BSIZE;
1525 1.3 deraadt
1526 1.16 deraadt #if DO_NOT_KNOW_HOW
1527 1.64 mycroft /* Operator aborting dump? non-blocking getc() */
1528 1.16 deraadt if (cngetc())
1529 1.37 mycroft return EINTR;
1530 1.16 deraadt #endif
1531 1.1 cgd }
1532 1.37 mycroft return 0;
1533 1.1 cgd }
1534 1.3 deraadt
1535 1.3 deraadt /*
1536 1.3 deraadt * Internalize the bad sector table.
1537 1.3 deraadt */
1538 1.3 deraadt void
1539 1.64 mycroft bad144intern(wd)
1540 1.64 mycroft struct wd_softc *wd;
1541 1.3 deraadt {
1542 1.98 mycroft struct dkbad *bt = &wd->sc_dk.dk_cpulabel.bad;
1543 1.98 mycroft struct disklabel *lp = &wd->sc_dk.dk_label;
1544 1.98 mycroft int i = 0;
1545 1.55 mycroft
1546 1.98 mycroft for (; i < 126; i++) {
1547 1.98 mycroft if (bt->bt_bad[i].bt_cyl == 0xffff)
1548 1.55 mycroft break;
1549 1.64 mycroft wd->sc_badsect[i] =
1550 1.98 mycroft bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1551 1.98 mycroft (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1552 1.98 mycroft (bt->bt_bad[i].bt_trksec & 0xff);
1553 1.3 deraadt }
1554 1.98 mycroft for (; i < 127; i++)
1555 1.98 mycroft wd->sc_badsect[i] = -1;
1556 1.3 deraadt }
1557 1.3 deraadt
1558 1.27 cgd static int
1559 1.64 mycroft wdcreset(wdc)
1560 1.64 mycroft struct wdc_softc *wdc;
1561 1.21 deraadt {
1562 1.107 mycroft int iobase = wdc->sc_iobase;
1563 1.21 deraadt
1564 1.64 mycroft /* Reset the device. */
1565 1.64 mycroft outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1566 1.61 mycroft delay(1000);
1567 1.64 mycroft outb(iobase+wd_ctlr, WDCTL_IDS);
1568 1.61 mycroft delay(1000);
1569 1.64 mycroft (void) inb(iobase+wd_error);
1570 1.64 mycroft outb(iobase+wd_ctlr, WDCTL_4BIT);
1571 1.64 mycroft
1572 1.64 mycroft if (wait_for_unbusy(wdc) < 0) {
1573 1.64 mycroft printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
1574 1.64 mycroft return 1;
1575 1.64 mycroft }
1576 1.64 mycroft
1577 1.64 mycroft return 0;
1578 1.64 mycroft }
1579 1.64 mycroft
1580 1.64 mycroft static void
1581 1.81 cgd wdcrestart(arg)
1582 1.81 cgd void *arg;
1583 1.64 mycroft {
1584 1.81 cgd struct wdc_softc *wdc = (struct wdc_softc *)arg;
1585 1.98 mycroft int s;
1586 1.64 mycroft
1587 1.98 mycroft s = splbio();
1588 1.64 mycroft wdcstart(wdc);
1589 1.64 mycroft splx(s);
1590 1.64 mycroft }
1591 1.64 mycroft
1592 1.64 mycroft /*
1593 1.64 mycroft * Unwedge the controller after an unexpected error. We do this by resetting
1594 1.64 mycroft * it, marking all drives for recalibration, and stalling the queue for a short
1595 1.64 mycroft * period to give the reset time to finish.
1596 1.64 mycroft * NOTE: We use a timeout here, so this routine must not be called during
1597 1.64 mycroft * autoconfig or dump.
1598 1.64 mycroft */
1599 1.64 mycroft static void
1600 1.64 mycroft wdcunwedge(wdc)
1601 1.64 mycroft struct wdc_softc *wdc;
1602 1.64 mycroft {
1603 1.108 mycroft int unit;
1604 1.64 mycroft
1605 1.98 mycroft untimeout(wdctimeout, wdc);
1606 1.64 mycroft (void) wdcreset(wdc);
1607 1.21 deraadt
1608 1.64 mycroft /* Schedule recalibrate for all drives on this controller. */
1609 1.108 mycroft for (unit = 0; unit < wdcd.cd_ndevs; unit++) {
1610 1.108 mycroft struct wd_softc *wd = wdcd.cd_devs[unit];
1611 1.64 mycroft if (!wd || (void *)wd->sc_dev.dv_parent != wdc)
1612 1.64 mycroft continue;
1613 1.69 mycroft if (wd->sc_state > RECAL)
1614 1.69 mycroft wd->sc_state = RECAL;
1615 1.64 mycroft }
1616 1.64 mycroft
1617 1.69 mycroft wdc->sc_flags |= WDCF_ERROR;
1618 1.78 mycroft ++wdc->sc_errors;
1619 1.64 mycroft
1620 1.64 mycroft /* Wake up in a little bit and restart the operation. */
1621 1.82 mycroft timeout(wdcrestart, wdc, RECOVERYTIME);
1622 1.40 mycroft }
1623 1.40 mycroft
1624 1.40 mycroft int
1625 1.64 mycroft wdcwait(wdc, mask)
1626 1.64 mycroft struct wdc_softc *wdc;
1627 1.49 mycroft int mask;
1628 1.40 mycroft {
1629 1.107 mycroft int iobase = wdc->sc_iobase;
1630 1.40 mycroft int timeout = 0;
1631 1.63 mycroft u_char status;
1632 1.87 mycroft extern int cold;
1633 1.40 mycroft
1634 1.40 mycroft for (;;) {
1635 1.87 mycroft wdc->sc_status = status = inb(iobase+wd_status);
1636 1.110 mycroft if ((status & WDCS_BSY) == 0 && (status & mask) == mask)
1637 1.54 mycroft break;
1638 1.40 mycroft if (++timeout > WDCNDELAY)
1639 1.40 mycroft return -1;
1640 1.61 mycroft delay(WDCDELAY);
1641 1.21 deraadt }
1642 1.87 mycroft if (status & WDCS_ERR) {
1643 1.64 mycroft wdc->sc_error = inb(iobase+wd_error);
1644 1.87 mycroft return WDCS_ERR;
1645 1.87 mycroft }
1646 1.23 deraadt #ifdef WDCNDELAY_DEBUG
1647 1.87 mycroft /* After autoconfig, there should be no long delays. */
1648 1.87 mycroft if (!cold && timeout > WDCNDELAY_DEBUG)
1649 1.87 mycroft printf("%s: warning: busy-wait took %dus\n",
1650 1.73 mycroft wdc->sc_dev.dv_xname, WDCDELAY * timeout);
1651 1.23 deraadt #endif
1652 1.87 mycroft return 0;
1653 1.27 cgd }
1654 1.27 cgd
1655 1.60 mycroft static void
1656 1.81 cgd wdctimeout(arg)
1657 1.81 cgd void *arg;
1658 1.27 cgd {
1659 1.81 cgd struct wdc_softc *wdc = (struct wdc_softc *)arg;
1660 1.98 mycroft int s;
1661 1.27 cgd
1662 1.98 mycroft s = splbio();
1663 1.98 mycroft wderror(wdc, NULL, "lost interrupt");
1664 1.98 mycroft wdcunwedge(wdc);
1665 1.55 mycroft splx(s);
1666 1.63 mycroft }
1667 1.63 mycroft
1668 1.63 mycroft static void
1669 1.64 mycroft wderror(dev, bp, msg)
1670 1.64 mycroft void *dev;
1671 1.63 mycroft struct buf *bp;
1672 1.63 mycroft char *msg;
1673 1.63 mycroft {
1674 1.64 mycroft struct wd_softc *wd = dev;
1675 1.64 mycroft struct wdc_softc *wdc = dev;
1676 1.64 mycroft
1677 1.63 mycroft if (bp)
1678 1.93 mycroft diskerr(bp, "wd", msg, LOG_PRINTF, wd->sc_skip,
1679 1.93 mycroft &wd->sc_dk.dk_label);
1680 1.63 mycroft else
1681 1.64 mycroft printf("%s: %s: status %b error %b\n", wdc->sc_dev.dv_xname,
1682 1.64 mycroft msg, wdc->sc_status, WDCS_BITS, wdc->sc_error, WDERR_BITS);
1683 1.21 deraadt }
1684