wd.c revision 1.183 1 /* $NetBSD: wd.c,v 1.183 1998/10/20 17:00:25 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 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->sc_dev;
236
237 /* read our drive info */
238 if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
239 printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
240 return;
241 }
242
243 for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
244 i < sizeof(wd->sc_params.atap_model); i++) {
245 c = *p++;
246 if (c == '\0')
247 break;
248 if (c != ' ') {
249 if (blank) {
250 *q++ = ' ';
251 blank = 0;
252 }
253 *q++ = c;
254 } else
255 blank = 1;
256 }
257 *q++ = '\0';
258
259 printf(": <%s>\n", buf);
260
261 if ((wd->sc_params.atap_multi & 0xff) > 1) {
262 wd->sc_multi = wd->sc_params.atap_multi & 0xff;
263 } else {
264 wd->sc_multi = 1;
265 }
266
267 printf("%s: using %d-sector pio transfers,", wd->sc_dev.dv_xname,
268 wd->sc_multi);
269
270 /* Prior to ATA-4, LBA was optional. */
271 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
272 wd->sc_flags |= WDF_LBA;
273 #if 0
274 /* ATA-4 requires LBA. */
275 if (wd->sc_params.atap_ataversion != 0xffff &&
276 wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
277 wd->sc_flags |= WDF_LBA;
278 #endif
279
280 if ((wd->sc_flags & WDF_LBA) != 0) {
281 printf(" lba mode\n");
282 wd->sc_capacity =
283 (wd->sc_params.atap_capacity[1] << 16) |
284 wd->sc_params.atap_capacity[0];
285 printf("%s: %dMB, %d cyl, %d head, %d sec, "
286 "%d bytes/sect x %d sectors\n",
287 self->dv_xname,
288 wd->sc_capacity / (1048576 / DEV_BSIZE),
289 wd->sc_params.atap_cylinders,
290 wd->sc_params.atap_heads,
291 wd->sc_params.atap_sectors,
292 DEV_BSIZE,
293 wd->sc_capacity);
294 } else {
295 printf(" chs mode\n");
296 wd->sc_capacity =
297 wd->sc_params.atap_cylinders *
298 wd->sc_params.atap_heads *
299 wd->sc_params.atap_sectors;
300 printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d "
301 "sectors\n", self->dv_xname,
302 wd->sc_capacity / (1048576 / DEV_BSIZE),
303 wd->sc_params.atap_cylinders,
304 wd->sc_params.atap_heads,
305 wd->sc_params.atap_sectors,
306 DEV_BSIZE,
307 wd->sc_capacity);
308 }
309 WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
310 self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
311 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
312 /*
313 * Initialize and attach the disk structure.
314 */
315 wd->sc_dk.dk_driver = &wddkdriver;
316 wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
317 disk_attach(&wd->sc_dk);
318 wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
319
320 #if NRND > 0
321 rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname, RND_TYPE_DISK);
322 #endif
323 }
324
325 /*
326 * Read/write routine for a buffer. Validates the arguments and schedules the
327 * transfer. Does not wait for the transfer to complete.
328 */
329 void
330 wdstrategy(bp)
331 struct buf *bp;
332 {
333 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
334 int s;
335 WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
336 DEBUG_XFERS);
337
338 /* Valid request? */
339 if (bp->b_blkno < 0 ||
340 (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
341 (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
342 bp->b_error = EINVAL;
343 goto bad;
344 }
345
346 /* If device invalidated (e.g. media change, door open), error. */
347 if ((wd->sc_flags & WDF_LOADED) == 0) {
348 bp->b_error = EIO;
349 goto bad;
350 }
351
352 /* If it's a null transfer, return immediately. */
353 if (bp->b_bcount == 0)
354 goto done;
355
356 /*
357 * Do bounds checking, adjust transfer. if error, process.
358 * If end of partition, just return.
359 */
360 if (WDPART(bp->b_dev) != RAW_PART &&
361 bounds_check_with_label(bp, wd->sc_dk.dk_label,
362 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
363 goto done;
364 /* Queue transfer on drive, activate drive and controller if idle. */
365 s = splbio();
366 disksort(&wd->sc_q, bp);
367 wdstart(wd);
368 splx(s);
369 return;
370 bad:
371 bp->b_flags |= B_ERROR;
372 done:
373 /* Toss transfer; we're done early. */
374 bp->b_resid = bp->b_bcount;
375 biodone(bp);
376 }
377
378 /*
379 * Queue a drive for I/O.
380 */
381 void
382 wdstart(arg)
383 void *arg;
384 {
385 struct wd_softc *wd = arg;
386 struct buf *dp, *bp=0;
387
388 WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
389 DEBUG_XFERS);
390 while (wd->openings > 0) {
391
392 /* Is there a buf for us ? */
393 dp = &wd->sc_q;
394 if ((bp = dp->b_actf) == NULL) /* yes, an assign */
395 return;
396 dp->b_actf = bp->b_actf;
397
398 /*
399 * Make the command. First lock the device
400 */
401 wd->openings--;
402
403 wd->retries = 0;
404 __wdstart(wd, bp);
405 }
406 }
407
408 void
409 __wdstart(wd, bp)
410 struct wd_softc *wd;
411 struct buf *bp;
412 {
413 daddr_t p_offset;
414 if (WDPART(bp->b_dev) != RAW_PART)
415 p_offset =
416 wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
417 else
418 p_offset = 0;
419 wd->sc_wdc_bio.blkno = bp->b_blkno + p_offset;
420 wd->sc_wdc_bio.blkno /= (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
421 wd->sc_wdc_bio.blkdone =0;
422 wd->sc_bp = bp;
423 /*
424 * If we're retrying, retry in single-sector mode. This will give us
425 * the sector number of the problem, and will eventually allow the
426 * transfert to succeed.
427 */
428 if (wd->sc_multi == 1 || wd->retries > 0)
429 wd->sc_wdc_bio.flags = ATA_SINGLE;
430 else
431 wd->sc_wdc_bio.flags = 0;
432 if (wd->sc_flags & WDF_LBA)
433 wd->sc_wdc_bio.flags |= ATA_LBA;
434 if (bp->b_flags & B_READ)
435 wd->sc_wdc_bio.flags |= ATA_READ;
436 wd->sc_wdc_bio.bcount = bp->b_bcount;
437 wd->sc_wdc_bio.databuf = bp->b_data;
438 /* Instrumentation. */
439 disk_busy(&wd->sc_dk);
440 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
441 case WDC_TRY_AGAIN:
442 timeout(wdrestart, wd, hz);
443 break;
444 case WDC_QUEUED:
445 break;
446 case WDC_COMPLETE:
447 wddone(wd);
448 break;
449 default:
450 panic("__wdstart: bad return code from wdc_ata_bio()");
451 }
452 }
453
454 void
455 wddone(v)
456 void *v;
457 {
458 struct wd_softc *wd = v;
459 struct buf *bp = wd->sc_bp;
460 char buf[256], *errbuf = buf;
461 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
462 DEBUG_XFERS);
463
464 bp->b_resid = wd->sc_wdc_bio.bcount;
465 errbuf[0] = '\0';
466 switch (wd->sc_wdc_bio.error) {
467 case ERR_DMA:
468 errbuf = "DMA error";
469 goto retry;
470 case ERR_DF:
471 errbuf = "device fault";
472 goto retry;
473 case TIMEOUT:
474 errbuf = "device timeout";
475 goto retry;
476 case ERROR:
477 /* Don't care about media change bits */
478 if (wd->sc_wdc_bio.r_error != 0 &&
479 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
480 goto noerror;
481 print_wderror(wd->sc_wdc_bio.r_error, errbuf);
482 retry: /* Just reset and retry. Can we do more ? */
483 wdc_reset_channel(wd->drvp);
484 diskerr(bp, "wd", errbuf, LOG_PRINTF,
485 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
486 if (wd->retries++ < WDIORETRIES) {
487 printf(", retrying\n");
488 timeout(wdrestart, wd, RECOVERYTIME);
489 return;
490 }
491 printf("\n");
492 bp->b_flags |= B_ERROR;
493 bp->b_error = EIO;
494 break;
495 case NOERROR:
496 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
497 printf("%s: soft error (corrected)\n",
498 wd->sc_dev.dv_xname);
499 }
500 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
501 #if NRND > 0
502 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
503 #endif
504 biodone(bp);
505 wd->openings++;
506 wdstart(wd);
507 }
508
509 void
510 wdrestart(v)
511 void *v;
512 {
513 struct wd_softc *wd = v;
514 struct buf *bp = wd->sc_bp;
515 int s;
516 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
517 DEBUG_XFERS);
518
519 s = splbio();
520 __wdstart(v, bp);
521 splx(s);
522 }
523
524 int
525 wdread(dev, uio, flags)
526 dev_t dev;
527 struct uio *uio;
528 int flags;
529 {
530
531 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
532 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
533 }
534
535 int
536 wdwrite(dev, uio, flags)
537 dev_t dev;
538 struct uio *uio;
539 int flags;
540 {
541
542 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
543 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
544 }
545
546 /*
547 * Wait interruptibly for an exclusive lock.
548 *
549 * XXX
550 * Several drivers do this; it should be abstracted and made MP-safe.
551 */
552 int
553 wdlock(wd)
554 struct wd_softc *wd;
555 {
556 int error;
557 int s;
558
559 WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
560
561 s = splbio();
562
563 while ((wd->sc_flags & WDF_LOCKED) != 0) {
564 wd->sc_flags |= WDF_WANTED;
565 if ((error = tsleep(wd, PRIBIO | PCATCH,
566 "wdlck", 0)) != 0) {
567 splx(s);
568 return error;
569 }
570 }
571 wd->sc_flags |= WDF_LOCKED;
572 splx(s);
573 return 0;
574 }
575
576 /*
577 * Unlock and wake up any waiters.
578 */
579 void
580 wdunlock(wd)
581 struct wd_softc *wd;
582 {
583
584 WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
585
586 wd->sc_flags &= ~WDF_LOCKED;
587 if ((wd->sc_flags & WDF_WANTED) != 0) {
588 wd->sc_flags &= ~WDF_WANTED;
589 wakeup(wd);
590 }
591 }
592
593 int
594 wdopen(dev, flag, fmt, p)
595 dev_t dev;
596 int flag, fmt;
597 struct proc *p;
598 {
599 struct wd_softc *wd;
600 int unit, part;
601 int error;
602
603 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
604 unit = WDUNIT(dev);
605 if (unit >= wd_cd.cd_ndevs)
606 return ENXIO;
607 wd = wd_cd.cd_devs[unit];
608 if (wd == NULL)
609 return ENXIO;
610
611 if ((error = wdlock(wd)) != 0)
612 return error;
613
614 if (wd->sc_dk.dk_openmask != 0) {
615 /*
616 * If any partition is open, but the disk has been invalidated,
617 * disallow further opens.
618 */
619 if ((wd->sc_flags & WDF_LOADED) == 0) {
620 error = EIO;
621 goto bad3;
622 }
623 } else {
624 if ((wd->sc_flags & WDF_LOADED) == 0) {
625 wd->sc_flags |= WDF_LOADED;
626
627 /* Load the physical device parameters. */
628 wd_get_params(wd, AT_WAIT, &wd->sc_params);
629
630 /* Load the partition info if not already loaded. */
631 wdgetdisklabel(wd);
632 }
633 }
634
635 part = WDPART(dev);
636
637 /* Check that the partition exists. */
638 if (part != RAW_PART &&
639 (part >= wd->sc_dk.dk_label->d_npartitions ||
640 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
641 error = ENXIO;
642 goto bad;
643 }
644
645 /* Insure only one open at a time. */
646 switch (fmt) {
647 case S_IFCHR:
648 wd->sc_dk.dk_copenmask |= (1 << part);
649 break;
650 case S_IFBLK:
651 wd->sc_dk.dk_bopenmask |= (1 << part);
652 break;
653 }
654 wd->sc_dk.dk_openmask =
655 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
656
657 wdunlock(wd);
658 return 0;
659
660 bad:
661 if (wd->sc_dk.dk_openmask == 0) {
662 }
663
664 bad3:
665 wdunlock(wd);
666 return error;
667 }
668
669 int
670 wdclose(dev, flag, fmt, p)
671 dev_t dev;
672 int flag, fmt;
673 struct proc *p;
674 {
675 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
676 int part = WDPART(dev);
677 int error;
678
679 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
680 if ((error = wdlock(wd)) != 0)
681 return error;
682
683 switch (fmt) {
684 case S_IFCHR:
685 wd->sc_dk.dk_copenmask &= ~(1 << part);
686 break;
687 case S_IFBLK:
688 wd->sc_dk.dk_bopenmask &= ~(1 << part);
689 break;
690 }
691 wd->sc_dk.dk_openmask =
692 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
693
694 if (wd->sc_dk.dk_openmask == 0) {
695 /* XXXX Must wait for I/O to complete! */
696 }
697
698 wdunlock(wd);
699 return 0;
700 }
701
702 void
703 wdgetdefaultlabel(wd, lp)
704 struct wd_softc *wd;
705 struct disklabel *lp;
706 {
707
708 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
709 memset(lp, 0, sizeof(struct disklabel));
710
711 lp->d_secsize = DEV_BSIZE;
712 lp->d_ntracks = wd->sc_params.atap_heads;
713 lp->d_nsectors = wd->sc_params.atap_sectors;
714 lp->d_ncylinders = wd->sc_params.atap_cylinders;
715 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
716
717 #if 0
718 if (strcmp(wd->sc_params.atap_model, "ST506") == 0) {
719 lp->d_type = DTYPE_ST506;
720 strncpy(lp->d_typename, "ST506 disk", 16);
721 } else {
722 lp->d_type = DTYPE_ESDI;
723 strncpy(lp->d_typename, "ESDI/IDE",
724 sizeof lp->d_typename);
725 }
726 #endif
727 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
728 strncpy(lp->d_packname, "fictitious", 16);
729 lp->d_secperunit = wd->sc_capacity;
730 lp->d_rpm = 3600;
731 lp->d_interleave = 1;
732 lp->d_flags = 0;
733
734 lp->d_partitions[RAW_PART].p_offset = 0;
735 lp->d_partitions[RAW_PART].p_size =
736 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
737 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
738 lp->d_npartitions = RAW_PART + 1;
739
740 lp->d_magic = DISKMAGIC;
741 lp->d_magic2 = DISKMAGIC;
742 lp->d_checksum = dkcksum(lp);
743 }
744
745 /*
746 * Fabricate a default disk label, and try to read the correct one.
747 */
748 void
749 wdgetdisklabel(wd)
750 struct wd_softc *wd;
751 {
752 struct disklabel *lp = wd->sc_dk.dk_label;
753 char *errstring;
754
755 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
756
757 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
758
759 wdgetdefaultlabel(wd, lp);
760
761 wd->sc_badsect[0] = -1;
762
763 if (wd->drvp->state > RECAL)
764 wd->drvp->drive_flags |= DRIVE_RESET;
765 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
766 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
767 if (errstring) {
768 /*
769 * This probably happened because the drive's default
770 * geometry doesn't match the DOS geometry. We
771 * assume the DOS geometry is now in the label and try
772 * again. XXX This is a kluge.
773 */
774 if (wd->drvp->state > RECAL)
775 wd->drvp->drive_flags |= DRIVE_RESET;
776 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
777 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
778 }
779 if (errstring) {
780 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
781 return;
782 }
783
784 if (wd->drvp->state > RECAL)
785 wd->drvp->drive_flags |= DRIVE_RESET;
786 #ifdef HAS_BAD144_HANDLING
787 if ((lp->d_flags & D_BADSECT) != 0)
788 bad144intern(wd);
789 #endif
790 }
791
792 int
793 wdioctl(dev, xfer, addr, flag, p)
794 dev_t dev;
795 u_long xfer;
796 caddr_t addr;
797 int flag;
798 struct proc *p;
799 {
800 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
801 int error;
802
803 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
804
805 if ((wd->sc_flags & WDF_LOADED) == 0)
806 return EIO;
807
808 switch (xfer) {
809 #ifdef HAS_BAD144_HANDLING
810 case DIOCSBAD:
811 if ((flag & FWRITE) == 0)
812 return EBADF;
813 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
814 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
815 bad144intern(wd);
816 return 0;
817 #endif
818
819 case DIOCGDINFO:
820 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
821 return 0;
822
823 case DIOCGPART:
824 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
825 ((struct partinfo *)addr)->part =
826 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
827 return 0;
828
829 case DIOCWDINFO:
830 case DIOCSDINFO:
831 if ((flag & FWRITE) == 0)
832 return EBADF;
833
834 if ((error = wdlock(wd)) != 0)
835 return error;
836 wd->sc_flags |= WDF_LABELLING;
837
838 error = setdisklabel(wd->sc_dk.dk_label,
839 (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
840 wd->sc_dk.dk_cpulabel);
841 if (error == 0) {
842 if (wd->drvp->state > RECAL)
843 wd->drvp->drive_flags |= DRIVE_RESET;
844 if (xfer == DIOCWDINFO)
845 error = writedisklabel(WDLABELDEV(dev),
846 wdstrategy, wd->sc_dk.dk_label,
847 wd->sc_dk.dk_cpulabel);
848 }
849
850 wd->sc_flags &= ~WDF_LABELLING;
851 wdunlock(wd);
852 return error;
853
854 case DIOCWLABEL:
855 if ((flag & FWRITE) == 0)
856 return EBADF;
857 if (*(int *)addr)
858 wd->sc_flags |= WDF_WLABEL;
859 else
860 wd->sc_flags &= ~WDF_WLABEL;
861 return 0;
862
863 case DIOCGDEFLABEL:
864 wdgetdefaultlabel(wd, (struct disklabel *)addr);
865 return 0;
866
867 #ifdef notyet
868 case DIOCWFORMAT:
869 if ((flag & FWRITE) == 0)
870 return EBADF;
871 {
872 register struct format_op *fop;
873 struct iovec aiov;
874 struct uio auio;
875
876 fop = (struct format_op *)addr;
877 aiov.iov_base = fop->df_buf;
878 aiov.iov_len = fop->df_count;
879 auio.uio_iov = &aiov;
880 auio.uio_iovcnt = 1;
881 auio.uio_resid = fop->df_count;
882 auio.uio_segflg = 0;
883 auio.uio_offset =
884 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
885 auio.uio_procp = p;
886 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
887 &auio);
888 fop->df_count -= auio.uio_resid;
889 fop->df_reg[0] = wdc->sc_status;
890 fop->df_reg[1] = wdc->sc_error;
891 return error;
892 }
893 #endif
894
895 default:
896 return ENOTTY;
897 }
898
899 #ifdef DIAGNOSTIC
900 panic("wdioctl: impossible");
901 #endif
902 }
903
904 #ifdef B_FORMAT
905 int
906 wdformat(struct buf *bp)
907 {
908
909 bp->b_flags |= B_FORMAT;
910 return wdstrategy(bp);
911 }
912 #endif
913
914 int
915 wdsize(dev)
916 dev_t dev;
917 {
918 struct wd_softc *wd;
919 int part, unit, omask;
920 int size;
921
922 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
923
924 unit = WDUNIT(dev);
925 if (unit >= wd_cd.cd_ndevs)
926 return (-1);
927 wd = wd_cd.cd_devs[unit];
928 if (wd == NULL)
929 return (-1);
930
931 part = WDPART(dev);
932 omask = wd->sc_dk.dk_openmask & (1 << part);
933
934 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
935 return (-1);
936 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
937 size = -1;
938 else
939 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
940 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
941 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
942 return (-1);
943 return (size);
944 }
945
946 #ifndef __BDEVSW_DUMP_OLD_TYPE
947 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
948 static int wddoingadump = 0;
949 static int wddumprecalibrated = 0;
950 static int wddumpmulti = 1;
951
952 /*
953 * Dump core after a system crash.
954 */
955 int
956 wddump(dev, blkno, va, size)
957 dev_t dev;
958 daddr_t blkno;
959 caddr_t va;
960 size_t size;
961 {
962 struct wd_softc *wd; /* disk unit to do the I/O */
963 struct disklabel *lp; /* disk's disklabel */
964 int unit, part;
965 int nblks; /* total number of sectors left to write */
966 int err;
967 char errbuf[256];
968
969 /* Check if recursive dump; if so, punt. */
970 if (wddoingadump)
971 return EFAULT;
972 wddoingadump = 1;
973
974 unit = WDUNIT(dev);
975 if (unit >= wd_cd.cd_ndevs)
976 return ENXIO;
977 wd = wd_cd.cd_devs[unit];
978 if (wd == (struct wd_softc *)0)
979 return ENXIO;
980
981 part = WDPART(dev);
982
983 /* Make sure it was initialized. */
984 if (wd->drvp->state < READY)
985 return ENXIO;
986
987 /* Convert to disk sectors. Request must be a multiple of size. */
988 lp = wd->sc_dk.dk_label;
989 if ((size % lp->d_secsize) != 0)
990 return EFAULT;
991 nblks = size / lp->d_secsize;
992 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
993
994 /* Check transfer bounds against partition size. */
995 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
996 return EINVAL;
997
998 /* Offset block number to start of partition. */
999 blkno += lp->d_partitions[part].p_offset;
1000
1001 /* Recalibrate, if first dump transfer. */
1002 if (wddumprecalibrated == 0) {
1003 wddumpmulti = wd->sc_multi;
1004 wddumprecalibrated = 1;
1005 wd->drvp->state = RECAL;
1006 }
1007
1008 while (nblks > 0) {
1009 again:
1010 wd->sc_wdc_bio.blkno = blkno;
1011 wd->sc_wdc_bio.flags = ATA_POLL;
1012 if (wddumpmulti == 1)
1013 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1014 if (wd->sc_flags & WDF_LBA)
1015 wd->sc_wdc_bio.flags |= ATA_LBA;
1016 wd->sc_wdc_bio.bcount =
1017 min(nblks, wddumpmulti) * lp->d_secsize;
1018 wd->sc_wdc_bio.databuf = va;
1019 #ifndef WD_DUMP_NOT_TRUSTED
1020 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1021 case WDC_TRY_AGAIN:
1022 panic("wddump: try again");
1023 break;
1024 case WDC_QUEUED:
1025 panic("wddump: polled command has been queued");
1026 break;
1027 case WDC_COMPLETE:
1028 break;
1029 }
1030 switch(wd->sc_wdc_bio.error) {
1031 case TIMEOUT:
1032 printf("wddump: device timed out");
1033 err = EIO;
1034 break;
1035 case ERR_DF:
1036 printf("wddump: drive fault");
1037 err = EIO;
1038 break;
1039 case ERR_DMA:
1040 printf("wddump: DMA error");
1041 err = EIO;
1042 break;
1043 case ERROR:
1044 errbuf[0] = '\0';
1045 print_wderror(wd->sc_wdc_bio.r_error, errbuf);
1046 printf("wddump: %s", errbuf);
1047 err = EIO;
1048 break;
1049 case NOERROR:
1050 err = 0;
1051 break;
1052 default:
1053 panic("wddump: unknown error type");
1054 }
1055 if (err != 0) {
1056 if (wddumpmulti != 1) {
1057 wddumpmulti = 1; /* retry in single-sector */
1058 printf(", retrying\n");
1059 goto again;
1060 }
1061 printf("\n");
1062 return err;
1063 }
1064 #else /* WD_DUMP_NOT_TRUSTED */
1065 /* Let's just talk about this first... */
1066 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1067 unit, va, cylin, head, sector);
1068 delay(500 * 1000); /* half a second */
1069 #endif
1070
1071 /* update block count */
1072 nblks -= min(nblks, wddumpmulti);
1073 blkno += min(nblks, wddumpmulti);
1074 va += min(nblks, wddumpmulti) * lp->d_secsize;
1075 }
1076
1077 wddoingadump = 0;
1078 return 0;
1079 }
1080 #else /* __BDEVSW_DUMP_NEW_TYPE */
1081
1082
1083 int
1084 wddump(dev, blkno, va, size)
1085 dev_t dev;
1086 daddr_t blkno;
1087 caddr_t va;
1088 size_t size;
1089 {
1090
1091 /* Not implemented. */
1092 return ENXIO;
1093 }
1094 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1095
1096 #ifdef HAS_BAD144_HANDLING
1097 /*
1098 * Internalize the bad sector table.
1099 */
1100 void
1101 bad144intern(wd)
1102 struct wd_softc *wd;
1103 {
1104 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1105 struct disklabel *lp = wd->sc_dk.dk_label;
1106 int i = 0;
1107
1108 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1109
1110 for (; i < NBT_BAD; i++) {
1111 if (bt->bt_bad[i].bt_cyl == 0xffff)
1112 break;
1113 wd->sc_badsect[i] =
1114 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1115 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1116 (bt->bt_bad[i].bt_trksec & 0xff);
1117 }
1118 for (; i < NBT_BAD+1; i++)
1119 wd->sc_badsect[i] = -1;
1120 }
1121 #endif
1122
1123 void
1124 print_wderror(errno, buf)
1125 int errno;
1126 char *buf;
1127 {
1128 static char *errstr[] = {"address mark not found", "track 0 not found",
1129 "aborted command", "media change requested", "id not found",
1130 "media changed", "uncorrectable data error", "bad block detected"};
1131 int i;
1132 char *sep = "";
1133
1134 if (errno == 0) {
1135 sprintf(buf, "error not notified");
1136 }
1137
1138 for (i = 0; i < 8; i++) {
1139 if (errno & (1 << i)) {
1140 buf += sprintf(buf, "%s %s", sep, errstr[i]);
1141 sep = ",";
1142 }
1143 }
1144 }
1145
1146 int
1147 wd_get_params(wd, flags, params)
1148 struct wd_softc *wd;
1149 u_int8_t flags;
1150 struct ataparams *params;
1151 {
1152 switch (ata_get_params(wd->drvp, flags, params)) {
1153 case CMD_AGAIN:
1154 return 1;
1155 case CMD_ERR:
1156 /*
1157 * We `know' there's a drive here; just assume it's old.
1158 * This geometry is only used to read the MBR and print a
1159 * (false) attach message.
1160 */
1161 strncpy(params->atap_model, "ST506",
1162 sizeof params->atap_model);
1163 params->atap_config = ATA_CFG_FIXED;
1164 params->atap_cylinders = 1024;
1165 params->atap_heads = 8;
1166 params->atap_sectors = 17;
1167 params->atap_multi = 1;
1168 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1169 return 0;
1170 case CMD_OK:
1171 return 0;
1172 default:
1173 panic("wd_get_params: bad return code from ata_get_params");
1174 /* NOTREACHED */
1175 }
1176 }
1177