Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.175.2.12
      1  1.175.2.12  bouyer /*	$NetBSD: wd.c,v 1.175.2.12 1998/09/20 19:00:15 bouyer Exp $ */
      2   1.175.2.2  bouyer 
      3   1.175.2.2  bouyer /*
      4   1.175.2.2  bouyer  * Copyright (c) 1998 Manuel Bouyer.  All rights reserved.
      5   1.175.2.2  bouyer  *
      6   1.175.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
      7   1.175.2.2  bouyer  * modification, are permitted provided that the following conditions
      8   1.175.2.2  bouyer  * are met:
      9   1.175.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     10   1.175.2.2  bouyer  *	notice, this list of conditions and the following disclaimer.
     11   1.175.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.175.2.2  bouyer  *	notice, this list of conditions and the following disclaimer in the
     13   1.175.2.2  bouyer  *	documentation and/or other materials provided with the distribution.
     14   1.175.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     15   1.175.2.2  bouyer  *	must display the following acknowledgement:
     16   1.175.2.2  bouyer  *  This product includes software developed by Manuel Bouyer.
     17   1.175.2.2  bouyer  * 4. The name of the author may not be used to endorse or promote products
     18   1.175.2.2  bouyer  *	derived from this software without specific prior written permission.
     19   1.175.2.2  bouyer  *
     20   1.175.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.175.2.2  bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.175.2.2  bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.175.2.2  bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.175.2.2  bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.175.2.2  bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.175.2.2  bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.175.2.2  bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.175.2.2  bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.175.2.2  bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.175.2.2  bouyer  */
     31   1.175.2.2  bouyer 
     32   1.175.2.8  bouyer /*-
     33   1.175.2.8  bouyer  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     34   1.175.2.8  bouyer  * All rights reserved.
     35   1.175.2.2  bouyer  *
     36   1.175.2.8  bouyer  * This code is derived from software contributed to The NetBSD Foundation
     37   1.175.2.8  bouyer  * by Charles M. Hannum and by Onno van der Linden.
     38   1.175.2.2  bouyer  *
     39   1.175.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     40   1.175.2.2  bouyer  * modification, are permitted provided that the following conditions
     41   1.175.2.2  bouyer  * are met:
     42   1.175.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     43   1.175.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     44   1.175.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     45   1.175.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     46   1.175.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     47   1.175.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     48   1.175.2.2  bouyer  *    must display the following acknowledgement:
     49   1.175.2.8  bouyer  *        This product includes software developed by the NetBSD
     50   1.175.2.8  bouyer  *        Foundation, Inc. and its contributors.
     51   1.175.2.8  bouyer  * 4. Neither the name of The NetBSD Foundation nor the names of its
     52   1.175.2.8  bouyer  *    contributors may be used to endorse or promote products derived
     53   1.175.2.8  bouyer  *    from this software without specific prior written permission.
     54   1.175.2.2  bouyer  *
     55   1.175.2.8  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     56   1.175.2.8  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57   1.175.2.8  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58   1.175.2.8  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     59   1.175.2.8  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60   1.175.2.8  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61   1.175.2.8  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62   1.175.2.8  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63   1.175.2.8  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64   1.175.2.8  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65   1.175.2.8  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     66   1.175.2.2  bouyer  */
     67   1.175.2.2  bouyer 
     68   1.175.2.2  bouyer #define WDCDEBUG
     69   1.175.2.2  bouyer 
     70   1.175.2.2  bouyer #include "rnd.h"
     71   1.175.2.2  bouyer 
     72   1.175.2.2  bouyer #include <sys/param.h>
     73   1.175.2.2  bouyer #include <sys/systm.h>
     74   1.175.2.2  bouyer #include <sys/kernel.h>
     75   1.175.2.2  bouyer #include <sys/conf.h>
     76   1.175.2.2  bouyer #include <sys/file.h>
     77   1.175.2.2  bouyer #include <sys/stat.h>
     78   1.175.2.2  bouyer #include <sys/ioctl.h>
     79   1.175.2.2  bouyer #include <sys/buf.h>
     80   1.175.2.2  bouyer #include <sys/uio.h>
     81   1.175.2.2  bouyer #include <sys/malloc.h>
     82   1.175.2.2  bouyer #include <sys/device.h>
     83   1.175.2.2  bouyer #include <sys/disklabel.h>
     84   1.175.2.2  bouyer #include <sys/disk.h>
     85   1.175.2.2  bouyer #include <sys/syslog.h>
     86   1.175.2.2  bouyer #include <sys/proc.h>
     87   1.175.2.2  bouyer #if NRND > 0
     88   1.175.2.2  bouyer #include <sys/rnd.h>
     89   1.175.2.2  bouyer #endif
     90   1.175.2.2  bouyer 
     91   1.175.2.2  bouyer #include <vm/vm.h>
     92   1.175.2.2  bouyer 
     93   1.175.2.2  bouyer #include <machine/intr.h>
     94   1.175.2.2  bouyer #include <machine/bus.h>
     95   1.175.2.2  bouyer 
     96   1.175.2.2  bouyer #include <dev/ata/atareg.h>
     97   1.175.2.2  bouyer #include <dev/ata/atavar.h>
     98   1.175.2.2  bouyer #include <dev/ata/wdvar.h>
     99   1.175.2.2  bouyer #include <dev/ic/wdcreg.h>
    100   1.175.2.2  bouyer #include "locators.h"
    101   1.175.2.2  bouyer 
    102   1.175.2.2  bouyer #define	WAITTIME	(4 * hz)	/* time to wait for a completion */
    103   1.175.2.2  bouyer #define	WDIORETRIES	5	/* number of retries before giving up */
    104   1.175.2.2  bouyer #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
    105   1.175.2.2  bouyer 
    106   1.175.2.2  bouyer #define	WDUNIT(dev)		DISKUNIT(dev)
    107   1.175.2.2  bouyer #define	WDPART(dev)		DISKPART(dev)
    108   1.175.2.2  bouyer #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
    109   1.175.2.2  bouyer 
    110   1.175.2.2  bouyer #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
    111   1.175.2.2  bouyer 
    112   1.175.2.2  bouyer #define DEBUG_INTR   0x01
    113   1.175.2.2  bouyer #define DEBUG_XFERS  0x02
    114   1.175.2.2  bouyer #define DEBUG_STATUS 0x04
    115   1.175.2.2  bouyer #define DEBUG_FUNCS  0x08
    116   1.175.2.2  bouyer #define DEBUG_PROBE  0x10
    117   1.175.2.2  bouyer #ifdef WDCDEBUG
    118   1.175.2.2  bouyer extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
    119   1.175.2.2  bouyer #define WDCDEBUG_PRINT(args, level) \
    120   1.175.2.2  bouyer 	if (wdcdebug_wd_mask & (level)) \
    121   1.175.2.2  bouyer 		printf args
    122   1.175.2.2  bouyer #else
    123   1.175.2.2  bouyer #define WDCDEBUG_PRINT(args, level)
    124   1.175.2.2  bouyer #endif
    125   1.175.2.2  bouyer 
    126   1.175.2.2  bouyer struct wd_softc {
    127   1.175.2.2  bouyer 	/* General disk infos */
    128   1.175.2.2  bouyer 	struct device sc_dev;
    129   1.175.2.2  bouyer 	struct disk sc_dk;
    130   1.175.2.2  bouyer 	struct buf sc_q;
    131   1.175.2.2  bouyer 	/* IDE disk soft states */
    132   1.175.2.2  bouyer 	struct ata_bio sc_wdc_bio; /* current transfert */
    133   1.175.2.2  bouyer 	struct buf *sc_bp; /* buf being transfered */
    134   1.175.2.2  bouyer 	void *wdc_softc;   /* pointer to our parent */
    135   1.175.2.2  bouyer 	struct ata_drive_datas *drvp; /* Our controller's infos */
    136   1.175.2.2  bouyer 	int openings;
    137   1.175.2.2  bouyer 	struct ataparams sc_params;/* drive characteistics found */
    138   1.175.2.2  bouyer 	int sc_flags;
    139   1.175.2.2  bouyer #define WDF_LOCKED	  0x01
    140   1.175.2.2  bouyer #define WDF_WANTED	  0x02
    141   1.175.2.2  bouyer #define WDF_WLABEL	  0x04 /* label is writable */
    142   1.175.2.2  bouyer #define WDF_LABELLING   0x08 /* writing label */
    143   1.175.2.2  bouyer /*
    144   1.175.2.2  bouyer  * XXX Nothing resets this yet, but disk change sensing will when ATA-4 is
    145   1.175.2.2  bouyer  * more fully implemented.
    146   1.175.2.2  bouyer  */
    147   1.175.2.2  bouyer #define WDF_LOADED	  0x10 /* parameters loaded */
    148   1.175.2.2  bouyer #define WDF_WAIT	0x20 /* waiting for resources */
    149   1.175.2.2  bouyer #define WDF_LBA	 0x40 /* using LBA mode */
    150   1.175.2.2  bouyer 	int sc_capacity;
    151   1.175.2.2  bouyer 	int cyl; /* actual drive parameters */
    152   1.175.2.2  bouyer 	int heads;
    153   1.175.2.2  bouyer 	int sectors;
    154   1.175.2.2  bouyer 	int retries; /* number of xfer retry */
    155   1.175.2.2  bouyer #if NRND > 0
    156   1.175.2.2  bouyer 	rndsource_element_t	rnd_source;
    157   1.175.2.2  bouyer #endif
    158   1.175.2.2  bouyer };
    159   1.175.2.2  bouyer 
    160   1.175.2.2  bouyer #define sc_drive sc_wdc_bio.drive
    161   1.175.2.2  bouyer #define sc_mode sc_wdc_bio.mode
    162   1.175.2.2  bouyer #define sc_multi sc_wdc_bio.multi
    163   1.175.2.2  bouyer #define sc_badsect sc_wdc_bio.badsect
    164   1.175.2.2  bouyer 
    165   1.175.2.6  bouyer int	wdprobe		__P((struct device *, struct cfdata *, void *));
    166   1.175.2.2  bouyer void	wdattach	__P((struct device *, struct device *, void *));
    167   1.175.2.2  bouyer int	wdprint	__P((void *, char *));
    168   1.175.2.2  bouyer 
    169   1.175.2.2  bouyer struct cfattach wd_ca = {
    170   1.175.2.2  bouyer 	sizeof(struct wd_softc), wdprobe, wdattach
    171   1.175.2.2  bouyer };
    172   1.175.2.2  bouyer 
    173   1.175.2.2  bouyer extern struct cfdriver wd_cd;
    174   1.175.2.2  bouyer 
    175   1.175.2.2  bouyer void  wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
    176   1.175.2.2  bouyer void  wdgetdisklabel	__P((struct wd_softc *));
    177   1.175.2.2  bouyer void  wdstrategy	__P((struct buf *));
    178   1.175.2.2  bouyer void  wdstart	__P((void *));
    179   1.175.2.2  bouyer void  __wdstart	__P((struct wd_softc*, struct buf *));
    180   1.175.2.2  bouyer void  wdrestart __P((void*));
    181   1.175.2.2  bouyer int   wd_get_params __P((struct wd_softc *, u_int8_t, struct ataparams *));
    182   1.175.2.2  bouyer 
    183   1.175.2.2  bouyer struct dkdriver wddkdriver = { wdstrategy };
    184   1.175.2.2  bouyer 
    185   1.175.2.2  bouyer /* XXX: these should go elsewhere */
    186   1.175.2.2  bouyer cdev_decl(wd);
    187   1.175.2.2  bouyer bdev_decl(wd);
    188   1.175.2.2  bouyer 
    189   1.175.2.2  bouyer #ifdef HAS_BAD144_HANDLING
    190   1.175.2.2  bouyer static void bad144intern __P((struct wd_softc *));
    191   1.175.2.2  bouyer #endif
    192   1.175.2.2  bouyer int	wdlock	__P((struct wd_softc *));
    193   1.175.2.2  bouyer void	wdunlock	__P((struct wd_softc *));
    194   1.175.2.2  bouyer void print_wderror __P((int, char*));
    195   1.175.2.2  bouyer 
    196   1.175.2.2  bouyer int
    197   1.175.2.2  bouyer wdprobe(parent, match, aux)
    198   1.175.2.2  bouyer 	struct device *parent;
    199   1.175.2.2  bouyer 	struct cfdata *match;
    200   1.175.2.2  bouyer 
    201   1.175.2.2  bouyer 	void *aux;
    202   1.175.2.2  bouyer {
    203   1.175.2.2  bouyer 	struct ata_atapi_attach *aa_link = aux;
    204   1.175.2.2  bouyer 
    205   1.175.2.2  bouyer 	if (aa_link == NULL)
    206   1.175.2.2  bouyer 		return 0;
    207   1.175.2.2  bouyer 	if (aa_link->aa_type != T_ATA)
    208   1.175.2.2  bouyer 		return 0;
    209   1.175.2.2  bouyer 
    210   1.175.2.2  bouyer 	if (match->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT &&
    211   1.175.2.2  bouyer 	    match->cf_loc[ATACF_CHANNEL] != aa_link->aa_channel)
    212   1.175.2.2  bouyer 		return 0;
    213   1.175.2.2  bouyer 
    214   1.175.2.2  bouyer 	if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
    215   1.175.2.2  bouyer 	    match->cf_loc[ATACF_DRIVE] != aa_link->aa_drv_data->drive)
    216   1.175.2.2  bouyer 		return 0;
    217   1.175.2.2  bouyer 	return 1;
    218   1.175.2.2  bouyer }
    219   1.175.2.2  bouyer 
    220   1.175.2.2  bouyer void
    221   1.175.2.2  bouyer wdattach(parent, self, aux)
    222   1.175.2.2  bouyer 	struct device *parent, *self;
    223   1.175.2.2  bouyer 	void *aux;
    224   1.175.2.2  bouyer {
    225   1.175.2.2  bouyer 	struct wd_softc *wd = (void *)self;
    226   1.175.2.2  bouyer 	struct ata_atapi_attach *aa_link= aux;
    227   1.175.2.2  bouyer 	int i, blank;
    228   1.175.2.2  bouyer 	char buf[41], c, *p, *q;
    229   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
    230   1.175.2.2  bouyer 
    231   1.175.2.2  bouyer 	wd->openings = aa_link->aa_openings;
    232   1.175.2.2  bouyer 	wd->drvp = aa_link->aa_drv_data;;
    233   1.175.2.2  bouyer 	wd->wdc_softc = parent;
    234   1.175.2.2  bouyer 	/* give back our softc to our caller */
    235   1.175.2.2  bouyer 	wd->drvp->drv_softc = wd;
    236   1.175.2.2  bouyer 
    237   1.175.2.2  bouyer 	/* read our drive info */
    238  1.175.2.10  bouyer 	if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
    239  1.175.2.10  bouyer 		printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
    240  1.175.2.10  bouyer 		return;
    241  1.175.2.10  bouyer 	}
    242   1.175.2.2  bouyer 
    243   1.175.2.2  bouyer 	for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
    244   1.175.2.2  bouyer 	    i < sizeof(wd->sc_params.atap_model); i++) {
    245   1.175.2.2  bouyer 		c = *p++;
    246   1.175.2.2  bouyer 		if (c == '\0')
    247   1.175.2.2  bouyer 			break;
    248   1.175.2.2  bouyer 		if (c != ' ') {
    249   1.175.2.2  bouyer 			if (blank) {
    250   1.175.2.2  bouyer 				*q++ = ' ';
    251   1.175.2.2  bouyer 				blank = 0;
    252   1.175.2.2  bouyer 			}
    253   1.175.2.2  bouyer 			*q++ = c;
    254   1.175.2.2  bouyer 		} else
    255   1.175.2.2  bouyer 			blank = 1;
    256   1.175.2.2  bouyer 		}
    257   1.175.2.2  bouyer 	*q++ = '\0';
    258   1.175.2.2  bouyer 
    259   1.175.2.2  bouyer 	printf(": <%s>\n", buf);
    260   1.175.2.2  bouyer 
    261   1.175.2.2  bouyer 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
    262   1.175.2.2  bouyer 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
    263   1.175.2.2  bouyer 	} else {
    264   1.175.2.2  bouyer 		wd->sc_multi = 1;
    265   1.175.2.2  bouyer 	}
    266   1.175.2.2  bouyer 
    267   1.175.2.2  bouyer 	printf("%s: using %d-sector pio transfers,", wd->sc_dev.dv_xname,
    268   1.175.2.2  bouyer 	    wd->sc_multi);
    269   1.175.2.2  bouyer 
    270   1.175.2.2  bouyer 	/* Prior to ATA-4, LBA was optional. */
    271   1.175.2.2  bouyer 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
    272   1.175.2.2  bouyer 		wd->sc_flags |= WDF_LBA;
    273   1.175.2.2  bouyer #if 0
    274   1.175.2.2  bouyer 	/* ATA-4 requires LBA. */
    275   1.175.2.2  bouyer 	if (wd->sc_params.atap_ataversion != 0xffff &&
    276   1.175.2.2  bouyer 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
    277   1.175.2.2  bouyer 		wd->sc_flags |= WDF_LBA;
    278   1.175.2.2  bouyer #endif
    279   1.175.2.2  bouyer 
    280   1.175.2.2  bouyer 	if ((wd->sc_flags & WDF_LBA) != 0) {
    281   1.175.2.2  bouyer 		printf(" lba mode\n");
    282   1.175.2.2  bouyer 		wd->sc_capacity =
    283   1.175.2.2  bouyer 		    (wd->sc_params.atap_capacity[1] << 16) |
    284   1.175.2.2  bouyer 		    wd->sc_params.atap_capacity[0];
    285   1.175.2.8  bouyer 		printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d sectors\n",
    286   1.175.2.2  bouyer 		    self->dv_xname,
    287   1.175.2.2  bouyer 		    wd->sc_capacity / (1048576 / DEV_BSIZE),
    288   1.175.2.8  bouyer 		    wd->sc_params.atap_cylinders,
    289   1.175.2.8  bouyer 		    wd->sc_params.atap_heads,
    290   1.175.2.8  bouyer 		    wd->sc_params.atap_sectors,
    291   1.175.2.8  bouyer 		    DEV_BSIZE,
    292   1.175.2.8  bouyer 		    wd->sc_capacity);
    293   1.175.2.2  bouyer 	} else {
    294   1.175.2.2  bouyer 		printf(" chs mode\n");
    295   1.175.2.2  bouyer 		wd->sc_capacity =
    296   1.175.2.2  bouyer 		    wd->sc_params.atap_cylinders *
    297   1.175.2.2  bouyer 		    wd->sc_params.atap_heads *
    298   1.175.2.2  bouyer 		    wd->sc_params.atap_sectors;
    299   1.175.2.8  bouyer 		printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d "
    300   1.175.2.8  bouyer 		    "sectors\n", self->dv_xname,
    301   1.175.2.2  bouyer 		    wd->sc_capacity / (1048576 / DEV_BSIZE),
    302   1.175.2.2  bouyer 		    wd->sc_params.atap_cylinders,
    303   1.175.2.2  bouyer 		    wd->sc_params.atap_heads,
    304   1.175.2.8  bouyer 		    wd->sc_params.atap_sectors,
    305   1.175.2.8  bouyer 		    DEV_BSIZE,
    306   1.175.2.8  bouyer 		    wd->sc_capacity);
    307   1.175.2.2  bouyer 	}
    308   1.175.2.5  bouyer 	WDCDEBUG_PRINT(("atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
    309   1.175.2.5  bouyer 	    wd->sc_params.atap_dmatiming_mimi,
    310   1.175.2.5  bouyer 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
    311   1.175.2.2  bouyer 	/*
    312   1.175.2.2  bouyer 	 * Initialize and attach the disk structure.
    313   1.175.2.2  bouyer 	 */
    314   1.175.2.2  bouyer 	wd->sc_dk.dk_driver = &wddkdriver;
    315   1.175.2.2  bouyer 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    316   1.175.2.2  bouyer 	disk_attach(&wd->sc_dk);
    317   1.175.2.2  bouyer 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
    318   1.175.2.2  bouyer 
    319   1.175.2.2  bouyer #if NRND > 0
    320   1.175.2.2  bouyer 	rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname, RND_TYPE_DISK);
    321   1.175.2.2  bouyer #endif
    322   1.175.2.2  bouyer }
    323   1.175.2.2  bouyer 
    324   1.175.2.2  bouyer /*
    325   1.175.2.2  bouyer  * Read/write routine for a buffer.  Validates the arguments and schedules the
    326   1.175.2.2  bouyer  * transfer.  Does not wait for the transfer to complete.
    327   1.175.2.2  bouyer  */
    328   1.175.2.2  bouyer void
    329   1.175.2.2  bouyer wdstrategy(bp)
    330   1.175.2.2  bouyer 	struct buf *bp;
    331   1.175.2.2  bouyer {
    332   1.175.2.2  bouyer 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
    333   1.175.2.2  bouyer 	int s;
    334  1.175.2.11  bouyer 	WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
    335  1.175.2.11  bouyer 	    DEBUG_FUNCS | DEBUG_XFERS);
    336   1.175.2.2  bouyer 
    337   1.175.2.2  bouyer 	/* Valid request?  */
    338   1.175.2.2  bouyer 	if (bp->b_blkno < 0 ||
    339   1.175.2.2  bouyer 	    (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
    340   1.175.2.2  bouyer 	    (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
    341   1.175.2.2  bouyer 		bp->b_error = EINVAL;
    342   1.175.2.2  bouyer 		goto bad;
    343   1.175.2.2  bouyer 	}
    344   1.175.2.2  bouyer 
    345   1.175.2.2  bouyer 	/* If device invalidated (e.g. media change, door open), error. */
    346   1.175.2.2  bouyer 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    347   1.175.2.2  bouyer 		bp->b_error = EIO;
    348   1.175.2.2  bouyer 		goto bad;
    349   1.175.2.2  bouyer 	}
    350   1.175.2.2  bouyer 
    351   1.175.2.2  bouyer 	/* If it's a null transfer, return immediately. */
    352   1.175.2.2  bouyer 	if (bp->b_bcount == 0)
    353   1.175.2.2  bouyer 		goto done;
    354   1.175.2.2  bouyer 
    355   1.175.2.2  bouyer 	/*
    356   1.175.2.2  bouyer 	 * Do bounds checking, adjust transfer. if error, process.
    357   1.175.2.2  bouyer 	 * If end of partition, just return.
    358   1.175.2.2  bouyer 	 */
    359   1.175.2.2  bouyer 	if (WDPART(bp->b_dev) != RAW_PART &&
    360   1.175.2.2  bouyer 	    bounds_check_with_label(bp, wd->sc_dk.dk_label,
    361   1.175.2.2  bouyer 	    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    362   1.175.2.2  bouyer 		goto done;
    363   1.175.2.2  bouyer 	/* Queue transfer on drive, activate drive and controller if idle. */
    364   1.175.2.2  bouyer 	s = splbio();
    365   1.175.2.2  bouyer 	disksort(&wd->sc_q, bp);
    366   1.175.2.2  bouyer 	wdstart(wd);
    367   1.175.2.2  bouyer 	splx(s);
    368   1.175.2.2  bouyer 	return;
    369   1.175.2.2  bouyer bad:
    370   1.175.2.2  bouyer 	bp->b_flags |= B_ERROR;
    371   1.175.2.2  bouyer done:
    372   1.175.2.2  bouyer 	/* Toss transfer; we're done early. */
    373   1.175.2.2  bouyer 	bp->b_resid = bp->b_bcount;
    374   1.175.2.2  bouyer 	biodone(bp);
    375   1.175.2.2  bouyer }
    376   1.175.2.2  bouyer 
    377   1.175.2.2  bouyer /*
    378   1.175.2.2  bouyer  * Queue a drive for I/O.
    379   1.175.2.2  bouyer  */
    380   1.175.2.2  bouyer void
    381   1.175.2.2  bouyer wdstart(arg)
    382   1.175.2.2  bouyer 	void *arg;
    383   1.175.2.2  bouyer {
    384   1.175.2.2  bouyer 	struct wd_softc *wd = arg;
    385   1.175.2.2  bouyer 	struct buf *dp, *bp=0;
    386   1.175.2.2  bouyer 
    387  1.175.2.11  bouyer 	WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
    388  1.175.2.11  bouyer 	    DEBUG_FUNCS | DEBUG_XFERS);
    389   1.175.2.2  bouyer 	while (wd->openings > 0) {
    390   1.175.2.2  bouyer 
    391   1.175.2.2  bouyer 		/* Is there a buf for us ? */
    392   1.175.2.2  bouyer 		dp = &wd->sc_q;
    393   1.175.2.2  bouyer 		if ((bp = dp->b_actf) == NULL)  /* yes, an assign */
    394   1.175.2.2  bouyer 			 return;
    395   1.175.2.2  bouyer 		dp->b_actf = bp->b_actf;
    396   1.175.2.2  bouyer 
    397   1.175.2.2  bouyer 		/*
    398   1.175.2.2  bouyer 		 * Make the command. First lock the device
    399   1.175.2.2  bouyer 		 */
    400   1.175.2.2  bouyer 		wd->openings--;
    401   1.175.2.2  bouyer 
    402   1.175.2.2  bouyer 		wd->retries = 0;
    403   1.175.2.2  bouyer 		__wdstart(wd, bp);
    404   1.175.2.2  bouyer 	}
    405   1.175.2.2  bouyer }
    406   1.175.2.2  bouyer 
    407   1.175.2.2  bouyer void
    408   1.175.2.2  bouyer __wdstart(wd, bp)
    409   1.175.2.2  bouyer 	struct wd_softc *wd;
    410   1.175.2.2  bouyer 	struct buf *bp;
    411   1.175.2.2  bouyer {
    412   1.175.2.2  bouyer 	daddr_t p_offset;
    413   1.175.2.2  bouyer 	if (WDPART(bp->b_dev) != RAW_PART)
    414   1.175.2.2  bouyer 		p_offset =
    415   1.175.2.2  bouyer 		    wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
    416   1.175.2.2  bouyer 	else
    417   1.175.2.2  bouyer 		p_offset = 0;
    418   1.175.2.2  bouyer 	wd->sc_wdc_bio.blkno = bp->b_blkno + p_offset;
    419   1.175.2.2  bouyer 	wd->sc_wdc_bio.blkno /= (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    420   1.175.2.7  bouyer 	wd->sc_wdc_bio.blkdone =0;
    421   1.175.2.2  bouyer 	wd->sc_bp = bp;
    422   1.175.2.7  bouyer 	/*
    423   1.175.2.7  bouyer 	 * If we're retrying, retry in single-sector mode. This will give us
    424   1.175.2.7  bouyer 	 * the sector number of the problem, and will eventually allow the
    425   1.175.2.7  bouyer 	 * transfert to succeed.
    426   1.175.2.7  bouyer 	 */
    427   1.175.2.2  bouyer 	if (wd->sc_multi == 1 || wd->retries > 0)
    428   1.175.2.2  bouyer 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    429   1.175.2.2  bouyer 	else
    430   1.175.2.2  bouyer 		wd->sc_wdc_bio.flags = 0;
    431   1.175.2.2  bouyer 	if (wd->sc_flags & WDF_LBA)
    432   1.175.2.2  bouyer 		wd->sc_wdc_bio.flags |= ATA_LBA;
    433   1.175.2.2  bouyer 	if (bp->b_flags & B_READ)
    434   1.175.2.2  bouyer 		wd->sc_wdc_bio.flags |= ATA_READ;
    435   1.175.2.2  bouyer 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    436   1.175.2.2  bouyer 	wd->sc_wdc_bio.databuf = bp->b_data;
    437   1.175.2.2  bouyer 	/* Instrumentation. */
    438   1.175.2.2  bouyer 	disk_busy(&wd->sc_dk);
    439   1.175.2.2  bouyer 	switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    440   1.175.2.2  bouyer 	case WDC_TRY_AGAIN:
    441   1.175.2.2  bouyer 		timeout(wdrestart, wd, hz);
    442   1.175.2.2  bouyer 		break;
    443   1.175.2.2  bouyer 	case WDC_QUEUED:
    444   1.175.2.2  bouyer 		break;
    445   1.175.2.2  bouyer 	case WDC_COMPLETE:
    446   1.175.2.2  bouyer 		wddone(wd);
    447   1.175.2.2  bouyer 		break;
    448   1.175.2.2  bouyer 	default:
    449   1.175.2.2  bouyer 		panic("__wdstart: bad return code from wdc_ata_bio()");
    450   1.175.2.2  bouyer 	}
    451   1.175.2.2  bouyer }
    452   1.175.2.2  bouyer 
    453   1.175.2.2  bouyer void
    454   1.175.2.2  bouyer wddone(v)
    455   1.175.2.2  bouyer 	void *v;
    456   1.175.2.2  bouyer {
    457   1.175.2.2  bouyer 	struct wd_softc *wd = v;
    458   1.175.2.2  bouyer 	struct buf *bp = wd->sc_bp;
    459   1.175.2.2  bouyer 	char buf[256], *errbuf = buf;
    460  1.175.2.11  bouyer 	WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
    461  1.175.2.11  bouyer 	    DEBUG_FUNCS | DEBUG_XFERS);
    462   1.175.2.2  bouyer 
    463   1.175.2.7  bouyer 	bp->b_resid = wd->sc_wdc_bio.bcount;
    464   1.175.2.2  bouyer 	errbuf[0] = '\0';
    465   1.175.2.2  bouyer 	switch (wd->sc_wdc_bio.error) {
    466   1.175.2.2  bouyer 	case ERR_DMA:
    467   1.175.2.2  bouyer 		errbuf = "DMA error";
    468   1.175.2.2  bouyer 		goto retry;
    469   1.175.2.2  bouyer 	case ERR_DF:
    470   1.175.2.2  bouyer 		errbuf = "device fault";
    471   1.175.2.2  bouyer 		goto retry;
    472   1.175.2.2  bouyer 	case TIMEOUT:
    473   1.175.2.2  bouyer 		errbuf = "device timeout";
    474   1.175.2.2  bouyer 		goto retry;
    475   1.175.2.2  bouyer 	case ERROR:
    476   1.175.2.2  bouyer 		/* Don't care about media change bits */
    477   1.175.2.2  bouyer 		if (wd->sc_wdc_bio.r_error != 0 &&
    478   1.175.2.2  bouyer 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    479   1.175.2.2  bouyer 			goto noerror;
    480   1.175.2.2  bouyer 		print_wderror(wd->sc_wdc_bio.r_error, errbuf);
    481   1.175.2.2  bouyer retry:		/* Just reset and retry. Can we do more ? */
    482   1.175.2.2  bouyer 		wdc_reset_channel(wd->drvp);
    483   1.175.2.7  bouyer 		diskerr(bp, "wd", errbuf, LOG_PRINTF,
    484   1.175.2.7  bouyer 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    485   1.175.2.2  bouyer 		if (wd->retries++ < WDIORETRIES) {
    486   1.175.2.2  bouyer 			printf(", retrying\n");
    487   1.175.2.2  bouyer 			timeout(wdrestart, wd, RECOVERYTIME);
    488   1.175.2.2  bouyer 			return;
    489   1.175.2.2  bouyer 		}
    490   1.175.2.2  bouyer 		printf("\n");
    491   1.175.2.2  bouyer 		bp->b_flags |= B_ERROR;
    492   1.175.2.2  bouyer 		bp->b_error = EIO;
    493   1.175.2.2  bouyer 		break;
    494   1.175.2.2  bouyer 	case NOERROR:
    495   1.175.2.2  bouyer noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    496   1.175.2.2  bouyer 			printf("%s: soft error (corrected)\n",
    497   1.175.2.2  bouyer 			    wd->sc_dev.dv_xname);
    498   1.175.2.2  bouyer 	}
    499   1.175.2.2  bouyer 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
    500   1.175.2.2  bouyer #if NRND > 0
    501   1.175.2.2  bouyer 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    502   1.175.2.2  bouyer #endif
    503   1.175.2.2  bouyer 	biodone(bp);
    504   1.175.2.2  bouyer 	wd->openings++;
    505   1.175.2.2  bouyer 	wdstart(wd);
    506   1.175.2.2  bouyer }
    507   1.175.2.2  bouyer 
    508   1.175.2.2  bouyer void
    509   1.175.2.2  bouyer wdrestart(v)
    510   1.175.2.2  bouyer 	void *v;
    511   1.175.2.2  bouyer {
    512   1.175.2.2  bouyer 	struct wd_softc *wd = v;
    513   1.175.2.2  bouyer 	struct buf *bp = wd->sc_bp;
    514   1.175.2.2  bouyer 	int s;
    515  1.175.2.11  bouyer 	WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
    516  1.175.2.11  bouyer 	    DEBUG_FUNCS | DEBUG_XFERS);
    517   1.175.2.2  bouyer 
    518   1.175.2.2  bouyer 	s = splbio();
    519   1.175.2.2  bouyer 	__wdstart(v, bp);
    520   1.175.2.2  bouyer 	splx(s);
    521   1.175.2.2  bouyer }
    522   1.175.2.2  bouyer 
    523   1.175.2.2  bouyer int
    524   1.175.2.2  bouyer wdread(dev, uio, flags)
    525   1.175.2.2  bouyer 	dev_t dev;
    526   1.175.2.2  bouyer 	struct uio *uio;
    527   1.175.2.2  bouyer 	int flags;
    528   1.175.2.2  bouyer {
    529   1.175.2.2  bouyer 
    530   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdread\n"), DEBUG_FUNCS | DEBUG_XFERS);
    531   1.175.2.2  bouyer 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    532   1.175.2.2  bouyer }
    533   1.175.2.2  bouyer 
    534   1.175.2.2  bouyer int
    535   1.175.2.2  bouyer wdwrite(dev, uio, flags)
    536   1.175.2.2  bouyer 	dev_t dev;
    537   1.175.2.2  bouyer 	struct uio *uio;
    538   1.175.2.2  bouyer 	int flags;
    539   1.175.2.2  bouyer {
    540   1.175.2.2  bouyer 
    541   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_FUNCS | DEBUG_XFERS);
    542   1.175.2.2  bouyer 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    543   1.175.2.2  bouyer }
    544   1.175.2.2  bouyer 
    545   1.175.2.2  bouyer /*
    546   1.175.2.2  bouyer  * Wait interruptibly for an exclusive lock.
    547   1.175.2.2  bouyer  *
    548   1.175.2.2  bouyer  * XXX
    549   1.175.2.2  bouyer  * Several drivers do this; it should be abstracted and made MP-safe.
    550   1.175.2.2  bouyer  */
    551   1.175.2.2  bouyer int
    552   1.175.2.2  bouyer wdlock(wd)
    553   1.175.2.2  bouyer 	struct wd_softc *wd;
    554   1.175.2.2  bouyer {
    555   1.175.2.2  bouyer 	int error;
    556   1.175.2.2  bouyer 	int s;
    557   1.175.2.2  bouyer 
    558   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
    559   1.175.2.2  bouyer 
    560   1.175.2.2  bouyer 	s = splbio();
    561   1.175.2.2  bouyer 
    562   1.175.2.2  bouyer 	while ((wd->sc_flags & WDF_LOCKED) != 0) {
    563   1.175.2.2  bouyer 		wd->sc_flags |= WDF_WANTED;
    564   1.175.2.2  bouyer 		if ((error = tsleep(wd, PRIBIO | PCATCH,
    565   1.175.2.2  bouyer 		    "wdlck", 0)) != 0) {
    566   1.175.2.2  bouyer 			splx(s);
    567   1.175.2.2  bouyer 			return error;
    568   1.175.2.2  bouyer 		}
    569   1.175.2.2  bouyer 	}
    570   1.175.2.2  bouyer 	wd->sc_flags |= WDF_LOCKED;
    571   1.175.2.2  bouyer 	splx(s);
    572   1.175.2.2  bouyer 	return 0;
    573   1.175.2.2  bouyer }
    574   1.175.2.2  bouyer 
    575   1.175.2.2  bouyer /*
    576   1.175.2.2  bouyer  * Unlock and wake up any waiters.
    577   1.175.2.2  bouyer  */
    578   1.175.2.2  bouyer void
    579   1.175.2.2  bouyer wdunlock(wd)
    580   1.175.2.2  bouyer 	struct wd_softc *wd;
    581   1.175.2.2  bouyer {
    582   1.175.2.2  bouyer 
    583   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
    584   1.175.2.2  bouyer 
    585   1.175.2.2  bouyer 	wd->sc_flags &= ~WDF_LOCKED;
    586   1.175.2.2  bouyer 	if ((wd->sc_flags & WDF_WANTED) != 0) {
    587   1.175.2.2  bouyer 		wd->sc_flags &= ~WDF_WANTED;
    588   1.175.2.2  bouyer 		wakeup(wd);
    589   1.175.2.2  bouyer 	}
    590   1.175.2.2  bouyer }
    591   1.175.2.2  bouyer 
    592   1.175.2.2  bouyer int
    593   1.175.2.2  bouyer wdopen(dev, flag, fmt, p)
    594   1.175.2.2  bouyer 	dev_t dev;
    595   1.175.2.2  bouyer 	int flag, fmt;
    596   1.175.2.2  bouyer 	struct proc *p;
    597   1.175.2.2  bouyer {
    598   1.175.2.2  bouyer 	struct wd_softc *wd;
    599   1.175.2.2  bouyer 	int unit, part;
    600   1.175.2.2  bouyer 	int error;
    601   1.175.2.2  bouyer 
    602   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    603   1.175.2.2  bouyer 	unit = WDUNIT(dev);
    604   1.175.2.2  bouyer 	if (unit >= wd_cd.cd_ndevs)
    605   1.175.2.2  bouyer 		return ENXIO;
    606   1.175.2.2  bouyer 	wd = wd_cd.cd_devs[unit];
    607   1.175.2.2  bouyer 	if (wd == NULL)
    608   1.175.2.2  bouyer 		return ENXIO;
    609   1.175.2.2  bouyer 
    610   1.175.2.2  bouyer 	if ((error = wdlock(wd)) != 0)
    611   1.175.2.2  bouyer 		return error;
    612   1.175.2.2  bouyer 
    613   1.175.2.2  bouyer 	if (wd->sc_dk.dk_openmask != 0) {
    614   1.175.2.2  bouyer 		/*
    615   1.175.2.2  bouyer 		 * If any partition is open, but the disk has been invalidated,
    616   1.175.2.2  bouyer 		 * disallow further opens.
    617   1.175.2.2  bouyer 		 */
    618   1.175.2.2  bouyer 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    619   1.175.2.2  bouyer 			error = EIO;
    620   1.175.2.2  bouyer 			goto bad3;
    621   1.175.2.2  bouyer 		}
    622   1.175.2.2  bouyer 	} else {
    623   1.175.2.2  bouyer 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    624   1.175.2.2  bouyer 			wd->sc_flags |= WDF_LOADED;
    625   1.175.2.2  bouyer 
    626   1.175.2.2  bouyer 			/* Load the physical device parameters. */
    627   1.175.2.2  bouyer 			wd_get_params(wd, AT_POLL, &wd->sc_params);
    628   1.175.2.2  bouyer 
    629   1.175.2.2  bouyer 			/* Load the partition info if not already loaded. */
    630   1.175.2.2  bouyer 			wdgetdisklabel(wd);
    631   1.175.2.2  bouyer 		}
    632   1.175.2.2  bouyer 	}
    633   1.175.2.2  bouyer 
    634   1.175.2.2  bouyer 	part = WDPART(dev);
    635   1.175.2.2  bouyer 
    636   1.175.2.2  bouyer 	/* Check that the partition exists. */
    637   1.175.2.2  bouyer 	if (part != RAW_PART &&
    638   1.175.2.2  bouyer 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    639   1.175.2.2  bouyer 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    640   1.175.2.2  bouyer 		error = ENXIO;
    641   1.175.2.2  bouyer 		goto bad;
    642   1.175.2.2  bouyer 	}
    643   1.175.2.2  bouyer 
    644   1.175.2.2  bouyer 	/* Insure only one open at a time. */
    645   1.175.2.2  bouyer 	switch (fmt) {
    646   1.175.2.2  bouyer 	case S_IFCHR:
    647   1.175.2.2  bouyer 		wd->sc_dk.dk_copenmask |= (1 << part);
    648   1.175.2.2  bouyer 		break;
    649   1.175.2.2  bouyer 	case S_IFBLK:
    650   1.175.2.2  bouyer 		wd->sc_dk.dk_bopenmask |= (1 << part);
    651   1.175.2.2  bouyer 		break;
    652   1.175.2.2  bouyer 	}
    653   1.175.2.2  bouyer 	wd->sc_dk.dk_openmask =
    654   1.175.2.2  bouyer 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    655   1.175.2.2  bouyer 
    656   1.175.2.2  bouyer 	wdunlock(wd);
    657   1.175.2.2  bouyer 	return 0;
    658   1.175.2.2  bouyer 
    659   1.175.2.2  bouyer bad:
    660   1.175.2.2  bouyer 	if (wd->sc_dk.dk_openmask == 0) {
    661   1.175.2.2  bouyer 	}
    662   1.175.2.2  bouyer 
    663   1.175.2.2  bouyer bad3:
    664   1.175.2.2  bouyer 	wdunlock(wd);
    665   1.175.2.2  bouyer 	return error;
    666   1.175.2.2  bouyer }
    667   1.175.2.2  bouyer 
    668   1.175.2.2  bouyer int
    669   1.175.2.2  bouyer wdclose(dev, flag, fmt, p)
    670   1.175.2.2  bouyer 	dev_t dev;
    671   1.175.2.2  bouyer 	int flag, fmt;
    672   1.175.2.2  bouyer 	struct proc *p;
    673   1.175.2.2  bouyer {
    674   1.175.2.2  bouyer 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    675   1.175.2.2  bouyer 	int part = WDPART(dev);
    676   1.175.2.2  bouyer 	int error;
    677   1.175.2.2  bouyer 
    678   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    679   1.175.2.2  bouyer 	if ((error = wdlock(wd)) != 0)
    680   1.175.2.2  bouyer 		return error;
    681   1.175.2.2  bouyer 
    682   1.175.2.2  bouyer 	switch (fmt) {
    683   1.175.2.2  bouyer 	case S_IFCHR:
    684   1.175.2.2  bouyer 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    685   1.175.2.2  bouyer 		break;
    686   1.175.2.2  bouyer 	case S_IFBLK:
    687   1.175.2.2  bouyer 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    688   1.175.2.2  bouyer 		break;
    689   1.175.2.2  bouyer 	}
    690   1.175.2.2  bouyer 	wd->sc_dk.dk_openmask =
    691   1.175.2.2  bouyer 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    692   1.175.2.2  bouyer 
    693   1.175.2.2  bouyer 	if (wd->sc_dk.dk_openmask == 0) {
    694   1.175.2.2  bouyer 		/* XXXX Must wait for I/O to complete! */
    695   1.175.2.2  bouyer 	}
    696   1.175.2.2  bouyer 
    697   1.175.2.2  bouyer 	wdunlock(wd);
    698   1.175.2.2  bouyer 	return 0;
    699   1.175.2.2  bouyer }
    700   1.175.2.2  bouyer 
    701   1.175.2.2  bouyer void
    702   1.175.2.2  bouyer wdgetdefaultlabel(wd, lp)
    703   1.175.2.2  bouyer 	struct wd_softc *wd;
    704   1.175.2.2  bouyer 	struct disklabel *lp;
    705   1.175.2.2  bouyer {
    706   1.175.2.2  bouyer 
    707   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
    708   1.175.2.7  bouyer 	memset(lp, 0, sizeof(struct disklabel));
    709   1.175.2.2  bouyer 
    710   1.175.2.2  bouyer 	lp->d_secsize = DEV_BSIZE;
    711   1.175.2.2  bouyer 	lp->d_ntracks = wd->sc_params.atap_heads;
    712   1.175.2.2  bouyer 	lp->d_nsectors = wd->sc_params.atap_sectors;
    713   1.175.2.2  bouyer 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    714   1.175.2.2  bouyer 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    715   1.175.2.2  bouyer 
    716   1.175.2.9  bouyer #if 0
    717   1.175.2.2  bouyer 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0) {
    718   1.175.2.2  bouyer 		lp->d_type = DTYPE_ST506;
    719   1.175.2.2  bouyer 		strncpy(lp->d_typename, "ST506 disk", 16);
    720   1.175.2.2  bouyer 	} else {
    721   1.175.2.2  bouyer 		lp->d_type = DTYPE_ESDI;
    722   1.175.2.2  bouyer 		strncpy(lp->d_typename, "ESDI/IDE",
    723   1.175.2.2  bouyer 		sizeof lp->d_typename);
    724   1.175.2.2  bouyer 	}
    725   1.175.2.9  bouyer #endif
    726   1.175.2.9  bouyer 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
    727   1.175.2.9  bouyer 	strncpy(lp->d_packname, "fictitious", 16);
    728   1.175.2.2  bouyer 	lp->d_secperunit = wd->sc_capacity;
    729   1.175.2.2  bouyer 	lp->d_rpm = 3600;
    730   1.175.2.2  bouyer 	lp->d_interleave = 1;
    731   1.175.2.2  bouyer 	lp->d_flags = 0;
    732   1.175.2.2  bouyer 
    733   1.175.2.2  bouyer 	lp->d_partitions[RAW_PART].p_offset = 0;
    734   1.175.2.2  bouyer 	lp->d_partitions[RAW_PART].p_size =
    735   1.175.2.2  bouyer 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    736   1.175.2.2  bouyer 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    737   1.175.2.2  bouyer 	lp->d_npartitions = RAW_PART + 1;
    738   1.175.2.2  bouyer 
    739   1.175.2.2  bouyer 	lp->d_magic = DISKMAGIC;
    740   1.175.2.2  bouyer 	lp->d_magic2 = DISKMAGIC;
    741   1.175.2.2  bouyer 	lp->d_checksum = dkcksum(lp);
    742   1.175.2.2  bouyer }
    743   1.175.2.2  bouyer 
    744   1.175.2.2  bouyer /*
    745   1.175.2.2  bouyer  * Fabricate a default disk label, and try to read the correct one.
    746   1.175.2.2  bouyer  */
    747   1.175.2.2  bouyer void
    748   1.175.2.2  bouyer wdgetdisklabel(wd)
    749   1.175.2.2  bouyer 	struct wd_softc *wd;
    750   1.175.2.2  bouyer {
    751   1.175.2.2  bouyer 	struct disklabel *lp = wd->sc_dk.dk_label;
    752   1.175.2.2  bouyer 	char *errstring;
    753   1.175.2.2  bouyer 
    754   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
    755   1.175.2.2  bouyer 
    756   1.175.2.7  bouyer 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    757   1.175.2.2  bouyer 
    758   1.175.2.2  bouyer 	wdgetdefaultlabel(wd, lp);
    759   1.175.2.2  bouyer 
    760   1.175.2.2  bouyer 	wd->sc_badsect[0] = -1;
    761   1.175.2.2  bouyer 
    762   1.175.2.2  bouyer 	if (wd->drvp->state > RECAL)
    763   1.175.2.2  bouyer 		wd->drvp->state = RECAL;
    764   1.175.2.2  bouyer 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
    765   1.175.2.2  bouyer 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    766   1.175.2.2  bouyer 	if (errstring) {
    767   1.175.2.2  bouyer 		/*
    768   1.175.2.2  bouyer 		 * This probably happened because the drive's default
    769   1.175.2.2  bouyer 		 * geometry doesn't match the DOS geometry.  We
    770   1.175.2.2  bouyer 		 * assume the DOS geometry is now in the label and try
    771   1.175.2.2  bouyer 		 * again.  XXX This is a kluge.
    772   1.175.2.2  bouyer 		 */
    773   1.175.2.2  bouyer 		if (wd->drvp->state > RECAL)
    774   1.175.2.2  bouyer 			wd->drvp->state = RECAL;
    775   1.175.2.2  bouyer 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
    776   1.175.2.2  bouyer 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    777   1.175.2.2  bouyer 	}
    778   1.175.2.2  bouyer 	if (errstring) {
    779   1.175.2.2  bouyer 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
    780   1.175.2.2  bouyer 		return;
    781   1.175.2.2  bouyer 	}
    782   1.175.2.2  bouyer 
    783   1.175.2.2  bouyer 	if (wd->drvp->state > RECAL)
    784   1.175.2.2  bouyer 		wd->drvp->state = RECAL;
    785   1.175.2.2  bouyer #ifdef HAS_BAD144_HANDLING
    786   1.175.2.2  bouyer 	if ((lp->d_flags & D_BADSECT) != 0)
    787   1.175.2.2  bouyer 		bad144intern(wd);
    788   1.175.2.2  bouyer #endif
    789   1.175.2.2  bouyer }
    790   1.175.2.2  bouyer 
    791   1.175.2.2  bouyer int
    792   1.175.2.2  bouyer wdioctl(dev, xfer, addr, flag, p)
    793   1.175.2.2  bouyer 	dev_t dev;
    794   1.175.2.2  bouyer 	u_long xfer;
    795   1.175.2.2  bouyer 	caddr_t addr;
    796   1.175.2.2  bouyer 	int flag;
    797   1.175.2.2  bouyer 	struct proc *p;
    798   1.175.2.2  bouyer {
    799   1.175.2.2  bouyer 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    800   1.175.2.2  bouyer 	int error;
    801   1.175.2.2  bouyer 
    802   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
    803   1.175.2.2  bouyer 
    804   1.175.2.2  bouyer 	if ((wd->sc_flags & WDF_LOADED) == 0)
    805   1.175.2.2  bouyer 		return EIO;
    806   1.175.2.2  bouyer 
    807   1.175.2.2  bouyer 	switch (xfer) {
    808   1.175.2.2  bouyer #ifdef HAS_BAD144_HANDLING
    809   1.175.2.2  bouyer 	case DIOCSBAD:
    810   1.175.2.2  bouyer 		if ((flag & FWRITE) == 0)
    811   1.175.2.2  bouyer 			return EBADF;
    812   1.175.2.2  bouyer 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
    813   1.175.2.2  bouyer 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
    814   1.175.2.2  bouyer 		bad144intern(wd);
    815   1.175.2.2  bouyer 		return 0;
    816   1.175.2.2  bouyer #endif
    817   1.175.2.2  bouyer 
    818   1.175.2.2  bouyer 	case DIOCGDINFO:
    819   1.175.2.2  bouyer 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
    820   1.175.2.2  bouyer 		return 0;
    821   1.175.2.2  bouyer 
    822   1.175.2.2  bouyer 	case DIOCGPART:
    823   1.175.2.2  bouyer 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
    824   1.175.2.2  bouyer 		((struct partinfo *)addr)->part =
    825   1.175.2.2  bouyer 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
    826   1.175.2.2  bouyer 		return 0;
    827   1.175.2.2  bouyer 
    828   1.175.2.2  bouyer 	case DIOCWDINFO:
    829   1.175.2.2  bouyer 	case DIOCSDINFO:
    830   1.175.2.2  bouyer 		if ((flag & FWRITE) == 0)
    831   1.175.2.2  bouyer 			return EBADF;
    832   1.175.2.2  bouyer 
    833   1.175.2.2  bouyer 		if ((error = wdlock(wd)) != 0)
    834   1.175.2.2  bouyer 			return error;
    835   1.175.2.2  bouyer 		wd->sc_flags |= WDF_LABELLING;
    836   1.175.2.2  bouyer 
    837   1.175.2.2  bouyer 		error = setdisklabel(wd->sc_dk.dk_label,
    838   1.175.2.2  bouyer 		    (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
    839   1.175.2.2  bouyer 		    wd->sc_dk.dk_cpulabel);
    840   1.175.2.2  bouyer 		if (error == 0) {
    841   1.175.2.2  bouyer 			if (wd->drvp->state > RECAL)
    842   1.175.2.2  bouyer 				wd->drvp->state = RECAL;
    843   1.175.2.2  bouyer 			if (xfer == DIOCWDINFO)
    844   1.175.2.2  bouyer 				error = writedisklabel(WDLABELDEV(dev),
    845   1.175.2.2  bouyer 				    wdstrategy, wd->sc_dk.dk_label,
    846   1.175.2.2  bouyer 				    wd->sc_dk.dk_cpulabel);
    847   1.175.2.2  bouyer 		}
    848   1.175.2.2  bouyer 
    849   1.175.2.2  bouyer 		wd->sc_flags &= ~WDF_LABELLING;
    850   1.175.2.2  bouyer 		wdunlock(wd);
    851   1.175.2.2  bouyer 		return error;
    852   1.175.2.2  bouyer 
    853   1.175.2.2  bouyer 	case DIOCWLABEL:
    854   1.175.2.2  bouyer 		if ((flag & FWRITE) == 0)
    855   1.175.2.2  bouyer 			return EBADF;
    856   1.175.2.2  bouyer 		if (*(int *)addr)
    857   1.175.2.2  bouyer 			wd->sc_flags |= WDF_WLABEL;
    858   1.175.2.2  bouyer 		else
    859   1.175.2.2  bouyer 			wd->sc_flags &= ~WDF_WLABEL;
    860   1.175.2.2  bouyer 		return 0;
    861   1.175.2.2  bouyer 
    862   1.175.2.2  bouyer 	case DIOCGDEFLABEL:
    863   1.175.2.2  bouyer 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
    864   1.175.2.2  bouyer 		return 0;
    865   1.175.2.2  bouyer 
    866   1.175.2.2  bouyer #ifdef notyet
    867   1.175.2.2  bouyer 	case DIOCWFORMAT:
    868   1.175.2.2  bouyer 		if ((flag & FWRITE) == 0)
    869   1.175.2.2  bouyer 			return EBADF;
    870   1.175.2.2  bouyer 		{
    871   1.175.2.2  bouyer 		register struct format_op *fop;
    872   1.175.2.2  bouyer 		struct iovec aiov;
    873   1.175.2.2  bouyer 		struct uio auio;
    874   1.175.2.2  bouyer 
    875   1.175.2.2  bouyer 		fop = (struct format_op *)addr;
    876   1.175.2.2  bouyer 		aiov.iov_base = fop->df_buf;
    877   1.175.2.2  bouyer 		aiov.iov_len = fop->df_count;
    878   1.175.2.2  bouyer 		auio.uio_iov = &aiov;
    879   1.175.2.2  bouyer 		auio.uio_iovcnt = 1;
    880   1.175.2.2  bouyer 		auio.uio_resid = fop->df_count;
    881   1.175.2.2  bouyer 		auio.uio_segflg = 0;
    882   1.175.2.2  bouyer 		auio.uio_offset =
    883   1.175.2.2  bouyer 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
    884   1.175.2.2  bouyer 		auio.uio_procp = p;
    885   1.175.2.2  bouyer 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
    886   1.175.2.2  bouyer 		    &auio);
    887   1.175.2.2  bouyer 		fop->df_count -= auio.uio_resid;
    888   1.175.2.2  bouyer 		fop->df_reg[0] = wdc->sc_status;
    889   1.175.2.2  bouyer 		fop->df_reg[1] = wdc->sc_error;
    890   1.175.2.2  bouyer 		return error;
    891   1.175.2.2  bouyer 		}
    892   1.175.2.2  bouyer #endif
    893   1.175.2.2  bouyer 
    894   1.175.2.2  bouyer 	default:
    895   1.175.2.2  bouyer 		return ENOTTY;
    896   1.175.2.2  bouyer 	}
    897   1.175.2.2  bouyer 
    898   1.175.2.2  bouyer #ifdef DIAGNOSTIC
    899   1.175.2.2  bouyer 	panic("wdioctl: impossible");
    900   1.175.2.2  bouyer #endif
    901   1.175.2.2  bouyer }
    902   1.175.2.2  bouyer 
    903   1.175.2.2  bouyer #ifdef B_FORMAT
    904   1.175.2.2  bouyer int
    905   1.175.2.2  bouyer wdformat(struct buf *bp)
    906   1.175.2.2  bouyer {
    907   1.175.2.2  bouyer 
    908   1.175.2.2  bouyer 	bp->b_flags |= B_FORMAT;
    909   1.175.2.2  bouyer 	return wdstrategy(bp);
    910   1.175.2.2  bouyer }
    911   1.175.2.2  bouyer #endif
    912   1.175.2.2  bouyer 
    913   1.175.2.2  bouyer int
    914   1.175.2.2  bouyer wdsize(dev)
    915   1.175.2.2  bouyer 	dev_t dev;
    916   1.175.2.2  bouyer {
    917   1.175.2.2  bouyer 	struct wd_softc *wd;
    918   1.175.2.2  bouyer 	int part, unit, omask;
    919   1.175.2.2  bouyer 	int size;
    920   1.175.2.2  bouyer 
    921   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
    922   1.175.2.2  bouyer 
    923   1.175.2.2  bouyer 	unit = WDUNIT(dev);
    924   1.175.2.2  bouyer 	if (unit >= wd_cd.cd_ndevs)
    925   1.175.2.2  bouyer 		return (-1);
    926   1.175.2.2  bouyer 	wd = wd_cd.cd_devs[unit];
    927   1.175.2.2  bouyer 	if (wd == NULL)
    928   1.175.2.2  bouyer 		return (-1);
    929   1.175.2.2  bouyer 
    930   1.175.2.2  bouyer 	part = WDPART(dev);
    931   1.175.2.2  bouyer 	omask = wd->sc_dk.dk_openmask & (1 << part);
    932   1.175.2.2  bouyer 
    933   1.175.2.2  bouyer 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
    934   1.175.2.2  bouyer 		return (-1);
    935   1.175.2.2  bouyer 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    936   1.175.2.2  bouyer 		size = -1;
    937   1.175.2.2  bouyer 	else
    938   1.175.2.2  bouyer 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
    939   1.175.2.2  bouyer 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    940   1.175.2.2  bouyer 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
    941   1.175.2.2  bouyer 		return (-1);
    942   1.175.2.2  bouyer 	return (size);
    943   1.175.2.2  bouyer }
    944   1.175.2.2  bouyer 
    945   1.175.2.2  bouyer #ifndef __BDEVSW_DUMP_OLD_TYPE
    946   1.175.2.2  bouyer /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
    947   1.175.2.7  bouyer static int wddoingadump = 0;
    948   1.175.2.7  bouyer static int wddumprecalibrated = 0;
    949   1.175.2.7  bouyer static int wddumpmulti = 1;
    950   1.175.2.2  bouyer 
    951   1.175.2.2  bouyer /*
    952   1.175.2.2  bouyer  * Dump core after a system crash.
    953   1.175.2.2  bouyer  */
    954   1.175.2.2  bouyer int
    955   1.175.2.2  bouyer wddump(dev, blkno, va, size)
    956   1.175.2.2  bouyer 	dev_t dev;
    957   1.175.2.2  bouyer 	daddr_t blkno;
    958   1.175.2.2  bouyer 	caddr_t va;
    959   1.175.2.2  bouyer 	size_t size;
    960   1.175.2.2  bouyer {
    961   1.175.2.2  bouyer 	struct wd_softc *wd;	/* disk unit to do the I/O */
    962   1.175.2.2  bouyer 	struct disklabel *lp;   /* disk's disklabel */
    963   1.175.2.2  bouyer 	int unit, part;
    964   1.175.2.2  bouyer 	int nblks;	/* total number of sectors left to write */
    965   1.175.2.7  bouyer 	int err;
    966   1.175.2.2  bouyer 	char errbuf[256];
    967   1.175.2.2  bouyer 
    968   1.175.2.2  bouyer 	/* Check if recursive dump; if so, punt. */
    969   1.175.2.2  bouyer 	if (wddoingadump)
    970   1.175.2.2  bouyer 		return EFAULT;
    971   1.175.2.2  bouyer 	wddoingadump = 1;
    972   1.175.2.2  bouyer 
    973   1.175.2.2  bouyer 	unit = WDUNIT(dev);
    974   1.175.2.2  bouyer 	if (unit >= wd_cd.cd_ndevs)
    975   1.175.2.2  bouyer 		return ENXIO;
    976   1.175.2.2  bouyer 	wd = wd_cd.cd_devs[unit];
    977   1.175.2.2  bouyer 	if (wd == (struct wd_softc *)0)
    978   1.175.2.2  bouyer 		return ENXIO;
    979   1.175.2.2  bouyer 
    980   1.175.2.2  bouyer 	part = WDPART(dev);
    981   1.175.2.2  bouyer 
    982   1.175.2.2  bouyer 	/* Make sure it was initialized. */
    983   1.175.2.2  bouyer 	if (wd->drvp->state < READY)
    984   1.175.2.2  bouyer 		return ENXIO;
    985   1.175.2.2  bouyer 
    986   1.175.2.2  bouyer 	/* Convert to disk sectors.  Request must be a multiple of size. */
    987   1.175.2.2  bouyer 	lp = wd->sc_dk.dk_label;
    988   1.175.2.2  bouyer 	if ((size % lp->d_secsize) != 0)
    989   1.175.2.2  bouyer 		return EFAULT;
    990   1.175.2.2  bouyer 	nblks = size / lp->d_secsize;
    991   1.175.2.2  bouyer 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
    992   1.175.2.2  bouyer 
    993   1.175.2.2  bouyer 	/* Check transfer bounds against partition size. */
    994   1.175.2.2  bouyer 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
    995   1.175.2.2  bouyer 		return EINVAL;
    996   1.175.2.2  bouyer 
    997   1.175.2.2  bouyer 	/* Offset block number to start of partition. */
    998   1.175.2.2  bouyer 	blkno += lp->d_partitions[part].p_offset;
    999   1.175.2.2  bouyer 
   1000   1.175.2.2  bouyer 	/* Recalibrate, if first dump transfer. */
   1001   1.175.2.2  bouyer 	if (wddumprecalibrated == 0) {
   1002   1.175.2.7  bouyer 		wddumpmulti = wd->sc_multi;
   1003   1.175.2.2  bouyer 		wddumprecalibrated = 1;
   1004   1.175.2.2  bouyer 		wd->drvp->state = RECAL;
   1005   1.175.2.2  bouyer 	}
   1006   1.175.2.7  bouyer 
   1007   1.175.2.2  bouyer 	while (nblks > 0) {
   1008   1.175.2.7  bouyer again:
   1009   1.175.2.2  bouyer 		wd->sc_wdc_bio.blkno = blkno;
   1010   1.175.2.2  bouyer 		wd->sc_wdc_bio.flags = ATA_POLL;
   1011   1.175.2.7  bouyer 		if (wddumpmulti == 1)
   1012   1.175.2.2  bouyer 			wd->sc_wdc_bio.flags |= ATA_SINGLE;
   1013   1.175.2.2  bouyer 		if (wd->sc_flags & WDF_LBA)
   1014   1.175.2.2  bouyer 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1015   1.175.2.2  bouyer 		wd->sc_wdc_bio.bcount =
   1016   1.175.2.7  bouyer 			min(nblks, wddumpmulti) * lp->d_secsize;
   1017   1.175.2.2  bouyer 		wd->sc_wdc_bio.databuf = va;
   1018   1.175.2.2  bouyer #ifndef WD_DUMP_NOT_TRUSTED
   1019   1.175.2.2  bouyer 		switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1020   1.175.2.2  bouyer 		case WDC_TRY_AGAIN:
   1021   1.175.2.2  bouyer 			panic("wddump: try again");
   1022   1.175.2.2  bouyer 			break;
   1023   1.175.2.2  bouyer 		case WDC_QUEUED:
   1024   1.175.2.2  bouyer 			panic("wddump: polled command has been queued");
   1025   1.175.2.2  bouyer 			break;
   1026   1.175.2.2  bouyer 		case WDC_COMPLETE:
   1027   1.175.2.2  bouyer 			break;
   1028   1.175.2.2  bouyer 		}
   1029   1.175.2.2  bouyer 		switch(wd->sc_wdc_bio.error) {
   1030   1.175.2.2  bouyer 		case TIMEOUT:
   1031   1.175.2.7  bouyer 			printf("wddump: device timed out");
   1032   1.175.2.7  bouyer 			err = EIO;
   1033   1.175.2.7  bouyer 			break;
   1034   1.175.2.2  bouyer 		case ERR_DF:
   1035   1.175.2.7  bouyer 			printf("wddump: drive fault");
   1036   1.175.2.7  bouyer 			err = EIO;
   1037   1.175.2.7  bouyer 			break;
   1038   1.175.2.2  bouyer 		case ERR_DMA:
   1039   1.175.2.7  bouyer 			printf("wddump: DMA error");
   1040   1.175.2.7  bouyer 			err = EIO;
   1041   1.175.2.7  bouyer 			break;
   1042   1.175.2.2  bouyer 		case ERROR:
   1043   1.175.2.2  bouyer 			errbuf[0] = '\0';
   1044   1.175.2.2  bouyer 			print_wderror(wd->sc_wdc_bio.r_error, errbuf);
   1045   1.175.2.7  bouyer 			printf("wddump: %s", errbuf);
   1046   1.175.2.7  bouyer 			err = EIO;
   1047   1.175.2.7  bouyer 			break;
   1048   1.175.2.2  bouyer 		case NOERROR:
   1049   1.175.2.7  bouyer 			err = 0;
   1050   1.175.2.2  bouyer 			break;
   1051   1.175.2.2  bouyer 		default:
   1052   1.175.2.2  bouyer 			panic("wddump: unknown error type");
   1053   1.175.2.2  bouyer 		}
   1054   1.175.2.7  bouyer 		if (err != 0) {
   1055   1.175.2.7  bouyer 			if (wddumpmulti != 1) {
   1056   1.175.2.7  bouyer 				wddumpmulti = 1; /* retry in single-sector */
   1057   1.175.2.7  bouyer 				printf(", retrying\n");
   1058   1.175.2.7  bouyer 				goto again;
   1059   1.175.2.7  bouyer 			}
   1060   1.175.2.7  bouyer 			printf("\n");
   1061   1.175.2.7  bouyer 			return err;
   1062   1.175.2.7  bouyer 		}
   1063   1.175.2.2  bouyer #else	/* WD_DUMP_NOT_TRUSTED */
   1064   1.175.2.2  bouyer 		/* Let's just talk about this first... */
   1065   1.175.2.2  bouyer 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1066   1.175.2.2  bouyer 		    unit, va, cylin, head, sector);
   1067   1.175.2.2  bouyer 		delay(500 * 1000);	/* half a second */
   1068   1.175.2.2  bouyer #endif
   1069   1.175.2.2  bouyer 
   1070   1.175.2.2  bouyer 		/* update block count */
   1071   1.175.2.7  bouyer 		nblks -= min(nblks, wddumpmulti);
   1072   1.175.2.7  bouyer 		blkno += min(nblks, wddumpmulti);
   1073   1.175.2.7  bouyer 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1074   1.175.2.2  bouyer 	}
   1075   1.175.2.2  bouyer 
   1076   1.175.2.2  bouyer 	wddoingadump = 0;
   1077   1.175.2.2  bouyer 	return 0;
   1078   1.175.2.2  bouyer }
   1079   1.175.2.2  bouyer #else /* __BDEVSW_DUMP_NEW_TYPE */
   1080   1.175.2.2  bouyer 
   1081   1.175.2.2  bouyer 
   1082   1.175.2.2  bouyer int
   1083   1.175.2.2  bouyer wddump(dev, blkno, va, size)
   1084   1.175.2.2  bouyer 	dev_t dev;
   1085   1.175.2.2  bouyer 	daddr_t blkno;
   1086   1.175.2.2  bouyer 	caddr_t va;
   1087   1.175.2.2  bouyer 	size_t size;
   1088   1.175.2.2  bouyer {
   1089   1.175.2.2  bouyer 
   1090   1.175.2.2  bouyer 	/* Not implemented. */
   1091   1.175.2.2  bouyer 	return ENXIO;
   1092   1.175.2.2  bouyer }
   1093   1.175.2.2  bouyer #endif /* __BDEVSW_DUMP_NEW_TYPE */
   1094   1.175.2.2  bouyer 
   1095   1.175.2.2  bouyer #ifdef HAS_BAD144_HANDLING
   1096   1.175.2.2  bouyer /*
   1097   1.175.2.2  bouyer  * Internalize the bad sector table.
   1098   1.175.2.2  bouyer  */
   1099   1.175.2.2  bouyer void
   1100   1.175.2.2  bouyer bad144intern(wd)
   1101   1.175.2.2  bouyer 	struct wd_softc *wd;
   1102   1.175.2.2  bouyer {
   1103   1.175.2.2  bouyer 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1104   1.175.2.2  bouyer 	struct disklabel *lp = wd->sc_dk.dk_label;
   1105   1.175.2.2  bouyer 	int i = 0;
   1106   1.175.2.2  bouyer 
   1107   1.175.2.2  bouyer 	WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_FUNCS | DEBUG_XFERS);
   1108   1.175.2.2  bouyer 
   1109   1.175.2.2  bouyer 	for (; i < NBT_BAD; i++) {
   1110   1.175.2.2  bouyer 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1111   1.175.2.2  bouyer 			break;
   1112   1.175.2.2  bouyer 		wd->sc_badsect[i] =
   1113   1.175.2.2  bouyer 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1114   1.175.2.2  bouyer 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1115   1.175.2.2  bouyer 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1116   1.175.2.2  bouyer 	}
   1117   1.175.2.2  bouyer 	for (; i < NBT_BAD+1; i++)
   1118   1.175.2.2  bouyer 		wd->sc_badsect[i] = -1;
   1119   1.175.2.2  bouyer }
   1120   1.175.2.2  bouyer #endif
   1121   1.175.2.2  bouyer 
   1122   1.175.2.2  bouyer void
   1123   1.175.2.2  bouyer print_wderror(errno, buf)
   1124   1.175.2.2  bouyer 	int errno;
   1125   1.175.2.2  bouyer 	char *buf;
   1126   1.175.2.2  bouyer {
   1127   1.175.2.2  bouyer 	static char *errstr[] = {"address mark not found", "track 0 not found",
   1128   1.175.2.2  bouyer 	"aborted command", "media change requested", "id not found",
   1129   1.175.2.2  bouyer 	"media changed", "uncorrectable data error", "bad block detected"};
   1130   1.175.2.2  bouyer 	int i;
   1131   1.175.2.2  bouyer 	char *sep = "";
   1132   1.175.2.2  bouyer 
   1133   1.175.2.2  bouyer 	if (errno == 0) {
   1134   1.175.2.2  bouyer 		sprintf(buf, "error not notified");
   1135   1.175.2.2  bouyer 	}
   1136   1.175.2.2  bouyer 
   1137   1.175.2.2  bouyer 	for (i = 0; i < 8; i++) {
   1138   1.175.2.2  bouyer 		if (errno & (1 << i)) {
   1139   1.175.2.2  bouyer 			buf += sprintf(buf, "%s %s", sep, errstr[i]);
   1140   1.175.2.2  bouyer 			sep = ",";
   1141   1.175.2.2  bouyer 		}
   1142   1.175.2.2  bouyer 	}
   1143   1.175.2.2  bouyer }
   1144   1.175.2.2  bouyer 
   1145   1.175.2.2  bouyer int
   1146   1.175.2.2  bouyer wd_get_params(wd, flags, params)
   1147   1.175.2.2  bouyer 	struct wd_softc *wd;
   1148   1.175.2.2  bouyer 	u_int8_t flags;
   1149   1.175.2.2  bouyer 	struct ataparams *params;
   1150   1.175.2.2  bouyer {
   1151   1.175.2.2  bouyer 	switch (ata_get_params(wd->drvp, flags, params)) {
   1152   1.175.2.2  bouyer 	case CMD_AGAIN:
   1153   1.175.2.2  bouyer 		return 1;
   1154   1.175.2.2  bouyer 	case CMD_ERR:
   1155   1.175.2.2  bouyer 		/*
   1156   1.175.2.2  bouyer 		 * We `know' there's a drive here; just assume it's old.
   1157   1.175.2.2  bouyer 		 * This geometry is only used to read the MBR and print a
   1158   1.175.2.2  bouyer 		 * (false) attach message.
   1159   1.175.2.2  bouyer 		 */
   1160   1.175.2.2  bouyer 		strncpy(params->atap_model, "ST506",
   1161   1.175.2.2  bouyer 		    sizeof params->atap_model);
   1162   1.175.2.2  bouyer 		params->atap_config = ATA_CFG_FIXED;
   1163   1.175.2.2  bouyer 		params->atap_cylinders = 1024;
   1164   1.175.2.2  bouyer 		params->atap_heads = 8;
   1165   1.175.2.2  bouyer 		params->atap_sectors = 17;
   1166   1.175.2.2  bouyer 		params->atap_multi = 1;
   1167   1.175.2.2  bouyer 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1168   1.175.2.2  bouyer 		return 0;
   1169   1.175.2.2  bouyer 	case CMD_OK:
   1170   1.175.2.2  bouyer 		return 0;
   1171   1.175.2.2  bouyer 	default:
   1172   1.175.2.2  bouyer 		panic("wd_get_params: bad return code from ata_get_params");
   1173   1.175.2.2  bouyer 		/* NOTREACHED */
   1174   1.175.2.2  bouyer 	}
   1175   1.175.2.2  bouyer }
   1176