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