ata_raid_intel.c revision 1.6 1 1.6 bsh /* $NetBSD: ata_raid_intel.c,v 1.6 2010/07/06 18:09:04 bsh Exp $ */
2 1.1 tron
3 1.1 tron /*-
4 1.1 tron * Copyright (c) 2000-2008 Sren Schmidt <sos (at) FreeBSD.org>
5 1.1 tron * All rights reserved.
6 1.1 tron *
7 1.1 tron * Redistribution and use in source and binary forms, with or without
8 1.1 tron * modification, are permitted provided that the following conditions
9 1.1 tron * are met:
10 1.1 tron * 1. Redistributions of source code must retain the above copyright
11 1.1 tron * notice, this list of conditions and the following disclaimer,
12 1.1 tron * without modification, immediately at the beginning of the file.
13 1.1 tron * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 tron * notice, this list of conditions and the following disclaimer in the
15 1.1 tron * documentation and/or other materials provided with the distribution.
16 1.1 tron *
17 1.1 tron * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 tron * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 tron * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 tron * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 tron * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 tron * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 tron * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 tron * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 tron * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.1 tron * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 tron */
28 1.1 tron
29 1.1 tron /*
30 1.1 tron * Support for parsing Intel MatrixRAID controller configuration blocks.
31 1.1 tron *
32 1.1 tron * Adapted to NetBSD by Juan Romero Pardines (xtraeme (at) gmail.org).
33 1.1 tron */
34 1.1 tron
35 1.1 tron #include <sys/cdefs.h>
36 1.6 bsh __KERNEL_RCSID(0, "$NetBSD: ata_raid_intel.c,v 1.6 2010/07/06 18:09:04 bsh Exp $");
37 1.1 tron
38 1.1 tron #include <sys/param.h>
39 1.1 tron #include <sys/buf.h>
40 1.1 tron #include <sys/bufq.h>
41 1.1 tron #include <sys/conf.h>
42 1.1 tron #include <sys/device.h>
43 1.1 tron #include <sys/disk.h>
44 1.1 tron #include <sys/disklabel.h>
45 1.1 tron #include <sys/fcntl.h>
46 1.1 tron #include <sys/malloc.h>
47 1.1 tron #include <sys/vnode.h>
48 1.1 tron #include <sys/kauth.h>
49 1.1 tron
50 1.1 tron #include <miscfs/specfs/specdev.h>
51 1.1 tron
52 1.1 tron #include <dev/ata/atareg.h>
53 1.1 tron #include <dev/ata/atavar.h>
54 1.1 tron #include <dev/ata/wdvar.h>
55 1.1 tron
56 1.1 tron #include <dev/ata/ata_raidreg.h>
57 1.1 tron #include <dev/ata/ata_raidvar.h>
58 1.1 tron
59 1.1 tron #ifdef ATA_RAID_DEBUG
60 1.1 tron #define DPRINTF(x) printf x
61 1.1 tron #else
62 1.1 tron #define DPRINTF(x) /* nothing */
63 1.1 tron #endif
64 1.1 tron
65 1.5 bsh static int find_volume_id(struct intel_raid_conf *);
66 1.5 bsh
67 1.5 bsh
68 1.1 tron #ifdef ATA_RAID_DEBUG
69 1.1 tron static const char *
70 1.1 tron ata_raid_intel_type(int type)
71 1.1 tron {
72 1.1 tron static char buffer[16];
73 1.1 tron
74 1.1 tron switch (type) {
75 1.1 tron case INTEL_T_RAID0:
76 1.1 tron return "RAID0";
77 1.1 tron case INTEL_T_RAID1:
78 1.1 tron return "RAID1";
79 1.1 tron case INTEL_T_RAID5:
80 1.1 tron return "RAID5";
81 1.1 tron default:
82 1.1 tron sprintf(buffer, "UNKNOWN 0x%02x", type);
83 1.1 tron return buffer;
84 1.1 tron }
85 1.1 tron }
86 1.1 tron
87 1.1 tron static void
88 1.1 tron ata_raid_intel_print_info(struct intel_raid_conf *info)
89 1.1 tron {
90 1.1 tron struct intel_raid_mapping *map;
91 1.1 tron int i, j;
92 1.1 tron
93 1.1 tron printf("********* ATA Intel MatrixRAID Metadata *********\n");
94 1.1 tron printf("intel_id <%.24s>\n", info->intel_id);
95 1.1 tron printf("version <%.6s>\n", info->version);
96 1.1 tron printf("checksum 0x%08x\n", info->checksum);
97 1.1 tron printf("config_size 0x%08x\n", info->config_size);
98 1.1 tron printf("config_id 0x%08x\n", info->config_id);
99 1.1 tron printf("generation 0x%08x\n", info->generation);
100 1.1 tron printf("total_disks %u\n", info->total_disks);
101 1.1 tron printf("total_volumes %u\n", info->total_volumes);
102 1.1 tron printf("DISK# serial disk sectors disk_id flags\n");
103 1.1 tron for (i = 0; i < info->total_disks; i++) {
104 1.1 tron printf(" %d <%.16s> %u 0x%08x 0x%08x\n",
105 1.1 tron i, info->disk[i].serial, info->disk[i].sectors,
106 1.1 tron info->disk[i].id, info->disk[i].flags);
107 1.1 tron }
108 1.1 tron
109 1.1 tron map = (struct intel_raid_mapping *)&info->disk[info->total_disks];
110 1.1 tron for (j = 0; j < info->total_volumes; j++) {
111 1.1 tron printf("name %.16s\n", map->name);
112 1.1 tron printf("total_sectors %ju\n", map->total_sectors);
113 1.1 tron printf("state %u\n", map->state);
114 1.1 tron printf("reserved %u\n", map->reserved);
115 1.1 tron printf("offset %u\n", map->offset);
116 1.1 tron printf("disk_sectors %u\n", map->disk_sectors);
117 1.1 tron printf("stripe_count %u\n", map->stripe_count);
118 1.1 tron printf("stripe_sectors %u\n", map->stripe_sectors);
119 1.1 tron printf("status %u\n", map->status);
120 1.1 tron printf("type %s\n", ata_raid_intel_type(map->type));
121 1.1 tron printf("total_disks %u\n", map->total_disks);
122 1.1 tron printf("magic[0] 0x%02x\n", map->magic[0]);
123 1.1 tron printf("magic[1] 0x%02x\n", map->magic[1]);
124 1.1 tron printf("magic[2] 0x%02x\n", map->magic[2]);
125 1.1 tron for (i = 0; i < map->total_disks; i++)
126 1.1 tron printf(" disk %d at disk_idx 0x%08x\n",
127 1.1 tron i, map->disk_idx[i]);
128 1.1 tron
129 1.1 tron map = (struct intel_raid_mapping *)
130 1.1 tron &map->disk_idx[map->total_disks];
131 1.1 tron }
132 1.1 tron printf("=================================================\n");
133 1.1 tron }
134 1.1 tron #endif
135 1.1 tron
136 1.1 tron int
137 1.1 tron ata_raid_read_config_intel(struct wd_softc *sc)
138 1.1 tron {
139 1.1 tron struct intel_raid_conf *info;
140 1.1 tron struct intel_raid_mapping *map;
141 1.1 tron struct ataraid_array_info *aai;
142 1.1 tron struct ataraid_disk_info *adi;
143 1.1 tron struct vnode *vp;
144 1.1 tron uint32_t checksum, *ptr;
145 1.3 tron int bmajor, count, curvol = 0, error = 0;
146 1.1 tron char *tmp;
147 1.1 tron dev_t dev;
148 1.5 bsh int volumeid, diskidx;
149 1.1 tron
150 1.1 tron info = malloc(1536, M_DEVBUF, M_WAITOK|M_ZERO);
151 1.1 tron
152 1.1 tron bmajor = devsw_name2blk(device_xname(sc->sc_dev), NULL, 0);
153 1.1 tron
154 1.1 tron /* Get a vnode for the raw partition of this disk. */
155 1.1 tron dev = MAKEDISKDEV(bmajor, device_unit(sc->sc_dev), RAW_PART);
156 1.1 tron error = bdevvp(dev, &vp);
157 1.1 tron if (error)
158 1.1 tron goto out;
159 1.1 tron
160 1.1 tron error = VOP_OPEN(vp, FREAD, NOCRED);
161 1.1 tron if (error) {
162 1.1 tron vput(vp);
163 1.1 tron goto out;
164 1.1 tron }
165 1.1 tron
166 1.1 tron error = ata_raid_config_block_rw(vp, INTEL_LBA(sc), info,
167 1.1 tron 1024, B_READ);
168 1.1 tron VOP_CLOSE(vp, FREAD, NOCRED);
169 1.1 tron vput(vp);
170 1.1 tron if (error) {
171 1.1 tron DPRINTF(("%s: error %d reading Intel MatrixRAID config block\n",
172 1.1 tron device_xname(sc->sc_dev), error));
173 1.1 tron goto out;
174 1.1 tron }
175 1.1 tron
176 1.1 tron tmp = (char *)info;
177 1.1 tron (void)memcpy(tmp + 1024, tmp, 512);
178 1.1 tron (void)memcpy(tmp, tmp + 512, 1024);
179 1.1 tron (void)memset(tmp + 1024, 0, 512);
180 1.1 tron
181 1.1 tron /* Check if this is a Intel RAID struct */
182 1.1 tron if (strncmp(info->intel_id, INTEL_MAGIC, strlen(INTEL_MAGIC))) {
183 1.1 tron DPRINTF(("%s: Intel MatrixRAID signature check failed\n",
184 1.1 tron device_xname(sc->sc_dev)));
185 1.1 tron error = ESRCH;
186 1.1 tron goto out;
187 1.1 tron }
188 1.1 tron
189 1.1 tron /* calculate checksum and compare for valid */
190 1.1 tron for (checksum = 0, ptr = (uint32_t *)info, count = 0;
191 1.1 tron count < (info->config_size / sizeof(uint32_t)); count++)
192 1.1 tron checksum += *ptr++;
193 1.1 tron
194 1.1 tron checksum -= info->checksum;
195 1.1 tron if (checksum != info->checksum) {
196 1.1 tron DPRINTF(("%s: Intel MatrixRAID checksum failed 0x%x != 0x%x\n",
197 1.1 tron device_xname(sc->sc_dev), checksum, info->checksum));
198 1.1 tron error = ESRCH;
199 1.1 tron goto out;
200 1.1 tron }
201 1.1 tron
202 1.1 tron #ifdef ATA_RAID_DEBUG
203 1.1 tron ata_raid_intel_print_info(info);
204 1.1 tron #endif
205 1.1 tron
206 1.1 tron /* This one points to the first volume */
207 1.1 tron map = (struct intel_raid_mapping *)&info->disk[info->total_disks];
208 1.1 tron
209 1.5 bsh volumeid = find_volume_id(info);
210 1.5 bsh if (volumeid < 0) {
211 1.5 bsh aprint_error_dev(sc->sc_dev,
212 1.5 bsh "too many RAID arrays\n");
213 1.5 bsh error = ENOMEM;
214 1.5 bsh goto out;
215 1.5 bsh }
216 1.5 bsh
217 1.3 tron findvol:
218 1.1 tron /*
219 1.1 tron * Lookup or allocate a new array info structure for this array.
220 1.1 tron */
221 1.5 bsh aai = ata_raid_get_array_info(ATA_RAID_TYPE_INTEL, volumeid + curvol);
222 1.1 tron
223 1.1 tron /* Fill in array info */
224 1.1 tron aai->aai_generation = info->generation;
225 1.1 tron aai->aai_status = AAI_S_READY;
226 1.1 tron
227 1.1 tron switch (map->type) {
228 1.1 tron case INTEL_T_RAID0:
229 1.1 tron aai->aai_level = AAI_L_RAID0;
230 1.1 tron aai->aai_width = map->total_disks;
231 1.1 tron break;
232 1.1 tron case INTEL_T_RAID1:
233 1.1 tron aai->aai_level = AAI_L_RAID1;
234 1.1 tron aai->aai_width = 1;
235 1.1 tron break;
236 1.1 tron default:
237 1.1 tron DPRINTF(("%s: unknown Intel MatrixRAID type 0x%02x\n",
238 1.4 cegger device_xname(sc->sc_dev), map->type));
239 1.1 tron error = EINVAL;
240 1.1 tron goto out;
241 1.1 tron }
242 1.1 tron
243 1.1 tron switch (map->state) {
244 1.1 tron case INTEL_S_DEGRADED:
245 1.1 tron aai->aai_status |= AAI_S_DEGRADED;
246 1.1 tron break;
247 1.1 tron case INTEL_S_DISABLED:
248 1.1 tron case INTEL_S_FAILURE:
249 1.1 tron aai->aai_status &= ~AAI_S_READY;
250 1.1 tron break;
251 1.1 tron }
252 1.1 tron
253 1.1 tron aai->aai_type = ATA_RAID_TYPE_INTEL;
254 1.1 tron aai->aai_capacity = map->total_sectors;
255 1.6 bsh aai->aai_interleave = map->stripe_sectors;
256 1.1 tron aai->aai_ndisks = map->total_disks;
257 1.1 tron aai->aai_heads = 255;
258 1.1 tron aai->aai_sectors = 63;
259 1.1 tron aai->aai_cylinders =
260 1.1 tron aai->aai_capacity / (aai->aai_heads * aai->aai_sectors);
261 1.1 tron aai->aai_offset = map->offset;
262 1.1 tron aai->aai_reserved = 3;
263 1.2 tron if (map->name)
264 1.2 tron strlcpy(aai->aai_name, map->name, sizeof(aai->aai_name));
265 1.1 tron
266 1.1 tron /* Fill in disk info */
267 1.5 bsh diskidx = aai->aai_curdisk++;
268 1.5 bsh adi = &aai->aai_disks[diskidx];
269 1.1 tron adi->adi_status = 0;
270 1.1 tron
271 1.5 bsh if (info->disk[diskidx].flags & INTEL_F_ONLINE)
272 1.1 tron adi->adi_status |= ADI_S_ONLINE;
273 1.5 bsh if (info->disk[diskidx].flags & INTEL_F_ASSIGNED)
274 1.1 tron adi->adi_status |= ADI_S_ASSIGNED;
275 1.5 bsh if (info->disk[diskidx].flags & INTEL_F_SPARE) {
276 1.1 tron adi->adi_status &= ~ADI_S_ONLINE;
277 1.1 tron adi->adi_status |= ADI_S_SPARE;
278 1.1 tron }
279 1.5 bsh if (info->disk[diskidx].flags & INTEL_F_DOWN)
280 1.1 tron adi->adi_status &= ~ADI_S_ONLINE;
281 1.1 tron
282 1.1 tron if (adi->adi_status) {
283 1.1 tron adi->adi_dev = sc->sc_dev;
284 1.5 bsh adi->adi_sectors = info->disk[diskidx].sectors;
285 1.1 tron adi->adi_compsize = adi->adi_sectors - aai->aai_reserved;
286 1.5 bsh
287 1.3 tron /*
288 1.3 tron * Check if that is the only volume, otherwise repeat
289 1.3 tron * the process to find more.
290 1.3 tron */
291 1.3 tron if ((curvol + 1) < info->total_volumes) {
292 1.3 tron curvol++;
293 1.3 tron map = (struct intel_raid_mapping *)
294 1.3 tron &map->disk_idx[map->total_disks];
295 1.3 tron goto findvol;
296 1.3 tron }
297 1.1 tron }
298 1.1 tron
299 1.1 tron out:
300 1.1 tron free(info, M_DEVBUF);
301 1.1 tron return error;
302 1.1 tron }
303 1.5 bsh
304 1.5 bsh
305 1.5 bsh /*
306 1.5 bsh * Assign `volume id' to RAID volumes.
307 1.5 bsh */
308 1.5 bsh static struct {
309 1.5 bsh /* We assume disks are on the same array if these three values
310 1.5 bsh are same. */
311 1.5 bsh uint32_t config_id;
312 1.5 bsh uint32_t generation;
313 1.5 bsh uint32_t checksum;
314 1.5 bsh
315 1.5 bsh int id;
316 1.5 bsh } array_note[10]; /* XXX: this array is not used after ld_ataraid is
317 1.5 bsh * configured. */
318 1.5 bsh
319 1.5 bsh static int n_array = 0;
320 1.5 bsh static int volume_id = 0;
321 1.5 bsh
322 1.5 bsh static int
323 1.5 bsh find_volume_id(struct intel_raid_conf *info)
324 1.5 bsh {
325 1.5 bsh int i, ret;
326 1.5 bsh
327 1.5 bsh for (i=0; i < n_array; ++i) {
328 1.5 bsh if (info->checksum == array_note[i].checksum &&
329 1.5 bsh info->config_id == array_note[i].config_id &&
330 1.5 bsh info->generation == array_note[i].generation) {
331 1.5 bsh /* we have already seen this array */
332 1.5 bsh return array_note[i].id;
333 1.5 bsh }
334 1.5 bsh }
335 1.5 bsh
336 1.5 bsh if (n_array >= __arraycount(array_note)) {
337 1.5 bsh /* Too many arrays */
338 1.5 bsh return -1;
339 1.5 bsh }
340 1.5 bsh
341 1.5 bsh array_note[n_array].checksum = info->checksum;
342 1.5 bsh array_note[n_array].config_id = info->config_id;
343 1.5 bsh array_note[n_array].generation = info->generation;
344 1.5 bsh array_note[n_array].id = ret = volume_id;
345 1.5 bsh
346 1.5 bsh /* Allocate volume ids for all volumes in this array */
347 1.5 bsh volume_id += info->total_volumes;
348 1.5 bsh ++n_array;
349 1.5 bsh return ret;
350 1.5 bsh }
351