1 /* $NetBSD: ata_raid.c,v 1.36 2015/08/20 14:40:17 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Support for autoconfiguration of RAID sets on ATA RAID controllers. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: ata_raid.c,v 1.36 2015/08/20 14:40:17 christos Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/buf.h> 47 #include <sys/bufq.h> 48 #include <sys/conf.h> 49 #include <sys/device.h> 50 #include <sys/disk.h> 51 #include <sys/disklabel.h> 52 #include <sys/fcntl.h> 53 #include <sys/malloc.h> 54 #include <sys/vnode.h> 55 #include <sys/proc.h> 56 57 #include <miscfs/specfs/specdev.h> 58 59 #include <dev/ata/atareg.h> 60 #include <dev/ata/atavar.h> 61 #include <dev/ata/wdvar.h> 62 63 #include <dev/ata/ata_raidreg.h> 64 #include <dev/ata/ata_raidvar.h> 65 66 #include "locators.h" 67 #include "ioconf.h" 68 69 #ifdef ATA_RAID_DEBUG 70 #define DPRINTF(x) printf x 71 #else 72 #define DPRINTF(x) /* nothing */ 73 #endif 74 75 static int ataraid_match(device_t, cfdata_t, void *); 76 static void ataraid_attach(device_t, device_t, void *); 77 static int ataraid_print(void *, const char *); 78 79 static int ata_raid_finalize(device_t); 80 81 ataraid_array_info_list_t ataraid_array_info_list = 82 TAILQ_HEAD_INITIALIZER(ataraid_array_info_list); 83 u_int ataraid_array_info_count; 84 85 CFATTACH_DECL_NEW(ataraid, 0, 86 ataraid_match, ataraid_attach, NULL, NULL); 87 88 /* 89 * ataraidattach: 90 * 91 * Pseudo-device attach routine. 92 */ 93 void 94 ataraidattach(int count) 95 { 96 97 /* 98 * Register a finalizer which will be used to actually configure 99 * the logical disks configured by ataraid. 100 */ 101 if (config_finalize_register(NULL, ata_raid_finalize) != 0) 102 printf("WARNING: unable to register ATA RAID finalizer\n"); 103 } 104 105 /* 106 * ata_raid_type_name: 107 * 108 * Return the type of ATA RAID. 109 */ 110 const char * 111 ata_raid_type_name(u_int type) 112 { 113 static const char *ata_raid_type_names[] = { 114 "Promise", 115 "Adaptec", 116 "VIA V-RAID", 117 "nVidia", 118 "JMicron", 119 "Intel MatrixRAID" 120 }; 121 122 if (type < __arraycount(ata_raid_type_names)) 123 return (ata_raid_type_names[type]); 124 125 return (NULL); 126 } 127 128 /* 129 * ata_raid_finalize: 130 * 131 * Autoconfiguration finalizer for ATA RAID. 132 */ 133 static int 134 ata_raid_finalize(device_t self) 135 { 136 static struct cfdata ataraid_cfdata = { 137 .cf_name = "ataraid", 138 .cf_atname = "ataraid", 139 .cf_unit = 0, 140 .cf_fstate = FSTATE_STAR, 141 }; 142 extern struct cfdriver ataraid_cd; 143 static int done_once; 144 int error; 145 146 /* 147 * Since we only handle real hardware, we only need to be 148 * called once. 149 */ 150 if (done_once) 151 return (0); 152 done_once = 1; 153 154 if (TAILQ_EMPTY(&ataraid_array_info_list)) 155 goto out; 156 157 error = config_cfattach_attach(ataraid_cd.cd_name, &ataraid_ca); 158 if (error) { 159 printf("%s: unable to register cfattach, error = %d\n", 160 ataraid_cd.cd_name, error); 161 (void) config_cfdriver_detach(&ataraid_cd); 162 goto out; 163 } 164 165 if (config_attach_pseudo(&ataraid_cfdata) == NULL) 166 printf("%s: unable to attach an instance\n", 167 ataraid_cd.cd_name); 168 169 out: 170 return (1); 171 } 172 173 /* 174 * ataraid_match: 175 * 176 * Autoconfiguration glue: match routine. 177 */ 178 static int 179 ataraid_match(device_t parent, cfdata_t cf, void *aux) 180 { 181 182 /* pseudo-device; always present */ 183 return (1); 184 } 185 186 /* 187 * ataraid_attach: 188 * 189 * Autoconfiguration glue: attach routine. We attach the children. 190 */ 191 static void 192 ataraid_attach(device_t parent, device_t self, void *aux) 193 { 194 struct ataraid_array_info *aai; 195 int locs[ATARAIDCF_NLOCS]; 196 197 /* 198 * We're a pseudo-device, so we get to announce our own 199 * presence. 200 */ 201 aprint_normal_dev(self, "found %u RAID volume%s\n", 202 ataraid_array_info_count, 203 ataraid_array_info_count == 1 ? "" : "s"); 204 205 TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) { 206 locs[ATARAIDCF_VENDTYPE] = aai->aai_type; 207 locs[ATARAIDCF_UNIT] = aai->aai_arrayno; 208 209 config_found_sm_loc(self, "ataraid", locs, aai, 210 ataraid_print, config_stdsubmatch); 211 } 212 } 213 214 /* 215 * ataraid_print: 216 * 217 * Autoconfiguration glue: print routine. 218 */ 219 static int 220 ataraid_print(void *aux, const char *pnp) 221 { 222 struct ataraid_array_info *aai = aux; 223 224 if (pnp != NULL) 225 aprint_normal("block device at %s", pnp); 226 aprint_normal(" vendtype %d unit %d", aai->aai_type, aai->aai_arrayno); 227 return (UNCONF); 228 } 229 230 /* 231 * ata_raid_check_component: 232 * 233 * Check the component for a RAID configuration structure. 234 * Called via autoconfiguration callback. 235 */ 236 void 237 ata_raid_check_component(device_t self) 238 { 239 struct wd_softc *sc = device_private(self); 240 241 if (ata_raid_read_config_adaptec(sc) == 0) 242 return; 243 if (ata_raid_read_config_promise(sc) == 0) 244 return; 245 if (ata_raid_read_config_via(sc) == 0) 246 return; 247 if (ata_raid_read_config_nvidia(sc) == 0) 248 return; 249 if (ata_raid_read_config_jmicron(sc) == 0) 250 return; 251 if (ata_raid_read_config_intel(sc) == 0) 252 return; 253 } 254 255 struct ataraid_array_info * 256 ata_raid_get_array_info(u_int type, u_int arrayno) 257 { 258 struct ataraid_array_info *aai, *laai; 259 260 TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) { 261 if (aai->aai_type == type && 262 aai->aai_arrayno == arrayno) 263 goto out; 264 } 265 266 /* Need to allocate a new one. */ 267 aai = malloc(sizeof(*aai), M_DEVBUF, M_WAITOK | M_ZERO); 268 aai->aai_type = type; 269 aai->aai_arrayno = arrayno; 270 aai->aai_curdisk = 0; 271 272 ataraid_array_info_count++; 273 274 /* Sort it into the list: type first, then array number. */ 275 TAILQ_FOREACH(laai, &ataraid_array_info_list, aai_list) { 276 if (aai->aai_type < laai->aai_type) { 277 TAILQ_INSERT_BEFORE(laai, aai, aai_list); 278 goto out; 279 } 280 if (aai->aai_type == laai->aai_type && 281 aai->aai_arrayno < laai->aai_arrayno) { 282 TAILQ_INSERT_BEFORE(laai, aai, aai_list); 283 goto out; 284 } 285 } 286 TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list); 287 288 out: 289 return (aai); 290 } 291 292 int 293 ata_raid_config_block_rw(struct vnode *vp, daddr_t blkno, void *tbuf, 294 size_t size, int bflags) 295 { 296 struct buf *bp; 297 int error; 298 299 bp = getiobuf(vp, false); 300 bp->b_blkno = blkno; 301 bp->b_bcount = bp->b_resid = size; 302 bp->b_flags = bflags; 303 bp->b_proc = curproc; 304 bp->b_data = tbuf; 305 SET(bp->b_cflags, BC_BUSY); /* mark buffer busy */ 306 307 VOP_STRATEGY(vp, bp); 308 error = biowait(bp); 309 310 putiobuf(bp); 311 return (error); 312 } 313