1 /* $NetBSD: ata_raid.c,v 1.11 2004/10/28 07:07:39 yamt 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.11 2004/10/28 07:07:39 yamt 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 56 #include <miscfs/specfs/specdev.h> 57 58 #include <dev/ata/atareg.h> 59 #include <dev/ata/atavar.h> 60 #include <dev/ata/wdvar.h> 61 62 #include <dev/ata/ata_raidreg.h> 63 #include <dev/ata/ata_raidvar.h> 64 65 #include "locators.h" 66 67 #ifdef ATA_RAID_DEBUG 68 #define DPRINTF(x) printf x 69 #else 70 #define DPRINTF(x) /* nothing */ 71 #endif 72 73 void ataraidattach(int); 74 75 static int ataraid_match(struct device *, struct cfdata *, void *); 76 static void ataraid_attach(struct device *, struct device *, void *); 77 static int ataraid_print(void *, const char *); 78 79 static int ataraid_submatch(struct device *, struct cfdata *, 80 const locdesc_t *, void *); 81 82 static int ata_raid_finalize(struct device *); 83 84 ataraid_array_info_list_t ataraid_array_info_list = 85 TAILQ_HEAD_INITIALIZER(ataraid_array_info_list); 86 u_int ataraid_array_info_count; 87 88 CFATTACH_DECL(ataraid, sizeof(struct device), 89 ataraid_match, ataraid_attach, NULL, NULL); 90 91 /* 92 * ataraidattach: 93 * 94 * Pseudo-device attach routine. 95 */ 96 void 97 ataraidattach(int count) 98 { 99 100 /* 101 * Register a finalizer which will be used to actually configure 102 * the logical disks configured by ataraid. 103 */ 104 if (config_finalize_register(NULL, ata_raid_finalize) != 0) 105 printf("WARNING: unable to register ATA RAID finalizer\n"); 106 } 107 108 /* 109 * ata_raid_type_name: 110 * 111 * Return the type of ATA RAID. 112 */ 113 const char * 114 ata_raid_type_name(u_int type) 115 { 116 static const char *ata_raid_type_names[] = { 117 "Promise", 118 }; 119 120 if (type <= ATA_RAID_TYPE_MAX) 121 return (ata_raid_type_names[type]); 122 123 return (NULL); 124 } 125 126 /* 127 * ata_raid_finalize: 128 * 129 * Autoconfiguration finalizer for ATA RAID. 130 */ 131 static int 132 ata_raid_finalize(struct device *self) 133 { 134 static struct cfdata ataraid_cfdata = { 135 .cf_name = "ataraid", 136 .cf_atname = "ataraid", 137 .cf_unit = DVUNIT_ANY, 138 .cf_fstate = FSTATE_STAR, 139 }; 140 extern struct cfdriver ataraid_cd; 141 static int done_once; 142 int error; 143 144 /* 145 * Since we only handle real hardware, we only need to be 146 * called once. 147 */ 148 if (done_once) 149 return (0); 150 done_once = 1; 151 152 if (TAILQ_EMPTY(&ataraid_array_info_list)) 153 goto out; 154 155 error = config_cfattach_attach(ataraid_cd.cd_name, &ataraid_ca); 156 if (error) { 157 printf("%s: unable to register cfattach, error = %d\n", 158 ataraid_cd.cd_name, error); 159 (void) config_cfdriver_detach(&ataraid_cd); 160 goto out; 161 } 162 163 if (config_attach_pseudo(&ataraid_cfdata) == NULL) 164 printf("%s: unable to attach an instance\n", 165 ataraid_cd.cd_name); 166 167 out: 168 return (1); 169 } 170 171 /* 172 * ataraid_match: 173 * 174 * Autoconfiguration glue: match routine. 175 */ 176 static int 177 ataraid_match(struct device *parent, struct cfdata *cf, void *aux) 178 { 179 180 /* pseudo-device; always present */ 181 return (1); 182 } 183 184 /* 185 * ataraid_attach: 186 * 187 * Autoconfiguration glue: attach routine. We attach the children. 188 */ 189 static void 190 ataraid_attach(struct device *parent, struct device *self, void *aux) 191 { 192 struct ataraid_array_info *aai; 193 int help[3]; 194 locdesc_t *ldesc = (void *)help; /* XXX */ 195 196 /* 197 * We're a pseudo-device, so we get to announce our own 198 * presence. 199 */ 200 aprint_normal("%s: found %u RAID volume%s\n", 201 self->dv_xname, ataraid_array_info_count, 202 ataraid_array_info_count == 1 ? "" : "s"); 203 204 TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) { 205 ldesc->len = 2; 206 ldesc->locs[ATARAIDCF_VENDTYPE] = aai->aai_type; 207 ldesc->locs[ATARAIDCF_UNIT] = aai->aai_arrayno; 208 209 config_found_sm_loc(self, "ataraid", NULL, aai, 210 ataraid_print, ataraid_submatch); 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 * ataraid_submatch: 232 * 233 * Submatch routine for ATA RAID logical disks. 234 */ 235 static int 236 ataraid_submatch(struct device *parent, struct cfdata *cf, 237 const locdesc_t *ldesc, void *aux) 238 { 239 240 if (cf->cf_loc[ATARAIDCF_VENDTYPE] != ATARAIDCF_VENDTYPE_DEFAULT && 241 cf->cf_loc[ATARAIDCF_VENDTYPE] != ldesc->locs[ATARAIDCF_VENDTYPE]) 242 return (0); 243 244 if (cf->cf_loc[ATARAIDCF_UNIT] != ATARAIDCF_UNIT_DEFAULT && 245 cf->cf_loc[ATARAIDCF_UNIT] != ldesc->locs[ATARAIDCF_UNIT]) 246 return (0); 247 248 return (config_match(parent, cf, aux)); 249 } 250 251 /* 252 * ata_raid_check_component: 253 * 254 * Check the component for a RAID configuration structure. 255 * Called via autoconfiguration callback. 256 */ 257 void 258 ata_raid_check_component(struct device *self) 259 { 260 struct wd_softc *sc = (void *) self; 261 262 if (ata_raid_read_config_promise(sc) == 0) 263 return; 264 } 265 266 struct ataraid_array_info * 267 ata_raid_get_array_info(u_int type, u_int arrayno) 268 { 269 struct ataraid_array_info *aai, *laai; 270 271 TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) { 272 if (aai->aai_type == type && 273 aai->aai_arrayno == arrayno) 274 goto out; 275 } 276 277 /* Need to allocate a new one. */ 278 aai = malloc(sizeof(*aai), M_DEVBUF, M_WAITOK | M_ZERO); 279 aai->aai_type = type; 280 aai->aai_arrayno = arrayno; 281 282 ataraid_array_info_count++; 283 284 if (TAILQ_EMPTY(&ataraid_array_info_list)) { 285 TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list); 286 goto out; 287 } 288 289 /* Sort it into the list: type first, then array number. */ 290 TAILQ_FOREACH(laai, &ataraid_array_info_list, aai_list) { 291 if (aai->aai_type < laai->aai_type) { 292 TAILQ_INSERT_BEFORE(laai, aai, aai_list); 293 goto out; 294 } 295 if (aai->aai_type == laai->aai_type && 296 aai->aai_arrayno < laai->aai_arrayno) { 297 TAILQ_INSERT_BEFORE(laai, aai, aai_list); 298 goto out; 299 } 300 } 301 TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list); 302 303 out: 304 return (aai); 305 } 306 307 int 308 ata_raid_config_block_rw(struct vnode *vp, daddr_t blkno, void *buf, 309 size_t size, int bflags) 310 { 311 struct buf *bp; 312 int error, s; 313 314 s = splbio(); 315 bp = pool_get(&bufpool, PR_WAITOK); 316 splx(s); 317 BUF_INIT(bp); 318 319 bp->b_vp = vp; 320 bp->b_blkno = blkno; 321 bp->b_bcount = bp->b_resid = size; 322 bp->b_flags = bflags; 323 bp->b_proc = curproc; 324 bp->b_data = buf; 325 326 VOP_STRATEGY(vp, bp); 327 error = biowait(bp); 328 329 s = splbio(); 330 pool_put(&bufpool, bp); 331 splx(s); 332 return (error); 333 } 334