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