ata.c revision 1.4 1 /* $NetBSD: ata.c,v 1.4 1999/01/18 20:06:24 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 #define WDCDEBUG
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/file.h>
37 #include <sys/stat.h>
38 #include <sys/malloc.h>
39 #include <sys/device.h>
40 #include <sys/syslog.h>
41
42 #include <dev/ic/wdcreg.h>
43 #include <dev/ata/atareg.h>
44 #include <dev/ata/atavar.h>
45
46 #define DEBUG_FUNCS 0x08
47 #define DEBUG_PROBE 0x10
48 #ifdef WDCDEBUG
49 extern int wdcdebug_mask; /* init'ed in wdc.c */
50 #define WDCDEBUG_PRINT(args, level) \
51 if (wdcdebug_mask & (level)) \
52 printf args
53 #else
54 #define WDCDEBUG_PRINT(args, level)
55 #endif
56
57 /* Get the disk's parameters */
58 int
59 ata_get_params(drvp, flags, prms)
60 struct ata_drive_datas *drvp;
61 u_int8_t flags;
62 struct ataparams *prms;
63 {
64 char tb[DEV_BSIZE];
65 struct wdc_command wdc_c;
66
67 #if BYTE_ORDER == LITTLE_ENDIAN
68 int i;
69 u_int16_t *p;
70 #endif
71
72 WDCDEBUG_PRINT(("wdc_ata_get_parms\n"), DEBUG_FUNCS);
73
74 memset(tb, 0, DEV_BSIZE);
75 memset(prms, 0, sizeof(struct ataparams));
76 memset(&wdc_c, 0, sizeof(struct wdc_command));
77
78 if (drvp->drive_flags & DRIVE_ATA) {
79 wdc_c.r_command = WDCC_IDENTIFY;
80 wdc_c.r_st_bmask = WDCS_DRDY | WDCS_DSC;
81 wdc_c.r_st_pmask = WDCS_DRDY | WDCS_DSC | WDCS_DRQ;
82 } else {
83 wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
84 wdc_c.r_st_bmask = 0;
85 wdc_c.r_st_pmask = WDCS_DRQ;
86 }
87 wdc_c.flags = AT_READ | flags;
88 wdc_c.timeout = 1000; /* 1s */
89 wdc_c.data = tb;
90 wdc_c.bcount = DEV_BSIZE;
91 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
92 return CMD_AGAIN;
93 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
94 return CMD_ERR;
95 } else {
96 /* Read in parameter block. */
97 memcpy(prms, tb, sizeof(struct ataparams));
98 #if BYTE_ORDER == LITTLE_ENDIAN
99 /*
100 * Shuffle string byte order.
101 * ATAPI Mitsumi and NEC drives don't need this.
102 */
103 if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
104 WDC_CFG_ATAPI &&
105 ((prms->atap_model[0] == 'N' &&
106 prms->atap_model[1] == 'E') ||
107 (prms->atap_model[0] == 'F' &&
108 prms->atap_model[1] == 'X')))
109 return 0;
110 for (i = 0; i < sizeof(prms->atap_model); i += 2) {
111 p = (u_short *)(prms->atap_model + i);
112 *p = ntohs(*p);
113 }
114 for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
115 p = (u_short *)(prms->atap_serial + i);
116 *p = ntohs(*p);
117 }
118 for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
119 p = (u_short *)(prms->atap_revision + i);
120 *p = ntohs(*p);
121 }
122 #endif
123 return CMD_OK;
124 }
125 }
126
127 int
128 ata_set_mode(drvp, mode, flags)
129 struct ata_drive_datas *drvp;
130 u_int8_t mode;
131 u_int8_t flags;
132 {
133 struct wdc_command wdc_c;
134
135 WDCDEBUG_PRINT(("wdc_ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
136 memset(&wdc_c, 0, sizeof(struct wdc_command));
137
138 wdc_c.r_command = SET_FEATURES;
139 wdc_c.r_st_bmask = 0;
140 wdc_c.r_st_pmask = 0;
141 wdc_c.r_precomp = WDSF_SET_MODE;
142 wdc_c.r_count = mode;
143 wdc_c.flags = AT_READ | flags;
144 wdc_c.timeout = 1000; /* 1s */
145 if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
146 return CMD_AGAIN;
147 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
148 return CMD_ERR;
149 }
150 return CMD_OK;
151 }
152
153 void
154 ata_perror(drvp, errno, buf)
155 struct ata_drive_datas *drvp;
156 int errno;
157 char *buf;
158 {
159 static char *errstr0_3[] = {"address mark not found",
160 "track 0 not found", "aborted command", "media change requested",
161 "id not found", "media changed", "uncorrectable data error",
162 "bad block detected"};
163 static char *errstr4_5[] = {"",
164 "no media/write protected", "aborted command",
165 "media change requested", "id not found", "media changed",
166 "uncorrectable data error", "interface CRC error"};
167 char **errstr;
168 int i;
169 char *sep = "";
170
171 if (drvp->ata_vers >= 4)
172 errstr = errstr4_5;
173 else
174 errstr = errstr0_3;
175
176 if (errno == 0) {
177 sprintf(buf, "error not notified");
178 }
179
180 for (i = 0; i < 8; i++) {
181 if (errno & (1 << i)) {
182 buf += sprintf(buf, "%s %s", sep, errstr[i]);
183 sep = ",";
184 }
185 }
186 }
187