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