1 1.12 mrg /* $NetBSD: ata_raid_intel.c,v 1.12 2023/08/01 07:58:41 mrg 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.12 mrg __KERNEL_RCSID(0, "$NetBSD: ata_raid_intel.c,v 1.12 2023/08/01 07:58:41 mrg 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/vnode.h> 47 1.1 tron #include <sys/kauth.h> 48 1.1 tron 49 1.1 tron #include <miscfs/specfs/specdev.h> 50 1.1 tron 51 1.1 tron #include <dev/ata/atareg.h> 52 1.1 tron #include <dev/ata/atavar.h> 53 1.1 tron #include <dev/ata/wdvar.h> 54 1.1 tron 55 1.1 tron #include <dev/ata/ata_raidreg.h> 56 1.1 tron #include <dev/ata/ata_raidvar.h> 57 1.1 tron 58 1.1 tron #ifdef ATA_RAID_DEBUG 59 1.1 tron #define DPRINTF(x) printf x 60 1.1 tron #else 61 1.1 tron #define DPRINTF(x) /* nothing */ 62 1.1 tron #endif 63 1.1 tron 64 1.5 bsh static int find_volume_id(struct intel_raid_conf *); 65 1.5 bsh 66 1.5 bsh 67 1.1 tron #ifdef ATA_RAID_DEBUG 68 1.1 tron static const char * 69 1.1 tron ata_raid_intel_type(int type) 70 1.1 tron { 71 1.1 tron static char buffer[16]; 72 1.1 tron 73 1.1 tron switch (type) { 74 1.1 tron case INTEL_T_RAID0: 75 1.1 tron return "RAID0"; 76 1.1 tron case INTEL_T_RAID1: 77 1.1 tron return "RAID1"; 78 1.1 tron case INTEL_T_RAID5: 79 1.1 tron return "RAID5"; 80 1.1 tron default: 81 1.7 christos snprintf(buffer, sizeof(buffer), "UNKNOWN 0x%02x", type); 82 1.1 tron return buffer; 83 1.1 tron } 84 1.1 tron } 85 1.1 tron 86 1.1 tron static void 87 1.1 tron ata_raid_intel_print_info(struct intel_raid_conf *info) 88 1.1 tron { 89 1.1 tron struct intel_raid_mapping *map; 90 1.1 tron int i, j; 91 1.1 tron 92 1.1 tron printf("********* ATA Intel MatrixRAID Metadata *********\n"); 93 1.1 tron printf("intel_id <%.24s>\n", info->intel_id); 94 1.1 tron printf("version <%.6s>\n", info->version); 95 1.1 tron printf("checksum 0x%08x\n", info->checksum); 96 1.1 tron printf("config_size 0x%08x\n", info->config_size); 97 1.1 tron printf("config_id 0x%08x\n", info->config_id); 98 1.1 tron printf("generation 0x%08x\n", info->generation); 99 1.1 tron printf("total_disks %u\n", info->total_disks); 100 1.1 tron printf("total_volumes %u\n", info->total_volumes); 101 1.1 tron printf("DISK# serial disk sectors disk_id flags\n"); 102 1.1 tron for (i = 0; i < info->total_disks; i++) { 103 1.1 tron printf(" %d <%.16s> %u 0x%08x 0x%08x\n", 104 1.1 tron i, info->disk[i].serial, info->disk[i].sectors, 105 1.1 tron info->disk[i].id, info->disk[i].flags); 106 1.1 tron } 107 1.1 tron 108 1.1 tron map = (struct intel_raid_mapping *)&info->disk[info->total_disks]; 109 1.1 tron for (j = 0; j < info->total_volumes; j++) { 110 1.1 tron printf("name %.16s\n", map->name); 111 1.1 tron printf("total_sectors %ju\n", map->total_sectors); 112 1.1 tron printf("state %u\n", map->state); 113 1.1 tron printf("reserved %u\n", map->reserved); 114 1.1 tron printf("offset %u\n", map->offset); 115 1.1 tron printf("disk_sectors %u\n", map->disk_sectors); 116 1.1 tron printf("stripe_count %u\n", map->stripe_count); 117 1.1 tron printf("stripe_sectors %u\n", map->stripe_sectors); 118 1.1 tron printf("status %u\n", map->status); 119 1.1 tron printf("type %s\n", ata_raid_intel_type(map->type)); 120 1.1 tron printf("total_disks %u\n", map->total_disks); 121 1.1 tron printf("magic[0] 0x%02x\n", map->magic[0]); 122 1.1 tron printf("magic[1] 0x%02x\n", map->magic[1]); 123 1.1 tron printf("magic[2] 0x%02x\n", map->magic[2]); 124 1.1 tron for (i = 0; i < map->total_disks; i++) 125 1.1 tron printf(" disk %d at disk_idx 0x%08x\n", 126 1.1 tron i, map->disk_idx[i]); 127 1.1 tron 128 1.1 tron map = (struct intel_raid_mapping *) 129 1.1 tron &map->disk_idx[map->total_disks]; 130 1.1 tron } 131 1.1 tron printf("=================================================\n"); 132 1.1 tron } 133 1.1 tron #endif 134 1.1 tron 135 1.1 tron int 136 1.1 tron ata_raid_read_config_intel(struct wd_softc *sc) 137 1.1 tron { 138 1.8 mlelstv struct dk_softc *dksc = &sc->sc_dksc; 139 1.1 tron struct intel_raid_conf *info; 140 1.9 jdolecek const size_t infosz = 1536; 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.9 jdolecek info = kmem_zalloc(infosz, KM_SLEEP); 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.11 hannken vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 162 1.1 tron error = VOP_OPEN(vp, FREAD, NOCRED); 163 1.1 tron if (error) { 164 1.1 tron vput(vp); 165 1.1 tron goto out; 166 1.1 tron } 167 1.1 tron 168 1.1 tron error = ata_raid_config_block_rw(vp, INTEL_LBA(sc), info, 169 1.1 tron 1024, B_READ); 170 1.1 tron VOP_CLOSE(vp, FREAD, NOCRED); 171 1.1 tron vput(vp); 172 1.1 tron if (error) { 173 1.1 tron DPRINTF(("%s: error %d reading Intel MatrixRAID config block\n", 174 1.8 mlelstv dksc->sc_xname, error)); 175 1.1 tron goto out; 176 1.1 tron } 177 1.1 tron 178 1.1 tron tmp = (char *)info; 179 1.1 tron (void)memcpy(tmp + 1024, tmp, 512); 180 1.10 mrg (void)memmove(tmp, tmp + 512, 1024); 181 1.1 tron (void)memset(tmp + 1024, 0, 512); 182 1.1 tron 183 1.1 tron /* Check if this is a Intel RAID struct */ 184 1.1 tron if (strncmp(info->intel_id, INTEL_MAGIC, strlen(INTEL_MAGIC))) { 185 1.1 tron DPRINTF(("%s: Intel MatrixRAID signature check failed\n", 186 1.8 mlelstv dksc->sc_xname)); 187 1.1 tron error = ESRCH; 188 1.1 tron goto out; 189 1.1 tron } 190 1.1 tron 191 1.1 tron /* calculate checksum and compare for valid */ 192 1.1 tron for (checksum = 0, ptr = (uint32_t *)info, count = 0; 193 1.1 tron count < (info->config_size / sizeof(uint32_t)); count++) 194 1.1 tron checksum += *ptr++; 195 1.1 tron 196 1.1 tron checksum -= info->checksum; 197 1.1 tron if (checksum != info->checksum) { 198 1.1 tron DPRINTF(("%s: Intel MatrixRAID checksum failed 0x%x != 0x%x\n", 199 1.8 mlelstv dksc->sc_xname, checksum, info->checksum)); 200 1.1 tron error = ESRCH; 201 1.1 tron goto out; 202 1.1 tron } 203 1.1 tron 204 1.1 tron #ifdef ATA_RAID_DEBUG 205 1.1 tron ata_raid_intel_print_info(info); 206 1.1 tron #endif 207 1.1 tron 208 1.1 tron /* This one points to the first volume */ 209 1.1 tron map = (struct intel_raid_mapping *)&info->disk[info->total_disks]; 210 1.1 tron 211 1.5 bsh volumeid = find_volume_id(info); 212 1.5 bsh if (volumeid < 0) { 213 1.8 mlelstv aprint_error_dev(dksc->sc_dev, 214 1.5 bsh "too many RAID arrays\n"); 215 1.5 bsh error = ENOMEM; 216 1.5 bsh goto out; 217 1.5 bsh } 218 1.5 bsh 219 1.3 tron findvol: 220 1.1 tron /* 221 1.1 tron * Lookup or allocate a new array info structure for this array. 222 1.1 tron */ 223 1.5 bsh aai = ata_raid_get_array_info(ATA_RAID_TYPE_INTEL, volumeid + curvol); 224 1.1 tron 225 1.1 tron /* Fill in array info */ 226 1.1 tron aai->aai_generation = info->generation; 227 1.1 tron aai->aai_status = AAI_S_READY; 228 1.1 tron 229 1.1 tron switch (map->type) { 230 1.1 tron case INTEL_T_RAID0: 231 1.1 tron aai->aai_level = AAI_L_RAID0; 232 1.1 tron aai->aai_width = map->total_disks; 233 1.1 tron break; 234 1.1 tron case INTEL_T_RAID1: 235 1.1 tron aai->aai_level = AAI_L_RAID1; 236 1.1 tron aai->aai_width = 1; 237 1.1 tron break; 238 1.1 tron default: 239 1.1 tron DPRINTF(("%s: unknown Intel MatrixRAID type 0x%02x\n", 240 1.8 mlelstv dksc->sc_xname, map->type)); 241 1.1 tron error = EINVAL; 242 1.1 tron goto out; 243 1.1 tron } 244 1.1 tron 245 1.1 tron switch (map->state) { 246 1.1 tron case INTEL_S_DEGRADED: 247 1.1 tron aai->aai_status |= AAI_S_DEGRADED; 248 1.1 tron break; 249 1.1 tron case INTEL_S_DISABLED: 250 1.1 tron case INTEL_S_FAILURE: 251 1.1 tron aai->aai_status &= ~AAI_S_READY; 252 1.1 tron break; 253 1.1 tron } 254 1.1 tron 255 1.1 tron aai->aai_type = ATA_RAID_TYPE_INTEL; 256 1.1 tron aai->aai_capacity = map->total_sectors; 257 1.6 bsh aai->aai_interleave = map->stripe_sectors; 258 1.1 tron aai->aai_ndisks = map->total_disks; 259 1.1 tron aai->aai_heads = 255; 260 1.1 tron aai->aai_sectors = 63; 261 1.1 tron aai->aai_cylinders = 262 1.1 tron aai->aai_capacity / (aai->aai_heads * aai->aai_sectors); 263 1.1 tron aai->aai_offset = map->offset; 264 1.1 tron aai->aai_reserved = 3; 265 1.12 mrg 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.9 jdolecek kmem_free(info, infosz); 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