xref: /netbsd-src/sys/dev/ata/ata.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*      $NetBSD: ata.c,v 1.16 2001/12/03 00:20:22 bouyer Exp $      */
2 /*
3  * Copyright (c) 1998, 2001 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 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.16 2001/12/03 00:20:22 bouyer Exp $");
33 
34 #ifndef WDCDEBUG
35 #define WDCDEBUG
36 #endif /* WDCDEBUG */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/malloc.h>
44 #include <sys/device.h>
45 #include <sys/syslog.h>
46 
47 #include <machine/intr.h>
48 #include <machine/bus.h>
49 
50 #include <dev/ata/atareg.h>
51 #include <dev/ata/atavar.h>
52 #include <dev/ic/wdcreg.h>
53 #include <dev/ic/wdcvar.h>
54 
55 #define DEBUG_FUNCS  0x08
56 #define DEBUG_PROBE  0x10
57 #ifdef WDCDEBUG
58 extern int wdcdebug_mask; /* init'ed in wdc.c */
59 #define WDCDEBUG_PRINT(args, level) \
60         if (wdcdebug_mask & (level)) \
61 		printf args
62 #else
63 #define WDCDEBUG_PRINT(args, level)
64 #endif
65 
66 /* Get the disk's parameters */
67 int
68 ata_get_params(drvp, flags, prms)
69 	struct ata_drive_datas *drvp;
70 	u_int8_t flags;
71 	struct ataparams *prms;
72 {
73 	char tb[DEV_BSIZE];
74 	struct wdc_command wdc_c;
75 
76 #if BYTE_ORDER == LITTLE_ENDIAN
77 	int i;
78 	u_int16_t *p;
79 #endif
80 
81 	WDCDEBUG_PRINT(("wdc_ata_get_parms\n"), DEBUG_FUNCS);
82 
83 	memset(tb, 0, DEV_BSIZE);
84 	memset(prms, 0, sizeof(struct ataparams));
85 	memset(&wdc_c, 0, sizeof(struct wdc_command));
86 
87 	if (drvp->drive_flags & DRIVE_ATA) {
88 		wdc_c.r_command = WDCC_IDENTIFY;
89 		wdc_c.r_st_bmask = WDCS_DRDY;
90 		wdc_c.r_st_pmask = WDCS_DRQ;
91 		wdc_c.timeout = 1000; /* 1s */
92 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
93 		wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
94 		wdc_c.r_st_bmask = 0;
95 		wdc_c.r_st_pmask = WDCS_DRQ;
96 		wdc_c.timeout = 10000; /* 10s */
97 	} else {
98 		WDCDEBUG_PRINT(("wdc_ata_get_parms: no disks\n"),
99 		    DEBUG_FUNCS|DEBUG_PROBE);
100 		return CMD_ERR;
101 	}
102 	wdc_c.flags = AT_READ | flags;
103 	wdc_c.data = tb;
104 	wdc_c.bcount = DEV_BSIZE;
105 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) {
106 		WDCDEBUG_PRINT(("wdc_ata_get_parms: wdc_exec_command failed\n"),
107 		    DEBUG_FUNCS|DEBUG_PROBE);
108 		return CMD_AGAIN;
109 	}
110 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
111 		WDCDEBUG_PRINT(("wdc_ata_get_parms: wdc_c.flags=0x%x\n",
112 		    wdc_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
113 		return CMD_ERR;
114 	} else {
115 		/* Read in parameter block. */
116 		memcpy(prms, tb, sizeof(struct ataparams));
117 #if BYTE_ORDER == LITTLE_ENDIAN
118 		/*
119 		 * Shuffle string byte order.
120 		 * ATAPI Mitsumi and NEC drives don't need this.
121 		 */
122 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
123 		    WDC_CFG_ATAPI &&
124 		    ((prms->atap_model[0] == 'N' &&
125 			prms->atap_model[1] == 'E') ||
126 		     (prms->atap_model[0] == 'F' &&
127 			 prms->atap_model[1] == 'X')))
128 			return 0;
129 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
130 			p = (u_short *)(prms->atap_model + i);
131 			*p = ntohs(*p);
132 		}
133 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
134 			p = (u_short *)(prms->atap_serial + i);
135 			*p = ntohs(*p);
136 		}
137 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
138 			p = (u_short *)(prms->atap_revision + i);
139 			*p = ntohs(*p);
140 		}
141 #endif
142 		return CMD_OK;
143 	}
144 }
145 
146 int
147 ata_set_mode(drvp, mode, flags)
148 	struct ata_drive_datas *drvp;
149 	u_int8_t mode;
150 	u_int8_t flags;
151 {
152 	struct wdc_command wdc_c;
153 
154 	WDCDEBUG_PRINT(("wdc_ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
155 	memset(&wdc_c, 0, sizeof(struct wdc_command));
156 
157 	wdc_c.r_command = SET_FEATURES;
158 	wdc_c.r_st_bmask = 0;
159 	wdc_c.r_st_pmask = 0;
160 	wdc_c.r_precomp = WDSF_SET_MODE;
161 	wdc_c.r_count = mode;
162 	wdc_c.flags = AT_READ | flags;
163 	wdc_c.timeout = 1000; /* 1s */
164 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
165 		return CMD_AGAIN;
166 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
167 		return CMD_ERR;
168 	}
169 	return CMD_OK;
170 }
171 
172 void
173 ata_dmaerr(drvp)
174 	struct ata_drive_datas *drvp;
175 {
176 	/*
177 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
178 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
179 	 * first error within the first NXFER ops will immediatly trigger
180 	 * a downgrade.
181 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
182 	 */
183 	drvp->n_dmaerrs++;
184 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
185 		wdc_downgrade_mode(drvp);
186 		drvp->n_dmaerrs = NERRS_MAX-1;
187 		drvp->n_xfers = 0;
188 		return;
189 	}
190 	if (drvp->n_xfers > NXFER) {
191 		drvp->n_dmaerrs = 1; /* just got an error */
192 		drvp->n_xfers = 1; /* restart counting from this error */
193 	}
194 }
195