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