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