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