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