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