xref: /titanic_50/usr/src/lib/libraidcfg/common/raidcfg.c (revision 5c9d25d25ae7531d61aca4904f76e3dae2f457bf)
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 /*
2372e1c055Sjc156560  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24711890bcSjc156560  * Use is subject to license terms.
25711890bcSjc156560  */
26711890bcSjc156560 
27711890bcSjc156560 #include <fcntl.h>
28711890bcSjc156560 #include <sys/types.h>
29711890bcSjc156560 #include <sys/stat.h>
30711890bcSjc156560 #include <stddef.h>
31711890bcSjc156560 #include <stdlib.h>
32711890bcSjc156560 #include <dirent.h>
33711890bcSjc156560 #include <dlfcn.h>
34711890bcSjc156560 #include <link.h>
35711890bcSjc156560 #include <strings.h>
36711890bcSjc156560 #include <stdio.h>
37711890bcSjc156560 #include <unistd.h>
38711890bcSjc156560 #include <sys/mnttab.h>
39711890bcSjc156560 #include <config_admin.h>
40711890bcSjc156560 #include <sys/param.h>
41711890bcSjc156560 #include <libintl.h>
42711890bcSjc156560 #include <libdevinfo.h>
43711890bcSjc156560 #include <raidcfg.h>
44711890bcSjc156560 #include <thread.h>
45711890bcSjc156560 #include <synch.h>
46711890bcSjc156560 
47711890bcSjc156560 #ifndef TEXT_DOMAIN
48711890bcSjc156560 #define	TEXT_DOMAIN	"SYS_TEST"
49711890bcSjc156560 #endif
50711890bcSjc156560 
51711890bcSjc156560 #define	HASH_SLOTS	16
52711890bcSjc156560 #define	HANDLER_SLOTS	256
53711890bcSjc156560 
54711890bcSjc156560 /*
55711890bcSjc156560  * Raid object status;
56711890bcSjc156560  */
57711890bcSjc156560 #define	OBJ_STATUS_CMD_CLEAN	-1
58711890bcSjc156560 #define	OBJ_STATUS_OPENED	1
59711890bcSjc156560 #define	OBJ_STATUS_SCANCOMP	1 << 1
60711890bcSjc156560 
61711890bcSjc156560 #if defined(__sparcv9)
62711890bcSjc156560 #define	SUPP_PLUGIN_DIR	"/usr/lib/raidcfg/sparcv9"
63711890bcSjc156560 #elif defined(__amd64)
64711890bcSjc156560 #define	SUPP_PLUGIN_DIR	"/usr/lib/raidcfg/amd64"
65711890bcSjc156560 #else
66711890bcSjc156560 #define	SUPP_PLUGIN_DIR	"/usr/lib/raidcfg"
67711890bcSjc156560 #endif
68711890bcSjc156560 
69711890bcSjc156560 /*
70711890bcSjc156560  * Basic types
71711890bcSjc156560  */
72711890bcSjc156560 typedef	int raid_obj_id_t;
73711890bcSjc156560 typedef	int raid_obj_status_t;
74711890bcSjc156560 
75711890bcSjc156560 /*
76711890bcSjc156560  * Data structures used for object maintennance
77711890bcSjc156560  */
78711890bcSjc156560 typedef	struct {
79711890bcSjc156560 	void *head;
80711890bcSjc156560 	void *tail;
81711890bcSjc156560 	size_t offset;	/* offset of double-linked element (raid_list_el_t) */
82711890bcSjc156560 			/* in the linked data structures (objects) */
83711890bcSjc156560 } raid_list_t;
84711890bcSjc156560 
85711890bcSjc156560 typedef	struct {
86711890bcSjc156560 	void *prev;
87711890bcSjc156560 	void *next;
88711890bcSjc156560 } raid_list_el_t;
89711890bcSjc156560 
90711890bcSjc156560 typedef	struct {
91711890bcSjc156560 	raid_obj_id_t obj_id_cnt;	/* id 0 is reserved */
92711890bcSjc156560 	size_t slots;			/* How many lists linked by *table */
93711890bcSjc156560 	raid_list_t *table;
94711890bcSjc156560 } raid_obj_tab_t;
95711890bcSjc156560 
96711890bcSjc156560 /*
97711890bcSjc156560  * Object type structure containing function pointers;
98711890bcSjc156560  */
99711890bcSjc156560 typedef	struct {
100711890bcSjc156560 	int (*compnum)(raid_obj_tab_t *, raid_obj_id_t, raid_obj_type_id_t);
101711890bcSjc156560 	int (*complist)(raid_obj_tab_t *, raid_obj_id_t, int, raid_obj_id_t *,
102711890bcSjc156560 		raid_obj_type_id_t);
103711890bcSjc156560 	int (*get_attr)(raid_obj_tab_t *, raid_obj_id_t);
104711890bcSjc156560 	int (*set_attr)(raid_obj_tab_t *, raid_obj_id_t, uint32_t, uint32_t *,
105711890bcSjc156560 		char **);
106711890bcSjc156560 	int (*act)(raid_obj_tab_t *, raid_obj_id_t, uint32_t, void *, char **);
107711890bcSjc156560 	int (*create_obj)(raid_obj_tab_t *, raid_obj_id_t, int,
108711890bcSjc156560 		raid_obj_id_t *, char **);
109711890bcSjc156560 	int (*delete_obj)(raid_obj_tab_t *, raid_obj_id_t, char **);
110*5c9d25d2SYu-Bo Ryan Wang 	int (*bind_obj)(raid_obj_tab_t *, raid_obj_id_t *, char **);
111*5c9d25d2SYu-Bo Ryan Wang 	int (*unbind_obj)(raid_obj_tab_t *, raid_obj_id_t *, char **);
112711890bcSjc156560 } raid_obj_op_t;
113711890bcSjc156560 
114711890bcSjc156560 /*
115711890bcSjc156560  * Common object data structure
116711890bcSjc156560  */
117711890bcSjc156560 typedef	struct {
118711890bcSjc156560 	raid_list_el_t		el;	/* double-links */
119711890bcSjc156560 
120711890bcSjc156560 	raid_obj_type_id_t	obj_type_id;
121711890bcSjc156560 	raid_obj_id_t		obj_id;
122711890bcSjc156560 	raid_obj_status_t	status;
123711890bcSjc156560 
124711890bcSjc156560 	raid_obj_id_t		container;
125711890bcSjc156560 	raid_obj_id_t		sibling;
126711890bcSjc156560 	raid_obj_id_t		component;
127711890bcSjc156560 
128711890bcSjc156560 	void			*data;	/* Pointer to attribute structure */
129711890bcSjc156560 	raid_obj_handle_t	handle;
130711890bcSjc156560 } raid_obj_t;
131711890bcSjc156560 
132711890bcSjc156560 /*
133711890bcSjc156560  * Definition about handle
134711890bcSjc156560  */
135711890bcSjc156560 typedef	struct {
136711890bcSjc156560 	uint32_t	next;
137711890bcSjc156560 	uint32_t	type;
138711890bcSjc156560 	uint32_t	controller_id;
139711890bcSjc156560 	uint32_t	array_id;
140711890bcSjc156560 	uint32_t	disk_id;
141711890bcSjc156560 	uint64_t	seq_id;
142711890bcSjc156560 	uint32_t	task_id;
143b449fa8aSyw161884 	uint32_t	prop_id;
144711890bcSjc156560 	uint32_t	fd;		/* Only for controller */
145711890bcSjc156560 	raid_lib_t	*raid_lib;	/* Only for controller */
146711890bcSjc156560 } handle_attr_t;
147711890bcSjc156560 
148711890bcSjc156560 #define	LIST_OBJ_TO_EL(list, obj)	\
149711890bcSjc156560 	((void *)((char *)(obj) + (list)->offset))
150711890bcSjc156560 #define	OBJ_TAB_SLOT(tab, id)	\
151711890bcSjc156560 	((tab)->table + ((id)%(tab)->slots))
152711890bcSjc156560 
153711890bcSjc156560 #pragma init(raidcfg_init)
154711890bcSjc156560 #pragma fini(raidcfg_fini)
155711890bcSjc156560 
156711890bcSjc156560 /*
157711890bcSjc156560  * Function prototypes
158711890bcSjc156560  */
159711890bcSjc156560 static int intcompare(const void *p1, const void *p2);
160711890bcSjc156560 static uint64_t raid_space_noalign(raid_obj_tab_t *, uint32_t, int,
161711890bcSjc156560 	raid_obj_id_t *, arraypart_attr_t *);
162711890bcSjc156560 static int raid_handle_init();
163711890bcSjc156560 static void raid_handle_fini();
164711890bcSjc156560 static raid_obj_handle_t raid_handle_new(raid_obj_type_id_t);
165711890bcSjc156560 static void raid_handle_delete(raid_obj_handle_t);
166711890bcSjc156560 static void raid_handle_delete_controller_comp(uint32_t);
167711890bcSjc156560 static raid_obj_id_t raid_handle_to_obj(raid_obj_tab_t *,
168711890bcSjc156560 	raid_obj_handle_t);
169711890bcSjc156560 static raid_obj_handle_t raid_obj_to_handle(raid_obj_tab_t *,
170711890bcSjc156560 	raid_obj_id_t);
171711890bcSjc156560 static raid_lib_t *raid_obj_get_lib(raid_obj_tab_t *, raid_obj_id_t);
172711890bcSjc156560 static int raid_obj_set_lib(raid_obj_tab_t *, raid_obj_id_t, raid_lib_t *);
173711890bcSjc156560 static int raid_obj_get_fd(raid_obj_tab_t *, raid_obj_id_t);
174711890bcSjc156560 static int raid_obj_set_fd(raid_obj_tab_t *, raid_obj_id_t, int);
175711890bcSjc156560 static int obj_scan_comp(raid_obj_tab_t *, raid_obj_id_t);
176711890bcSjc156560 static int obj_rescan(raid_obj_tab_t *);
177711890bcSjc156560 static raid_obj_id_t obj_get_comp(raid_obj_tab_t *, raid_obj_id_t,
178711890bcSjc156560 	raid_obj_type_id_t);
179711890bcSjc156560 static raid_obj_id_t obj_get_sibling(raid_obj_tab_t *, raid_obj_id_t);
180711890bcSjc156560 static int obj_get_attr(raid_obj_tab_t *, raid_obj_id_t, void **);
181711890bcSjc156560 static raid_obj_id_t obj_locate_controller(raid_obj_tab_t *, uint32_t);
182711890bcSjc156560 static raid_obj_id_t obj_locate_array(raid_obj_tab_t *, uint32_t, uint32_t);
183711890bcSjc156560 static raid_obj_id_t obj_locate_array_recur(raid_obj_tab_t *, raid_obj_id_t,
184711890bcSjc156560 	uint32_t);
185711890bcSjc156560 static raid_obj_id_t obj_locate_hsp(raid_obj_tab_t *, uint32_t,
186711890bcSjc156560 	uint32_t, uint32_t);
187711890bcSjc156560 static raid_obj_id_t obj_locate_disk(raid_obj_tab_t *, uint32_t, uint32_t);
188711890bcSjc156560 static raid_obj_id_t obj_locate_arraypart(raid_obj_tab_t *, uint32_t,
189711890bcSjc156560 	uint32_t, uint32_t);
190711890bcSjc156560 static raid_obj_id_t obj_locate_diskseg(raid_obj_tab_t *, uint32_t,
191711890bcSjc156560 	uint32_t, uint32_t);
192711890bcSjc156560 static raid_obj_id_t obj_locate_task(raid_obj_tab_t *, uint32_t, uint32_t);
193b449fa8aSyw161884 static raid_obj_id_t obj_locate_prop(raid_obj_tab_t *, uint32_t, uint32_t,
194b449fa8aSyw161884 	uint32_t);
195711890bcSjc156560 static raid_obj_id_t obj_get_controller(raid_obj_tab_t *, raid_obj_id_t);
196711890bcSjc156560 
197711890bcSjc156560 static int obj_sys_compnum(raid_obj_tab_t *, raid_obj_id_t,
198711890bcSjc156560 	raid_obj_type_id_t);
199711890bcSjc156560 static int obj_sys_complist(raid_obj_tab_t *, raid_obj_id_t, int,
200711890bcSjc156560 	raid_obj_id_t *, raid_obj_type_id_t);
201711890bcSjc156560 static int obj_controller_compnum(raid_obj_tab_t *, raid_obj_id_t,
202711890bcSjc156560 	raid_obj_type_id_t);
203711890bcSjc156560 static int obj_controller_complist(raid_obj_tab_t *, raid_obj_id_t, int,
204711890bcSjc156560 	raid_obj_id_t *, raid_obj_type_id_t);
205711890bcSjc156560 static int obj_controller_get_attr(raid_obj_tab_t *, raid_obj_id_t);
206711890bcSjc156560 static int obj_controller_act(raid_obj_tab_t *, raid_obj_id_t,
207711890bcSjc156560 	uint32_t, void *, char **);
208711890bcSjc156560 static int obj_array_compnum(raid_obj_tab_t *, raid_obj_id_t,
209711890bcSjc156560 	raid_obj_type_id_t);
210711890bcSjc156560 static int obj_array_complist(raid_obj_tab_t *, raid_obj_id_t, int,
211711890bcSjc156560 	raid_obj_id_t *, raid_obj_type_id_t);
212711890bcSjc156560 static int obj_array_get_attr(raid_obj_tab_t *, raid_obj_id_t);
213711890bcSjc156560 static int obj_array_set_attr(raid_obj_tab_t *, raid_obj_id_t,
214711890bcSjc156560 	uint32_t, uint32_t *, char **);
215711890bcSjc156560 static int obj_disk_compnum(raid_obj_tab_t *, raid_obj_id_t,
216711890bcSjc156560 	raid_obj_type_id_t);
217711890bcSjc156560 static int obj_disk_complist(raid_obj_tab_t *, raid_obj_id_t, int,
218711890bcSjc156560 	raid_obj_id_t *, raid_obj_type_id_t);
219711890bcSjc156560 static int obj_disk_get_attr(raid_obj_tab_t *, raid_obj_id_t);
220711890bcSjc156560 static int obj_hsp_get_attr(raid_obj_tab_t *, raid_obj_id_t);
221711890bcSjc156560 static int obj_arraypart_get_attr(raid_obj_tab_t *, raid_obj_id_t);
222711890bcSjc156560 static int obj_diskseg_get_attr(raid_obj_tab_t *, raid_obj_id_t);
223711890bcSjc156560 static int obj_task_get_attr(raid_obj_tab_t *, raid_obj_id_t);
224b449fa8aSyw161884 static int obj_prop_get_attr(raid_obj_tab_t *, raid_obj_id_t);
225711890bcSjc156560 static int obj_array_create(raid_obj_tab_t *, raid_obj_id_t, int,
226711890bcSjc156560 	raid_obj_id_t *, char **);
227711890bcSjc156560 static int obj_array_delete(raid_obj_tab_t *, raid_obj_id_t, char **);
228*5c9d25d2SYu-Bo Ryan Wang static int obj_hsp_bind(raid_obj_tab_t *, raid_obj_id_t *, char **);
229*5c9d25d2SYu-Bo Ryan Wang static int obj_hsp_unbind(raid_obj_tab_t *, raid_obj_id_t *, char **);
230711890bcSjc156560 
231711890bcSjc156560 static int raid_obj_create_system_obj(raid_obj_tab_t *);
232711890bcSjc156560 static raid_obj_id_t raid_obj_id_new(raid_obj_tab_t *);
233711890bcSjc156560 static void *raid_obj_attr_new(raid_obj_type_id_t);
234711890bcSjc156560 static raid_obj_id_t raid_obj_create(raid_obj_tab_t *, raid_obj_type_id_t);
235711890bcSjc156560 static int raid_obj_delete(raid_obj_tab_t *, raid_obj_id_t);
236711890bcSjc156560 static int raid_obj_add_org(raid_obj_tab_t *, raid_obj_id_t, raid_obj_id_t);
237711890bcSjc156560 static raid_obj_type_id_t raid_obj_get_type(raid_obj_tab_t *, raid_obj_id_t);
238711890bcSjc156560 static int raid_obj_set_type(raid_obj_tab_t *, raid_obj_id_t,
239711890bcSjc156560 	raid_obj_type_id_t);
240711890bcSjc156560 static raid_obj_status_t raid_obj_get_status(raid_obj_tab_t *, raid_obj_id_t);
241711890bcSjc156560 static int raid_obj_set_status(raid_obj_tab_t *, raid_obj_id_t,
242711890bcSjc156560 	raid_obj_status_t);
243711890bcSjc156560 static int raid_obj_clear_status(raid_obj_tab_t *, raid_obj_id_t,
244711890bcSjc156560 	raid_obj_status_t);
245711890bcSjc156560 static raid_obj_id_t raid_obj_get_container(raid_obj_tab_t *, raid_obj_id_t);
246711890bcSjc156560 static int raid_obj_set_container(raid_obj_tab_t *, raid_obj_id_t,
247711890bcSjc156560 	raid_obj_id_t);
248711890bcSjc156560 static raid_obj_id_t raid_obj_get_comp(raid_obj_tab_t *, raid_obj_id_t);
249711890bcSjc156560 static int raid_obj_set_comp(raid_obj_tab_t *, raid_obj_id_t, raid_obj_id_t);
250711890bcSjc156560 static raid_obj_id_t raid_obj_get_sibling(raid_obj_tab_t *, raid_obj_id_t);
251711890bcSjc156560 static int raid_obj_set_sibling(raid_obj_tab_t *, raid_obj_id_t,
252711890bcSjc156560 	raid_obj_id_t);
253711890bcSjc156560 static void *raid_obj_get_data_ptr(raid_obj_tab_t *, raid_obj_id_t);
254711890bcSjc156560 static int raid_obj_set_data_ptr(raid_obj_tab_t *, raid_obj_id_t, void *);
255711890bcSjc156560 static raid_obj_handle_t raid_obj_get_handle(raid_obj_tab_t *,
256711890bcSjc156560 	raid_obj_id_t);
257711890bcSjc156560 static int raid_obj_set_handle(raid_obj_tab_t *, raid_obj_id_t,
258711890bcSjc156560 	raid_obj_handle_t);
259711890bcSjc156560 
260711890bcSjc156560 static void raid_list_create(raid_list_t *, size_t);
261711890bcSjc156560 static void *raid_list_head(raid_list_t *);
262711890bcSjc156560 static void *raid_list_next(raid_list_t *, void *);
263711890bcSjc156560 static void raid_list_insert_tail(raid_list_t *, void *);
264711890bcSjc156560 static void raid_list_remove(raid_list_t *, void *);
265711890bcSjc156560 static void *raid_list_remove_head(raid_list_t *);
266711890bcSjc156560 static void *raid_list_find(raid_list_t *, raid_obj_id_t);
267711890bcSjc156560 static int raid_obj_tab_create(raid_obj_tab_t *, size_t);
268711890bcSjc156560 static void raid_obj_tab_destroy(raid_obj_tab_t *);
269711890bcSjc156560 static int raid_obj_tab_insert(raid_obj_tab_t *, raid_obj_id_t, void *);
270711890bcSjc156560 static void *raid_obj_tab_remove(raid_obj_tab_t *, raid_obj_id_t);
271711890bcSjc156560 static void *raid_obj_tab_find(raid_obj_tab_t *, raid_obj_id_t);
272711890bcSjc156560 static void raid_list_destroy(raid_list_t *);
273711890bcSjc156560 
274711890bcSjc156560 static int controller_id_to_path(uint32_t, char *);
275711890bcSjc156560 static char *controller_id_to_driver_name(uint32_t);
276711890bcSjc156560 static void raid_plugin_init();
277711890bcSjc156560 static raid_lib_t *raid_plugin_load(char *);
278711890bcSjc156560 static raid_lib_t *raid_find_lib(raid_obj_tab_t *, raid_obj_id_t);
279711890bcSjc156560 
280711890bcSjc156560 /* Global object table */
281711890bcSjc156560 static raid_obj_tab_t raid_tab_sys = {0, 0, NULL};
282711890bcSjc156560 
283711890bcSjc156560 /* Plug-in modules maintenance data structures */
284711890bcSjc156560 static raid_lib_t *raid_lib_sys = NULL;
285711890bcSjc156560 
286711890bcSjc156560 /* Handle table definition */
287711890bcSjc156560 static struct {
288711890bcSjc156560 	int		handle_num;
289711890bcSjc156560 	int		used;
290711890bcSjc156560 	int		unused;
291711890bcSjc156560 	handle_attr_t	*handles;
292711890bcSjc156560 } raid_handle_sys = {0, 0, 0, NULL};
293711890bcSjc156560 
294711890bcSjc156560 /*
295711890bcSjc156560  * RAID object method table definition
296711890bcSjc156560  */
297711890bcSjc156560 static raid_obj_op_t raid_obj_op_sys[OBJ_TYPE_ALL] = {
298711890bcSjc156560 	{obj_sys_compnum, obj_sys_complist, NULL, NULL, NULL,
299711890bcSjc156560 		NULL, NULL, NULL, NULL},	/* system object methods */
300711890bcSjc156560 	{obj_controller_compnum, obj_controller_complist,
301711890bcSjc156560 		obj_controller_get_attr, NULL, obj_controller_act,
302711890bcSjc156560 		NULL, NULL, NULL, NULL},	/* controller object methods */
303711890bcSjc156560 	{obj_array_compnum, obj_array_complist, obj_array_get_attr,
304711890bcSjc156560 		obj_array_set_attr, NULL, obj_array_create,
305711890bcSjc156560 		obj_array_delete, NULL, NULL},	/* array object methods */
306711890bcSjc156560 	{obj_disk_compnum, obj_disk_complist, obj_disk_get_attr, NULL,
307711890bcSjc156560 		NULL, NULL, NULL, NULL, NULL},	/* disk object methods */
308711890bcSjc156560 	{NULL, NULL, obj_hsp_get_attr, NULL, NULL, NULL, NULL, obj_hsp_bind,
309711890bcSjc156560 		obj_hsp_unbind},		/* hsp object methods */
310711890bcSjc156560 	{NULL, NULL, obj_arraypart_get_attr, NULL, NULL, NULL, NULL,
311711890bcSjc156560 		NULL, NULL},			/* array part object methods */
312711890bcSjc156560 	{NULL, NULL, obj_diskseg_get_attr, NULL, NULL, NULL, NULL, NULL, NULL},
313711890bcSjc156560 	{NULL, NULL, obj_task_get_attr, NULL, NULL, NULL, NULL,
314b449fa8aSyw161884 		NULL, NULL},			/* disk seg object methods */
315b449fa8aSyw161884 	{NULL, NULL, obj_prop_get_attr, NULL, NULL, NULL, NULL,
316b449fa8aSyw161884 		NULL, NULL}			/* property object methods */
317711890bcSjc156560 };
318711890bcSjc156560 
319711890bcSjc156560 /*
320711890bcSjc156560  * Mutex for multithread safe
321711890bcSjc156560  */
322711890bcSjc156560 static mutex_t raidcfg_mp;
323711890bcSjc156560 
324711890bcSjc156560 /*
325711890bcSjc156560  * RaidCfg library APIs
326711890bcSjc156560  */
327711890bcSjc156560 const char *
328711890bcSjc156560 raidcfg_errstr(int err_code)
329711890bcSjc156560 {
330711890bcSjc156560 	char *ret_val;
331711890bcSjc156560 
332711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
333711890bcSjc156560 	switch (err_code) {
334711890bcSjc156560 	case	SUCCESS:
335711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Operation succeeded.\n");
336711890bcSjc156560 		break;
337711890bcSjc156560 	case	STD_IOCTL:
338711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
339711890bcSjc156560 		    "Request standard IOCTL service.\n");
340711890bcSjc156560 		break;
341711890bcSjc156560 	case	ERR_DRIVER_NOT_FOUND:
342711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
343711890bcSjc156560 		    "Controller device can not be found.\n");
344711890bcSjc156560 		break;
345711890bcSjc156560 	case	ERR_DRIVER_OPEN:
346711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Can not open controller.\n");
347711890bcSjc156560 		break;
348711890bcSjc156560 	case	ERR_DRIVER_LOCK:
349711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Controller is locked.\n");
350711890bcSjc156560 		break;
351711890bcSjc156560 	case	ERR_DRIVER_CLOSED:
352711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Controller is not opened.\n");
353711890bcSjc156560 		break;
354711890bcSjc156560 	case	ERR_DRIVER_ACROSS:
355711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
356711890bcSjc156560 		    "Operation across multiple controllers.\n");
357711890bcSjc156560 		break;
358711890bcSjc156560 	case	ERR_ARRAY_LEVEL:
359711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
360711890bcSjc156560 		    "Operation not support with volume of this level.\n");
361711890bcSjc156560 		break;
362711890bcSjc156560 	case	ERR_ARRAY_SIZE:
363711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
364711890bcSjc156560 		    "Capacity of array out of range.\n");
365711890bcSjc156560 		break;
366711890bcSjc156560 	case	ERR_ARRAY_STRIPE_SIZE:
367711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Illegal stripe size.\n");
368711890bcSjc156560 		break;
369711890bcSjc156560 	case	ERR_ARRAY_CACHE_POLICY:
370711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
371711890bcSjc156560 		    "Illegal cache-write policy.\n");
372711890bcSjc156560 		break;
373711890bcSjc156560 	case	ERR_ARRAY_IN_USE:
374474adcbbSyw161884 		ret_val = dgettext(TEXT_DOMAIN, "Array or disk in use.\n");
375711890bcSjc156560 		break;
376711890bcSjc156560 	case	ERR_ARRAY_TASK:
377711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Array has background task.\n");
378711890bcSjc156560 		break;
379711890bcSjc156560 	case	ERR_ARRAY_CONFIG:
380711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
381711890bcSjc156560 		    "Configuration over device node failed.\n");
382711890bcSjc156560 		break;
383711890bcSjc156560 	case	ERR_ARRAY_DISKNUM:
384711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Incorrect number of disks.\n");
385711890bcSjc156560 		break;
386711890bcSjc156560 	case	ERR_ARRAY_LAYOUT:
387711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Illegal array layout.\n");
388711890bcSjc156560 		break;
389711890bcSjc156560 	case	ERR_ARRAY_AMOUNT:
390711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Too many arrays.\n");
391711890bcSjc156560 		break;
392711890bcSjc156560 	case	ERR_DISK_STATE:
393711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
394711890bcSjc156560 		    "Incorrect disk status for current operation.\n");
395711890bcSjc156560 		break;
396711890bcSjc156560 	case	ERR_DISK_SPACE:
397711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "No enough disk space.\n");
398711890bcSjc156560 		break;
399711890bcSjc156560 	case	ERR_DISK_SEG_AMOUNT:
400711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Too many disk segments.\n");
401711890bcSjc156560 		break;
402711890bcSjc156560 	case	ERR_DISK_NOT_EMPTY:
403711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Disk has occupied space.\n");
404711890bcSjc156560 		break;
405711890bcSjc156560 	case	ERR_DISK_TASK:
406711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Disk has background task.\n");
407711890bcSjc156560 		break;
408711890bcSjc156560 	case	ERR_TASK_STATE:
409711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
410711890bcSjc156560 		    "Incorrect task state for current operation.\n");
411711890bcSjc156560 		break;
412711890bcSjc156560 	case	ERR_OP_ILLEGAL:
413711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Illegal operation.\n");
414711890bcSjc156560 		break;
415711890bcSjc156560 	case	ERR_OP_NO_IMPL:
416711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
417711890bcSjc156560 		    "Operation is not implemented.\n");
418711890bcSjc156560 		break;
419711890bcSjc156560 	case	ERR_OP_FAILED:
420474adcbbSyw161884 		ret_val = dgettext(TEXT_DOMAIN, "Operation failed.\n");
421711890bcSjc156560 		break;
422711890bcSjc156560 	case	ERR_DEVICE_NOENT:
423711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Device not found.\n");
424711890bcSjc156560 		break;
425711890bcSjc156560 	case	ERR_DEVICE_TYPE:
426711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Illegal type of device.\n");
427711890bcSjc156560 		break;
428711890bcSjc156560 	case	ERR_DEVICE_DUP:
429711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Device record duplicated.\n");
430711890bcSjc156560 		break;
431711890bcSjc156560 	case	ERR_DEVICE_OVERFLOW:
432711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Too many devices.\n");
433711890bcSjc156560 		break;
434711890bcSjc156560 	case	ERR_DEVICE_UNCLEAN:
435711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Device pool is not clean.\n");
436711890bcSjc156560 		break;
437711890bcSjc156560 	case	ERR_DEVICE_INVALID:
438711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Device record is invalid.\n");
439711890bcSjc156560 		break;
440711890bcSjc156560 	case	ERR_NOMEM:
441711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN,
442711890bcSjc156560 		    "Can not allocate more memory space.\n");
443711890bcSjc156560 		break;
444711890bcSjc156560 	case	ERR_PRIV:
445711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "No privilege.\n");
446711890bcSjc156560 		break;
447711890bcSjc156560 	default:
448711890bcSjc156560 		ret_val = dgettext(TEXT_DOMAIN, "Undefined error.\n");
449711890bcSjc156560 	}
450711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
451711890bcSjc156560 
452711890bcSjc156560 	return (ret_val);
453711890bcSjc156560 }
454711890bcSjc156560 
455711890bcSjc156560 int
456711890bcSjc156560 raidcfg_get_controller(uint32_t controller_id)
457711890bcSjc156560 {
458711890bcSjc156560 	raid_obj_id_t obj_id;
459711890bcSjc156560 	int ret_val;
460711890bcSjc156560 
461711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
462711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
463711890bcSjc156560 	obj_id = obj_locate_controller(&raid_tab_sys, controller_id);
464711890bcSjc156560 	if (obj_id < OBJ_NONE) {
465711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
466711890bcSjc156560 		return (obj_id);
467711890bcSjc156560 	}
468711890bcSjc156560 
469711890bcSjc156560 	if (obj_id == OBJ_NONE) {
470711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
471711890bcSjc156560 		return (ERR_DEVICE_NOENT);
472711890bcSjc156560 	}
473711890bcSjc156560 	ret_val = raid_obj_to_handle(&raid_tab_sys, obj_id);
474711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
475711890bcSjc156560 
476711890bcSjc156560 	return (ret_val);
477711890bcSjc156560 }
478711890bcSjc156560 
479711890bcSjc156560 int
480711890bcSjc156560 raidcfg_get_array(int controller_handle, uint64_t target_id, uint64_t lun)
481711890bcSjc156560 {
482711890bcSjc156560 	raid_obj_id_t obj_id;
483711890bcSjc156560 	raidcfg_array_t *attr;
484711890bcSjc156560 	int ret_val;
485711890bcSjc156560 
486711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
487711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
488711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, controller_handle);
489711890bcSjc156560 	if (obj_id < OBJ_NONE) {
490711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
491711890bcSjc156560 		return (obj_id);
492711890bcSjc156560 	}
493711890bcSjc156560 
494711890bcSjc156560 	obj_id = obj_get_comp(&raid_tab_sys, obj_id, OBJ_TYPE_ARRAY);
495711890bcSjc156560 
496711890bcSjc156560 	while (obj_id > OBJ_NONE) {
497711890bcSjc156560 		(void) obj_get_attr(&raid_tab_sys, obj_id, (void **)(&attr));
498711890bcSjc156560 		if (attr->tag.idl.target_id == target_id &&
499711890bcSjc156560 		    attr->tag.idl.lun == lun)
500711890bcSjc156560 			break;
501711890bcSjc156560 
502711890bcSjc156560 		obj_id = obj_get_sibling(&raid_tab_sys, obj_id);
503711890bcSjc156560 	}
504711890bcSjc156560 
505711890bcSjc156560 	if (obj_id < OBJ_NONE) {
506711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
507711890bcSjc156560 		return (obj_id);
508711890bcSjc156560 	}
509711890bcSjc156560 	if (obj_id == OBJ_NONE) {
510711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
511711890bcSjc156560 		return (ERR_DEVICE_NOENT);
512711890bcSjc156560 	}
513711890bcSjc156560 	ret_val = raid_obj_to_handle(&raid_tab_sys, obj_id);
514711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
515711890bcSjc156560 
516711890bcSjc156560 	return (ret_val);
517711890bcSjc156560 }
518711890bcSjc156560 
519711890bcSjc156560 int
520711890bcSjc156560 raidcfg_get_disk(int controller_handle, disk_tag_t tag)
521711890bcSjc156560 {
522711890bcSjc156560 	raid_obj_id_t obj_id;
523711890bcSjc156560 	raidcfg_disk_t *attr;
524711890bcSjc156560 	int ret_val;
525711890bcSjc156560 
526711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
527711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
528711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, controller_handle);
529711890bcSjc156560 	if (obj_id < OBJ_NONE) {
530711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
531711890bcSjc156560 		return (obj_id);
532711890bcSjc156560 	}
533711890bcSjc156560 
534711890bcSjc156560 	obj_id = obj_get_comp(&raid_tab_sys, obj_id, OBJ_TYPE_DISK);
535711890bcSjc156560 
536711890bcSjc156560 	while (obj_id > OBJ_NONE) {
537711890bcSjc156560 		(void) obj_get_attr(&raid_tab_sys, obj_id, (void **)(&attr));
538711890bcSjc156560 		if (attr->tag.cidl.bus == tag.cidl.bus &&
539711890bcSjc156560 		    attr->tag.cidl.target_id == tag.cidl.target_id &&
540711890bcSjc156560 		    attr->tag.cidl.lun == tag.cidl.lun)
541711890bcSjc156560 			break;
542711890bcSjc156560 
543711890bcSjc156560 		obj_id = obj_get_sibling(&raid_tab_sys, obj_id);
544711890bcSjc156560 	}
545711890bcSjc156560 
546711890bcSjc156560 	if (obj_id < OBJ_NONE) {
547711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
548711890bcSjc156560 		return (obj_id);
549711890bcSjc156560 	}
550711890bcSjc156560 	if (obj_id == OBJ_NONE) {
551711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
552711890bcSjc156560 		return (ERR_DEVICE_NOENT);
553711890bcSjc156560 	}
554711890bcSjc156560 	ret_val = raid_obj_to_handle(&raid_tab_sys, obj_id);
555711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
556711890bcSjc156560 
557711890bcSjc156560 	return (ret_val);
558711890bcSjc156560 }
559711890bcSjc156560 
560711890bcSjc156560 int
561711890bcSjc156560 raidcfg_open_controller(int handle, char **plugin_err_str)
562711890bcSjc156560 {
563711890bcSjc156560 	raid_obj_id_t obj_id;
564711890bcSjc156560 	int ret;
565711890bcSjc156560 
566711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
567711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
568711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
569711890bcSjc156560 	if (obj_id < OBJ_NONE) {
570711890bcSjc156560 		raid_handle_delete(handle);
571711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
572711890bcSjc156560 		return (ERR_DEVICE_NOENT);
573711890bcSjc156560 	}
574711890bcSjc156560 
575711890bcSjc156560 	ret = obj_controller_act(&raid_tab_sys, obj_id,
576711890bcSjc156560 	    ACT_CONTROLLER_OPEN, NULL, plugin_err_str);
577711890bcSjc156560 	if (ret < SUCCESS) {
578711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
579711890bcSjc156560 		return (ret);
580711890bcSjc156560 	}
581711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
582711890bcSjc156560 
583711890bcSjc156560 	return (SUCCESS);
584711890bcSjc156560 }
585711890bcSjc156560 
586711890bcSjc156560 int
587711890bcSjc156560 raidcfg_close_controller(int handle, char **plugin_err_str)
588711890bcSjc156560 {
589711890bcSjc156560 	raid_obj_id_t obj_id;
590711890bcSjc156560 	int ret;
591711890bcSjc156560 
592711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
593711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
594711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
595711890bcSjc156560 	if (obj_id < OBJ_NONE) {
596711890bcSjc156560 		raid_handle_delete(handle);
597711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
598711890bcSjc156560 		return (ERR_DEVICE_NOENT);
599711890bcSjc156560 	}
600711890bcSjc156560 
601711890bcSjc156560 	ret = obj_controller_act(&raid_tab_sys, obj_id,
602711890bcSjc156560 	    ACT_CONTROLLER_CLOSE, NULL, plugin_err_str);
603711890bcSjc156560 	if (ret < SUCCESS) {
604711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
605711890bcSjc156560 		return (ret);
606711890bcSjc156560 	}
607711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
608711890bcSjc156560 
609711890bcSjc156560 	return (SUCCESS);
610711890bcSjc156560 }
611711890bcSjc156560 
612711890bcSjc156560 int
613711890bcSjc156560 raidcfg_get_type(int handle)
614711890bcSjc156560 {
615711890bcSjc156560 	raid_obj_id_t obj_id;
616711890bcSjc156560 	int ret_val;
617711890bcSjc156560 
618711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
619711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
620711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
621711890bcSjc156560 	if (obj_id < OBJ_NONE) {
622711890bcSjc156560 		raid_handle_delete(handle);
623711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
624711890bcSjc156560 		return (ERR_DEVICE_NOENT);
625711890bcSjc156560 	}
626711890bcSjc156560 	ret_val = raid_obj_get_type(&raid_tab_sys, obj_id);
627711890bcSjc156560 	(void) mutex_unlock(&raidcfg_mp);
628711890bcSjc156560 
629711890bcSjc156560 	return (ret_val);
630711890bcSjc156560 }
631711890bcSjc156560 
632711890bcSjc156560 int
633711890bcSjc156560 raidcfg_get_attr(int handle, void *attr)
634711890bcSjc156560 {
635711890bcSjc156560 	raid_obj_id_t obj_id;
636711890bcSjc156560 	raid_obj_type_id_t type;
637711890bcSjc156560 	void *data;
638711890bcSjc156560 	int ret, size;
639711890bcSjc156560 
640711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
641711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
642711890bcSjc156560 	if (attr == NULL) {
643711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
644711890bcSjc156560 		return (ERR_DEVICE_INVALID);
645711890bcSjc156560 	}
646711890bcSjc156560 
647711890bcSjc156560 	obj_id = raid_handle_to_obj(&raid_tab_sys, handle);
648711890bcSjc156560 	if (obj_id < OBJ_NONE) {
649711890bcSjc156560 		raid_handle_delete(handle);
650711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
651711890bcSjc156560 		return (ERR_DEVICE_NOENT);
652711890bcSjc156560 	}
653711890bcSjc156560 
654711890bcSjc156560 	type = raid_obj_get_type(&raid_tab_sys, obj_id);
655711890bcSjc156560 	ret = obj_get_attr(&raid_tab_sys, obj_id, &data);
656711890bcSjc156560 	if (ret < SUCCESS) {
657711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
658711890bcSjc156560 		return (ret);
659711890bcSjc156560 	}
660711890bcSjc156560 
661711890bcSjc156560 	switch (type) {
662711890bcSjc156560 	case	OBJ_TYPE_CONTROLLER:
663711890bcSjc156560 		size = sizeof (controller_attr_t);
664711890bcSjc156560 		break;
665711890bcSjc156560 	case	OBJ_TYPE_ARRAY:
666711890bcSjc156560 		size = sizeof (array_attr_t);
667711890bcSjc156560 		break;
668711890bcSjc156560 	case	OBJ_TYPE_HSP:
669711890bcSjc156560 		{
670711890bcSjc156560 			raidcfg_hsp_t *dst = attr;
671711890bcSjc156560 			hsp_attr_t *src = data;
672711890bcSjc156560 			controller_attr_t *ctlr_attr;
673711890bcSjc156560 			array_attr_t *array_attr;
674711890bcSjc156560 
675711890bcSjc156560 			dst->associated_id = src->associated_id;
676711890bcSjc156560 			dst->type = src->type;
677711890bcSjc156560 
678711890bcSjc156560 			obj_id = obj_get_controller(&raid_tab_sys, obj_id);
679711890bcSjc156560 			ret = obj_get_attr(&raid_tab_sys, obj_id,
680711890bcSjc156560 			    (void **)(&ctlr_attr));
681711890bcSjc156560 			if (ret < SUCCESS) {
682711890bcSjc156560 				(void) mutex_unlock(&raidcfg_mp);
683711890bcSjc156560 				return (ret);
684711890bcSjc156560 			}
685711890bcSjc156560 
686711890bcSjc156560 			if (src->type == HSP_TYPE_LOCAL) {
687711890bcSjc156560 				obj_id = obj_locate_array(&raid_tab_sys,
688711890bcSjc156560 				    ctlr_attr->controller_id,
689711890bcSjc156560 				    src->associated_id);
690711890bcSjc156560 				ret = obj_get_attr(&raid_tab_sys, obj_id,
691711890bcSjc156560 				    (void **)(&array_attr));
692711890bcSjc156560 				if (ret < SUCCESS) {
693711890bcSjc156560 					(void) mutex_unlock(&raidcfg_mp);
694711890bcSjc156560 					return (ret);
695711890bcSjc156560 				}
696711890bcSjc156560 
697711890bcSjc156560 				dst->tag.idl.target_id =
698711890bcSjc156560 				    array_attr->tag.idl.target_id;
699711890bcSjc156560 				dst->tag.idl.lun = array_attr->tag.idl.lun;
700711890bcSjc156560 			}
701711890bcSjc156560 		}
702711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
703711890bcSjc156560 		return (SUCCESS);
704711890bcSjc156560 	case	OBJ_TYPE_DISK:
705711890bcSjc156560 		size = sizeof (disk_attr_t);
706711890bcSjc156560 		break;
707711890bcSjc156560 	case	OBJ_TYPE_ARRAY_PART:
708711890bcSjc156560 		{
709711890bcSjc156560 			raidcfg_arraypart_t *dst = attr;
710711890bcSjc156560 			arraypart_attr_t *src = data;
711711890bcSjc156560 			controller_attr_t *ctlr_attr;
712711890bcSjc156560 			disk_attr_t *disk_attr;
713711890bcSjc156560 
714711890bcSjc156560 			dst->disk_id = src->disk_id;
715711890bcSjc156560 			dst->offset = src->offset;
716711890bcSjc156560 			dst->size = src->size;
717711890bcSjc156560 			dst->state = src->state;
718711890bcSjc156560 
719711890bcSjc156560 			obj_id = obj_get_controller(&raid_tab_sys, obj_id);
720711890bcSjc156560 			ret = obj_get_attr(&raid_tab_sys, obj_id,
721711890bcSjc156560 			    (void **)(&ctlr_attr));
722711890bcSjc156560 			if (ret < SUCCESS) {
723711890bcSjc156560 				(void) mutex_unlock(&raidcfg_mp);
724711890bcSjc156560 				return (ret);
725711890bcSjc156560 			}
726711890bcSjc156560 
727711890bcSjc156560 			obj_id = obj_locate_disk(&raid_tab_sys,
728711890bcSjc156560 			    ctlr_attr->controller_id, src->disk_id);
729711890bcSjc156560 			if (obj_id <= OBJ_NONE) {
730711890bcSjc156560 				dst->tag.cidl.bus = (uint64_t)OBJ_ATTR_NONE;
731711890bcSjc156560 				dst->tag.cidl.target_id =
732711890bcSjc156560 				    (uint64_t)OBJ_ATTR_NONE;
733711890bcSjc156560 				dst->tag.cidl.lun = (uint64_t)OBJ_ATTR_NONE;
734711890bcSjc156560 				(void) mutex_unlock(&raidcfg_mp);
735711890bcSjc156560 				return (SUCCESS);
736711890bcSjc156560 			}
737711890bcSjc156560 
738711890bcSjc156560 			ret = obj_get_attr(&raid_tab_sys, obj_id,
739711890bcSjc156560 			    (void **)(&disk_attr));
740711890bcSjc156560 			if (ret < SUCCESS) {
741711890bcSjc156560 				(void) mutex_unlock(&raidcfg_mp);
742711890bcSjc156560 				return (ret);
743711890bcSjc156560 			}
744711890bcSjc156560 
745711890bcSjc156560 			dst->tag.cidl.bus = disk_attr->tag.cidl.bus;
746711890bcSjc156560 			dst->tag.cidl.target_id = disk_attr->tag.cidl.target_id;
747711890bcSjc156560 			dst->tag.cidl.lun = disk_attr->tag.cidl.lun;
748711890bcSjc156560 		}
749711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
750711890bcSjc156560 		return (SUCCESS);
751711890bcSjc156560 	case	OBJ_TYPE_DISK_SEG:
752711890bcSjc156560 		size = sizeof (diskseg_attr_t);
753711890bcSjc156560 		break;
754711890bcSjc156560 	case	OBJ_TYPE_TASK:
755711890bcSjc156560 		size = sizeof (task_attr_t);
756711890bcSjc156560 		break;
757b449fa8aSyw161884 	case	OBJ_TYPE_PROP:
758b449fa8aSyw161884 		{
759b449fa8aSyw161884 			property_attr_t *src = data, *dst = attr;
760b449fa8aSyw161884 
761b449fa8aSyw161884 			dst->prop_id = src->prop_id;
762b449fa8aSyw161884 			dst->prop_type = src->prop_type;
763b449fa8aSyw161884 			if (dst->prop_size == 0) {
764b449fa8aSyw161884 				dst->prop_size = src->prop_size;
765b449fa8aSyw161884 				(void) mutex_unlock(&raidcfg_mp);
766b449fa8aSyw161884 				return (SUCCESS);
767b449fa8aSyw161884 			}
768b449fa8aSyw161884 
769b449fa8aSyw161884 			if (dst->prop_size < src->prop_size)
770b449fa8aSyw161884 				size = dst->prop_size;
771b449fa8aSyw161884 			else
772b449fa8aSyw161884 				size = src->prop_size;
773b449fa8aSyw161884 
774b449fa8aSyw161884 			(void) memcpy(dst->prop, src->prop, size);
775b449fa8aSyw161884 			(void) mutex_unlock(&raidcfg_mp);
776b449fa8aSyw161884 			return (SUCCESS);
777b449fa8aSyw161884 		}
778b449fa8aSyw161884 		break;
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
1033*5c9d25d2SYu-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;
1038*5c9d25d2SYu-Bo Ryan Wang 	int ret;
1039711890bcSjc156560 
1040711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
1041711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1042*5c9d25d2SYu-Bo Ryan Wang 	if (hsp_relations == NULL) {
1043711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1044711890bcSjc156560 		return (ERR_OP_ILLEGAL);
1045711890bcSjc156560 	}
1046711890bcSjc156560 
1047*5c9d25d2SYu-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,
1057*5c9d25d2SYu-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 		}
1068*5c9d25d2SYu-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,
1078*5c9d25d2SYu-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 	}
1089*5c9d25d2SYu-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 
1096*5c9d25d2SYu-Bo Ryan Wang 	hsp_relation_objs[0] = array_obj_id;
1097*5c9d25d2SYu-Bo Ryan Wang 	hsp_relation_objs[1] = disk_obj_id;
1098711890bcSjc156560 
1099*5c9d25d2SYu-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
1110*5c9d25d2SYu-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;
1115*5c9d25d2SYu-Bo Ryan Wang 	int ret;
1116711890bcSjc156560 
1117711890bcSjc156560 	(void) mutex_lock(&raidcfg_mp);
1118711890bcSjc156560 	(void) obj_rescan(&raid_tab_sys);
1119*5c9d25d2SYu-Bo Ryan Wang 	if (hsp_relations == NULL) {
1120711890bcSjc156560 		(void) mutex_unlock(&raidcfg_mp);
1121711890bcSjc156560 		return (ERR_OP_ILLEGAL);
1122711890bcSjc156560 	}
1123711890bcSjc156560 
1124*5c9d25d2SYu-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,
1134*5c9d25d2SYu-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 		}
1145*5c9d25d2SYu-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,
1155*5c9d25d2SYu-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 	}
1166*5c9d25d2SYu-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 
1173*5c9d25d2SYu-Bo Ryan Wang 	hsp_relation_objs[0] = array_obj_id;
1174*5c9d25d2SYu-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,
1177*5c9d25d2SYu-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 		break;
1319711890bcSjc156560 	}
1320711890bcSjc156560 
1321711890bcSjc156560 	return (capacity);
1322711890bcSjc156560 }
1323711890bcSjc156560 
1324711890bcSjc156560 /*
1325711890bcSjc156560  * Raid handle maintenance routines
1326711890bcSjc156560  */
1327711890bcSjc156560 static int
1328711890bcSjc156560 raid_handle_init()
1329711890bcSjc156560 {
1330711890bcSjc156560 	int i;
1331711890bcSjc156560 	void *ptr;
1332711890bcSjc156560 
1333711890bcSjc156560 	raid_handle_sys.handle_num += HANDLER_SLOTS;
1334711890bcSjc156560 	ptr = realloc(raid_handle_sys.handles,
1335711890bcSjc156560 	    raid_handle_sys.handle_num * sizeof (handle_attr_t));
1336711890bcSjc156560 	if (ptr == NULL)
1337711890bcSjc156560 		return (ERR_NOMEM);
1338711890bcSjc156560 	raid_handle_sys.handles = ptr;
1339711890bcSjc156560 
1340711890bcSjc156560 	/* Clean up the new allocated handles */
1341711890bcSjc156560 	for (i = raid_handle_sys.handle_num - HANDLER_SLOTS;
1342711890bcSjc156560 	    i < raid_handle_sys.handle_num; ++i) {
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);
1747711890bcSjc156560 	if (type < OBJ_TYPE_SYSTEM || type > OBJ_TYPE_ALL)
1748711890bcSjc156560 		return (ERR_DEVICE_INVALID);
1749711890bcSjc156560 
1750711890bcSjc156560 	for (obj_type_cnt = OBJ_SYSTEM; obj_type_cnt < OBJ_TYPE_ALL;
1751711890bcSjc156560 	    ++obj_type_cnt) {
1752711890bcSjc156560 		if (raid_obj_op_sys[type].compnum != NULL)
1753711890bcSjc156560 			comp_num = raid_obj_op_sys[type].compnum(
1754711890bcSjc156560 			    raid_tab, obj_id, obj_type_cnt);
1755711890bcSjc156560 		else
1756711890bcSjc156560 			comp_num = 0;
1757711890bcSjc156560 
1758711890bcSjc156560 		if (comp_num < SUCCESS)
1759711890bcSjc156560 			return (comp_num);
1760711890bcSjc156560 		if (comp_num == 0)
1761711890bcSjc156560 			continue;
1762711890bcSjc156560 
1763711890bcSjc156560 		comp_list = calloc(comp_num, sizeof (raid_obj_id_t));
1764711890bcSjc156560 		if (comp_list == NULL)
1765711890bcSjc156560 			return (ERR_NOMEM);
1766711890bcSjc156560 
1767711890bcSjc156560 		for (i = 0; i < comp_num; ++i) {
1768711890bcSjc156560 			*(comp_list + i) = raid_obj_create(raid_tab,
1769711890bcSjc156560 			    obj_type_cnt);
1770711890bcSjc156560 			if (*(comp_list + i) < SUCCESS) {
1771711890bcSjc156560 				ret = *(comp_list + i);
1772711890bcSjc156560 				free(comp_list);
1773711890bcSjc156560 				return (ret);
1774711890bcSjc156560 			}
1775711890bcSjc156560 
1776711890bcSjc156560 			(void) raid_obj_clear_status(raid_tab,
1777711890bcSjc156560 			    *(comp_list + i), OBJ_STATUS_CMD_CLEAN);
1778711890bcSjc156560 			(void) raid_obj_add_org(raid_tab, *(comp_list + i),
1779711890bcSjc156560 			    obj_id);
1780711890bcSjc156560 		}
1781711890bcSjc156560 
1782711890bcSjc156560 		if (raid_obj_op_sys[type].complist != NULL)
1783711890bcSjc156560 			raid_obj_op_sys[type].complist(raid_tab,
1784711890bcSjc156560 			    obj_id, comp_num, comp_list, obj_type_cnt);
1785711890bcSjc156560 		free(comp_list);
1786711890bcSjc156560 	}
1787711890bcSjc156560 
1788711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_SCANCOMP);
1789711890bcSjc156560 	return (SUCCESS);
1790711890bcSjc156560 }
1791711890bcSjc156560 
1792711890bcSjc156560 static int
1793711890bcSjc156560 obj_rescan(raid_obj_tab_t *raid_tab)
1794711890bcSjc156560 {
1795711890bcSjc156560 	int ret;
1796711890bcSjc156560 
1797711890bcSjc156560 	raid_obj_tab_destroy(raid_tab);
1798711890bcSjc156560 
1799711890bcSjc156560 	if (raid_obj_tab_create(raid_tab, HASH_SLOTS) != SUCCESS)
1800711890bcSjc156560 		return (ERR_NOMEM);
1801711890bcSjc156560 
1802711890bcSjc156560 	if ((ret = raid_obj_create_system_obj(raid_tab)) != SUCCESS) {
1803711890bcSjc156560 		raid_obj_tab_destroy(raid_tab);
1804711890bcSjc156560 		return (ret);
1805711890bcSjc156560 	}
1806711890bcSjc156560 
1807711890bcSjc156560 	return (SUCCESS);
1808711890bcSjc156560 }
1809711890bcSjc156560 
1810711890bcSjc156560 static raid_obj_id_t
1811711890bcSjc156560 obj_get_comp(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
1812711890bcSjc156560 	raid_obj_type_id_t obj_type)
1813711890bcSjc156560 {
1814711890bcSjc156560 	raid_obj_id_t id;
1815711890bcSjc156560 	raid_obj_type_id_t type;
1816711890bcSjc156560 	raid_obj_status_t status;
1817711890bcSjc156560 	int ret;
1818711890bcSjc156560 
1819711890bcSjc156560 	if ((obj_type < OBJ_TYPE_SYSTEM) || (obj_type > OBJ_TYPE_ALL))
1820711890bcSjc156560 		return (ERR_DEVICE_TYPE);
1821711890bcSjc156560 
1822711890bcSjc156560 	status = raid_obj_get_status(raid_tab, obj_id);
1823711890bcSjc156560 	if (status < SUCCESS)
1824711890bcSjc156560 		return (status);
1825711890bcSjc156560 
1826711890bcSjc156560 	if (!(status & OBJ_STATUS_SCANCOMP)) {
1827711890bcSjc156560 		ret = obj_scan_comp(raid_tab, obj_id);
1828711890bcSjc156560 		if (ret < SUCCESS)
1829711890bcSjc156560 			return (ret);
1830711890bcSjc156560 	}
1831711890bcSjc156560 
1832711890bcSjc156560 	id = raid_obj_get_comp(raid_tab, obj_id);
1833711890bcSjc156560 	if (id <= OBJ_NONE)
1834711890bcSjc156560 		return (id);
1835711890bcSjc156560 
1836711890bcSjc156560 	type = raid_obj_get_type(raid_tab, id);
1837711890bcSjc156560 	if (type < OBJ_TYPE_SYSTEM)
1838711890bcSjc156560 		return (type);
1839711890bcSjc156560 
1840711890bcSjc156560 	if (type == obj_type)
1841711890bcSjc156560 		return (id);
1842711890bcSjc156560 
1843711890bcSjc156560 	while (id > OBJ_NONE) {
1844711890bcSjc156560 		id = raid_obj_get_sibling(raid_tab, id);
1845711890bcSjc156560 		if (id <= OBJ_NONE)
1846711890bcSjc156560 			return (id);
1847711890bcSjc156560 
1848711890bcSjc156560 		type = raid_obj_get_type(raid_tab, id);
1849711890bcSjc156560 		if (type < OBJ_TYPE_SYSTEM)
1850711890bcSjc156560 			return (type);
1851711890bcSjc156560 
1852711890bcSjc156560 		if (type == obj_type)
1853711890bcSjc156560 			break;
1854711890bcSjc156560 	};
1855711890bcSjc156560 
1856711890bcSjc156560 	return (id);
1857711890bcSjc156560 }
1858711890bcSjc156560 
1859711890bcSjc156560 static raid_obj_id_t
1860711890bcSjc156560 obj_get_sibling(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
1861711890bcSjc156560 {
1862711890bcSjc156560 	raid_obj_id_t id;
1863711890bcSjc156560 	raid_obj_type_id_t type, obj_type;
1864711890bcSjc156560 
1865711890bcSjc156560 	id = obj_id;
1866711890bcSjc156560 	obj_type = raid_obj_get_type(raid_tab, id);
1867711890bcSjc156560 	if (obj_type < OBJ_TYPE_SYSTEM)
1868711890bcSjc156560 		return (obj_type);
1869711890bcSjc156560 
1870711890bcSjc156560 	do {
1871711890bcSjc156560 		id = raid_obj_get_sibling(raid_tab, id);
1872711890bcSjc156560 		if (id < OBJ_NONE)
1873711890bcSjc156560 			return (id);
1874711890bcSjc156560 
1875711890bcSjc156560 		type = raid_obj_get_type(raid_tab, id);
1876711890bcSjc156560 		if (type < OBJ_TYPE_SYSTEM)
1877711890bcSjc156560 			return (type);
1878711890bcSjc156560 	} while ((type != obj_type) && (id != OBJ_NONE));
1879711890bcSjc156560 
1880711890bcSjc156560 	return (id);
1881711890bcSjc156560 }
1882711890bcSjc156560 
1883711890bcSjc156560 static int
1884711890bcSjc156560 obj_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id, void **data)
1885711890bcSjc156560 {
1886711890bcSjc156560 	raid_obj_type_id_t type;
1887711890bcSjc156560 	raid_obj_status_t status;
1888711890bcSjc156560 	void *attr;
1889711890bcSjc156560 	int ret = SUCCESS;
1890711890bcSjc156560 
1891711890bcSjc156560 	status = raid_obj_get_status(raid_tab, obj_id);
1892711890bcSjc156560 	if (status < SUCCESS)
1893711890bcSjc156560 		return (status);
1894711890bcSjc156560 
1895711890bcSjc156560 	type = raid_obj_get_type(raid_tab, obj_id);
1896711890bcSjc156560 	if (type < OBJ_TYPE_SYSTEM)
1897711890bcSjc156560 		return (type);
1898711890bcSjc156560 
1899711890bcSjc156560 	if (!(status & OBJ_STATUS_OPENED)) {
1900711890bcSjc156560 		if (raid_obj_op_sys[type].get_attr == NULL)
1901711890bcSjc156560 			(void) raid_obj_set_status(raid_tab, obj_id,
1902711890bcSjc156560 			    OBJ_STATUS_OPENED);
1903711890bcSjc156560 		else
1904711890bcSjc156560 			ret = raid_obj_op_sys[type].get_attr(raid_tab, obj_id);
1905711890bcSjc156560 	}
1906711890bcSjc156560 	if (ret < SUCCESS)
1907711890bcSjc156560 		return (ret);
1908711890bcSjc156560 
1909711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1910711890bcSjc156560 	if (attr == NULL && type != OBJ_TYPE_SYSTEM)
1911711890bcSjc156560 		return (ERR_DEVICE_INVALID);
1912711890bcSjc156560 
1913711890bcSjc156560 	*data = attr;
1914711890bcSjc156560 	return (SUCCESS);
1915711890bcSjc156560 }
1916711890bcSjc156560 
1917711890bcSjc156560 static raid_obj_id_t
1918711890bcSjc156560 obj_locate_controller(raid_obj_tab_t *raid_tab, uint32_t controller_id)
1919711890bcSjc156560 {
1920711890bcSjc156560 	raid_obj_id_t obj_id;
1921711890bcSjc156560 	controller_attr_t *attr;
1922711890bcSjc156560 
1923711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, OBJ_SYSTEM, OBJ_TYPE_CONTROLLER);
1924711890bcSjc156560 	if (obj_id <= OBJ_NONE)
1925711890bcSjc156560 		return (obj_id);
1926711890bcSjc156560 
1927711890bcSjc156560 	do {
1928711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1929711890bcSjc156560 		if (attr == NULL)
1930711890bcSjc156560 			return (ERR_DEVICE_INVALID);
1931711890bcSjc156560 
1932711890bcSjc156560 		if (attr->controller_id == controller_id)
1933711890bcSjc156560 			break;
1934711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) != OBJ_NONE);
1935711890bcSjc156560 
1936711890bcSjc156560 	return (obj_id);
1937711890bcSjc156560 }
1938711890bcSjc156560 
1939711890bcSjc156560 static raid_obj_id_t
1940711890bcSjc156560 obj_locate_array(raid_obj_tab_t *raid_tab, uint32_t controller_id,
1941711890bcSjc156560 	uint32_t array_id)
1942711890bcSjc156560 {
1943711890bcSjc156560 	raid_obj_id_t obj_id;
1944711890bcSjc156560 
1945711890bcSjc156560 	obj_id = obj_locate_controller(raid_tab, controller_id);
1946711890bcSjc156560 	if (obj_id < OBJ_NONE)
1947711890bcSjc156560 		return (obj_id);
1948711890bcSjc156560 
1949711890bcSjc156560 	obj_id = obj_locate_array_recur(raid_tab, obj_id, array_id);
1950711890bcSjc156560 
1951711890bcSjc156560 	return (obj_id);
1952711890bcSjc156560 }
1953711890bcSjc156560 
1954711890bcSjc156560 static raid_obj_id_t
1955711890bcSjc156560 obj_locate_array_recur(raid_obj_tab_t *raid_tab,
1956711890bcSjc156560 	raid_obj_id_t container_obj_id, uint32_t array_id)
1957711890bcSjc156560 {
1958711890bcSjc156560 	raid_obj_id_t obj_id, ret;
1959711890bcSjc156560 	array_attr_t *attr;
1960711890bcSjc156560 
1961711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, container_obj_id, OBJ_TYPE_ARRAY);
1962711890bcSjc156560 	if (obj_id <= OBJ_NONE)
1963711890bcSjc156560 		return (obj_id);
1964711890bcSjc156560 
1965711890bcSjc156560 	do {
1966711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
1967711890bcSjc156560 		if (attr == NULL)
1968711890bcSjc156560 			return (ERR_DEVICE_INVALID);
1969711890bcSjc156560 
1970711890bcSjc156560 		if (attr->array_id == array_id)
1971711890bcSjc156560 			break;
1972711890bcSjc156560 
1973711890bcSjc156560 		ret = obj_locate_array_recur(raid_tab, obj_id, array_id);
1974711890bcSjc156560 		if (ret != OBJ_NONE)
1975711890bcSjc156560 			return (ret);
1976711890bcSjc156560 
1977711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
1978711890bcSjc156560 
1979711890bcSjc156560 	return (obj_id);
1980711890bcSjc156560 }
1981711890bcSjc156560 
1982711890bcSjc156560 static raid_obj_id_t
1983711890bcSjc156560 obj_locate_hsp(raid_obj_tab_t *raid_tab, uint32_t controller_id,
1984711890bcSjc156560 	uint32_t disk_id, uint32_t array_id)
1985711890bcSjc156560 {
1986711890bcSjc156560 	raid_obj_id_t obj_id;
1987711890bcSjc156560 	hsp_attr_t *hsp_attr;
1988711890bcSjc156560 
1989711890bcSjc156560 	obj_id = obj_locate_disk(raid_tab, controller_id, disk_id);
1990711890bcSjc156560 	if (obj_id <= OBJ_NONE)
1991711890bcSjc156560 		return (obj_id);
1992711890bcSjc156560 
1993711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_HSP);
1994711890bcSjc156560 	if (obj_id <= OBJ_NONE)
1995711890bcSjc156560 		return (obj_id);
1996711890bcSjc156560 
1997711890bcSjc156560 	do {
1998711890bcSjc156560 		(void) obj_get_attr(raid_tab, obj_id, (void **)(&hsp_attr));
1999711890bcSjc156560 		if (hsp_attr->associated_id == array_id)
2000711890bcSjc156560 			break;
2001711890bcSjc156560 
2002711890bcSjc156560 		obj_id = obj_get_sibling(raid_tab, obj_id);
2003711890bcSjc156560 		if (obj_id < OBJ_NONE)
2004711890bcSjc156560 			return (obj_id);
2005711890bcSjc156560 	} while (obj_id > OBJ_NONE);
2006711890bcSjc156560 
2007711890bcSjc156560 	return (obj_id);
2008711890bcSjc156560 }
2009711890bcSjc156560 
2010711890bcSjc156560 static raid_obj_id_t
2011711890bcSjc156560 obj_locate_disk(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2012711890bcSjc156560 	uint32_t disk_id)
2013711890bcSjc156560 {
2014711890bcSjc156560 	raid_obj_id_t obj_id;
2015711890bcSjc156560 	disk_attr_t *attr;
2016711890bcSjc156560 
2017711890bcSjc156560 	obj_id = obj_locate_controller(raid_tab, controller_id);
2018711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2019711890bcSjc156560 		return (obj_id);
2020711890bcSjc156560 
2021711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_DISK);
2022711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2023711890bcSjc156560 		return (obj_id);
2024711890bcSjc156560 
2025711890bcSjc156560 	do {
2026711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2027711890bcSjc156560 		if (attr == NULL)
2028711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2029711890bcSjc156560 
2030711890bcSjc156560 		if (attr->disk_id == disk_id)
2031711890bcSjc156560 			break;
2032711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
2033711890bcSjc156560 
2034711890bcSjc156560 	return (obj_id);
2035711890bcSjc156560 }
2036711890bcSjc156560 
2037711890bcSjc156560 static raid_obj_id_t
2038711890bcSjc156560 obj_locate_arraypart(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2039711890bcSjc156560 	uint32_t array_id, uint32_t disk_id)
2040711890bcSjc156560 {
2041711890bcSjc156560 	raid_obj_id_t obj_id;
2042711890bcSjc156560 
2043711890bcSjc156560 	arraypart_attr_t *attr;
2044711890bcSjc156560 
2045711890bcSjc156560 	obj_id = obj_locate_array(raid_tab, controller_id, array_id);
2046711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2047711890bcSjc156560 		return (obj_id);
2048711890bcSjc156560 
2049711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_ARRAY_PART);
2050711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2051711890bcSjc156560 		return (obj_id);
2052711890bcSjc156560 
2053711890bcSjc156560 	do {
2054711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2055711890bcSjc156560 		if (attr == NULL)
2056711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2057711890bcSjc156560 
2058711890bcSjc156560 		if (attr->disk_id == disk_id)
2059711890bcSjc156560 			break;
2060711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) >
2061711890bcSjc156560 	    OBJ_NONE);
2062711890bcSjc156560 
2063711890bcSjc156560 	return (obj_id);
2064711890bcSjc156560 }
2065711890bcSjc156560 
2066711890bcSjc156560 static raid_obj_id_t
2067711890bcSjc156560 obj_locate_diskseg(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2068711890bcSjc156560 	uint32_t disk_id, uint32_t seq_no)
2069711890bcSjc156560 {
2070711890bcSjc156560 	raid_obj_id_t obj_id;
2071711890bcSjc156560 	diskseg_attr_t *attr;
2072711890bcSjc156560 
2073711890bcSjc156560 	obj_id = obj_locate_disk(raid_tab, controller_id, disk_id);
2074711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2075711890bcSjc156560 		return (obj_id);
2076711890bcSjc156560 
2077711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_DISK_SEG);
2078711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2079711890bcSjc156560 		return (obj_id);
2080711890bcSjc156560 
2081711890bcSjc156560 	do {
2082711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2083711890bcSjc156560 		if (attr == NULL)
2084711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2085711890bcSjc156560 
2086711890bcSjc156560 		if (attr->seq_no == seq_no)
2087711890bcSjc156560 			break;
2088711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
2089711890bcSjc156560 
2090711890bcSjc156560 	return (obj_id);
2091711890bcSjc156560 }
2092711890bcSjc156560 
2093711890bcSjc156560 static raid_obj_id_t
2094711890bcSjc156560 obj_locate_task(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2095711890bcSjc156560 	uint32_t task_id)
2096711890bcSjc156560 {
2097711890bcSjc156560 	raid_obj_id_t obj_id, obj_id2, task_obj_id;
2098711890bcSjc156560 	task_attr_t *attr;
2099711890bcSjc156560 
2100711890bcSjc156560 	obj_id = obj_locate_controller(raid_tab, controller_id);
2101711890bcSjc156560 	if (obj_id <= OBJ_NONE)
2102711890bcSjc156560 		return (obj_id);
2103711890bcSjc156560 
2104711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_ARRAY);
2105711890bcSjc156560 	if (obj_id < OBJ_NONE)
2106711890bcSjc156560 		return (obj_id);
2107711890bcSjc156560 
2108711890bcSjc156560 	do {
2109711890bcSjc156560 		obj_id2 = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_ARRAY);
2110711890bcSjc156560 		while (obj_id2 != OBJ_NONE) {
2111711890bcSjc156560 			task_obj_id = obj_get_comp(raid_tab, obj_id2,
2112711890bcSjc156560 			    OBJ_TYPE_TASK);
2113711890bcSjc156560 
2114711890bcSjc156560 			if (task_obj_id < OBJ_NONE)
2115711890bcSjc156560 				return (task_obj_id);
2116711890bcSjc156560 
2117711890bcSjc156560 			if (task_obj_id == OBJ_NONE) {
2118711890bcSjc156560 				obj_id2 = obj_get_sibling(raid_tab, obj_id2);
2119711890bcSjc156560 				continue;
2120711890bcSjc156560 			}
2121711890bcSjc156560 
2122711890bcSjc156560 			attr = raid_obj_get_data_ptr(raid_tab, task_obj_id);
2123711890bcSjc156560 			if (attr == NULL)
2124711890bcSjc156560 				return (ERR_DEVICE_INVALID);
2125711890bcSjc156560 
2126711890bcSjc156560 			if (attr->task_id == task_id)
2127711890bcSjc156560 				return (task_obj_id);
2128711890bcSjc156560 
2129711890bcSjc156560 			obj_id2 = obj_get_sibling(raid_tab, obj_id2);
2130711890bcSjc156560 		}
2131711890bcSjc156560 
2132711890bcSjc156560 		task_obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_TASK);
2133711890bcSjc156560 		if (task_obj_id < OBJ_NONE)
2134711890bcSjc156560 			return (task_obj_id);
2135711890bcSjc156560 
2136711890bcSjc156560 		if (task_obj_id == OBJ_NONE)
2137711890bcSjc156560 			continue;
2138711890bcSjc156560 
2139711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, task_obj_id);
2140711890bcSjc156560 		if (attr == NULL)
2141711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2142711890bcSjc156560 
2143711890bcSjc156560 		if (attr->task_id == task_id)
2144711890bcSjc156560 			return (task_obj_id);
2145711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
2146711890bcSjc156560 
2147711890bcSjc156560 	if (obj_id < OBJ_NONE)
2148711890bcSjc156560 		return (obj_id);
2149711890bcSjc156560 
2150711890bcSjc156560 	obj_id = obj_locate_controller(raid_tab, controller_id);
2151711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_DISK);
2152711890bcSjc156560 	if (obj_id < OBJ_NONE)
2153711890bcSjc156560 		return (obj_id);
2154711890bcSjc156560 
2155711890bcSjc156560 	do {
2156711890bcSjc156560 		task_obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_TASK);
2157711890bcSjc156560 		if (task_obj_id < OBJ_NONE)
2158711890bcSjc156560 			return (task_obj_id);
2159711890bcSjc156560 
2160711890bcSjc156560 		if (task_obj_id == OBJ_NONE)
2161711890bcSjc156560 			continue;
2162711890bcSjc156560 
2163711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab, task_obj_id);
2164711890bcSjc156560 		if (attr == NULL)
2165711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2166711890bcSjc156560 
2167711890bcSjc156560 		if (attr->task_id == task_id)
2168711890bcSjc156560 			return (task_obj_id);
2169711890bcSjc156560 	} while ((obj_id = obj_get_sibling(raid_tab, obj_id)) > OBJ_NONE);
2170711890bcSjc156560 
2171711890bcSjc156560 	return (obj_id);
2172b449fa8aSyw161884 }
2173711890bcSjc156560 
2174b449fa8aSyw161884 static raid_obj_id_t
2175b449fa8aSyw161884 obj_locate_prop(raid_obj_tab_t *raid_tab, uint32_t controller_id,
2176b449fa8aSyw161884 	uint32_t disk_id, uint32_t prop_id)
2177b449fa8aSyw161884 {
2178b449fa8aSyw161884 	raid_obj_id_t obj_id;
2179b449fa8aSyw161884 	property_attr_t *prop_attr;
2180b449fa8aSyw161884 
2181b449fa8aSyw161884 	obj_id = obj_locate_disk(raid_tab, controller_id, disk_id);
2182b449fa8aSyw161884 	if (obj_id < OBJ_NONE)
2183b449fa8aSyw161884 		return (obj_id);
2184b449fa8aSyw161884 
2185b449fa8aSyw161884 	obj_id = obj_get_comp(raid_tab, obj_id, OBJ_TYPE_PROP);
2186b449fa8aSyw161884 	if (obj_id <= OBJ_NONE)
2187b449fa8aSyw161884 		return (obj_id);
2188b449fa8aSyw161884 
2189b449fa8aSyw161884 	do {
2190b449fa8aSyw161884 		(void) obj_get_attr(raid_tab, obj_id, (void **)(&prop_attr));
2191b449fa8aSyw161884 		if (prop_attr->prop_id == prop_id)
2192b449fa8aSyw161884 			break;
2193b449fa8aSyw161884 
2194b449fa8aSyw161884 		obj_id = obj_get_sibling(raid_tab, obj_id);
2195b449fa8aSyw161884 		if (obj_id < OBJ_NONE)
2196b449fa8aSyw161884 			return (obj_id);
2197b449fa8aSyw161884 	} while (obj_id > OBJ_NONE);
2198b449fa8aSyw161884 
2199b449fa8aSyw161884 	return (obj_id);
2200711890bcSjc156560 }
2201711890bcSjc156560 
2202711890bcSjc156560 static raid_obj_id_t
2203711890bcSjc156560 obj_get_controller(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2204711890bcSjc156560 {
2205711890bcSjc156560 	raid_obj_id_t id = obj_id;
2206711890bcSjc156560 
2207711890bcSjc156560 	while (raid_obj_get_type(raid_tab, id) != OBJ_TYPE_CONTROLLER) {
2208711890bcSjc156560 		id = raid_obj_get_container(raid_tab, id);
2209711890bcSjc156560 		if ((id == OBJ_SYSTEM) || (id < OBJ_NONE))
2210711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2211711890bcSjc156560 	}
2212711890bcSjc156560 
2213711890bcSjc156560 	return (id);
2214711890bcSjc156560 }
2215711890bcSjc156560 
2216711890bcSjc156560 /*
2217711890bcSjc156560  * Raid object operation routines
2218711890bcSjc156560  */
2219711890bcSjc156560 static int
2220711890bcSjc156560 obj_sys_compnum(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2221711890bcSjc156560 	raid_obj_type_id_t comp_type)
2222711890bcSjc156560 {
2223711890bcSjc156560 	DIR *dir;
2224711890bcSjc156560 	struct dirent *dp;
2225711890bcSjc156560 	int num = 0;
2226711890bcSjc156560 
2227711890bcSjc156560 	if ((raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_SYSTEM))
2228711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2229711890bcSjc156560 
2230711890bcSjc156560 	if (comp_type != OBJ_TYPE_CONTROLLER)
2231711890bcSjc156560 		return (0);
2232711890bcSjc156560 
2233711890bcSjc156560 	if ((dir = opendir(CFGDIR)) == NULL)
2234711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
2235711890bcSjc156560 
2236711890bcSjc156560 	while ((dp = readdir(dir)) != NULL) {
2237711890bcSjc156560 		uint32_t controller_id;
2238711890bcSjc156560 		char path[MAX_PATH_LEN];
2239711890bcSjc156560 
2240711890bcSjc156560 		if (strcmp(dp->d_name, ".") == 0 ||
2241711890bcSjc156560 		    strcmp(dp->d_name, "..") == 0)
2242711890bcSjc156560 			continue;
2243711890bcSjc156560 
2244711890bcSjc156560 		if (sscanf(dp->d_name, "c%u", &controller_id) != 1)
2245711890bcSjc156560 			continue;
2246711890bcSjc156560 
2247711890bcSjc156560 		if (controller_id_to_path(controller_id, path) == SUCCESS)
2248711890bcSjc156560 			++ num;
2249711890bcSjc156560 	}
2250711890bcSjc156560 
2251711890bcSjc156560 	(void) closedir(dir);
2252711890bcSjc156560 	return (num);
2253711890bcSjc156560 }
2254711890bcSjc156560 
2255711890bcSjc156560 static int
2256711890bcSjc156560 obj_sys_complist(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2257711890bcSjc156560 	int num, raid_obj_id_t *comp_list, raid_obj_type_id_t comp_type)
2258711890bcSjc156560 {
2259711890bcSjc156560 	DIR *dir;
2260711890bcSjc156560 	struct dirent *dp;
2261711890bcSjc156560 	controller_attr_t *attr;
2262711890bcSjc156560 	uint32_t controller_id;
2263711890bcSjc156560 	uint32_t *tmplist;
2264711890bcSjc156560 	char path[MAX_PATH_LEN];
2265711890bcSjc156560 	int i = 0;
2266711890bcSjc156560 
2267711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_SYSTEM)
2268711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2269711890bcSjc156560 	if ((num <= 0) || (comp_list == NULL))
2270711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2271711890bcSjc156560 
2272711890bcSjc156560 	if (comp_type != OBJ_TYPE_CONTROLLER)
2273711890bcSjc156560 		return (0);
2274711890bcSjc156560 
2275711890bcSjc156560 	if ((dir = opendir(CFGDIR)) == NULL)
2276711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
2277711890bcSjc156560 	tmplist = calloc(num, sizeof (uint32_t));
2278711890bcSjc156560 	if (tmplist == NULL) {
2279711890bcSjc156560 		return (ERR_NOMEM);
2280711890bcSjc156560 	}
2281711890bcSjc156560 	while ((dp = readdir(dir)) != NULL) {
2282711890bcSjc156560 		if (strcmp(dp->d_name, ".") == 0 ||
2283711890bcSjc156560 		    strcmp(dp->d_name, "..") == 0)
2284711890bcSjc156560 			continue;
2285711890bcSjc156560 
2286711890bcSjc156560 		if (sscanf(dp->d_name, "c%u", &controller_id) != 1)
2287711890bcSjc156560 			continue;
2288711890bcSjc156560 
2289711890bcSjc156560 		if (controller_id_to_path(controller_id, path) == SUCCESS) {
2290711890bcSjc156560 			tmplist[i] = controller_id;
2291711890bcSjc156560 			++ i;
2292711890bcSjc156560 		}
2293711890bcSjc156560 	}
2294711890bcSjc156560 	qsort((void *)tmplist, num, sizeof (uint32_t), intcompare);
2295711890bcSjc156560 	for (i = 0; i < num; i++) {
2296711890bcSjc156560 		attr = raid_obj_get_data_ptr(raid_tab,
2297711890bcSjc156560 		    *(comp_list + i));
2298711890bcSjc156560 
2299711890bcSjc156560 		if (attr == NULL) {
2300711890bcSjc156560 			free(tmplist);
2301711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2302711890bcSjc156560 		}
2303711890bcSjc156560 
2304711890bcSjc156560 		attr->controller_id = tmplist[i];
2305711890bcSjc156560 	}
2306711890bcSjc156560 	free(tmplist);
2307711890bcSjc156560 	(void) closedir(dir);
2308711890bcSjc156560 	return (SUCCESS);
2309711890bcSjc156560 }
2310711890bcSjc156560 
2311711890bcSjc156560 static int
2312711890bcSjc156560 obj_controller_compnum(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2313711890bcSjc156560 	raid_obj_type_id_t comp_type)
2314711890bcSjc156560 {
2315711890bcSjc156560 	raid_lib_t *raid_lib;
2316711890bcSjc156560 	int ret = SUCCESS, fd;
2317711890bcSjc156560 	controller_attr_t *ctl_attrp;
2318711890bcSjc156560 
2319711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
2320711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2321711890bcSjc156560 
2322711890bcSjc156560 	if ((comp_type != OBJ_TYPE_ARRAY) && (comp_type != OBJ_TYPE_DISK))
2323711890bcSjc156560 		return (0);
2324711890bcSjc156560 
2325711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, obj_id);
2326711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, obj_id);
2327711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, obj_id);
2328711890bcSjc156560 	if ((raid_lib == NULL) || (ctl_attrp == NULL) || (fd == 0))
2329711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2330711890bcSjc156560 
2331711890bcSjc156560 	ret = raid_lib->compnum(ctl_attrp->controller_id, 0,
2332711890bcSjc156560 	    OBJ_TYPE_CONTROLLER, comp_type);
2333711890bcSjc156560 
2334711890bcSjc156560 	return (ret);
2335711890bcSjc156560 }
2336711890bcSjc156560 
2337711890bcSjc156560 static int
2338711890bcSjc156560 obj_controller_complist(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2339711890bcSjc156560 	int comp_num, raid_obj_id_t *comp_list, raid_obj_type_id_t comp_type)
2340711890bcSjc156560 {
2341711890bcSjc156560 	raid_lib_t *raid_lib;
2342711890bcSjc156560 	controller_attr_t *ctl_attrp;
2343711890bcSjc156560 	int ret, i, fd;
2344711890bcSjc156560 	uint32_t *ids;
2345711890bcSjc156560 
2346711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
2347711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2348711890bcSjc156560 
2349711890bcSjc156560 	if ((comp_type != OBJ_TYPE_ARRAY) && (comp_type != OBJ_TYPE_DISK))
2350711890bcSjc156560 		return (0);
2351711890bcSjc156560 
2352711890bcSjc156560 	if ((comp_num <= 0) || (comp_list == NULL))
2353711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2354711890bcSjc156560 
2355711890bcSjc156560 	for (i = 0; i < comp_num; ++i)
2356711890bcSjc156560 		if (raid_obj_get_type(raid_tab, *(comp_list + i)) !=
2357711890bcSjc156560 		    comp_type)
2358711890bcSjc156560 			return (ERR_DEVICE_TYPE);
2359711890bcSjc156560 
2360711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, obj_id);
2361711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, obj_id);
2362711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, obj_id);
2363711890bcSjc156560 	if ((raid_lib == NULL) || (ctl_attrp == NULL)|| (fd == 0))
2364711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2365711890bcSjc156560 
2366711890bcSjc156560 	ids = malloc(comp_num * sizeof (uint32_t));
2367711890bcSjc156560 	if (ids == NULL)
2368711890bcSjc156560 		return (ERR_NOMEM);
2369711890bcSjc156560 
2370711890bcSjc156560 	ret = raid_lib->complist(ctl_attrp->controller_id, 0,
2371711890bcSjc156560 	    OBJ_TYPE_CONTROLLER, comp_type, comp_num, ids);
2372711890bcSjc156560 	if (ret < SUCCESS) {
2373711890bcSjc156560 		free(ids);
2374711890bcSjc156560 		return (ret);
2375711890bcSjc156560 	}
2376711890bcSjc156560 	qsort((void *)ids, comp_num, sizeof (uint32_t), intcompare);
2377711890bcSjc156560 	for (i = 0; i < comp_num; ++ i) {
2378711890bcSjc156560 		array_attr_t *array_attr;
2379711890bcSjc156560 		disk_attr_t *disk_attr;
2380711890bcSjc156560 		void *attr_buf;
2381711890bcSjc156560 
2382711890bcSjc156560 		attr_buf = raid_obj_get_data_ptr(raid_tab, *(comp_list + i));
2383711890bcSjc156560 		if (attr_buf == NULL) {
2384711890bcSjc156560 			free(ids);
2385711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2386711890bcSjc156560 		}
2387711890bcSjc156560 
2388711890bcSjc156560 		switch (comp_type) {
2389711890bcSjc156560 		case OBJ_TYPE_ARRAY:
2390711890bcSjc156560 			array_attr = attr_buf;
2391711890bcSjc156560 			array_attr->array_id = *(ids + i);
2392711890bcSjc156560 			break;
2393711890bcSjc156560 		case OBJ_TYPE_DISK:
2394711890bcSjc156560 			disk_attr = attr_buf;
2395711890bcSjc156560 			disk_attr->disk_id = *(ids + i);
2396711890bcSjc156560 			break;
2397711890bcSjc156560 		default:
2398711890bcSjc156560 			free(ids);
2399711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2400711890bcSjc156560 		}
2401711890bcSjc156560 	}
2402711890bcSjc156560 
2403711890bcSjc156560 	free(ids);
2404711890bcSjc156560 	return (SUCCESS);
2405711890bcSjc156560 }
2406711890bcSjc156560 
2407711890bcSjc156560 static int
2408711890bcSjc156560 obj_controller_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2409711890bcSjc156560 {
2410711890bcSjc156560 	controller_attr_t *attr;
2411711890bcSjc156560 	raid_lib_t *raid_lib;
2412711890bcSjc156560 	int ret = SUCCESS, fd;
2413711890bcSjc156560 
2414711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
2415711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2416711890bcSjc156560 
2417711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
2418711890bcSjc156560 		return (SUCCESS);
2419711890bcSjc156560 
2420711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2421711890bcSjc156560 	if (attr == NULL)
2422711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2423711890bcSjc156560 
2424711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, obj_id);
2425711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, obj_id);
2426711890bcSjc156560 
2427711890bcSjc156560 	/*
2428711890bcSjc156560 	 * For a controller, even it's not opened, we can still
2429711890bcSjc156560 	 * get the driver name
2430711890bcSjc156560 	 */
2431711890bcSjc156560 
2432711890bcSjc156560 	if (fd == 0)
2433711890bcSjc156560 		return (SUCCESS);
2434711890bcSjc156560 
2435711890bcSjc156560 	if (raid_lib == NULL) {
2436711890bcSjc156560 		return (SUCCESS);
2437711890bcSjc156560 	}
2438711890bcSjc156560 
2439711890bcSjc156560 	ret = raid_lib->get_attr(attr->controller_id, OBJ_ATTR_NONE,
2440711890bcSjc156560 	    OBJ_ATTR_NONE, OBJ_TYPE_CONTROLLER, attr);
2441711890bcSjc156560 	if (ret < SUCCESS)
2442711890bcSjc156560 		return (ret);
2443711890bcSjc156560 
2444711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
2445711890bcSjc156560 
2446711890bcSjc156560 	return (ret);
2447711890bcSjc156560 }
2448711890bcSjc156560 
2449711890bcSjc156560 static int
2450711890bcSjc156560 obj_controller_act(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2451711890bcSjc156560 	uint32_t sub_cmd, void *prop_list, char **plugin_err_str)
2452711890bcSjc156560 {
2453711890bcSjc156560 	controller_attr_t *attr;
2454711890bcSjc156560 	raid_lib_t *raid_lib;
2455711890bcSjc156560 	int ret, fd;
2456711890bcSjc156560 
2457711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_CONTROLLER)
2458711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2459711890bcSjc156560 
2460711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2461711890bcSjc156560 
2462711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, obj_id);
2463711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, obj_id);
2464711890bcSjc156560 
2465711890bcSjc156560 	switch (sub_cmd) {
2466711890bcSjc156560 	case ACT_CONTROLLER_OPEN:
2467711890bcSjc156560 		/* Check if already opened */
2468711890bcSjc156560 
2469711890bcSjc156560 		if (fd > 0)
2470711890bcSjc156560 			return (SUCCESS);
2471711890bcSjc156560 
2472711890bcSjc156560 		/* Check if plugin is already attached */
2473711890bcSjc156560 		if (raid_lib == NULL) {
2474711890bcSjc156560 			raid_lib = raid_find_lib(raid_tab, obj_id);
2475711890bcSjc156560 			if (raid_lib == NULL)
2476711890bcSjc156560 				return (ERR_DRIVER_NOT_FOUND);
2477711890bcSjc156560 		}
2478711890bcSjc156560 
2479711890bcSjc156560 		ret = raid_lib->open_controller(attr->controller_id,
2480711890bcSjc156560 		    plugin_err_str);
2481711890bcSjc156560 		if (ret == SUCCESS) {
2482711890bcSjc156560 			(void) raid_obj_set_lib(raid_tab, obj_id, raid_lib);
2483711890bcSjc156560 			(void) raid_obj_set_fd(raid_tab, obj_id, 1);
2484711890bcSjc156560 		}
2485711890bcSjc156560 		break;
2486711890bcSjc156560 	case ACT_CONTROLLER_CLOSE:
2487711890bcSjc156560 
2488711890bcSjc156560 		if (fd <= 0)
2489711890bcSjc156560 			return (SUCCESS);
2490711890bcSjc156560 
2491711890bcSjc156560 		if (raid_lib == NULL) {
2492711890bcSjc156560 			return (SUCCESS);
2493711890bcSjc156560 		}
2494711890bcSjc156560 		ret = raid_lib->close_controller(attr->controller_id,
2495711890bcSjc156560 		    plugin_err_str);
2496711890bcSjc156560 		if (ret == SUCCESS) {
2497711890bcSjc156560 			(void) raid_obj_set_fd(raid_tab, obj_id, 0);
2498711890bcSjc156560 			(void) raid_obj_set_lib(raid_tab, obj_id, NULL);
2499711890bcSjc156560 			raid_handle_delete_controller_comp(attr->controller_id);
2500711890bcSjc156560 		}
2501711890bcSjc156560 		break;
2502711890bcSjc156560 	case ACT_CONTROLLER_FLASH_FW:
2503711890bcSjc156560 		{
2504711890bcSjc156560 			char		*filebuf;
2505711890bcSjc156560 			int		image_fd;
2506711890bcSjc156560 			uint32_t	size;
2507711890bcSjc156560 			struct stat	statbuf;
2508711890bcSjc156560 
2509711890bcSjc156560 			if (prop_list == NULL)
2510711890bcSjc156560 				return (ERR_OP_ILLEGAL);
2511711890bcSjc156560 
2512711890bcSjc156560 			/* Open firmware image file */
2513711890bcSjc156560 			image_fd = open((const char *)prop_list,
2514711890bcSjc156560 			    O_RDONLY | O_NDELAY);
2515711890bcSjc156560 			if (image_fd == -1)
2516711890bcSjc156560 				return (ERR_OP_FAILED);
2517711890bcSjc156560 
2518711890bcSjc156560 			if (fstat(image_fd, &statbuf) != 0) {
2519711890bcSjc156560 				(void) close(image_fd);
2520711890bcSjc156560 				return (ERR_OP_FAILED);
2521711890bcSjc156560 			}
2522711890bcSjc156560 
2523711890bcSjc156560 			filebuf = malloc(statbuf.st_size);
2524711890bcSjc156560 			if (filebuf == NULL) {
2525711890bcSjc156560 				(void) close(image_fd);
2526711890bcSjc156560 				return (ERR_NOMEM);
2527711890bcSjc156560 			}
2528711890bcSjc156560 
2529711890bcSjc156560 			size = read(image_fd, filebuf, statbuf.st_size);
2530711890bcSjc156560 			if (size != statbuf.st_size) {
2531711890bcSjc156560 				(void) close(image_fd);
2532711890bcSjc156560 				free(filebuf);
2533711890bcSjc156560 				return (ERR_OP_FAILED);
2534711890bcSjc156560 			}
2535711890bcSjc156560 
2536711890bcSjc156560 			if (fd <= 0) {
2537711890bcSjc156560 				(void) close(image_fd);
2538711890bcSjc156560 				free(filebuf);
2539711890bcSjc156560 				return (ERR_DRIVER_CLOSED);
2540711890bcSjc156560 			}
2541711890bcSjc156560 
2542711890bcSjc156560 			if (raid_lib == NULL) {
2543711890bcSjc156560 				(void) close(image_fd);
2544711890bcSjc156560 				free(filebuf);
2545711890bcSjc156560 				return (ERR_DRIVER_CLOSED);
2546711890bcSjc156560 			}
2547711890bcSjc156560 			if (raid_lib->flash_fw == NULL) {
2548711890bcSjc156560 				(void) close(image_fd);
2549711890bcSjc156560 				free(filebuf);
2550711890bcSjc156560 				return (ERR_OP_NO_IMPL);
2551711890bcSjc156560 			}
2552711890bcSjc156560 
2553711890bcSjc156560 			ret = raid_lib->flash_fw(attr->controller_id,
2554711890bcSjc156560 			    filebuf, size, plugin_err_str);
2555711890bcSjc156560 		}
2556711890bcSjc156560 		break;
2557711890bcSjc156560 	default:
2558711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2559711890bcSjc156560 	}
2560711890bcSjc156560 
2561711890bcSjc156560 	return (ret);
2562711890bcSjc156560 }
2563711890bcSjc156560 
2564711890bcSjc156560 static int
2565711890bcSjc156560 obj_array_compnum(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2566711890bcSjc156560 	raid_obj_type_id_t comp_type)
2567711890bcSjc156560 {
2568711890bcSjc156560 	array_attr_t *attr;
2569711890bcSjc156560 	controller_attr_t *ctl_attrp;
2570711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2571711890bcSjc156560 	raid_lib_t *raid_lib;
2572711890bcSjc156560 	int ret = SUCCESS, fd;
2573711890bcSjc156560 
2574711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY)
2575711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2576711890bcSjc156560 
2577711890bcSjc156560 	if (comp_type != OBJ_TYPE_ARRAY_PART &&
2578711890bcSjc156560 	    comp_type != OBJ_TYPE_ARRAY &&
2579711890bcSjc156560 	    comp_type != OBJ_TYPE_TASK)
2580711890bcSjc156560 		return (0);
2581711890bcSjc156560 
2582711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2583711890bcSjc156560 	if (attr == NULL)
2584711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2585711890bcSjc156560 
2586711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2587711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2588711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2589711890bcSjc156560 
2590711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2591711890bcSjc156560 	if (ctl_attrp == NULL) {
2592711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2593711890bcSjc156560 	}
2594711890bcSjc156560 
2595711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2596711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2597711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2598711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2599711890bcSjc156560 
2600711890bcSjc156560 	ret = raid_lib->compnum(ctl_attrp->controller_id, attr->array_id,
2601711890bcSjc156560 	    OBJ_TYPE_ARRAY, comp_type);
2602711890bcSjc156560 
2603711890bcSjc156560 	return (ret);
2604711890bcSjc156560 }
2605711890bcSjc156560 
2606711890bcSjc156560 static int
2607711890bcSjc156560 obj_array_complist(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2608711890bcSjc156560 	int comp_num, raid_obj_id_t *comp_list, raid_obj_type_id_t comp_type)
2609711890bcSjc156560 {
2610711890bcSjc156560 	array_attr_t *attr;
2611711890bcSjc156560 	controller_attr_t *ctl_attrp;
2612711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2613711890bcSjc156560 	raid_lib_t *raid_lib;
2614711890bcSjc156560 	int ret, i, fd;
2615711890bcSjc156560 	uint32_t *ids;
2616711890bcSjc156560 
2617711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY)
2618711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2619711890bcSjc156560 
2620711890bcSjc156560 	if (comp_type != OBJ_TYPE_ARRAY_PART &&
2621711890bcSjc156560 	    comp_type != OBJ_TYPE_ARRAY &&
2622711890bcSjc156560 	    comp_type != OBJ_TYPE_TASK)
2623711890bcSjc156560 		return (0);
2624711890bcSjc156560 
2625711890bcSjc156560 	if (comp_num <= 0 || comp_list == NULL)
2626711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2627711890bcSjc156560 
2628711890bcSjc156560 	for (i = 0; i < comp_num; ++i)
2629711890bcSjc156560 		if (raid_obj_get_type(raid_tab, *(comp_list + i)) !=
2630711890bcSjc156560 		    comp_type)
2631711890bcSjc156560 			return (ERR_DEVICE_TYPE);
2632711890bcSjc156560 
2633711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2634711890bcSjc156560 	if (attr == NULL)
2635711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2636711890bcSjc156560 
2637711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2638711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2639711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2640711890bcSjc156560 
2641711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2642711890bcSjc156560 	if (ctl_attrp == NULL) {
2643711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2644711890bcSjc156560 	}
2645711890bcSjc156560 
2646711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2647711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2648711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2649711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2650711890bcSjc156560 
2651711890bcSjc156560 	ids = malloc(comp_num * sizeof (uint32_t));
2652711890bcSjc156560 	if (ids == NULL)
2653711890bcSjc156560 		return (ERR_NOMEM);
2654711890bcSjc156560 
2655711890bcSjc156560 	ret = raid_lib->complist(ctl_attrp->controller_id,
2656711890bcSjc156560 	    attr->array_id, OBJ_TYPE_ARRAY, comp_type, comp_num, ids);
2657711890bcSjc156560 
2658711890bcSjc156560 	if (ret < SUCCESS) {
2659711890bcSjc156560 		free(ids);
2660711890bcSjc156560 		return (ret);
2661711890bcSjc156560 	}
2662711890bcSjc156560 
2663711890bcSjc156560 	for (i = 0; i < comp_num; ++ i) {
2664711890bcSjc156560 		array_attr_t *array_attr;
2665711890bcSjc156560 		arraypart_attr_t *arraypart_attr;
2666711890bcSjc156560 		task_attr_t *task_attr;
2667711890bcSjc156560 		void *attr_buf;
2668711890bcSjc156560 
2669711890bcSjc156560 		attr_buf = raid_obj_get_data_ptr(raid_tab, *(comp_list + i));
2670711890bcSjc156560 		if (attr_buf == NULL) {
2671711890bcSjc156560 			free(ids);
2672711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2673711890bcSjc156560 		}
2674711890bcSjc156560 
2675711890bcSjc156560 		switch (comp_type) {
2676711890bcSjc156560 		case OBJ_TYPE_ARRAY:
2677711890bcSjc156560 			array_attr = attr_buf;
2678711890bcSjc156560 			array_attr->array_id = *(ids + i);
2679711890bcSjc156560 			break;
2680711890bcSjc156560 		case OBJ_TYPE_ARRAY_PART:
2681711890bcSjc156560 			arraypart_attr = attr_buf;
2682711890bcSjc156560 			arraypart_attr->disk_id = *(ids + i);
2683711890bcSjc156560 			break;
2684711890bcSjc156560 		case OBJ_TYPE_TASK:
2685711890bcSjc156560 			task_attr = attr_buf;
2686711890bcSjc156560 			task_attr->task_id = *(ids + i);
2687711890bcSjc156560 			break;
2688711890bcSjc156560 		default:
2689711890bcSjc156560 			free(ids);
2690711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2691711890bcSjc156560 		}
2692711890bcSjc156560 	}
2693711890bcSjc156560 
2694711890bcSjc156560 
2695711890bcSjc156560 	free(ids);
2696711890bcSjc156560 	return (ret);
2697711890bcSjc156560 }
2698711890bcSjc156560 
2699711890bcSjc156560 static int
2700711890bcSjc156560 obj_array_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2701711890bcSjc156560 {
2702711890bcSjc156560 	array_attr_t *attr;
2703711890bcSjc156560 	controller_attr_t *ctl_attrp;
2704711890bcSjc156560 	raid_lib_t *raid_lib;
2705711890bcSjc156560 	int ret = SUCCESS, fd;
2706711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2707711890bcSjc156560 
2708711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY)
2709711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2710711890bcSjc156560 
2711711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
2712711890bcSjc156560 		return (SUCCESS);
2713711890bcSjc156560 
2714711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2715711890bcSjc156560 	if (attr == NULL)
2716711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2717711890bcSjc156560 
2718711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2719711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2720711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2721711890bcSjc156560 
2722711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2723711890bcSjc156560 	if (ctl_attrp == NULL) {
2724711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2725711890bcSjc156560 	}
2726711890bcSjc156560 
2727711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2728711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2729711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2730711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2731711890bcSjc156560 
2732711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
2733711890bcSjc156560 	    attr->array_id, 0, OBJ_TYPE_ARRAY, attr);
2734711890bcSjc156560 
2735711890bcSjc156560 	if (ret < SUCCESS)
2736711890bcSjc156560 		return (ret);
2737711890bcSjc156560 
2738711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
2739711890bcSjc156560 
2740711890bcSjc156560 	return (ret);
2741711890bcSjc156560 }
2742711890bcSjc156560 
2743711890bcSjc156560 static int
2744711890bcSjc156560 obj_array_set_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2745711890bcSjc156560 	uint32_t sub_cmd, uint32_t *value, char **plugin_err_str)
2746711890bcSjc156560 {
2747711890bcSjc156560 	array_attr_t *attr;
2748711890bcSjc156560 	controller_attr_t *ctl_attrp;
2749711890bcSjc156560 	raid_lib_t *raid_lib;
2750711890bcSjc156560 	int ret = SUCCESS, fd;
2751711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2752711890bcSjc156560 
2753711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY)
2754711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2755711890bcSjc156560 
2756711890bcSjc156560 	switch (sub_cmd) {
2757711890bcSjc156560 	case SET_CACHE_WR_PLY:
2758711890bcSjc156560 		if (*value != CACHE_WR_OFF &&
2759711890bcSjc156560 		    *value != CACHE_WR_ON)
2760711890bcSjc156560 			return (ERR_OP_ILLEGAL);
2761711890bcSjc156560 		break;
2762711890bcSjc156560 	case SET_CACHE_RD_PLY:
2763711890bcSjc156560 		if (*value != CACHE_RD_OFF &&
2764711890bcSjc156560 		    *value != CACHE_RD_ON)
2765711890bcSjc156560 			return (ERR_OP_ILLEGAL);
2766711890bcSjc156560 		break;
2767b449fa8aSyw161884 	case SET_ACTIVATION_PLY:
2768b449fa8aSyw161884 		if (*value != ARRAY_ACT_ACTIVATE)
2769b449fa8aSyw161884 			return (ERR_OP_ILLEGAL);
2770b449fa8aSyw161884 		break;
2771711890bcSjc156560 	default:
2772711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2773711890bcSjc156560 	}
2774711890bcSjc156560 
2775711890bcSjc156560 	(void) obj_get_attr(raid_tab, obj_id, (void **)(&attr));
2776711890bcSjc156560 
2777711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2778711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2779711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2780711890bcSjc156560 
2781711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2782711890bcSjc156560 	if (ctl_attrp == NULL) {
2783711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2784711890bcSjc156560 	}
2785711890bcSjc156560 
2786711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2787711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2788711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2789711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2790711890bcSjc156560 
2791711890bcSjc156560 	if (raid_lib->set_attr == NULL)
2792711890bcSjc156560 		return (ERR_OP_NO_IMPL);
2793711890bcSjc156560 
2794711890bcSjc156560 	ret = raid_lib->set_attr(ctl_attrp->controller_id,
2795711890bcSjc156560 	    attr->array_id, sub_cmd, value, plugin_err_str);
2796711890bcSjc156560 
2797711890bcSjc156560 	return (ret);
2798711890bcSjc156560 }
2799711890bcSjc156560 
2800711890bcSjc156560 static int
2801711890bcSjc156560 obj_disk_compnum(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2802711890bcSjc156560 	raid_obj_type_id_t comp_type)
2803711890bcSjc156560 {
2804711890bcSjc156560 	disk_attr_t *attr;
2805711890bcSjc156560 	controller_attr_t *ctl_attrp;
2806711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2807711890bcSjc156560 	raid_lib_t *raid_lib;
2808711890bcSjc156560 	int ret = SUCCESS, fd;
2809711890bcSjc156560 
2810711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_DISK)
2811711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2812711890bcSjc156560 
2813711890bcSjc156560 	if (comp_type != OBJ_TYPE_DISK_SEG &&
2814711890bcSjc156560 	    comp_type != OBJ_TYPE_HSP &&
2815b449fa8aSyw161884 	    comp_type != OBJ_TYPE_TASK &&
2816b449fa8aSyw161884 	    comp_type != OBJ_TYPE_PROP)
2817711890bcSjc156560 		return (0);
2818711890bcSjc156560 	ret = obj_get_attr(raid_tab, obj_id, (void **)(&attr));
2819711890bcSjc156560 	if ((ret != SUCCESS) || (attr == NULL)) {
2820711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2821711890bcSjc156560 	}
2822711890bcSjc156560 	if (attr->state == DISK_STATE_FAILED) {
2823711890bcSjc156560 		return (SUCCESS);
2824711890bcSjc156560 	}
2825711890bcSjc156560 
2826711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2827711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2828711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2829711890bcSjc156560 
2830711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2831711890bcSjc156560 	if (ctl_attrp == NULL) {
2832711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2833711890bcSjc156560 	}
2834711890bcSjc156560 
2835711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2836711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2837711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2838711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2839711890bcSjc156560 
2840711890bcSjc156560 	ret = raid_lib->compnum(ctl_attrp->controller_id,
2841711890bcSjc156560 	    attr->disk_id, OBJ_TYPE_DISK, comp_type);
2842711890bcSjc156560 
2843711890bcSjc156560 	return (ret);
2844711890bcSjc156560 }
2845711890bcSjc156560 
2846711890bcSjc156560 static int
2847711890bcSjc156560 obj_disk_complist(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
2848711890bcSjc156560 	int comp_num, raid_obj_id_t *comp_list, raid_obj_type_id_t comp_type)
2849711890bcSjc156560 {
2850711890bcSjc156560 	disk_attr_t *attr;
2851711890bcSjc156560 	controller_attr_t *ctl_attrp;
2852711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2853711890bcSjc156560 	raid_lib_t *raid_lib;
2854711890bcSjc156560 	int ret, i, fd;
2855711890bcSjc156560 	uint32_t *ids;
2856711890bcSjc156560 
2857711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_DISK)
2858711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2859711890bcSjc156560 
2860711890bcSjc156560 	if (comp_type != OBJ_TYPE_DISK_SEG &&
2861711890bcSjc156560 	    comp_type != OBJ_TYPE_HSP &&
2862b449fa8aSyw161884 	    comp_type != OBJ_TYPE_TASK &&
2863b449fa8aSyw161884 	    comp_type != OBJ_TYPE_PROP)
2864711890bcSjc156560 		return (0);
2865711890bcSjc156560 
2866711890bcSjc156560 	if (comp_num <= 0 || comp_list == NULL)
2867711890bcSjc156560 		return (ERR_OP_ILLEGAL);
2868711890bcSjc156560 
2869711890bcSjc156560 	for (i = 0; i < comp_num; ++i)
2870711890bcSjc156560 		if (raid_obj_get_type(raid_tab, *(comp_list + i)) !=
2871711890bcSjc156560 		    comp_type)
2872711890bcSjc156560 			return (ERR_DEVICE_TYPE);
2873711890bcSjc156560 	ret = obj_get_attr(raid_tab, obj_id, (void **)(&attr));
2874711890bcSjc156560 	if ((ret != SUCCESS) || (attr == NULL)) {
2875711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2876711890bcSjc156560 	}
2877711890bcSjc156560 	if (attr->state == DISK_STATE_FAILED) {
2878711890bcSjc156560 		return (SUCCESS);
2879711890bcSjc156560 	}
2880711890bcSjc156560 
2881711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2882711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2883711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2884711890bcSjc156560 
2885711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2886711890bcSjc156560 	if (ctl_attrp == NULL) {
2887711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2888711890bcSjc156560 	}
2889711890bcSjc156560 
2890711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2891711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2892711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2893711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2894711890bcSjc156560 
2895711890bcSjc156560 	ids = malloc(comp_num * sizeof (uint32_t));
2896711890bcSjc156560 	if (ids == NULL)
2897711890bcSjc156560 		return (ERR_NOMEM);
2898711890bcSjc156560 
2899711890bcSjc156560 	ret = raid_lib->complist(ctl_attrp->controller_id,
2900711890bcSjc156560 	    attr->disk_id, OBJ_TYPE_DISK, comp_type, comp_num, ids);
2901711890bcSjc156560 
2902711890bcSjc156560 	if (ret < SUCCESS) {
2903711890bcSjc156560 		free(ids);
2904711890bcSjc156560 		return (ret);
2905711890bcSjc156560 	}
2906711890bcSjc156560 
2907711890bcSjc156560 	for (i = 0; i < comp_num; ++ i) {
2908711890bcSjc156560 		diskseg_attr_t *diskseg_attr;
2909711890bcSjc156560 		hsp_attr_t *hsp_attr;
2910711890bcSjc156560 		task_attr_t *task_attr;
2911b449fa8aSyw161884 		property_attr_t *prop_attr;
2912711890bcSjc156560 		void *attr_buf;
2913711890bcSjc156560 
2914711890bcSjc156560 		attr_buf = raid_obj_get_data_ptr(raid_tab, *(comp_list + i));
2915711890bcSjc156560 		if (attr_buf == NULL) {
2916711890bcSjc156560 			free(ids);
2917711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2918711890bcSjc156560 		}
2919711890bcSjc156560 
2920711890bcSjc156560 		switch (comp_type) {
2921711890bcSjc156560 		case OBJ_TYPE_DISK_SEG:
2922711890bcSjc156560 			diskseg_attr = attr_buf;
2923711890bcSjc156560 			diskseg_attr->seq_no = *(ids + i);
2924711890bcSjc156560 			break;
2925711890bcSjc156560 		case OBJ_TYPE_HSP:
2926711890bcSjc156560 			hsp_attr = attr_buf;
2927711890bcSjc156560 			hsp_attr->associated_id = *(ids + i);
2928711890bcSjc156560 			break;
2929711890bcSjc156560 		case OBJ_TYPE_TASK:
2930711890bcSjc156560 			task_attr = attr_buf;
2931711890bcSjc156560 			task_attr->task_id = *(ids + i);
2932711890bcSjc156560 			break;
2933b449fa8aSyw161884 		case OBJ_TYPE_PROP:
2934b449fa8aSyw161884 			prop_attr = attr_buf;
2935b449fa8aSyw161884 			prop_attr->prop_id = *(ids + i);
2936b449fa8aSyw161884 			break;
2937711890bcSjc156560 		default:
2938711890bcSjc156560 			free(ids);
2939711890bcSjc156560 			return (ERR_DEVICE_INVALID);
2940711890bcSjc156560 		}
2941711890bcSjc156560 	}
2942711890bcSjc156560 
2943711890bcSjc156560 
2944711890bcSjc156560 	free(ids);
2945711890bcSjc156560 	return (ret);
2946711890bcSjc156560 }
2947711890bcSjc156560 
2948711890bcSjc156560 static int
2949711890bcSjc156560 obj_disk_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2950711890bcSjc156560 {
2951711890bcSjc156560 	disk_attr_t *attr;
2952711890bcSjc156560 	controller_attr_t *ctl_attrp;
2953711890bcSjc156560 	raid_lib_t *raid_lib;
2954711890bcSjc156560 	int ret = SUCCESS, fd;
2955711890bcSjc156560 	raid_obj_id_t controller_obj_id;
2956711890bcSjc156560 
2957711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_DISK)
2958711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2959711890bcSjc156560 
2960711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
2961711890bcSjc156560 		return (SUCCESS);
2962711890bcSjc156560 
2963711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
2964711890bcSjc156560 	if (attr == NULL)
2965711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2966711890bcSjc156560 
2967711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
2968711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
2969711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2970711890bcSjc156560 
2971711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
2972711890bcSjc156560 	if (ctl_attrp == NULL) {
2973711890bcSjc156560 		return (ERR_DEVICE_INVALID);
2974711890bcSjc156560 	}
2975711890bcSjc156560 
2976711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
2977711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
2978711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
2979711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
2980711890bcSjc156560 
2981711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
2982711890bcSjc156560 	    attr->disk_id, 0, OBJ_TYPE_DISK, attr);
2983711890bcSjc156560 
2984711890bcSjc156560 	if (ret < SUCCESS)
2985711890bcSjc156560 		return (ret);
2986711890bcSjc156560 
2987711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
2988711890bcSjc156560 
2989711890bcSjc156560 	return (ret);
2990711890bcSjc156560 }
2991711890bcSjc156560 
2992711890bcSjc156560 static int
2993711890bcSjc156560 obj_hsp_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
2994711890bcSjc156560 {
2995711890bcSjc156560 	hsp_attr_t *attr;
2996711890bcSjc156560 
2997711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_HSP)
2998711890bcSjc156560 		return (ERR_DEVICE_TYPE);
2999711890bcSjc156560 
3000711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
3001711890bcSjc156560 		return (SUCCESS);
3002711890bcSjc156560 
3003711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3004711890bcSjc156560 	if (attr == NULL)
3005711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3006711890bcSjc156560 
3007711890bcSjc156560 	if (attr->associated_id == (uint32_t)OBJ_ATTR_NONE)
3008711890bcSjc156560 		attr->type = HSP_TYPE_GLOBAL;
3009711890bcSjc156560 	else
3010711890bcSjc156560 		attr->type = HSP_TYPE_LOCAL;
3011711890bcSjc156560 
3012711890bcSjc156560 	return (SUCCESS);
3013711890bcSjc156560 }
3014711890bcSjc156560 
3015711890bcSjc156560 static int
3016711890bcSjc156560 obj_arraypart_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
3017711890bcSjc156560 {
3018711890bcSjc156560 	arraypart_attr_t *attr;
3019711890bcSjc156560 	array_attr_t *array_attr;
3020711890bcSjc156560 	controller_attr_t *ctl_attrp;
3021711890bcSjc156560 	raid_lib_t *raid_lib;
3022711890bcSjc156560 	int ret = SUCCESS, fd;
3023711890bcSjc156560 	raid_obj_id_t controller_obj_id, array_obj_id;
3024711890bcSjc156560 
3025711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_ARRAY_PART)
3026711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3027711890bcSjc156560 
3028711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
3029711890bcSjc156560 		return (SUCCESS);
3030711890bcSjc156560 
3031711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3032711890bcSjc156560 	if (attr == NULL)
3033711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3034711890bcSjc156560 
3035711890bcSjc156560 	array_obj_id = raid_obj_get_container(raid_tab, obj_id);
3036711890bcSjc156560 	if (array_obj_id < OBJ_NONE)
3037711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3038711890bcSjc156560 
3039711890bcSjc156560 	array_attr = raid_obj_get_data_ptr(raid_tab, array_obj_id);
3040711890bcSjc156560 	if (array_attr == NULL)
3041711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3042711890bcSjc156560 
3043711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
3044711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
3045711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3046711890bcSjc156560 
3047711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
3048711890bcSjc156560 	if (ctl_attrp == NULL) {
3049711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3050711890bcSjc156560 	}
3051711890bcSjc156560 
3052711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3053711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3054711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3055711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3056711890bcSjc156560 
3057711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3058711890bcSjc156560 	    array_attr->array_id, attr->disk_id,
3059711890bcSjc156560 	    OBJ_TYPE_ARRAY_PART, attr);
3060711890bcSjc156560 
3061711890bcSjc156560 	if (ret < SUCCESS)
3062711890bcSjc156560 		return (ret);
3063711890bcSjc156560 
3064711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
3065711890bcSjc156560 
3066711890bcSjc156560 	return (ret);
3067711890bcSjc156560 }
3068711890bcSjc156560 
3069711890bcSjc156560 static int
3070711890bcSjc156560 obj_diskseg_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
3071711890bcSjc156560 {
3072711890bcSjc156560 	diskseg_attr_t *attr;
3073711890bcSjc156560 	disk_attr_t *disk_attr;
3074711890bcSjc156560 	controller_attr_t *ctl_attrp;
3075711890bcSjc156560 	raid_lib_t *raid_lib;
3076711890bcSjc156560 	int ret = SUCCESS, fd;
3077711890bcSjc156560 	raid_obj_id_t controller_obj_id, disk_obj_id;
3078711890bcSjc156560 
3079711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_DISK_SEG)
3080711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3081711890bcSjc156560 
3082711890bcSjc156560 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
3083711890bcSjc156560 		return (SUCCESS);
3084711890bcSjc156560 
3085711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3086711890bcSjc156560 	if (attr == NULL)
3087711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3088711890bcSjc156560 
3089711890bcSjc156560 	disk_obj_id = raid_obj_get_container(raid_tab, obj_id);
3090711890bcSjc156560 	if (disk_obj_id < OBJ_NONE)
3091711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3092711890bcSjc156560 
3093711890bcSjc156560 	disk_attr = raid_obj_get_data_ptr(raid_tab, disk_obj_id);
3094711890bcSjc156560 	if (disk_attr == NULL)
3095711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3096711890bcSjc156560 
3097711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
3098711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
3099711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3100711890bcSjc156560 
3101711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
3102711890bcSjc156560 	if (ctl_attrp == NULL) {
3103711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3104711890bcSjc156560 	}
3105711890bcSjc156560 
3106711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3107711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3108711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3109711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3110711890bcSjc156560 
3111711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3112711890bcSjc156560 	    disk_attr->disk_id, attr->seq_no, OBJ_TYPE_DISK_SEG, attr);
3113711890bcSjc156560 
3114711890bcSjc156560 	if (ret < SUCCESS)
3115711890bcSjc156560 		return (ret);
3116711890bcSjc156560 
3117711890bcSjc156560 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
3118711890bcSjc156560 
3119711890bcSjc156560 	return (ret);
3120711890bcSjc156560 }
3121711890bcSjc156560 
3122711890bcSjc156560 static int
3123711890bcSjc156560 obj_task_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
3124711890bcSjc156560 {
3125711890bcSjc156560 	task_attr_t *attr;
3126711890bcSjc156560 	controller_attr_t *ctl_attrp;
3127711890bcSjc156560 	raid_lib_t *raid_lib;
3128711890bcSjc156560 	int ret = SUCCESS, fd;
3129711890bcSjc156560 	raid_obj_id_t controller_obj_id;
3130711890bcSjc156560 
3131711890bcSjc156560 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_TASK)
3132711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3133711890bcSjc156560 
3134711890bcSjc156560 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3135711890bcSjc156560 	if (attr == NULL)
3136711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3137711890bcSjc156560 
3138711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
3139711890bcSjc156560 	if (controller_obj_id < OBJ_NONE)
3140711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3141711890bcSjc156560 
3142711890bcSjc156560 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
3143711890bcSjc156560 	if (ctl_attrp == NULL) {
3144711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3145711890bcSjc156560 	}
3146711890bcSjc156560 
3147711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3148711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3149711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3150711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3151711890bcSjc156560 
3152711890bcSjc156560 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3153711890bcSjc156560 	    attr->task_id, OBJ_ATTR_NONE, OBJ_TYPE_TASK, attr);
3154711890bcSjc156560 
3155711890bcSjc156560 	return (ret);
3156711890bcSjc156560 }
3157711890bcSjc156560 
3158711890bcSjc156560 static int
3159b449fa8aSyw161884 obj_prop_get_attr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
3160b449fa8aSyw161884 {
3161b449fa8aSyw161884 	property_attr_t *attr, *attr_new;
3162b449fa8aSyw161884 	disk_attr_t *disk_attr;
3163b449fa8aSyw161884 	controller_attr_t *ctl_attrp;
3164b449fa8aSyw161884 	raid_lib_t *raid_lib;
3165b449fa8aSyw161884 	int ret = SUCCESS, fd;
3166b449fa8aSyw161884 	raid_obj_id_t controller_obj_id, disk_obj_id;
3167b449fa8aSyw161884 
3168b449fa8aSyw161884 	if (raid_obj_get_type(raid_tab, obj_id) != OBJ_TYPE_PROP)
3169b449fa8aSyw161884 		return (ERR_DEVICE_TYPE);
3170b449fa8aSyw161884 
3171b449fa8aSyw161884 	if (raid_obj_get_status(raid_tab, obj_id) & OBJ_STATUS_OPENED)
3172b449fa8aSyw161884 		return (SUCCESS);
3173b449fa8aSyw161884 
3174b449fa8aSyw161884 	attr = raid_obj_get_data_ptr(raid_tab, obj_id);
3175b449fa8aSyw161884 	if (attr == NULL)
3176b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3177b449fa8aSyw161884 
3178b449fa8aSyw161884 	disk_obj_id = raid_obj_get_container(raid_tab, obj_id);
3179b449fa8aSyw161884 	if (disk_obj_id < OBJ_NONE)
3180b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3181b449fa8aSyw161884 
3182b449fa8aSyw161884 	disk_attr = raid_obj_get_data_ptr(raid_tab, disk_obj_id);
3183b449fa8aSyw161884 	if (disk_attr == NULL)
3184b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3185b449fa8aSyw161884 
3186b449fa8aSyw161884 	controller_obj_id = obj_get_controller(raid_tab, obj_id);
3187b449fa8aSyw161884 	if (controller_obj_id < OBJ_NONE)
3188b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3189b449fa8aSyw161884 
3190b449fa8aSyw161884 	ctl_attrp = raid_obj_get_data_ptr(raid_tab, controller_obj_id);
3191b449fa8aSyw161884 	if (ctl_attrp == NULL) {
3192b449fa8aSyw161884 		return (ERR_DEVICE_INVALID);
3193b449fa8aSyw161884 	}
3194b449fa8aSyw161884 
3195b449fa8aSyw161884 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3196b449fa8aSyw161884 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3197b449fa8aSyw161884 	if ((raid_lib == NULL) || (fd == 0))
3198b449fa8aSyw161884 		return (ERR_DRIVER_CLOSED);
3199b449fa8aSyw161884 
3200b449fa8aSyw161884 	/* Get the property size at first */
3201b449fa8aSyw161884 	attr->prop_size = 0;
3202b449fa8aSyw161884 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3203b449fa8aSyw161884 	    disk_attr->disk_id, OBJ_ATTR_NONE, OBJ_TYPE_PROP, attr);
3204b449fa8aSyw161884 
3205b449fa8aSyw161884 	if (ret < SUCCESS)
3206b449fa8aSyw161884 		return (ret);
3207b449fa8aSyw161884 
3208b449fa8aSyw161884 	/* Allocate memory for property and fill the buffer */
3209b449fa8aSyw161884 	attr_new = realloc(attr, sizeof (property_attr_t) + attr->prop_size);
3210b449fa8aSyw161884 	if (attr_new == NULL)
3211b449fa8aSyw161884 		return (ERR_NOMEM);
3212b449fa8aSyw161884 
3213b449fa8aSyw161884 	(void) raid_obj_set_data_ptr(raid_tab, obj_id, attr_new);
3214b449fa8aSyw161884 
3215b449fa8aSyw161884 	ret = raid_lib->get_attr(ctl_attrp->controller_id,
3216b449fa8aSyw161884 	    disk_attr->disk_id, OBJ_ATTR_NONE, OBJ_TYPE_PROP, attr_new);
3217b449fa8aSyw161884 
3218b449fa8aSyw161884 	if (ret < SUCCESS)
3219b449fa8aSyw161884 		return (ret);
3220b449fa8aSyw161884 
3221b449fa8aSyw161884 	(void) raid_obj_set_status(raid_tab, obj_id, OBJ_STATUS_OPENED);
3222b449fa8aSyw161884 
3223b449fa8aSyw161884 	return (ret);
3224b449fa8aSyw161884 }
3225b449fa8aSyw161884 
3226b449fa8aSyw161884 static int
3227711890bcSjc156560 obj_array_create(raid_obj_tab_t *raid_tab, raid_obj_id_t array_obj_id,
3228711890bcSjc156560 	int num_of_comp, raid_obj_id_t *disk_list, char **plugin_err_str)
3229711890bcSjc156560 {
3230711890bcSjc156560 	controller_attr_t *controller_attr;
3231711890bcSjc156560 	array_attr_t *array_attr, array_attr2;
3232711890bcSjc156560 	disk_attr_t *disk_attr;
3233711890bcSjc156560 	arraypart_attr_t *arraypart_attrs;
3234711890bcSjc156560 	raid_obj_id_t obj_id, controller_obj_id = OBJ_NONE;
3235711890bcSjc156560 	raid_lib_t *raid_lib;
3236711890bcSjc156560 	int i, j, ret, fd;
3237711890bcSjc156560 	int disk_cnt = 0, disk_set_num = 0, set_num = 0, layer_cnt = 0;
3238711890bcSjc156560 	uint64_t min_disk_capacity = 0;
3239711890bcSjc156560 
3240711890bcSjc156560 	array_attr = raid_obj_get_data_ptr(raid_tab, array_obj_id);
3241711890bcSjc156560 	if (array_attr == NULL)
3242711890bcSjc156560 		return (ERR_DEVICE_INVALID);
3243711890bcSjc156560 
3244711890bcSjc156560 	/* Check the disk layout expression */
3245711890bcSjc156560 	if (disk_list[0] != OBJ_SEPARATOR_BEGIN ||
3246711890bcSjc156560 	    disk_list[num_of_comp - 1] != OBJ_SEPARATOR_END)
3247711890bcSjc156560 		return (ERR_ARRAY_LAYOUT);
3248711890bcSjc156560 	for (i = 0; i < num_of_comp; ++i) {
3249711890bcSjc156560 		if (disk_list[i] == OBJ_SEPARATOR_BEGIN) {
3250711890bcSjc156560 			if (disk_cnt != 0)
3251711890bcSjc156560 				return (ERR_ARRAY_LAYOUT);
3252711890bcSjc156560 			++layer_cnt;
3253711890bcSjc156560 			continue;
3254711890bcSjc156560 		}
3255711890bcSjc156560 		if (disk_list[i] == OBJ_SEPARATOR_END) {
3256711890bcSjc156560 			if (disk_set_num == 0)
3257711890bcSjc156560 				disk_set_num = disk_cnt;
3258711890bcSjc156560 			else if (disk_set_num != disk_cnt && disk_cnt != 0)
3259711890bcSjc156560 				return (ERR_ARRAY_LAYOUT);
3260711890bcSjc156560 			disk_cnt = 0;
3261711890bcSjc156560 			++set_num;
3262711890bcSjc156560 			--layer_cnt;
3263711890bcSjc156560 			continue;
3264711890bcSjc156560 		}
3265711890bcSjc156560 		switch (array_attr->raid_level) {
3266711890bcSjc156560 		case RAID_LEVEL_0:
3267711890bcSjc156560 		case RAID_LEVEL_1:
3268711890bcSjc156560 		case RAID_LEVEL_1E:
3269711890bcSjc156560 		case RAID_LEVEL_5:
3270711890bcSjc156560 			if (layer_cnt != 1)
3271711890bcSjc156560 				return (ERR_ARRAY_LAYOUT);
3272711890bcSjc156560 			break;
3273711890bcSjc156560 		case RAID_LEVEL_10:
3274711890bcSjc156560 		case RAID_LEVEL_50:
3275711890bcSjc156560 			if (layer_cnt != 2)
3276711890bcSjc156560 				return (ERR_ARRAY_LAYOUT);
3277711890bcSjc156560 			break;
3278711890bcSjc156560 		default:
3279711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3280711890bcSjc156560 		}
3281711890bcSjc156560 		++disk_cnt;
3282711890bcSjc156560 	}
3283711890bcSjc156560 
3284711890bcSjc156560 	if (layer_cnt != 0)
3285711890bcSjc156560 		return (ERR_ARRAY_LAYOUT);
3286711890bcSjc156560 
3287711890bcSjc156560 	switch (array_attr->raid_level) {
3288711890bcSjc156560 	case RAID_LEVEL_0:
3289711890bcSjc156560 		if (disk_set_num < 2 || set_num != 1)
3290711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3291711890bcSjc156560 		break;
3292711890bcSjc156560 	case RAID_LEVEL_1:
3293711890bcSjc156560 		if (disk_set_num != 2 || set_num != 1)
3294711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3295711890bcSjc156560 		break;
3296711890bcSjc156560 	case RAID_LEVEL_1E:
3297711890bcSjc156560 	case RAID_LEVEL_5:
3298711890bcSjc156560 		if (disk_set_num < 3 || set_num != 1)
3299711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3300711890bcSjc156560 		break;
3301711890bcSjc156560 	case RAID_LEVEL_10:
3302711890bcSjc156560 		if (disk_set_num != 2 || set_num < 2)
3303711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3304711890bcSjc156560 		break;
3305711890bcSjc156560 	case RAID_LEVEL_50:
3306711890bcSjc156560 		if (disk_set_num < 3 || set_num < 2)
3307711890bcSjc156560 			return (ERR_ARRAY_LAYOUT);
3308711890bcSjc156560 		break;
3309711890bcSjc156560 	default:
3310711890bcSjc156560 		return (ERR_ARRAY_LEVEL);
3311711890bcSjc156560 	}
3312711890bcSjc156560 
3313711890bcSjc156560 	arraypart_attrs = calloc(num_of_comp, sizeof (arraypart_attr_t));
3314711890bcSjc156560 	if (arraypart_attrs == NULL)
3315711890bcSjc156560 		return (ERR_NOMEM);
3316711890bcSjc156560 
3317711890bcSjc156560 	for (i = 0; i < num_of_comp; ++i) {
3318711890bcSjc156560 		/* Keep seperators */
3319711890bcSjc156560 		if (*(disk_list + i) == OBJ_SEPARATOR_BEGIN) {
3320711890bcSjc156560 			arraypart_attrs[i].disk_id =
3321711890bcSjc156560 			    (uint32_t)OBJ_SEPARATOR_BEGIN;
3322711890bcSjc156560 			continue;
3323711890bcSjc156560 		}
3324711890bcSjc156560 
3325711890bcSjc156560 		if (*(disk_list + i) == OBJ_SEPARATOR_END) {
3326711890bcSjc156560 			arraypart_attrs[i].disk_id =
3327711890bcSjc156560 			    (uint32_t)OBJ_SEPARATOR_END;
3328711890bcSjc156560 			continue;
3329711890bcSjc156560 		}
3330711890bcSjc156560 
3331711890bcSjc156560 		disk_cnt++;
3332711890bcSjc156560 		/* Check if it's a disk */
3333711890bcSjc156560 		if (raid_obj_get_type(raid_tab, *(disk_list + i)) !=
3334711890bcSjc156560 		    OBJ_TYPE_DISK)
3335711890bcSjc156560 			return (ERR_DEVICE_TYPE);
3336711890bcSjc156560 
3337711890bcSjc156560 		/* Check if it's duplicated with other disks */
3338711890bcSjc156560 		for (j = 0; j < i; ++j)
3339711890bcSjc156560 			if (*(disk_list + j) == *(disk_list + i)) {
3340711890bcSjc156560 				free(arraypart_attrs);
3341711890bcSjc156560 				return (ERR_DEVICE_DUP);
3342711890bcSjc156560 			}
3343711890bcSjc156560 
3344711890bcSjc156560 		/* Check disk status */
3345711890bcSjc156560 		ret = obj_get_attr(raid_tab, *(disk_list + i),
3346711890bcSjc156560 		    (void **)(&disk_attr));
3347711890bcSjc156560 		if (ret != SUCCESS)
3348711890bcSjc156560 			return (ret);
3349711890bcSjc156560 
3350711890bcSjc156560 		if (disk_attr->state != DISK_STATE_GOOD) {
3351711890bcSjc156560 			free(arraypart_attrs);
3352711890bcSjc156560 			return (ERR_DISK_STATE);
3353711890bcSjc156560 		}
3354711890bcSjc156560 
3355711890bcSjc156560 		/* All disks must belong to the same controller */
3356711890bcSjc156560 		obj_id = obj_get_controller(raid_tab, *(disk_list + i));
3357711890bcSjc156560 		if (obj_id <= OBJ_NONE)
3358711890bcSjc156560 			return (obj_id);
3359711890bcSjc156560 		if (controller_obj_id == OBJ_NONE) {
3360711890bcSjc156560 			controller_obj_id = obj_id;
3361711890bcSjc156560 			ret = obj_get_attr(raid_tab, controller_obj_id,
3362711890bcSjc156560 			    (void **)(&controller_attr));
3363711890bcSjc156560 		} else if (obj_id != controller_obj_id) {
3364711890bcSjc156560 			free(arraypart_attrs);
3365711890bcSjc156560 			return (ERR_DRIVER_ACROSS);
3366711890bcSjc156560 		}
3367711890bcSjc156560 
3368711890bcSjc156560 		/* Check if the disk contains too many segments */
3369711890bcSjc156560 		obj_id = obj_get_comp(raid_tab, *(disk_list + i),
3370711890bcSjc156560 		    OBJ_TYPE_DISK_SEG);
3371711890bcSjc156560 		j = 0;
3372711890bcSjc156560 		while (obj_id > OBJ_NONE) {
3373711890bcSjc156560 			++j;
3374711890bcSjc156560 			obj_id = obj_get_sibling(raid_tab, obj_id);
3375711890bcSjc156560 		}
3376711890bcSjc156560 		if (j > controller_attr->max_seg_per_disk) {
3377711890bcSjc156560 			free(arraypart_attrs);
3378711890bcSjc156560 			return (ERR_DISK_SEG_AMOUNT);
3379711890bcSjc156560 		}
3380711890bcSjc156560 
3381711890bcSjc156560 		/* Check if controller is a hostraid controller */
3382711890bcSjc156560 		if (controller_attr->capability & RAID_CAP_DISK_TRANS) {
3383711890bcSjc156560 			/*
3384711890bcSjc156560 			 * For hostraid, the first disk should
3385711890bcSjc156560 			 * be with of minimum capacity
3386711890bcSjc156560 			 */
3387711890bcSjc156560 			if (min_disk_capacity == 0) {
3388711890bcSjc156560 				min_disk_capacity = disk_attr->capacity;
3389711890bcSjc156560 
3390711890bcSjc156560 				/* Can not specify capacity for hostraid */
3391711890bcSjc156560 				if (array_attr->capacity != 0) {
3392711890bcSjc156560 					free(arraypart_attrs);
3393700682b8Syw161884 					return (ERR_OP_ILLEGAL);
3394711890bcSjc156560 				}
3395711890bcSjc156560 			} else if (min_disk_capacity > disk_attr->capacity) {
3396711890bcSjc156560 				free(arraypart_attrs);
3397711890bcSjc156560 				return (ERR_DISK_SPACE);
3398711890bcSjc156560 			}
3399711890bcSjc156560 
3400711890bcSjc156560 			/* Disk should not be used for hostraid */
3401711890bcSjc156560 			obj_id = obj_get_comp(raid_tab, *(disk_list + i),
3402711890bcSjc156560 			    OBJ_TYPE_DISK_SEG);
3403711890bcSjc156560 			if (obj_id < OBJ_NONE) {
3404711890bcSjc156560 				free(arraypart_attrs);
3405711890bcSjc156560 				return (obj_id);
3406711890bcSjc156560 			} else if (obj_id > OBJ_NONE) {
3407711890bcSjc156560 				free(arraypart_attrs);
3408711890bcSjc156560 				return (ERR_DISK_NOT_EMPTY);
3409711890bcSjc156560 			}
3410711890bcSjc156560 		}
3411711890bcSjc156560 
3412711890bcSjc156560 		arraypart_attrs[i].disk_id = disk_attr->disk_id;
3413711890bcSjc156560 		arraypart_attrs[i].offset = OBJ_ATTR_NONE;
3414711890bcSjc156560 		arraypart_attrs[i].size = OBJ_ATTR_NONE;
3415711890bcSjc156560 	}
3416711890bcSjc156560 
3417711890bcSjc156560 	/* Check if array amount exceeds limit */
3418711890bcSjc156560 	if (controller_attr->max_array_num <=
3419711890bcSjc156560 	    obj_controller_compnum(raid_tab, controller_obj_id,
3420711890bcSjc156560 	    OBJ_TYPE_ARRAY))
3421711890bcSjc156560 		return (ERR_ARRAY_AMOUNT);
3422711890bcSjc156560 
3423711890bcSjc156560 
3424711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3425711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3426711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3427711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3428711890bcSjc156560 
3429711890bcSjc156560 	/* Check if the controller can support the array RAID level */
3430711890bcSjc156560 	switch (array_attr->raid_level) {
3431711890bcSjc156560 	case	RAID_LEVEL_0:
3432711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID0)) {
3433711890bcSjc156560 			free(arraypart_attrs);
3434711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3435711890bcSjc156560 		}
3436711890bcSjc156560 		break;
3437711890bcSjc156560 	case	RAID_LEVEL_1:
3438711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID1)) {
3439711890bcSjc156560 			free(arraypart_attrs);
3440711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3441711890bcSjc156560 		}
3442711890bcSjc156560 		break;
3443711890bcSjc156560 	case	RAID_LEVEL_1E:
3444711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID1E)) {
3445711890bcSjc156560 			free(arraypart_attrs);
3446711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3447711890bcSjc156560 		}
3448711890bcSjc156560 		break;
3449711890bcSjc156560 	case	RAID_LEVEL_5:
3450711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID5)) {
3451711890bcSjc156560 			free(arraypart_attrs);
3452711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3453711890bcSjc156560 		}
3454711890bcSjc156560 		break;
3455711890bcSjc156560 	case	RAID_LEVEL_10:
3456711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID10)) {
3457711890bcSjc156560 			free(arraypart_attrs);
3458711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3459711890bcSjc156560 		}
3460711890bcSjc156560 		break;
3461711890bcSjc156560 	case	RAID_LEVEL_50:
3462711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_RAID50)) {
3463711890bcSjc156560 			free(arraypart_attrs);
3464711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3465711890bcSjc156560 		}
3466711890bcSjc156560 		break;
3467711890bcSjc156560 	default:
3468711890bcSjc156560 		free(arraypart_attrs);
3469711890bcSjc156560 		return (ERR_ARRAY_LEVEL);
3470711890bcSjc156560 	}
3471711890bcSjc156560 
3472711890bcSjc156560 	/* Check if plug in can calculate the maximum size */
3473711890bcSjc156560 	(void) memcpy(&array_attr2, array_attr, sizeof (array_attr_t));
3474711890bcSjc156560 	array_attr2.capacity = OBJ_ATTR_NONE;
3475711890bcSjc156560 	ret = raid_lib->array_create(controller_attr->controller_id,
3476711890bcSjc156560 	    &array_attr2, num_of_comp, arraypart_attrs, plugin_err_str);
3477711890bcSjc156560 
3478711890bcSjc156560 	/* If plugin/driver will not calculate space */
3479711890bcSjc156560 	if (ret == ERR_OP_NO_IMPL) {
3480711890bcSjc156560 		/* Calculate the maximum capacity */
3481711890bcSjc156560 		array_attr2.capacity = raid_space_noalign(raid_tab,
3482711890bcSjc156560 		    array_attr2.raid_level, num_of_comp, disk_list,
3483711890bcSjc156560 		    arraypart_attrs);
3484711890bcSjc156560 
3485711890bcSjc156560 		/*
3486711890bcSjc156560 		 * If controller is capable to allocate space,
3487711890bcSjc156560 		 * set offset and size attributes to OBJ_ATTR_NONE
3488711890bcSjc156560 		 * and let the controller to determine these value
3489711890bcSjc156560 		 */
3490711890bcSjc156560 		if (controller_attr->capability & RAID_CAP_SMART_ALLOC)
3491711890bcSjc156560 			for (i = 0; i < num_of_comp; ++i) {
3492711890bcSjc156560 				arraypart_attrs[i].offset =
3493711890bcSjc156560 				    OBJ_ATTR_NONE;
3494711890bcSjc156560 				arraypart_attrs[i].size =
3495711890bcSjc156560 				    OBJ_ATTR_NONE;
3496711890bcSjc156560 			}
3497711890bcSjc156560 
3498711890bcSjc156560 		/* There's no enough space for specified capacity */
3499711890bcSjc156560 		if (array_attr->capacity > array_attr2.capacity) {
3500711890bcSjc156560 			free(arraypart_attrs);
3501711890bcSjc156560 			return (ERR_ARRAY_SIZE);
3502711890bcSjc156560 		}
3503711890bcSjc156560 
3504711890bcSjc156560 		/* capacity == 0, allocate maximum space */
3505711890bcSjc156560 		if (array_attr->capacity == 0)
3506711890bcSjc156560 			array_attr->capacity = array_attr2.capacity;
3507711890bcSjc156560 	} else if (ret < SUCCESS) {
3508711890bcSjc156560 		free(arraypart_attrs);
3509711890bcSjc156560 		return (ret);
3510711890bcSjc156560 	} else if (array_attr2.capacity < array_attr->capacity) {
3511711890bcSjc156560 		/* Return the maximum size */
3512711890bcSjc156560 		array_attr->capacity = array_attr2.capacity;
3513711890bcSjc156560 		free(arraypart_attrs);
3514711890bcSjc156560 		return (ERR_ARRAY_SIZE);
3515711890bcSjc156560 	}
3516711890bcSjc156560 
3517711890bcSjc156560 	if (array_attr->capacity < ARRAYPART_MIN_SIZE * disk_cnt) {
3518711890bcSjc156560 		free(arraypart_attrs);
3519711890bcSjc156560 		return (ERR_ARRAY_SIZE);
3520711890bcSjc156560 	}
3521711890bcSjc156560 
3522711890bcSjc156560 
3523711890bcSjc156560 	ret = raid_lib->array_create(controller_attr->controller_id,
3524711890bcSjc156560 	    array_attr, num_of_comp, arraypart_attrs, plugin_err_str);
3525711890bcSjc156560 	free(arraypart_attrs);
3526711890bcSjc156560 
3527711890bcSjc156560 	if (ret != SUCCESS)
3528711890bcSjc156560 		return (ret);
3529711890bcSjc156560 
3530711890bcSjc156560 	/* Add array object into device tree so that we can map the handle */
3531711890bcSjc156560 	(void) raid_obj_add_org(raid_tab, array_obj_id, controller_obj_id);
3532711890bcSjc156560 
3533711890bcSjc156560 	return (ret);
3534711890bcSjc156560 }
3535711890bcSjc156560 
3536711890bcSjc156560 static int
3537711890bcSjc156560 obj_array_delete(raid_obj_tab_t *raid_tab, raid_obj_id_t array_obj_id,
3538711890bcSjc156560 	char **plugin_err_str)
3539711890bcSjc156560 {
3540711890bcSjc156560 	raid_obj_id_t controller_obj_id;
3541711890bcSjc156560 	controller_attr_t *controller_attr;
3542711890bcSjc156560 	array_attr_t *array_attr;
3543711890bcSjc156560 	raid_lib_t *raid_lib;
354472e1c055Sjc156560 	int ret, fd;
3545711890bcSjc156560 	uint32_t *disk_ids = NULL;
3546711890bcSjc156560 
3547711890bcSjc156560 	controller_obj_id = obj_get_controller(raid_tab, array_obj_id);
3548711890bcSjc156560 	if (controller_obj_id <= OBJ_NONE)
3549711890bcSjc156560 		return (controller_obj_id);
3550711890bcSjc156560 
3551711890bcSjc156560 	ret = obj_get_attr(raid_tab, controller_obj_id,
3552711890bcSjc156560 	    (void **)(&controller_attr));
3553711890bcSjc156560 	if (ret < SUCCESS) {
3554711890bcSjc156560 		return (ret);
3555711890bcSjc156560 	}
3556711890bcSjc156560 	ret = obj_get_attr(raid_tab, array_obj_id, (void **)(&array_attr));
3557711890bcSjc156560 	if (ret < SUCCESS)
3558711890bcSjc156560 		return (ret);
3559711890bcSjc156560 
3560711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3561711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3562711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3563711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3564711890bcSjc156560 
3565711890bcSjc156560 	ret = raid_lib->array_delete(controller_attr->controller_id,
3566711890bcSjc156560 	    array_attr->array_id, plugin_err_str);
3567711890bcSjc156560 	if (ret < SUCCESS) {
3568711890bcSjc156560 		if (disk_ids)
3569711890bcSjc156560 			free(disk_ids);
3570711890bcSjc156560 		return (ret);
3571711890bcSjc156560 	}
3572711890bcSjc156560 
3573711890bcSjc156560 	if (disk_ids)
3574711890bcSjc156560 		free(disk_ids);
3575711890bcSjc156560 	return (ret);
3576711890bcSjc156560 }
3577711890bcSjc156560 
3578711890bcSjc156560 static int
3579*5c9d25d2SYu-Bo Ryan Wang obj_hsp_bind(raid_obj_tab_t *raid_tab, raid_obj_id_t *obj_ids,
3580711890bcSjc156560 	char **plugin_err_str)
3581711890bcSjc156560 {
3582711890bcSjc156560 	raid_obj_id_t obj_id, controller_obj_id = OBJ_NONE;
3583711890bcSjc156560 	raid_obj_id_t array_obj_id, disk_obj_id;
3584711890bcSjc156560 	hsp_relation_t *hsp_relation;
3585711890bcSjc156560 	controller_attr_t *controller_attr;
3586711890bcSjc156560 	array_attr_t *array_attr;
3587711890bcSjc156560 	arraypart_attr_t *arraypart_attr;
3588711890bcSjc156560 	disk_attr_t *disk_attr;
3589711890bcSjc156560 	diskseg_attr_t *diskseg_attr;
3590711890bcSjc156560 	hsp_attr_t *hsp_attr;
3591711890bcSjc156560 	raid_lib_t *raid_lib;
3592*5c9d25d2SYu-Bo Ryan Wang 	int ret, fd;
3593711890bcSjc156560 
3594*5c9d25d2SYu-Bo Ryan Wang 	hsp_relation = malloc(sizeof (hsp_relation_t));
3595711890bcSjc156560 	if (hsp_relation == NULL)
3596711890bcSjc156560 		return (ERR_NOMEM);
3597711890bcSjc156560 
3598*5c9d25d2SYu-Bo Ryan Wang 	array_obj_id = *(obj_ids);
3599*5c9d25d2SYu-Bo Ryan Wang 	disk_obj_id = *(obj_ids + 1);
3600711890bcSjc156560 
3601711890bcSjc156560 	if (raid_obj_get_type(raid_tab, disk_obj_id) != OBJ_TYPE_DISK ||
3602711890bcSjc156560 	    (array_obj_id != OBJ_ATTR_NONE &&
3603711890bcSjc156560 	    raid_obj_get_type(raid_tab, array_obj_id) !=
3604711890bcSjc156560 	    OBJ_TYPE_ARRAY)) {
3605711890bcSjc156560 		free(hsp_relation);
3606711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3607711890bcSjc156560 	}
3608711890bcSjc156560 
3609711890bcSjc156560 	/* Get controller attributes */
3610711890bcSjc156560 	if (controller_obj_id == OBJ_NONE)
3611711890bcSjc156560 		controller_obj_id = obj_get_controller(raid_tab,
3612711890bcSjc156560 		    disk_obj_id);
3613711890bcSjc156560 	else if (controller_obj_id != obj_get_controller(raid_tab,
3614711890bcSjc156560 	    disk_obj_id)) {
3615711890bcSjc156560 		free(hsp_relation);
3616711890bcSjc156560 		return (ERR_DRIVER_ACROSS);
3617711890bcSjc156560 	}
3618711890bcSjc156560 
3619711890bcSjc156560 	ret = obj_get_attr(raid_tab, controller_obj_id,
3620711890bcSjc156560 	    (void **)(&controller_attr));
3621711890bcSjc156560 
3622711890bcSjc156560 	/* Get disk attributes */
3623711890bcSjc156560 	ret = obj_get_attr(raid_tab,  disk_obj_id,
3624711890bcSjc156560 	    (void **)(&disk_attr));
3625711890bcSjc156560 	if (disk_attr->state == DISK_STATE_FAILED) {
3626711890bcSjc156560 		free(hsp_relation);
3627711890bcSjc156560 		return (ERR_DISK_STATE);
3628711890bcSjc156560 	}
3629711890bcSjc156560 
3630711890bcSjc156560 	/* If it's not a hsp disk, check if there's occupied space */
3631711890bcSjc156560 	if (obj_get_comp(raid_tab, disk_obj_id, OBJ_TYPE_HSP) ==
3632711890bcSjc156560 	    OBJ_NONE) {
3633711890bcSjc156560 		obj_id = obj_get_comp(raid_tab, disk_obj_id,
3634711890bcSjc156560 		    OBJ_TYPE_DISK_SEG);
3635711890bcSjc156560 		while (obj_id != OBJ_NONE) {
3636711890bcSjc156560 			ret = obj_get_attr(raid_tab, obj_id,
3637711890bcSjc156560 			    (void **)(&diskseg_attr));
3638711890bcSjc156560 			if (!(diskseg_attr->state &
3639711890bcSjc156560 			    DISKSEG_STATE_RESERVED)) {
3640711890bcSjc156560 				free(hsp_relation);
3641711890bcSjc156560 				return (ERR_DISK_NOT_EMPTY);
3642711890bcSjc156560 			}
3643711890bcSjc156560 			obj_id = obj_get_sibling(raid_tab, obj_id);
3644711890bcSjc156560 		}
3645711890bcSjc156560 	}
3646711890bcSjc156560 
3647711890bcSjc156560 	if (array_obj_id != OBJ_ATTR_NONE) {
3648711890bcSjc156560 		/* If local hsp is supported */
3649711890bcSjc156560 		if (!(controller_attr->capability & RAID_CAP_L_HSP)) {
3650711890bcSjc156560 			free(hsp_relation);
3651711890bcSjc156560 			return (ERR_OP_ILLEGAL);
3652711890bcSjc156560 		}
3653711890bcSjc156560 
3654711890bcSjc156560 		if (raid_obj_get_type(raid_tab, array_obj_id) !=
3655711890bcSjc156560 		    OBJ_TYPE_ARRAY) {
3656711890bcSjc156560 			free(hsp_relation);
3657711890bcSjc156560 			return (ERR_DEVICE_TYPE);
3658711890bcSjc156560 		}
3659711890bcSjc156560 
3660711890bcSjc156560 		/* Get array attributes */
3661711890bcSjc156560 		ret = obj_get_attr(raid_tab, array_obj_id,
3662711890bcSjc156560 		    (void **)(&array_attr));
3663711890bcSjc156560 		/* RAID 0 array can not use hsp */
3664711890bcSjc156560 		if (array_attr->raid_level == RAID_LEVEL_0) {
3665711890bcSjc156560 			free(hsp_relation);
3666711890bcSjc156560 			return (ERR_ARRAY_LEVEL);
3667711890bcSjc156560 		}
3668711890bcSjc156560 
3669711890bcSjc156560 		/* If It's belong to another controller */
3670711890bcSjc156560 		if (controller_obj_id != obj_get_controller(raid_tab,
3671711890bcSjc156560 		    array_obj_id)) {
3672711890bcSjc156560 			free(hsp_relation);
3673711890bcSjc156560 			return (ERR_DRIVER_ACROSS);
3674711890bcSjc156560 		}
3675711890bcSjc156560 
3676711890bcSjc156560 		/* Get an array part attributes */
3677711890bcSjc156560 		if ((array_attr->raid_level == RAID_LEVEL_10) ||
3678711890bcSjc156560 		    (array_attr->raid_level == RAID_LEVEL_50))
3679711890bcSjc156560 			obj_id = obj_get_comp(raid_tab, array_obj_id,
3680711890bcSjc156560 			    OBJ_TYPE_ARRAY);
3681711890bcSjc156560 		else
3682711890bcSjc156560 			obj_id = array_obj_id;
3683711890bcSjc156560 		obj_id = obj_get_comp(raid_tab, obj_id,
3684711890bcSjc156560 		    OBJ_TYPE_ARRAY_PART);
3685711890bcSjc156560 		ret = obj_get_attr(raid_tab, obj_id,
3686711890bcSjc156560 		    (void **)(&arraypart_attr));
3687711890bcSjc156560 
3688711890bcSjc156560 		/* Check if disk space is enough for array */
3689711890bcSjc156560 		if (arraypart_attr->size > disk_attr->capacity) {
3690711890bcSjc156560 			free(hsp_relation);
3691711890bcSjc156560 			return (ERR_DISK_SPACE);
3692711890bcSjc156560 		}
3693711890bcSjc156560 		if (controller_attr->capability & RAID_CAP_ARRAY_ALIGN)
3694711890bcSjc156560 			if ((arraypart_attr->size +
3695711890bcSjc156560 			    arraypart_attr->offset) >
3696711890bcSjc156560 			    disk_attr->capacity) {
3697711890bcSjc156560 			free(hsp_relation);
3698711890bcSjc156560 			return (ERR_DISK_SPACE);
3699711890bcSjc156560 			}
3700711890bcSjc156560 	} else if (!(controller_attr->capability & RAID_CAP_G_HSP)) {
3701711890bcSjc156560 		/* if global hsp is supported */
3702711890bcSjc156560 		free(hsp_relation);
3703711890bcSjc156560 		return (ERR_OP_ILLEGAL);
3704711890bcSjc156560 	}
3705711890bcSjc156560 
3706711890bcSjc156560 	/*
3707711890bcSjc156560 	 * If the array is already associated with the
3708711890bcSjc156560 	 * local hsp, or it's a global hsp, ignore it
3709711890bcSjc156560 	 */
3710711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, disk_obj_id, OBJ_TYPE_HSP);
3711711890bcSjc156560 	if (obj_id > OBJ_NONE) {
3712711890bcSjc156560 		if (obj_get_attr(raid_tab, obj_id,
3713711890bcSjc156560 		    (void **)&hsp_attr) >= SUCCESS) {
3714711890bcSjc156560 			if (((hsp_attr->type == HSP_TYPE_GLOBAL) &&
3715711890bcSjc156560 			    (array_obj_id != OBJ_ATTR_NONE)) ||
3716711890bcSjc156560 			    ((hsp_attr->type == HSP_TYPE_LOCAL) &&
3717711890bcSjc156560 			    (array_obj_id == OBJ_ATTR_NONE))) {
3718711890bcSjc156560 				free(hsp_relation);
3719711890bcSjc156560 				return (ERR_OP_ILLEGAL);
3720711890bcSjc156560 			}
3721711890bcSjc156560 		}
3722711890bcSjc156560 	}
3723711890bcSjc156560 
3724711890bcSjc156560 	if (array_obj_id != OBJ_ATTR_NONE)
3725*5c9d25d2SYu-Bo Ryan Wang 		hsp_relation->array_id = array_attr->array_id;
3726711890bcSjc156560 	else
3727*5c9d25d2SYu-Bo Ryan Wang 		hsp_relation->array_id = (uint32_t)OBJ_ATTR_NONE;
3728*5c9d25d2SYu-Bo Ryan Wang 	hsp_relation->disk_id = disk_attr->disk_id;
3729711890bcSjc156560 
3730711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3731711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3732711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3733711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3734711890bcSjc156560 
3735711890bcSjc156560 	if (raid_lib->hsp_bind == NULL) {
3736711890bcSjc156560 		free(hsp_relation);
3737711890bcSjc156560 		return (ERR_OP_NO_IMPL);
3738711890bcSjc156560 	}
3739711890bcSjc156560 
3740711890bcSjc156560 	ret = raid_lib->hsp_bind(controller_attr->controller_id,
3741*5c9d25d2SYu-Bo Ryan Wang 	    hsp_relation, plugin_err_str);
3742711890bcSjc156560 
3743711890bcSjc156560 	free(hsp_relation);
3744711890bcSjc156560 	return (ret);
3745711890bcSjc156560 }
3746711890bcSjc156560 
3747711890bcSjc156560 static int
3748*5c9d25d2SYu-Bo Ryan Wang obj_hsp_unbind(raid_obj_tab_t *raid_tab, raid_obj_id_t *obj_ids,
3749711890bcSjc156560 	char **plugin_err_str)
3750711890bcSjc156560 {
3751711890bcSjc156560 	raid_obj_id_t obj_id, controller_obj_id = OBJ_NONE;
3752711890bcSjc156560 	raid_obj_id_t array_obj_id, disk_obj_id;
3753711890bcSjc156560 	hsp_relation_t *hsp_relation;
3754711890bcSjc156560 	controller_attr_t *controller_attr;
3755711890bcSjc156560 	array_attr_t *array_attr;
3756711890bcSjc156560 	disk_attr_t *disk_attr;
3757711890bcSjc156560 	hsp_attr_t *hsp_attr;
3758711890bcSjc156560 	raid_lib_t *raid_lib;
3759*5c9d25d2SYu-Bo Ryan Wang 	int ret, fd;
3760711890bcSjc156560 
3761*5c9d25d2SYu-Bo Ryan Wang 	hsp_relation = malloc(sizeof (hsp_relation_t));
3762711890bcSjc156560 	if (hsp_relation == NULL)
3763711890bcSjc156560 		return (ERR_NOMEM);
3764711890bcSjc156560 
3765*5c9d25d2SYu-Bo Ryan Wang 	array_obj_id = *(obj_ids);
3766*5c9d25d2SYu-Bo Ryan Wang 	disk_obj_id = *(obj_ids + 1);
3767711890bcSjc156560 
3768711890bcSjc156560 	if (raid_obj_get_type(raid_tab, disk_obj_id) != OBJ_TYPE_DISK) {
3769711890bcSjc156560 		free(hsp_relation);
3770711890bcSjc156560 		return (ERR_DEVICE_TYPE);
3771711890bcSjc156560 	}
3772711890bcSjc156560 
3773711890bcSjc156560 	/* Get controller attributes */
3774711890bcSjc156560 	if (controller_obj_id == OBJ_NONE)
3775711890bcSjc156560 		controller_obj_id = obj_get_controller(raid_tab,
3776711890bcSjc156560 		    disk_obj_id);
3777711890bcSjc156560 	else if (controller_obj_id != obj_get_controller(raid_tab,
3778711890bcSjc156560 	    disk_obj_id)) {
3779711890bcSjc156560 		free(hsp_relation);
3780711890bcSjc156560 		return (ERR_DRIVER_ACROSS);
3781711890bcSjc156560 	}
3782711890bcSjc156560 
3783711890bcSjc156560 	ret = obj_get_attr(raid_tab, controller_obj_id,
3784711890bcSjc156560 	    (void **)(&controller_attr));
3785711890bcSjc156560 
3786711890bcSjc156560 	/* Get disk attributes */
3787711890bcSjc156560 	ret = obj_get_attr(raid_tab,  disk_obj_id,
3788711890bcSjc156560 	    (void **)(&disk_attr));
3789711890bcSjc156560 	if (disk_attr->state == DISK_STATE_FAILED) {
3790711890bcSjc156560 		free(hsp_relation);
3791711890bcSjc156560 		return (ERR_DISK_STATE);
3792711890bcSjc156560 	}
3793711890bcSjc156560 
3794711890bcSjc156560 	/* If it's not a hsp disk */
3795711890bcSjc156560 	obj_id = obj_get_comp(raid_tab, disk_obj_id, OBJ_TYPE_HSP);
3796711890bcSjc156560 	if (obj_id == OBJ_NONE) {
3797711890bcSjc156560 		free(hsp_relation);
3798711890bcSjc156560 		return (ERR_DISK_STATE);
3799711890bcSjc156560 	}
3800711890bcSjc156560 	ret = obj_get_attr(raid_tab, obj_id, (void **)(&hsp_attr));
3801711890bcSjc156560 
3802711890bcSjc156560 	if (array_obj_id != OBJ_ATTR_NONE) {
3803711890bcSjc156560 		if (raid_obj_get_type(raid_tab, array_obj_id) !=
3804711890bcSjc156560 		    OBJ_TYPE_ARRAY) {
3805711890bcSjc156560 			free(hsp_relation);
3806711890bcSjc156560 			return (ERR_DEVICE_TYPE);
3807711890bcSjc156560 		}
3808711890bcSjc156560 
3809711890bcSjc156560 		/* Get array attributes */
3810711890bcSjc156560 		ret = obj_get_attr(raid_tab, array_obj_id,
3811711890bcSjc156560 		    (void **)(&array_attr));
3812711890bcSjc156560 
3813711890bcSjc156560 		/* If It's belong to another controller */
3814711890bcSjc156560 		if (controller_obj_id != obj_get_controller(raid_tab,
3815711890bcSjc156560 		    array_obj_id)) {
3816711890bcSjc156560 			free(hsp_relation);
3817711890bcSjc156560 			return (ERR_DRIVER_ACROSS);
3818711890bcSjc156560 		}
3819711890bcSjc156560 
3820711890bcSjc156560 		/* If want to remove an array from a global hsp */
3821711890bcSjc156560 		if (hsp_attr->type == HSP_TYPE_GLOBAL) {
3822711890bcSjc156560 			free(hsp_relation);
3823711890bcSjc156560 			return (ERR_OP_ILLEGAL);
3824711890bcSjc156560 		}
3825711890bcSjc156560 
3826711890bcSjc156560 		do {
3827711890bcSjc156560 			(void) obj_get_attr(raid_tab, obj_id,
3828711890bcSjc156560 			    (void **)(&hsp_attr));
3829711890bcSjc156560 
3830711890bcSjc156560 			if (hsp_attr->associated_id ==
3831711890bcSjc156560 			    array_attr->array_id ||
3832711890bcSjc156560 			    hsp_attr->type == HSP_TYPE_GLOBAL)
3833711890bcSjc156560 				break;
3834711890bcSjc156560 
3835711890bcSjc156560 			obj_id = obj_get_sibling(raid_tab, obj_id);
3836711890bcSjc156560 		} while (obj_id > OBJ_NONE);
3837711890bcSjc156560 	} else if (hsp_attr->type != HSP_TYPE_GLOBAL) {
3838711890bcSjc156560 		/* if global hsp is supported */
3839711890bcSjc156560 		free(hsp_relation);
3840711890bcSjc156560 		return (ERR_OP_ILLEGAL);
3841711890bcSjc156560 	}
3842711890bcSjc156560 
3843711890bcSjc156560 	/*
3844711890bcSjc156560 	 * If array is associated with a local hsp, or remove a
3845711890bcSjc156560 	 * global hsp disk
3846711890bcSjc156560 	 */
3847711890bcSjc156560 	if ((obj_id && (array_obj_id != OBJ_ATTR_NONE)) ||
3848711890bcSjc156560 	    (array_obj_id == OBJ_ATTR_NONE)) {
3849711890bcSjc156560 		if (array_obj_id != OBJ_ATTR_NONE)
3850*5c9d25d2SYu-Bo Ryan Wang 			hsp_relation->array_id = array_attr->array_id;
3851711890bcSjc156560 		else
3852*5c9d25d2SYu-Bo Ryan Wang 			hsp_relation->array_id =
3853711890bcSjc156560 			    (uint32_t)OBJ_ATTR_NONE;
3854*5c9d25d2SYu-Bo Ryan Wang 		hsp_relation->disk_id = disk_attr->disk_id;
3855711890bcSjc156560 	} else {
3856711890bcSjc156560 		free(hsp_relation);
3857711890bcSjc156560 		return (ERR_OP_ILLEGAL);
3858711890bcSjc156560 	}
3859711890bcSjc156560 
3860711890bcSjc156560 	raid_lib = raid_obj_get_lib(raid_tab, controller_obj_id);
3861711890bcSjc156560 	fd = raid_obj_get_fd(raid_tab, controller_obj_id);
3862711890bcSjc156560 	if ((raid_lib == NULL) || (fd == 0))
3863711890bcSjc156560 		return (ERR_DRIVER_CLOSED);
3864711890bcSjc156560 
3865711890bcSjc156560 	if (raid_lib->hsp_unbind == NULL) {
3866711890bcSjc156560 		free(hsp_relation);
3867711890bcSjc156560 		return (ERR_OP_NO_IMPL);
3868711890bcSjc156560 	}
3869711890bcSjc156560 
3870711890bcSjc156560 	ret = raid_lib->hsp_unbind(controller_attr->controller_id,
3871*5c9d25d2SYu-Bo Ryan Wang 	    hsp_relation, plugin_err_str);
3872711890bcSjc156560 
3873711890bcSjc156560 	free(hsp_relation);
3874711890bcSjc156560 	return (ret);
3875711890bcSjc156560 }
3876711890bcSjc156560 
3877711890bcSjc156560 /*
3878711890bcSjc156560  * Object maintennance routines
3879711890bcSjc156560  */
3880711890bcSjc156560 static int
3881711890bcSjc156560 raid_obj_create_system_obj(raid_obj_tab_t *raid_tab)
3882711890bcSjc156560 {
3883711890bcSjc156560 	raid_obj_t *raid_obj;
3884711890bcSjc156560 	int ret;
3885711890bcSjc156560 
3886711890bcSjc156560 	raid_obj = calloc(1, sizeof (raid_obj_t));
3887711890bcSjc156560 	if (raid_obj == NULL)
3888711890bcSjc156560 		return (ERR_NOMEM);
3889711890bcSjc156560 
3890711890bcSjc156560 	raid_obj->obj_id = OBJ_SYSTEM;
3891711890bcSjc156560 	raid_obj->obj_type_id = OBJ_TYPE_SYSTEM;
3892711890bcSjc156560 	raid_obj->data = NULL;
3893711890bcSjc156560 
3894711890bcSjc156560 	ret = raid_obj_tab_insert(raid_tab, raid_obj->obj_id, raid_obj);
3895711890bcSjc156560 	if (ret == ERR_DEVICE_DUP) {
3896711890bcSjc156560 		free(raid_obj);
3897711890bcSjc156560 		return (ERR_DEVICE_UNCLEAN);
3898711890bcSjc156560 	}
3899711890bcSjc156560 
3900711890bcSjc156560 	return (SUCCESS);
3901711890bcSjc156560 }
3902711890bcSjc156560 
3903711890bcSjc156560 static raid_obj_id_t
3904711890bcSjc156560 raid_obj_id_new(raid_obj_tab_t *raid_tab)
3905711890bcSjc156560 {
3906711890bcSjc156560 	++ raid_tab->obj_id_cnt;
3907711890bcSjc156560 	if (raid_tab->obj_id_cnt <= 0)
3908711890bcSjc156560 		return (ERR_DEVICE_OVERFLOW);
3909711890bcSjc156560 
3910711890bcSjc156560 	return (raid_tab->obj_id_cnt);
3911711890bcSjc156560 }
3912711890bcSjc156560 
3913711890bcSjc156560 static void *
3914711890bcSjc156560 raid_obj_attr_new(raid_obj_type_id_t obj_type)
3915711890bcSjc156560 {
3916711890bcSjc156560 	void *obj_attr = NULL;
3917711890bcSjc156560 
3918711890bcSjc156560 	switch (obj_type) {
3919711890bcSjc156560 	case	OBJ_TYPE_CONTROLLER:
3920711890bcSjc156560 		obj_attr = calloc(1, sizeof (controller_attr_t));
3921711890bcSjc156560 		break;
3922711890bcSjc156560 	case	OBJ_TYPE_ARRAY:
3923711890bcSjc156560 		obj_attr = calloc(1, sizeof (array_attr_t));
3924711890bcSjc156560 		break;
3925711890bcSjc156560 	case	OBJ_TYPE_DISK:
3926711890bcSjc156560 		obj_attr = calloc(1, sizeof (disk_attr_t));
3927711890bcSjc156560 		break;
3928711890bcSjc156560 	case	OBJ_TYPE_HSP:
3929711890bcSjc156560 		obj_attr = calloc(1, sizeof (hsp_attr_t));
3930711890bcSjc156560 		break;
3931711890bcSjc156560 	case	OBJ_TYPE_ARRAY_PART:
3932711890bcSjc156560 		obj_attr = calloc(1, sizeof (arraypart_attr_t));
3933711890bcSjc156560 		break;
3934711890bcSjc156560 	case	OBJ_TYPE_DISK_SEG:
3935711890bcSjc156560 		obj_attr = calloc(1, sizeof (diskseg_attr_t));
3936711890bcSjc156560 		break;
3937711890bcSjc156560 	case	OBJ_TYPE_TASK:
3938711890bcSjc156560 		obj_attr = calloc(1, sizeof (task_attr_t));
3939711890bcSjc156560 		break;
3940b449fa8aSyw161884 	case	OBJ_TYPE_PROP:
3941b449fa8aSyw161884 		obj_attr = calloc(1, sizeof (property_attr_t));
3942b449fa8aSyw161884 		break;
3943711890bcSjc156560 	default:
3944711890bcSjc156560 		break;
3945711890bcSjc156560 	}
3946711890bcSjc156560 
3947711890bcSjc156560 	return (obj_attr);
3948711890bcSjc156560 }
3949711890bcSjc156560 
3950711890bcSjc156560 static raid_obj_id_t
3951711890bcSjc156560 raid_obj_create(raid_obj_tab_t *raid_tab, raid_obj_type_id_t obj_type)
3952711890bcSjc156560 {
3953711890bcSjc156560 	raid_obj_t *raid_obj;
3954711890bcSjc156560 	int ret;
3955711890bcSjc156560 	void *data_ptr;
3956711890bcSjc156560 
3957711890bcSjc156560 	raid_obj = calloc(1, sizeof (raid_obj_t));
3958711890bcSjc156560 	if (raid_obj == NULL)
3959711890bcSjc156560 		return (ERR_NOMEM);
3960711890bcSjc156560 
3961711890bcSjc156560 	raid_obj->obj_id = raid_obj_id_new(raid_tab);
3962711890bcSjc156560 	if (raid_obj->obj_id < OBJ_NONE)
3963711890bcSjc156560 		return (ERR_DEVICE_OVERFLOW);
3964711890bcSjc156560 
3965711890bcSjc156560 	ret = raid_obj_tab_insert(raid_tab, raid_obj->obj_id, raid_obj);
3966711890bcSjc156560 	if (ret == ERR_DEVICE_DUP) {
3967711890bcSjc156560 		free(raid_obj);
3968711890bcSjc156560 		return (ERR_DEVICE_DUP);
3969711890bcSjc156560 	}
3970711890bcSjc156560 
3971711890bcSjc156560 	data_ptr = raid_obj_attr_new(obj_type);
3972711890bcSjc156560 	if (data_ptr == NULL) {
3973711890bcSjc156560 		(void) raid_obj_delete(raid_tab, raid_obj->obj_id);
3974711890bcSjc156560 		return (ERR_NOMEM);
3975711890bcSjc156560 	}
3976711890bcSjc156560 
3977711890bcSjc156560 	(void) raid_obj_set_data_ptr(raid_tab, raid_obj->obj_id, data_ptr);
3978711890bcSjc156560 
3979711890bcSjc156560 	(void) raid_obj_set_type(raid_tab, raid_obj->obj_id, obj_type);
3980711890bcSjc156560 	return (raid_obj->obj_id);
3981711890bcSjc156560 }
3982711890bcSjc156560 
3983711890bcSjc156560 static int
3984711890bcSjc156560 raid_obj_delete(raid_obj_tab_t *raid_tab, raid_obj_id_t raid_obj_id)
3985711890bcSjc156560 {
3986711890bcSjc156560 	raid_obj_t *obj;
3987711890bcSjc156560 
3988711890bcSjc156560 	obj = raid_obj_tab_remove(raid_tab, raid_obj_id);
3989711890bcSjc156560 	if (obj != NULL) {
3990711890bcSjc156560 		free(obj->data);
3991711890bcSjc156560 		free(obj);
3992711890bcSjc156560 		return (SUCCESS);
3993711890bcSjc156560 	}
3994711890bcSjc156560 
3995711890bcSjc156560 	return (ERR_DEVICE_NOENT);
3996711890bcSjc156560 }
3997711890bcSjc156560 
3998711890bcSjc156560 static int
3999711890bcSjc156560 raid_obj_add_org(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4000711890bcSjc156560 	raid_obj_id_t container_id)
4001711890bcSjc156560 {
4002711890bcSjc156560 	raid_obj_id_t tmp, tmp1;
4003711890bcSjc156560 
4004711890bcSjc156560 	tmp = raid_obj_get_comp(raid_tab, container_id);
4005711890bcSjc156560 	if (tmp < OBJ_NONE)
4006711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4007711890bcSjc156560 
4008711890bcSjc156560 	if (tmp == OBJ_NONE) {
4009711890bcSjc156560 		(void) raid_obj_set_container(raid_tab, obj_id, container_id);
4010711890bcSjc156560 		(void) raid_obj_set_comp(raid_tab, container_id, obj_id);
4011711890bcSjc156560 		return (SUCCESS);
4012711890bcSjc156560 	}
4013711890bcSjc156560 
4014711890bcSjc156560 	while ((tmp1 = raid_obj_get_sibling(raid_tab, tmp)) != OBJ_NONE)
4015711890bcSjc156560 		tmp = tmp1;
4016711890bcSjc156560 
4017711890bcSjc156560 	if (raid_obj_set_sibling(raid_tab, tmp, obj_id) < SUCCESS)
4018711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4019711890bcSjc156560 	(void) raid_obj_set_container(raid_tab, obj_id, container_id);
4020711890bcSjc156560 
4021711890bcSjc156560 	return (SUCCESS);
4022711890bcSjc156560 }
4023711890bcSjc156560 
4024711890bcSjc156560 static raid_obj_type_id_t
4025711890bcSjc156560 raid_obj_get_type(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4026711890bcSjc156560 {
4027711890bcSjc156560 	raid_obj_t *obj;
4028711890bcSjc156560 
4029711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4030711890bcSjc156560 	if (obj == NULL)
4031711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4032711890bcSjc156560 
4033711890bcSjc156560 	if ((obj->obj_type_id < OBJ_TYPE_SYSTEM) ||
4034711890bcSjc156560 	    (obj->obj_type_id >= OBJ_TYPE_ALL))
4035711890bcSjc156560 		return (ERR_DEVICE_INVALID);
4036711890bcSjc156560 
4037711890bcSjc156560 	return (obj->obj_type_id);
4038711890bcSjc156560 }
4039711890bcSjc156560 
4040711890bcSjc156560 static int
4041711890bcSjc156560 raid_obj_set_type(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4042711890bcSjc156560 	raid_obj_type_id_t type)
4043711890bcSjc156560 {
4044711890bcSjc156560 	raid_obj_t *obj;
4045711890bcSjc156560 
4046711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4047711890bcSjc156560 	if (obj == NULL)
4048711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4049711890bcSjc156560 
4050711890bcSjc156560 	if ((type < OBJ_TYPE_SYSTEM) || (type >= OBJ_TYPE_ALL))
4051711890bcSjc156560 		return (ERR_DEVICE_TYPE);
4052711890bcSjc156560 
4053711890bcSjc156560 	obj->obj_type_id = type;
4054711890bcSjc156560 	return (SUCCESS);
4055711890bcSjc156560 }
4056711890bcSjc156560 
4057711890bcSjc156560 static raid_obj_status_t
4058711890bcSjc156560 raid_obj_get_status(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4059711890bcSjc156560 {
4060711890bcSjc156560 	raid_obj_t *obj;
4061711890bcSjc156560 
4062711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4063711890bcSjc156560 	if (obj == NULL)
4064711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4065711890bcSjc156560 
4066711890bcSjc156560 	return (obj->status);
4067711890bcSjc156560 }
4068711890bcSjc156560 
4069711890bcSjc156560 static int
4070711890bcSjc156560 raid_obj_set_status(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4071711890bcSjc156560 	raid_obj_status_t status)
4072711890bcSjc156560 {
4073711890bcSjc156560 	raid_obj_t *obj;
4074711890bcSjc156560 
4075711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4076711890bcSjc156560 	if (obj == NULL)
4077711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4078711890bcSjc156560 
4079711890bcSjc156560 	obj->status = obj->status | status;
4080711890bcSjc156560 
4081711890bcSjc156560 	return (SUCCESS);
4082711890bcSjc156560 }
4083711890bcSjc156560 
4084711890bcSjc156560 static int
4085711890bcSjc156560 raid_obj_clear_status(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4086711890bcSjc156560 	raid_obj_status_t status)
4087711890bcSjc156560 {
4088711890bcSjc156560 	raid_obj_t *obj;
4089711890bcSjc156560 
4090711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4091711890bcSjc156560 	if (obj == NULL)
4092711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4093711890bcSjc156560 
4094711890bcSjc156560 	obj->status = obj->status & ~status;
4095711890bcSjc156560 
4096711890bcSjc156560 	return (SUCCESS);
4097711890bcSjc156560 }
4098711890bcSjc156560 
4099711890bcSjc156560 static raid_obj_id_t
4100711890bcSjc156560 raid_obj_get_container(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4101711890bcSjc156560 {
4102711890bcSjc156560 	raid_obj_t *obj;
4103711890bcSjc156560 
4104711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4105711890bcSjc156560 	if (obj == NULL)
4106711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4107711890bcSjc156560 
4108711890bcSjc156560 	return (obj->container);
4109711890bcSjc156560 }
4110711890bcSjc156560 
4111711890bcSjc156560 static int
4112711890bcSjc156560 raid_obj_set_container(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4113711890bcSjc156560 	raid_obj_id_t container_id)
4114711890bcSjc156560 {
4115711890bcSjc156560 	raid_obj_t *obj;
4116711890bcSjc156560 
4117711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4118711890bcSjc156560 	if (obj == NULL)
4119711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4120711890bcSjc156560 
4121711890bcSjc156560 	obj->container = container_id;
4122711890bcSjc156560 	return (SUCCESS);
4123711890bcSjc156560 }
4124711890bcSjc156560 
4125711890bcSjc156560 static raid_obj_id_t
4126711890bcSjc156560 raid_obj_get_comp(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4127711890bcSjc156560 {
4128711890bcSjc156560 	raid_obj_t *obj;
4129711890bcSjc156560 
4130711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4131711890bcSjc156560 	if (obj == NULL)
4132711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4133711890bcSjc156560 
4134711890bcSjc156560 	return (obj->component);
4135711890bcSjc156560 }
4136711890bcSjc156560 
4137711890bcSjc156560 static int
4138711890bcSjc156560 raid_obj_set_comp(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4139711890bcSjc156560 	raid_obj_id_t comp)
4140711890bcSjc156560 {
4141711890bcSjc156560 	raid_obj_t *obj;
4142711890bcSjc156560 
4143711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4144711890bcSjc156560 	if (obj == NULL)
4145711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4146711890bcSjc156560 
4147711890bcSjc156560 	obj->component = comp;
4148711890bcSjc156560 	return (SUCCESS);
4149711890bcSjc156560 }
4150711890bcSjc156560 
4151711890bcSjc156560 static raid_obj_id_t
4152711890bcSjc156560 raid_obj_get_sibling(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4153711890bcSjc156560 {
4154711890bcSjc156560 	raid_obj_t *obj;
4155711890bcSjc156560 
4156711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4157711890bcSjc156560 	if (obj == NULL)
4158711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4159711890bcSjc156560 
4160711890bcSjc156560 	return (obj->sibling);
4161711890bcSjc156560 }
4162711890bcSjc156560 
4163711890bcSjc156560 static int
4164711890bcSjc156560 raid_obj_set_sibling(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4165711890bcSjc156560 	raid_obj_id_t sibling)
4166711890bcSjc156560 {
4167711890bcSjc156560 	raid_obj_t *obj;
4168711890bcSjc156560 
4169711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4170711890bcSjc156560 	if (obj == NULL)
4171711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4172711890bcSjc156560 
4173711890bcSjc156560 	obj->sibling = sibling;
4174711890bcSjc156560 
4175711890bcSjc156560 	return (SUCCESS);
4176711890bcSjc156560 }
4177711890bcSjc156560 
4178711890bcSjc156560 static void *
4179711890bcSjc156560 raid_obj_get_data_ptr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4180711890bcSjc156560 {
4181711890bcSjc156560 	raid_obj_t *obj;
4182711890bcSjc156560 
4183711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4184711890bcSjc156560 	if (obj == NULL)
4185711890bcSjc156560 		return (NULL);
4186711890bcSjc156560 
4187711890bcSjc156560 	return (obj->data);
4188711890bcSjc156560 }
4189711890bcSjc156560 
4190711890bcSjc156560 static int
4191711890bcSjc156560 raid_obj_set_data_ptr(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4192711890bcSjc156560 	void *data)
4193711890bcSjc156560 {
4194711890bcSjc156560 	raid_obj_t *obj;
4195711890bcSjc156560 
4196711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4197711890bcSjc156560 	if (obj == NULL)
4198711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4199711890bcSjc156560 
4200711890bcSjc156560 	obj->data = data;
4201711890bcSjc156560 
4202711890bcSjc156560 	return (SUCCESS);
4203711890bcSjc156560 }
4204711890bcSjc156560 
4205711890bcSjc156560 static raid_obj_handle_t
4206711890bcSjc156560 raid_obj_get_handle(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id)
4207711890bcSjc156560 {
4208711890bcSjc156560 	raid_obj_t *obj;
4209711890bcSjc156560 
4210711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4211711890bcSjc156560 	if (obj == NULL)
4212711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4213711890bcSjc156560 
4214711890bcSjc156560 	return (obj->handle);
4215711890bcSjc156560 }
4216711890bcSjc156560 
4217711890bcSjc156560 static int
4218711890bcSjc156560 raid_obj_set_handle(raid_obj_tab_t *raid_tab, raid_obj_id_t obj_id,
4219711890bcSjc156560 	raid_obj_handle_t handle)
4220711890bcSjc156560 {
4221711890bcSjc156560 	raid_obj_t *obj;
4222711890bcSjc156560 
4223711890bcSjc156560 	obj = raid_obj_tab_find(raid_tab, obj_id);
4224711890bcSjc156560 	if (obj == NULL)
4225711890bcSjc156560 		return (ERR_DEVICE_NOENT);
4226711890bcSjc156560 
4227711890bcSjc156560 	obj->handle = handle;
4228711890bcSjc156560 	return (SUCCESS);
4229711890bcSjc156560 }
4230711890bcSjc156560 /*
4231711890bcSjc156560  * Object list maintennance routines
4232711890bcSjc156560  */
4233711890bcSjc156560 static void
4234711890bcSjc156560 raid_list_create(raid_list_t *list, size_t offset)
4235711890bcSjc156560 {
4236711890bcSjc156560 	list->head = NULL;
4237711890bcSjc156560 	list->tail = NULL;
4238711890bcSjc156560 	list->offset = offset;
4239711890bcSjc156560 }
4240711890bcSjc156560 
4241711890bcSjc156560 static void *
4242711890bcSjc156560 raid_list_head(raid_list_t *list)
4243711890bcSjc156560 {
4244711890bcSjc156560 	return (list->head);
4245711890bcSjc156560 }
4246711890bcSjc156560 
4247711890bcSjc156560 static void *
4248711890bcSjc156560 raid_list_next(raid_list_t *list, void *obj)
4249711890bcSjc156560 {
4250711890bcSjc156560 	raid_list_el_t *el = LIST_OBJ_TO_EL(list, obj);
4251711890bcSjc156560 
4252711890bcSjc156560 	return (el->next);
4253711890bcSjc156560 }
4254711890bcSjc156560 
4255711890bcSjc156560 static void
4256711890bcSjc156560 raid_list_insert_tail(raid_list_t *list, void *obj)
4257711890bcSjc156560 {
4258711890bcSjc156560 	raid_list_el_t *el = LIST_OBJ_TO_EL(list, obj), *el1;
4259711890bcSjc156560 
4260711890bcSjc156560 	el->prev = list->tail;
4261711890bcSjc156560 	list->tail = obj;
4262711890bcSjc156560 
4263711890bcSjc156560 	el->next = NULL;
4264711890bcSjc156560 
4265711890bcSjc156560 	if (list->head == NULL)
4266711890bcSjc156560 		list->head = obj;
4267711890bcSjc156560 
4268711890bcSjc156560 	if (el->prev != NULL) {
4269711890bcSjc156560 		el1 = LIST_OBJ_TO_EL(list, el->prev);
4270711890bcSjc156560 		el1->next = obj;
4271711890bcSjc156560 	}
4272711890bcSjc156560 }
4273711890bcSjc156560 
4274711890bcSjc156560 static void
4275711890bcSjc156560 raid_list_remove(raid_list_t *list, void *obj)
4276711890bcSjc156560 {
4277711890bcSjc156560 	raid_list_el_t *el = LIST_OBJ_TO_EL(list, obj), *el1;
4278711890bcSjc156560 
4279711890bcSjc156560 	if (list->head == obj)
4280711890bcSjc156560 		list->head = el->next;
4281711890bcSjc156560 
4282711890bcSjc156560 	if (list->tail == obj)
4283711890bcSjc156560 		list->tail = el->prev;
4284711890bcSjc156560 
4285711890bcSjc156560 	if (el->next != NULL) {
4286711890bcSjc156560 		el1 = LIST_OBJ_TO_EL(list, el->next);
4287711890bcSjc156560 		el1->prev = el->prev;
4288711890bcSjc156560 	}
4289711890bcSjc156560 
4290711890bcSjc156560 	if (el->prev != NULL) {
4291711890bcSjc156560 		el1 = LIST_OBJ_TO_EL(list, el->prev);
4292711890bcSjc156560 		el1->next = el->next;
4293711890bcSjc156560 	}
4294711890bcSjc156560 
4295711890bcSjc156560 	el->prev = el->next = NULL;
4296711890bcSjc156560 }
4297711890bcSjc156560 
4298711890bcSjc156560 static void *
4299711890bcSjc156560 raid_list_remove_head(raid_list_t *list)
4300711890bcSjc156560 {
4301711890bcSjc156560 	void *obj = list->head;
4302711890bcSjc156560 
4303711890bcSjc156560 	if (obj != NULL)
4304711890bcSjc156560 		raid_list_remove(list, obj);
4305711890bcSjc156560 
4306711890bcSjc156560 	return (obj);
4307711890bcSjc156560 }
4308711890bcSjc156560 
4309711890bcSjc156560 static void *
4310711890bcSjc156560 raid_list_find(raid_list_t *list, raid_obj_id_t obj_id)
4311711890bcSjc156560 {
4312711890bcSjc156560 	raid_obj_t *obj;
4313711890bcSjc156560 
4314711890bcSjc156560 	for (obj = raid_list_head(list); obj != NULL;
4315711890bcSjc156560 	    obj = raid_list_next(list, obj))
4316711890bcSjc156560 			if (obj->obj_id == obj_id)
4317711890bcSjc156560 				break;
4318711890bcSjc156560 
4319711890bcSjc156560 	return (obj);
4320711890bcSjc156560 }
4321711890bcSjc156560 
4322711890bcSjc156560 static int
4323711890bcSjc156560 raid_obj_tab_create(raid_obj_tab_t *tab, size_t hash_slots)
4324711890bcSjc156560 {
4325711890bcSjc156560 	unsigned i;
4326711890bcSjc156560 
4327711890bcSjc156560 	if (hash_slots == 0)
4328711890bcSjc156560 		return (ERR_OP_ILLEGAL);
4329711890bcSjc156560 
4330711890bcSjc156560 	tab->slots = hash_slots;
4331711890bcSjc156560 
4332711890bcSjc156560 	if ((tab->table = calloc(hash_slots, sizeof (raid_list_t))) == NULL)
4333711890bcSjc156560 		return (ERR_NOMEM);
4334711890bcSjc156560 
4335711890bcSjc156560 	for (i = 0; i < hash_slots; i++)
4336711890bcSjc156560 		raid_list_create(&tab->table[i], offsetof(raid_obj_t, el));
4337711890bcSjc156560 
4338711890bcSjc156560 	return (SUCCESS);
4339711890bcSjc156560 }
4340711890bcSjc156560 
4341711890bcSjc156560 static void
4342711890bcSjc156560 raid_obj_tab_destroy(raid_obj_tab_t *tab)
4343711890bcSjc156560 {
4344711890bcSjc156560 	unsigned i;
4345711890bcSjc156560 
4346711890bcSjc156560 	for (i = 0; i < tab->slots; i++) {
4347711890bcSjc156560 		struct raid_obj_t *obj;
4348711890bcSjc156560 
4349711890bcSjc156560 		while ((obj = raid_list_remove_head(&tab->table[i])) != NULL)
4350711890bcSjc156560 			free(obj);
4351711890bcSjc156560 
4352711890bcSjc156560 		raid_list_destroy(&tab->table[i]);
4353711890bcSjc156560 	}
4354711890bcSjc156560 
4355711890bcSjc156560 	if (tab->table)
4356711890bcSjc156560 		free(tab->table);
4357711890bcSjc156560 
4358711890bcSjc156560 	tab->table = NULL;
4359711890bcSjc156560 	tab->slots = 0;
4360711890bcSjc156560 	tab->obj_id_cnt = 0;
4361711890bcSjc156560 }
4362711890bcSjc156560 
4363711890bcSjc156560 static int
4364711890bcSjc156560 raid_obj_tab_insert(raid_obj_tab_t *tab, raid_obj_id_t id, void *obj)
4365711890bcSjc156560 {
4366711890bcSjc156560 	raid_list_t *list;
4367711890bcSjc156560 
4368711890bcSjc156560 	list = OBJ_TAB_SLOT(tab, id);
4369711890bcSjc156560 
4370711890bcSjc156560 	if (raid_list_find(list, id) != NULL)
4371711890bcSjc156560 		return (ERR_DEVICE_DUP);
4372711890bcSjc156560 
4373711890bcSjc156560 	raid_list_insert_tail(list, obj);
4374711890bcSjc156560 
4375711890bcSjc156560 	return (SUCCESS);
4376711890bcSjc156560 }
4377711890bcSjc156560 
4378711890bcSjc156560 static void *
4379711890bcSjc156560 raid_obj_tab_remove(raid_obj_tab_t *tab, raid_obj_id_t id)
4380711890bcSjc156560 {
4381711890bcSjc156560 	raid_list_t *list;
4382711890bcSjc156560 	raid_obj_t *obj;
4383711890bcSjc156560 
4384711890bcSjc156560 	list = OBJ_TAB_SLOT(tab, id);
4385711890bcSjc156560 
4386711890bcSjc156560 	if ((obj = raid_list_find(list, id)) != NULL)
4387711890bcSjc156560 		raid_list_remove(list, obj);
4388711890bcSjc156560 
4389711890bcSjc156560 	return (obj);
4390711890bcSjc156560 }
4391711890bcSjc156560 
4392711890bcSjc156560 static void *
4393711890bcSjc156560 raid_obj_tab_find(raid_obj_tab_t *tab, raid_obj_id_t id)
4394711890bcSjc156560 {
4395711890bcSjc156560 	raid_list_t *list;
4396711890bcSjc156560 	raid_obj_t *obj;
4397711890bcSjc156560 
4398711890bcSjc156560 	list = OBJ_TAB_SLOT(tab, id);
4399711890bcSjc156560 	obj = raid_list_find(list, id);
4400711890bcSjc156560 
4401711890bcSjc156560 	return (obj);
4402711890bcSjc156560 }
4403711890bcSjc156560 
4404711890bcSjc156560 static void
4405711890bcSjc156560 raid_list_destroy(raid_list_t *list)
4406711890bcSjc156560 {
4407711890bcSjc156560 	list->head = NULL;
4408711890bcSjc156560 	list->tail = NULL;
4409711890bcSjc156560 	list->offset = 0;
4410711890bcSjc156560 }
4411711890bcSjc156560 
4412711890bcSjc156560 /*
4413711890bcSjc156560  * Plug-in maintennance routines
4414711890bcSjc156560  */
4415711890bcSjc156560 static int
4416711890bcSjc156560 controller_id_to_path(uint32_t controller_id, char *path)
4417711890bcSjc156560 {
4418317fb4acSYu-Bo Ryan Wang 	int fd;
4419711890bcSjc156560 	char buf[MAX_PATH_LEN] = {0}, buf1[MAX_PATH_LEN] = {0}, *colon;
4420711890bcSjc156560 
4421711890bcSjc156560 	(void) snprintf(buf, MAX_PATH_LEN, "%s/c%d", CFGDIR, controller_id);
4422711890bcSjc156560 	if (readlink(buf, buf1, sizeof (buf1)) < 0)
4423711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
4424711890bcSjc156560 
4425711890bcSjc156560 	if (buf1[0] != '/')
4426711890bcSjc156560 		(void) snprintf(buf, sizeof (buf), "%s/", CFGDIR);
4427711890bcSjc156560 	else
4428711890bcSjc156560 		buf[0] = 0;
4429711890bcSjc156560 	(void) strlcat(buf, buf1, MAX_PATH_LEN);
4430711890bcSjc156560 
4431711890bcSjc156560 	colon = strrchr(buf, ':');
4432711890bcSjc156560 	if (colon == NULL)
4433711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
4434711890bcSjc156560 	else
4435711890bcSjc156560 		*colon = 0;
4436711890bcSjc156560 
4437711890bcSjc156560 	(void) snprintf(path, MAX_PATH_LEN, "%s:devctl", buf);
4438711890bcSjc156560 
4439317fb4acSYu-Bo Ryan Wang 	fd = open(path, O_RDONLY | O_NDELAY);
4440317fb4acSYu-Bo Ryan Wang 
4441317fb4acSYu-Bo Ryan Wang 	if (fd < 0)
4442711890bcSjc156560 		return (ERR_DRIVER_NOT_FOUND);
4443711890bcSjc156560 
4444317fb4acSYu-Bo Ryan Wang 	(void) close(fd);
4445317fb4acSYu-Bo Ryan Wang 
4446711890bcSjc156560 	return (SUCCESS);
4447711890bcSjc156560 }
4448711890bcSjc156560 
4449711890bcSjc156560 static char *
4450711890bcSjc156560 controller_id_to_driver_name(uint32_t controller_id)
4451711890bcSjc156560 {
4452711890bcSjc156560 	char buf[MAX_PATH_LEN];
4453711890bcSjc156560 	di_node_t di_node;
4454711890bcSjc156560 	char *name, *tmp;
4455711890bcSjc156560 	int ret;
4456711890bcSjc156560 
4457711890bcSjc156560 	ret = controller_id_to_path(controller_id, buf);
4458711890bcSjc156560 	if (ret < SUCCESS)
4459711890bcSjc156560 		return (NULL);
4460711890bcSjc156560 
4461711890bcSjc156560 	tmp = strrchr(buf, ':');
4462711890bcSjc156560 	if (tmp != NULL)
4463711890bcSjc156560 		*tmp = 0;
4464711890bcSjc156560 
4465711890bcSjc156560 	tmp = strstr(buf, "pci");
4466711890bcSjc156560 	if (tmp == NULL)
4467711890bcSjc156560 		return (NULL);
4468711890bcSjc156560 
4469711890bcSjc156560 	di_node = di_init(tmp, DINFOPROP);
4470711890bcSjc156560 	if (di_node == DI_NODE_NIL)
4471711890bcSjc156560 		return (NULL);
4472711890bcSjc156560 
4473711890bcSjc156560 	name = di_driver_name(di_node);
4474711890bcSjc156560 
4475711890bcSjc156560 	return (name);
4476711890bcSjc156560 }
4477711890bcSjc156560 
4478711890bcSjc156560 static void
4479711890bcSjc156560 raid_plugin_init()
4480711890bcSjc156560 {
4481711890bcSjc156560 	raid_lib_t *raid_lib = raid_lib_sys;
4482711890bcSjc156560 
4483711890bcSjc156560 	while (raid_lib) {
4484711890bcSjc156560 		raid_lib_sys = raid_lib->next;
4485711890bcSjc156560 		(void) dlclose(raid_lib->lib_handle);
4486711890bcSjc156560 		free(raid_lib);
4487711890bcSjc156560 		raid_lib = raid_lib_sys;
4488711890bcSjc156560 	}
4489711890bcSjc156560 }
4490711890bcSjc156560 
4491711890bcSjc156560 static raid_lib_t *
4492711890bcSjc156560 raid_plugin_load(char *driver_name)
4493711890bcSjc156560 {
4494711890bcSjc156560 	char buf[MAX_PATH_LEN] = {0};
4495711890bcSjc156560 	raid_lib_t *supplib;
4496711890bcSjc156560 	void *sym;
4497711890bcSjc156560 
4498711890bcSjc156560 	supplib = calloc(1, sizeof (raid_lib_t));
4499711890bcSjc156560 	if (supplib == NULL)
4500711890bcSjc156560 		return (NULL);
4501711890bcSjc156560 
4502711890bcSjc156560 	(void) snprintf(buf, MAX_PATH_LEN, "%s/%s.so.1",
4503711890bcSjc156560 	    SUPP_PLUGIN_DIR, driver_name);
4504711890bcSjc156560 
4505711890bcSjc156560 	supplib->lib_handle = dlopen(buf, RTLD_LAZY);
4506711890bcSjc156560 	if (supplib->lib_handle == NULL) {
4507711890bcSjc156560 		free(supplib);
4508711890bcSjc156560 		return (NULL);
4509711890bcSjc156560 	}
4510711890bcSjc156560 
4511711890bcSjc156560 	supplib->name = driver_name;
4512711890bcSjc156560 
4513711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_version")) == NULL)
4514711890bcSjc156560 		supplib->version = RDCFG_PLUGIN_V1;
4515711890bcSjc156560 	else {
4516711890bcSjc156560 		supplib->version = *((uint32_t *)sym);
4517711890bcSjc156560 		if (supplib->version != RDCFG_PLUGIN_V1) {
4518711890bcSjc156560 			(void) dlclose(supplib->lib_handle);
4519711890bcSjc156560 			free(supplib);
4520711890bcSjc156560 			return (NULL);
4521711890bcSjc156560 		}
4522711890bcSjc156560 	}
4523711890bcSjc156560 
4524711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_open_controller")) ==
4525711890bcSjc156560 	    NULL) {
4526711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4527711890bcSjc156560 		free(supplib);
4528711890bcSjc156560 		return (NULL);
4529711890bcSjc156560 	} else
4530711890bcSjc156560 		supplib->open_controller = (int(*)(uint32_t, char **))sym;
4531711890bcSjc156560 
4532711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_close_controller")) ==
4533711890bcSjc156560 	    NULL) {
4534711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4535711890bcSjc156560 		free(supplib);
4536711890bcSjc156560 		return (NULL);
4537711890bcSjc156560 	} else
4538711890bcSjc156560 		supplib->close_controller = (int (*)(uint32_t, char **))sym;
4539711890bcSjc156560 
4540711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_compnum")) == NULL) {
4541711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4542711890bcSjc156560 		free(supplib);
4543711890bcSjc156560 		return (NULL);
4544711890bcSjc156560 	} else
4545711890bcSjc156560 		supplib->compnum = (int (*)(uint32_t, uint32_t,
4546711890bcSjc156560 		    raid_obj_type_id_t, raid_obj_type_id_t))sym;
4547711890bcSjc156560 
4548711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_complist")) == NULL) {
4549711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4550711890bcSjc156560 		free(supplib);
4551711890bcSjc156560 		return (NULL);
4552711890bcSjc156560 	} else
4553711890bcSjc156560 		supplib->complist = (int (*)(uint32_t, uint32_t,
4554711890bcSjc156560 		    raid_obj_type_id_t, raid_obj_type_id_t, int, void *))sym;
4555711890bcSjc156560 
4556711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_get_attr")) == NULL) {
4557711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4558711890bcSjc156560 		free(supplib);
4559711890bcSjc156560 		return (NULL);
4560711890bcSjc156560 	} else
4561711890bcSjc156560 		supplib->get_attr = (int (*)(uint32_t, uint32_t, uint32_t,
4562711890bcSjc156560 		    raid_obj_type_id_t, void*))sym;
4563711890bcSjc156560 
4564711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_array_create")) == NULL) {
4565711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4566711890bcSjc156560 		free(supplib);
4567711890bcSjc156560 		return (NULL);
4568711890bcSjc156560 	} else
4569711890bcSjc156560 		supplib->array_create = (int (*)(uint32_t, array_attr_t *, int,
4570711890bcSjc156560 		    arraypart_attr_t *, char **))sym;
4571711890bcSjc156560 
4572711890bcSjc156560 	if ((sym = dlsym(supplib->lib_handle, "rdcfg_array_delete")) == NULL) {
4573711890bcSjc156560 		(void) dlclose(supplib->lib_handle);
4574711890bcSjc156560 		free(supplib);
4575711890bcSjc156560 		return (NULL);
4576711890bcSjc156560 	} else
4577711890bcSjc156560 		supplib->array_delete =
4578711890bcSjc156560 		    (int (*)(uint32_t, uint32_t, char **))sym;
4579711890bcSjc156560 
4580*5c9d25d2SYu-Bo Ryan Wang 	supplib->hsp_bind = (int (*)(uint32_t, hsp_relation_t *,
4581711890bcSjc156560 	    char **))dlsym(supplib->lib_handle, "rdcfg_hsp_bind");
4582*5c9d25d2SYu-Bo Ryan Wang 	supplib->hsp_unbind = (int (*)(uint32_t, hsp_relation_t *,
4583711890bcSjc156560 	    char **))dlsym(supplib->lib_handle, "rdcfg_hsp_unbind");
4584711890bcSjc156560 	supplib->set_attr = (int (*)(uint32_t, uint32_t, uint32_t, uint32_t *,
4585711890bcSjc156560 	    char **))dlsym(supplib->lib_handle, "rdcfg_set_attr");
4586711890bcSjc156560 	supplib->flash_fw = (int (*)(uint32_t, char *, uint32_t, char **))
4587711890bcSjc156560 	    dlsym(supplib->lib_handle, "rdcfg_flash_fw");
4588711890bcSjc156560 
4589711890bcSjc156560 	supplib->next = raid_lib_sys;
4590711890bcSjc156560 	raid_lib_sys = supplib;
4591711890bcSjc156560 	return (supplib);
4592711890bcSjc156560 }
4593711890bcSjc156560 
4594711890bcSjc156560 static raid_lib_t *
4595711890bcSjc156560 raid_find_lib(raid_obj_tab_t *raid_tab, raid_obj_id_t controller_obj_id)
4596711890bcSjc156560 {
4597711890bcSjc156560 	controller_attr_t *controller_attr;
4598711890bcSjc156560 	raid_lib_t *raid_lib;
4599711890bcSjc156560 	char *driver_name;
4600711890bcSjc156560 	raid_obj_handle_t handle;
4601711890bcSjc156560 
4602711890bcSjc156560 	/* Check if it's mapped to handle structure */
4603711890bcSjc156560 	handle = raid_obj_to_handle(raid_tab, controller_obj_id);
4604711890bcSjc156560 	if (raid_handle_sys.handles[handle].raid_lib != NULL)
4605711890bcSjc156560 		return (raid_handle_sys.handles[handle].raid_lib);
4606711890bcSjc156560 
4607711890bcSjc156560 	(void) obj_get_attr(raid_tab, controller_obj_id,
4608711890bcSjc156560 	    (void **)(&controller_attr));
4609711890bcSjc156560 
4610711890bcSjc156560 	/* Check if the plugin module is already loaded */
4611711890bcSjc156560 	driver_name = controller_id_to_driver_name(
4612711890bcSjc156560 	    controller_attr->controller_id);
4613711890bcSjc156560 	if (driver_name == NULL)
4614711890bcSjc156560 		return (NULL);
4615711890bcSjc156560 
4616711890bcSjc156560 	raid_lib = raid_lib_sys;
4617711890bcSjc156560 	while (raid_lib != NULL) {
4618711890bcSjc156560 		if (raid_lib->name != NULL &&
4619711890bcSjc156560 		    strcmp(driver_name, raid_lib->name) == 0)
4620711890bcSjc156560 			return (raid_lib);
4621711890bcSjc156560 
4622711890bcSjc156560 		raid_lib = raid_lib->next;
4623711890bcSjc156560 	}
4624711890bcSjc156560 
4625711890bcSjc156560 	/* Loading the plugin module */
4626711890bcSjc156560 	raid_lib = raid_plugin_load(driver_name);
4627711890bcSjc156560 
4628711890bcSjc156560 	return (raid_lib);
4629711890bcSjc156560 }
4630