ata_raid_intel.c revision 1.3 1 1.3 tron /* $NetBSD: ata_raid_intel.c,v 1.3 2008/09/16 11:45:30 tron 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.3 tron __KERNEL_RCSID(0, "$NetBSD: ata_raid_intel.c,v 1.3 2008/09/16 11:45:30 tron 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.1 tron #ifdef ATA_RAID_DEBUG
66 1.1 tron static const char *
67 1.1 tron ata_raid_intel_type(int type)
68 1.1 tron {
69 1.1 tron static char buffer[16];
70 1.1 tron
71 1.1 tron switch (type) {
72 1.1 tron case INTEL_T_RAID0:
73 1.1 tron return "RAID0";
74 1.1 tron case INTEL_T_RAID1:
75 1.1 tron return "RAID1";
76 1.1 tron case INTEL_T_RAID5:
77 1.1 tron return "RAID5";
78 1.1 tron default:
79 1.1 tron sprintf(buffer, "UNKNOWN 0x%02x", type);
80 1.1 tron return buffer;
81 1.1 tron }
82 1.1 tron }
83 1.1 tron
84 1.1 tron static void
85 1.1 tron ata_raid_intel_print_info(struct intel_raid_conf *info)
86 1.1 tron {
87 1.1 tron struct intel_raid_mapping *map;
88 1.1 tron int i, j;
89 1.1 tron
90 1.1 tron printf("********* ATA Intel MatrixRAID Metadata *********\n");
91 1.1 tron printf("intel_id <%.24s>\n", info->intel_id);
92 1.1 tron printf("version <%.6s>\n", info->version);
93 1.1 tron printf("checksum 0x%08x\n", info->checksum);
94 1.1 tron printf("config_size 0x%08x\n", info->config_size);
95 1.1 tron printf("config_id 0x%08x\n", info->config_id);
96 1.1 tron printf("generation 0x%08x\n", info->generation);
97 1.1 tron printf("total_disks %u\n", info->total_disks);
98 1.1 tron printf("total_volumes %u\n", info->total_volumes);
99 1.1 tron printf("DISK# serial disk sectors disk_id flags\n");
100 1.1 tron for (i = 0; i < info->total_disks; i++) {
101 1.1 tron printf(" %d <%.16s> %u 0x%08x 0x%08x\n",
102 1.1 tron i, info->disk[i].serial, info->disk[i].sectors,
103 1.1 tron info->disk[i].id, info->disk[i].flags);
104 1.1 tron }
105 1.1 tron
106 1.1 tron map = (struct intel_raid_mapping *)&info->disk[info->total_disks];
107 1.1 tron for (j = 0; j < info->total_volumes; j++) {
108 1.1 tron printf("name %.16s\n", map->name);
109 1.1 tron printf("total_sectors %ju\n", map->total_sectors);
110 1.1 tron printf("state %u\n", map->state);
111 1.1 tron printf("reserved %u\n", map->reserved);
112 1.1 tron printf("offset %u\n", map->offset);
113 1.1 tron printf("disk_sectors %u\n", map->disk_sectors);
114 1.1 tron printf("stripe_count %u\n", map->stripe_count);
115 1.1 tron printf("stripe_sectors %u\n", map->stripe_sectors);
116 1.1 tron printf("status %u\n", map->status);
117 1.1 tron printf("type %s\n", ata_raid_intel_type(map->type));
118 1.1 tron printf("total_disks %u\n", map->total_disks);
119 1.1 tron printf("magic[0] 0x%02x\n", map->magic[0]);
120 1.1 tron printf("magic[1] 0x%02x\n", map->magic[1]);
121 1.1 tron printf("magic[2] 0x%02x\n", map->magic[2]);
122 1.1 tron for (i = 0; i < map->total_disks; i++)
123 1.1 tron printf(" disk %d at disk_idx 0x%08x\n",
124 1.1 tron i, map->disk_idx[i]);
125 1.1 tron
126 1.1 tron map = (struct intel_raid_mapping *)
127 1.1 tron &map->disk_idx[map->total_disks];
128 1.1 tron }
129 1.1 tron printf("=================================================\n");
130 1.1 tron }
131 1.1 tron #endif
132 1.1 tron
133 1.1 tron int
134 1.1 tron ata_raid_read_config_intel(struct wd_softc *sc)
135 1.1 tron {
136 1.1 tron struct intel_raid_conf *info;
137 1.1 tron struct intel_raid_mapping *map;
138 1.1 tron struct ataraid_array_info *aai;
139 1.1 tron struct ataraid_disk_info *adi;
140 1.1 tron struct vnode *vp;
141 1.1 tron uint32_t checksum, *ptr;
142 1.1 tron static int curdrive;
143 1.3 tron int bmajor, count, curvol = 0, error = 0;
144 1.1 tron char *tmp;
145 1.1 tron dev_t dev;
146 1.1 tron
147 1.1 tron info = malloc(1536, M_DEVBUF, M_WAITOK|M_ZERO);
148 1.1 tron
149 1.1 tron bmajor = devsw_name2blk(device_xname(sc->sc_dev), NULL, 0);
150 1.1 tron
151 1.1 tron /* Get a vnode for the raw partition of this disk. */
152 1.1 tron dev = MAKEDISKDEV(bmajor, device_unit(sc->sc_dev), RAW_PART);
153 1.1 tron error = bdevvp(dev, &vp);
154 1.1 tron if (error)
155 1.1 tron goto out;
156 1.1 tron
157 1.1 tron error = VOP_OPEN(vp, FREAD, NOCRED);
158 1.1 tron if (error) {
159 1.1 tron vput(vp);
160 1.1 tron goto out;
161 1.1 tron }
162 1.1 tron
163 1.1 tron error = ata_raid_config_block_rw(vp, INTEL_LBA(sc), info,
164 1.1 tron 1024, B_READ);
165 1.1 tron VOP_CLOSE(vp, FREAD, NOCRED);
166 1.1 tron vput(vp);
167 1.1 tron if (error) {
168 1.1 tron DPRINTF(("%s: error %d reading Intel MatrixRAID config block\n",
169 1.1 tron device_xname(sc->sc_dev), error));
170 1.1 tron goto out;
171 1.1 tron }
172 1.1 tron
173 1.1 tron tmp = (char *)info;
174 1.1 tron (void)memcpy(tmp + 1024, tmp, 512);
175 1.1 tron (void)memcpy(tmp, tmp + 512, 1024);
176 1.1 tron (void)memset(tmp + 1024, 0, 512);
177 1.1 tron
178 1.1 tron /* Check if this is a Intel RAID struct */
179 1.1 tron if (strncmp(info->intel_id, INTEL_MAGIC, strlen(INTEL_MAGIC))) {
180 1.1 tron DPRINTF(("%s: Intel MatrixRAID signature check failed\n",
181 1.1 tron device_xname(sc->sc_dev)));
182 1.1 tron error = ESRCH;
183 1.1 tron goto out;
184 1.1 tron }
185 1.1 tron
186 1.1 tron /* calculate checksum and compare for valid */
187 1.1 tron for (checksum = 0, ptr = (uint32_t *)info, count = 0;
188 1.1 tron count < (info->config_size / sizeof(uint32_t)); count++)
189 1.1 tron checksum += *ptr++;
190 1.1 tron
191 1.1 tron checksum -= info->checksum;
192 1.1 tron if (checksum != info->checksum) {
193 1.1 tron DPRINTF(("%s: Intel MatrixRAID checksum failed 0x%x != 0x%x\n",
194 1.1 tron device_xname(sc->sc_dev), checksum, info->checksum));
195 1.1 tron error = ESRCH;
196 1.1 tron goto out;
197 1.1 tron }
198 1.1 tron
199 1.1 tron #ifdef ATA_RAID_DEBUG
200 1.1 tron ata_raid_intel_print_info(info);
201 1.1 tron #endif
202 1.1 tron
203 1.1 tron /* This one points to the first volume */
204 1.1 tron map = (struct intel_raid_mapping *)&info->disk[info->total_disks];
205 1.1 tron
206 1.3 tron findvol:
207 1.1 tron /*
208 1.1 tron * Lookup or allocate a new array info structure for this array.
209 1.1 tron */
210 1.3 tron aai = ata_raid_get_array_info(ATA_RAID_TYPE_INTEL, curvol);
211 1.1 tron
212 1.1 tron /* Fill in array info */
213 1.1 tron aai->aai_generation = info->generation;
214 1.1 tron aai->aai_status = AAI_S_READY;
215 1.1 tron
216 1.1 tron switch (map->type) {
217 1.1 tron case INTEL_T_RAID0:
218 1.1 tron aai->aai_level = AAI_L_RAID0;
219 1.1 tron aai->aai_width = map->total_disks;
220 1.1 tron break;
221 1.1 tron case INTEL_T_RAID1:
222 1.1 tron aai->aai_level = AAI_L_RAID1;
223 1.1 tron aai->aai_width = 1;
224 1.1 tron break;
225 1.1 tron default:
226 1.1 tron DPRINTF(("%s: unknown Intel MatrixRAID type 0x%02x\n",
227 1.1 tron sc->sc_dev->dv_xname, map->type));
228 1.1 tron error = EINVAL;
229 1.1 tron goto out;
230 1.1 tron }
231 1.1 tron
232 1.1 tron switch (map->state) {
233 1.1 tron case INTEL_S_DEGRADED:
234 1.1 tron aai->aai_status |= AAI_S_DEGRADED;
235 1.1 tron break;
236 1.1 tron case INTEL_S_DISABLED:
237 1.1 tron case INTEL_S_FAILURE:
238 1.1 tron aai->aai_status &= ~AAI_S_READY;
239 1.1 tron break;
240 1.1 tron }
241 1.1 tron
242 1.1 tron aai->aai_type = ATA_RAID_TYPE_INTEL;
243 1.1 tron aai->aai_capacity = map->total_sectors;
244 1.3 tron aai->aai_interleave = map->stripe_sectors / 2;
245 1.1 tron aai->aai_ndisks = map->total_disks;
246 1.1 tron aai->aai_heads = 255;
247 1.1 tron aai->aai_sectors = 63;
248 1.1 tron aai->aai_cylinders =
249 1.1 tron aai->aai_capacity / (aai->aai_heads * aai->aai_sectors);
250 1.1 tron aai->aai_offset = map->offset;
251 1.1 tron aai->aai_reserved = 3;
252 1.2 tron if (map->name)
253 1.2 tron strlcpy(aai->aai_name, map->name, sizeof(aai->aai_name));
254 1.1 tron
255 1.1 tron /* Fill in disk info */
256 1.1 tron adi = &aai->aai_disks[curdrive];
257 1.1 tron adi->adi_status = 0;
258 1.1 tron
259 1.1 tron if (info->disk[curdrive].flags & INTEL_F_ONLINE)
260 1.1 tron adi->adi_status |= ADI_S_ONLINE;
261 1.1 tron if (info->disk[curdrive].flags & INTEL_F_ASSIGNED)
262 1.1 tron adi->adi_status |= ADI_S_ASSIGNED;
263 1.1 tron if (info->disk[curdrive].flags & INTEL_F_SPARE) {
264 1.1 tron adi->adi_status &= ~ADI_S_ONLINE;
265 1.1 tron adi->adi_status |= ADI_S_SPARE;
266 1.1 tron }
267 1.1 tron if (info->disk[curdrive].flags & INTEL_F_DOWN)
268 1.1 tron adi->adi_status &= ~ADI_S_ONLINE;
269 1.1 tron
270 1.1 tron if (adi->adi_status) {
271 1.1 tron adi->adi_dev = sc->sc_dev;
272 1.1 tron adi->adi_sectors = info->disk[curdrive].sectors;
273 1.1 tron adi->adi_compsize = adi->adi_sectors - aai->aai_reserved;
274 1.3 tron /*
275 1.3 tron * Check if that is the only volume, otherwise repeat
276 1.3 tron * the process to find more.
277 1.3 tron */
278 1.3 tron if ((curvol + 1) < info->total_volumes) {
279 1.3 tron curvol++;
280 1.3 tron map = (struct intel_raid_mapping *)
281 1.3 tron &map->disk_idx[map->total_disks];
282 1.3 tron goto findvol;
283 1.3 tron }
284 1.1 tron curdrive++;
285 1.1 tron }
286 1.1 tron
287 1.1 tron out:
288 1.1 tron free(info, M_DEVBUF);
289 1.1 tron return error;
290 1.1 tron }
291