Home | History | Annotate | Line # | Download | only in ata
ata.c revision 1.6
      1 /*      $NetBSD: ata.c,v 1.6 1999/02/21 00:52:04 hubertf 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 	} else {
     85 		wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
     86 		wdc_c.r_st_bmask = 0;
     87 		wdc_c.r_st_pmask = WDCS_DRQ;
     88 	}
     89 	wdc_c.flags = AT_READ | flags;
     90 	wdc_c.timeout = 1000; /* 1s */
     91 	wdc_c.data = tb;
     92 	wdc_c.bcount = DEV_BSIZE;
     93 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
     94 		return CMD_AGAIN;
     95 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
     96 		return CMD_ERR;
     97 	} else {
     98 		/* Read in parameter block. */
     99 		memcpy(prms, tb, sizeof(struct ataparams));
    100 #if BYTE_ORDER == LITTLE_ENDIAN
    101 		/*
    102 		 * Shuffle string byte order.
    103 		 * ATAPI Mitsumi and NEC drives don't need this.
    104 		 */
    105 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
    106 		    WDC_CFG_ATAPI &&
    107 		    ((prms->atap_model[0] == 'N' &&
    108 			prms->atap_model[1] == 'E') ||
    109 		     (prms->atap_model[0] == 'F' &&
    110 			 prms->atap_model[1] == 'X')))
    111 			return 0;
    112 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
    113 			p = (u_short *)(prms->atap_model + i);
    114 			*p = ntohs(*p);
    115 		}
    116 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
    117 			p = (u_short *)(prms->atap_serial + i);
    118 			*p = ntohs(*p);
    119 		}
    120 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
    121 			p = (u_short *)(prms->atap_revision + i);
    122 			*p = ntohs(*p);
    123 		}
    124 #endif
    125 		return CMD_OK;
    126 	}
    127 }
    128 
    129 int
    130 ata_set_mode(drvp, mode, flags)
    131 	struct ata_drive_datas *drvp;
    132 	u_int8_t mode;
    133 	u_int8_t flags;
    134 {
    135 	struct wdc_command wdc_c;
    136 
    137 	WDCDEBUG_PRINT(("wdc_ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
    138 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    139 
    140 	wdc_c.r_command = SET_FEATURES;
    141 	wdc_c.r_st_bmask = 0;
    142 	wdc_c.r_st_pmask = 0;
    143 	wdc_c.r_precomp = WDSF_SET_MODE;
    144 	wdc_c.r_count = mode;
    145 	wdc_c.flags = AT_READ | flags;
    146 	wdc_c.timeout = 1000; /* 1s */
    147 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
    148 		return CMD_AGAIN;
    149 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    150 		return CMD_ERR;
    151 	}
    152 	return CMD_OK;
    153 }
    154 
    155 void
    156 ata_perror(drvp, errno, buf)
    157 	struct ata_drive_datas *drvp;
    158 	int errno;
    159 	char *buf;
    160 {
    161 	static char *errstr0_3[] = {"address mark not found",
    162 	    "track 0 not found", "aborted command", "media change requested",
    163 	    "id not found", "media changed", "uncorrectable data error",
    164 	    "bad block detected"};
    165 	static char *errstr4_5[] = {"",
    166 	    "no media/write protected", "aborted command",
    167 	    "media change requested", "id not found", "media changed",
    168 	    "uncorrectable data error", "interface CRC error"};
    169 	char **errstr;
    170 	int i;
    171 	char *sep = "";
    172 
    173 	if (drvp->ata_vers >= 4)
    174 		errstr = errstr4_5;
    175 	else
    176 		errstr = errstr0_3;
    177 
    178 	if (errno == 0) {
    179 		sprintf(buf, "error not notified");
    180 	}
    181 
    182 	for (i = 0; i < 8; i++) {
    183 		if (errno & (1 << i)) {
    184 			buf += sprintf(buf, "%s %s", sep, errstr[i]);
    185 			sep = ",";
    186 		}
    187 	}
    188 }
    189