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