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