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