wd.c revision 1.165 1 /* $NetBSD: wd.c,v 1.165 1997/10/10 01:17:33 explorer Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
5 *
6 * DMA and multi-sector PIO handling are derived from code contributed by
7 * Onno van der Linden.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Charles M. Hannum.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/conf.h>
39 #include <sys/file.h>
40 #include <sys/stat.h>
41 #include <sys/ioctl.h>
42 #include <sys/buf.h>
43 #include <sys/uio.h>
44 #include <sys/malloc.h>
45 #include <sys/device.h>
46 #include <sys/disklabel.h>
47 #include <sys/disk.h>
48 #include <sys/syslog.h>
49 #include <sys/proc.h>
50 #include <sys/rnd.h>
51
52 #include <vm/vm.h>
53
54 #include <machine/cpu.h>
55 #include <machine/intr.h>
56 #include <machine/pio.h>
57
58 #include <dev/isa/isavar.h>
59 #include <dev/isa/wdreg.h>
60 #include <dev/isa/wdlink.h>
61 #include "locators.h"
62
63 #define WAITTIME (4 * hz) /* time to wait for a completion */
64
65 #define WDIORETRIES 5 /* number of retries before giving up */
66
67 #define WDUNIT(dev) DISKUNIT(dev)
68 #define WDPART(dev) DISKPART(dev)
69 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
70
71 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
72
73 #ifdef WDDEBUG
74 #define WDDEBUG_PRINT(args) printf args
75 #else
76 #define WDDEBUG_PRINT(args)
77 #endif
78
79 struct wd_softc {
80 struct device sc_dev;
81 struct disk sc_dk;
82 struct wd_link *d_link;
83 struct buf sc_q;
84 rndsource_element_t rnd_source;
85 };
86
87 int wdprobe __P((struct device *, void *, void *));
88 void wdattach __P((struct device *, struct device *, void *));
89 int wdprint __P((void *, char *));
90
91 struct cfattach wd_ca = {
92 sizeof(struct wd_softc), wdprobe, wdattach
93 };
94
95 struct cfdriver wd_cd = {
96 NULL, "wd", DV_DISK
97 };
98
99 void wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
100 void wdgetdisklabel __P((struct wd_softc *));
101 int wd_get_parms __P((struct wd_softc *));
102 void wdstrategy __P((struct buf *));
103 void wdstart __P((void *));
104
105 struct dkdriver wddkdriver = { wdstrategy };
106
107 /* XXX: these should go elsewhere */
108 cdev_decl(wd);
109 bdev_decl(wd);
110
111 void wdfinish __P((struct wd_softc *, struct buf *));
112 int wdsetctlr __P((struct wd_link *));
113 static void bad144intern __P((struct wd_softc *));
114 int wdlock __P((struct wd_link *));
115 void wdunlock __P((struct wd_link *));
116
117 int
118 wdprobe(parent, match, aux)
119 struct device *parent;
120 void *match, *aux;
121 {
122 struct cfdata *cf = match;
123 struct wd_link *d_link = aux;
124 int drive;
125
126 if (d_link == NULL)
127 return 0;
128 if (d_link->type != ATA)
129 return 0;
130
131 drive = d_link->drive;
132 if (cf->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
133 cf->cf_loc[ATACF_DRIVE] != drive)
134 return 0;
135
136 return 1;
137 }
138
139 void
140 wdattach(parent, self, aux)
141 struct device *parent, *self;
142 void *aux;
143 {
144 struct wd_softc *wd = (void *)self;
145 struct wd_link *d_link= aux;
146 int i, blank;
147 char buf[41], c, *p, *q;
148
149 wd->d_link = d_link;
150 d_link->openings = 1;
151 d_link->wd_softc = (caddr_t)wd;
152
153 /*
154 * Initialize and attach the disk structure.
155 */
156 wd->sc_dk.dk_driver = &wddkdriver;
157 wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
158 disk_attach(&wd->sc_dk);
159
160 d_link->sc_lp = wd->sc_dk.dk_label;
161
162 wdc_get_parms((struct wdc_softc *)d_link->wdc_softc, d_link);
163 for (blank = 0, p = d_link->sc_params.wdp_model, q = buf, i = 0;
164 i < sizeof(d_link->sc_params.wdp_model); i++) {
165 c = *p++;
166 if (c == '\0')
167 break;
168 if (c != ' ') {
169 if (blank) {
170 *q++ = ' ';
171 blank = 0;
172 }
173 *q++ = c;
174 } else
175 blank = 1;
176 }
177 *q++ = '\0';
178
179 printf(": <%s>\n%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
180 buf, self->dv_xname,
181 d_link->sc_params.wdp_cylinders *
182 (d_link->sc_params.wdp_heads * d_link->sc_params.wdp_sectors) /
183 (1048576 / DEV_BSIZE),
184 d_link->sc_params.wdp_cylinders,
185 d_link->sc_params.wdp_heads,
186 d_link->sc_params.wdp_sectors,
187 DEV_BSIZE);
188
189 if ((d_link->sc_params.wdp_capabilities & WD_CAP_DMA) != 0 &&
190 d_link->sc_mode == WDM_DMA) {
191 d_link->sc_mode = WDM_DMA;
192 } else if (d_link->sc_params.wdp_maxmulti > 1) {
193 d_link->sc_mode = WDM_PIOMULTI;
194 d_link->sc_multiple = min(d_link->sc_params.wdp_maxmulti, 16);
195 } else {
196 d_link->sc_mode = WDM_PIOSINGLE;
197 d_link->sc_multiple = 1;
198 }
199
200 printf("%s: using", wd->sc_dev.dv_xname);
201 if (d_link->sc_mode == WDM_DMA)
202 printf(" dma transfers,");
203 else
204 printf(" %d-sector %d-bit pio transfers,",
205 d_link->sc_multiple,
206 (d_link->sc_flags & WDF_32BIT) == 0 ? 16 : 32);
207 if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0)
208 printf(" lba addressing\n");
209 else
210 printf(" chs addressing\n");
211
212 rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname, RND_TYPE_DISK);
213 }
214
215 /*
216 * Read/write routine for a buffer. Validates the arguments and schedules the
217 * transfer. Does not wait for the transfer to complete.
218 */
219 void
220 wdstrategy(bp)
221 struct buf *bp;
222 {
223 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
224 struct wd_link *d_link= wd->d_link;
225 int s;
226
227 /* Valid request? */
228 if (bp->b_blkno < 0 ||
229 (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
230 (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
231 bp->b_error = EINVAL;
232 goto bad;
233 }
234
235 /* If device invalidated (e.g. media change, door open), error. */
236 if ((d_link->sc_flags & WDF_LOADED) == 0) {
237 bp->b_error = EIO;
238 goto bad;
239 }
240
241 /* If it's a null transfer, return immediately. */
242 if (bp->b_bcount == 0)
243 goto done;
244
245 /*
246 * Do bounds checking, adjust transfer. if error, process.
247 * If end of partition, just return.
248 */
249 if (WDPART(bp->b_dev) != RAW_PART &&
250 bounds_check_with_label(bp, wd->sc_dk.dk_label,
251 (d_link->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
252 goto done;
253
254 /* Queue transfer on drive, activate drive and controller if idle. */
255 s = splbio();
256 disksort(&wd->sc_q, bp);
257 wdstart(wd);
258 splx(s);
259 return;
260
261 bad:
262 bp->b_flags |= B_ERROR;
263 done:
264 /* Toss transfer; we're done early. */
265 bp->b_resid = bp->b_bcount;
266 biodone(bp);
267 }
268
269 /*
270 * Queue a drive for I/O.
271 */
272 void
273 wdstart(arg)
274 void *arg;
275 {
276 struct wd_softc *wd = arg;
277 struct buf *dp, *bp=0;
278 struct wd_link *d_link = wd->d_link;
279 struct wdc_xfer *xfer;
280 u_long p_offset;
281
282 while (d_link->openings > 0) {
283
284 /* Is there a buf for us ? */
285 dp = &wd->sc_q;
286 if ((bp = dp->b_actf) == NULL) /* yes, an assign */
287 return;
288 dp->b_actf = bp->b_actf;
289
290 /*
291 * Make the command. First lock the device
292 */
293 d_link->openings--;
294 if (WDPART(bp->b_dev) != RAW_PART)
295 p_offset =
296 wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
297 else
298 p_offset = 0;
299
300 xfer = wdc_get_xfer(0);
301 if (xfer == NULL)
302 panic("wdc_xfer");
303
304 xfer->d_link = d_link;
305 xfer->c_bp = bp;
306 xfer->c_p_offset = p_offset;
307 xfer->databuf = bp->b_data;
308 xfer->c_bcount = bp->b_bcount;
309 xfer->c_flags |= bp->b_flags & (B_READ|B_WRITE);
310 xfer->c_blkno = bp->b_blkno;
311
312 /* Instrumentation. */
313 disk_busy(&wd->sc_dk);
314 wdc_exec_xfer((struct wdc_softc *)wd->d_link->wdc_softc,
315 wd->d_link, xfer);
316 }
317 }
318
319 int
320 wdread(dev, uio, flags)
321 dev_t dev;
322 struct uio *uio;
323 int flags;
324 {
325
326 WDDEBUG_PRINT(("wdread\n"));
327 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
328 }
329
330 int
331 wdwrite(dev, uio, flags)
332 dev_t dev;
333 struct uio *uio;
334 int flags;
335 {
336
337 WDDEBUG_PRINT(("wdwrite\n"));
338 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
339 }
340
341 /*
342 * Wait interruptibly for an exclusive lock.
343 *
344 * XXX
345 * Several drivers do this; it should be abstracted and made MP-safe.
346 */
347 int
348 wdlock(d_link)
349 struct wd_link *d_link;
350 {
351 int error;
352 int s;
353
354 WDDEBUG_PRINT(("wdlock\n"));
355
356 s = splbio();
357
358 while ((d_link->sc_flags & WDF_LOCKED) != 0) {
359 d_link->sc_flags |= WDF_WANTED;
360 if ((error = tsleep(d_link, PRIBIO | PCATCH,
361 "wdlck", 0)) != 0) {
362 splx(s);
363 return error;
364 }
365 }
366 d_link->sc_flags |= WDF_LOCKED;
367 splx(s);
368 return 0;
369 }
370
371 /*
372 * Unlock and wake up any waiters.
373 */
374 void
375 wdunlock(d_link)
376 struct wd_link *d_link;
377 {
378
379 WDDEBUG_PRINT(("wdunlock"));
380
381 d_link->sc_flags &= ~WDF_LOCKED;
382 if ((d_link->sc_flags & WDF_WANTED) != 0) {
383 d_link->sc_flags &= ~WDF_WANTED;
384 wakeup(d_link);
385 }
386 }
387
388 int
389 wdopen(dev, flag, fmt, p)
390 dev_t dev;
391 int flag, fmt;
392 struct proc *p;
393 {
394 struct wd_softc *wd;
395 struct wd_link *d_link;
396 int unit, part;
397 int error;
398
399 WDDEBUG_PRINT(("wdopen\n"));
400
401 unit = WDUNIT(dev);
402 if (unit >= wd_cd.cd_ndevs)
403 return ENXIO;
404 wd = wd_cd.cd_devs[unit];
405 if (wd == NULL)
406 return ENXIO;
407
408 d_link = wd->d_link;
409 if ((error = wdlock(d_link)) != 0)
410 return error;
411
412 if (wd->sc_dk.dk_openmask != 0) {
413 /*
414 * If any partition is open, but the disk has been invalidated,
415 * disallow further opens.
416 */
417 if ((d_link->sc_flags & WDF_LOADED) == 0) {
418 error = EIO;
419 goto bad3;
420 }
421 } else {
422 if ((d_link->sc_flags & WDF_LOADED) == 0) {
423 d_link->sc_flags |= WDF_LOADED;
424
425 /* Load the physical device parameters. */
426 if (wdc_get_parms((struct wdc_softc *)d_link->wdc_softc,
427 d_link) != 0) {
428 error = ENXIO;
429 goto bad2;
430 }
431
432 /* Load the partition info if not already loaded. */
433 wdgetdisklabel(wd);
434 }
435 }
436
437 part = WDPART(dev);
438
439 /* Check that the partition exists. */
440 if (part != RAW_PART &&
441 (part >= wd->sc_dk.dk_label->d_npartitions ||
442 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
443 error = ENXIO;
444 goto bad;
445 }
446
447 /* Insure only one open at a time. */
448 switch (fmt) {
449 case S_IFCHR:
450 wd->sc_dk.dk_copenmask |= (1 << part);
451 break;
452 case S_IFBLK:
453 wd->sc_dk.dk_bopenmask |= (1 << part);
454 break;
455 }
456 wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
457
458 wdunlock(d_link);
459 return 0;
460
461 bad2:
462 d_link->sc_flags &= ~WDF_LOADED;
463
464 bad:
465 if (wd->sc_dk.dk_openmask == 0) {
466 }
467
468 bad3:
469 wdunlock(d_link);
470 return error;
471 }
472
473 int
474 wdclose(dev, flag, fmt, p)
475 dev_t dev;
476 int flag, fmt;
477 struct proc *p;
478 {
479 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
480 int part = WDPART(dev);
481 int error;
482
483 if ((error = wdlock(wd->d_link)) != 0)
484 return error;
485
486 switch (fmt) {
487 case S_IFCHR:
488 wd->sc_dk.dk_copenmask &= ~(1 << part);
489 break;
490 case S_IFBLK:
491 wd->sc_dk.dk_bopenmask &= ~(1 << part);
492 break;
493 }
494 wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
495
496 if (wd->sc_dk.dk_openmask == 0) {
497 /* XXXX Must wait for I/O to complete! */
498 }
499
500 wdunlock(wd->d_link);
501 return 0;
502 }
503
504 void
505 wdgetdefaultlabel(wd, lp)
506 struct wd_softc *wd;
507 struct disklabel *lp;
508 {
509 struct wd_link *d_link = wd->d_link;
510
511 bzero(lp, sizeof(struct disklabel));
512
513 lp->d_secsize = DEV_BSIZE;
514 lp->d_ntracks = d_link->sc_params.wdp_heads;
515 lp->d_nsectors = d_link->sc_params.wdp_sectors;
516 lp->d_ncylinders = d_link->sc_params.wdp_cylinders;
517 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
518
519 #if 0
520 strncpy(lp->d_typename, "ST506 disk", 16);
521 lp->d_type = DTYPE_ST506;
522 #endif
523 strncpy(lp->d_packname, d_link->sc_params.wdp_model, 16);
524 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
525 lp->d_rpm = 3600;
526 lp->d_interleave = 1;
527 lp->d_flags = 0;
528
529 lp->d_partitions[RAW_PART].p_offset = 0;
530 lp->d_partitions[RAW_PART].p_size =
531 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
532 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
533 lp->d_npartitions = RAW_PART + 1;
534
535 lp->d_magic = DISKMAGIC;
536 lp->d_magic2 = DISKMAGIC;
537 lp->d_checksum = dkcksum(lp);
538 }
539
540 /*
541 * Fabricate a default disk label, and try to read the correct one.
542 */
543 void
544 wdgetdisklabel(wd)
545 struct wd_softc *wd;
546 {
547 struct disklabel *lp = wd->sc_dk.dk_label;
548 struct wd_link *d_link = wd->d_link;
549 char *errstring;
550
551 WDDEBUG_PRINT(("wdgetdisklabel\n"));
552
553 bzero(wd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
554
555 wdgetdefaultlabel(wd, lp);
556
557 d_link->sc_badsect[0] = -1;
558
559 if (d_link->sc_state > RECAL)
560 d_link->sc_state = RECAL;
561 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
562 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
563 if (errstring) {
564 /*
565 * This probably happened because the drive's default
566 * geometry doesn't match the DOS geometry. We
567 * assume the DOS geometry is now in the label and try
568 * again. XXX This is a kluge.
569 */
570 if (d_link->sc_state > GEOMETRY)
571 d_link->sc_state = GEOMETRY;
572 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
573 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
574 }
575 if (errstring) {
576 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
577 return;
578 }
579
580 if (d_link->sc_state > GEOMETRY)
581 d_link->sc_state = GEOMETRY;
582 if ((lp->d_flags & D_BADSECT) != 0)
583 bad144intern(wd);
584 }
585
586
587 /*
588 * Tell the drive what geometry to use.
589 */
590 int
591 wdsetctlr(d_link)
592 struct wd_link *d_link;
593 {
594 struct wd_softc *wd=(struct wd_softc *)d_link->wd_softc;
595
596 WDDEBUG_PRINT(("wd(%d,%d) C%dH%dS%d\n", wd->sc_dev.dv_unit,
597 d_link->drive, wd->sc_dk.dk_label->d_ncylinders,
598 wd->sc_dk.dk_label->d_ntracks, wd->sc_dk.dk_label->d_nsectors));
599
600 if (wdccommand((struct wdc_softc *)d_link->wdc_softc,
601 d_link, WDCC_IDP, d_link->drive,
602 wd->sc_dk.dk_label->d_ncylinders,
603 wd->sc_dk.dk_label->d_ntracks - 1, 0,
604 wd->sc_dk.dk_label->d_nsectors) != 0) {
605 wderror(d_link, NULL, "wdsetctlr: geometry upload failed");
606 return -1;
607 }
608
609 return 0;
610 }
611
612 int
613 wdioctl(dev, xfer, addr, flag, p)
614 dev_t dev;
615 u_long xfer;
616 caddr_t addr;
617 int flag;
618 struct proc *p;
619 {
620 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
621 struct wd_link *d_link = wd->d_link;
622 int error;
623
624 WDDEBUG_PRINT(("wdioctl\n"));
625
626 if ((d_link->sc_flags & WDF_LOADED) == 0)
627 return EIO;
628
629 switch (xfer) {
630 case DIOCSBAD:
631 if ((flag & FWRITE) == 0)
632 return EBADF;
633 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
634 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
635 bad144intern(wd);
636 return 0;
637
638 case DIOCGDINFO:
639 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
640 return 0;
641
642 case DIOCGPART:
643 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
644 ((struct partinfo *)addr)->part =
645 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
646 return 0;
647
648 case DIOCWDINFO:
649 case DIOCSDINFO:
650 if ((flag & FWRITE) == 0)
651 return EBADF;
652
653 if ((error = wdlock(wd->d_link)) != 0)
654 return error;
655 d_link->sc_flags |= WDF_LABELLING;
656
657 error = setdisklabel(wd->sc_dk.dk_label,
658 (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
659 wd->sc_dk.dk_cpulabel);
660 if (error == 0) {
661 if (d_link->sc_state > GEOMETRY)
662 d_link->sc_state = GEOMETRY;
663 if (xfer == DIOCWDINFO)
664 error = writedisklabel(WDLABELDEV(dev),
665 wdstrategy, wd->sc_dk.dk_label,
666 wd->sc_dk.dk_cpulabel);
667 }
668
669 d_link->sc_flags &= ~WDF_LABELLING;
670 wdunlock(d_link);
671 return error;
672
673 case DIOCWLABEL:
674 if ((flag & FWRITE) == 0)
675 return EBADF;
676 if (*(int *)addr)
677 d_link->sc_flags |= WDF_WLABEL;
678 else
679 d_link->sc_flags &= ~WDF_WLABEL;
680 return 0;
681
682 case DIOCGDEFLABEL:
683 wdgetdefaultlabel(wd, (struct disklabel *)addr);
684 return 0;
685
686 #ifdef notyet
687 case DIOCWFORMAT:
688 if ((flag & FWRITE) == 0)
689 return EBADF;
690 {
691 register struct format_op *fop;
692 struct iovec aiov;
693 struct uio auio;
694
695 fop = (struct format_op *)addr;
696 aiov.iov_base = fop->df_buf;
697 aiov.iov_len = fop->df_count;
698 auio.uio_iov = &aiov;
699 auio.uio_iovcnt = 1;
700 auio.uio_resid = fop->df_count;
701 auio.uio_segflg = 0;
702 auio.uio_offset =
703 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
704 auio.uio_procp = p;
705 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
706 &auio);
707 fop->df_count -= auio.uio_resid;
708 fop->df_reg[0] = wdc->sc_status;
709 fop->df_reg[1] = wdc->sc_error;
710 return error;
711 }
712 #endif
713
714 default:
715 return ENOTTY;
716 }
717
718 #ifdef DIAGNOSTIC
719 panic("wdioctl: impossible");
720 #endif
721 }
722
723 #ifdef B_FORMAT
724 int
725 wdformat(struct buf *bp)
726 {
727
728 bp->b_flags |= B_FORMAT;
729 return wdstrategy(bp);
730 }
731 #endif
732
733 int
734 wdsize(dev)
735 dev_t dev;
736 {
737 struct wd_softc *wd;
738 int part, unit, omask;
739 int size;
740
741 WDDEBUG_PRINT(("wdsize\n"));
742
743 unit = WDUNIT(dev);
744 if (unit >= wd_cd.cd_ndevs)
745 return (-1);
746 wd = wd_cd.cd_devs[unit];
747 if (wd == NULL)
748 return (-1);
749
750 part = WDPART(dev);
751 omask = wd->sc_dk.dk_openmask & (1 << part);
752
753 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
754 return (-1);
755 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
756 size = -1;
757 else
758 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
759 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
760 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
761 return (-1);
762 return (size);
763 }
764
765
766 #ifndef __BDEVSW_DUMP_OLD_TYPE
767 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
768 static int wddoingadump;
769 static int wddumprecalibrated;
770
771 /*
772 * Dump core after a system crash.
773 *
774 * XXX: This needs work! Currently, it's a major hack: the
775 * use of wdc_softc is very bad and should go away.
776 */
777 int
778 wddump(dev, blkno, va, size)
779 dev_t dev;
780 daddr_t blkno;
781 caddr_t va;
782 size_t size;
783 {
784 struct wd_softc *wd; /* disk unit to do the I/O */
785 struct wdc_softc *wdc; /* disk controller to do the I/O */
786 struct disklabel *lp; /* disk's disklabel */
787 struct wd_link *d_link;
788 int unit, part;
789 int nblks; /* total number of sectors left to write */
790
791 /* Check if recursive dump; if so, punt. */
792 if (wddoingadump)
793 return EFAULT;
794 wddoingadump = 1;
795
796 unit = WDUNIT(dev);
797 if (unit >= wd_cd.cd_ndevs)
798 return ENXIO;
799 wd = wd_cd.cd_devs[unit];
800 if (wd == (struct wd_softc *)0)
801 return ENXIO;
802 d_link = wd->d_link;
803
804 part = WDPART(dev);
805
806 /* Make sure it was initialized. */
807 if (d_link->sc_state < READY)
808 return ENXIO;
809
810 wdc = (void *)wd->sc_dev.dv_parent;
811
812 /* Convert to disk sectors. Request must be a multiple of size. */
813 lp = wd->sc_dk.dk_label;
814 if ((size % lp->d_secsize) != 0)
815 return EFAULT;
816 nblks = size / lp->d_secsize;
817 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
818
819 /* Check transfer bounds against partition size. */
820 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
821 return EINVAL;
822
823 /* Offset block number to start of partition. */
824 blkno += lp->d_partitions[part].p_offset;
825
826 /* Recalibrate, if first dump transfer. */
827 if (wddumprecalibrated == 0) {
828 wddumprecalibrated = 1;
829 if (wdccommandshort(wdc, d_link->drive, WDCC_RECAL) != 0 ||
830 wait_for_ready(wdc) != 0 || wdsetctlr(d_link) != 0 ||
831 wait_for_ready(wdc) != 0) {
832 wderror(d_link, NULL, "wddump: recal failed");
833 return EIO;
834 }
835 }
836
837 while (nblks > 0) {
838 daddr_t xlt_blkno = blkno;
839 long cylin, head, sector;
840
841 if ((lp->d_flags & D_BADSECT) != 0) {
842 long blkdiff;
843 int i;
844
845 for (i = 0; (blkdiff = d_link->sc_badsect[i]) != -1; i++) {
846 blkdiff -= xlt_blkno;
847 if (blkdiff < 0)
848 continue;
849 if (blkdiff == 0) {
850 /* Replace current block of transfer. */
851 xlt_blkno = lp->d_secperunit -
852 lp->d_nsectors - i - 1;
853 }
854 break;
855 }
856 /* Tranfer is okay now. */
857 }
858
859 if ((d_link->sc_params.wdp_capabilities & WD_CAP_LBA) != 0) {
860 sector = (xlt_blkno >> 0) & 0xff;
861 cylin = (xlt_blkno >> 8) & 0xffff;
862 head = (xlt_blkno >> 24) & 0xf;
863 head |= WDSD_LBA;
864 } else {
865 sector = xlt_blkno % lp->d_nsectors;
866 sector++; /* Sectors begin with 1, not 0. */
867 xlt_blkno /= lp->d_nsectors;
868 head = xlt_blkno % lp->d_ntracks;
869 xlt_blkno /= lp->d_ntracks;
870 cylin = xlt_blkno;
871 head |= WDSD_CHS;
872 }
873
874 #ifndef WD_DUMP_NOT_TRUSTED
875 if (wdccommand((struct wdc_softc *)d_link->wdc_softc, d_link,
876 WDCC_WRITE, d_link->drive, cylin, head, sector, 1) != 0 ||
877 wait_for_drq(wdc) != 0) {
878 wderror(d_link, NULL, "wddump: write failed");
879 return EIO;
880 }
881
882 /* XXX XXX XXX */
883 outsw(wdc->sc_iobase + wd_data, va, lp->d_secsize >> 1);
884
885 /* Check data request (should be done). */
886 if (wait_for_ready(wdc) != 0) {
887 wderror(d_link, NULL,
888 "wddump: timeout waiting for ready");
889 return EIO;
890 }
891 #else /* WD_DUMP_NOT_TRUSTED */
892 /* Let's just talk about this first... */
893 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
894 unit, va, cylin, head, sector);
895 delay(500 * 1000); /* half a second */
896 #endif
897
898 /* update block count */
899 nblks -= 1;
900 blkno += 1;
901 va += lp->d_secsize;
902 }
903
904 wddoingadump = 0;
905 return 0;
906 }
907 #else /* __BDEVSW_DUMP_NEW_TYPE */
908
909
910 int
911 wddump(dev, blkno, va, size)
912 dev_t dev;
913 daddr_t blkno;
914 caddr_t va;
915 size_t size;
916 {
917
918 /* Not implemented. */
919 return ENXIO;
920 }
921 #endif /* __BDEVSW_DUMP_NEW_TYPE */
922
923 /*
924 * Internalize the bad sector table.
925 */
926 void
927 bad144intern(wd)
928 struct wd_softc *wd;
929 {
930 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
931 struct disklabel *lp = wd->sc_dk.dk_label;
932 struct wd_link *d_link = wd->d_link;
933 int i = 0;
934
935 WDDEBUG_PRINT(("bad144intern\n"));
936
937 for (; i < 126; i++) {
938 if (bt->bt_bad[i].bt_cyl == 0xffff)
939 break;
940 d_link->sc_badsect[i] =
941 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
942 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
943 (bt->bt_bad[i].bt_trksec & 0xff);
944 }
945 for (; i < 127; i++)
946 d_link->sc_badsect[i] = -1;
947 }
948
949 void
950 wderror(d_link, bp, msg)
951 struct wd_link *d_link;
952 struct buf *bp;
953 char *msg;
954 {
955 struct wd_softc *wd = (struct wd_softc *)d_link->wd_softc;
956
957 if (bp) {
958 diskerr(bp, "wd", msg, LOG_PRINTF, bp->b_bcount,
959 wd->sc_dk.dk_label);
960 printf("\n");
961 } else
962 printf("%s: %s\n", wd->sc_dev.dv_xname, msg);
963 }
964
965 void
966 wddone(d_link, bp)
967 struct wd_link *d_link;
968 struct buf *bp;
969 {
970 struct wd_softc *wd = (void *)d_link->wd_softc;
971
972 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
973 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
974 }
975