All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC v1 0/7] NCSI Support
@ 2015-10-06  3:09 Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 1/7] net/ncsi: Resource management Gavin Shan
                   ` (8 more replies)
  0 siblings, 9 replies; 12+ messages in thread
From: Gavin Shan @ 2015-10-06  3:09 UTC (permalink / raw)
  To: netdev; +Cc: benh, davem, Gavin Shan

This patchset is prototype requesting for comments.

This series of patches intends to support NCSI stack, which is specified by
DSP0222. One NCSI enabled interface potentially connects to multiple packages
and channels, but there is one active channel at once when hardware arbitration
is disabled. No hardware arbitration isn't supported by this patchset:

   * NIC driver registers NCSI device when the network device is registered. The
     NCSI device should be started when bringing up the interface so that the
     active channel can be choosed from the available channels and starts to provide
     service.
   * When NCSI AEN packet is received from currently active channel for sake of
     failure, another active channel is tried to choosed to accomplish the failover.
   * Reserved netlink interface for NCSI is supported so that userland can retrieve
     NCSI information or configure from/to NCSI channels.
   * The first user of NCSI stack is faraday driver (ftgmac100.c).

Gavin Shan (7):
  net/ncsi: Resource management
  net/ncsi: Packet handler
  net/ncsi: Manage NCSI device
  net/ncsi: Netlink support
  net/faraday: Replace use_nc_si with use_ncsi
  net/faraday: Enable NCSI interface
  net/faraday: Enable offload checksum according to device-tree

 drivers/net/ethernet/faraday/ftgmac100.c |  109 ++-
 include/net/ncsi.h                       |   59 ++
 include/uapi/linux/if_ether.h            |    1 +
 include/uapi/linux/ncsi.h                |  200 +++++
 include/uapi/linux/netlink.h             |    1 +
 net/Kconfig                              |    1 +
 net/Makefile                             |    1 +
 net/ncsi/Kconfig                         |   10 +
 net/ncsi/Makefile                        |    5 +
 net/ncsi/internal.h                      |  165 +++++
 net/ncsi/ncsi-aen.c                      |  197 +++++
 net/ncsi/ncsi-cmd.c                      |  380 ++++++++++
 net/ncsi/ncsi-manage.c                   |  914 +++++++++++++++++++++++
 net/ncsi/ncsi-netlink.c                  | 1042 ++++++++++++++++++++++++++
 net/ncsi/ncsi-pkt.h                      |  391 ++++++++++
 net/ncsi/ncsi-rsp.c                      | 1167 ++++++++++++++++++++++++++++++
 net/ncsi/ncsi.c                          |   49 ++
 17 files changed, 4661 insertions(+), 31 deletions(-)
 create mode 100644 include/net/ncsi.h
 create mode 100644 include/uapi/linux/ncsi.h
 create mode 100644 net/ncsi/Kconfig
 create mode 100644 net/ncsi/Makefile
 create mode 100644 net/ncsi/internal.h
 create mode 100644 net/ncsi/ncsi-aen.c
 create mode 100644 net/ncsi/ncsi-cmd.c
 create mode 100644 net/ncsi/ncsi-manage.c
 create mode 100644 net/ncsi/ncsi-netlink.c
 create mode 100644 net/ncsi/ncsi-pkt.h
 create mode 100644 net/ncsi/ncsi-rsp.c
 create mode 100644 net/ncsi/ncsi.c

-- 
2.1.0

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH RFC v1 1/7] net/ncsi: Resource management
  2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
@ 2015-10-06  3:09 ` Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 2/7] net/ncsi: Packet handler Gavin Shan
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Gavin Shan @ 2015-10-06  3:09 UTC (permalink / raw)
  To: netdev; +Cc: benh, davem, Gavin Shan

According to NCSI spec (DSP0222), the NCSI enabled interface can
connected to multiple packages simultaneously, up to 8 packages.
Each package includes multiple channels, up to 32 channels. At
one moment, one channel is enabled to provide service to the NCSI
enabled interface. Besides, each channel comprises capabilities,
modes, filters, version and statistics etc. All of them are
resources to NCSI protocol stack.

At the meanwhile, the NCSI device seen from NIC driver is
represented by "struct ncsi_dev", which is expected to populated
and started by NIC driver before the NIC can work. All possible
NCSI packages and NCSI channels connected to the NCSI interface
are tracked from the NCSI device. Also, the NCSI device recognizes
active channel that is currently providing service to NCSI enabled
interface. Also, the NCSI requests (pairs of command and response)
are embedded in NCSI device.

This introduces the data structs to represents the NCSI resources
mentioned as above. Also, functions used by NCSI stack internally
are implemented. Besides, this introduces kernel config option
CONFIG_NET_NCSI to enable NCSI stack.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
---
 include/net/ncsi.h        |  21 +++
 include/uapi/linux/ncsi.h | 200 +++++++++++++++++++++++++
 net/Kconfig               |   1 +
 net/Makefile              |   1 +
 net/ncsi/Kconfig          |  10 ++
 net/ncsi/Makefile         |   4 +
 net/ncsi/internal.h       | 153 +++++++++++++++++++
 net/ncsi/ncsi-manage.c    | 368 ++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 758 insertions(+)
 create mode 100644 include/net/ncsi.h
 create mode 100644 include/uapi/linux/ncsi.h
 create mode 100644 net/ncsi/Kconfig
 create mode 100644 net/ncsi/Makefile
 create mode 100644 net/ncsi/internal.h
 create mode 100644 net/ncsi/ncsi-manage.c

diff --git a/include/net/ncsi.h b/include/net/ncsi.h
new file mode 100644
index 0000000..2eecfbd
--- /dev/null
+++ b/include/net/ncsi.h
@@ -0,0 +1,21 @@
+#ifndef __NET_NCSI_H
+#define __NET_NCSI_H
+
+#include <uapi/linux/ncsi.h>
+
+enum {
+	ncsi_dev_state_registered	= 0x0000,
+	ncsi_dev_state_functional	= 0x0100,
+	ncsi_dev_state_start		= 0x0200,
+	ncsi_dev_state_config		= 0x0300,
+	ncsi_dev_state_stop		= 0x0400
+};
+
+struct ncsi_dev {
+	int			nd_state;
+	int			nd_link_up;
+	struct net_device	*nd_dev;
+	void			(*nd_handler)(struct ncsi_dev *ndev);
+};
+
+#endif /* __NET_NCSI_H */
diff --git a/include/uapi/linux/ncsi.h b/include/uapi/linux/ncsi.h
new file mode 100644
index 0000000..9a3d180
--- /dev/null
+++ b/include/uapi/linux/ncsi.h
@@ -0,0 +1,200 @@
+#ifndef _UAPI_LINUX_NCSI_H
+#define _UAPI_LINUX_NCSI_H
+
+/* NCSI netlink message type */
+enum {
+	NCSI_MSG_BASE		= 16,
+	NCSI_MSG_GET_LAYOUT	= 16,
+	NCSI_MSG_GET_VERSION,
+	NCSI_MSG_GET_CAP,
+	NCSI_MSG_GET_MODE,
+	NCSI_MSG_GET_FILTER,
+	NCSI_MSG_GET_STATS,
+	NCSI_MSG_SET_MODE,
+	NCSI_MSG_SET_FILTER,
+	NCSI_MSG_MAX
+};
+
+
+/* NCSI channel capabilities */
+enum {
+	NCSI_CAP_BASE		= 0,
+	NCSI_CAP_GENERIC	= 0,
+	NCSI_CAP_BC,
+	NCSI_CAP_MC,
+	NCSI_CAP_BUFFER,
+	NCSI_CAP_AEN,
+	NCSI_CAP_VLAN,
+	NCSI_CAP_MAX
+};
+
+enum {
+	NCSI_CAP_GENERIC_HWA	= 0x01,	/* HW arbitration             */
+	NCSI_CAP_GENERIC_HDS	= 0x02,	/* HNC driver status change   */
+	NCSI_CAP_GENERIC_FC	= 0x04,	/* HNC to MC flow control     */
+	NCSI_CAP_GENERIC_FC1	= 0x08,	/* MC to HNC flow control     */
+	NCSI_CAP_GENERIC_MC	= 0x10,	/* Global multicast filtering */
+	NCSI_CAP_GENERIC_MASK	= 0x1f,
+	NCSI_CAP_BC_ARP		= 0x01,	/* ARP packet filtering       */
+	NCSI_CAP_BC_DHCPC	= 0x02,	/* DHCP client filtering      */
+	NCSI_CAP_BC_DHCPS	= 0x04,	/* DHCP server filtering      */
+	NCSI_CAP_BC_NETBIOS	= 0x08,	/* NetBIOS packet filtering   */
+	NCSI_CAP_BC_MASK	= 0x0f,
+	NCSI_CAP_MC_NEIGHBOR	= 0x01,	/* IPv6 neighbor filtering    */
+	NCSI_CAP_MC_ROUTER	= 0x02,	/* IPv6 router filering       */
+	NCSI_CAP_MC_DHCPv6	= 0x04,	/* DHCPv6 filtering           */
+	NCSI_CAP_MC_MASK	= 0x07,
+	NCSI_CAP_AEN_LSC	= 0x01,	/* Link status change AEN     */
+	NCSI_CAP_AEN_CR		= 0x02,	/* Configuration required AEN */
+	NCSI_CAP_AEN_HDS	= 0x04,	/* HNC driver status AEN      */
+	NCSI_CAP_AEN_MASK	= 0x07,
+	NCSI_CAP_VLAN_ONLY	= 0x01,	/* VLAN is supported          */
+	NCSI_CAP_VLAN_NO	= 0x02,	/* Filter VLAN and non-VLAN   */
+	NCSI_CAP_VLAN_ANY	= 0x04,	/* Filter Any-and-non-VLAN    */
+	NCSI_CAP_VLAN_MASK	= 0x07
+};
+
+/* NCSI channel mode */
+enum {
+	NCSI_MODE_BASE		= 0,
+	NCSI_MODE_ENABLE	= 0,
+	NCSI_MODE_TX_ENABLE,
+	NCSI_MODE_LINK,
+	NCSI_MODE_VLAN,
+	NCSI_MODE_BC,
+	NCSI_MODE_MC,
+	NCSI_MODE_AEN,
+	NCSI_MODE_FC,
+	NCSI_MODE_MAX
+};
+
+/* NCSI channel filters */
+enum {
+	NCSI_FILTER_BASE	= 0,
+	NCSI_FILTER_VLAN	= 0,
+	NCSI_FILTER_UC,
+	NCSI_FILTER_MC,
+	NCSI_FILTER_MIXED,
+	NCSI_FILTER_MAX
+};
+
+/*
+ * It's put right after netlink message header. Also, it's
+ * used to convey NCSI topology layout.
+ */
+struct ncsi_msg {
+	__u32	nm_flag;
+#define NCSI_FLAG_REQUEST		0x1
+#define NCSI_FLAG_RESPONSE		0x2
+#define NCSI_FLAG_ACTIVE_CHANNEL	0x4
+
+	__u32	nm_ifindex;		/* ID of network device               */
+	__u32	nm_package_id;		/* ID of NCSI package                 */
+	__u32	nm_channel_id;		/* ID of NCSI channel                 */
+	__u32	nm_index;		/* ID of mode, capability or filter   */
+	__u32	nm_errcode;		/* Error code                         */
+};
+
+enum {
+	NCSI_SUCCESS,
+	NCSI_ERR_PARAM,
+	NCSI_ERR_NO_MEM,
+	NCSI_ERR_NO_DEV,
+	NCSI_ERR_NOT_ACTIVE,
+	NCSI_ERR_INTERNAL,
+};
+
+/* NCSI channel version */
+struct ncsi_channel_version {
+	__u32	ncv_version;		/* Supported BCD encoded NCSI version */
+	__u32	ncv_alpha2;		/* Supported BCD encoded NCSI version */
+	__u8	ncv_fw_name[12];	/* Firware name string                */
+	__u32	ncv_fw_version;		/* Firmware version                   */
+	__u16	ncv_pci_ids[4];		/* PCI identification                 */
+	__u32	ncv_mf_id;		/* Manufacture ID                     */
+};
+
+/* NCSI channel capability */
+struct ncsi_channel_cap {
+	__u32	ncc_index;		/* Index of channel capabilities     */
+	__u32	ncc_cap;		/* NCSI channel capability           */
+};
+
+/* NCSI channel mode */
+struct ncsi_channel_mode {
+	__u32	ncm_index;		/* Index of channel modes            */
+	__u32	ncm_enable;		/* Enabled or disabled               */
+	__u32	ncm_size;		/* Valid entries in ncm_data[]       */
+	__u32	ncm_data[8];		/* Data entries                      */
+};
+
+/* NCSI channel filter */
+struct ncsi_channel_filter {
+	__u32	ncf_index;		/* Index of channel filters          */
+	__u32	ncf_total;		/* Total entries in the filter table */
+	__u64	ncf_bitmap;		/* Bitmap of valid entries           */
+	__u8	ncf_data[];		/* Data for the valid entries        */
+};
+
+/* NCSI channel statistics */
+struct ncsi_channel_stats {
+	__u32	ncs_hnc_cnt_hi;			/* Counter cleared            */
+	__u32	ncs_hnc_cnt_lo;			/* Counter cleared            */
+	__u32	ncs_hnc_rx_bytes;		/* Rx bytes                   */
+	__u32	ncs_hnc_tx_bytes;		/* Tx bytes                   */
+	__u32	ncs_hnc_rx_uc_pkts;		/* Rx UC packets              */
+	__u32	ncs_hnc_rx_mc_pkts;		/* Rx MC packets              */
+	__u32	ncs_hnc_rx_bc_pkts;		/* Rx BC packets              */
+	__u32	ncs_hnc_tx_uc_pkts;		/* Tx UC packets              */
+	__u32	ncs_hnc_tx_mc_pkts;		/* Tx MC packets              */
+	__u32	ncs_hnc_tx_bc_pkts;		/* Tx BC packets              */
+	__u32	ncs_hnc_fcs_err;		/* FCS errors                 */
+	__u32	ncs_hnc_align_err;		/* Alignment errors           */
+	__u32	ncs_hnc_false_carrier;		/* False carrier detection    */
+	__u32	ncs_hnc_runt_pkts;		/* Rx runt packets            */
+	__u32	ncs_hnc_jabber_pkts;		/* Rx jabber packets          */
+	__u32	ncs_hnc_rx_pause_xon;		/* Rx pause XON frames        */
+	__u32	ncs_hnc_rx_pause_xoff;		/* Rx XOFF frames             */
+	__u32	ncs_hnc_tx_pause_xon;		/* Tx XON frames              */
+	__u32	ncs_hnc_tx_pause_xoff;		/* Tx XOFF frames             */
+	__u32	ncs_hnc_tx_s_collision;		/* Single collision frames    */
+	__u32	ncs_hnc_tx_m_collision;		/* Multiple collision frames  */
+	__u32	ncs_hnc_l_collision;		/* Late collision frames      */
+	__u32	ncs_hnc_e_collision;		/* Excessive collision frames */
+	__u32	ncs_hnc_rx_ctl_frames;		/* Rx control frames          */
+	__u32	ncs_hnc_rx_64_frames;		/* Rx 64-bytes frames         */
+	__u32	ncs_hnc_rx_127_frames;		/* Rx 65-127 bytes frames     */
+	__u32	ncs_hnc_rx_255_frames;		/* Rx 128-255 bytes frames    */
+	__u32	ncs_hnc_rx_511_frames;		/* Rx 256-511 bytes frames    */
+	__u32	ncs_hnc_rx_1023_frames;		/* Rx 512-1023 bytes frames   */
+	__u32	ncs_hnc_rx_1522_frames;		/* Rx 1024-1522 bytes frames  */
+	__u32	ncs_hnc_rx_9022_frames;		/* Rx 1523-9022 bytes frames  */
+	__u32	ncs_hnc_tx_64_frames;		/* Tx 64-bytes frames         */
+	__u32	ncs_hnc_tx_127_frames;		/* Tx 65-127 bytes frames     */
+	__u32	ncs_hnc_tx_255_frames;		/* Tx 128-255 bytes frames    */
+	__u32	ncs_hnc_tx_511_frames;		/* Tx 256-511 bytes frames    */
+	__u32	ncs_hnc_tx_1023_frames;		/* Tx 512-1023 bytes frames   */
+	__u32	ncs_hnc_tx_1522_frames;		/* Tx 1024-1522 bytes frames  */
+	__u32	ncs_hnc_tx_9022_frames;		/* Tx 1523-9022 bytes frames  */
+	__u32	ncs_hnc_rx_valid_bytes;		/* Rx valid bytes             */
+	__u32	ncs_hnc_rx_runt_pkts;		/* Rx error runt packets      */
+	__u32	ncs_hnc_rx_jabber_pkts;		/* Rx error jabber packets    */
+	__u32	ncs_ncsi_rx_cmds;		/* Rx NCSI commands           */
+	__u32	ncs_ncsi_dropped_cmds;		/* Dropped commands           */
+	__u32	ncs_ncsi_cmd_type_errs;		/* Command type errors        */
+	__u32	ncs_ncsi_cmd_csum_errs;		/* Command checksum errors    */
+	__u32	ncs_ncsi_rx_pkts;		/* Rx NCSI packets            */
+	__u32	ncs_ncsi_tx_pkts;		/* Tx NCSI packets            */
+	__u32	ncs_ncsi_tx_aen_pkts;		/* Tx AEN packets             */
+	__u32	ncs_pt_tx_pkts;			/* Tx packets                 */
+	__u32	ncs_pt_tx_dropped;		/* Tx dropped packets         */
+	__u32	ncs_pt_tx_channel_err;		/* Tx channel errors          */
+	__u32	ncs_pt_tx_us_err;		/* Tx undersize errors        */
+	__u32	ncs_pt_rx_pkts;			/* Rx packets                 */
+	__u32	ncs_pt_rx_dropped;		/* Rx dropped packets         */
+	__u32	ncs_pt_rx_channel_err;		/* Rx channel errors          */
+	__u32	ncs_pt_rx_us_err;		/* Rx undersize errors        */
+	__u32	ncs_pt_rx_os_err;		/* Rx oversize errors         */
+};
+
+#endif /* _UAPI_LINUX_NCSI_H */
diff --git a/net/Kconfig b/net/Kconfig
index 57a7c5a..a92fc73 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -232,6 +232,7 @@ source "net/netlink/Kconfig"
 source "net/mpls/Kconfig"
 source "net/hsr/Kconfig"
 source "net/switchdev/Kconfig"
+source "net/ncsi/Kconfig"
 
 config RPS
 	bool
diff --git a/net/Makefile b/net/Makefile
index 3995613..9c1d4b1 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -74,3 +74,4 @@ obj-$(CONFIG_HSR)		+= hsr/
 ifneq ($(CONFIG_NET_SWITCHDEV),)
 obj-y				+= switchdev/
 endif
+obj-$(CONFIG_NET_NCSI)		+= ncsi/
diff --git a/net/ncsi/Kconfig b/net/ncsi/Kconfig
new file mode 100644
index 0000000..723f0eb
--- /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 0000000..07b5625
--- /dev/null
+++ b/net/ncsi/Makefile
@@ -0,0 +1,4 @@
+#
+# Makefile for NCSI API
+#
+obj-$(CONFIG_NET_NCSI) += ncsi-manage.o
diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
new file mode 100644
index 0000000..c9511bb
--- /dev/null
+++ b/net/ncsi/internal.h
@@ -0,0 +1,153 @@
+#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_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_skb_parms {
+	unsigned int		nsp_valid;
+	unsigned int		nsp_portid;
+	struct nlmsghdr		nsp_nlh;
+};
+
+#define NCSI_CB(skb)	(*(struct ncsi_skb_parms*)&((skb)->cb))
+
+struct ncsi_req {
+	unsigned char		nr_id;
+	bool			nr_used;
+	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_start_deselect	= 0x0201,
+	ncsi_dev_state_start_package,
+	ncsi_dev_state_start_channel,
+	ncsi_dev_state_start_sp,
+	ncsi_dev_state_start_cis,
+	ncsi_dev_state_start_gvi,
+	ncsi_dev_state_start_gc,
+	ncsi_dev_state_start_dp,
+	ncsi_dev_state_start_active,
+	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_gls,
+	ncsi_dev_state_config_done,
+	ncsi_dev_state_stop_select	= 0x0401,
+	ncsi_dev_state_stop_dcnt,
+	ncsi_dev_state_stop_dc,
+	ncsi_dev_state_stop_deselect,
+	ncsi_dev_state_stop_done
+};
+
+struct ncsi_dev_priv {
+	struct ncsi_dev		ndp_ndev;
+	int			ndp_flags;
+#define NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE	0x1
+	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;
+	atomic_t		ndp_pending_reqs;
+	atomic_t		ndp_last_req_idx;
+	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;
+	struct nlmsghdr		*nca_nlh;
+	unsigned int		nca_portid;
+	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);
+void ncsi_free_req(struct ncsi_req *nr, bool check, bool timeout);
+struct ncsi_dev *ncsi_find_dev(struct net_device *dev);
+
+#endif /* __NCSI_INTERNAL_H__ */
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
new file mode 100644
index 0000000..765217c
--- /dev/null
+++ b/net/ncsi/ncsi-manage.c
@@ -0,0 +1,368 @@
+/*
+ * 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 "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)
+{
+	struct ncsi_req *nr = NULL;
+	int idx, limit = 256;
+
+	spin_lock(&ndp->ndp_req_lock);
+
+	/* Check if there is one available request until the ceiling */
+	for (idx = atomic_read(&ndp->ndp_last_req_idx);
+	     !nr && idx < limit; idx++) {
+		if (ndp->ndp_reqs[idx].nr_used)
+			continue;
+
+		ndp->ndp_reqs[idx].nr_used = true;
+		nr = &ndp->ndp_reqs[idx];
+		atomic_inc(&ndp->ndp_last_req_idx);
+		if (atomic_read(&ndp->ndp_last_req_idx) >= limit)
+			atomic_set(&ndp->ndp_last_req_idx, 0);
+	}
+
+	/* Fail back to check from the starting cursor */
+	for (idx = 0; !nr && idx < atomic_read(&ndp->ndp_last_req_idx); idx++) {
+		if (ndp->ndp_reqs[idx].nr_used)
+			continue;
+
+		ndp->ndp_reqs[idx].nr_used = true;
+		nr = &ndp->ndp_reqs[idx];
+		atomic_inc(&ndp->ndp_last_req_idx);
+		if (atomic_read(&ndp->ndp_last_req_idx) >= limit)
+			atomic_set(&ndp->ndp_last_req_idx, 0);
+	}
+
+	spin_unlock(&ndp->ndp_req_lock);
+	return nr;
+}
+
+void ncsi_free_req(struct ncsi_req *nr, bool check, bool timeout)
+{
+	struct ncsi_dev_priv *ndp = nr->nr_ndp;
+	struct sk_buff *cmd, *rsp;
+
+	if (nr->nr_timer_enabled) {
+		nr->nr_timer_enabled = false;
+		del_timer_sync(&nr->nr_timer);
+	}
+
+	spin_lock(&ndp->ndp_req_lock);
+	cmd = nr->nr_cmd;
+	rsp = nr->nr_rsp;
+	nr->nr_cmd = NULL;
+	nr->nr_rsp = NULL;
+	nr->nr_used = false;
+	spin_unlock(&ndp->ndp_req_lock);
+
+	/* 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;
+}
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH RFC v1 2/7] net/ncsi: Packet handler
  2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 1/7] net/ncsi: Resource management Gavin Shan
@ 2015-10-06  3:09 ` Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 3/7] net/ncsi: Manage NCSI device Gavin Shan
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Gavin Shan @ 2015-10-06  3:09 UTC (permalink / raw)
  To: netdev; +Cc: benh, davem, Gavin Shan

NCSI packet encapsulated in ethernet frame is recognized by the
ethernet protocol field (0x88F8). According to NCSI spec, the
destination MAC field should be 0xFF's, but the source MAC field
could be arbitrary one since channel field in NCSI header indicates
the source of the packet.

There are 3 types of packets depending on the type field in the NCSI
packet header: command, response, Asynchronous Event Notification
(AEN). The command packets are sent from NCSI enabled interace to
the NCSI package or channel for purpose of information retrival or
configuration. The response packets, as responses to the received
command packets, originate from NCSI package or channel and terminate
at NCSI enabled interface. AEN packets are sent from NCSI channel
for asynchronous events that might indicate failure detected on
the active channel.

This implements the functions to handle NCSI packets.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
---
 include/uapi/linux/if_ether.h |    1 +
 net/ncsi/Makefile             |    3 +-
 net/ncsi/internal.h           |    6 +
 net/ncsi/ncsi-aen.c           |  194 +++++++
 net/ncsi/ncsi-cmd.c           |  380 ++++++++++++++
 net/ncsi/ncsi-pkt.h           |  391 ++++++++++++++
 net/ncsi/ncsi-rsp.c           | 1167 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 2141 insertions(+), 1 deletion(-)
 create mode 100644 net/ncsi/ncsi-aen.c
 create mode 100644 net/ncsi/ncsi-cmd.c
 create mode 100644 net/ncsi/ncsi-pkt.h
 create mode 100644 net/ncsi/ncsi-rsp.c

diff --git a/include/uapi/linux/if_ether.h b/include/uapi/linux/if_ether.h
index aa63ed0..db85863 100644
--- a/include/uapi/linux/if_ether.h
+++ b/include/uapi/linux/if_ether.h
@@ -85,6 +85,7 @@
 #define ETH_P_8021AH	0x88E7          /* 802.1ah Backbone Service Tag */
 #define ETH_P_MVRP	0x88F5          /* 802.1Q MVRP                  */
 #define ETH_P_1588	0x88F7		/* IEEE 1588 Timesync */
+#define ETH_P_NCSI	0x88F8		/* NCSI protocol                */
 #define ETH_P_PRP	0x88FB		/* IEC 62439-3 PRP/HSRv0	*/
 #define ETH_P_FCOE	0x8906		/* Fibre Channel over Ethernet  */
 #define ETH_P_TDLS	0x890D          /* TDLS */
diff --git a/net/ncsi/Makefile b/net/ncsi/Makefile
index 07b5625..e4094c2 100644
--- a/net/ncsi/Makefile
+++ b/net/ncsi/Makefile
@@ -1,4 +1,5 @@
 #
 # Makefile for NCSI API
 #
-obj-$(CONFIG_NET_NCSI) += ncsi-manage.o
+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
index c9511bb..9262717 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -150,4 +150,10 @@ struct ncsi_req *ncsi_alloc_req(struct ncsi_dev_priv *ndp);
 void ncsi_free_req(struct ncsi_req *nr, bool check, bool timeout);
 struct ncsi_dev *ncsi_find_dev(struct net_device *dev);
 
+/* 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 0000000..f5f4d2f
--- /dev/null
+++ b/net/ncsi/ncsi-aen.c
@@ -0,0 +1,194 @@
+/*
+ * 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_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 ||
+	    ncm->ncm_data[2] & 0x1)
+		return 0;
+
+	/* If this channel is the active one and the link is down,
+	 * we have to choose another channel to be active one.
+	 */
+	ndp->ndp_flags |= NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE;
+	/* FIXME: Stop active channel and choose another one */
+
+	return 0;
+}
+
+static int ncsi_aen_handler_cr(struct ncsi_dev_priv *ndp,
+			       struct ncsi_aen_pkt_hdr *h)
+{
+	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;
+
+	/* FIXME: Reconfigure active channel */
+
+	return 0;
+}
+
+static int ncsi_aen_handler_hncdsc(struct ncsi_dev_priv *ndp,
+				   struct ncsi_aen_pkt_hdr *h)
+{
+	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 ||
+	    ncm->ncm_data[3] & 0x1)
+		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.
+	 */
+	ndp->ndp_flags |= NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE;
+	/* FIXME: Stop and choose another channel as active one */
+
+	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 0000000..d8efa25
--- /dev/null
+++ b/net/ncsi/ncsi-cmd.c
@@ -0,0 +1,380 @@
+/*
+ * 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);
+	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, false, false);
+		return NULL;
+	}
+
+	nr->nr_cmd = skb;
+	skb_reserve(skb, hlen);
+	skb_reset_network_header(skb);
+
+	skb->dev = dev;
+	skb->protocol = htons(ETH_P_NCSI);
+
+	if (nca->nca_nlh) {
+		NCSI_CB(skb).nsp_valid = 1;
+		memcpy(&NCSI_CB(skb).nsp_nlh, nca->nca_nlh,
+		       nlmsg_total_size(sizeof(struct ncsi_msg)));
+	} else {
+		NCSI_CB(skb).nsp_valid = 0;
+	}
+
+	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;
+	}
+
+	/* Send NCSI packet */
+	skb_get(nr->nr_cmd);
+	ret = dev_queue_xmit_sk(NULL, nr->nr_cmd);
+	if (ret)
+		goto out;
+
+	/* Start the timer for the request that might not have
+	 * corresponding response. I'm not sure 1 second delay
+	 * here is enough. Anyway, NCSI is internal network, so
+	 * the responsiveness should be as fast as enough.
+	 */
+	nr->nr_timer_enabled = true;
+	mod_timer(&nr->nr_timer, jiffies + 1 * HZ);
+
+	return 0;
+out:
+	ncsi_free_req(nr, false, false);
+	return ret;
+}
diff --git a/net/ncsi/ncsi-pkt.h b/net/ncsi/ncsi-pkt.h
new file mode 100644
index 0000000..646f1fc
--- /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 0000000..2b19653
--- /dev/null
+++ b/net/ncsi/ncsi-rsp.c
@@ -0,0 +1,1167 @@
+/*
+ * 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 (!np)
+		return -ENODEV;
+
+	/* 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 (!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;
+	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(&ndp->ndp_req_lock);
+	if (!nr->nr_used) {
+		spin_unlock(&ndp->ndp_req_lock);
+		return -ENODEV;
+	}
+
+	nr->nr_rsp = skb;
+	if (!nr->nr_timer_enabled) {
+		spin_unlock(&ndp->ndp_req_lock);
+		ret = -ENOENT;
+		goto out;
+	}
+
+	/* Process the response */
+	ret = nrh->nrh_handler(nr);
+
+out:
+	ncsi_free_req(nr, true, false);
+	return ret;
+}
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH RFC v1 3/7] net/ncsi: Manage NCSI device
  2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 1/7] net/ncsi: Resource management Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 2/7] net/ncsi: Packet handler Gavin Shan
@ 2015-10-06  3:09 ` Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 4/7] net/ncsi: Netlink support Gavin Shan
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Gavin Shan @ 2015-10-06  3:09 UTC (permalink / raw)
  To: netdev; +Cc: benh, davem, Gavin Shan

There are multiple packages and channels connecting to NCSI enabled
interface. The available packages and channels are probed with NCSI
packets when NCSI device is started. Among the available packages
and channels, only one channel should be selected to be active to
provide service. When the active channel enters to failure state,
we need choose another channel as active one to do failover.

This implements the state machine to support above tasks. Also, the
public interfaces are exposed for NIC driver to manage NCSI device.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
---
 include/net/ncsi.h     |  38 ++++
 net/ncsi/internal.h    |   1 +
 net/ncsi/ncsi-aen.c    |   9 +-
 net/ncsi/ncsi-manage.c | 539 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 584 insertions(+), 3 deletions(-)

diff --git a/include/net/ncsi.h b/include/net/ncsi.h
index 2eecfbd..6041ca3 100644
--- a/include/net/ncsi.h
+++ b/include/net/ncsi.h
@@ -3,6 +3,17 @@
 
 #include <uapi/linux/ncsi.h>
 
+/*
+ * The NCSI device states seen from external. More NCSI device states are
+ * only visible internally (in net/ncsi/internal.h). When the NCSI device
+ * is registered, it's in ncsi_dev_state_registered state. The state
+ * ncsi_dev_state_start is used to drive to choose active package and
+ * channel. After that, its state is changed to ncsi_dev_state_functional.
+ *
+ * The state ncsi_dev_state_stop helps to shut down the currently active
+ * package and channel while ncsi_dev_state_config helps to reconfigure
+ * them.
+ */
 enum {
 	ncsi_dev_state_registered	= 0x0000,
 	ncsi_dev_state_functional	= 0x0100,
@@ -18,4 +29,31 @@ struct ncsi_dev {
 	void			(*nd_handler)(struct ncsi_dev *ndev);
 };
 
+#ifdef CONFIG_NET_NCSI
+struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
+				   void (*notifier)(struct ncsi_dev *nd));
+int ncsi_start_dev(struct ncsi_dev *nd);
+int ncsi_stop_dev(struct ncsi_dev *nd);
+void ncsi_unregister_dev(struct ncsi_dev *nd);
+#else /* !CONFIG_NET_NCSI */
+static inline struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
+						 void (*notifier)(struct ncsi_dev *nd))
+{
+	return NULL;
+}
+
+static inline int ncsi_start_dev(struct ncsi_dev *nd)
+{
+	return -ENOTTY;
+}
+
+static inline int ncsi_stop_dev(struct ncsi_dev *nd)
+{
+	return -ENOTTY;
+}
+
+void inline ncsi_unregister_dev(struct ncsi_dev *nd)
+{
+}
+#endif /* CONFIG_NET_NCSI */
 #endif /* __NET_NCSI_H */
diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index 9262717..02cab91 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -149,6 +149,7 @@ void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
 struct ncsi_req *ncsi_alloc_req(struct ncsi_dev_priv *ndp);
 void ncsi_free_req(struct ncsi_req *nr, bool check, bool timeout);
 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);
diff --git a/net/ncsi/ncsi-aen.c b/net/ncsi/ncsi-aen.c
index f5f4d2f..89433bc 100644
--- a/net/ncsi/ncsi-aen.c
+++ b/net/ncsi/ncsi-aen.c
@@ -60,6 +60,7 @@ static int ncsi_validate_aen_pkt(struct ncsi_aen_pkt_hdr *h,
 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;
@@ -88,7 +89,7 @@ static int ncsi_aen_handler_lsc(struct ncsi_dev_priv *ndp,
 	 * we have to choose another channel to be active one.
 	 */
 	ndp->ndp_flags |= NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE;
-	/* FIXME: Stop active channel and choose another one */
+	ncsi_stop_dev(nd);
 
 	return 0;
 }
@@ -96,6 +97,7 @@ static int ncsi_aen_handler_lsc(struct ncsi_dev_priv *ndp,
 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;
 
@@ -113,7 +115,7 @@ static int ncsi_aen_handler_cr(struct ncsi_dev_priv *ndp,
 	    ndp->ndp_active_channel != nc)
 		return 0;
 
-	/* FIXME: Reconfigure active channel */
+	ncsi_config_dev(nd);
 
 	return 0;
 }
@@ -121,6 +123,7 @@ static int ncsi_aen_handler_cr(struct ncsi_dev_priv *ndp,
 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;
@@ -149,7 +152,7 @@ static int ncsi_aen_handler_hncdsc(struct ncsi_dev_priv *ndp,
 	 * is down on the active channel.
 	 */
 	ndp->ndp_flags |= NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE;
-	/* FIXME: Stop and choose another channel as active one */
+	ncsi_stop_dev(nd);
 
 	return 0;
 }
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index 765217c..5d66b6f 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -18,6 +18,7 @@
 #include <net/net_namespace.h>
 #include <net/sock.h>
 
+#include "ncsi-pkt.h"
 #include "internal.h"
 
 LIST_HEAD(ncsi_dev_list);
@@ -350,6 +351,8 @@ void ncsi_free_req(struct ncsi_req *nr, bool check, bool timeout)
 	nr->nr_used = false;
 	spin_unlock(&ndp->ndp_req_lock);
 
+	if (check && cmd && atomic_dec_return(&ndp->ndp_pending_reqs) == 0)
+		schedule_work(&ndp->ndp_work);
 	/* Release command and response */
 	consume_skb(cmd);
 	consume_skb(rsp);
@@ -366,3 +369,539 @@ struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
 
 	return NULL;
 }
+
+static int ncsi_select_active_channel(struct ncsi_dev_priv *ndp)
+{
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+
+	/* For now, we simply choose the first valid channel as active one.
+	 * There might be more factors, like the channel's capacity, can
+	 * be considered to pick the active channel in future.
+	 */
+	NCSI_FOR_EACH_PACKAGE(ndp, np) {
+		NCSI_FOR_EACH_CHANNEL(np, nc) {
+			ndp->ndp_active_package = np;
+			ndp->ndp_active_channel = nc;
+			return 0;
+		}
+	}
+
+	return -ENXIO;
+}
+
+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_cmd_arg nca;
+	unsigned char index;
+	int ret;
+
+	nca.nca_ndp = ndp;
+	nca.nca_nlh = NULL;
+
+	/* 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 = 0x1f;
+		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_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_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;
+
+		if (!(ndp->ndp_flags & NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE))
+			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_dev_start(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_nlh = NULL;
+	switch (nd->nd_state) {
+	case ncsi_dev_state_start:
+		nd->nd_state = ncsi_dev_state_start_deselect;
+		/* Fall through */
+	case ncsi_dev_state_start_deselect:
+		atomic_set(&ndp->ndp_pending_reqs, 8);
+
+		/* Deselect all possible packages */
+		nca.nca_type = NCSI_PKT_CMD_DP;
+		nca.nca_channel = 0x1f;
+		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_start_package;
+		break;
+	case ncsi_dev_state_start_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 = 0x1f;
+		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_start_channel;
+		break;
+	case ncsi_dev_state_start_channel:
+		/* The available packages should have been detected. To
+		 * iterate every package to probe its channels.
+		 */
+		if (!ndp->ndp_active_package) {
+			ndp->ndp_active_package = list_first_or_null_rcu(
+				&ndp->ndp_packages, struct ncsi_package,
+				np_node);
+			if (!ndp->ndp_active_package)
+				goto error;
+		} else {
+			if (list_is_last(&ndp->ndp_active_package->np_node,
+					 &ndp->ndp_packages)) {
+				nd->nd_state = ncsi_dev_state_start_active;
+				goto choose_active_channel;
+			}
+
+			ndp->ndp_active_package = list_entry_rcu(
+				ndp->ndp_active_package->np_node.next,
+				struct ncsi_package, np_node);
+		}
+		/* Fall through */
+	case ncsi_dev_state_start_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 = ndp->ndp_active_package->np_id;
+		nca.nca_channel = 0x1f;
+		ret = ncsi_xmit_cmd(&nca);
+		if (ret)
+			goto error;
+
+		nd->nd_state = ncsi_dev_state_start_cis;
+		break;
+	case ncsi_dev_state_start_cis:
+		atomic_set(&ndp->ndp_pending_reqs, 0x20);
+
+		/* Clear initial state */
+		nca.nca_type = NCSI_PKT_CMD_CIS;
+		nca.nca_package = ndp->ndp_active_package->np_id;
+		for (index = 0; index < 0x20; index++) {
+			nca.nca_channel = index;
+			ret = ncsi_xmit_cmd(&nca);
+			if (ret)
+				goto error;
+		}
+
+		nd->nd_state = ncsi_dev_state_start_gvi;
+		break;
+	case ncsi_dev_state_start_gvi:
+	case ncsi_dev_state_start_gc:
+		/* The available channels of the active package should have
+		 * been populated.
+		 */
+		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_start_gvi)
+			nca.nca_type = NCSI_PKT_CMD_GVI;
+		else
+			nca.nca_type = NCSI_PKT_CMD_GC;
+
+		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_start_gvi)
+			nd->nd_state = ncsi_dev_state_start_gc;
+		else
+			nd->nd_state = ncsi_dev_state_start_dp;
+		break;
+	case ncsi_dev_state_start_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 = 0x1f;
+		ret = ncsi_xmit_cmd(&nca);
+		if (ret)
+			goto error;
+
+		nd->nd_state = ncsi_dev_state_start_channel;
+		break;
+	case ncsi_dev_state_start_active:
+choose_active_channel:
+		/* All packages and channels should have been populated. Also,
+		 * the information for all channels should have been retrieved.
+		 */
+		ndp->ndp_active_package = NULL;
+		ncsi_select_active_channel(ndp);
+		if (!ndp->ndp_active_package ||
+		    !ndp->ndp_active_channel)
+			goto error;
+
+		/* To configure the active channel */
+		nd->nd_state = ncsi_dev_state_config_sma;
+		ncsi_dev_config(ndp);
+	default:
+		pr_debug("%s: Unrecognized NCSI dev state 0x%x\n",
+			 __func__, nd->nd_state);
+	}
+
+	return;
+
+error:
+	ndp->ndp_flags &= ~NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE;
+	nd->nd_state = ncsi_dev_state_functional;
+	nd->nd_link_up = 0;
+	nd->nd_handler(nd);
+}
+
+static void ncsi_dev_stop(struct ncsi_dev_priv *ndp)
+{
+	struct ncsi_dev *nd = &ndp->ndp_ndev;
+	struct ncsi_package *np, *tmp;
+	struct ncsi_channel *nc;
+	struct ncsi_cmd_arg nca;
+	int ret;
+
+	nca.nca_ndp = ndp;
+	nca.nca_nlh = NULL;
+	switch (nd->nd_state) {
+	case ncsi_dev_state_stop:
+		/* If there're no active channel, we're done */
+		if (!ndp->ndp_active_channel) {
+			nd->nd_state = ncsi_dev_state_stop_done;
+			goto done;
+		}
+
+		nd->nd_state = ncsi_dev_state_stop_select;
+		/* Fall through */
+	case ncsi_dev_state_stop_select:
+	case ncsi_dev_state_stop_dcnt:
+	case ncsi_dev_state_stop_dc:
+	case ncsi_dev_state_stop_deselect:
+		atomic_set(&ndp->ndp_pending_reqs, 1);
+
+		np = ndp->ndp_active_package;
+		nc = ndp->ndp_active_channel;
+		nca.nca_package = np->np_id;
+		if (nd->nd_state == ncsi_dev_state_stop_select) {
+			nca.nca_type = NCSI_PKT_CMD_SP;
+			nca.nca_channel = 0x1f;
+			nca.nca_bytes[0] = 1;
+			nd->nd_state = ncsi_dev_state_stop_dcnt;
+		} else if (nd->nd_state == ncsi_dev_state_stop_dcnt) {
+			nca.nca_type = NCSI_PKT_CMD_DCNT;
+			nca.nca_channel = nc->nc_id;
+			nd->nd_state = ncsi_dev_state_stop_dc;
+		} else if (nd->nd_state == ncsi_dev_state_stop_dc) {
+			nca.nca_type = NCSI_PKT_CMD_DC;
+			nca.nca_channel = nc->nc_id;
+			nca.nca_bytes[0] = 1;
+			nd->nd_state = ncsi_dev_state_stop_deselect;
+		} else if (nd->nd_state == ncsi_dev_state_stop_deselect) {
+			nca.nca_type = NCSI_PKT_CMD_DP;
+			nca.nca_channel = 0x1f;
+			nd->nd_state = ncsi_dev_state_stop_done;
+		}
+
+		ret = ncsi_xmit_cmd(&nca);
+		if (ret) {
+			nd->nd_state = ncsi_dev_state_stop_done;
+			goto done;
+		}
+
+		break;
+	case ncsi_dev_state_stop_done:
+done:
+		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);
+
+		if (!(ndp->ndp_flags & NCSI_DEV_PRIV_FLAG_CHANGE_ACTIVE)) {
+			nd->nd_state = ncsi_dev_state_functional;
+			nd->nd_link_up = 0;
+			nd->nd_handler(nd);
+		} else {
+			nd->nd_state = ncsi_dev_state_start;
+			ncsi_dev_start(ndp);
+		}
+
+		break;
+	default:
+		pr_warn("%s: Unsupported NCSI dev state 0x%x\n",
+			__func__, nd->nd_state);
+	}
+}
+
+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_start:
+		ncsi_dev_start(ndp);
+		break;
+	case ncsi_dev_state_stop:
+		ncsi_dev_stop(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;
+
+	/* If the request already had associated response,
+	 * let the response handler to release it.
+	 */
+	spin_lock(&ndp->ndp_req_lock);
+	nr->nr_timer_enabled = false;
+	if (nr->nr_rsp || !nr->nr_cmd) {
+		spin_unlock(&ndp->ndp_req_lock);
+		return;
+	}
+	spin_unlock(&ndp->ndp_req_lock);
+
+	/* Release the request */
+	ncsi_free_req(nr, true, true);
+}
+
+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, 0);
+	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;
+
+	nd->nd_state = ncsi_dev_state_start;
+	schedule_work(&ndp->ndp_work);
+
+	return 0;
+}
+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_stop_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_stop;
+	schedule_work(&ndp->ndp_work);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(ncsi_stop_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);
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH RFC v1 4/7] net/ncsi: Netlink support
  2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
                   ` (2 preceding siblings ...)
  2015-10-06  3:09 ` [PATCH RFC v1 3/7] net/ncsi: Manage NCSI device Gavin Shan
@ 2015-10-06  3:09 ` Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 5/7] net/faraday: Replace use_nc_si with use_ncsi Gavin Shan
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Gavin Shan @ 2015-10-06  3:09 UTC (permalink / raw)
  To: netdev; +Cc: benh, davem, Gavin Shan

This adds netlink support so that userland can retrieve the NCSI
topology on the specified interface. Also, more netlink commands
are introduced to retrieve information from the active channel
or configure the active channel with provided options.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
---
 include/uapi/linux/netlink.h |    1 +
 net/ncsi/Makefile            |    2 +-
 net/ncsi/internal.h          |    5 +
 net/ncsi/ncsi-manage.c       |    7 +
 net/ncsi/ncsi-netlink.c      | 1042 ++++++++++++++++++++++++++++++++++++++++++
 net/ncsi/ncsi.c              |   49 ++
 6 files changed, 1105 insertions(+), 1 deletion(-)
 create mode 100644 net/ncsi/ncsi-netlink.c
 create mode 100644 net/ncsi/ncsi.c

diff --git a/include/uapi/linux/netlink.h b/include/uapi/linux/netlink.h
index cf6a65c..c51af4b 100644
--- a/include/uapi/linux/netlink.h
+++ b/include/uapi/linux/netlink.h
@@ -27,6 +27,7 @@
 #define NETLINK_ECRYPTFS	19
 #define NETLINK_RDMA		20
 #define NETLINK_CRYPTO		21	/* Crypto layer */
+#define NETLINK_NCSI		22	/* NCSI */
 
 #define NETLINK_INET_DIAG	NETLINK_SOCK_DIAG
 
diff --git a/net/ncsi/Makefile b/net/ncsi/Makefile
index e4094c2..bf9f7a9 100644
--- a/net/ncsi/Makefile
+++ b/net/ncsi/Makefile
@@ -2,4 +2,4 @@
 # Makefile for NCSI API
 #
 obj-$(CONFIG_NET_NCSI) += ncsi-cmd.o ncsi-rsp.o ncsi-aen.o \
-			  ncsi-manage.o
+			  ncsi-manage.o ncsi-netlink.o ncsi.o
diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index 02cab91..51683f2 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -157,4 +157,9 @@ 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);
 
+/* Netlink */
+int __net_init ncsi_netlink_init(struct net *net);
+void __net_exit ncsi_netlink_exit(struct net *net);
+void ncsi_netlink_reply(struct nlmsghdr *h,
+			unsigned int portid, bool timeout);
 #endif /* __NCSI_INTERNAL_H__ */
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index 5d66b6f..eb71c71 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -351,6 +351,13 @@ void ncsi_free_req(struct ncsi_req *nr, bool check, bool timeout)
 	nr->nr_used = false;
 	spin_unlock(&ndp->ndp_req_lock);
 
+	/* If the NCSI command was sent because of netlink
+	 * messages, we need reply with the result or error.
+	 */
+	if (check && cmd && NCSI_CB(cmd).nsp_valid)
+		ncsi_netlink_reply(&NCSI_CB(cmd).nsp_nlh,
+				   NCSI_CB(cmd).nsp_portid, timeout);
+
 	if (check && cmd && atomic_dec_return(&ndp->ndp_pending_reqs) == 0)
 		schedule_work(&ndp->ndp_work);
 	/* Release command and response */
diff --git a/net/ncsi/ncsi-netlink.c b/net/ncsi/ncsi-netlink.c
new file mode 100644
index 0000000..bcabd39
--- /dev/null
+++ b/net/ncsi/ncsi-netlink.c
@@ -0,0 +1,1042 @@
+/*
+ * 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 "internal.h"
+#include "ncsi-pkt.h"
+
+static struct sock *ncsi_sock;
+
+static void ncsi_netlink_error(struct nlmsghdr *h,
+			       unsigned int portid,
+			       int errcode)
+{
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	struct ncsi_msg *src, *dst;
+	int ret;
+
+	skb = nlmsg_new(sizeof(*dst), GFP_ATOMIC);
+	if (!skb)
+		return;
+
+	/* The request might not have the common data instance */
+	nlh = nlmsg_put(skb, h->nlmsg_pid, h->nlmsg_seq,
+			h->nlmsg_type, sizeof(*dst), 0);
+	dst = nlmsg_data(nlh);
+	if (nlmsg_len(h) >= sizeof(*src)) {
+		src = nlmsg_data(h);
+		memcpy(dst, src, sizeof(*dst));
+		dst->nm_flag &= ~NCSI_FLAG_REQUEST;
+		dst->nm_flag |= NCSI_FLAG_RESPONSE;
+		dst->nm_errcode = errcode;
+	} else {
+		memset(dst, 0, sizeof(*dst));
+		dst->nm_flag = NCSI_FLAG_RESPONSE;
+		dst->nm_errcode = errcode;
+	}
+
+	nlmsg_end(skb, nlh);
+	ret = nlmsg_notify(ncsi_sock, skb, portid, 0, 1, 0);
+	if (ret) {
+		pr_warn("%s: Error %d sending message (%d)\n",
+			__func__, ret, errcode);
+		nlmsg_free(skb);
+	}
+}
+
+static struct sk_buff *ncsi_netlink_get_layout(struct nlmsghdr *h,
+					       int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	struct ncsi_msg nm, *src, *dst;
+	size_t size = sizeof(*dst);;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	if (!ndp) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+
+	/* Allocate response */
+	NCSI_FOR_EACH_PACKAGE(ndp, np)
+		size += nla_total_size(sizeof(nm)) *
+			atomic_read(&np->np_channel_num);
+	if (size <= sizeof(nm)) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+	skb = nlmsg_new(size, GFP_KERNEL);
+	if (!skb) {
+		*errcode = NCSI_ERR_NO_MEM;
+		return NULL;
+	}
+
+	/* Fill header */
+	nlh = nlmsg_put(skb, h->nlmsg_pid, h->nlmsg_seq,
+			h->nlmsg_type, sizeof(nm), 0);
+	dst = nlmsg_data(nlh);
+	memcpy(dst, src, sizeof(*dst));
+	dst->nm_flag &= ~NCSI_FLAG_REQUEST;
+	dst->nm_flag |= NCSI_FLAG_RESPONSE;
+
+	/* All available channels */
+	NCSI_FOR_EACH_PACKAGE(ndp, np) {
+		NCSI_FOR_EACH_CHANNEL(np, nc) {
+			nm.nm_flag = 0;
+			if (nc == ndp->ndp_active_channel)
+				nm.nm_flag = NCSI_FLAG_ACTIVE_CHANNEL;
+			nm.nm_ifindex = src->nm_ifindex;
+			nm.nm_package_id = np->np_id;
+			nm.nm_channel_id = nc->nc_id;
+			nm.nm_index = 0;
+			nm.nm_index = NCSI_SUCCESS;
+
+			if (nla_put(skb, 0, sizeof(nm), &nm)) {
+				*errcode = NCSI_ERR_INTERNAL;
+				nlmsg_free(skb);
+				return NULL;
+			}
+		}
+	}
+
+	nlmsg_end(skb, nlh);
+	return skb;
+}
+
+static struct sk_buff *ncsi_netlink_get_version(struct nlmsghdr *h,
+						int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_version *ncv;
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	struct ncsi_msg *src, *dst;
+	size_t size = sizeof(*dst);;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+
+	/* Allocate response */
+	size += nla_total_size(sizeof(*ncv));
+	skb = nlmsg_new(size, GFP_KERNEL);
+	if (!skb) {
+		*errcode = NCSI_ERR_NO_MEM;
+		return NULL;
+	}
+
+	/* Fill header */
+	nlh = nlmsg_put(skb, h->nlmsg_pid, h->nlmsg_seq,
+			h->nlmsg_type, sizeof(*dst), 0);
+	dst = nlmsg_data(nlh);
+	memcpy(dst, src, sizeof(*dst));
+	dst->nm_flag &= ~NCSI_FLAG_REQUEST;
+	dst->nm_flag |= NCSI_FLAG_RESPONSE;
+
+	/* Fill channel version */
+	ncv = &nc->nc_version;
+	if (nla_put(skb, 0, sizeof(*ncv), ncv)) {
+		*errcode = NCSI_ERR_INTERNAL;
+		nlmsg_free(skb);
+		return NULL;
+	}
+
+	nlmsg_end(skb, nlh);
+	return skb;
+}
+
+static struct sk_buff *ncsi_netlink_get_cap(struct nlmsghdr *h,
+					    int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_cap *ncc;
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	struct ncsi_msg *src, *dst;
+	size_t size = sizeof(*dst);
+	int index;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+
+	/* Allocate response */
+	if (src->nm_index > NCSI_CAP_MAX) {
+		*errcode = NCSI_ERR_PARAM;
+		return NULL;
+	} else if (src->nm_index == NCSI_CAP_MAX) {
+		size += nla_total_size(sizeof(*ncc)) * NCSI_CAP_MAX;
+	} else {
+		size += nla_total_size(sizeof(*ncc));
+	}
+	skb = nlmsg_new(size, GFP_KERNEL);
+	if (!skb) {
+		*errcode = NCSI_ERR_NO_MEM;
+		return NULL;
+	}
+
+	/* Fill header */
+	nlh = nlmsg_put(skb, h->nlmsg_pid, h->nlmsg_seq,
+			h->nlmsg_type, sizeof(*dst), 0);
+	dst = nlmsg_data(nlh);
+	memcpy(dst, src, sizeof(*dst));
+	dst->nm_flag &= ~NCSI_FLAG_REQUEST;
+	dst->nm_flag |= NCSI_FLAG_RESPONSE;
+
+	/* Fill one or all capabilities */
+	for (index = 0; index < NCSI_CAP_MAX; index++) {
+		ncc = &nc->nc_caps[index];
+		if (src->nm_index == NCSI_CAP_MAX ||
+		    src->nm_index == index) {
+			if (nla_put(skb, 0, sizeof(*ncc), ncc)) {
+				*errcode = NCSI_ERR_INTERNAL;
+				nlmsg_free(skb);
+				return NULL;
+			}
+		}
+	}
+
+	nlmsg_end(skb, nlh);
+	return skb;
+}
+
+static struct sk_buff *ncsi_netlink_get_mode(struct nlmsghdr *h,
+					     int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_cmd_arg nca;
+	struct ncsi_msg *src;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+	if (nd->nd_state != ncsi_dev_state_functional ||
+	    ndp->ndp_active_channel != nc) {
+		*errcode = NCSI_ERR_NOT_ACTIVE;
+		return NULL;
+	}
+	if (src->nm_index > NCSI_MODE_MAX) {
+		*errcode = NCSI_ERR_PARAM;
+		return NULL;
+	}
+
+	/* Send NCSI GP command */
+	nca.nca_ndp = ndp;
+	nca.nca_nlh = h;
+	nca.nca_type = NCSI_PKT_CMD_GP;
+	nca.nca_package = np->np_id;
+	nca.nca_channel = nc->nc_id;
+	if (ncsi_xmit_cmd(&nca)) {
+		*errcode = NCSI_ERR_INTERNAL;
+		return NULL;
+	}
+
+	*errcode = NCSI_SUCCESS;
+	return NULL;
+}
+
+static struct sk_buff *ncsi_netlink_get_filter(struct nlmsghdr *h,
+					       int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_cmd_arg nca;
+	struct ncsi_msg *src;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+	if (nd->nd_state != ncsi_dev_state_functional ||
+	    ndp->ndp_active_channel != nc) {
+		*errcode = NCSI_ERR_NOT_ACTIVE;
+		return NULL;
+	}
+	if (src->nm_index > NCSI_FILTER_MAX) {
+		*errcode = NCSI_ERR_PARAM;
+		return NULL;
+	}
+
+	/* Send NCSI GP command */
+	nca.nca_ndp = ndp;
+	nca.nca_nlh = h;
+	nca.nca_type = NCSI_PKT_CMD_GP;
+	nca.nca_package = np->np_id;
+	nca.nca_channel = nc->nc_id;
+	if (ncsi_xmit_cmd(&nca)) {
+		*errcode = NCSI_ERR_INTERNAL;
+		return NULL;
+	}
+
+	*errcode = NCSI_SUCCESS;
+	return NULL;
+}
+
+struct sk_buff *ncsi_netlink_get_stats(struct nlmsghdr *h, int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_msg *src;
+	struct ncsi_cmd_arg nca;
+	unsigned char cmd;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+	if (nd->nd_state != ncsi_dev_state_functional ||
+	    ndp->ndp_active_channel != nc) {
+		*errcode = NCSI_ERR_NOT_ACTIVE;
+		return NULL;
+	}
+	if (src->nm_index > NCSI_FILTER_MAX) {
+		*errcode = NCSI_ERR_PARAM;
+		return NULL;
+	}
+
+	/* Get NIC statistics */
+	nca.nca_ndp = ndp;
+	nca.nca_nlh = NULL;
+	nca.nca_type = NCSI_PKT_CMD_GCPS;
+	nca.nca_package = np->np_id;
+	nca.nca_channel = nc->nc_id;
+	for (cmd = NCSI_PKT_CMD_GCPS; cmd <= NCSI_PKT_CMD_GNPTS; cmd++) {
+		nca.nca_type = cmd;
+		if (cmd == NCSI_PKT_CMD_GNPTS)
+			nca.nca_nlh = h;
+		if (ncsi_xmit_cmd(&nca)) {
+			*errcode = NCSI_ERR_INTERNAL;
+			return NULL;
+		}
+	}
+
+	*errcode = NCSI_SUCCESS;
+	return NULL;
+}
+
+struct sk_buff *ncsi_netlink_set_mode(struct nlmsghdr *h, int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+	struct ncsi_msg *src;
+	struct nlattr *nla;
+	struct ncsi_cmd_arg nca;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+	if (nd->nd_state != ncsi_dev_state_functional ||
+	    ndp->ndp_active_channel != nc) {
+		*errcode = NCSI_ERR_NOT_ACTIVE;
+		return NULL;
+	}
+
+	/* Get NIC statistics */
+	nla = nlmsg_attrdata(h, sizeof(*src));
+	ncm = nla_data(nla);
+
+	nca.nca_ndp = ndp;
+	nca.nca_nlh = h;
+	nca.nca_package = np->np_id;
+	nca.nca_channel = nc->nc_id;
+	switch (src->nm_index) {
+	case NCSI_MODE_ENABLE:
+		if (ncm->ncm_enable) {
+			nca.nca_type = NCSI_PKT_CMD_EC;
+		} else {
+			nca.nca_type = NCSI_PKT_CMD_DC;
+			nca.nca_bytes[0] = ncm->ncm_data[0];
+		}
+		break;
+	case NCSI_MODE_TX_ENABLE:
+		if (ncm->ncm_enable)
+			nca.nca_type = NCSI_PKT_CMD_ECNT;
+		else
+			nca.nca_type = NCSI_PKT_CMD_DCNT;
+		break;
+	case NCSI_MODE_LINK:
+		nca.nca_type = NCSI_PKT_CMD_SL;
+		nca.nca_dwords[0] = ncm->ncm_data[0];
+		nca.nca_dwords[1] = ncm->ncm_data[1];
+		break;
+	case NCSI_MODE_VLAN:
+		if (ncm->ncm_enable) {
+			nca.nca_type = NCSI_PKT_CMD_EV;
+			nca.nca_bytes[0] = ncm->ncm_data[0];
+		} else {
+			nca.nca_type = NCSI_PKT_CMD_DV;
+		}
+		break;
+	case NCSI_MODE_BC:
+		if (ncm->ncm_enable) {
+			nca.nca_type = NCSI_PKT_CMD_EBF;
+			nca.nca_dwords[0] = ncm->ncm_data[0];
+		} else {
+			nca.nca_type = NCSI_PKT_CMD_DBF;
+		}
+		break;
+	case NCSI_MODE_MC:
+		if (ncm->ncm_enable) {
+			nca.nca_type = NCSI_PKT_CMD_EGMF;
+			nca.nca_dwords[0] = ncm->ncm_data[0];
+		} else {
+			nca.nca_type = NCSI_PKT_CMD_DGMF;
+		}
+		break;
+	case NCSI_MODE_AEN:
+		nca.nca_type = NCSI_PKT_CMD_AE;
+		nca.nca_bytes[0] = ncm->ncm_data[0];
+		nca.nca_dwords[1] = ncm->ncm_data[1];
+		break;
+	case NCSI_MODE_FC:
+		nca.nca_type = NCSI_PKT_CMD_SNFC;
+		nca.nca_bytes[0] = ncm->ncm_data[0];
+		break;
+	default:
+		*errcode = NCSI_ERR_PARAM;
+		return NULL;
+	}
+
+	if (ncsi_xmit_cmd(&nca)) {
+		*errcode = NCSI_ERR_INTERNAL;
+		return NULL;
+	}
+
+	*errcode = NCSI_SUCCESS;
+	return NULL;
+}
+
+struct sk_buff *ncsi_netlink_set_filter(struct nlmsghdr *h, int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_filter *ncf;
+	struct ncsi_msg *src;
+	struct nlattr *nla;
+	struct ncsi_cmd_arg nca;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+	if (nd->nd_state != ncsi_dev_state_functional ||
+	    ndp->ndp_active_channel != nc) {
+		*errcode = NCSI_ERR_NOT_ACTIVE;
+		return NULL;
+	}
+
+	/* Get NIC statistics */
+	nla = nlmsg_attrdata(h, sizeof(*src));
+	ncf = nla_data(nla);
+
+	nca.nca_ndp = ndp;
+	nca.nca_nlh = h;
+	nca.nca_package = np->np_id;
+	nca.nca_channel = nc->nc_id;
+	switch (src->nm_index) {
+	case NCSI_FILTER_VLAN:
+		nca.nca_type = NCSI_PKT_CMD_SVF;
+		memcpy(nca.nca_bytes, ncf->ncf_data, 4);
+		break;
+	case NCSI_FILTER_UC:
+	case NCSI_FILTER_MC:
+		memcpy(nca.nca_bytes, ncf->ncf_data, 8);
+		nca.nca_bytes[7] &= 0x1f;
+		if (src->nm_index == NCSI_FILTER_MC)
+			nca.nca_bytes[7] |= 0x20;
+		break;
+	default:
+		*errcode = NCSI_ERR_PARAM;
+		return NULL;
+	}
+
+	if (ncsi_xmit_cmd(&nca)) {
+		*errcode = NCSI_ERR_INTERNAL;
+		return NULL;
+	}
+
+	*errcode = NCSI_SUCCESS;
+	return NULL;
+}
+
+static struct sk_buff *ncsi_netlink_get_mode_reply(struct nlmsghdr *h,
+						   int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	struct ncsi_msg *src, *dst;
+	size_t size = sizeof(*dst);
+	int index;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+
+	/* Allocate response */
+	if (src->nm_index > NCSI_MODE_MAX) {
+		*errcode = NCSI_ERR_PARAM;
+		return NULL;
+	} else if (src->nm_index == NCSI_MODE_MAX) {
+		size += nla_total_size(sizeof(*ncm)) * NCSI_MODE_MAX;
+	} else {
+		size += nla_total_size(sizeof(*ncm));
+	}
+	skb = nlmsg_new(size, GFP_ATOMIC);
+	if (!skb) {
+		*errcode = NCSI_ERR_NO_MEM;
+		return NULL;
+	}
+
+	/* Fill header */
+	nlh = nlmsg_put(skb, h->nlmsg_pid, h->nlmsg_seq,
+			h->nlmsg_type, sizeof(*dst), 0);
+	dst = nlmsg_data(nlh);
+	memcpy(dst, src, sizeof(*dst));
+	dst->nm_flag &= ~NCSI_FLAG_REQUEST;
+	dst->nm_flag |= NCSI_FLAG_RESPONSE;
+
+	/* Fill one or all modes */
+	for (index = 0; index < NCSI_MODE_MAX; index++) {
+		ncm = &nc->nc_modes[index];
+		if (src->nm_index == NCSI_MODE_MAX ||
+		    src->nm_index == index) {
+			if (nla_put(skb, 0, sizeof(*ncm), ncm)) {
+				*errcode = NCSI_ERR_INTERNAL;
+				nlmsg_free(skb);
+				return NULL;
+			}
+		}
+	}
+
+	nlmsg_end(skb, nlh);
+	return skb;
+}
+
+static struct sk_buff *ncsi_netlink_get_filter_reply(struct nlmsghdr *h,
+						     int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_filter *ncf;
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	struct ncsi_msg *src, *dst;
+	size_t entry_size, size = sizeof(*dst);
+	int index;
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+
+	/* Allocate response */
+	if (src->nm_index > NCSI_FILTER_MAX) {
+		*errcode = NCSI_ERR_PARAM;
+		return NULL;
+	}
+
+	for (index = 0; index < NCSI_FILTER_MAX; index++) {
+		ncf = nc->nc_filters[index];
+		switch (index) {
+		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:
+			continue;
+		}
+
+		if (src->nm_index == NCSI_FILTER_MAX ||
+		    src->nm_index == index) {
+			if (!ncf)
+				continue;
+
+			size += nla_total_size(sizeof(*ncf) +
+					       entry_size * ncf->ncf_total);
+		}
+	}
+
+	skb = nlmsg_new(size, GFP_ATOMIC);
+	if (!skb) {
+		*errcode = NCSI_ERR_NO_MEM;
+		return NULL;
+	}
+
+	/* Fill header */
+	nlh = nlmsg_put(skb, h->nlmsg_pid, h->nlmsg_seq,
+			h->nlmsg_type, sizeof(*dst), 0);
+	dst = nlmsg_data(nlh);
+	memcpy(dst, src, sizeof(*dst));
+	dst->nm_flag &= ~NCSI_FLAG_REQUEST;
+	dst->nm_flag |= NCSI_FLAG_RESPONSE;
+
+	/* Fill one or all filters */
+	for (index = 0; index < NCSI_FILTER_MAX; index++) {
+		ncf = nc->nc_filters[index];
+		switch (index) {
+		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:
+			continue;
+		}
+
+		if (src->nm_index == NCSI_FILTER_MAX ||
+		    src->nm_index == index) {
+			if (!ncf)
+				continue;
+
+			size = sizeof(*ncf) + entry_size * ncf->ncf_total;
+			if (nla_put(skb, 0, size, ncf)) {
+				nlmsg_free(skb);
+				*errcode = NCSI_ERR_INTERNAL;
+				return NULL;
+			}
+		}
+	}
+
+	nlmsg_end(skb, nlh);
+	return skb;
+}
+
+struct sk_buff *ncsi_netlink_get_stats_reply(struct nlmsghdr *h, int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_stats *ncs;
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	struct ncsi_msg *src, *dst;
+	size_t size = sizeof(*dst);
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+
+	/* Allocate response */
+	size += nla_total_size(sizeof(*ncs));
+	skb = nlmsg_new(size, GFP_ATOMIC);
+	if (!skb) {
+		*errcode = NCSI_ERR_NO_MEM;
+		return NULL;
+	}
+
+	/* Fill header */
+	nlh = nlmsg_put(skb, h->nlmsg_pid, h->nlmsg_seq,
+			h->nlmsg_type, sizeof(*dst), 0);
+	dst = nlmsg_data(nlh);
+	memcpy(dst, src, sizeof(*dst));
+	dst->nm_flag &= ~NCSI_FLAG_REQUEST;
+	dst->nm_flag |= NCSI_FLAG_RESPONSE;
+
+	/* Fill one or all filters */
+	ncs = &nc->nc_stats;
+	if (nla_put(skb, 0, sizeof(*ncs), ncs)) {
+		nlmsg_free(skb);
+		*errcode = NCSI_ERR_INTERNAL;
+		return NULL;
+	}
+
+	nlmsg_end(skb, nlh);
+	return skb;
+}
+
+struct sk_buff *ncsi_netlink_set_mode_reply(struct nlmsghdr *h, int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	struct nlattr *nla;
+	struct ncsi_msg *src, *dst;
+	struct ncsi_channel_mode *ncm;
+	size_t size = sizeof(*dst);
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+
+	/* Allocate response */
+	size += nla_total_size(sizeof(*ncm));
+	skb = nlmsg_new(size, GFP_ATOMIC);
+	if (!skb) {
+		*errcode = NCSI_ERR_NO_MEM;
+		return NULL;
+	}
+
+	/* Fill header */
+	nlh = nlmsg_put(skb, h->nlmsg_pid, h->nlmsg_seq,
+			h->nlmsg_type, sizeof(*dst), 0);
+	dst = nlmsg_data(nlh);
+	memcpy(dst, src, sizeof(*dst));
+	dst->nm_flag &= ~NCSI_FLAG_REQUEST;
+	dst->nm_flag |= NCSI_FLAG_RESPONSE;
+
+	/* Fill one or all filters */
+	nla = nlmsg_attrdata(h, sizeof(*dst));
+	ncm = nla_data(nla);
+	if (nla_put(skb, 0, sizeof(*ncm), ncm)) {
+		*errcode = NCSI_ERR_INTERNAL;
+		return NULL;
+	}
+
+	nlmsg_end(skb, nlh);
+	return skb;
+}
+
+struct sk_buff *ncsi_netlink_set_filter_reply(struct nlmsghdr *h, int *errcode)
+{
+	struct net_device *dev;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct sk_buff *skb;
+	struct nlmsghdr *nlh;
+	struct nlattr *nla;
+	struct ncsi_msg *src, *dst;
+	struct ncsi_channel_filter *ncf;
+	size_t extra, size = sizeof(*dst);
+
+	/* Find the NCSI device */
+	src = nlmsg_data(h);
+	dev = dev_get_by_index(ncsi_net, src->nm_ifindex);
+	nd = dev ? ncsi_find_dev(dev) : NULL;
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	np = ndp ? ncsi_find_package(ndp, src->nm_package_id) : NULL;
+	nc = np ? ncsi_find_channel(np, src->nm_channel_id) : NULL;
+	if (!nc) {
+		*errcode = NCSI_ERR_NO_DEV;
+		return NULL;
+	}
+
+	/* Allocate response */
+	size += nla_total_size(sizeof(*ncf) + 16);
+	skb = nlmsg_new(size, GFP_ATOMIC);
+	if (!skb) {
+		*errcode = NCSI_ERR_NO_MEM;
+		return NULL;
+	}
+
+	/* Fill header */
+	nlh = nlmsg_put(skb, h->nlmsg_pid, h->nlmsg_seq,
+			h->nlmsg_type, sizeof(*dst), 0);
+	dst = nlmsg_data(nlh);
+	memcpy(dst, src, sizeof(*dst));
+	dst->nm_flag &= ~NCSI_FLAG_REQUEST;
+	dst->nm_flag |= NCSI_FLAG_RESPONSE;
+
+	/* Fill one or all filters */
+	nla = nlmsg_attrdata(h, sizeof(*dst));
+	ncf = nla_data(nla);
+	switch(src->nm_index) {
+	case NCSI_FILTER_VLAN:
+		extra = 2;
+		break;
+	case NCSI_FILTER_UC:
+	case NCSI_FILTER_MC:
+		extra = 6;
+		break;
+	default:
+		nlmsg_free(skb);
+		*errcode = NCSI_ERR_PARAM;
+		return NULL;
+	}
+
+	if (nla_put(skb, 0, sizeof(*ncf) + extra, ncf)) {
+		*errcode = NCSI_ERR_INTERNAL;
+		return NULL;
+	}
+
+	nlmsg_end(skb, nlh);
+	return skb;
+}
+
+void ncsi_netlink_reply(struct nlmsghdr *h, unsigned int portid, bool timeout)
+{
+	struct sk_buff *skb;
+	int errcode = NCSI_SUCCESS;
+	struct sk_buff *(*func)(struct nlmsghdr *, int *) = NULL;
+
+	if (timeout) {
+		ncsi_netlink_error(h, portid, NCSI_ERR_INTERNAL);
+		return;
+	}
+
+	switch (h->nlmsg_type) {
+	case NCSI_MSG_GET_MODE:
+		func = ncsi_netlink_get_mode_reply;
+		break;
+	case NCSI_MSG_GET_FILTER:
+		func = ncsi_netlink_get_filter_reply;
+		break;
+	case NCSI_MSG_GET_STATS:
+		func = ncsi_netlink_get_stats_reply;
+		break;
+	case NCSI_MSG_SET_MODE:
+		func = ncsi_netlink_set_mode_reply;
+		break;
+	case NCSI_MSG_SET_FILTER:
+		func = ncsi_netlink_set_filter_reply;
+		break;
+	default:
+		errcode = NCSI_ERR_PARAM;
+		goto out;
+	}
+
+	skb = func(h, &errcode);
+	if (!skb)
+		goto out;
+
+	nlmsg_notify(ncsi_sock, skb, portid, 0, 0, GFP_ATOMIC);
+	return;
+out:
+	ncsi_netlink_error(h, portid, errcode);
+}
+
+static int ncsi_netlink_rcv_msg(struct sk_buff *cmd, struct nlmsghdr *h)
+{
+	struct ncsi_msg *nm;
+	struct sk_buff *skb;
+	int errcode = NCSI_SUCCESS;
+	unsigned int portid = NETLINK_CB(cmd).portid;
+	struct sk_buff *(*func)(struct nlmsghdr *, int *) = NULL;
+
+	if (h->nlmsg_type >= NCSI_MSG_MAX ||
+	    nlmsg_len(h) < sizeof(*nm)) {
+		errcode = NCSI_ERR_PARAM;
+		goto out;
+	}
+
+	nm = nlmsg_data(h);
+	if (!(nm->nm_flag & NCSI_FLAG_REQUEST)) {
+		errcode = NCSI_ERR_PARAM;
+		goto out;
+	}
+
+	switch (h->nlmsg_type) {
+	case NCSI_MSG_GET_LAYOUT:
+		func = ncsi_netlink_get_layout;
+		break;
+	case NCSI_MSG_GET_VERSION:
+		func = ncsi_netlink_get_version;
+		break;
+	case NCSI_MSG_GET_CAP:
+		func = ncsi_netlink_get_cap;
+		break;
+	case NCSI_MSG_GET_MODE:
+		func = ncsi_netlink_get_mode;
+		break;
+	case NCSI_MSG_GET_FILTER:
+		func = ncsi_netlink_get_filter;
+		break;
+	case NCSI_MSG_GET_STATS:
+		func = ncsi_netlink_get_stats;
+		break;
+	case NCSI_MSG_SET_MODE:
+		func = ncsi_netlink_set_mode;
+		break;
+	case NCSI_MSG_SET_FILTER:
+		func = ncsi_netlink_set_filter;
+		break;
+	default:
+		goto out;
+	}
+
+	skb = func(h, &errcode);
+	if (errcode != NCSI_SUCCESS)
+		goto out;
+
+	/* Split transactions */
+	if (!skb)
+		return 0;
+
+	return nlmsg_notify(ncsi_sock, skb, portid, 0, 0, GFP_KERNEL);
+out:
+	ncsi_netlink_error(h, portid, errcode);
+	return -EINTR;
+}
+
+static void ncsi_netlink_rcv(struct sk_buff *skb)
+{
+	netlink_rcv_skb(skb, &ncsi_netlink_rcv_msg);
+}
+
+int __net_init ncsi_netlink_init(struct net *net)
+{
+	struct netlink_kernel_cfg cfg = {
+		.groups   = 0,
+		.flags    = NL_CFG_F_NONROOT_RECV |
+			    NL_CFG_F_NONROOT_SEND,
+		.input    = ncsi_netlink_rcv,
+	};
+
+	ncsi_sock = netlink_kernel_create(net, NETLINK_NCSI, &cfg);
+	return ncsi_sock ? 0 : -ENOMEM;
+}
+
+void __net_exit ncsi_netlink_exit(struct net *net)
+{
+	netlink_kernel_release(ncsi_sock);
+	ncsi_sock = NULL;
+}
diff --git a/net/ncsi/ncsi.c b/net/ncsi/ncsi.c
new file mode 100644
index 0000000..c84939e
--- /dev/null
+++ b/net/ncsi/ncsi.c
@@ -0,0 +1,49 @@
+/*
+ * 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"
+
+struct net *ncsi_net = NULL;
+
+static int __net_init ncsi_net_init(struct net *net)
+{
+	ncsi_net = net;
+	return ncsi_netlink_init(net);
+}
+
+static void __net_exit ncsi_net_exit(struct net *net)
+{
+	ncsi_netlink_exit(net);
+	ncsi_net = NULL;
+}
+
+static struct pernet_operations ncsi_net_ops = {
+	.init = ncsi_net_init,
+	.exit = ncsi_net_exit,
+};
+
+static int __init ncsi_init(void)
+{
+	if (ncsi_net)
+		return -EEXIST;
+
+	return register_pernet_subsys(&ncsi_net_ops);
+}
+
+fs_initcall_sync(ncsi_init);
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH RFC v1 5/7] net/faraday: Replace use_nc_si with use_ncsi
  2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
                   ` (3 preceding siblings ...)
  2015-10-06  3:09 ` [PATCH RFC v1 4/7] net/ncsi: Netlink support Gavin Shan
@ 2015-10-06  3:09 ` Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 6/7] net/faraday: Enable NCSI interface Gavin Shan
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Gavin Shan @ 2015-10-06  3:09 UTC (permalink / raw)
  To: netdev; +Cc: benh, davem, Gavin Shan

The patch replaces variable name "use_nc_si" with "use_ncsi". No
function changed.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
---
 drivers/net/ethernet/faraday/ftgmac100.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
index 08e0336..778bee8 100644
--- a/drivers/net/ethernet/faraday/ftgmac100.c
+++ b/drivers/net/ethernet/faraday/ftgmac100.c
@@ -75,7 +75,7 @@ struct ftgmac100 {
 	struct phy_device *phydev;
 	int old_speed;
 
-	bool use_nc_si;
+	bool use_ncsi;
 };
 
 static int ftgmac100_alloc_rx_page(struct ftgmac100 *priv,
@@ -1150,11 +1150,11 @@ static int ftgmac100_open(struct net_device *netdev)
 		goto err_hw;
 
 	ftgmac100_init_hw(priv);
-	ftgmac100_start_hw(priv, priv->use_nc_si ? 100 : 10);
+	ftgmac100_start_hw(priv, priv->use_ncsi ? 100 : 10);
 
 	if (priv->phydev)
 		phy_start(priv->phydev);
-	else if (priv->use_nc_si)
+	else if (priv->use_ncsi)
 		netif_carrier_on(priv->netdev);
 
 	napi_enable(&priv->napi);
@@ -1275,7 +1275,7 @@ err_alloc_mdiobus:
 
 static void ftgmac100_destroy_mdio(struct ftgmac100 *priv)
 {
-	if (!priv->use_nc_si)
+	if (!priv->use_ncsi)
 		return;
 	phy_disconnect(priv->phydev);
 	mdiobus_unregister(priv->mii_bus);
@@ -1359,9 +1359,9 @@ static int ftgmac100_probe(struct platform_device *pdev)
 	/* Check for NC-SI mode */
 	if (pdev->dev.of_node &&
 	    of_get_property(pdev->dev.of_node, "use-nc-si", NULL))
-		priv->use_nc_si = true;
+		priv->use_ncsi = true;
 	else
-		priv->use_nc_si = false;
+		priv->use_ncsi = false;
 
 	/* If we use NC-SI, we need to set that up, which isn't implemented yet
 	 * so we pray things were setup by the bootloader and just mark our link
@@ -1370,7 +1370,7 @@ static int ftgmac100_probe(struct platform_device *pdev)
 	 * Eventually, we'll have a proper NC-SI stack as a helper we can
 	 * instanciate
 	 */
-	if (priv->use_nc_si) {
+	if (priv->use_ncsi) {
 		/* XXX */
 		priv->phydev = NULL;
 		dev_info(&pdev->dev, "Using NC-SI interface\n");
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH RFC v1 6/7] net/faraday: Enable NCSI interface
  2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
                   ` (4 preceding siblings ...)
  2015-10-06  3:09 ` [PATCH RFC v1 5/7] net/faraday: Replace use_nc_si with use_ncsi Gavin Shan
@ 2015-10-06  3:09 ` Gavin Shan
  2015-10-06  3:09 ` [PATCH RFC v1 7/7] net/faraday: Enable offload checksum according to device-tree Gavin Shan
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Gavin Shan @ 2015-10-06  3:09 UTC (permalink / raw)
  To: netdev; +Cc: benh, davem, Gavin Shan

The NIC has the possibility to connect to NCSI package and channel.
This supports NCSI enabled interface. When the network device is
registered, the accompanying NCSI device is registered. When the
interface is to be brought up, the NCSI device is started to probe
NCSI topology and choose one active channel automatically. On the
other handle, when the interface is to be brought down, the interface
will be shuted down when the NCSI device is teared down.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
---
 drivers/net/ethernet/faraday/ftgmac100.c | 91 +++++++++++++++++++++++---------
 1 file changed, 67 insertions(+), 24 deletions(-)

diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
index 778bee8..1b13fd4 100644
--- a/drivers/net/ethernet/faraday/ftgmac100.c
+++ b/drivers/net/ethernet/faraday/ftgmac100.c
@@ -31,6 +31,7 @@
 #include <linux/phy.h>
 #include <linux/platform_device.h>
 #include <net/ip.h>
+#include <net/ncsi.h>
 
 #include "ftgmac100.h"
 
@@ -68,6 +69,7 @@ struct ftgmac100 {
 
 	struct net_device *netdev;
 	struct device *dev;
+	struct ncsi_dev *ndev;
 	struct napi_struct napi;
 
 	struct mii_bus *mii_bus;
@@ -1038,7 +1040,11 @@ static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
 	struct net_device *netdev = dev_id;
 	struct ftgmac100 *priv = netdev_priv(netdev);
 
-	if (likely(netif_running(netdev))) {
+	/* When running in NCSI mode, the interface should be
+	 * ready to receive or transmit NCSI packet before it's
+	 * opened.
+	 */
+	if (likely(priv->use_ncsi || netif_running(netdev))) {
 		/* Disable interrupts for polling */
 		iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
 		napi_schedule(&priv->napi);
@@ -1162,8 +1168,18 @@ static int ftgmac100_open(struct net_device *netdev)
 
 	/* enable all interrupts */
 	iowrite32(INT_MASK_ALL_ENABLED, priv->base + FTGMAC100_OFFSET_IER);
+	/* Start the NCSI device */
+	if (priv->use_ncsi){
+		err = ncsi_start_dev(priv->ndev);
+		if (err)
+			goto err_ncsi;
+	}
 	return 0;
 
+err_ncsi:
+	napi_disable(&priv->napi);
+	netif_stop_queue(netdev);
+	iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
 err_hw:
 	free_irq(priv->irq, netdev);
 err_irq:
@@ -1172,7 +1188,7 @@ err_alloc:
 	return err;
 }
 
-static int ftgmac100_stop(struct net_device *netdev)
+static int ftgmac100_stop_dev(struct net_device *netdev)
 {
 	struct ftgmac100 *priv = netdev_priv(netdev);
 
@@ -1191,6 +1207,18 @@ static int ftgmac100_stop(struct net_device *netdev)
 	return 0;
 }
 
+static int ftgmac100_stop(struct net_device *netdev)
+{
+	struct ftgmac100 *priv = netdev_priv(netdev);
+
+	/* Stop NCSI device */
+	if (priv->use_ncsi) {
+		ncsi_stop_dev(priv->ndev);
+		return 0;
+	}
+
+	return ftgmac100_stop_dev(netdev);
+}
 static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
 				     struct net_device *netdev)
 {
@@ -1291,6 +1319,21 @@ static const struct net_device_ops ftgmac100_netdev_ops = {
 	.ndo_do_ioctl		= ftgmac100_do_ioctl,
 };
 
+static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
+{
+	struct net_device *netdev = nd->nd_dev;
+
+	if (nd->nd_state != ncsi_dev_state_functional)
+		return;
+
+	if (nd->nd_link_up) {
+		pr_info("NCSI dev is up\n");
+		netif_start_queue(netdev);
+	} else {
+		pr_info("NCSI dev is down\n");
+		ftgmac100_stop_dev(netdev);
+	}
+}
 /******************************************************************************
  * struct platform_driver functions
  *****************************************************************************/
@@ -1300,7 +1343,7 @@ static int ftgmac100_probe(struct platform_device *pdev)
 	int irq;
 	struct net_device *netdev;
 	struct ftgmac100 *priv;
-	int err;
+	int err = 0;
 
 	if (!pdev)
 		return -ENODEV;
@@ -1320,7 +1363,17 @@ static int ftgmac100_probe(struct platform_device *pdev)
 		goto err_alloc_etherdev;
 	}
 
+	/* Check for NCSI mode */
+	priv = netdev_priv(netdev);
 	SET_NETDEV_DEV(netdev, &pdev->dev);
+	if (pdev->dev.of_node &&
+	    of_get_property(pdev->dev.of_node, "use-nc-si", NULL)) {
+		dev_info(&pdev->dev, "Using NCSI interface\n");
+		priv->phydev = NULL;
+		priv->use_ncsi = true;
+	} else {
+		priv->use_ncsi = false;
+	}
 
 	netdev->ethtool_ops = &ftgmac100_ethtool_ops;
 	netdev->netdev_ops = &ftgmac100_netdev_ops;
@@ -1329,7 +1382,6 @@ static int ftgmac100_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, netdev);
 
 	/* setup private data */
-	priv = netdev_priv(netdev);
 	priv->netdev = netdev;
 	priv->dev = &pdev->dev;
 
@@ -1356,24 +1408,14 @@ static int ftgmac100_probe(struct platform_device *pdev)
 
 	priv->irq = irq;
 
-	/* Check for NC-SI mode */
-	if (pdev->dev.of_node &&
-	    of_get_property(pdev->dev.of_node, "use-nc-si", NULL))
-		priv->use_ncsi = true;
-	else
-		priv->use_ncsi = false;
+	/* Read MAC address or setup a new one */
+	ftgmac100_setup_mac(priv);
 
-	/* If we use NC-SI, we need to set that up, which isn't implemented yet
-	 * so we pray things were setup by the bootloader and just mark our link
-	 * as up (otherwise we can't get packets through).
-	 *
-	 * Eventually, we'll have a proper NC-SI stack as a helper we can
-	 * instanciate
-	 */
+	/* Register NCSI device */
 	if (priv->use_ncsi) {
-		/* XXX */
-		priv->phydev = NULL;
-		dev_info(&pdev->dev, "Using NC-SI interface\n");
+		priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
+		if (!priv->ndev)
+			goto err_ncsi_dev;
 	} else {
 		err = ftgmac100_setup_mdio(priv);
 
@@ -1384,9 +1426,6 @@ static int ftgmac100_probe(struct platform_device *pdev)
 			dev_warn(&pdev->dev, "Error %d setting up MDIO\n", err);
 	}
 
-	/* Read MAC address or setup a new one */
-	ftgmac100_setup_mac(priv);
-
 	/* Register network device */
 	err = register_netdev(netdev);
 	if (err) {
@@ -1399,7 +1438,11 @@ static int ftgmac100_probe(struct platform_device *pdev)
 	return 0;
 
 err_register_netdev:
-	ftgmac100_destroy_mdio(priv);
+	if (!priv->use_ncsi)
+		ftgmac100_destroy_mdio(priv);
+	else
+		ncsi_unregister_dev(priv->ndev);
+err_ncsi_dev:
 	iounmap(priv->base);
 err_ioremap:
 	release_resource(priv->res);
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH RFC v1 7/7] net/faraday: Enable offload checksum according to device-tree
  2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
                   ` (5 preceding siblings ...)
  2015-10-06  3:09 ` [PATCH RFC v1 6/7] net/faraday: Enable NCSI interface Gavin Shan
@ 2015-10-06  3:09 ` Gavin Shan
  2015-10-06 14:19   ` Sergei Shtylyov
  2015-10-06 23:03 ` [PATCH RFC v1 0/7] NCSI Support Gavin Shan
  2015-11-09  0:12 ` Gavin Shan
  8 siblings, 1 reply; 12+ messages in thread
From: Gavin Shan @ 2015-10-06  3:09 UTC (permalink / raw)
  To: netdev; +Cc: benh, davem, Gavin Shan

This enables IP/UDP/TCP offload checksum according to information
passed on from bootloader through device-tree. The offload doesn't
working properly when the interface works in NCSI mode.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
---
 drivers/net/ethernet/faraday/ftgmac100.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
index 1b13fd4..8caed35 100644
--- a/drivers/net/ethernet/faraday/ftgmac100.c
+++ b/drivers/net/ethernet/faraday/ftgmac100.c
@@ -1377,7 +1377,11 @@ static int ftgmac100_probe(struct platform_device *pdev)
 
 	netdev->ethtool_ops = &ftgmac100_ethtool_ops;
 	netdev->netdev_ops = &ftgmac100_netdev_ops;
-	netdev->features = NETIF_F_IP_CSUM | NETIF_F_GRO;
+	if (pdev->dev.of_node &&
+	    of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))
+		netdev->features = NETIF_F_GRO;
+	else
+		netdev->features = NETIF_F_IP_CSUM | NETIF_F_GRO;
 
 	platform_set_drvdata(pdev, netdev);
 
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH RFC v1 7/7] net/faraday: Enable offload checksum according to device-tree
  2015-10-06  3:09 ` [PATCH RFC v1 7/7] net/faraday: Enable offload checksum according to device-tree Gavin Shan
@ 2015-10-06 14:19   ` Sergei Shtylyov
  2015-10-06 23:19     ` Gavin Shan
  0 siblings, 1 reply; 12+ messages in thread
From: Sergei Shtylyov @ 2015-10-06 14:19 UTC (permalink / raw)
  To: Gavin Shan, netdev; +Cc: benh, davem

Hello.

On 10/6/2015 6:09 AM, Gavin Shan wrote:

> This enables IP/UDP/TCP offload checksum according to information
> passed on from bootloader through device-tree. The offload doesn't
> working properly when the interface works in NCSI mode.
>
> Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
> ---
>   drivers/net/ethernet/faraday/ftgmac100.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
> index 1b13fd4..8caed35 100644
> --- a/drivers/net/ethernet/faraday/ftgmac100.c
> +++ b/drivers/net/ethernet/faraday/ftgmac100.c
> @@ -1377,7 +1377,11 @@ static int ftgmac100_probe(struct platform_device *pdev)
>
>   	netdev->ethtool_ops = &ftgmac100_ethtool_ops;
>   	netdev->netdev_ops = &ftgmac100_netdev_ops;
> -	netdev->features = NETIF_F_IP_CSUM | NETIF_F_GRO;
> +	if (pdev->dev.of_node &&
> +	    of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))

    Why not of_property_read_bool()?

> +		netdev->features = NETIF_F_GRO;
> +	else
> +		netdev->features = NETIF_F_IP_CSUM | NETIF_F_GRO;

    Why not set NETIF_F_GRO outside of *if*?

[...]

MBR, Sergei

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH RFC v1 0/7] NCSI Support
  2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
                   ` (6 preceding siblings ...)
  2015-10-06  3:09 ` [PATCH RFC v1 7/7] net/faraday: Enable offload checksum according to device-tree Gavin Shan
@ 2015-10-06 23:03 ` Gavin Shan
  2015-11-09  0:12 ` Gavin Shan
  8 siblings, 0 replies; 12+ messages in thread
From: Gavin Shan @ 2015-10-06 23:03 UTC (permalink / raw)
  To: Gavin Shan; +Cc: netdev, benh, davem

On Tue, Oct 06, 2015 at 02:09:42PM +1100, Gavin Shan wrote:
>This patchset is prototype requesting for comments.
>
>This series of patches intends to support NCSI stack, which is specified by
>DSP0222. One NCSI enabled interface potentially connects to multiple packages
>and channels, but there is one active channel at once when hardware arbitration
>is disabled. No hardware arbitration isn't supported by this patchset:
>
>   * NIC driver registers NCSI device when the network device is registered. The
>     NCSI device should be started when bringing up the interface so that the
>     active channel can be choosed from the available channels and starts to provide
>     service.
>   * When NCSI AEN packet is received from currently active channel for sake of
>     failure, another active channel is tried to choosed to accomplish the failover.
>   * Reserved netlink interface for NCSI is supported so that userland can retrieve
>     NCSI information or configure from/to NCSI channels.
>   * The first user of NCSI stack is faraday driver (ftgmac100.c).
>

I received the suggestion to add short description about NCSI and put the link to
the spec after the patches were sent. I'm fixing it as below:

http://www.dmtf.org/sites/default/files/standards/documents/DSP0222_1.0.0.pdf

The NCSI spec (DSP0222) defines the functionality and behavior of the Network Controller
Sideband Interface (NCSI) responsible for connecting the network controller on host side
to the network controller on BMC (Base Management Controller) from below aspects:

  * behavior of the interface, which include its operational states as well as the states
    of the associated components 
  * the electrical characteristics of the interface
  * the payloads and commands of the communicati on protocol supported over the interface 

>Gavin Shan (7):
>  net/ncsi: Resource management
>  net/ncsi: Packet handler
>  net/ncsi: Manage NCSI device
>  net/ncsi: Netlink support
>  net/faraday: Replace use_nc_si with use_ncsi
>  net/faraday: Enable NCSI interface
>  net/faraday: Enable offload checksum according to device-tree
>
> drivers/net/ethernet/faraday/ftgmac100.c |  109 ++-
> include/net/ncsi.h                       |   59 ++
> include/uapi/linux/if_ether.h            |    1 +
> include/uapi/linux/ncsi.h                |  200 +++++
> include/uapi/linux/netlink.h             |    1 +
> net/Kconfig                              |    1 +
> net/Makefile                             |    1 +
> net/ncsi/Kconfig                         |   10 +
> net/ncsi/Makefile                        |    5 +
> net/ncsi/internal.h                      |  165 +++++
> net/ncsi/ncsi-aen.c                      |  197 +++++
> net/ncsi/ncsi-cmd.c                      |  380 ++++++++++
> net/ncsi/ncsi-manage.c                   |  914 +++++++++++++++++++++++
> net/ncsi/ncsi-netlink.c                  | 1042 ++++++++++++++++++++++++++
> net/ncsi/ncsi-pkt.h                      |  391 ++++++++++
> net/ncsi/ncsi-rsp.c                      | 1167 ++++++++++++++++++++++++++++++
> net/ncsi/ncsi.c                          |   49 ++
> 17 files changed, 4661 insertions(+), 31 deletions(-)
> create mode 100644 include/net/ncsi.h
> create mode 100644 include/uapi/linux/ncsi.h
> create mode 100644 net/ncsi/Kconfig
> create mode 100644 net/ncsi/Makefile
> create mode 100644 net/ncsi/internal.h
> create mode 100644 net/ncsi/ncsi-aen.c
> create mode 100644 net/ncsi/ncsi-cmd.c
> create mode 100644 net/ncsi/ncsi-manage.c
> create mode 100644 net/ncsi/ncsi-netlink.c
> create mode 100644 net/ncsi/ncsi-pkt.h
> create mode 100644 net/ncsi/ncsi-rsp.c
> create mode 100644 net/ncsi/ncsi.c
>

Thanks,
Gavin

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH RFC v1 7/7] net/faraday: Enable offload checksum according to device-tree
  2015-10-06 14:19   ` Sergei Shtylyov
@ 2015-10-06 23:19     ` Gavin Shan
  0 siblings, 0 replies; 12+ messages in thread
From: Gavin Shan @ 2015-10-06 23:19 UTC (permalink / raw)
  To: Sergei Shtylyov; +Cc: Gavin Shan, netdev, benh, davem

On Tue, Oct 06, 2015 at 05:19:10PM +0300, Sergei Shtylyov wrote:
>Hello.
>
>On 10/6/2015 6:09 AM, Gavin Shan wrote:
>
>>This enables IP/UDP/TCP offload checksum according to information
>>passed on from bootloader through device-tree. The offload doesn't
>>working properly when the interface works in NCSI mode.
>>
>>Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
>>---
>>  drivers/net/ethernet/faraday/ftgmac100.c | 6 +++++-
>>  1 file changed, 5 insertions(+), 1 deletion(-)
>>
>>diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
>>index 1b13fd4..8caed35 100644
>>--- a/drivers/net/ethernet/faraday/ftgmac100.c
>>+++ b/drivers/net/ethernet/faraday/ftgmac100.c
>>@@ -1377,7 +1377,11 @@ static int ftgmac100_probe(struct platform_device *pdev)
>>
>>  	netdev->ethtool_ops = &ftgmac100_ethtool_ops;
>>  	netdev->netdev_ops = &ftgmac100_netdev_ops;
>>-	netdev->features = NETIF_F_IP_CSUM | NETIF_F_GRO;
>>+	if (pdev->dev.of_node &&
>>+	    of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))
>
>   Why not of_property_read_bool()?
>

of_property_read_bool() is more appropriate here though both functions
are wrappers of of_find_property().

>>+		netdev->features = NETIF_F_GRO;
>>+	else
>>+		netdev->features = NETIF_F_IP_CSUM | NETIF_F_GRO;
>
>   Why not set NETIF_F_GRO outside of *if*?
>

Ok.

Thanks,
Gavin

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH RFC v1 0/7] NCSI Support
  2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
                   ` (7 preceding siblings ...)
  2015-10-06 23:03 ` [PATCH RFC v1 0/7] NCSI Support Gavin Shan
@ 2015-11-09  0:12 ` Gavin Shan
  8 siblings, 0 replies; 12+ messages in thread
From: Gavin Shan @ 2015-11-09  0:12 UTC (permalink / raw)
  To: Gavin Shan; +Cc: netdev, benh, davem

On Tue, Oct 06, 2015 at 02:09:42PM +1100, Gavin Shan wrote:

Please ignore this series. A new RFC patchset is going to be sent with more improved
as I was suggested:

  * Explaning what's NCSI and how the code is designed.
  * Dropped netlink interface so that we can focus on NCSI stack at current stage

Thanks,
Gavin

>This patchset is prototype requesting for comments.
>
>This series of patches intends to support NCSI stack, which is specified by
>DSP0222. One NCSI enabled interface potentially connects to multiple packages
>and channels, but there is one active channel at once when hardware arbitration
>is disabled. No hardware arbitration isn't supported by this patchset:
>
>   * NIC driver registers NCSI device when the network device is registered. The
>     NCSI device should be started when bringing up the interface so that the
>     active channel can be choosed from the available channels and starts to provide
>     service.
>   * When NCSI AEN packet is received from currently active channel for sake of
>     failure, another active channel is tried to choosed to accomplish the failover.
>   * Reserved netlink interface for NCSI is supported so that userland can retrieve
>     NCSI information or configure from/to NCSI channels.
>   * The first user of NCSI stack is faraday driver (ftgmac100.c).
>
>Gavin Shan (7):
>  net/ncsi: Resource management
>  net/ncsi: Packet handler
>  net/ncsi: Manage NCSI device
>  net/ncsi: Netlink support
>  net/faraday: Replace use_nc_si with use_ncsi
>  net/faraday: Enable NCSI interface
>  net/faraday: Enable offload checksum according to device-tree
>
> drivers/net/ethernet/faraday/ftgmac100.c |  109 ++-
> include/net/ncsi.h                       |   59 ++
> include/uapi/linux/if_ether.h            |    1 +
> include/uapi/linux/ncsi.h                |  200 +++++
> include/uapi/linux/netlink.h             |    1 +
> net/Kconfig                              |    1 +
> net/Makefile                             |    1 +
> net/ncsi/Kconfig                         |   10 +
> net/ncsi/Makefile                        |    5 +
> net/ncsi/internal.h                      |  165 +++++
> net/ncsi/ncsi-aen.c                      |  197 +++++
> net/ncsi/ncsi-cmd.c                      |  380 ++++++++++
> net/ncsi/ncsi-manage.c                   |  914 +++++++++++++++++++++++
> net/ncsi/ncsi-netlink.c                  | 1042 ++++++++++++++++++++++++++
> net/ncsi/ncsi-pkt.h                      |  391 ++++++++++
> net/ncsi/ncsi-rsp.c                      | 1167 ++++++++++++++++++++++++++++++
> net/ncsi/ncsi.c                          |   49 ++
> 17 files changed, 4661 insertions(+), 31 deletions(-)
> create mode 100644 include/net/ncsi.h
> create mode 100644 include/uapi/linux/ncsi.h
> create mode 100644 net/ncsi/Kconfig
> create mode 100644 net/ncsi/Makefile
> create mode 100644 net/ncsi/internal.h
> create mode 100644 net/ncsi/ncsi-aen.c
> create mode 100644 net/ncsi/ncsi-cmd.c
> create mode 100644 net/ncsi/ncsi-manage.c
> create mode 100644 net/ncsi/ncsi-netlink.c
> create mode 100644 net/ncsi/ncsi-pkt.h
> create mode 100644 net/ncsi/ncsi-rsp.c
> create mode 100644 net/ncsi/ncsi.c
>
>-- 
>2.1.0
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2015-11-09  0:13 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-06  3:09 [PATCH RFC v1 0/7] NCSI Support Gavin Shan
2015-10-06  3:09 ` [PATCH RFC v1 1/7] net/ncsi: Resource management Gavin Shan
2015-10-06  3:09 ` [PATCH RFC v1 2/7] net/ncsi: Packet handler Gavin Shan
2015-10-06  3:09 ` [PATCH RFC v1 3/7] net/ncsi: Manage NCSI device Gavin Shan
2015-10-06  3:09 ` [PATCH RFC v1 4/7] net/ncsi: Netlink support Gavin Shan
2015-10-06  3:09 ` [PATCH RFC v1 5/7] net/faraday: Replace use_nc_si with use_ncsi Gavin Shan
2015-10-06  3:09 ` [PATCH RFC v1 6/7] net/faraday: Enable NCSI interface Gavin Shan
2015-10-06  3:09 ` [PATCH RFC v1 7/7] net/faraday: Enable offload checksum according to device-tree Gavin Shan
2015-10-06 14:19   ` Sergei Shtylyov
2015-10-06 23:19     ` Gavin Shan
2015-10-06 23:03 ` [PATCH RFC v1 0/7] NCSI Support Gavin Shan
2015-11-09  0:12 ` Gavin Shan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.