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