Home | History | Annotate | Line # | Download | only in ata
ata.c revision 1.22
      1  1.22  thorpej /*      $NetBSD: ata.c,v 1.22 2003/12/30 00:43:31 thorpej Exp $      */
      2  1.21  thorpej 
      3   1.2   bouyer /*
      4  1.16   bouyer  * Copyright (c) 1998, 2001 Manuel Bouyer.  All rights reserved.
      5   1.2   bouyer  *
      6   1.2   bouyer  * Redistribution and use in source and binary forms, with or without
      7   1.2   bouyer  * modification, are permitted provided that the following conditions
      8   1.2   bouyer  * are met:
      9   1.2   bouyer  * 1. Redistributions of source code must retain the above copyright
     10   1.2   bouyer  *    notice, this list of conditions and the following disclaimer.
     11   1.2   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.2   bouyer  *    notice, this list of conditions and the following disclaimer in the
     13   1.2   bouyer  *    documentation and/or other materials provided with the distribution.
     14   1.2   bouyer  * 3. All advertising materials mentioning features or use of this software
     15   1.2   bouyer  *    must display the following acknowledgement:
     16   1.2   bouyer  *  This product includes software developed by Manuel Bouyer.
     17   1.2   bouyer  * 4. The name of the author may not be used to endorse or promote products
     18   1.2   bouyer  *    derived from this software without specific prior written permission.
     19   1.2   bouyer  *
     20   1.2   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.2   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.2   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.13   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.2   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.2   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.2   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.2   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.2   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.2   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.2   bouyer  */
     31  1.14    lukem 
     32  1.14    lukem #include <sys/cdefs.h>
     33  1.22  thorpej __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.22 2003/12/30 00:43:31 thorpej Exp $");
     34   1.2   bouyer 
     35   1.6  hubertf #ifndef WDCDEBUG
     36   1.2   bouyer #define WDCDEBUG
     37   1.6  hubertf #endif /* WDCDEBUG */
     38   1.2   bouyer 
     39   1.2   bouyer #include <sys/param.h>
     40   1.2   bouyer #include <sys/systm.h>
     41   1.2   bouyer #include <sys/kernel.h>
     42   1.2   bouyer #include <sys/file.h>
     43   1.2   bouyer #include <sys/stat.h>
     44   1.2   bouyer #include <sys/malloc.h>
     45   1.2   bouyer #include <sys/device.h>
     46   1.2   bouyer #include <sys/syslog.h>
     47   1.2   bouyer 
     48  1.15   bouyer #include <machine/intr.h>
     49  1.15   bouyer #include <machine/bus.h>
     50  1.15   bouyer 
     51   1.2   bouyer #include <dev/ata/atareg.h>
     52   1.2   bouyer #include <dev/ata/atavar.h>
     53  1.15   bouyer #include <dev/ic/wdcreg.h>
     54  1.15   bouyer #include <dev/ic/wdcvar.h>
     55   1.2   bouyer 
     56   1.2   bouyer #define DEBUG_FUNCS  0x08
     57   1.2   bouyer #define DEBUG_PROBE  0x10
     58   1.2   bouyer #ifdef WDCDEBUG
     59   1.2   bouyer extern int wdcdebug_mask; /* init'ed in wdc.c */
     60   1.2   bouyer #define WDCDEBUG_PRINT(args, level) \
     61   1.2   bouyer         if (wdcdebug_mask & (level)) \
     62   1.2   bouyer 		printf args
     63   1.2   bouyer #else
     64   1.2   bouyer #define WDCDEBUG_PRINT(args, level)
     65   1.2   bouyer #endif
     66   1.2   bouyer 
     67   1.2   bouyer /* Get the disk's parameters */
     68   1.2   bouyer int
     69  1.21  thorpej ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
     70  1.21  thorpej     struct ataparams *prms)
     71   1.2   bouyer {
     72   1.2   bouyer 	char tb[DEV_BSIZE];
     73   1.2   bouyer 	struct wdc_command wdc_c;
     74   1.2   bouyer 
     75   1.2   bouyer #if BYTE_ORDER == LITTLE_ENDIAN
     76   1.2   bouyer 	int i;
     77   1.2   bouyer 	u_int16_t *p;
     78   1.2   bouyer #endif
     79   1.2   bouyer 
     80  1.22  thorpej 	WDCDEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
     81   1.2   bouyer 
     82   1.2   bouyer 	memset(tb, 0, DEV_BSIZE);
     83   1.2   bouyer 	memset(prms, 0, sizeof(struct ataparams));
     84   1.2   bouyer 	memset(&wdc_c, 0, sizeof(struct wdc_command));
     85   1.2   bouyer 
     86   1.2   bouyer 	if (drvp->drive_flags & DRIVE_ATA) {
     87   1.2   bouyer 		wdc_c.r_command = WDCC_IDENTIFY;
     88   1.5   bouyer 		wdc_c.r_st_bmask = WDCS_DRDY;
     89  1.19  mycroft 		wdc_c.r_st_pmask = 0;
     90  1.18   bouyer 		wdc_c.timeout = 3000; /* 3s */
     91   1.7   bouyer 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
     92   1.2   bouyer 		wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
     93   1.2   bouyer 		wdc_c.r_st_bmask = 0;
     94  1.19  mycroft 		wdc_c.r_st_pmask = 0;
     95   1.9   bouyer 		wdc_c.timeout = 10000; /* 10s */
     96   1.7   bouyer 	} else {
     97  1.22  thorpej 		WDCDEBUG_PRINT(("ata_get_parms: no disks\n"),
     98  1.10   bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
     99   1.7   bouyer 		return CMD_ERR;
    100   1.2   bouyer 	}
    101   1.2   bouyer 	wdc_c.flags = AT_READ | flags;
    102   1.2   bouyer 	wdc_c.data = tb;
    103   1.2   bouyer 	wdc_c.bcount = DEV_BSIZE;
    104  1.10   bouyer 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) {
    105  1.22  thorpej 		WDCDEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
    106  1.10   bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    107   1.2   bouyer 		return CMD_AGAIN;
    108  1.10   bouyer 	}
    109   1.2   bouyer 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    110  1.22  thorpej 		WDCDEBUG_PRINT(("ata_get_parms: wdc_c.flags=0x%x\n",
    111  1.10   bouyer 		    wdc_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
    112   1.2   bouyer 		return CMD_ERR;
    113   1.2   bouyer 	} else {
    114  1.17   bouyer 		/* if we didn't read any data something is wrong */
    115  1.17   bouyer 		if ((wdc_c.flags & AT_XFDONE) == 0)
    116  1.17   bouyer 			return CMD_ERR;
    117   1.2   bouyer 		/* Read in parameter block. */
    118   1.2   bouyer 		memcpy(prms, tb, sizeof(struct ataparams));
    119   1.2   bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    120   1.2   bouyer 		/*
    121   1.2   bouyer 		 * Shuffle string byte order.
    122   1.2   bouyer 		 * ATAPI Mitsumi and NEC drives don't need this.
    123   1.2   bouyer 		 */
    124   1.2   bouyer 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
    125   1.2   bouyer 		    WDC_CFG_ATAPI &&
    126   1.2   bouyer 		    ((prms->atap_model[0] == 'N' &&
    127   1.2   bouyer 			prms->atap_model[1] == 'E') ||
    128   1.2   bouyer 		     (prms->atap_model[0] == 'F' &&
    129   1.2   bouyer 			 prms->atap_model[1] == 'X')))
    130   1.2   bouyer 			return 0;
    131   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
    132   1.2   bouyer 			p = (u_short *)(prms->atap_model + i);
    133   1.2   bouyer 			*p = ntohs(*p);
    134   1.2   bouyer 		}
    135   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
    136   1.2   bouyer 			p = (u_short *)(prms->atap_serial + i);
    137   1.2   bouyer 			*p = ntohs(*p);
    138   1.2   bouyer 		}
    139   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
    140   1.2   bouyer 			p = (u_short *)(prms->atap_revision + i);
    141   1.2   bouyer 			*p = ntohs(*p);
    142   1.2   bouyer 		}
    143   1.2   bouyer #endif
    144   1.2   bouyer 		return CMD_OK;
    145   1.2   bouyer 	}
    146   1.2   bouyer }
    147   1.2   bouyer 
    148   1.2   bouyer int
    149  1.21  thorpej ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
    150   1.2   bouyer {
    151   1.2   bouyer 	struct wdc_command wdc_c;
    152   1.2   bouyer 
    153  1.22  thorpej 	WDCDEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
    154   1.2   bouyer 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    155   1.2   bouyer 
    156   1.2   bouyer 	wdc_c.r_command = SET_FEATURES;
    157   1.2   bouyer 	wdc_c.r_st_bmask = 0;
    158   1.2   bouyer 	wdc_c.r_st_pmask = 0;
    159   1.2   bouyer 	wdc_c.r_precomp = WDSF_SET_MODE;
    160   1.2   bouyer 	wdc_c.r_count = mode;
    161  1.19  mycroft 	wdc_c.flags = flags;
    162   1.2   bouyer 	wdc_c.timeout = 1000; /* 1s */
    163   1.2   bouyer 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
    164   1.2   bouyer 		return CMD_AGAIN;
    165   1.2   bouyer 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    166   1.2   bouyer 		return CMD_ERR;
    167   1.2   bouyer 	}
    168   1.2   bouyer 	return CMD_OK;
    169  1.11   bouyer }
    170  1.11   bouyer 
    171  1.11   bouyer void
    172  1.21  thorpej ata_dmaerr(struct ata_drive_datas *drvp, int flags)
    173  1.11   bouyer {
    174  1.11   bouyer 	/*
    175  1.11   bouyer 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
    176  1.11   bouyer 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
    177  1.11   bouyer 	 * first error within the first NXFER ops will immediatly trigger
    178  1.11   bouyer 	 * a downgrade.
    179  1.11   bouyer 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
    180  1.11   bouyer 	 */
    181  1.11   bouyer 	drvp->n_dmaerrs++;
    182  1.11   bouyer 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
    183  1.20   bouyer 		wdc_downgrade_mode(drvp, flags);
    184  1.11   bouyer 		drvp->n_dmaerrs = NERRS_MAX-1;
    185  1.11   bouyer 		drvp->n_xfers = 0;
    186  1.11   bouyer 		return;
    187  1.11   bouyer 	}
    188  1.11   bouyer 	if (drvp->n_xfers > NXFER) {
    189  1.11   bouyer 		drvp->n_dmaerrs = 1; /* just got an error */
    190  1.11   bouyer 		drvp->n_xfers = 1; /* restart counting from this error */
    191   1.4   bouyer 	}
    192   1.2   bouyer }
    193