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