wd.c revision 1.59 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.59 mycroft * $Id: wd.c,v 1.59 1994/03/04 23:43:14 mycroft Exp $
39 1.1 cgd */
40 1.1 cgd
41 1.44 mycroft #define QUIETWORKS /* define this to make wdopen() set DKFL_QUIET */
42 1.46 mycroft #define INSTRUMENT /* instrumentation stuff by Brad Parker */
43 1.1 cgd
44 1.1 cgd #include "wd.h"
45 1.43 mycroft #if NWDC > 0
46 1.1 cgd
47 1.29 mycroft #include <sys/param.h>
48 1.29 mycroft #include <sys/dkbad.h>
49 1.29 mycroft #include <sys/systm.h>
50 1.43 mycroft #include <sys/kernel.h>
51 1.29 mycroft #include <sys/conf.h>
52 1.29 mycroft #include <sys/file.h>
53 1.29 mycroft #include <sys/stat.h>
54 1.29 mycroft #include <sys/ioctl.h>
55 1.29 mycroft #include <sys/disklabel.h>
56 1.29 mycroft #include <sys/buf.h>
57 1.29 mycroft #include <sys/uio.h>
58 1.29 mycroft #include <sys/malloc.h>
59 1.29 mycroft #include <sys/syslog.h>
60 1.3 deraadt #ifdef INSTRUMENT
61 1.29 mycroft #include <sys/dkstat.h>
62 1.3 deraadt #endif
63 1.29 mycroft
64 1.29 mycroft #include <vm/vm.h>
65 1.29 mycroft
66 1.29 mycroft #include <machine/cpu.h>
67 1.32 mycroft #include <machine/cpufunc.h>
68 1.29 mycroft #include <machine/pio.h>
69 1.29 mycroft
70 1.29 mycroft #include <i386/isa/isa.h>
71 1.29 mycroft #include <i386/isa/isa_device.h>
72 1.29 mycroft #include <i386/isa/icu.h>
73 1.29 mycroft #include <i386/isa/wdreg.h>
74 1.1 cgd
75 1.41 mycroft #define WDCNDELAY 100000 /* delay = 100us; so 10s for a controller state change */
76 1.41 mycroft #define WDCDELAY 100
77 1.19 deraadt
78 1.51 mycroft #if 0
79 1.41 mycroft /* if you enable this, it will report any delays more than 100us * N long */
80 1.51 mycroft #define WDCNDELAY_DEBUG 100
81 1.51 mycroft #endif
82 1.23 deraadt
83 1.21 deraadt #define WDIORETRIES 5 /* number of retries before giving up */
84 1.1 cgd
85 1.51 mycroft #define WDUNIT(dev) ((minor(dev) & 0xf8) >> 3)
86 1.51 mycroft #define WDPART(dev) ((minor(dev) & 0x07) )
87 1.37 mycroft #define makewddev(maj, unit, part) (makedev(maj, ((unit << 3) + part)))
88 1.1 cgd #define WDRAW 3 /* 'd' partition isn't a partition! */
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.1 cgd #define CLOSED 0 /* disk is closed. */
97 1.1 cgd #define WANTOPEN 1 /* open requested, not started */
98 1.1 cgd #define RECAL 2 /* doing restore */
99 1.1 cgd #define OPEN 3 /* done with open */
100 1.1 cgd
101 1.1 cgd /*
102 1.37 mycroft * Drive status.
103 1.1 cgd */
104 1.1 cgd struct disk {
105 1.1 cgd long dk_bc; /* byte count left */
106 1.3 deraadt long dk_bct; /* total byte count left */
107 1.1 cgd short dk_skip; /* blocks already transferred */
108 1.3 deraadt short dk_skipm; /* blocks already transferred for multi */
109 1.3 deraadt char dk_ctrlr; /* physical controller number */
110 1.1 cgd char dk_unit; /* physical unit number */
111 1.3 deraadt char dk_lunit; /* logical unit number */
112 1.1 cgd char dk_state; /* control state */
113 1.55 mycroft int dk_timeout; /* timeout counter */
114 1.1 cgd u_char dk_status; /* copy of status reg. */
115 1.1 cgd u_char dk_error; /* copy of error reg. */
116 1.1 cgd short dk_port; /* i/o port base */
117 1.1 cgd
118 1.3 deraadt u_long dk_copenpart; /* character units open on this drive */
119 1.3 deraadt u_long dk_bopenpart; /* block units open on this drive */
120 1.3 deraadt u_long dk_openpart; /* all units open on this drive */
121 1.1 cgd short dk_wlabel; /* label writable? */
122 1.1 cgd short dk_flags; /* drive characteistics found */
123 1.1 cgd #define DKFL_QUIET 0x00002 /* report errors back, but don't complain */
124 1.1 cgd #define DKFL_SINGLE 0x00004 /* sector at a time mode */
125 1.1 cgd #define DKFL_ERROR 0x00008 /* processing a disk error */
126 1.1 cgd #define DKFL_BSDLABEL 0x00010 /* has a BSD disk label */
127 1.1 cgd #define DKFL_BADSECT 0x00020 /* has a bad144 badsector table */
128 1.1 cgd #define DKFL_WRITEPROT 0x00040 /* manual unit write protect */
129 1.1 cgd struct wdparams dk_params; /* ESDI/IDE drive/controller parameters */
130 1.1 cgd struct disklabel dk_dd; /* device configuration data */
131 1.14 deraadt struct cpu_disklabel dk_cpd;
132 1.3 deraadt long dk_badsect[127]; /* 126 plus trailing -1 marker */
133 1.1 cgd };
134 1.1 cgd
135 1.9 deraadt struct board {
136 1.9 deraadt short dkc_port;
137 1.9 deraadt };
138 1.9 deraadt
139 1.9 deraadt struct board wdcontroller[NWDC];
140 1.3 deraadt struct disk *wddrives[NWD]; /* table of units */
141 1.3 deraadt struct buf wdtab[NWDC]; /* various per-controller info */
142 1.3 deraadt struct buf wdutab[NWD]; /* head of queue per drive */
143 1.3 deraadt struct buf rwdbuf[NWD]; /* buffers for raw IO */
144 1.3 deraadt long wdxfer[NWD]; /* count of transfers */
145 1.3 deraadt
146 1.3 deraadt int wdprobe(), wdattach();
147 1.3 deraadt
148 1.3 deraadt struct isa_driver wdcdriver = {
149 1.3 deraadt wdprobe, wdattach, "wdc",
150 1.1 cgd };
151 1.1 cgd
152 1.55 mycroft static void wdustart __P((struct disk *));
153 1.55 mycroft static void wdstart __P((int));
154 1.55 mycroft static int wdcommand __P((struct disk *, int));
155 1.55 mycroft static int wdcontrol __P((struct buf *));
156 1.55 mycroft static int wdsetctlr __P((struct disk *));
157 1.55 mycroft static int wdgetctlr __P((struct disk *));
158 1.55 mycroft static void bad144intern __P((struct disk *));
159 1.55 mycroft static int wdreset __P((struct disk *, int));
160 1.55 mycroft static int wdtimeout __P((caddr_t));
161 1.55 mycroft void wddisksort __P((struct buf *dp, struct buf *bp));
162 1.55 mycroft int wdc_wait __P((struct disk *, int));
163 1.51 mycroft #define wait_for_drq(d) wdc_wait(d, WDCS_DRQ)
164 1.54 mycroft #define wait_for_ready(d) wdc_wait(d, WDCS_READY | WDCS_SEEKCMPLT)
165 1.51 mycroft #define wait_for_unbusy(d) wdc_wait(d, 0)
166 1.1 cgd
167 1.1 cgd /*
168 1.1 cgd * Probe for controller.
169 1.1 cgd */
170 1.1 cgd int
171 1.55 mycroft wdprobe(isa_dev)
172 1.55 mycroft struct isa_device *isa_dev;
173 1.1 cgd {
174 1.1 cgd struct disk *du;
175 1.1 cgd int wdc;
176 1.1 cgd
177 1.55 mycroft if (isa_dev->id_unit >= NWDC)
178 1.37 mycroft return 0;
179 1.1 cgd
180 1.37 mycroft du = (struct disk *)malloc(sizeof(struct disk), M_TEMP, M_NOWAIT);
181 1.3 deraadt bzero(du, sizeof(struct disk));
182 1.3 deraadt
183 1.55 mycroft du->dk_ctrlr = isa_dev->id_unit;
184 1.3 deraadt du->dk_unit = 0;
185 1.3 deraadt du->dk_lunit = 0;
186 1.55 mycroft wdcontroller[isa_dev->id_unit].dkc_port = isa_dev->id_iobase;
187 1.1 cgd
188 1.55 mycroft wdc = du->dk_port = isa_dev->id_iobase;
189 1.3 deraadt
190 1.1 cgd /* check if we have registers that work */
191 1.3 deraadt outb(wdc+wd_error, 0x5a); /* error register not writable */
192 1.3 deraadt outb(wdc+wd_cyl_lo, 0xa5); /* but all of cyllo are implemented */
193 1.37 mycroft if (inb(wdc+wd_error) == 0x5a || inb(wdc+wd_cyl_lo) != 0xa5)
194 1.1 cgd goto nodevice;
195 1.1 cgd
196 1.51 mycroft wdreset(du, 0);
197 1.1 cgd
198 1.1 cgd /* execute a controller only command */
199 1.51 mycroft if (wdcommand(du, WDCC_DIAGNOSE) < 0)
200 1.1 cgd goto nodevice;
201 1.1 cgd
202 1.3 deraadt free(du, M_TEMP);
203 1.37 mycroft return 8;
204 1.1 cgd
205 1.1 cgd nodevice:
206 1.1 cgd free(du, M_TEMP);
207 1.37 mycroft return 0;
208 1.1 cgd }
209 1.1 cgd
210 1.1 cgd /*
211 1.9 deraadt * Called for the controller too
212 1.1 cgd * Attach each drive if possible.
213 1.1 cgd */
214 1.1 cgd int
215 1.55 mycroft wdattach(isa_dev)
216 1.55 mycroft struct isa_device *isa_dev;
217 1.1 cgd {
218 1.3 deraadt int unit, lunit;
219 1.3 deraadt struct disk *du;
220 1.56 mycroft int i, blank;
221 1.3 deraadt
222 1.55 mycroft if (isa_dev->id_masunit == -1)
223 1.37 mycroft return 0;
224 1.55 mycroft if (isa_dev->id_masunit >= NWDC)
225 1.37 mycroft return 0;
226 1.3 deraadt
227 1.55 mycroft lunit = isa_dev->id_unit;
228 1.11 deraadt if (lunit == -1) {
229 1.55 mycroft printf("wdc%d: cannot support unit ?\n", isa_dev->id_masunit);
230 1.11 deraadt return 0;
231 1.11 deraadt }
232 1.9 deraadt if (lunit >= NWD)
233 1.37 mycroft return 0;
234 1.55 mycroft unit = isa_dev->id_physid;
235 1.3 deraadt
236 1.37 mycroft du = wddrives[lunit] =
237 1.37 mycroft (struct disk *)malloc(sizeof(struct disk), M_TEMP, M_NOWAIT);
238 1.9 deraadt bzero(du, sizeof(struct disk));
239 1.9 deraadt bzero(&wdutab[lunit], sizeof(struct buf));
240 1.9 deraadt bzero(&rwdbuf[lunit], sizeof(struct buf));
241 1.9 deraadt wdxfer[lunit] = 0;
242 1.55 mycroft du->dk_ctrlr = isa_dev->id_masunit;
243 1.9 deraadt du->dk_unit = unit;
244 1.9 deraadt du->dk_lunit = lunit;
245 1.55 mycroft du->dk_port = wdcontroller[isa_dev->id_masunit].dkc_port;
246 1.9 deraadt
247 1.56 mycroft if (wdgetctlr(du) != 0) {
248 1.37 mycroft /*printf("wd%d at wdc%d slave %d -- error\n", lunit,
249 1.55 mycroft isa_dev->id_masunit, unit);*/
250 1.9 deraadt wddrives[lunit] = 0;
251 1.9 deraadt free(du, M_TEMP);
252 1.9 deraadt return 0;
253 1.1 cgd }
254 1.56 mycroft
255 1.56 mycroft printf("wd%d at wdc%d targ %d: ", isa_dev->id_unit,
256 1.56 mycroft isa_dev->id_masunit, isa_dev->id_physid);
257 1.56 mycroft if (du->dk_params.wdp_heads == 0)
258 1.56 mycroft printf("(unknown size) <");
259 1.56 mycroft else
260 1.56 mycroft printf("%dMB %d cyl, %d head, %d sec <",
261 1.56 mycroft du->dk_dd.d_ncylinders * du->dk_dd.d_secpercyl /
262 1.56 mycroft (1048576 / DEV_BSIZE),
263 1.56 mycroft du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks,
264 1.56 mycroft du->dk_dd.d_nsectors);
265 1.56 mycroft for (i = blank = 0; i < sizeof(du->dk_params.wdp_model); i++) {
266 1.56 mycroft char c = du->dk_params.wdp_model[i];
267 1.56 mycroft if (c == '\0')
268 1.56 mycroft break;
269 1.56 mycroft if (c != ' ') {
270 1.56 mycroft if (blank)
271 1.56 mycroft printf(" %c", c);
272 1.56 mycroft else
273 1.56 mycroft printf("%c", c);
274 1.56 mycroft blank = 0;
275 1.56 mycroft } else
276 1.56 mycroft blank = 1;
277 1.56 mycroft }
278 1.56 mycroft printf(">\n");
279 1.56 mycroft
280 1.56 mycroft wdtimeout((caddr_t)du);
281 1.56 mycroft
282 1.9 deraadt return 1;
283 1.1 cgd }
284 1.1 cgd
285 1.1 cgd /* Read/write routine for a buffer. Finds the proper unit, range checks
286 1.1 cgd * arguments, and schedules the transfer. Does not wait for the transfer
287 1.1 cgd * to complete. Multi-page transfers are supported. All I/O requests must
288 1.1 cgd * be a multiple of a sector in length.
289 1.1 cgd */
290 1.1 cgd int
291 1.55 mycroft wdstrategy(bp)
292 1.55 mycroft struct buf *bp;
293 1.1 cgd {
294 1.55 mycroft struct buf *dp;
295 1.1 cgd struct disk *du; /* Disk unit to do the IO. */
296 1.51 mycroft int lunit = WDUNIT(bp->b_dev);
297 1.37 mycroft int s;
298 1.3 deraadt
299 1.1 cgd /* valid unit, controller, and request? */
300 1.3 deraadt if (lunit >= NWD || bp->b_blkno < 0 ||
301 1.37 mycroft howmany(bp->b_bcount, DEV_BSIZE) >= (1 << NBBY) ||
302 1.3 deraadt (du = wddrives[lunit]) == 0) {
303 1.1 cgd bp->b_error = EINVAL;
304 1.1 cgd bp->b_flags |= B_ERROR;
305 1.1 cgd goto done;
306 1.1 cgd }
307 1.3 deraadt
308 1.1 cgd /* "soft" write protect check */
309 1.1 cgd if ((du->dk_flags & DKFL_WRITEPROT) && (bp->b_flags & B_READ) == 0) {
310 1.1 cgd bp->b_error = EROFS;
311 1.1 cgd bp->b_flags |= B_ERROR;
312 1.1 cgd goto done;
313 1.1 cgd }
314 1.3 deraadt
315 1.1 cgd /* have partitions and want to use them? */
316 1.51 mycroft if ((du->dk_flags & DKFL_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW) {
317 1.1 cgd /*
318 1.1 cgd * do bounds checking, adjust transfer. if error, process.
319 1.1 cgd * if end of partition, just return
320 1.1 cgd */
321 1.1 cgd if (bounds_check_with_label(bp, &du->dk_dd, du->dk_wlabel) <= 0)
322 1.1 cgd goto done;
323 1.1 cgd /* otherwise, process transfer request */
324 1.1 cgd }
325 1.3 deraadt
326 1.38 mycroft /* don't bother */
327 1.38 mycroft bp->b_cylin = 0;
328 1.38 mycroft
329 1.1 cgd /* queue transfer on drive, activate drive and controller if idle */
330 1.3 deraadt dp = &wdutab[lunit];
331 1.1 cgd s = splbio();
332 1.49 mycroft wddisksort(dp, bp);
333 1.1 cgd if (dp->b_active == 0)
334 1.1 cgd wdustart(du); /* start drive */
335 1.3 deraadt if (wdtab[du->dk_ctrlr].b_active == 0)
336 1.3 deraadt wdstart(du->dk_ctrlr); /* start controller */
337 1.1 cgd splx(s);
338 1.3 deraadt return 0;
339 1.3 deraadt
340 1.1 cgd done:
341 1.1 cgd /* toss transfer, we're done early */
342 1.1 cgd biodone(bp);
343 1.1 cgd }
344 1.1 cgd
345 1.1 cgd /*
346 1.49 mycroft * Need to skip over multitransfer bufs.
347 1.49 mycroft */
348 1.49 mycroft void
349 1.55 mycroft wddisksort(dp, bp)
350 1.55 mycroft struct buf *dp, *bp;
351 1.49 mycroft {
352 1.55 mycroft struct buf *ap;
353 1.49 mycroft
354 1.49 mycroft while ((ap = dp->b_actf) && ap->b_flags & B_XXX)
355 1.49 mycroft dp = ap;
356 1.49 mycroft disksort(dp, bp);
357 1.49 mycroft }
358 1.49 mycroft
359 1.49 mycroft /*
360 1.1 cgd * Routine to queue a command to the controller. The unit's
361 1.1 cgd * request is linked into the active list for the controller.
362 1.1 cgd * If the controller is idle, the transfer is started.
363 1.1 cgd */
364 1.1 cgd static void
365 1.55 mycroft wdustart(du)
366 1.55 mycroft struct disk *du;
367 1.1 cgd {
368 1.55 mycroft struct buf *dp = &wdutab[du->dk_lunit];
369 1.3 deraadt int ctrlr = du->dk_ctrlr;
370 1.3 deraadt
371 1.1 cgd /* unit already active? */
372 1.1 cgd if (dp->b_active)
373 1.1 cgd return;
374 1.3 deraadt
375 1.1 cgd /* anything to start? */
376 1.53 mycroft if (dp->b_actf == NULL)
377 1.1 cgd return;
378 1.3 deraadt
379 1.1 cgd /* link onto controller queue */
380 1.1 cgd dp->b_forw = NULL;
381 1.51 mycroft if (wdtab[ctrlr].b_forw == NULL)
382 1.51 mycroft wdtab[ctrlr].b_forw = dp;
383 1.1 cgd else
384 1.3 deraadt wdtab[ctrlr].b_actl->b_forw = dp;
385 1.3 deraadt wdtab[ctrlr].b_actl = dp;
386 1.3 deraadt
387 1.1 cgd /* mark the drive unit as busy */
388 1.1 cgd dp->b_active = 1;
389 1.1 cgd }
390 1.1 cgd
391 1.1 cgd /*
392 1.1 cgd * Controller startup routine. This does the calculation, and starts
393 1.1 cgd * a single-sector read or write operation. Called to start a transfer,
394 1.1 cgd * or from the interrupt routine to continue a multi-sector transfer.
395 1.1 cgd * RESTRICTIONS:
396 1.1 cgd * 1. The transfer length must be an exact multiple of the sector size.
397 1.1 cgd */
398 1.1 cgd static void
399 1.55 mycroft wdstart(ctrlr)
400 1.55 mycroft int ctrlr;
401 1.1 cgd {
402 1.1 cgd register struct disk *du; /* disk unit for IO */
403 1.1 cgd register struct buf *bp;
404 1.1 cgd struct disklabel *lp;
405 1.1 cgd struct buf *dp;
406 1.54 mycroft long blknum, cylin, head, sector;
407 1.54 mycroft long secpertrk, secpercyl, addr;
408 1.54 mycroft int lunit, wdc;
409 1.3 deraadt int xfrblknum;
410 1.3 deraadt
411 1.1 cgd loop:
412 1.1 cgd /* is there a drive for the controller to do a transfer with? */
413 1.51 mycroft dp = wdtab[ctrlr].b_forw;
414 1.53 mycroft if (dp == NULL) {
415 1.53 mycroft wdtab[ctrlr].b_active = 0;
416 1.1 cgd return;
417 1.53 mycroft }
418 1.3 deraadt
419 1.1 cgd /* is there a transfer to this drive ? if so, link it on
420 1.1 cgd the controller's queue */
421 1.1 cgd bp = dp->b_actf;
422 1.1 cgd if (bp == NULL) {
423 1.53 mycroft dp->b_active = 0;
424 1.51 mycroft wdtab[ctrlr].b_forw = dp->b_forw;
425 1.1 cgd goto loop;
426 1.1 cgd }
427 1.3 deraadt
428 1.1 cgd /* obtain controller and drive information */
429 1.51 mycroft lunit = WDUNIT(bp->b_dev);
430 1.3 deraadt du = wddrives[lunit];
431 1.51 mycroft
432 1.51 mycroft /* clear any pending timeout, just in case */
433 1.55 mycroft du->dk_timeout = 0;
434 1.3 deraadt
435 1.1 cgd /* if not really a transfer, do control operations specially */
436 1.1 cgd if (du->dk_state < OPEN) {
437 1.1 cgd (void) wdcontrol(bp);
438 1.1 cgd return;
439 1.1 cgd }
440 1.3 deraadt
441 1.1 cgd /* calculate transfer details */
442 1.1 cgd blknum = bp->b_blkno + du->dk_skip;
443 1.44 mycroft #ifdef WDDEBUG
444 1.1 cgd if (du->dk_skip == 0)
445 1.3 deraadt printf("\nwdstart %d: %s %d@%d; map ", lunit,
446 1.37 mycroft (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
447 1.37 mycroft blknum);
448 1.1 cgd else
449 1.3 deraadt printf(" %d)%x", du->dk_skip, inb(du->dk_port+wd_altsts));
450 1.1 cgd #endif
451 1.37 mycroft addr = (int)bp->b_un.b_addr;
452 1.37 mycroft if (du->dk_skip == 0)
453 1.1 cgd du->dk_bc = bp->b_bcount;
454 1.3 deraadt if (du->dk_skipm == 0) {
455 1.3 deraadt struct buf *oldbp, *nextbp;
456 1.3 deraadt oldbp = bp;
457 1.33 mycroft nextbp = bp->b_actf;
458 1.3 deraadt du->dk_bct = du->dk_bc;
459 1.3 deraadt oldbp->b_flags |= B_XXX;
460 1.44 mycroft while (nextbp && (oldbp->b_flags & DKFL_SINGLE) == 0 &&
461 1.44 mycroft oldbp->b_dev == nextbp->b_dev && nextbp->b_blkno ==
462 1.44 mycroft (oldbp->b_blkno + (oldbp->b_bcount / DEV_BSIZE)) &&
463 1.37 mycroft (oldbp->b_flags & B_READ) == (nextbp->b_flags & B_READ)) {
464 1.37 mycroft if ((du->dk_bct + nextbp->b_bcount) / DEV_BSIZE >= 240)
465 1.3 deraadt break;
466 1.3 deraadt du->dk_bct += nextbp->b_bcount;
467 1.49 mycroft nextbp->b_flags |= B_XXX;
468 1.3 deraadt oldbp = nextbp;
469 1.33 mycroft nextbp = nextbp->b_actf;
470 1.3 deraadt }
471 1.3 deraadt }
472 1.3 deraadt
473 1.1 cgd lp = &du->dk_dd;
474 1.1 cgd secpertrk = lp->d_nsectors;
475 1.1 cgd secpercyl = lp->d_secpercyl;
476 1.51 mycroft if ((du->dk_flags & DKFL_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW)
477 1.51 mycroft blknum += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
478 1.1 cgd cylin = blknum / secpercyl;
479 1.1 cgd head = (blknum % secpercyl) / secpertrk;
480 1.1 cgd sector = blknum % secpertrk;
481 1.3 deraadt
482 1.3 deraadt /* Check for bad sectors if we have them, and not formatting */
483 1.3 deraadt /* Only do this in single-sector mode, or when starting a */
484 1.3 deraadt /* multiple-sector transfer. */
485 1.37 mycroft if ((du->dk_flags & DKFL_BADSECT) &&
486 1.44 mycroft #ifdef B_FORMAT
487 1.37 mycroft (bp->b_flags & B_FORMAT) == 0 &&
488 1.3 deraadt #endif
489 1.44 mycroft (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE))) {
490 1.1 cgd
491 1.3 deraadt long blkchk, blkend, blknew;
492 1.3 deraadt int i;
493 1.3 deraadt
494 1.3 deraadt blkend = blknum + howmany(du->dk_bct, DEV_BSIZE) - 1;
495 1.3 deraadt for (i = 0; (blkchk = du->dk_badsect[i]) != -1; i++) {
496 1.37 mycroft if (blkchk > blkend)
497 1.3 deraadt break; /* transfer is completely OK; done */
498 1.37 mycroft if (blkchk == blknum) {
499 1.37 mycroft blknew =
500 1.37 mycroft lp->d_secperunit - lp->d_nsectors - i - 1;
501 1.3 deraadt cylin = blknew / secpercyl;
502 1.3 deraadt head = (blknew % secpercyl) / secpertrk;
503 1.3 deraadt sector = blknew % secpertrk;
504 1.3 deraadt du->dk_flags |= DKFL_SINGLE;
505 1.3 deraadt /* found and replaced first blk of transfer; done */
506 1.3 deraadt break;
507 1.3 deraadt } else if (blkchk > blknum) {
508 1.3 deraadt du->dk_flags |= DKFL_SINGLE;
509 1.3 deraadt break; /* bad block inside transfer; done */
510 1.3 deraadt }
511 1.3 deraadt }
512 1.3 deraadt }
513 1.37 mycroft if (du->dk_flags & DKFL_SINGLE) {
514 1.3 deraadt du->dk_bct = du->dk_bc;
515 1.3 deraadt du->dk_skipm = du->dk_skip;
516 1.3 deraadt }
517 1.3 deraadt
518 1.3 deraadt #ifdef WDDEBUG
519 1.51 mycroft printf("c%d h%d s%d ", cylin, head, sector);
520 1.1 cgd #endif
521 1.3 deraadt
522 1.49 mycroft sector++; /* sectors begin with 1, not 0 */
523 1.3 deraadt
524 1.3 deraadt wdtab[ctrlr].b_active = 1; /* mark controller active */
525 1.1 cgd wdc = du->dk_port;
526 1.3 deraadt
527 1.3 deraadt #ifdef INSTRUMENT
528 1.3 deraadt /* instrumentation */
529 1.3 deraadt if (du->dk_unit >= 0 && du->dk_skip == 0) {
530 1.3 deraadt dk_busy |= 1 << du->dk_lunit;
531 1.3 deraadt dk_wds[du->dk_lunit] += bp->b_bcount >> 6;
532 1.3 deraadt }
533 1.3 deraadt if (du->dk_unit >= 0 && du->dk_skipm == 0) {
534 1.3 deraadt ++dk_seek[du->dk_lunit];
535 1.3 deraadt ++dk_xfer[du->dk_lunit];
536 1.3 deraadt }
537 1.3 deraadt #endif
538 1.1 cgd
539 1.19 deraadt retry:
540 1.1 cgd /* if starting a multisector transfer, or doing single transfers */
541 1.3 deraadt if (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE)) {
542 1.51 mycroft int command;
543 1.51 mycroft
544 1.3 deraadt if (wdtab[ctrlr].b_errcnt && (bp->b_flags & B_READ) == 0) {
545 1.1 cgd du->dk_bc += DEV_BSIZE;
546 1.3 deraadt du->dk_bct += DEV_BSIZE;
547 1.3 deraadt }
548 1.1 cgd
549 1.1 cgd /* controller idle? */
550 1.51 mycroft if (wait_for_unbusy(du) < 0) {
551 1.51 mycroft wdreset(du, 1);
552 1.51 mycroft goto retry;
553 1.51 mycroft }
554 1.3 deraadt
555 1.1 cgd /* stuff the task file */
556 1.1 cgd outb(wdc+wd_precomp, lp->d_precompcyl / 4);
557 1.37 mycroft #ifdef B_FORMAT
558 1.1 cgd if (bp->b_flags & B_FORMAT) {
559 1.1 cgd outb(wdc+wd_sector, lp->d_gap3);
560 1.1 cgd outb(wdc+wd_seccnt, lp->d_nsectors);
561 1.1 cgd } else {
562 1.3 deraadt if (du->dk_flags & DKFL_SINGLE)
563 1.3 deraadt outb(wdc+wd_seccnt, 1);
564 1.3 deraadt else
565 1.37 mycroft outb(wdc+wd_seccnt,
566 1.37 mycroft howmany(du->dk_bct, DEV_BSIZE));
567 1.3 deraadt outb(wdc+wd_sector, sector);
568 1.3 deraadt }
569 1.3 deraadt #else
570 1.1 cgd if (du->dk_flags & DKFL_SINGLE)
571 1.1 cgd outb(wdc+wd_seccnt, 1);
572 1.1 cgd else
573 1.3 deraadt outb(wdc+wd_seccnt, howmany(du->dk_bct, DEV_BSIZE));
574 1.1 cgd outb(wdc+wd_sector, sector);
575 1.1 cgd #endif
576 1.1 cgd outb(wdc+wd_cyl_lo, cylin);
577 1.1 cgd outb(wdc+wd_cyl_hi, cylin >> 8);
578 1.3 deraadt
579 1.1 cgd /* set up the SDH register (select drive) */
580 1.3 deraadt outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit<<4) | (head & 0xf));
581 1.1 cgd /* wait for drive to become ready */
582 1.51 mycroft if (wait_for_ready(du) < 0) {
583 1.51 mycroft wdreset(du, 1);
584 1.19 deraadt goto retry;
585 1.19 deraadt }
586 1.3 deraadt
587 1.1 cgd /* initiate command! */
588 1.44 mycroft #ifdef B_FORMAT
589 1.1 cgd if (bp->b_flags & B_FORMAT)
590 1.51 mycroft command = WDCC_FORMAT;
591 1.1 cgd else
592 1.51 mycroft command =
593 1.51 mycroft (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
594 1.3 deraadt #else
595 1.51 mycroft command = (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
596 1.1 cgd #endif
597 1.51 mycroft if (wdcommand(du, command) < 0) {
598 1.51 mycroft wdreset(du, 1);
599 1.51 mycroft goto retry;
600 1.51 mycroft }
601 1.44 mycroft #ifdef WDDEBUG
602 1.37 mycroft printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
603 1.37 mycroft cylin, head, addr, inb(wdc+wd_altsts));
604 1.1 cgd #endif
605 1.1 cgd }
606 1.3 deraadt
607 1.1 cgd /* if this is a read operation, just go away until it's done. */
608 1.27 cgd if (bp->b_flags & B_READ) {
609 1.55 mycroft du->dk_timeout = 2;
610 1.3 deraadt return;
611 1.27 cgd }
612 1.3 deraadt
613 1.51 mycroft if (wait_for_drq(du) < 0) {
614 1.51 mycroft wdreset(du, 1);
615 1.19 deraadt goto retry;
616 1.19 deraadt }
617 1.1 cgd /* then send it! */
618 1.37 mycroft outsw(wdc+wd_data, addr + du->dk_skip * DEV_BSIZE,
619 1.44 mycroft DEV_BSIZE / sizeof(short));
620 1.51 mycroft
621 1.1 cgd du->dk_bc -= DEV_BSIZE;
622 1.3 deraadt du->dk_bct -= DEV_BSIZE;
623 1.49 mycroft
624 1.55 mycroft du->dk_timeout = 2;
625 1.1 cgd }
626 1.1 cgd
627 1.1 cgd /* Interrupt routine for the controller. Acknowledge the interrupt, check for
628 1.1 cgd * errors on the current operation, mark it done if necessary, and start
629 1.1 cgd * the next request. Also check for a partially done transfer, and
630 1.1 cgd * continue with the next chunk if so.
631 1.1 cgd */
632 1.1 cgd void
633 1.55 mycroft wdintr(ctrlr)
634 1.55 mycroft int ctrlr;
635 1.1 cgd {
636 1.1 cgd register struct disk *du;
637 1.1 cgd register struct buf *bp, *dp;
638 1.51 mycroft int stat, wdc;
639 1.59 mycroft
640 1.59 mycroft /* clear the pending interrupt */
641 1.59 mycroft (void) inb(wdcontroller[ctrlr].dkc_port+wd_status);
642 1.51 mycroft
643 1.3 deraadt if (!wdtab[ctrlr].b_active) {
644 1.3 deraadt printf("wdc%d: extra interrupt\n", ctrlr);
645 1.1 cgd return;
646 1.1 cgd }
647 1.3 deraadt
648 1.51 mycroft dp = wdtab[ctrlr].b_forw;
649 1.1 cgd bp = dp->b_actf;
650 1.51 mycroft du = wddrives[WDUNIT(bp->b_dev)];
651 1.1 cgd wdc = du->dk_port;
652 1.55 mycroft du->dk_timeout = 0;
653 1.3 deraadt
654 1.44 mycroft #ifdef WDDEBUG
655 1.3 deraadt printf("I%d ", ctrlr);
656 1.1 cgd #endif
657 1.13 deraadt
658 1.51 mycroft stat = wait_for_unbusy(du);
659 1.51 mycroft if (stat < 0) {
660 1.51 mycroft printf("wdc%d: timeout waiting for unbusy\n", ctrlr);
661 1.51 mycroft goto lose;
662 1.27 cgd }
663 1.3 deraadt
664 1.1 cgd /* is it not a transfer, but a control operation? */
665 1.1 cgd if (du->dk_state < OPEN) {
666 1.47 mycroft wdtab[ctrlr].b_active = 0;
667 1.1 cgd if (wdcontrol(bp))
668 1.3 deraadt wdstart(ctrlr);
669 1.1 cgd return;
670 1.1 cgd }
671 1.3 deraadt
672 1.1 cgd /* have we an error? */
673 1.40 mycroft if (stat & (WDCS_ERR | WDCS_ECCCOR)) {
674 1.51 mycroft lose:
675 1.40 mycroft du->dk_status = stat;
676 1.44 mycroft du->dk_error = inb(wdc+wd_error);
677 1.44 mycroft #ifdef WDDEBUG
678 1.40 mycroft printf("stat %x error %x\n", stat, du->dk_error);
679 1.1 cgd #endif
680 1.37 mycroft if ((du->dk_flags & DKFL_SINGLE) == 0) {
681 1.37 mycroft du->dk_flags |= DKFL_ERROR;
682 1.1 cgd goto outt;
683 1.1 cgd }
684 1.1 cgd #ifdef B_FORMAT
685 1.1 cgd if (bp->b_flags & B_FORMAT) {
686 1.46 mycroft bp->b_error = EIO;
687 1.1 cgd bp->b_flags |= B_ERROR;
688 1.1 cgd goto done;
689 1.1 cgd }
690 1.1 cgd #endif
691 1.3 deraadt
692 1.1 cgd /* error or error correction? */
693 1.40 mycroft if (stat & WDCS_ERR) {
694 1.37 mycroft if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
695 1.3 deraadt wdtab[ctrlr].b_active = 0;
696 1.37 mycroft else {
697 1.37 mycroft if ((du->dk_flags & DKFL_QUIET) == 0) {
698 1.1 cgd diskerr(bp, "wd", "hard error",
699 1.37 mycroft LOG_PRINTF, du->dk_skip,
700 1.37 mycroft &du->dk_dd);
701 1.1 cgd #ifdef WDDEBUG
702 1.40 mycroft printf("stat %b error %b\n", stat,
703 1.37 mycroft WDCS_BITS, inb(wdc+wd_error),
704 1.37 mycroft WDERR_BITS);
705 1.1 cgd #endif
706 1.1 cgd }
707 1.49 mycroft bp->b_error = EIO;
708 1.1 cgd bp->b_flags |= B_ERROR; /* flag the error */
709 1.1 cgd }
710 1.37 mycroft } else if ((du->dk_flags & DKFL_QUIET) == 0)
711 1.37 mycroft diskerr(bp, "wd", "soft ecc", 0, du->dk_skip,
712 1.37 mycroft &du->dk_dd);
713 1.1 cgd }
714 1.3 deraadt
715 1.1 cgd /*
716 1.1 cgd * If this was a successful read operation, fetch the data.
717 1.1 cgd */
718 1.37 mycroft if (((bp->b_flags & (B_READ | B_ERROR)) == B_READ) &&
719 1.37 mycroft wdtab[ctrlr].b_active) {
720 1.51 mycroft int chk;
721 1.3 deraadt
722 1.1 cgd chk = min(DEV_BSIZE / sizeof(short), du->dk_bc / sizeof(short));
723 1.3 deraadt
724 1.1 cgd /* ready to receive data? */
725 1.51 mycroft if (wait_for_drq(du) < 0) {
726 1.51 mycroft printf("wdc%d: timeout waiting for drq\n", ctrlr);
727 1.51 mycroft goto lose;
728 1.27 cgd }
729 1.27 cgd
730 1.1 cgd /* suck in data */
731 1.37 mycroft insw(wdc+wd_data,
732 1.40 mycroft (int)bp->b_un.b_addr + du->dk_skip * DEV_BSIZE, chk);
733 1.1 cgd du->dk_bc -= chk * sizeof(short);
734 1.3 deraadt du->dk_bct -= chk * sizeof(short);
735 1.3 deraadt
736 1.1 cgd /* for obselete fractional sector reads */
737 1.3 deraadt while (chk++ < (DEV_BSIZE / sizeof(short)))
738 1.51 mycroft (void) inw(wdc+wd_data);
739 1.1 cgd }
740 1.3 deraadt
741 1.3 deraadt wdxfer[du->dk_lunit]++;
742 1.46 mycroft outt:
743 1.3 deraadt if (wdtab[ctrlr].b_active) {
744 1.3 deraadt #ifdef INSTRUMENT
745 1.3 deraadt if (du->dk_unit >= 0)
746 1.37 mycroft dk_busy &= ~(1 << du->dk_unit);
747 1.3 deraadt #endif
748 1.1 cgd if ((bp->b_flags & B_ERROR) == 0) {
749 1.37 mycroft du->dk_skip++; /* Add to succ. sect */
750 1.37 mycroft du->dk_skipm++; /* Add to succ. sect for multitransfer */
751 1.37 mycroft if (wdtab[ctrlr].b_errcnt &&
752 1.37 mycroft (du->dk_flags & DKFL_QUIET) == 0)
753 1.37 mycroft diskerr(bp, "wd", "soft error", 0, du->dk_skip,
754 1.37 mycroft &du->dk_dd);
755 1.3 deraadt wdtab[ctrlr].b_errcnt = 0;
756 1.3 deraadt
757 1.1 cgd /* see if more to transfer */
758 1.1 cgd if (du->dk_bc > 0 && (du->dk_flags & DKFL_ERROR) == 0) {
759 1.49 mycroft wdtab[ctrlr].b_active = 0;
760 1.48 mycroft wdstart(ctrlr);
761 1.48 mycroft return; /* next chunk is started */
762 1.37 mycroft } else if ((du->dk_flags & (DKFL_SINGLE | DKFL_ERROR)) == DKFL_ERROR) {
763 1.1 cgd du->dk_skip = 0;
764 1.3 deraadt du->dk_skipm = 0;
765 1.1 cgd du->dk_flags &= ~DKFL_ERROR;
766 1.1 cgd du->dk_flags |= DKFL_SINGLE;
767 1.51 mycroft wdtab[ctrlr].b_active = 0;
768 1.3 deraadt wdstart(ctrlr);
769 1.51 mycroft return; /* redo xfer sector by sector */
770 1.1 cgd }
771 1.1 cgd }
772 1.1 cgd
773 1.1 cgd done:
774 1.1 cgd /* done with this transfer, with or without error */
775 1.1 cgd du->dk_flags &= ~DKFL_SINGLE;
776 1.3 deraadt wdtab[ctrlr].b_errcnt = 0;
777 1.37 mycroft if (du->dk_bct == 0) {
778 1.51 mycroft wdtab[ctrlr].b_forw = dp->b_forw;
779 1.3 deraadt du->dk_skipm = 0;
780 1.3 deraadt dp->b_active = 0;
781 1.3 deraadt }
782 1.50 mycroft bp->b_resid = bp->b_bcount - du->dk_skip * DEV_BSIZE;
783 1.50 mycroft bp->b_flags &= ~B_XXX;
784 1.50 mycroft du->dk_skip = 0;
785 1.33 mycroft dp->b_actf = bp->b_actf;
786 1.1 cgd dp->b_errcnt = 0;
787 1.1 cgd biodone(bp);
788 1.1 cgd }
789 1.49 mycroft
790 1.49 mycroft /* controller now idle */
791 1.46 mycroft wdtab[ctrlr].b_active = 0;
792 1.46 mycroft
793 1.1 cgd /* anything more on drive queue? */
794 1.3 deraadt if (dp->b_actf && du->dk_bct == 0)
795 1.1 cgd wdustart(du);
796 1.3 deraadt
797 1.1 cgd /* anything more for controller to do? */
798 1.51 mycroft if (wdtab[ctrlr].b_forw)
799 1.3 deraadt wdstart(ctrlr);
800 1.1 cgd }
801 1.1 cgd
802 1.1 cgd /*
803 1.1 cgd * Initialize a drive.
804 1.1 cgd */
805 1.1 cgd int
806 1.55 mycroft wdopen(dev, flag, fmt, p)
807 1.55 mycroft dev_t dev;
808 1.55 mycroft int flag;
809 1.55 mycroft int fmt;
810 1.55 mycroft struct proc *p;
811 1.1 cgd {
812 1.54 mycroft int lunit;
813 1.54 mycroft struct disk *du;
814 1.51 mycroft int part = WDPART(dev), mask = 1 << part;
815 1.3 deraadt struct partition *pp;
816 1.1 cgd char *msg;
817 1.3 deraadt
818 1.51 mycroft lunit = WDUNIT(dev);
819 1.3 deraadt if (lunit >= NWD)
820 1.37 mycroft return ENXIO;
821 1.3 deraadt du = wddrives[lunit];
822 1.3 deraadt if (du == 0)
823 1.37 mycroft return ENXIO;
824 1.3 deraadt
825 1.3 deraadt #ifdef QUIETWORKS
826 1.3 deraadt if (part == WDRAW)
827 1.3 deraadt du->dk_flags |= DKFL_QUIET;
828 1.3 deraadt else
829 1.3 deraadt du->dk_flags &= ~DKFL_QUIET;
830 1.3 deraadt #else
831 1.3 deraadt du->dk_flags &= ~DKFL_QUIET;
832 1.3 deraadt #endif
833 1.3 deraadt
834 1.1 cgd if ((du->dk_flags & DKFL_BSDLABEL) == 0) {
835 1.1 cgd du->dk_flags |= DKFL_WRITEPROT;
836 1.3 deraadt wdutab[lunit].b_actf = NULL;
837 1.1 cgd
838 1.1 cgd /*
839 1.1 cgd * Use the default sizes until we've read the label,
840 1.1 cgd * or longer if there isn't one there.
841 1.1 cgd */
842 1.1 cgd bzero(&du->dk_dd, sizeof(du->dk_dd));
843 1.1 cgd du->dk_dd.d_type = DTYPE_ST506;
844 1.1 cgd du->dk_dd.d_ncylinders = 1024;
845 1.1 cgd du->dk_dd.d_secsize = DEV_BSIZE;
846 1.1 cgd du->dk_dd.d_ntracks = 8;
847 1.1 cgd du->dk_dd.d_nsectors = 17;
848 1.1 cgd du->dk_dd.d_secpercyl = 17*8;
849 1.3 deraadt du->dk_dd.d_secperunit = 17*8*1024;
850 1.1 cgd du->dk_state = WANTOPEN;
851 1.1 cgd
852 1.3 deraadt /* read label using "raw" partition */
853 1.55 mycroft #ifdef notdef
854 1.55 mycroft /* wdsetctlr(du); */ /* Maybe do this TIH */
855 1.51 mycroft msg = readdisklabel(makewddev(major(dev), WDUNIT(dev), WDRAW),
856 1.37 mycroft wdstrategy, &du->dk_dd, &du->dk_cpd);
857 1.55 mycroft wdsetctlr(du);
858 1.3 deraadt #endif
859 1.51 mycroft if (msg = readdisklabel(makewddev(major(dev), WDUNIT(dev),
860 1.42 mycroft WDRAW), wdstrategy, &du->dk_dd, &du->dk_cpd)) {
861 1.37 mycroft if ((du->dk_flags & DKFL_QUIET) == 0) {
862 1.37 mycroft log(LOG_WARNING,
863 1.37 mycroft "wd%d: cannot find label (%s)\n",
864 1.37 mycroft lunit, msg);
865 1.46 mycroft return EINVAL; /* XXX needs translation */
866 1.1 cgd }
867 1.1 cgd } else {
868 1.55 mycroft wdsetctlr(du);
869 1.1 cgd du->dk_flags |= DKFL_BSDLABEL;
870 1.1 cgd du->dk_flags &= ~DKFL_WRITEPROT;
871 1.1 cgd if (du->dk_dd.d_flags & D_BADSECT)
872 1.1 cgd du->dk_flags |= DKFL_BADSECT;
873 1.1 cgd }
874 1.3 deraadt }
875 1.3 deraadt
876 1.3 deraadt if (du->dk_flags & DKFL_BADSECT)
877 1.3 deraadt bad144intern(du);
878 1.1 cgd
879 1.3 deraadt /*
880 1.3 deraadt * Warn if a partion is opened
881 1.3 deraadt * that overlaps another partition which is open
882 1.3 deraadt * unless one is the "raw" partition (whole disk).
883 1.3 deraadt */
884 1.46 mycroft if ((du->dk_openpart & mask) == 0 && part != WDRAW) {
885 1.46 mycroft int start, end;
886 1.3 deraadt
887 1.3 deraadt pp = &du->dk_dd.d_partitions[part];
888 1.3 deraadt start = pp->p_offset;
889 1.3 deraadt end = pp->p_offset + pp->p_size;
890 1.3 deraadt for (pp = du->dk_dd.d_partitions;
891 1.44 mycroft pp < &du->dk_dd.d_partitions[du->dk_dd.d_npartitions];
892 1.44 mycroft pp++) {
893 1.44 mycroft if (pp->p_offset + pp->p_size <= start ||
894 1.44 mycroft pp->p_offset >= end)
895 1.3 deraadt continue;
896 1.3 deraadt if (pp - du->dk_dd.d_partitions == WDRAW)
897 1.3 deraadt continue;
898 1.3 deraadt if (du->dk_openpart & (1 << (pp - du->dk_dd.d_partitions)))
899 1.3 deraadt log(LOG_WARNING,
900 1.37 mycroft "wd%d%c: overlaps open partition (%c)\n",
901 1.37 mycroft lunit, part + 'a',
902 1.37 mycroft pp - du->dk_dd.d_partitions + 'a');
903 1.3 deraadt }
904 1.1 cgd }
905 1.37 mycroft if (part >= du->dk_dd.d_npartitions && part != WDRAW)
906 1.37 mycroft return ENXIO;
907 1.3 deraadt
908 1.1 cgd /* insure only one open at a time */
909 1.3 deraadt du->dk_openpart |= mask;
910 1.3 deraadt switch (fmt) {
911 1.3 deraadt case S_IFCHR:
912 1.3 deraadt du->dk_copenpart |= mask;
913 1.3 deraadt break;
914 1.3 deraadt case S_IFBLK:
915 1.3 deraadt du->dk_bopenpart |= mask;
916 1.3 deraadt break;
917 1.3 deraadt }
918 1.37 mycroft return 0;
919 1.1 cgd }
920 1.1 cgd
921 1.1 cgd /*
922 1.1 cgd * Implement operations other than read/write.
923 1.1 cgd * Called from wdstart or wdintr during opens and formats.
924 1.1 cgd * Uses finite-state-machine to track progress of operation in progress.
925 1.1 cgd * Returns 0 if operation still in progress, 1 if completed.
926 1.1 cgd */
927 1.1 cgd static int
928 1.55 mycroft wdcontrol(bp)
929 1.55 mycroft struct buf *bp;
930 1.1 cgd {
931 1.54 mycroft struct disk *du;
932 1.54 mycroft int unit, lunit;
933 1.54 mycroft int stat;
934 1.40 mycroft int s, ctrlr;
935 1.3 deraadt int wdc;
936 1.3 deraadt
937 1.51 mycroft du = wddrives[WDUNIT(bp->b_dev)];
938 1.3 deraadt ctrlr = du->dk_ctrlr;
939 1.1 cgd unit = du->dk_unit;
940 1.3 deraadt lunit = du->dk_lunit;
941 1.1 cgd wdc = du->dk_port;
942 1.3 deraadt
943 1.1 cgd switch (du->dk_state) {
944 1.1 cgd case WANTOPEN: /* set SDH, step rate, do restore */
945 1.46 mycroft tryagainrecal:
946 1.44 mycroft #ifdef WDDEBUG
947 1.3 deraadt printf("wd%d: recal ", lunit);
948 1.1 cgd #endif
949 1.1 cgd s = splbio(); /* not called from intr level ... */
950 1.55 mycroft wdgetctlr(du);
951 1.21 deraadt
952 1.51 mycroft if (wait_for_unbusy(du) < 0) {
953 1.55 mycroft lose:
954 1.51 mycroft wdreset(du, 1);
955 1.58 mycroft splx(s);
956 1.21 deraadt goto tryagainrecal;
957 1.21 deraadt }
958 1.1 cgd outb(wdc+wd_sdh, WDSD_IBM | (unit << 4));
959 1.55 mycroft if (wait_for_ready(du) < 0)
960 1.55 mycroft goto lose;
961 1.3 deraadt wdtab[ctrlr].b_active = 1;
962 1.55 mycroft if (wdcommand(du, WDCC_RESTORE | WD_STEP) < 0)
963 1.55 mycroft goto lose;
964 1.3 deraadt du->dk_state = RECAL;
965 1.1 cgd splx(s);
966 1.37 mycroft return 0;
967 1.55 mycroft
968 1.1 cgd case RECAL:
969 1.51 mycroft if ((stat = inb(wdc+wd_altsts)) & WDCS_ERR) {
970 1.1 cgd if ((du->dk_flags & DKFL_QUIET) == 0) {
971 1.3 deraadt printf("wd%d: recal", du->dk_lunit);
972 1.37 mycroft printf(": status %b error %b\n", stat,
973 1.37 mycroft WDCS_BITS, inb(wdc+wd_error), WDERR_BITS);
974 1.1 cgd }
975 1.21 deraadt if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
976 1.1 cgd goto tryagainrecal;
977 1.1 cgd bp->b_error = ENXIO; /* XXX needs translation */
978 1.1 cgd goto badopen;
979 1.1 cgd }
980 1.1 cgd
981 1.1 cgd /* some controllers require this ... */
982 1.55 mycroft wdsetctlr(du);
983 1.3 deraadt
984 1.3 deraadt wdtab[ctrlr].b_errcnt = 0;
985 1.1 cgd du->dk_state = OPEN;
986 1.1 cgd /*
987 1.1 cgd * The rest of the initialization can be done
988 1.1 cgd * by normal means.
989 1.1 cgd */
990 1.37 mycroft return 1;
991 1.55 mycroft
992 1.1 cgd default:
993 1.1 cgd panic("wdcontrol");
994 1.1 cgd }
995 1.1 cgd /* NOTREACHED */
996 1.1 cgd
997 1.1 cgd badopen:
998 1.1 cgd if ((du->dk_flags & DKFL_QUIET) == 0)
999 1.44 mycroft printf(": status %b error %b\n", stat, WDCS_BITS,
1000 1.44 mycroft inb(wdc+wd_error), WDERR_BITS);
1001 1.1 cgd bp->b_flags |= B_ERROR;
1002 1.37 mycroft return 1;
1003 1.1 cgd }
1004 1.1 cgd
1005 1.1 cgd /*
1006 1.1 cgd * send a command and wait uninterruptibly until controller is finished.
1007 1.1 cgd * return -1 if controller busy for too long, otherwise
1008 1.1 cgd * return status. intended for brief controller commands at critical points.
1009 1.1 cgd * assumes interrupts are blocked.
1010 1.1 cgd */
1011 1.1 cgd static int
1012 1.55 mycroft wdcommand(du, cmd)
1013 1.55 mycroft struct disk *du;
1014 1.55 mycroft int cmd;
1015 1.3 deraadt {
1016 1.54 mycroft int stat, wdc;
1017 1.3 deraadt
1018 1.1 cgd wdc = du->dk_port;
1019 1.19 deraadt
1020 1.21 deraadt /* controller ready for command? */
1021 1.51 mycroft if (wait_for_unbusy(du) < 0)
1022 1.40 mycroft return -1;
1023 1.3 deraadt
1024 1.1 cgd /* send command, await results */
1025 1.1 cgd outb(wdc+wd_command, cmd);
1026 1.40 mycroft
1027 1.51 mycroft switch (cmd) {
1028 1.51 mycroft default:
1029 1.51 mycroft #if 0
1030 1.51 mycroft case WDCC_FORMAT:
1031 1.51 mycroft case WDCC_RESTORE | WD_STEP:
1032 1.51 mycroft #endif
1033 1.51 mycroft return wait_for_unbusy(du);
1034 1.52 mycroft case WDCC_READ:
1035 1.52 mycroft case WDCC_WRITE:
1036 1.52 mycroft return 0;
1037 1.54 mycroft case WDCC_IDC:
1038 1.54 mycroft case WDCC_DIAGNOSE:
1039 1.54 mycroft return wdc_wait(du, WDCS_READY);
1040 1.51 mycroft case WDCC_READP:
1041 1.51 mycroft return wait_for_drq(du);
1042 1.51 mycroft }
1043 1.1 cgd }
1044 1.1 cgd
1045 1.1 cgd /*
1046 1.1 cgd * issue IDC to drive to tell it just what geometry it is to be.
1047 1.1 cgd */
1048 1.1 cgd static int
1049 1.55 mycroft wdsetctlr(du)
1050 1.55 mycroft struct disk *du;
1051 1.3 deraadt {
1052 1.54 mycroft int stat, s, wdc;
1053 1.3 deraadt
1054 1.44 mycroft #ifdef WDDEBUG
1055 1.3 deraadt printf("wd(%d,%d) C%dH%dS%d\n", du->dk_ctrlr, du->dk_unit,
1056 1.37 mycroft du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks, du->dk_dd.d_nsectors);
1057 1.44 mycroft #endif
1058 1.3 deraadt
1059 1.21 deraadt wdc = du->dk_port;
1060 1.21 deraadt
1061 1.54 mycroft s = splbio();
1062 1.57 mycroft if (wait_for_unbusy(du) < 0) {
1063 1.57 mycroft lose:
1064 1.57 mycroft splx(s);
1065 1.51 mycroft return -1;
1066 1.57 mycroft }
1067 1.37 mycroft outb(wdc+wd_cyl_lo, du->dk_dd.d_ncylinders); /* TIH: was ...ders+1 */
1068 1.37 mycroft outb(wdc+wd_cyl_hi, du->dk_dd.d_ncylinders>>8); /* TIH: was ...ders+1 */
1069 1.46 mycroft outb(wdc+wd_sdh,
1070 1.46 mycroft WDSD_IBM | (du->dk_unit << 4) + du->dk_dd.d_ntracks - 1);
1071 1.1 cgd outb(wdc+wd_seccnt, du->dk_dd.d_nsectors);
1072 1.51 mycroft if (wait_for_ready(du) < 0)
1073 1.57 mycroft goto lose;
1074 1.1 cgd stat = wdcommand(du, WDCC_IDC);
1075 1.51 mycroft if (stat < 0 || stat & WDCS_ERR)
1076 1.46 mycroft printf("wdsetctlr: stat %b error %b\n", stat, WDCS_BITS,
1077 1.37 mycroft inb(wdc+wd_error), WDERR_BITS);
1078 1.54 mycroft splx(s);
1079 1.37 mycroft return stat;
1080 1.1 cgd }
1081 1.1 cgd
1082 1.1 cgd /*
1083 1.1 cgd * issue READP to drive to ask it what it is.
1084 1.1 cgd */
1085 1.1 cgd static int
1086 1.55 mycroft wdgetctlr(du)
1087 1.55 mycroft struct disk *du;
1088 1.3 deraadt {
1089 1.54 mycroft int stat, s, i, wdc;
1090 1.1 cgd char tb[DEV_BSIZE];
1091 1.1 cgd struct wdparams *wp;
1092 1.3 deraadt
1093 1.54 mycroft s = splbio(); /* not called from intr level ... */
1094 1.1 cgd wdc = du->dk_port;
1095 1.51 mycroft if (wait_for_unbusy(du) < 0) {
1096 1.55 mycroft lose:
1097 1.54 mycroft splx(s);
1098 1.40 mycroft return -1;
1099 1.26 mycroft }
1100 1.55 mycroft outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4));
1101 1.55 mycroft if (wait_for_ready(du) < 0)
1102 1.55 mycroft goto lose;
1103 1.1 cgd stat = wdcommand(du, WDCC_READP);
1104 1.55 mycroft if (stat < 0)
1105 1.55 mycroft goto lose;
1106 1.8 deraadt
1107 1.37 mycroft if ((stat & WDCS_ERR) == 0) {
1108 1.13 deraadt /* obtain parameters */
1109 1.13 deraadt wp = &du->dk_params;
1110 1.13 deraadt insw(wdc+wd_data, tb, sizeof(tb)/sizeof(short));
1111 1.13 deraadt bcopy(tb, wp, sizeof(struct wdparams));
1112 1.13 deraadt
1113 1.13 deraadt /* shuffle string byte order */
1114 1.37 mycroft for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
1115 1.13 deraadt u_short *p;
1116 1.37 mycroft p = (u_short *)(wp->wdp_model + i);
1117 1.13 deraadt *p = ntohs(*p);
1118 1.13 deraadt }
1119 1.13 deraadt
1120 1.37 mycroft strncpy(du->dk_dd.d_typename, "ESDI/IDE",
1121 1.37 mycroft sizeof du->dk_dd.d_typename);
1122 1.13 deraadt du->dk_dd.d_type = DTYPE_ESDI;
1123 1.13 deraadt bcopy(wp->wdp_model+20, du->dk_dd.d_packname, 14-1);
1124 1.13 deraadt
1125 1.13 deraadt /* update disklabel given drive information */
1126 1.44 mycroft du->dk_dd.d_ncylinders =
1127 1.44 mycroft wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
1128 1.13 deraadt du->dk_dd.d_ntracks = wp->wdp_heads;
1129 1.13 deraadt du->dk_dd.d_nsectors = wp->wdp_sectors;
1130 1.44 mycroft du->dk_dd.d_secpercyl =
1131 1.44 mycroft du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
1132 1.44 mycroft du->dk_dd.d_partitions[1].p_size =
1133 1.44 mycroft du->dk_dd.d_secpercyl * wp->wdp_sectors;
1134 1.13 deraadt du->dk_dd.d_partitions[1].p_offset = 0;
1135 1.13 deraadt } else {
1136 1.13 deraadt /*
1137 1.13 deraadt * If WDCC_READP fails then we might have an old drive
1138 1.13 deraadt * so we try a seek to 0; if that passes then the
1139 1.13 deraadt * drive is there but it's OLD AND KRUSTY.
1140 1.13 deraadt */
1141 1.8 deraadt stat = wdcommand(du, WDCC_RESTORE | WD_STEP);
1142 1.57 mycroft if (stat < 0 || stat & WDCS_ERR)
1143 1.57 mycroft goto lose;
1144 1.3 deraadt
1145 1.37 mycroft strncpy(du->dk_dd.d_typename, "ST506",
1146 1.37 mycroft sizeof du->dk_dd.d_typename);
1147 1.10 deraadt strncpy(du->dk_params.wdp_model, "Unknown Type",
1148 1.37 mycroft sizeof du->dk_params.wdp_model);
1149 1.8 deraadt du->dk_dd.d_type = DTYPE_ST506;
1150 1.8 deraadt }
1151 1.13 deraadt
1152 1.13 deraadt #if 0
1153 1.37 mycroft printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1154 1.37 mycroft wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
1155 1.37 mycroft wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
1156 1.13 deraadt #endif
1157 1.13 deraadt
1158 1.1 cgd /* better ... */
1159 1.1 cgd du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
1160 1.3 deraadt
1161 1.1 cgd /* XXX sometimes possibly needed */
1162 1.1 cgd (void) inb(wdc+wd_status);
1163 1.54 mycroft splx(s);
1164 1.37 mycroft return 0;
1165 1.1 cgd }
1166 1.1 cgd
1167 1.1 cgd int
1168 1.55 mycroft wdclose(dev, flag, fmt)
1169 1.55 mycroft dev_t dev;
1170 1.55 mycroft int flag;
1171 1.55 mycroft int fmt;
1172 1.1 cgd {
1173 1.54 mycroft struct disk *du;
1174 1.51 mycroft int part = WDPART(dev), mask = 1 << part;
1175 1.3 deraadt
1176 1.51 mycroft du = wddrives[WDUNIT(dev)];
1177 1.3 deraadt
1178 1.1 cgd /* insure only one open at a time */
1179 1.3 deraadt du->dk_openpart &= ~mask;
1180 1.3 deraadt switch (fmt) {
1181 1.3 deraadt case S_IFCHR:
1182 1.3 deraadt du->dk_copenpart &= ~mask;
1183 1.3 deraadt break;
1184 1.3 deraadt case S_IFBLK:
1185 1.3 deraadt du->dk_bopenpart &= ~mask;
1186 1.3 deraadt break;
1187 1.3 deraadt }
1188 1.37 mycroft return 0;
1189 1.1 cgd }
1190 1.1 cgd
1191 1.1 cgd int
1192 1.55 mycroft wdioctl(dev, cmd, addr, flag, p)
1193 1.55 mycroft dev_t dev;
1194 1.55 mycroft int cmd;
1195 1.55 mycroft caddr_t addr;
1196 1.55 mycroft int flag;
1197 1.55 mycroft struct proc *p;
1198 1.1 cgd {
1199 1.51 mycroft int lunit = WDUNIT(dev);
1200 1.54 mycroft struct disk *du = wddrives[lunit];
1201 1.54 mycroft int error;
1202 1.3 deraadt
1203 1.1 cgd switch (cmd) {
1204 1.1 cgd case DIOCSBAD:
1205 1.3 deraadt if ((flag & FWRITE) == 0)
1206 1.54 mycroft return EBADF;
1207 1.54 mycroft du->dk_cpd.bad = *(struct dkbad *)addr;
1208 1.54 mycroft bad144intern(du);
1209 1.54 mycroft return 0;
1210 1.1 cgd
1211 1.1 cgd case DIOCGDINFO:
1212 1.1 cgd *(struct disklabel *)addr = du->dk_dd;
1213 1.54 mycroft return 0;
1214 1.3 deraadt
1215 1.3 deraadt case DIOCGPART:
1216 1.3 deraadt ((struct partinfo *)addr)->disklab = &du->dk_dd;
1217 1.3 deraadt ((struct partinfo *)addr)->part =
1218 1.51 mycroft &du->dk_dd.d_partitions[WDPART(dev)];
1219 1.54 mycroft return 0;
1220 1.3 deraadt
1221 1.3 deraadt case DIOCSDINFO:
1222 1.3 deraadt if ((flag & FWRITE) == 0)
1223 1.54 mycroft return EBADF;
1224 1.54 mycroft error = setdisklabel(&du->dk_dd,
1225 1.54 mycroft (struct disklabel *)addr,
1226 1.54 mycroft /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart : */0,
1227 1.54 mycroft &du->dk_cpd);
1228 1.3 deraadt if (error == 0) {
1229 1.1 cgd du->dk_flags |= DKFL_BSDLABEL;
1230 1.55 mycroft wdsetctlr(du);
1231 1.1 cgd }
1232 1.54 mycroft return error;
1233 1.3 deraadt
1234 1.44 mycroft case DIOCWLABEL:
1235 1.1 cgd du->dk_flags &= ~DKFL_WRITEPROT;
1236 1.3 deraadt if ((flag & FWRITE) == 0)
1237 1.54 mycroft return EBADF;
1238 1.54 mycroft du->dk_wlabel = *(int *)addr;
1239 1.54 mycroft return 0;
1240 1.3 deraadt
1241 1.44 mycroft case DIOCWDINFO:
1242 1.1 cgd du->dk_flags &= ~DKFL_WRITEPROT;
1243 1.3 deraadt if ((flag & FWRITE) == 0)
1244 1.54 mycroft return EBADF;
1245 1.54 mycroft error = setdisklabel(&du->dk_dd,
1246 1.37 mycroft (struct disklabel *)addr,
1247 1.44 mycroft /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart :*/0,
1248 1.54 mycroft &du->dk_cpd);
1249 1.54 mycroft if (error == 0) {
1250 1.3 deraadt int wlab;
1251 1.3 deraadt
1252 1.1 cgd du->dk_flags |= DKFL_BSDLABEL;
1253 1.55 mycroft wdsetctlr(du);
1254 1.3 deraadt
1255 1.3 deraadt /* simulate opening partition 0 so write succeeds */
1256 1.13 deraadt du->dk_openpart |= (1 << 0); /* XXX */
1257 1.3 deraadt wlab = du->dk_wlabel;
1258 1.3 deraadt du->dk_wlabel = 1;
1259 1.37 mycroft error = writedisklabel(dev, wdstrategy, &du->dk_dd, &du->dk_cpd);
1260 1.3 deraadt du->dk_openpart = du->dk_copenpart | du->dk_bopenpart;
1261 1.3 deraadt du->dk_wlabel = wlab;
1262 1.3 deraadt }
1263 1.54 mycroft return error;
1264 1.3 deraadt
1265 1.1 cgd #ifdef notyet
1266 1.1 cgd case DIOCGDINFOP:
1267 1.37 mycroft *(struct disklabel **)addr = &du->dk_dd;
1268 1.54 mycroft return 0;
1269 1.3 deraadt
1270 1.1 cgd case DIOCWFORMAT:
1271 1.1 cgd if ((flag & FWRITE) == 0)
1272 1.54 mycroft return EBADF;
1273 1.54 mycroft {
1274 1.54 mycroft register struct format_op *fop;
1275 1.54 mycroft struct iovec aiov;
1276 1.54 mycroft struct uio auio;
1277 1.3 deraadt
1278 1.54 mycroft fop = (struct format_op *)addr;
1279 1.54 mycroft aiov.iov_base = fop->df_buf;
1280 1.54 mycroft aiov.iov_len = fop->df_count;
1281 1.54 mycroft auio.uio_iov = &aiov;
1282 1.54 mycroft auio.uio_iovcnt = 1;
1283 1.54 mycroft auio.uio_resid = fop->df_count;
1284 1.54 mycroft auio.uio_segflg = 0;
1285 1.54 mycroft auio.uio_offset =
1286 1.54 mycroft fop->df_startblk * du->dk_dd.d_secsize;
1287 1.54 mycroft error = physio(wdformat, &rwdbuf[lunit], dev, B_WRITE,
1288 1.54 mycroft minphys, &auio);
1289 1.54 mycroft fop->df_count -= auio.uio_resid;
1290 1.54 mycroft fop->df_reg[0] = du->dk_status;
1291 1.54 mycroft fop->df_reg[1] = du->dk_error;
1292 1.54 mycroft return error;
1293 1.54 mycroft }
1294 1.1 cgd #endif
1295 1.3 deraadt
1296 1.1 cgd default:
1297 1.54 mycroft return ENOTTY;
1298 1.1 cgd }
1299 1.54 mycroft
1300 1.54 mycroft #ifdef DIAGNOSTIC
1301 1.54 mycroft panic("wdioctl: impossible");
1302 1.54 mycroft #endif
1303 1.1 cgd }
1304 1.1 cgd
1305 1.44 mycroft #ifdef B_FORMAT
1306 1.1 cgd int
1307 1.1 cgd wdformat(struct buf *bp)
1308 1.1 cgd {
1309 1.44 mycroft
1310 1.1 cgd bp->b_flags |= B_FORMAT;
1311 1.37 mycroft return wdstrategy(bp);
1312 1.1 cgd }
1313 1.1 cgd #endif
1314 1.1 cgd
1315 1.1 cgd int
1316 1.55 mycroft wdsize(dev)
1317 1.55 mycroft dev_t dev;
1318 1.1 cgd {
1319 1.51 mycroft int lunit = WDUNIT(dev), part = WDPART(dev);
1320 1.1 cgd struct disk *du;
1321 1.3 deraadt
1322 1.3 deraadt if (lunit >= NWD)
1323 1.37 mycroft return -1;
1324 1.46 mycroft du = wddrives[lunit];
1325 1.46 mycroft if (du == 0)
1326 1.37 mycroft return -1;
1327 1.3 deraadt
1328 1.3 deraadt if (du->dk_state < OPEN || (du->dk_flags & DKFL_BSDLABEL) == 0) {
1329 1.3 deraadt int val;
1330 1.44 mycroft val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
1331 1.44 mycroft S_IFBLK, 0);
1332 1.55 mycroft /* XXX Clear the open flag? */
1333 1.3 deraadt if (val != 0)
1334 1.37 mycroft return -1;
1335 1.3 deraadt }
1336 1.3 deraadt
1337 1.37 mycroft if ((du->dk_flags & (DKFL_WRITEPROT | DKFL_BSDLABEL)) != DKFL_BSDLABEL)
1338 1.37 mycroft return -1;
1339 1.46 mycroft
1340 1.46 mycroft return (int)du->dk_dd.d_partitions[part].p_size;
1341 1.1 cgd }
1342 1.1 cgd
1343 1.3 deraadt /* dump core after a system crash */
1344 1.1 cgd int
1345 1.55 mycroft wddump(dev)
1346 1.55 mycroft dev_t dev;
1347 1.1 cgd {
1348 1.1 cgd register struct disk *du; /* disk unit to do the IO */
1349 1.46 mycroft long num; /* number of sectors to write */
1350 1.46 mycroft int ctrlr, lunit, part, wdc;
1351 1.46 mycroft long blkoff, blknum;
1352 1.46 mycroft long cylin, head, sector, stat;
1353 1.46 mycroft long secpertrk, secpercyl, nblocks, i;
1354 1.1 cgd char *addr;
1355 1.46 mycroft static wddoingadump = 0;
1356 1.1 cgd extern caddr_t CADDR1;
1357 1.30 ws extern struct pte *CMAP1;
1358 1.30 ws
1359 1.37 mycroft addr = (char *)0; /* starting address */
1360 1.3 deraadt
1361 1.16 deraadt #if DO_NOT_KNOW_HOW
1362 1.16 deraadt /* toss any characters present prior to dump, ie. non-blocking getc */
1363 1.16 deraadt while (cngetc())
1364 1.1 cgd ;
1365 1.16 deraadt #endif
1366 1.3 deraadt
1367 1.1 cgd /* size of memory to dump */
1368 1.51 mycroft lunit = WDUNIT(dev);
1369 1.51 mycroft part = WDPART(dev); /* file system */
1370 1.1 cgd /* check for acceptable drive number */
1371 1.3 deraadt if (lunit >= NWD)
1372 1.37 mycroft return ENXIO;
1373 1.3 deraadt du = wddrives[lunit];
1374 1.1 cgd /* was it ever initialized ? */
1375 1.46 mycroft if (du == 0 || du->dk_state < OPEN || du->dk_flags & DKFL_WRITEPROT)
1376 1.37 mycroft return ENXIO;
1377 1.46 mycroft
1378 1.1 cgd wdc = du->dk_port;
1379 1.3 deraadt ctrlr = du->dk_ctrlr;
1380 1.3 deraadt
1381 1.1 cgd /* Convert to disk sectors */
1382 1.28 mycroft num = ctob(physmem) / du->dk_dd.d_secsize;
1383 1.3 deraadt
1384 1.1 cgd /* check if controller active */
1385 1.3 deraadt /*if (wdtab[ctrlr].b_active)
1386 1.46 mycroft return EFAULT;*/
1387 1.3 deraadt if (wddoingadump)
1388 1.37 mycroft return EFAULT;
1389 1.3 deraadt
1390 1.1 cgd secpertrk = du->dk_dd.d_nsectors;
1391 1.1 cgd secpercyl = du->dk_dd.d_secpercyl;
1392 1.1 cgd nblocks = du->dk_dd.d_partitions[part].p_size;
1393 1.1 cgd blkoff = du->dk_dd.d_partitions[part].p_offset;
1394 1.3 deraadt
1395 1.51 mycroft /*printf("xunit %x, nblocks %d, dumplo %d num %d\n", part, nblocks,
1396 1.51 mycroft dumplo, num);*/
1397 1.1 cgd /* check transfer bounds against partition size */
1398 1.46 mycroft if (dumplo < 0 || dumplo + num > nblocks)
1399 1.37 mycroft return EINVAL;
1400 1.3 deraadt
1401 1.3 deraadt /* mark controller active for if we panic during the dump */
1402 1.46 mycroft /*wdtab[ctrlr].b_active = 1;*/
1403 1.3 deraadt wddoingadump = 1;
1404 1.3 deraadt i = 200000000;
1405 1.51 mycroft if (wait_for_unbusy(du) < 0)
1406 1.51 mycroft return EIO;
1407 1.3 deraadt outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4));
1408 1.51 mycroft if (wait_for_ready(du) < 0)
1409 1.51 mycroft return EIO;
1410 1.51 mycroft if (wdcommand(du, WDCC_RESTORE | WD_STEP) < 0)
1411 1.51 mycroft return EIO;
1412 1.3 deraadt
1413 1.1 cgd /* some compaq controllers require this ... */
1414 1.55 mycroft wdsetctlr(du);
1415 1.3 deraadt
1416 1.1 cgd blknum = dumplo + blkoff;
1417 1.1 cgd while (num > 0) {
1418 1.1 cgd /* compute disk address */
1419 1.1 cgd cylin = blknum / secpercyl;
1420 1.1 cgd head = (blknum % secpercyl) / secpertrk;
1421 1.1 cgd sector = blknum % secpertrk;
1422 1.3 deraadt
1423 1.3 deraadt if (du->dk_flags & DKFL_BADSECT) {
1424 1.3 deraadt long newblk;
1425 1.3 deraadt int i;
1426 1.3 deraadt
1427 1.3 deraadt for (i = 0; du->dk_badsect[i] != -1; i++) {
1428 1.3 deraadt if (blknum < du->dk_badsect[i]) {
1429 1.45 mycroft /* sorted list, passed our block by */
1430 1.44 mycroft break;
1431 1.3 deraadt } else if (blknum == du->dk_badsect[i]) {
1432 1.3 deraadt newblk = du->dk_dd.d_secperunit -
1433 1.3 deraadt du->dk_dd.d_nsectors - i - 1;
1434 1.3 deraadt cylin = newblk / secpercyl;
1435 1.3 deraadt head = (newblk % secpercyl) / secpertrk;
1436 1.3 deraadt sector = newblk % secpertrk;
1437 1.44 mycroft /* found and replaced; done */
1438 1.3 deraadt break;
1439 1.3 deraadt }
1440 1.1 cgd }
1441 1.3 deraadt }
1442 1.1 cgd sector++; /* origin 1 */
1443 1.3 deraadt
1444 1.51 mycroft if (wait_for_unbusy(du) < 0)
1445 1.51 mycroft return EIO;
1446 1.1 cgd /* select drive. */
1447 1.37 mycroft outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4) | (head & 0xf));
1448 1.51 mycroft if (wait_for_ready(du) < 0)
1449 1.51 mycroft return EIO;
1450 1.46 mycroft
1451 1.1 cgd /* transfer some blocks */
1452 1.1 cgd outb(wdc+wd_sector, sector);
1453 1.37 mycroft outb(wdc+wd_seccnt, 1);
1454 1.1 cgd outb(wdc+wd_cyl_lo, cylin);
1455 1.1 cgd outb(wdc+wd_cyl_hi, cylin >> 8);
1456 1.1 cgd #ifdef notdef
1457 1.1 cgd /* lets just talk about this first...*/
1458 1.51 mycroft printf("sdh 0%o sector %d cyl %d addr 0x%x", inb(wdc+wd_sdh),
1459 1.44 mycroft inb(wdc+wd_sector),
1460 1.44 mycroft (inb(wdc+wd_cyl_hi) << 8) + inb(wdc+wd_cyl_lo), addr);
1461 1.1 cgd #endif
1462 1.51 mycroft stat = wdcommand(du, WDCC_WRITE);
1463 1.51 mycroft if (stat < 0 || stat & WDCS_ERR)
1464 1.51 mycroft return EIO;
1465 1.3 deraadt
1466 1.46 mycroft #ifdef notdef /* cannot use this since this address was mapped differently */
1467 1.46 mycroft pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
1468 1.46 mycroft #else
1469 1.46 mycroft *(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
1470 1.46 mycroft tlbflush();
1471 1.46 mycroft #endif
1472 1.46 mycroft
1473 1.46 mycroft outsw(wdc+wd_data, CADDR1 + ((int)addr & PGOFSET),
1474 1.51 mycroft DEV_BSIZE / sizeof(short));
1475 1.3 deraadt
1476 1.13 deraadt /* Check data request (should be done). */
1477 1.51 mycroft if (inb(wdc+wd_status) & (WDCS_ERR | WDCS_DRQ))
1478 1.37 mycroft return EIO;
1479 1.3 deraadt
1480 1.51 mycroft if (wait_for_unbusy(du) < 0)
1481 1.51 mycroft return EIO;
1482 1.3 deraadt
1483 1.1 cgd /* error check the xfer */
1484 1.3 deraadt if (inb(wdc+wd_status) & WDCS_ERR)
1485 1.37 mycroft return EIO;
1486 1.3 deraadt
1487 1.46 mycroft if ((unsigned)addr % 1048576 == 0)
1488 1.46 mycroft printf("%d ", num / (1048576 / DEV_BSIZE));
1489 1.1 cgd
1490 1.1 cgd /* update block count */
1491 1.1 cgd num--;
1492 1.3 deraadt blknum++;
1493 1.46 mycroft (int)addr += DEV_BSIZE;
1494 1.3 deraadt
1495 1.16 deraadt #if DO_NOT_KNOW_HOW
1496 1.16 deraadt /* operator aborting dump? non-blocking getc() */
1497 1.16 deraadt if (cngetc())
1498 1.37 mycroft return EINTR;
1499 1.16 deraadt #endif
1500 1.1 cgd }
1501 1.37 mycroft return 0;
1502 1.1 cgd }
1503 1.3 deraadt
1504 1.3 deraadt /*
1505 1.3 deraadt * Internalize the bad sector table.
1506 1.3 deraadt */
1507 1.3 deraadt void
1508 1.55 mycroft bad144intern(du)
1509 1.55 mycroft struct disk *du;
1510 1.3 deraadt {
1511 1.3 deraadt int i;
1512 1.55 mycroft
1513 1.55 mycroft if ((du->dk_flags & DKFL_BADSECT) == 0)
1514 1.55 mycroft return;
1515 1.55 mycroft
1516 1.55 mycroft for (i = 0; i < 127; i++)
1517 1.55 mycroft du->dk_badsect[i] = -1;
1518 1.55 mycroft for (i = 0; i < 126; i++) {
1519 1.55 mycroft if (du->dk_cpd.bad.bt_bad[i].bt_cyl == 0xffff)
1520 1.55 mycroft break;
1521 1.55 mycroft du->dk_badsect[i] =
1522 1.55 mycroft du->dk_cpd.bad.bt_bad[i].bt_cyl * du->dk_dd.d_secpercyl +
1523 1.55 mycroft (du->dk_cpd.bad.bt_bad[i].bt_trksec >> 8) *
1524 1.55 mycroft du->dk_dd.d_nsectors +
1525 1.55 mycroft (du->dk_cpd.bad.bt_bad[i].bt_trksec & 0x00ff);
1526 1.3 deraadt }
1527 1.3 deraadt }
1528 1.3 deraadt
1529 1.27 cgd static int
1530 1.55 mycroft wdreset(du, err)
1531 1.55 mycroft struct disk *du;
1532 1.55 mycroft int err;
1533 1.21 deraadt {
1534 1.51 mycroft int wdc = du->dk_port;
1535 1.40 mycroft int stat;
1536 1.21 deraadt
1537 1.37 mycroft if (err)
1538 1.51 mycroft printf("wdc%d: busy too long, resetting\n", du->dk_ctrlr);
1539 1.21 deraadt
1540 1.21 deraadt /* reset the device */
1541 1.37 mycroft outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1542 1.21 deraadt DELAY(1000);
1543 1.55 mycroft outb(wdc+wd_ctlr, WDCTL_IDS);
1544 1.55 mycroft DELAY(1000);
1545 1.55 mycroft (void) inb(wdc+wd_error);
1546 1.21 deraadt outb(wdc+wd_ctlr, WDCTL_4BIT);
1547 1.21 deraadt
1548 1.51 mycroft if (wait_for_unbusy(du) < 0)
1549 1.51 mycroft printf("wdc%d: failed to reset controller\n", du->dk_ctrlr);
1550 1.40 mycroft }
1551 1.40 mycroft
1552 1.40 mycroft int
1553 1.51 mycroft wdc_wait(du, mask)
1554 1.51 mycroft struct disk *du;
1555 1.49 mycroft int mask;
1556 1.40 mycroft {
1557 1.51 mycroft int wdc = du->dk_port;
1558 1.40 mycroft int timeout = 0;
1559 1.40 mycroft int stat;
1560 1.40 mycroft
1561 1.40 mycroft for (;;) {
1562 1.51 mycroft stat = inb(wdc+wd_altsts);
1563 1.54 mycroft if ((stat & WDCS_BUSY) == 0 && (stat & mask) == mask)
1564 1.54 mycroft break;
1565 1.40 mycroft if (++timeout > WDCNDELAY)
1566 1.40 mycroft return -1;
1567 1.55 mycroft DELAY(WDCDELAY);
1568 1.21 deraadt }
1569 1.23 deraadt #ifdef WDCNDELAY_DEBUG
1570 1.37 mycroft if (timeout > WDCNDELAY_DEBUG)
1571 1.51 mycroft printf("wdc%d: timeout took %dus\n", du->dk_ctrlr,
1572 1.51 mycroft WDCDELAY * timeout);
1573 1.23 deraadt #endif
1574 1.40 mycroft return stat;
1575 1.27 cgd }
1576 1.27 cgd
1577 1.27 cgd static int
1578 1.55 mycroft wdtimeout(arg)
1579 1.55 mycroft caddr_t arg;
1580 1.27 cgd {
1581 1.55 mycroft int s = splbio();
1582 1.55 mycroft struct disk *du = (void *)arg;
1583 1.27 cgd
1584 1.55 mycroft if (du->dk_timeout && --du->dk_timeout == 0) {
1585 1.37 mycroft int wdc = du->dk_port;
1586 1.41 mycroft
1587 1.37 mycroft printf("wd%d: lost interrupt - status %x, error %x\n",
1588 1.55 mycroft du->dk_lunit, inb(wdc+wd_status), inb(wdc+wd_error));
1589 1.55 mycroft wdreset(du, 0);
1590 1.37 mycroft du->dk_skip = 0;
1591 1.37 mycroft du->dk_flags |= DKFL_SINGLE;
1592 1.37 mycroft wdstart(du->dk_ctrlr); /* start controller */
1593 1.27 cgd }
1594 1.55 mycroft timeout((timeout_t)wdtimeout, arg, hz);
1595 1.55 mycroft splx(s);
1596 1.37 mycroft return 0;
1597 1.21 deraadt }
1598 1.43 mycroft
1599 1.43 mycroft #endif /* NWDC > 0 */
1600