xref: /titanic_52/usr/src/lib/libraidcfg/common/raidcfg.c (revision 33f5ff17089e3a43e6e730bf80384c233123dbd9)
1711890bcSjc156560 /*
2711890bcSjc156560  * CDDL HEADER START
3711890bcSjc156560  *
4711890bcSjc156560  * The contents of this file are subject to the terms of the
5711890bcSjc156560  * Common Development and Distribution License (the "License").
6711890bcSjc156560  * You may not use this file except in compliance with the License.
7711890bcSjc156560  *
8711890bcSjc156560  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9711890bcSjc156560  * or http://www.opensolaris.org/os/licensing.
10711890bcSjc156560  * See the License for the specific language governing permissions
11711890bcSjc156560  * and limitations under the License.
12711890bcSjc156560  *
13711890bcSjc156560  * When distributing Covered Code, include this CDDL HEADER in each
14711890bcSjc156560  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15711890bcSjc156560  * If applicable, add the following below this CDDL HEADER, with the
16711890bcSjc156560  * fields enclosed by brackets "[]" replaced with your own identifying
17711890bcSjc156560  * information: Portions Copyright [yyyy] [name of copyright owner]
18711890bcSjc156560  *
19711890bcSjc156560  * CDDL HEADER END
20711890bcSjc156560  */
21711890bcSjc156560 
22711890bcSjc156560 /*
23edabaf6fSMilan Jurik  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24711890bcSjc156560  * Use is subject to license terms.
25*33f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
26711890bcSjc156560  */
27711890bcSjc156560 
28711890bcSjc156560 #include <fcntl.h>
29711890bcSjc156560 #include <sys/types.h>
30711890bcSjc156560 #include <sys/stat.h>
31711890bcSjc156560 #include <stddef.h>
32711890bcSjc156560 #include <stdlib.h>
33711890bcSjc156560 #include <dirent.h>
34711890bcSjc156560 #include <dlfcn.h>
35711890bcSjc156560 #include <link.h>
36711890bcSjc156560 #include <strings.h>
37711890bcSjc156560 #include <stdio.h>
38711890bcSjc156560 #include <unistd.h>
39711890bcSjc156560 #include <sys/mnttab.h>
40711890bcSjc156560 #include <config_admin.h>
41711890bcSjc156560 #include <sys/param.h>
42711890bcSjc156560 #include <libintl.h>
43711890bcSjc156560 #include <libdevinfo.h>
44711890bcSjc156560 #include <raidcfg.h>
45711890bcSjc156560 #include <thread.h>
46711890bcSjc156560 #include <synch.h>
47711890bcSjc156560 
48711890bcSjc156560 #ifndef TEXT_DOMAIN
49711890bcSjc156560 #define	TEXT_DOMAIN	"SYS_TEST"
50711890bcSjc156560 #endif
51711890bcSjc156560 
52711890bcSjc156560 #define	HASH_SLOTS	16
53711890bcSjc156560 #define	HANDLER_SLOTS	256
54711890bcSjc156560 
55711890bcSjc156560 /*
56711890bcSjc156560  * Raid object status;
57711890bcSjc156560  */
58711890bcSjc156560 #define	OBJ_STATUS_CMD_CLEAN	-1
59711890bcSjc156560 #define	OBJ_STATUS_OPENED	1
60711890bcSjc156560 #define	OBJ_STATUS_SCANCOMP	1 << 1
61711890bcSjc156560 
62711890bcSjc156560 #if defined(__sparcv9)
63711890bcSjc156560 #define	SUPP_PLUGIN_DIR	"/usr/lib/raidcfg/sparcv9"
64711890bcSjc156560 #elif defined(__amd64)
65711890bcSjc156560 #define	SUPP_PLUGIN_DIR	"/usr/lib/raidcfg/amd64"
66711890bcSjc156560 #else
67711890bcSjc156560 #define	SUPP_PLUGIN_DIR	"/usr/lib/raidcfg"
68711890bcSjc156560 #endif
69711890bcSjc156560 
70711890bcSjc156560 /*
71711890bcSjc156560  * Basic types
72711890bcSjc156560  */
73711890bcSjc156560 typedef	int raid_obj_id_t;
74711890bcSjc156560 typedef	int raid_obj_status_t;
75711890bcSjc156560 
76711890bcSjc156560 /*
77711890bcSjc156560  * Data structures used for object maintennance
78711890bcSjc156560  */
79711890bcSjc156560 typedef	struct {
80711890bcSjc156560 	void *head;
81711890bcSjc156560 	void *tail;
82711890bcSjc156560 	size_t offset;	/* offset of double-linked element (raid_list_el_t) */
83711890bcSjc156560 			/* in the linked data structures (objects) */
84711890bcSjc156560 } raid_list_t;
85711890bcSjc156560 
86711890bcSjc156560 typedef	struct {
87711890bcSjc156560 	void *prev;
88711890bcSjc156560 	void *next;
89711890bcSjc156560 } raid_list_el_t;
90711890bcSjc156560 
91711890bcSjc156560 typedef	struct {
92711890bcSjc156560 	raid_obj_id_t obj_id_cnt;	/* id 0 is reserved */
93711890bcSjc156560 	size_t slots;			/* How many lists linked by *table */
94711890bcSjc156560 	raid_list_t *table;
95711890bcSjc156560 } raid_obj_tab_t;
96711890bcSjc156560 
97711890bcSjc156560 /*
98711890bcSjc156560  * Object type structure containing function pointers;
99711890bcSjc156560  */
100711890bcSjc156560 typedef	struct {
101711890bcSjc156560 	int (*compnum)(raid_obj_tab_t *, raid_obj_id_t, raid_obj_type_id_t);
102711890bcSjc156560 	int (*complist)(raid_obj_tab_t *, raid_obj_id_t, int, raid_obj_id_t *,
103711890bcSjc156560 		raid_obj_type_id_t);
104711890bcSjc156560 	int (*get_attr)(raid_obj_tab_t *, raid_obj_id_t);
105711890bcSjc156560 	int (*set_attr)(raid_obj_tab_t *, raid_obj_id_t, uint32_t, uint32_t *,
106711890bcSjc156560 		char **);
107711890bcSjc156560 	int (*act)(raid_obj_tab_t *, raid_obj_id_t, uint32_t, void *, char **);
108711890bcSjc156560 	int (*create_obj)(raid_obj_tab_t *, raid_obj_id_t, int,
109711890bcSjc156560 		raid_obj_id_t *, char **);
110711890bcSjc156560 	int (*delete_obj)(raid_obj_tab_t *, raid_obj_id_t, char **);
1115c9d25d2SYu-Bo Ryan Wang 	int (*bind_obj)(raid_obj_tab_t *, raid_obj_id_t *, char **);
1125c9d25d2SYu-Bo Ryan Wang 	int (*unbind_obj)(raid_obj_tab_t *, raid_obj_id_t *, char **);
113711890bcSjc156560 } raid_obj_op_t;
114711890bcSjc156560 
115711890bcSjc156560 /*
116711890bcSjc156560  * Common object data structure
117711890bcSjc156560  */
118711890bcSjc156560 typedef	struct {
119711890bcSjc156560 	raid_list_el_t		el;	/* double-links */
120711890bcSjc156560 
121711890bcSjc156560 	raid_obj_type_id_t	obj_type_id;
122711890bcSjc156560 	raid_obj_id_t		obj_id;
123711890bcSjc156560 	raid_obj_status_t	status;
124711890bcSjc156560 
125711890bcSjc156560 	raid_obj_id_t		container;
126711890bcSjc156560 	raid_obj_id_t		sibling;
127711890bcSjc156560 	raid_obj_id_t		component;
128711890bcSjc156560 
129711890bcSjc156560 	void			*data;	/* Pointer to attribute structure */
130711890bcSjc156560 	raid_obj_handle_t	handle;
131711890bcSjc156560 } raid_obj_t;
132711890bcSjc156560 
133711890bcSjc156560 /*
134711890bcSjc156560  * Definition about handle
135711890bcSjc156560  */
136711890bcSjc156560 typedef	struct {
137711890bcSjc156560 	uint32_t	next;
138711890bcSjc156560 	uint32_t	type;
139711890bcSjc156560 	uint32_t	controller_id;
140711890bcSjc156560 	uint32_t	array_id;
141711890bcSjc156560 	uint32_t	disk_id;
142711890bcSjc156560 	uint64_t	seq_id;
143711890bcSjc156560 	uint32_t	task_id;
144b449fa8aSyw161884 	uint32_t	prop_id;
145711890bcSjc156560 	uint32_t	fd;		/* Only for controller */
146711890bcSjc156560 	raid_lib_t	*raid_lib;	/* Only for controller */
147711890bcSjc156560 } handle_attr_t;
148711890bcSjc156560 
149711890bcSjc156560 #define	LIST_OBJ_TO_EL(list, obj)	\
150711890bcSjc156560 	((void *)((char *)(obj) + (list)->offset))
151711890bcSjc156560 #define	OBJ_TAB_SLOT(tab, id)	\
152711890bcSjc156560 	((tab)->table + ((id)%(tab)->slots))
153711890bcSjc156560 
154711890bcSjc156560 #pragma init(raidcfg_init)
155711890bcSjc156560 #pragma fini(raidcfg_fini)
156711890bcSjc156560 
157711890bcSjc156560 /*
158711890bcSjc156560  * Function prototypes
159711890bcSjc156560  */
160711890bcSjc156560 static int intcompare(const void *p1, const void *p2);
161711890bcSjc156560 static uint64_t raid_space_noalign(raid_obj_tab_t *, uint32_t, int,
162711890bcSjc156560 	raid_obj_id_t *, arraypart_attr_t *);
163711890bcSjc156560 static int raid_handle_init();
164711890bcSjc156560 static void raid_handle_fini();
165711890bcSjc156560 static raid_obj_handle_t raid_handle_new(raid_obj_type_id_t);
166711890bcSjc156560 static void raid_handle_delete(raid_obj_handle_t);
167711890bcSjc156560 static void raid_handle_delete_controller_comp(uint32_t);
168711890bcSjc156560 static raid_obj_id_t raid_handle_to_obj(raid_obj_tab_t *,
169711890bcSjc156560 	raid_obj_handle_t);
170711890bcSjc156560 static raid_obj_handle_t raid_obj_to_handle(raid_obj_tab_t *,
171711890bcSjc156560 	raid_obj_id_t);
172711890bcSjc156560 static raid_lib_t *raid_obj_get_lib(raid_obj_tab_t *, raid_obj_id_t);
173711890bcSjc156560 static int raid_obj_set_lib(raid_obj_tab_t *, raid_obj_id_t, raid_lib_t *);
174711890bcSjc156560 static int raid_obj_get_fd(raid_obj_tab_t *, raid_obj_id_t);
175711890bcSjc156560 static int raid_obj_set_fd(raid_obj_tab_t *, raid_obj_id_t, int);
176711890bcSjc156560 static int obj_scan_comp(raid_obj_tab_t *, raid_obj_id_t);
177711890bcSjc156560 static int obj_rescan(raid_obj_tab_t *);
178711890bcSjc156560 static raid_obj_id_t obj_get_comp(raid_obj_tab_t *, raid_obj_id_t,
179711890bcSjc156560 	raid_obj_type_id_t);
180711890bcSjc156560 static raid_obj_id_t obj_get_sibling(raid_obj_tab_t *, raid_obj_id_t);
181711890bcSjc156560 static int obj_get_attr(raid_obj_tab_t *, raid_obj_id_t, void **);
182711890bcSjc156560 static raid_obj_id_t obj_locate_controller(raid_obj_tab_t *, uint32_t);
183711890bcSjc156560 static raid_obj_id_t obj_locate_array(raid_obj_tab_t *, uint32_t, uint32_t);
184711890bcSjc156560 static raid_obj_id_t obj_locate_array_recur(raid_obj_tab_t *, raid_obj_id_t,
185711890bcSjc156560 	uint32_t);
186711890bcSjc156560 static raid_obj_id_t obj_locate_hsp(raid_obj_tab_t *, uint32_t,
187711890bcSjc156560 	uint32_t, uint32_t);
188711890bcSjc156560 static raid_obj_id_t obj_locate_disk(raid_obj_tab_t *, uint32_t, uint32_t);
189711890bcSjc156560 static raid_obj_id_t obj_locate_arraypart(raid_obj_tab_t *, uint32_t,
190711890bcSjc156560 	uint32_t, uint32_t);
191711890bcSjc156560 static raid_obj_id_t obj_locate_diskseg(raid_obj_tab_t *, uint32_t,
192711890bcSjc156560 	uint32_t, uint32_t);
193711890bcSjc156560 static raid_obj_id_t obj_locate_task(raid_obj_tab_t *, uint32_t, uint32_t);
194b449fa8aSyw161884 static raid_obj_id_t obj_locate_prop(raid_obj_tab_t *, uint32_t, uint32_t,
195b449fa8aSyw161884 	uint32_t);
196711890bcSjc156560 static raid_obj_id_t obj_get_controller(raid_obj_tab_t *, raid_obj_id_t);
197711890bcSjc156560 
198711890bcSjc156560 static int obj_sys_compnum(raid_obj_tab_t *, raid_obj_id_t,
199711890bcSjc156560 	raid_obj_type_id_t);
200711890bcSjc156560 static int obj_sys_complist(raid_obj_tab_t *, raid_obj_id_t, int,
201711890bcSjc156560 	raid_obj_id_t *, raid_obj_type_id_t);
202711890bcSjc156560 static int obj_controller_compnum(raid_obj_tab_t *, raid_obj_id_t,
203711890bcSjc156560 	raid_obj_type_id_t);
204711890bcSjc156560 static int obj_controller_complist(raid_obj_tab_t *, raid_obj_id_t, int,
205711890bcSjc156560 	raid_obj_id_t *, raid_obj_type_id_t);
206711890bcSjc156560 static int obj_controller_get_attr(raid_obj_tab_t *, raid_obj_id_t);
207711890bcSjc156560 static int obj_controller_act(raid_obj_tab_t *, raid_obj_id_t,
208711890bcSjc156560 	uint32_t, void *, char **);
209711890bcSjc156560 static int obj_array_compnum(raid_obj_tab_t *, raid_obj_id_t,
210711890bcSjc156560 	raid_obj_type_id_t);
211711890bcSjc156560 static int obj_array_complist(raid_obj_tab_t *, raid_obj_id_t, int,
212711890bcSjc156560 	raid_obj_id_t *, raid_obj_type_id_t);
213711890bcSjc156560 static int obj_array_get_attr(raid_obj_tab_t *, raid_obj_id_t);
214711890bcSjc156560 static int obj_array_set_attr(raid_obj_tab_t *, raid_obj_id_t,
215711890bcSjc156560 	uint32_t, uint32_t *, char **);
216711890bcSjc156560 static int obj_disk_compnum(raid_obj_tab_t *, raid_obj_id_t,
217711890bcSjc156560 	raid_obj_type_id_t);
218711890bcSjc156560 static int obj_disk_complist(raid_obj_tab_t *, raid_obj_id_t, int,
219711890bcSjc156560 	raid_obj_id_t *, raid_obj_type_id_t);
220711890bcSjc156560 static int obj_disk_get_attr(raid_obj_tab_t *, raid_obj_id_t);
221711890bcSjc156560 static int obj_hsp_get_attr(raid_obj_tab_t *, raid_obj_id_t);
222711890bcSjc156560 static int obj_arraypart_get_attr(raid_obj_tab_t *, raid_obj_id_t);
223711890bcSjc156560 static int obj_diskseg_get_attr(raid_obj_tab_t *, raid_obj_id_t);
224711890bcSjc156560 static int obj_task_get_attr(raid_obj_tab_t *, raid_obj_id_t);
225b449fa8aSyw161884 static int obj_prop_get_attr(raid_obj_tab_t *, raid_obj_id_t);
226711890bcSjc156560 static int obj_array_create(raid_obj_tab_t *, raid_obj_id_t, int,
227711890bcSjc156560 	raid_obj_id_t *, char **);
228711890bcSjc156560 static int obj_array_delete(raid_obj_tab_t *, raid_obj_id_t, char **);
2295c9d25d2SYu-Bo Ryan Wang static int obj_hsp_bind(raid_obj_tab_t *, raid_obj_id_t *, char **);
2305c9d25d2SYu-Bo Ryan Wang static int obj_hsp_unbind(raid_obj_tab_t *, raid_obj_id_t *, char **);
231711890bcSjc156560 
232711890bcSjc156560 static int raid_obj_create_system_obj(raid_obj_tab_t *);
233711890bcSjc156560 static raid_obj_id_t raid_obj_id_new(raid_obj_tab_t *);
234711890bcSjc156560 static void *raid_obj_attr_new(raid_obj_type_id_t);
235711890bcSjc156560 static raid_obj_id_t raid_obj_create(raid_obj_tab_t *, raid_obj_type_id_t);
236711890bcSjc156560 static int raid_obj_delete(raid_obj_tab_t *, raid_obj_id_t);
237711890bcSjc156560 static int raid_obj_add_org(raid_obj_tab_t *, raid_obj_id_t, raid_obj_id_t);
238711890bcSjc156560 static raid_obj_type_id_t raid_obj_get_type(raid_obj_tab_t *, raid_obj_id_t);
239711890bcSjc156560 static int raid_obj_set_type(raid_obj_tab_t *, raid_obj_id_t,
240711890bcSjc156560 	raid_obj_type_id_t);
241711890bcSjc156560 static raid_obj_status_t raid_obj_get_status(raid_obj_tab_t *, raid_obj_id_t);
242711890bcSjc156560 static int raid_obj_set_status(raid_obj_tab_t *, raid_obj_id_t,
243711890bcSjc156560 	raid_obj_status_t);
244711890bcSjc156560 static int raid_obj_clear_status(raid_obj_tab_t *, raid_obj_id_t,
245711890bcSjc156560 	raid_obj_status_t);
246711890bcSjc156560 static raid_obj_id_t raid_obj_get_container(raid_obj_tab_t *, raid_obj_id_t);
247711890bcSjc156560 static int raid_obj_set_container(raid_obj_tab_t *, raid_obj_id_t,
248711890bcSjc156560 	raid_obj_id_t);
249711890bcSjc156560 static raid_obj_id_t raid_obj_get_comp(raid_obj_tab_t *, raid_obj_id_t);
250711890bcSjc156560 static int raid_obj_set_comp(raid_obj_tab_t *, raid_obj_id_t, raid_obj_id_t);
251711890bcSjc156560 static raid_obj_id_t raid_obj_get_sibling(raid_obj_tab_t *, raid_obj_id_t);
252711890bcSjc156560 static int raid_obj_set_sibling(raid_obj_tab_t *, raid_obj_id_t,
253711890bcSjc156560 	raid_obj_id_t);
254711890bcSjc156560 static void *raid_obj_get_data_ptr(raid_obj_tab_t *, raid_obj_id_t);
255711890bcSjc156560 static int raid_obj_set_data_ptr(raid_obj_tab_t *, raid_obj_id_t, void *);
256711890bcSjc156560 static raid_obj_handle_t raid_obj_get_handle(raid_obj_tab_t *,
257711890bcSjc156560 	raid_obj_id_t);
258711890bcSjc156560 static int raid_obj_set_handle(raid_obj_tab_t *, raid_obj_id_t,
259711890bcSjc156560 	raid_obj_handle_t);
260711890bcSjc156560 
261711890bcSjc156560 static void raid_list_create(raid_list_t *, size_t);
262711890bcSjc156560 static void *raid_list_head(raid_list_t *);
263711890bcSjc156560 static void *raid_list_next(raid_list_t *, void *);
264711890bcSjc156560 static void raid_list_insert_tail(raid_list_t *, void *);
265711890bcSjc156560 static void raid_list_remove(raid_list_t *, void *);
266711890bcSjc156560 static void *raid_list_remove_head(raid_list_t *);
267711890bcSjc156560 static void *raid_list_find(raid_list_t *, raid_obj_id_t);
268711890bcSjc156560 static int raid_obj_tab_create(raid_obj_tab_t *, size_t);
269711890bcSjc156560 static void raid_obj_tab_destroy(raid_obj_tab_t *);
270711890bcSjc156560 static int raid_obj_tab_insert(raid_obj_tab_t *, raid_obj_id_t, void *);
271711890bcSjc156560 static void *raid_obj_tab_remove(raid_obj_tab_t *, raid_obj_id_t);
272711890bcSjc156560 static void *raid_obj_tab_find(raid_obj_tab_t *, raid_obj_id_t);
273711890bcSjc156560 static void raid_list_destroy(raid_list_t *);
274711890bcSjc156560 
275711890bcSjc156560 static int controller_id_to_path(uint32_t, char *);
276711890bcSjc156560 static char *controller_id_to_driver_name(uint32_t);
277711890bcSjc156560 static void raid_plugin_init();
278711890bcSjc156560 static raid_lib_t *raid_plugin_load(char *);
279711890bcSjc156560 static raid_lib_t *raid_find_lib(raid_obj_tab_t *, raid_obj_id_t);
280711890bcSjc156560 
281711890bcSjc156560 /* Global object table */
282711890bcSjc156560 static raid_obj_tab_t raid_tab_sys = {0, 0, NULL};
283711890bcSjc156560 
284711890bcSjc156560 /* Plug-in modules maintenance data structures */
285711890bcSjc156560 static raid_lib_t *raid_lib_sys = NULL;
286711890bcSjc156560 
287711890bcSjc156560 /* Handle table definition */
288711890bcSjc156560 static struct {
289711890bcSjc156560 	int		handle_num;
290711890bcSjc156560 	int		used;
291711890bcSjc156560 	int		unused;
292711890bcSjc156560 	handle_attr_t	*handles;
293711890bcSjc156560 } raid_handle_sys = {0, 0, 0, NULL};
294711890bcSjc156560 
295711890bcSjc156560 /*
296711890bcSjc156560  * RAID object method table definition
297711890bcSjc156560  */
298711890bcSjc156560 static raid_obj_op_t raid_obj_op_sys[OBJ_TYPE_ALL] = {
299711890bcSjc156560 	{obj_sys_compnum, obj_sys_complist, NULL, NULL, NULL,
300711890bcSjc156560 		NULL, NULL, NULL, NULL},	/* system object methods */
301711890bcSjc156560 	{obj_controller_compnum, obj_controller_complist,
302711890bcSjc156560 		obj_controller_get_attr, NULL, obj_controller_act,
303711890bcSjc156560 		NULL, NULL, NULL, NULL},	/* controller object methods */
304711890bcSjc156560 	{obj_array_compnum, obj_array_complist, obj_array_get_attr,
305711890bcSjc156560 		obj_array_set_attr, NULL, obj_array_create,
306711890bcSjc156560 		obj_array_delete, NULL, NULL},	/* array object methods */
307711890bcSjc156560 	{obj_disk_compnum, obj_disk_complist, obj_disk_get_attr, NULL,
308711890bcSjc156560 		NULL, NULL, NULL, NULL, NULL},	/* disk object methods */
309711890bcSjc156560 	{NULL, NULL, obj_hsp_get_attr, NULL, NULL, NULL, NULL, obj_hsp_bind,
310711890bcSjc156560 		obj_hsp_unbind},		/* hsp object methods */
311711890bcSjc156560 	{NULL, NULL, obj_arraypart_get_attr, NULL, NULL, NULL, NULL,
312711890bcSjc156560 		NULL, NULL},			/* array part object methods */
313711890bcSjc156560 	{NULL, NULL, obj_diskseg_get_attr, NULL, NULL, NULL, NULL, NULL, NULL},
314711890bcSjc156560 	{NULL, NULL, obj_task_get_attr, NULL, NULL, NULL, NULL,
315b449fa8aSyw161884 		NULL, NULL},			/* disk seg object methods */
316b449fa8aSyw161884 	{NULL, NULL, obj_prop_get_attr, NULL, NULL, NULL, NULL,
317b449fa8aSyw161884 		NULL, NULL}			/* property object methods */
318711890bcSjc156560 };
319711890bcSjc156560 
320711890bcSjc156560 /*
321711890bcSjc156560  * Mutex for multithread safe
322711890bcSjc156560  */
323711890bcSjc156560 static mutex_t raidcfg_mp;
324711890bcSjc156560 
325711890bcSjc156560 /*
326711890bcSjc156560  * RaidCfg library APIs
327711890bcSjc156560  */
328711890bcSjc156560 const char *
329711890bcSjc156560 raidcfg_errstr(int err_code)
330711890bcSjc156560 {
331711890bcSjc156560 	char *ret_val;
332711890bcSjc156560 
333711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
334711890bcSjc156560 	switch (err_code) {
335711890bcSjc156560 	case	SUCCESS:
336711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Operation succeeded.\n");
337711890bcSjc156560 		break;
338711890bcSjc156560 	case	STD_IOCTL:
339711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
340711890bcSjc156560 		    "Request standard IOCTL service.\n");
341711890bcSjc156560 		break;
342711890bcSjc156560 	case	ERR_DRIVER_NOT_FOUND:
343711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
344711890bcSjc156560 		    "Controller device can not be found.\n");
345711890bcSjc156560 		break;
346711890bcSjc156560 	case	ERR_DRIVER_OPEN:
347711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Can not open controller.\n");
348711890bcSjc156560 		break;
349711890bcSjc156560 	case	ERR_DRIVER_LOCK:
350711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Controller is locked.\n");
351711890bcSjc156560 		break;
352711890bcSjc156560 	case	ERR_DRIVER_CLOSED:
353711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Controller is not opened.\n");
354711890bcSjc156560 		break;
355711890bcSjc156560 	case	ERR_DRIVER_ACROSS:
356711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
357711890bcSjc156560 		    "Operation across multiple controllers.\n");
358711890bcSjc156560 		break;
359711890bcSjc156560 	case	ERR_ARRAY_LEVEL:
360711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
361711890bcSjc156560 		    "Operation not support with volume of this level.\n");
362711890bcSjc156560 		break;
363711890bcSjc156560 	case	ERR_ARRAY_SIZE:
364711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
365711890bcSjc156560 		    "Capacity of array out of range.\n");
366711890bcSjc156560 		break;
367711890bcSjc156560 	case	ERR_ARRAY_STRIPE_SIZE:
368711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Illegal stripe size.\n");
369711890bcSjc156560 		break;
370711890bcSjc156560 	case	ERR_ARRAY_CACHE_POLICY:
371711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
372711890bcSjc156560 		    "Illegal cache-write policy.\n");
373711890bcSjc156560 		break;
374711890bcSjc156560 	case	ERR_ARRAY_IN_USE:
375474adcbbSyw161884 		ret_val = dgettext(TEXT_DOMAIN, "Array or disk in use.\n");
376711890bcSjc156560 		break;
377711890bcSjc156560 	case	ERR_ARRAY_TASK:
378711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Array has background task.\n");
379711890bcSjc156560 		break;
380711890bcSjc156560 	case	ERR_ARRAY_CONFIG:
381711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
382711890bcSjc156560 		    "Configuration over device node failed.\n");
383711890bcSjc156560 		break;
384711890bcSjc156560 	case	ERR_ARRAY_DISKNUM:
385711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Incorrect number of disks.\n");
386711890bcSjc156560 		break;
387711890bcSjc156560 	case	ERR_ARRAY_LAYOUT:
388711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Illegal array layout.\n");
389711890bcSjc156560 		break;
390711890bcSjc156560 	case	ERR_ARRAY_AMOUNT:
391711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Too many arrays.\n");
392711890bcSjc156560 		break;
393711890bcSjc156560 	case	ERR_DISK_STATE:
394711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
395711890bcSjc156560 		    "Incorrect disk status for current operation.\n");
396711890bcSjc156560 		break;
397711890bcSjc156560 	case	ERR_DISK_SPACE:
398711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "No enough disk space.\n");
399711890bcSjc156560 		break;
400711890bcSjc156560 	case	ERR_DISK_SEG_AMOUNT:
401711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Too many disk segments.\n");
402711890bcSjc156560 		break;
403711890bcSjc156560 	case	ERR_DISK_NOT_EMPTY:
404711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Disk has occupied space.\n");
405711890bcSjc156560 		break;
406711890bcSjc156560 	case	ERR_DISK_TASK:
407711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Disk has background task.\n");
408711890bcSjc156560 		break;
409711890bcSjc156560 	case	ERR_TASK_STATE:
410711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
411711890bcSjc156560 		    "Incorrect task state for current operation.\n");
412711890bcSjc156560 		break;
413711890bcSjc156560 	case	ERR_OP_ILLEGAL:
414711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Illegal operation.\n");
415711890bcSjc156560 		break;
416711890bcSjc156560 	case	ERR_OP_NO_IMPL:
417711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
418711890bcSjc156560 		    "Operation is not implemented.\n");
419711890bcSjc156560 		break;
420711890bcSjc156560 	case	ERR_OP_FAILED:
421474adcbbSyw161884 		ret_val = dgettext(TEXT_DOMAIN, "Operation failed.\n");
422711890bcSjc156560 		break;
423711890bcSjc156560 	case	ERR_DEVICE_NOENT:
424711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Device not found.\n");
425711890bcSjc156560 		break;
426711890bcSjc156560 	case	ERR_DEVICE_TYPE:
427711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Illegal type of device.\n");
428711890bcSjc156560 		break;
429711890bcSjc156560 	case	ERR_DEVICE_DUP:
430711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Device record duplicated.\n");
431711890bcSjc156560 		break;
432711890bcSjc156560 	case	ERR_DEVICE_OVERFLOW:
433711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Too many devices.\n");
434711890bcSjc156560 		break;
435711890bcSjc156560 	case	ERR_DEVICE_UNCLEAN:
436711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Device pool is not clean.\n");
437711890bcSjc156560 		break;
438711890bcSjc156560 	case	ERR_DEVICE_INVALID:
439711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Device record is invalid.\n");
440711890bcSjc156560 		break;
441711890bcSjc156560 	case	ERR_NOMEM:
442711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
443711890bcSjc156560 		    "Can not allocate more memory space.\n");
444711890bcSjc156560 		break;
445711890bcSjc156560 	case	ERR_PRIV:
446711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "No privilege.\n");
447711890bcSjc156560 		break;
448711890bcSjc156560 	default:
449711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Undefined error.\n");
450711890bcSjc156560 	}
451711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
452711890bcSjc156560 
453711890bcSjc156560 	return (ret_val);
454711890bcSjc156560 }
455711890bcSjc156560 
456711890bcSjc156560 int
457711890bcSjc156560 raidcfg_get_controller(uint32_t controller_id)
458711890bcSjc156560 {
459711890bcSjc156560 	raid_obj_id_t obj_id;
460711890bcSjc156560 	int ret_val;
461711890bcSjc156560 
462711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
463711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
464711890bcSjc156560 	obj_id = obj_locate_controller(&raid_tab_sys, controller_id);
465711890bcSjc156560 	if (obj_id < OBJ_NONE) {
466711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
467711890bcSjc156560 		return (obj_id);
468711890bcSjc156560 	}
469711890bcSjc156560 
470711890bcSjc156560 	if (obj_id == OBJ_NONE) {
471711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
472711890bcSjc156560 		return (ERR_DEVICE_NOENT);
473711890bcSjc156560 	}
474711890bcSjc156560 	ret_val = raid_obj_to_handle(&raid_tab_sys, obj_id);
475711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
476711890bcSjc156560 
477711890bcSjc156560 	return (ret_val);
478711890bcSjc156560 }
479711890bcSjc156560 
480711890bcSjc156560 int
481711890bcSjc156560 raidcfg_get_array(int controller_handle, uint64_t target_id, uint64_t lun)
482711890bcSjc156560 {
483711890bcSjc156560 	raid_obj_id_t obj_id;
484711890bcSjc156560 	raidcfg_array_t *attr;
485711890bcSjc156560 	int ret_val;
486711890bcSjc156560 
487711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
488711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
489711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, controller_handle);
490711890bcSjc156560 	if (obj_id < OBJ_NONE) {
491711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
492711890bcSjc156560 		return (obj_id);
493711890bcSjc156560 	}
494711890bcSjc156560 
495711890bcSjc156560 	obj_id = obj_get_comp(&raid_tab_sys, obj_id, OBJ_TYPE_ARRAY);
496711890bcSjc156560 
497711890bcSjc156560 	while (obj_id > OBJ_NONE) {
498711890bcSjc156560 		(void) obj_get_attr(&raid_tab_sys, obj_id, (void **)(&attr));
499711890bcSjc156560 		if (attr->tag.idl.target_id == target_id &&
500711890bcSjc156560 		    attr->tag.idl.lun == lun)
501711890bcSjc156560 			break;
502711890bcSjc156560 
503711890bcSjc156560 		obj_id = obj_get_sibling(&raid_tab_sys, obj_id);
504711890bcSjc156560 	}
505711890bcSjc156560 
506711890bcSjc156560 	if (obj_id < OBJ_NONE) {
507711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
508711890bcSjc156560 		return (obj_id);
509711890bcSjc156560 	}
510711890bcSjc156560 	if (obj_id == OBJ_NONE) {
511711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
512711890bcSjc156560 		return (ERR_DEVICE_NOENT);
513711890bcSjc156560 	}
514711890bcSjc156560 	ret_val = raid_obj_to_handle(&raid_tab_sys, obj_id);
515711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
516711890bcSjc156560 
517711890bcSjc156560 	return (ret_val);
518711890bcSjc156560 }
519711890bcSjc156560 
520711890bcSjc156560 int
521711890bcSjc156560 raidcfg_get_disk(int controller_handle, disk_tag_t tag)
522711890bcSjc156560 {
523711890bcSjc156560 	raid_obj_id_t obj_id;
524711890bcSjc156560 	raidcfg_disk_t *attr;
525711890bcSjc156560 	int ret_val;
526711890bcSjc156560 
527711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
528711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
529711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, controller_handle);
530711890bcSjc156560 	if (obj_id < OBJ_NONE) {
531711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
532711890bcSjc156560 		return (obj_id);
533711890bcSjc156560 	}
534711890bcSjc156560 
535711890bcSjc156560 	obj_id = obj_get_comp(&raid_tab_sys, obj_id, OBJ_TYPE_DISK);
536711890bcSjc156560 
537711890bcSjc156560 	while (obj_id > OBJ_NONE) {
538711890bcSjc156560 		(void) obj_get_attr(&raid_tab_sys, obj_id, (void **)(&attr));
539711890bcSjc156560 		if (attr->tag.cidl.bus == tag.cidl.bus &&
540711890bcSjc156560 		    attr->tag.cidl.target_id == tag.cidl.target_id &&
541711890bcSjc156560 		    attr->tag.cidl.lun == tag.cidl.lun)
542711890bcSjc156560 			break;
543711890bcSjc156560 
544711890bcSjc156560 		obj_id = obj_get_sibling(&raid_tab_sys, obj_id);
545711890bcSjc156560 	}
546711890bcSjc156560 
547711890bcSjc156560 	if (obj_id < OBJ_NONE) {
548711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
549711890bcSjc156560 		return (obj_id);
550711890bcSjc156560 	}
551711890bcSjc156560 	if (obj_id == OBJ_NONE) {
552711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
553711890bcSjc156560 		return (ERR_DEVICE_NOENT);
554711890bcSjc156560 	}
555711890bcSjc156560 	ret_val = raid_obj_to_handle(&raid_tab_sys, obj_id);
556711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
557711890bcSjc156560 
558711890bcSjc156560 	return (ret_val);
559711890bcSjc156560 }
560711890bcSjc156560 
561711890bcSjc156560 int
562711890bcSjc156560 raidcfg_open_controller(int handle, char **plugin_err_str)
563711890bcSjc156560 {
564711890bcSjc156560 	raid_obj_id_t obj_id;
565711890bcSjc156560 	int ret;
566711890bcSjc156560 
567711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
568711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
569711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
570711890bcSjc156560 	if (obj_id < OBJ_NONE) {
571711890bcSjc156560 		raid_handle_delete(handle);
572711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
573711890bcSjc156560 		return (ERR_DEVICE_NOENT);
574711890bcSjc156560 	}
575711890bcSjc156560 
576711890bcSjc156560 	ret = obj_controller_act(&raid_tab_sys, obj_id,
577711890bcSjc156560 	    ACT_CONTROLLER_OPEN, NULL, plugin_err_str);
578711890bcSjc156560 	if (ret < SUCCESS) {
579711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
580711890bcSjc156560 		return (ret);
581711890bcSjc156560 	}
582711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
583711890bcSjc156560 
584711890bcSjc156560 	return (SUCCESS);
585711890bcSjc156560 }
586711890bcSjc156560 
587711890bcSjc156560 int
588711890bcSjc156560 raidcfg_close_controller(int handle, char **plugin_err_str)
589711890bcSjc156560 {
590711890bcSjc156560 	raid_obj_id_t obj_id;
591711890bcSjc156560 	int ret;
592711890bcSjc156560 
593711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
594711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
595711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
596711890bcSjc156560 	if (obj_id < OBJ_NONE) {
597711890bcSjc156560 		raid_handle_delete(handle);
598711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
599711890bcSjc156560 		return (ERR_DEVICE_NOENT);
600711890bcSjc156560 	}
601711890bcSjc156560 
602711890bcSjc156560 	ret = obj_controller_act(&raid_tab_sys, obj_id,
603711890bcSjc156560 	    ACT_CONTROLLER_CLOSE, NULL, plugin_err_str);
604711890bcSjc156560 	if (ret < SUCCESS) {
605711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
606711890bcSjc156560 		return (ret);
607711890bcSjc156560 	}
608711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
609711890bcSjc156560 
610711890bcSjc156560 	return (SUCCESS);
611711890bcSjc156560 }
612711890bcSjc156560 
613711890bcSjc156560 int
614711890bcSjc156560 raidcfg_get_type(int handle)
615711890bcSjc156560 {
616711890bcSjc156560 	raid_obj_id_t obj_id;
617711890bcSjc156560 	int ret_val;
618711890bcSjc156560 
619711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
620711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
621711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
622711890bcSjc156560 	if (obj_id < OBJ_NONE) {
623711890bcSjc156560 		raid_handle_delete(handle);
624711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
625711890bcSjc156560 		return (ERR_DEVICE_NOENT);
626711890bcSjc156560 	}
627711890bcSjc156560 	ret_val = raid_obj_get_type(&raid_tab_sys, obj_id);
628711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
629711890bcSjc156560 
630711890bcSjc156560 	return (ret_val);
631711890bcSjc156560 }
632711890bcSjc156560 
633711890bcSjc156560 int
634711890bcSjc156560 raidcfg_get_attr(int handle, void *attr)
635711890bcSjc156560 {
636711890bcSjc156560 	raid_obj_id_t obj_id;
637711890bcSjc156560 	raid_obj_type_id_t type;
638711890bcSjc156560 	void *data;
639711890bcSjc156560 	int ret, size;
640711890bcSjc156560 
641711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
642711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
643711890bcSjc156560 	if (attr == NULL) {
644711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
645711890bcSjc156560 		return (ERR_DEVICE_INVALID);
646711890bcSjc156560 	}
647711890bcSjc156560 
648711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
649711890bcSjc156560 	if (obj_id < OBJ_NONE) {
650711890bcSjc156560 		raid_handle_delete(handle);
651711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
652711890bcSjc156560 		return (ERR_DEVICE_NOENT);
653711890bcSjc156560 	}
654711890bcSjc156560 
655711890bcSjc156560 	type = raid_obj_get_type(&raid_tab_sys, obj_id);
656711890bcSjc156560 	ret = obj_get_attr(&raid_tab_sys, obj_id, &data);
657711890bcSjc156560 	if (ret < SUCCESS) {
658711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
659711890bcSjc156560 		return (ret);
660711890bcSjc156560 	}
661711890bcSjc156560 
662711890bcSjc156560 	switch (type) {
663711890bcSjc156560 	case	OBJ_TYPE_CONTROLLER:
664711890bcSjc156560 		size = sizeof (controller_attr_t);
665711890bcSjc156560 		break;
666711890bcSjc156560 	case	OBJ_TYPE_ARRAY:
667711890bcSjc156560 		size = sizeof (array_attr_t);
668711890bcSjc156560 		break;
669711890bcSjc156560 	case	OBJ_TYPE_HSP:
670711890bcSjc156560 		{
671711890bcSjc156560 			raidcfg_hsp_t *dst = attr;
672711890bcSjc156560 			hsp_attr_t *src = data;
673711890bcSjc156560 			controller_attr_t *ctlr_attr;
674711890bcSjc156560 			array_attr_t *array_attr;
675711890bcSjc156560 
676711890bcSjc156560 			dst->associated_id = src->associated_id;
677711890bcSjc156560 			dst->type = src->type;
678711890bcSjc156560 
679711890bcSjc156560 			obj_id = obj_get_controller(&raid_tab_sys, obj_id);
680711890bcSjc156560 			ret = obj_get_attr(&raid_tab_sys, obj_id,
681711890bcSjc156560 			    (void **)(&ctlr_attr));
682711890bcSjc156560 			if (ret < SUCCESS) {
683711890bcSjc156560 				(void) mutex_unlock(&raidcfg_mp);
684711890bcSjc156560 				return (ret);
685711890bcSjc156560 			}
686711890bcSjc156560 
687711890bcSjc156560 			if (src->type == HSP_TYPE_LOCAL) {
688711890bcSjc156560 				obj_id = obj_locate_array(&raid_tab_sys,
689711890bcSjc156560 				    ctlr_attr->controller_id,
690711890bcSjc156560 				    src->associated_id);
691711890bcSjc156560 				ret = obj_get_attr(&raid_tab_sys, obj_id,
692711890bcSjc156560 				    (void **)(&array_attr));
693711890bcSjc156560 				if (ret < SUCCESS) {
694711890bcSjc156560 					(void) mutex_unlock(&raidcfg_mp);
695711890bcSjc156560 					return (ret);
696711890bcSjc156560 				}
697711890bcSjc156560 
698711890bcSjc156560 				dst->tag.idl.target_id =
699711890bcSjc156560 				    array_attr->tag.idl.target_id;
700711890bcSjc156560 				dst->tag.idl.lun = array_attr->tag.idl.lun;
701711890bcSjc156560 			}
702711890bcSjc156560 		}
703711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
704711890bcSjc156560 		return (SUCCESS);
705711890bcSjc156560 	case	OBJ_TYPE_DISK:
706711890bcSjc156560 		size = sizeof (disk_attr_t);
707711890bcSjc156560 		break;
708711890bcSjc156560 	case	OBJ_TYPE_ARRAY_PART:
709711890bcSjc156560 		{
710711890bcSjc156560 			raidcfg_arraypart_t *dst = attr;
711711890bcSjc156560 			arraypart_attr_t *src = data;
712711890bcSjc156560 			controller_attr_t *ctlr_attr;
713711890bcSjc156560 			disk_attr_t *disk_attr;
714711890bcSjc156560 
715711890bcSjc156560 			dst->disk_id = src->disk_id;
716711890bcSjc156560 			dst->offset = src->offset;
717711890bcSjc156560 			dst->size = src->size;
718711890bcSjc156560 			dst->state = src->state;
719711890bcSjc156560 
720711890bcSjc156560 			obj_id = obj_get_controller(&raid_tab_sys, obj_id);
721711890bcSjc156560 			ret = obj_get_attr(&raid_tab_sys, obj_id,
722711890bcSjc156560 			    (void **)(&ctlr_attr));
723711890bcSjc156560 			if (ret < SUCCESS) {
724711890bcSjc156560 				(void) mutex_unlock(&raidcfg_mp);
725711890bcSjc156560 				return (ret);
726711890bcSjc156560 			}
727711890bcSjc156560 
728711890bcSjc156560 			obj_id = obj_locate_disk(&raid_tab_sys,
729711890bcSjc156560 			    ctlr_attr->controller_id, src->disk_id);
730711890bcSjc156560 			if (obj_id <= OBJ_NONE) {
731711890bcSjc156560 				dst->tag.cidl.bus = (uint64_t)OBJ_ATTR_NONE;
732711890bcSjc156560 				dst->tag.cidl.target_id =
733711890bcSjc156560 				    (uint64_t)OBJ_ATTR_NONE;
734711890bcSjc156560 				dst->tag.cidl.lun = (uint64_t)OBJ_ATTR_NONE;
735711890bcSjc156560 				(void) mutex_unlock(&raidcfg_mp);
736711890bcSjc156560 				return (SUCCESS);
737711890bcSjc156560 			}
738711890bcSjc156560 
739711890bcSjc156560 			ret = obj_get_attr(&raid_tab_sys, obj_id,
740711890bcSjc156560 			    (void **)(&disk_attr));
741711890bcSjc156560 			if (ret < SUCCESS) {
742711890bcSjc156560 				(void) mutex_unlock(&raidcfg_mp);
743711890bcSjc156560 				return (ret);
744711890bcSjc156560 			}
745711890bcSjc156560 
746711890bcSjc156560 			dst->tag.cidl.bus = disk_attr->tag.cidl.bus;
747711890bcSjc156560 			dst->tag.cidl.target_id = disk_attr->tag.cidl.target_id;
748711890bcSjc156560 			dst->tag.cidl.lun = disk_attr->tag.cidl.lun;
749711890bcSjc156560 		}
750711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
751711890bcSjc156560 		return (SUCCESS);
752711890bcSjc156560 	case	OBJ_TYPE_DISK_SEG:
753711890bcSjc156560 		size = sizeof (diskseg_attr_t);
754711890bcSjc156560 		break;
755711890bcSjc156560 	case	OBJ_TYPE_TASK:
756711890bcSjc156560 		size = sizeof (task_attr_t);
757711890bcSjc156560 		break;
758b449fa8aSyw161884 	case	OBJ_TYPE_PROP:
759b449fa8aSyw161884 		{
760b449fa8aSyw161884 			property_attr_t *src = data, *dst = attr;
761b449fa8aSyw161884 
762b449fa8aSyw161884 			dst->prop_id = src->prop_id;
763b449fa8aSyw161884 			dst->prop_type = src->prop_type;
764b449fa8aSyw161884 			if (dst->prop_size == 0) {
765b449fa8aSyw161884 				dst->prop_size = src->prop_size;
766b449fa8aSyw161884 				(void) mutex_unlock(&raidcfg_mp);
767b449fa8aSyw161884 				return (SUCCESS);
768b449fa8aSyw161884 			}
769b449fa8aSyw161884 
770b449fa8aSyw161884 			if (dst->prop_size < src->prop_size)
771b449fa8aSyw161884 				size = dst->prop_size;
772b449fa8aSyw161884 			else
773b449fa8aSyw161884 				size = src->prop_size;
774b449fa8aSyw161884 
775b449fa8aSyw161884 			(void) memcpy(dst->prop, src->prop, size);
776b449fa8aSyw161884 			(void) mutex_unlock(&raidcfg_mp);
777b449fa8aSyw161884 			return (SUCCESS);
778b449fa8aSyw161884 		}
779711890bcSjc156560 	default:
780711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
781711890bcSjc156560 		return (ERR_DEVICE_TYPE);
782711890bcSjc156560 	}
783711890bcSjc156560 
784711890bcSjc156560 	(void) memcpy(attr, data, size);
785711890bcSjc156560 
786711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
787711890bcSjc156560 	return (ret);
788711890bcSjc156560 }
789711890bcSjc156560 
790711890bcSjc156560 int
791711890bcSjc156560 raidcfg_get_container(int handle)
792711890bcSjc156560 {
793711890bcSjc156560 	raid_obj_id_t obj_id;
794711890bcSjc156560 	int ret_val;
795711890bcSjc156560 
796711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
797711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
798711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
799711890bcSjc156560 	if (obj_id < OBJ_NONE) {
800711890bcSjc156560 		raid_handle_delete(handle);
801711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
802711890bcSjc156560 		return (ERR_DEVICE_NOENT);
803711890bcSjc156560 	}
804711890bcSjc156560 
805711890bcSjc156560 	obj_id = raid_obj_get_container(&raid_tab_sys, obj_id);
806711890bcSjc156560 	if (obj_id < OBJ_NONE) {
807711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
808711890bcSjc156560 		return (obj_id);
809711890bcSjc156560 	}
810711890bcSjc156560 	ret_val = raid_obj_to_handle(&raid_tab_sys, obj_id);
811711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
812711890bcSjc156560 
813711890bcSjc156560 	return (ret_val);
814711890bcSjc156560 }
815711890bcSjc156560 
816711890bcSjc156560 int
817711890bcSjc156560 raidcfg_list_head(int handle, raid_obj_type_id_t type)
818711890bcSjc156560 {
819711890bcSjc156560 	raid_obj_id_t obj_id;
820711890bcSjc156560 	int ret_val;
821711890bcSjc156560 
822711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
823711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
824711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
825711890bcSjc156560 	if (obj_id < OBJ_NONE) {
826711890bcSjc156560 		raid_handle_delete(handle);
827711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
828711890bcSjc156560 		return (ERR_DEVICE_NOENT);
829711890bcSjc156560 	}
830711890bcSjc156560 
831711890bcSjc156560 	obj_id = obj_get_comp(&raid_tab_sys, obj_id, type);
832711890bcSjc156560 	if (obj_id < OBJ_NONE) {
833711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
834711890bcSjc156560 		return (obj_id);
835711890bcSjc156560 	}
836711890bcSjc156560 	ret_val = raid_obj_to_handle(&raid_tab_sys, obj_id);
837711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
838711890bcSjc156560 
839711890bcSjc156560 	return (ret_val);
840711890bcSjc156560 }
841711890bcSjc156560 
842711890bcSjc156560 int
843711890bcSjc156560 raidcfg_list_next(int handle)
844711890bcSjc156560 {
845711890bcSjc156560 	raid_obj_id_t obj_id;
846711890bcSjc156560 	int ret_val;
847711890bcSjc156560 
848711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
849711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
850711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
851711890bcSjc156560 	if (obj_id < OBJ_NONE) {
852711890bcSjc156560 		raid_handle_delete(handle);
853711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
854711890bcSjc156560 		return (ERR_DEVICE_NOENT);
855711890bcSjc156560 	}
856711890bcSjc156560 
857711890bcSjc156560 	obj_id = obj_get_sibling(&raid_tab_sys, obj_id);
858711890bcSjc156560 	if (obj_id < OBJ_NONE) {
859711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
860711890bcSjc156560 		return (obj_id);
861711890bcSjc156560 	}
862711890bcSjc156560 	ret_val = raid_obj_to_handle(&raid_tab_sys, obj_id);
863711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
864711890bcSjc156560 
865711890bcSjc156560 	return (ret_val);
866711890bcSjc156560 }
867711890bcSjc156560 
868711890bcSjc156560 int
869711890bcSjc156560 raidcfg_set_attr(int handle, uint32_t set_cmd, void *value,
870711890bcSjc156560 	char **plugin_err_str)
871711890bcSjc156560 {
872711890bcSjc156560 	raid_obj_id_t obj_id;
873711890bcSjc156560 	raid_obj_type_id_t type;
874711890bcSjc156560 	int ret;
875711890bcSjc156560 
876711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
877711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
878711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
879711890bcSjc156560 	if (obj_id < OBJ_NONE) {
880711890bcSjc156560 		raid_handle_delete(handle);
881711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
882711890bcSjc156560 		return (ERR_DEVICE_NOENT);
883711890bcSjc156560 	}
884711890bcSjc156560 
885711890bcSjc156560 	type = raid_obj_get_type(&raid_tab_sys, obj_id);
886711890bcSjc156560 	if (raid_obj_op_sys[type].set_attr == NULL) {
887711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
888711890bcSjc156560 		return (ERR_OP_NO_IMPL);
889711890bcSjc156560 	}
890711890bcSjc156560 
891711890bcSjc156560 	ret = raid_obj_op_sys[type].set_attr(&raid_tab_sys,
892711890bcSjc156560 	    obj_id, set_cmd, value, plugin_err_str);
893711890bcSjc156560 
894711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
895711890bcSjc156560 	return (ret);
896711890bcSjc156560 }
897711890bcSjc156560 
898711890bcSjc156560 int
899711890bcSjc156560 raidcfg_update_fw(int handle, char *file, char **plugin_err_str)
900711890bcSjc156560 {
901711890bcSjc156560 	raid_obj_id_t obj_id;
902711890bcSjc156560 	int ret;
903711890bcSjc156560 
904711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
905711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
906711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
907711890bcSjc156560 	if (obj_id < OBJ_NONE) {
908711890bcSjc156560 		raid_handle_delete(handle);
909711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
910711890bcSjc156560 		return (ERR_DEVICE_NOENT);
911711890bcSjc156560 	}
912711890bcSjc156560 
913711890bcSjc156560 	if (raid_obj_get_type(&raid_tab_sys, obj_id) != OBJ_TYPE_CONTROLLER) {
914711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
915711890bcSjc156560 		return (ERR_OP_NO_IMPL);
916711890bcSjc156560 	}
917711890bcSjc156560 
918711890bcSjc156560 	ret = raid_obj_op_sys[OBJ_TYPE_CONTROLLER].act(&raid_tab_sys,
919711890bcSjc156560 	    obj_id, ACT_CONTROLLER_FLASH_FW, file, plugin_err_str);
920711890bcSjc156560 
921711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
922711890bcSjc156560 	return (ret);
923711890bcSjc156560 }
924711890bcSjc156560 
925711890bcSjc156560 int
926711890bcSjc156560 raidcfg_create_array(int num_of_comps, int *disk_handles,
927711890bcSjc156560 	uint32_t raid_level, uint64_t size, uint32_t stripe_size,
928711890bcSjc156560 	char **plugin_err_str)
929711890bcSjc156560 {
930711890bcSjc156560 	raid_obj_id_t *disk_obj_ids, obj_id;
931711890bcSjc156560 	array_attr_t *array_attr;
932711890bcSjc156560 	raid_obj_handle_t array_handle;
933711890bcSjc156560 	int i, ret;
934711890bcSjc156560 
935711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
936711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
937711890bcSjc156560 
938711890bcSjc156560 	disk_obj_ids = calloc(num_of_comps, sizeof (raid_obj_id_t));
939711890bcSjc156560 	if (disk_obj_ids == NULL) {
940711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
941711890bcSjc156560 		return (ERR_NOMEM);
942711890bcSjc156560 	}
943711890bcSjc156560 
944711890bcSjc156560 	/* convert disk handles into disk object ids; */
945711890bcSjc156560 	for (i = 0; i < num_of_comps; ++i) {
946711890bcSjc156560 		if (*(disk_handles + i) == OBJ_SEPARATOR_BEGIN ||
947711890bcSjc156560 		    *(disk_handles + i) == OBJ_SEPARATOR_END) {
948711890bcSjc156560 			*(disk_obj_ids + i) = *(disk_handles + i);
949711890bcSjc156560 			continue;
950711890bcSjc156560 		}
951711890bcSjc156560 
952711890bcSjc156560 		*(disk_obj_ids + i) = raid_handle_to_obj(&raid_tab_sys,
953711890bcSjc156560 		    *(disk_handles + i));
954711890bcSjc156560 		if (raid_obj_get_type(&raid_tab_sys, *(disk_obj_ids + i)) !=
955711890bcSjc156560 		    OBJ_TYPE_DISK) {
956711890bcSjc156560 			free(disk_obj_ids);
957711890bcSjc156560 			(void) obj_rescan(&raid_tab_sys);
958711890bcSjc156560 			(void) mutex_unlock(&raidcfg_mp);
959711890bcSjc156560 			return (ERR_DEVICE_TYPE);
960711890bcSjc156560 		}
961711890bcSjc156560 	}
962711890bcSjc156560 
963711890bcSjc156560 	/* Create an empty array object */
964711890bcSjc156560 	obj_id = raid_obj_create(&raid_tab_sys, OBJ_TYPE_ARRAY);
965711890bcSjc156560 	if (obj_id < OBJ_NONE) {
966711890bcSjc156560 		free(disk_obj_ids);
967711890bcSjc156560 		(void) obj_rescan(&raid_tab_sys);
968711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
969711890bcSjc156560 		return (obj_id);
970711890bcSjc156560 	}
971711890bcSjc156560 	(void) raid_obj_clear_status(&raid_tab_sys, obj_id,
972711890bcSjc156560 	    OBJ_STATUS_CMD_CLEAN);
973711890bcSjc156560 
974711890bcSjc156560 	array_attr = raid_obj_get_data_ptr(&raid_tab_sys, obj_id);
975711890bcSjc156560 	array_attr->array_id = (uint32_t)OBJ_ATTR_NONE;
976711890bcSjc156560 	array_attr->raid_level = raid_level;
977711890bcSjc156560 	array_attr->capacity = size;
978711890bcSjc156560 	array_attr->stripe_size = stripe_size;
979711890bcSjc156560 	array_attr->write_policy = CACHE_WR_ON;
980711890bcSjc156560 	array_attr->read_policy = CACHE_RD_ON;
981711890bcSjc156560 
982711890bcSjc156560 	ret = raid_obj_op_sys[OBJ_TYPE_ARRAY].create_obj(&raid_tab_sys, obj_id,
983711890bcSjc156560 	    num_of_comps, disk_obj_ids, plugin_err_str);
984711890bcSjc156560 	free(disk_obj_ids);
985711890bcSjc156560 
986711890bcSjc156560 	if (ret < SUCCESS) {
987711890bcSjc156560 		(void) obj_rescan(&raid_tab_sys);
988711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
989711890bcSjc156560 		return (ret);
990711890bcSjc156560 	}
991711890bcSjc156560 
992711890bcSjc156560 	/* create_obj() method should put the array object in the device tree */
993711890bcSjc156560 	array_handle = raid_obj_to_handle(&raid_tab_sys, obj_id);
994711890bcSjc156560 
995711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
996711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
997711890bcSjc156560 	return (array_handle);
998711890bcSjc156560 }
999711890bcSjc156560 
1000711890bcSjc156560 int
1001711890bcSjc156560 raidcfg_delete_array(int array_handle, char **plugin_err_str)
1002711890bcSjc156560 {
1003711890bcSjc156560 	raid_obj_id_t array_obj_id;
1004711890bcSjc156560 	int ret;
1005711890bcSjc156560 
1006711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
1007711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1008711890bcSjc156560 
1009711890bcSjc156560 	if (raidcfg_get_type(array_handle) != OBJ_TYPE_ARRAY) {
1010711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1011711890bcSjc156560 		return (ERR_DEVICE_TYPE);
1012711890bcSjc156560 	}
1013711890bcSjc156560 
1014711890bcSjc156560 	array_obj_id = raid_handle_to_obj(&raid_tab_sys, array_handle);
1015711890bcSjc156560 	if (array_obj_id < OBJ_NONE) {
1016711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1017711890bcSjc156560 		return (array_obj_id);
1018711890bcSjc156560 	}
1019711890bcSjc156560 	if (array_obj_id == OBJ_NONE) {
1020711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1021711890bcSjc156560 		return (ERR_DEVICE_INVALID);
1022711890bcSjc156560 	}
1023711890bcSjc156560 
1024711890bcSjc156560 	ret = raid_obj_op_sys[OBJ_TYPE_ARRAY].delete_obj(&raid_tab_sys,
1025711890bcSjc156560 	    array_obj_id, plugin_err_str);
1026711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1027711890bcSjc156560 
1028711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
1029711890bcSjc156560 	return (ret);
1030711890bcSjc156560 }
1031711890bcSjc156560 
1032711890bcSjc156560 int
10335c9d25d2SYu-Bo Ryan Wang raidcfg_set_hsp(raidcfg_hsp_relation_t *hsp_relations,
1034711890bcSjc156560 	char **plugin_err_str)
1035711890bcSjc156560 {
1036711890bcSjc156560 	raid_obj_id_t disk_obj_id, array_obj_id;
1037711890bcSjc156560 	raid_obj_id_t *hsp_relation_objs;
10385c9d25d2SYu-Bo Ryan Wang 	int ret;
1039711890bcSjc156560 
1040711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
1041711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
10425c9d25d2SYu-Bo Ryan Wang 	if (hsp_relations == NULL) {
1043711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1044711890bcSjc156560 		return (ERR_OP_ILLEGAL);
1045711890bcSjc156560 	}
1046711890bcSjc156560 
10475c9d25d2SYu-Bo Ryan Wang 	hsp_relation_objs = malloc(2 * sizeof (raid_obj_id_t));
1048711890bcSjc156560 	if (hsp_relation_objs == NULL) {
1049711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1050711890bcSjc156560 		return (ERR_NOMEM);
1051711890bcSjc156560 	}
1052711890bcSjc156560 
1053711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1054711890bcSjc156560 
1055711890bcSjc156560 	if (hsp_relations->array_handle != OBJ_ATTR_NONE) {
1056711890bcSjc156560 		array_obj_id = raid_handle_to_obj(&raid_tab_sys,
10575c9d25d2SYu-Bo Ryan Wang 		    hsp_relations->array_handle);
1058711890bcSjc156560 		if (array_obj_id < OBJ_NONE) {
1059711890bcSjc156560 			free(hsp_relation_objs);
1060711890bcSjc156560 			(void) mutex_unlock(&raidcfg_mp);
1061711890bcSjc156560 			return (array_obj_id);
1062711890bcSjc156560 		}
1063711890bcSjc156560 		if (array_obj_id == OBJ_NONE) {
1064711890bcSjc156560 			(void) free(hsp_relation_objs);
1065711890bcSjc156560 			(void) mutex_unlock(&raidcfg_mp);
1066711890bcSjc156560 			return (ERR_DEVICE_NOENT);
1067711890bcSjc156560 		}
10685c9d25d2SYu-Bo Ryan Wang 		if (raidcfg_get_type(hsp_relations->array_handle) !=
1069711890bcSjc156560 		    OBJ_TYPE_ARRAY) {
1070711890bcSjc156560 			free(hsp_relation_objs);
1071711890bcSjc156560 			(void) mutex_unlock(&raidcfg_mp);
1072711890bcSjc156560 			return (ERR_DEVICE_TYPE);
1073711890bcSjc156560 		}
1074711890bcSjc156560 	} else
1075711890bcSjc156560 		array_obj_id = OBJ_ATTR_NONE;
1076711890bcSjc156560 
1077711890bcSjc156560 	disk_obj_id = raid_handle_to_obj(&raid_tab_sys,
10785c9d25d2SYu-Bo Ryan Wang 	    hsp_relations->disk_handle);
1079711890bcSjc156560 	if (disk_obj_id < OBJ_NONE) {
1080711890bcSjc156560 		free(hsp_relation_objs);
1081711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1082711890bcSjc156560 		return (disk_obj_id);
1083711890bcSjc156560 	}
1084711890bcSjc156560 	if (disk_obj_id == OBJ_NONE) {
1085711890bcSjc156560 		free(hsp_relation_objs);
1086711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1087711890bcSjc156560 		return (ERR_DEVICE_NOENT);
1088711890bcSjc156560 	}
10895c9d25d2SYu-Bo Ryan Wang 	if (raidcfg_get_type(hsp_relations->disk_handle) !=
1090711890bcSjc156560 	    OBJ_TYPE_DISK) {
1091711890bcSjc156560 		free(hsp_relation_objs);
1092711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1093711890bcSjc156560 		return (ERR_DEVICE_TYPE);
1094711890bcSjc156560 	}
1095711890bcSjc156560 
10965c9d25d2SYu-Bo Ryan Wang 	hsp_relation_objs[0] = array_obj_id;
10975c9d25d2SYu-Bo Ryan Wang 	hsp_relation_objs[1] = disk_obj_id;
1098711890bcSjc156560 
10995c9d25d2SYu-Bo Ryan Wang 	ret = raid_obj_op_sys[OBJ_TYPE_HSP].bind_obj(&raid_tab_sys,
1100711890bcSjc156560 	    hsp_relation_objs, plugin_err_str);
1101711890bcSjc156560 
1102711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1103711890bcSjc156560 	free(hsp_relation_objs);
1104711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
1105711890bcSjc156560 
1106711890bcSjc156560 	return (ret);
1107711890bcSjc156560 }
1108711890bcSjc156560 
1109711890bcSjc156560 int
11105c9d25d2SYu-Bo Ryan Wang raidcfg_unset_hsp(raidcfg_hsp_relation_t *hsp_relations,
1111711890bcSjc156560 	char **plugin_err_str)
1112711890bcSjc156560 {
1113711890bcSjc156560 	raid_obj_id_t disk_obj_id, array_obj_id;
1114711890bcSjc156560 	raid_obj_id_t *hsp_relation_objs;
11155c9d25d2SYu-Bo Ryan Wang 	int ret;
1116711890bcSjc156560 
1117711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
1118711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
11195c9d25d2SYu-Bo Ryan Wang 	if (hsp_relations == NULL) {
1120711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1121711890bcSjc156560 		return (ERR_OP_ILLEGAL);
1122711890bcSjc156560 	}
1123711890bcSjc156560 
11245c9d25d2SYu-Bo Ryan Wang 	hsp_relation_objs = malloc(2 * sizeof (raid_obj_id_t));
1125711890bcSjc156560 	if (hsp_relation_objs == NULL) {
1126711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1127711890bcSjc156560 		return (ERR_NOMEM);
1128711890bcSjc156560 	}
1129711890bcSjc156560 
1130711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1131711890bcSjc156560 
1132711890bcSjc156560 	if (hsp_relations->array_handle != OBJ_ATTR_NONE) {
1133711890bcSjc156560 		array_obj_id = raid_handle_to_obj(&raid_tab_sys,
11345c9d25d2SYu-Bo Ryan Wang 		    hsp_relations->array_handle);
1135711890bcSjc156560 		if (array_obj_id < OBJ_NONE) {
1136711890bcSjc156560 			free(hsp_relation_objs);
1137711890bcSjc156560 			(void) mutex_unlock(&raidcfg_mp);
1138711890bcSjc156560 			return (array_obj_id);
1139711890bcSjc156560 		}
1140711890bcSjc156560 		if (array_obj_id == OBJ_NONE) {
1141711890bcSjc156560 			free(hsp_relation_objs);
1142711890bcSjc156560 			(void) mutex_unlock(&raidcfg_mp);
1143711890bcSjc156560 			return (ERR_DEVICE_NOENT);
1144711890bcSjc156560 		}
11455c9d25d2SYu-Bo Ryan Wang 		if (raidcfg_get_type(hsp_relations->array_handle) !=
1146711890bcSjc156560 		    OBJ_TYPE_ARRAY) {
1147711890bcSjc156560 			free(hsp_relation_objs);
1148711890bcSjc156560 			(void) mutex_unlock(&raidcfg_mp);
1149711890bcSjc156560 			return (ERR_DEVICE_TYPE);
1150711890bcSjc156560 		}
1151711890bcSjc156560 	} else
1152711890bcSjc156560 		array_obj_id = OBJ_ATTR_NONE;
1153711890bcSjc156560 
1154711890bcSjc156560 	disk_obj_id = raid_handle_to_obj(&raid_tab_sys,
11555c9d25d2SYu-Bo Ryan Wang 	    hsp_relations->disk_handle);
1156711890bcSjc156560 	if (disk_obj_id < OBJ_NONE) {
1157711890bcSjc156560 		free(hsp_relation_objs);
1158711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1159711890bcSjc156560 		return (disk_obj_id);
1160711890bcSjc156560 	}
1161711890bcSjc156560 	if (disk_obj_id == OBJ_NONE) {
1162711890bcSjc156560 		free(hsp_relation_objs);
1163711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1164711890bcSjc156560 		return (ERR_DEVICE_NOENT);
1165711890bcSjc156560 	}
11665c9d25d2SYu-Bo Ryan Wang 	if (raidcfg_get_type(hsp_relations->disk_handle) !=
1167711890bcSjc156560 	    OBJ_TYPE_DISK) {
1168711890bcSjc156560 		free(hsp_relation_objs);
1169711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1170711890bcSjc156560 		return (ERR_DEVICE_TYPE);
1171711890bcSjc156560 	}
1172711890bcSjc156560 
11735c9d25d2SYu-Bo Ryan Wang 	hsp_relation_objs[0] = array_obj_id;
11745c9d25d2SYu-Bo Ryan Wang 	hsp_relation_objs[1] = disk_obj_id;
1175711890bcSjc156560 
1176711890bcSjc156560 	ret = raid_obj_op_sys[OBJ_TYPE_HSP].unbind_obj(&raid_tab_sys,
11775c9d25d2SYu-Bo Ryan Wang 	    hsp_relation_objs, plugin_err_str);
1178711890bcSjc156560 
1179711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1180711890bcSjc156560 	free(hsp_relation_objs);
1181711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
1182711890bcSjc156560 
1183711890bcSjc156560 	return (ret);
1184711890bcSjc156560 }
1185711890bcSjc156560 
1186711890bcSjc156560 /*
1187711890bcSjc156560  * RaidCfg lib routines
1188711890bcSjc156560  */
1189711890bcSjc156560 void
1190711890bcSjc156560 raidcfg_init(void)
1191711890bcSjc156560 {
1192711890bcSjc156560 	(void) mutex_init(&raidcfg_mp, NULL, NULL);
1193711890bcSjc156560 	raid_plugin_init();
1194711890bcSjc156560 	(void) raid_handle_init();
1195711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1196711890bcSjc156560 }
1197711890bcSjc156560 
1198711890bcSjc156560 void
1199711890bcSjc156560 raidcfg_fini(void)
1200711890bcSjc156560 {
1201711890bcSjc156560 	/*
1202711890bcSjc156560 	 * Need to close all opened controllers before destroying object table
1203711890bcSjc156560 	 */
1204711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1205711890bcSjc156560 	raid_handle_fini();
1206711890bcSjc156560 	raid_obj_tab_destroy(&raid_tab_sys);
1207711890bcSjc156560 	raid_plugin_init();
1208711890bcSjc156560 	(void) mutex_destroy(&raidcfg_mp);
1209711890bcSjc156560 }
1210711890bcSjc156560 
1211711890bcSjc156560 /*
1212711890bcSjc156560  * Support routines
1213711890bcSjc156560  */
1214711890bcSjc156560 static int
1215711890bcSjc156560 intcompare(const void *p1, const void *p2)
1216711890bcSjc156560 {
1217711890bcSjc156560 	int i, j;
1218711890bcSjc156560 	i = *((int *)p1);
1219711890bcSjc156560 	j = *((int *)p2);
1220711890bcSjc156560 	return (i - j);
1221711890bcSjc156560 }
1222711890bcSjc156560 
1223711890bcSjc156560 static uint64_t
1224711890bcSjc156560 raid_space_noalign(raid_obj_tab_t *raid_tab, uint32_t raid_level, int num,
1225711890bcSjc156560 	raid_obj_id_t *disk_objs, arraypart_attr_t *arraypart_attrs)
1226711890bcSjc156560 {
1227711890bcSjc156560 	disk_attr_t *disk_attr;
1228711890bcSjc156560 	diskseg_attr_t *diskseg_attr;
1229711890bcSjc156560 	raid_obj_id_t obj_id;
1230711890bcSjc156560 	uint64_t offset, capacity;
1231711890bcSjc156560 	int i, disk_num, sub_array_num, disk_layer;
1232711890bcSjc156560 
1233711890bcSjc156560 	/* Find out the maximum available space for all disks */
1234711890bcSjc156560 	for (i = 0; i < num; ++i) {
1235711890bcSjc156560 		if ((disk_objs[i] == OBJ_SEPARATOR_BEGIN) ||
1236711890bcSjc156560 		    (disk_objs[i] == OBJ_SEPARATOR_END))
1237711890bcSjc156560 			continue;
1238711890bcSjc156560 
1239711890bcSjc156560 		(void) obj_get_attr(raid_tab, disk_objs[i],
1240711890bcSjc156560 		    (void **)(&disk_attr));
1241711890bcSjc156560 		obj_id = obj_get_comp(raid_tab, disk_objs[i],
1242711890bcSjc156560 		    OBJ_TYPE_DISK_SEG);
1243711890bcSjc156560 		if (obj_id == OBJ_NONE) {
1244711890bcSjc156560 			arraypart_attrs[i].offset = 0;
1245711890bcSjc156560 			arraypart_attrs[i].size = disk_attr->capacity;
1246711890bcSjc156560 			continue;
1247711890bcSjc156560 		}
1248711890bcSjc156560 
1249711890bcSjc156560 		(void) obj_get_attr(raid_tab, obj_id, (void **)
1250711890bcSjc156560 		    (&diskseg_attr));
1251711890bcSjc156560 		arraypart_attrs[i].offset = 0;
1252711890bcSjc156560 		arraypart_attrs[i].size = diskseg_attr->offset;
1253711890bcSjc156560 		offset = diskseg_attr->offset + diskseg_attr->size;
1254711890bcSjc156560 
1255711890bcSjc156560 		while ((obj_id = obj_get_sibling(raid_tab, obj_id)) !=
1256711890bcSjc156560 		    OBJ_NONE) {
1257711890bcSjc156560 			(void) obj_get_attr(raid_tab, obj_id,
1258711890bcSjc156560 			    (void **)(&diskseg_attr));
1259711890bcSjc156560 			if ((diskseg_attr->offset - offset) >
1260711890bcSjc156560 			    arraypart_attrs[i].size) {
1261711890bcSjc156560 				arraypart_attrs[i].offset = offset;
1262700682b8Syw161884 				arraypart_attrs[i].size = diskseg_attr->offset -
1263700682b8Syw161884 				    offset;
1264711890bcSjc156560 			}
1265711890bcSjc156560 
1266711890bcSjc156560 			offset = diskseg_attr->offset + diskseg_attr->size;
1267711890bcSjc156560 		}
1268711890bcSjc156560 
1269711890bcSjc156560 		if ((disk_attr->capacity - offset) > arraypart_attrs[i].size) {
1270711890bcSjc156560 			arraypart_attrs[i].offset = offset;
1271700682b8Syw161884 			arraypart_attrs[i].size = disk_attr->capacity -
1272700682b8Syw161884 			    offset;
1273711890bcSjc156560 		}
1274711890bcSjc156560 	}
1275711890bcSjc156560 
1276711890bcSjc156560 	capacity = OBJ_ATTR_NONE;
1277711890bcSjc156560 	disk_num = 0;
1278711890bcSjc156560 	disk_layer = 0;
1279711890bcSjc156560 	sub_array_num = 0;
1280711890bcSjc156560 	for (i = 0; i < num; ++i) {
1281711890bcSjc156560 		if (disk_objs[i] == OBJ_SEPARATOR_BEGIN) {
1282711890bcSjc156560 			++ disk_layer;
1283711890bcSjc156560 			continue;
1284711890bcSjc156560 		}
1285711890bcSjc156560 		if (disk_objs[i] == OBJ_SEPARATOR_END) {
1286711890bcSjc156560 			-- disk_layer;
1287711890bcSjc156560 			if (disk_layer != 0)
1288711890bcSjc156560 				++ sub_array_num;
1289711890bcSjc156560 			continue;
1290711890bcSjc156560 		}
1291711890bcSjc156560 
1292711890bcSjc156560 		if (capacity > arraypart_attrs[i].size)
1293711890bcSjc156560 			capacity = arraypart_attrs[i].size;
1294711890bcSjc156560 		++disk_num;
1295711890bcSjc156560 	}
1296711890bcSjc156560 
1297711890bcSjc156560 	switch (raid_level) {
1298711890bcSjc156560 	case	RAID_LEVEL_0:
1299711890bcSjc156560 		capacity = capacity * disk_num;
1300711890bcSjc156560 		break;
1301711890bcSjc156560 	case	RAID_LEVEL_1:
1302711890bcSjc156560 		capacity = capacity * disk_num / 2;
1303711890bcSjc156560 		break;
1304711890bcSjc156560 	case	RAID_LEVEL_1E:
1305711890bcSjc156560 		capacity = capacity * disk_num / 2;
1306711890bcSjc156560 		break;
1307711890bcSjc156560 	case	RAID_LEVEL_5:
1308711890bcSjc156560 		capacity = capacity * (disk_num - 1);
1309711890bcSjc156560 		break;
1310711890bcSjc156560 	case	RAID_LEVEL_10:
1311711890bcSjc156560 		capacity = capacity * disk_num / 2;
1312711890bcSjc156560 		break;
1313711890bcSjc156560 	case	RAID_LEVEL_50:
1314711890bcSjc156560 		capacity = capacity * (disk_num - sub_array_num);
1315711890bcSjc156560 		break;
1316711890bcSjc156560 	default:
1317711890bcSjc156560 		return (ERR_ARRAY_LEVEL);
1318711890bcSjc156560 	}
1319711890bcSjc156560 
1320711890bcSjc156560 	return (capacity);
1321711890bcSjc156560 }
1322711890bcSjc156560 
1323711890bcSjc156560 /*
1324711890bcSjc156560  * Raid handle maintenance routines
1325711890bcSjc156560  */
1326711890bcSjc156560 static int
1327711890bcSjc156560 raid_handle_init()
1328711890bcSjc156560 {
1329711890bcSjc156560 	int i;
1330711890bcSjc156560 	void *ptr;
1331711890bcSjc156560 
1332711890bcSjc156560 	raid_handle_sys.handle_num += HANDLER_SLOTS;
1333711890bcSjc156560 	ptr = realloc(raid_handle_sys.handles,
1334711890bcSjc156560 	    raid_handle_sys.handle_num * sizeof (handle_attr_t));
1335711890bcSjc156560 	if (ptr == NULL)
1336711890bcSjc156560 		return (ERR_NOMEM);
1337711890bcSjc156560 	raid_handle_sys.handles = ptr;
1338711890bcSjc156560 
1339711890bcSjc156560 	/* Clean up the new allocated handles */
1340711890bcSjc156560 	for (i = raid_handle_sys.handle_num - HANDLER_SLOTS;
1341711890bcSjc156560 	    i < raid_handle_sys.handle_num; ++i) {
1342ee992693SZach Kissel 		bzero(&raid_handle_sys.handles[i], sizeof (handle_attr_t));
1343711890bcSjc156560 		raid_handle_sys.handles[i].type = OBJ_TYPE_ALL;
1344711890bcSjc156560 		raid_handle_sys.handles[i].next = i + 1;
1345711890bcSjc156560 	}
1346711890bcSjc156560 
1347711890bcSjc156560 	/* For the first time of allocation, set up the system object handle */
1348711890bcSjc156560 	if (raid_handle_sys.handle_num == HANDLER_SLOTS) {
1349711890bcSjc156560 		raid_handle_sys.handles[0].type = OBJ_TYPE_SYSTEM;
1350711890bcSjc156560 		raid_handle_sys.handles[0].next = 0;
1351711890bcSjc156560 		raid_handle_sys.unused = 1;
1352711890bcSjc156560 		raid_handle_sys.used = 0;
1353711890bcSjc156560 	}
1354711890bcSjc156560 	return (SUCCESS);
1355711890bcSjc156560 }
1356711890bcSjc156560 
1357711890bcSjc156560 static void
1358711890bcSjc156560 raid_handle_fini()
1359711890bcSjc156560 {
1360711890bcSjc156560 	raid_obj_handle_t i;
1361711890bcSjc156560 
1362711890bcSjc156560 	i = raid_handle_sys.used;
1363711890bcSjc156560 
1364711890bcSjc156560 	/* Close all opened controllers */
1365711890bcSjc156560 	while (i != 0) {
1366711890bcSjc156560 		if ((raid_handle_sys.handles[i].type == OBJ_TYPE_CONTROLLER) &&
1367711890bcSjc156560 		    (raid_handle_sys.handles[i].fd != 0) &&
1368711890bcSjc156560 		    (raid_handle_sys.handles[i].raid_lib != NULL))
1369711890bcSjc156560 			raid_handle_sys.handles[i].raid_lib->close_controller(
1370711890bcSjc156560 			    raid_handle_sys.handles[i].controller_id, NULL);
1371711890bcSjc156560 		i = raid_handle_sys.handles[i].next;
1372711890bcSjc156560 	}
1373711890bcSjc156560 
1374711890bcSjc156560 	/* Clean up handle space */
1375711890bcSjc156560 	raid_handle_sys.handle_num = 0;
1376711890bcSjc156560 	raid_handle_sys.unused = 0;
1377711890bcSjc156560 	raid_handle_sys.used = 0;
1378711890bcSjc156560 	free(raid_handle_sys.handles);
1379711890bcSjc156560 	raid_handle_sys.handles = NULL;
1380711890bcSjc156560 }
1381711890bcSjc156560 
1382711890bcSjc156560 static raid_obj_handle_t
1383711890bcSjc156560 raid_handle_new(raid_obj_type_id_t type)
1384711890bcSjc156560 {
1385711890bcSjc156560 	int ret;
1386711890bcSjc156560 
1387711890bcSjc156560 	if (raid_handle_sys.unused == raid_handle_sys.handle_num - 1) {
1388711890bcSjc156560 		ret = raid_handle_init();
1389711890bcSjc156560 		if (ret < SUCCESS)
1390711890bcSjc156560 			return (ret);
1391711890bcSjc156560 	}
1392711890bcSjc156560 
1393711890bcSjc156560 	ret = raid_handle_sys.unused;
1394711890bcSjc156560 	raid_handle_sys.unused = raid_handle_sys.handles[ret].next;
1395711890bcSjc156560 
1396711890bcSjc156560 	raid_handle_sys.handles[ret].next = raid_handle_sys.used;
1397711890bcSjc156560 	raid_handle_sys.used = ret;
1398711890bcSjc156560 	raid_handle_sys.handles[ret].type = type;
1399711890bcSjc156560 
1400711890bcSjc156560 	return (ret);
1401711890bcSjc156560 }
1402711890bcSjc156560 
1403711890bcSjc156560 static void
1404711890bcSjc156560 raid_handle_delete(raid_obj_handle_t handle)
1405711890bcSjc156560 {
1406711890bcSjc156560 	int i = raid_handle_sys.used, j = 0;
1407711890bcSjc156560 
1408711890bcSjc156560 	if (handle == 0)
1409711890bcSjc156560 		return;
1410711890bcSjc156560 
1411711890bcSjc156560 	while (i != 0 && i != handle) {
1412711890bcSjc156560 		j = i;
1413711890bcSjc156560 		i = raid_handle_sys.handles[i].next;
1414711890bcSjc156560 	}
1415711890bcSjc156560 
1416711890bcSjc156560 	if (i == handle) {
1417711890bcSjc156560 		if (j != 0)
1418711890bcSjc156560 			raid_handle_sys.handles[j].next =
1419711890bcSjc156560 			    raid_handle_sys.handles[i].next;
1420711890bcSjc156560 		else
1421711890bcSjc156560 			raid_handle_sys.used =
1422711890bcSjc156560 			    raid_handle_sys.handles[i].next;
1423711890bcSjc156560 
1424711890bcSjc156560 		raid_handle_sys.handles[i].type = OBJ_TYPE_ALL;
1425711890bcSjc156560 		raid_handle_sys.handles[i].next =
1426711890bcSjc156560 		    raid_handle_sys.unused;
1427711890bcSjc156560 		raid_handle_sys.unused = i;
1428711890bcSjc156560 	}
1429711890bcSjc156560 }
1430711890bcSjc156560 
1431711890bcSjc156560 static void
1432711890bcSjc156560 raid_handle_delete_controller_comp(uint32_t controller_id)
1433711890bcSjc156560 {
1434711890bcSjc156560 	int i = raid_handle_sys.used, j;
1435711890bcSjc156560 
1436711890bcSjc156560 	while (i != 0) {
1437711890bcSjc156560 		j = i;
1438711890bcSjc156560 		i = raid_handle_sys.handles[i].next;
1439700682b8Syw161884 		if ((raid_handle_sys.handles[j].controller_id ==
1440700682b8Syw161884 		    controller_id) &&
1441700682b8Syw161884 		    (raid_handle_sys.handles[j].type !=
1442700682b8Syw161884 		    OBJ_TYPE_CONTROLLER))
1443711890bcSjc156560 		raid_handle_delete(j);
1444711890bcSjc156560 	}
1445711890bcSjc156560 }
1446711890bcSjc156560 
1447711890bcSjc156560 static raid_obj_id_t
1448711890bcSjc156560 raid_handle_to_obj(raid_obj_tab_t *raid_tab, raid_obj_handle_t handle)
1449711890bcSjc156560 {
1450711890bcSjc156560 	handle_attr_t *handle_attr;
1451711890bcSjc156560 	raid_obj_id_t obj_id;
1452711890bcSjc156560 
1453711890bcSjc156560 	if (handle == OBJ_SYSTEM)
1454711890bcSjc156560 		return (OBJ_SYSTEM);
1455711890bcSjc156560 
1456711890bcSjc156560 	handle_attr = raid_handle_sys.handles + handle;
1457711890bcSjc156560 
1458711890bcSjc156560 	switch (handle_attr->type) {
1459711890bcSjc156560 	case	OBJ_TYPE_SYSTEM:
1460711890bcSjc156560 		return (OBJ_SYSTEM);
1461711890bcSjc156560 	case	OBJ_TYPE_CONTROLLER:
1462711890bcSjc156560 		obj_id = obj_locate_controller(raid_tab,
1463711890bcSjc156560 		    handle_attr->controller_id);
1464711890bcSjc156560 		break;
1465711890bcSjc156560 	case	OBJ_TYPE_ARRAY:
1466711890bcSjc156560 		obj_id = obj_locate_array(raid_tab,
1467711890bcSjc156560 		    handle_attr->controller_id, handle_attr->array_id);
1468711890bcSjc156560 		break;
1469711890bcSjc156560 	case	OBJ_TYPE_HSP:
1470711890bcSjc156560 		obj_id = obj_locate_hsp(raid_tab,
1471711890bcSjc156560 		    handle_attr->controller_id, handle_attr->disk_id,
1472711890bcSjc156560 		    handle_attr->array_id);
1473711890bcSjc156560 		break;
1474711890bcSjc156560 	case	OBJ_TYPE_DISK:
1475711890bcSjc156560 		obj_id = obj_locate_disk(raid_tab,
1476711890bcSjc156560 		    handle_attr->controller_id, handle_attr->disk_id);
1477711890bcSjc156560 		break;
1478711890bcSjc156560 	case	OBJ_TYPE_ARRAY_PART:
1479711890bcSjc156560 		obj_id = obj_locate_arraypart(raid_tab,
1480711890bcSjc156560 		    handle_attr->controller_id, handle_attr->array_id,
1481711890bcSjc156560 		    handle_attr->disk_id);
1482711890bcSjc156560 		break;
1483711890bcSjc156560 	case	OBJ_TYPE_DISK_SEG:
1484711890bcSjc156560 		obj_id = obj_locate_diskseg(raid_tab,
1485711890bcSjc156560 		    handle_attr->controller_id,
1486711890bcSjc156560 		    handle_attr->disk_id, handle_attr->seq_id);
1487711890bcSjc156560 		break;
1488711890bcSjc156560 	case	OBJ_TYPE_TASK:
1489711890bcSjc156560 		obj_id = obj_locate_task(raid_tab,
1490711890bcSjc156560 		    handle_attr->controller_id, handle_attr->task_id);
1491711890bcSjc156560 		break;
1492b449fa8aSyw161884 	case	OBJ_TYPE_PROP:
1493b449fa8aSyw161884 		obj_id = obj_locate_prop(raid_tab,
1494b449fa8aSyw161884 		    handle_attr->controller_id, handle_attr->disk_id,
1495b449fa8aSyw161884 		    handle_attr->prop_id);
1496b449fa8aSyw161884 		break;
1497711890bcSjc156560 	default:
1498711890bcSjc156560 		return (ERR_DEVICE_INVALID);
1499711890bcSjc156560 	}
1500711890bcSjc156560 
1501711890bcSjc156560 	if (obj_id < OBJ_NONE)
1502711890bcSjc156560 		return (obj_id);
1503711890bcSjc156560 	if (obj_id == OBJ_NONE)
1504711890bcSjc156560 		return (ERR_DEVICE_NOENT);
1505711890bcSjc156560 
1506711890bcSjc156560 	(void) raid_obj_set_handle(raid_tab, obj_id, handle);
1507711890bcSjc156560 	return (obj_id);
1508711890bcSjc156560 }
1509711890bcSjc156560 
1510711890bcSjc156560 static raid_obj_handle_t
1511711890bcSjc156560 raid_obj_to_handle(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
1512711890bcSjc156560 {
1513711890bcSjc156560 	raid_obj_id_t obj_id_backup = obj_id;
1514711890bcSjc156560 	raid_obj_type_id_t type;
1515711890bcSjc156560 	raid_obj_handle_t handle;
1516711890bcSjc156560 	controller_attr_t *controller_attr;
1517711890bcSjc156560 	array_attr_t *array_attr;
1518711890bcSjc156560 	hsp_attr_t *hsp_attr;
1519711890bcSjc156560 	disk_attr_t *disk_attr;
1520711890bcSjc156560 	arraypart_attr_t *arraypart_attr;
1521711890bcSjc156560 	diskseg_attr_t *diskseg_attr;
1522711890bcSjc156560 	task_attr_t *task_attr;
1523b449fa8aSyw161884 	property_attr_t *prop_attr;
1524711890bcSjc156560 
1525711890bcSjc156560 	if (obj_id == OBJ_SYSTEM)
1526711890bcSjc156560 		return (OBJ_SYSTEM);
1527711890bcSjc156560 
1528711890bcSjc156560 	/* If the object mapped by a handle */
1529711890bcSjc156560 	handle = raid_obj_get_handle(raid_tab, obj_id);
1530711890bcSjc156560 	if (handle != 0)
1531711890bcSjc156560 		return (handle);
1532711890bcSjc156560 
1533711890bcSjc156560 	/* Search for existing handles */
1534711890bcSjc156560 	for (handle = raid_handle_sys.used; handle != 0;
1535711890bcSjc156560 	    handle = raid_handle_sys.handles[handle].next)
1536711890bcSjc156560 		if (raid_handle_to_obj(raid_tab, handle) == obj_id)
1537711890bcSjc156560 			break;
1538711890bcSjc156560 
1539711890bcSjc156560 	if (handle != 0)
1540711890bcSjc156560 		return (handle);
1541711890bcSjc156560 
1542711890bcSjc156560 	/* Allocate new handle for this object */
1543711890bcSjc156560 	type = raid_obj_get_type(raid_tab, obj_id);
1544711890bcSjc156560 	handle = raid_handle_new(type);
1545711890bcSjc156560 	(void) raid_obj_set_handle(raid_tab, obj_id, handle);
1546711890bcSjc156560 	raid_handle_sys.handles[handle].type = type;
1547711890bcSjc156560 
1548711890bcSjc156560 	switch (type) {
1549711890bcSjc156560 	case OBJ_TYPE_SYSTEM:
1550711890bcSjc156560 		break;
1551711890bcSjc156560 	case OBJ_TYPE_CONTROLLER:
1552711890bcSjc156560 		controller_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1553711890bcSjc156560 		raid_handle_sys.handles[handle].controller_id =
1554711890bcSjc156560 		    controller_attr->controller_id;
1555711890bcSjc156560 		break;
1556711890bcSjc156560 	case OBJ_TYPE_ARRAY:
1557711890bcSjc156560 		array_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1558711890bcSjc156560 		raid_handle_sys.handles[handle].array_id = array_attr->array_id;
1559711890bcSjc156560 		obj_id = obj_get_controller(raid_tab, obj_id);
1560711890bcSjc156560 		controller_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1561711890bcSjc156560 		raid_handle_sys.handles[handle].controller_id =
1562711890bcSjc156560 		    controller_attr->controller_id;
1563711890bcSjc156560 		break;
1564711890bcSjc156560 	case OBJ_TYPE_HSP:
1565711890bcSjc156560 		hsp_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1566711890bcSjc156560 		raid_handle_sys.handles[handle].array_id =
1567711890bcSjc156560 		    hsp_attr->associated_id;
1568711890bcSjc156560 		obj_id = raid_obj_get_container(raid_tab, obj_id);
1569711890bcSjc156560 		disk_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1570711890bcSjc156560 		raid_handle_sys.handles[handle].disk_id = disk_attr->disk_id;
1571711890bcSjc156560 		obj_id = obj_get_controller(raid_tab, obj_id);
1572711890bcSjc156560 		controller_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1573711890bcSjc156560 		raid_handle_sys.handles[handle].controller_id =
1574711890bcSjc156560 		    controller_attr->controller_id;
1575711890bcSjc156560 		break;
1576711890bcSjc156560 	case OBJ_TYPE_DISK:
1577711890bcSjc156560 		disk_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1578711890bcSjc156560 		raid_handle_sys.handles[handle].disk_id = disk_attr->disk_id;
1579711890bcSjc156560 		obj_id = obj_get_controller(raid_tab, obj_id);
1580711890bcSjc156560 		controller_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1581711890bcSjc156560 		raid_handle_sys.handles[handle].controller_id =
1582711890bcSjc156560 		    controller_attr->controller_id;
1583711890bcSjc156560 		break;
1584711890bcSjc156560 	case OBJ_TYPE_ARRAY_PART:
1585711890bcSjc156560 		arraypart_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1586711890bcSjc156560 		raid_handle_sys.handles[handle].disk_id =
1587711890bcSjc156560 		    arraypart_attr->disk_id;
1588711890bcSjc156560 		obj_id = raid_obj_get_container(raid_tab, obj_id);
1589711890bcSjc156560 		array_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1590711890bcSjc156560 		raid_handle_sys.handles[handle].array_id =
1591711890bcSjc156560 		    array_attr->array_id;
1592711890bcSjc156560 		obj_id = obj_get_controller(raid_tab, obj_id);
1593711890bcSjc156560 		controller_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1594711890bcSjc156560 		raid_handle_sys.handles[handle].controller_id =
1595711890bcSjc156560 		    controller_attr->controller_id;
1596711890bcSjc156560 		break;
1597711890bcSjc156560 	case OBJ_TYPE_DISK_SEG:
1598711890bcSjc156560 		diskseg_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1599711890bcSjc156560 		raid_handle_sys.handles[handle].seq_id = diskseg_attr->seq_no;
1600711890bcSjc156560 		obj_id = raid_obj_get_container(raid_tab, obj_id);
1601711890bcSjc156560 		disk_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1602711890bcSjc156560 		raid_handle_sys.handles[handle].disk_id =
1603711890bcSjc156560 		    disk_attr->disk_id;
1604711890bcSjc156560 		obj_id = obj_get_controller(raid_tab, obj_id);
1605711890bcSjc156560 		controller_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1606711890bcSjc156560 		raid_handle_sys.handles[handle].controller_id =
1607711890bcSjc156560 		    controller_attr->controller_id;
1608711890bcSjc156560 		break;
1609711890bcSjc156560 	case OBJ_TYPE_TASK:
1610711890bcSjc156560 		task_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1611711890bcSjc156560 		raid_handle_sys.handles[handle].task_id = task_attr->task_id;
1612711890bcSjc156560 		obj_id = obj_get_controller(raid_tab, obj_id);
1613711890bcSjc156560 		controller_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1614711890bcSjc156560 		raid_handle_sys.handles[handle].controller_id =
1615711890bcSjc156560 		    controller_attr->controller_id;
1616711890bcSjc156560 		break;
1617b449fa8aSyw161884 	case OBJ_TYPE_PROP:
1618b449fa8aSyw161884 		prop_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1619b449fa8aSyw161884 		raid_handle_sys.handles[handle].prop_id =
1620b449fa8aSyw161884 		    prop_attr->prop_id;
1621b449fa8aSyw161884 		obj_id = raid_obj_get_container(raid_tab, obj_id);
1622b449fa8aSyw161884 		disk_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1623b449fa8aSyw161884 		raid_handle_sys.handles[handle].disk_id = disk_attr->disk_id;
1624b449fa8aSyw161884 		obj_id = obj_get_controller(raid_tab, obj_id);
1625b449fa8aSyw161884 		controller_attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1626b449fa8aSyw161884 		raid_handle_sys.handles[handle].controller_id =
1627b449fa8aSyw161884 		    controller_attr->controller_id;
1628b449fa8aSyw161884 		break;
1629711890bcSjc156560 	default:
1630711890bcSjc156560 		return (ERR_DEVICE_INVALID);
1631711890bcSjc156560 	}
1632711890bcSjc156560 
1633711890bcSjc156560 	(void) raid_obj_set_handle(raid_tab, obj_id_backup, handle);
1634711890bcSjc156560 	return (handle);
1635711890bcSjc156560 }
1636711890bcSjc156560 
1637711890bcSjc156560 static raid_lib_t *
1638711890bcSjc156560 raid_obj_get_lib(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
1639711890bcSjc156560 {
1640711890bcSjc156560 	raid_obj_handle_t handle;
1641711890bcSjc156560 	controller_attr_t *attr;
1642711890bcSjc156560 
1643711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
1644711890bcSjc156560 		return (NULL);
1645711890bcSjc156560 
1646711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1647711890bcSjc156560 	handle = raid_handle_sys.used;
1648711890bcSjc156560 	while (raid_handle_sys.handles[handle].type != OBJ_TYPE_CONTROLLER ||
1649711890bcSjc156560 	    raid_handle_sys.handles[handle].controller_id !=
1650711890bcSjc156560 	    attr->controller_id)
1651711890bcSjc156560 		handle = raid_handle_sys.handles[handle].next;
1652711890bcSjc156560 
1653711890bcSjc156560 	if (handle == 0)
1654711890bcSjc156560 		return (NULL);
1655711890bcSjc156560 
1656711890bcSjc156560 	return (raid_handle_sys.handles[handle].raid_lib);
1657711890bcSjc156560 }
1658711890bcSjc156560 
1659711890bcSjc156560 static int
1660711890bcSjc156560 raid_obj_set_lib(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
1661711890bcSjc156560 	raid_lib_t *raid_lib)
1662711890bcSjc156560 {
1663711890bcSjc156560 	raid_obj_handle_t handle;
1664711890bcSjc156560 	controller_attr_t *attr;
1665711890bcSjc156560 
1666711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
1667711890bcSjc156560 		return (ERR_DEVICE_TYPE);
1668711890bcSjc156560 
1669711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1670711890bcSjc156560 	handle = raid_handle_sys.used;
1671711890bcSjc156560 	while (raid_handle_sys.handles[handle].type != OBJ_TYPE_CONTROLLER ||
1672711890bcSjc156560 	    raid_handle_sys.handles[handle].controller_id !=
1673711890bcSjc156560 	    attr->controller_id)
1674711890bcSjc156560 		handle = raid_handle_sys.handles[handle].next;
1675711890bcSjc156560 
1676711890bcSjc156560 	if (handle == 0)
1677711890bcSjc156560 		return (ERR_DEVICE_NOENT);
1678711890bcSjc156560 
1679711890bcSjc156560 	raid_handle_sys.handles[handle].raid_lib = raid_lib;
1680711890bcSjc156560 	return (SUCCESS);
1681711890bcSjc156560 }
1682711890bcSjc156560 
1683711890bcSjc156560 static int
1684711890bcSjc156560 raid_obj_get_fd(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
1685711890bcSjc156560 {
1686711890bcSjc156560 	raid_obj_handle_t handle;
1687711890bcSjc156560 	controller_attr_t *attr;
1688711890bcSjc156560 
1689711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
1690711890bcSjc156560 		return (ERR_DEVICE_TYPE);
1691711890bcSjc156560 
1692711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1693711890bcSjc156560 	handle = raid_handle_sys.used;
1694711890bcSjc156560 	while (raid_handle_sys.handles[handle].type != OBJ_TYPE_CONTROLLER ||
1695711890bcSjc156560 	    raid_handle_sys.handles[handle].controller_id !=
1696711890bcSjc156560 	    attr->controller_id)
1697711890bcSjc156560 		handle = raid_handle_sys.handles[handle].next;
1698711890bcSjc156560 
1699711890bcSjc156560 	if (handle == 0)
1700711890bcSjc156560 		return (ERR_DEVICE_NOENT);
1701711890bcSjc156560 
1702711890bcSjc156560 	return (raid_handle_sys.handles[handle].fd);
1703711890bcSjc156560 }
1704711890bcSjc156560 
1705711890bcSjc156560 static int
1706711890bcSjc156560 raid_obj_set_fd(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id, int fd)
1707711890bcSjc156560 {
1708711890bcSjc156560 	raid_obj_handle_t handle;
1709711890bcSjc156560 	controller_attr_t *attr;
1710711890bcSjc156560 
1711711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
1712711890bcSjc156560 		return (ERR_DEVICE_TYPE);
1713711890bcSjc156560 
1714711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1715711890bcSjc156560 	handle = raid_handle_sys.used;
1716711890bcSjc156560 	while (raid_handle_sys.handles[handle].type != OBJ_TYPE_CONTROLLER ||
1717711890bcSjc156560 	    raid_handle_sys.handles[handle].controller_id !=
1718711890bcSjc156560 	    attr->controller_id)
1719711890bcSjc156560 		handle = raid_handle_sys.handles[handle].next;
1720711890bcSjc156560 
1721711890bcSjc156560 	if (handle == 0)
1722711890bcSjc156560 		return (ERR_DEVICE_NOENT);
1723711890bcSjc156560 
1724711890bcSjc156560 	raid_handle_sys.handles[handle].fd = fd;
1725711890bcSjc156560 	return (SUCCESS);
1726711890bcSjc156560 }
1727711890bcSjc156560 
1728711890bcSjc156560 /*
1729711890bcSjc156560  * Raid object maintenance routines
1730711890bcSjc156560  */
1731711890bcSjc156560 static int
1732711890bcSjc156560 obj_scan_comp(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
1733711890bcSjc156560 {
1734711890bcSjc156560 	raid_obj_status_t status;
1735711890bcSjc156560 	raid_obj_type_id_t type;
1736711890bcSjc156560 	int ret, i, obj_type_cnt, comp_num;
1737711890bcSjc156560 	raid_obj_id_t *comp_list;
1738711890bcSjc156560 
1739711890bcSjc156560 	status = raid_obj_get_status(raid_tab, obj_id);
1740711890bcSjc156560 	if (status < SUCCESS)
1741711890bcSjc156560 		return (status);
1742711890bcSjc156560 
1743711890bcSjc156560 	if (status & OBJ_STATUS_SCANCOMP)
1744711890bcSjc156560 		return (SUCCESS);
1745711890bcSjc156560 
1746711890bcSjc156560 	type = raid_obj_get_type(raid_tab, obj_id);
1747edabaf6fSMilan Jurik 	/* type less than OBJ_TYPE_SYSTEM means error */
1748edabaf6fSMilan Jurik 	if (type < OBJ_TYPE_SYSTEM)
1749711890bcSjc156560 		return (ERR_DEVICE_INVALID);
1750711890bcSjc156560 
1751711890bcSjc156560 	for (obj_type_cnt = OBJ_SYSTEM; obj_type_cnt < OBJ_TYPE_ALL;
1752711890bcSjc156560 	    ++obj_type_cnt) {
1753711890bcSjc156560 		if (raid_obj_op_sys[type].compnum != NULL)
1754711890bcSjc156560 			comp_num = raid_obj_op_sys[type].compnum(
1755711890bcSjc156560 			    raid_tab, obj_id, obj_type_cnt);
1756711890bcSjc156560 		else
1757711890bcSjc156560 			comp_num = 0;
1758711890bcSjc156560 
1759711890bcSjc156560 		if (comp_num < SUCCESS)
1760711890bcSjc156560 			return (comp_num);
1761711890bcSjc156560 		if (comp_num == 0)
1762711890bcSjc156560 			continue;
1763711890bcSjc156560 
1764711890bcSjc156560 		comp_list = calloc(comp_num, sizeof (raid_obj_id_t));
1765711890bcSjc156560 		if (comp_list == NULL)
1766711890bcSjc156560 			return (ERR_NOMEM);
1767711890bcSjc156560 
1768711890bcSjc156560 		for (i = 0; i < comp_num; ++i) {
1769711890bcSjc156560 			*(comp_list + i) = raid_obj_create(raid_tab,
1770711890bcSjc156560 			    obj_type_cnt);
1771711890bcSjc156560 			if (*(comp_list + i) < SUCCESS) {
1772711890bcSjc156560 				ret = *(comp_list + i);
1773711890bcSjc156560 				free(comp_list);
1774711890bcSjc156560 				return (ret);
1775711890bcSjc156560 			}
1776711890bcSjc156560 
1777711890bcSjc156560 			(void) raid_obj_clear_status(raid_tab,
1778711890bcSjc156560 			    *(comp_list + i), OBJ_STATUS_CMD_CLEAN);
1779711890bcSjc156560 			(void) raid_obj_add_org(raid_tab, *(comp_list + i),
1780711890bcSjc156560 			    obj_id);
1781711890bcSjc156560 		}
1782711890bcSjc156560 
1783711890bcSjc156560 		if (raid_obj_op_sys[type].complist != NULL)
1784711890bcSjc156560 			raid_obj_op_sys[type].complist(raid_tab,
1785711890bcSjc156560 			    obj_id, comp_num, comp_list, obj_type_cnt);
1786711890bcSjc156560 		free(comp_list);
1787711890bcSjc156560 	}
1788711890bcSjc156560 
1789711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_SCANCOMP);
1790711890bcSjc156560 	return (SUCCESS);
1791711890bcSjc156560 }
1792711890bcSjc156560 
1793711890bcSjc156560 static int
1794711890bcSjc156560 obj_rescan(raid_obj_tab_t *raid_tab)
1795711890bcSjc156560 {
1796711890bcSjc156560 	int ret;
1797711890bcSjc156560 
1798711890bcSjc156560 	raid_obj_tab_destroy(raid_tab);
1799711890bcSjc156560 
1800711890bcSjc156560 	if (raid_obj_tab_create(raid_tab, HASH_SLOTS) != SUCCESS)
1801711890bcSjc156560 		return (ERR_NOMEM);
1802711890bcSjc156560 
1803711890bcSjc156560 	if ((ret = raid_obj_create_system_obj(raid_tab)) != SUCCESS) {
1804711890bcSjc156560 		raid_obj_tab_destroy(raid_tab);
1805711890bcSjc156560 		return (ret);
1806711890bcSjc156560 	}
1807711890bcSjc156560 
1808711890bcSjc156560 	return (SUCCESS);
1809711890bcSjc156560 }
1810711890bcSjc156560 
1811711890bcSjc156560 static raid_obj_id_t
1812711890bcSjc156560 obj_get_comp(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
1813711890bcSjc156560 	raid_obj_type_id_t obj_type)
1814711890bcSjc156560 {
1815711890bcSjc156560 	raid_obj_id_t id;
1816711890bcSjc156560 	raid_obj_type_id_t type;
1817711890bcSjc156560 	raid_obj_status_t status;
1818711890bcSjc156560 	int ret;
1819711890bcSjc156560 
1820711890bcSjc156560 	if ((obj_type < OBJ_TYPE_SYSTEM) || (obj_type > OBJ_TYPE_ALL))
1821711890bcSjc156560 		return (ERR_DEVICE_TYPE);
1822711890bcSjc156560 
1823711890bcSjc156560 	status = raid_obj_get_status(raid_tab, obj_id);
1824711890bcSjc156560 	if (status < SUCCESS)
1825711890bcSjc156560 		return (status);
1826711890bcSjc156560 
1827711890bcSjc156560 	if (!(status & OBJ_STATUS_SCANCOMP)) {
1828711890bcSjc156560 		ret = obj_scan_comp(raid_tab, obj_id);
1829711890bcSjc156560 		if (ret < SUCCESS)
1830711890bcSjc156560 			return (ret);
1831711890bcSjc156560 	}
1832711890bcSjc156560 
1833711890bcSjc156560 	id = raid_obj_get_comp(raid_tab, obj_id);
1834711890bcSjc156560 	if (id <= OBJ_NONE)
1835711890bcSjc156560 		return (id);
1836711890bcSjc156560 
1837711890bcSjc156560 	type = raid_obj_get_type(raid_tab, id);
1838711890bcSjc156560 	if (type < OBJ_TYPE_SYSTEM)
1839711890bcSjc156560 		return (type);
1840711890bcSjc156560 
1841711890bcSjc156560 	if (type == obj_type)
1842711890bcSjc156560 		return (id);
1843711890bcSjc156560 
1844711890bcSjc156560 	while (id > OBJ_NONE) {
1845711890bcSjc156560 		id = raid_obj_get_sibling(raid_tab, id);
1846711890bcSjc156560 		if (id <= OBJ_NONE)
1847711890bcSjc156560 			return (id);
1848711890bcSjc156560 
1849711890bcSjc156560 		type = raid_obj_get_type(raid_tab, id);
1850711890bcSjc156560 		if (type < OBJ_TYPE_SYSTEM)
1851711890bcSjc156560 			return (type);
1852711890bcSjc156560 
1853711890bcSjc156560 		if (type == obj_type)
1854711890bcSjc156560 			break;
1855711890bcSjc156560 	};
1856711890bcSjc156560 
1857711890bcSjc156560 	return (id);
1858711890bcSjc156560 }
1859711890bcSjc156560 
1860711890bcSjc156560 static raid_obj_id_t
1861711890bcSjc156560 obj_get_sibling(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
1862711890bcSjc156560 {
1863711890bcSjc156560 	raid_obj_id_t id;
1864711890bcSjc156560 	raid_obj_type_id_t type, obj_type;
1865711890bcSjc156560 
1866711890bcSjc156560 	id = obj_id;
1867711890bcSjc156560 	obj_type = raid_obj_get_type(raid_tab, id);
1868711890bcSjc156560 	if (obj_type < OBJ_TYPE_SYSTEM)
1869711890bcSjc156560 		return (obj_type);
1870711890bcSjc156560 
1871711890bcSjc156560 	do {
1872711890bcSjc156560 		id = raid_obj_get_sibling(raid_tab, id);
1873711890bcSjc156560 		if (id < OBJ_NONE)
1874711890bcSjc156560 			return (id);
1875711890bcSjc156560 
1876711890bcSjc156560 		type = raid_obj_get_type(raid_tab, id);
1877711890bcSjc156560 		if (type < OBJ_TYPE_SYSTEM)
1878711890bcSjc156560 			return (type);
1879711890bcSjc156560 	} while ((type != obj_type) && (id != OBJ_NONE));
1880711890bcSjc156560 
1881711890bcSjc156560 	return (id);
1882711890bcSjc156560 }
1883711890bcSjc156560 
1884711890bcSjc156560 static int
1885711890bcSjc156560 obj_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id, void **data)
1886711890bcSjc156560 {
1887711890bcSjc156560 	raid_obj_type_id_t type;
1888711890bcSjc156560 	raid_obj_status_t status;
1889711890bcSjc156560 	void *attr;
1890711890bcSjc156560 	int ret = SUCCESS;
1891711890bcSjc156560 
1892711890bcSjc156560 	status = raid_obj_get_status(raid_tab, obj_id);
1893711890bcSjc156560 	if (status < SUCCESS)
1894711890bcSjc156560 		return (status);
1895711890bcSjc156560 
1896711890bcSjc156560 	type = raid_obj_get_type(raid_tab, obj_id);
1897711890bcSjc156560 	if (type < OBJ_TYPE_SYSTEM)
1898711890bcSjc156560 		return (type);
1899711890bcSjc156560 
1900711890bcSjc156560 	if (!(status & OBJ_STATUS_OPENED)) {
1901711890bcSjc156560 		if (raid_obj_op_sys[type].get_attr == NULL)
1902711890bcSjc156560 			(void) raid_obj_set_status(raid_tab, obj_id,
1903711890bcSjc156560 			    OBJ_STATUS_OPENED);
1904711890bcSjc156560 		else
1905711890bcSjc156560 			ret = raid_obj_op_sys[type].get_attr(raid_tab, obj_id);
1906711890bcSjc156560 	}
1907711890bcSjc156560 	if (ret < SUCCESS)
1908711890bcSjc156560 		return (ret);
1909711890bcSjc156560 
1910711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1911711890bcSjc156560 	if (attr == NULL && type != OBJ_TYPE_SYSTEM)
1912711890bcSjc156560 		return (ERR_DEVICE_INVALID);
1913711890bcSjc156560 
1914711890bcSjc156560 	*data = attr;
1915711890bcSjc156560 	return (SUCCESS);
1916711890bcSjc156560 }
1917711890bcSjc156560 
1918711890bcSjc156560 static raid_obj_id_t
1919711890bcSjc156560 obj_locate_controller(raid_obj_tab_t *raid_tab, uint32_t controller_id)
1920711890bcSjc156560 {
1921711890bcSjc156560 	raid_obj_id_t obj_id;
1922711890bcSjc156560 	controller_attr_t *attr;
1923711890bcSjc156560 
1924711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, OBJ_SYSTEM, OBJ_TYPE_CONTROLLER);
1925711890bcSjc156560 	if (obj_id <= OBJ_NONE)
1926711890bcSjc156560 		return (obj_id);
1927711890bcSjc156560 
1928711890bcSjc156560 	do {
1929711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1930711890bcSjc156560 		if (attr == NULL)
1931711890bcSjc156560 			return (ERR_DEVICE_INVALID);
1932711890bcSjc156560 
1933711890bcSjc156560 		if (attr->controller_id == controller_id)
1934711890bcSjc156560 			break;
1935711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) != OBJ_NONE);
1936711890bcSjc156560 
1937711890bcSjc156560 	return (obj_id);
1938711890bcSjc156560 }
1939711890bcSjc156560 
1940711890bcSjc156560 static raid_obj_id_t
1941711890bcSjc156560 obj_locate_array(raid_obj_tab_t *raid_tab, uint32_t controller_id,
1942711890bcSjc156560 	uint32_t array_id)
1943711890bcSjc156560 {
1944711890bcSjc156560 	raid_obj_id_t obj_id;
1945711890bcSjc156560 
1946711890bcSjc156560 	obj_id = obj_locate_controller(raid_tab, controller_id);
1947711890bcSjc156560 	if (obj_id < OBJ_NONE)
1948711890bcSjc156560 		return (obj_id);
1949711890bcSjc156560 
1950711890bcSjc156560 	obj_id = obj_locate_array_recur(raid_tab, obj_id, array_id);
1951711890bcSjc156560 
1952711890bcSjc156560 	return (obj_id);
1953711890bcSjc156560 }
1954711890bcSjc156560 
1955711890bcSjc156560 static raid_obj_id_t
1956711890bcSjc156560 obj_locate_array_recur(raid_obj_tab_t *raid_tab,
1957711890bcSjc156560 	raid_obj_id_t container_obj_id, uint32_t array_id)
1958711890bcSjc156560 {
1959711890bcSjc156560 	raid_obj_id_t obj_id, ret;
1960711890bcSjc156560 	array_attr_t *attr;
1961711890bcSjc156560 
1962711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, container_obj_id, OBJ_TYPE_ARRAY);
1963711890bcSjc156560 	if (obj_id <= OBJ_NONE)
1964711890bcSjc156560 		return (obj_id);
1965711890bcSjc156560 
1966711890bcSjc156560 	do {
1967711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1968711890bcSjc156560 		if (attr == NULL)
1969711890bcSjc156560 			return (ERR_DEVICE_INVALID);
1970711890bcSjc156560 
1971711890bcSjc156560 		if (attr->array_id == array_id)
1972711890bcSjc156560 			break;
1973711890bcSjc156560 
1974711890bcSjc156560 		ret = obj_locate_array_recur(raid_tab, obj_id, array_id);
1975711890bcSjc156560 		if (ret != OBJ_NONE)
1976711890bcSjc156560 			return (ret);
1977711890bcSjc156560 
1978711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
1979711890bcSjc156560 
1980711890bcSjc156560 	return (obj_id);
1981711890bcSjc156560 }
1982711890bcSjc156560 
1983711890bcSjc156560 static raid_obj_id_t
1984711890bcSjc156560 obj_locate_hsp(raid_obj_tab_t *raid_tab, uint32_t controller_id,
1985711890bcSjc156560 	uint32_t disk_id, uint32_t array_id)
1986711890bcSjc156560 {
1987711890bcSjc156560 	raid_obj_id_t obj_id;
1988711890bcSjc156560 	hsp_attr_t *hsp_attr;
1989711890bcSjc156560 
1990711890bcSjc156560 	obj_id = obj_locate_disk(raid_tab, controller_id, disk_id);
1991711890bcSjc156560 	if (obj_id <= OBJ_NONE)
1992711890bcSjc156560 		return (obj_id);
1993711890bcSjc156560 
1994711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_HSP);
1995711890bcSjc156560 	if (obj_id <= OBJ_NONE)
1996711890bcSjc156560 		return (obj_id);
1997711890bcSjc156560 
1998711890bcSjc156560 	do {
1999711890bcSjc156560 		(void) obj_get_attr(raid_tab, obj_id, (void **)(&hsp_attr));
2000711890bcSjc156560 		if (hsp_attr->associated_id == array_id)
2001711890bcSjc156560 			break;
2002711890bcSjc156560 
2003711890bcSjc156560 		obj_id = obj_get_sibling(raid_tab, obj_id);
2004711890bcSjc156560 		if (obj_id < OBJ_NONE)
2005711890bcSjc156560 			return (obj_id);
2006711890bcSjc156560 	} while (obj_id > OBJ_NONE);
2007711890bcSjc156560 
2008711890bcSjc156560 	return (obj_id);
2009711890bcSjc156560 }
2010711890bcSjc156560 
2011711890bcSjc156560 static raid_obj_id_t
2012711890bcSjc156560 obj_locate_disk(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2013711890bcSjc156560 	uint32_t disk_id)
2014711890bcSjc156560 {
2015711890bcSjc156560 	raid_obj_id_t obj_id;
2016711890bcSjc156560 	disk_attr_t *attr;
2017711890bcSjc156560 
2018711890bcSjc156560 	obj_id = obj_locate_controller(raid_tab, controller_id);
2019711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2020711890bcSjc156560 		return (obj_id);
2021711890bcSjc156560 
2022711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_DISK);
2023711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2024711890bcSjc156560 		return (obj_id);
2025711890bcSjc156560 
2026711890bcSjc156560 	do {
2027711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2028711890bcSjc156560 		if (attr == NULL)
2029711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2030711890bcSjc156560 
2031711890bcSjc156560 		if (attr->disk_id == disk_id)
2032711890bcSjc156560 			break;
2033711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
2034711890bcSjc156560 
2035711890bcSjc156560 	return (obj_id);
2036711890bcSjc156560 }
2037711890bcSjc156560 
2038711890bcSjc156560 static raid_obj_id_t
2039711890bcSjc156560 obj_locate_arraypart(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2040711890bcSjc156560 	uint32_t array_id, uint32_t disk_id)
2041711890bcSjc156560 {
2042711890bcSjc156560 	raid_obj_id_t obj_id;
2043711890bcSjc156560 
2044711890bcSjc156560 	arraypart_attr_t *attr;
2045711890bcSjc156560 
2046711890bcSjc156560 	obj_id = obj_locate_array(raid_tab, controller_id, array_id);
2047711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2048711890bcSjc156560 		return (obj_id);
2049711890bcSjc156560 
2050711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_ARRAY_PART);
2051711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2052711890bcSjc156560 		return (obj_id);
2053711890bcSjc156560 
2054711890bcSjc156560 	do {
2055711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2056711890bcSjc156560 		if (attr == NULL)
2057711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2058711890bcSjc156560 
2059711890bcSjc156560 		if (attr->disk_id == disk_id)
2060711890bcSjc156560 			break;
2061711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) >
2062711890bcSjc156560 	    OBJ_NONE);
2063711890bcSjc156560 
2064711890bcSjc156560 	return (obj_id);
2065711890bcSjc156560 }
2066711890bcSjc156560 
2067711890bcSjc156560 static raid_obj_id_t
2068711890bcSjc156560 obj_locate_diskseg(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2069711890bcSjc156560 	uint32_t disk_id, uint32_t seq_no)
2070711890bcSjc156560 {
2071711890bcSjc156560 	raid_obj_id_t obj_id;
2072711890bcSjc156560 	diskseg_attr_t *attr;
2073711890bcSjc156560 
2074711890bcSjc156560 	obj_id = obj_locate_disk(raid_tab, controller_id, disk_id);
2075711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2076711890bcSjc156560 		return (obj_id);
2077711890bcSjc156560 
2078711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_DISK_SEG);
2079711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2080711890bcSjc156560 		return (obj_id);
2081711890bcSjc156560 
2082711890bcSjc156560 	do {
2083711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2084711890bcSjc156560 		if (attr == NULL)
2085711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2086711890bcSjc156560 
2087711890bcSjc156560 		if (attr->seq_no == seq_no)
2088711890bcSjc156560 			break;
2089711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
2090711890bcSjc156560 
2091711890bcSjc156560 	return (obj_id);
2092711890bcSjc156560 }
2093711890bcSjc156560 
2094711890bcSjc156560 static raid_obj_id_t
2095711890bcSjc156560 obj_locate_task(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2096711890bcSjc156560 	uint32_t task_id)
2097711890bcSjc156560 {
2098711890bcSjc156560 	raid_obj_id_t obj_id, obj_id2, task_obj_id;
2099711890bcSjc156560 	task_attr_t *attr;
2100711890bcSjc156560 
2101711890bcSjc156560 	obj_id = obj_locate_controller(raid_tab, controller_id);
2102711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2103711890bcSjc156560 		return (obj_id);
2104711890bcSjc156560 
2105711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_ARRAY);
2106711890bcSjc156560 	if (obj_id < OBJ_NONE)
2107711890bcSjc156560 		return (obj_id);
2108711890bcSjc156560 
2109711890bcSjc156560 	do {
2110711890bcSjc156560 		obj_id2 = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_ARRAY);
2111711890bcSjc156560 		while (obj_id2 != OBJ_NONE) {
2112711890bcSjc156560 			task_obj_id = obj_get_comp(raid_tab, obj_id2,
2113711890bcSjc156560 			    OBJ_TYPE_TASK);
2114711890bcSjc156560 
2115711890bcSjc156560 			if (task_obj_id < OBJ_NONE)
2116711890bcSjc156560 				return (task_obj_id);
2117711890bcSjc156560 
2118711890bcSjc156560 			if (task_obj_id == OBJ_NONE) {
2119711890bcSjc156560 				obj_id2 = obj_get_sibling(raid_tab, obj_id2);
2120711890bcSjc156560 				continue;
2121711890bcSjc156560 			}
2122711890bcSjc156560 
2123711890bcSjc156560 			attr = raid_obj_get_data_ptr(raid_tab, task_obj_id);
2124711890bcSjc156560 			if (attr == NULL)
2125711890bcSjc156560 				return (ERR_DEVICE_INVALID);
2126711890bcSjc156560 
2127711890bcSjc156560 			if (attr->task_id == task_id)
2128711890bcSjc156560 				return (task_obj_id);
2129711890bcSjc156560 
2130711890bcSjc156560 			obj_id2 = obj_get_sibling(raid_tab, obj_id2);
2131711890bcSjc156560 		}
2132711890bcSjc156560 
2133711890bcSjc156560 		task_obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_TASK);
2134711890bcSjc156560 		if (task_obj_id < OBJ_NONE)
2135711890bcSjc156560 			return (task_obj_id);
2136711890bcSjc156560 
2137711890bcSjc156560 		if (task_obj_id == OBJ_NONE)
2138711890bcSjc156560 			continue;
2139711890bcSjc156560 
2140711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, task_obj_id);
2141711890bcSjc156560 		if (attr == NULL)
2142711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2143711890bcSjc156560 
2144711890bcSjc156560 		if (attr->task_id == task_id)
2145711890bcSjc156560 			return (task_obj_id);
2146711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
2147711890bcSjc156560 
2148711890bcSjc156560 	if (obj_id < OBJ_NONE)
2149711890bcSjc156560 		return (obj_id);
2150711890bcSjc156560 
2151711890bcSjc156560 	obj_id = obj_locate_controller(raid_tab, controller_id);
2152711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_DISK);
2153711890bcSjc156560 	if (obj_id < OBJ_NONE)
2154711890bcSjc156560 		return (obj_id);
2155711890bcSjc156560 
2156711890bcSjc156560 	do {
2157711890bcSjc156560 		task_obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_TASK);
2158711890bcSjc156560 		if (task_obj_id < OBJ_NONE)
2159711890bcSjc156560 			return (task_obj_id);
2160711890bcSjc156560 
2161711890bcSjc156560 		if (task_obj_id == OBJ_NONE)
2162711890bcSjc156560 			continue;
2163711890bcSjc156560 
2164711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, task_obj_id);
2165711890bcSjc156560 		if (attr == NULL)
2166711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2167711890bcSjc156560 
2168711890bcSjc156560 		if (attr->task_id == task_id)
2169711890bcSjc156560 			return (task_obj_id);
2170711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
2171711890bcSjc156560 
2172711890bcSjc156560 	return (obj_id);
2173b449fa8aSyw161884 }
2174711890bcSjc156560 
2175b449fa8aSyw161884 static raid_obj_id_t
2176b449fa8aSyw161884 obj_locate_prop(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2177b449fa8aSyw161884 	uint32_t disk_id, uint32_t prop_id)
2178b449fa8aSyw161884 {
2179b449fa8aSyw161884 	raid_obj_id_t obj_id;
2180b449fa8aSyw161884 	property_attr_t *prop_attr;
2181b449fa8aSyw161884 
2182b449fa8aSyw161884 	obj_id = obj_locate_disk(raid_tab, controller_id, disk_id);
2183b449fa8aSyw161884 	if (obj_id < OBJ_NONE)
2184b449fa8aSyw161884 		return (obj_id);
2185b449fa8aSyw161884 
2186b449fa8aSyw161884 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_PROP);
2187b449fa8aSyw161884 	if (obj_id <= OBJ_NONE)
2188b449fa8aSyw161884 		return (obj_id);
2189b449fa8aSyw161884 
2190b449fa8aSyw161884 	do {
2191b449fa8aSyw161884 		(void) obj_get_attr(raid_tab, obj_id, (void **)(&prop_attr));
2192b449fa8aSyw161884 		if (prop_attr->prop_id == prop_id)
2193b449fa8aSyw161884 			break;
2194b449fa8aSyw161884 
2195b449fa8aSyw161884 		obj_id = obj_get_sibling(raid_tab, obj_id);
2196b449fa8aSyw161884 		if (obj_id < OBJ_NONE)
2197b449fa8aSyw161884 			return (obj_id);
2198b449fa8aSyw161884 	} while (obj_id > OBJ_NONE);
2199b449fa8aSyw161884 
2200b449fa8aSyw161884 	return (obj_id);
2201711890bcSjc156560 }
2202711890bcSjc156560 
2203711890bcSjc156560 static raid_obj_id_t
2204711890bcSjc156560 obj_get_controller(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2205711890bcSjc156560 {
2206711890bcSjc156560 	raid_obj_id_t id = obj_id;
2207711890bcSjc156560 
2208711890bcSjc156560 	while (raid_obj_get_type(raid_tab, id) != OBJ_TYPE_CONTROLLER) {
2209711890bcSjc156560 		id = raid_obj_get_container(raid_tab, id);
2210711890bcSjc156560 		if ((id == OBJ_SYSTEM) || (id < OBJ_NONE))
2211711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2212711890bcSjc156560 	}
2213711890bcSjc156560 
2214711890bcSjc156560 	return (id);
2215711890bcSjc156560 }
2216711890bcSjc156560 
2217711890bcSjc156560 /*
2218711890bcSjc156560  * Raid object operation routines
2219711890bcSjc156560  */
2220711890bcSjc156560 static int
2221711890bcSjc156560 obj_sys_compnum(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2222711890bcSjc156560 	raid_obj_type_id_t comp_type)
2223711890bcSjc156560 {
2224711890bcSjc156560 	DIR *dir;
2225711890bcSjc156560 	struct dirent *dp;
2226711890bcSjc156560 	int num = 0;
2227711890bcSjc156560 
2228711890bcSjc156560 	if ((raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_SYSTEM))
2229711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2230711890bcSjc156560 
2231711890bcSjc156560 	if (comp_type != OBJ_TYPE_CONTROLLER)
2232711890bcSjc156560 		return (0);
2233711890bcSjc156560 
2234711890bcSjc156560 	if ((dir = opendir(CFGDIR)) == NULL)
2235711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
2236711890bcSjc156560 
2237711890bcSjc156560 	while ((dp = readdir(dir)) != NULL) {
2238711890bcSjc156560 		uint32_t controller_id;
2239711890bcSjc156560 		char path[MAX_PATH_LEN];
2240711890bcSjc156560 
2241711890bcSjc156560 		if (strcmp(dp->d_name, ".") == 0 ||
2242711890bcSjc156560 		    strcmp(dp->d_name, "..") == 0)
2243711890bcSjc156560 			continue;
2244711890bcSjc156560 
2245711890bcSjc156560 		if (sscanf(dp->d_name, "c%u", &controller_id) != 1)
2246711890bcSjc156560 			continue;
2247711890bcSjc156560 
2248711890bcSjc156560 		if (controller_id_to_path(controller_id, path) == SUCCESS)
2249711890bcSjc156560 			++ num;
2250711890bcSjc156560 	}
2251711890bcSjc156560 
2252711890bcSjc156560 	(void) closedir(dir);
2253711890bcSjc156560 	return (num);
2254711890bcSjc156560 }
2255711890bcSjc156560 
2256711890bcSjc156560 static int
2257711890bcSjc156560 obj_sys_complist(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2258711890bcSjc156560 	int num, raid_obj_id_t *comp_list, raid_obj_type_id_t comp_type)
2259711890bcSjc156560 {
2260711890bcSjc156560 	DIR *dir;
2261711890bcSjc156560 	struct dirent *dp;
2262711890bcSjc156560 	controller_attr_t *attr;
2263711890bcSjc156560 	uint32_t controller_id;
2264711890bcSjc156560 	uint32_t *tmplist;
2265711890bcSjc156560 	char path[MAX_PATH_LEN];
2266711890bcSjc156560 	int i = 0;
2267711890bcSjc156560 
2268711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_SYSTEM)
2269711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2270711890bcSjc156560 	if ((num <= 0) || (comp_list == NULL))
2271711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2272711890bcSjc156560 
2273711890bcSjc156560 	if (comp_type != OBJ_TYPE_CONTROLLER)
2274711890bcSjc156560 		return (0);
2275711890bcSjc156560 
2276711890bcSjc156560 	if ((dir = opendir(CFGDIR)) == NULL)
2277711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
2278711890bcSjc156560 	tmplist = calloc(num, sizeof (uint32_t));
2279711890bcSjc156560 	if (tmplist == NULL) {
2280711890bcSjc156560 		return (ERR_NOMEM);
2281711890bcSjc156560 	}
2282711890bcSjc156560 	while ((dp = readdir(dir)) != NULL) {
2283711890bcSjc156560 		if (strcmp(dp->d_name, ".") == 0 ||
2284711890bcSjc156560 		    strcmp(dp->d_name, "..") == 0)
2285711890bcSjc156560 			continue;
2286711890bcSjc156560 
2287711890bcSjc156560 		if (sscanf(dp->d_name, "c%u", &controller_id) != 1)
2288711890bcSjc156560 			continue;
2289711890bcSjc156560 
2290711890bcSjc156560 		if (controller_id_to_path(controller_id, path) == SUCCESS) {
2291711890bcSjc156560 			tmplist[i] = controller_id;
2292711890bcSjc156560 			++ i;
2293711890bcSjc156560 		}
2294711890bcSjc156560 	}
2295711890bcSjc156560 	qsort((void *)tmplist, num, sizeof (uint32_t), intcompare);
2296711890bcSjc156560 	for (i = 0; i < num; i++) {
2297711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab,
2298711890bcSjc156560 		    *(comp_list + i));
2299711890bcSjc156560 
2300711890bcSjc156560 		if (attr == NULL) {
2301711890bcSjc156560 			free(tmplist);
2302711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2303711890bcSjc156560 		}
2304711890bcSjc156560 
2305711890bcSjc156560 		attr->controller_id = tmplist[i];
2306711890bcSjc156560 	}
2307711890bcSjc156560 	free(tmplist);
2308711890bcSjc156560 	(void) closedir(dir);
2309711890bcSjc156560 	return (SUCCESS);
2310711890bcSjc156560 }
2311711890bcSjc156560 
2312711890bcSjc156560 static int
2313711890bcSjc156560 obj_controller_compnum(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2314711890bcSjc156560 	raid_obj_type_id_t comp_type)
2315711890bcSjc156560 {
2316711890bcSjc156560 	raid_lib_t *raid_lib;
2317711890bcSjc156560 	int ret = SUCCESS, fd;
2318711890bcSjc156560 	controller_attr_t *ctl_attrp;
2319711890bcSjc156560 
2320711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
2321711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2322711890bcSjc156560 
2323711890bcSjc156560 	if ((comp_type != OBJ_TYPE_ARRAY) && (comp_type != OBJ_TYPE_DISK))
2324711890bcSjc156560 		return (0);
2325711890bcSjc156560 
2326711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, obj_id);
2327711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, obj_id);
2328711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, obj_id);
2329711890bcSjc156560 	if ((raid_lib == NULL) || (ctl_attrp == NULL) || (fd == 0))
2330711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2331711890bcSjc156560 
2332711890bcSjc156560 	ret = raid_lib->compnum(ctl_attrp->controller_id, 0,
2333711890bcSjc156560 	    OBJ_TYPE_CONTROLLER, comp_type);
2334711890bcSjc156560 
2335711890bcSjc156560 	return (ret);
2336711890bcSjc156560 }
2337711890bcSjc156560 
2338711890bcSjc156560 static int
2339711890bcSjc156560 obj_controller_complist(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2340711890bcSjc156560 	int comp_num, raid_obj_id_t *comp_list, raid_obj_type_id_t comp_type)
2341711890bcSjc156560 {
2342711890bcSjc156560 	raid_lib_t *raid_lib;
2343711890bcSjc156560 	controller_attr_t *ctl_attrp;
2344711890bcSjc156560 	int ret, i, fd;
2345711890bcSjc156560 	uint32_t *ids;
2346711890bcSjc156560 
2347711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
2348711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2349711890bcSjc156560 
2350711890bcSjc156560 	if ((comp_type != OBJ_TYPE_ARRAY) && (comp_type != OBJ_TYPE_DISK))
2351711890bcSjc156560 		return (0);
2352711890bcSjc156560 
2353711890bcSjc156560 	if ((comp_num <= 0) || (comp_list == NULL))
2354711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2355711890bcSjc156560 
2356711890bcSjc156560 	for (i = 0; i < comp_num; ++i)
2357711890bcSjc156560 		if (raid_obj_get_type(raid_tab, *(comp_list + i)) !=
2358711890bcSjc156560 		    comp_type)
2359711890bcSjc156560 			return (ERR_DEVICE_TYPE);
2360711890bcSjc156560 
2361711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, obj_id);
2362711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, obj_id);
2363711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, obj_id);
2364711890bcSjc156560 	if ((raid_lib == NULL) || (ctl_attrp == NULL)|| (fd == 0))
2365711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2366711890bcSjc156560 
2367711890bcSjc156560 	ids = malloc(comp_num * sizeof (uint32_t));
2368711890bcSjc156560 	if (ids == NULL)
2369711890bcSjc156560 		return (ERR_NOMEM);
2370711890bcSjc156560 
2371711890bcSjc156560 	ret = raid_lib->complist(ctl_attrp->controller_id, 0,
2372711890bcSjc156560 	    OBJ_TYPE_CONTROLLER, comp_type, comp_num, ids);
2373711890bcSjc156560 	if (ret < SUCCESS) {
2374711890bcSjc156560 		free(ids);
2375711890bcSjc156560 		return (ret);
2376711890bcSjc156560 	}
2377711890bcSjc156560 	qsort((void *)ids, comp_num, sizeof (uint32_t), intcompare);
2378711890bcSjc156560 	for (i = 0; i < comp_num; ++ i) {
2379711890bcSjc156560 		array_attr_t *array_attr;
2380711890bcSjc156560 		disk_attr_t *disk_attr;
2381711890bcSjc156560 		void *attr_buf;
2382711890bcSjc156560 
2383711890bcSjc156560 		attr_buf = raid_obj_get_data_ptr(raid_tab, *(comp_list + i));
2384711890bcSjc156560 		if (attr_buf == NULL) {
2385711890bcSjc156560 			free(ids);
2386711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2387711890bcSjc156560 		}
2388711890bcSjc156560 
2389711890bcSjc156560 		switch (comp_type) {
2390711890bcSjc156560 		case OBJ_TYPE_ARRAY:
2391711890bcSjc156560 			array_attr = attr_buf;
2392711890bcSjc156560 			array_attr->array_id = *(ids + i);
2393711890bcSjc156560 			break;
2394711890bcSjc156560 		case OBJ_TYPE_DISK:
2395711890bcSjc156560 			disk_attr = attr_buf;
2396711890bcSjc156560 			disk_attr->disk_id = *(ids + i);
2397711890bcSjc156560 			break;
2398711890bcSjc156560 		default:
2399711890bcSjc156560 			free(ids);
2400711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2401711890bcSjc156560 		}
2402711890bcSjc156560 	}
2403711890bcSjc156560 
2404711890bcSjc156560 	free(ids);
2405711890bcSjc156560 	return (SUCCESS);
2406711890bcSjc156560 }
2407711890bcSjc156560 
2408711890bcSjc156560 static int
2409711890bcSjc156560 obj_controller_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2410711890bcSjc156560 {
2411711890bcSjc156560 	controller_attr_t *attr;
2412711890bcSjc156560 	raid_lib_t *raid_lib;
2413711890bcSjc156560 	int ret = SUCCESS, fd;
2414711890bcSjc156560 
2415711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
2416711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2417711890bcSjc156560 
2418711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
2419711890bcSjc156560 		return (SUCCESS);
2420711890bcSjc156560 
2421711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2422711890bcSjc156560 	if (attr == NULL)
2423711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2424711890bcSjc156560 
2425711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, obj_id);
2426711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, obj_id);
2427711890bcSjc156560 
2428711890bcSjc156560 	/*
2429711890bcSjc156560 	 * For a controller, even it's not opened, we can still
2430711890bcSjc156560 	 * get the driver name
2431711890bcSjc156560 	 */
2432711890bcSjc156560 
2433711890bcSjc156560 	if (fd == 0)
2434711890bcSjc156560 		return (SUCCESS);
2435711890bcSjc156560 
2436711890bcSjc156560 	if (raid_lib == NULL) {
2437711890bcSjc156560 		return (SUCCESS);
2438711890bcSjc156560 	}
2439711890bcSjc156560 
2440711890bcSjc156560 	ret = raid_lib->get_attr(attr->controller_id, OBJ_ATTR_NONE,
2441711890bcSjc156560 	    OBJ_ATTR_NONE, OBJ_TYPE_CONTROLLER, attr);
2442711890bcSjc156560 	if (ret < SUCCESS)
2443711890bcSjc156560 		return (ret);
2444711890bcSjc156560 
2445711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
2446711890bcSjc156560 
2447711890bcSjc156560 	return (ret);
2448711890bcSjc156560 }
2449711890bcSjc156560 
2450711890bcSjc156560 static int
2451711890bcSjc156560 obj_controller_act(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2452711890bcSjc156560 	uint32_t sub_cmd, void *prop_list, char **plugin_err_str)
2453711890bcSjc156560 {
2454711890bcSjc156560 	controller_attr_t *attr;
2455711890bcSjc156560 	raid_lib_t *raid_lib;
2456711890bcSjc156560 	int ret, fd;
2457711890bcSjc156560 
2458711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
2459711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2460711890bcSjc156560 
2461711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2462711890bcSjc156560 
2463711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, obj_id);
2464711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, obj_id);
2465711890bcSjc156560 
2466711890bcSjc156560 	switch (sub_cmd) {
2467711890bcSjc156560 	case ACT_CONTROLLER_OPEN:
2468711890bcSjc156560 		/* Check if already opened */
2469711890bcSjc156560 
2470711890bcSjc156560 		if (fd > 0)
2471711890bcSjc156560 			return (SUCCESS);
2472711890bcSjc156560 
2473711890bcSjc156560 		/* Check if plugin is already attached */
2474711890bcSjc156560 		if (raid_lib == NULL) {
2475711890bcSjc156560 			raid_lib = raid_find_lib(raid_tab, obj_id);
2476711890bcSjc156560 			if (raid_lib == NULL)
2477711890bcSjc156560 				return (ERR_DRIVER_NOT_FOUND);
2478711890bcSjc156560 		}
2479711890bcSjc156560 
2480711890bcSjc156560 		ret = raid_lib->open_controller(attr->controller_id,
2481711890bcSjc156560 		    plugin_err_str);
2482711890bcSjc156560 		if (ret == SUCCESS) {
2483711890bcSjc156560 			(void) raid_obj_set_lib(raid_tab, obj_id, raid_lib);
2484711890bcSjc156560 			(void) raid_obj_set_fd(raid_tab, obj_id, 1);
2485711890bcSjc156560 		}
2486711890bcSjc156560 		break;
2487711890bcSjc156560 	case ACT_CONTROLLER_CLOSE:
2488711890bcSjc156560 
2489711890bcSjc156560 		if (fd <= 0)
2490711890bcSjc156560 			return (SUCCESS);
2491711890bcSjc156560 
2492711890bcSjc156560 		if (raid_lib == NULL) {
2493711890bcSjc156560 			return (SUCCESS);
2494711890bcSjc156560 		}
2495711890bcSjc156560 		ret = raid_lib->close_controller(attr->controller_id,
2496711890bcSjc156560 		    plugin_err_str);
2497711890bcSjc156560 		if (ret == SUCCESS) {
2498711890bcSjc156560 			(void) raid_obj_set_fd(raid_tab, obj_id, 0);
2499711890bcSjc156560 			(void) raid_obj_set_lib(raid_tab, obj_id, NULL);
2500711890bcSjc156560 			raid_handle_delete_controller_comp(attr->controller_id);
2501711890bcSjc156560 		}
2502711890bcSjc156560 		break;
2503711890bcSjc156560 	case ACT_CONTROLLER_FLASH_FW:
2504711890bcSjc156560 		{
2505711890bcSjc156560 			char		*filebuf;
2506711890bcSjc156560 			int		image_fd;
2507711890bcSjc156560 			uint32_t	size;
2508711890bcSjc156560 			struct stat	statbuf;
2509711890bcSjc156560 
2510711890bcSjc156560 			if (prop_list == NULL)
2511711890bcSjc156560 				return (ERR_OP_ILLEGAL);
2512711890bcSjc156560 
2513711890bcSjc156560 			/* Open firmware image file */
2514711890bcSjc156560 			image_fd = open((const char *)prop_list,
2515711890bcSjc156560 			    O_RDONLY | O_NDELAY);
2516711890bcSjc156560 			if (image_fd == -1)
2517711890bcSjc156560 				return (ERR_OP_FAILED);
2518711890bcSjc156560 
2519711890bcSjc156560 			if (fstat(image_fd, &statbuf) != 0) {
2520711890bcSjc156560 				(void) close(image_fd);
2521711890bcSjc156560 				return (ERR_OP_FAILED);
2522711890bcSjc156560 			}
2523711890bcSjc156560 
2524711890bcSjc156560 			filebuf = malloc(statbuf.st_size);
2525711890bcSjc156560 			if (filebuf == NULL) {
2526711890bcSjc156560 				(void) close(image_fd);
2527711890bcSjc156560 				return (ERR_NOMEM);
2528711890bcSjc156560 			}
2529711890bcSjc156560 
2530711890bcSjc156560 			size = read(image_fd, filebuf, statbuf.st_size);
2531711890bcSjc156560 			if (size != statbuf.st_size) {
2532711890bcSjc156560 				(void) close(image_fd);
2533711890bcSjc156560 				free(filebuf);
2534711890bcSjc156560 				return (ERR_OP_FAILED);
2535711890bcSjc156560 			}
2536711890bcSjc156560 
2537711890bcSjc156560 			if (fd <= 0) {
2538711890bcSjc156560 				(void) close(image_fd);
2539711890bcSjc156560 				free(filebuf);
2540711890bcSjc156560 				return (ERR_DRIVER_CLOSED);
2541711890bcSjc156560 			}
2542711890bcSjc156560 
2543711890bcSjc156560 			if (raid_lib == NULL) {
2544711890bcSjc156560 				(void) close(image_fd);
2545711890bcSjc156560 				free(filebuf);
2546711890bcSjc156560 				return (ERR_DRIVER_CLOSED);
2547711890bcSjc156560 			}
2548711890bcSjc156560 			if (raid_lib->flash_fw == NULL) {
2549711890bcSjc156560 				(void) close(image_fd);
2550711890bcSjc156560 				free(filebuf);
2551711890bcSjc156560 				return (ERR_OP_NO_IMPL);
2552711890bcSjc156560 			}
2553711890bcSjc156560 
2554711890bcSjc156560 			ret = raid_lib->flash_fw(attr->controller_id,
2555711890bcSjc156560 			    filebuf, size, plugin_err_str);
2556711890bcSjc156560 		}
2557711890bcSjc156560 		break;
2558711890bcSjc156560 	default:
2559711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2560711890bcSjc156560 	}
2561711890bcSjc156560 
2562711890bcSjc156560 	return (ret);
2563711890bcSjc156560 }
2564711890bcSjc156560 
2565711890bcSjc156560 static int
2566711890bcSjc156560 obj_array_compnum(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2567711890bcSjc156560 	raid_obj_type_id_t comp_type)
2568711890bcSjc156560 {
2569711890bcSjc156560 	array_attr_t *attr;
2570711890bcSjc156560 	controller_attr_t *ctl_attrp;
2571711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2572711890bcSjc156560 	raid_lib_t *raid_lib;
2573711890bcSjc156560 	int ret = SUCCESS, fd;
2574711890bcSjc156560 
2575711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY)
2576711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2577711890bcSjc156560 
2578711890bcSjc156560 	if (comp_type != OBJ_TYPE_ARRAY_PART &&
2579711890bcSjc156560 	    comp_type != OBJ_TYPE_ARRAY &&
2580711890bcSjc156560 	    comp_type != OBJ_TYPE_TASK)
2581711890bcSjc156560 		return (0);
2582711890bcSjc156560 
2583711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2584711890bcSjc156560 	if (attr == NULL)
2585711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2586711890bcSjc156560 
2587711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2588711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2589711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2590711890bcSjc156560 
2591711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2592711890bcSjc156560 	if (ctl_attrp == NULL) {
2593711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2594711890bcSjc156560 	}
2595711890bcSjc156560 
2596711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2597711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2598711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2599711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2600711890bcSjc156560 
2601711890bcSjc156560 	ret = raid_lib->compnum(ctl_attrp->controller_id, attr->array_id,
2602711890bcSjc156560 	    OBJ_TYPE_ARRAY, comp_type);
2603711890bcSjc156560 
2604711890bcSjc156560 	return (ret);
2605711890bcSjc156560 }
2606711890bcSjc156560 
2607711890bcSjc156560 static int
2608711890bcSjc156560 obj_array_complist(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2609711890bcSjc156560 	int comp_num, raid_obj_id_t *comp_list, raid_obj_type_id_t comp_type)
2610711890bcSjc156560 {
2611711890bcSjc156560 	array_attr_t *attr;
2612711890bcSjc156560 	controller_attr_t *ctl_attrp;
2613711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2614711890bcSjc156560 	raid_lib_t *raid_lib;
2615711890bcSjc156560 	int ret, i, fd;
2616711890bcSjc156560 	uint32_t *ids;
2617711890bcSjc156560 
2618711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY)
2619711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2620711890bcSjc156560 
2621711890bcSjc156560 	if (comp_type != OBJ_TYPE_ARRAY_PART &&
2622711890bcSjc156560 	    comp_type != OBJ_TYPE_ARRAY &&
2623711890bcSjc156560 	    comp_type != OBJ_TYPE_TASK)
2624711890bcSjc156560 		return (0);
2625711890bcSjc156560 
2626711890bcSjc156560 	if (comp_num <= 0 || comp_list == NULL)
2627711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2628711890bcSjc156560 
2629711890bcSjc156560 	for (i = 0; i < comp_num; ++i)
2630711890bcSjc156560 		if (raid_obj_get_type(raid_tab, *(comp_list + i)) !=
2631711890bcSjc156560 		    comp_type)
2632711890bcSjc156560 			return (ERR_DEVICE_TYPE);
2633711890bcSjc156560 
2634711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2635711890bcSjc156560 	if (attr == NULL)
2636711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2637711890bcSjc156560 
2638711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2639711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2640711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2641711890bcSjc156560 
2642711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2643711890bcSjc156560 	if (ctl_attrp == NULL) {
2644711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2645711890bcSjc156560 	}
2646711890bcSjc156560 
2647711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2648711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2649711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2650711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2651711890bcSjc156560 
2652711890bcSjc156560 	ids = malloc(comp_num * sizeof (uint32_t));
2653711890bcSjc156560 	if (ids == NULL)
2654711890bcSjc156560 		return (ERR_NOMEM);
2655711890bcSjc156560 
2656711890bcSjc156560 	ret = raid_lib->complist(ctl_attrp->controller_id,
2657711890bcSjc156560 	    attr->array_id, OBJ_TYPE_ARRAY, comp_type, comp_num, ids);
2658711890bcSjc156560 
2659711890bcSjc156560 	if (ret < SUCCESS) {
2660711890bcSjc156560 		free(ids);
2661711890bcSjc156560 		return (ret);
2662711890bcSjc156560 	}
2663711890bcSjc156560 
2664711890bcSjc156560 	for (i = 0; i < comp_num; ++ i) {
2665711890bcSjc156560 		array_attr_t *array_attr;
2666711890bcSjc156560 		arraypart_attr_t *arraypart_attr;
2667711890bcSjc156560 		task_attr_t *task_attr;
2668711890bcSjc156560 		void *attr_buf;
2669711890bcSjc156560 
2670711890bcSjc156560 		attr_buf = raid_obj_get_data_ptr(raid_tab, *(comp_list + i));
2671711890bcSjc156560 		if (attr_buf == NULL) {
2672711890bcSjc156560 			free(ids);
2673711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2674711890bcSjc156560 		}
2675711890bcSjc156560 
2676711890bcSjc156560 		switch (comp_type) {
2677711890bcSjc156560 		case OBJ_TYPE_ARRAY:
2678711890bcSjc156560 			array_attr = attr_buf;
2679711890bcSjc156560 			array_attr->array_id = *(ids + i);
2680711890bcSjc156560 			break;
2681711890bcSjc156560 		case OBJ_TYPE_ARRAY_PART:
2682711890bcSjc156560 			arraypart_attr = attr_buf;
2683711890bcSjc156560 			arraypart_attr->disk_id = *(ids + i);
2684711890bcSjc156560 			break;
2685711890bcSjc156560 		case OBJ_TYPE_TASK:
2686711890bcSjc156560 			task_attr = attr_buf;
2687711890bcSjc156560 			task_attr->task_id = *(ids + i);
2688711890bcSjc156560 			break;
2689711890bcSjc156560 		default:
2690711890bcSjc156560 			free(ids);
2691711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2692711890bcSjc156560 		}
2693711890bcSjc156560 	}
2694711890bcSjc156560 
2695711890bcSjc156560 
2696711890bcSjc156560 	free(ids);
2697711890bcSjc156560 	return (ret);
2698711890bcSjc156560 }
2699711890bcSjc156560 
2700711890bcSjc156560 static int
2701711890bcSjc156560 obj_array_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2702711890bcSjc156560 {
2703711890bcSjc156560 	array_attr_t *attr;
2704711890bcSjc156560 	controller_attr_t *ctl_attrp;
2705711890bcSjc156560 	raid_lib_t *raid_lib;
2706711890bcSjc156560 	int ret = SUCCESS, fd;
2707711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2708711890bcSjc156560 
2709711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY)
2710711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2711711890bcSjc156560 
2712711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
2713711890bcSjc156560 		return (SUCCESS);
2714711890bcSjc156560 
2715711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2716711890bcSjc156560 	if (attr == NULL)
2717711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2718711890bcSjc156560 
2719711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2720711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2721711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2722711890bcSjc156560 
2723711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2724711890bcSjc156560 	if (ctl_attrp == NULL) {
2725711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2726711890bcSjc156560 	}
2727711890bcSjc156560 
2728711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2729711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2730711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2731711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2732711890bcSjc156560 
2733711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
2734711890bcSjc156560 	    attr->array_id, 0, OBJ_TYPE_ARRAY, attr);
2735711890bcSjc156560 
2736711890bcSjc156560 	if (ret < SUCCESS)
2737711890bcSjc156560 		return (ret);
2738711890bcSjc156560 
2739711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
2740711890bcSjc156560 
2741711890bcSjc156560 	return (ret);
2742711890bcSjc156560 }
2743711890bcSjc156560 
2744711890bcSjc156560 static int
2745711890bcSjc156560 obj_array_set_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2746711890bcSjc156560 	uint32_t sub_cmd, uint32_t *value, char **plugin_err_str)
2747711890bcSjc156560 {
2748711890bcSjc156560 	array_attr_t *attr;
2749711890bcSjc156560 	controller_attr_t *ctl_attrp;
2750711890bcSjc156560 	raid_lib_t *raid_lib;
2751711890bcSjc156560 	int ret = SUCCESS, fd;
2752711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2753711890bcSjc156560 
2754711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY)
2755711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2756711890bcSjc156560 
2757711890bcSjc156560 	switch (sub_cmd) {
2758711890bcSjc156560 	case SET_CACHE_WR_PLY:
2759711890bcSjc156560 		if (*value != CACHE_WR_OFF &&
2760711890bcSjc156560 		    *value != CACHE_WR_ON)
2761711890bcSjc156560 			return (ERR_OP_ILLEGAL);
2762711890bcSjc156560 		break;
2763711890bcSjc156560 	case SET_CACHE_RD_PLY:
2764711890bcSjc156560 		if (*value != CACHE_RD_OFF &&
2765711890bcSjc156560 		    *value != CACHE_RD_ON)
2766711890bcSjc156560 			return (ERR_OP_ILLEGAL);
2767711890bcSjc156560 		break;
2768b449fa8aSyw161884 	case SET_ACTIVATION_PLY:
2769b449fa8aSyw161884 		if (*value != ARRAY_ACT_ACTIVATE)
2770b449fa8aSyw161884 			return (ERR_OP_ILLEGAL);
2771b449fa8aSyw161884 		break;
2772711890bcSjc156560 	default:
2773711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2774711890bcSjc156560 	}
2775711890bcSjc156560 
2776711890bcSjc156560 	(void) obj_get_attr(raid_tab, obj_id, (void **)(&attr));
2777711890bcSjc156560 
2778711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2779711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2780711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2781711890bcSjc156560 
2782711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2783711890bcSjc156560 	if (ctl_attrp == NULL) {
2784711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2785711890bcSjc156560 	}
2786711890bcSjc156560 
2787711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2788711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2789711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2790711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2791711890bcSjc156560 
2792711890bcSjc156560 	if (raid_lib->set_attr == NULL)
2793711890bcSjc156560 		return (ERR_OP_NO_IMPL);
2794711890bcSjc156560 
2795711890bcSjc156560 	ret = raid_lib->set_attr(ctl_attrp->controller_id,
2796711890bcSjc156560 	    attr->array_id, sub_cmd, value, plugin_err_str);
2797711890bcSjc156560 
2798711890bcSjc156560 	return (ret);
2799711890bcSjc156560 }
2800711890bcSjc156560 
2801711890bcSjc156560 static int
2802711890bcSjc156560 obj_disk_compnum(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2803711890bcSjc156560 	raid_obj_type_id_t comp_type)
2804711890bcSjc156560 {
2805711890bcSjc156560 	disk_attr_t *attr;
2806711890bcSjc156560 	controller_attr_t *ctl_attrp;
2807711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2808711890bcSjc156560 	raid_lib_t *raid_lib;
2809711890bcSjc156560 	int ret = SUCCESS, fd;
2810711890bcSjc156560 
2811711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_DISK)
2812711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2813711890bcSjc156560 
2814711890bcSjc156560 	if (comp_type != OBJ_TYPE_DISK_SEG &&
2815711890bcSjc156560 	    comp_type != OBJ_TYPE_HSP &&
2816b449fa8aSyw161884 	    comp_type != OBJ_TYPE_TASK &&
2817b449fa8aSyw161884 	    comp_type != OBJ_TYPE_PROP)
2818711890bcSjc156560 		return (0);
2819711890bcSjc156560 	ret = obj_get_attr(raid_tab, obj_id, (void **)(&attr));
2820711890bcSjc156560 	if ((ret != SUCCESS) || (attr == NULL)) {
2821711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2822711890bcSjc156560 	}
2823711890bcSjc156560 	if (attr->state == DISK_STATE_FAILED) {
2824711890bcSjc156560 		return (SUCCESS);
2825711890bcSjc156560 	}
2826711890bcSjc156560 
2827711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2828711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2829711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2830711890bcSjc156560 
2831711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2832711890bcSjc156560 	if (ctl_attrp == NULL) {
2833711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2834711890bcSjc156560 	}
2835711890bcSjc156560 
2836711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2837711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2838711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2839711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2840711890bcSjc156560 
2841711890bcSjc156560 	ret = raid_lib->compnum(ctl_attrp->controller_id,
2842711890bcSjc156560 	    attr->disk_id, OBJ_TYPE_DISK, comp_type);
2843711890bcSjc156560 
2844711890bcSjc156560 	return (ret);
2845711890bcSjc156560 }
2846711890bcSjc156560 
2847711890bcSjc156560 static int
2848711890bcSjc156560 obj_disk_complist(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2849711890bcSjc156560 	int comp_num, raid_obj_id_t *comp_list, raid_obj_type_id_t comp_type)
2850711890bcSjc156560 {
2851711890bcSjc156560 	disk_attr_t *attr;
2852711890bcSjc156560 	controller_attr_t *ctl_attrp;
2853711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2854711890bcSjc156560 	raid_lib_t *raid_lib;
2855711890bcSjc156560 	int ret, i, fd;
2856711890bcSjc156560 	uint32_t *ids;
2857711890bcSjc156560 
2858711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_DISK)
2859711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2860711890bcSjc156560 
2861711890bcSjc156560 	if (comp_type != OBJ_TYPE_DISK_SEG &&
2862711890bcSjc156560 	    comp_type != OBJ_TYPE_HSP &&
2863b449fa8aSyw161884 	    comp_type != OBJ_TYPE_TASK &&
2864b449fa8aSyw161884 	    comp_type != OBJ_TYPE_PROP)
2865711890bcSjc156560 		return (0);
2866711890bcSjc156560 
2867711890bcSjc156560 	if (comp_num <= 0 || comp_list == NULL)
2868711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2869711890bcSjc156560 
2870711890bcSjc156560 	for (i = 0; i < comp_num; ++i)
2871711890bcSjc156560 		if (raid_obj_get_type(raid_tab, *(comp_list + i)) !=
2872711890bcSjc156560 		    comp_type)
2873711890bcSjc156560 			return (ERR_DEVICE_TYPE);
2874711890bcSjc156560 	ret = obj_get_attr(raid_tab, obj_id, (void **)(&attr));
2875711890bcSjc156560 	if ((ret != SUCCESS) || (attr == NULL)) {
2876711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2877711890bcSjc156560 	}
2878711890bcSjc156560 	if (attr->state == DISK_STATE_FAILED) {
2879711890bcSjc156560 		return (SUCCESS);
2880711890bcSjc156560 	}
2881711890bcSjc156560 
2882711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2883711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2884711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2885711890bcSjc156560 
2886711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2887711890bcSjc156560 	if (ctl_attrp == NULL) {
2888711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2889711890bcSjc156560 	}
2890711890bcSjc156560 
2891711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2892711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2893711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2894711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2895711890bcSjc156560 
2896711890bcSjc156560 	ids = malloc(comp_num * sizeof (uint32_t));
2897711890bcSjc156560 	if (ids == NULL)
2898711890bcSjc156560 		return (ERR_NOMEM);
2899711890bcSjc156560 
2900711890bcSjc156560 	ret = raid_lib->complist(ctl_attrp->controller_id,
2901711890bcSjc156560 	    attr->disk_id, OBJ_TYPE_DISK, comp_type, comp_num, ids);
2902711890bcSjc156560 
2903711890bcSjc156560 	if (ret < SUCCESS) {
2904711890bcSjc156560 		free(ids);
2905711890bcSjc156560 		return (ret);
2906711890bcSjc156560 	}
2907711890bcSjc156560 
2908711890bcSjc156560 	for (i = 0; i < comp_num; ++ i) {
2909711890bcSjc156560 		diskseg_attr_t *diskseg_attr;
2910711890bcSjc156560 		hsp_attr_t *hsp_attr;
2911711890bcSjc156560 		task_attr_t *task_attr;
2912b449fa8aSyw161884 		property_attr_t *prop_attr;
2913711890bcSjc156560 		void *attr_buf;
2914711890bcSjc156560 
2915711890bcSjc156560 		attr_buf = raid_obj_get_data_ptr(raid_tab, *(comp_list + i));
2916711890bcSjc156560 		if (attr_buf == NULL) {
2917711890bcSjc156560 			free(ids);
2918711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2919711890bcSjc156560 		}
2920711890bcSjc156560 
2921711890bcSjc156560 		switch (comp_type) {
2922711890bcSjc156560 		case OBJ_TYPE_DISK_SEG:
2923711890bcSjc156560 			diskseg_attr = attr_buf;
2924711890bcSjc156560 			diskseg_attr->seq_no = *(ids + i);
2925711890bcSjc156560 			break;
2926711890bcSjc156560 		case OBJ_TYPE_HSP:
2927711890bcSjc156560 			hsp_attr = attr_buf;
2928711890bcSjc156560 			hsp_attr->associated_id = *(ids + i);
2929711890bcSjc156560 			break;
2930711890bcSjc156560 		case OBJ_TYPE_TASK:
2931711890bcSjc156560 			task_attr = attr_buf;
2932711890bcSjc156560 			task_attr->task_id = *(ids + i);
2933711890bcSjc156560 			break;
2934b449fa8aSyw161884 		case OBJ_TYPE_PROP:
2935b449fa8aSyw161884 			prop_attr = attr_buf;
2936b449fa8aSyw161884 			prop_attr->prop_id = *(ids + i);
2937b449fa8aSyw161884 			break;
2938711890bcSjc156560 		default:
2939711890bcSjc156560 			free(ids);
2940711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2941711890bcSjc156560 		}
2942711890bcSjc156560 	}
2943711890bcSjc156560 
2944711890bcSjc156560 
2945711890bcSjc156560 	free(ids);
2946711890bcSjc156560 	return (ret);
2947711890bcSjc156560 }
2948711890bcSjc156560 
2949711890bcSjc156560 static int
2950711890bcSjc156560 obj_disk_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2951711890bcSjc156560 {
2952711890bcSjc156560 	disk_attr_t *attr;
2953711890bcSjc156560 	controller_attr_t *ctl_attrp;
2954711890bcSjc156560 	raid_lib_t *raid_lib;
2955711890bcSjc156560 	int ret = SUCCESS, fd;
2956711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2957711890bcSjc156560 
2958711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_DISK)
2959711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2960711890bcSjc156560 
2961711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
2962711890bcSjc156560 		return (SUCCESS);
2963711890bcSjc156560 
2964711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2965711890bcSjc156560 	if (attr == NULL)
2966711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2967711890bcSjc156560 
2968711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2969711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2970711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2971711890bcSjc156560 
2972711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2973711890bcSjc156560 	if (ctl_attrp == NULL) {
2974711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2975711890bcSjc156560 	}
2976711890bcSjc156560 
2977711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2978711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2979711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2980711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2981711890bcSjc156560 
2982711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
2983711890bcSjc156560 	    attr->disk_id, 0, OBJ_TYPE_DISK, attr);
2984711890bcSjc156560 
2985711890bcSjc156560 	if (ret < SUCCESS)
2986711890bcSjc156560 		return (ret);
2987711890bcSjc156560 
2988711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
2989711890bcSjc156560 
2990711890bcSjc156560 	return (ret);
2991711890bcSjc156560 }
2992711890bcSjc156560 
2993711890bcSjc156560 static int
2994711890bcSjc156560 obj_hsp_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2995711890bcSjc156560 {
2996711890bcSjc156560 	hsp_attr_t *attr;
2997711890bcSjc156560 
2998711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_HSP)
2999711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3000711890bcSjc156560 
3001711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
3002711890bcSjc156560 		return (SUCCESS);
3003711890bcSjc156560 
3004711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3005711890bcSjc156560 	if (attr == NULL)
3006711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3007711890bcSjc156560 
3008711890bcSjc156560 	if (attr->associated_id == (uint32_t)OBJ_ATTR_NONE)
3009711890bcSjc156560 		attr->type = HSP_TYPE_GLOBAL;
3010711890bcSjc156560 	else
3011711890bcSjc156560 		attr->type = HSP_TYPE_LOCAL;
3012711890bcSjc156560 
3013711890bcSjc156560 	return (SUCCESS);
3014711890bcSjc156560 }
3015711890bcSjc156560 
3016711890bcSjc156560 static int
3017711890bcSjc156560 obj_arraypart_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
3018711890bcSjc156560 {
3019711890bcSjc156560 	arraypart_attr_t *attr;
3020711890bcSjc156560 	array_attr_t *array_attr;
3021711890bcSjc156560 	controller_attr_t *ctl_attrp;
3022711890bcSjc156560 	raid_lib_t *raid_lib;
3023711890bcSjc156560 	int ret = SUCCESS, fd;
3024711890bcSjc156560 	raid_obj_id_t controller_obj_id, array_obj_id;
3025711890bcSjc156560 
3026711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY_PART)
3027711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3028711890bcSjc156560 
3029711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
3030711890bcSjc156560 		return (SUCCESS);
3031711890bcSjc156560 
3032711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3033711890bcSjc156560 	if (attr == NULL)
3034711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3035711890bcSjc156560 
3036711890bcSjc156560 	array_obj_id = raid_obj_get_container(raid_tab, obj_id);
3037711890bcSjc156560 	if (array_obj_id < OBJ_NONE)
3038711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3039711890bcSjc156560 
3040711890bcSjc156560 	array_attr = raid_obj_get_data_ptr(raid_tab, array_obj_id);
3041711890bcSjc156560 	if (array_attr == NULL)
3042711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3043711890bcSjc156560 
3044711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
3045711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
3046711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3047711890bcSjc156560 
3048711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
3049711890bcSjc156560 	if (ctl_attrp == NULL) {
3050711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3051711890bcSjc156560 	}
3052711890bcSjc156560 
3053711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3054711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3055711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3056711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3057711890bcSjc156560 
3058711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3059711890bcSjc156560 	    array_attr->array_id, attr->disk_id,
3060711890bcSjc156560 	    OBJ_TYPE_ARRAY_PART, attr);
3061711890bcSjc156560 
3062711890bcSjc156560 	if (ret < SUCCESS)
3063711890bcSjc156560 		return (ret);
3064711890bcSjc156560 
3065711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
3066711890bcSjc156560 
3067711890bcSjc156560 	return (ret);
3068711890bcSjc156560 }
3069711890bcSjc156560 
3070711890bcSjc156560 static int
3071711890bcSjc156560 obj_diskseg_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
3072711890bcSjc156560 {
3073711890bcSjc156560 	diskseg_attr_t *attr;
3074711890bcSjc156560 	disk_attr_t *disk_attr;
3075711890bcSjc156560 	controller_attr_t *ctl_attrp;
3076711890bcSjc156560 	raid_lib_t *raid_lib;
3077711890bcSjc156560 	int ret = SUCCESS, fd;
3078711890bcSjc156560 	raid_obj_id_t controller_obj_id, disk_obj_id;
3079711890bcSjc156560 
3080711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_DISK_SEG)
3081711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3082711890bcSjc156560 
3083711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
3084711890bcSjc156560 		return (SUCCESS);
3085711890bcSjc156560 
3086711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3087711890bcSjc156560 	if (attr == NULL)
3088711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3089711890bcSjc156560 
3090711890bcSjc156560 	disk_obj_id = raid_obj_get_container(raid_tab, obj_id);
3091711890bcSjc156560 	if (disk_obj_id < OBJ_NONE)
3092711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3093711890bcSjc156560 
3094711890bcSjc156560 	disk_attr = raid_obj_get_data_ptr(raid_tab, disk_obj_id);
3095711890bcSjc156560 	if (disk_attr == NULL)
3096711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3097711890bcSjc156560 
3098711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
3099711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
3100711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3101711890bcSjc156560 
3102711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
3103711890bcSjc156560 	if (ctl_attrp == NULL) {
3104711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3105711890bcSjc156560 	}
3106711890bcSjc156560 
3107711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3108711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3109711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3110711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3111711890bcSjc156560 
3112711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3113711890bcSjc156560 	    disk_attr->disk_id, attr->seq_no, OBJ_TYPE_DISK_SEG, attr);
3114711890bcSjc156560 
3115711890bcSjc156560 	if (ret < SUCCESS)
3116711890bcSjc156560 		return (ret);
3117711890bcSjc156560 
3118711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
3119711890bcSjc156560 
3120711890bcSjc156560 	return (ret);
3121711890bcSjc156560 }
3122711890bcSjc156560 
3123711890bcSjc156560 static int
3124711890bcSjc156560 obj_task_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
3125711890bcSjc156560 {
3126711890bcSjc156560 	task_attr_t *attr;
3127711890bcSjc156560 	controller_attr_t *ctl_attrp;
3128711890bcSjc156560 	raid_lib_t *raid_lib;
3129711890bcSjc156560 	int ret = SUCCESS, fd;
3130711890bcSjc156560 	raid_obj_id_t controller_obj_id;
3131711890bcSjc156560 
3132711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_TASK)
3133711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3134711890bcSjc156560 
3135711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3136711890bcSjc156560 	if (attr == NULL)
3137711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3138711890bcSjc156560 
3139711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
3140711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
3141711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3142711890bcSjc156560 
3143711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
3144711890bcSjc156560 	if (ctl_attrp == NULL) {
3145711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3146711890bcSjc156560 	}
3147711890bcSjc156560 
3148711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3149711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3150711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3151711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3152711890bcSjc156560 
3153711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3154711890bcSjc156560 	    attr->task_id, OBJ_ATTR_NONE, OBJ_TYPE_TASK, attr);
3155711890bcSjc156560 
3156711890bcSjc156560 	return (ret);
3157711890bcSjc156560 }
3158711890bcSjc156560 
3159711890bcSjc156560 static int
3160b449fa8aSyw161884 obj_prop_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
3161b449fa8aSyw161884 {
3162b449fa8aSyw161884 	property_attr_t *attr, *attr_new;
3163b449fa8aSyw161884 	disk_attr_t *disk_attr;
3164b449fa8aSyw161884 	controller_attr_t *ctl_attrp;
3165b449fa8aSyw161884 	raid_lib_t *raid_lib;
3166b449fa8aSyw161884 	int ret = SUCCESS, fd;
3167b449fa8aSyw161884 	raid_obj_id_t controller_obj_id, disk_obj_id;
3168b449fa8aSyw161884 
3169b449fa8aSyw161884 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_PROP)
3170b449fa8aSyw161884 		return (ERR_DEVICE_TYPE);
3171b449fa8aSyw161884 
3172b449fa8aSyw161884 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
3173b449fa8aSyw161884 		return (SUCCESS);
3174b449fa8aSyw161884 
3175b449fa8aSyw161884 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3176b449fa8aSyw161884 	if (attr == NULL)
3177b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3178b449fa8aSyw161884 
3179b449fa8aSyw161884 	disk_obj_id = raid_obj_get_container(raid_tab, obj_id);
3180b449fa8aSyw161884 	if (disk_obj_id < OBJ_NONE)
3181b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3182b449fa8aSyw161884 
3183b449fa8aSyw161884 	disk_attr = raid_obj_get_data_ptr(raid_tab, disk_obj_id);
3184b449fa8aSyw161884 	if (disk_attr == NULL)
3185b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3186b449fa8aSyw161884 
3187b449fa8aSyw161884 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
3188b449fa8aSyw161884 	if (controller_obj_id < OBJ_NONE)
3189b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3190b449fa8aSyw161884 
3191b449fa8aSyw161884 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
3192b449fa8aSyw161884 	if (ctl_attrp == NULL) {
3193b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3194b449fa8aSyw161884 	}
3195b449fa8aSyw161884 
3196b449fa8aSyw161884 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3197b449fa8aSyw161884 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3198b449fa8aSyw161884 	if ((raid_lib == NULL) || (fd == 0))
3199b449fa8aSyw161884 		return (ERR_DRIVER_CLOSED);
3200b449fa8aSyw161884 
3201b449fa8aSyw161884 	/* Get the property size at first */
3202b449fa8aSyw161884 	attr->prop_size = 0;
3203b449fa8aSyw161884 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3204b449fa8aSyw161884 	    disk_attr->disk_id, OBJ_ATTR_NONE, OBJ_TYPE_PROP, attr);
3205b449fa8aSyw161884 
3206b449fa8aSyw161884 	if (ret < SUCCESS)
3207b449fa8aSyw161884 		return (ret);
3208b449fa8aSyw161884 
3209b449fa8aSyw161884 	/* Allocate memory for property and fill the buffer */
3210b449fa8aSyw161884 	attr_new = realloc(attr, sizeof (property_attr_t) + attr->prop_size);
3211b449fa8aSyw161884 	if (attr_new == NULL)
3212b449fa8aSyw161884 		return (ERR_NOMEM);
3213b449fa8aSyw161884 
3214b449fa8aSyw161884 	(void) raid_obj_set_data_ptr(raid_tab, obj_id, attr_new);
3215b449fa8aSyw161884 
3216b449fa8aSyw161884 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3217b449fa8aSyw161884 	    disk_attr->disk_id, OBJ_ATTR_NONE, OBJ_TYPE_PROP, attr_new);
3218b449fa8aSyw161884 
3219b449fa8aSyw161884 	if (ret < SUCCESS)
3220b449fa8aSyw161884 		return (ret);
3221b449fa8aSyw161884 
3222b449fa8aSyw161884 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
3223b449fa8aSyw161884 
3224b449fa8aSyw161884 	return (ret);
3225b449fa8aSyw161884 }
3226b449fa8aSyw161884 
3227b449fa8aSyw161884 static int
3228711890bcSjc156560 obj_array_create(raid_obj_tab_t *raid_tab, raid_obj_id_t array_obj_id,
3229711890bcSjc156560 	int num_of_comp, raid_obj_id_t *disk_list, char **plugin_err_str)
3230711890bcSjc156560 {
3231711890bcSjc156560 	controller_attr_t *controller_attr;
3232711890bcSjc156560 	array_attr_t *array_attr, array_attr2;
3233711890bcSjc156560 	disk_attr_t *disk_attr;
3234711890bcSjc156560 	arraypart_attr_t *arraypart_attrs;
3235711890bcSjc156560 	raid_obj_id_t obj_id, controller_obj_id = OBJ_NONE;
3236711890bcSjc156560 	raid_lib_t *raid_lib;
3237711890bcSjc156560 	int i, j, ret, fd;
3238711890bcSjc156560 	int disk_cnt = 0, disk_set_num = 0, set_num = 0, layer_cnt = 0;
3239711890bcSjc156560 	uint64_t min_disk_capacity = 0;
3240711890bcSjc156560 
3241711890bcSjc156560 	array_attr = raid_obj_get_data_ptr(raid_tab, array_obj_id);
3242711890bcSjc156560 	if (array_attr == NULL)
3243711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3244711890bcSjc156560 
3245711890bcSjc156560 	/* Check the disk layout expression */
3246711890bcSjc156560 	if (disk_list[0] != OBJ_SEPARATOR_BEGIN ||
3247711890bcSjc156560 	    disk_list[num_of_comp - 1] != OBJ_SEPARATOR_END)
3248711890bcSjc156560 		return (ERR_ARRAY_LAYOUT);
3249711890bcSjc156560 	for (i = 0; i < num_of_comp; ++i) {
3250711890bcSjc156560 		if (disk_list[i] == OBJ_SEPARATOR_BEGIN) {
3251711890bcSjc156560 			if (disk_cnt != 0)
3252711890bcSjc156560 				return (ERR_ARRAY_LAYOUT);
3253711890bcSjc156560 			++layer_cnt;
3254711890bcSjc156560 			continue;
3255711890bcSjc156560 		}
3256711890bcSjc156560 		if (disk_list[i] == OBJ_SEPARATOR_END) {
3257711890bcSjc156560 			if (disk_set_num == 0)
3258711890bcSjc156560 				disk_set_num = disk_cnt;
3259711890bcSjc156560 			else if (disk_set_num != disk_cnt && disk_cnt != 0)
3260711890bcSjc156560 				return (ERR_ARRAY_LAYOUT);
3261711890bcSjc156560 			disk_cnt = 0;
3262711890bcSjc156560 			++set_num;
3263711890bcSjc156560 			--layer_cnt;
3264711890bcSjc156560 			continue;
3265711890bcSjc156560 		}
3266711890bcSjc156560 		switch (array_attr->raid_level) {
3267711890bcSjc156560 		case RAID_LEVEL_0:
3268711890bcSjc156560 		case RAID_LEVEL_1:
3269711890bcSjc156560 		case RAID_LEVEL_1E:
3270711890bcSjc156560 		case RAID_LEVEL_5:
3271711890bcSjc156560 			if (layer_cnt != 1)
3272711890bcSjc156560 				return (ERR_ARRAY_LAYOUT);
3273711890bcSjc156560 			break;
3274711890bcSjc156560 		case RAID_LEVEL_10:
3275711890bcSjc156560 		case RAID_LEVEL_50:
3276711890bcSjc156560 			if (layer_cnt != 2)
3277711890bcSjc156560 				return (ERR_ARRAY_LAYOUT);
3278711890bcSjc156560 			break;
3279711890bcSjc156560 		default:
3280711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3281711890bcSjc156560 		}
3282711890bcSjc156560 		++disk_cnt;
3283711890bcSjc156560 	}
3284711890bcSjc156560 
3285711890bcSjc156560 	if (layer_cnt != 0)
3286711890bcSjc156560 		return (ERR_ARRAY_LAYOUT);
3287711890bcSjc156560 
3288711890bcSjc156560 	switch (array_attr->raid_level) {
3289711890bcSjc156560 	case RAID_LEVEL_0:
3290711890bcSjc156560 		if (disk_set_num < 2 || set_num != 1)
3291711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3292711890bcSjc156560 		break;
3293711890bcSjc156560 	case RAID_LEVEL_1:
3294711890bcSjc156560 		if (disk_set_num != 2 || set_num != 1)
3295711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3296711890bcSjc156560 		break;
3297711890bcSjc156560 	case RAID_LEVEL_1E:
3298711890bcSjc156560 	case RAID_LEVEL_5:
3299711890bcSjc156560 		if (disk_set_num < 3 || set_num != 1)
3300711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3301711890bcSjc156560 		break;
3302711890bcSjc156560 	case RAID_LEVEL_10:
3303711890bcSjc156560 		if (disk_set_num != 2 || set_num < 2)
3304711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3305711890bcSjc156560 		break;
3306711890bcSjc156560 	case RAID_LEVEL_50:
3307711890bcSjc156560 		if (disk_set_num < 3 || set_num < 2)
3308711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3309711890bcSjc156560 		break;
3310711890bcSjc156560 	default:
3311711890bcSjc156560 		return (ERR_ARRAY_LEVEL);
3312711890bcSjc156560 	}
3313711890bcSjc156560 
3314711890bcSjc156560 	arraypart_attrs = calloc(num_of_comp, sizeof (arraypart_attr_t));
3315711890bcSjc156560 	if (arraypart_attrs == NULL)
3316711890bcSjc156560 		return (ERR_NOMEM);
3317711890bcSjc156560 
3318711890bcSjc156560 	for (i = 0; i < num_of_comp; ++i) {
3319711890bcSjc156560 		/* Keep seperators */
3320711890bcSjc156560 		if (*(disk_list + i) == OBJ_SEPARATOR_BEGIN) {
3321711890bcSjc156560 			arraypart_attrs[i].disk_id =
3322711890bcSjc156560 			    (uint32_t)OBJ_SEPARATOR_BEGIN;
3323711890bcSjc156560 			continue;
3324711890bcSjc156560 		}
3325711890bcSjc156560 
3326711890bcSjc156560 		if (*(disk_list + i) == OBJ_SEPARATOR_END) {
3327711890bcSjc156560 			arraypart_attrs[i].disk_id =
3328711890bcSjc156560 			    (uint32_t)OBJ_SEPARATOR_END;
3329711890bcSjc156560 			continue;
3330711890bcSjc156560 		}
3331711890bcSjc156560 
3332711890bcSjc156560 		disk_cnt++;
3333711890bcSjc156560 		/* Check if it's a disk */
3334711890bcSjc156560 		if (raid_obj_get_type(raid_tab, *(disk_list + i)) !=
3335711890bcSjc156560 		    OBJ_TYPE_DISK)
3336711890bcSjc156560 			return (ERR_DEVICE_TYPE);
3337711890bcSjc156560 
3338711890bcSjc156560 		/* Check if it's duplicated with other disks */
3339711890bcSjc156560 		for (j = 0; j < i; ++j)
3340711890bcSjc156560 			if (*(disk_list + j) == *(disk_list + i)) {
3341711890bcSjc156560 				free(arraypart_attrs);
3342711890bcSjc156560 				return (ERR_DEVICE_DUP);
3343711890bcSjc156560 			}
3344711890bcSjc156560 
3345711890bcSjc156560 		/* Check disk status */
3346711890bcSjc156560 		ret = obj_get_attr(raid_tab, *(disk_list + i),
3347711890bcSjc156560 		    (void **)(&disk_attr));
3348711890bcSjc156560 		if (ret != SUCCESS)
3349711890bcSjc156560 			return (ret);
3350711890bcSjc156560 
3351711890bcSjc156560 		if (disk_attr->state != DISK_STATE_GOOD) {
3352711890bcSjc156560 			free(arraypart_attrs);
3353711890bcSjc156560 			return (ERR_DISK_STATE);
3354711890bcSjc156560 		}
3355711890bcSjc156560 
3356711890bcSjc156560 		/* All disks must belong to the same controller */
3357711890bcSjc156560 		obj_id = obj_get_controller(raid_tab, *(disk_list + i));
3358711890bcSjc156560 		if (obj_id <= OBJ_NONE)
3359711890bcSjc156560 			return (obj_id);
3360711890bcSjc156560 		if (controller_obj_id == OBJ_NONE) {
3361711890bcSjc156560 			controller_obj_id = obj_id;
3362711890bcSjc156560 			ret = obj_get_attr(raid_tab, controller_obj_id,
3363711890bcSjc156560 			    (void **)(&controller_attr));
3364711890bcSjc156560 		} else if (obj_id != controller_obj_id) {
3365711890bcSjc156560 			free(arraypart_attrs);
3366711890bcSjc156560 			return (ERR_DRIVER_ACROSS);
3367711890bcSjc156560 		}
3368711890bcSjc156560 
3369711890bcSjc156560 		/* Check if the disk contains too many segments */
3370711890bcSjc156560 		obj_id = obj_get_comp(raid_tab, *(disk_list + i),
3371711890bcSjc156560 		    OBJ_TYPE_DISK_SEG);
3372711890bcSjc156560 		j = 0;
3373711890bcSjc156560 		while (obj_id > OBJ_NONE) {
3374711890bcSjc156560 			++j;
3375711890bcSjc156560 			obj_id = obj_get_sibling(raid_tab, obj_id);
3376711890bcSjc156560 		}
3377711890bcSjc156560 		if (j > controller_attr->max_seg_per_disk) {
3378711890bcSjc156560 			free(arraypart_attrs);
3379711890bcSjc156560 			return (ERR_DISK_SEG_AMOUNT);
3380711890bcSjc156560 		}
3381711890bcSjc156560 
3382711890bcSjc156560 		/* Check if controller is a hostraid controller */
3383711890bcSjc156560 		if (controller_attr->capability & RAID_CAP_DISK_TRANS) {
3384711890bcSjc156560 			/*
3385711890bcSjc156560 			 * For hostraid, the first disk should
3386711890bcSjc156560 			 * be with of minimum capacity
3387711890bcSjc156560 			 */
3388711890bcSjc156560 			if (min_disk_capacity == 0) {
3389711890bcSjc156560 				min_disk_capacity = disk_attr->capacity;
3390711890bcSjc156560 
3391711890bcSjc156560 				/* Can not specify capacity for hostraid */
3392711890bcSjc156560 				if (array_attr->capacity != 0) {
3393711890bcSjc156560 					free(arraypart_attrs);
3394700682b8Syw161884 					return (ERR_OP_ILLEGAL);
3395711890bcSjc156560 				}
3396711890bcSjc156560 			} else if (min_disk_capacity > disk_attr->capacity) {
3397711890bcSjc156560 				free(arraypart_attrs);
3398711890bcSjc156560 				return (ERR_DISK_SPACE);
3399711890bcSjc156560 			}
3400711890bcSjc156560 
3401711890bcSjc156560 			/* Disk should not be used for hostraid */
3402711890bcSjc156560 			obj_id = obj_get_comp(raid_tab, *(disk_list + i),
3403711890bcSjc156560 			    OBJ_TYPE_DISK_SEG);
3404711890bcSjc156560 			if (obj_id < OBJ_NONE) {
3405711890bcSjc156560 				free(arraypart_attrs);
3406711890bcSjc156560 				return (obj_id);
3407711890bcSjc156560 			} else if (obj_id > OBJ_NONE) {
3408711890bcSjc156560 				free(arraypart_attrs);
3409711890bcSjc156560 				return (ERR_DISK_NOT_EMPTY);
3410711890bcSjc156560 			}
3411711890bcSjc156560 		}
3412711890bcSjc156560 
3413711890bcSjc156560 		arraypart_attrs[i].disk_id = disk_attr->disk_id;
3414711890bcSjc156560 		arraypart_attrs[i].offset = OBJ_ATTR_NONE;
3415711890bcSjc156560 		arraypart_attrs[i].size = OBJ_ATTR_NONE;
3416711890bcSjc156560 	}
3417711890bcSjc156560 
3418711890bcSjc156560 	/* Check if array amount exceeds limit */
3419711890bcSjc156560 	if (controller_attr->max_array_num <=
3420711890bcSjc156560 	    obj_controller_compnum(raid_tab, controller_obj_id,
3421711890bcSjc156560 	    OBJ_TYPE_ARRAY))
3422711890bcSjc156560 		return (ERR_ARRAY_AMOUNT);
3423711890bcSjc156560 
3424711890bcSjc156560 
3425711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3426711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3427711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3428711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3429711890bcSjc156560 
3430711890bcSjc156560 	/* Check if the controller can support the array RAID level */
3431711890bcSjc156560 	switch (array_attr->raid_level) {
3432711890bcSjc156560 	case	RAID_LEVEL_0:
3433711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID0)) {
3434711890bcSjc156560 			free(arraypart_attrs);
3435711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3436711890bcSjc156560 		}
3437711890bcSjc156560 		break;
3438711890bcSjc156560 	case	RAID_LEVEL_1:
3439711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID1)) {
3440711890bcSjc156560 			free(arraypart_attrs);
3441711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3442711890bcSjc156560 		}
3443711890bcSjc156560 		break;
3444711890bcSjc156560 	case	RAID_LEVEL_1E:
3445711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID1E)) {
3446711890bcSjc156560 			free(arraypart_attrs);
3447711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3448711890bcSjc156560 		}
3449711890bcSjc156560 		break;
3450711890bcSjc156560 	case	RAID_LEVEL_5:
3451711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID5)) {
3452711890bcSjc156560 			free(arraypart_attrs);
3453711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3454711890bcSjc156560 		}
3455711890bcSjc156560 		break;
3456711890bcSjc156560 	case	RAID_LEVEL_10:
3457711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID10)) {
3458711890bcSjc156560 			free(arraypart_attrs);
3459711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3460711890bcSjc156560 		}
3461711890bcSjc156560 		break;
3462711890bcSjc156560 	case	RAID_LEVEL_50:
3463711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID50)) {
3464711890bcSjc156560 			free(arraypart_attrs);
3465711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3466711890bcSjc156560 		}
3467711890bcSjc156560 		break;
3468711890bcSjc156560 	default:
3469711890bcSjc156560 		free(arraypart_attrs);
3470711890bcSjc156560 		return (ERR_ARRAY_LEVEL);
3471711890bcSjc156560 	}
3472711890bcSjc156560 
3473711890bcSjc156560 	/* Check if plug in can calculate the maximum size */
3474711890bcSjc156560 	(void) memcpy(&array_attr2, array_attr, sizeof (array_attr_t));
3475711890bcSjc156560 	array_attr2.capacity = OBJ_ATTR_NONE;
3476711890bcSjc156560 	ret = raid_lib->array_create(controller_attr->controller_id,
3477711890bcSjc156560 	    &array_attr2, num_of_comp, arraypart_attrs, plugin_err_str);
3478711890bcSjc156560 
3479711890bcSjc156560 	/* If plugin/driver will not calculate space */
3480711890bcSjc156560 	if (ret == ERR_OP_NO_IMPL) {
3481711890bcSjc156560 		/* Calculate the maximum capacity */
3482711890bcSjc156560 		array_attr2.capacity = raid_space_noalign(raid_tab,
3483711890bcSjc156560 		    array_attr2.raid_level, num_of_comp, disk_list,
3484711890bcSjc156560 		    arraypart_attrs);
3485711890bcSjc156560 
3486711890bcSjc156560 		/*
3487711890bcSjc156560 		 * If controller is capable to allocate space,
3488711890bcSjc156560 		 * set offset and size attributes to OBJ_ATTR_NONE
3489711890bcSjc156560 		 * and let the controller to determine these value
3490711890bcSjc156560 		 */
3491711890bcSjc156560 		if (controller_attr->capability & RAID_CAP_SMART_ALLOC)
3492711890bcSjc156560 			for (i = 0; i < num_of_comp; ++i) {
3493711890bcSjc156560 				arraypart_attrs[i].offset =
3494711890bcSjc156560 				    OBJ_ATTR_NONE;
3495711890bcSjc156560 				arraypart_attrs[i].size =
3496711890bcSjc156560 				    OBJ_ATTR_NONE;
3497711890bcSjc156560 			}
3498711890bcSjc156560 
3499711890bcSjc156560 		/* There's no enough space for specified capacity */
3500711890bcSjc156560 		if (array_attr->capacity > array_attr2.capacity) {
3501711890bcSjc156560 			free(arraypart_attrs);
3502711890bcSjc156560 			return (ERR_ARRAY_SIZE);
3503711890bcSjc156560 		}
3504711890bcSjc156560 
3505711890bcSjc156560 		/* capacity == 0, allocate maximum space */
3506711890bcSjc156560 		if (array_attr->capacity == 0)
3507711890bcSjc156560 			array_attr->capacity = array_attr2.capacity;
3508711890bcSjc156560 	} else if (ret < SUCCESS) {
3509711890bcSjc156560 		free(arraypart_attrs);
3510711890bcSjc156560 		return (ret);
3511711890bcSjc156560 	} else if (array_attr2.capacity < array_attr->capacity) {
3512711890bcSjc156560 		/* Return the maximum size */
3513711890bcSjc156560 		array_attr->capacity = array_attr2.capacity;
3514711890bcSjc156560 		free(arraypart_attrs);
3515711890bcSjc156560 		return (ERR_ARRAY_SIZE);
3516711890bcSjc156560 	}
3517711890bcSjc156560 
3518711890bcSjc156560 	if (array_attr->capacity < ARRAYPART_MIN_SIZE * disk_cnt) {
3519711890bcSjc156560 		free(arraypart_attrs);
3520711890bcSjc156560 		return (ERR_ARRAY_SIZE);
3521711890bcSjc156560 	}
3522711890bcSjc156560 
3523711890bcSjc156560 
3524711890bcSjc156560 	ret = raid_lib->array_create(controller_attr->controller_id,
3525711890bcSjc156560 	    array_attr, num_of_comp, arraypart_attrs, plugin_err_str);
3526711890bcSjc156560 	free(arraypart_attrs);
3527711890bcSjc156560 
3528711890bcSjc156560 	if (ret != SUCCESS)
3529711890bcSjc156560 		return (ret);
3530711890bcSjc156560 
3531711890bcSjc156560 	/* Add array object into device tree so that we can map the handle */
3532711890bcSjc156560 	(void) raid_obj_add_org(raid_tab, array_obj_id, controller_obj_id);
3533711890bcSjc156560 
3534711890bcSjc156560 	return (ret);
3535711890bcSjc156560 }
3536711890bcSjc156560 
3537711890bcSjc156560 static int
3538711890bcSjc156560 obj_array_delete(raid_obj_tab_t *raid_tab, raid_obj_id_t array_obj_id,
3539711890bcSjc156560 	char **plugin_err_str)
3540711890bcSjc156560 {
3541711890bcSjc156560 	raid_obj_id_t controller_obj_id;
3542711890bcSjc156560 	controller_attr_t *controller_attr;
3543711890bcSjc156560 	array_attr_t *array_attr;
3544711890bcSjc156560 	raid_lib_t *raid_lib;
354572e1c055Sjc156560 	int ret, fd;
3546711890bcSjc156560 	uint32_t *disk_ids = NULL;
3547711890bcSjc156560 
3548711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, array_obj_id);
3549711890bcSjc156560 	if (controller_obj_id <= OBJ_NONE)
3550711890bcSjc156560 		return (controller_obj_id);
3551711890bcSjc156560 
3552711890bcSjc156560 	ret = obj_get_attr(raid_tab, controller_obj_id,
3553711890bcSjc156560 	    (void **)(&controller_attr));
3554711890bcSjc156560 	if (ret < SUCCESS) {
3555711890bcSjc156560 		return (ret);
3556711890bcSjc156560 	}
3557711890bcSjc156560 	ret = obj_get_attr(raid_tab, array_obj_id, (void **)(&array_attr));
3558711890bcSjc156560 	if (ret < SUCCESS)
3559711890bcSjc156560 		return (ret);
3560711890bcSjc156560 
3561711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3562711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3563711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3564711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3565711890bcSjc156560 
3566711890bcSjc156560 	ret = raid_lib->array_delete(controller_attr->controller_id,
3567711890bcSjc156560 	    array_attr->array_id, plugin_err_str);
3568711890bcSjc156560 	if (ret < SUCCESS) {
3569711890bcSjc156560 		if (disk_ids)
3570711890bcSjc156560 			free(disk_ids);
3571711890bcSjc156560 		return (ret);
3572711890bcSjc156560 	}
3573711890bcSjc156560 
3574711890bcSjc156560 	if (disk_ids)
3575711890bcSjc156560 		free(disk_ids);
3576711890bcSjc156560 	return (ret);
3577711890bcSjc156560 }
3578711890bcSjc156560 
3579711890bcSjc156560 static int
35805c9d25d2SYu-Bo Ryan Wang obj_hsp_bind(raid_obj_tab_t *raid_tab, raid_obj_id_t *obj_ids,
3581711890bcSjc156560 	char **plugin_err_str)
3582711890bcSjc156560 {
3583711890bcSjc156560 	raid_obj_id_t obj_id, controller_obj_id = OBJ_NONE;
3584711890bcSjc156560 	raid_obj_id_t array_obj_id, disk_obj_id;
3585711890bcSjc156560 	hsp_relation_t *hsp_relation;
3586711890bcSjc156560 	controller_attr_t *controller_attr;
3587711890bcSjc156560 	array_attr_t *array_attr;
3588711890bcSjc156560 	arraypart_attr_t *arraypart_attr;
3589711890bcSjc156560 	disk_attr_t *disk_attr;
3590711890bcSjc156560 	diskseg_attr_t *diskseg_attr;
3591711890bcSjc156560 	hsp_attr_t *hsp_attr;
3592711890bcSjc156560 	raid_lib_t *raid_lib;
35935c9d25d2SYu-Bo Ryan Wang 	int ret, fd;
3594711890bcSjc156560 
35955c9d25d2SYu-Bo Ryan Wang 	hsp_relation = malloc(sizeof (hsp_relation_t));
3596711890bcSjc156560 	if (hsp_relation == NULL)
3597711890bcSjc156560 		return (ERR_NOMEM);
3598711890bcSjc156560 
35995c9d25d2SYu-Bo Ryan Wang 	array_obj_id = *(obj_ids);
36005c9d25d2SYu-Bo Ryan Wang 	disk_obj_id = *(obj_ids + 1);
3601711890bcSjc156560 
3602711890bcSjc156560 	if (raid_obj_get_type(raid_tab, disk_obj_id) != OBJ_TYPE_DISK ||
3603711890bcSjc156560 	    (array_obj_id != OBJ_ATTR_NONE &&
3604711890bcSjc156560 	    raid_obj_get_type(raid_tab, array_obj_id) !=
3605711890bcSjc156560 	    OBJ_TYPE_ARRAY)) {
3606711890bcSjc156560 		free(hsp_relation);
3607711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3608711890bcSjc156560 	}
3609711890bcSjc156560 
3610711890bcSjc156560 	/* Get controller attributes */
3611711890bcSjc156560 	if (controller_obj_id == OBJ_NONE)
3612711890bcSjc156560 		controller_obj_id = obj_get_controller(raid_tab,
3613711890bcSjc156560 		    disk_obj_id);
3614711890bcSjc156560 	else if (controller_obj_id != obj_get_controller(raid_tab,
3615711890bcSjc156560 	    disk_obj_id)) {
3616711890bcSjc156560 		free(hsp_relation);
3617711890bcSjc156560 		return (ERR_DRIVER_ACROSS);
3618711890bcSjc156560 	}
3619711890bcSjc156560 
3620711890bcSjc156560 	ret = obj_get_attr(raid_tab, controller_obj_id,
3621711890bcSjc156560 	    (void **)(&controller_attr));
3622711890bcSjc156560 
3623711890bcSjc156560 	/* Get disk attributes */
3624711890bcSjc156560 	ret = obj_get_attr(raid_tab,  disk_obj_id,
3625711890bcSjc156560 	    (void **)(&disk_attr));
3626711890bcSjc156560 	if (disk_attr->state == DISK_STATE_FAILED) {
3627711890bcSjc156560 		free(hsp_relation);
3628711890bcSjc156560 		return (ERR_DISK_STATE);
3629711890bcSjc156560 	}
3630711890bcSjc156560 
3631711890bcSjc156560 	/* If it's not a hsp disk, check if there's occupied space */
3632711890bcSjc156560 	if (obj_get_comp(raid_tab, disk_obj_id, OBJ_TYPE_HSP) ==
3633711890bcSjc156560 	    OBJ_NONE) {
3634711890bcSjc156560 		obj_id = obj_get_comp(raid_tab, disk_obj_id,
3635711890bcSjc156560 		    OBJ_TYPE_DISK_SEG);
3636711890bcSjc156560 		while (obj_id != OBJ_NONE) {
3637711890bcSjc156560 			ret = obj_get_attr(raid_tab, obj_id,
3638711890bcSjc156560 			    (void **)(&diskseg_attr));
3639711890bcSjc156560 			if (!(diskseg_attr->state &
3640711890bcSjc156560 			    DISKSEG_STATE_RESERVED)) {
3641711890bcSjc156560 				free(hsp_relation);
3642711890bcSjc156560 				return (ERR_DISK_NOT_EMPTY);
3643711890bcSjc156560 			}
3644711890bcSjc156560 			obj_id = obj_get_sibling(raid_tab, obj_id);
3645711890bcSjc156560 		}
3646711890bcSjc156560 	}
3647711890bcSjc156560 
3648711890bcSjc156560 	if (array_obj_id != OBJ_ATTR_NONE) {
3649711890bcSjc156560 		/* If local hsp is supported */
3650711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_L_HSP)) {
3651711890bcSjc156560 			free(hsp_relation);
3652711890bcSjc156560 			return (ERR_OP_ILLEGAL);
3653711890bcSjc156560 		}
3654711890bcSjc156560 
3655711890bcSjc156560 		if (raid_obj_get_type(raid_tab, array_obj_id) !=
3656711890bcSjc156560 		    OBJ_TYPE_ARRAY) {
3657711890bcSjc156560 			free(hsp_relation);
3658711890bcSjc156560 			return (ERR_DEVICE_TYPE);
3659711890bcSjc156560 		}
3660711890bcSjc156560 
3661711890bcSjc156560 		/* Get array attributes */
3662711890bcSjc156560 		ret = obj_get_attr(raid_tab, array_obj_id,
3663711890bcSjc156560 		    (void **)(&array_attr));
3664711890bcSjc156560 		/* RAID 0 array can not use hsp */
3665711890bcSjc156560 		if (array_attr->raid_level == RAID_LEVEL_0) {
3666711890bcSjc156560 			free(hsp_relation);
3667711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3668711890bcSjc156560 		}
3669711890bcSjc156560 
3670711890bcSjc156560 		/* If It's belong to another controller */
3671711890bcSjc156560 		if (controller_obj_id != obj_get_controller(raid_tab,
3672711890bcSjc156560 		    array_obj_id)) {
3673711890bcSjc156560 			free(hsp_relation);
3674711890bcSjc156560 			return (ERR_DRIVER_ACROSS);
3675711890bcSjc156560 		}
3676711890bcSjc156560 
3677711890bcSjc156560 		/* Get an array part attributes */
3678711890bcSjc156560 		if ((array_attr->raid_level == RAID_LEVEL_10) ||
3679711890bcSjc156560 		    (array_attr->raid_level == RAID_LEVEL_50))
3680711890bcSjc156560 			obj_id = obj_get_comp(raid_tab, array_obj_id,
3681711890bcSjc156560 			    OBJ_TYPE_ARRAY);
3682711890bcSjc156560 		else
3683711890bcSjc156560 			obj_id = array_obj_id;
3684711890bcSjc156560 		obj_id = obj_get_comp(raid_tab, obj_id,
3685711890bcSjc156560 		    OBJ_TYPE_ARRAY_PART);
3686711890bcSjc156560 		ret = obj_get_attr(raid_tab, obj_id,
3687711890bcSjc156560 		    (void **)(&arraypart_attr));
3688711890bcSjc156560 
3689711890bcSjc156560 		/* Check if disk space is enough for array */
3690711890bcSjc156560 		if (arraypart_attr->size > disk_attr->capacity) {
3691711890bcSjc156560 			free(hsp_relation);
3692711890bcSjc156560 			return (ERR_DISK_SPACE);
3693711890bcSjc156560 		}
3694711890bcSjc156560 		if (controller_attr->capability & RAID_CAP_ARRAY_ALIGN)
3695711890bcSjc156560 			if ((arraypart_attr->size +
3696711890bcSjc156560 			    arraypart_attr->offset) >
3697711890bcSjc156560 			    disk_attr->capacity) {
3698711890bcSjc156560 			free(hsp_relation);
3699711890bcSjc156560 			return (ERR_DISK_SPACE);
3700711890bcSjc156560 			}
3701711890bcSjc156560 	} else if (!(controller_attr->capability & RAID_CAP_G_HSP)) {
3702711890bcSjc156560 		/* if global hsp is supported */
3703711890bcSjc156560 		free(hsp_relation);
3704711890bcSjc156560 		return (ERR_OP_ILLEGAL);
3705711890bcSjc156560 	}
3706711890bcSjc156560 
3707711890bcSjc156560 	/*
3708711890bcSjc156560 	 * If the array is already associated with the
3709711890bcSjc156560 	 * local hsp, or it's a global hsp, ignore it
3710711890bcSjc156560 	 */
3711711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, disk_obj_id, OBJ_TYPE_HSP);
3712711890bcSjc156560 	if (obj_id > OBJ_NONE) {
3713711890bcSjc156560 		if (obj_get_attr(raid_tab, obj_id,
3714711890bcSjc156560 		    (void **)&hsp_attr) >= SUCCESS) {
3715711890bcSjc156560 			if (((hsp_attr->type == HSP_TYPE_GLOBAL) &&
3716711890bcSjc156560 			    (array_obj_id != OBJ_ATTR_NONE)) ||
3717711890bcSjc156560 			    ((hsp_attr->type == HSP_TYPE_LOCAL) &&
3718711890bcSjc156560 			    (array_obj_id == OBJ_ATTR_NONE))) {
3719711890bcSjc156560 				free(hsp_relation);
3720711890bcSjc156560 				return (ERR_OP_ILLEGAL);
3721711890bcSjc156560 			}
3722711890bcSjc156560 		}
3723711890bcSjc156560 	}
3724711890bcSjc156560 
3725711890bcSjc156560 	if (array_obj_id != OBJ_ATTR_NONE)
37265c9d25d2SYu-Bo Ryan Wang 		hsp_relation->array_id = array_attr->array_id;
3727711890bcSjc156560 	else
37285c9d25d2SYu-Bo Ryan Wang 		hsp_relation->array_id = (uint32_t)OBJ_ATTR_NONE;
37295c9d25d2SYu-Bo Ryan Wang 	hsp_relation->disk_id = disk_attr->disk_id;
3730711890bcSjc156560 
3731711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3732711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3733711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3734711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3735711890bcSjc156560 
3736711890bcSjc156560 	if (raid_lib->hsp_bind == NULL) {
3737711890bcSjc156560 		free(hsp_relation);
3738711890bcSjc156560 		return (ERR_OP_NO_IMPL);
3739711890bcSjc156560 	}
3740711890bcSjc156560 
3741711890bcSjc156560 	ret = raid_lib->hsp_bind(controller_attr->controller_id,
37425c9d25d2SYu-Bo Ryan Wang 	    hsp_relation, plugin_err_str);
3743711890bcSjc156560 
3744711890bcSjc156560 	free(hsp_relation);
3745711890bcSjc156560 	return (ret);
3746711890bcSjc156560 }
3747711890bcSjc156560 
3748711890bcSjc156560 static int
37495c9d25d2SYu-Bo Ryan Wang obj_hsp_unbind(raid_obj_tab_t *raid_tab, raid_obj_id_t *obj_ids,
3750711890bcSjc156560 	char **plugin_err_str)
3751711890bcSjc156560 {
3752711890bcSjc156560 	raid_obj_id_t obj_id, controller_obj_id = OBJ_NONE;
3753711890bcSjc156560 	raid_obj_id_t array_obj_id, disk_obj_id;
3754711890bcSjc156560 	hsp_relation_t *hsp_relation;
3755711890bcSjc156560 	controller_attr_t *controller_attr;
3756711890bcSjc156560 	array_attr_t *array_attr;
3757711890bcSjc156560 	disk_attr_t *disk_attr;
3758711890bcSjc156560 	hsp_attr_t *hsp_attr;
3759711890bcSjc156560 	raid_lib_t *raid_lib;
37605c9d25d2SYu-Bo Ryan Wang 	int ret, fd;
3761711890bcSjc156560 
37625c9d25d2SYu-Bo Ryan Wang 	hsp_relation = malloc(sizeof (hsp_relation_t));
3763711890bcSjc156560 	if (hsp_relation == NULL)
3764711890bcSjc156560 		return (ERR_NOMEM);
3765711890bcSjc156560 
37665c9d25d2SYu-Bo Ryan Wang 	array_obj_id = *(obj_ids);
37675c9d25d2SYu-Bo Ryan Wang 	disk_obj_id = *(obj_ids + 1);
3768711890bcSjc156560 
3769711890bcSjc156560 	if (raid_obj_get_type(raid_tab, disk_obj_id) != OBJ_TYPE_DISK) {
3770711890bcSjc156560 		free(hsp_relation);
3771711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3772711890bcSjc156560 	}
3773711890bcSjc156560 
3774711890bcSjc156560 	/* Get controller attributes */
3775711890bcSjc156560 	if (controller_obj_id == OBJ_NONE)
3776711890bcSjc156560 		controller_obj_id = obj_get_controller(raid_tab,
3777711890bcSjc156560 		    disk_obj_id);
3778711890bcSjc156560 	else if (controller_obj_id != obj_get_controller(raid_tab,
3779711890bcSjc156560 	    disk_obj_id)) {
3780711890bcSjc156560 		free(hsp_relation);
3781711890bcSjc156560 		return (ERR_DRIVER_ACROSS);
3782711890bcSjc156560 	}
3783711890bcSjc156560 
3784711890bcSjc156560 	ret = obj_get_attr(raid_tab, controller_obj_id,
3785711890bcSjc156560 	    (void **)(&controller_attr));
3786711890bcSjc156560 
3787711890bcSjc156560 	/* Get disk attributes */
3788711890bcSjc156560 	ret = obj_get_attr(raid_tab,  disk_obj_id,
3789711890bcSjc156560 	    (void **)(&disk_attr));
3790711890bcSjc156560 	if (disk_attr->state == DISK_STATE_FAILED) {
3791711890bcSjc156560 		free(hsp_relation);
3792711890bcSjc156560 		return (ERR_DISK_STATE);
3793711890bcSjc156560 	}
3794711890bcSjc156560 
3795711890bcSjc156560 	/* If it's not a hsp disk */
3796711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, disk_obj_id, OBJ_TYPE_HSP);
3797711890bcSjc156560 	if (obj_id == OBJ_NONE) {
3798711890bcSjc156560 		free(hsp_relation);
3799711890bcSjc156560 		return (ERR_DISK_STATE);
3800711890bcSjc156560 	}
3801711890bcSjc156560 	ret = obj_get_attr(raid_tab, obj_id, (void **)(&hsp_attr));
3802711890bcSjc156560 
3803711890bcSjc156560 	if (array_obj_id != OBJ_ATTR_NONE) {
3804711890bcSjc156560 		if (raid_obj_get_type(raid_tab, array_obj_id) !=
3805711890bcSjc156560 		    OBJ_TYPE_ARRAY) {
3806711890bcSjc156560 			free(hsp_relation);
3807711890bcSjc156560 			return (ERR_DEVICE_TYPE);
3808711890bcSjc156560 		}
3809711890bcSjc156560 
3810711890bcSjc156560 		/* Get array attributes */
3811711890bcSjc156560 		ret = obj_get_attr(raid_tab, array_obj_id,
3812711890bcSjc156560 		    (void **)(&array_attr));
3813711890bcSjc156560 
3814711890bcSjc156560 		/* If It's belong to another controller */
3815711890bcSjc156560 		if (controller_obj_id != obj_get_controller(raid_tab,
3816711890bcSjc156560 		    array_obj_id)) {
3817711890bcSjc156560 			free(hsp_relation);
3818711890bcSjc156560 			return (ERR_DRIVER_ACROSS);
3819711890bcSjc156560 		}
3820711890bcSjc156560 
3821711890bcSjc156560 		/* If want to remove an array from a global hsp */
3822711890bcSjc156560 		if (hsp_attr->type == HSP_TYPE_GLOBAL) {
3823711890bcSjc156560 			free(hsp_relation);
3824711890bcSjc156560 			return (ERR_OP_ILLEGAL);
3825711890bcSjc156560 		}
3826711890bcSjc156560 
3827711890bcSjc156560 		do {
3828711890bcSjc156560 			(void) obj_get_attr(raid_tab, obj_id,
3829711890bcSjc156560 			    (void **)(&hsp_attr));
3830711890bcSjc156560 
3831711890bcSjc156560 			if (hsp_attr->associated_id ==
3832711890bcSjc156560 			    array_attr->array_id ||
3833711890bcSjc156560 			    hsp_attr->type == HSP_TYPE_GLOBAL)
3834711890bcSjc156560 				break;
3835711890bcSjc156560 
3836711890bcSjc156560 			obj_id = obj_get_sibling(raid_tab, obj_id);
3837711890bcSjc156560 		} while (obj_id > OBJ_NONE);
3838711890bcSjc156560 	} else if (hsp_attr->type != HSP_TYPE_GLOBAL) {
3839711890bcSjc156560 		/* if global hsp is supported */
3840711890bcSjc156560 		free(hsp_relation);
3841711890bcSjc156560 		return (ERR_OP_ILLEGAL);
3842711890bcSjc156560 	}
3843711890bcSjc156560 
3844711890bcSjc156560 	/*
3845711890bcSjc156560 	 * If array is associated with a local hsp, or remove a
3846711890bcSjc156560 	 * global hsp disk
3847711890bcSjc156560 	 */
3848711890bcSjc156560 	if ((obj_id && (array_obj_id != OBJ_ATTR_NONE)) ||
3849711890bcSjc156560 	    (array_obj_id == OBJ_ATTR_NONE)) {
3850711890bcSjc156560 		if (array_obj_id != OBJ_ATTR_NONE)
38515c9d25d2SYu-Bo Ryan Wang 			hsp_relation->array_id = array_attr->array_id;
3852711890bcSjc156560 		else
38535c9d25d2SYu-Bo Ryan Wang 			hsp_relation->array_id =
3854711890bcSjc156560 			    (uint32_t)OBJ_ATTR_NONE;
38555c9d25d2SYu-Bo Ryan Wang 		hsp_relation->disk_id = disk_attr->disk_id;
3856711890bcSjc156560 	} else {
3857711890bcSjc156560 		free(hsp_relation);
3858711890bcSjc156560 		return (ERR_OP_ILLEGAL);
3859711890bcSjc156560 	}
3860711890bcSjc156560 
3861711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3862711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3863711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3864711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3865711890bcSjc156560 
3866711890bcSjc156560 	if (raid_lib->hsp_unbind == NULL) {
3867711890bcSjc156560 		free(hsp_relation);
3868711890bcSjc156560 		return (ERR_OP_NO_IMPL);
3869711890bcSjc156560 	}
3870711890bcSjc156560 
3871711890bcSjc156560 	ret = raid_lib->hsp_unbind(controller_attr->controller_id,
38725c9d25d2SYu-Bo Ryan Wang 	    hsp_relation, plugin_err_str);
3873711890bcSjc156560 
3874711890bcSjc156560 	free(hsp_relation);
3875711890bcSjc156560 	return (ret);
3876711890bcSjc156560 }
3877711890bcSjc156560 
3878711890bcSjc156560 /*
3879711890bcSjc156560  * Object maintennance routines
3880711890bcSjc156560  */
3881711890bcSjc156560 static int
3882711890bcSjc156560 raid_obj_create_system_obj(raid_obj_tab_t *raid_tab)
3883711890bcSjc156560 {
3884711890bcSjc156560 	raid_obj_t *raid_obj;
3885711890bcSjc156560 	int ret;
3886711890bcSjc156560 
3887711890bcSjc156560 	raid_obj = calloc(1, sizeof (raid_obj_t));
3888711890bcSjc156560 	if (raid_obj == NULL)
3889711890bcSjc156560 		return (ERR_NOMEM);
3890711890bcSjc156560 
3891711890bcSjc156560 	raid_obj->obj_id = OBJ_SYSTEM;
3892711890bcSjc156560 	raid_obj->obj_type_id = OBJ_TYPE_SYSTEM;
3893711890bcSjc156560 	raid_obj->data = NULL;
3894711890bcSjc156560 
3895711890bcSjc156560 	ret = raid_obj_tab_insert(raid_tab, raid_obj->obj_id, raid_obj);
3896711890bcSjc156560 	if (ret == ERR_DEVICE_DUP) {
3897711890bcSjc156560 		free(raid_obj);
3898711890bcSjc156560 		return (ERR_DEVICE_UNCLEAN);
3899711890bcSjc156560 	}
3900711890bcSjc156560 
3901711890bcSjc156560 	return (SUCCESS);
3902711890bcSjc156560 }
3903711890bcSjc156560 
3904711890bcSjc156560 static raid_obj_id_t
3905711890bcSjc156560 raid_obj_id_new(raid_obj_tab_t *raid_tab)
3906711890bcSjc156560 {
3907711890bcSjc156560 	++ raid_tab->obj_id_cnt;
3908711890bcSjc156560 	if (raid_tab->obj_id_cnt <= 0)
3909711890bcSjc156560 		return (ERR_DEVICE_OVERFLOW);
3910711890bcSjc156560 
3911711890bcSjc156560 	return (raid_tab->obj_id_cnt);
3912711890bcSjc156560 }
3913711890bcSjc156560 
3914711890bcSjc156560 static void *
3915711890bcSjc156560 raid_obj_attr_new(raid_obj_type_id_t obj_type)
3916711890bcSjc156560 {
3917711890bcSjc156560 	void *obj_attr = NULL;
3918711890bcSjc156560 
3919711890bcSjc156560 	switch (obj_type) {
3920711890bcSjc156560 	case	OBJ_TYPE_CONTROLLER:
3921711890bcSjc156560 		obj_attr = calloc(1, sizeof (controller_attr_t));
3922711890bcSjc156560 		break;
3923711890bcSjc156560 	case	OBJ_TYPE_ARRAY:
3924711890bcSjc156560 		obj_attr = calloc(1, sizeof (array_attr_t));
3925711890bcSjc156560 		break;
3926711890bcSjc156560 	case	OBJ_TYPE_DISK:
3927711890bcSjc156560 		obj_attr = calloc(1, sizeof (disk_attr_t));
3928711890bcSjc156560 		break;
3929711890bcSjc156560 	case	OBJ_TYPE_HSP:
3930711890bcSjc156560 		obj_attr = calloc(1, sizeof (hsp_attr_t));
3931711890bcSjc156560 		break;
3932711890bcSjc156560 	case	OBJ_TYPE_ARRAY_PART:
3933711890bcSjc156560 		obj_attr = calloc(1, sizeof (arraypart_attr_t));
3934711890bcSjc156560 		break;
3935711890bcSjc156560 	case	OBJ_TYPE_DISK_SEG:
3936711890bcSjc156560 		obj_attr = calloc(1, sizeof (diskseg_attr_t));
3937711890bcSjc156560 		break;
3938711890bcSjc156560 	case	OBJ_TYPE_TASK:
3939711890bcSjc156560 		obj_attr = calloc(1, sizeof (task_attr_t));
3940711890bcSjc156560 		break;
3941b449fa8aSyw161884 	case	OBJ_TYPE_PROP:
3942b449fa8aSyw161884 		obj_attr = calloc(1, sizeof (property_attr_t));
3943b449fa8aSyw161884 		break;
3944711890bcSjc156560 	default:
3945711890bcSjc156560 		break;
3946711890bcSjc156560 	}
3947711890bcSjc156560 
3948711890bcSjc156560 	return (obj_attr);
3949711890bcSjc156560 }
3950711890bcSjc156560 
3951711890bcSjc156560 static raid_obj_id_t
3952711890bcSjc156560 raid_obj_create(raid_obj_tab_t *raid_tab, raid_obj_type_id_t obj_type)
3953711890bcSjc156560 {
3954711890bcSjc156560 	raid_obj_t *raid_obj;
3955711890bcSjc156560 	int ret;
3956711890bcSjc156560 	void *data_ptr;
3957711890bcSjc156560 
3958711890bcSjc156560 	raid_obj = calloc(1, sizeof (raid_obj_t));
3959711890bcSjc156560 	if (raid_obj == NULL)
3960711890bcSjc156560 		return (ERR_NOMEM);
3961711890bcSjc156560 
3962711890bcSjc156560 	raid_obj->obj_id = raid_obj_id_new(raid_tab);
3963711890bcSjc156560 	if (raid_obj->obj_id < OBJ_NONE)
3964711890bcSjc156560 		return (ERR_DEVICE_OVERFLOW);
3965711890bcSjc156560 
3966711890bcSjc156560 	ret = raid_obj_tab_insert(raid_tab, raid_obj->obj_id, raid_obj);
3967711890bcSjc156560 	if (ret == ERR_DEVICE_DUP) {
3968711890bcSjc156560 		free(raid_obj);
3969711890bcSjc156560 		return (ERR_DEVICE_DUP);
3970711890bcSjc156560 	}
3971711890bcSjc156560 
3972711890bcSjc156560 	data_ptr = raid_obj_attr_new(obj_type);
3973711890bcSjc156560 	if (data_ptr == NULL) {
3974711890bcSjc156560 		(void) raid_obj_delete(raid_tab, raid_obj->obj_id);
3975711890bcSjc156560 		return (ERR_NOMEM);
3976711890bcSjc156560 	}
3977711890bcSjc156560 
3978711890bcSjc156560 	(void) raid_obj_set_data_ptr(raid_tab, raid_obj->obj_id, data_ptr);
3979711890bcSjc156560 
3980711890bcSjc156560 	(void) raid_obj_set_type(raid_tab, raid_obj->obj_id, obj_type);
3981711890bcSjc156560 	return (raid_obj->obj_id);
3982711890bcSjc156560 }
3983711890bcSjc156560 
3984711890bcSjc156560 static int
3985711890bcSjc156560 raid_obj_delete(raid_obj_tab_t *raid_tab, raid_obj_id_t raid_obj_id)
3986711890bcSjc156560 {
3987711890bcSjc156560 	raid_obj_t *obj;
3988711890bcSjc156560 
3989711890bcSjc156560 	obj = raid_obj_tab_remove(raid_tab, raid_obj_id);
3990711890bcSjc156560 	if (obj != NULL) {
3991711890bcSjc156560 		free(obj->data);
3992711890bcSjc156560 		free(obj);
3993711890bcSjc156560 		return (SUCCESS);
3994711890bcSjc156560 	}
3995711890bcSjc156560 
3996711890bcSjc156560 	return (ERR_DEVICE_NOENT);
3997711890bcSjc156560 }
3998711890bcSjc156560 
3999711890bcSjc156560 static int
4000711890bcSjc156560 raid_obj_add_org(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4001711890bcSjc156560 	raid_obj_id_t container_id)
4002711890bcSjc156560 {
4003711890bcSjc156560 	raid_obj_id_t tmp, tmp1;
4004711890bcSjc156560 
4005711890bcSjc156560 	tmp = raid_obj_get_comp(raid_tab, container_id);
4006711890bcSjc156560 	if (tmp < OBJ_NONE)
4007711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4008711890bcSjc156560 
4009711890bcSjc156560 	if (tmp == OBJ_NONE) {
4010711890bcSjc156560 		(void) raid_obj_set_container(raid_tab, obj_id, container_id);
4011711890bcSjc156560 		(void) raid_obj_set_comp(raid_tab, container_id, obj_id);
4012711890bcSjc156560 		return (SUCCESS);
4013711890bcSjc156560 	}
4014711890bcSjc156560 
4015711890bcSjc156560 	while ((tmp1 = raid_obj_get_sibling(raid_tab, tmp)) != OBJ_NONE)
4016711890bcSjc156560 		tmp = tmp1;
4017711890bcSjc156560 
4018711890bcSjc156560 	if (raid_obj_set_sibling(raid_tab, tmp, obj_id) < SUCCESS)
4019711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4020711890bcSjc156560 	(void) raid_obj_set_container(raid_tab, obj_id, container_id);
4021711890bcSjc156560 
4022711890bcSjc156560 	return (SUCCESS);
4023711890bcSjc156560 }
4024711890bcSjc156560 
4025711890bcSjc156560 static raid_obj_type_id_t
4026711890bcSjc156560 raid_obj_get_type(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4027711890bcSjc156560 {
4028711890bcSjc156560 	raid_obj_t *obj;
4029711890bcSjc156560 
4030711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4031711890bcSjc156560 	if (obj == NULL)
4032711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4033711890bcSjc156560 
4034711890bcSjc156560 	if ((obj->obj_type_id < OBJ_TYPE_SYSTEM) ||
4035711890bcSjc156560 	    (obj->obj_type_id >= OBJ_TYPE_ALL))
4036711890bcSjc156560 		return (ERR_DEVICE_INVALID);
4037711890bcSjc156560 
4038711890bcSjc156560 	return (obj->obj_type_id);
4039711890bcSjc156560 }
4040711890bcSjc156560 
4041711890bcSjc156560 static int
4042711890bcSjc156560 raid_obj_set_type(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4043711890bcSjc156560 	raid_obj_type_id_t type)
4044711890bcSjc156560 {
4045711890bcSjc156560 	raid_obj_t *obj;
4046711890bcSjc156560 
4047711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4048711890bcSjc156560 	if (obj == NULL)
4049711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4050711890bcSjc156560 
4051711890bcSjc156560 	if ((type < OBJ_TYPE_SYSTEM) || (type >= OBJ_TYPE_ALL))
4052711890bcSjc156560 		return (ERR_DEVICE_TYPE);
4053711890bcSjc156560 
4054711890bcSjc156560 	obj->obj_type_id = type;
4055711890bcSjc156560 	return (SUCCESS);
4056711890bcSjc156560 }
4057711890bcSjc156560 
4058711890bcSjc156560 static raid_obj_status_t
4059711890bcSjc156560 raid_obj_get_status(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4060711890bcSjc156560 {
4061711890bcSjc156560 	raid_obj_t *obj;
4062711890bcSjc156560 
4063711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4064711890bcSjc156560 	if (obj == NULL)
4065711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4066711890bcSjc156560 
4067711890bcSjc156560 	return (obj->status);
4068711890bcSjc156560 }
4069711890bcSjc156560 
4070711890bcSjc156560 static int
4071711890bcSjc156560 raid_obj_set_status(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4072711890bcSjc156560 	raid_obj_status_t status)
4073711890bcSjc156560 {
4074711890bcSjc156560 	raid_obj_t *obj;
4075711890bcSjc156560 
4076711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4077711890bcSjc156560 	if (obj == NULL)
4078711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4079711890bcSjc156560 
4080711890bcSjc156560 	obj->status = obj->status | status;
4081711890bcSjc156560 
4082711890bcSjc156560 	return (SUCCESS);
4083711890bcSjc156560 }
4084711890bcSjc156560 
4085711890bcSjc156560 static int
4086711890bcSjc156560 raid_obj_clear_status(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4087711890bcSjc156560 	raid_obj_status_t status)
4088711890bcSjc156560 {
4089711890bcSjc156560 	raid_obj_t *obj;
4090711890bcSjc156560 
4091711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4092711890bcSjc156560 	if (obj == NULL)
4093711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4094711890bcSjc156560 
4095711890bcSjc156560 	obj->status = obj->status & ~status;
4096711890bcSjc156560 
4097711890bcSjc156560 	return (SUCCESS);
4098711890bcSjc156560 }
4099711890bcSjc156560 
4100711890bcSjc156560 static raid_obj_id_t
4101711890bcSjc156560 raid_obj_get_container(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4102711890bcSjc156560 {
4103711890bcSjc156560 	raid_obj_t *obj;
4104711890bcSjc156560 
4105711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4106711890bcSjc156560 	if (obj == NULL)
4107711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4108711890bcSjc156560 
4109711890bcSjc156560 	return (obj->container);
4110711890bcSjc156560 }
4111711890bcSjc156560 
4112711890bcSjc156560 static int
4113711890bcSjc156560 raid_obj_set_container(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4114711890bcSjc156560 	raid_obj_id_t container_id)
4115711890bcSjc156560 {
4116711890bcSjc156560 	raid_obj_t *obj;
4117711890bcSjc156560 
4118711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4119711890bcSjc156560 	if (obj == NULL)
4120711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4121711890bcSjc156560 
4122711890bcSjc156560 	obj->container = container_id;
4123711890bcSjc156560 	return (SUCCESS);
4124711890bcSjc156560 }
4125711890bcSjc156560 
4126711890bcSjc156560 static raid_obj_id_t
4127711890bcSjc156560 raid_obj_get_comp(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4128711890bcSjc156560 {
4129711890bcSjc156560 	raid_obj_t *obj;
4130711890bcSjc156560 
4131711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4132711890bcSjc156560 	if (obj == NULL)
4133711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4134711890bcSjc156560 
4135711890bcSjc156560 	return (obj->component);
4136711890bcSjc156560 }
4137711890bcSjc156560 
4138711890bcSjc156560 static int
4139711890bcSjc156560 raid_obj_set_comp(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4140711890bcSjc156560 	raid_obj_id_t comp)
4141711890bcSjc156560 {
4142711890bcSjc156560 	raid_obj_t *obj;
4143711890bcSjc156560 
4144711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4145711890bcSjc156560 	if (obj == NULL)
4146711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4147711890bcSjc156560 
4148711890bcSjc156560 	obj->component = comp;
4149711890bcSjc156560 	return (SUCCESS);
4150711890bcSjc156560 }
4151711890bcSjc156560 
4152711890bcSjc156560 static raid_obj_id_t
4153711890bcSjc156560 raid_obj_get_sibling(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4154711890bcSjc156560 {
4155711890bcSjc156560 	raid_obj_t *obj;
4156711890bcSjc156560 
4157711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4158711890bcSjc156560 	if (obj == NULL)
4159711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4160711890bcSjc156560 
4161711890bcSjc156560 	return (obj->sibling);
4162711890bcSjc156560 }
4163711890bcSjc156560 
4164711890bcSjc156560 static int
4165711890bcSjc156560 raid_obj_set_sibling(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4166711890bcSjc156560 	raid_obj_id_t sibling)
4167711890bcSjc156560 {
4168711890bcSjc156560 	raid_obj_t *obj;
4169711890bcSjc156560 
4170711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4171711890bcSjc156560 	if (obj == NULL)
4172711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4173711890bcSjc156560 
4174711890bcSjc156560 	obj->sibling = sibling;
4175711890bcSjc156560 
4176711890bcSjc156560 	return (SUCCESS);
4177711890bcSjc156560 }
4178711890bcSjc156560 
4179711890bcSjc156560 static void *
4180711890bcSjc156560 raid_obj_get_data_ptr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4181711890bcSjc156560 {
4182711890bcSjc156560 	raid_obj_t *obj;
4183711890bcSjc156560 
4184711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4185711890bcSjc156560 	if (obj == NULL)
4186711890bcSjc156560 		return (NULL);
4187711890bcSjc156560 
4188711890bcSjc156560 	return (obj->data);
4189711890bcSjc156560 }
4190711890bcSjc156560 
4191711890bcSjc156560 static int
4192711890bcSjc156560 raid_obj_set_data_ptr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4193711890bcSjc156560 	void *data)
4194711890bcSjc156560 {
4195711890bcSjc156560 	raid_obj_t *obj;
4196711890bcSjc156560 
4197711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4198711890bcSjc156560 	if (obj == NULL)
4199711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4200711890bcSjc156560 
4201711890bcSjc156560 	obj->data = data;
4202711890bcSjc156560 
4203711890bcSjc156560 	return (SUCCESS);
4204711890bcSjc156560 }
4205711890bcSjc156560 
4206711890bcSjc156560 static raid_obj_handle_t
4207711890bcSjc156560 raid_obj_get_handle(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4208711890bcSjc156560 {
4209711890bcSjc156560 	raid_obj_t *obj;
4210711890bcSjc156560 
4211711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4212711890bcSjc156560 	if (obj == NULL)
4213711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4214711890bcSjc156560 
4215711890bcSjc156560 	return (obj->handle);
4216711890bcSjc156560 }
4217711890bcSjc156560 
4218711890bcSjc156560 static int
4219711890bcSjc156560 raid_obj_set_handle(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4220711890bcSjc156560 	raid_obj_handle_t handle)
4221711890bcSjc156560 {
4222711890bcSjc156560 	raid_obj_t *obj;
4223711890bcSjc156560 
4224711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4225711890bcSjc156560 	if (obj == NULL)
4226711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4227711890bcSjc156560 
4228711890bcSjc156560 	obj->handle = handle;
4229711890bcSjc156560 	return (SUCCESS);
4230711890bcSjc156560 }
4231711890bcSjc156560 /*
4232711890bcSjc156560  * Object list maintennance routines
4233711890bcSjc156560  */
4234711890bcSjc156560 static void
4235711890bcSjc156560 raid_list_create(raid_list_t *list, size_t offset)
4236711890bcSjc156560 {
4237711890bcSjc156560 	list->head = NULL;
4238711890bcSjc156560 	list->tail = NULL;
4239711890bcSjc156560 	list->offset = offset;
4240711890bcSjc156560 }
4241711890bcSjc156560 
4242711890bcSjc156560 static void *
4243711890bcSjc156560 raid_list_head(raid_list_t *list)
4244711890bcSjc156560 {
4245711890bcSjc156560 	return (list->head);
4246711890bcSjc156560 }
4247711890bcSjc156560 
4248711890bcSjc156560 static void *
4249711890bcSjc156560 raid_list_next(raid_list_t *list, void *obj)
4250711890bcSjc156560 {
4251711890bcSjc156560 	raid_list_el_t *el = LIST_OBJ_TO_EL(list, obj);
4252711890bcSjc156560 
4253711890bcSjc156560 	return (el->next);
4254711890bcSjc156560 }
4255711890bcSjc156560 
4256711890bcSjc156560 static void
4257711890bcSjc156560 raid_list_insert_tail(raid_list_t *list, void *obj)
4258711890bcSjc156560 {
4259711890bcSjc156560 	raid_list_el_t *el = LIST_OBJ_TO_EL(list, obj), *el1;
4260711890bcSjc156560 
4261711890bcSjc156560 	el->prev = list->tail;
4262711890bcSjc156560 	list->tail = obj;
4263711890bcSjc156560 
4264711890bcSjc156560 	el->next = NULL;
4265711890bcSjc156560 
4266711890bcSjc156560 	if (list->head == NULL)
4267711890bcSjc156560 		list->head = obj;
4268711890bcSjc156560 
4269711890bcSjc156560 	if (el->prev != NULL) {
4270711890bcSjc156560 		el1 = LIST_OBJ_TO_EL(list, el->prev);
4271711890bcSjc156560 		el1->next = obj;
4272711890bcSjc156560 	}
4273711890bcSjc156560 }
4274711890bcSjc156560 
4275711890bcSjc156560 static void
4276711890bcSjc156560 raid_list_remove(raid_list_t *list, void *obj)
4277711890bcSjc156560 {
4278711890bcSjc156560 	raid_list_el_t *el = LIST_OBJ_TO_EL(list, obj), *el1;
4279711890bcSjc156560 
4280711890bcSjc156560 	if (list->head == obj)
4281711890bcSjc156560 		list->head = el->next;
4282711890bcSjc156560 
4283711890bcSjc156560 	if (list->tail == obj)
4284711890bcSjc156560 		list->tail = el->prev;
4285711890bcSjc156560 
4286711890bcSjc156560 	if (el->next != NULL) {
4287711890bcSjc156560 		el1 = LIST_OBJ_TO_EL(list, el->next);
4288711890bcSjc156560 		el1->prev = el->prev;
4289711890bcSjc156560 	}
4290711890bcSjc156560 
4291711890bcSjc156560 	if (el->prev != NULL) {
4292711890bcSjc156560 		el1 = LIST_OBJ_TO_EL(list, el->prev);
4293711890bcSjc156560 		el1->next = el->next;
4294711890bcSjc156560 	}
4295711890bcSjc156560 
4296711890bcSjc156560 	el->prev = el->next = NULL;
4297711890bcSjc156560 }
4298711890bcSjc156560 
4299711890bcSjc156560 static void *
4300711890bcSjc156560 raid_list_remove_head(raid_list_t *list)
4301711890bcSjc156560 {
4302711890bcSjc156560 	void *obj = list->head;
4303711890bcSjc156560 
4304711890bcSjc156560 	if (obj != NULL)
4305711890bcSjc156560 		raid_list_remove(list, obj);
4306711890bcSjc156560 
4307711890bcSjc156560 	return (obj);
4308711890bcSjc156560 }
4309711890bcSjc156560 
4310711890bcSjc156560 static void *
4311711890bcSjc156560 raid_list_find(raid_list_t *list, raid_obj_id_t obj_id)
4312711890bcSjc156560 {
4313711890bcSjc156560 	raid_obj_t *obj;
4314711890bcSjc156560 
4315711890bcSjc156560 	for (obj = raid_list_head(list); obj != NULL;
4316711890bcSjc156560 	    obj = raid_list_next(list, obj))
4317711890bcSjc156560 			if (obj->obj_id == obj_id)
4318711890bcSjc156560 				break;
4319711890bcSjc156560 
4320711890bcSjc156560 	return (obj);
4321711890bcSjc156560 }
4322711890bcSjc156560 
4323711890bcSjc156560 static int
4324711890bcSjc156560 raid_obj_tab_create(raid_obj_tab_t *tab, size_t hash_slots)
4325711890bcSjc156560 {
4326711890bcSjc156560 	unsigned i;
4327711890bcSjc156560 
4328711890bcSjc156560 	if (hash_slots == 0)
4329711890bcSjc156560 		return (ERR_OP_ILLEGAL);
4330711890bcSjc156560 
4331711890bcSjc156560 	tab->slots = hash_slots;
4332711890bcSjc156560 
4333711890bcSjc156560 	if ((tab->table = calloc(hash_slots, sizeof (raid_list_t))) == NULL)
4334711890bcSjc156560 		return (ERR_NOMEM);
4335711890bcSjc156560 
4336711890bcSjc156560 	for (i = 0; i < hash_slots; i++)
4337711890bcSjc156560 		raid_list_create(&tab->table[i], offsetof(raid_obj_t, el));
4338711890bcSjc156560 
4339711890bcSjc156560 	return (SUCCESS);
4340711890bcSjc156560 }
4341711890bcSjc156560 
4342711890bcSjc156560 static void
4343711890bcSjc156560 raid_obj_tab_destroy(raid_obj_tab_t *tab)
4344711890bcSjc156560 {
4345711890bcSjc156560 	unsigned i;
4346711890bcSjc156560 
4347711890bcSjc156560 	for (i = 0; i < tab->slots; i++) {
4348711890bcSjc156560 		struct raid_obj_t *obj;
4349711890bcSjc156560 
4350711890bcSjc156560 		while ((obj = raid_list_remove_head(&tab->table[i])) != NULL)
4351711890bcSjc156560 			free(obj);
4352711890bcSjc156560 
4353711890bcSjc156560 		raid_list_destroy(&tab->table[i]);
4354711890bcSjc156560 	}
4355711890bcSjc156560 
4356711890bcSjc156560 	if (tab->table)
4357711890bcSjc156560 		free(tab->table);
4358711890bcSjc156560 
4359711890bcSjc156560 	tab->table = NULL;
4360711890bcSjc156560 	tab->slots = 0;
4361711890bcSjc156560 	tab->obj_id_cnt = 0;
4362711890bcSjc156560 }
4363711890bcSjc156560 
4364711890bcSjc156560 static int
4365711890bcSjc156560 raid_obj_tab_insert(raid_obj_tab_t *tab, raid_obj_id_t id, void *obj)
4366711890bcSjc156560 {
4367711890bcSjc156560 	raid_list_t *list;
4368711890bcSjc156560 
4369711890bcSjc156560 	list = OBJ_TAB_SLOT(tab, id);
4370711890bcSjc156560 
4371711890bcSjc156560 	if (raid_list_find(list, id) != NULL)
4372711890bcSjc156560 		return (ERR_DEVICE_DUP);
4373711890bcSjc156560 
4374711890bcSjc156560 	raid_list_insert_tail(list, obj);
4375711890bcSjc156560 
4376711890bcSjc156560 	return (SUCCESS);
4377711890bcSjc156560 }
4378711890bcSjc156560 
4379711890bcSjc156560 static void *
4380711890bcSjc156560 raid_obj_tab_remove(raid_obj_tab_t *tab, raid_obj_id_t id)
4381711890bcSjc156560 {
4382711890bcSjc156560 	raid_list_t *list;
4383711890bcSjc156560 	raid_obj_t *obj;
4384711890bcSjc156560 
4385711890bcSjc156560 	list = OBJ_TAB_SLOT(tab, id);
4386711890bcSjc156560 
4387711890bcSjc156560 	if ((obj = raid_list_find(list, id)) != NULL)
4388711890bcSjc156560 		raid_list_remove(list, obj);
4389711890bcSjc156560 
4390711890bcSjc156560 	return (obj);
4391711890bcSjc156560 }
4392711890bcSjc156560 
4393711890bcSjc156560 static void *
4394711890bcSjc156560 raid_obj_tab_find(raid_obj_tab_t *tab, raid_obj_id_t id)
4395711890bcSjc156560 {
4396711890bcSjc156560 	raid_list_t *list;
4397711890bcSjc156560 	raid_obj_t *obj;
4398711890bcSjc156560 
4399711890bcSjc156560 	list = OBJ_TAB_SLOT(tab, id);
4400711890bcSjc156560 	obj = raid_list_find(list, id);
4401711890bcSjc156560 
4402711890bcSjc156560 	return (obj);
4403711890bcSjc156560 }
4404711890bcSjc156560 
4405711890bcSjc156560 static void
4406711890bcSjc156560 raid_list_destroy(raid_list_t *list)
4407711890bcSjc156560 {
4408711890bcSjc156560 	list->head = NULL;
4409711890bcSjc156560 	list->tail = NULL;
4410711890bcSjc156560 	list->offset = 0;
4411711890bcSjc156560 }
4412711890bcSjc156560 
4413711890bcSjc156560 /*
4414711890bcSjc156560  * Plug-in maintennance routines
4415711890bcSjc156560  */
4416711890bcSjc156560 static int
4417711890bcSjc156560 controller_id_to_path(uint32_t controller_id, char *path)
4418711890bcSjc156560 {
4419317fb4acSYu-Bo Ryan Wang 	int fd;
4420711890bcSjc156560 	char buf[MAX_PATH_LEN] = {0}, buf1[MAX_PATH_LEN] = {0}, *colon;
4421711890bcSjc156560 
4422711890bcSjc156560 	(void) snprintf(buf, MAX_PATH_LEN, "%s/c%d", CFGDIR, controller_id);
4423711890bcSjc156560 	if (readlink(buf, buf1, sizeof (buf1)) < 0)
4424711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
4425711890bcSjc156560 
4426711890bcSjc156560 	if (buf1[0] != '/')
4427711890bcSjc156560 		(void) snprintf(buf, sizeof (buf), "%s/", CFGDIR);
4428711890bcSjc156560 	else
4429711890bcSjc156560 		buf[0] = 0;
4430711890bcSjc156560 	(void) strlcat(buf, buf1, MAX_PATH_LEN);
4431711890bcSjc156560 
4432711890bcSjc156560 	colon = strrchr(buf, ':');
4433711890bcSjc156560 	if (colon == NULL)
4434711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
4435711890bcSjc156560 	else
4436711890bcSjc156560 		*colon = 0;
4437711890bcSjc156560 
4438711890bcSjc156560 	(void) snprintf(path, MAX_PATH_LEN, "%s:devctl", buf);
4439711890bcSjc156560 
4440317fb4acSYu-Bo Ryan Wang 	fd = open(path, O_RDONLY | O_NDELAY);
4441317fb4acSYu-Bo Ryan Wang 
4442317fb4acSYu-Bo Ryan Wang 	if (fd < 0)
4443711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
4444711890bcSjc156560 
4445317fb4acSYu-Bo Ryan Wang 	(void) close(fd);
4446317fb4acSYu-Bo Ryan Wang 
4447711890bcSjc156560 	return (SUCCESS);
4448711890bcSjc156560 }
4449711890bcSjc156560 
4450711890bcSjc156560 static char *
4451711890bcSjc156560 controller_id_to_driver_name(uint32_t controller_id)
4452711890bcSjc156560 {
4453711890bcSjc156560 	char buf[MAX_PATH_LEN];
4454711890bcSjc156560 	di_node_t di_node;
4455711890bcSjc156560 	char *name, *tmp;
4456711890bcSjc156560 	int ret;
4457711890bcSjc156560 
4458711890bcSjc156560 	ret = controller_id_to_path(controller_id, buf);
4459711890bcSjc156560 	if (ret < SUCCESS)
4460711890bcSjc156560 		return (NULL);
4461711890bcSjc156560 
4462711890bcSjc156560 	tmp = strrchr(buf, ':');
4463711890bcSjc156560 	if (tmp != NULL)
4464711890bcSjc156560 		*tmp = 0;
4465711890bcSjc156560 
4466711890bcSjc156560 	tmp = strstr(buf, "pci");
4467711890bcSjc156560 	if (tmp == NULL)
4468711890bcSjc156560 		return (NULL);
4469711890bcSjc156560 
4470711890bcSjc156560 	di_node = di_init(tmp, DINFOPROP);
4471711890bcSjc156560 	if (di_node == DI_NODE_NIL)
4472711890bcSjc156560 		return (NULL);
4473711890bcSjc156560 
4474711890bcSjc156560 	name = di_driver_name(di_node);
4475711890bcSjc156560 
4476711890bcSjc156560 	return (name);
4477711890bcSjc156560 }
4478711890bcSjc156560 
4479711890bcSjc156560 static void
4480711890bcSjc156560 raid_plugin_init()
4481711890bcSjc156560 {
4482711890bcSjc156560 	raid_lib_t *raid_lib = raid_lib_sys;
4483711890bcSjc156560 
4484711890bcSjc156560 	while (raid_lib) {
4485711890bcSjc156560 		raid_lib_sys = raid_lib->next;
4486711890bcSjc156560 		(void) dlclose(raid_lib->lib_handle);
4487711890bcSjc156560 		free(raid_lib);
4488711890bcSjc156560 		raid_lib = raid_lib_sys;
4489711890bcSjc156560 	}
4490711890bcSjc156560 }
4491711890bcSjc156560 
4492711890bcSjc156560 static raid_lib_t *
4493711890bcSjc156560 raid_plugin_load(char *driver_name)
4494711890bcSjc156560 {
4495711890bcSjc156560 	char buf[MAX_PATH_LEN] = {0};
4496711890bcSjc156560 	raid_lib_t *supplib;
4497711890bcSjc156560 	void *sym;
4498711890bcSjc156560 
4499711890bcSjc156560 	supplib = calloc(1, sizeof (raid_lib_t));
4500711890bcSjc156560 	if (supplib == NULL)
4501711890bcSjc156560 		return (NULL);
4502711890bcSjc156560 
4503711890bcSjc156560 	(void) snprintf(buf, MAX_PATH_LEN, "%s/%s.so.1",
4504711890bcSjc156560 	    SUPP_PLUGIN_DIR, driver_name);
4505711890bcSjc156560 
4506711890bcSjc156560 	supplib->lib_handle = dlopen(buf, RTLD_LAZY);
4507711890bcSjc156560 	if (supplib->lib_handle == NULL) {
4508711890bcSjc156560 		free(supplib);
4509711890bcSjc156560 		return (NULL);
4510711890bcSjc156560 	}
4511711890bcSjc156560 
4512711890bcSjc156560 	supplib->name = driver_name;
4513711890bcSjc156560 
4514711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_version")) == NULL)
4515711890bcSjc156560 		supplib->version = RDCFG_PLUGIN_V1;
4516711890bcSjc156560 	else {
4517711890bcSjc156560 		supplib->version = *((uint32_t *)sym);
4518711890bcSjc156560 		if (supplib->version != RDCFG_PLUGIN_V1) {
4519711890bcSjc156560 			(void) dlclose(supplib->lib_handle);
4520711890bcSjc156560 			free(supplib);
4521711890bcSjc156560 			return (NULL);
4522711890bcSjc156560 		}
4523711890bcSjc156560 	}
4524711890bcSjc156560 
4525711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_open_controller")) ==
4526711890bcSjc156560 	    NULL) {
4527711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4528711890bcSjc156560 		free(supplib);
4529711890bcSjc156560 		return (NULL);
4530711890bcSjc156560 	} else
4531711890bcSjc156560 		supplib->open_controller = (int(*)(uint32_t, char **))sym;
4532711890bcSjc156560 
4533711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_close_controller")) ==
4534711890bcSjc156560 	    NULL) {
4535711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4536711890bcSjc156560 		free(supplib);
4537711890bcSjc156560 		return (NULL);
4538711890bcSjc156560 	} else
4539711890bcSjc156560 		supplib->close_controller = (int (*)(uint32_t, char **))sym;
4540711890bcSjc156560 
4541711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_compnum")) == NULL) {
4542711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4543711890bcSjc156560 		free(supplib);
4544711890bcSjc156560 		return (NULL);
4545711890bcSjc156560 	} else
4546711890bcSjc156560 		supplib->compnum = (int (*)(uint32_t, uint32_t,
4547711890bcSjc156560 		    raid_obj_type_id_t, raid_obj_type_id_t))sym;
4548711890bcSjc156560 
4549711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_complist")) == NULL) {
4550711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4551711890bcSjc156560 		free(supplib);
4552711890bcSjc156560 		return (NULL);
4553711890bcSjc156560 	} else
4554711890bcSjc156560 		supplib->complist = (int (*)(uint32_t, uint32_t,
4555711890bcSjc156560 		    raid_obj_type_id_t, raid_obj_type_id_t, int, void *))sym;
4556711890bcSjc156560 
4557711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_get_attr")) == NULL) {
4558711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4559711890bcSjc156560 		free(supplib);
4560711890bcSjc156560 		return (NULL);
4561711890bcSjc156560 	} else
4562711890bcSjc156560 		supplib->get_attr = (int (*)(uint32_t, uint32_t, uint32_t,
4563711890bcSjc156560 		    raid_obj_type_id_t, void*))sym;
4564711890bcSjc156560 
4565711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_array_create")) == NULL) {
4566711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4567711890bcSjc156560 		free(supplib);
4568711890bcSjc156560 		return (NULL);
4569711890bcSjc156560 	} else
4570711890bcSjc156560 		supplib->array_create = (int (*)(uint32_t, array_attr_t *, int,
4571711890bcSjc156560 		    arraypart_attr_t *, char **))sym;
4572711890bcSjc156560 
4573711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_array_delete")) == NULL) {
4574711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4575711890bcSjc156560 		free(supplib);
4576711890bcSjc156560 		return (NULL);
4577711890bcSjc156560 	} else
4578711890bcSjc156560 		supplib->array_delete =
4579711890bcSjc156560 		    (int (*)(uint32_t, uint32_t, char **))sym;
4580711890bcSjc156560 
45815c9d25d2SYu-Bo Ryan Wang 	supplib->hsp_bind = (int (*)(uint32_t, hsp_relation_t *,
4582711890bcSjc156560 	    char **))dlsym(supplib->lib_handle, "rdcfg_hsp_bind");
45835c9d25d2SYu-Bo Ryan Wang 	supplib->hsp_unbind = (int (*)(uint32_t, hsp_relation_t *,
4584711890bcSjc156560 	    char **))dlsym(supplib->lib_handle, "rdcfg_hsp_unbind");
4585711890bcSjc156560 	supplib->set_attr = (int (*)(uint32_t, uint32_t, uint32_t, uint32_t *,
4586711890bcSjc156560 	    char **))dlsym(supplib->lib_handle, "rdcfg_set_attr");
4587711890bcSjc156560 	supplib->flash_fw = (int (*)(uint32_t, char *, uint32_t, char **))
4588711890bcSjc156560 	    dlsym(supplib->lib_handle, "rdcfg_flash_fw");
4589711890bcSjc156560 
4590711890bcSjc156560 	supplib->next = raid_lib_sys;
4591711890bcSjc156560 	raid_lib_sys = supplib;
4592711890bcSjc156560 	return (supplib);
4593711890bcSjc156560 }
4594711890bcSjc156560 
4595711890bcSjc156560 static raid_lib_t *
4596711890bcSjc156560 raid_find_lib(raid_obj_tab_t *raid_tab, raid_obj_id_t controller_obj_id)
4597711890bcSjc156560 {
4598711890bcSjc156560 	controller_attr_t *controller_attr;
4599711890bcSjc156560 	raid_lib_t *raid_lib;
4600711890bcSjc156560 	char *driver_name;
4601711890bcSjc156560 	raid_obj_handle_t handle;
4602711890bcSjc156560 
4603711890bcSjc156560 	/* Check if it's mapped to handle structure */
4604711890bcSjc156560 	handle = raid_obj_to_handle(raid_tab, controller_obj_id);
4605711890bcSjc156560 	if (raid_handle_sys.handles[handle].raid_lib != NULL)
4606711890bcSjc156560 		return (raid_handle_sys.handles[handle].raid_lib);
4607711890bcSjc156560 
4608711890bcSjc156560 	(void) obj_get_attr(raid_tab, controller_obj_id,
4609711890bcSjc156560 	    (void **)(&controller_attr));
4610711890bcSjc156560 
4611711890bcSjc156560 	/* Check if the plugin module is already loaded */
4612711890bcSjc156560 	driver_name = controller_id_to_driver_name(
4613711890bcSjc156560 	    controller_attr->controller_id);
4614711890bcSjc156560 	if (driver_name == NULL)
4615711890bcSjc156560 		return (NULL);
4616711890bcSjc156560 
4617711890bcSjc156560 	raid_lib = raid_lib_sys;
4618711890bcSjc156560 	while (raid_lib != NULL) {
4619711890bcSjc156560 		if (raid_lib->name != NULL &&
4620711890bcSjc156560 		    strcmp(driver_name, raid_lib->name) == 0)
4621711890bcSjc156560 			return (raid_lib);
4622711890bcSjc156560 
4623711890bcSjc156560 		raid_lib = raid_lib->next;
4624711890bcSjc156560 	}
4625711890bcSjc156560 
4626711890bcSjc156560 	/* Loading the plugin module */
4627711890bcSjc156560 	raid_lib = raid_plugin_load(driver_name);
4628711890bcSjc156560 
4629711890bcSjc156560 	return (raid_lib);
4630711890bcSjc156560 }
4631