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