2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdlib.h>
2N/A#include <stdio.h>
2N/A#include <wchar.h>
2N/A#include <strings.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include <libintl.h>
2N/A#include <errno.h>
2N/A#include <string.h>
2N/A#include <assert.h>
2N/A#include <syslog.h>
2N/A#include <libfcoe.h>
2N/A#include <libdllink.h>
2N/A#include <libnetcfg.h>
2N/A#include <libnvpair.h>
2N/A#include <liblldp.h>
2N/A#include <fcoeio.h>
2N/A#include <libscf.h>
2N/A#include <inttypes.h>
2N/A
2N/A#define FCOE_DEV_PATH "/devices/fcoe:admin"
2N/A
2N/A#define OPEN_FCOE 0
2N/A#define OPEN_EXCL_FCOE O_EXCL
2N/A
2N/A/*
2N/A * Open for fcoe module
2N/A *
2N/A * flag - open flag (OPEN_FCOE, OPEN_EXCL_FCOE)
2N/A * fd - pointer to integer. On success, contains the fcoe file descriptor
2N/A */
2N/Astatic int
2N/AopenFcoe(int flag, int *fd)
2N/A{
2N/A int ret = FCOE_STATUS_ERROR;
2N/A
2N/A if ((*fd = open(FCOE_DEV_PATH, O_NDELAY | O_RDONLY | flag)) != -1) {
2N/A ret = FCOE_STATUS_OK;
2N/A } else {
2N/A if (errno == EPERM || errno == EACCES) {
2N/A ret = FCOE_STATUS_ERROR_PERM;
2N/A } else {
2N/A ret = FCOE_STATUS_ERROR_OPEN_DEV;
2N/A }
2N/A syslog(LOG_DEBUG, "openFcoe:open failure:%s:errno(%d)",
2N/A FCOE_DEV_PATH, errno);
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Astatic void
2N/AWWN2str(char *buf, FCOE_PORT_WWN *wwn) {
2N/A int j;
2N/A unsigned char *pc = (unsigned char *)&(wwn->wwn[0]);
2N/A buf[0] = '\0';
2N/A for (j = 0; j < 16; j += 2) {
2N/A (void) sprintf(&buf[j], "%02X", (int)*pc++);
2N/A }
2N/A}
2N/A
2N/Astatic int
2N/AisWWNZero(FCOE_PORT_WWN portwwn)
2N/A{
2N/A int i;
2N/A int size = sizeof (FCOE_PORT_WWN);
2N/A
2N/A for (i = 0; i < size; i++) {
2N/A if (portwwn.wwn[i] != 0) {
2N/A return (0);
2N/A }
2N/A }
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * Initialize scf fcoe service access
2N/A * handle - returned handle
2N/A * service - returned service handle
2N/A */
2N/Astatic int
2N/Afcoe_cfg_scf_init(scf_handle_t **handle, scf_service_t **service, int is_target)
2N/A{
2N/A scf_scope_t *scope = NULL;
2N/A int ret;
2N/A
2N/A if ((*handle = scf_handle_create(SCF_VERSION)) == NULL) {
2N/A syslog(LOG_ERR, "scf_handle_create failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto err;
2N/A }
2N/A
2N/A if (scf_handle_bind(*handle) == -1) {
2N/A syslog(LOG_ERR, "scf_handle_bind failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto err;
2N/A }
2N/A
2N/A if ((*service = scf_service_create(*handle)) == NULL) {
2N/A syslog(LOG_ERR, "scf_service_create failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto err;
2N/A }
2N/A
2N/A if ((scope = scf_scope_create(*handle)) == NULL) {
2N/A syslog(LOG_ERR, "scf_scope_create failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto err;
2N/A }
2N/A
2N/A if (scf_handle_get_scope(*handle, SCF_SCOPE_LOCAL, scope) == -1) {
2N/A syslog(LOG_ERR, "scf_handle_get_scope failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto err;
2N/A }
2N/A
2N/A if (scf_scope_get_service(scope,
2N/A is_target ? FCOE_TARGET_SERVICE: FCOE_INITIATOR_SERVICE,
2N/A *service) == -1) {
2N/A syslog(LOG_ERR, "scf_scope_get_service failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR_SERVICE_NOT_FOUND;
2N/A goto err;
2N/A }
2N/A
2N/A scf_scope_destroy(scope);
2N/A
2N/A return (FCOE_SUCCESS);
2N/A
2N/Aerr:
2N/A if (*handle != NULL) {
2N/A scf_handle_destroy(*handle);
2N/A }
2N/A if (*service != NULL) {
2N/A scf_service_destroy(*service);
2N/A *service = NULL;
2N/A }
2N/A if (scope != NULL) {
2N/A scf_scope_destroy(scope);
2N/A }
2N/A return (ret);
2N/A}
2N/A
2N/Astatic int
2N/Afcoe_add_remove_scf_entry(char *mac_name,
2N/A char *pwwn, char *nwwn,
2N/A int is_target, int is_promiscuous, int addRemoveFlag)
2N/A{
2N/A scf_handle_t *handle = NULL;
2N/A scf_service_t *svc = NULL;
2N/A scf_propertygroup_t *pg = NULL;
2N/A scf_transaction_t *tran = NULL;
2N/A scf_transaction_entry_t *entry = NULL;
2N/A scf_property_t *prop = NULL;
2N/A scf_value_t *valueLookup = NULL;
2N/A scf_iter_t *valueIter = NULL;
2N/A scf_value_t **valueSet = NULL;
2N/A int ret = FCOE_SUCCESS;
2N/A boolean_t createProp = B_FALSE;
2N/A int lastAlloc = 0;
2N/A char buf[FCOE_PORT_LIST_LENGTH] = {0};
2N/A char memberName[FCOE_PORT_LIST_LENGTH] = {0};
2N/A boolean_t found = B_FALSE;
2N/A int i = 0;
2N/A int valueArraySize = 0;
2N/A int commitRet;
2N/A int portListAlloc = 100;
2N/A
2N/A (void) snprintf(memberName, FCOE_PORT_LIST_LENGTH,
2N/A "%s:%s:%s:%d:%d", mac_name, pwwn, nwwn,
2N/A is_target, is_promiscuous);
2N/A
2N/A ret = fcoe_cfg_scf_init(&handle, &svc, is_target);
2N/A if (ret != FCOE_SUCCESS) {
2N/A goto out;
2N/A }
2N/A
2N/A if (((pg = scf_pg_create(handle)) == NULL) ||
2N/A ((tran = scf_transaction_create(handle)) == NULL) ||
2N/A ((entry = scf_entry_create(handle)) == NULL) ||
2N/A ((prop = scf_property_create(handle)) == NULL) ||
2N/A ((valueIter = scf_iter_create(handle)) == NULL)) {
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A }
2N/A
2N/A /* get property group or create it */
2N/A if (scf_service_get_pg(svc, FCOE_PG_NAME, pg) == -1) {
2N/A if ((scf_error() == SCF_ERROR_NOT_FOUND)) {
2N/A if (scf_service_add_pg(svc, FCOE_PG_NAME,
2N/A SCF_GROUP_APPLICATION, 0, pg) == -1) {
2N/A syslog(LOG_ERR, "add pg failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A } else {
2N/A createProp = B_TRUE;
2N/A }
2N/A } else {
2N/A syslog(LOG_ERR, "get pg failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A }
2N/A if (ret != FCOE_SUCCESS) {
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A /* to make sure property exists */
2N/A if (createProp == B_FALSE) {
2N/A if (scf_pg_get_property(pg, FCOE_PORT_LIST, prop) == -1) {
2N/A if ((scf_error() == SCF_ERROR_NOT_FOUND)) {
2N/A createProp = B_TRUE;
2N/A } else {
2N/A syslog(LOG_ERR, "get property failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* Begin the transaction */
2N/A if (scf_transaction_start(tran, pg) == -1) {
2N/A syslog(LOG_ERR, "start transaction failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A }
2N/A
2N/A valueSet = (scf_value_t **)calloc(1, sizeof (*valueSet)
2N/A * (lastAlloc = portListAlloc));
2N/A if (valueSet == NULL) {
2N/A ret = FCOE_ERROR_NOMEM;
2N/A goto out;
2N/A }
2N/A
2N/A if (createProp) {
2N/A if (scf_transaction_property_new(tran, entry, FCOE_PORT_LIST,
2N/A SCF_TYPE_USTRING) == -1) {
2N/A if (scf_error() == SCF_ERROR_EXISTS) {
2N/A ret = FCOE_ERROR_EXISTS;
2N/A } else {
2N/A syslog(LOG_ERR,
2N/A "transaction property new failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A }
2N/A goto out;
2N/A }
2N/A } else {
2N/A if (scf_transaction_property_change(tran, entry,
2N/A FCOE_PORT_LIST, SCF_TYPE_USTRING) == -1) {
2N/A syslog(LOG_ERR,
2N/A "transaction property change failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_pg_get_property(pg, FCOE_PORT_LIST, prop) == -1) {
2N/A syslog(LOG_ERR, "get property failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A }
2N/A
2N/A valueLookup = scf_value_create(handle);
2N/A if (valueLookup == NULL) {
2N/A syslog(LOG_ERR, "scf value alloc failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_iter_property_values(valueIter, prop) == -1) {
2N/A syslog(LOG_ERR, "iter value failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A }
2N/A
2N/A while (scf_iter_next_value(valueIter, valueLookup) == 1) {
2N/A char *macnameIter = NULL;
2N/A char buftmp[FCOE_PORT_LIST_LENGTH] = {0};
2N/A
2N/A bzero(buf, sizeof (buf));
2N/A if (scf_value_get_ustring(valueLookup,
2N/A buf, MAXNAMELEN) == -1) {
2N/A syslog(LOG_ERR, "iter value failed- %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A break;
2N/A }
2N/A (void) strcpy(buftmp, buf);
2N/A macnameIter = strtok(buftmp, ":");
2N/A if (strcmp(macnameIter, mac_name) == 0) {
2N/A if (addRemoveFlag == FCOE_SCF_ADD) {
2N/A ret = FCOE_ERROR_EXISTS;
2N/A break;
2N/A } else {
2N/A found = B_TRUE;
2N/A continue;
2N/A }
2N/A }
2N/A
2N/A valueSet[i] = scf_value_create(handle);
2N/A if (valueSet[i] == NULL) {
2N/A syslog(LOG_ERR, "scf value alloc failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A break;
2N/A }
2N/A
2N/A if (scf_value_set_ustring(valueSet[i], buf) == -1) {
2N/A syslog(LOG_ERR, "set value failed 1- %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A break;
2N/A }
2N/A
2N/A if (scf_entry_add_value(entry, valueSet[i]) == -1) {
2N/A syslog(LOG_ERR, "add value failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A break;
2N/A }
2N/A
2N/A i++;
2N/A
2N/A if (i >= lastAlloc) {
2N/A lastAlloc += portListAlloc;
2N/A valueSet = realloc(valueSet,
2N/A sizeof (*valueSet) * lastAlloc);
2N/A if (valueSet == NULL) {
2N/A ret = FCOE_ERROR;
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A valueArraySize = i;
2N/A if (!found && (addRemoveFlag == FCOE_SCF_REMOVE)) {
2N/A ret = FCOE_ERROR_MEMBER_NOT_FOUND;
2N/A }
2N/A if (ret != FCOE_SUCCESS) {
2N/A goto out;
2N/A }
2N/A
2N/A if (addRemoveFlag == FCOE_SCF_ADD) {
2N/A /*
2N/A * Now create the new entry
2N/A */
2N/A valueSet[i] = scf_value_create(handle);
2N/A if (valueSet[i] == NULL) {
2N/A syslog(LOG_ERR, "scf value alloc failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A } else {
2N/A valueArraySize++;
2N/A }
2N/A
2N/A /*
2N/A * Set the new member name
2N/A */
2N/A if (scf_value_set_ustring(valueSet[i], memberName) == -1) {
2N/A syslog(LOG_ERR, "set value failed 2- %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Add the new member
2N/A */
2N/A if (scf_entry_add_value(entry, valueSet[i]) == -1) {
2N/A syslog(LOG_ERR, "add value failed - %s",
2N/A scf_strerror(scf_error()));
2N/A ret = FCOE_ERROR;
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A if ((commitRet = scf_transaction_commit(tran)) != 1) {
2N/A syslog(LOG_ERR, "transaction commit failed - %s",
2N/A scf_strerror(scf_error()));
2N/A if (commitRet == 0) {
2N/A ret = FCOE_ERROR_BUSY;
2N/A } else {
2N/A ret = FCOE_ERROR;
2N/A }
2N/A goto out;
2N/A }
2N/A
2N/Aout:
2N/A /*
2N/A * Free resources
2N/A */
2N/A if (handle != NULL) {
2N/A scf_handle_destroy(handle);
2N/A }
2N/A if (svc != NULL) {
2N/A scf_service_destroy(svc);
2N/A }
2N/A if (pg != NULL) {
2N/A scf_pg_destroy(pg);
2N/A }
2N/A if (tran != NULL) {
2N/A scf_transaction_destroy(tran);
2N/A }
2N/A if (entry != NULL) {
2N/A scf_entry_destroy(entry);
2N/A }
2N/A if (prop != NULL) {
2N/A scf_property_destroy(prop);
2N/A }
2N/A if (valueIter != NULL) {
2N/A scf_iter_destroy(valueIter);
2N/A }
2N/A if (valueLookup != NULL) {
2N/A scf_value_destroy(valueLookup);
2N/A }
2N/A
2N/A /*
2N/A * Free valueSet scf resources
2N/A */
2N/A if (valueArraySize > 0) {
2N/A for (i = 0; i < valueArraySize; i++) {
2N/A scf_value_destroy(valueSet[i]);
2N/A }
2N/A }
2N/A /*
2N/A * Now free the pointer array to the resources
2N/A */
2N/A if (valueSet != NULL) {
2N/A free(valueSet);
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/AFCOE_STATUS
2N/AFCOE_CreatePort(
2N/A const FCOE_UINT8 *macLinkName,
2N/A FCOE_UINT8 portType,
2N/A FCOE_PORT_WWN pwwn,
2N/A FCOE_PORT_WWN nwwn,
2N/A FCOE_UINT8 promiscuous)
2N/A{
2N/A FCOE_STATUS status;
2N/A int fcoe_fd;
2N/A fcoeio_t fcoeio;
2N/A fcoeio_create_port_param_t param;
2N/A dladm_handle_t handle;
2N/A datalink_id_t linkid;
2N/A datalink_class_t class;
2N/A
2N/A bzero(&param, sizeof (fcoeio_create_port_param_t));
2N/A
2N/A if (macLinkName == NULL) {
2N/A return (FCOE_STATUS_ERROR_INVAL_ARG);
2N/A }
2N/A
2N/A if (strlen((char *)macLinkName) > MAXLINKNAMELEN-1) {
2N/A return (FCOE_STATUS_ERROR_MAC_LEN);
2N/A }
2N/A
2N/A if (dladm_open(&handle, NETADM_ACTIVE_PROFILE) != DLADM_STATUS_OK) {
2N/A return (FCOE_STATUS_ERROR);
2N/A }
2N/A
2N/A if (dladm_name2info(handle, (const char *)macLinkName,
2N/A &linkid, NULL, &class, NULL) != DLADM_STATUS_OK) {
2N/A dladm_close(handle);
2N/A (void) fcoe_add_remove_scf_entry((char *)macLinkName,
2N/A "",
2N/A "",
2N/A portType,
2N/A 0,
2N/A FCOE_SCF_REMOVE);
2N/A return (FCOE_STATUS_ERROR_GET_LINKINFO);
2N/A }
2N/A
2N/A if (class != DATALINK_CLASS_PHYS) {
2N/A dladm_close(handle);
2N/A return (FCOE_STATUS_ERROR_CLASS_UNSUPPORT);
2N/A }
2N/A
2N/A if (portType != FCOE_PORTTYPE_INITIATOR &&
2N/A portType != FCOE_PORTTYPE_TARGET) {
2N/A dladm_close(handle);
2N/A return (FCOE_STATUS_ERROR_INVAL_ARG);
2N/A }
2N/A
2N/A if (!isWWNZero(pwwn)) {
2N/A param.fcp_pwwn_provided = 1;
2N/A bcopy(pwwn.wwn, param.fcp_pwwn, 8);
2N/A }
2N/A
2N/A if (!isWWNZero(nwwn)) {
2N/A param.fcp_nwwn_provided = 1;
2N/A bcopy(nwwn.wwn, param.fcp_nwwn, 8);
2N/A }
2N/A
2N/A if (param.fcp_pwwn_provided == 1 &&
2N/A param.fcp_nwwn_provided == 1 &&
2N/A bcmp(&pwwn, &nwwn, 8) == 0) {
2N/A dladm_close(handle);
2N/A return (FCOE_STATUS_ERROR_WWN_SAME);
2N/A }
2N/A
2N/A param.fcp_force_promisc = promiscuous;
2N/A param.fcp_mac_linkid = linkid;
2N/A param.fcp_port_type = (fcoe_cli_type_t)portType;
2N/A
2N/A if ((status = openFcoe(OPEN_FCOE, &fcoe_fd)) != FCOE_STATUS_OK) {
2N/A dladm_close(handle);
2N/A return (status);
2N/A }
2N/A
2N/A (void) memset(&fcoeio, 0, sizeof (fcoeio));
2N/A fcoeio.fcoeio_cmd = FCOEIO_CREATE_FCOE_PORT;
2N/A
2N/A fcoeio.fcoeio_ilen = sizeof (param);
2N/A fcoeio.fcoeio_xfer = FCOEIO_XFER_WRITE;
2N/A fcoeio.fcoeio_ibuf = (uintptr_t)&param;
2N/A
2N/A if (ioctl(fcoe_fd, FCOEIO_CMD, &fcoeio) != 0) {
2N/A switch (fcoeio.fcoeio_status) {
2N/A case FCOEIOE_INVAL_ARG:
2N/A status = FCOE_STATUS_ERROR_INVAL_ARG;
2N/A break;
2N/A
2N/A case FCOEIOE_BUSY:
2N/A status = FCOE_STATUS_ERROR_BUSY;
2N/A break;
2N/A
2N/A case FCOEIOE_ALREADY:
2N/A status = FCOE_STATUS_ERROR_ALREADY;
2N/A break;
2N/A
2N/A case FCOEIOE_PWWN_CONFLICTED:
2N/A status = FCOE_STATUS_ERROR_PWWN_CONFLICTED;
2N/A break;
2N/A
2N/A case FCOEIOE_NWWN_CONFLICTED:
2N/A status = FCOE_STATUS_ERROR_NWWN_CONFLICTED;
2N/A break;
2N/A
2N/A case FCOEIOE_CREATE_MAC:
2N/A status = FCOE_STATUS_ERROR_CREATE_MAC;
2N/A break;
2N/A
2N/A case FCOEIOE_OPEN_MAC:
2N/A status = FCOE_STATUS_ERROR_OPEN_MAC;
2N/A break;
2N/A
2N/A case FCOEIOE_CREATE_PORT:
2N/A status = FCOE_STATUS_ERROR_CREATE_PORT;
2N/A break;
2N/A
2N/A case FCOEIOE_NEED_JUMBO_FRAME:
2N/A status = FCOE_STATUS_ERROR_NEED_JUMBO_FRAME;
2N/A break;
2N/A
2N/A default:
2N/A status = FCOE_STATUS_ERROR;
2N/A }
2N/A } else {
2N/A char cpwwn[17], cnwwn[17];
2N/A
2N/A WWN2str(cpwwn, &pwwn);
2N/A WWN2str(cnwwn, &nwwn);
2N/A
2N/A (void) fcoe_add_remove_scf_entry((char *)macLinkName,
2N/A cpwwn,
2N/A cnwwn,
2N/A portType,
2N/A promiscuous,
2N/A FCOE_SCF_ADD);
2N/A status = FCOE_STATUS_OK;
2N/A }
2N/A (void) close(fcoe_fd);
2N/A dladm_close(handle);
2N/A return (status);
2N/A}
2N/A
2N/AFCOE_STATUS
2N/AFCOE_DeletePort(const FCOE_UINT8 *macLinkName)
2N/A{
2N/A FCOE_STATUS status = FCOE_STATUS_OK;
2N/A int fcoe_fd;
2N/A fcoeio_t fcoeio;
2N/A dladm_handle_t handle;
2N/A datalink_id_t linkid;
2N/A fcoeio_delete_port_param_t fc_del_port;
2N/A uint64_t is_target = 0;
2N/A int io_ret = 0;
2N/A
2N/A if (macLinkName == NULL) {
2N/A return (FCOE_STATUS_ERROR_INVAL_ARG);
2N/A }
2N/A
2N/A if (strlen((char *)macLinkName) > MAXLINKNAMELEN-1) {
2N/A return (FCOE_STATUS_ERROR_MAC_LEN);
2N/A }
2N/A if (dladm_open(&handle, NETADM_ACTIVE_PROFILE) != DLADM_STATUS_OK) {
2N/A return (FCOE_STATUS_ERROR);
2N/A }
2N/A
2N/A if (dladm_name2info(handle, (const char *)macLinkName,
2N/A &linkid, NULL, NULL, NULL) != DLADM_STATUS_OK) {
2N/A dladm_close(handle);
2N/A return (FCOE_STATUS_ERROR_GET_LINKINFO);
2N/A }
2N/A dladm_close(handle);
2N/A
2N/A if ((status = openFcoe(OPEN_FCOE, &fcoe_fd)) != FCOE_STATUS_OK) {
2N/A return (status);
2N/A }
2N/A
2N/A fc_del_port.fdp_mac_linkid = linkid;
2N/A
2N/A (void) memset(&fcoeio, 0, sizeof (fcoeio));
2N/A fcoeio.fcoeio_cmd = FCOEIO_DELETE_FCOE_PORT;
2N/A
2N/A /* only 4 bytes here, need to change */
2N/A fcoeio.fcoeio_ilen = sizeof (fcoeio_delete_port_param_t);
2N/A fcoeio.fcoeio_olen = sizeof (uint64_t);
2N/A fcoeio.fcoeio_xfer = FCOEIO_XFER_RW;
2N/A fcoeio.fcoeio_ibuf = (uintptr_t)&fc_del_port;
2N/A fcoeio.fcoeio_obuf = (uintptr_t)&is_target;
2N/A
2N/A io_ret = ioctl(fcoe_fd, FCOEIO_CMD, &fcoeio);
2N/A if (io_ret != 0) {
2N/A switch (fcoeio.fcoeio_status) {
2N/A case FCOEIOE_INVAL_ARG:
2N/A status = FCOE_STATUS_ERROR_INVAL_ARG;
2N/A break;
2N/A
2N/A case FCOEIOE_BUSY:
2N/A status = FCOE_STATUS_ERROR_BUSY;
2N/A break;
2N/A
2N/A case FCOEIOE_ALREADY:
2N/A status = FCOE_STATUS_ERROR_ALREADY;
2N/A break;
2N/A
2N/A case FCOEIOE_MAC_NOT_FOUND:
2N/A status = FCOE_STATUS_ERROR_MAC_NOT_FOUND;
2N/A break;
2N/A
2N/A case FCOEIOE_OFFLINE_FAILURE:
2N/A status = FCOE_STATUS_ERROR_OFFLINE_DEV;
2N/A break;
2N/A
2N/A default:
2N/A status = FCOE_STATUS_ERROR;
2N/A }
2N/A } else {
2N/A (void) fcoe_add_remove_scf_entry((char *)macLinkName,
2N/A "",
2N/A "",
2N/A is_target,
2N/A 0,
2N/A FCOE_SCF_REMOVE);
2N/A status = FCOE_STATUS_OK;
2N/A }
2N/A
2N/A if (io_ret == FCOEIOE_MAC_NOT_FOUND) {
2N/A (void) fcoe_add_remove_scf_entry((char *)macLinkName,
2N/A "",
2N/A "",
2N/A 0,
2N/A 0,
2N/A FCOE_SCF_REMOVE);
2N/A (void) fcoe_add_remove_scf_entry((char *)macLinkName,
2N/A "",
2N/A "",
2N/A 1,
2N/A 0,
2N/A FCOE_SCF_REMOVE);
2N/A }
2N/A (void) close(fcoe_fd);
2N/A return (status);
2N/A}
2N/A
2N/AFCOE_STATUS
2N/AFCOE_GetPortList(
2N/A FCOE_UINT32 *port_num,
2N/A FCOE_PORT_ATTRIBUTE **portlist)
2N/A{
2N/A FCOE_STATUS status = FCOE_STATUS_OK;
2N/A int fcoe_fd;
2N/A fcoeio_t fcoeio;
2N/A fcoe_port_list_t *inportlist = NULL;
2N/A FCOE_PORT_ATTRIBUTE *outportlist = NULL;
2N/A int i;
2N/A int size = 64; /* default first attempt */
2N/A int retry = 0;
2N/A int bufsize;
2N/A dladm_handle_t handle;
2N/A char mac_name[MAXLINKNAMELEN];
2N/A
2N/A if (port_num == NULL || portlist == NULL) {
2N/A return (FCOE_STATUS_ERROR_INVAL_ARG);
2N/A }
2N/A *port_num = 0;
2N/A
2N/A if ((status = openFcoe(OPEN_FCOE, &fcoe_fd)) != FCOE_STATUS_OK) {
2N/A return (status);
2N/A }
2N/A
2N/A /* Get fcoe port list */
2N/A (void) memset(&fcoeio, 0, sizeof (fcoeio));
2N/A retry = 0;
2N/A
2N/A do {
2N/A bufsize = sizeof (fcoe_port_instance_t) * (size - 1) +
2N/A sizeof (fcoe_port_list_t);
2N/A inportlist = (fcoe_port_list_t *)malloc(bufsize);
2N/A fcoeio.fcoeio_cmd = FCOEIO_GET_FCOE_PORT_LIST;
2N/A fcoeio.fcoeio_olen = bufsize;
2N/A fcoeio.fcoeio_xfer = FCOEIO_XFER_READ;
2N/A fcoeio.fcoeio_obuf = (uintptr_t)inportlist;
2N/A
2N/A if (ioctl(fcoe_fd, FCOEIO_CMD, &fcoeio) != 0) {
2N/A if (fcoeio.fcoeio_status == FCOEIOE_MORE_DATA) {
2N/A size = inportlist->numPorts;
2N/A }
2N/A free(inportlist);
2N/A switch (fcoeio.fcoeio_status) {
2N/A case FCOEIOE_INVAL_ARG:
2N/A status = FCOE_STATUS_ERROR_INVAL_ARG;
2N/A (void) close(fcoe_fd);
2N/A return (status);
2N/A
2N/A case FCOEIOE_BUSY:
2N/A status = FCOE_STATUS_ERROR_BUSY;
2N/A retry++;
2N/A break;
2N/A
2N/A case FCOEIOE_MORE_DATA:
2N/A status = FCOE_STATUS_ERROR_MORE_DATA;
2N/A retry++;
2N/A default:
2N/A status = FCOE_STATUS_ERROR;
2N/A (void) close(fcoe_fd);
2N/A return (status);
2N/A }
2N/A } else {
2N/A status = FCOE_STATUS_OK;
2N/A break;
2N/A }
2N/A } while (retry <= 3 && status != FCOE_STATUS_OK);
2N/A
2N/A if (status == FCOE_STATUS_OK && inportlist->numPorts > 0) {
2N/A if (dladm_open(&handle, NETADM_ACTIVE_PROFILE)
2N/A != DLADM_STATUS_OK) {
2N/A handle = NULL;
2N/A }
2N/A
2N/A outportlist = (PFCOE_PORT_ATTRIBUTE)
2N/A malloc(sizeof (FCOE_PORT_ATTRIBUTE) * inportlist->numPorts);
2N/A
2N/A for (i = 0; i < inportlist->numPorts; i++) {
2N/A fcoe_port_instance_t *pi = &inportlist->ports[i];
2N/A FCOE_PORT_ATTRIBUTE *po = &outportlist[i];
2N/A bcopy(pi->fpi_pwwn, &po->port_wwn, 8);
2N/A
2N/A if (handle == NULL ||
2N/A dladm_datalink_id2info(handle, pi->fpi_mac_linkid,
2N/A NULL, NULL, NULL, mac_name, sizeof (mac_name))
2N/A != DLADM_STATUS_OK) {
2N/A (void) strcpy((char *)po->mac_link_name,
2N/A "<unknown>");
2N/A } else {
2N/A (void) strcpy((char *)po->mac_link_name,
2N/A mac_name);
2N/A }
2N/A bcopy(pi->fpi_mac_factory_addr,
2N/A po->mac_factory_addr, 6);
2N/A bcopy(pi->fpi_mac_current_addr,
2N/A po->mac_current_addr, 6);
2N/A po->port_type = (FCOE_UINT8)pi->fpi_port_type;
2N/A po->mtu_size = pi->fpi_mtu_size;
2N/A po->mac_promisc = pi->fpi_mac_promisc;
2N/A }
2N/A
2N/A if (handle != NULL) {
2N/A dladm_close(handle);
2N/A }
2N/A *port_num = inportlist->numPorts;
2N/A *portlist = outportlist;
2N/A free(inportlist);
2N/A } else {
2N/A *port_num = 0;
2N/A *portlist = NULL;
2N/A }
2N/A (void) close(fcoe_fd);
2N/A return (status);
2N/A}
2N/A
2N/AFCOE_STATUS FCOE_LoadConfig(
2N/A FCOE_UINT8 portType,
2N/A FCOE_SMF_PORT_LIST **portlist)
2N/A{
2N/A scf_handle_t *handle = NULL;
2N/A scf_service_t *svc = NULL;
2N/A scf_propertygroup_t *pg = NULL;
2N/A scf_transaction_t *tran = NULL;
2N/A scf_transaction_entry_t *entry = NULL;
2N/A scf_property_t *prop = NULL;
2N/A scf_value_t *valueLookup = NULL;
2N/A scf_iter_t *valueIter = NULL;
2N/A char buf[FCOE_PORT_LIST_LENGTH] = {0};
2N/A int commitRet;
2N/A FCOE_UINT32 portIndex;
2N/A int bufsize, retry;
2N/A int size = 10; /* default first attempt */
2N/A int pg_or_prop_not_found = 0;
2N/A
2N/A commitRet = fcoe_cfg_scf_init(&handle, &svc, portType);
2N/A if (commitRet != FCOE_SUCCESS) {
2N/A goto out;
2N/A }
2N/A
2N/A if (((pg = scf_pg_create(handle)) == NULL) ||
2N/A ((tran = scf_transaction_create(handle)) == NULL) ||
2N/A ((entry = scf_entry_create(handle)) == NULL) ||
2N/A ((prop = scf_property_create(handle)) == NULL) ||
2N/A ((valueIter = scf_iter_create(handle)) == NULL)) {
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_service_get_pg(svc, FCOE_PG_NAME, pg) == -1) {
2N/A pg_or_prop_not_found = 1;
2N/A goto out;
2N/A }
2N/A
2N/A if (scf_pg_get_property(pg, FCOE_PORT_LIST, prop) == -1) {
2N/A pg_or_prop_not_found = 1;
2N/A goto out;
2N/A }
2N/A
2N/A valueLookup = scf_value_create(handle);
2N/A if (valueLookup == NULL) {
2N/A syslog(LOG_ERR, "scf value alloc failed - %s",
2N/A scf_strerror(scf_error()));
2N/A goto out;
2N/A }
2N/A
2N/A portIndex = 0;
2N/A
2N/A do {
2N/A if (scf_iter_property_values(valueIter, prop) == -1) {
2N/A syslog(LOG_ERR, "iter value failed - %s",
2N/A scf_strerror(scf_error()));
2N/A goto out;
2N/A }
2N/A
2N/A retry = 0;
2N/A bufsize = sizeof (FCOE_SMF_PORT_INSTANCE) * (size - 1) +
2N/A sizeof (FCOE_SMF_PORT_LIST);
2N/A *portlist = (PFCOE_SMF_PORT_LIST)malloc(bufsize);
2N/A
2N/A while (scf_iter_next_value(valueIter, valueLookup) == 1) {
2N/A uint8_t *macLinkName = NULL;
2N/A char *remainder = NULL;
2N/A uint64_t nodeWWN, portWWN;
2N/A int is_target, is_promiscuous;
2N/A
2N/A bzero(buf, sizeof (buf));
2N/A if (scf_value_get_ustring(valueLookup, buf,
2N/A MAXNAMELEN) == -1) {
2N/A syslog(LOG_ERR, "iter value failed - %s",
2N/A scf_strerror(scf_error()));
2N/A break;
2N/A }
2N/A macLinkName = (uint8_t *)strtok(buf, ":");
2N/A remainder = strtok(NULL, "#");
2N/A (void) sscanf(remainder,
2N/A "%016" PRIx64 ":%016" PRIx64 ":%d:%d",
2N/A &portWWN, &nodeWWN, &is_target, &is_promiscuous);
2N/A
2N/A if (portIndex >= size) {
2N/A free(*portlist);
2N/A retry = 1;
2N/A size *= 2;
2N/A break;
2N/A } else {
2N/A PFCOE_SMF_PORT_INSTANCE pi =
2N/A &(*portlist)->ports[portIndex++];
2N/A (void) strcpy((char *)pi->mac_link_name,
2N/A (char *)macLinkName);
2N/A pi->port_type = is_target ?
2N/A FCOE_PORTTYPE_TARGET:
2N/A FCOE_PORTTYPE_INITIATOR;
2N/A portWWN = htonll(portWWN);
2N/A nodeWWN = htonll(nodeWWN);
2N/A (void) memcpy(&pi->port_pwwn, &portWWN,
2N/A sizeof (FCOE_PORT_WWN));
2N/A (void) memcpy(&pi->port_nwwn, &nodeWWN,
2N/A sizeof (FCOE_PORT_WWN));
2N/A pi->mac_promisc = is_promiscuous;
2N/A }
2N/A }
2N/A
2N/A (*portlist)->port_num = portIndex;
2N/A } while (retry == 1);
2N/A
2N/A return (FCOE_STATUS_OK);
2N/Aout:
2N/A /*
2N/A * Free resources
2N/A */
2N/A if (handle != NULL) {
2N/A scf_handle_destroy(handle);
2N/A }
2N/A if (svc != NULL) {
2N/A scf_service_destroy(svc);
2N/A }
2N/A if (pg != NULL) {
2N/A scf_pg_destroy(pg);
2N/A }
2N/A if (tran != NULL) {
2N/A scf_transaction_destroy(tran);
2N/A }
2N/A if (entry != NULL) {
2N/A scf_entry_destroy(entry);
2N/A }
2N/A if (prop != NULL) {
2N/A scf_property_destroy(prop);
2N/A }
2N/A if (valueIter != NULL) {
2N/A scf_iter_destroy(valueIter);
2N/A }
2N/A if (valueLookup != NULL) {
2N/A scf_value_destroy(valueLookup);
2N/A }
2N/A
2N/A if (pg_or_prop_not_found == 1) {
2N/A return (FCOE_STATUS_OK);
2N/A } else {
2N/A return (FCOE_STATUS_ERROR);
2N/A }
2N/A}
2N/A
2N/AFCOE_STATUS
2N/AFCOE_Set_Priority(FCOE_PORT_PROP *fpp, uint8_t pvalue)
2N/A{
2N/A if (fpp == NULL)
2N/A return (FCOE_STATUS_ERROR_INVAL_ARG);
2N/A
2N/A fpp->port_pmask |= FCOE_DCB_PRIORITY;
2N/A fpp->port_priority = pvalue;
2N/A
2N/A return (FCOE_STATUS_OK);
2N/A}
2N/A
2N/AFCOE_STATUS
2N/AFCOE_SetpropPort(const FCOE_UINT8 *macLinkName, const FCOE_PORT_PROP *fpp)
2N/A{
2N/A FCOE_STATUS status = FCOE_STATUS_OK;
2N/A int fcoe_fd;
2N/A fcoeio_t fcoeio;
2N/A dladm_handle_t handle;
2N/A datalink_id_t linkid;
2N/A int io_ret = 0;
2N/A fcoeio_setprop_port_t fsp;
2N/A
2N/A /* the only supported property is the DCB priority */
2N/A if (macLinkName == NULL || fpp == NULL ||
2N/A fpp->port_pmask != FCOE_DCB_PRIORITY) {
2N/A return (FCOE_STATUS_ERROR_INVAL_ARG);
2N/A }
2N/A
2N/A if (strlen((char *)macLinkName) > MAXLINKNAMELEN-1) {
2N/A return (FCOE_STATUS_ERROR_MAC_LEN);
2N/A }
2N/A if (dladm_open(&handle, NETADM_ACTIVE_PROFILE) != DLADM_STATUS_OK) {
2N/A return (FCOE_STATUS_ERROR);
2N/A }
2N/A
2N/A if (dladm_name2info(handle, (const char *)macLinkName,
2N/A &linkid, NULL, NULL, NULL) != DLADM_STATUS_OK) {
2N/A dladm_close(handle);
2N/A return (FCOE_STATUS_ERROR_GET_LINKINFO);
2N/A }
2N/A dladm_close(handle);
2N/A
2N/A if ((status = openFcoe(OPEN_FCOE, &fcoe_fd)) != FCOE_STATUS_OK) {
2N/A return (status);
2N/A }
2N/A
2N/A bzero(&fsp, sizeof (fsp));
2N/A fsp.fsp_mac_linkid = linkid;
2N/A fsp.fsp_mask = fpp->port_pmask;
2N/A fsp.fsp_priority = fpp->port_priority;
2N/A
2N/A (void) memset(&fcoeio, 0, sizeof (fcoeio));
2N/A fcoeio.fcoeio_cmd = FCOEIO_SETPROP_FCOE_PORT;
2N/A fcoeio.fcoeio_ilen = sizeof (fcoeio_setprop_port_t);
2N/A fcoeio.fcoeio_xfer = FCOEIO_XFER_WRITE;
2N/A fcoeio.fcoeio_ibuf = (uintptr_t)&fsp;
2N/A
2N/A io_ret = ioctl(fcoe_fd, FCOEIO_CMD, &fcoeio);
2N/A if (io_ret != 0) {
2N/A switch (fcoeio.fcoeio_status) {
2N/A case FCOEIOE_INVAL_ARG:
2N/A status = FCOE_STATUS_ERROR_INVAL_ARG;
2N/A break;
2N/A
2N/A case FCOEIOE_MAC_NOT_FOUND:
2N/A status = FCOE_STATUS_ERROR_MAC_NOT_FOUND;
2N/A break;
2N/A
2N/A default:
2N/A status = FCOE_STATUS_ERROR;
2N/A }
2N/A }
2N/A (void) close(fcoe_fd);
2N/A return (status);
2N/A}