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