"Fossies" - the Fresh Open Source Software archive

Member "open-fcoe/usr/ofc/lib/libhbaofc/src/ofc_test.c" of archive open-fcoe.tar.gz:


/*
 * Copyright(c) 2007 Intel Corporation. All rights reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Maintained at www.Open-FCoE.org
 */
#include <sys/types.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include "sa_log.h"
#include "sa_assert.h"
#include "hbaapi.h"
#include "vendorhbaapi.h"
#include "ofc_api_lib.h"
#include "ofc_adapt_impl.h"
#include "fc_types.h"
#include "fc_fs.h"
#include "openfc_ioctl.h"

/*
 * Code for test pseudo-adapter.
 */

#define OFC_TEST_HBAS       1           /* number of adapters */
#define OFC_TEST_PORTS      4           /* number of ports */
#define OFC_TEST_FD         20          /* fake file descriptor value */

static const fc_wwn_t ofc_test_wwnn = 0x1000456789abcdefULL;
static const fc_wwn_t ofc_test_wwpn = 0x2000456789abcdefULL;
static const fc_wwn_t ofc_test_fab_name = 0x1000456789abcdffULL;

static int ofc_test_get_hba_info(struct ofc_io_hba_info *);
static int ofc_test_get_port_info(struct ofc_io_port_info *);
static int ofc_test_get_port_stats(struct ofc_io_port_stats *);
static int ofc_test_get_fc4_stats(struct ofc_io_fc4_stats *);

int
ofc_open(char *name, int opt)
{
    return (OFC_TEST_FD);
}

int
ofc_close(int fd)
{
    ASSERT(fd == OFC_TEST_FD);
    return (0);
}

int
ofc_ioctl(int fd, int cmd, void *arg)
{
    int rc = 0;
    int error = 0;
    
    ASSERT(fd == OFC_TEST_FD);

    switch (cmd) {
    case OFC_GET_VERSION:
        * (int *) arg = OFC_IOCTL_VER;
        break;
    case OFC_GET_HBA_COUNT:
        * (int *) arg = OFC_TEST_HBAS;
        break;
    case OFC_GET_HBA_INFO:
        error = ofc_test_get_hba_info((struct ofc_io_hba_info *) arg);
        break;
    case OFC_GET_PORT_INFO:
        error = ofc_test_get_port_info((struct ofc_io_port_info *) arg);
        break;
    case OFC_GET_PORT_STATS:
        error = ofc_test_get_port_stats((struct ofc_io_port_stats *) arg);
        break;
    case OFC_GET_FC4_STATS:
        error = ofc_test_get_fc4_stats((struct ofc_io_fc4_stats *) arg);
        break;
    default:
        error = EINVAL;
        break;
    }
    if (error) {
        errno = error;
        rc = -1;
    }
    return (rc);
}

static int
ofc_test_get_hba_info(struct ofc_io_hba_info *hip)
{
    if (hip->hi_hba >= OFC_TEST_HBAS) {
        return (ENXIO);
    }
    hip->hi_port_count = OFC_TEST_PORTS;
    hip->hi_wwnn = ofc_test_wwnn;
    strncpy(hip->hi_node_sym_name, "node sym name",
      sizeof (hip->hi_node_sym_name));
    strncpy(hip->hi_model, "TEST123", sizeof (hip->hi_model));
    strncpy(hip->hi_vendor, OFC_API_VENDOR, sizeof (hip->hi_vendor));
    strncpy(hip->hi_sn, "serial", sizeof (hip->hi_sn));
    strncpy(hip->hi_model_desc, "Test code in libhbaofc",
      sizeof (hip->hi_model_desc));
    strncpy(hip->hi_node, "node", sizeof (hip->hi_node));
    strncpy(hip->hi_hw_vers, "h/w vers", sizeof (hip->hi_hw_vers));
    strncpy(hip->hi_fw_vers, "f/w vers", sizeof (hip->hi_fw_vers));
    strncpy(hip->hi_driver_name, "driver name", sizeof (hip->hi_driver_name));
    strncpy(hip->hi_driver_vers, "driver vers", sizeof (hip->hi_driver_vers));
    strncpy(hip->hi_opt_rom_vers, "opt rom vers",
      sizeof (hip->hi_opt_rom_vers));
    return (0);
}

static int
ofc_test_get_port_info(struct ofc_io_port_info *pip)
{
    if (pip->pi_hba >= OFC_TEST_HBAS || pip->pi_port >= OFC_TEST_PORTS) {
        return (ENXIO);
    }
    pip->pi_wwnn = ofc_test_wwnn; 
    pip->pi_wwpn = ofc_test_wwpn + ((u_quad_t) pip->pi_port << 48);
    pip->pi_fab_name = ofc_test_fab_name;
    pip->pi_fcid = 0x112233;
    pip->pi_port_type = OFC_PTYPE_N;
    pip->pi_port_state = OFC_PSTATE_ONLINE;
    pip->pi_speed = OFC_SPEED_10GBIT;
    pip->pi_speed_support = OFC_SPEED_1GBIT | OFC_SPEED_2GBIT |
      OFC_SPEED_4GBIT | OFC_SPEED_8GBIT | OFC_SPEED_10GBIT | OFC_SPEED_AUTO;
    pip->pi_port_mode = OFC_MODE_INIT;
    pip->pi_max_frame_size = 2112;
    pip->pi_class = 3;
    pip->pi_fc4_support[0] = 8;     /* types 1, 3 */
    pip->pi_fc4_support[1] = 1;     /* type 8 */
    pip->pi_fc4_active[1] = 1;      /* type 8 */
    pip->pi_disc_ports = 0;
    strncpy(pip->pi_os_dev_name, "local_port_dev_name",
      sizeof (pip->pi_os_dev_name));
    strncpy(pip->pi_port_sym_name, "port symbolic name",
      sizeof (pip->pi_port_sym_name));
    return (0);
}

static time_t
ofc_test_time_since_reset(void)
{
    time_t t;
    static time_t reset_time;

    time(&t);
    if (reset_time == 0) {
        reset_time = t - 1;
    }
    return (t - reset_time);
}

static int
ofc_test_get_port_stats(struct ofc_io_port_stats *isp)
{
    struct ofc_port_stats *psp;
    time_t t;

    if (isp->ps_hba >= OFC_TEST_HBAS || isp->ps_port >= OFC_TEST_PORTS) {
        return (ENXIO);
    }
    psp = &isp->ps_stats;
    memset(psp, 0xff, sizeof (*psp));       /* invalid stats = -1 */
    t = ofc_test_time_since_reset();
    psp->ps_sec_since_reset = t;
    psp->ps_tx_frames = 1000 * t;
    psp->ps_tx_words = 16 * 1000 * t;
    psp->ps_rx_frames = 2000 * t;
    psp->ps_rx_frames = ((2112 + 24 + 64) / 4) / t;
    psp->ps_LIP_count = 1;
    psp->ps_NOS_count = 2;
    psp->ps_error_frames = 3;
    psp->ps_dumped_frames = 4;
    psp->ps_link_fails = 5;
    psp->ps_loss_of_sync = 6;
    psp->ps_loss_of_signal = 7;
    psp->ps_primitive_seq_proto_errs = 8;
    psp->ps_invalid_tx_words = 9;
    psp->ps_invalid_CRC_count = 10;
    return (0);
}

static int
ofc_test_get_fc4_stats(struct ofc_io_fc4_stats *ifp)
{
    struct ofc_fc4_stats *fp;
    time_t t;

    if (ifp->fs_hba >= OFC_TEST_HBAS || ifp->fs_port >= OFC_TEST_PORTS) {
        return (ENXIO);
    }
    if (ifp->fs_fc4_type != FC_TYPE_FCP) {
        return (ENXIO);
    }
    fp = &ifp->fs_stats;
    memset(fp, 0xff, sizeof (*fp));             /* invalid stats = -1 */
    t = ofc_test_time_since_reset();
    fp->fs_sec_since_reset = t;
    fp->fs_in_req = 1000 * t;
    fp->fs_out_req = 2;
    fp->fs_ctl_req = 3;
    fp->fs_in_bytes = 512 * 1000 * t;
    fp->fs_out_bytes = 0;
    return (0);
}