xref: /onnv-gate/usr/src/lib/libsrpt/common/libsrpt.c (revision 12075:b143b9fb49fc)
1*12075SSusan.Gleeson@Sun.COM /*
2*12075SSusan.Gleeson@Sun.COM  * CDDL HEADER START
3*12075SSusan.Gleeson@Sun.COM  *
4*12075SSusan.Gleeson@Sun.COM  * The contents of this file are subject to the terms of the
5*12075SSusan.Gleeson@Sun.COM  * Common Development and Distribution License (the "License").
6*12075SSusan.Gleeson@Sun.COM  * You may not use this file except in compliance with the License.
7*12075SSusan.Gleeson@Sun.COM  *
8*12075SSusan.Gleeson@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12075SSusan.Gleeson@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*12075SSusan.Gleeson@Sun.COM  * See the License for the specific language governing permissions
11*12075SSusan.Gleeson@Sun.COM  * and limitations under the License.
12*12075SSusan.Gleeson@Sun.COM  *
13*12075SSusan.Gleeson@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*12075SSusan.Gleeson@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12075SSusan.Gleeson@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*12075SSusan.Gleeson@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*12075SSusan.Gleeson@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*12075SSusan.Gleeson@Sun.COM  *
19*12075SSusan.Gleeson@Sun.COM  * CDDL HEADER END
20*12075SSusan.Gleeson@Sun.COM  */
21*12075SSusan.Gleeson@Sun.COM /*
22*12075SSusan.Gleeson@Sun.COM  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23*12075SSusan.Gleeson@Sun.COM  */
24*12075SSusan.Gleeson@Sun.COM 
25*12075SSusan.Gleeson@Sun.COM #include <sys/types.h>
26*12075SSusan.Gleeson@Sun.COM #include <sys/stat.h>
27*12075SSusan.Gleeson@Sun.COM #include <limits.h>
28*12075SSusan.Gleeson@Sun.COM #include <ctype.h>
29*12075SSusan.Gleeson@Sun.COM #include <fcntl.h>
30*12075SSusan.Gleeson@Sun.COM #include <errno.h>
31*12075SSusan.Gleeson@Sun.COM #include <unistd.h>
32*12075SSusan.Gleeson@Sun.COM #include <strings.h>
33*12075SSusan.Gleeson@Sun.COM #include <libintl.h>
34*12075SSusan.Gleeson@Sun.COM #include <libscf.h>
35*12075SSusan.Gleeson@Sun.COM #include <libnvpair.h>
36*12075SSusan.Gleeson@Sun.COM 
37*12075SSusan.Gleeson@Sun.COM #include <libstmf.h>
38*12075SSusan.Gleeson@Sun.COM #include <libsrpt.h>
39*12075SSusan.Gleeson@Sun.COM 
40*12075SSusan.Gleeson@Sun.COM #include "srpt_common.h"
41*12075SSusan.Gleeson@Sun.COM 
42*12075SSusan.Gleeson@Sun.COM #define	SRPT_PROV_NAME	"srpt"
43*12075SSusan.Gleeson@Sun.COM 
44*12075SSusan.Gleeson@Sun.COM /*
45*12075SSusan.Gleeson@Sun.COM  * Function:  srpt_GetConfig()
46*12075SSusan.Gleeson@Sun.COM  *
47*12075SSusan.Gleeson@Sun.COM  * Parameters:
48*12075SSusan.Gleeson@Sun.COM  *    cfg	Current SRPT configuration in nvlist form
49*12075SSusan.Gleeson@Sun.COM  *    token	Configuration generation number.  Use this token
50*12075SSusan.Gleeson@Sun.COM  *		if updating the configuration with srpt_SetConfig.
51*12075SSusan.Gleeson@Sun.COM  *
52*12075SSusan.Gleeson@Sun.COM  * Return Values:
53*12075SSusan.Gleeson@Sun.COM  *    0		Success
54*12075SSusan.Gleeson@Sun.COM  *    ENOMEM	Could not allocate resources
55*12075SSusan.Gleeson@Sun.COM  *    EINVAL	Invalid parameter
56*12075SSusan.Gleeson@Sun.COM  */
57*12075SSusan.Gleeson@Sun.COM int
srpt_GetConfig(nvlist_t ** cfg,uint64_t * token)58*12075SSusan.Gleeson@Sun.COM srpt_GetConfig(nvlist_t **cfg, uint64_t *token)
59*12075SSusan.Gleeson@Sun.COM {
60*12075SSusan.Gleeson@Sun.COM 	int		ret = 0;
61*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*cfg_nv = NULL;
62*12075SSusan.Gleeson@Sun.COM 	uint64_t	stmf_token = 0;
63*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*hcanv = NULL;
64*12075SSusan.Gleeson@Sun.COM 
65*12075SSusan.Gleeson@Sun.COM 	if (!cfg) {
66*12075SSusan.Gleeson@Sun.COM 		return (EINVAL);
67*12075SSusan.Gleeson@Sun.COM 	}
68*12075SSusan.Gleeson@Sun.COM 
69*12075SSusan.Gleeson@Sun.COM 	*cfg = NULL;
70*12075SSusan.Gleeson@Sun.COM 
71*12075SSusan.Gleeson@Sun.COM 	ret = stmfGetProviderDataProt(SRPT_PROV_NAME, &cfg_nv,
72*12075SSusan.Gleeson@Sun.COM 	    STMF_PORT_PROVIDER_TYPE, &stmf_token);
73*12075SSusan.Gleeson@Sun.COM 
74*12075SSusan.Gleeson@Sun.COM 	if (ret == STMF_STATUS_SUCCESS) {
75*12075SSusan.Gleeson@Sun.COM 		ret = 0;
76*12075SSusan.Gleeson@Sun.COM 	} else if (ret == STMF_ERROR_NOT_FOUND) {
77*12075SSusan.Gleeson@Sun.COM 		/* Not initialized yet */
78*12075SSusan.Gleeson@Sun.COM 		ret = nvlist_alloc(&cfg_nv, NV_UNIQUE_NAME, 0);
79*12075SSusan.Gleeson@Sun.COM 		if (ret != 0) {
80*12075SSusan.Gleeson@Sun.COM 			return (ret);
81*12075SSusan.Gleeson@Sun.COM 		}
82*12075SSusan.Gleeson@Sun.COM 		/* create the HCA list */
83*12075SSusan.Gleeson@Sun.COM 		ret = nvlist_alloc(&hcanv, NV_UNIQUE_NAME, 0);
84*12075SSusan.Gleeson@Sun.COM 		if (ret == 0) {
85*12075SSusan.Gleeson@Sun.COM 			ret = nvlist_add_nvlist(cfg_nv, SRPT_PROP_HCALIST,
86*12075SSusan.Gleeson@Sun.COM 			    hcanv);
87*12075SSusan.Gleeson@Sun.COM 			if (ret != 0) {
88*12075SSusan.Gleeson@Sun.COM 				nvlist_free(hcanv);
89*12075SSusan.Gleeson@Sun.COM 			}
90*12075SSusan.Gleeson@Sun.COM 		}
91*12075SSusan.Gleeson@Sun.COM 		if (ret != 0) {
92*12075SSusan.Gleeson@Sun.COM 			nvlist_free(cfg_nv);
93*12075SSusan.Gleeson@Sun.COM 			cfg_nv = NULL;
94*12075SSusan.Gleeson@Sun.COM 		}
95*12075SSusan.Gleeson@Sun.COM 	} else if (ret == STMF_ERROR_NOMEM) {
96*12075SSusan.Gleeson@Sun.COM 		ret = ENOMEM;
97*12075SSusan.Gleeson@Sun.COM 	} else {
98*12075SSusan.Gleeson@Sun.COM 		ret = EINVAL;
99*12075SSusan.Gleeson@Sun.COM 	}
100*12075SSusan.Gleeson@Sun.COM 
101*12075SSusan.Gleeson@Sun.COM 	*cfg = cfg_nv;
102*12075SSusan.Gleeson@Sun.COM 	*token = stmf_token;
103*12075SSusan.Gleeson@Sun.COM 
104*12075SSusan.Gleeson@Sun.COM 	return (ret);
105*12075SSusan.Gleeson@Sun.COM }
106*12075SSusan.Gleeson@Sun.COM 
107*12075SSusan.Gleeson@Sun.COM /*
108*12075SSusan.Gleeson@Sun.COM  * Function:  srpt_SetConfig()
109*12075SSusan.Gleeson@Sun.COM  *
110*12075SSusan.Gleeson@Sun.COM  * Parameters:
111*12075SSusan.Gleeson@Sun.COM  *    cfg	SRPT configuration in nvlist form
112*12075SSusan.Gleeson@Sun.COM  *    token	Configuration generation number from srpt_GetConfig.
113*12075SSusan.Gleeson@Sun.COM  *		Use this token to ensure the configuration hasn't been
114*12075SSusan.Gleeson@Sun.COM  *		updated by another user since the time it was fetched.
115*12075SSusan.Gleeson@Sun.COM  *
116*12075SSusan.Gleeson@Sun.COM  * Return Values:
117*12075SSusan.Gleeson@Sun.COM  *    0		Success
118*12075SSusan.Gleeson@Sun.COM  *    ENOMEM	Could not allocate resources
119*12075SSusan.Gleeson@Sun.COM  *    EINVAL	Invalid parameter
120*12075SSusan.Gleeson@Sun.COM  *    ECANCELED Configuration updated by another user
121*12075SSusan.Gleeson@Sun.COM  */
122*12075SSusan.Gleeson@Sun.COM int
srpt_SetConfig(nvlist_t * cfg,uint64_t token)123*12075SSusan.Gleeson@Sun.COM srpt_SetConfig(nvlist_t *cfg, uint64_t token)
124*12075SSusan.Gleeson@Sun.COM {
125*12075SSusan.Gleeson@Sun.COM 	int		ret = 0;
126*12075SSusan.Gleeson@Sun.COM 
127*12075SSusan.Gleeson@Sun.COM 	ret = stmfSetProviderDataProt(SRPT_PROV_NAME, cfg,
128*12075SSusan.Gleeson@Sun.COM 	    STMF_PORT_PROVIDER_TYPE, &token);
129*12075SSusan.Gleeson@Sun.COM 
130*12075SSusan.Gleeson@Sun.COM 	if (ret == STMF_STATUS_SUCCESS) {
131*12075SSusan.Gleeson@Sun.COM 		ret = 0;
132*12075SSusan.Gleeson@Sun.COM 	} else if (ret == STMF_ERROR_NOMEM) {
133*12075SSusan.Gleeson@Sun.COM 		ret = ENOMEM;
134*12075SSusan.Gleeson@Sun.COM 	} else if (ret == STMF_ERROR_PROV_DATA_STALE) {
135*12075SSusan.Gleeson@Sun.COM 		ret = ECANCELED;  /* could be a better errno */
136*12075SSusan.Gleeson@Sun.COM 	} else {
137*12075SSusan.Gleeson@Sun.COM 		ret = EINVAL;
138*12075SSusan.Gleeson@Sun.COM 	}
139*12075SSusan.Gleeson@Sun.COM 
140*12075SSusan.Gleeson@Sun.COM 	return (ret);
141*12075SSusan.Gleeson@Sun.COM }
142*12075SSusan.Gleeson@Sun.COM 
143*12075SSusan.Gleeson@Sun.COM /*
144*12075SSusan.Gleeson@Sun.COM  * Function:  srpt_GetDefaultState()
145*12075SSusan.Gleeson@Sun.COM  *
146*12075SSusan.Gleeson@Sun.COM  * Parameters:
147*12075SSusan.Gleeson@Sun.COM  *    enabled	If B_TRUE, indicates that targets will be created for all
148*12075SSusan.Gleeson@Sun.COM  *		discovered HCAs that have not been specifically disabled.
149*12075SSusan.Gleeson@Sun.COM  *		If B_FALSE, targets will not be created unless the HCA has
150*12075SSusan.Gleeson@Sun.COM  *		been specifically enabled.  See also srpt_SetDefaultState().
151*12075SSusan.Gleeson@Sun.COM  *
152*12075SSusan.Gleeson@Sun.COM  * Return Values:
153*12075SSusan.Gleeson@Sun.COM  *    0		Success
154*12075SSusan.Gleeson@Sun.COM  *    ENOMEM	Could not allocate resources
155*12075SSusan.Gleeson@Sun.COM  *    EINVAL	Invalid parameter
156*12075SSusan.Gleeson@Sun.COM  */
157*12075SSusan.Gleeson@Sun.COM int
srpt_GetDefaultState(boolean_t * enabled)158*12075SSusan.Gleeson@Sun.COM srpt_GetDefaultState(boolean_t *enabled)
159*12075SSusan.Gleeson@Sun.COM {
160*12075SSusan.Gleeson@Sun.COM 	int		ret;
161*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*cfgnv;
162*12075SSusan.Gleeson@Sun.COM 	uint64_t	token;
163*12075SSusan.Gleeson@Sun.COM 	boolean_t	val = B_TRUE;
164*12075SSusan.Gleeson@Sun.COM 
165*12075SSusan.Gleeson@Sun.COM 	if (enabled == NULL) {
166*12075SSusan.Gleeson@Sun.COM 		return (EINVAL);
167*12075SSusan.Gleeson@Sun.COM 	}
168*12075SSusan.Gleeson@Sun.COM 
169*12075SSusan.Gleeson@Sun.COM 	ret = srpt_GetConfig(&cfgnv, &token);
170*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
171*12075SSusan.Gleeson@Sun.COM 		return (ret);
172*12075SSusan.Gleeson@Sun.COM 	}
173*12075SSusan.Gleeson@Sun.COM 
174*12075SSusan.Gleeson@Sun.COM 	if (cfgnv != NULL) {
175*12075SSusan.Gleeson@Sun.COM 		ret = nvlist_lookup_boolean_value(cfgnv,
176*12075SSusan.Gleeson@Sun.COM 		    SRPT_PROP_DEFAULT_ENABLED, &val);
177*12075SSusan.Gleeson@Sun.COM 
178*12075SSusan.Gleeson@Sun.COM 		if (ret == ENOENT) {
179*12075SSusan.Gleeson@Sun.COM 			ret = 0;
180*12075SSusan.Gleeson@Sun.COM 		}
181*12075SSusan.Gleeson@Sun.COM 	}
182*12075SSusan.Gleeson@Sun.COM 
183*12075SSusan.Gleeson@Sun.COM 	*enabled = val;
184*12075SSusan.Gleeson@Sun.COM 	return (ret);
185*12075SSusan.Gleeson@Sun.COM }
186*12075SSusan.Gleeson@Sun.COM 
187*12075SSusan.Gleeson@Sun.COM /*
188*12075SSusan.Gleeson@Sun.COM  * Function:  srpt_SetDefaultState()
189*12075SSusan.Gleeson@Sun.COM  *
190*12075SSusan.Gleeson@Sun.COM  * Parameters:
191*12075SSusan.Gleeson@Sun.COM  *    enabled	If B_TRUE, indicates that targets will be created for all
192*12075SSusan.Gleeson@Sun.COM  *		discovered HCAs that have not been specifically disabled.
193*12075SSusan.Gleeson@Sun.COM  *		If B_FALSE, targets will not be created unless the HCA has
194*12075SSusan.Gleeson@Sun.COM  *		been specifically enabled.  See also srpt_SetDefaultState().
195*12075SSusan.Gleeson@Sun.COM  *
196*12075SSusan.Gleeson@Sun.COM  * Return Values:
197*12075SSusan.Gleeson@Sun.COM  *    0		Success
198*12075SSusan.Gleeson@Sun.COM  *    ENOMEM	Could not allocate resources
199*12075SSusan.Gleeson@Sun.COM  *    EINVAL	Invalid parameter
200*12075SSusan.Gleeson@Sun.COM  */
201*12075SSusan.Gleeson@Sun.COM int
srpt_SetDefaultState(boolean_t enabled)202*12075SSusan.Gleeson@Sun.COM srpt_SetDefaultState(boolean_t enabled)
203*12075SSusan.Gleeson@Sun.COM {
204*12075SSusan.Gleeson@Sun.COM 	int		ret;
205*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*cfgnv;
206*12075SSusan.Gleeson@Sun.COM 	uint64_t	token;
207*12075SSusan.Gleeson@Sun.COM 
208*12075SSusan.Gleeson@Sun.COM 	ret = srpt_GetConfig(&cfgnv, &token);
209*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
210*12075SSusan.Gleeson@Sun.COM 		return (ret);
211*12075SSusan.Gleeson@Sun.COM 	}
212*12075SSusan.Gleeson@Sun.COM 
213*12075SSusan.Gleeson@Sun.COM 	if (cfgnv == NULL) {
214*12075SSusan.Gleeson@Sun.COM 		ret = nvlist_alloc(&cfgnv, NV_UNIQUE_NAME, 0);
215*12075SSusan.Gleeson@Sun.COM 		if (ret != 0) {
216*12075SSusan.Gleeson@Sun.COM 			return (ret);
217*12075SSusan.Gleeson@Sun.COM 		}
218*12075SSusan.Gleeson@Sun.COM 	}
219*12075SSusan.Gleeson@Sun.COM 
220*12075SSusan.Gleeson@Sun.COM 	ret = nvlist_add_boolean_value(cfgnv, SRPT_PROP_DEFAULT_ENABLED,
221*12075SSusan.Gleeson@Sun.COM 	    enabled);
222*12075SSusan.Gleeson@Sun.COM 
223*12075SSusan.Gleeson@Sun.COM 	if (ret == 0) {
224*12075SSusan.Gleeson@Sun.COM 		ret = srpt_SetConfig(cfgnv, token);
225*12075SSusan.Gleeson@Sun.COM 	}
226*12075SSusan.Gleeson@Sun.COM 
227*12075SSusan.Gleeson@Sun.COM 	nvlist_free(cfgnv);
228*12075SSusan.Gleeson@Sun.COM 
229*12075SSusan.Gleeson@Sun.COM 	return (ret);
230*12075SSusan.Gleeson@Sun.COM }
231*12075SSusan.Gleeson@Sun.COM 
232*12075SSusan.Gleeson@Sun.COM /*
233*12075SSusan.Gleeson@Sun.COM  * Function:  srpt_SetTargetState()
234*12075SSusan.Gleeson@Sun.COM  *
235*12075SSusan.Gleeson@Sun.COM  * Parameters:
236*12075SSusan.Gleeson@Sun.COM  *    hca_guid	HCA GUID.  See description of srpt_NormalizeGuid
237*12075SSusan.Gleeson@Sun.COM  *    enabled	If B_TRUE, indicates that a target will be created for
238*12075SSusan.Gleeson@Sun.COM  *		this HCA when the SRPT SMF service is enabled.  If B_FALSE,
239*12075SSusan.Gleeson@Sun.COM  *		a target will not be created
240*12075SSusan.Gleeson@Sun.COM  *
241*12075SSusan.Gleeson@Sun.COM  * Return Values:
242*12075SSusan.Gleeson@Sun.COM  *    0		Success
243*12075SSusan.Gleeson@Sun.COM  *    ENOMEM	Could not allocate resources
244*12075SSusan.Gleeson@Sun.COM  *    EINVAL	Invalid parameter
245*12075SSusan.Gleeson@Sun.COM  */
246*12075SSusan.Gleeson@Sun.COM int
srpt_SetTargetState(char * hca_guid,boolean_t enabled)247*12075SSusan.Gleeson@Sun.COM srpt_SetTargetState(char *hca_guid, boolean_t enabled)
248*12075SSusan.Gleeson@Sun.COM {
249*12075SSusan.Gleeson@Sun.COM 	int		ret;
250*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*cfgnv;
251*12075SSusan.Gleeson@Sun.COM 	uint64_t	token;
252*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*hcalist;
253*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*hcanv;
254*12075SSusan.Gleeson@Sun.COM 	char		guid[32];
255*12075SSusan.Gleeson@Sun.COM 	uint64_t	hcaguid;
256*12075SSusan.Gleeson@Sun.COM 
257*12075SSusan.Gleeson@Sun.COM 	if (hca_guid == NULL) {
258*12075SSusan.Gleeson@Sun.COM 		return (EINVAL);
259*12075SSusan.Gleeson@Sun.COM 	}
260*12075SSusan.Gleeson@Sun.COM 
261*12075SSusan.Gleeson@Sun.COM 	ret = srpt_NormalizeGuid(hca_guid, guid, sizeof (guid), &hcaguid);
262*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
263*12075SSusan.Gleeson@Sun.COM 		return (ret);
264*12075SSusan.Gleeson@Sun.COM 	}
265*12075SSusan.Gleeson@Sun.COM 
266*12075SSusan.Gleeson@Sun.COM 	ret = srpt_GetConfig(&cfgnv, &token);
267*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
268*12075SSusan.Gleeson@Sun.COM 		return (ret);
269*12075SSusan.Gleeson@Sun.COM 	}
270*12075SSusan.Gleeson@Sun.COM 
271*12075SSusan.Gleeson@Sun.COM 	/* get the list of HCAs */
272*12075SSusan.Gleeson@Sun.COM 	ret = nvlist_lookup_nvlist(cfgnv, SRPT_PROP_HCALIST, &hcalist);
273*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
274*12075SSusan.Gleeson@Sun.COM 		nvlist_free(cfgnv);
275*12075SSusan.Gleeson@Sun.COM 		return (ret);
276*12075SSusan.Gleeson@Sun.COM 	}
277*12075SSusan.Gleeson@Sun.COM 
278*12075SSusan.Gleeson@Sun.COM 	ret = nvlist_lookup_nvlist(hcalist, guid, &hcanv);
279*12075SSusan.Gleeson@Sun.COM 	if (ret == ENOENT) {
280*12075SSusan.Gleeson@Sun.COM 		/* no entry yet */
281*12075SSusan.Gleeson@Sun.COM 		ret = nvlist_alloc(&hcanv, NV_UNIQUE_NAME, 0);
282*12075SSusan.Gleeson@Sun.COM 		if (ret == 0) {
283*12075SSusan.Gleeson@Sun.COM 			ret = nvlist_add_uint64(hcanv, SRPT_PROP_GUID, hcaguid);
284*12075SSusan.Gleeson@Sun.COM 		}
285*12075SSusan.Gleeson@Sun.COM 	}
286*12075SSusan.Gleeson@Sun.COM 
287*12075SSusan.Gleeson@Sun.COM 	if (ret == 0) {
288*12075SSusan.Gleeson@Sun.COM 		ret = nvlist_add_boolean_value(hcanv, SRPT_PROP_ENABLED,
289*12075SSusan.Gleeson@Sun.COM 		    enabled);
290*12075SSusan.Gleeson@Sun.COM 	}
291*12075SSusan.Gleeson@Sun.COM 
292*12075SSusan.Gleeson@Sun.COM 	if (ret == 0) {
293*12075SSusan.Gleeson@Sun.COM 		ret = nvlist_add_nvlist(hcalist, guid, hcanv);
294*12075SSusan.Gleeson@Sun.COM 	}
295*12075SSusan.Gleeson@Sun.COM 
296*12075SSusan.Gleeson@Sun.COM 	if (ret == 0) {
297*12075SSusan.Gleeson@Sun.COM 		ret = srpt_SetConfig(cfgnv, token);
298*12075SSusan.Gleeson@Sun.COM 	}
299*12075SSusan.Gleeson@Sun.COM 
300*12075SSusan.Gleeson@Sun.COM 	nvlist_free(cfgnv);
301*12075SSusan.Gleeson@Sun.COM 
302*12075SSusan.Gleeson@Sun.COM 	return (ret);
303*12075SSusan.Gleeson@Sun.COM }
304*12075SSusan.Gleeson@Sun.COM 
305*12075SSusan.Gleeson@Sun.COM /*
306*12075SSusan.Gleeson@Sun.COM  * Function:  srpt_GetTargetState()
307*12075SSusan.Gleeson@Sun.COM  *
308*12075SSusan.Gleeson@Sun.COM  * Parameters:
309*12075SSusan.Gleeson@Sun.COM  *    hca_guid	HCA GUID.  See description of srpt_NormalizeGuid
310*12075SSusan.Gleeson@Sun.COM  *    enabled	If B_TRUE, indicates that a target will be created for
311*12075SSusan.Gleeson@Sun.COM  *		this HCA when the SRPT SMF service is enabled.  If B_FALSE,
312*12075SSusan.Gleeson@Sun.COM  *		a target will not be created
313*12075SSusan.Gleeson@Sun.COM  *
314*12075SSusan.Gleeson@Sun.COM  * Return Values:
315*12075SSusan.Gleeson@Sun.COM  *    0		Success
316*12075SSusan.Gleeson@Sun.COM  *    ENOMEM	Could not allocate resources
317*12075SSusan.Gleeson@Sun.COM  *    EINVAL	Invalid parameter
318*12075SSusan.Gleeson@Sun.COM  */
319*12075SSusan.Gleeson@Sun.COM int
srpt_GetTargetState(char * hca_guid,boolean_t * enabled)320*12075SSusan.Gleeson@Sun.COM srpt_GetTargetState(char *hca_guid, boolean_t *enabled)
321*12075SSusan.Gleeson@Sun.COM {
322*12075SSusan.Gleeson@Sun.COM 	int		ret;
323*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*cfgnv;
324*12075SSusan.Gleeson@Sun.COM 	uint64_t	token;
325*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*hcalist;
326*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*hcanv;
327*12075SSusan.Gleeson@Sun.COM 	boolean_t	defaultState = B_TRUE;
328*12075SSusan.Gleeson@Sun.COM 	char		guid[32];
329*12075SSusan.Gleeson@Sun.COM 
330*12075SSusan.Gleeson@Sun.COM 	if (hca_guid == NULL) {
331*12075SSusan.Gleeson@Sun.COM 		return (EINVAL);
332*12075SSusan.Gleeson@Sun.COM 	}
333*12075SSusan.Gleeson@Sun.COM 
334*12075SSusan.Gleeson@Sun.COM 	ret = srpt_NormalizeGuid(hca_guid, guid, sizeof (guid), NULL);
335*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
336*12075SSusan.Gleeson@Sun.COM 		return (ret);
337*12075SSusan.Gleeson@Sun.COM 	}
338*12075SSusan.Gleeson@Sun.COM 
339*12075SSusan.Gleeson@Sun.COM 	ret = srpt_GetConfig(&cfgnv, &token);
340*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
341*12075SSusan.Gleeson@Sun.COM 		return (ret);
342*12075SSusan.Gleeson@Sun.COM 	}
343*12075SSusan.Gleeson@Sun.COM 
344*12075SSusan.Gleeson@Sun.COM 	/* get the list of HCAs */
345*12075SSusan.Gleeson@Sun.COM 	ret = nvlist_lookup_nvlist(cfgnv, SRPT_PROP_HCALIST, &hcalist);
346*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
347*12075SSusan.Gleeson@Sun.COM 		nvlist_free(cfgnv);
348*12075SSusan.Gleeson@Sun.COM 		return (ret);
349*12075SSusan.Gleeson@Sun.COM 	}
350*12075SSusan.Gleeson@Sun.COM 
351*12075SSusan.Gleeson@Sun.COM 	/*
352*12075SSusan.Gleeson@Sun.COM 	 * Find the default, for the likely case that this HCA isn't
353*12075SSusan.Gleeson@Sun.COM 	 * explicitly set.
354*12075SSusan.Gleeson@Sun.COM 	 */
355*12075SSusan.Gleeson@Sun.COM 	(void) nvlist_lookup_boolean_value(cfgnv, SRPT_PROP_DEFAULT_ENABLED,
356*12075SSusan.Gleeson@Sun.COM 	    &defaultState);
357*12075SSusan.Gleeson@Sun.COM 
358*12075SSusan.Gleeson@Sun.COM 	ret = nvlist_lookup_nvlist(hcalist, guid, &hcanv);
359*12075SSusan.Gleeson@Sun.COM 	if (ret == 0) {
360*12075SSusan.Gleeson@Sun.COM 		ret = nvlist_lookup_boolean_value(hcanv, SRPT_PROP_ENABLED,
361*12075SSusan.Gleeson@Sun.COM 		    enabled);
362*12075SSusan.Gleeson@Sun.COM 	}
363*12075SSusan.Gleeson@Sun.COM 
364*12075SSusan.Gleeson@Sun.COM 	if (ret == ENOENT) {
365*12075SSusan.Gleeson@Sun.COM 		/* not explicitly set, use the default */
366*12075SSusan.Gleeson@Sun.COM 		*enabled = defaultState;
367*12075SSusan.Gleeson@Sun.COM 		ret = 0;
368*12075SSusan.Gleeson@Sun.COM 	}
369*12075SSusan.Gleeson@Sun.COM 
370*12075SSusan.Gleeson@Sun.COM 	nvlist_free(cfgnv);
371*12075SSusan.Gleeson@Sun.COM 
372*12075SSusan.Gleeson@Sun.COM 	return (ret);
373*12075SSusan.Gleeson@Sun.COM 
374*12075SSusan.Gleeson@Sun.COM }
375*12075SSusan.Gleeson@Sun.COM 
376*12075SSusan.Gleeson@Sun.COM /*
377*12075SSusan.Gleeson@Sun.COM  * Function:  srpt_ResetTarget()
378*12075SSusan.Gleeson@Sun.COM  *
379*12075SSusan.Gleeson@Sun.COM  * Clears the HCA-specific configuration.  Target creation will revert to
380*12075SSusan.Gleeson@Sun.COM  * the default.
381*12075SSusan.Gleeson@Sun.COM  *
382*12075SSusan.Gleeson@Sun.COM  * Parameters:
383*12075SSusan.Gleeson@Sun.COM  *    hca_guid	HCA GUID.  See description of srpt_NormalizeGuid
384*12075SSusan.Gleeson@Sun.COM  *
385*12075SSusan.Gleeson@Sun.COM  * Return Values:
386*12075SSusan.Gleeson@Sun.COM  *    0		Success
387*12075SSusan.Gleeson@Sun.COM  *    ENOMEM	Could not allocate resources
388*12075SSusan.Gleeson@Sun.COM  *    EINVAL	Invalid parameter
389*12075SSusan.Gleeson@Sun.COM  */
390*12075SSusan.Gleeson@Sun.COM int
srpt_ResetTarget(char * hca_guid)391*12075SSusan.Gleeson@Sun.COM srpt_ResetTarget(char *hca_guid)
392*12075SSusan.Gleeson@Sun.COM {
393*12075SSusan.Gleeson@Sun.COM 	int		ret;
394*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*cfgnv;
395*12075SSusan.Gleeson@Sun.COM 	nvlist_t	*hcalist;
396*12075SSusan.Gleeson@Sun.COM 	uint64_t	token;
397*12075SSusan.Gleeson@Sun.COM 	char		guid[32];
398*12075SSusan.Gleeson@Sun.COM 
399*12075SSusan.Gleeson@Sun.COM 	if (hca_guid == NULL) {
400*12075SSusan.Gleeson@Sun.COM 		return (EINVAL);
401*12075SSusan.Gleeson@Sun.COM 	}
402*12075SSusan.Gleeson@Sun.COM 
403*12075SSusan.Gleeson@Sun.COM 	ret = srpt_NormalizeGuid(hca_guid, guid, sizeof (guid), NULL);
404*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
405*12075SSusan.Gleeson@Sun.COM 		return (ret);
406*12075SSusan.Gleeson@Sun.COM 	}
407*12075SSusan.Gleeson@Sun.COM 
408*12075SSusan.Gleeson@Sun.COM 	ret = srpt_GetConfig(&cfgnv, &token);
409*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
410*12075SSusan.Gleeson@Sun.COM 		return (ret);
411*12075SSusan.Gleeson@Sun.COM 	}
412*12075SSusan.Gleeson@Sun.COM 
413*12075SSusan.Gleeson@Sun.COM 	/* get the list of HCAs */
414*12075SSusan.Gleeson@Sun.COM 	ret = nvlist_lookup_nvlist(cfgnv, SRPT_PROP_HCALIST, &hcalist);
415*12075SSusan.Gleeson@Sun.COM 	if (ret != 0) {
416*12075SSusan.Gleeson@Sun.COM 		nvlist_free(cfgnv);
417*12075SSusan.Gleeson@Sun.COM 		return (ret);
418*12075SSusan.Gleeson@Sun.COM 	}
419*12075SSusan.Gleeson@Sun.COM 
420*12075SSusan.Gleeson@Sun.COM 	/* don't set config if we don't actually change anything */
421*12075SSusan.Gleeson@Sun.COM 	if (nvlist_exists(hcalist, guid)) {
422*12075SSusan.Gleeson@Sun.COM 		(void) nvlist_remove_all(hcalist, guid);
423*12075SSusan.Gleeson@Sun.COM 
424*12075SSusan.Gleeson@Sun.COM 		if (ret == 0) {
425*12075SSusan.Gleeson@Sun.COM 			ret = srpt_SetConfig(cfgnv, token);
426*12075SSusan.Gleeson@Sun.COM 		}
427*12075SSusan.Gleeson@Sun.COM 	}
428*12075SSusan.Gleeson@Sun.COM 
429*12075SSusan.Gleeson@Sun.COM 	nvlist_free(cfgnv);
430*12075SSusan.Gleeson@Sun.COM 
431*12075SSusan.Gleeson@Sun.COM 	return (ret);
432*12075SSusan.Gleeson@Sun.COM }
433*12075SSusan.Gleeson@Sun.COM 
434*12075SSusan.Gleeson@Sun.COM /*
435*12075SSusan.Gleeson@Sun.COM  * srpt_NormalizeGuid()
436*12075SSusan.Gleeson@Sun.COM  *
437*12075SSusan.Gleeson@Sun.COM  * Parameters:
438*12075SSusan.Gleeson@Sun.COM  *    in	HCA GUID.  Must be in one of the following forms:
439*12075SSusan.Gleeson@Sun.COM  *		    3BA000100CD18	- base hex form
440*12075SSusan.Gleeson@Sun.COM  *		    0003BA000100CD18	- base hex form with leading zeroes
441*12075SSusan.Gleeson@Sun.COM  *		    hca:3BA000100CD18	- form from cfgadm and/or /dev/cfg
442*12075SSusan.Gleeson@Sun.COM  *		    eui.0003BA000100CD18 - EUI form
443*12075SSusan.Gleeson@Sun.COM  *
444*12075SSusan.Gleeson@Sun.COM  *    buf	Buffer to hold normalized guid string.  Must be at least
445*12075SSusan.Gleeson@Sun.COM  *		17 chars long.
446*12075SSusan.Gleeson@Sun.COM  *    buflen	Length of provided buffer
447*12075SSusan.Gleeson@Sun.COM  *    int_guid	Optional.  If not NULL, the integer form of the GUID will also
448*12075SSusan.Gleeson@Sun.COM  *		be returned.
449*12075SSusan.Gleeson@Sun.COM  * Return Values:
450*12075SSusan.Gleeson@Sun.COM  *    0		Success
451*12075SSusan.Gleeson@Sun.COM  *    EINVAL	Invalid HCA GUID or invalid parameter.
452*12075SSusan.Gleeson@Sun.COM  */
453*12075SSusan.Gleeson@Sun.COM int
srpt_NormalizeGuid(char * in,char * buf,size_t buflen,uint64_t * int_guid)454*12075SSusan.Gleeson@Sun.COM srpt_NormalizeGuid(char *in, char *buf, size_t buflen, uint64_t *int_guid)
455*12075SSusan.Gleeson@Sun.COM {
456*12075SSusan.Gleeson@Sun.COM 	uint64_t	guid;
457*12075SSusan.Gleeson@Sun.COM 	char		*bufp = in;
458*12075SSusan.Gleeson@Sun.COM 	char		*end = NULL;
459*12075SSusan.Gleeson@Sun.COM 
460*12075SSusan.Gleeson@Sun.COM 	if ((in == NULL) || (buf == NULL)) {
461*12075SSusan.Gleeson@Sun.COM 		return (EINVAL);
462*12075SSusan.Gleeson@Sun.COM 	}
463*12075SSusan.Gleeson@Sun.COM 
464*12075SSusan.Gleeson@Sun.COM 	if (strncasecmp(bufp, "eui.", 4) == 0) {
465*12075SSusan.Gleeson@Sun.COM 		/* EUI form */
466*12075SSusan.Gleeson@Sun.COM 		bufp += 4;
467*12075SSusan.Gleeson@Sun.COM 	} else if (strncasecmp(bufp, "hca:", 4) == 0) {
468*12075SSusan.Gleeson@Sun.COM 		/* cfgadm and /dev/hca form */
469*12075SSusan.Gleeson@Sun.COM 		bufp += 4;
470*12075SSusan.Gleeson@Sun.COM 	}
471*12075SSusan.Gleeson@Sun.COM 
472*12075SSusan.Gleeson@Sun.COM 	/*
473*12075SSusan.Gleeson@Sun.COM 	 * strtoull() does not return EINVAL as documented.  Lucky
474*12075SSusan.Gleeson@Sun.COM 	 * for us, neither 0 nor ULLONG_MAX will be valid.  Trap on
475*12075SSusan.Gleeson@Sun.COM 	 * those and fail.
476*12075SSusan.Gleeson@Sun.COM 	 */
477*12075SSusan.Gleeson@Sun.COM 	guid = strtoull(bufp, &end, 16);
478*12075SSusan.Gleeson@Sun.COM 	if ((guid == 0) || (guid == ULLONG_MAX) ||
479*12075SSusan.Gleeson@Sun.COM 	    ((end != NULL) && (strlen(end) > 0))) {
480*12075SSusan.Gleeson@Sun.COM 		return (EINVAL);
481*12075SSusan.Gleeson@Sun.COM 	}
482*12075SSusan.Gleeson@Sun.COM 
483*12075SSusan.Gleeson@Sun.COM #if 0
484*12075SSusan.Gleeson@Sun.COM 	(void) snprintf(buf, buflen, "%llX", guid);
485*12075SSusan.Gleeson@Sun.COM #endif
486*12075SSusan.Gleeson@Sun.COM 	SRPT_FORMAT_HCAKEY(buf, buflen, guid);
487*12075SSusan.Gleeson@Sun.COM 
488*12075SSusan.Gleeson@Sun.COM 	if (int_guid) {
489*12075SSusan.Gleeson@Sun.COM 		*int_guid = guid;
490*12075SSusan.Gleeson@Sun.COM 	}
491*12075SSusan.Gleeson@Sun.COM 
492*12075SSusan.Gleeson@Sun.COM 	return (0);
493*12075SSusan.Gleeson@Sun.COM }
494