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