1 1.5 hannken /* $NetBSD: ata_raid_nvidia.c,v 1.5 2022/03/19 13:51:01 hannken Exp $ */ 2 1.1 tacha 3 1.1 tacha /*- 4 1.1 tacha * Copyright (c) 2000 - 2008 Sren Schmidt <sos (at) FreeBSD.org> 5 1.1 tacha * All rights reserved. 6 1.1 tacha * 7 1.1 tacha * Redistribution and use in source and binary forms, with or without 8 1.1 tacha * modification, are permitted provided that the following conditions 9 1.1 tacha * are met: 10 1.1 tacha * 1. Redistributions of source code must retain the above copyright 11 1.1 tacha * notice, this list of conditions and the following disclaimer, 12 1.1 tacha * without modification, immediately at the beginning of the file. 13 1.1 tacha * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 tacha * notice, this list of conditions and the following disclaimer in the 15 1.1 tacha * documentation and/or other materials provided with the distribution. 16 1.1 tacha * 17 1.1 tacha * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 1.1 tacha * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 1.1 tacha * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 1.1 tacha * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 1.1 tacha * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 1.1 tacha * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 1.1 tacha * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 1.1 tacha * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 1.1 tacha * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 1.1 tacha * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 1.1 tacha */ 28 1.1 tacha 29 1.1 tacha /* 30 1.1 tacha * Support for parsing nVIDIA MediaShield RAID controller configuration blocks. 31 1.1 tacha * 32 1.1 tacha * Adapted to NetBSD by Tatoku Ogaito (tacha (at) NetBSD.org) 33 1.1 tacha */ 34 1.1 tacha 35 1.1 tacha #include <sys/cdefs.h> 36 1.5 hannken __KERNEL_RCSID(0, "$NetBSD: ata_raid_nvidia.c,v 1.5 2022/03/19 13:51:01 hannken Exp $"); 37 1.1 tacha 38 1.1 tacha #include <sys/param.h> 39 1.1 tacha #include <sys/buf.h> 40 1.1 tacha #include <sys/bufq.h> 41 1.1 tacha #include <sys/conf.h> 42 1.1 tacha #include <sys/device.h> 43 1.1 tacha #include <sys/disk.h> 44 1.1 tacha #include <sys/disklabel.h> 45 1.1 tacha #include <sys/fcntl.h> 46 1.1 tacha #include <sys/vnode.h> 47 1.1 tacha #include <sys/kauth.h> 48 1.1 tacha 49 1.1 tacha #include <miscfs/specfs/specdev.h> 50 1.1 tacha 51 1.1 tacha #include <dev/ata/atareg.h> 52 1.1 tacha #include <dev/ata/atavar.h> 53 1.1 tacha #include <dev/ata/wdvar.h> 54 1.1 tacha 55 1.1 tacha #include <dev/ata/ata_raidreg.h> 56 1.1 tacha #include <dev/ata/ata_raidvar.h> 57 1.1 tacha 58 1.1 tacha #ifdef ATA_RAID_DEBUG 59 1.1 tacha #define DPRINTF(x) printf x 60 1.1 tacha #else 61 1.1 tacha #define DPRINTF(x) /* nothing */ 62 1.1 tacha #endif 63 1.1 tacha 64 1.1 tacha #ifdef ATA_RAID_DEBUG 65 1.1 tacha static const char * 66 1.1 tacha ata_raid_nvidia_type(int type) 67 1.1 tacha { 68 1.1 tacha static char buffer[16]; 69 1.1 tacha 70 1.1 tacha switch (type) { 71 1.1 tacha case NV_T_SPAN: return "SPAN"; 72 1.1 tacha case NV_T_RAID0: return "RAID0"; 73 1.1 tacha case NV_T_RAID1: return "RAID1"; 74 1.1 tacha case NV_T_RAID3: return "RAID3"; 75 1.1 tacha case NV_T_RAID5: return "RAID5"; 76 1.1 tacha case NV_T_RAID01: return "RAID0+1"; 77 1.1 tacha default: 78 1.2 christos snprintf(buffer, sizeof(buffer), "UNKNOWN 0x%02x", type); 79 1.1 tacha return buffer; 80 1.1 tacha } 81 1.1 tacha } 82 1.1 tacha 83 1.1 tacha static void 84 1.1 tacha ata_raid_nvidia_print_info(struct nvidia_raid_conf *info) 85 1.1 tacha { 86 1.1 tacha printf("******** ATA nVidia MediaShield Metadata ********\n"); 87 1.1 tacha printf("nvidia_id <%.8s>\n", info->nvidia_id); 88 1.1 tacha printf("config_size %d\n", info->config_size); 89 1.1 tacha printf("checksum 0x%08x\n", info->checksum); 90 1.1 tacha printf("version 0x%04x\n", info->version); 91 1.1 tacha printf("disk_number %d\n", info->disk_number); 92 1.1 tacha printf("dummy_0 0x%02x\n", info->dummy_0); 93 1.1 tacha printf("total_sectors %d\n", info->total_sectors); 94 1.1 tacha printf("sectors_size %d\n", info->sector_size); 95 1.1 tacha printf("serial %.16s\n", info->serial); 96 1.1 tacha printf("revision %.4s\n", info->revision); 97 1.1 tacha printf("dummy_1 0x%08x\n", info->dummy_1); 98 1.1 tacha printf("magic_0 0x%08x\n", info->magic_0); 99 1.1 tacha printf("magic_1 0x%016jx\n", info->magic_1); 100 1.1 tacha printf("magic_2 0x%016jx\n", info->magic_2); 101 1.1 tacha printf("flags 0x%02x\n", info->flags); 102 1.1 tacha printf("array_width %d\n", info->array_width); 103 1.1 tacha printf("total_disks %d\n", info->total_disks); 104 1.1 tacha printf("dummy_2 0x%02x\n", info->dummy_2); 105 1.1 tacha printf("type %s\n", ata_raid_nvidia_type(info->type)); 106 1.1 tacha printf("dummy_3 0x%04x\n", info->dummy_3); 107 1.1 tacha printf("stripe_sectors %d\n", info->stripe_sectors); 108 1.1 tacha printf("stripe_bytes %d\n", info->stripe_bytes); 109 1.1 tacha printf("stripe_shift %d\n", info->stripe_shift); 110 1.1 tacha printf("stripe_mask 0x%08x\n", info->stripe_mask); 111 1.1 tacha printf("stripe_sizesectors %d\n", info->stripe_sizesectors); 112 1.1 tacha printf("stripe_sizebytes %d\n", info->stripe_sizebytes); 113 1.1 tacha printf("rebuild_lba %d\n", info->rebuild_lba); 114 1.1 tacha printf("dummy_4 0x%08x\n", info->dummy_4); 115 1.1 tacha printf("dummy_5 0x%08x\n", info->dummy_5); 116 1.1 tacha printf("status 0x%08x\n", info->status); 117 1.1 tacha printf("=================================================\n"); 118 1.1 tacha } 119 1.1 tacha #endif 120 1.1 tacha 121 1.1 tacha int 122 1.1 tacha ata_raid_read_config_nvidia(struct wd_softc *sc) 123 1.1 tacha { 124 1.3 mlelstv struct dk_softc *dksc = &sc->sc_dksc; 125 1.1 tacha struct nvidia_raid_conf *info; 126 1.1 tacha struct vnode *vp; 127 1.1 tacha int bmajor, error, count; 128 1.1 tacha dev_t dev; 129 1.1 tacha uint32_t cksum, *ckptr; 130 1.1 tacha struct ataraid_array_info *aai; 131 1.1 tacha struct ataraid_disk_info *adi; 132 1.1 tacha static struct _arrayno { 133 1.1 tacha uint64_t magic1; 134 1.1 tacha uint64_t magic2; 135 1.1 tacha struct _arrayno *next; 136 1.1 tacha } arrayno = { 0, 0, NULL}, *anptr; 137 1.1 tacha 138 1.4 jdolecek info = kmem_zalloc(sizeof(*info), KM_SLEEP); 139 1.1 tacha 140 1.3 mlelstv bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0); 141 1.1 tacha 142 1.1 tacha /* Get a vnode for the raw partition of this disk. */ 143 1.3 mlelstv dev = MAKEDISKDEV(bmajor, device_unit(dksc->sc_dev), RAW_PART); 144 1.1 tacha error = bdevvp(dev, &vp); 145 1.1 tacha if (error) 146 1.1 tacha goto out; 147 1.1 tacha 148 1.5 hannken vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 149 1.1 tacha error = VOP_OPEN(vp, FREAD, NOCRED); 150 1.1 tacha if (error) { 151 1.1 tacha vput(vp); 152 1.1 tacha goto out; 153 1.1 tacha } 154 1.1 tacha 155 1.1 tacha error = ata_raid_config_block_rw(vp, NVIDIA_LBA(sc), info, 156 1.1 tacha sizeof(*info), B_READ); 157 1.1 tacha VOP_CLOSE(vp, FREAD, NOCRED); 158 1.1 tacha vput(vp); 159 1.1 tacha if (error) { 160 1.3 mlelstv aprint_error_dev(dksc->sc_dev, 161 1.1 tacha "error %d reading nVidia MediaShield config block\n", error); 162 1.1 tacha goto out; 163 1.1 tacha } 164 1.1 tacha 165 1.1 tacha #ifdef ATA_RAID_DEBUG 166 1.1 tacha ata_raid_nvidia_print_info(info); 167 1.1 tacha #endif 168 1.1 tacha 169 1.1 tacha /* Check the signature. */ 170 1.1 tacha if (strncmp(info->nvidia_id, NV_MAGIC, strlen(NV_MAGIC)) != 0) { 171 1.1 tacha DPRINTF(("%s: nVidia signature check failed\n", 172 1.3 mlelstv dksc->sc_xname)); 173 1.1 tacha error = ESRCH; 174 1.1 tacha goto out; 175 1.1 tacha } 176 1.1 tacha 177 1.1 tacha /* check if the checksum is OK */ 178 1.1 tacha for (cksum = 0, ckptr = (uint32_t *)info, count = 0; count < info->config_size; 179 1.1 tacha count++) 180 1.1 tacha cksum += *ckptr++; 181 1.1 tacha if (cksum) { 182 1.1 tacha DPRINTF(("%s: nVidia checksum failed (0x%02x)\n", 183 1.3 mlelstv dksc->sc_xname, cksum)); 184 1.1 tacha error = ESRCH; 185 1.1 tacha goto out; 186 1.1 tacha } 187 1.1 tacha 188 1.1 tacha /* 189 1.1 tacha * Lookup or allocate a new array info structure for 190 1.1 tacha * this array. Since nVidia raid information does not 191 1.1 tacha * provides array# directory, we must count the number. 192 1.1 tacha * The available traces are magic_1 and magic_2. 193 1.1 tacha */ 194 1.1 tacha for (anptr = &arrayno, count = 0; anptr->next; anptr = anptr->next, count++) { 195 1.1 tacha if (anptr->magic1 == info->magic_1 && 196 1.1 tacha anptr->magic2 == info->magic_2) 197 1.1 tacha break; 198 1.1 tacha } 199 1.1 tacha if (anptr->next == NULL) { 200 1.1 tacha /* new array */ 201 1.1 tacha anptr->magic1 = info->magic_1; 202 1.1 tacha anptr->magic2 = info->magic_2; 203 1.4 jdolecek anptr->next = kmem_zalloc(sizeof(arrayno), KM_SLEEP); 204 1.1 tacha } 205 1.1 tacha aai = ata_raid_get_array_info(ATA_RAID_TYPE_NVIDIA, count); 206 1.1 tacha 207 1.1 tacha aai->aai_status = AAI_S_READY; 208 1.1 tacha if (info->status & NV_S_DEGRADED) 209 1.1 tacha aai->aai_status |= AAI_S_DEGRADED; 210 1.1 tacha 211 1.1 tacha switch (info->type) { 212 1.1 tacha case NV_T_RAID0: 213 1.1 tacha aai->aai_level = AAI_L_RAID0; 214 1.1 tacha break; 215 1.1 tacha 216 1.1 tacha case NV_T_RAID1: 217 1.1 tacha aai->aai_level = AAI_L_RAID1; 218 1.1 tacha break; 219 1.1 tacha 220 1.1 tacha case NV_T_RAID5: 221 1.1 tacha aai->aai_level = AAI_L_RAID5; 222 1.1 tacha break; 223 1.1 tacha 224 1.1 tacha case NV_T_RAID01: 225 1.1 tacha aai->aai_level = AAI_L_RAID0 | AAI_L_RAID1; 226 1.1 tacha break; 227 1.1 tacha 228 1.1 tacha case NV_T_SPAN: 229 1.1 tacha aai->aai_level = AAI_L_SPAN; 230 1.1 tacha break; 231 1.1 tacha 232 1.1 tacha default: 233 1.3 mlelstv aprint_error_dev(dksc->sc_dev, 234 1.1 tacha "unknown nVidia type 0x%02x\n", info->type); 235 1.1 tacha error = EINVAL; 236 1.1 tacha goto out; 237 1.1 tacha } 238 1.1 tacha 239 1.1 tacha aai->aai_type = ATA_RAID_TYPE_NVIDIA; 240 1.1 tacha aai->aai_interleave = info->stripe_sectors; 241 1.1 tacha aai->aai_width = info->array_width; 242 1.1 tacha 243 1.1 tacha aai->aai_ndisks = info->total_disks; 244 1.1 tacha aai->aai_capacity = info->total_sectors; 245 1.1 tacha aai->aai_heads = 255; 246 1.1 tacha aai->aai_sectors = 63; 247 1.1 tacha aai->aai_cylinders = aai->aai_capacity / (aai->aai_heads * aai->aai_sectors); 248 1.1 tacha aai->aai_offset = 0; 249 1.1 tacha aai->aai_reserved = 2; 250 1.1 tacha 251 1.1 tacha adi = &aai->aai_disks[info->disk_number]; 252 1.3 mlelstv adi->adi_dev = dksc->sc_dev; 253 1.1 tacha adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED; 254 1.1 tacha adi->adi_sectors = aai->aai_capacity; 255 1.1 tacha adi->adi_compsize = aai->aai_capacity / info->array_width; 256 1.1 tacha 257 1.1 tacha error = 0; 258 1.1 tacha 259 1.1 tacha out: 260 1.4 jdolecek kmem_free(info, sizeof(*info)); 261 1.1 tacha return (error); 262 1.1 tacha } 263