xref: /netbsd-src/sys/dev/ata/wd.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: wd.c,v 1.371 2009/04/02 00:09:32 dyoung Exp $ */
2 
3 /*
4  * Copyright (c) 1998, 2001 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, 2003, 2004 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  *
48  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58  * POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.371 2009/04/02 00:09:32 dyoung Exp $");
63 
64 #include "opt_ata.h"
65 
66 #include "rnd.h"
67 
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/conf.h>
72 #include <sys/file.h>
73 #include <sys/stat.h>
74 #include <sys/ioctl.h>
75 #include <sys/buf.h>
76 #include <sys/bufq.h>
77 #include <sys/uio.h>
78 #include <sys/malloc.h>
79 #include <sys/device.h>
80 #include <sys/disklabel.h>
81 #include <sys/disk.h>
82 #include <sys/syslog.h>
83 #include <sys/proc.h>
84 #include <sys/reboot.h>
85 #include <sys/vnode.h>
86 #if NRND > 0
87 #include <sys/rnd.h>
88 #endif
89 
90 #include <sys/intr.h>
91 #include <sys/bus.h>
92 
93 #include <dev/ata/atareg.h>
94 #include <dev/ata/atavar.h>
95 #include <dev/ata/wdvar.h>
96 #include <dev/ic/wdcreg.h>
97 #include <sys/ataio.h>
98 #include "locators.h"
99 
100 #include <prop/proplib.h>
101 
102 #define	LBA48_THRESHOLD		(0xfffffff)	/* 128GB / DEV_BSIZE */
103 
104 #define	WDIORETRIES_SINGLE 4	/* number of retries before single-sector */
105 #define	WDIORETRIES	5	/* number of retries before giving up */
106 #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
107 
108 #define	WDUNIT(dev)		DISKUNIT(dev)
109 #define	WDPART(dev)		DISKPART(dev)
110 #define	WDMINOR(unit, part)	DISKMINOR(unit, part)
111 #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
112 
113 #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
114 
115 #define DEBUG_INTR   0x01
116 #define DEBUG_XFERS  0x02
117 #define DEBUG_STATUS 0x04
118 #define DEBUG_FUNCS  0x08
119 #define DEBUG_PROBE  0x10
120 #ifdef ATADEBUG
121 int wdcdebug_wd_mask = 0x0;
122 #define ATADEBUG_PRINT(args, level) \
123 	if (wdcdebug_wd_mask & (level)) \
124 		printf args
125 #else
126 #define ATADEBUG_PRINT(args, level)
127 #endif
128 
129 int	wdprobe(struct device *, struct cfdata *, void *);
130 void	wdattach(struct device *, struct device *, void *);
131 int	wddetach(struct device *, int);
132 int	wdactivate(struct device *, enum devact);
133 int	wdprint(void *, char *);
134 void	wdperror(const struct wd_softc *);
135 
136 static bool	wd_suspend(device_t PMF_FN_PROTO);
137 static int	wd_standby(struct wd_softc *, int);
138 
139 CFATTACH_DECL3_NEW(wd, sizeof(struct wd_softc),
140     wdprobe, wdattach, wddetach, wdactivate, NULL, NULL, DVF_DETACH_SHUTDOWN);
141 
142 extern struct cfdriver wd_cd;
143 
144 dev_type_open(wdopen);
145 dev_type_close(wdclose);
146 dev_type_read(wdread);
147 dev_type_write(wdwrite);
148 dev_type_ioctl(wdioctl);
149 dev_type_strategy(wdstrategy);
150 dev_type_dump(wddump);
151 dev_type_size(wdsize);
152 
153 const struct bdevsw wd_bdevsw = {
154 	wdopen, wdclose, wdstrategy, wdioctl, wddump, wdsize, D_DISK
155 };
156 
157 const struct cdevsw wd_cdevsw = {
158 	wdopen, wdclose, wdread, wdwrite, wdioctl,
159 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
160 };
161 
162 /*
163  * Glue necessary to hook WDCIOCCOMMAND into physio
164  */
165 
166 struct wd_ioctl {
167 	LIST_ENTRY(wd_ioctl) wi_list;
168 	struct buf wi_bp;
169 	struct uio wi_uio;
170 	struct iovec wi_iov;
171 	atareq_t wi_atareq;
172 	struct wd_softc *wi_softc;
173 };
174 
175 LIST_HEAD(, wd_ioctl) wi_head;
176 
177 struct	wd_ioctl *wi_find(struct buf *);
178 void	wi_free(struct wd_ioctl *);
179 struct	wd_ioctl *wi_get(void);
180 void	wdioctlstrategy(struct buf *);
181 
182 void  wdgetdefaultlabel(struct wd_softc *, struct disklabel *);
183 void  wdgetdisklabel(struct wd_softc *);
184 void  wdstart(void *);
185 void  __wdstart(struct wd_softc*, struct buf *);
186 void  wdrestart(void *);
187 void  wddone(void *);
188 int   wd_get_params(struct wd_softc *, u_int8_t, struct ataparams *);
189 int   wd_flushcache(struct wd_softc *, int);
190 bool  wd_shutdown(device_t, int);
191 
192 int   wd_getcache(struct wd_softc *, int *);
193 int   wd_setcache(struct wd_softc *, int);
194 
195 struct dkdriver wddkdriver = { wdstrategy, minphys };
196 
197 #ifdef HAS_BAD144_HANDLING
198 static void bad144intern(struct wd_softc *);
199 #endif
200 
201 #define	WD_QUIRK_SPLIT_MOD15_WRITE	0x0001	/* must split certain writes */
202 #define	WD_QUIRK_FORCE_LBA48		0x0002	/* must use LBA48 commands */
203 
204 #define	WD_QUIRK_FMT "\20\1SPLIT_MOD15_WRITE\2FORCE_LBA48"
205 
206 /*
207  * Quirk table for IDE drives.  Put more-specific matches first, since
208  * a simple globbing routine is used for matching.
209  */
210 static const struct wd_quirk {
211 	const char *wdq_match;		/* inquiry pattern to match */
212 	int wdq_quirks;			/* drive quirks */
213 } wd_quirk_table[] = {
214 	/*
215 	 * Some Seagate S-ATA drives have a PHY which can get confused
216 	 * with the way data is packetized by some S-ATA controllers.
217 	 *
218 	 * The work-around is to split in two any write transfer whose
219 	 * sector count % 15 == 1 (assuming 512 byte sectors).
220 	 *
221 	 * XXX This is an incomplete list.  There are at least a couple
222 	 * XXX more model numbers.  If you have trouble with such transfers
223 	 * XXX (8K is the most common) on Seagate S-ATA drives, please
224 	 * XXX notify thorpej@NetBSD.org.
225 	 */
226 	{ "ST3120023AS",
227 	  WD_QUIRK_SPLIT_MOD15_WRITE },
228 	{ "ST380023AS",
229 	  WD_QUIRK_SPLIT_MOD15_WRITE },
230 
231 	/*
232 	 * These seagate drives seems to have issue addressing sector 0xfffffff
233 	 * (aka LBA48_THRESHOLD) in LBA mode. The workaround is to force
234 	 * LBA48
235 	 * Note that we can't just change the code to always use LBA48 for
236 	 * sector 0xfffffff, because this would break valid and working
237 	 * setups using LBA48 drives on non-LBA48-capable controllers
238 	 * (and it's hard to get a list of such controllers)
239 	 */
240 	{ "ST3160021A*",
241 	  WD_QUIRK_FORCE_LBA48 },
242 	{ "ST3160811A*",
243 	  WD_QUIRK_FORCE_LBA48 },
244 	{ "ST3160812A*",
245 	  WD_QUIRK_FORCE_LBA48 },
246 	{ "ST3160023A*",
247 	  WD_QUIRK_FORCE_LBA48 },
248 	{ "ST3160827A*",
249 	  WD_QUIRK_FORCE_LBA48 },
250 	/* Attempt to catch all seagate drives larger than 200GB */
251 	{ "ST3[2-9][0-9][0-9][0-9][0-9][0-9][A-Z]*",
252 	  WD_QUIRK_FORCE_LBA48 },
253 	{ "ST3[1-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]*",
254 	  WD_QUIRK_FORCE_LBA48 },
255 	{ NULL,
256 	  0 }
257 };
258 
259 static const struct wd_quirk *
260 wd_lookup_quirks(const char *name)
261 {
262 	const struct wd_quirk *wdq;
263 	const char *estr;
264 
265 	for (wdq = wd_quirk_table; wdq->wdq_match != NULL; wdq++) {
266 		/*
267 		 * We only want exact matches (which include matches
268 		 * against globbing characters).
269 		 */
270 		if (pmatch(name, wdq->wdq_match, &estr) == 2)
271 			return (wdq);
272 	}
273 	return (NULL);
274 }
275 
276 int
277 wdprobe(struct device *parent, struct cfdata *match, void *aux)
278 {
279 	struct ata_device *adev = aux;
280 
281 	if (adev == NULL)
282 		return 0;
283 	if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA)
284 		return 0;
285 
286 	if (match->cf_loc[ATA_HLCF_DRIVE] != ATA_HLCF_DRIVE_DEFAULT &&
287 	    match->cf_loc[ATA_HLCF_DRIVE] != adev->adev_drv_data->drive)
288 		return 0;
289 	return 1;
290 }
291 
292 void
293 wdattach(struct device *parent, struct device *self, void *aux)
294 {
295 	struct wd_softc *wd = device_private(self);
296 	struct ata_device *adev= aux;
297 	int i, blank;
298 	char tbuf[41], pbuf[9], c, *p, *q;
299 	const struct wd_quirk *wdq;
300 
301 	wd->sc_dev = self;
302 
303 	ATADEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
304 	callout_init(&wd->sc_restart_ch, 0);
305 	bufq_alloc(&wd->sc_q, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
306 #ifdef WD_SOFTBADSECT
307 	SLIST_INIT(&wd->sc_bslist);
308 #endif
309 	wd->atabus = adev->adev_bustype;
310 	wd->openings = adev->adev_openings;
311 	wd->drvp = adev->adev_drv_data;
312 
313 	wd->drvp->drv_done = wddone;
314 	wd->drvp->drv_softc = wd->sc_dev;
315 
316 	aprint_naive("\n");
317 
318 	/* read our drive info */
319 	if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) {
320 		aprint_error("\n%s: IDENTIFY failed\n", device_xname(self));
321 		return;
322 	}
323 
324 	for (blank = 0, p = wd->sc_params.atap_model, q = tbuf, i = 0;
325 	    i < sizeof(wd->sc_params.atap_model); i++) {
326 		c = *p++;
327 		if (c == '\0')
328 			break;
329 		if (c != ' ') {
330 			if (blank) {
331 				*q++ = ' ';
332 				blank = 0;
333 			}
334 			*q++ = c;
335 		} else
336 			blank = 1;
337 	}
338 	*q++ = '\0';
339 
340 	aprint_normal(": <%s>\n", tbuf);
341 
342 	wdq = wd_lookup_quirks(tbuf);
343 	if (wdq != NULL)
344 		wd->sc_quirks = wdq->wdq_quirks;
345 
346 	if (wd->sc_quirks != 0) {
347 		char sbuf[sizeof(WD_QUIRK_FMT) + 64];
348 		snprintb(sbuf, sizeof(sbuf), WD_QUIRK_FMT, wd->sc_quirks);
349 		aprint_normal_dev(self, "quirks %s\n", sbuf);
350 	}
351 
352 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
353 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
354 	} else {
355 		wd->sc_multi = 1;
356 	}
357 
358 	aprint_verbose_dev(self, "drive supports %d-sector PIO transfers,",
359 	    wd->sc_multi);
360 
361 	/* 48-bit LBA addressing */
362 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0)
363 		wd->sc_flags |= WDF_LBA48;
364 
365 	/* Prior to ATA-4, LBA was optional. */
366 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
367 		wd->sc_flags |= WDF_LBA;
368 #if 0
369 	/* ATA-4 requires LBA. */
370 	if (wd->sc_params.atap_ataversion != 0xffff &&
371 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
372 		wd->sc_flags |= WDF_LBA;
373 #endif
374 
375 	if ((wd->sc_flags & WDF_LBA48) != 0) {
376 		aprint_verbose(" LBA48 addressing\n");
377 		wd->sc_capacity =
378 		    ((u_int64_t) wd->sc_params.__reserved6[11] << 48) |
379 		    ((u_int64_t) wd->sc_params.__reserved6[10] << 32) |
380 		    ((u_int64_t) wd->sc_params.__reserved6[9]  << 16) |
381 		    ((u_int64_t) wd->sc_params.__reserved6[8]  << 0);
382 	} else if ((wd->sc_flags & WDF_LBA) != 0) {
383 		aprint_verbose(" LBA addressing\n");
384 		wd->sc_capacity =
385 		    ((u_int64_t)wd->sc_params.atap_capacity[1] << 16) |
386 		    wd->sc_params.atap_capacity[0];
387 	} else {
388 		aprint_verbose(" chs addressing\n");
389 		wd->sc_capacity =
390 		    wd->sc_params.atap_cylinders *
391 		    wd->sc_params.atap_heads *
392 		    wd->sc_params.atap_sectors;
393 	}
394 	format_bytes(pbuf, sizeof(pbuf), wd->sc_capacity * DEV_BSIZE);
395 	aprint_normal_dev(self, "%s, %d cyl, %d head, %d sec, "
396 	    "%d bytes/sect x %llu sectors\n",
397 	    pbuf,
398 	    (wd->sc_flags & WDF_LBA) ? (int)(wd->sc_capacity /
399 		(wd->sc_params.atap_heads * wd->sc_params.atap_sectors)) :
400 		wd->sc_params.atap_cylinders,
401 	    wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
402 	    DEV_BSIZE, (unsigned long long)wd->sc_capacity);
403 
404 	ATADEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
405 	    device_xname(self), wd->sc_params.atap_dmatiming_mimi,
406 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
407 	/*
408 	 * Initialize and attach the disk structure.
409 	 */
410 	/* we fill in dk_info later */
411 	disk_init(&wd->sc_dk, device_xname(wd->sc_dev), &wddkdriver);
412 	disk_attach(&wd->sc_dk);
413 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
414 #if NRND > 0
415 	rnd_attach_source(&wd->rnd_source, device_xname(wd->sc_dev),
416 			  RND_TYPE_DISK, 0);
417 #endif
418 
419 	/* Discover wedges on this disk. */
420 	dkwedge_discover(&wd->sc_dk);
421 
422 	if (!pmf_device_register1(self, wd_suspend, NULL, wd_shutdown))
423 		aprint_error_dev(self, "couldn't establish power handler\n");
424 }
425 
426 static bool
427 wd_suspend(device_t dv PMF_FN_ARGS)
428 {
429 	struct wd_softc *sc = device_private(dv);
430 
431 	wd_flushcache(sc, AT_WAIT);
432 	wd_standby(sc, AT_WAIT);
433 	return true;
434 }
435 
436 int
437 wdactivate(struct device *self, enum devact act)
438 {
439 	int rv = 0;
440 
441 	switch (act) {
442 	case DVACT_ACTIVATE:
443 		rv = EOPNOTSUPP;
444 		break;
445 
446 	case DVACT_DEACTIVATE:
447 		/*
448 		 * Nothing to do; we key off the device's DVF_ACTIVATE.
449 		 */
450 		break;
451 	}
452 	return (rv);
453 }
454 
455 int
456 wddetach(struct device *self, int flags)
457 {
458 	struct wd_softc *sc = device_private(self);
459 	int s, bmaj, cmaj, i, mn;
460 
461 	/* locate the major number */
462 	bmaj = bdevsw_lookup_major(&wd_bdevsw);
463 	cmaj = cdevsw_lookup_major(&wd_cdevsw);
464 
465 	/* Nuke the vnodes for any open instances. */
466 	for (i = 0; i < MAXPARTITIONS; i++) {
467 		mn = WDMINOR(device_unit(self), i);
468 		vdevgone(bmaj, mn, mn, VBLK);
469 		vdevgone(cmaj, mn, mn, VCHR);
470 	}
471 
472 	/* Delete all of our wedges. */
473 	dkwedge_delall(&sc->sc_dk);
474 
475 	s = splbio();
476 
477 	/* Kill off any queued buffers. */
478 	bufq_drain(sc->sc_q);
479 
480 	bufq_free(sc->sc_q);
481 	sc->atabus->ata_killpending(sc->drvp);
482 
483 	splx(s);
484 
485 	/* Detach disk. */
486 	disk_detach(&sc->sc_dk);
487 	disk_destroy(&sc->sc_dk);
488 
489 #ifdef WD_SOFTBADSECT
490 	/* Clean out the bad sector list */
491 	while (!SLIST_EMPTY(&sc->sc_bslist)) {
492 		void *head = SLIST_FIRST(&sc->sc_bslist);
493 		SLIST_REMOVE_HEAD(&sc->sc_bslist, dbs_next);
494 		free(head, M_TEMP);
495 	}
496 	sc->sc_bscount = 0;
497 #endif
498 
499 	pmf_device_deregister(self);
500 
501 #if NRND > 0
502 	/* Unhook the entropy source. */
503 	rnd_detach_source(&sc->rnd_source);
504 #endif
505 
506 	callout_destroy(&sc->sc_restart_ch);
507 
508 	sc->drvp->drive_flags = 0; /* no drive any more here */
509 
510 	return (0);
511 }
512 
513 /*
514  * Read/write routine for a buffer.  Validates the arguments and schedules the
515  * transfer.  Does not wait for the transfer to complete.
516  */
517 void
518 wdstrategy(struct buf *bp)
519 {
520 	struct wd_softc *wd =
521 	    device_lookup_private(&wd_cd, WDUNIT(bp->b_dev));
522 	struct disklabel *lp = wd->sc_dk.dk_label;
523 	daddr_t blkno;
524 	int s;
525 
526 	ATADEBUG_PRINT(("wdstrategy (%s)\n", device_xname(wd->sc_dev)),
527 	    DEBUG_XFERS);
528 
529 	/* Valid request?  */
530 	if (bp->b_blkno < 0 ||
531 	    (bp->b_bcount % lp->d_secsize) != 0 ||
532 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
533 		bp->b_error = EINVAL;
534 		goto done;
535 	}
536 
537 	/* If device invalidated (e.g. media change, door open), error. */
538 	if ((wd->sc_flags & WDF_LOADED) == 0) {
539 		bp->b_error = EIO;
540 		goto done;
541 	}
542 
543 	/* If it's a null transfer, return immediately. */
544 	if (bp->b_bcount == 0)
545 		goto done;
546 
547 	/*
548 	 * Do bounds checking, adjust transfer. if error, process.
549 	 * If end of partition, just return.
550 	 */
551 	if (WDPART(bp->b_dev) == RAW_PART) {
552 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
553 		    wd->sc_capacity) <= 0)
554 			goto done;
555 	} else {
556 		if (bounds_check_with_label(&wd->sc_dk, bp,
557 		    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
558 			goto done;
559 	}
560 
561 	/*
562 	 * Now convert the block number to absolute and put it in
563 	 * terms of the device's logical block size.
564 	 */
565 	if (lp->d_secsize >= DEV_BSIZE)
566 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
567 	else
568 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
569 
570 	if (WDPART(bp->b_dev) != RAW_PART)
571 		blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
572 
573 	bp->b_rawblkno = blkno;
574 
575 #ifdef WD_SOFTBADSECT
576 	/*
577 	 * If the transfer about to be attempted contains only a block that
578 	 * is known to be bad then return an error for the transfer without
579 	 * even attempting to start a transfer up under the premis that we
580 	 * will just end up doing more retries for a transfer that will end
581 	 * up failing again.
582 	 * XXX:SMP - mutex required to protect with DIOCBSFLUSH
583 	 */
584 	if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) {
585 		struct disk_badsectors *dbs;
586 		daddr_t maxblk = blkno + (bp->b_bcount >> DEV_BSHIFT) - 1;
587 
588 		SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next)
589 			if ((dbs->dbs_min <= blkno && blkno <= dbs->dbs_max) ||
590 			    (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){
591 				bp->b_error = EIO;
592 				goto done;
593 			}
594 	}
595 #endif
596 
597 	/* Queue transfer on drive, activate drive and controller if idle. */
598 	s = splbio();
599 	bufq_put(wd->sc_q, bp);
600 	wdstart(wd);
601 	splx(s);
602 	return;
603 done:
604 	/* Toss transfer; we're done early. */
605 	bp->b_resid = bp->b_bcount;
606 	biodone(bp);
607 }
608 
609 /*
610  * Queue a drive for I/O.
611  */
612 void
613 wdstart(void *arg)
614 {
615 	struct wd_softc *wd = arg;
616 	struct buf *bp = NULL;
617 
618 	ATADEBUG_PRINT(("wdstart %s\n", device_xname(wd->sc_dev)),
619 	    DEBUG_XFERS);
620 	while (wd->openings > 0) {
621 
622 		/* Is there a buf for us ? */
623 		if ((bp = bufq_get(wd->sc_q)) == NULL)
624 			return;
625 
626 		/*
627 		 * Make the command. First lock the device
628 		 */
629 		wd->openings--;
630 
631 		wd->retries = 0;
632 		__wdstart(wd, bp);
633 	}
634 }
635 
636 static void
637 wd_split_mod15_write(struct buf *bp)
638 {
639 	struct buf *obp = bp->b_private;
640 	struct wd_softc *sc =
641 	    device_lookup_private(&wd_cd, DISKUNIT(obp->b_dev));
642 
643 	if (__predict_false(bp->b_error != 0)) {
644 		/*
645 		 * Propagate the error.  If this was the first half of
646 		 * the original transfer, make sure to account for that
647 		 * in the residual.
648 		 */
649 		if (bp->b_data == obp->b_data)
650 			bp->b_resid += bp->b_bcount;
651 		goto done;
652 	}
653 
654 	/*
655 	 * If this was the second half of the transfer, we're all done!
656 	 */
657 	if (bp->b_data != obp->b_data)
658 		goto done;
659 
660 	/*
661 	 * Advance the pointer to the second half and issue that command
662 	 * using the same opening.
663 	 */
664 	bp->b_flags = obp->b_flags;
665 	bp->b_oflags = obp->b_oflags;
666 	bp->b_cflags = obp->b_cflags;
667 	bp->b_data = (char *)bp->b_data + bp->b_bcount;
668 	bp->b_blkno += (bp->b_bcount / 512);
669 	bp->b_rawblkno += (bp->b_bcount / 512);
670 	__wdstart(sc, bp);
671 	return;
672 
673  done:
674 	obp->b_error = bp->b_error;
675 	obp->b_resid = bp->b_resid;
676 	putiobuf(bp);
677 	biodone(obp);
678 	sc->openings++;
679 	/* wddone() will call wdstart() */
680 }
681 
682 void
683 __wdstart(struct wd_softc *wd, struct buf *bp)
684 {
685 
686 	/*
687 	 * Deal with the "split mod15 write" quirk.  We just divide the
688 	 * transfer in two, doing the first half and then then second half
689 	 * with the same command opening.
690 	 *
691 	 * Note we MUST do this here, because we can't let insertion
692 	 * into the bufq cause the transfers to be re-merged.
693 	 */
694 	if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 &&
695 			    (bp->b_flags & B_READ) == 0 &&
696 			    bp->b_bcount > 512 &&
697 			    ((bp->b_bcount / 512) % 15) == 1)) {
698 		struct buf *nbp;
699 
700 		/* already at splbio */
701 		nbp = getiobuf(NULL, false);
702 		if (__predict_false(nbp == NULL)) {
703 			/* No memory -- fail the iop. */
704 			bp->b_error = ENOMEM;
705 			bp->b_resid = bp->b_bcount;
706 			biodone(bp);
707 			wd->openings++;
708 			return;
709 		}
710 
711 		nbp->b_error = 0;
712 		nbp->b_proc = bp->b_proc;
713 		nbp->b_dev = bp->b_dev;
714 
715 		nbp->b_bcount = bp->b_bcount / 2;
716 		nbp->b_bufsize = bp->b_bcount / 2;
717 		nbp->b_data = bp->b_data;
718 
719 		nbp->b_blkno = bp->b_blkno;
720 		nbp->b_rawblkno = bp->b_rawblkno;
721 
722 		nbp->b_flags = bp->b_flags;
723 		nbp->b_oflags = bp->b_oflags;
724 		nbp->b_cflags = bp->b_cflags;
725 		nbp->b_iodone = wd_split_mod15_write;
726 
727 		/* Put ptr to orig buf in b_private and use new buf */
728 		nbp->b_private = bp;
729 
730 		BIO_COPYPRIO(nbp, bp);
731 
732 		bp = nbp;
733 	}
734 
735 	wd->sc_wdc_bio.blkno = bp->b_rawblkno;
736 	wd->sc_wdc_bio.blkdone =0;
737 	wd->sc_bp = bp;
738 	/*
739 	 * If we're retrying, retry in single-sector mode. This will give us
740 	 * the sector number of the problem, and will eventually allow the
741 	 * transfer to succeed.
742 	 */
743 	if (wd->retries >= WDIORETRIES_SINGLE)
744 		wd->sc_wdc_bio.flags = ATA_SINGLE;
745 	else
746 		wd->sc_wdc_bio.flags = 0;
747 	if (wd->sc_flags & WDF_LBA48 &&
748 	    (wd->sc_wdc_bio.blkno > LBA48_THRESHOLD ||
749 	    (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
750 		wd->sc_wdc_bio.flags |= ATA_LBA48;
751 	if (wd->sc_flags & WDF_LBA)
752 		wd->sc_wdc_bio.flags |= ATA_LBA;
753 	if (bp->b_flags & B_READ)
754 		wd->sc_wdc_bio.flags |= ATA_READ;
755 	wd->sc_wdc_bio.bcount = bp->b_bcount;
756 	wd->sc_wdc_bio.databuf = bp->b_data;
757 	/* Instrumentation. */
758 	disk_busy(&wd->sc_dk);
759 	switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
760 	case ATACMD_TRY_AGAIN:
761 		callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
762 		break;
763 	case ATACMD_QUEUED:
764 	case ATACMD_COMPLETE:
765 		break;
766 	default:
767 		panic("__wdstart: bad return code from ata_bio()");
768 	}
769 }
770 
771 void
772 wddone(void *v)
773 {
774 	struct wd_softc *wd = device_private(v);
775 	struct buf *bp = wd->sc_bp;
776 	const char *errmsg;
777 	int do_perror = 0;
778 	int nblks;
779 
780 	ATADEBUG_PRINT(("wddone %s\n", device_xname(wd->sc_dev)),
781 	    DEBUG_XFERS);
782 	if (bp == NULL)
783 		return;
784 	bp->b_resid = wd->sc_wdc_bio.bcount;
785 	switch (wd->sc_wdc_bio.error) {
786 	case ERR_DMA:
787 		errmsg = "DMA error";
788 		goto retry;
789 	case ERR_DF:
790 		errmsg = "device fault";
791 		goto retry;
792 	case TIMEOUT:
793 		errmsg = "device timeout";
794 		goto retry;
795 	case ERR_RESET:
796 		errmsg = "channel reset";
797 		goto retry2;
798 	case ERROR:
799 		/* Don't care about media change bits */
800 		if (wd->sc_wdc_bio.r_error != 0 &&
801 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
802 			goto noerror;
803 		errmsg = "error";
804 		do_perror = 1;
805 		if ((wd->sc_wdc_bio.r_error & (WDCE_IDNF | WDCE_ABRT)) &&
806 		    (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) == 0) {
807 			nblks = wd->sc_wdc_bio.bcount /
808 			    wd->sc_dk.dk_label->d_secsize;
809 			/*
810 			 * If we get a "id not found" when crossing the
811 			 * LBA48_THRESHOLD, and the drive is larger than
812 			 * 128GB, then we can assume the drive has the
813 			 * LBA48 bug and we switch to LBA48.
814 			 */
815 			if (wd->sc_wdc_bio.blkno <= LBA48_THRESHOLD &&
816 			    wd->sc_wdc_bio.blkno + nblks > LBA48_THRESHOLD &&
817 			    wd->sc_capacity > LBA48_THRESHOLD + 1) {
818 				errmsg = "LBA48 bug";
819 				wd->sc_quirks |= WD_QUIRK_FORCE_LBA48;
820 				do_perror = 0;
821 				goto retry2;
822 			}
823 		}
824 retry:		/* Just reset and retry. Can we do more ? */
825 		(*wd->atabus->ata_reset_drive)(wd->drvp, AT_RST_NOCMD);
826 retry2:
827 		diskerr(bp, "wd", errmsg, LOG_PRINTF,
828 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
829 		if (wd->retries < WDIORETRIES)
830 			printf(", retrying");
831 		printf("\n");
832 		if (do_perror)
833 			wdperror(wd);
834 		if (wd->retries < WDIORETRIES) {
835 			wd->retries++;
836 			callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
837 			    wdrestart, wd);
838 			return;
839 		}
840 
841 #ifdef WD_SOFTBADSECT
842 		/*
843 		 * Not all errors indicate a failed block but those that do,
844 		 * put the block on the bad-block list for the device.  Only
845 		 * do this for reads because the drive should do it for writes,
846 		 * itself, according to Manuel.
847 		 */
848 		if ((bp->b_flags & B_READ) &&
849 		    ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) ||
850 	     	     (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) {
851 			struct disk_badsectors *dbs;
852 
853 			dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK);
854 			dbs->dbs_min = bp->b_rawblkno;
855 			dbs->dbs_max = dbs->dbs_min + (bp->b_bcount >> DEV_BSHIFT) - 1;
856 			microtime(&dbs->dbs_failedat);
857 			SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
858 			wd->sc_bscount++;
859 		}
860 #endif
861 		bp->b_error = EIO;
862 		break;
863 	case NOERROR:
864 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
865 			aprint_error_dev(wd->sc_dev,
866 			    "soft error (corrected)\n");
867 		break;
868 	case ERR_NODEV:
869 		bp->b_error = EIO;
870 		break;
871 	}
872 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
873 	    (bp->b_flags & B_READ));
874 #if NRND > 0
875 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
876 #endif
877 	/* XXX Yuck, but we don't want to increment openings in this case */
878 	if (__predict_false(bp->b_iodone == wd_split_mod15_write))
879 		biodone(bp);
880 	else {
881 		biodone(bp);
882 		wd->openings++;
883 	}
884 	wdstart(wd);
885 }
886 
887 void
888 wdrestart(void *v)
889 {
890 	struct wd_softc *wd = v;
891 	struct buf *bp = wd->sc_bp;
892 	int s;
893 
894 	ATADEBUG_PRINT(("wdrestart %s\n", device_xname(wd->sc_dev)),
895 	    DEBUG_XFERS);
896 	s = splbio();
897 	__wdstart(v, bp);
898 	splx(s);
899 }
900 
901 int
902 wdread(dev_t dev, struct uio *uio, int flags)
903 {
904 
905 	ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
906 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
907 }
908 
909 int
910 wdwrite(dev_t dev, struct uio *uio, int flags)
911 {
912 
913 	ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
914 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
915 }
916 
917 int
918 wdopen(dev_t dev, int flag, int fmt, struct lwp *l)
919 {
920 	struct wd_softc *wd;
921 	int part, error;
922 
923 	ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
924 	wd = device_lookup_private(&wd_cd, WDUNIT(dev));
925 	if (wd == NULL)
926 		return (ENXIO);
927 
928 	if (! device_is_active(wd->sc_dev))
929 		return (ENODEV);
930 
931 	part = WDPART(dev);
932 
933 	mutex_enter(&wd->sc_dk.dk_openlock);
934 
935 	/*
936 	 * If there are wedges, and this is not RAW_PART, then we
937 	 * need to fail.
938 	 */
939 	if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
940 		error = EBUSY;
941 		goto bad1;
942 	}
943 
944 	/*
945 	 * If this is the first open of this device, add a reference
946 	 * to the adapter.
947 	 */
948 	if (wd->sc_dk.dk_openmask == 0 &&
949 	    (error = wd->atabus->ata_addref(wd->drvp)) != 0)
950 		goto bad1;
951 
952 	if (wd->sc_dk.dk_openmask != 0) {
953 		/*
954 		 * If any partition is open, but the disk has been invalidated,
955 		 * disallow further opens.
956 		 */
957 		if ((wd->sc_flags & WDF_LOADED) == 0) {
958 			error = EIO;
959 			goto bad2;
960 		}
961 	} else {
962 		if ((wd->sc_flags & WDF_LOADED) == 0) {
963 			wd->sc_flags |= WDF_LOADED;
964 
965 			/* Load the physical device parameters. */
966 			wd_get_params(wd, AT_WAIT, &wd->sc_params);
967 
968 			/* Load the partition info if not already loaded. */
969 			wdgetdisklabel(wd);
970 		}
971 	}
972 
973 	/* Check that the partition exists. */
974 	if (part != RAW_PART &&
975 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
976 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
977 		error = ENXIO;
978 		goto bad2;
979 	}
980 
981 	/* Insure only one open at a time. */
982 	switch (fmt) {
983 	case S_IFCHR:
984 		wd->sc_dk.dk_copenmask |= (1 << part);
985 		break;
986 	case S_IFBLK:
987 		wd->sc_dk.dk_bopenmask |= (1 << part);
988 		break;
989 	}
990 	wd->sc_dk.dk_openmask =
991 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
992 
993 	mutex_exit(&wd->sc_dk.dk_openlock);
994 	return 0;
995 
996  bad2:
997 	if (wd->sc_dk.dk_openmask == 0)
998 		wd->atabus->ata_delref(wd->drvp);
999  bad1:
1000 	mutex_exit(&wd->sc_dk.dk_openlock);
1001 	return error;
1002 }
1003 
1004 int
1005 wdclose(dev_t dev, int flag, int fmt, struct lwp *l)
1006 {
1007 	struct wd_softc *wd =
1008 	    device_lookup_private(&wd_cd, WDUNIT(dev));
1009 	int part = WDPART(dev);
1010 
1011 	ATADEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
1012 
1013 	mutex_enter(&wd->sc_dk.dk_openlock);
1014 
1015 	switch (fmt) {
1016 	case S_IFCHR:
1017 		wd->sc_dk.dk_copenmask &= ~(1 << part);
1018 		break;
1019 	case S_IFBLK:
1020 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
1021 		break;
1022 	}
1023 	wd->sc_dk.dk_openmask =
1024 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1025 
1026 	if (wd->sc_dk.dk_openmask == 0) {
1027 		wd_flushcache(wd, AT_WAIT);
1028 
1029 		if (! (wd->sc_flags & WDF_KLABEL))
1030 			wd->sc_flags &= ~WDF_LOADED;
1031 
1032 		wd->atabus->ata_delref(wd->drvp);
1033 	}
1034 
1035 	mutex_exit(&wd->sc_dk.dk_openlock);
1036 	return 0;
1037 }
1038 
1039 void
1040 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
1041 {
1042 
1043 	ATADEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
1044 	memset(lp, 0, sizeof(struct disklabel));
1045 
1046 	lp->d_secsize = DEV_BSIZE;
1047 	lp->d_ntracks = wd->sc_params.atap_heads;
1048 	lp->d_nsectors = wd->sc_params.atap_sectors;
1049 	lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity /
1050 		(wd->sc_params.atap_heads * wd->sc_params.atap_sectors) :
1051 		wd->sc_params.atap_cylinders;
1052 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1053 
1054 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
1055 		lp->d_type = DTYPE_ST506;
1056 	else
1057 		lp->d_type = DTYPE_ESDI;
1058 
1059 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
1060 	strncpy(lp->d_packname, "fictitious", 16);
1061 	if (wd->sc_capacity > UINT32_MAX)
1062 		lp->d_secperunit = UINT32_MAX;
1063 	else
1064 		lp->d_secperunit = wd->sc_capacity;
1065 	lp->d_rpm = 3600;
1066 	lp->d_interleave = 1;
1067 	lp->d_flags = 0;
1068 
1069 	lp->d_partitions[RAW_PART].p_offset = 0;
1070 	lp->d_partitions[RAW_PART].p_size =
1071 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1072 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1073 	lp->d_npartitions = RAW_PART + 1;
1074 
1075 	lp->d_magic = DISKMAGIC;
1076 	lp->d_magic2 = DISKMAGIC;
1077 	lp->d_checksum = dkcksum(lp);
1078 }
1079 
1080 /*
1081  * Fabricate a default disk label, and try to read the correct one.
1082  */
1083 void
1084 wdgetdisklabel(struct wd_softc *wd)
1085 {
1086 	struct disklabel *lp = wd->sc_dk.dk_label;
1087 	const char *errstring;
1088 	int s;
1089 
1090 	ATADEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
1091 
1092 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1093 
1094 	wdgetdefaultlabel(wd, lp);
1095 
1096 	wd->sc_badsect[0] = -1;
1097 
1098 	if (wd->drvp->state > RESET) {
1099 		s = splbio();
1100 		wd->drvp->drive_flags |= DRIVE_RESET;
1101 		splx(s);
1102 	}
1103 	errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev),
1104 				  RAW_PART), wdstrategy, lp,
1105 				  wd->sc_dk.dk_cpulabel);
1106 	if (errstring) {
1107 		/*
1108 		 * This probably happened because the drive's default
1109 		 * geometry doesn't match the DOS geometry.  We
1110 		 * assume the DOS geometry is now in the label and try
1111 		 * again.  XXX This is a kluge.
1112 		 */
1113 		if (wd->drvp->state > RESET) {
1114 			s = splbio();
1115 			wd->drvp->drive_flags |= DRIVE_RESET;
1116 			splx(s);
1117 		}
1118 		errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev),
1119 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1120 	}
1121 	if (errstring) {
1122 		aprint_error_dev(wd->sc_dev, "%s\n", errstring);
1123 		return;
1124 	}
1125 
1126 	if (wd->drvp->state > RESET) {
1127 		s = splbio();
1128 		wd->drvp->drive_flags |= DRIVE_RESET;
1129 		splx(s);
1130 	}
1131 #ifdef HAS_BAD144_HANDLING
1132 	if ((lp->d_flags & D_BADSECT) != 0)
1133 		bad144intern(wd);
1134 #endif
1135 }
1136 
1137 void
1138 wdperror(const struct wd_softc *wd)
1139 {
1140 	static const char *const errstr0_3[] = {"address mark not found",
1141 	    "track 0 not found", "aborted command", "media change requested",
1142 	    "id not found", "media changed", "uncorrectable data error",
1143 	    "bad block detected"};
1144 	static const char *const errstr4_5[] = {
1145 	    "obsolete (address mark not found)",
1146 	    "no media/write protected", "aborted command",
1147 	    "media change requested", "id not found", "media changed",
1148 	    "uncorrectable data error", "interface CRC error"};
1149 	const char *const *errstr;
1150 	int i;
1151 	const char *sep = "";
1152 
1153 	const char *devname = device_xname(wd->sc_dev);
1154 	struct ata_drive_datas *drvp = wd->drvp;
1155 	int errno = wd->sc_wdc_bio.r_error;
1156 
1157 	if (drvp->ata_vers >= 4)
1158 		errstr = errstr4_5;
1159 	else
1160 		errstr = errstr0_3;
1161 
1162 	printf("%s: (", devname);
1163 
1164 	if (errno == 0)
1165 		printf("error not notified");
1166 
1167 	for (i = 0; i < 8; i++) {
1168 		if (errno & (1 << i)) {
1169 			printf("%s%s", sep, errstr[i]);
1170 			sep = ", ";
1171 		}
1172 	}
1173 	printf(")\n");
1174 }
1175 
1176 int
1177 wdioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
1178 {
1179 	struct wd_softc *wd =
1180 	    device_lookup_private(&wd_cd, WDUNIT(dev));
1181 	int error = 0, s;
1182 #ifdef __HAVE_OLD_DISKLABEL
1183 	struct disklabel *newlabel = NULL;
1184 #endif
1185 
1186 	ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1187 
1188 	if ((wd->sc_flags & WDF_LOADED) == 0)
1189 		return EIO;
1190 
1191 	error = disk_ioctl(&wd->sc_dk, xfer, addr, flag, l);
1192 	if (error != EPASSTHROUGH)
1193 		return (error);
1194 
1195 	switch (xfer) {
1196 #ifdef HAS_BAD144_HANDLING
1197 	case DIOCSBAD:
1198 		if ((flag & FWRITE) == 0)
1199 			return EBADF;
1200 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1201 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1202 		bad144intern(wd);
1203 		return 0;
1204 #endif
1205 #ifdef WD_SOFTBADSECT
1206 	case DIOCBSLIST :
1207 	{
1208 		u_int32_t count, missing, skip;
1209 		struct disk_badsecinfo dbsi;
1210 		struct disk_badsectors *dbs;
1211 		size_t available;
1212 		uint8_t *laddr;
1213 
1214 		dbsi = *(struct disk_badsecinfo *)addr;
1215 		missing = wd->sc_bscount;
1216 		count = 0;
1217 		available = dbsi.dbsi_bufsize;
1218 		skip = dbsi.dbsi_skip;
1219 		laddr = (uint8_t *)dbsi.dbsi_buffer;
1220 
1221 		/*
1222 		 * We start this loop with the expectation that all of the
1223 		 * entries will be missed and decrement this counter each
1224 		 * time we either skip over one (already copied out) or
1225 		 * we actually copy it back to user space.  The structs
1226 		 * holding the bad sector information are copied directly
1227 		 * back to user space whilst the summary is returned via
1228 		 * the struct passed in via the ioctl.
1229 		 */
1230 		SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
1231 			if (skip > 0) {
1232 				missing--;
1233 				skip--;
1234 				continue;
1235 			}
1236 			if (available < sizeof(*dbs))
1237 				break;
1238 			available -= sizeof(*dbs);
1239 			copyout(dbs, laddr, sizeof(*dbs));
1240 			laddr += sizeof(*dbs);
1241 			missing--;
1242 			count++;
1243 		}
1244 		dbsi.dbsi_left = missing;
1245 		dbsi.dbsi_copied = count;
1246 		*(struct disk_badsecinfo *)addr = dbsi;
1247 		return 0;
1248 	}
1249 
1250 	case DIOCBSFLUSH :
1251 		/* Clean out the bad sector list */
1252 		while (!SLIST_EMPTY(&wd->sc_bslist)) {
1253 			void *head = SLIST_FIRST(&wd->sc_bslist);
1254 			SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
1255 			free(head, M_TEMP);
1256 		}
1257 		wd->sc_bscount = 0;
1258 		return 0;
1259 #endif
1260 	case DIOCGDINFO:
1261 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
1262 		return 0;
1263 #ifdef __HAVE_OLD_DISKLABEL
1264 	case ODIOCGDINFO:
1265 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1266 		if (newlabel == NULL)
1267 			return EIO;
1268 		*newlabel = *(wd->sc_dk.dk_label);
1269 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1270 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
1271 		else
1272 			error = ENOTTY;
1273 		free(newlabel, M_TEMP);
1274 		return error;
1275 #endif
1276 
1277 	case DIOCGPART:
1278 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1279 		((struct partinfo *)addr)->part =
1280 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1281 		return 0;
1282 
1283 	case DIOCWDINFO:
1284 	case DIOCSDINFO:
1285 #ifdef __HAVE_OLD_DISKLABEL
1286 	case ODIOCWDINFO:
1287 	case ODIOCSDINFO:
1288 #endif
1289 	{
1290 		struct disklabel *lp;
1291 
1292 		if ((flag & FWRITE) == 0)
1293 			return EBADF;
1294 
1295 #ifdef __HAVE_OLD_DISKLABEL
1296 		if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1297 			newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1298 			if (newlabel == NULL)
1299 				return EIO;
1300 			memset(newlabel, 0, sizeof newlabel);
1301 			memcpy(newlabel, addr, sizeof (struct olddisklabel));
1302 			lp = newlabel;
1303 		} else
1304 #endif
1305 		lp = (struct disklabel *)addr;
1306 
1307 		mutex_enter(&wd->sc_dk.dk_openlock);
1308 		wd->sc_flags |= WDF_LABELLING;
1309 
1310 		error = setdisklabel(wd->sc_dk.dk_label,
1311 		    lp, /*wd->sc_dk.dk_openmask : */0,
1312 		    wd->sc_dk.dk_cpulabel);
1313 		if (error == 0) {
1314 			if (wd->drvp->state > RESET) {
1315 				s = splbio();
1316 				wd->drvp->drive_flags |= DRIVE_RESET;
1317 				splx(s);
1318 			}
1319 			if (xfer == DIOCWDINFO
1320 #ifdef __HAVE_OLD_DISKLABEL
1321 			    || xfer == ODIOCWDINFO
1322 #endif
1323 			    )
1324 				error = writedisklabel(WDLABELDEV(dev),
1325 				    wdstrategy, wd->sc_dk.dk_label,
1326 				    wd->sc_dk.dk_cpulabel);
1327 		}
1328 
1329 		wd->sc_flags &= ~WDF_LABELLING;
1330 		mutex_exit(&wd->sc_dk.dk_openlock);
1331 #ifdef __HAVE_OLD_DISKLABEL
1332 		if (newlabel != NULL)
1333 			free(newlabel, M_TEMP);
1334 #endif
1335 		return error;
1336 	}
1337 
1338 	case DIOCKLABEL:
1339 		if (*(int *)addr)
1340 			wd->sc_flags |= WDF_KLABEL;
1341 		else
1342 			wd->sc_flags &= ~WDF_KLABEL;
1343 		return 0;
1344 
1345 	case DIOCWLABEL:
1346 		if ((flag & FWRITE) == 0)
1347 			return EBADF;
1348 		if (*(int *)addr)
1349 			wd->sc_flags |= WDF_WLABEL;
1350 		else
1351 			wd->sc_flags &= ~WDF_WLABEL;
1352 		return 0;
1353 
1354 	case DIOCGDEFLABEL:
1355 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
1356 		return 0;
1357 #ifdef __HAVE_OLD_DISKLABEL
1358 	case ODIOCGDEFLABEL:
1359 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1360 		if (newlabel == NULL)
1361 			return EIO;
1362 		wdgetdefaultlabel(wd, newlabel);
1363 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1364 			memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1365 		else
1366 			error = ENOTTY;
1367 		free(newlabel, M_TEMP);
1368 		return error;
1369 #endif
1370 
1371 #ifdef notyet
1372 	case DIOCWFORMAT:
1373 		if ((flag & FWRITE) == 0)
1374 			return EBADF;
1375 		{
1376 		register struct format_op *fop;
1377 		struct iovec aiov;
1378 		struct uio auio;
1379 
1380 		fop = (struct format_op *)addr;
1381 		aiov.iov_base = fop->df_buf;
1382 		aiov.iov_len = fop->df_count;
1383 		auio.uio_iov = &aiov;
1384 		auio.uio_iovcnt = 1;
1385 		auio.uio_resid = fop->df_count;
1386 		auio.uio_offset =
1387 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1388 		auio.uio_vmspace = l->l_proc->p_vmspace;
1389 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1390 		    &auio);
1391 		fop->df_count -= auio.uio_resid;
1392 		fop->df_reg[0] = wdc->sc_status;
1393 		fop->df_reg[1] = wdc->sc_error;
1394 		return error;
1395 		}
1396 #endif
1397 	case DIOCGCACHE:
1398 		return wd_getcache(wd, (int *)addr);
1399 
1400 	case DIOCSCACHE:
1401 		return wd_setcache(wd, *(int *)addr);
1402 
1403 	case DIOCCACHESYNC:
1404 		return wd_flushcache(wd, AT_WAIT);
1405 
1406 	case ATAIOCCOMMAND:
1407 		/*
1408 		 * Make sure this command is (relatively) safe first
1409 		 */
1410 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1411 		    (flag & FWRITE) == 0)
1412 			return (EBADF);
1413 		{
1414 		struct wd_ioctl *wi;
1415 		atareq_t *atareq = (atareq_t *) addr;
1416 		int error1;
1417 
1418 		wi = wi_get();
1419 		wi->wi_softc = wd;
1420 		wi->wi_atareq = *atareq;
1421 
1422 		if (atareq->datalen && atareq->flags &
1423 		    (ATACMD_READ | ATACMD_WRITE)) {
1424 			void *tbuf;
1425 			if (atareq->datalen < DEV_BSIZE
1426 			    && atareq->command == WDCC_IDENTIFY) {
1427 				tbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1428 				wi->wi_iov.iov_base = tbuf;
1429 				wi->wi_iov.iov_len = DEV_BSIZE;
1430 				UIO_SETUP_SYSSPACE(&wi->wi_uio);
1431 			} else {
1432 				tbuf = NULL;
1433 				wi->wi_iov.iov_base = atareq->databuf;
1434 				wi->wi_iov.iov_len = atareq->datalen;
1435 				wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace;
1436 			}
1437 			wi->wi_uio.uio_iov = &wi->wi_iov;
1438 			wi->wi_uio.uio_iovcnt = 1;
1439 			wi->wi_uio.uio_resid = atareq->datalen;
1440 			wi->wi_uio.uio_offset = 0;
1441 			wi->wi_uio.uio_rw =
1442 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1443 			error1 = physio(wdioctlstrategy, &wi->wi_bp, dev,
1444 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1445 			    minphys, &wi->wi_uio);
1446 			if (tbuf != NULL && error1 == 0) {
1447 				error1 = copyout(tbuf, atareq->databuf,
1448 				    atareq->datalen);
1449 				free(tbuf, M_TEMP);
1450 			}
1451 		} else {
1452 			/* No need to call physio if we don't have any
1453 			   user data */
1454 			wi->wi_bp.b_flags = 0;
1455 			wi->wi_bp.b_data = 0;
1456 			wi->wi_bp.b_bcount = 0;
1457 			wi->wi_bp.b_dev = 0;
1458 			wi->wi_bp.b_proc = l->l_proc;
1459 			wdioctlstrategy(&wi->wi_bp);
1460 			error1 = wi->wi_bp.b_error;
1461 		}
1462 		*atareq = wi->wi_atareq;
1463 		wi_free(wi);
1464 		return(error1);
1465 		}
1466 
1467 	case DIOCAWEDGE:
1468 	    {
1469 	    	struct dkwedge_info *dkw = (void *) addr;
1470 
1471 		if ((flag & FWRITE) == 0)
1472 			return (EBADF);
1473 
1474 		/* If the ioctl happens here, the parent is us. */
1475 		strcpy(dkw->dkw_parent, device_xname(wd->sc_dev));
1476 		return (dkwedge_add(dkw));
1477 	    }
1478 
1479 	case DIOCDWEDGE:
1480 	    {
1481 	    	struct dkwedge_info *dkw = (void *) addr;
1482 
1483 		if ((flag & FWRITE) == 0)
1484 			return (EBADF);
1485 
1486 		/* If the ioctl happens here, the parent is us. */
1487 		strcpy(dkw->dkw_parent, device_xname(wd->sc_dev));
1488 		return (dkwedge_del(dkw));
1489 	    }
1490 
1491 	case DIOCLWEDGES:
1492 	    {
1493 	    	struct dkwedge_list *dkwl = (void *) addr;
1494 
1495 		return (dkwedge_list(&wd->sc_dk, dkwl, l));
1496 	    }
1497 
1498 	case DIOCGSTRATEGY:
1499 	    {
1500 		struct disk_strategy *dks = (void *)addr;
1501 
1502 		s = splbio();
1503 		strlcpy(dks->dks_name, bufq_getstrategyname(wd->sc_q),
1504 		    sizeof(dks->dks_name));
1505 		splx(s);
1506 		dks->dks_paramlen = 0;
1507 
1508 		return 0;
1509 	    }
1510 
1511 	case DIOCSSTRATEGY:
1512 	    {
1513 		struct disk_strategy *dks = (void *)addr;
1514 		struct bufq_state *new;
1515 		struct bufq_state *old;
1516 
1517 		if ((flag & FWRITE) == 0) {
1518 			return EBADF;
1519 		}
1520 		if (dks->dks_param != NULL) {
1521 			return EINVAL;
1522 		}
1523 		dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
1524 		error = bufq_alloc(&new, dks->dks_name,
1525 		    BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
1526 		if (error) {
1527 			return error;
1528 		}
1529 		s = splbio();
1530 		old = wd->sc_q;
1531 		bufq_move(new, old);
1532 		wd->sc_q = new;
1533 		splx(s);
1534 		bufq_free(old);
1535 
1536 		return 0;
1537 	    }
1538 
1539 	default:
1540 		return ENOTTY;
1541 	}
1542 
1543 #ifdef DIAGNOSTIC
1544 	panic("wdioctl: impossible");
1545 #endif
1546 }
1547 
1548 #ifdef B_FORMAT
1549 int
1550 wdformat(struct buf *bp)
1551 {
1552 
1553 	bp->b_flags |= B_FORMAT;
1554 	return wdstrategy(bp);
1555 }
1556 #endif
1557 
1558 int
1559 wdsize(dev_t dev)
1560 {
1561 	struct wd_softc *wd;
1562 	int part, omask;
1563 	int size;
1564 
1565 	ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1566 
1567 	wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1568 	if (wd == NULL)
1569 		return (-1);
1570 
1571 	part = WDPART(dev);
1572 	omask = wd->sc_dk.dk_openmask & (1 << part);
1573 
1574 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1575 		return (-1);
1576 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1577 		size = -1;
1578 	else
1579 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1580 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1581 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1582 		return (-1);
1583 	return (size);
1584 }
1585 
1586 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1587 static int wddoingadump = 0;
1588 static int wddumprecalibrated = 0;
1589 
1590 /*
1591  * Dump core after a system crash.
1592  */
1593 int
1594 wddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1595 {
1596 	struct wd_softc *wd;	/* disk unit to do the I/O */
1597 	struct disklabel *lp;   /* disk's disklabel */
1598 	int part, err;
1599 	int nblks;	/* total number of sectors left to write */
1600 
1601 	/* Check if recursive dump; if so, punt. */
1602 	if (wddoingadump)
1603 		return EFAULT;
1604 	wddoingadump = 1;
1605 
1606 	wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1607 	if (wd == NULL)
1608 		return (ENXIO);
1609 
1610 	part = WDPART(dev);
1611 
1612 	/* Convert to disk sectors.  Request must be a multiple of size. */
1613 	lp = wd->sc_dk.dk_label;
1614 	if ((size % lp->d_secsize) != 0)
1615 		return EFAULT;
1616 	nblks = size / lp->d_secsize;
1617 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1618 
1619 	/* Check transfer bounds against partition size. */
1620 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1621 		return EINVAL;
1622 
1623 	/* Offset block number to start of partition. */
1624 	blkno += lp->d_partitions[part].p_offset;
1625 
1626 	/* Recalibrate, if first dump transfer. */
1627 	if (wddumprecalibrated == 0) {
1628 		wddumprecalibrated = 1;
1629 		(*wd->atabus->ata_reset_drive)(wd->drvp,
1630 					       AT_POLL | AT_RST_EMERG);
1631 		wd->drvp->state = RESET;
1632 	}
1633 
1634 	wd->sc_bp = NULL;
1635 	wd->sc_wdc_bio.blkno = blkno;
1636 	wd->sc_wdc_bio.flags = ATA_POLL;
1637 	if (wd->sc_flags & WDF_LBA48 &&
1638 	    (blkno > LBA48_THRESHOLD ||
1639     	    (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
1640 		wd->sc_wdc_bio.flags |= ATA_LBA48;
1641 	if (wd->sc_flags & WDF_LBA)
1642 		wd->sc_wdc_bio.flags |= ATA_LBA;
1643 	wd->sc_wdc_bio.bcount = nblks * lp->d_secsize;
1644 	wd->sc_wdc_bio.databuf = va;
1645 #ifndef WD_DUMP_NOT_TRUSTED
1646 	switch (err = wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1647 	case ATACMD_TRY_AGAIN:
1648 		panic("wddump: try again");
1649 		break;
1650 	case ATACMD_QUEUED:
1651 		panic("wddump: polled command has been queued");
1652 		break;
1653 	case ATACMD_COMPLETE:
1654 		break;
1655 	default:
1656 		panic("wddump: unknown atacmd code %d", err);
1657 	}
1658 	switch(err = wd->sc_wdc_bio.error) {
1659 	case TIMEOUT:
1660 		printf("wddump: device timed out");
1661 		err = EIO;
1662 		break;
1663 	case ERR_DF:
1664 		printf("wddump: drive fault");
1665 		err = EIO;
1666 		break;
1667 	case ERR_DMA:
1668 		printf("wddump: DMA error");
1669 		err = EIO;
1670 		break;
1671 	case ERROR:
1672 		printf("wddump: ");
1673 		wdperror(wd);
1674 		err = EIO;
1675 		break;
1676 	case NOERROR:
1677 		err = 0;
1678 		break;
1679 	default:
1680 		panic("wddump: unknown error type %d", err);
1681 	}
1682 	if (err != 0) {
1683 		printf("\n");
1684 		return err;
1685 	}
1686 #else	/* WD_DUMP_NOT_TRUSTED */
1687 	/* Let's just talk about this first... */
1688 	printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1689 	    unit, va, cylin, head, sector);
1690 	delay(500 * 1000);	/* half a second */
1691 #endif
1692 
1693 	wddoingadump = 0;
1694 	return 0;
1695 }
1696 
1697 #ifdef HAS_BAD144_HANDLING
1698 /*
1699  * Internalize the bad sector table.
1700  */
1701 void
1702 bad144intern(struct wd_softc *wd)
1703 {
1704 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1705 	struct disklabel *lp = wd->sc_dk.dk_label;
1706 	int i = 0;
1707 
1708 	ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1709 
1710 	for (; i < NBT_BAD; i++) {
1711 		if (bt->bt_bad[i].bt_cyl == 0xffff)
1712 			break;
1713 		wd->sc_badsect[i] =
1714 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1715 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1716 		    (bt->bt_bad[i].bt_trksec & 0xff);
1717 	}
1718 	for (; i < NBT_BAD+1; i++)
1719 		wd->sc_badsect[i] = -1;
1720 }
1721 #endif
1722 
1723 static void
1724 wd_params_to_properties(struct wd_softc *wd, struct ataparams *params)
1725 {
1726 	prop_dictionary_t disk_info, odisk_info, geom;
1727 	const char *cp;
1728 
1729 	disk_info = prop_dictionary_create();
1730 
1731 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
1732 		cp = "ST506";
1733 	else {
1734 		/* XXX Should have a case for ATA here, too. */
1735 		cp = "ESDI";
1736 	}
1737 	prop_dictionary_set_cstring_nocopy(disk_info, "type", cp);
1738 
1739 	geom = prop_dictionary_create();
1740 
1741 	prop_dictionary_set_uint64(geom, "sectors-per-unit", wd->sc_capacity);
1742 
1743 	prop_dictionary_set_uint32(geom, "sector-size",
1744 				   DEV_BSIZE /* XXX 512? */);
1745 
1746 	prop_dictionary_set_uint16(geom, "sectors-per-track",
1747 				   wd->sc_params.atap_sectors);
1748 
1749 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
1750 				   wd->sc_params.atap_heads);
1751 
1752 	if (wd->sc_flags & WDF_LBA)
1753 		prop_dictionary_set_uint64(geom, "cylinders-per-unit",
1754 					   wd->sc_capacity /
1755 					       (wd->sc_params.atap_heads *
1756 					        wd->sc_params.atap_sectors));
1757 	else
1758 		prop_dictionary_set_uint16(geom, "cylinders-per-unit",
1759 					   wd->sc_params.atap_cylinders);
1760 
1761 	prop_dictionary_set(disk_info, "geometry", geom);
1762 	prop_object_release(geom);
1763 
1764 	prop_dictionary_set(device_properties(wd->sc_dev),
1765 			    "disk-info", disk_info);
1766 
1767 	/*
1768 	 * Don't release disk_info here; we keep a reference to it.
1769 	 * disk_detach() will release it when we go away.
1770 	 */
1771 
1772 	odisk_info = wd->sc_dk.dk_info;
1773 	wd->sc_dk.dk_info = disk_info;
1774 	if (odisk_info)
1775 		prop_object_release(odisk_info);
1776 }
1777 
1778 int
1779 wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params)
1780 {
1781 
1782 	switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1783 	case CMD_AGAIN:
1784 		return 1;
1785 	case CMD_ERR:
1786 		/*
1787 		 * We `know' there's a drive here; just assume it's old.
1788 		 * This geometry is only used to read the MBR and print a
1789 		 * (false) attach message.
1790 		 */
1791 		strncpy(params->atap_model, "ST506",
1792 		    sizeof params->atap_model);
1793 		params->atap_config = ATA_CFG_FIXED;
1794 		params->atap_cylinders = 1024;
1795 		params->atap_heads = 8;
1796 		params->atap_sectors = 17;
1797 		params->atap_multi = 1;
1798 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
1799 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1800 		/* FALLTHROUGH */
1801 	case CMD_OK:
1802 		wd_params_to_properties(wd, params);
1803 		return 0;
1804 	default:
1805 		panic("wd_get_params: bad return code from ata_get_params");
1806 		/* NOTREACHED */
1807 	}
1808 }
1809 
1810 int
1811 wd_getcache(struct wd_softc *wd, int *bitsp)
1812 {
1813 	struct ataparams params;
1814 
1815 	if (wd_get_params(wd, AT_WAIT, &params) != 0)
1816 		return EIO;
1817 	if (params.atap_cmd_set1 == 0x0000 ||
1818 	    params.atap_cmd_set1 == 0xffff ||
1819 	    (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
1820 		*bitsp = 0;
1821 		return 0;
1822 	}
1823 	*bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
1824 	if (params.atap_cmd1_en & WDC_CMD1_CACHE)
1825 		*bitsp |= DKCACHE_WRITE;
1826 
1827 	return 0;
1828 }
1829 
1830 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF";
1831 
1832 int
1833 wd_setcache(struct wd_softc *wd, int bits)
1834 {
1835 	struct ataparams params;
1836 	struct ata_command ata_c;
1837 
1838 	if (wd_get_params(wd, AT_WAIT, &params) != 0)
1839 		return EIO;
1840 
1841 	if (params.atap_cmd_set1 == 0x0000 ||
1842 	    params.atap_cmd_set1 == 0xffff ||
1843 	    (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
1844 		return EOPNOTSUPP;
1845 
1846 	if ((bits & DKCACHE_READ) == 0 ||
1847 	    (bits & DKCACHE_SAVE) != 0)
1848 		return EOPNOTSUPP;
1849 
1850 	memset(&ata_c, 0, sizeof(struct ata_command));
1851 	ata_c.r_command = SET_FEATURES;
1852 	ata_c.r_st_bmask = 0;
1853 	ata_c.r_st_pmask = 0;
1854 	ata_c.timeout = 30000; /* 30s timeout */
1855 	ata_c.flags = AT_WAIT;
1856 	if (bits & DKCACHE_WRITE)
1857 		ata_c.r_features = WDSF_WRITE_CACHE_EN;
1858 	else
1859 		ata_c.r_features = WDSF_WRITE_CACHE_DS;
1860 	if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1861 		aprint_error_dev(wd->sc_dev,
1862 		    "wd_setcache command not complete\n");
1863 		return EIO;
1864 	}
1865 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1866 		char sbuf[sizeof(at_errbits) + 64];
1867 		snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1868 		aprint_error_dev(wd->sc_dev, "wd_setcache: status=%s\n", sbuf);
1869 		return EIO;
1870 	}
1871 	return 0;
1872 }
1873 
1874 static int
1875 wd_standby(struct wd_softc *wd, int flags)
1876 {
1877 	struct ata_command ata_c;
1878 
1879 	memset(&ata_c, 0, sizeof(struct ata_command));
1880 	ata_c.r_command = WDCC_STANDBY_IMMED;
1881 	ata_c.r_st_bmask = WDCS_DRDY;
1882 	ata_c.r_st_pmask = WDCS_DRDY;
1883 	ata_c.flags = flags;
1884 	ata_c.timeout = 30000; /* 30s timeout */
1885 	if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1886 		aprint_error_dev(wd->sc_dev,
1887 		    "standby immediate command didn't complete\n");
1888 		return EIO;
1889 	}
1890 	if (ata_c.flags & AT_ERROR) {
1891 		if (ata_c.r_error == WDCE_ABRT) /* command not supported */
1892 			return ENODEV;
1893 	}
1894 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1895 		char sbuf[sizeof(at_errbits) + 64];
1896 		snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1897 		aprint_error_dev(wd->sc_dev, "wd_standby: status=%s\n", sbuf);
1898 		return EIO;
1899 	}
1900 	return 0;
1901 }
1902 
1903 int
1904 wd_flushcache(struct wd_softc *wd, int flags)
1905 {
1906 	struct ata_command ata_c;
1907 
1908 	/*
1909 	 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report
1910 	 * only ATA-2 and still support it.
1911 	 */
1912 	if (wd->drvp->ata_vers < 4 &&
1913 	    ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 ||
1914 	    wd->sc_params.atap_cmd_set2 == 0xffff))
1915 		return ENODEV;
1916 	memset(&ata_c, 0, sizeof(struct ata_command));
1917 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
1918 	    (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0)
1919 		ata_c.r_command = WDCC_FLUSHCACHE_EXT;
1920 	else
1921 		ata_c.r_command = WDCC_FLUSHCACHE;
1922 	ata_c.r_st_bmask = WDCS_DRDY;
1923 	ata_c.r_st_pmask = WDCS_DRDY;
1924 	ata_c.flags = flags;
1925 	ata_c.timeout = 30000; /* 30s timeout */
1926 	if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1927 		aprint_error_dev(wd->sc_dev,
1928 		    "flush cache command didn't complete\n");
1929 		return EIO;
1930 	}
1931 	if (ata_c.flags & AT_ERROR) {
1932 		if (ata_c.r_error == WDCE_ABRT) /* command not supported */
1933 			return ENODEV;
1934 	}
1935 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1936 		char sbuf[sizeof(at_errbits) + 64];
1937 		snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1938 		aprint_error_dev(wd->sc_dev, "wd_flushcache: status=%s\n",
1939 		    sbuf);
1940 		return EIO;
1941 	}
1942 	return 0;
1943 }
1944 
1945 bool
1946 wd_shutdown(device_t dev, int how)
1947 {
1948 	struct wd_softc *wd = device_private(dev);
1949 
1950 	/* the adapter needs to be enabled */
1951 	if (wd->atabus->ata_addref(wd->drvp))
1952 		return true; /* no need to complain */
1953 
1954 	wd_flushcache(wd, AT_POLL);
1955 	if ((how & RB_POWERDOWN) == RB_POWERDOWN)
1956 		wd_standby(wd, AT_POLL);
1957 	return true;
1958 }
1959 
1960 /*
1961  * Allocate space for a ioctl queue structure.  Mostly taken from
1962  * scsipi_ioctl.c
1963  */
1964 struct wd_ioctl *
1965 wi_get(void)
1966 {
1967 	struct wd_ioctl *wi;
1968 	int s;
1969 
1970 	wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
1971 	buf_init(&wi->wi_bp);
1972 	s = splbio();
1973 	LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1974 	splx(s);
1975 	return (wi);
1976 }
1977 
1978 /*
1979  * Free an ioctl structure and remove it from our list
1980  */
1981 
1982 void
1983 wi_free(struct wd_ioctl *wi)
1984 {
1985 	int s;
1986 
1987 	s = splbio();
1988 	LIST_REMOVE(wi, wi_list);
1989 	splx(s);
1990 	buf_destroy(&wi->wi_bp);
1991 	free(wi, M_TEMP);
1992 }
1993 
1994 /*
1995  * Find a wd_ioctl structure based on the struct buf.
1996  */
1997 
1998 struct wd_ioctl *
1999 wi_find(struct buf *bp)
2000 {
2001 	struct wd_ioctl *wi;
2002 	int s;
2003 
2004 	s = splbio();
2005 	for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
2006 		if (bp == &wi->wi_bp)
2007 			break;
2008 	splx(s);
2009 	return (wi);
2010 }
2011 
2012 /*
2013  * Ioctl pseudo strategy routine
2014  *
2015  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
2016  * happens here is:
2017  *
2018  * - wdioctl() queues a wd_ioctl structure.
2019  *
2020  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
2021  *   user space I/O is required.  If physio() is called, physio() eventually
2022  *   calls wdioctlstrategy().
2023  *
2024  * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
2025  *   to perform the actual command
2026  *
2027  * The reason for the use of the pseudo strategy routine is because
2028  * when doing I/O to/from user space, physio _really_ wants to be in
2029  * the loop.  We could put the entire buffer into the ioctl request
2030  * structure, but that won't scale if we want to do things like download
2031  * microcode.
2032  */
2033 
2034 void
2035 wdioctlstrategy(struct buf *bp)
2036 {
2037 	struct wd_ioctl *wi;
2038 	struct ata_command ata_c;
2039 	int error = 0;
2040 
2041 	wi = wi_find(bp);
2042 	if (wi == NULL) {
2043 		printf("wdioctlstrategy: "
2044 		    "No matching ioctl request found in queue\n");
2045 		error = EINVAL;
2046 		goto bad;
2047 	}
2048 
2049 	memset(&ata_c, 0, sizeof(ata_c));
2050 
2051 	/*
2052 	 * Abort if physio broke up the transfer
2053 	 */
2054 
2055 	if (bp->b_bcount != wi->wi_atareq.datalen) {
2056 		printf("physio split wd ioctl request... cannot proceed\n");
2057 		error = EIO;
2058 		goto bad;
2059 	}
2060 
2061 	/*
2062 	 * Abort if we didn't get a buffer size that was a multiple of
2063 	 * our sector size (or was larger than NBBY)
2064 	 */
2065 
2066 	if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
2067 	    (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
2068 	     (1 << NBBY)) {
2069 		error = EINVAL;
2070 		goto bad;
2071 	}
2072 
2073 	/*
2074 	 * Make sure a timeout was supplied in the ioctl request
2075 	 */
2076 
2077 	if (wi->wi_atareq.timeout == 0) {
2078 		error = EINVAL;
2079 		goto bad;
2080 	}
2081 
2082 	if (wi->wi_atareq.flags & ATACMD_READ)
2083 		ata_c.flags |= AT_READ;
2084 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
2085 		ata_c.flags |= AT_WRITE;
2086 
2087 	if (wi->wi_atareq.flags & ATACMD_READREG)
2088 		ata_c.flags |= AT_READREG;
2089 
2090 	ata_c.flags |= AT_WAIT;
2091 
2092 	ata_c.timeout = wi->wi_atareq.timeout;
2093 	ata_c.r_command = wi->wi_atareq.command;
2094 	ata_c.r_head = wi->wi_atareq.head & 0x0f;
2095 	ata_c.r_cyl = wi->wi_atareq.cylinder;
2096 	ata_c.r_sector = wi->wi_atareq.sec_num;
2097 	ata_c.r_count = wi->wi_atareq.sec_count;
2098 	ata_c.r_features = wi->wi_atareq.features;
2099 	ata_c.r_st_bmask = WDCS_DRDY;
2100 	ata_c.r_st_pmask = WDCS_DRDY;
2101 	ata_c.data = wi->wi_bp.b_data;
2102 	ata_c.bcount = wi->wi_bp.b_bcount;
2103 
2104 	if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &ata_c)
2105 	    != ATACMD_COMPLETE) {
2106 		wi->wi_atareq.retsts = ATACMD_ERROR;
2107 		goto bad;
2108 	}
2109 
2110 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
2111 		if (ata_c.flags & AT_ERROR) {
2112 			wi->wi_atareq.retsts = ATACMD_ERROR;
2113 			wi->wi_atareq.error = ata_c.r_error;
2114 		} else if (ata_c.flags & AT_DF)
2115 			wi->wi_atareq.retsts = ATACMD_DF;
2116 		else
2117 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
2118 	} else {
2119 		wi->wi_atareq.retsts = ATACMD_OK;
2120 		if (wi->wi_atareq.flags & ATACMD_READREG) {
2121 			wi->wi_atareq.head = ata_c.r_head ;
2122 			wi->wi_atareq.cylinder = ata_c.r_cyl;
2123 			wi->wi_atareq.sec_num = ata_c.r_sector;
2124 			wi->wi_atareq.sec_count = ata_c.r_count;
2125 			wi->wi_atareq.features = ata_c.r_features;
2126 			wi->wi_atareq.error = ata_c.r_error;
2127 		}
2128 	}
2129 
2130 	bp->b_error = 0;
2131 	biodone(bp);
2132 	return;
2133 bad:
2134 	bp->b_error = error;
2135 	biodone(bp);
2136 }
2137