summaryrefslogtreecommitdiff
path: root/usr/src/lib/libfcoe/common
diff options
context:
space:
mode:
Diffstat (limited to 'usr/src/lib/libfcoe/common')
-rw-r--r--usr/src/lib/libfcoe/common/libfcoe.c347
-rw-r--r--usr/src/lib/libfcoe/common/libfcoe.h120
-rw-r--r--usr/src/lib/libfcoe/common/llib-lfcoe29
-rw-r--r--usr/src/lib/libfcoe/common/mapfile-vers48
4 files changed, 544 insertions, 0 deletions
diff --git a/usr/src/lib/libfcoe/common/libfcoe.c b/usr/src/lib/libfcoe/common/libfcoe.c
new file mode 100644
index 0000000000..f06a620f26
--- /dev/null
+++ b/usr/src/lib/libfcoe/common/libfcoe.c
@@ -0,0 +1,347 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <wchar.h>
+#include <strings.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <libintl.h>
+#include <errno.h>
+#include <string.h>
+#include <assert.h>
+#include <syslog.h>
+#include <libfcoe.h>
+#include <fcoeio.h>
+
+#define FCOE_DEV_PATH "/devices/fcoe:admin"
+
+#define OPEN_FCOE 0
+#define OPEN_EXCL_FCOE O_EXCL
+
+/*
+ * Open for fcoe module
+ *
+ * flag - open flag (OPEN_FCOE, OPEN_EXCL_FCOE)
+ * fd - pointer to integer. On success, contains the fcoe file descriptor
+ */
+static int
+openFcoe(int flag, int *fd)
+{
+ int ret = FCOE_STATUS_ERROR;
+
+ if ((*fd = open(FCOE_DEV_PATH, O_NDELAY | O_RDONLY | flag)) != -1) {
+ ret = FCOE_STATUS_OK;
+ } else {
+ if (errno == EPERM || errno == EACCES) {
+ ret = FCOE_STATUS_ERROR_PERM;
+ } else {
+ ret = FCOE_STATUS_ERROR_OPEN_DEV;
+ }
+ syslog(LOG_DEBUG, "openFcoe:open failure:%s:errno(%d)",
+ FCOE_DEV_PATH, errno);
+ }
+
+ return (ret);
+}
+
+static int
+isWWNZero(FCOE_PORT_WWN portwwn)
+{
+ int i;
+ int size = sizeof (FCOE_PORT_WWN);
+
+ for (i = 0; i < size; i++) {
+ if (portwwn.wwn[i] != 0) {
+ return (0);
+ }
+ }
+ return (1);
+}
+
+FCOE_STATUS
+FCOE_CreatePort(
+ const FCOE_UINT8 *macLinkName,
+ FCOE_UINT8 portType,
+ FCOE_PORT_WWN pwwn,
+ FCOE_PORT_WWN nwwn,
+ FCOE_UINT8 promiscuous)
+{
+ FCOE_STATUS status = FCOE_STATUS_OK;
+ int fcoe_fd;
+ fcoeio_t fcoeio;
+ fcoeio_create_port_param_t param;
+
+ bzero(&param, sizeof (fcoeio_create_port_param_t));
+
+ if (macLinkName == NULL) {
+ return (FCOE_STATUS_ERROR_INVAL_ARG);
+ }
+
+ if (portType != FCOE_PORTTYPE_INITIATOR &&
+ portType != FCOE_PORTTYPE_TARGET) {
+ return (FCOE_STATUS_ERROR_INVAL_ARG);
+ }
+
+ if (!isWWNZero(pwwn)) {
+ param.fcp_pwwn_provided = 1;
+ bcopy(pwwn.wwn, param.fcp_pwwn, 8);
+ }
+
+ if (!isWWNZero(nwwn)) {
+ param.fcp_nwwn_provided = 1;
+ bcopy(nwwn.wwn, param.fcp_nwwn, 8);
+ }
+
+ if (param.fcp_pwwn_provided == 1 &&
+ param.fcp_nwwn_provided == 1 &&
+ bcmp(&pwwn, &nwwn, 8) == 0) {
+ return (FCOE_STATUS_ERROR_WWN_SAME);
+ }
+
+ if (strlen((char *)macLinkName) > FCOE_MAX_MAC_NAME_LEN-1) {
+ return (FCOE_STATUS_ERROR_MAC_LEN);
+ }
+
+ param.fcp_force_promisc = promiscuous;
+ (void) strcpy((char *)param.fcp_mac_name, (char *)macLinkName);
+ param.fcp_port_type = (fcoe_cli_type_t)portType;
+
+ if ((status = openFcoe(OPEN_FCOE, &fcoe_fd)) != FCOE_STATUS_OK) {
+ return (status);
+ }
+
+ (void) memset(&fcoeio, 0, sizeof (fcoeio));
+ fcoeio.fcoeio_cmd = FCOEIO_CREATE_FCOE_PORT;
+
+ fcoeio.fcoeio_ilen = sizeof (param);
+ fcoeio.fcoeio_xfer = FCOEIO_XFER_WRITE;
+ fcoeio.fcoeio_ibuf = (uintptr_t)&param;
+
+ if (ioctl(fcoe_fd, FCOEIO_CMD, &fcoeio) != 0) {
+ switch (fcoeio.fcoeio_status) {
+ case FCOEIOE_INVAL_ARG:
+ status = FCOE_STATUS_ERROR_INVAL_ARG;
+ break;
+
+ case FCOEIOE_BUSY:
+ status = FCOE_STATUS_ERROR_BUSY;
+ break;
+
+ case FCOEIOE_ALREADY:
+ status = FCOE_STATUS_ERROR_ALREADY;
+ break;
+
+ case FCOEIOE_PWWN_CONFLICTED:
+ status = FCOE_STATUS_ERROR_PWWN_CONFLICTED;
+ break;
+
+ case FCOEIOE_NWWN_CONFLICTED:
+ status = FCOE_STATUS_ERROR_NWWN_CONFLICTED;
+ break;
+
+ case FCOEIOE_CREATE_MAC:
+ status = FCOE_STATUS_ERROR_CREATE_MAC;
+ break;
+
+ case FCOEIOE_OPEN_MAC:
+ status = FCOE_STATUS_ERROR_OPEN_MAC;
+ break;
+
+ case FCOEIOE_CREATE_PORT:
+ status = FCOE_STATUS_ERROR_CREATE_PORT;
+ break;
+
+ case FCOEIOE_NEED_JUMBO_FRAME:
+ status = FCOE_STATUS_ERROR_NEED_JUMBO_FRAME;
+ break;
+
+ case FCOEIOE_VNIC_UNSUPPORT:
+ status = FCOE_STATUS_ERROR_VNIC_UNSUPPORT;
+ break;
+
+ default:
+ status = FCOE_STATUS_ERROR;
+ }
+ } else {
+ status = FCOE_STATUS_OK;
+ }
+ (void) close(fcoe_fd);
+ return (status);
+}
+
+FCOE_STATUS
+FCOE_DeletePort(const FCOE_UINT8 *macLinkName)
+{
+ FCOE_STATUS status = FCOE_STATUS_OK;
+ int fcoe_fd;
+ fcoeio_t fcoeio;
+
+ if (macLinkName == NULL) {
+ return (FCOE_STATUS_ERROR_INVAL_ARG);
+ }
+
+ if (strlen((char *)macLinkName) > FCOE_MAX_MAC_NAME_LEN-1) {
+ return (FCOE_STATUS_ERROR_MAC_LEN);
+ }
+
+ if ((status = openFcoe(OPEN_FCOE, &fcoe_fd)) != FCOE_STATUS_OK) {
+ return (status);
+ }
+
+ (void) memset(&fcoeio, 0, sizeof (fcoeio));
+ fcoeio.fcoeio_cmd = FCOEIO_DELETE_FCOE_PORT;
+
+ fcoeio.fcoeio_ilen = strlen((char *)macLinkName)+1;
+ fcoeio.fcoeio_xfer = FCOEIO_XFER_WRITE;
+ fcoeio.fcoeio_ibuf = (uintptr_t)macLinkName;
+
+ if (ioctl(fcoe_fd, FCOEIO_CMD, &fcoeio) != 0) {
+ switch (fcoeio.fcoeio_status) {
+ case FCOEIOE_INVAL_ARG:
+ status = FCOE_STATUS_ERROR_INVAL_ARG;
+ break;
+
+ case FCOEIOE_BUSY:
+ status = FCOE_STATUS_ERROR_BUSY;
+ break;
+
+ case FCOEIOE_ALREADY:
+ status = FCOE_STATUS_ERROR_ALREADY;
+ break;
+
+ case FCOEIOE_MAC_NOT_FOUND:
+ status = FCOE_STATUS_ERROR_MAC_NOT_FOUND;
+ break;
+
+ case FCOEIOE_OFFLINE_FAILURE:
+ status = FCOE_STATUS_ERROR_OFFLINE_DEV;
+ break;
+
+ default:
+ status = FCOE_STATUS_ERROR;
+ }
+ } else {
+ status = FCOE_STATUS_OK;
+ }
+ (void) close(fcoe_fd);
+ return (status);
+}
+
+FCOE_STATUS
+FCOE_GetPortList(
+ FCOE_UINT32 *port_num,
+ FCOE_PORT_ATTRIBUTE **portlist)
+{
+ FCOE_STATUS status = FCOE_STATUS_OK;
+ int fcoe_fd;
+ fcoeio_t fcoeio;
+ fcoe_port_list_t *inportlist = NULL;
+ FCOE_PORT_ATTRIBUTE *outportlist = NULL;
+ int i;
+ int size = 64; /* default first attempt */
+ int retry = 0;
+ int bufsize;
+
+ if (port_num == NULL || portlist == NULL) {
+ return (FCOE_STATUS_ERROR_INVAL_ARG);
+ }
+ *port_num = 0;
+
+ if ((status = openFcoe(OPEN_FCOE, &fcoe_fd)) != FCOE_STATUS_OK) {
+ return (status);
+ }
+
+ /* Get fcoe port list */
+ (void) memset(&fcoeio, 0, sizeof (fcoeio));
+ retry = 0;
+
+ do {
+ bufsize = sizeof (fcoe_port_instance_t) * (size - 1) +
+ sizeof (fcoe_port_list_t);
+ inportlist = (fcoe_port_list_t *)malloc(bufsize);
+ fcoeio.fcoeio_cmd = FCOEIO_GET_FCOE_PORT_LIST;
+ fcoeio.fcoeio_olen = bufsize;
+ fcoeio.fcoeio_xfer = FCOEIO_XFER_READ;
+ fcoeio.fcoeio_obuf = (uintptr_t)inportlist;
+
+ if (ioctl(fcoe_fd, FCOEIO_CMD, &fcoeio) != 0) {
+ if (fcoeio.fcoeio_status == FCOEIOE_MORE_DATA) {
+ size = inportlist->numPorts;
+ }
+ free(inportlist);
+ switch (fcoeio.fcoeio_status) {
+ case FCOEIOE_INVAL_ARG:
+ status = FCOE_STATUS_ERROR_INVAL_ARG;
+ (void) close(fcoe_fd);
+ return (status);
+
+ case FCOEIOE_BUSY:
+ status = FCOE_STATUS_ERROR_BUSY;
+ retry++;
+ break;
+
+ case FCOEIOE_MORE_DATA:
+ status = FCOE_STATUS_ERROR_MORE_DATA;
+ retry++;
+ default:
+ status = FCOE_STATUS_ERROR;
+ }
+ } else {
+ status = FCOE_STATUS_OK;
+ break;
+ }
+ } while (retry <= 3 && status != FCOE_STATUS_OK);
+
+ if (status == FCOE_STATUS_OK) {
+ outportlist = (PFCOE_PORT_ATTRIBUTE)
+ malloc(sizeof (FCOE_PORT_ATTRIBUTE) * inportlist->numPorts);
+
+ for (i = 0; i < inportlist->numPorts; i++) {
+ fcoe_port_instance_t *pi = &inportlist->ports[i];
+ FCOE_PORT_ATTRIBUTE *po = &outportlist[i];
+ bcopy(pi->fpi_pwwn, &po->port_wwn, 8);
+ bcopy(pi->fpi_mac_link_name, po->mac_link_name, 32);
+ bcopy(pi->fpi_mac_factory_addr,
+ po->mac_factory_addr, 6);
+ bcopy(pi->fpi_mac_current_addr,
+ po->mac_current_addr, 6);
+ po->port_type = (FCOE_UINT8)pi->fpi_port_type;
+ po->mtu_size = pi->fpi_mtu_size;
+ po->mac_promisc = pi->fpi_mac_promisc;
+ }
+ *port_num = inportlist->numPorts;
+ *portlist = outportlist;
+ free(inportlist);
+ } else {
+ *port_num = 0;
+ *portlist = NULL;
+ }
+ (void) close(fcoe_fd);
+ return (status);
+}
diff --git a/usr/src/lib/libfcoe/common/libfcoe.h b/usr/src/lib/libfcoe/common/libfcoe.h
new file mode 100644
index 0000000000..30570205a7
--- /dev/null
+++ b/usr/src/lib/libfcoe/common/libfcoe.h
@@ -0,0 +1,120 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#ifndef _LIBFCOE_H
+#define _LIBFCOE_H
+
+#include <time.h>
+#include <wchar.h>
+#include <sys/param.h>
+#include <sys/ethernet.h>
+#include <libnvpair.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * FCOE Port Type
+ */
+#define FCOE_PORTTYPE_INITIATOR 0
+#define FCOE_PORTTYPE_TARGET 1
+
+#define FCOE_MAX_MAC_NAME_LEN 32
+typedef unsigned char FCOE_UINT8;
+typedef char FCOE_INT8;
+typedef unsigned short FCOE_UINT16;
+typedef short FCOE_INT16;
+typedef unsigned int FCOE_UINT32;
+typedef int FCOE_INT32;
+
+typedef unsigned int FCOE_STATUS;
+
+#define FCOE_STATUS_OK 0
+#define FCOE_STATUS_ERROR 1
+#define FCOE_STATUS_ERROR_INVAL_ARG 2
+#define FCOE_STATUS_ERROR_BUSY 3
+#define FCOE_STATUS_ERROR_ALREADY 4
+#define FCOE_STATUS_ERROR_PERM 5
+#define FCOE_STATUS_ERROR_OPEN_DEV 6
+#define FCOE_STATUS_ERROR_WWN_SAME 7
+#define FCOE_STATUS_ERROR_MAC_LEN 8
+#define FCOE_STATUS_ERROR_PWWN_CONFLICTED 9
+#define FCOE_STATUS_ERROR_NWWN_CONFLICTED 10
+#define FCOE_STATUS_ERROR_NEED_JUMBO_FRAME 11
+#define FCOE_STATUS_ERROR_CREATE_MAC 12
+#define FCOE_STATUS_ERROR_OPEN_MAC 13
+#define FCOE_STATUS_ERROR_CREATE_PORT 14
+#define FCOE_STATUS_ERROR_MAC_NOT_FOUND 15
+#define FCOE_STATUS_ERROR_OFFLINE_DEV 16
+#define FCOE_STATUS_ERROR_MORE_DATA 17
+#define FCOE_STATUS_ERROR_VNIC_UNSUPPORT 18
+
+typedef struct fcoe_port_wwn {
+ uchar_t wwn[8];
+} FCOE_PORT_WWN, *PFCOE_PORT_WWN;
+
+typedef struct fcoe_port_attr {
+ FCOE_PORT_WWN port_wwn;
+ FCOE_UINT8 mac_link_name[MAXLINKNAMELEN];
+ FCOE_UINT8 mac_factory_addr[ETHERADDRL];
+ FCOE_UINT8 mac_current_addr[ETHERADDRL];
+ FCOE_UINT8 port_type;
+ FCOE_UINT32 mtu_size;
+ FCOE_UINT8 mac_promisc;
+} FCOE_PORT_ATTRIBUTE, *PFCOE_PORT_ATTRIBUTE;
+
+/*
+ * macLinkName: mac name with maximum lenth 32
+ * portType: 0 (Initiator)/ 1(Target)
+ * pwwn: Port WWN
+ * nwwn: Nodw WWN
+ * promiscous: to enable promisc mode for mac interface
+ */
+FCOE_STATUS FCOE_CreatePort(
+ const FCOE_UINT8 *macLinkName, /* maximum len: 32 */
+ FCOE_UINT8 portType,
+ FCOE_PORT_WWN pwwn,
+ FCOE_PORT_WWN nwwn,
+ FCOE_UINT8 promiscusous
+);
+
+FCOE_STATUS FCOE_DeletePort(
+ const FCOE_UINT8 *macLinkName
+);
+
+/*
+ * Make sure to free the memory pointed by portlist
+ */
+FCOE_STATUS FCOE_GetPortList(
+ FCOE_UINT32 *port_num,
+ FCOE_PORT_ATTRIBUTE **portlist
+);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _LIBFCOE_H */
diff --git a/usr/src/lib/libfcoe/common/llib-lfcoe b/usr/src/lib/libfcoe/common/llib-lfcoe
new file mode 100644
index 0000000000..888c17113e
--- /dev/null
+++ b/usr/src/lib/libfcoe/common/llib-lfcoe
@@ -0,0 +1,29 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+/*LINTLIBRARY*/
+/*PROTOLIB1*/
+
+#include <libfcoe.h>
diff --git a/usr/src/lib/libfcoe/common/mapfile-vers b/usr/src/lib/libfcoe/common/mapfile-vers
new file mode 100644
index 0000000000..ba73d9e4dd
--- /dev/null
+++ b/usr/src/lib/libfcoe/common/mapfile-vers
@@ -0,0 +1,48 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+#
+# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+
+#
+# MAPFILE HEADER START
+#
+# WARNING: STOP NOW. DO NOT MODIFY THIS FILE.
+# Object versioning must comply with the rules detailed in
+#
+# usr/src/lib/README.mapfiles
+#
+# You should not be making modifications here until you've read the most current
+# copy of that file. If you need help, contact a gatekeeper for guidance.
+#
+# MAPFILE HEADER END
+#
+
+SUNWprivate {
+ global:
+ FCOE_CreatePort;
+ FCOE_DeletePort;
+ FCOE_GetPortList;
+
+ local:
+ *;
+};