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