wd.c revision 1.175.2.9 1 /* $NetBSD: wd.c,v 1.175.2.9 1998/09/11 16:23:12 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1998 Manuel Bouyer. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Manuel Bouyer.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Charles M. Hannum and by Onno van der Linden.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the NetBSD
50 * Foundation, Inc. and its contributors.
51 * 4. Neither the name of The NetBSD Foundation nor the names of its
52 * contributors may be used to endorse or promote products derived
53 * from this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 * POSSIBILITY OF SUCH DAMAGE.
66 */
67
68 #define WDCDEBUG
69
70 #include "rnd.h"
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/conf.h>
76 #include <sys/file.h>
77 #include <sys/stat.h>
78 #include <sys/ioctl.h>
79 #include <sys/buf.h>
80 #include <sys/uio.h>
81 #include <sys/malloc.h>
82 #include <sys/device.h>
83 #include <sys/disklabel.h>
84 #include <sys/disk.h>
85 #include <sys/syslog.h>
86 #include <sys/proc.h>
87 #if NRND > 0
88 #include <sys/rnd.h>
89 #endif
90
91 #include <vm/vm.h>
92
93 #include <machine/intr.h>
94 #include <machine/bus.h>
95
96 #include <dev/ata/atareg.h>
97 #include <dev/ata/atavar.h>
98 #include <dev/ata/wdvar.h>
99 #include <dev/ic/wdcreg.h>
100 #include "locators.h"
101
102 #define WAITTIME (4 * hz) /* time to wait for a completion */
103 #define WDIORETRIES 5 /* number of retries before giving up */
104 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
105
106 #define WDUNIT(dev) DISKUNIT(dev)
107 #define WDPART(dev) DISKPART(dev)
108 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
109
110 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
111
112 #define DEBUG_INTR 0x01
113 #define DEBUG_XFERS 0x02
114 #define DEBUG_STATUS 0x04
115 #define DEBUG_FUNCS 0x08
116 #define DEBUG_PROBE 0x10
117 #ifdef WDCDEBUG
118 extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
119 #define WDCDEBUG_PRINT(args, level) \
120 if (wdcdebug_wd_mask & (level)) \
121 printf args
122 #else
123 #define WDCDEBUG_PRINT(args, level)
124 #endif
125
126 struct wd_softc {
127 /* General disk infos */
128 struct device sc_dev;
129 struct disk sc_dk;
130 struct buf sc_q;
131 /* IDE disk soft states */
132 struct ata_bio sc_wdc_bio; /* current transfert */
133 struct buf *sc_bp; /* buf being transfered */
134 void *wdc_softc; /* pointer to our parent */
135 struct ata_drive_datas *drvp; /* Our controller's infos */
136 int openings;
137 struct ataparams sc_params;/* drive characteistics found */
138 int sc_flags;
139 #define WDF_LOCKED 0x01
140 #define WDF_WANTED 0x02
141 #define WDF_WLABEL 0x04 /* label is writable */
142 #define WDF_LABELLING 0x08 /* writing label */
143 /*
144 * XXX Nothing resets this yet, but disk change sensing will when ATA-4 is
145 * more fully implemented.
146 */
147 #define WDF_LOADED 0x10 /* parameters loaded */
148 #define WDF_WAIT 0x20 /* waiting for resources */
149 #define WDF_LBA 0x40 /* using LBA mode */
150 int sc_capacity;
151 int cyl; /* actual drive parameters */
152 int heads;
153 int sectors;
154 int retries; /* number of xfer retry */
155 #if NRND > 0
156 rndsource_element_t rnd_source;
157 #endif
158 };
159
160 #define sc_drive sc_wdc_bio.drive
161 #define sc_mode sc_wdc_bio.mode
162 #define sc_multi sc_wdc_bio.multi
163 #define sc_badsect sc_wdc_bio.badsect
164
165 int wdprobe __P((struct device *, struct cfdata *, void *));
166 void wdattach __P((struct device *, struct device *, void *));
167 int wdprint __P((void *, char *));
168
169 struct cfattach wd_ca = {
170 sizeof(struct wd_softc), wdprobe, wdattach
171 };
172
173 extern struct cfdriver wd_cd;
174
175 void wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
176 void wdgetdisklabel __P((struct wd_softc *));
177 void wdstrategy __P((struct buf *));
178 void wdstart __P((void *));
179 void __wdstart __P((struct wd_softc*, struct buf *));
180 void wdrestart __P((void*));
181 int wd_get_params __P((struct wd_softc *, u_int8_t, struct ataparams *));
182
183 struct dkdriver wddkdriver = { wdstrategy };
184
185 /* XXX: these should go elsewhere */
186 cdev_decl(wd);
187 bdev_decl(wd);
188
189 #ifdef HAS_BAD144_HANDLING
190 static void bad144intern __P((struct wd_softc *));
191 #endif
192 int wdlock __P((struct wd_softc *));
193 void wdunlock __P((struct wd_softc *));
194 void print_wderror __P((int, char*));
195
196 int
197 wdprobe(parent, match, aux)
198 struct device *parent;
199 struct cfdata *match;
200
201 void *aux;
202 {
203 struct ata_atapi_attach *aa_link = aux;
204
205 if (aa_link == NULL)
206 return 0;
207 if (aa_link->aa_type != T_ATA)
208 return 0;
209
210 if (match->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT &&
211 match->cf_loc[ATACF_CHANNEL] != aa_link->aa_channel)
212 return 0;
213
214 if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
215 match->cf_loc[ATACF_DRIVE] != aa_link->aa_drv_data->drive)
216 return 0;
217
218 return 1;
219 }
220
221 void
222 wdattach(parent, self, aux)
223 struct device *parent, *self;
224 void *aux;
225 {
226 struct wd_softc *wd = (void *)self;
227 struct ata_atapi_attach *aa_link= aux;
228 int i, blank;
229 char buf[41], c, *p, *q;
230 WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
231
232 wd->openings = aa_link->aa_openings;
233 wd->drvp = aa_link->aa_drv_data;;
234 wd->wdc_softc = parent;
235 /* give back our softc to our caller */
236 wd->drvp->drv_softc = wd;
237
238 /* read our drive info */
239 wd_get_params(wd, AT_POLL, &wd->sc_params);
240
241 for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
242 i < sizeof(wd->sc_params.atap_model); i++) {
243 c = *p++;
244 if (c == '\0')
245 break;
246 if (c != ' ') {
247 if (blank) {
248 *q++ = ' ';
249 blank = 0;
250 }
251 *q++ = c;
252 } else
253 blank = 1;
254 }
255 *q++ = '\0';
256
257 printf(": <%s>\n", buf);
258
259 if ((wd->sc_params.atap_multi & 0xff) > 1) {
260 wd->sc_multi = wd->sc_params.atap_multi & 0xff;
261 } else {
262 wd->sc_multi = 1;
263 }
264
265 printf("%s: using %d-sector pio transfers,", wd->sc_dev.dv_xname,
266 wd->sc_multi);
267
268 /* Prior to ATA-4, LBA was optional. */
269 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
270 wd->sc_flags |= WDF_LBA;
271 #if 0
272 /* ATA-4 requires LBA. */
273 if (wd->sc_params.atap_ataversion != 0xffff &&
274 wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
275 wd->sc_flags |= WDF_LBA;
276 #endif
277
278 if ((wd->sc_flags & WDF_LBA) != 0) {
279 printf(" lba mode\n");
280 wd->sc_capacity =
281 (wd->sc_params.atap_capacity[1] << 16) |
282 wd->sc_params.atap_capacity[0];
283 printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d sectors\n",
284 self->dv_xname,
285 wd->sc_capacity / (1048576 / DEV_BSIZE),
286 wd->sc_params.atap_cylinders,
287 wd->sc_params.atap_heads,
288 wd->sc_params.atap_sectors,
289 DEV_BSIZE,
290 wd->sc_capacity);
291 } else {
292 printf(" chs mode\n");
293 wd->sc_capacity =
294 wd->sc_params.atap_cylinders *
295 wd->sc_params.atap_heads *
296 wd->sc_params.atap_sectors;
297 printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d "
298 "sectors\n", self->dv_xname,
299 wd->sc_capacity / (1048576 / DEV_BSIZE),
300 wd->sc_params.atap_cylinders,
301 wd->sc_params.atap_heads,
302 wd->sc_params.atap_sectors,
303 DEV_BSIZE,
304 wd->sc_capacity);
305 }
306 WDCDEBUG_PRINT(("atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
307 wd->sc_params.atap_dmatiming_mimi,
308 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
309 /*
310 * Initialize and attach the disk structure.
311 */
312 wd->sc_dk.dk_driver = &wddkdriver;
313 wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
314 disk_attach(&wd->sc_dk);
315 wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
316
317 #if NRND > 0
318 rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname, RND_TYPE_DISK);
319 #endif
320 }
321
322 /*
323 * Read/write routine for a buffer. Validates the arguments and schedules the
324 * transfer. Does not wait for the transfer to complete.
325 */
326 void
327 wdstrategy(bp)
328 struct buf *bp;
329 {
330 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
331 int s;
332 WDCDEBUG_PRINT(("wdstrategy\n"), DEBUG_FUNCS | DEBUG_XFERS);
333
334 /* Valid request? */
335 if (bp->b_blkno < 0 ||
336 (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
337 (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
338 bp->b_error = EINVAL;
339 goto bad;
340 }
341
342 /* If device invalidated (e.g. media change, door open), error. */
343 if ((wd->sc_flags & WDF_LOADED) == 0) {
344 bp->b_error = EIO;
345 goto bad;
346 }
347
348 /* If it's a null transfer, return immediately. */
349 if (bp->b_bcount == 0)
350 goto done;
351
352 /*
353 * Do bounds checking, adjust transfer. if error, process.
354 * If end of partition, just return.
355 */
356 if (WDPART(bp->b_dev) != RAW_PART &&
357 bounds_check_with_label(bp, wd->sc_dk.dk_label,
358 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
359 goto done;
360 /* Queue transfer on drive, activate drive and controller if idle. */
361 s = splbio();
362 disksort(&wd->sc_q, bp);
363 wdstart(wd);
364 splx(s);
365 return;
366 bad:
367 bp->b_flags |= B_ERROR;
368 done:
369 /* Toss transfer; we're done early. */
370 bp->b_resid = bp->b_bcount;
371 biodone(bp);
372 }
373
374 /*
375 * Queue a drive for I/O.
376 */
377 void
378 wdstart(arg)
379 void *arg;
380 {
381 struct wd_softc *wd = arg;
382 struct buf *dp, *bp=0;
383
384 WDCDEBUG_PRINT(("wdstart\n"), DEBUG_FUNCS | DEBUG_XFERS);
385 while (wd->openings > 0) {
386
387 /* Is there a buf for us ? */
388 dp = &wd->sc_q;
389 if ((bp = dp->b_actf) == NULL) /* yes, an assign */
390 return;
391 dp->b_actf = bp->b_actf;
392
393 /*
394 * Make the command. First lock the device
395 */
396 wd->openings--;
397
398 wd->retries = 0;
399 __wdstart(wd, bp);
400 }
401 }
402
403 void
404 __wdstart(wd, bp)
405 struct wd_softc *wd;
406 struct buf *bp;
407 {
408 daddr_t p_offset;
409 if (WDPART(bp->b_dev) != RAW_PART)
410 p_offset =
411 wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
412 else
413 p_offset = 0;
414 wd->sc_wdc_bio.blkno = bp->b_blkno + p_offset;
415 wd->sc_wdc_bio.blkno /= (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
416 wd->sc_wdc_bio.blkdone =0;
417 wd->sc_bp = bp;
418 /*
419 * If we're retrying, retry in single-sector mode. This will give us
420 * the sector number of the problem, and will eventually allow the
421 * transfert to succeed.
422 */
423 if (wd->sc_multi == 1 || wd->retries > 0)
424 wd->sc_wdc_bio.flags = ATA_SINGLE;
425 else
426 wd->sc_wdc_bio.flags = 0;
427 if (wd->sc_flags & WDF_LBA)
428 wd->sc_wdc_bio.flags |= ATA_LBA;
429 if (bp->b_flags & B_READ)
430 wd->sc_wdc_bio.flags |= ATA_READ;
431 wd->sc_wdc_bio.bcount = bp->b_bcount;
432 wd->sc_wdc_bio.databuf = bp->b_data;
433 /* Instrumentation. */
434 disk_busy(&wd->sc_dk);
435 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
436 case WDC_TRY_AGAIN:
437 timeout(wdrestart, wd, hz);
438 break;
439 case WDC_QUEUED:
440 break;
441 case WDC_COMPLETE:
442 wddone(wd);
443 break;
444 default:
445 panic("__wdstart: bad return code from wdc_ata_bio()");
446 }
447 }
448
449 void
450 wddone(v)
451 void *v;
452 {
453 struct wd_softc *wd = v;
454 struct buf *bp = wd->sc_bp;
455 char buf[256], *errbuf = buf;
456 WDCDEBUG_PRINT(("wddone\n"), DEBUG_FUNCS | DEBUG_XFERS);
457
458 bp->b_resid = wd->sc_wdc_bio.bcount;
459 errbuf[0] = '\0';
460 switch (wd->sc_wdc_bio.error) {
461 case ERR_DMA:
462 errbuf = "DMA error";
463 goto retry;
464 case ERR_DF:
465 errbuf = "device fault";
466 goto retry;
467 case TIMEOUT:
468 errbuf = "device timeout";
469 goto retry;
470 case ERROR:
471 /* Don't care about media change bits */
472 if (wd->sc_wdc_bio.r_error != 0 &&
473 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
474 goto noerror;
475 print_wderror(wd->sc_wdc_bio.r_error, errbuf);
476 retry: /* Just reset and retry. Can we do more ? */
477 wdc_reset_channel(wd->drvp);
478 diskerr(bp, "wd", errbuf, LOG_PRINTF,
479 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
480 if (wd->retries++ < WDIORETRIES) {
481 printf(", retrying\n");
482 timeout(wdrestart, wd, RECOVERYTIME);
483 return;
484 }
485 printf("\n");
486 bp->b_flags |= B_ERROR;
487 bp->b_error = EIO;
488 break;
489 case NOERROR:
490 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
491 printf("%s: soft error (corrected)\n",
492 wd->sc_dev.dv_xname);
493 }
494 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
495 #if NRND > 0
496 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
497 #endif
498 biodone(bp);
499 wd->openings++;
500 wdstart(wd);
501 }
502
503 void
504 wdrestart(v)
505 void *v;
506 {
507 struct wd_softc *wd = v;
508 struct buf *bp = wd->sc_bp;
509 int s;
510 WDCDEBUG_PRINT(("wdrestart\n"), DEBUG_FUNCS | DEBUG_XFERS);
511
512 s = splbio();
513 __wdstart(v, bp);
514 splx(s);
515 }
516
517 int
518 wdread(dev, uio, flags)
519 dev_t dev;
520 struct uio *uio;
521 int flags;
522 {
523
524 WDCDEBUG_PRINT(("wdread\n"), DEBUG_FUNCS | DEBUG_XFERS);
525 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
526 }
527
528 int
529 wdwrite(dev, uio, flags)
530 dev_t dev;
531 struct uio *uio;
532 int flags;
533 {
534
535 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_FUNCS | DEBUG_XFERS);
536 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
537 }
538
539 /*
540 * Wait interruptibly for an exclusive lock.
541 *
542 * XXX
543 * Several drivers do this; it should be abstracted and made MP-safe.
544 */
545 int
546 wdlock(wd)
547 struct wd_softc *wd;
548 {
549 int error;
550 int s;
551
552 WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
553
554 s = splbio();
555
556 while ((wd->sc_flags & WDF_LOCKED) != 0) {
557 wd->sc_flags |= WDF_WANTED;
558 if ((error = tsleep(wd, PRIBIO | PCATCH,
559 "wdlck", 0)) != 0) {
560 splx(s);
561 return error;
562 }
563 }
564 wd->sc_flags |= WDF_LOCKED;
565 splx(s);
566 return 0;
567 }
568
569 /*
570 * Unlock and wake up any waiters.
571 */
572 void
573 wdunlock(wd)
574 struct wd_softc *wd;
575 {
576
577 WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
578
579 wd->sc_flags &= ~WDF_LOCKED;
580 if ((wd->sc_flags & WDF_WANTED) != 0) {
581 wd->sc_flags &= ~WDF_WANTED;
582 wakeup(wd);
583 }
584 }
585
586 int
587 wdopen(dev, flag, fmt, p)
588 dev_t dev;
589 int flag, fmt;
590 struct proc *p;
591 {
592 struct wd_softc *wd;
593 int unit, part;
594 int error;
595
596 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
597 unit = WDUNIT(dev);
598 if (unit >= wd_cd.cd_ndevs)
599 return ENXIO;
600 wd = wd_cd.cd_devs[unit];
601 if (wd == NULL)
602 return ENXIO;
603
604 if ((error = wdlock(wd)) != 0)
605 return error;
606
607 if (wd->sc_dk.dk_openmask != 0) {
608 /*
609 * If any partition is open, but the disk has been invalidated,
610 * disallow further opens.
611 */
612 if ((wd->sc_flags & WDF_LOADED) == 0) {
613 error = EIO;
614 goto bad3;
615 }
616 } else {
617 if ((wd->sc_flags & WDF_LOADED) == 0) {
618 wd->sc_flags |= WDF_LOADED;
619
620 /* Load the physical device parameters. */
621 wd_get_params(wd, AT_POLL, &wd->sc_params);
622
623 /* Load the partition info if not already loaded. */
624 wdgetdisklabel(wd);
625 }
626 }
627
628 part = WDPART(dev);
629
630 /* Check that the partition exists. */
631 if (part != RAW_PART &&
632 (part >= wd->sc_dk.dk_label->d_npartitions ||
633 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
634 error = ENXIO;
635 goto bad;
636 }
637
638 /* Insure only one open at a time. */
639 switch (fmt) {
640 case S_IFCHR:
641 wd->sc_dk.dk_copenmask |= (1 << part);
642 break;
643 case S_IFBLK:
644 wd->sc_dk.dk_bopenmask |= (1 << part);
645 break;
646 }
647 wd->sc_dk.dk_openmask =
648 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
649
650 wdunlock(wd);
651 return 0;
652
653 bad:
654 if (wd->sc_dk.dk_openmask == 0) {
655 }
656
657 bad3:
658 wdunlock(wd);
659 return error;
660 }
661
662 int
663 wdclose(dev, flag, fmt, p)
664 dev_t dev;
665 int flag, fmt;
666 struct proc *p;
667 {
668 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
669 int part = WDPART(dev);
670 int error;
671
672 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
673 if ((error = wdlock(wd)) != 0)
674 return error;
675
676 switch (fmt) {
677 case S_IFCHR:
678 wd->sc_dk.dk_copenmask &= ~(1 << part);
679 break;
680 case S_IFBLK:
681 wd->sc_dk.dk_bopenmask &= ~(1 << part);
682 break;
683 }
684 wd->sc_dk.dk_openmask =
685 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
686
687 if (wd->sc_dk.dk_openmask == 0) {
688 /* XXXX Must wait for I/O to complete! */
689 }
690
691 wdunlock(wd);
692 return 0;
693 }
694
695 void
696 wdgetdefaultlabel(wd, lp)
697 struct wd_softc *wd;
698 struct disklabel *lp;
699 {
700
701 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
702 memset(lp, 0, sizeof(struct disklabel));
703
704 lp->d_secsize = DEV_BSIZE;
705 lp->d_ntracks = wd->sc_params.atap_heads;
706 lp->d_nsectors = wd->sc_params.atap_sectors;
707 lp->d_ncylinders = wd->sc_params.atap_cylinders;
708 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
709
710 #if 0
711 if (strcmp(wd->sc_params.atap_model, "ST506") == 0) {
712 lp->d_type = DTYPE_ST506;
713 strncpy(lp->d_typename, "ST506 disk", 16);
714 } else {
715 lp->d_type = DTYPE_ESDI;
716 strncpy(lp->d_typename, "ESDI/IDE",
717 sizeof lp->d_typename);
718 }
719 #endif
720 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
721 strncpy(lp->d_packname, "fictitious", 16);
722 lp->d_secperunit = wd->sc_capacity;
723 lp->d_rpm = 3600;
724 lp->d_interleave = 1;
725 lp->d_flags = 0;
726
727 lp->d_partitions[RAW_PART].p_offset = 0;
728 lp->d_partitions[RAW_PART].p_size =
729 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
730 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
731 lp->d_npartitions = RAW_PART + 1;
732
733 lp->d_magic = DISKMAGIC;
734 lp->d_magic2 = DISKMAGIC;
735 lp->d_checksum = dkcksum(lp);
736 }
737
738 /*
739 * Fabricate a default disk label, and try to read the correct one.
740 */
741 void
742 wdgetdisklabel(wd)
743 struct wd_softc *wd;
744 {
745 struct disklabel *lp = wd->sc_dk.dk_label;
746 char *errstring;
747
748 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
749
750 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
751
752 wdgetdefaultlabel(wd, lp);
753
754 wd->sc_badsect[0] = -1;
755
756 if (wd->drvp->state > RECAL)
757 wd->drvp->state = RECAL;
758 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
759 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
760 if (errstring) {
761 /*
762 * This probably happened because the drive's default
763 * geometry doesn't match the DOS geometry. We
764 * assume the DOS geometry is now in the label and try
765 * again. XXX This is a kluge.
766 */
767 if (wd->drvp->state > RECAL)
768 wd->drvp->state = RECAL;
769 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
770 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
771 }
772 if (errstring) {
773 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
774 return;
775 }
776
777 if (wd->drvp->state > RECAL)
778 wd->drvp->state = RECAL;
779 #ifdef HAS_BAD144_HANDLING
780 if ((lp->d_flags & D_BADSECT) != 0)
781 bad144intern(wd);
782 #endif
783 }
784
785 int
786 wdioctl(dev, xfer, addr, flag, p)
787 dev_t dev;
788 u_long xfer;
789 caddr_t addr;
790 int flag;
791 struct proc *p;
792 {
793 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
794 int error;
795
796 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
797
798 if ((wd->sc_flags & WDF_LOADED) == 0)
799 return EIO;
800
801 switch (xfer) {
802 #ifdef HAS_BAD144_HANDLING
803 case DIOCSBAD:
804 if ((flag & FWRITE) == 0)
805 return EBADF;
806 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
807 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
808 bad144intern(wd);
809 return 0;
810 #endif
811
812 case DIOCGDINFO:
813 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
814 return 0;
815
816 case DIOCGPART:
817 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
818 ((struct partinfo *)addr)->part =
819 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
820 return 0;
821
822 case DIOCWDINFO:
823 case DIOCSDINFO:
824 if ((flag & FWRITE) == 0)
825 return EBADF;
826
827 if ((error = wdlock(wd)) != 0)
828 return error;
829 wd->sc_flags |= WDF_LABELLING;
830
831 error = setdisklabel(wd->sc_dk.dk_label,
832 (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
833 wd->sc_dk.dk_cpulabel);
834 if (error == 0) {
835 if (wd->drvp->state > RECAL)
836 wd->drvp->state = RECAL;
837 if (xfer == DIOCWDINFO)
838 error = writedisklabel(WDLABELDEV(dev),
839 wdstrategy, wd->sc_dk.dk_label,
840 wd->sc_dk.dk_cpulabel);
841 }
842
843 wd->sc_flags &= ~WDF_LABELLING;
844 wdunlock(wd);
845 return error;
846
847 case DIOCWLABEL:
848 if ((flag & FWRITE) == 0)
849 return EBADF;
850 if (*(int *)addr)
851 wd->sc_flags |= WDF_WLABEL;
852 else
853 wd->sc_flags &= ~WDF_WLABEL;
854 return 0;
855
856 case DIOCGDEFLABEL:
857 wdgetdefaultlabel(wd, (struct disklabel *)addr);
858 return 0;
859
860 #ifdef notyet
861 case DIOCWFORMAT:
862 if ((flag & FWRITE) == 0)
863 return EBADF;
864 {
865 register struct format_op *fop;
866 struct iovec aiov;
867 struct uio auio;
868
869 fop = (struct format_op *)addr;
870 aiov.iov_base = fop->df_buf;
871 aiov.iov_len = fop->df_count;
872 auio.uio_iov = &aiov;
873 auio.uio_iovcnt = 1;
874 auio.uio_resid = fop->df_count;
875 auio.uio_segflg = 0;
876 auio.uio_offset =
877 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
878 auio.uio_procp = p;
879 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
880 &auio);
881 fop->df_count -= auio.uio_resid;
882 fop->df_reg[0] = wdc->sc_status;
883 fop->df_reg[1] = wdc->sc_error;
884 return error;
885 }
886 #endif
887
888 default:
889 return ENOTTY;
890 }
891
892 #ifdef DIAGNOSTIC
893 panic("wdioctl: impossible");
894 #endif
895 }
896
897 #ifdef B_FORMAT
898 int
899 wdformat(struct buf *bp)
900 {
901
902 bp->b_flags |= B_FORMAT;
903 return wdstrategy(bp);
904 }
905 #endif
906
907 int
908 wdsize(dev)
909 dev_t dev;
910 {
911 struct wd_softc *wd;
912 int part, unit, omask;
913 int size;
914
915 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
916
917 unit = WDUNIT(dev);
918 if (unit >= wd_cd.cd_ndevs)
919 return (-1);
920 wd = wd_cd.cd_devs[unit];
921 if (wd == NULL)
922 return (-1);
923
924 part = WDPART(dev);
925 omask = wd->sc_dk.dk_openmask & (1 << part);
926
927 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
928 return (-1);
929 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
930 size = -1;
931 else
932 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
933 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
934 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
935 return (-1);
936 return (size);
937 }
938
939 #ifndef __BDEVSW_DUMP_OLD_TYPE
940 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
941 static int wddoingadump = 0;
942 static int wddumprecalibrated = 0;
943 static int wddumpmulti = 1;
944
945 /*
946 * Dump core after a system crash.
947 */
948 int
949 wddump(dev, blkno, va, size)
950 dev_t dev;
951 daddr_t blkno;
952 caddr_t va;
953 size_t size;
954 {
955 struct wd_softc *wd; /* disk unit to do the I/O */
956 struct disklabel *lp; /* disk's disklabel */
957 int unit, part;
958 int nblks; /* total number of sectors left to write */
959 int err;
960 char errbuf[256];
961
962 /* Check if recursive dump; if so, punt. */
963 if (wddoingadump)
964 return EFAULT;
965 wddoingadump = 1;
966
967 unit = WDUNIT(dev);
968 if (unit >= wd_cd.cd_ndevs)
969 return ENXIO;
970 wd = wd_cd.cd_devs[unit];
971 if (wd == (struct wd_softc *)0)
972 return ENXIO;
973
974 part = WDPART(dev);
975
976 /* Make sure it was initialized. */
977 if (wd->drvp->state < READY)
978 return ENXIO;
979
980 /* Convert to disk sectors. Request must be a multiple of size. */
981 lp = wd->sc_dk.dk_label;
982 if ((size % lp->d_secsize) != 0)
983 return EFAULT;
984 nblks = size / lp->d_secsize;
985 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
986
987 /* Check transfer bounds against partition size. */
988 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
989 return EINVAL;
990
991 /* Offset block number to start of partition. */
992 blkno += lp->d_partitions[part].p_offset;
993
994 /* Recalibrate, if first dump transfer. */
995 if (wddumprecalibrated == 0) {
996 wddumpmulti = wd->sc_multi;
997 wddumprecalibrated = 1;
998 wd->drvp->state = RECAL;
999 }
1000
1001 while (nblks > 0) {
1002 again:
1003 wd->sc_wdc_bio.blkno = blkno;
1004 wd->sc_wdc_bio.flags = ATA_POLL;
1005 if (wddumpmulti == 1)
1006 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1007 if (wd->sc_flags & WDF_LBA)
1008 wd->sc_wdc_bio.flags |= ATA_LBA;
1009 wd->sc_wdc_bio.bcount =
1010 min(nblks, wddumpmulti) * lp->d_secsize;
1011 wd->sc_wdc_bio.databuf = va;
1012 #ifndef WD_DUMP_NOT_TRUSTED
1013 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1014 case WDC_TRY_AGAIN:
1015 panic("wddump: try again");
1016 break;
1017 case WDC_QUEUED:
1018 panic("wddump: polled command has been queued");
1019 break;
1020 case WDC_COMPLETE:
1021 break;
1022 }
1023 switch(wd->sc_wdc_bio.error) {
1024 case TIMEOUT:
1025 printf("wddump: device timed out");
1026 err = EIO;
1027 break;
1028 case ERR_DF:
1029 printf("wddump: drive fault");
1030 err = EIO;
1031 break;
1032 case ERR_DMA:
1033 printf("wddump: DMA error");
1034 err = EIO;
1035 break;
1036 case ERROR:
1037 errbuf[0] = '\0';
1038 print_wderror(wd->sc_wdc_bio.r_error, errbuf);
1039 printf("wddump: %s", errbuf);
1040 err = EIO;
1041 break;
1042 case NOERROR:
1043 err = 0;
1044 break;
1045 default:
1046 panic("wddump: unknown error type");
1047 }
1048 if (err != 0) {
1049 if (wddumpmulti != 1) {
1050 wddumpmulti = 1; /* retry in single-sector */
1051 printf(", retrying\n");
1052 goto again;
1053 }
1054 printf("\n");
1055 return err;
1056 }
1057 #else /* WD_DUMP_NOT_TRUSTED */
1058 /* Let's just talk about this first... */
1059 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1060 unit, va, cylin, head, sector);
1061 delay(500 * 1000); /* half a second */
1062 #endif
1063
1064 /* update block count */
1065 nblks -= min(nblks, wddumpmulti);
1066 blkno += min(nblks, wddumpmulti);
1067 va += min(nblks, wddumpmulti) * lp->d_secsize;
1068 }
1069
1070 wddoingadump = 0;
1071 return 0;
1072 }
1073 #else /* __BDEVSW_DUMP_NEW_TYPE */
1074
1075
1076 int
1077 wddump(dev, blkno, va, size)
1078 dev_t dev;
1079 daddr_t blkno;
1080 caddr_t va;
1081 size_t size;
1082 {
1083
1084 /* Not implemented. */
1085 return ENXIO;
1086 }
1087 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1088
1089 #ifdef HAS_BAD144_HANDLING
1090 /*
1091 * Internalize the bad sector table.
1092 */
1093 void
1094 bad144intern(wd)
1095 struct wd_softc *wd;
1096 {
1097 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1098 struct disklabel *lp = wd->sc_dk.dk_label;
1099 int i = 0;
1100
1101 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_FUNCS | DEBUG_XFERS);
1102
1103 for (; i < NBT_BAD; i++) {
1104 if (bt->bt_bad[i].bt_cyl == 0xffff)
1105 break;
1106 wd->sc_badsect[i] =
1107 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1108 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1109 (bt->bt_bad[i].bt_trksec & 0xff);
1110 }
1111 for (; i < NBT_BAD+1; i++)
1112 wd->sc_badsect[i] = -1;
1113 }
1114 #endif
1115
1116 void
1117 print_wderror(errno, buf)
1118 int errno;
1119 char *buf;
1120 {
1121 static char *errstr[] = {"address mark not found", "track 0 not found",
1122 "aborted command", "media change requested", "id not found",
1123 "media changed", "uncorrectable data error", "bad block detected"};
1124 int i;
1125 char *sep = "";
1126
1127 if (errno == 0) {
1128 sprintf(buf, "error not notified");
1129 }
1130
1131 for (i = 0; i < 8; i++) {
1132 if (errno & (1 << i)) {
1133 buf += sprintf(buf, "%s %s", sep, errstr[i]);
1134 sep = ",";
1135 }
1136 }
1137 }
1138
1139 int
1140 wd_get_params(wd, flags, params)
1141 struct wd_softc *wd;
1142 u_int8_t flags;
1143 struct ataparams *params;
1144 {
1145 switch (ata_get_params(wd->drvp, flags, params)) {
1146 case CMD_AGAIN:
1147 return 1;
1148 case CMD_ERR:
1149 /*
1150 * We `know' there's a drive here; just assume it's old.
1151 * This geometry is only used to read the MBR and print a
1152 * (false) attach message.
1153 */
1154 strncpy(params->atap_model, "ST506",
1155 sizeof params->atap_model);
1156 params->atap_config = ATA_CFG_FIXED;
1157 params->atap_cylinders = 1024;
1158 params->atap_heads = 8;
1159 params->atap_sectors = 17;
1160 params->atap_multi = 1;
1161 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1162 return 0;
1163 case CMD_OK:
1164 return 0;
1165 default:
1166 panic("wd_get_params: bad return code from ata_get_params");
1167 /* NOTREACHED */
1168 }
1169 }
1170