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