xref: /netbsd-src/sys/dev/ata/ata_raid.c (revision 9c1da17e908379b8a470f1117a6395bd6a0ca559)
1 /*	$NetBSD: ata_raid.c,v 1.14 2005/07/18 15:21:48 briggs 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.14 2005/07/18 15:21:48 briggs 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 		"Adaptec",
119 	};
120 
121 	if (type < sizeof(ata_raid_type_names) / sizeof(ata_raid_type_names[0]))
122 		return (ata_raid_type_names[type]);
123 
124 	return (NULL);
125 }
126 
127 /*
128  * ata_raid_finalize:
129  *
130  *	Autoconfiguration finalizer for ATA RAID.
131  */
132 static int
133 ata_raid_finalize(struct device *self)
134 {
135 	static struct cfdata ataraid_cfdata = {
136 		.cf_name = "ataraid",
137 		.cf_atname = "ataraid",
138 		.cf_unit = DVUNIT_ANY,
139 		.cf_fstate = FSTATE_STAR,
140 	};
141 	extern struct cfdriver ataraid_cd;
142 	static int done_once;
143 	int error;
144 
145 	/*
146 	 * Since we only handle real hardware, we only need to be
147 	 * called once.
148 	 */
149 	if (done_once)
150 		return (0);
151 	done_once = 1;
152 
153 	if (TAILQ_EMPTY(&ataraid_array_info_list))
154 		goto out;
155 
156 	error = config_cfattach_attach(ataraid_cd.cd_name, &ataraid_ca);
157 	if (error) {
158 		printf("%s: unable to register cfattach, error = %d\n",
159 		    ataraid_cd.cd_name, error);
160 		(void) config_cfdriver_detach(&ataraid_cd);
161 		goto out;
162 	}
163 
164 	if (config_attach_pseudo(&ataraid_cfdata) == NULL)
165 		printf("%s: unable to attach an instance\n",
166 		    ataraid_cd.cd_name);
167 
168  out:
169 	return (1);
170 }
171 
172 /*
173  * ataraid_match:
174  *
175  *	Autoconfiguration glue: match routine.
176  */
177 static int
178 ataraid_match(struct device *parent, struct cfdata *cf, void *aux)
179 {
180 
181 	/* pseudo-device; always present */
182 	return (1);
183 }
184 
185 /*
186  * ataraid_attach:
187  *
188  *	Autoconfiguration glue: attach routine.  We attach the children.
189  */
190 static void
191 ataraid_attach(struct device *parent, struct device *self, void *aux)
192 {
193 	struct ataraid_array_info *aai;
194 	int help[3];
195 	locdesc_t *ldesc = (void *)help; /* XXX */
196 
197 	/*
198 	 * We're a pseudo-device, so we get to announce our own
199 	 * presence.
200 	 */
201 	aprint_normal("%s: found %u RAID volume%s\n",
202 	    self->dv_xname, ataraid_array_info_count,
203 	    ataraid_array_info_count == 1 ? "" : "s");
204 
205 	TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) {
206 		ldesc->len = 2;
207 		ldesc->locs[ATARAIDCF_VENDTYPE] = aai->aai_type;
208 		ldesc->locs[ATARAIDCF_UNIT] = aai->aai_arrayno;
209 
210 		config_found_sm_loc(self, "ataraid", NULL, aai,
211 				    ataraid_print, ataraid_submatch);
212 	}
213 }
214 
215 /*
216  * ataraid_print:
217  *
218  *	Autoconfiguration glue: print routine.
219  */
220 static int
221 ataraid_print(void *aux, const char *pnp)
222 {
223 	struct ataraid_array_info *aai = aux;
224 
225 	if (pnp != NULL)
226 		aprint_normal("block device at %s", pnp);
227 	aprint_normal(" vendtype %d unit %d", aai->aai_type, aai->aai_arrayno);
228 	return (UNCONF);
229 }
230 
231 /*
232  * ataraid_submatch:
233  *
234  *	Submatch routine for ATA RAID logical disks.
235  */
236 static int
237 ataraid_submatch(struct device *parent, struct cfdata *cf,
238 		 const locdesc_t *ldesc, void *aux)
239 {
240 
241 	if (cf->cf_loc[ATARAIDCF_VENDTYPE] != ATARAIDCF_VENDTYPE_DEFAULT &&
242 	    cf->cf_loc[ATARAIDCF_VENDTYPE] != ldesc->locs[ATARAIDCF_VENDTYPE])
243 		return (0);
244 
245 	if (cf->cf_loc[ATARAIDCF_UNIT] != ATARAIDCF_UNIT_DEFAULT &&
246 	    cf->cf_loc[ATARAIDCF_UNIT] != ldesc->locs[ATARAIDCF_UNIT])
247 		return (0);
248 
249 	return (config_match(parent, cf, aux));
250 }
251 
252 /*
253  * ata_raid_check_component:
254  *
255  *	Check the component for a RAID configuration structure.
256  *	Called via autoconfiguration callback.
257  */
258 void
259 ata_raid_check_component(struct device *self)
260 {
261 	struct wd_softc *sc = (void *) self;
262 
263 	if (ata_raid_read_config_adaptec(sc) == 0)
264 		return;
265 	if (ata_raid_read_config_promise(sc) == 0)
266 		return;
267 }
268 
269 struct ataraid_array_info *
270 ata_raid_get_array_info(u_int type, u_int arrayno)
271 {
272 	struct ataraid_array_info *aai, *laai;
273 
274 	TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) {
275 		if (aai->aai_type == type &&
276 		    aai->aai_arrayno == arrayno)
277 			goto out;
278 	}
279 
280 	/* Need to allocate a new one. */
281 	aai = malloc(sizeof(*aai), M_DEVBUF, M_WAITOK | M_ZERO);
282 	aai->aai_type = type;
283 	aai->aai_arrayno = arrayno;
284 
285 	ataraid_array_info_count++;
286 
287 	if (TAILQ_EMPTY(&ataraid_array_info_list)) {
288 		TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list);
289 		goto out;
290 	}
291 
292 	/* Sort it into the list: type first, then array number. */
293 	TAILQ_FOREACH(laai, &ataraid_array_info_list, aai_list) {
294 		if (aai->aai_type < laai->aai_type) {
295 			TAILQ_INSERT_BEFORE(laai, aai, aai_list);
296 			goto out;
297 		}
298 		if (aai->aai_type == laai->aai_type &&
299 		    aai->aai_arrayno < laai->aai_arrayno) {
300 			TAILQ_INSERT_BEFORE(laai, aai, aai_list);
301 			goto out;
302 		}
303 	}
304 	TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list);
305 
306  out:
307 	return (aai);
308 }
309 
310 int
311 ata_raid_config_block_rw(struct vnode *vp, daddr_t blkno, void *tbuf,
312     size_t size, int bflags)
313 {
314 	struct buf *bp;
315 	int error, s;
316 
317 	s = splbio();
318 	bp = pool_get(&bufpool, PR_WAITOK);
319 	splx(s);
320 	BUF_INIT(bp);
321 
322 	bp->b_vp = vp;
323 	bp->b_blkno = blkno;
324 	bp->b_bcount = bp->b_resid = size;
325 	bp->b_flags = bflags;
326 	bp->b_proc = curproc;
327 	bp->b_data = tbuf;
328 
329 	VOP_STRATEGY(vp, bp);
330 	error = biowait(bp);
331 
332 	s = splbio();
333 	pool_put(&bufpool, bp);
334 	splx(s);
335 	return (error);
336 }
337