xref: /netbsd-src/sys/dev/ata/ata_raid.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: ata_raid.c,v 1.4 2003/02/25 21:25:40 thorpej 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/param.h>
43 #include <sys/buf.h>
44 #include <sys/conf.h>
45 #include <sys/device.h>
46 #include <sys/disk.h>
47 #include <sys/disklabel.h>
48 #include <sys/fcntl.h>
49 #include <sys/malloc.h>
50 #include <sys/vnode.h>
51 
52 #include <miscfs/specfs/specdev.h>
53 
54 #define	__ATA_DISK_PRIVATE
55 
56 #include <dev/ata/atareg.h>
57 #include <dev/ata/atavar.h>
58 #include <dev/ata/wdvar.h>
59 
60 #include <dev/ata/ata_raidreg.h>
61 #include <dev/ata/ata_raidvar.h>
62 
63 #include "locators.h"
64 
65 #ifdef ATA_RAID_DEBUG
66 #define	DPRINTF(x)	printf x
67 #else
68 #define	DPRINTF(x)	/* nothing */
69 #endif
70 
71 void		ataraidattach(int);
72 
73 static int	ataraid_match(struct device *, struct cfdata *, void *);
74 static void	ataraid_attach(struct device *, struct device *, void *);
75 static int	ataraid_print(void *, const char *);
76 
77 static int	ataraid_submatch(struct device *, struct cfdata *, void *);
78 
79 static int	ata_raid_finalize(struct device *);
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(ataraid, sizeof(struct device),
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 	};
116 
117 	if (type <= ATA_RAID_TYPE_MAX)
118 		return (ata_raid_type_names[type]);
119 
120 	return (NULL);
121 }
122 
123 /*
124  * ata_raid_finalize:
125  *
126  *	Autoconfiguration finalizer for ATA RAID.
127  */
128 static int
129 ata_raid_finalize(struct device *self)
130 {
131 	extern struct cfdriver ataraid_cd;
132 	static int done_once;
133 	int error;
134 
135 	/*
136 	 * Since we only handle real hardware, we only need to be
137 	 * called once.
138 	 */
139 	if (done_once)
140 		return (0);
141 	done_once = 1;
142 
143 	if (TAILQ_EMPTY(&ataraid_array_info_list))
144 		goto out;
145 
146 	error = config_cfattach_attach(ataraid_cd.cd_name, &ataraid_ca);
147 	if (error) {
148 		printf("%s: unable to register cfattach, error = %d\n",
149 		    ataraid_cd.cd_name, error);
150 		(void) config_cfdriver_detach(&ataraid_cd);
151 		goto out;
152 	}
153 
154 	if (config_attach_pseudo(ataraid_cd.cd_name, -1) == NULL)
155 		printf("%s: unable to attach an instance\n",
156 		    ataraid_cd.cd_name);
157 
158  out:
159 	return (1);
160 }
161 
162 /*
163  * ataraid_match:
164  *
165  *	Autoconfiguration glue: match routine.
166  */
167 static int
168 ataraid_match(struct device *parent, struct cfdata *cf, void *aux)
169 {
170 
171 	/* pseudo-device; always present */
172 	return (1);
173 }
174 
175 /*
176  * ataraid_attach:
177  *
178  *	Autoconfiguration glue: attach routine.  We attach the children.
179  */
180 static void
181 ataraid_attach(struct device *parent, struct device *self, void *aux)
182 {
183 	struct ataraid_array_info *aai;
184 
185 	/*
186 	 * We're a pseudo-device, so we get to announce our own
187 	 * presence.
188 	 */
189 	aprint_normal("%s: found %u RAID volume%s\n",
190 	    self->dv_xname, ataraid_array_info_count,
191 	    ataraid_array_info_count == 1 ? "" : "s");
192 
193 	TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) {
194 		config_found_sm(self, aai, ataraid_print, ataraid_submatch);
195 	}
196 }
197 
198 /*
199  * ataraid_print:
200  *
201  *	Autoconfiguration glue: print routine.
202  */
203 static int
204 ataraid_print(void *aux, const char *pnp)
205 {
206 	struct ataraid_array_info *aai = aux;
207 
208 	if (pnp != NULL)
209 		aprint_normal("block device at %s", pnp);
210 	aprint_normal(" vendtype %d unit %d", aai->aai_type, aai->aai_arrayno);
211 	return (UNCONF);
212 }
213 
214 /*
215  * ataraid_submatch:
216  *
217  *	Submatch routine for ATA RAID logical disks.
218  */
219 static int
220 ataraid_submatch(struct device *parent, struct cfdata *cf, void *aux)
221 {
222 	struct ataraid_array_info *aai = aux;
223 
224 	if (cf->cf_loc[ATARAIDCF_VENDTYPE] != ATARAIDCF_VENDTYPE_DEFAULT &&
225 	    cf->cf_loc[ATARAIDCF_VENDTYPE] != aai->aai_type)
226 		return (0);
227 
228 	if (cf->cf_loc[ATARAIDCF_UNIT] != ATARAIDCF_UNIT_DEFAULT &&
229 	    cf->cf_loc[ATARAIDCF_UNIT] != aai->aai_arrayno)
230 		return (0);
231 
232 	return (config_match(parent, cf, aux));
233 }
234 
235 /*
236  * ata_raid_check_component:
237  *
238  *	Check the componet for a RAID configuration structure.
239  *	Called via autoconfiguration callback.
240  */
241 void
242 ata_raid_check_component(struct device *self)
243 {
244 	struct wd_softc *sc = (void *) self;
245 
246 	if (ata_raid_read_config_promise(sc) == 0)
247 		return;
248 }
249 
250 struct ataraid_array_info *
251 ata_raid_get_array_info(u_int type, u_int arrayno)
252 {
253 	struct ataraid_array_info *aai, *laai;
254 
255 	TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) {
256 		if (aai->aai_type == type &&
257 		    aai->aai_arrayno == arrayno)
258 			goto out;
259 	}
260 
261 	/* Need to allocate a new one. */
262 	aai = malloc(sizeof(*aai), M_DEVBUF, M_WAITOK | M_ZERO);
263 	aai->aai_type = type;
264 	aai->aai_arrayno = arrayno;
265 
266 	ataraid_array_info_count++;
267 
268 	if (TAILQ_EMPTY(&ataraid_array_info_list)) {
269 		TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list);
270 		goto out;
271 	}
272 
273 	/* Sort it into the list: type first, then array number. */
274 	TAILQ_FOREACH(laai, &ataraid_array_info_list, aai_list) {
275 		if (aai->aai_type < laai->aai_type) {
276 			TAILQ_INSERT_BEFORE(laai, aai, aai_list);
277 			goto out;
278 		}
279 		if (aai->aai_type == laai->aai_type &&
280 		    aai->aai_arrayno < laai->aai_arrayno) {
281 			TAILQ_INSERT_BEFORE(laai, aai, aai_list);
282 			goto out;
283 		}
284 	}
285 	TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list);
286 
287  out:
288 	return (aai);
289 }
290 
291 int
292 ata_raid_config_block_rw(struct vnode *vp, daddr_t blkno, void *buf,
293     size_t size, int bflags)
294 {
295 	struct buf *bp;
296 	int error, s;
297 
298 	s = splbio();
299 	bp = pool_get(&bufpool, PR_WAITOK);
300 	splx(s);
301 	BUF_INIT(bp);
302 
303 	bp->b_vp = vp;
304 	bp->b_dev = vp->v_rdev;
305 	bp->b_blkno = blkno;
306 	bp->b_bcount = bp->b_resid = size;
307 	bp->b_flags = bflags;
308 	bp->b_proc = curproc;
309 	bp->b_data = buf;
310 
311 	VOP_STRATEGY(bp);
312 	error = biowait(bp);
313 
314 	s = splbio();
315 	pool_put(&bufpool, bp);
316 	splx(s);
317 	return (error);
318 }
319