1/* $NetBSD: ata_raid_jmicron.c,v 1.9 2023/08/01 07:58:41 mrg Exp $ */ 2 3/*- 4 * Copyright (c) 2000-2008 S�ren Schmidt <sos@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 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31/* 32 * Support for parsing JMicron Technology RAID controller configuration blocks. 33 * 34 * Adapted to NetBSD by Juan Romero Pardines (xtraeme@gmail.org). 35 */ 36 37#include <sys/cdefs.h> 38__KERNEL_RCSID(0, "$NetBSD: ata_raid_jmicron.c,v 1.9 2023/08/01 07:58:41 mrg Exp $"); 39 40#include <sys/param.h> 41#include <sys/buf.h> 42#include <sys/bufq.h> 43#include <sys/conf.h> 44#include <sys/device.h> 45#include <sys/disk.h> 46#include <sys/disklabel.h> 47#include <sys/fcntl.h> 48#include <sys/vnode.h> 49#include <sys/kauth.h> 50 51#include <miscfs/specfs/specdev.h> 52 53#include <dev/ata/atareg.h> 54#include <dev/ata/atavar.h> 55#include <dev/ata/wdvar.h> 56 57#include <dev/ata/ata_raidreg.h> 58#include <dev/ata/ata_raidvar.h> 59 60#ifdef ATA_RAID_DEBUG 61#define DPRINTF(x) printf x 62#else 63#define DPRINTF(x) /* nothing */ 64#endif 65 66#ifdef ATA_RAID_DEBUG 67static const char * 68ata_raid_jmicron_type(int type) 69{ 70 static char buffer[16]; 71 72 switch (type) { 73 case JM_T_RAID0: 74 return "RAID0"; 75 case JM_T_RAID1: 76 return "RAID1"; 77 case JM_T_RAID01: 78 return "RAID0+1"; 79 case JM_T_RAID5: 80 return "RAID5"; 81 case JM_T_JBOD: 82 return "JBOD"; 83 default: 84 snprintf(buffer, sizeof(buffer), "UNKNOWN 0x%02x", type); 85 return buffer; 86 } 87} 88 89static void 90ata_raid_jmicron_print_info(struct jmicron_raid_conf *info) 91{ 92 int i; 93 94 printf("****** ATA JMicron Technology Corp Metadata ******\n"); 95 printf("signature %.2s\n", info->signature); 96 printf("version 0x%04x\n", info->version); 97 printf("checksum 0x%04x\n", info->checksum); 98 printf("disk_id 0x%08x\n", info->disk_id); 99 printf("offset 0x%08x\n", info->offset); 100 printf("disk_sectors_low 0x%08x\n", info->disk_sectors_low); 101 printf("disk_sectors_high 0x%08x\n", info->disk_sectors_high); 102 printf("name %.16s\n", info->name); 103 printf("type %s\n", 104 ata_raid_jmicron_type(info->type)); 105 printf("stripe_shift %d\n", info->stripe_shift); 106 printf("flags 0x%04x\n", info->flags); 107 printf("spare:\n"); 108 for (i = 0; i < 2 && info->spare[i]; i++) 109 printf(" %d 0x%08x\n", i, info->spare[i]); 110 printf("disks:\n"); 111 for (i = 0; i < 8 && info->disks[i]; i++) 112 printf(" %d 0x%08x\n", i, info->disks[i]); 113 printf("=================================================\n"); 114} 115#endif 116 117int 118ata_raid_read_config_jmicron(struct wd_softc *sc) 119{ 120 struct dk_softc *dksc = &sc->sc_dksc; 121 struct atabus_softc *atabus; 122 struct jmicron_raid_conf *info; 123 struct vnode *vp; 124 struct ataraid_array_info *aai; 125 struct ataraid_disk_info *adi; 126 uint64_t disk_size; 127 uint32_t drive; 128 uint16_t checksum, *ptr; 129 int bmajor, error, count, disk, total_disks; 130 dev_t dev; 131 132 info = kmem_zalloc(sizeof(*info), KM_SLEEP); 133 134 bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0); 135 136 /* Get a vnode for the raw partition of this disk. */ 137 dev = MAKEDISKDEV(bmajor, device_unit(dksc->sc_dev), RAW_PART); 138 error = bdevvp(dev, &vp); 139 if (error) 140 goto out; 141 142 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 143 error = VOP_OPEN(vp, FREAD, NOCRED); 144 if (error) { 145 vput(vp); 146 goto out; 147 } 148 149 error = ata_raid_config_block_rw(vp, JMICRON_LBA(sc), info, 150 sizeof(*info), B_READ); 151 VOP_CLOSE(vp, FREAD, NOCRED); 152 vput(vp); 153 if (error) { 154 DPRINTF(("%s: error %d reading JMicron config block\n", 155 dksc->sc_xname, error)); 156 goto out; 157 } 158 159 /* Check for JMicron signature. */ 160 if (strncmp(info->signature, JMICRON_MAGIC, 2)) { 161 DPRINTF(("%s: JMicron RAID signature check failed\n", 162 dksc->sc_xname)); 163 error = ESRCH; 164 goto out; 165 } 166 167 /* calculate checksum and compare for valid */ 168 for (checksum = 0, ptr = (uint16_t *)info, count = 0; 169 count < 64; count++) 170 checksum += *ptr++; 171 if (checksum) { 172 DPRINTF(("%s: JMicron checksum failed\n", 173 dksc->sc_xname)); 174 error = ESRCH; 175 goto out; 176 } 177 178#ifdef ATA_RAID_DEBUG 179 ata_raid_jmicron_print_info(info); 180#endif 181 /* 182 * Check that there aren't stale config blocks without 183 * any array set configured. 184 */ 185 for (total_disks = 0, disk = 0; disk < JM_MAX_DISKS; disk++) 186 if (info->disks[disk] == info->disk_id) 187 total_disks++; 188 if (total_disks <= 1) { 189 error = EINVAL; 190 goto out; 191 } 192 193 /* 194 * Check volume's state and bail out if it's not acceptable. 195 */ 196 if ((info->flags & (JM_F_READY|JM_F_BOOTABLE|JM_F_ACTIVE)) == 0) { 197 error = EINVAL; 198 goto out; 199 } 200 201 /* 202 * Lookup or allocate a new array info structure for 203 * this array. 204 */ 205 aai = ata_raid_get_array_info(ATA_RAID_TYPE_JMICRON, 0); 206 aai->aai_status = AAI_S_READY; 207 208 switch (info->type) { 209 case JM_T_RAID0: 210 aai->aai_level = AAI_L_RAID0; 211 aai->aai_width = total_disks; 212 break; 213 case JM_T_RAID1: 214 aai->aai_level = AAI_L_RAID1; 215 aai->aai_width = 1; 216 break; 217 case JM_T_RAID01: 218 aai->aai_level = AAI_L_RAID0 | AAI_L_RAID1; 219 aai->aai_width = total_disks / 2; 220 break; 221 case JM_T_JBOD: 222 aai->aai_level = AAI_L_SPAN; 223 aai->aai_width = total_disks; 224 break; 225 default: 226 DPRINTF(("%s: unknown JMicron RAID type 0x%02x\n", 227 dksc->sc_xname, info->type)); 228 error = EINVAL; 229 goto out; 230 } 231 232 disk_size = (info->disk_sectors_high << 16) + info->disk_sectors_low; 233 aai->aai_type = ATA_RAID_TYPE_JMICRON; 234 aai->aai_generation = 0; 235 aai->aai_capacity = disk_size * aai->aai_width; 236 aai->aai_interleave = 2 << info->stripe_shift; 237 aai->aai_ndisks = total_disks; 238 aai->aai_heads = 255; 239 aai->aai_sectors = 63; 240 aai->aai_cylinders = 241 aai->aai_capacity / (aai->aai_heads * aai->aai_sectors); 242 aai->aai_offset = info->offset * 16; 243 aai->aai_reserved = 2; 244 strlcpy(aai->aai_name, info->name, sizeof(aai->aai_name)); 245 246 atabus = device_private(device_parent(dksc->sc_dev)); 247 drive = atabus->sc_chan->ch_channel; 248 if (drive >= aai->aai_ndisks) { 249 DPRINTF(("%s: drive number %d doesn't make sense within " 250 "%d-disk array\n", dksc->sc_xname, 251 drive, aai->aai_ndisks)); 252 error = EINVAL; 253 goto out; 254 } 255 256 if (info->disks[drive] == info->disk_id) { 257 adi = &aai->aai_disks[drive]; 258 adi->adi_dev = dksc->sc_dev; 259 adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED; 260 adi->adi_sectors = aai->aai_capacity; 261 adi->adi_compsize = disk_size - aai->aai_reserved; 262 } 263 264 error = 0; 265 266 out: 267 kmem_free(info, sizeof(*info)); 268 return error; 269} 270