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