4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/types.h>
35 #include <libnvpair.h>
40 #include "srpt_common.h"
42 #define SRPT_PROV_NAME "srpt"
45 * Function: srpt_GetConfig()
48 * cfg Current SRPT configuration in nvlist form
49 * token Configuration generation number. Use this token
50 * if updating the configuration with srpt_SetConfig.
54 * ENOMEM Could not allocate resources
55 * EINVAL Invalid parameter
58 srpt_GetConfig(nvlist_t
**cfg
, uint64_t *token
)
61 nvlist_t
*cfg_nv
= NULL
;
62 uint64_t stmf_token
= 0;
63 nvlist_t
*hcanv
= NULL
;
71 ret
= stmfGetProviderDataProt(SRPT_PROV_NAME
, &cfg_nv
,
72 STMF_PORT_PROVIDER_TYPE
, &stmf_token
);
74 if (ret
== STMF_STATUS_SUCCESS
) {
76 } else if (ret
== STMF_ERROR_NOT_FOUND
) {
77 /* Not initialized yet */
78 ret
= nvlist_alloc(&cfg_nv
, NV_UNIQUE_NAME
, 0);
82 /* create the HCA list */
83 ret
= nvlist_alloc(&hcanv
, NV_UNIQUE_NAME
, 0);
85 ret
= nvlist_add_nvlist(cfg_nv
, SRPT_PROP_HCALIST
,
95 } else if (ret
== STMF_ERROR_NOMEM
) {
108 * Function: srpt_SetConfig()
111 * cfg SRPT configuration in nvlist form
112 * token Configuration generation number from srpt_GetConfig.
113 * Use this token to ensure the configuration hasn't been
114 * updated by another user since the time it was fetched.
118 * ENOMEM Could not allocate resources
119 * EINVAL Invalid parameter
120 * ECANCELED Configuration updated by another user
123 srpt_SetConfig(nvlist_t
*cfg
, uint64_t token
)
127 ret
= stmfSetProviderDataProt(SRPT_PROV_NAME
, cfg
,
128 STMF_PORT_PROVIDER_TYPE
, &token
);
130 if (ret
== STMF_STATUS_SUCCESS
) {
132 } else if (ret
== STMF_ERROR_NOMEM
) {
134 } else if (ret
== STMF_ERROR_PROV_DATA_STALE
) {
135 ret
= ECANCELED
; /* could be a better errno */
144 * Function: srpt_GetDefaultState()
147 * enabled If B_TRUE, indicates that targets will be created for all
148 * discovered HCAs that have not been specifically disabled.
149 * If B_FALSE, targets will not be created unless the HCA has
150 * been specifically enabled. See also srpt_SetDefaultState().
154 * ENOMEM Could not allocate resources
155 * EINVAL Invalid parameter
158 srpt_GetDefaultState(boolean_t
*enabled
)
163 boolean_t val
= B_TRUE
;
165 if (enabled
== NULL
) {
169 ret
= srpt_GetConfig(&cfgnv
, &token
);
175 ret
= nvlist_lookup_boolean_value(cfgnv
,
176 SRPT_PROP_DEFAULT_ENABLED
, &val
);
188 * Function: srpt_SetDefaultState()
191 * enabled If B_TRUE, indicates that targets will be created for all
192 * discovered HCAs that have not been specifically disabled.
193 * If B_FALSE, targets will not be created unless the HCA has
194 * been specifically enabled. See also srpt_SetDefaultState().
198 * ENOMEM Could not allocate resources
199 * EINVAL Invalid parameter
202 srpt_SetDefaultState(boolean_t enabled
)
208 ret
= srpt_GetConfig(&cfgnv
, &token
);
214 ret
= nvlist_alloc(&cfgnv
, NV_UNIQUE_NAME
, 0);
220 ret
= nvlist_add_boolean_value(cfgnv
, SRPT_PROP_DEFAULT_ENABLED
,
224 ret
= srpt_SetConfig(cfgnv
, token
);
233 * Function: srpt_SetTargetState()
236 * hca_guid HCA GUID. See description of srpt_NormalizeGuid
237 * enabled If B_TRUE, indicates that a target will be created for
238 * this HCA when the SRPT SMF service is enabled. If B_FALSE,
239 * a target will not be created
243 * ENOMEM Could not allocate resources
244 * EINVAL Invalid parameter
247 srpt_SetTargetState(char *hca_guid
, boolean_t enabled
)
257 if (hca_guid
== NULL
) {
261 ret
= srpt_NormalizeGuid(hca_guid
, guid
, sizeof (guid
), &hcaguid
);
266 ret
= srpt_GetConfig(&cfgnv
, &token
);
271 /* get the list of HCAs */
272 ret
= nvlist_lookup_nvlist(cfgnv
, SRPT_PROP_HCALIST
, &hcalist
);
278 ret
= nvlist_lookup_nvlist(hcalist
, guid
, &hcanv
);
281 ret
= nvlist_alloc(&hcanv
, NV_UNIQUE_NAME
, 0);
283 ret
= nvlist_add_uint64(hcanv
, SRPT_PROP_GUID
, hcaguid
);
288 ret
= nvlist_add_boolean_value(hcanv
, SRPT_PROP_ENABLED
,
293 ret
= nvlist_add_nvlist(hcalist
, guid
, hcanv
);
297 ret
= srpt_SetConfig(cfgnv
, token
);
306 * Function: srpt_GetTargetState()
309 * hca_guid HCA GUID. See description of srpt_NormalizeGuid
310 * enabled If B_TRUE, indicates that a target will be created for
311 * this HCA when the SRPT SMF service is enabled. If B_FALSE,
312 * a target will not be created
316 * ENOMEM Could not allocate resources
317 * EINVAL Invalid parameter
320 srpt_GetTargetState(char *hca_guid
, boolean_t
*enabled
)
327 boolean_t defaultState
= B_TRUE
;
330 if (hca_guid
== NULL
) {
334 ret
= srpt_NormalizeGuid(hca_guid
, guid
, sizeof (guid
), NULL
);
339 ret
= srpt_GetConfig(&cfgnv
, &token
);
344 /* get the list of HCAs */
345 ret
= nvlist_lookup_nvlist(cfgnv
, SRPT_PROP_HCALIST
, &hcalist
);
352 * Find the default, for the likely case that this HCA isn't
355 (void) nvlist_lookup_boolean_value(cfgnv
, SRPT_PROP_DEFAULT_ENABLED
,
358 ret
= nvlist_lookup_nvlist(hcalist
, guid
, &hcanv
);
360 ret
= nvlist_lookup_boolean_value(hcanv
, SRPT_PROP_ENABLED
,
365 /* not explicitly set, use the default */
366 *enabled
= defaultState
;
377 * Function: srpt_ResetTarget()
379 * Clears the HCA-specific configuration. Target creation will revert to
383 * hca_guid HCA GUID. See description of srpt_NormalizeGuid
387 * ENOMEM Could not allocate resources
388 * EINVAL Invalid parameter
391 srpt_ResetTarget(char *hca_guid
)
399 if (hca_guid
== NULL
) {
403 ret
= srpt_NormalizeGuid(hca_guid
, guid
, sizeof (guid
), NULL
);
408 ret
= srpt_GetConfig(&cfgnv
, &token
);
413 /* get the list of HCAs */
414 ret
= nvlist_lookup_nvlist(cfgnv
, SRPT_PROP_HCALIST
, &hcalist
);
420 /* don't set config if we don't actually change anything */
421 if (nvlist_exists(hcalist
, guid
)) {
422 (void) nvlist_remove_all(hcalist
, guid
);
425 ret
= srpt_SetConfig(cfgnv
, token
);
435 * srpt_NormalizeGuid()
438 * in HCA GUID. Must be in one of the following forms:
439 * 3BA000100CD18 - base hex form
440 * 0003BA000100CD18 - base hex form with leading zeroes
441 * hca:3BA000100CD18 - form from cfgadm and/or /dev/cfg
442 * eui.0003BA000100CD18 - EUI form
444 * buf Buffer to hold normalized guid string. Must be at least
446 * buflen Length of provided buffer
447 * int_guid Optional. If not NULL, the integer form of the GUID will also
451 * EINVAL Invalid HCA GUID or invalid parameter.
454 srpt_NormalizeGuid(char *in
, char *buf
, size_t buflen
, uint64_t *int_guid
)
460 if ((in
== NULL
) || (buf
== NULL
)) {
464 if (strncasecmp(bufp
, "eui.", 4) == 0) {
467 } else if (strncasecmp(bufp
, "hca:", 4) == 0) {
468 /* cfgadm and /dev/hca form */
473 * strtoull() does not return EINVAL as documented. Lucky
474 * for us, neither 0 nor ULLONG_MAX will be valid. Trap on
477 guid
= strtoull(bufp
, &end
, 16);
478 if ((guid
== 0) || (guid
== ULLONG_MAX
) ||
479 ((end
!= NULL
) && (strlen(end
) > 0))) {
484 (void) snprintf(buf
, buflen
, "%llX", guid
);
486 SRPT_FORMAT_HCAKEY(buf
, buflen
, guid
);