17836SJohn.Forte@Sun.COM /*
27836SJohn.Forte@Sun.COM * CDDL HEADER START
37836SJohn.Forte@Sun.COM *
47836SJohn.Forte@Sun.COM * The contents of this file are subject to the terms of the
57836SJohn.Forte@Sun.COM * Common Development and Distribution License (the "License").
67836SJohn.Forte@Sun.COM * You may not use this file except in compliance with the License.
77836SJohn.Forte@Sun.COM *
87836SJohn.Forte@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97836SJohn.Forte@Sun.COM * or http://www.opensolaris.org/os/licensing.
107836SJohn.Forte@Sun.COM * See the License for the specific language governing permissions
117836SJohn.Forte@Sun.COM * and limitations under the License.
127836SJohn.Forte@Sun.COM *
137836SJohn.Forte@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
147836SJohn.Forte@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157836SJohn.Forte@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
167836SJohn.Forte@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
177836SJohn.Forte@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
187836SJohn.Forte@Sun.COM *
197836SJohn.Forte@Sun.COM * CDDL HEADER END
207836SJohn.Forte@Sun.COM */
217836SJohn.Forte@Sun.COM /*
22*10275SReed.Liu@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237836SJohn.Forte@Sun.COM * Use is subject to license terms.
247836SJohn.Forte@Sun.COM */
257836SJohn.Forte@Sun.COM
267836SJohn.Forte@Sun.COM #include <libscf.h>
277836SJohn.Forte@Sun.COM #include <stdio.h>
287836SJohn.Forte@Sun.COM #include <stdlib.h>
297836SJohn.Forte@Sun.COM #include <errno.h>
307836SJohn.Forte@Sun.COM #include <syslog.h>
317836SJohn.Forte@Sun.COM #include <strings.h>
327836SJohn.Forte@Sun.COM #include <ctype.h>
337836SJohn.Forte@Sun.COM #include <fcinfo.h>
347836SJohn.Forte@Sun.COM
357836SJohn.Forte@Sun.COM
367836SJohn.Forte@Sun.COM #define FCADM_RETRY_TIMES 10
377836SJohn.Forte@Sun.COM #define FCADM_SLEEP_TIME 1
387836SJohn.Forte@Sun.COM
397836SJohn.Forte@Sun.COM static char *
WWN2str(char * buf,HBA_WWN * wwn)407836SJohn.Forte@Sun.COM WWN2str(char *buf, HBA_WWN *wwn) {
417836SJohn.Forte@Sun.COM int j;
427836SJohn.Forte@Sun.COM unsigned char *pc = (unsigned char *)&(wwn->wwn[0]);
437836SJohn.Forte@Sun.COM buf[0] = '\0';
447836SJohn.Forte@Sun.COM for (j = 0; j < 16; j += 2) {
457836SJohn.Forte@Sun.COM sprintf(&buf[j], "%02X", (int)*pc++);
467836SJohn.Forte@Sun.COM }
477836SJohn.Forte@Sun.COM return (buf);
487836SJohn.Forte@Sun.COM }
497836SJohn.Forte@Sun.COM
507836SJohn.Forte@Sun.COM static int
isValidWWN(char * wwn)517836SJohn.Forte@Sun.COM isValidWWN(char *wwn)
527836SJohn.Forte@Sun.COM {
537836SJohn.Forte@Sun.COM int index;
547836SJohn.Forte@Sun.COM
557836SJohn.Forte@Sun.COM if (wwn == NULL) {
567836SJohn.Forte@Sun.COM return (0);
577836SJohn.Forte@Sun.COM }
587836SJohn.Forte@Sun.COM
597836SJohn.Forte@Sun.COM if (strlen(wwn) != 16) {
607836SJohn.Forte@Sun.COM return (0);
617836SJohn.Forte@Sun.COM }
627836SJohn.Forte@Sun.COM
637836SJohn.Forte@Sun.COM for (index = 0; index < 16; index++) {
647836SJohn.Forte@Sun.COM if (isxdigit(wwn[index])) {
657836SJohn.Forte@Sun.COM continue;
667836SJohn.Forte@Sun.COM }
677836SJohn.Forte@Sun.COM return (0);
687836SJohn.Forte@Sun.COM }
697836SJohn.Forte@Sun.COM return (1);
707836SJohn.Forte@Sun.COM }
717836SJohn.Forte@Sun.COM
727836SJohn.Forte@Sun.COM
737836SJohn.Forte@Sun.COM /*
747836SJohn.Forte@Sun.COM * Initialize scf stmf service access
757836SJohn.Forte@Sun.COM * handle - returned handle
767836SJohn.Forte@Sun.COM * service - returned service handle
777836SJohn.Forte@Sun.COM */
787836SJohn.Forte@Sun.COM static int
cfgInit(scf_handle_t ** handle,scf_service_t ** service)797836SJohn.Forte@Sun.COM cfgInit(scf_handle_t **handle, scf_service_t **service)
807836SJohn.Forte@Sun.COM {
817836SJohn.Forte@Sun.COM scf_scope_t *scope = NULL;
827836SJohn.Forte@Sun.COM int ret;
837836SJohn.Forte@Sun.COM
847836SJohn.Forte@Sun.COM if ((*handle = scf_handle_create(SCF_VERSION)) == NULL) {
857836SJohn.Forte@Sun.COM /* log error */
867836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
877836SJohn.Forte@Sun.COM goto err;
887836SJohn.Forte@Sun.COM }
897836SJohn.Forte@Sun.COM
907836SJohn.Forte@Sun.COM if (scf_handle_bind(*handle) == -1) {
917836SJohn.Forte@Sun.COM /* log error */
927836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
937836SJohn.Forte@Sun.COM goto err;
947836SJohn.Forte@Sun.COM }
957836SJohn.Forte@Sun.COM
967836SJohn.Forte@Sun.COM if ((*service = scf_service_create(*handle)) == NULL) {
977836SJohn.Forte@Sun.COM /* log error */
987836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
997836SJohn.Forte@Sun.COM goto err;
1007836SJohn.Forte@Sun.COM }
1017836SJohn.Forte@Sun.COM
1027836SJohn.Forte@Sun.COM if ((scope = scf_scope_create(*handle)) == NULL) {
1037836SJohn.Forte@Sun.COM /* log error */
1047836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
1057836SJohn.Forte@Sun.COM goto err;
1067836SJohn.Forte@Sun.COM }
1077836SJohn.Forte@Sun.COM
1087836SJohn.Forte@Sun.COM if (scf_handle_get_scope(*handle, SCF_SCOPE_LOCAL, scope) == -1) {
1097836SJohn.Forte@Sun.COM /* log error */
1107836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
1117836SJohn.Forte@Sun.COM goto err;
1127836SJohn.Forte@Sun.COM }
1137836SJohn.Forte@Sun.COM
1147836SJohn.Forte@Sun.COM if (scf_scope_get_service(scope, NPIV_SERVICE, *service) == -1) {
1157836SJohn.Forte@Sun.COM /* log error */
1167836SJohn.Forte@Sun.COM ret = NPIV_ERROR_SERVICE_NOT_FOUND;
1177836SJohn.Forte@Sun.COM goto err;
1187836SJohn.Forte@Sun.COM }
1197836SJohn.Forte@Sun.COM
1207836SJohn.Forte@Sun.COM scf_scope_destroy(scope);
1217836SJohn.Forte@Sun.COM
1227836SJohn.Forte@Sun.COM return (NPIV_SUCCESS);
1237836SJohn.Forte@Sun.COM
1247836SJohn.Forte@Sun.COM err:
1257836SJohn.Forte@Sun.COM if (*handle != NULL) {
1267836SJohn.Forte@Sun.COM scf_handle_destroy(*handle);
1277836SJohn.Forte@Sun.COM }
1287836SJohn.Forte@Sun.COM if (*service != NULL) {
1297836SJohn.Forte@Sun.COM scf_service_destroy(*service);
1307836SJohn.Forte@Sun.COM *service = NULL;
1317836SJohn.Forte@Sun.COM }
1327836SJohn.Forte@Sun.COM if (scope != NULL) {
1337836SJohn.Forte@Sun.COM scf_scope_destroy(scope);
1347836SJohn.Forte@Sun.COM }
1357836SJohn.Forte@Sun.COM return (ret);
1367836SJohn.Forte@Sun.COM }
1377836SJohn.Forte@Sun.COM
1387836SJohn.Forte@Sun.COM static int
npivAddRemoveNPIVEntry(char * ppwwn,char * vnwwn,char * vpwwn,int vindex,int addRemoveFlag)1397836SJohn.Forte@Sun.COM npivAddRemoveNPIVEntry(char *ppwwn, char *vnwwn,
1407836SJohn.Forte@Sun.COM char *vpwwn, int vindex, int addRemoveFlag) {
1417836SJohn.Forte@Sun.COM scf_handle_t *handle = NULL;
1427836SJohn.Forte@Sun.COM scf_service_t *svc = NULL;
1437836SJohn.Forte@Sun.COM scf_propertygroup_t *pg = NULL;
1447836SJohn.Forte@Sun.COM scf_transaction_t *tran = NULL;
1457836SJohn.Forte@Sun.COM scf_transaction_entry_t *entry = NULL;
1467836SJohn.Forte@Sun.COM scf_property_t *prop = NULL;
1477836SJohn.Forte@Sun.COM scf_value_t *valueLookup = NULL;
1487836SJohn.Forte@Sun.COM scf_iter_t *valueIter = NULL;
1497836SJohn.Forte@Sun.COM scf_value_t **valueSet = NULL;
1507836SJohn.Forte@Sun.COM int ret = NPIV_SUCCESS;
1517836SJohn.Forte@Sun.COM boolean_t createProp = B_FALSE;
1527836SJohn.Forte@Sun.COM int lastAlloc = 0;
1537836SJohn.Forte@Sun.COM char buf[NPIV_PORT_LIST_LENGTH] = {0};
1547836SJohn.Forte@Sun.COM char memberName[NPIV_PORT_LIST_LENGTH] = {0};
1557836SJohn.Forte@Sun.COM boolean_t found = B_FALSE;
1567836SJohn.Forte@Sun.COM int i = 0;
1577836SJohn.Forte@Sun.COM int valueArraySize = 0;
1587836SJohn.Forte@Sun.COM int commitRet;
1597836SJohn.Forte@Sun.COM
1607836SJohn.Forte@Sun.COM if (vnwwn) {
1617836SJohn.Forte@Sun.COM sprintf(memberName, "%s:%s:%s:%d", ppwwn, vpwwn, vnwwn, vindex);
1627836SJohn.Forte@Sun.COM } else {
1637836SJohn.Forte@Sun.COM sprintf(memberName, "%s:%s", ppwwn, vpwwn);
1647836SJohn.Forte@Sun.COM }
1657836SJohn.Forte@Sun.COM
1667836SJohn.Forte@Sun.COM ret = cfgInit(&handle, &svc);
1677836SJohn.Forte@Sun.COM if (ret != NPIV_SUCCESS) {
1687836SJohn.Forte@Sun.COM goto out;
1697836SJohn.Forte@Sun.COM }
1707836SJohn.Forte@Sun.COM
1717836SJohn.Forte@Sun.COM if (((pg = scf_pg_create(handle)) == NULL) ||
1727836SJohn.Forte@Sun.COM ((tran = scf_transaction_create(handle)) == NULL) ||
1737836SJohn.Forte@Sun.COM ((entry = scf_entry_create(handle)) == NULL) ||
1747836SJohn.Forte@Sun.COM ((prop = scf_property_create(handle)) == NULL) ||
1757836SJohn.Forte@Sun.COM ((valueIter = scf_iter_create(handle)) == NULL)) {
1767836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
1777836SJohn.Forte@Sun.COM goto out;
1787836SJohn.Forte@Sun.COM }
1797836SJohn.Forte@Sun.COM
1807836SJohn.Forte@Sun.COM /* get property group or create it */
1817836SJohn.Forte@Sun.COM if (scf_service_get_pg(svc, NPIV_PG_NAME, pg) == -1) {
1827836SJohn.Forte@Sun.COM if ((scf_error() == SCF_ERROR_NOT_FOUND) &&
1837836SJohn.Forte@Sun.COM (addRemoveFlag == NPIV_ADD)) {
1847836SJohn.Forte@Sun.COM if (scf_service_add_pg(svc, NPIV_PG_NAME,
1857836SJohn.Forte@Sun.COM SCF_GROUP_APPLICATION, 0, pg) == -1) {
1867836SJohn.Forte@Sun.COM syslog(LOG_ERR, "add pg failed - %s",
1877836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
1887836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
1897836SJohn.Forte@Sun.COM } else {
1907836SJohn.Forte@Sun.COM createProp = B_TRUE;
1917836SJohn.Forte@Sun.COM }
1927836SJohn.Forte@Sun.COM } else if (scf_error() == SCF_ERROR_NOT_FOUND) {
1937836SJohn.Forte@Sun.COM ret = NPIV_ERROR_NOT_FOUND;
1947836SJohn.Forte@Sun.COM } else {
1957836SJohn.Forte@Sun.COM syslog(LOG_ERR, "get pg failed - %s",
1967836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
1977836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
1987836SJohn.Forte@Sun.COM }
1997836SJohn.Forte@Sun.COM if (ret != NPIV_SUCCESS) {
2007836SJohn.Forte@Sun.COM goto out;
2017836SJohn.Forte@Sun.COM }
2027836SJohn.Forte@Sun.COM }
2037836SJohn.Forte@Sun.COM
2047836SJohn.Forte@Sun.COM /* Begin the transaction */
2057836SJohn.Forte@Sun.COM if (scf_transaction_start(tran, pg) == -1) {
2067836SJohn.Forte@Sun.COM syslog(LOG_ERR, "start transaction failed - %s",
2077836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
2087836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
2097836SJohn.Forte@Sun.COM goto out;
2107836SJohn.Forte@Sun.COM }
2117836SJohn.Forte@Sun.COM
2127836SJohn.Forte@Sun.COM valueSet = (scf_value_t **)calloc(1, sizeof (*valueSet)
2137836SJohn.Forte@Sun.COM * (lastAlloc = PORT_LIST_ALLOC));
2147836SJohn.Forte@Sun.COM if (valueSet == NULL) {
2157836SJohn.Forte@Sun.COM ret = NPIV_ERROR_NOMEM;
2167836SJohn.Forte@Sun.COM goto out;
2177836SJohn.Forte@Sun.COM }
2187836SJohn.Forte@Sun.COM
2197836SJohn.Forte@Sun.COM if (createProp) {
2207836SJohn.Forte@Sun.COM if (scf_transaction_property_new(tran, entry, NPIV_PORT_LIST,
2217836SJohn.Forte@Sun.COM SCF_TYPE_USTRING) == -1) {
2227836SJohn.Forte@Sun.COM if (scf_error() == SCF_ERROR_EXISTS) {
2237836SJohn.Forte@Sun.COM ret = NPIV_ERROR_EXISTS;
2247836SJohn.Forte@Sun.COM } else {
2257836SJohn.Forte@Sun.COM syslog(LOG_ERR,
2267836SJohn.Forte@Sun.COM "transaction property new failed - %s",
2277836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
2287836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
2297836SJohn.Forte@Sun.COM }
2307836SJohn.Forte@Sun.COM goto out;
2317836SJohn.Forte@Sun.COM }
2327836SJohn.Forte@Sun.COM } else {
2337836SJohn.Forte@Sun.COM if (scf_transaction_property_change(tran, entry,
2347836SJohn.Forte@Sun.COM NPIV_PORT_LIST, SCF_TYPE_USTRING) == -1) {
2357836SJohn.Forte@Sun.COM syslog(LOG_ERR,
2367836SJohn.Forte@Sun.COM "transaction property change failed - %s",
2377836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
2387836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
2397836SJohn.Forte@Sun.COM goto out;
2407836SJohn.Forte@Sun.COM }
2417836SJohn.Forte@Sun.COM
2427836SJohn.Forte@Sun.COM if (scf_pg_get_property(pg, NPIV_PORT_LIST, prop) == -1) {
2437836SJohn.Forte@Sun.COM syslog(LOG_ERR, "get property failed - %s",
2447836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
2457836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
2467836SJohn.Forte@Sun.COM goto out;
2477836SJohn.Forte@Sun.COM }
2487836SJohn.Forte@Sun.COM
2497836SJohn.Forte@Sun.COM valueLookup = scf_value_create(handle);
2507836SJohn.Forte@Sun.COM if (valueLookup == NULL) {
2517836SJohn.Forte@Sun.COM syslog(LOG_ERR, "scf value alloc failed - %s",
2527836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
2537836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
2547836SJohn.Forte@Sun.COM goto out;
2557836SJohn.Forte@Sun.COM }
2567836SJohn.Forte@Sun.COM
2577836SJohn.Forte@Sun.COM if (scf_iter_property_values(valueIter, prop) == -1) {
2587836SJohn.Forte@Sun.COM syslog(LOG_ERR, "iter value failed - %s",
2597836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
2607836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
2617836SJohn.Forte@Sun.COM goto out;
2627836SJohn.Forte@Sun.COM }
2637836SJohn.Forte@Sun.COM
2647836SJohn.Forte@Sun.COM while (scf_iter_next_value(valueIter, valueLookup) == 1) {
2657836SJohn.Forte@Sun.COM bzero(buf, sizeof (buf));
2667836SJohn.Forte@Sun.COM if (scf_value_get_ustring(valueLookup,
2677836SJohn.Forte@Sun.COM buf, MAXNAMELEN) == -1) {
2687836SJohn.Forte@Sun.COM syslog(LOG_ERR, "iter value failed - %s",
2697836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
2707836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
2717836SJohn.Forte@Sun.COM break;
2727836SJohn.Forte@Sun.COM }
2737836SJohn.Forte@Sun.COM
2747836SJohn.Forte@Sun.COM if ((strlen(buf) >= strlen(memberName)) &&
2757836SJohn.Forte@Sun.COM bcmp(buf, memberName, strlen(memberName)) == 0) {
2767836SJohn.Forte@Sun.COM if (addRemoveFlag == NPIV_ADD) {
2777836SJohn.Forte@Sun.COM ret = NPIV_ERROR_EXISTS;
2787836SJohn.Forte@Sun.COM break;
2797836SJohn.Forte@Sun.COM } else {
2807836SJohn.Forte@Sun.COM found = B_TRUE;
2817836SJohn.Forte@Sun.COM continue;
2827836SJohn.Forte@Sun.COM }
2837836SJohn.Forte@Sun.COM }
2847836SJohn.Forte@Sun.COM
2857836SJohn.Forte@Sun.COM valueSet[i] = scf_value_create(handle);
2867836SJohn.Forte@Sun.COM if (valueSet[i] == NULL) {
2877836SJohn.Forte@Sun.COM syslog(LOG_ERR, "scf value alloc failed - %s",
2887836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
2897836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
2907836SJohn.Forte@Sun.COM break;
2917836SJohn.Forte@Sun.COM }
2927836SJohn.Forte@Sun.COM
2937836SJohn.Forte@Sun.COM if (scf_value_set_ustring(valueSet[i], buf) == -1) {
2947836SJohn.Forte@Sun.COM syslog(LOG_ERR, "set value failed - %s",
2957836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
2967836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
2977836SJohn.Forte@Sun.COM break;
2987836SJohn.Forte@Sun.COM }
2997836SJohn.Forte@Sun.COM
3007836SJohn.Forte@Sun.COM if (scf_entry_add_value(entry, valueSet[i]) == -1) {
3017836SJohn.Forte@Sun.COM syslog(LOG_ERR, "add value failed - %s",
3027836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
3037836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
3047836SJohn.Forte@Sun.COM break;
3057836SJohn.Forte@Sun.COM }
3067836SJohn.Forte@Sun.COM
3077836SJohn.Forte@Sun.COM i++;
3087836SJohn.Forte@Sun.COM
3097836SJohn.Forte@Sun.COM if (i >= lastAlloc) {
3107836SJohn.Forte@Sun.COM lastAlloc += PORT_LIST_ALLOC;
3117836SJohn.Forte@Sun.COM valueSet = realloc(valueSet,
3127836SJohn.Forte@Sun.COM sizeof (*valueSet) * lastAlloc);
3137836SJohn.Forte@Sun.COM if (valueSet == NULL) {
3147836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
3157836SJohn.Forte@Sun.COM break;
3167836SJohn.Forte@Sun.COM }
3177836SJohn.Forte@Sun.COM }
3187836SJohn.Forte@Sun.COM }
3197836SJohn.Forte@Sun.COM }
3207836SJohn.Forte@Sun.COM
3217836SJohn.Forte@Sun.COM valueArraySize = i;
3227836SJohn.Forte@Sun.COM if (!found && (addRemoveFlag == NPIV_REMOVE)) {
3237836SJohn.Forte@Sun.COM ret = NPIV_ERROR_MEMBER_NOT_FOUND;
3247836SJohn.Forte@Sun.COM }
3257836SJohn.Forte@Sun.COM if (ret != NPIV_SUCCESS) {
3267836SJohn.Forte@Sun.COM goto out;
3277836SJohn.Forte@Sun.COM }
3287836SJohn.Forte@Sun.COM
3297836SJohn.Forte@Sun.COM if (addRemoveFlag == NPIV_ADD) {
3307836SJohn.Forte@Sun.COM /*
3317836SJohn.Forte@Sun.COM * Now create the new entry
3327836SJohn.Forte@Sun.COM */
3337836SJohn.Forte@Sun.COM valueSet[i] = scf_value_create(handle);
3347836SJohn.Forte@Sun.COM if (valueSet[i] == NULL) {
3357836SJohn.Forte@Sun.COM syslog(LOG_ERR, "scf value alloc failed - %s",
3367836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
3377836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
3387836SJohn.Forte@Sun.COM goto out;
3397836SJohn.Forte@Sun.COM } else {
3407836SJohn.Forte@Sun.COM valueArraySize++;
3417836SJohn.Forte@Sun.COM }
3427836SJohn.Forte@Sun.COM
3437836SJohn.Forte@Sun.COM /*
3447836SJohn.Forte@Sun.COM * Set the new member name
3457836SJohn.Forte@Sun.COM */
3467836SJohn.Forte@Sun.COM if (scf_value_set_ustring(valueSet[i], memberName) == -1) {
3477836SJohn.Forte@Sun.COM syslog(LOG_ERR, "set value failed - %s",
3487836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
3497836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
3507836SJohn.Forte@Sun.COM goto out;
3517836SJohn.Forte@Sun.COM }
3527836SJohn.Forte@Sun.COM
3537836SJohn.Forte@Sun.COM /*
3547836SJohn.Forte@Sun.COM * Add the new member
3557836SJohn.Forte@Sun.COM */
3567836SJohn.Forte@Sun.COM if (scf_entry_add_value(entry, valueSet[i]) == -1) {
3577836SJohn.Forte@Sun.COM syslog(LOG_ERR, "add value failed - %s",
3587836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
3597836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
3607836SJohn.Forte@Sun.COM goto out;
3617836SJohn.Forte@Sun.COM }
3627836SJohn.Forte@Sun.COM }
3637836SJohn.Forte@Sun.COM
3647836SJohn.Forte@Sun.COM if ((commitRet = scf_transaction_commit(tran)) != 1) {
3657836SJohn.Forte@Sun.COM syslog(LOG_ERR, "transaction commit failed - %s",
3667836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
3677836SJohn.Forte@Sun.COM if (commitRet == 0) {
3687836SJohn.Forte@Sun.COM ret = NPIV_ERROR_BUSY;
3697836SJohn.Forte@Sun.COM } else {
3707836SJohn.Forte@Sun.COM ret = NPIV_ERROR;
3717836SJohn.Forte@Sun.COM }
3727836SJohn.Forte@Sun.COM goto out;
3737836SJohn.Forte@Sun.COM }
3747836SJohn.Forte@Sun.COM
3757836SJohn.Forte@Sun.COM out:
3767836SJohn.Forte@Sun.COM /*
3777836SJohn.Forte@Sun.COM * Free resources
3787836SJohn.Forte@Sun.COM */
3797836SJohn.Forte@Sun.COM if (handle != NULL) {
3807836SJohn.Forte@Sun.COM scf_handle_destroy(handle);
3817836SJohn.Forte@Sun.COM }
3827836SJohn.Forte@Sun.COM if (svc != NULL) {
3837836SJohn.Forte@Sun.COM scf_service_destroy(svc);
3847836SJohn.Forte@Sun.COM }
3857836SJohn.Forte@Sun.COM if (pg != NULL) {
3867836SJohn.Forte@Sun.COM scf_pg_destroy(pg);
3877836SJohn.Forte@Sun.COM }
3887836SJohn.Forte@Sun.COM if (tran != NULL) {
3897836SJohn.Forte@Sun.COM scf_transaction_destroy(tran);
3907836SJohn.Forte@Sun.COM }
3917836SJohn.Forte@Sun.COM if (entry != NULL) {
3927836SJohn.Forte@Sun.COM scf_entry_destroy(entry);
3937836SJohn.Forte@Sun.COM }
3947836SJohn.Forte@Sun.COM if (prop != NULL) {
3957836SJohn.Forte@Sun.COM scf_property_destroy(prop);
3967836SJohn.Forte@Sun.COM }
3977836SJohn.Forte@Sun.COM if (valueIter != NULL) {
3987836SJohn.Forte@Sun.COM scf_iter_destroy(valueIter);
3997836SJohn.Forte@Sun.COM }
4007836SJohn.Forte@Sun.COM if (valueLookup != NULL) {
4017836SJohn.Forte@Sun.COM scf_value_destroy(valueLookup);
4027836SJohn.Forte@Sun.COM }
4037836SJohn.Forte@Sun.COM
4047836SJohn.Forte@Sun.COM /*
4057836SJohn.Forte@Sun.COM * Free valueSet scf resources
4067836SJohn.Forte@Sun.COM */
4077836SJohn.Forte@Sun.COM if (valueArraySize > 0) {
4087836SJohn.Forte@Sun.COM for (i = 0; i < valueArraySize; i++) {
4097836SJohn.Forte@Sun.COM scf_value_destroy(valueSet[i]);
4107836SJohn.Forte@Sun.COM }
4117836SJohn.Forte@Sun.COM }
4127836SJohn.Forte@Sun.COM /*
4137836SJohn.Forte@Sun.COM * Now free the pointer array to the resources
4147836SJohn.Forte@Sun.COM */
4157836SJohn.Forte@Sun.COM if (valueSet != NULL) {
4167836SJohn.Forte@Sun.COM free(valueSet);
4177836SJohn.Forte@Sun.COM }
4187836SJohn.Forte@Sun.COM
4197836SJohn.Forte@Sun.COM return (ret);
4207836SJohn.Forte@Sun.COM }
4217836SJohn.Forte@Sun.COM
4227836SJohn.Forte@Sun.COM static int
retrieveNPIVAttrs(HBA_HANDLE handle,HBA_WWN portWWN,HBA_PORTNPIVATTRIBUTES * npivattrs,HBA_UINT32 * portIndex)4237836SJohn.Forte@Sun.COM retrieveNPIVAttrs(HBA_HANDLE handle, HBA_WWN portWWN,
4247836SJohn.Forte@Sun.COM HBA_PORTNPIVATTRIBUTES *npivattrs, HBA_UINT32 *portIndex) {
4257836SJohn.Forte@Sun.COM HBA_STATUS status;
4267836SJohn.Forte@Sun.COM HBA_ADAPTERATTRIBUTES attrs;
4277836SJohn.Forte@Sun.COM HBA_PORTATTRIBUTES portattrs;
4287836SJohn.Forte@Sun.COM int portCtr;
4297836SJohn.Forte@Sun.COM int times = 0;
4307836SJohn.Forte@Sun.COM
4317836SJohn.Forte@Sun.COM /* argument checking */
4327836SJohn.Forte@Sun.COM if (npivattrs == NULL || portIndex == NULL) {
4337836SJohn.Forte@Sun.COM return (1);
4347836SJohn.Forte@Sun.COM }
4357836SJohn.Forte@Sun.COM
4367836SJohn.Forte@Sun.COM memset(&attrs, 0, sizeof (HBA_ADAPTERATTRIBUTES));
4377836SJohn.Forte@Sun.COM status = HBA_GetAdapterAttributes(handle, &attrs);
4387836SJohn.Forte@Sun.COM while ((status == HBA_STATUS_ERROR_TRY_AGAIN ||
4397836SJohn.Forte@Sun.COM status == HBA_STATUS_ERROR_BUSY) &&
4407836SJohn.Forte@Sun.COM times++ < 130) {
4417836SJohn.Forte@Sun.COM status = HBA_GetAdapterAttributes(handle, &attrs);
4427836SJohn.Forte@Sun.COM if (status == HBA_STATUS_OK) {
4437836SJohn.Forte@Sun.COM break;
4447836SJohn.Forte@Sun.COM }
4457836SJohn.Forte@Sun.COM (void) sleep(1);
4467836SJohn.Forte@Sun.COM }
4477836SJohn.Forte@Sun.COM if (status != HBA_STATUS_OK) {
4487836SJohn.Forte@Sun.COM return (1);
4497836SJohn.Forte@Sun.COM }
4507836SJohn.Forte@Sun.COM
4517836SJohn.Forte@Sun.COM memset(&portattrs, 0, sizeof (HBA_PORTATTRIBUTES));
4527836SJohn.Forte@Sun.COM for (portCtr = 0; portCtr < attrs.NumberOfPorts; portCtr++) {
4537836SJohn.Forte@Sun.COM status = HBA_GetAdapterPortAttributes(handle,
4547836SJohn.Forte@Sun.COM portCtr, &portattrs);
4557836SJohn.Forte@Sun.COM times = 0;
4567836SJohn.Forte@Sun.COM while ((status == HBA_STATUS_ERROR_TRY_AGAIN ||
4577836SJohn.Forte@Sun.COM status == HBA_STATUS_ERROR_BUSY) &&
4587836SJohn.Forte@Sun.COM times++ < HBA_MAX_RETRIES) {
4597836SJohn.Forte@Sun.COM status = HBA_GetAdapterPortAttributes(handle,
4607836SJohn.Forte@Sun.COM portCtr, &portattrs);
4617836SJohn.Forte@Sun.COM if (status == HBA_STATUS_OK) {
4627836SJohn.Forte@Sun.COM break;
4637836SJohn.Forte@Sun.COM }
4647836SJohn.Forte@Sun.COM (void) sleep(1);
4657836SJohn.Forte@Sun.COM }
4667836SJohn.Forte@Sun.COM
4677836SJohn.Forte@Sun.COM if (status != HBA_STATUS_OK) {
4687836SJohn.Forte@Sun.COM return (1);
4697836SJohn.Forte@Sun.COM }
4707836SJohn.Forte@Sun.COM
4717836SJohn.Forte@Sun.COM if (memcmp(portWWN.wwn, portattrs.PortWWN.wwn,
4727836SJohn.Forte@Sun.COM sizeof (portattrs.PortWWN.wwn)) == 0) {
4737836SJohn.Forte@Sun.COM break;
4747836SJohn.Forte@Sun.COM }
4757836SJohn.Forte@Sun.COM }
4767836SJohn.Forte@Sun.COM if (portCtr >= attrs.NumberOfPorts) {
4777836SJohn.Forte@Sun.COM *portIndex = 0;
4787836SJohn.Forte@Sun.COM return (1);
4797836SJohn.Forte@Sun.COM }
4807836SJohn.Forte@Sun.COM *portIndex = portCtr;
4817836SJohn.Forte@Sun.COM
4827836SJohn.Forte@Sun.COM status = Sun_HBA_GetPortNPIVAttributes(handle, portCtr, npivattrs);
4837836SJohn.Forte@Sun.COM times = 0;
4847836SJohn.Forte@Sun.COM while ((status == HBA_STATUS_ERROR_TRY_AGAIN ||
4857836SJohn.Forte@Sun.COM status == HBA_STATUS_ERROR_BUSY) &&
4867836SJohn.Forte@Sun.COM times++ < HBA_MAX_RETRIES) {
4877836SJohn.Forte@Sun.COM status = Sun_HBA_GetPortNPIVAttributes(handle,
4887836SJohn.Forte@Sun.COM portCtr, npivattrs);
4897836SJohn.Forte@Sun.COM if (status == HBA_STATUS_OK) {
4907836SJohn.Forte@Sun.COM break;
4917836SJohn.Forte@Sun.COM }
4927836SJohn.Forte@Sun.COM (void) sleep(1);
4937836SJohn.Forte@Sun.COM }
4947836SJohn.Forte@Sun.COM if (status != HBA_STATUS_OK) {
4957836SJohn.Forte@Sun.COM return (1);
4967836SJohn.Forte@Sun.COM }
4977836SJohn.Forte@Sun.COM
4987836SJohn.Forte@Sun.COM return (0);
4997836SJohn.Forte@Sun.COM }
5007836SJohn.Forte@Sun.COM
5017836SJohn.Forte@Sun.COM
5027836SJohn.Forte@Sun.COM int
fc_util_delete_npivport(int wwnCount,char ** wwn_argv,cmdOptions_t * options)5037836SJohn.Forte@Sun.COM fc_util_delete_npivport(int wwnCount, char **wwn_argv,
5047836SJohn.Forte@Sun.COM cmdOptions_t *options)
5057836SJohn.Forte@Sun.COM {
5067836SJohn.Forte@Sun.COM uint64_t physicalportWWN, virtualportWWN;
5077836SJohn.Forte@Sun.COM HBA_WWN portWWN, vportWWN;
5087836SJohn.Forte@Sun.COM HBA_STATUS status;
5097836SJohn.Forte@Sun.COM HBA_HANDLE handle;
5107836SJohn.Forte@Sun.COM HBA_PORTNPIVATTRIBUTES npivattrs;
5117836SJohn.Forte@Sun.COM HBA_UINT32 portIndex;
5127836SJohn.Forte@Sun.COM char pwwn[17];
5137836SJohn.Forte@Sun.COM int times;
5147836SJohn.Forte@Sun.COM
5157836SJohn.Forte@Sun.COM if (wwnCount != 1) {
5167836SJohn.Forte@Sun.COM fprintf(stderr,
5177836SJohn.Forte@Sun.COM gettext("Invalid Parameter\n"));
5187836SJohn.Forte@Sun.COM return (1);
5197836SJohn.Forte@Sun.COM }
5207836SJohn.Forte@Sun.COM
5217836SJohn.Forte@Sun.COM for (; options->optval; options++) {
5227836SJohn.Forte@Sun.COM switch (options->optval) {
5237836SJohn.Forte@Sun.COM case 'p':
5247836SJohn.Forte@Sun.COM if (!isValidWWN(options->optarg)) {
5257836SJohn.Forte@Sun.COM fprintf(stderr,
5267836SJohn.Forte@Sun.COM gettext("Invalid Port WWN\n"));
5277836SJohn.Forte@Sun.COM return (1);
5287836SJohn.Forte@Sun.COM }
5297836SJohn.Forte@Sun.COM sscanf(options->optarg, "%016llx", &virtualportWWN);
5307836SJohn.Forte@Sun.COM break;
5317836SJohn.Forte@Sun.COM default:
5327836SJohn.Forte@Sun.COM return (1);
5337836SJohn.Forte@Sun.COM }
5347836SJohn.Forte@Sun.COM }
5357836SJohn.Forte@Sun.COM
5367836SJohn.Forte@Sun.COM if (!isValidWWN(wwn_argv[0])) {
5377836SJohn.Forte@Sun.COM fprintf(stderr,
5387836SJohn.Forte@Sun.COM gettext("Invalid Physical Port WWN\n"));
5397836SJohn.Forte@Sun.COM return (1);
5407836SJohn.Forte@Sun.COM }
5417836SJohn.Forte@Sun.COM
5427836SJohn.Forte@Sun.COM if ((status = HBA_LoadLibrary()) != HBA_STATUS_OK) {
5437836SJohn.Forte@Sun.COM fprintf(stderr,
5447836SJohn.Forte@Sun.COM gettext("Failed to load FC-HBA common library\n"));
5457836SJohn.Forte@Sun.COM printStatus(status);
5467836SJohn.Forte@Sun.COM fprintf(stderr, "\n");
5477836SJohn.Forte@Sun.COM return (1);
5487836SJohn.Forte@Sun.COM }
5497836SJohn.Forte@Sun.COM sscanf(wwn_argv[0], "%016llx", &physicalportWWN);
5507836SJohn.Forte@Sun.COM physicalportWWN = htonll(physicalportWWN);
5517836SJohn.Forte@Sun.COM memcpy(portWWN.wwn, &physicalportWWN, sizeof (physicalportWWN));
5527836SJohn.Forte@Sun.COM
5537836SJohn.Forte@Sun.COM virtualportWWN = htonll(virtualportWWN);
5547836SJohn.Forte@Sun.COM memcpy(vportWWN.wwn, &virtualportWWN, sizeof (virtualportWWN));
5557836SJohn.Forte@Sun.COM
5567836SJohn.Forte@Sun.COM status = HBA_OpenAdapterByWWN(&handle, portWWN);
5577836SJohn.Forte@Sun.COM if (status != HBA_STATUS_OK) {
5587836SJohn.Forte@Sun.COM fprintf(stderr,
5597836SJohn.Forte@Sun.COM gettext("Error: HBA port %s: not found\n"),
5607836SJohn.Forte@Sun.COM wwn_argv[0]);
5617836SJohn.Forte@Sun.COM HBA_FreeLibrary();
5627836SJohn.Forte@Sun.COM return (1);
5637836SJohn.Forte@Sun.COM }
5647836SJohn.Forte@Sun.COM
5657836SJohn.Forte@Sun.COM /* Get physical port NPIV attributes */
5667836SJohn.Forte@Sun.COM if (retrieveNPIVAttrs(handle, portWWN, &npivattrs, &portIndex) == 0) {
5677836SJohn.Forte@Sun.COM /* Check port NPIV attributes */
5687836SJohn.Forte@Sun.COM if (npivattrs.MaxNumberOfNPIVPorts == 0) {
5697836SJohn.Forte@Sun.COM fprintf(stderr,
5707836SJohn.Forte@Sun.COM gettext("Error: NPIV not Supported\n"));
5717836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
5727836SJohn.Forte@Sun.COM HBA_FreeLibrary();
5737836SJohn.Forte@Sun.COM return (1);
5747836SJohn.Forte@Sun.COM }
5757836SJohn.Forte@Sun.COM
5767836SJohn.Forte@Sun.COM /* Delete a virtual port */
5777836SJohn.Forte@Sun.COM status = Sun_HBA_DeleteNPIVPort(handle, portIndex,
5787836SJohn.Forte@Sun.COM vportWWN);
5797836SJohn.Forte@Sun.COM times = 0;
5807836SJohn.Forte@Sun.COM while ((status == HBA_STATUS_ERROR_TRY_AGAIN ||
5817836SJohn.Forte@Sun.COM status == HBA_STATUS_ERROR_BUSY) &&
5827836SJohn.Forte@Sun.COM times++ < HBA_MAX_RETRIES) {
5837836SJohn.Forte@Sun.COM (void) sleep(1);
5847836SJohn.Forte@Sun.COM status = Sun_HBA_DeleteNPIVPort(handle, portIndex,
5857836SJohn.Forte@Sun.COM vportWWN);
5867836SJohn.Forte@Sun.COM if (status == HBA_STATUS_OK) {
5877836SJohn.Forte@Sun.COM break;
5887836SJohn.Forte@Sun.COM }
5897836SJohn.Forte@Sun.COM }
5907836SJohn.Forte@Sun.COM if (status != HBA_STATUS_OK) {
5917836SJohn.Forte@Sun.COM fprintf(stderr,
5927836SJohn.Forte@Sun.COM gettext("Error: failed to delete a npiv port\n"));
5937836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
5947836SJohn.Forte@Sun.COM HBA_FreeLibrary();
5957836SJohn.Forte@Sun.COM return (1);
5967836SJohn.Forte@Sun.COM }
5977836SJohn.Forte@Sun.COM } else {
5987836SJohn.Forte@Sun.COM fprintf(stderr,
5997836SJohn.Forte@Sun.COM gettext("Error: failed to get port NPIV attributes\n"));
6007836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
6017836SJohn.Forte@Sun.COM HBA_FreeLibrary();
6027836SJohn.Forte@Sun.COM return (1);
6037836SJohn.Forte@Sun.COM }
6047836SJohn.Forte@Sun.COM
6057836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
6067836SJohn.Forte@Sun.COM HBA_FreeLibrary();
6077836SJohn.Forte@Sun.COM
6087836SJohn.Forte@Sun.COM WWN2str(pwwn, &vportWWN);
6097836SJohn.Forte@Sun.COM npivAddRemoveNPIVEntry(wwn_argv[0],
6107836SJohn.Forte@Sun.COM NULL, pwwn, 0, NPIV_REMOVE);
6117836SJohn.Forte@Sun.COM
6127836SJohn.Forte@Sun.COM return (0);
6137836SJohn.Forte@Sun.COM }
6147836SJohn.Forte@Sun.COM
6157836SJohn.Forte@Sun.COM int
fc_util_create_npivport(int wwnCount,char ** wwn_argv,cmdOptions_t * options)6167836SJohn.Forte@Sun.COM fc_util_create_npivport(int wwnCount,
6177836SJohn.Forte@Sun.COM char **wwn_argv, cmdOptions_t *options)
6187836SJohn.Forte@Sun.COM {
6197836SJohn.Forte@Sun.COM uint64_t physicalportWWN, virtualnodeWWN, virtualportWWN;
6207836SJohn.Forte@Sun.COM HBA_WWN portWWN, vnodeWWN, vportWWN;
6217836SJohn.Forte@Sun.COM HBA_STATUS status;
6227836SJohn.Forte@Sun.COM HBA_HANDLE handle;
6237836SJohn.Forte@Sun.COM HBA_PORTNPIVATTRIBUTES npivattrs;
6247836SJohn.Forte@Sun.COM HBA_UINT32 portIndex;
6257836SJohn.Forte@Sun.COM HBA_UINT32 npivportIndex = 0;
6267836SJohn.Forte@Sun.COM char nwwn[17], pwwn[17];
6277836SJohn.Forte@Sun.COM int randomflag = 0;
6287836SJohn.Forte@Sun.COM int times;
6297836SJohn.Forte@Sun.COM
6307836SJohn.Forte@Sun.COM if (wwnCount != 1) {
6317836SJohn.Forte@Sun.COM fprintf(stderr,
6327836SJohn.Forte@Sun.COM gettext("Invalid Parameter\n"));
6337836SJohn.Forte@Sun.COM return (1);
6347836SJohn.Forte@Sun.COM }
6357836SJohn.Forte@Sun.COM
6367836SJohn.Forte@Sun.COM for (; options->optval; options++) {
6377836SJohn.Forte@Sun.COM switch (options->optval) {
6387836SJohn.Forte@Sun.COM case 'p':
6397836SJohn.Forte@Sun.COM if (!isValidWWN(options->optarg)) {
6407836SJohn.Forte@Sun.COM fprintf(stderr,
6417836SJohn.Forte@Sun.COM gettext("Invalid Port WWN\n"));
6427836SJohn.Forte@Sun.COM return (1);
6437836SJohn.Forte@Sun.COM }
6447836SJohn.Forte@Sun.COM sscanf(options->optarg, "%016llx", &virtualportWWN);
6457836SJohn.Forte@Sun.COM randomflag++;
6467836SJohn.Forte@Sun.COM break;
6477836SJohn.Forte@Sun.COM case 'n':
6487836SJohn.Forte@Sun.COM if (!isValidWWN(options->optarg)) {
6497836SJohn.Forte@Sun.COM fprintf(stderr,
6507836SJohn.Forte@Sun.COM gettext("Invalid Node WWN\n"));
6517836SJohn.Forte@Sun.COM return (1);
6527836SJohn.Forte@Sun.COM }
6537836SJohn.Forte@Sun.COM sscanf(options->optarg, "%016llx", &virtualnodeWWN);
6547836SJohn.Forte@Sun.COM randomflag++;
6557836SJohn.Forte@Sun.COM break;
6567836SJohn.Forte@Sun.COM default:
6577836SJohn.Forte@Sun.COM return (1);
6587836SJohn.Forte@Sun.COM }
6597836SJohn.Forte@Sun.COM }
6607836SJohn.Forte@Sun.COM
6617836SJohn.Forte@Sun.COM if (!isValidWWN(wwn_argv[0])) {
6627836SJohn.Forte@Sun.COM fprintf(stderr,
6637836SJohn.Forte@Sun.COM gettext("Invalid Physical Port WWN\n"));
6647836SJohn.Forte@Sun.COM wwnCount = 0;
6657836SJohn.Forte@Sun.COM return (1);
6667836SJohn.Forte@Sun.COM }
6677836SJohn.Forte@Sun.COM
6687836SJohn.Forte@Sun.COM if ((status = HBA_LoadLibrary()) != HBA_STATUS_OK) {
6697836SJohn.Forte@Sun.COM fprintf(stderr,
6707836SJohn.Forte@Sun.COM gettext("Failed to load FC-HBA common library\n"));
6717836SJohn.Forte@Sun.COM printStatus(status);
6727836SJohn.Forte@Sun.COM fprintf(stderr, "\n");
6737836SJohn.Forte@Sun.COM return (1);
6747836SJohn.Forte@Sun.COM }
6757836SJohn.Forte@Sun.COM
6767836SJohn.Forte@Sun.COM sscanf(wwn_argv[0], "%016llx", &physicalportWWN);
6777836SJohn.Forte@Sun.COM physicalportWWN = htonll(physicalportWWN);
6787836SJohn.Forte@Sun.COM memcpy(portWWN.wwn, &physicalportWWN, sizeof (physicalportWWN));
6797836SJohn.Forte@Sun.COM
6807836SJohn.Forte@Sun.COM status = HBA_OpenAdapterByWWN(&handle, portWWN);
6817836SJohn.Forte@Sun.COM if (status != HBA_STATUS_OK) {
6827836SJohn.Forte@Sun.COM fprintf(stderr,
6837836SJohn.Forte@Sun.COM gettext("Error: HBA port %s: not found\n"),
6847836SJohn.Forte@Sun.COM wwn_argv[0]);
6857836SJohn.Forte@Sun.COM HBA_FreeLibrary();
6867836SJohn.Forte@Sun.COM return (1);
6877836SJohn.Forte@Sun.COM }
6887836SJohn.Forte@Sun.COM
6897836SJohn.Forte@Sun.COM if (randomflag != 2) {
6907836SJohn.Forte@Sun.COM status = Sun_HBA_AdapterCreateWWN(handle, 0,
6917836SJohn.Forte@Sun.COM &vnodeWWN, &vportWWN, NULL, HBA_CREATE_WWN_RANDOM);
6927836SJohn.Forte@Sun.COM if (status != HBA_STATUS_OK) {
6937836SJohn.Forte@Sun.COM fprintf(stderr,
6947836SJohn.Forte@Sun.COM gettext("Error: Fail to get Random WWN\n"));
6957836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
6967836SJohn.Forte@Sun.COM HBA_FreeLibrary();
6977836SJohn.Forte@Sun.COM return (1);
6987836SJohn.Forte@Sun.COM }
6997836SJohn.Forte@Sun.COM } else {
7007836SJohn.Forte@Sun.COM virtualnodeWWN = htonll(virtualnodeWWN);
7017836SJohn.Forte@Sun.COM memcpy(vnodeWWN.wwn, &virtualnodeWWN, sizeof (virtualnodeWWN));
7027836SJohn.Forte@Sun.COM virtualportWWN = htonll(virtualportWWN);
7037836SJohn.Forte@Sun.COM memcpy(vportWWN.wwn, &virtualportWWN, sizeof (virtualportWWN));
7047836SJohn.Forte@Sun.COM }
7057836SJohn.Forte@Sun.COM
7067836SJohn.Forte@Sun.COM if (memcmp(vnodeWWN.wwn, vportWWN.wwn, 8) == 0) {
7077836SJohn.Forte@Sun.COM fprintf(stderr,
7087836SJohn.Forte@Sun.COM gettext("Error: Port WWN is same as Node WWN\n"));
7097836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
7107836SJohn.Forte@Sun.COM HBA_FreeLibrary();
7117836SJohn.Forte@Sun.COM return (1);
7127836SJohn.Forte@Sun.COM }
7137836SJohn.Forte@Sun.COM
7147836SJohn.Forte@Sun.COM /* Get physical port NPIV attributes */
7157836SJohn.Forte@Sun.COM if (retrieveNPIVAttrs(handle, portWWN, &npivattrs, &portIndex) == 0) {
7167836SJohn.Forte@Sun.COM /* Check port NPIV attributes */
7177836SJohn.Forte@Sun.COM if (npivattrs.MaxNumberOfNPIVPorts == 0) {
7187836SJohn.Forte@Sun.COM fprintf(stderr,
7197836SJohn.Forte@Sun.COM gettext("Error: NPIV not Supported\n"));
7207836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
7217836SJohn.Forte@Sun.COM HBA_FreeLibrary();
7227836SJohn.Forte@Sun.COM return (1);
7237836SJohn.Forte@Sun.COM }
7247836SJohn.Forte@Sun.COM if (npivattrs.MaxNumberOfNPIVPorts ==
7257836SJohn.Forte@Sun.COM npivattrs.NumberOfNPIVPorts) {
7267836SJohn.Forte@Sun.COM fprintf(stderr,
7277836SJohn.Forte@Sun.COM gettext("Error: Can not create more NPIV port\n"));
7287836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
7297836SJohn.Forte@Sun.COM HBA_FreeLibrary();
7307836SJohn.Forte@Sun.COM return (1);
7317836SJohn.Forte@Sun.COM }
7327836SJohn.Forte@Sun.COM
7337836SJohn.Forte@Sun.COM /* Create a virtual port */
7347836SJohn.Forte@Sun.COM status = Sun_HBA_CreateNPIVPort(handle, portIndex,
7357836SJohn.Forte@Sun.COM vnodeWWN, vportWWN, &npivportIndex);
7367836SJohn.Forte@Sun.COM times = 0;
7377836SJohn.Forte@Sun.COM while ((status == HBA_STATUS_ERROR_TRY_AGAIN ||
7387836SJohn.Forte@Sun.COM status == HBA_STATUS_ERROR_BUSY) &&
7397836SJohn.Forte@Sun.COM times++ < HBA_MAX_RETRIES) {
7407836SJohn.Forte@Sun.COM (void) sleep(1);
7417836SJohn.Forte@Sun.COM status = Sun_HBA_CreateNPIVPort(handle, portIndex,
7427836SJohn.Forte@Sun.COM vnodeWWN, vportWWN, &npivportIndex);
7437836SJohn.Forte@Sun.COM if (status == HBA_STATUS_OK) {
7447836SJohn.Forte@Sun.COM break;
7457836SJohn.Forte@Sun.COM }
7467836SJohn.Forte@Sun.COM }
7477836SJohn.Forte@Sun.COM
7487836SJohn.Forte@Sun.COM if (status != HBA_STATUS_OK) {
7497836SJohn.Forte@Sun.COM fprintf(stderr,
7507836SJohn.Forte@Sun.COM gettext("Error: failed to create a npiv port\n"));
7517836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
7527836SJohn.Forte@Sun.COM HBA_FreeLibrary();
7537836SJohn.Forte@Sun.COM return (1);
7547836SJohn.Forte@Sun.COM }
7557836SJohn.Forte@Sun.COM } else {
7567836SJohn.Forte@Sun.COM fprintf(stderr,
7577836SJohn.Forte@Sun.COM gettext("Error: failed to get port NPIV attributes\n"));
7587836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
7597836SJohn.Forte@Sun.COM HBA_FreeLibrary();
7607836SJohn.Forte@Sun.COM return (1);
7617836SJohn.Forte@Sun.COM }
7627836SJohn.Forte@Sun.COM
7637836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
7647836SJohn.Forte@Sun.COM HBA_FreeLibrary();
7657836SJohn.Forte@Sun.COM
7667836SJohn.Forte@Sun.COM WWN2str(nwwn, &vnodeWWN);
7677836SJohn.Forte@Sun.COM WWN2str(pwwn, &vportWWN);
7687836SJohn.Forte@Sun.COM npivAddRemoveNPIVEntry(wwn_argv[0],
7697836SJohn.Forte@Sun.COM nwwn, pwwn, npivportIndex, NPIV_ADD);
7707836SJohn.Forte@Sun.COM
7717836SJohn.Forte@Sun.COM return (0);
7727836SJohn.Forte@Sun.COM }
7737836SJohn.Forte@Sun.COM
7747836SJohn.Forte@Sun.COM int
create_npivport(char * ppwwn_str,char * vnwwn_str,char * vpwwn_str,int vindex)7757836SJohn.Forte@Sun.COM create_npivport(char *ppwwn_str, char *vnwwn_str,
7767836SJohn.Forte@Sun.COM char *vpwwn_str, int vindex)
7777836SJohn.Forte@Sun.COM {
7787836SJohn.Forte@Sun.COM uint64_t physicalportWWN, virtualnodeWWN, virtualportWWN;
7797836SJohn.Forte@Sun.COM HBA_WWN portWWN, vnodeWWN, vportWWN;
7807836SJohn.Forte@Sun.COM HBA_STATUS status;
7817836SJohn.Forte@Sun.COM HBA_HANDLE handle;
7827836SJohn.Forte@Sun.COM HBA_PORTNPIVATTRIBUTES npivattrs;
7837836SJohn.Forte@Sun.COM HBA_UINT32 portIndex;
7847836SJohn.Forte@Sun.COM HBA_UINT32 npivportIndex;
7857836SJohn.Forte@Sun.COM int times = 0;
7867836SJohn.Forte@Sun.COM
7877836SJohn.Forte@Sun.COM sscanf(ppwwn_str, "%016llx", &physicalportWWN);
7887836SJohn.Forte@Sun.COM physicalportWWN = htonll(physicalportWWN);
7897836SJohn.Forte@Sun.COM memcpy(portWWN.wwn, &physicalportWWN, sizeof (physicalportWWN));
7907836SJohn.Forte@Sun.COM sscanf(vnwwn_str, "%016llx", &virtualnodeWWN);
7917836SJohn.Forte@Sun.COM virtualnodeWWN = htonll(virtualnodeWWN);
7927836SJohn.Forte@Sun.COM memcpy(vnodeWWN.wwn, &virtualnodeWWN, sizeof (virtualnodeWWN));
7937836SJohn.Forte@Sun.COM sscanf(vpwwn_str, "%016llx", &virtualportWWN);
7947836SJohn.Forte@Sun.COM virtualportWWN = htonll(virtualportWWN);
7957836SJohn.Forte@Sun.COM memcpy(vportWWN.wwn, &virtualportWWN, sizeof (virtualportWWN));
7967836SJohn.Forte@Sun.COM npivportIndex = vindex;
7977836SJohn.Forte@Sun.COM
7987836SJohn.Forte@Sun.COM status = HBA_OpenAdapterByWWN(&handle, portWWN);
7997836SJohn.Forte@Sun.COM while (status == HBA_STATUS_ERROR_TRY_AGAIN ||
8007836SJohn.Forte@Sun.COM status == HBA_STATUS_ERROR_BUSY) {
8017836SJohn.Forte@Sun.COM (void) sleep(FCADM_SLEEP_TIME);
8027836SJohn.Forte@Sun.COM status = HBA_OpenAdapterByWWN(&handle, portWWN);
8037836SJohn.Forte@Sun.COM if (times++ > FCADM_RETRY_TIMES) {
8047836SJohn.Forte@Sun.COM return (1);
8057836SJohn.Forte@Sun.COM }
8067836SJohn.Forte@Sun.COM }
8077836SJohn.Forte@Sun.COM
8087836SJohn.Forte@Sun.COM /* Get physical port NPIV attributes */
8097836SJohn.Forte@Sun.COM if (retrieveNPIVAttrs(handle, portWWN,
8107836SJohn.Forte@Sun.COM &npivattrs, &portIndex) == 0) {
8117836SJohn.Forte@Sun.COM /* Check port NPIV attributes */
8127836SJohn.Forte@Sun.COM if (npivattrs.MaxNumberOfNPIVPorts == 0) {
8137836SJohn.Forte@Sun.COM goto failed;
8147836SJohn.Forte@Sun.COM }
8157836SJohn.Forte@Sun.COM if (npivattrs.MaxNumberOfNPIVPorts ==
8167836SJohn.Forte@Sun.COM npivattrs.NumberOfNPIVPorts) {
8177836SJohn.Forte@Sun.COM goto failed;
8187836SJohn.Forte@Sun.COM }
8197836SJohn.Forte@Sun.COM
8207836SJohn.Forte@Sun.COM /* Create a virtual port */
8217836SJohn.Forte@Sun.COM status = Sun_HBA_CreateNPIVPort(handle, portIndex,
8227836SJohn.Forte@Sun.COM vnodeWWN, vportWWN, &npivportIndex);
8237836SJohn.Forte@Sun.COM times = 0;
8247836SJohn.Forte@Sun.COM while (status == HBA_STATUS_ERROR_TRY_AGAIN ||
8257836SJohn.Forte@Sun.COM status == HBA_STATUS_ERROR_BUSY) {
8267836SJohn.Forte@Sun.COM (void) sleep(FCADM_SLEEP_TIME);
8277836SJohn.Forte@Sun.COM status = Sun_HBA_CreateNPIVPort(handle, portIndex,
8287836SJohn.Forte@Sun.COM vnodeWWN, vportWWN, &npivportIndex);
8297836SJohn.Forte@Sun.COM if (times++ > FCADM_RETRY_TIMES) {
8307836SJohn.Forte@Sun.COM goto failed;
8317836SJohn.Forte@Sun.COM }
8327836SJohn.Forte@Sun.COM }
8337836SJohn.Forte@Sun.COM }
8347836SJohn.Forte@Sun.COM
8357836SJohn.Forte@Sun.COM failed:
8367836SJohn.Forte@Sun.COM HBA_CloseAdapter(handle);
8377836SJohn.Forte@Sun.COM
8387836SJohn.Forte@Sun.COM return (0);
8397836SJohn.Forte@Sun.COM }
8407836SJohn.Forte@Sun.COM
8417836SJohn.Forte@Sun.COM int
fc_util_create_portlist()8427836SJohn.Forte@Sun.COM fc_util_create_portlist()
8437836SJohn.Forte@Sun.COM {
8447836SJohn.Forte@Sun.COM scf_handle_t *handle = NULL;
8457836SJohn.Forte@Sun.COM scf_service_t *svc = NULL;
8467836SJohn.Forte@Sun.COM scf_propertygroup_t *pg = NULL;
8477836SJohn.Forte@Sun.COM scf_transaction_t *tran = NULL;
8487836SJohn.Forte@Sun.COM scf_transaction_entry_t *entry = NULL;
8497836SJohn.Forte@Sun.COM scf_property_t *prop = NULL;
8507836SJohn.Forte@Sun.COM scf_value_t *valueLookup = NULL;
8517836SJohn.Forte@Sun.COM scf_iter_t *valueIter = NULL;
8527836SJohn.Forte@Sun.COM char buf[NPIV_PORT_LIST_LENGTH] = {0};
8537836SJohn.Forte@Sun.COM int commitRet;
8547836SJohn.Forte@Sun.COM
8557836SJohn.Forte@Sun.COM commitRet = cfgInit(&handle, &svc);
8567836SJohn.Forte@Sun.COM if (commitRet != NPIV_SUCCESS) {
8577836SJohn.Forte@Sun.COM goto out;
8587836SJohn.Forte@Sun.COM }
8597836SJohn.Forte@Sun.COM
8607836SJohn.Forte@Sun.COM if (((pg = scf_pg_create(handle)) == NULL) ||
8617836SJohn.Forte@Sun.COM ((tran = scf_transaction_create(handle)) == NULL) ||
8627836SJohn.Forte@Sun.COM ((entry = scf_entry_create(handle)) == NULL) ||
8637836SJohn.Forte@Sun.COM ((prop = scf_property_create(handle)) == NULL) ||
8647836SJohn.Forte@Sun.COM ((valueIter = scf_iter_create(handle)) == NULL)) {
8657836SJohn.Forte@Sun.COM goto out;
8667836SJohn.Forte@Sun.COM }
8677836SJohn.Forte@Sun.COM
8687836SJohn.Forte@Sun.COM /* get property group or create it */
8697836SJohn.Forte@Sun.COM if (scf_service_get_pg(svc, NPIV_PG_NAME, pg) == -1) {
8707836SJohn.Forte@Sun.COM goto out;
8717836SJohn.Forte@Sun.COM }
8727836SJohn.Forte@Sun.COM
8737836SJohn.Forte@Sun.COM if (scf_pg_get_property(pg, NPIV_PORT_LIST, prop) == -1) {
8747836SJohn.Forte@Sun.COM syslog(LOG_ERR, "get property failed - %s",
8757836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
8767836SJohn.Forte@Sun.COM goto out;
8777836SJohn.Forte@Sun.COM }
8787836SJohn.Forte@Sun.COM
8797836SJohn.Forte@Sun.COM valueLookup = scf_value_create(handle);
8807836SJohn.Forte@Sun.COM if (valueLookup == NULL) {
8817836SJohn.Forte@Sun.COM syslog(LOG_ERR, "scf value alloc failed - %s",
8827836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
8837836SJohn.Forte@Sun.COM goto out;
8847836SJohn.Forte@Sun.COM }
8857836SJohn.Forte@Sun.COM
8867836SJohn.Forte@Sun.COM if (scf_iter_property_values(valueIter, prop) == -1) {
8877836SJohn.Forte@Sun.COM syslog(LOG_ERR, "iter value failed - %s",
8887836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
8897836SJohn.Forte@Sun.COM goto out;
8907836SJohn.Forte@Sun.COM }
8917836SJohn.Forte@Sun.COM
8927836SJohn.Forte@Sun.COM if (HBA_LoadLibrary() != HBA_STATUS_OK) {
8937836SJohn.Forte@Sun.COM goto out;
8947836SJohn.Forte@Sun.COM }
8957836SJohn.Forte@Sun.COM HBA_GetNumberOfAdapters();
8967836SJohn.Forte@Sun.COM
8977836SJohn.Forte@Sun.COM while (scf_iter_next_value(valueIter, valueLookup) == 1) {
8987836SJohn.Forte@Sun.COM char ppwwn[17] = {0};
8997836SJohn.Forte@Sun.COM char vnwwn[17] = {0};
9007836SJohn.Forte@Sun.COM char vpwwn[17] = {0};
9017836SJohn.Forte@Sun.COM int vindex = 0;
9027836SJohn.Forte@Sun.COM
9037836SJohn.Forte@Sun.COM bzero(buf, sizeof (buf));
9047836SJohn.Forte@Sun.COM if (scf_value_get_ustring(valueLookup, buf, MAXNAMELEN) == -1) {
9057836SJohn.Forte@Sun.COM syslog(LOG_ERR, "iter value failed - %s",
9067836SJohn.Forte@Sun.COM scf_strerror(scf_error()));
9077836SJohn.Forte@Sun.COM break;
9087836SJohn.Forte@Sun.COM }
9097836SJohn.Forte@Sun.COM
9107836SJohn.Forte@Sun.COM sscanf(buf, "%16s:%16s:%16s:%d", ppwwn, vpwwn, vnwwn, &vindex);
9117836SJohn.Forte@Sun.COM create_npivport(ppwwn, vnwwn, vpwwn, vindex);
9127836SJohn.Forte@Sun.COM }
9137836SJohn.Forte@Sun.COM
9147836SJohn.Forte@Sun.COM HBA_FreeLibrary();
9157836SJohn.Forte@Sun.COM out:
9167836SJohn.Forte@Sun.COM /*
9177836SJohn.Forte@Sun.COM * Free resources
9187836SJohn.Forte@Sun.COM */
9197836SJohn.Forte@Sun.COM if (handle != NULL) {
9207836SJohn.Forte@Sun.COM scf_handle_destroy(handle);
9217836SJohn.Forte@Sun.COM }
9227836SJohn.Forte@Sun.COM if (svc != NULL) {
9237836SJohn.Forte@Sun.COM scf_service_destroy(svc);
9247836SJohn.Forte@Sun.COM }
9257836SJohn.Forte@Sun.COM if (pg != NULL) {
9267836SJohn.Forte@Sun.COM scf_pg_destroy(pg);
9277836SJohn.Forte@Sun.COM }
9287836SJohn.Forte@Sun.COM if (tran != NULL) {
9297836SJohn.Forte@Sun.COM scf_transaction_destroy(tran);
9307836SJohn.Forte@Sun.COM }
9317836SJohn.Forte@Sun.COM if (entry != NULL) {
9327836SJohn.Forte@Sun.COM scf_entry_destroy(entry);
9337836SJohn.Forte@Sun.COM }
9347836SJohn.Forte@Sun.COM if (prop != NULL) {
9357836SJohn.Forte@Sun.COM scf_property_destroy(prop);
9367836SJohn.Forte@Sun.COM }
9377836SJohn.Forte@Sun.COM if (valueIter != NULL) {
9387836SJohn.Forte@Sun.COM scf_iter_destroy(valueIter);
9397836SJohn.Forte@Sun.COM }
9407836SJohn.Forte@Sun.COM if (valueLookup != NULL) {
9417836SJohn.Forte@Sun.COM scf_value_destroy(valueLookup);
9427836SJohn.Forte@Sun.COM }
9437836SJohn.Forte@Sun.COM
9447836SJohn.Forte@Sun.COM return (0);
9457836SJohn.Forte@Sun.COM }
946*10275SReed.Liu@Sun.COM
947*10275SReed.Liu@Sun.COM /* ARGSUSED */
948*10275SReed.Liu@Sun.COM int
fc_util_force_lip(int objects,char * argv[])949*10275SReed.Liu@Sun.COM fc_util_force_lip(int objects, char *argv[])
950*10275SReed.Liu@Sun.COM {
951*10275SReed.Liu@Sun.COM uint64_t hbaWWN;
952*10275SReed.Liu@Sun.COM HBA_WWN myWWN;
953*10275SReed.Liu@Sun.COM HBA_HANDLE handle;
954*10275SReed.Liu@Sun.COM HBA_STATUS status;
955*10275SReed.Liu@Sun.COM int rval = 0;
956*10275SReed.Liu@Sun.COM
957*10275SReed.Liu@Sun.COM if ((status = HBA_LoadLibrary()) != HBA_STATUS_OK) {
958*10275SReed.Liu@Sun.COM fprintf(stderr, gettext("Failed to load FC-HBA library\n"));
959*10275SReed.Liu@Sun.COM printStatus(status);
960*10275SReed.Liu@Sun.COM fprintf(stderr, "\n");
961*10275SReed.Liu@Sun.COM return (1);
962*10275SReed.Liu@Sun.COM }
963*10275SReed.Liu@Sun.COM
964*10275SReed.Liu@Sun.COM sscanf(argv[0], "%016llx", &hbaWWN);
965*10275SReed.Liu@Sun.COM hbaWWN = htonll(hbaWWN);
966*10275SReed.Liu@Sun.COM memcpy(myWWN.wwn, &hbaWWN, sizeof (hbaWWN));
967*10275SReed.Liu@Sun.COM
968*10275SReed.Liu@Sun.COM /*
969*10275SReed.Liu@Sun.COM * Try target mode first
970*10275SReed.Liu@Sun.COM */
971*10275SReed.Liu@Sun.COM if ((status = Sun_HBA_OpenTgtAdapterByWWN(&handle, myWWN)) !=
972*10275SReed.Liu@Sun.COM HBA_STATUS_OK) {
973*10275SReed.Liu@Sun.COM /*
974*10275SReed.Liu@Sun.COM * Continue to try initiator mode
975*10275SReed.Liu@Sun.COM */
976*10275SReed.Liu@Sun.COM if ((status = HBA_OpenAdapterByWWN(&handle, myWWN)) !=
977*10275SReed.Liu@Sun.COM HBA_STATUS_OK) {
978*10275SReed.Liu@Sun.COM fprintf(stderr, gettext("Error: HBA %s not found\n"),
979*10275SReed.Liu@Sun.COM argv[0]);
980*10275SReed.Liu@Sun.COM return (0);
981*10275SReed.Liu@Sun.COM }
982*10275SReed.Liu@Sun.COM }
983*10275SReed.Liu@Sun.COM
984*10275SReed.Liu@Sun.COM status = Sun_HBA_ForceLip(handle, &rval);
985*10275SReed.Liu@Sun.COM if ((status != HBA_STATUS_OK) || (rval != 0)) {
986*10275SReed.Liu@Sun.COM fprintf(stderr, gettext("Error: Failed to reinitialize the "
987*10275SReed.Liu@Sun.COM "link of HBA %s\n"), argv[0]);
988*10275SReed.Liu@Sun.COM }
989*10275SReed.Liu@Sun.COM
990*10275SReed.Liu@Sun.COM return (0);
991*10275SReed.Liu@Sun.COM }
992