wd.c revision 1.6 1 /* $NetBSD: wd.c,v 1.6 2007/08/03 12:57:38 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Manuel Bouyer.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/types.h>
40 #include <sys/stdint.h>
41
42 #include <lib/libsa/stand.h>
43
44 #include <machine/param.h>
45 #include <machine/stdarg.h>
46 #include <dev/raidframe/raidframevar.h> /* For RF_PROTECTED_SECTORS */
47
48 #include "boot.h"
49 #include "wdvar.h"
50
51 static int wd_get_params(struct wd_softc *wd);
52 static int wdgetdisklabel(struct wd_softc *wd);
53 static void wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp);
54
55 /*
56 * Get drive parameters through 'device identify' command.
57 */
58 int
59 wd_get_params(wd)
60 struct wd_softc *wd;
61 {
62 int error;
63 unsigned char buf[DEV_BSIZE];
64
65 if ( (error = wdc_exec_identify(wd, buf)) != 0)
66 return (error);
67
68 wd->sc_params = *(struct ataparams *)buf;
69
70 /* 48-bit LBA addressing */
71 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0) {
72 DPRINTF(("Drive supports LBA48.\n"));
73 #if defined(_ENABLE_LBA48)
74 wd->sc_flags |= WDF_LBA48;
75 #endif
76 }
77
78 /* Prior to ATA-4, LBA was optional. */
79 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0) {
80 DPRINTF(("Drive supports LBA.\n"));
81 wd->sc_flags |= WDF_LBA;
82 }
83
84 return (0);
85 }
86
87 /*
88 * Initialize disk label to the default value.
89 */
90 void
91 wdgetdefaultlabel(wd, lp)
92 struct wd_softc *wd;
93 struct disklabel *lp;
94 {
95 memset(lp, 0, sizeof(struct disklabel));
96
97 lp->d_secsize = DEV_BSIZE;
98 lp->d_ntracks = wd->sc_params.atap_heads;
99 lp->d_nsectors = wd->sc_params.atap_sectors;
100 lp->d_ncylinders = wd->sc_params.atap_cylinders;
101 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
102
103 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
104 lp->d_type = DTYPE_ST506;
105 else
106 lp->d_type = DTYPE_ESDI;
107
108 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
109 strncpy(lp->d_packname, "fictitious", 16);
110 if (wd->sc_capacity > UINT32_MAX)
111 lp->d_secperunit = UINT32_MAX;
112 else
113 lp->d_secperunit = wd->sc_capacity;
114 lp->d_rpm = 3600;
115 lp->d_interleave = 1;
116 lp->d_flags = 0;
117
118 lp->d_partitions[RAW_PART].p_offset = 0;
119 lp->d_partitions[RAW_PART].p_size =
120 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
121 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
122 lp->d_npartitions = MAXPARTITIONS; /* RAW_PART + 1 ??? */
123
124 lp->d_magic = DISKMAGIC;
125 lp->d_magic2 = DISKMAGIC;
126 lp->d_checksum = dkcksum(lp);
127 }
128
129 /*
130 * Read disk label from the device.
131 */
132 int
133 wdgetdisklabel(wd)
134 struct wd_softc *wd;
135 {
136 char *msg;
137 int sector;
138 size_t rsize;
139 struct disklabel *lp;
140 unsigned char buf[DEV_BSIZE];
141
142 wdgetdefaultlabel(wd, &wd->sc_label);
143
144 /*
145 * Find NetBSD Partition in DOS partition table.
146 */
147 sector = 0;
148 if (wdstrategy(wd, F_READ, MBR_BBSECTOR, DEV_BSIZE, buf, &rsize))
149 return EOFFSET;
150
151 if (*(u_int16_t *)&buf[MBR_MAGIC_OFFSET] == MBR_MAGIC) {
152 int i;
153 struct mbr_partition *mp;
154
155 /*
156 * Lookup NetBSD slice. If there is none, go ahead
157 * and try to read the disklabel off sector #0.
158 */
159 mp = (struct mbr_partition *)&buf[MBR_PART_OFFSET];
160 for (i = 0; i < MBR_PART_COUNT; i++) {
161 if (mp[i].mbrp_type == MBR_PTYPE_NETBSD) {
162 sector = mp[i].mbrp_start;
163 break;
164 }
165 }
166 }
167
168 if (wdstrategy(wd, F_READ, sector + LABELSECTOR, DEV_BSIZE,
169 buf, &rsize))
170 return EOFFSET;
171
172 if ( (msg = getdisklabel(buf + LABELOFFSET, &wd->sc_label)))
173 printf("wd%d: getdisklabel: %s\n", wd->sc_unit, msg);
174
175 lp = &wd->sc_label;
176
177 /* check partition */
178 if ((wd->sc_part >= lp->d_npartitions) ||
179 (lp->d_partitions[wd->sc_part].p_fstype == FS_UNUSED)) {
180 DPRINTF(("illegal partition\n"));
181 return (EPART);
182 }
183
184 DPRINTF(("label info: d_secsize %d, d_nsectors %d, d_ncylinders %d,"
185 "d_ntracks %d, d_secpercyl %d\n",
186 wd->sc_label.d_secsize,
187 wd->sc_label.d_nsectors,
188 wd->sc_label.d_ncylinders,
189 wd->sc_label.d_ntracks,
190 wd->sc_label.d_secpercyl));
191
192 return (0);
193 }
194
195 /*
196 * Open device (read drive parameters and disklabel)
197 */
198 int
199 wdopen(struct open_file *f, ...)
200 {
201 int error;
202 va_list ap;
203 u_int unit, part;
204 struct wd_softc *wd;
205
206 va_start(ap, f);
207 unit = va_arg(ap, u_int);
208 part = va_arg(ap, u_int);
209 va_end(ap);
210
211 DPRINTF(("wdopen: %d:%d\n", unit, part));
212
213 wd = alloc(sizeof(struct wd_softc));
214 if (wd == NULL)
215 return ENOMEM;
216
217 memset(wd, 0, sizeof(struct wd_softc));
218
219 if (wdc_init(wd, &unit) != 0)
220 return (ENXIO);
221
222 wd->sc_part = part;
223 wd->sc_unit = unit;
224
225 if ( (error = wd_get_params(wd)) != 0)
226 return (error);
227
228 if ( (error = wdgetdisklabel(wd)) != 0)
229 return error;
230
231 f->f_devdata = wd;
232 return (0);
233 }
234
235 /*
236 * Close device.
237 */
238 int
239 wdclose(struct open_file *f)
240 {
241 return 0;
242 }
243
244 /*
245 * Read some data.
246 */
247 int
248 wdstrategy(f, rw, dblk, size, buf, rsize)
249 void *f;
250 int rw;
251 daddr_t dblk;
252 size_t size;
253 void *buf;
254 size_t *rsize;
255 {
256 int i, nsect;
257 daddr_t blkno;
258 struct wd_softc *wd;
259 struct partition *pp;
260
261 if (size == 0)
262 return (0);
263
264 if (rw != F_READ)
265 return EOPNOTSUPP;
266
267 wd = f;
268 pp = &wd->sc_label.d_partitions[wd->sc_part];
269
270 nsect = howmany(size, wd->sc_label.d_secsize);
271 blkno = dblk + pp->p_offset;
272 if (pp->p_fstype == FS_RAID)
273 blkno += RF_PROTECTED_SECTORS;
274
275 for (i = 0; i < nsect; i++, blkno++) {
276 int error;
277
278 if ( (error = wdc_exec_read(wd, WDCC_READ, blkno, buf)) != 0)
279 return (error);
280
281 buf += wd->sc_label.d_secsize;
282 }
283
284 *rsize = size;
285 return (0);
286 }
287