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