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