xref: /netbsd-src/sys/dev/ata/ata_raid.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: ata_raid.c,v 1.27 2008/04/05 22:04:36 cegger 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.27 2008/04/05 22:04:36 cegger 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 
68 #ifdef ATA_RAID_DEBUG
69 #define	DPRINTF(x)	printf x
70 #else
71 #define	DPRINTF(x)	/* nothing */
72 #endif
73 
74 void		ataraidattach(int);
75 
76 static int	ataraid_match(device_t, cfdata_t, void *);
77 static void	ataraid_attach(device_t, device_t, void *);
78 static int	ataraid_print(void *, const char *);
79 
80 static int	ata_raid_finalize(device_t );
81 
82 ataraid_array_info_list_t ataraid_array_info_list =
83     TAILQ_HEAD_INITIALIZER(ataraid_array_info_list);
84 u_int ataraid_array_info_count;
85 
86 CFATTACH_DECL_NEW(ataraid, 0,
87     ataraid_match, ataraid_attach, NULL, NULL);
88 
89 /*
90  * ataraidattach:
91  *
92  *	Pseudo-device attach routine.
93  */
94 void
95 ataraidattach(int count)
96 {
97 
98 	/*
99 	 * Register a finalizer which will be used to actually configure
100 	 * the logical disks configured by ataraid.
101 	 */
102 	if (config_finalize_register(NULL, ata_raid_finalize) != 0)
103 		printf("WARNING: unable to register ATA RAID finalizer\n");
104 }
105 
106 /*
107  * ata_raid_type_name:
108  *
109  *	Return the type of ATA RAID.
110  */
111 const char *
112 ata_raid_type_name(u_int type)
113 {
114 	static const char *ata_raid_type_names[] = {
115 		"Promise",
116 		"Adaptec",
117 		"VIA V-RAID",
118 	};
119 
120 	if (type < sizeof(ata_raid_type_names) / sizeof(ata_raid_type_names[0]))
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(device_t self)
133 {
134 	static struct cfdata ataraid_cfdata = {
135 		.cf_name = "ataraid",
136 		.cf_atname = "ataraid",
137 		.cf_unit = 0,
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(device_t parent, cfdata_t cf,
178     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(device_t parent, device_t self,
192     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 }
248 
249 struct ataraid_array_info *
250 ata_raid_get_array_info(u_int type, u_int arrayno)
251 {
252 	struct ataraid_array_info *aai, *laai;
253 
254 	TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) {
255 		if (aai->aai_type == type &&
256 		    aai->aai_arrayno == arrayno)
257 			goto out;
258 	}
259 
260 	/* Need to allocate a new one. */
261 	aai = malloc(sizeof(*aai), M_DEVBUF, M_WAITOK | M_ZERO);
262 	aai->aai_type = type;
263 	aai->aai_arrayno = arrayno;
264 
265 	ataraid_array_info_count++;
266 
267 	if (TAILQ_EMPTY(&ataraid_array_info_list)) {
268 		TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list);
269 		goto out;
270 	}
271 
272 	/* Sort it into the list: type first, then array number. */
273 	TAILQ_FOREACH(laai, &ataraid_array_info_list, aai_list) {
274 		if (aai->aai_type < laai->aai_type) {
275 			TAILQ_INSERT_BEFORE(laai, aai, aai_list);
276 			goto out;
277 		}
278 		if (aai->aai_type == laai->aai_type &&
279 		    aai->aai_arrayno < laai->aai_arrayno) {
280 			TAILQ_INSERT_BEFORE(laai, aai, aai_list);
281 			goto out;
282 		}
283 	}
284 	TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list);
285 
286  out:
287 	return (aai);
288 }
289 
290 int
291 ata_raid_config_block_rw(struct vnode *vp, daddr_t blkno, void *tbuf,
292     size_t size, int bflags)
293 {
294 	struct buf *bp;
295 	int error;
296 
297 	bp = getiobuf(vp, NULL);
298 	bp->b_blkno = blkno;
299 	bp->b_bcount = bp->b_resid = size;
300 	bp->b_flags = bflags;
301 	bp->b_proc = curproc;
302 	bp->b_data = tbuf;
303 
304 	VOP_STRATEGY(vp, bp);
305 	error = biowait(bp);
306 
307 	putiobuf(bp);
308 	return (error);
309 }
310