summaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/Kconfig1
-rw-r--r--net/Makefile1
-rw-r--r--net/ncsi/Kconfig10
-rw-r--r--net/ncsi/Makefile5
-rw-r--r--net/ncsi/internal.h159
-rw-r--r--net/ncsi/ncsi-aen.c197
-rw-r--r--net/ncsi/ncsi-cmd.c371
-rw-r--r--net/ncsi/ncsi-manage.c989
-rw-r--r--net/ncsi/ncsi-pkt.h391
-rw-r--r--net/ncsi/ncsi-rsp.c1171
10 files changed, 3295 insertions, 0 deletions
diff --git a/net/Kconfig b/net/Kconfig
index 127da94ae25e..f320b1fac5a1 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -233,6 +233,7 @@ source "net/mpls/Kconfig"
source "net/hsr/Kconfig"
source "net/switchdev/Kconfig"
source "net/l3mdev/Kconfig"
+source "net/ncsi/Kconfig"
config RPS
bool
diff --git a/net/Makefile b/net/Makefile
index a5d04098dfce..4d81a6ea74a2 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -77,3 +77,4 @@ endif
ifneq ($(CONFIG_NET_L3_MASTER_DEV),)
obj-y += l3mdev/
endif
+obj-$(CONFIG_NET_NCSI) += ncsi/
diff --git a/net/ncsi/Kconfig b/net/ncsi/Kconfig
new file mode 100644
index 000000000000..723f0ebd58e6
--- /dev/null
+++ b/net/ncsi/Kconfig
@@ -0,0 +1,10 @@
+#
+# Configuration for NCSI support
+#
+
+config NET_NCSI
+ bool "NCSI interface support (EXPERIMENTAL)"
+ depends on INET
+ ---help---
+ This module provides NCSI (Network Controller Sideband Interface)
+ support.
diff --git a/net/ncsi/Makefile b/net/ncsi/Makefile
new file mode 100644
index 000000000000..e4094c27538e
--- /dev/null
+++ b/net/ncsi/Makefile
@@ -0,0 +1,5 @@
+#
+# Makefile for NCSI API
+#
+obj-$(CONFIG_NET_NCSI) += ncsi-cmd.o ncsi-rsp.o ncsi-aen.o \
+ ncsi-manage.o
diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
new file mode 100644
index 000000000000..9476652f98e2
--- /dev/null
+++ b/net/ncsi/internal.h
@@ -0,0 +1,159 @@
+#ifndef __NCSI_INTERNAL_H__
+#define __NCSI_INTERNAL_H__
+
+struct ncsi_dev_priv;
+struct ncsi_package;
+
+#define NCSI_PACKAGE_INDEX(c) (((c) >> 5) & 0x7)
+#define NCSI_RESERVED_CHANNEL 0x1f
+#define NCSI_CHANNEL_INDEX(c) ((c) & 0x1ffff)
+#define NCSI_TO_CHANNEL(p, c) ((((p) & 0x7) << 5) | ((c) & 0x1ffff))
+
+/* Channel state */
+enum {
+ ncsi_channel_state_deselected_initial,
+ ncsi_channel_state_selected_initial,
+ ncsi_channel_state_deselected_ready,
+ ncsi_channel_state_selected_ready,
+};
+
+struct ncsi_channel {
+ unsigned char nc_id;
+ int nc_state;
+ struct ncsi_package *nc_package;
+ struct ncsi_channel_version nc_version;
+ struct ncsi_channel_cap nc_caps[NCSI_CAP_MAX];
+ struct ncsi_channel_mode nc_modes[NCSI_MODE_MAX];
+ struct ncsi_channel_filter *nc_filters[NCSI_FILTER_MAX];
+ struct ncsi_channel_stats nc_stats;
+ struct list_head nc_node;
+};
+
+struct ncsi_package {
+ unsigned char np_id;
+ struct ncsi_dev_priv *np_ndp;
+ atomic_t np_channel_num;
+ spinlock_t np_channel_lock;
+ struct list_head np_channels;
+ struct list_head np_node;
+};
+
+struct ncsi_req {
+ unsigned char nr_id;
+ bool nr_used;
+ unsigned int nr_flags;
+#define NCSI_REQ_FLAG_EVENT_DRIVEN 1
+ struct ncsi_dev_priv *nr_ndp;
+ struct sk_buff *nr_cmd;
+ struct sk_buff *nr_rsp;
+ struct timer_list nr_timer;
+ bool nr_timer_enabled;
+};
+
+enum {
+ ncsi_dev_state_major = 0xff00,
+ ncsi_dev_state_minor = 0x00ff,
+ ncsi_dev_state_probe_deselect = 0x0201,
+ ncsi_dev_state_probe_package,
+ ncsi_dev_state_probe_channel,
+ ncsi_dev_state_probe_cis,
+ ncsi_dev_state_probe_gvi,
+ ncsi_dev_state_probe_gc,
+ ncsi_dev_state_probe_gls,
+ ncsi_dev_state_probe_dp,
+ ncsi_dev_state_config_sp = 0x0301,
+ ncsi_dev_state_config_cis,
+ ncsi_dev_state_config_sma,
+ ncsi_dev_state_config_ebf,
+ ncsi_dev_state_config_ecnt,
+ ncsi_dev_state_config_ec,
+ ncsi_dev_state_config_ae,
+ ncsi_dev_state_config_gls,
+ ncsi_dev_state_config_done,
+ ncsi_dev_state_suspend_select = 0x0401,
+ ncsi_dev_state_suspend_gls,
+ ncsi_dev_state_suspend_dcnt,
+ ncsi_dev_state_suspend_dc,
+ ncsi_dev_state_suspend_deselect,
+ ncsi_dev_state_suspend_done
+};
+
+struct ncsi_dev_priv {
+ struct ncsi_dev ndp_ndev;
+ int ndp_flags;
+#define NCSI_DEV_PRIV_FLAG_POPULATED 0x1
+#define NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE 0x2
+ struct ncsi_package *ndp_active_package;
+ struct ncsi_channel *ndp_active_channel;
+ atomic_t ndp_package_num;
+ spinlock_t ndp_package_lock;
+ struct list_head ndp_packages;
+ struct ncsi_channel *ndp_hot_channel;
+ atomic_t ndp_pending_reqs;
+ atomic_t ndp_last_req_idx;
+#define NCSI_REQ_START_IDX 1
+ spinlock_t ndp_req_lock;
+ struct ncsi_req ndp_reqs[256];
+ struct work_struct ndp_work;
+ struct packet_type ndp_ptype;
+ struct list_head ndp_node;
+};
+
+struct ncsi_cmd_arg {
+ struct ncsi_dev_priv *nca_ndp;
+ unsigned char nca_type;
+ unsigned char nca_id;
+ unsigned char nca_package;
+ unsigned char nca_channel;
+ unsigned short nca_payload;
+ unsigned int nca_portid;
+ unsigned int nca_req_flags;
+ union {
+ unsigned char nca_bytes[16];
+ unsigned short nca_words[8];
+ unsigned int nca_dwords[4];
+ };
+};
+
+extern struct net *ncsi_net;
+extern struct list_head ncsi_dev_list;
+extern spinlock_t ncsi_dev_lock;
+
+#define TO_NCSI_DEV_PRIV(nd) \
+ container_of(nd, struct ncsi_dev_priv, ndp_ndev)
+#define NCSI_FOR_EACH_DEV(ndp) \
+ list_for_each_entry_rcu(ndp, &ncsi_dev_list, ndp_node)
+#define NCSI_FOR_EACH_PACKAGE(ndp, np) \
+ list_for_each_entry_rcu(np, &ndp->ndp_packages, np_node)
+#define NCSI_FOR_EACH_CHANNEL(np, nc) \
+ list_for_each_entry_rcu(nc, &np->np_channels, nc_node)
+
+/* Resources */
+int ncsi_find_channel_filter(struct ncsi_channel *nc, int table, void *data);
+int ncsi_add_channel_filter(struct ncsi_channel *nc, int table, void *data);
+int ncsi_del_channel_filter(struct ncsi_channel *nc, int table, int index);
+struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np,
+ unsigned char id);
+struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
+ unsigned char id);
+struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
+ unsigned char id);
+struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
+ unsigned char id);
+void ncsi_release_package(struct ncsi_package *np);
+void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
+ unsigned char id,
+ struct ncsi_package **np,
+ struct ncsi_channel **nc);
+struct ncsi_req *ncsi_alloc_req(struct ncsi_dev_priv *ndp,
+ unsigned int req_flags);
+void ncsi_free_req(struct ncsi_req *nr);
+struct ncsi_dev *ncsi_find_dev(struct net_device *dev);
+int ncsi_config_dev(struct ncsi_dev *nd);
+
+/* Packet handlers */
+int ncsi_xmit_cmd(struct ncsi_cmd_arg *nca);
+int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
+ struct packet_type *pt, struct net_device *orig_dev);
+int ncsi_aen_handler(struct ncsi_dev_priv *ndp, struct sk_buff *skb);
+#endif /* __NCSI_INTERNAL_H__ */
diff --git a/net/ncsi/ncsi-aen.c b/net/ncsi/ncsi-aen.c
new file mode 100644
index 000000000000..26ac93dde60d
--- /dev/null
+++ b/net/ncsi/ncsi-aen.c
@@ -0,0 +1,197 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2015.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>
+
+#include <net/ncsi.h>
+#include <net/net_namespace.h>
+#include <net/sock.h>
+
+#include "internal.h"
+#include "ncsi-pkt.h"
+
+static int ncsi_validate_aen_pkt(struct ncsi_aen_pkt_hdr *h,
+ const unsigned short payload)
+{
+ unsigned char *stream;
+ __be32 *checksum, csum;
+ __be32 high, low;
+ int i;
+
+ if (h->common.revision != NCSI_PKT_REVISION)
+ return -EINVAL;
+ if (ntohs(h->common.length) != payload)
+ return -EINVAL;
+
+ /*
+ * Validate checksum, which might be zeroes if the
+ * sender doesn't support checksum according to NCSI
+ * specification.
+ */
+ checksum = (__be32 *)((void *)(h + 1) + payload - 4);
+ if (ntohl(*checksum) == 0)
+ return 0;
+
+ csum = 0;
+ stream = (unsigned char *)h;
+ for (i = 0; i < sizeof(*h) + payload - 4; i += 2) {
+ high = stream[i];
+ low = stream[i + 1];
+ csum += ((high << 8) | low);
+ }
+
+ csum = ~csum + 1;
+ if (*checksum != htonl(csum))
+ return -EINVAL;
+
+ return 0;
+}
+
+static int ncsi_aen_handler_lsc(struct ncsi_dev_priv *ndp,
+ struct ncsi_aen_pkt_hdr *h)
+{
+ struct ncsi_dev *nd = &ndp->ndp_ndev;
+ struct ncsi_aen_lsc_pkt *lsc;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_aen_pkt(h, 12);
+ if (ret)
+ return ret;
+
+ /* Find the NCSI channel */
+ ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Update the link status */
+ ncm = &nc->nc_modes[NCSI_MODE_LINK];
+ lsc = (struct ncsi_aen_lsc_pkt *)h;
+ ncm->ncm_data[2] = ntohl(lsc->status);
+ ncm->ncm_data[4] = ntohl(lsc->oem_status);
+ if (!ndp->ndp_active_channel ||
+ ndp->ndp_active_channel != nc)
+ return 0;
+
+ /* If this channel is the active one and the link is down,
+ * we have to choose another channel to be active one.
+ */
+ if (!(ncm->ncm_data[2] & 0x1))
+ ndp->ndp_flags |= NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE;
+ ncsi_suspend_dev(nd);
+
+ return 0;
+}
+
+static int ncsi_aen_handler_cr(struct ncsi_dev_priv *ndp,
+ struct ncsi_aen_pkt_hdr *h)
+{
+ struct ncsi_dev *nd = &ndp->ndp_ndev;
+ struct ncsi_channel *nc;
+ int ret;
+
+ ret = ncsi_validate_aen_pkt(h, 4);
+ if (ret)
+ return ret;
+
+ /* Find the NCSI channel */
+ ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* If the channel is active one, we need reconfigure it */
+ if (!ndp->ndp_active_channel ||
+ ndp->ndp_active_channel != nc)
+ return 0;
+
+ ncsi_config_dev(nd);
+
+ return 0;
+}
+
+static int ncsi_aen_handler_hncdsc(struct ncsi_dev_priv *ndp,
+ struct ncsi_aen_pkt_hdr *h)
+{
+ struct ncsi_dev *nd = &ndp->ndp_ndev;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ struct ncsi_aen_hncdsc_pkt *hncdsc;
+ int ret;
+
+ ret = ncsi_validate_aen_pkt(h, 4);
+ if (ret)
+ return ret;
+
+ /* Find the NCSI channel */
+ ncsi_find_package_and_channel(ndp, h->common.channel, NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* If the channel is active one, we need reconfigure it */
+ ncm = &nc->nc_modes[NCSI_MODE_LINK];
+ hncdsc = (struct ncsi_aen_hncdsc_pkt *)h;
+ ncm->ncm_data[3] = ntohl(hncdsc->status);
+ if (ndp->ndp_active_channel != nc)
+ return 0;
+
+ /* If this channel is the active one and the link doesn't
+ * work, we have to choose another channel to be active one.
+ * The logic here is exactly similar to what we do when link
+ * is down on the active channel.
+ */
+ if (!(ncm->ncm_data[3] & 0x1))
+ ndp->ndp_flags |= NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE;
+ ncsi_suspend_dev(nd);
+
+ return 0;
+}
+
+static struct ncsi_aen_handler {
+ unsigned char nah_type;
+ int (*nah_handler)(struct ncsi_dev_priv *ndp,
+ struct ncsi_aen_pkt_hdr *h);
+} ncsi_aen_handlers[] = {
+ { NCSI_PKT_AEN_LSC, ncsi_aen_handler_lsc },
+ { NCSI_PKT_AEN_CR, ncsi_aen_handler_cr },
+ { NCSI_PKT_AEN_HNCDSC, ncsi_aen_handler_hncdsc },
+ { 0, NULL }
+};
+
+int ncsi_aen_handler(struct ncsi_dev_priv *ndp, struct sk_buff *skb)
+{
+ struct ncsi_aen_pkt_hdr *h;
+ struct ncsi_aen_handler *nah;
+ int ret;
+
+ /* Find the handler */
+ h = (struct ncsi_aen_pkt_hdr *)skb_network_header(skb);
+ nah = ncsi_aen_handlers;
+ while (nah->nah_handler) {
+ if (nah->nah_type == h->type)
+ break;
+
+ nah++;
+ }
+
+ if (!nah->nah_handler) {
+ pr_warn("NCSI: Received unrecognized AEN packet (0x%x)\n",
+ h->type);
+ return -ENOENT;
+ }
+
+ ret = nah->nah_handler(ndp, h);
+ consume_skb(skb);
+
+ return ret;
+}
diff --git a/net/ncsi/ncsi-cmd.c b/net/ncsi/ncsi-cmd.c
new file mode 100644
index 000000000000..9b0ddb73840d
--- /dev/null
+++ b/net/ncsi/ncsi-cmd.c
@@ -0,0 +1,371 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2015.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>
+
+#include <net/ncsi.h>
+#include <net/net_namespace.h>
+#include <net/sock.h>
+
+#include "internal.h"
+#include "ncsi-pkt.h"
+
+/*
+ * This function should be called after the data area has been
+ * populated completely.
+ */
+static int ncsi_cmd_build_header(struct ncsi_pkt_hdr *h,
+ struct ncsi_cmd_arg *nca)
+{
+ __be32 csum, *checksum;
+ __be32 low, high;
+ unsigned char *stream;
+ int i;
+
+ h->mc_id = 0;
+ h->revision = NCSI_PKT_REVISION;
+ h->reserved = 0;
+ h->id = nca->nca_id;
+ h->type = nca->nca_type;
+ h->channel = NCSI_TO_CHANNEL(nca->nca_package,
+ nca->nca_channel);
+ h->length = htons(nca->nca_payload);
+ h->reserved1[0] = 0;
+ h->reserved1[1] = 0;
+
+ /* Calculate the checksum */
+ csum = 0;
+ stream = (unsigned char *)h;
+ for (i = 0; i < sizeof(*h) + nca->nca_payload; i += 2) {
+ high = stream[i];
+ low = stream[i + 1];
+ csum += ((high << 8) | low);
+ }
+
+ /* Fill with the calculated checksum */
+ checksum = (__be32 *)((void *)(h + 1) + nca->nca_payload);
+ csum = (~csum + 1);
+ *checksum = htonl(csum);
+
+ return 0;
+}
+
+static int ncsi_cmd_handler_default(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_pkt *cmd;
+
+ if (!nca)
+ return 0;
+
+ cmd = (struct ncsi_cmd_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_sp(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_sp_pkt *cmd;
+
+ if (!nca)
+ return 4;
+
+ cmd = (struct ncsi_cmd_sp_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->hw_arbitration = nca->nca_bytes[0];
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_dc(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_dc_pkt *cmd;
+
+ if (!nca)
+ return 4;
+
+ cmd = (struct ncsi_cmd_dc_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->ald = nca->nca_bytes[0];
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_rc(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_rc_pkt *cmd;
+
+ if (!nca)
+ return 4;
+
+ cmd = (struct ncsi_cmd_rc_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_ae(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_ae_pkt *cmd;
+
+ if (!nca)
+ return 8;
+
+ cmd = (struct ncsi_cmd_ae_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->mc_id = nca->nca_bytes[0];
+ cmd->mode = htonl(nca->nca_dwords[1]);
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_sl(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_sl_pkt *cmd;
+
+ if (!nca)
+ return 8;
+
+ cmd = (struct ncsi_cmd_sl_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->mode = htonl(nca->nca_dwords[0]);
+ cmd->oem_mode = htonl(nca->nca_dwords[1]);
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_svf(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_svf_pkt *cmd;
+
+ if (!nca)
+ return 4;
+
+ cmd = (struct ncsi_cmd_svf_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->vlan = htons(nca->nca_words[0]);
+ cmd->index = nca->nca_bytes[2];
+ cmd->enable = nca->nca_bytes[3];
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_ev(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_ev_pkt *cmd;
+
+ if (!nca)
+ return 4;
+
+ cmd = (struct ncsi_cmd_ev_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->mode = nca->nca_bytes[0];
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_sma(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_sma_pkt *cmd;
+ int i;
+
+ if (!nca)
+ return 8;
+
+ cmd = (struct ncsi_cmd_sma_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ for (i = 0; i < 6; i++)
+ cmd->mac[i] = nca->nca_bytes[i];
+ cmd->index = nca->nca_bytes[6];
+ cmd->at_e = nca->nca_bytes[7];
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_ebf(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_ebf_pkt *cmd;
+
+ if (!nca)
+ return 4;
+
+ cmd = (struct ncsi_cmd_ebf_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->mode = htonl(nca->nca_dwords[0]);
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_egmf(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_egmf_pkt *cmd;
+
+ if (!nca)
+ return 4;
+
+ cmd = (struct ncsi_cmd_egmf_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->mode = htonl(nca->nca_dwords[0]);
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static int ncsi_cmd_handler_snfc(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_cmd_snfc_pkt *cmd;
+
+ if (!nca)
+ return 4;
+
+ cmd = (struct ncsi_cmd_snfc_pkt *)skb_put(skb, sizeof(*cmd));
+ memset(cmd, 0, sizeof(*cmd));
+ cmd->mode = nca->nca_bytes[0];
+ return ncsi_cmd_build_header(&cmd->cmd.common, nca);
+}
+
+static struct ncsi_cmd_handler {
+ unsigned char nch_type;
+ int (*nch_handler)(struct sk_buff *skb,
+ struct ncsi_cmd_arg *nca);
+} ncsi_cmd_handlers[] = {
+ { NCSI_PKT_CMD_CIS, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_SP, ncsi_cmd_handler_sp },
+ { NCSI_PKT_CMD_DP, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_EC, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_DC, ncsi_cmd_handler_dc },
+ { NCSI_PKT_CMD_RC, ncsi_cmd_handler_rc },
+ { NCSI_PKT_CMD_ECNT, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_DCNT, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_AE, ncsi_cmd_handler_ae },
+ { NCSI_PKT_CMD_SL, ncsi_cmd_handler_sl },
+ { NCSI_PKT_CMD_GLS, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_SVF, ncsi_cmd_handler_svf },
+ { NCSI_PKT_CMD_EV, ncsi_cmd_handler_ev },
+ { NCSI_PKT_CMD_DV, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_SMA, ncsi_cmd_handler_sma },
+ { NCSI_PKT_CMD_EBF, ncsi_cmd_handler_ebf },
+ { NCSI_PKT_CMD_DBF, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_EGMF, ncsi_cmd_handler_egmf },
+ { NCSI_PKT_CMD_DGMF, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_SNFC, ncsi_cmd_handler_snfc },
+ { NCSI_PKT_CMD_GVI, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_GC, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_GP, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_GCPS, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_GNS, ncsi_cmd_handler_default },
+ { NCSI_PKT_CMD_GNPTS, ncsi_cmd_handler_default },
+ { 0, NULL }
+};
+
+static struct ncsi_req *ncsi_alloc_cmd_req(struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_dev_priv *ndp = nca->nca_ndp;
+ struct ncsi_dev *nd = &ndp->ndp_ndev;
+ struct net_device *dev = nd->nd_dev;
+ int hlen = LL_RESERVED_SPACE(dev);
+ int tlen = dev->needed_tailroom;
+ int len = hlen + tlen;
+ struct sk_buff *skb;
+ struct ncsi_req *nr;
+
+ nr = ncsi_alloc_req(ndp, nca->nca_req_flags);
+ if (!nr)
+ return NULL;
+
+ /* NCSI command packet has 16-bytes header, payload,
+ * 4-bytes checksum and optional padding.
+ */
+ len += sizeof(struct ncsi_cmd_pkt_hdr);
+ len += 4;
+ if (nca->nca_payload < 26)
+ len += 26;
+ else
+ len += nca->nca_payload;
+
+ /* Allocate skb */
+ skb = alloc_skb(len, GFP_ATOMIC);
+ if (!skb) {
+ ncsi_free_req(nr);
+ return NULL;
+ }
+
+ nr->nr_cmd = skb;
+ skb_reserve(skb, hlen);
+ skb_reset_network_header(skb);
+
+ skb->dev = dev;
+ skb->protocol = htons(ETH_P_NCSI);
+
+ return nr;
+}
+
+int ncsi_xmit_cmd(struct ncsi_cmd_arg *nca)
+{
+ struct ncsi_req *nr;
+ struct ethhdr *eh;
+ struct ncsi_cmd_handler *nch;
+ int i, ret;
+
+ /* Search for the handler */
+ nch = ncsi_cmd_handlers;
+ while (nch->nch_handler) {
+ if (nch->nch_type == nca->nca_type)
+ break;
+ nch++;
+ }
+
+ if (!nch->nch_handler) {
+ pr_info("%s: Cannot send packet with type 0x%x\n",
+ __func__, nca->nca_type);
+ return -ENOENT;
+ }
+
+ /* Get packet payload length and allocate the request */
+ nca->nca_payload = nch->nch_handler(NULL, NULL);
+ nr = ncsi_alloc_cmd_req(nca);
+ if (!nr)
+ return -ENOMEM;
+
+ /* Prepare the packet */
+ nca->nca_id = nr->nr_id;
+ ret = nch->nch_handler(nr->nr_cmd, nca);
+ if (ret)
+ goto out;
+
+ /* Fill the ethernet header */
+ eh = (struct ethhdr *)skb_push(nr->nr_cmd, sizeof(*eh));
+ eh->h_proto = htons(ETH_P_NCSI);
+ for (i = 0; i < ETH_ALEN; i++) {
+ eh->h_dest[i] = 0xff;
+ eh->h_source[i] = 0xff;
+ }
+
+ /* Start the timer for the request that might not have
+ * corresponding response. Given NCSI is an internal
+ * connection a 1 second delay should be sufficient.
+ */
+ mod_timer(&nr->nr_timer, jiffies + 1 * HZ);
+ nr->nr_timer_enabled = true;
+
+ /* Send NCSI packet */
+ skb_get(nr->nr_cmd);
+ ret = dev_queue_xmit(nr->nr_cmd);
+ if (ret)
+ goto out;
+
+ return 0;
+out:
+ ncsi_free_req(nr);
+ return ret;
+}
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
new file mode 100644
index 000000000000..be767a81ccc5
--- /dev/null
+++ b/net/ncsi/ncsi-manage.c
@@ -0,0 +1,989 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2015.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>
+#include <linux/netlink.h>
+
+#include <net/ncsi.h>
+#include <net/net_namespace.h>
+#include <net/sock.h>
+
+#include "ncsi-pkt.h"
+#include "internal.h"
+
+LIST_HEAD(ncsi_dev_list);
+DEFINE_SPINLOCK(ncsi_dev_lock);
+
+int ncsi_find_channel_filter(struct ncsi_channel *nc, int table, void *data)
+{
+ struct ncsi_channel_filter *ncf;
+ int idx, entry_size;
+ void *bitmap;
+
+ switch (table) {
+ case NCSI_FILTER_VLAN:
+ entry_size = 2;
+ break;
+ case NCSI_FILTER_UC:
+ case NCSI_FILTER_MC:
+ case NCSI_FILTER_MIXED:
+ entry_size = 6;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ /* Check if the filter table has been initialized */
+ ncf = nc->nc_filters[table];
+ if (!ncf)
+ return -ENODEV;
+
+ /* Check the valid entries one by one */
+ bitmap = (void *)&ncf->ncf_bitmap;
+ idx = -1;
+ while ((idx = find_next_bit(bitmap, ncf->ncf_total, idx+1))
+ < ncf->ncf_total) {
+ if (!memcmp(ncf->ncf_data + entry_size * idx, data, entry_size))
+ return idx;
+ }
+
+ return -ENOENT;
+}
+
+int ncsi_add_channel_filter(struct ncsi_channel *nc, int table, void *data)
+{
+ struct ncsi_channel_filter *ncf;
+ int idx, entry_size;
+ void *bitmap;
+
+ /* Needn't add it if it's already existing */
+ idx = ncsi_find_channel_filter(nc, table, data);
+ if (idx >= 0)
+ return idx;
+
+ switch (table) {
+ case NCSI_FILTER_VLAN:
+ entry_size = 2;
+ break;
+ case NCSI_FILTER_UC:
+ case NCSI_FILTER_MC:
+ case NCSI_FILTER_MIXED:
+ entry_size = 6;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ /* Check if the filter table has been initialized */
+ ncf = nc->nc_filters[table];
+ if (!ncf)
+ return -ENODEV;
+
+ /* Propagate the filter */
+ bitmap = (void *)&ncf->ncf_bitmap;
+ do {
+ idx = find_next_zero_bit(bitmap, ncf->ncf_total, 0);
+ if (idx >= ncf->ncf_total)
+ return -ENOSPC;
+ } while (test_and_set_bit(idx, bitmap));
+
+ memcpy(ncf->ncf_data + entry_size * idx, data, entry_size);
+ return idx;
+}
+
+int ncsi_del_channel_filter(struct ncsi_channel *nc, int table, int index)
+{
+ struct ncsi_channel_filter *ncf;
+ int entry_size;
+ void *bitmap;
+
+ switch (table) {
+ case NCSI_FILTER_VLAN:
+ entry_size = 2;
+ break;
+ case NCSI_FILTER_UC:
+ case NCSI_FILTER_MC:
+ case NCSI_FILTER_MIXED:
+ entry_size = 6;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ /* Check if the filter table has been initialized */
+ ncf = nc->nc_filters[table];
+ if (!ncf || index >= ncf->ncf_total)
+ return -ENODEV;
+
+ /* Check if the entry is valid */
+ bitmap = (void *)&ncf->ncf_bitmap;
+ if (test_and_clear_bit(index, bitmap))
+ memset(ncf->ncf_data + entry_size * index, 0, entry_size);
+
+ return 0;
+}
+
+struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
+{
+ struct ncsi_channel *nc, *tmp;
+ int index;
+
+ nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
+ if (!nc) {
+ pr_warn("%s: Out of memory !\n", __func__);
+ return NULL;
+ }
+
+ nc->nc_package = np;
+ nc->nc_id = id;
+ for (index = 0; index < NCSI_CAP_MAX; index++)
+ nc->nc_caps[index].ncc_index = index;
+ for (index = 0; index < NCSI_MODE_MAX; index++)
+ nc->nc_modes[index].ncm_index = index;
+
+ spin_lock(&np->np_channel_lock);
+ tmp = ncsi_find_channel(np, id);
+ if (tmp) {
+ spin_unlock(&np->np_channel_lock);
+ kfree(nc);
+ return tmp;
+ }
+ list_add_tail_rcu(&nc->nc_node, &np->np_channels);
+ spin_unlock(&np->np_channel_lock);
+
+ atomic_inc(&np->np_channel_num);
+ return nc;
+}
+
+struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
+ unsigned char id)
+{
+ struct ncsi_channel *nc;
+
+ NCSI_FOR_EACH_CHANNEL(np, nc) {
+ if (nc->nc_id == id)
+ return nc;
+ }
+
+ return NULL;
+}
+
+static void ncsi_release_channel(struct ncsi_channel *nc)
+{
+ struct ncsi_dev_priv *ndp = nc->nc_package->np_ndp;
+ struct ncsi_package *np = nc->nc_package;
+ struct ncsi_channel_filter *ncf;
+ int i;
+
+ /* Release filters */
+ for (i = 0; i < NCSI_FILTER_MAX; i++) {
+ ncf = nc->nc_filters[i];
+ if (!ncf)
+ continue;
+
+ nc->nc_filters[i] = NULL;
+ kfree(ncf);
+ }
+
+ /* Update active channel if necessary */
+ if (ndp->ndp_active_channel == nc) {
+ ndp->ndp_active_package = NULL;
+ ndp->ndp_active_channel = NULL;
+ }
+
+ /* Remove and free channel */
+ list_del_rcu(&nc->nc_node);
+ kfree(nc);
+ BUG_ON(atomic_dec_return(&np->np_channel_num) < 0);
+}
+
+struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
+ unsigned char id)
+{
+ struct ncsi_package *np, *tmp;
+
+ np = kzalloc(sizeof(*np), GFP_ATOMIC);
+ if (!np) {
+ pr_warn("%s: Out of memory !\n", __func__);
+ return NULL;
+ }
+
+ np->np_id = id;
+ np->np_ndp = ndp;
+ spin_lock_init(&np->np_channel_lock);
+ INIT_LIST_HEAD(&np->np_channels);
+
+ spin_lock(&ndp->ndp_package_lock);
+ tmp = ncsi_find_package(ndp, id);
+ if (tmp) {
+ spin_unlock(&ndp->ndp_package_lock);
+ kfree(np);
+ return tmp;
+ }
+ list_add_tail_rcu(&np->np_node, &ndp->ndp_packages);
+ spin_unlock(&ndp->ndp_package_lock);
+
+ atomic_inc(&ndp->ndp_package_num);
+ return np;
+}
+
+struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
+ unsigned char id)
+{
+ struct ncsi_package *np;
+
+ NCSI_FOR_EACH_PACKAGE(ndp, np) {
+ if (np->np_id == id)
+ return np;
+ }
+
+ return NULL;
+}
+
+void ncsi_release_package(struct ncsi_package *np)
+{
+ struct ncsi_dev_priv *ndp = np->np_ndp;
+ struct ncsi_channel *nc, *tmp;
+
+ /* Release all child channels */
+ spin_lock(&np->np_channel_lock);
+ list_for_each_entry_safe(nc, tmp, &np->np_channels, nc_node)
+ ncsi_release_channel(nc);
+ spin_unlock(&np->np_channel_lock);
+
+ /* Clear active package if necessary */
+ if (ndp->ndp_active_package == np) {
+ ndp->ndp_active_package = NULL;
+ ndp->ndp_active_channel = NULL;
+ }
+
+ /* Remove and free package */
+ list_del_rcu(&np->np_node);
+ kfree(np);
+
+ /* Decrease number of packages */
+ BUG_ON(atomic_dec_return(&ndp->ndp_package_num) < 0);
+}
+
+void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
+ unsigned char id,
+ struct ncsi_package **np,
+ struct ncsi_channel **nc)
+{
+ struct ncsi_package *p;
+ struct ncsi_channel *c;
+
+ p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
+ c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
+
+ if (np)
+ *np = p;
+ if (nc)
+ *nc = c;
+}
+
+/*
+ * For two consective NCSI commands, the packet IDs shouldn't be
+ * same. Otherwise, the bogus response might be replied. So the
+ * available IDs are allocated in round-robin fasion.
+ */
+struct ncsi_req *ncsi_alloc_req(struct ncsi_dev_priv *ndp,
+ unsigned int req_flags)
+{
+ struct ncsi_req *nr = NULL;
+ int idx, limit = 256;
+ unsigned long flags;
+
+ spin_lock_irqsave(&ndp->ndp_req_lock, flags);
+
+ /* Check if there is one available request until the ceiling */
+ for (idx = atomic_read(&ndp->ndp_last_req_idx); idx < limit; idx++) {
+ if (ndp->ndp_reqs[idx].nr_used)
+ continue;
+
+ nr = &ndp->ndp_reqs[idx];
+ nr->nr_used = true;
+ nr->nr_flags = req_flags;
+ atomic_set(&ndp->ndp_last_req_idx, idx + 1);
+ goto found;
+ }
+
+ /* Fail back to check from the starting cursor */
+ for (idx = NCSI_REQ_START_IDX;
+ idx < atomic_read(&ndp->ndp_last_req_idx); idx++) {
+ if (ndp->ndp_reqs[idx].nr_used)
+ continue;
+
+ nr = &ndp->ndp_reqs[idx];
+ nr->nr_used = true;
+ nr->nr_flags = req_flags;
+ atomic_set(&ndp->ndp_last_req_idx, idx + 1);
+ goto found;
+ }
+
+found:
+ spin_unlock_irqrestore(&ndp->ndp_req_lock, flags);
+ return nr;
+}
+
+void ncsi_free_req(struct ncsi_req *nr)
+{
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct sk_buff *cmd, *rsp;
+ unsigned long flags;
+ bool driven;
+
+ if (nr->nr_timer_enabled) {
+ nr->nr_timer_enabled = false;
+ del_timer_sync(&nr->nr_timer);
+ }
+
+ spin_lock_irqsave(&ndp->ndp_req_lock, flags);
+ cmd = nr->nr_cmd;
+ rsp = nr->nr_rsp;
+ nr->nr_cmd = NULL;
+ nr->nr_rsp = NULL;
+ nr->nr_used = false;
+ driven = !!(nr->nr_flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
+ spin_unlock_irqrestore(&ndp->ndp_req_lock, flags);
+
+ if (driven && cmd && atomic_dec_return(&ndp->ndp_pending_reqs) == 0)
+ schedule_work(&ndp->ndp_work);
+ /* Release command and response */
+ consume_skb(cmd);
+ consume_skb(rsp);
+}
+
+struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
+{
+ struct ncsi_dev_priv *ndp;
+
+ NCSI_FOR_EACH_DEV(ndp) {
+ if (ndp->ndp_ndev.nd_dev == dev)
+ return &ndp->ndp_ndev;
+ }
+
+ return NULL;
+}
+
+static void ncsi_dev_config(struct ncsi_dev_priv *ndp)
+{
+ struct ncsi_dev *nd = &ndp->ndp_ndev;
+ struct net_device *dev = nd->nd_dev;
+ struct ncsi_package *np = ndp->ndp_active_package;
+ struct ncsi_channel *nc = ndp->ndp_active_channel;
+ struct ncsi_channel *hot_nc = NULL;
+ struct ncsi_cmd_arg nca;
+ unsigned char index;
+ int ret;
+
+ nca.nca_ndp = ndp;
+ nca.nca_req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
+
+ /* When we're reconfiguring the active channel, the active package
+ * should be selected and the old setting on the active channel
+ * should be cleared.
+ */
+ switch (nd->nd_state) {
+ case ncsi_dev_state_config:
+ case ncsi_dev_state_config_sp:
+ atomic_set(&ndp->ndp_pending_reqs, 1);
+
+ /* Select the specific package */
+ nca.nca_type = NCSI_PKT_CMD_SP;
+ nca.nca_bytes[0] = 1;
+ nca.nca_package = np->np_id;
+ nca.nca_channel = NCSI_RESERVED_CHANNEL;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+
+ nd->nd_state = ncsi_dev_state_config_cis;
+ break;
+ case ncsi_dev_state_config_cis:
+ atomic_set(&ndp->ndp_pending_reqs, 1);
+
+ /* Clear initial state */
+ nca.nca_type = NCSI_PKT_CMD_CIS;
+ nca.nca_package = np->np_id;
+ nca.nca_channel = nc->nc_id;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+
+ nd->nd_state = ncsi_dev_state_config_sma;
+ break;
+ case ncsi_dev_state_config_sma:
+ case ncsi_dev_state_config_ebf:
+ case ncsi_dev_state_config_ecnt:
+ case ncsi_dev_state_config_ec:
+ case ncsi_dev_state_config_ae:
+ case ncsi_dev_state_config_gls:
+ atomic_set(&ndp->ndp_pending_reqs, 1);
+
+ nca.nca_package = np->np_id;
+ nca.nca_channel = nc->nc_id;
+
+ /* Use first entry in unicast filter table. Note that
+ * the MAC filter table starts from entry 1 instead of
+ * 0.
+ */
+ if (nd->nd_state == ncsi_dev_state_config_sma) {
+ nca.nca_type = NCSI_PKT_CMD_SMA;
+ for (index = 0; index < 6; index++)
+ nca.nca_bytes[index] = dev->dev_addr[index];
+ nca.nca_bytes[6] = 0x1;
+ nca.nca_bytes[7] = 0x1;
+ nd->nd_state = ncsi_dev_state_config_ebf;
+ } else if (nd->nd_state == ncsi_dev_state_config_ebf) {
+ nca.nca_type = NCSI_PKT_CMD_EBF;
+ nca.nca_dwords[0] = nc->nc_caps[NCSI_CAP_BC].ncc_cap;
+ nd->nd_state = ncsi_dev_state_config_ecnt;
+ } else if (nd->nd_state == ncsi_dev_state_config_ecnt) {
+ nca.nca_type = NCSI_PKT_CMD_ECNT;
+ nd->nd_state = ncsi_dev_state_config_ec;
+ } else if (nd->nd_state == ncsi_dev_state_config_ec) {
+ nca.nca_type = NCSI_PKT_CMD_EC;
+ nd->nd_state = ncsi_dev_state_config_ae;
+ } else if (nd->nd_state == ncsi_dev_state_config_ae) {
+ nca.nca_type = NCSI_PKT_CMD_AE;
+ nca.nca_bytes[0] = 0;
+ nca.nca_dwords[1] = 0x7;
+ nd->nd_state = ncsi_dev_state_config_gls;
+ } else if (nd->nd_state == ncsi_dev_state_config_gls) {
+ nca.nca_type = NCSI_PKT_CMD_GLS;
+ nd->nd_state = ncsi_dev_state_config_done;
+ }
+
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+
+ break;
+ case ncsi_dev_state_config_done:
+ nd->nd_state = ncsi_dev_state_functional;
+ nd->nd_link_up = 0;
+ if (nc->nc_modes[NCSI_MODE_LINK].ncm_data[2] & 0x1) {
+ nd->nd_link_up = 1;
+ hot_nc = nc;
+ } else {
+ hot_nc = NULL;
+ }
+
+ /* Update the hot channel */
+ ndp->ndp_hot_channel = hot_nc;
+
+ nd->nd_handler(nd);
+ ndp->ndp_flags &= ~NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE;
+
+ break;
+ default:
+ pr_debug("%s: Unrecognized NCSI dev state 0x%x\n",
+ __func__, nd->nd_state);
+ return;
+ }
+
+ return;
+
+error:
+ nd->nd_state = ncsi_dev_state_functional;
+ nd->nd_link_up = 0;
+ ndp->ndp_flags &= ~NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE;
+ nd->nd_handler(nd);
+}
+
+static void ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
+{
+ struct ncsi_package *np;
+ struct ncsi_channel *nc, *hot_nc;
+ struct ncsi_channel_mode *ncm;
+
+ ndp->ndp_active_package = NULL;
+ ndp->ndp_active_channel = NULL;
+
+ hot_nc = ndp->ndp_hot_channel;
+ NCSI_FOR_EACH_PACKAGE(ndp, np) {
+ NCSI_FOR_EACH_CHANNEL(np, nc) {
+ if (!ndp->ndp_active_channel) {
+ ndp->ndp_active_package = np;
+ ndp->ndp_active_channel = nc;
+ }
+
+ if (nc == hot_nc) {
+ ndp->ndp_active_package = np;
+ ndp->ndp_active_channel = nc;
+ }
+
+ ncm = &nc->nc_modes[NCSI_MODE_LINK];
+ if (ncm->ncm_data[2] & 0x1) {
+ ndp->ndp_active_package = np;
+ ndp->ndp_active_channel = nc;
+ return;
+ }
+ }
+ }
+}
+
+static void ncsi_dev_probe(struct ncsi_dev_priv *ndp)
+{
+ struct ncsi_dev *nd = &ndp->ndp_ndev;
+ struct ncsi_package *np;
+ struct ncsi_channel *nc;
+ struct ncsi_cmd_arg nca;
+ unsigned char index;
+ int ret;
+
+ nca.nca_ndp = ndp;
+ nca.nca_req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
+ switch (nd->nd_state) {
+ case ncsi_dev_state_probe:
+ nd->nd_state = ncsi_dev_state_probe_deselect;
+ /* Fall through */
+ case ncsi_dev_state_probe_deselect:
+ atomic_set(&ndp->ndp_pending_reqs, 8);
+
+ /* Deselect all possible packages */
+ nca.nca_type = NCSI_PKT_CMD_DP;
+ nca.nca_channel = NCSI_RESERVED_CHANNEL;
+ for (index = 0; index < 8; index++) {
+ nca.nca_package = index;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+ }
+
+ nd->nd_state = ncsi_dev_state_probe_package;
+ break;
+ case ncsi_dev_state_probe_package:
+ atomic_set(&ndp->ndp_pending_reqs, 16);
+
+ /* Select all possible packages */
+ nca.nca_type = NCSI_PKT_CMD_SP;
+ nca.nca_bytes[0] = 1;
+ nca.nca_channel = NCSI_RESERVED_CHANNEL;
+ for (index = 0; index < 8; index++) {
+ nca.nca_package = index;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+ }
+
+ /* Disable all possible packages */
+ nca.nca_type = NCSI_PKT_CMD_DP;
+ for (index = 0; index < 8; index++) {
+ nca.nca_package = index;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+ }
+
+ nd->nd_state = ncsi_dev_state_probe_channel;
+ break;
+ case ncsi_dev_state_probe_channel:
+ if (!ndp->ndp_active_package)
+ ndp->ndp_active_package = list_first_or_null_rcu(
+ &ndp->ndp_packages,
+ struct ncsi_package,
+ np_node);
+ else if (list_is_last(&ndp->ndp_active_package->np_node,
+ &ndp->ndp_packages))
+ ndp->ndp_active_package = NULL;
+ else
+ ndp->ndp_active_package = list_next_entry(
+ ndp->ndp_active_package,
+ np_node);
+
+ /*
+ * All available packages and channels are enumerated. The
+ * enumeration happens for once when the NCSI interface is
+ * started. So we need continue to start the interface after
+ * the enumeration.
+ *
+ * We have to choose an active channel before configuring it.
+ * Note that we possibly don't have active channel in extreme
+ * situation.
+ */
+ if (!ndp->ndp_active_package) {
+ ndp->ndp_flags |= NCSI_DEV_PRIV_FLAG_POPULATED;
+
+ ncsi_choose_active_channel(ndp);
+ if (!ndp->ndp_active_channel)
+ goto error;
+
+ nd->nd_state = ncsi_dev_state_config;
+ return ncsi_dev_config(ndp);
+ }
+
+ /* Select the active package */
+ atomic_set(&ndp->ndp_pending_reqs, 1);
+ nca.nca_type = NCSI_PKT_CMD_SP;
+ nca.nca_bytes[0] = 1;
+ nca.nca_package = ndp->ndp_active_package->np_id;
+ nca.nca_channel = NCSI_RESERVED_CHANNEL;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+
+ nd->nd_state = ncsi_dev_state_probe_cis;
+ break;
+ case ncsi_dev_state_probe_cis:
+ atomic_set(&ndp->ndp_pending_reqs, NCSI_RESERVED_CHANNEL);
+
+ /* Clear initial state */
+ nca.nca_type = NCSI_PKT_CMD_CIS;
+ nca.nca_package = ndp->ndp_active_package->np_id;
+ for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
+ nca.nca_channel = index;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+ }
+
+ nd->nd_state = ncsi_dev_state_probe_gvi;
+ break;
+ case ncsi_dev_state_probe_gvi:
+ case ncsi_dev_state_probe_gc:
+ case ncsi_dev_state_probe_gls:
+ np = ndp->ndp_active_package;
+ atomic_set(&ndp->ndp_pending_reqs,
+ atomic_read(&np->np_channel_num));
+
+ /* Get version information or get capacity */
+ if (nd->nd_state == ncsi_dev_state_probe_gvi)
+ nca.nca_type = NCSI_PKT_CMD_GVI;
+ else if (nd->nd_state == ncsi_dev_state_probe_gc)
+ nca.nca_type = NCSI_PKT_CMD_GC;
+ else
+ nca.nca_type = NCSI_PKT_CMD_GLS;
+
+ nca.nca_package = np->np_id;
+ NCSI_FOR_EACH_CHANNEL(np, nc) {
+ nca.nca_channel = nc->nc_id;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+ }
+
+ if (nd->nd_state == ncsi_dev_state_probe_gvi)
+ nd->nd_state = ncsi_dev_state_probe_gc;
+ else if (nd->nd_state == ncsi_dev_state_probe_gc)
+ nd->nd_state = ncsi_dev_state_probe_gls;
+ else
+ nd->nd_state = ncsi_dev_state_probe_dp;
+ break;
+ case ncsi_dev_state_probe_dp:
+ atomic_set(&ndp->ndp_pending_reqs, 1);
+
+ /* Deselect the active package */
+ nca.nca_type = NCSI_PKT_CMD_DP;
+ nca.nca_package = ndp->ndp_active_package->np_id;
+ nca.nca_channel = NCSI_RESERVED_CHANNEL;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+
+ /* Scan channels in next package */
+ nd->nd_state = ncsi_dev_state_probe_channel;
+ break;
+ default:
+ pr_warn("%s: Unrecognized NCSI dev state 0x%x\n",
+ __func__, nd->nd_state);
+ }
+
+ return;
+error:
+ nd->nd_state = ncsi_dev_state_functional;
+ nd->nd_link_up = 0;
+ nd->nd_handler(nd);
+}
+
+static void ncsi_dev_suspend(struct ncsi_dev_priv *ndp)
+{
+ struct ncsi_dev *nd = &ndp->ndp_ndev;
+ struct ncsi_package *np = ndp->ndp_active_package;
+ struct ncsi_channel *nc = ndp->ndp_active_channel;
+ struct ncsi_cmd_arg nca;
+ int ret;
+
+ nca.nca_ndp = ndp;
+ nca.nca_req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
+ switch (nd->nd_state) {
+ case ncsi_dev_state_suspend:
+ /* If there're no active channel, we're done */
+ if (!ndp->ndp_active_channel)
+ goto error;
+
+ nd->nd_state = ncsi_dev_state_suspend_select;
+ /* Fall through */
+ case ncsi_dev_state_suspend_select:
+ atomic_set(&ndp->ndp_pending_reqs, 1);
+
+ nca.nca_type = NCSI_PKT_CMD_SP;
+ nca.nca_package = np->np_id;
+ nca.nca_channel = NCSI_RESERVED_CHANNEL;
+ nca.nca_bytes[0] = 1;
+
+ /* To retrieve the last link states of channels in current
+ * package when current active channel needs fail over to
+ * another one. It means we will possibly select another
+ * channel as next active one. The link states of channels
+ * are most important factor of the selection. So we need
+ * accurate link states. Unfortunately, the link states on
+ * inactive channels can't be updated with LSC AEN in time.
+ */
+ if (ndp->ndp_flags & NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE)
+ nd->nd_state = ncsi_dev_state_suspend_gls;
+ else
+ nd->nd_state = ncsi_dev_state_suspend_dcnt;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+
+ break;
+ case ncsi_dev_state_suspend_gls:
+ atomic_set(&ndp->ndp_pending_reqs,
+ atomic_read(&np->np_channel_num));
+
+ nca.nca_type = NCSI_PKT_CMD_GLS;
+ nca.nca_package = np->np_id;
+
+ nd->nd_state = ncsi_dev_state_suspend_dcnt;
+ NCSI_FOR_EACH_CHANNEL(np, nc) {
+ nca.nca_channel = nc->nc_id;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+ }
+
+ break;
+ case ncsi_dev_state_suspend_dcnt:
+ atomic_set(&ndp->ndp_pending_reqs, 1);
+
+ nca.nca_type = NCSI_PKT_CMD_DCNT;
+ nca.nca_package = np->np_id;
+ nca.nca_channel = nc->nc_id;
+
+ nd->nd_state = ncsi_dev_state_suspend_dc;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+
+ break;
+ case ncsi_dev_state_suspend_dc:
+ atomic_set(&ndp->ndp_pending_reqs, 1);
+
+ nca.nca_type = NCSI_PKT_CMD_DC;
+ nca.nca_package = np->np_id;
+ nca.nca_channel = nc->nc_id;
+ nca.nca_bytes[0] = 1;
+
+ nd->nd_state = ncsi_dev_state_suspend_deselect;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+
+ break;
+ case ncsi_dev_state_suspend_deselect:
+ atomic_set(&ndp->ndp_pending_reqs, 1);
+
+ nca.nca_type = NCSI_PKT_CMD_DP;
+ nca.nca_package = np->np_id;
+ nca.nca_channel = NCSI_RESERVED_CHANNEL;
+
+ nd->nd_state = ncsi_dev_state_suspend_done;
+ ret = ncsi_xmit_cmd(&nca);
+ if (ret)
+ goto error;
+
+ break;
+ case ncsi_dev_state_suspend_done:
+ if (ndp->ndp_flags & NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE)
+ ncsi_choose_active_channel(ndp);
+
+ if (!ndp->ndp_active_channel) {
+ nd->nd_state = ncsi_dev_state_functional;
+ nd->nd_link_up = 0;
+ nd->nd_handler(nd);
+ } else {
+ nd->nd_state = ncsi_dev_state_config;
+ ncsi_dev_config(ndp);
+ }
+
+ break;
+ default:
+ pr_warn("%s: Unsupported NCSI dev state 0x%x\n",
+ __func__, nd->nd_state);
+ }
+
+ return;
+error:
+ nd->nd_state = ncsi_dev_state_functional;
+}
+
+static void ncsi_dev_work(struct work_struct *work)
+{
+ struct ncsi_dev_priv *ndp = container_of(work, struct ncsi_dev_priv,
+ ndp_work);
+ struct ncsi_dev *nd = &ndp->ndp_ndev;
+
+ switch (nd->nd_state & ncsi_dev_state_major) {
+ case ncsi_dev_state_probe:
+ ncsi_dev_probe(ndp);
+ break;
+ case ncsi_dev_state_suspend:
+ ncsi_dev_suspend(ndp);
+ break;
+ case ncsi_dev_state_config:
+ ncsi_dev_config(ndp);
+ break;
+ default:
+ pr_warn("%s: Unsupported NCSI dev state 0x%x\n",
+ __func__, nd->nd_state);
+ }
+}
+
+static void ncsi_req_timeout(unsigned long data)
+{
+ struct ncsi_req *nr = (struct ncsi_req *)data;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ unsigned long flags;
+
+ /* If the request already had associated response,
+ * let the response handler to release it.
+ */
+ spin_lock_irqsave(&ndp->ndp_req_lock, flags);
+ nr->nr_timer_enabled = false;
+ if (nr->nr_rsp || !nr->nr_cmd) {
+ spin_unlock_irqrestore(&ndp->ndp_req_lock, flags);
+ return;
+ }
+ spin_unlock_irqrestore(&ndp->ndp_req_lock, flags);
+
+ /* Release the request */
+ ncsi_free_req(nr);
+}
+
+struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
+ void (*handler)(struct ncsi_dev *ndev))
+{
+ struct ncsi_dev_priv *ndp;
+ struct ncsi_dev *nd;
+ int idx;
+
+ /* Check if the device has been registered or not */
+ nd = ncsi_find_dev(dev);
+ if (nd)
+ return nd;
+
+ /* Create NCSI device */
+ ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
+ if (!ndp) {
+ pr_warn("%s: Out of memory !\n", __func__);
+ return NULL;
+ }
+
+ nd = &ndp->ndp_ndev;
+ nd->nd_state = ncsi_dev_state_registered;
+ nd->nd_dev = dev;
+ nd->nd_handler = handler;
+
+ /* Initialize private NCSI device */
+ spin_lock_init(&ndp->ndp_package_lock);
+ INIT_LIST_HEAD(&ndp->ndp_packages);
+ INIT_WORK(&ndp->ndp_work, ncsi_dev_work);
+ spin_lock_init(&ndp->ndp_req_lock);
+ atomic_set(&ndp->ndp_last_req_idx, NCSI_REQ_START_IDX);
+ for (idx = 0; idx < 256; idx++) {
+ ndp->ndp_reqs[idx].nr_id = idx;
+ ndp->ndp_reqs[idx].nr_ndp = ndp;
+ setup_timer(&ndp->ndp_reqs[idx].nr_timer, ncsi_req_timeout,
+ (unsigned long)&ndp->ndp_reqs[idx]);
+ }
+
+ spin_lock(&ncsi_dev_lock);
+ list_add_tail_rcu(&ndp->ndp_node, &ncsi_dev_list);
+ spin_unlock(&ncsi_dev_lock);
+
+ /* Register NCSI packet receiption handler */
+ ndp->ndp_ptype.type = cpu_to_be16(ETH_P_NCSI);
+ ndp->ndp_ptype.func = ncsi_rcv_rsp;
+ ndp->ndp_ptype.dev = dev;
+ dev_add_pack(&ndp->ndp_ptype);
+
+ return nd;
+}
+EXPORT_SYMBOL_GPL(ncsi_register_dev);
+
+int ncsi_start_dev(struct ncsi_dev *nd)
+{
+ struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
+
+ if (nd->nd_state != ncsi_dev_state_registered &&
+ nd->nd_state != ncsi_dev_state_functional)
+ return -ENOTTY;
+
+ if (!(ndp->ndp_flags & NCSI_DEV_PRIV_FLAG_POPULATED)) {
+ nd->nd_state = ncsi_dev_state_probe;
+ schedule_work(&ndp->ndp_work);
+ return 0;
+ }
+
+ /* Choose active package and channel */
+ ncsi_choose_active_channel(ndp);
+ if (!ndp->ndp_active_channel)
+ return -ENXIO;
+
+ return ncsi_config_dev(nd);
+}
+EXPORT_SYMBOL_GPL(ncsi_start_dev);
+
+int ncsi_config_dev(struct ncsi_dev *nd)
+{
+ struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
+
+ if (nd->nd_state != ncsi_dev_state_functional)
+ return -ENOTTY;
+
+ nd->nd_state = ncsi_dev_state_config;
+ schedule_work(&ndp->ndp_work);
+
+ return 0;
+}
+
+int ncsi_suspend_dev(struct ncsi_dev *nd)
+{
+ struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
+
+ if (nd->nd_state != ncsi_dev_state_functional)
+ return -ENOTTY;
+
+ nd->nd_state = ncsi_dev_state_suspend;
+ schedule_work(&ndp->ndp_work);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(ncsi_suspend_dev);
+
+void ncsi_unregister_dev(struct ncsi_dev *nd)
+{
+ struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
+ struct ncsi_package *np, *tmp;
+
+ dev_remove_pack(&ndp->ndp_ptype);
+
+ spin_lock(&ndp->ndp_package_lock);
+ list_for_each_entry_safe(np, tmp, &ndp->ndp_packages, np_node)
+ ncsi_release_package(np);
+ spin_unlock(&ndp->ndp_package_lock);
+}
+EXPORT_SYMBOL_GPL(ncsi_unregister_dev);
diff --git a/net/ncsi/ncsi-pkt.h b/net/ncsi/ncsi-pkt.h
new file mode 100644
index 000000000000..646f1fc0bd21
--- /dev/null
+++ b/net/ncsi/ncsi-pkt.h
@@ -0,0 +1,391 @@
+#ifndef __NCSI_PKT_H__
+#define __NCSI_PKT_H__
+
+struct ncsi_pkt_hdr {
+ unsigned char mc_id; /* Management controller ID */
+ unsigned char revision; /* NCSI version - 0x01 */
+ unsigned char reserved; /* Reserved */
+ unsigned char id; /* Packet sequence number */
+ unsigned char type; /* Packet type */
+ unsigned char channel; /* Network controller ID */
+ __be16 length; /* Payload length */
+ __be32 reserved1[2]; /* Reserved */
+};
+
+struct ncsi_cmd_pkt_hdr {
+ struct ncsi_pkt_hdr common; /* Common NCSI packet header */
+};
+
+struct ncsi_rsp_pkt_hdr {
+ struct ncsi_pkt_hdr common; /* Common NCSI packet header */
+ __be16 code; /* Response code */
+ __be16 reason; /* Response reason */
+};
+
+struct ncsi_aen_pkt_hdr {
+ struct ncsi_pkt_hdr common; /* Common NCSI packet header */
+ unsigned char reserved2[3]; /* Reserved */
+ unsigned char type; /* AEN packet type */
+};
+
+/* NCSI common command and response packets */
+struct ncsi_cmd_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[26];
+};
+
+struct ncsi_rsp_pkt {
+ struct ncsi_rsp_pkt_hdr rsp; /* Response header */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[22];
+};
+
+/* Select Package */
+struct ncsi_cmd_sp_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ unsigned char reserved[3]; /* Reserved */
+ unsigned char hw_arbitration; /* HW arbitration */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[22];
+};
+
+/* Disable Channel */
+struct ncsi_cmd_dc_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ unsigned char reserved[3]; /* Reserved */
+ unsigned char ald; /* Allow link down */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[22];
+};
+
+/* Reset Channel */
+struct ncsi_cmd_rc_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ __be32 reserved; /* Reserved */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[22];
+};
+
+/* AEN Enable */
+struct ncsi_cmd_ae_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ unsigned char reserved[3]; /* Reserved */
+ unsigned char mc_id; /* MC ID */
+ __be32 mode; /* AEN working mode */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[18];
+};
+
+/* Set Link */
+struct ncsi_cmd_sl_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ __be32 mode; /* Link working mode */
+ __be32 oem_mode; /* OEM link mode */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[18];
+};
+
+/* Get Link Status */
+struct ncsi_rsp_gls_pkt {
+ struct ncsi_rsp_pkt_hdr rsp; /* Response header */
+ __be32 status; /* Link status */
+ __be32 other; /* Other indications */
+ __be32 oem_status; /* OEM link status */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[10];
+};
+
+/* Set VLAN Filter */
+struct ncsi_cmd_svf_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ __be16 reserved; /* Reserved */
+ __be16 vlan; /* VLAN ID */
+ __be16 reserved1; /* Reserved */
+ unsigned char index; /* VLAN table index */
+ unsigned char enable; /* Enable or disable */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[14];
+};
+
+/* Enable VLAN */
+struct ncsi_cmd_ev_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ unsigned char reserved[3]; /* Reserved */
+ unsigned char mode; /* VLAN filter mode */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[22];
+};
+
+/* Set MAC Address */
+struct ncsi_cmd_sma_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ unsigned char mac[6]; /* MAC address */
+ unsigned char index; /* MAC table index */
+ unsigned char at_e; /* Addr type and operation */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[18];
+};
+
+/* Enable Broadcast Filter */
+struct ncsi_cmd_ebf_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ __be32 mode; /* Filter mode */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[22];
+};
+
+/* Enable Global Multicast Filter */
+struct ncsi_cmd_egmf_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ __be32 mode; /* Global MC mode */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[22];
+};
+
+/* Set NCSI Flow Control */
+struct ncsi_cmd_snfc_pkt {
+ struct ncsi_cmd_pkt_hdr cmd; /* Command header */
+ unsigned char reserved[3]; /* Reserved */
+ unsigned char mode; /* Flow control mode */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[22];
+};
+
+/* Get Version ID */
+struct ncsi_rsp_gvi_pkt {
+ struct ncsi_rsp_pkt_hdr rsp; /* Response header */
+ __be32 ncsi_version; /* NCSI version */
+ unsigned char reserved[3]; /* Reserved */
+ unsigned char alpha2; /* NCSI version */
+ unsigned char fw_name[12]; /* f/w name string */
+ __be32 fw_version; /* f/w version */
+ __be16 pci_ids[4]; /* PCI IDs */
+ __be32 mf_id; /* Manufacture ID */
+ __be32 checksum;
+};
+
+/* Get Capabilities */
+struct ncsi_rsp_gc_pkt {
+ struct ncsi_rsp_pkt_hdr rsp; /* Response header */
+ __be32 cap; /* Capabilities */
+ __be32 bc_cap; /* Broadcast cap */
+ __be32 mc_cap; /* Multicast cap */
+ __be32 buf_cap; /* Buffering cap */
+ __be32 aen_cap; /* AEN cap */
+ unsigned char vlan_cnt; /* VLAN filter count */
+ unsigned char mixed_cnt; /* Mix filter count */
+ unsigned char mc_cnt; /* MC filter count */
+ unsigned char uc_cnt; /* UC filter count */
+ unsigned char reserved[2]; /* Reserved */
+ unsigned char vlan_mode; /* VLAN mode */
+ unsigned char channel_cnt; /* Channel count */
+ __be32 checksum; /* Checksum */
+};
+
+/* Get Parameters */
+struct ncsi_rsp_gp_pkt {
+ struct ncsi_rsp_pkt_hdr rsp; /* Response header */
+ unsigned char mac_cnt; /* Number of MAC addr */
+ unsigned char reserved[2]; /* Reserved */
+ unsigned char mac_enable; /* MAC addr enable flags */
+ unsigned char vlan_cnt; /* VLAN tag count */
+ unsigned char reserved1; /* Reserved */
+ __be16 vlan_enable; /* VLAN tag enable flags */
+ __be32 link_mode; /* Link setting */
+ __be32 bc_mode; /* BC filter mode */
+ __be32 valid_modes; /* Valid mode parameters */
+ unsigned char vlan_mode; /* VLAN mode */
+ unsigned char fc_mode; /* Flow control mode */
+ unsigned char reserved2[2]; /* Reserved */
+ __be32 aen_mode; /* AEN mode */
+ unsigned char mac[6]; /* Supported MAC addr */
+ __be16 vlan; /* Supported VLAN tags */
+ __be32 checksum; /* Checksum */
+};
+
+/* Get Controller Packet Statistics */
+struct ncsi_rsp_gcps_pkt {
+ struct ncsi_rsp_pkt_hdr rsp; /* Response header */
+ __be32 cnt_hi; /* Counter cleared */
+ __be32 cnt_lo; /* Counter cleared */
+ __be32 rx_bytes; /* Rx bytes */
+ __be32 tx_bytes; /* Tx bytes */
+ __be32 rx_uc_pkts; /* Rx UC packets */
+ __be32 rx_mc_pkts; /* Rx MC packets */
+ __be32 rx_bc_pkts; /* Rx BC packets */
+ __be32 tx_uc_pkts; /* Tx UC packets */
+ __be32 tx_mc_pkts; /* Tx MC packets */
+ __be32 tx_bc_pkts; /* Tx BC packets */
+ __be32 fcs_err; /* FCS errors */
+ __be32 align_err; /* Alignment errors */
+ __be32 false_carrier; /* False carrier detection */
+ __be32 runt_pkts; /* Rx runt packets */
+ __be32 jabber_pkts; /* Rx jabber packets */
+ __be32 rx_pause_xon; /* Rx pause XON frames */
+ __be32 rx_pause_xoff; /* Rx XOFF frames */
+ __be32 tx_pause_xon; /* Tx XON frames */
+ __be32 tx_pause_xoff; /* Tx XOFF frames */
+ __be32 tx_s_collision; /* Single collision frames */
+ __be32 tx_m_collision; /* Multiple collision frames */
+ __be32 l_collision; /* Late collision frames */
+ __be32 e_collision; /* Excessive collision frames */
+ __be32 rx_ctl_frames; /* Rx control frames */
+ __be32 rx_64_frames; /* Rx 64-bytes frames */
+ __be32 rx_127_frames; /* Rx 65-127 bytes frames */
+ __be32 rx_255_frames; /* Rx 128-255 bytes frames */
+ __be32 rx_511_frames; /* Rx 256-511 bytes frames */
+ __be32 rx_1023_frames; /* Rx 512-1023 bytes frames */
+ __be32 rx_1522_frames; /* Rx 1024-1522 bytes frames */
+ __be32 rx_9022_frames; /* Rx 1523-9022 bytes frames */
+ __be32 tx_64_frames; /* Tx 64-bytes frames */
+ __be32 tx_127_frames; /* Tx 65-127 bytes frames */
+ __be32 tx_255_frames; /* Tx 128-255 bytes frames */
+ __be32 tx_511_frames; /* Tx 256-511 bytes frames */
+ __be32 tx_1023_frames; /* Tx 512-1023 bytes frames */
+ __be32 tx_1522_frames; /* Tx 1024-1522 bytes frames */
+ __be32 tx_9022_frames; /* Tx 1523-9022 bytes frames */
+ __be32 rx_valid_bytes; /* Rx valid bytes */
+ __be32 rx_runt_pkts; /* Rx error runt packets */
+ __be32 rx_jabber_pkts; /* Rx error jabber packets */
+ __be32 checksum; /* Checksum */
+};
+
+/* Get NCSI Statistics */
+struct ncsi_rsp_gns_pkt {
+ struct ncsi_rsp_pkt_hdr rsp; /* Response header */
+ __be32 rx_cmds; /* Rx NCSI commands */
+ __be32 dropped_cmds; /* Dropped commands */
+ __be32 cmd_type_errs; /* Command type errors */
+ __be32 cmd_csum_errs; /* Command checksum errors */
+ __be32 rx_pkts; /* Rx NCSI packets */
+ __be32 tx_pkts; /* Tx NCSI packets */
+ __be32 tx_aen_pkts; /* Tx AEN packets */
+ __be32 checksum; /* Checksum */
+};
+
+/* Get NCSI Pass-through Statistics */
+struct ncsi_rsp_gnpts_pkt {
+ struct ncsi_rsp_pkt_hdr rsp; /* Response header */
+ __be32 tx_pkts; /* Tx packets */
+ __be32 tx_dropped; /* Tx dropped packets */
+ __be32 tx_channel_err; /* Tx channel errors */
+ __be32 tx_us_err; /* Tx undersize errors */
+ __be32 rx_pkts; /* Rx packets */
+ __be32 rx_dropped; /* Rx dropped packets */
+ __be32 rx_channel_err; /* Rx channel errors */
+ __be32 rx_us_err; /* Rx undersize errors */
+ __be32 rx_os_err; /* Rx oversize errors */
+ __be32 checksum; /* Checksum */
+};
+
+/* AEN: Link State Change */
+struct ncsi_aen_lsc_pkt {
+ struct ncsi_aen_pkt_hdr aen; /* AEN header */
+ __be32 status; /* Link status */
+ __be32 oem_status; /* OEM link status */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[14];
+};
+
+/* AEN: Configuration Required */
+struct ncsi_aen_cr_pkt {
+ struct ncsi_aen_pkt_hdr aen; /* AEN header */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[22];
+};
+
+/* AEN: Host Network Controller Driver Status Change */
+struct ncsi_aen_hncdsc_pkt {
+ struct ncsi_aen_pkt_hdr aen; /* AEN header */
+ __be32 status; /* Status */
+ __be32 checksum; /* Checksum */
+ unsigned char pad[18];
+};
+
+/* NCSI packet revision */
+#define NCSI_PKT_REVISION 0x01
+
+/* NCSI packet commands */
+#define NCSI_PKT_CMD_CIS 0x00 /* Clear Initial State */
+#define NCSI_PKT_CMD_SP 0x01 /* Select Package */
+#define NCSI_PKT_CMD_DP 0x02 /* Deselect Package */
+#define NCSI_PKT_CMD_EC 0x03 /* Enable Channel */
+#define NCSI_PKT_CMD_DC 0x04 /* Disable Channel */
+#define NCSI_PKT_CMD_RC 0x05 /* Reset Channel */
+#define NCSI_PKT_CMD_ECNT 0x06 /* Enable Channel Network Tx */
+#define NCSI_PKT_CMD_DCNT 0x07 /* Disable Channel Network Tx */
+#define NCSI_PKT_CMD_AE 0x08 /* AEN Enable */
+#define NCSI_PKT_CMD_SL 0x09 /* Set Link */
+#define NCSI_PKT_CMD_GLS 0x0a /* Get Link */
+#define NCSI_PKT_CMD_SVF 0x0b /* Set VLAN Filter */
+#define NCSI_PKT_CMD_EV 0x0c /* Enable VLAN */
+#define NCSI_PKT_CMD_DV 0x0d /* Disable VLAN */
+#define NCSI_PKT_CMD_SMA 0x0e /* Set MAC address */
+#define NCSI_PKT_CMD_EBF 0x10 /* Enable Broadcast Filter */
+#define NCSI_PKT_CMD_DBF 0x11 /* Disable Broadcast Filter */
+#define NCSI_PKT_CMD_EGMF 0x12 /* Enable Global Multicast Filter */
+#define NCSI_PKT_CMD_DGMF 0x13 /* Disable Global Multicast Filter */
+#define NCSI_PKT_CMD_SNFC 0x14 /* Set NCSI Flow Control */
+#define NCSI_PKT_CMD_GVI 0x15 /* Get Version ID */
+#define NCSI_PKT_CMD_GC 0x16 /* Get Capabilities */
+#define NCSI_PKT_CMD_GP 0x17 /* Get Parameters */
+#define NCSI_PKT_CMD_GCPS 0x18 /* Get Controller Packet Statistics */
+#define NCSI_PKT_CMD_GNS 0x19 /* Get NCSI Statistics */
+#define NCSI_PKT_CMD_GNPTS 0x1a /* Get NCSI Pass-throu Statistics */
+#define NCSI_PKT_CMD_OEM 0x50 /* OEM */
+
+/* NCSI packet responses */
+#define NCSI_PKT_RSP_CIS (NCSI_PKT_CMD_CIS + 0x80)
+#define NCSI_PKT_RSP_SP (NCSI_PKT_CMD_SP + 0x80)
+#define NCSI_PKT_RSP_DP (NCSI_PKT_CMD_DP + 0x80)
+#define NCSI_PKT_RSP_EC (NCSI_PKT_CMD_EC + 0x80)
+#define NCSI_PKT_RSP_DC (NCSI_PKT_CMD_DC + 0x80)
+#define NCSI_PKT_RSP_RC (NCSI_PKT_CMD_RC + 0x80)
+#define NCSI_PKT_RSP_ECNT (NCSI_PKT_CMD_ECNT + 0x80)
+#define NCSI_PKT_RSP_DCNT (NCSI_PKT_CMD_DCNT + 0x80)
+#define NCSI_PKT_RSP_AE (NCSI_PKT_CMD_AE + 0x80)
+#define NCSI_PKT_RSP_SL (NCSI_PKT_CMD_SL + 0x80)
+#define NCSI_PKT_RSP_GLS (NCSI_PKT_CMD_GLS + 0x80)
+#define NCSI_PKT_RSP_SVF (NCSI_PKT_CMD_SVF + 0x80)
+#define NCSI_PKT_RSP_EV (NCSI_PKT_CMD_EV + 0x80)
+#define NCSI_PKT_RSP_DV (NCSI_PKT_CMD_DV + 0x80)
+#define NCSI_PKT_RSP_SMA (NCSI_PKT_CMD_SMA + 0x80)
+#define NCSI_PKT_RSP_EBF (NCSI_PKT_CMD_EBF + 0x80)
+#define NCSI_PKT_RSP_DBF (NCSI_PKT_CMD_DBF + 0x80)
+#define NCSI_PKT_RSP_EGMF (NCSI_PKT_CMD_EGMF + 0x80)
+#define NCSI_PKT_RSP_DGMF (NCSI_PKT_CMD_DGMF + 0x80)
+#define NCSI_PKT_RSP_SNFC (NCSI_PKT_CMD_SNFC + 0x80)
+#define NCSI_PKT_RSP_GVI (NCSI_PKT_CMD_GVI + 0x80)
+#define NCSI_PKT_RSP_GC (NCSI_PKT_CMD_GC + 0x80)
+#define NCSI_PKT_RSP_GP (NCSI_PKT_CMD_GP + 0x80)
+#define NCSI_PKT_RSP_GCPS (NCSI_PKT_CMD_GCPS + 0x80)
+#define NCSI_PKT_RSP_GNS (NCSI_PKT_CMD_GNS + 0x80)
+#define NCSI_PKT_RSP_GNPTS (NCSI_PKT_CMD_GNPTS + 0x80)
+#define NCSI_PKT_RSP_OEM (NCSI_PKT_CMD_OEM + 0x80)
+
+/* NCSI packet type: AEN */
+#define NCSI_PKT_AEN 0xFF /* AEN Packet */
+#define NCSI_PKT_AEN_LSC 0x00 /* Link status change */
+#define NCSI_PKT_AEN_CR 0x01 /* Configuration required */
+#define NCSI_PKT_AEN_HNCDSC 0x02 /* HNC driver status change */
+
+/* NCSI response code/reason */
+#define NCSI_PKT_RSP_C_COMPLETED 0x0000 /* Command Completed */
+#define NCSI_PKT_RSP_C_FAILED 0x0001 /* Command Failed */
+#define NCSI_PKT_RSP_C_UNAVAILABLE 0x0002 /* Command Unavailable */
+#define NCSI_PKT_RSP_C_UNSUPPORTED 0x0003 /* Command Unsupported */
+#define NCSI_PKT_RSP_R_NO_ERROR 0x0000 /* No Error */
+#define NCSI_PKT_RSP_R_INTERFACE 0x0001 /* Interface not ready */
+#define NCSI_PKT_RSP_R_PARAM 0x0002 /* Invalid Parameter */
+#define NCSI_PKT_RSP_R_CHANNEL 0x0003 /* Channel not Ready */
+#define NCSI_PKT_RSP_R_PACKAGE 0x0004 /* Package not Ready */
+#define NCSI_PKT_RSP_R_LENGTH 0x0005 /* Invalid payload length */
+#define NCSI_PKT_RSP_R_UNKNOWN 0x7fff /* Command type unsupported */
+
+/* NCSI AEN packet type */
+#define NCSI_PKT_AEN_LSC 0x00 /* Link status change */
+#define NCSI_PKT_AEN_CR 0x01 /* Configuration required */
+#define NCSI_PKT_AEN_HNCDSC 0x02 /* Host driver status change */
+
+#endif /* __NCSI_PKT_H__ */
diff --git a/net/ncsi/ncsi-rsp.c b/net/ncsi/ncsi-rsp.c
new file mode 100644
index 000000000000..c24a1e14de8c
--- /dev/null
+++ b/net/ncsi/ncsi-rsp.c
@@ -0,0 +1,1171 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2015.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>
+
+#include <net/ncsi.h>
+#include <net/net_namespace.h>
+#include <net/sock.h>
+
+#include "internal.h"
+#include "ncsi-pkt.h"
+
+static int ncsi_validate_rsp_pkt(struct ncsi_req *nr,
+ unsigned short payload)
+{
+ struct ncsi_rsp_pkt_hdr *h;
+ unsigned char *stream;
+ __be32 *checksum, csum;
+ __be32 high, low;
+ int i;
+
+ /*
+ * Check NCSI packet header. We don't need validate
+ * the packet type, which should have been checked
+ * before calling this function.
+ */
+ h = (struct ncsi_rsp_pkt_hdr *)skb_network_header(nr->nr_rsp);
+ if (h->common.revision != NCSI_PKT_REVISION)
+ return -EINVAL;
+ if (ntohs(h->common.length) != payload)
+ return -EINVAL;
+
+ /* Check on code and reason */
+ if (ntohs(h->code) != NCSI_PKT_RSP_C_COMPLETED ||
+ ntohs(h->reason) != NCSI_PKT_RSP_R_NO_ERROR)
+ return -EINVAL;
+
+ /*
+ * Validate checksum, which might be zeroes if the
+ * sender doesn't support checksum according to NCSI
+ * specification.
+ */
+ checksum = (__be32 *)((void *)(h + 1) + payload - 4);
+ if (ntohl(*checksum) == 0)
+ return 0;
+
+ csum = 0;
+ stream = (unsigned char *)h;
+ for (i = 0; i < sizeof(*h) + payload - 4; i += 2) {
+ high = stream[i];
+ low = stream[i + 1];
+ csum += ((high << 8) | low);
+ }
+
+ csum = ~csum + 1;
+ if (*checksum != htonl(csum))
+ return -EINVAL;
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_default(struct ncsi_req *nr)
+{
+ return ncsi_validate_rsp_pkt(nr, 0);
+}
+
+static int ncsi_rsp_handler_cis(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_package *np;
+ struct ncsi_channel *nc;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
+ if ((ndp->ndp_flags & NCSI_DEV_PRIV_FLAG_POPULATED) && !nc)
+ return -ENXIO;
+
+ /* Add the channel if necessary */
+ if (!nc)
+ nc = ncsi_add_channel(np,
+ NCSI_CHANNEL_INDEX(rsp->rsp.common.channel));
+ else if (nc->nc_state == ncsi_channel_state_deselected_initial)
+ nc->nc_state = ncsi_channel_state_deselected_ready;
+ else if (nc->nc_state == ncsi_channel_state_selected_initial)
+ nc->nc_state = ncsi_channel_state_selected_ready;
+
+ return nc ? 0 : -ENODEV;
+}
+
+static int ncsi_rsp_handler_sp(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_package *np;
+ struct ncsi_channel *nc;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /*
+ * Add the package if it's not existing. Otherwise,
+ * to change the state of its child channels.
+ */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ &np, NULL);
+ if ((ndp->ndp_flags & NCSI_DEV_PRIV_FLAG_POPULATED) && !np)
+ return -ENXIO;
+
+ if (!np) {
+ np = ncsi_add_package(ndp,
+ NCSI_PACKAGE_INDEX(rsp->rsp.common.channel));
+ if (!np)
+ return -ENODEV;
+ }
+
+ NCSI_FOR_EACH_CHANNEL(np, nc) {
+ if (nc->nc_state == ncsi_channel_state_deselected_initial)
+ nc->nc_state = ncsi_channel_state_selected_initial;
+ else if (nc->nc_state == ncsi_channel_state_deselected_ready)
+ nc->nc_state = ncsi_channel_state_selected_ready;
+ }
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_dp(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_package *np;
+ struct ncsi_channel *nc;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ &np, NULL);
+ if (!np)
+ return -ENODEV;
+
+ /* Change state of all channels attached to the package */
+ NCSI_FOR_EACH_CHANNEL(np, nc) {
+ if (nc->nc_state == ncsi_channel_state_selected_initial)
+ nc->nc_state = ncsi_channel_state_deselected_initial;
+ else if (nc->nc_state == ncsi_channel_state_selected_ready)
+ nc->nc_state = ncsi_channel_state_deselected_ready;
+ }
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_ec(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ ncm = &nc->nc_modes[NCSI_MODE_ENABLE];
+ if (ncm->ncm_enable)
+ return -EBUSY;
+
+ ncm->ncm_enable = 1;
+ return 0;
+}
+
+static int ncsi_rsp_handler_dc(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp= nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ ncm = &nc->nc_modes[NCSI_MODE_ENABLE];
+ if (!ncm->ncm_enable)
+ return -EBUSY;
+
+ ncm->ncm_enable = 0;;
+ return 0;
+}
+
+static int ncsi_rsp_handler_rc(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Update state for the specified channel */
+ if (nc->nc_state == ncsi_channel_state_deselected_ready)
+ nc->nc_state = ncsi_channel_state_deselected_initial;
+ else if (nc->nc_state == ncsi_channel_state_selected_ready)
+ nc->nc_state = ncsi_channel_state_selected_initial;
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_ecnt(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ ncm = &nc->nc_modes[NCSI_MODE_TX_ENABLE];
+ if (ncm->ncm_enable)
+ return -EBUSY;
+
+ ncm->ncm_enable = 1;
+ return 0;
+}
+
+static int ncsi_rsp_handler_dcnt(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ ncm = &nc->nc_modes[NCSI_MODE_TX_ENABLE];
+ if (!ncm->ncm_enable)
+ return -EBUSY;
+
+ ncm->ncm_enable = 1;
+ return 0;
+}
+
+static int ncsi_rsp_handler_ae(struct ncsi_req *nr)
+{
+ struct ncsi_cmd_ae_pkt *cmd;
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Check if the AEN has been enabled */
+ ncm = &nc->nc_modes[NCSI_MODE_AEN];
+ if (ncm->ncm_enable)
+ return -EBUSY;
+
+ /* Update to AEN configuration */
+ cmd = (struct ncsi_cmd_ae_pkt *)skb_network_header(nr->nr_cmd);
+ ncm->ncm_enable = 1;
+ ncm->ncm_data[0] = cmd->mc_id;
+ ncm->ncm_data[1] = ntohl(cmd->mode);
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_sl(struct ncsi_req *nr)
+{
+ struct ncsi_cmd_sl_pkt *cmd;
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ cmd = (struct ncsi_cmd_sl_pkt *)skb_network_header(nr->nr_cmd);
+ ncm = &nc->nc_modes[NCSI_MODE_LINK];
+ ncm->ncm_data[0] = ntohl(cmd->mode);
+ ncm->ncm_data[1] = ntohl(cmd->oem_mode);
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_gls(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_gls_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 16);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_gls_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ ncm = &nc->nc_modes[NCSI_MODE_LINK];
+ ncm->ncm_data[2] = ntohl(rsp->status);
+ ncm->ncm_data[3] = ntohl(rsp->other);
+ ncm->ncm_data[4] = ntohl(rsp->oem_status);
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_svf(struct ncsi_req *nr)
+{
+ struct ncsi_cmd_svf_pkt *cmd;
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_filter *ncf;
+ unsigned short vlan;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->nr_cmd);
+ ncf = nc->nc_filters[NCSI_FILTER_VLAN];
+ if (!ncf)
+ return -ENOENT;
+ if (cmd->index >= ncf->ncf_total)
+ return -ERANGE;
+
+ /* Add or remove the VLAN filter */
+ if (!(cmd->enable & 0x1)) {
+ ret = ncsi_del_channel_filter(nc, NCSI_FILTER_VLAN, cmd->index);
+ } else {
+ vlan = ntohs(cmd->vlan);
+ ret = ncsi_add_channel_filter(nc, NCSI_FILTER_VLAN, &vlan);
+ }
+
+ return ret;
+}
+
+static int ncsi_rsp_handler_ev(struct ncsi_req *nr)
+{
+ struct ncsi_cmd_ev_pkt *cmd;
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Check if VLAN mode has been enabled */
+ ncm = &nc->nc_modes[NCSI_MODE_VLAN];
+ if (ncm->ncm_enable)
+ return -EBUSY;
+
+ /* Update to VLAN mode */
+ cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->nr_cmd);
+ ncm->ncm_enable = 1;
+ ncm->ncm_data[0] = ntohl(cmd->mode);
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_dv(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Check if VLAN mode has been enabled */
+ ncm = &nc->nc_modes[NCSI_MODE_VLAN];
+ if (!ncm->ncm_enable)
+ return -EBUSY;
+
+ /* Update to VLAN mode */
+ ncm->ncm_enable = 0;
+ return 0;
+}
+
+static int ncsi_rsp_handler_sma(struct ncsi_req *nr)
+{
+ struct ncsi_cmd_sma_pkt *cmd;
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_filter *ncf;
+ void *bitmap;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* According to NCSI spec 1.01, the mixed filter table
+ * isn't supported yet.
+ */
+ cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->nr_cmd);
+ switch (cmd->at_e >> 5) {
+ case 0x0: /* UC address */
+ ncf = nc->nc_filters[NCSI_FILTER_UC];
+ break;
+ case 0x1: /* MC address */
+ ncf = nc->nc_filters[NCSI_FILTER_MC];
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ /* Sanity check on the filter */
+ if (!ncf)
+ return -ENOENT;
+ else if (cmd->index >= ncf->ncf_total)
+ return -ERANGE;
+
+ bitmap = &ncf->ncf_bitmap;
+ if (cmd->at_e & 0x1) {
+ if (test_and_set_bit(cmd->index, bitmap))
+ return -EBUSY;
+ memcpy(ncf->ncf_data + 6 * cmd->index, cmd->mac, 6);
+ } else {
+ if (!test_and_clear_bit(cmd->index, bitmap))
+ return -EBUSY;
+
+ memset(ncf->ncf_data + 6 * cmd->index, 0, 6);
+ }
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_ebf(struct ncsi_req *nr)
+{
+ struct ncsi_cmd_ebf_pkt *cmd;
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the package and channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Check if broadcast filter has been enabled */
+ ncm = &nc->nc_modes[NCSI_MODE_BC];
+ if (ncm->ncm_enable)
+ return -EBUSY;
+
+ /* Update to broadcast filter mode */
+ cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->nr_cmd);
+ ncm->ncm_enable = 1;
+ ncm->ncm_data[0] = ntohl(cmd->mode);
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_dbf(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Check if broadcast filter isn't enabled */
+ ncm = &nc->nc_modes[NCSI_MODE_BC];
+ if (!ncm->ncm_enable)
+ return -EBUSY;
+
+ /* Update to broadcast filter mode */
+ ncm->ncm_enable = 0;
+ ncm->ncm_data[0] = 0;
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_egmf(struct ncsi_req *nr)
+{
+ struct ncsi_cmd_egmf_pkt *cmd;
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Check if multicast filter has been enabled */
+ ncm = &nc->nc_modes[NCSI_MODE_MC];
+ if (ncm->ncm_enable)
+ return -EBUSY;
+
+ /* Update to multicast filter mode */
+ cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->nr_cmd);
+ ncm->ncm_enable = 1;
+ ncm->ncm_data[0] = ntohl(cmd->mode);
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_dgmf(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Check if multicast filter has been enabled */
+ ncm = &nc->nc_modes[NCSI_MODE_MC];
+ if (!ncm->ncm_enable)
+ return -EBUSY;
+
+ /* Update to multicast filter mode */
+ ncm->ncm_enable = 0;
+ ncm->ncm_data[0] = 0;
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_snfc(struct ncsi_req *nr)
+{
+ struct ncsi_cmd_snfc_pkt *cmd;
+ struct ncsi_rsp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_mode *ncm;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 4);
+ if (ret)
+ return ret;
+
+ /* Find the channel */
+ rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Check if flow control has been enabled */
+ ncm = &nc->nc_modes[NCSI_MODE_FC];
+ if (ncm->ncm_enable)
+ return -EBUSY;
+
+ /* Update to flow control mode */
+ cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->nr_cmd);
+ ncm->ncm_enable = 1;
+ ncm->ncm_data[0] = cmd->mode;
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_gvi(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_gvi_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_version *ncv;
+ int i, ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 36);
+ if (ret)
+ return ret;
+
+ /* Find the channel */
+ rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Update to channel's version info */
+ ncv = &nc->nc_version;
+ ncv->ncv_version = ntohl(rsp->ncsi_version);
+ ncv->ncv_alpha2 = rsp->alpha2;
+ memcpy(ncv->ncv_fw_name, rsp->fw_name, 12);
+ ncv->ncv_fw_version = ntohl(rsp->fw_version);
+ for (i = 0; i < 4; i++)
+ ncv->ncv_pci_ids[i] = ntohs(rsp->pci_ids[i]);
+ ncv->ncv_mf_id = ntohl(rsp->mf_id);
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_gc(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_gc_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_filter *ncf;
+ size_t size, entry_size;
+ int cnt, i, ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 32);
+ if (ret)
+ return ret;
+
+ /* Find the channel */
+ rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Update channel's capabilities */
+ nc->nc_caps[NCSI_CAP_GENERIC].ncc_cap = ntohl(rsp->cap) &
+ NCSI_CAP_GENERIC_MASK;
+ nc->nc_caps[NCSI_CAP_BC].ncc_cap = ntohl(rsp->bc_cap) &
+ NCSI_CAP_BC_MASK;
+ nc->nc_caps[NCSI_CAP_MC].ncc_cap = ntohl(rsp->mc_cap) &
+ NCSI_CAP_MC_MASK;
+ nc->nc_caps[NCSI_CAP_BUFFER].ncc_cap = ntohl(rsp->buf_cap);
+ nc->nc_caps[NCSI_CAP_AEN].ncc_cap = ntohl(rsp->aen_cap) &
+ NCSI_CAP_AEN_MASK;
+ nc->nc_caps[NCSI_CAP_VLAN].ncc_cap = rsp->vlan_mode &
+ NCSI_CAP_VLAN_MASK;
+
+ /* Build filters */
+ for (i = 0; i < NCSI_FILTER_MAX; i++) {
+ switch (i) {
+ case NCSI_FILTER_VLAN:
+ cnt = rsp->vlan_cnt;
+ entry_size = 2;
+ break;
+ case NCSI_FILTER_MIXED:
+ cnt = rsp->mixed_cnt;
+ entry_size = 6;
+ break;
+ case NCSI_FILTER_MC:
+ cnt = rsp->mc_cnt;
+ entry_size = 6;
+ break;
+ case NCSI_FILTER_UC:
+ cnt = rsp->uc_cnt;
+ entry_size = 6;
+ break;
+ default:
+ continue;
+ }
+
+ if (!cnt || nc->nc_filters[i])
+ continue;
+
+ size = sizeof(*ncf) + cnt * entry_size;
+ ncf = kzalloc(size, GFP_ATOMIC);
+ if (!ncf) {
+ pr_warn("%s: Cannot alloc filter table (%d)\n",
+ __func__, i);
+ return -ENOMEM;
+ }
+
+ ncf->ncf_index = i;
+ ncf->ncf_total = cnt;
+ ncf->ncf_bitmap = 0x0ul;
+ nc->nc_filters[i] = ncf;
+ }
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_gp(struct ncsi_req *nr)
+{
+ struct ncsi_pkt_hdr *h;
+ struct ncsi_rsp_gp_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ unsigned short length, enable, vlan;
+ unsigned char *pdata;
+ int table, i, ret;
+
+ /*
+ * The get parameter response packet has variable length.
+ * The payload should be figured out from the packet
+ * header, instead of the fixed one we have for other types
+ * of packets.
+ */
+ h = (struct ncsi_pkt_hdr *)skb_network_header(nr->nr_rsp);
+ length = ntohs(h->length);
+ if (length < 32)
+ return -EINVAL;
+
+ ret = ncsi_validate_rsp_pkt( nr, length);
+ if (ret)
+ return ret;
+
+ /* Find the channel */
+ rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Modes with explicit enabled indications */
+ if (ntohl(rsp->valid_modes) & 0x1) { /* BC filter mode */
+ nc->nc_modes[NCSI_MODE_BC].ncm_enable = 1;
+ nc->nc_modes[NCSI_MODE_BC].ncm_data[0] = ntohl(rsp->bc_mode);
+ }
+ if (ntohl(rsp->valid_modes) & 0x2) /* Channel enabled */
+ nc->nc_modes[NCSI_MODE_ENABLE].ncm_enable = 1;
+ if (ntohl(rsp->valid_modes) & 0x4) /* Channel Tx enabled */
+ nc->nc_modes[NCSI_MODE_TX_ENABLE].ncm_enable = 1;
+ if (ntohl(rsp->valid_modes) & 0x8) /* MC filter mode */
+ nc->nc_modes[NCSI_MODE_MC].ncm_enable = 1;
+
+ /* Modes without explicit enabled indications */
+ nc->nc_modes[NCSI_MODE_LINK].ncm_enable = 1;
+ nc->nc_modes[NCSI_MODE_LINK].ncm_data[0] = ntohl(rsp->link_mode);
+ nc->nc_modes[NCSI_MODE_VLAN].ncm_enable = 1;
+ nc->nc_modes[NCSI_MODE_VLAN].ncm_data[0] = rsp->vlan_mode;
+ nc->nc_modes[NCSI_MODE_FC].ncm_enable = 1;
+ nc->nc_modes[NCSI_MODE_FC].ncm_data[0] = rsp->fc_mode;
+ nc->nc_modes[NCSI_MODE_AEN].ncm_enable = 1;
+ nc->nc_modes[NCSI_MODE_AEN].ncm_data[0] = ntohl(rsp->aen_mode);
+
+ /* MAC addresses filter table */
+ pdata = (unsigned char *)rsp + 48;
+ enable = rsp->mac_enable;
+ for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
+ if (i >= (nc->nc_filters[NCSI_FILTER_UC]->ncf_total +
+ nc->nc_filters[NCSI_FILTER_MC]->ncf_total))
+ table = NCSI_FILTER_MIXED;
+ else if (i >= nc->nc_filters[NCSI_FILTER_UC]->ncf_total)
+ table = NCSI_FILTER_MC;
+ else
+ table = NCSI_FILTER_UC;
+
+ if (!(enable & (0x1 << i)))
+ continue;
+
+ if (ncsi_find_channel_filter(nc, table, pdata) >= 0)
+ continue;
+
+ ncsi_add_channel_filter(nc, table, pdata);
+ }
+
+ /* VLAN filter table */
+ enable = ntohs(rsp->vlan_enable);
+ for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
+ if (!(enable & (0x1 << i)))
+ continue;
+
+ vlan = ntohs(*(__be16 *)pdata);
+ if (ncsi_find_channel_filter(nc, NCSI_FILTER_VLAN, &vlan) >= 0)
+ continue;
+
+ ncsi_add_channel_filter(nc, NCSI_FILTER_VLAN, &vlan);
+ }
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_gcps(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_gcps_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_stats *ncs;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 172);
+ if (ret)
+ return ret;
+
+ /* Find the channel */
+ rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Update HNC's statistics */
+ ncs = &nc->nc_stats;
+ ncs->ncs_hnc_cnt_hi = ntohl(rsp->cnt_hi);
+ ncs->ncs_hnc_cnt_lo = ntohl(rsp->cnt_lo);
+ ncs->ncs_hnc_rx_bytes = ntohl(rsp->rx_bytes);
+ ncs->ncs_hnc_tx_bytes = ntohl(rsp->tx_bytes);
+ ncs->ncs_hnc_rx_uc_pkts = ntohl(rsp->rx_uc_pkts);
+ ncs->ncs_hnc_rx_mc_pkts = ntohl(rsp->rx_mc_pkts);
+ ncs->ncs_hnc_rx_bc_pkts = ntohl(rsp->rx_bc_pkts);
+ ncs->ncs_hnc_tx_uc_pkts = ntohl(rsp->tx_uc_pkts);
+ ncs->ncs_hnc_tx_mc_pkts = ntohl(rsp->tx_mc_pkts);
+ ncs->ncs_hnc_tx_bc_pkts = ntohl(rsp->tx_bc_pkts);
+ ncs->ncs_hnc_fcs_err = ntohl(rsp->fcs_err);
+ ncs->ncs_hnc_align_err = ntohl(rsp->align_err);
+ ncs->ncs_hnc_false_carrier = ntohl(rsp->false_carrier);
+ ncs->ncs_hnc_runt_pkts = ntohl(rsp->runt_pkts);
+ ncs->ncs_hnc_jabber_pkts = ntohl(rsp->jabber_pkts);
+ ncs->ncs_hnc_rx_pause_xon = ntohl(rsp->rx_pause_xon);
+ ncs->ncs_hnc_rx_pause_xoff = ntohl(rsp->rx_pause_xoff);
+ ncs->ncs_hnc_tx_pause_xon = ntohl(rsp->tx_pause_xon);
+ ncs->ncs_hnc_tx_pause_xoff = ntohl(rsp->tx_pause_xoff);
+ ncs->ncs_hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
+ ncs->ncs_hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
+ ncs->ncs_hnc_l_collision = ntohl(rsp->l_collision);
+ ncs->ncs_hnc_e_collision = ntohl(rsp->e_collision);
+ ncs->ncs_hnc_rx_ctl_frames = ntohl(rsp->rx_ctl_frames);
+ ncs->ncs_hnc_rx_64_frames = ntohl(rsp->rx_64_frames);
+ ncs->ncs_hnc_rx_127_frames = ntohl(rsp->rx_127_frames);
+ ncs->ncs_hnc_rx_255_frames = ntohl(rsp->rx_255_frames);
+ ncs->ncs_hnc_rx_511_frames = ntohl(rsp->rx_511_frames);
+ ncs->ncs_hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
+ ncs->ncs_hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
+ ncs->ncs_hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
+ ncs->ncs_hnc_tx_64_frames = ntohl(rsp->tx_64_frames);
+ ncs->ncs_hnc_tx_127_frames = ntohl(rsp->tx_127_frames);
+ ncs->ncs_hnc_tx_255_frames = ntohl(rsp->tx_255_frames);
+ ncs->ncs_hnc_tx_511_frames = ntohl(rsp->tx_511_frames);
+ ncs->ncs_hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
+ ncs->ncs_hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
+ ncs->ncs_hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
+ ncs->ncs_hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
+ ncs->ncs_hnc_rx_runt_pkts = ntohl(rsp->rx_runt_pkts);
+ ncs->ncs_hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_gns(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_gns_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_stats *ncs;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 172);
+ if (ret)
+ return ret;
+
+ /* Find the channel */
+ rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Update HNC's statistics */
+ ncs = &nc->nc_stats;
+ ncs->ncs_ncsi_rx_cmds = ntohl(rsp->rx_cmds);
+ ncs->ncs_ncsi_dropped_cmds = ntohl(rsp->dropped_cmds);
+ ncs->ncs_ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
+ ncs->ncs_ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
+ ncs->ncs_ncsi_rx_pkts = ntohl(rsp->rx_pkts);
+ ncs->ncs_ncsi_tx_pkts = ntohl(rsp->tx_pkts);
+ ncs->ncs_ncsi_tx_aen_pkts = ntohl(rsp->tx_aen_pkts);
+
+ return 0;
+}
+
+static int ncsi_rsp_handler_gnpts(struct ncsi_req *nr)
+{
+ struct ncsi_rsp_gnpts_pkt *rsp;
+ struct ncsi_dev_priv *ndp = nr->nr_ndp;
+ struct ncsi_channel *nc;
+ struct ncsi_channel_stats *ncs;
+ int ret;
+
+ ret = ncsi_validate_rsp_pkt( nr, 172);
+ if (ret)
+ return ret;
+
+ /* Find the channel */
+ rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->nr_rsp);
+ ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+ NULL, &nc);
+ if (!nc)
+ return -ENODEV;
+
+ /* Update HNC's statistics */
+ ncs = &nc->nc_stats;
+ ncs->ncs_pt_tx_pkts = ntohl(rsp->tx_pkts);
+ ncs->ncs_pt_tx_dropped = ntohl(rsp->tx_dropped);
+ ncs->ncs_pt_tx_channel_err = ntohl(rsp->tx_channel_err);
+ ncs->ncs_pt_tx_us_err = ntohl(rsp->tx_us_err);
+ ncs->ncs_pt_rx_pkts = ntohl(rsp->rx_pkts);
+ ncs->ncs_pt_rx_dropped = ntohl(rsp->rx_dropped);
+ ncs->ncs_pt_rx_channel_err = ntohl(rsp->rx_channel_err);
+ ncs->ncs_pt_rx_us_err = ntohl(rsp->rx_us_err);
+ ncs->ncs_pt_rx_os_err = ntohl(rsp->rx_os_err);
+
+ return 0;
+}
+
+static struct ncsi_rsp_handler {
+ unsigned char nrh_type;
+ int (*nrh_handler)(struct ncsi_req *nr);
+} ncsi_rsp_handlers[] = {
+ { NCSI_PKT_RSP_CIS, ncsi_rsp_handler_cis },
+ { NCSI_PKT_RSP_SP, ncsi_rsp_handler_sp },
+ { NCSI_PKT_RSP_DP, ncsi_rsp_handler_dp },
+ { NCSI_PKT_RSP_EC, ncsi_rsp_handler_ec },
+ { NCSI_PKT_RSP_DC, ncsi_rsp_handler_dc },
+ { NCSI_PKT_RSP_RC, ncsi_rsp_handler_rc },
+ { NCSI_PKT_RSP_ECNT, ncsi_rsp_handler_ecnt },
+ { NCSI_PKT_RSP_DCNT, ncsi_rsp_handler_dcnt },
+ { NCSI_PKT_RSP_AE, ncsi_rsp_handler_ae },
+ { NCSI_PKT_RSP_SL, ncsi_rsp_handler_sl },
+ { NCSI_PKT_RSP_GLS, ncsi_rsp_handler_gls },
+ { NCSI_PKT_RSP_SVF, ncsi_rsp_handler_svf },
+ { NCSI_PKT_RSP_EV, ncsi_rsp_handler_ev },
+ { NCSI_PKT_RSP_DV, ncsi_rsp_handler_dv },
+ { NCSI_PKT_RSP_SMA, ncsi_rsp_handler_sma },
+ { NCSI_PKT_RSP_EBF, ncsi_rsp_handler_ebf },
+ { NCSI_PKT_RSP_DBF, ncsi_rsp_handler_dbf },
+ { NCSI_PKT_RSP_EGMF, ncsi_rsp_handler_egmf },
+ { NCSI_PKT_RSP_DGMF, ncsi_rsp_handler_dgmf },
+ { NCSI_PKT_RSP_SNFC, ncsi_rsp_handler_snfc },
+ { NCSI_PKT_RSP_GVI, ncsi_rsp_handler_gvi },
+ { NCSI_PKT_RSP_GC, ncsi_rsp_handler_gc },
+ { NCSI_PKT_RSP_GP, ncsi_rsp_handler_gp },
+ { NCSI_PKT_RSP_GCPS, ncsi_rsp_handler_gcps },
+ { NCSI_PKT_RSP_GNS, ncsi_rsp_handler_gns },
+ { NCSI_PKT_RSP_GNPTS, ncsi_rsp_handler_gnpts },
+ { NCSI_PKT_RSP_OEM, ncsi_rsp_handler_default },
+ { 0, NULL }
+};
+
+#if 0
+void ncsi_pkt_dump(struct sk_buff *skb)
+{
+ struct skb_shared_info *info = skb_shinfo(skb);
+ skb_frag_t *frag;
+ char *data;
+ int limit, i;
+
+ pr_info("head: 0x%p data: 0x%p tail: 0x%p end: 0x%p\n",
+ skb->head, skb->data,
+ skb_tail_pointer(skb), skb_end_pointer(skb));
+ pr_info("mac_header: 0x%p network_header: 0x%p\n",
+ skb_mac_header(skb), skb_network_header(skb));
+ pr_info("len: 0x%x data_len: 0x%x truesize: 0x%x\n",
+ skb->len, skb->data_len, skb->truesize);
+
+ for (i = 0; i < info->nr_frags; i++) {
+ frag = &info->frags[i];
+ pr_info("FRAG[%d]: 0x%p offset: 0x%x size: 0x%x\n",
+ i, frag->page.p, frag->page_offset, frag->size);
+ }
+
+ data = skb_mac_header(skb);
+ limit = skb->len + sizeof(struct ethhdr);
+ for (i = 0; i < limit; data++, i++) {
+ if (i % 16 == 0)
+ printk("\n%02x ", *data);
+ else
+ printk("%02x ", *data);
+ }
+}
+#endif
+
+int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
+ struct packet_type *pt, struct net_device *orig_dev)
+{
+ struct ncsi_rsp_handler *nrh = NULL;
+ struct ncsi_dev *nd;
+ struct ncsi_dev_priv *ndp;
+ struct ncsi_req *nr;
+ struct ncsi_pkt_hdr *hdr;
+ unsigned long flags;
+ int ret;
+
+ /* Find the NCSI device */
+ nd = ncsi_find_dev(dev);
+ ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+ if (!ndp)
+ return -ENODEV;
+
+ /* Check if it's AEN packet */
+ hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
+ if (hdr->type == NCSI_PKT_AEN)
+ return ncsi_aen_handler(ndp, skb);
+
+ /* Find the handler */
+ nrh = ncsi_rsp_handlers;
+ while (nrh->nrh_handler) {
+ if (nrh->nrh_type == hdr->type)
+ break;
+
+ nrh++;
+ }
+
+ if (!nrh->nrh_handler) {
+ pr_warn("NCSI: Received unrecognized packet (0x%x)\n",
+ hdr->type);
+ return -ENOENT;
+ }
+
+ /* Associate with the request */
+ nr = &ndp->ndp_reqs[hdr->id];
+ spin_lock_irqsave(&ndp->ndp_req_lock, flags);
+ if (!nr->nr_used) {
+ spin_unlock_irqrestore(&ndp->ndp_req_lock, flags);
+ return -ENODEV;
+ }
+
+ nr->nr_rsp = skb;
+ if (!nr->nr_timer_enabled) {
+ ret = -ENOENT;
+ goto out;
+ }
+
+ /* Process the response */
+ ret = nrh->nrh_handler(nr);
+
+out:
+ spin_unlock_irqrestore(&ndp->ndp_req_lock, flags);
+ ncsi_free_req(nr);
+ return ret;
+}