ata_raid_intel.c revision 1.8 1 1.8 mlelstv /* $NetBSD: ata_raid_intel.c,v 1.8 2017/11/01 19:34:46 mlelstv 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.8 mlelstv __KERNEL_RCSID(0, "$NetBSD: ata_raid_intel.c,v 1.8 2017/11/01 19:34:46 mlelstv 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.7 christos snprintf(buffer, sizeof(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.8 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
140 1.1 tron struct intel_raid_conf *info;
141 1.1 tron struct intel_raid_mapping *map;
142 1.1 tron struct ataraid_array_info *aai;
143 1.1 tron struct ataraid_disk_info *adi;
144 1.1 tron struct vnode *vp;
145 1.1 tron uint32_t checksum, *ptr;
146 1.3 tron int bmajor, count, curvol = 0, error = 0;
147 1.1 tron char *tmp;
148 1.1 tron dev_t dev;
149 1.5 bsh int volumeid, diskidx;
150 1.1 tron
151 1.1 tron info = malloc(1536, M_DEVBUF, M_WAITOK|M_ZERO);
152 1.1 tron
153 1.8 mlelstv bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0);
154 1.1 tron
155 1.1 tron /* Get a vnode for the raw partition of this disk. */
156 1.8 mlelstv dev = MAKEDISKDEV(bmajor, device_unit(dksc->sc_dev), RAW_PART);
157 1.1 tron error = bdevvp(dev, &vp);
158 1.1 tron if (error)
159 1.1 tron goto out;
160 1.1 tron
161 1.1 tron error = VOP_OPEN(vp, FREAD, NOCRED);
162 1.1 tron if (error) {
163 1.1 tron vput(vp);
164 1.1 tron goto out;
165 1.1 tron }
166 1.1 tron
167 1.1 tron error = ata_raid_config_block_rw(vp, INTEL_LBA(sc), info,
168 1.1 tron 1024, B_READ);
169 1.1 tron VOP_CLOSE(vp, FREAD, NOCRED);
170 1.1 tron vput(vp);
171 1.1 tron if (error) {
172 1.1 tron DPRINTF(("%s: error %d reading Intel MatrixRAID config block\n",
173 1.8 mlelstv dksc->sc_xname, error));
174 1.1 tron goto out;
175 1.1 tron }
176 1.1 tron
177 1.1 tron tmp = (char *)info;
178 1.1 tron (void)memcpy(tmp + 1024, tmp, 512);
179 1.1 tron (void)memcpy(tmp, tmp + 512, 1024);
180 1.1 tron (void)memset(tmp + 1024, 0, 512);
181 1.1 tron
182 1.1 tron /* Check if this is a Intel RAID struct */
183 1.1 tron if (strncmp(info->intel_id, INTEL_MAGIC, strlen(INTEL_MAGIC))) {
184 1.1 tron DPRINTF(("%s: Intel MatrixRAID signature check failed\n",
185 1.8 mlelstv dksc->sc_xname));
186 1.1 tron error = ESRCH;
187 1.1 tron goto out;
188 1.1 tron }
189 1.1 tron
190 1.1 tron /* calculate checksum and compare for valid */
191 1.1 tron for (checksum = 0, ptr = (uint32_t *)info, count = 0;
192 1.1 tron count < (info->config_size / sizeof(uint32_t)); count++)
193 1.1 tron checksum += *ptr++;
194 1.1 tron
195 1.1 tron checksum -= info->checksum;
196 1.1 tron if (checksum != info->checksum) {
197 1.1 tron DPRINTF(("%s: Intel MatrixRAID checksum failed 0x%x != 0x%x\n",
198 1.8 mlelstv dksc->sc_xname, checksum, info->checksum));
199 1.1 tron error = ESRCH;
200 1.1 tron goto out;
201 1.1 tron }
202 1.1 tron
203 1.1 tron #ifdef ATA_RAID_DEBUG
204 1.1 tron ata_raid_intel_print_info(info);
205 1.1 tron #endif
206 1.1 tron
207 1.1 tron /* This one points to the first volume */
208 1.1 tron map = (struct intel_raid_mapping *)&info->disk[info->total_disks];
209 1.1 tron
210 1.5 bsh volumeid = find_volume_id(info);
211 1.5 bsh if (volumeid < 0) {
212 1.8 mlelstv aprint_error_dev(dksc->sc_dev,
213 1.5 bsh "too many RAID arrays\n");
214 1.5 bsh error = ENOMEM;
215 1.5 bsh goto out;
216 1.5 bsh }
217 1.5 bsh
218 1.3 tron findvol:
219 1.1 tron /*
220 1.1 tron * Lookup or allocate a new array info structure for this array.
221 1.1 tron */
222 1.5 bsh aai = ata_raid_get_array_info(ATA_RAID_TYPE_INTEL, volumeid + curvol);
223 1.1 tron
224 1.1 tron /* Fill in array info */
225 1.1 tron aai->aai_generation = info->generation;
226 1.1 tron aai->aai_status = AAI_S_READY;
227 1.1 tron
228 1.1 tron switch (map->type) {
229 1.1 tron case INTEL_T_RAID0:
230 1.1 tron aai->aai_level = AAI_L_RAID0;
231 1.1 tron aai->aai_width = map->total_disks;
232 1.1 tron break;
233 1.1 tron case INTEL_T_RAID1:
234 1.1 tron aai->aai_level = AAI_L_RAID1;
235 1.1 tron aai->aai_width = 1;
236 1.1 tron break;
237 1.1 tron default:
238 1.1 tron DPRINTF(("%s: unknown Intel MatrixRAID type 0x%02x\n",
239 1.8 mlelstv dksc->sc_xname, map->type));
240 1.1 tron error = EINVAL;
241 1.1 tron goto out;
242 1.1 tron }
243 1.1 tron
244 1.1 tron switch (map->state) {
245 1.1 tron case INTEL_S_DEGRADED:
246 1.1 tron aai->aai_status |= AAI_S_DEGRADED;
247 1.1 tron break;
248 1.1 tron case INTEL_S_DISABLED:
249 1.1 tron case INTEL_S_FAILURE:
250 1.1 tron aai->aai_status &= ~AAI_S_READY;
251 1.1 tron break;
252 1.1 tron }
253 1.1 tron
254 1.1 tron aai->aai_type = ATA_RAID_TYPE_INTEL;
255 1.1 tron aai->aai_capacity = map->total_sectors;
256 1.6 bsh aai->aai_interleave = map->stripe_sectors;
257 1.1 tron aai->aai_ndisks = map->total_disks;
258 1.1 tron aai->aai_heads = 255;
259 1.1 tron aai->aai_sectors = 63;
260 1.1 tron aai->aai_cylinders =
261 1.1 tron aai->aai_capacity / (aai->aai_heads * aai->aai_sectors);
262 1.1 tron aai->aai_offset = map->offset;
263 1.1 tron aai->aai_reserved = 3;
264 1.2 tron if (map->name)
265 1.2 tron strlcpy(aai->aai_name, map->name, sizeof(aai->aai_name));
266 1.1 tron
267 1.1 tron /* Fill in disk info */
268 1.5 bsh diskidx = aai->aai_curdisk++;
269 1.5 bsh adi = &aai->aai_disks[diskidx];
270 1.1 tron adi->adi_status = 0;
271 1.1 tron
272 1.5 bsh if (info->disk[diskidx].flags & INTEL_F_ONLINE)
273 1.1 tron adi->adi_status |= ADI_S_ONLINE;
274 1.5 bsh if (info->disk[diskidx].flags & INTEL_F_ASSIGNED)
275 1.1 tron adi->adi_status |= ADI_S_ASSIGNED;
276 1.5 bsh if (info->disk[diskidx].flags & INTEL_F_SPARE) {
277 1.1 tron adi->adi_status &= ~ADI_S_ONLINE;
278 1.1 tron adi->adi_status |= ADI_S_SPARE;
279 1.1 tron }
280 1.5 bsh if (info->disk[diskidx].flags & INTEL_F_DOWN)
281 1.1 tron adi->adi_status &= ~ADI_S_ONLINE;
282 1.1 tron
283 1.1 tron if (adi->adi_status) {
284 1.8 mlelstv adi->adi_dev = dksc->sc_dev;
285 1.5 bsh adi->adi_sectors = info->disk[diskidx].sectors;
286 1.1 tron adi->adi_compsize = adi->adi_sectors - aai->aai_reserved;
287 1.5 bsh
288 1.3 tron /*
289 1.3 tron * Check if that is the only volume, otherwise repeat
290 1.3 tron * the process to find more.
291 1.3 tron */
292 1.3 tron if ((curvol + 1) < info->total_volumes) {
293 1.3 tron curvol++;
294 1.3 tron map = (struct intel_raid_mapping *)
295 1.3 tron &map->disk_idx[map->total_disks];
296 1.3 tron goto findvol;
297 1.3 tron }
298 1.1 tron }
299 1.1 tron
300 1.1 tron out:
301 1.1 tron free(info, M_DEVBUF);
302 1.1 tron return error;
303 1.1 tron }
304 1.5 bsh
305 1.5 bsh
306 1.5 bsh /*
307 1.5 bsh * Assign `volume id' to RAID volumes.
308 1.5 bsh */
309 1.5 bsh static struct {
310 1.5 bsh /* We assume disks are on the same array if these three values
311 1.5 bsh are same. */
312 1.5 bsh uint32_t config_id;
313 1.5 bsh uint32_t generation;
314 1.5 bsh uint32_t checksum;
315 1.5 bsh
316 1.5 bsh int id;
317 1.5 bsh } array_note[10]; /* XXX: this array is not used after ld_ataraid is
318 1.5 bsh * configured. */
319 1.5 bsh
320 1.5 bsh static int n_array = 0;
321 1.5 bsh static int volume_id = 0;
322 1.5 bsh
323 1.5 bsh static int
324 1.5 bsh find_volume_id(struct intel_raid_conf *info)
325 1.5 bsh {
326 1.5 bsh int i, ret;
327 1.5 bsh
328 1.5 bsh for (i=0; i < n_array; ++i) {
329 1.5 bsh if (info->checksum == array_note[i].checksum &&
330 1.5 bsh info->config_id == array_note[i].config_id &&
331 1.5 bsh info->generation == array_note[i].generation) {
332 1.5 bsh /* we have already seen this array */
333 1.5 bsh return array_note[i].id;
334 1.5 bsh }
335 1.5 bsh }
336 1.5 bsh
337 1.5 bsh if (n_array >= __arraycount(array_note)) {
338 1.5 bsh /* Too many arrays */
339 1.5 bsh return -1;
340 1.5 bsh }
341 1.5 bsh
342 1.5 bsh array_note[n_array].checksum = info->checksum;
343 1.5 bsh array_note[n_array].config_id = info->config_id;
344 1.5 bsh array_note[n_array].generation = info->generation;
345 1.5 bsh array_note[n_array].id = ret = volume_id;
346 1.5 bsh
347 1.5 bsh /* Allocate volume ids for all volumes in this array */
348 1.5 bsh volume_id += info->total_volumes;
349 1.5 bsh ++n_array;
350 1.5 bsh return ret;
351 1.5 bsh }
352