All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next 00/10] NCSI Support
@ 2016-07-03  5:32 Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 01/10] net/ncsi: Resource management Gavin Shan
                   ` (11 more replies)
  0 siblings, 12 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

This series rebases on David's linux-net git repo ("master" branch). It's
to support NCSI stack on net/farady/ftgmac100.c

The following figure gives an example about how NCSI is deployed: The NCSI is
specified by DSP0222, which can be downloaded from the following link here
(http://www.dmtf.org/sites/default/files/standards/documents/DSP0222_1.0.0.pdf).

   * The NC-SI (aka NCSI) is defined as the interface between a (Base) Management
     Controller (BMC) and one or multiple Network Controlers (NC) on host side.
     The interface is responsible for providing external network connectivity
     for BMC.
   * Each BMC can connect to multiple packages, up to 8. Each package can have
     multiple channels, up to 32. Every package and channel are identified by
     3-bits and 5-bits in NCSI packet. At one moment, one channel is active to
     provide service.
   * NCSI packet, encapsulated in ethernet frame, has 0x88F8 in the protocol
     field. The destination MAC address should be 0xFF's while the source MAC
     address can be arbitrary one.
   * NCSI packets are classified to command, response, AEN (Asynchronous Event
     Notification). Commands are sent from BMC to host for configuration and
     information retrival. Responses, corresponding to commands, are sent from
     host to BMC for confirmation and requested information. One command should
     have one and only one response. AEN is sent from host to BMC for notification
     (e.g. link down on active channel) so that BMC can take appropriate action.

   +------------------+        +----------------------------------------------+
   |                  |        |                     Host                     |
   |        BMC       |        |                                              |
   |                  |        | +-------------------+  +-------------------+ |
   |    +---------+   |        | |     Package-A     |  |     Package-B     | |
   |    |         |   |        | +---------+---------+  +-------------------+ |
   |    |   NIC   |   |        | | Channel | Channel |  | Channel | Channel | |
   +----+----+----+---+        +-+---------+---------+--+---------+---------+-+
             |                             |                      |
             |                             |                      |
             +-----------------------------+----------------------+

The design for the patchset is highlighted as below:

   * The NCSI interface is abstracted with "struct ncsi_dev". It's registered
     when net_device is created, started to work by calling ncsi_start_dev()
     when net_device is opened (ndo_open()). For the first time, NCSI packets
     are sent and received to/from the far end (host in above figure) to probe
     available NCSI packages and channels. After that, one channel is chosen as
     active one to provide service.
   * The NCSI stack is driven by workqueue and state machine internally.
   * AEN (Asychronous Event Notification) might be received from the far end
     (host). The currently active NCSI channel fails over to another available
     one if possible. Otherwise, the NCSI channel is out of service.
   * NCSI stack should be configurable through netlink or another mechanism,
     but it's not implemented in this patchset. It's something TBD.
   * The first NIC driver that is aware of NCSI: drivers/net/ethernet/faraday/ftgmac100.c

Gavin Shan (10):
  net/ncsi: Resource management
  net/ncsi: NCSI command packet handler
  net/ncsi: NCSI response packet handler
  net/ncsi: Package and channel management
  net/ncsi: NCSI AEN packet handler
  net/farady: Helper functions to create or destroy MDIO interface
  net/farady: Read MAC address from chip
  net/farady: Support NCSI mode
  net/farady: Match driver according to compatible property
  net/farady: Mask PHY interrupt with NCSI mode

 drivers/net/ethernet/faraday/ftgmac100.c | 280 +++++++--
 include/net/ncsi.h                       |  52 ++
 include/uapi/linux/if_ether.h            |   1 +
 net/Kconfig                              |   1 +
 net/Makefile                             |   1 +
 net/ncsi/Kconfig                         |  12 +
 net/ncsi/Makefile                        |   4 +
 net/ncsi/internal.h                      | 310 ++++++++++
 net/ncsi/ncsi-aen.c                      | 169 ++++++
 net/ncsi/ncsi-cmd.c                      | 396 ++++++++++++
 net/ncsi/ncsi-manage.c                   | 893 +++++++++++++++++++++++++++
 net/ncsi/ncsi-pkt.h                      | 395 ++++++++++++
 net/ncsi/ncsi-rsp.c                      | 997 +++++++++++++++++++++++++++++++
 13 files changed, 3448 insertions(+), 63 deletions(-)
 create mode 100644 include/net/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-pkt.h
 create mode 100644 net/ncsi/ncsi-rsp.c

-- 
2.1.0

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

* [PATCH net-next 01/10] net/ncsi: Resource management
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 02/10] net/ncsi: NCSI command packet handler Gavin Shan
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

NCSI spec (DSP0222) defines several objects: package, channel, mode,
filter, version and statistics etc. This introduces the data structs
to represent those objects and implement functions to manage them.
Also, this introduces CONFIG_NET_NCSI for the newly implemented NCSI
stack.

   * The user (e.g. netdev driver) dereference NCSI device by
     "struct ncsi_dev", which is embedded to "struct ncsi_dev_priv".
     The later one is used by NCSI stack internally.
   * Every NCSI device can have multiple packages simultaneously, up
     to 8 packages. It's represented by "struct ncsi_package" and
     identified by 3-bits ID.
   * Every NCSI package can have multiple channels, up to 32. It's
     represented by "struct ncsi_channel" and identified by 5-bits ID.
   * Every NCSI channel has version, statistics, various modes and
     filters. They are represented by "struct ncsi_channel_version",
     "struct ncsi_channel_stats", "struct ncsi_channel_mode" and
     "struct ncsi_channel_filter" separately.
   * Apart from AEN (Asynchronous Event Notification), the NCSI stack
     works in terms of command and response. This introduces "struct
     ncsi_req" to represent a complete NCSI transaction made of NCSI
     request and response.

link: http://www.dmtf.org/sites/default/files/standards/documents/DSP0222_1.0.0.pdf
Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 include/net/ncsi.h     |  46 ++++++
 net/Kconfig            |   1 +
 net/Makefile           |   1 +
 net/ncsi/Kconfig       |  12 ++
 net/ncsi/Makefile      |   4 +
 net/ncsi/internal.h    | 255 ++++++++++++++++++++++++++++++
 net/ncsi/ncsi-manage.c | 422 +++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 741 insertions(+)
 create mode 100644 include/net/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..70d14ee
--- /dev/null
+++ b/include/net/ncsi.h
@@ -0,0 +1,46 @@
+#ifndef __NET_NCSI_H
+#define __NET_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,
+	ncsi_dev_state_probe		= 0x0200,
+	ncsi_dev_state_config		= 0x0300,
+	ncsi_dev_state_suspend		= 0x0400,
+};
+
+struct ncsi_dev {
+	int               state;
+	int		  link_up;
+	struct net_device *dev;
+	void		  (*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));
+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 void ncsi_unregister_dev(struct ncsi_dev *nd)
+{
+}
+#endif /* CONFIG_NET_NCSI */
+
+#endif /* __NET_NCSI_H */
diff --git a/net/Kconfig b/net/Kconfig
index ff40562..c2cdbce 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -237,6 +237,7 @@ source "net/hsr/Kconfig"
 source "net/switchdev/Kconfig"
 source "net/l3mdev/Kconfig"
 source "net/qrtr/Kconfig"
+source "net/ncsi/Kconfig"
 
 config RPS
 	bool
diff --git a/net/Makefile b/net/Makefile
index bdd1455..9bd20bb 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -79,3 +79,4 @@ ifneq ($(CONFIG_NET_L3_MASTER_DEV),)
 obj-y				+= l3mdev/
 endif
 obj-$(CONFIG_QRTR)		+= qrtr/
+obj-$(CONFIG_NET_NCSI)		+= ncsi/
diff --git a/net/ncsi/Kconfig b/net/ncsi/Kconfig
new file mode 100644
index 0000000..08a8a60
--- /dev/null
+++ b/net/ncsi/Kconfig
@@ -0,0 +1,12 @@
+#
+# Configuration for NCSI support
+#
+
+config NET_NCSI
+	bool "NCSI interface support"
+	depends on INET
+	---help---
+	  This module provides NCSI (Network Controller Sideband Interface)
+	  support. Enable this only if your system connects to a network
+	  device via NCSI and the ethernet driver you're using supports
+	  the protocol explicitly.
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..6875b58
--- /dev/null
+++ b/net/ncsi/internal.h
@@ -0,0 +1,255 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2016.
+ *
+ * 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.
+ */
+
+#ifndef __NCSI_INTERNAL_H__
+#define __NCSI_INTERNAL_H__
+
+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
+};
+
+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
+};
+
+enum {
+	NCSI_FILTER_BASE	= 0,
+	NCSI_FILTER_VLAN	= 0,
+	NCSI_FILTER_UC,
+	NCSI_FILTER_MC,
+	NCSI_FILTER_MIXED,
+	NCSI_FILTER_MAX
+};
+
+struct ncsi_channel_version {
+	u32 version;		/* Supported BCD encoded NCSI version */
+	u32 alpha2;		/* Supported BCD encoded NCSI version */
+	u8  fw_name[12];	/* Firware name string                */
+	u32 fw_version;		/* Firmware version                   */
+	u16 pci_ids[4];		/* PCI identification                 */
+	u32 mf_id;		/* Manufacture ID                     */
+};
+
+struct ncsi_channel_cap {
+	u32 index;	/* Index of channel capabilities */
+	u32 cap;	/* NCSI channel capability       */
+};
+
+struct ncsi_channel_mode {
+	u32 index;	/* Index of channel modes      */
+	u32 enable;	/* Enabled or disabled         */
+	u32 size;	/* Valid entries in ncm_data[] */
+	u32 data[8];	/* Data entries                */
+};
+
+struct ncsi_channel_filter {
+	u32 index;	/* Index of channel filters          */
+	u32 total;	/* Total entries in the filter table */
+	u64 bitmap;	/* Bitmap of valid entries           */
+	u32 data[];	/* Data for the valid entries        */
+};
+
+struct ncsi_channel_stats {
+	u32 hnc_cnt_hi;		/* Counter cleared            */
+	u32 hnc_cnt_lo;		/* Counter cleared            */
+	u32 hnc_rx_bytes;	/* Rx bytes                   */
+	u32 hnc_tx_bytes;	/* Tx bytes                   */
+	u32 hnc_rx_uc_pkts;	/* Rx UC packets              */
+	u32 hnc_rx_mc_pkts;     /* Rx MC packets              */
+	u32 hnc_rx_bc_pkts;	/* Rx BC packets              */
+	u32 hnc_tx_uc_pkts;	/* Tx UC packets              */
+	u32 hnc_tx_mc_pkts;	/* Tx MC packets              */
+	u32 hnc_tx_bc_pkts;	/* Tx BC packets              */
+	u32 hnc_fcs_err;	/* FCS errors                 */
+	u32 hnc_align_err;	/* Alignment errors           */
+	u32 hnc_false_carrier;	/* False carrier detection    */
+	u32 hnc_runt_pkts;	/* Rx runt packets            */
+	u32 hnc_jabber_pkts;	/* Rx jabber packets          */
+	u32 hnc_rx_pause_xon;	/* Rx pause XON frames        */
+	u32 hnc_rx_pause_xoff;	/* Rx XOFF frames             */
+	u32 hnc_tx_pause_xon;	/* Tx XON frames              */
+	u32 hnc_tx_pause_xoff;	/* Tx XOFF frames             */
+	u32 hnc_tx_s_collision;	/* Single collision frames    */
+	u32 hnc_tx_m_collision;	/* Multiple collision frames  */
+	u32 hnc_l_collision;	/* Late collision frames      */
+	u32 hnc_e_collision;	/* Excessive collision frames */
+	u32 hnc_rx_ctl_frames;	/* Rx control frames          */
+	u32 hnc_rx_64_frames;	/* Rx 64-bytes frames         */
+	u32 hnc_rx_127_frames;	/* Rx 65-127 bytes frames     */
+	u32 hnc_rx_255_frames;	/* Rx 128-255 bytes frames    */
+	u32 hnc_rx_511_frames;	/* Rx 256-511 bytes frames    */
+	u32 hnc_rx_1023_frames;	/* Rx 512-1023 bytes frames   */
+	u32 hnc_rx_1522_frames;	/* Rx 1024-1522 bytes frames  */
+	u32 hnc_rx_9022_frames;	/* Rx 1523-9022 bytes frames  */
+	u32 hnc_tx_64_frames;	/* Tx 64-bytes frames         */
+	u32 hnc_tx_127_frames;	/* Tx 65-127 bytes frames     */
+	u32 hnc_tx_255_frames;	/* Tx 128-255 bytes frames    */
+	u32 hnc_tx_511_frames;	/* Tx 256-511 bytes frames    */
+	u32 hnc_tx_1023_frames;	/* Tx 512-1023 bytes frames   */
+	u32 hnc_tx_1522_frames;	/* Tx 1024-1522 bytes frames  */
+	u32 hnc_tx_9022_frames;	/* Tx 1523-9022 bytes frames  */
+	u32 hnc_rx_valid_bytes;	/* Rx valid bytes             */
+	u32 hnc_rx_runt_pkts;	/* Rx error runt packets      */
+	u32 hnc_rx_jabber_pkts;	/* Rx error jabber packets    */
+	u32 ncsi_rx_cmds;	/* Rx NCSI commands           */
+	u32 ncsi_dropped_cmds;	/* Dropped commands           */
+	u32 ncsi_cmd_type_errs;	/* Command type errors        */
+	u32 ncsi_cmd_csum_errs;	/* Command checksum errors    */
+	u32 ncsi_rx_pkts;	/* Rx NCSI packets            */
+	u32 ncsi_tx_pkts;	/* Tx NCSI packets            */
+	u32 ncsi_tx_aen_pkts;	/* Tx AEN packets             */
+	u32 pt_tx_pkts;		/* Tx packets                 */
+	u32 pt_tx_dropped;	/* Tx dropped packets         */
+	u32 pt_tx_channel_err;	/* Tx channel errors          */
+	u32 pt_tx_us_err;	/* Tx undersize errors        */
+	u32 pt_rx_pkts;		/* Rx packets                 */
+	u32 pt_rx_dropped;	/* Rx dropped packets         */
+	u32 pt_rx_channel_err;	/* Rx channel errors          */
+	u32 pt_rx_us_err;	/* Rx undersize errors        */
+	u32 pt_rx_os_err;	/* Rx oversize errors         */
+};
+
+struct ncsi_dev_priv;
+struct ncsi_package;
+
+#define NCSI_PACKAGE_SHIFT	5
+#define NCSI_PACKAGE_INDEX(c)	(((c) >> NCSI_PACKAGE_SHIFT) & 0x7)
+#define NCSI_CHANNEL_INDEX(c)	((c) & ((1 << NCSI_PACKAGE_SHIFT) - 1))
+#define NCSI_TO_CHANNEL(p, c)	(((p) << NCSI_PACKAGE_SHIFT) | (c))
+
+/* 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               id;
+	int                         state;
+	spinlock_t                  lock;	/* Protect filters etc */
+	struct ncsi_package         *package;
+	struct ncsi_channel_version version;
+	struct ncsi_channel_cap	    caps[NCSI_CAP_MAX];
+	struct ncsi_channel_mode    modes[NCSI_MODE_MAX];
+	struct ncsi_channel_filter  *filters[NCSI_FILTER_MAX];
+	struct ncsi_channel_stats   stats;
+	struct list_head            node;
+};
+
+struct ncsi_package {
+	unsigned char        id;		/* NCSI package ID          */
+	struct ncsi_dev_priv *ndp;		/* NCSI device              */
+	atomic_t             channel_num;	/* Number of channels       */
+	spinlock_t           channel_lock;	/* Protect list of channels */
+	struct list_head     channels;		/* List of chanels          */
+	struct list_head     node;
+};
+
+struct ncsi_req {
+	unsigned char        id;		/* Request ID          */
+	bool                 used;		/* Request used or not */
+	struct ncsi_dev_priv *ndp;		/* NCSI device         */
+	struct sk_buff       *cmd;		/* Command packet      */
+	struct sk_buff       *rsp;		/* Response packet     */
+	struct timer_list    timer;		/* Associated timer    */
+	bool                 timer_enabled;	/* Timer was started   */
+};
+
+struct ncsi_dev_priv {
+	struct ncsi_dev     ndev;		/* NCSI device              */
+	int                 flags;		/* NCSI device flags        */
+	struct ncsi_package *active_package;	/* Active NCSI package      */
+	struct ncsi_channel *active_channel;	/* Active NCSI channel      */
+	atomic_t            package_num;	/* Number of packages       */
+	spinlock_t          package_lock;	/* Protect list of packages */
+	struct list_head    packages;		/* List of packages         */
+	spinlock_t          req_lock;		/* Protect request table    */
+	struct ncsi_req     reqs[256];		/* Request table            */
+	int                 last_req_idx;	/* Last used request ID     */
+	struct list_head    node;
+};
+
+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, ndev)
+#define NCSI_FOR_EACH_DEV(ndp) \
+	list_for_each_entry_rcu(ndp, &ncsi_dev_list, node)
+#define NCSI_FOR_EACH_PACKAGE(ndp, np) \
+	list_for_each_entry_rcu(np, &ndp->packages, node)
+#define NCSI_FOR_EACH_CHANNEL(np, nc) \
+	list_for_each_entry_rcu(nc, &np->channels, node)
+
+/* Resources */
+int ncsi_find_filter(struct ncsi_channel *nc, int table, void *data);
+int ncsi_add_filter(struct ncsi_channel *nc, int table, void *data);
+int ncsi_remove_filter(struct ncsi_channel *nc, int table, int index);
+struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
+				       unsigned char id);
+struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np,
+				      unsigned char id);
+struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
+				       unsigned char id);
+struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
+				      unsigned char id);
+void ncsi_remove_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..b36e93d
--- /dev/null
+++ b/net/ncsi/ncsi-manage.c
@@ -0,0 +1,422 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2016.
+ *
+ * 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);
+
+static inline int ncsi_filter_size(int table)
+{
+	int sizes[] = { 2, 6, 6, 6 };
+
+	BUILD_BUG_ON(ARRAY_SIZE(sizes) != NCSI_FILTER_MAX);
+	if (table < NCSI_FILTER_BASE || table >= NCSI_FILTER_MAX)
+		return -EINVAL;
+
+	return sizes[table];
+}
+
+int ncsi_find_filter(struct ncsi_channel *nc, int table, void *data)
+{
+	struct ncsi_channel_filter *ncf;
+	void *bitmap;
+	int index, size;
+
+	ncf = nc->filters[table];
+	if (!ncf)
+		return -ENXIO;
+
+	size = ncsi_filter_size(table);
+	if (size < 0)
+		return size;
+
+	spin_lock(&nc->lock);
+	bitmap = (void *)&ncf->bitmap;
+	index = -1;
+	while ((index = find_next_bit(bitmap, ncf->total, index + 1))
+	       < ncf->total) {
+		if (!memcmp(ncf->data + size * index, data, size)) {
+			spin_unlock(&nc->lock);
+			return index;
+		}
+	}
+
+	spin_unlock(&nc->lock);
+	return -ENOENT;
+}
+
+int ncsi_add_filter(struct ncsi_channel *nc, int table, void *data)
+{
+	struct ncsi_channel_filter *ncf;
+	int index, size;
+	void *bitmap;
+
+	size = ncsi_filter_size(table);
+	if (size < 0)
+		return size;
+
+	index = ncsi_find_filter(nc, table, data);
+	if (index >= 0)
+		return index;
+
+	ncf = nc->filters[table];
+	if (!ncf)
+		return -ENODEV;
+
+	spin_lock(&nc->lock);
+	bitmap = (void *)&ncf->bitmap;
+	do {
+		index = find_next_zero_bit(bitmap, ncf->total, 0);
+		if (index >= ncf->total) {
+			spin_unlock(&nc->lock);
+			return -ENOSPC;
+		}
+	} while (test_and_set_bit(index, bitmap));
+
+	memcpy(ncf->data + size * index, data, size);
+	spin_unlock(&nc->lock);
+
+	return index;
+}
+
+int ncsi_remove_filter(struct ncsi_channel *nc, int table, int index)
+{
+	struct ncsi_channel_filter *ncf;
+	int size;
+	void *bitmap;
+
+	size = ncsi_filter_size(table);
+	if (size < 0)
+		return size;
+
+	ncf = nc->filters[table];
+	if (!ncf || index >= ncf->total)
+		return -ENODEV;
+
+	spin_lock(&nc->lock);
+	bitmap = (void *)&ncf->bitmap;
+	if (test_and_clear_bit(index, bitmap))
+		memset(ncf->data + size * index, 0, size);
+	spin_unlock(&nc->lock);
+
+	return 0;
+}
+
+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->id == id)
+			return nc;
+	}
+
+	return NULL;
+}
+
+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)
+		return NULL;
+
+	nc->id = id;
+	nc->package = np;
+	spin_lock_init(&nc->lock);
+	for (index = 0; index < NCSI_CAP_MAX; index++)
+		nc->caps[index].index = index;
+	for (index = 0; index < NCSI_MODE_MAX; index++)
+		nc->modes[index].index = index;
+
+	spin_lock(&np->channel_lock);
+	tmp = ncsi_find_channel(np, id);
+	if (tmp) {
+		spin_unlock(&np->channel_lock);
+		kfree(nc);
+		return tmp;
+	}
+	list_add_tail_rcu(&nc->node, &np->channels);
+	spin_unlock(&np->channel_lock);
+
+	atomic_inc(&np->channel_num);
+	return nc;
+}
+
+static void ncsi_remove_channel(struct ncsi_channel *nc)
+{
+	struct ncsi_dev_priv *ndp = nc->package->ndp;
+	struct ncsi_channel_filter *ncf;
+	int i;
+
+	/* Release filters */
+	for (i = 0; i < NCSI_FILTER_MAX; i++) {
+		ncf = nc->filters[i];
+		if (!ncf)
+			continue;
+
+		nc->filters[i] = NULL;
+		kfree(ncf);
+	}
+
+	/* Update active channel if necessary */
+	if (ndp->active_channel == nc) {
+		ndp->active_package = NULL;
+		ndp->active_channel = NULL;
+	}
+
+	/* Remove and free channel */
+	list_del_rcu(&nc->node);
+	kfree(nc);
+}
+
+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->id == id)
+			return np;
+	}
+
+	return NULL;
+}
+
+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)
+		return NULL;
+
+	np->id = id;
+	np->ndp = ndp;
+	spin_lock_init(&np->channel_lock);
+	INIT_LIST_HEAD(&np->channels);
+
+	spin_lock(&ndp->package_lock);
+	tmp = ncsi_find_package(ndp, id);
+	if (tmp) {
+		spin_unlock(&ndp->package_lock);
+		kfree(np);
+		return tmp;
+	}
+
+	list_add_tail_rcu(&np->node, &ndp->packages);
+	spin_unlock(&ndp->package_lock);
+
+	atomic_inc(&ndp->package_num);
+	return np;
+}
+
+void ncsi_remove_package(struct ncsi_package *np)
+{
+	struct ncsi_dev_priv *ndp = np->ndp;
+	struct ncsi_channel *nc, *tmp;
+
+	/* Release all child channels */
+	spin_lock(&np->channel_lock);
+	list_for_each_entry_safe(nc, tmp, &np->channels, node)
+		ncsi_remove_channel(nc);
+	spin_unlock(&np->channel_lock);
+
+	/* Clear active package if necessary */
+	if (ndp->active_package == np) {
+		ndp->active_package = NULL;
+		ndp->active_channel = NULL;
+	}
+
+	/* Remove and free package */
+	list_del_rcu(&np->node);
+	kfree(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_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 consecutive 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 fashion.
+ */
+struct ncsi_req *ncsi_alloc_req(struct ncsi_dev_priv *ndp)
+{
+	struct ncsi_req *nr = NULL;
+	int idx, limit = ARRAY_SIZE(ndp->reqs);
+	unsigned long flags;
+
+	spin_lock_irqsave(&ndp->req_lock, flags);
+
+	/* Check if there is one available request until the ceiling */
+	for (idx = ndp->last_req_idx; !nr && idx < limit; idx++) {
+		if (ndp->reqs[idx].used)
+			continue;
+
+		ndp->reqs[idx].used = true;
+		nr = &ndp->reqs[idx];
+		if (++ndp->last_req_idx >= limit)
+			ndp->last_req_idx = 0;
+	}
+
+	/* Fail back to check from the starting cursor */
+	for (idx = 0; !nr && idx < ndp->last_req_idx; idx++) {
+		if (ndp->reqs[idx].used)
+			continue;
+
+		ndp->reqs[idx].used = true;
+		nr = &ndp->reqs[idx];
+		if (++ndp->last_req_idx >= limit)
+			ndp->last_req_idx = 0;
+	}
+
+	spin_unlock_irqrestore(&ndp->req_lock, flags);
+	return nr;
+}
+
+void ncsi_free_req(struct ncsi_req *nr, bool check, bool timeout)
+{
+	struct ncsi_dev_priv *ndp = nr->ndp;
+	struct sk_buff *cmd, *rsp;
+	unsigned long flags;
+
+	if (nr->timer_enabled) {
+		nr->timer_enabled = false;
+		del_timer_sync(&nr->timer);
+	}
+
+	spin_lock_irqsave(&ndp->req_lock, flags);
+	cmd = nr->cmd;
+	rsp = nr->rsp;
+	nr->cmd = NULL;
+	nr->rsp = NULL;
+	nr->used = false;
+	spin_unlock_irqrestore(&ndp->req_lock, flags);
+
+	/* 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->ndev.dev == dev)
+			return &ndp->ndev;
+	}
+
+	return NULL;
+}
+
+static void ncsi_req_timeout(unsigned long data)
+{
+	struct ncsi_req *nr = (struct ncsi_req *)data;
+	struct ncsi_dev_priv *ndp = nr->ndp;
+	unsigned long flags;
+
+	/* If the request already had associated response,
+	 * let the response handler to release it.
+	 */
+	spin_lock_irqsave(&ndp->req_lock, flags);
+	nr->timer_enabled = false;
+	if (nr->rsp || !nr->cmd) {
+		spin_unlock_irqrestore(&ndp->req_lock, flags);
+		return;
+	}
+	spin_unlock_irqrestore(&ndp->req_lock, flags);
+
+	/* 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)
+		return NULL;
+
+	nd = &ndp->ndev;
+	nd->state = ncsi_dev_state_registered;
+	nd->dev = dev;
+	nd->handler = handler;
+
+	/* Initialize private NCSI device */
+	spin_lock_init(&ndp->package_lock);
+	INIT_LIST_HEAD(&ndp->packages);
+	spin_lock_init(&ndp->req_lock);
+	ndp->last_req_idx = 0;
+	for (idx = 0; idx < ARRAY_SIZE(ndp->reqs); idx++) {
+		ndp->reqs[idx].id = idx;
+		ndp->reqs[idx].ndp = ndp;
+		setup_timer(&ndp->reqs[idx].timer, ncsi_req_timeout,
+			    (unsigned long)&ndp->reqs[idx]);
+	}
+
+	spin_lock(&ncsi_dev_lock);
+	list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
+	spin_unlock(&ncsi_dev_lock);
+
+	return nd;
+}
+EXPORT_SYMBOL_GPL(ncsi_register_dev);
+
+void ncsi_unregister_dev(struct ncsi_dev *nd)
+{
+	struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
+	struct ncsi_package *np, *tmp;
+
+	spin_lock(&ndp->package_lock);
+	list_for_each_entry_safe(np, tmp, &ndp->packages, node)
+		ncsi_remove_package(np);
+	spin_unlock(&ndp->package_lock);
+}
+EXPORT_SYMBOL_GPL(ncsi_unregister_dev);
-- 
2.1.0

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

* [PATCH net-next 02/10] net/ncsi: NCSI command packet handler
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 01/10] net/ncsi: Resource management Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 03/10] net/ncsi: NCSI response " Gavin Shan
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

The NCSI command packets are sent from MC (Management Controller)
to remote end. They are used for multiple purposes: probe existing
NCSI package/channel, retrieve NCSI channel's capability, configure
NCSI channel etc.

This defines struct to represent NCSI command packets and introduces
function ncsi_xmit_cmd(), which will be used to transmit NCSI command
packet according to the request. The request is represented by struct
ncsi_cmd_arg.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 include/uapi/linux/if_ether.h |   1 +
 net/ncsi/Makefile             |   2 +-
 net/ncsi/internal.h           |  18 ++
 net/ncsi/ncsi-cmd.c           | 396 ++++++++++++++++++++++++++++++++++++++++++
 net/ncsi/ncsi-pkt.h           | 168 ++++++++++++++++++
 5 files changed, 584 insertions(+), 1 deletion(-)
 create mode 100644 net/ncsi/ncsi-cmd.c
 create mode 100644 net/ncsi/ncsi-pkt.h

diff --git a/include/uapi/linux/if_ether.h b/include/uapi/linux/if_ether.h
index cec849a..7300b2d 100644
--- a/include/uapi/linux/if_ether.h
+++ b/include/uapi/linux/if_ether.h
@@ -87,6 +87,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..abc4046 100644
--- a/net/ncsi/Makefile
+++ b/net/ncsi/Makefile
@@ -1,4 +1,4 @@
 #
 # Makefile for NCSI API
 #
-obj-$(CONFIG_NET_NCSI) += ncsi-manage.o
+obj-$(CONFIG_NET_NCSI) += ncsi-cmd.o ncsi-manage.o
diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index 6875b58..842a687 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -219,6 +219,20 @@ struct ncsi_dev_priv {
 	struct list_head    node;
 };
 
+struct ncsi_cmd_arg {
+	struct ncsi_dev_priv   *ndp;
+	unsigned char          type;
+	unsigned char          id;
+	unsigned char          package;
+	unsigned char          channel;
+	unsigned short         payload;
+	union {
+		unsigned char  bytes[16];
+		unsigned short words[8];
+		unsigned int   dwords[4];
+	};
+};
+
 extern struct list_head ncsi_dev_list;
 extern spinlock_t ncsi_dev_lock;
 
@@ -252,4 +266,8 @@ 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 */
+u32 ncsi_calculate_checksum(unsigned char *data, int len);
+int ncsi_xmit_cmd(struct ncsi_cmd_arg *nca);
+
 #endif /* __NCSI_INTERNAL_H__ */
diff --git a/net/ncsi/ncsi-cmd.c b/net/ncsi/ncsi-cmd.c
new file mode 100644
index 0000000..234f3e2
--- /dev/null
+++ b/net/ncsi/ncsi-cmd.c
@@ -0,0 +1,396 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2016.
+ *
+ * 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/etherdevice.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"
+
+u32 ncsi_calculate_checksum(unsigned char *data, int len)
+{
+	u32 checksum = 0;
+	int i;
+
+	for (i = 0; i < len; i += 2)
+		checksum += (((u32)data[i] << 8) | data[i + 1]);
+
+	checksum = (~checksum + 1);
+	return checksum;
+}
+
+/* This function should be called after the data area has been
+ * populated completely.
+ */
+static void ncsi_cmd_build_header(struct ncsi_pkt_hdr *h,
+				  struct ncsi_cmd_arg *nca)
+{
+	u32 checksum;
+	__be32 *pchecksum;
+
+	h->mc_id        = 0;
+	h->revision     = NCSI_PKT_REVISION;
+	h->reserved     = 0;
+	h->id           = nca->id;
+	h->type         = nca->type;
+	h->channel      = NCSI_TO_CHANNEL(nca->package,
+					  nca->channel);
+	h->length       = htons(nca->payload);
+	h->reserved1[0] = 0;
+	h->reserved1[1] = 0;
+
+	/* Fill with calculated checksum */
+	checksum = ncsi_calculate_checksum((unsigned char *)h,
+					   sizeof(*h) + nca->payload);
+	pchecksum = (__be32 *)((void *)h + sizeof(struct ncsi_pkt_hdr) +
+		    nca->payload);
+	*pchecksum = htonl(checksum);
+}
+
+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));
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->bytes[0];
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->bytes[0];
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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));
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->bytes[0];
+	cmd->mode = htonl(nca->dwords[1]);
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->dwords[0]);
+	cmd->oem_mode = htonl(nca->dwords[1]);
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->words[0]);
+	cmd->index = nca->bytes[2];
+	cmd->enable = nca->bytes[3];
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->bytes[0];
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->bytes[i];
+	cmd->index = nca->bytes[6];
+	cmd->at_e = nca->bytes[7];
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->dwords[0]);
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->dwords[0]);
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+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->bytes[0];
+	ncsi_cmd_build_header(&cmd->cmd.common, nca);
+
+	return 0;
+}
+
+static struct ncsi_cmd_handler {
+	unsigned char type;
+	int           payload;
+	int           (*handler)(struct sk_buff *skb,
+				 struct ncsi_cmd_arg *nca);
+} ncsi_cmd_handlers[] = {
+	{ NCSI_PKT_CMD_CIS,   0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_SP,    4, ncsi_cmd_handler_sp      },
+	{ NCSI_PKT_CMD_DP,    0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_EC,    0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_DC,    4, ncsi_cmd_handler_dc      },
+	{ NCSI_PKT_CMD_RC,    4, ncsi_cmd_handler_rc      },
+	{ NCSI_PKT_CMD_ECNT,  0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_DCNT,  0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_AE,    8, ncsi_cmd_handler_ae      },
+	{ NCSI_PKT_CMD_SL,    8, ncsi_cmd_handler_sl      },
+	{ NCSI_PKT_CMD_GLS,   0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_SVF,   4, ncsi_cmd_handler_svf     },
+	{ NCSI_PKT_CMD_EV,    4, ncsi_cmd_handler_ev      },
+	{ NCSI_PKT_CMD_DV,    0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_SMA,   8, ncsi_cmd_handler_sma     },
+	{ NCSI_PKT_CMD_EBF,   4, ncsi_cmd_handler_ebf     },
+	{ NCSI_PKT_CMD_DBF,   0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_EGMF,  4, ncsi_cmd_handler_egmf    },
+	{ NCSI_PKT_CMD_DGMF,  0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_SNFC,  4, ncsi_cmd_handler_snfc    },
+	{ NCSI_PKT_CMD_GVI,   0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_GC,    0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_GP,    0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_GCPS,  0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_GNS,   0, ncsi_cmd_handler_default },
+	{ NCSI_PKT_CMD_GNPTS, 0, ncsi_cmd_handler_default },
+	{ 0,                  0, NULL                     }
+};
+
+static struct ncsi_req *ncsi_alloc_cmd_req(struct ncsi_cmd_arg *nca)
+{
+	struct ncsi_dev_priv *ndp = nca->ndp;
+	struct ncsi_dev *nd = &ndp->ndev;
+	struct net_device *dev = 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.
+	 * The packet needs padding if its payload is less than 26 bytes to
+	 * meet 64 bytes minimal ethernet frame length.
+	 */
+	len += sizeof(struct ncsi_cmd_pkt_hdr) + 4;
+	if (nca->payload < 26)
+		len += 26;
+	else
+		len += nca->payload;
+
+	/* Allocate skb */
+	skb = alloc_skb(len, GFP_ATOMIC);
+	if (!skb) {
+		ncsi_free_req(nr, false, false);
+		return NULL;
+	}
+
+	nr->cmd = skb;
+	skb_reserve(skb, hlen);
+	skb_reset_network_header(skb);
+
+	skb->dev = dev;
+	skb->protocol = htons(ETH_P_NCSI);
+
+	return nr;
+}
+
+int ncsi_xmit_cmd(struct ncsi_cmd_arg *nca)
+{
+	struct ncsi_req *nr;
+	struct ethhdr *eh;
+	struct ncsi_cmd_handler *nch;
+	int ret;
+
+	/* Search for the handler */
+	nch = ncsi_cmd_handlers;
+	while (nch->handler) {
+		if (nch->type == nca->type)
+			break;
+		nch++;
+	}
+
+	if (!nch->handler) {
+		pr_info("%s: Cannot send packet with type 0x%x\n",
+			__func__, nca->type);
+		return -ENOENT;
+	}
+
+	/* Get packet payload length and allocate the request */
+	nca->payload = nch->payload;
+	nr = ncsi_alloc_cmd_req(nca);
+	if (!nr)
+		return -ENOMEM;
+
+	/* Prepare the packet */
+	nca->id = nr->id;
+	ret = nch->handler(nr->cmd, nca);
+	if (ret) {
+		ncsi_free_req(nr, true, false);
+		return ret;
+	}
+
+	/* Fill the ethernet header */
+	eh = (struct ethhdr *)skb_push(nr->cmd, sizeof(*eh));
+	eh->h_proto = htons(ETH_P_NCSI);
+	eth_broadcast_addr(eh->h_dest);
+	eth_broadcast_addr(eh->h_source);
+
+	/* Start the timer for the request that might not have
+	 * corresponding response. Given NCSI is an internal
+	 * connection a 1 second delay should be sufficient.
+	 */
+	nr->timer_enabled = true;
+	mod_timer(&nr->timer, jiffies + 1 * HZ);
+
+	/* Send NCSI packet */
+	skb_get(nr->cmd);
+	ret = dev_queue_xmit(nr->cmd);
+	if (ret < 0) {
+		ncsi_free_req(nr, true, false);
+		return ret;
+	}
+
+	return 0;
+}
diff --git a/net/ncsi/ncsi-pkt.h b/net/ncsi/ncsi-pkt.h
new file mode 100644
index 0000000..7f030b5
--- /dev/null
+++ b/net/ncsi/ncsi-pkt.h
@@ -0,0 +1,168 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2016.
+ *
+ * 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.
+ */
+
+#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 */
+};
+
+/* NCSI common command packet */
+struct ncsi_cmd_pkt {
+	struct ncsi_cmd_pkt_hdr cmd;      /* Command header */
+	__be32                  checksum; /* Checksum       */
+	unsigned char           pad[26];
+};
+
+/* 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];
+};
+
+/* 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];
+};
+
+/* 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                              */
+
+#endif /* __NCSI_PKT_H__ */
-- 
2.1.0

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

* [PATCH net-next 03/10] net/ncsi: NCSI response packet handler
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 01/10] net/ncsi: Resource management Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 02/10] net/ncsi: NCSI command packet handler Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 04/10] net/ncsi: Package and channel management Gavin Shan
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

The NCSI response packets are sent to MC (Management Controller)
from the remote end. They are responses of NCSI command packets
for multiple purposes: completion status of NCSI command packets,
return NCSI channel's capability or configuration etc.

This defines struct to represent NCSI response packets and introduces
function ncsi_rcv_rsp() which will be used to receive NCSI response
packets and parse them.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 net/ncsi/Makefile   |   2 +-
 net/ncsi/internal.h |   2 +
 net/ncsi/ncsi-pkt.h | 191 ++++++++++
 net/ncsi/ncsi-rsp.c | 987 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1181 insertions(+), 1 deletion(-)
 create mode 100644 net/ncsi/ncsi-rsp.c

diff --git a/net/ncsi/Makefile b/net/ncsi/Makefile
index abc4046..4751819 100644
--- a/net/ncsi/Makefile
+++ b/net/ncsi/Makefile
@@ -1,4 +1,4 @@
 #
 # Makefile for NCSI API
 #
-obj-$(CONFIG_NET_NCSI) += ncsi-cmd.o ncsi-manage.o
+obj-$(CONFIG_NET_NCSI) += ncsi-cmd.o ncsi-rsp.o ncsi-manage.o
diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index 842a687..508836a 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -269,5 +269,7 @@ struct ncsi_dev *ncsi_find_dev(struct net_device *dev);
 /* Packet handlers */
 u32 ncsi_calculate_checksum(unsigned char *data, int len);
 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);
 
 #endif /* __NCSI_INTERNAL_H__ */
diff --git a/net/ncsi/ncsi-pkt.h b/net/ncsi/ncsi-pkt.h
index 7f030b5..95882f3 100644
--- a/net/ncsi/ncsi-pkt.h
+++ b/net/ncsi/ncsi-pkt.h
@@ -25,6 +25,12 @@ 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           */
+};
+
 /* NCSI common command packet */
 struct ncsi_cmd_pkt {
 	struct ncsi_cmd_pkt_hdr cmd;      /* Command header */
@@ -32,6 +38,12 @@ struct ncsi_cmd_pkt {
 	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 */
@@ -133,6 +145,143 @@ struct ncsi_cmd_snfc_pkt {
 	unsigned char           pad[22];
 };
 
+/* 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;
+	unsigned char           pad[10];
+};
+
+/* 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            */
+};
+
 /* NCSI packet revision */
 #define NCSI_PKT_REVISION	0x01
 
@@ -165,4 +314,46 @@ struct ncsi_cmd_snfc_pkt {
 #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 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 */
+
 #endif /* __NCSI_PKT_H__ */
diff --git a/net/ncsi/ncsi-rsp.c b/net/ncsi/ncsi-rsp.c
new file mode 100644
index 0000000..20a45dc
--- /dev/null
+++ b/net/ncsi/ncsi-rsp.c
@@ -0,0 +1,987 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2016.
+ *
+ * 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;
+	u32 checksum;
+	__be32 *pchecksum;
+
+	/* 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->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.
+	 */
+	pchecksum = (__be32 *)((void *)(h + 1) + payload - 4);
+	if (ntohl(*pchecksum) == 0)
+		return 0;
+
+	checksum = ncsi_calculate_checksum((unsigned char *)h,
+					   sizeof(*h) + payload - 4);
+	if (*pchecksum != htonl(checksum))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int ncsi_rsp_handler_cis(struct ncsi_req *nr)
+{
+	struct ncsi_rsp_pkt *rsp;
+	struct ncsi_dev_priv *ndp = nr->ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	unsigned char id;
+
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
+	if (!nc) {
+		id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
+		nc = ncsi_add_channel(np, id);
+	} else if (nc->state == ncsi_channel_state_deselected_initial) {
+		nc->state = ncsi_channel_state_deselected_ready;
+	} else if (nc->state == ncsi_channel_state_selected_initial) {
+		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->ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	unsigned char id;
+
+	/* 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->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      &np, NULL);
+	if (!np) {
+		id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
+		np = ncsi_add_package(ndp, id);
+		if (!np)
+			return -ENODEV;
+	}
+
+	NCSI_FOR_EACH_CHANNEL(np, nc) {
+		if (nc->state == ncsi_channel_state_deselected_initial)
+			nc->state = ncsi_channel_state_selected_initial;
+		else if (nc->state == ncsi_channel_state_deselected_ready)
+			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->ndp;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+
+	/* Find the package */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->state == ncsi_channel_state_selected_initial)
+			nc->state = ncsi_channel_state_deselected_initial;
+		else if (nc->state == ncsi_channel_state_selected_ready)
+			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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      NULL, &nc);
+	if (!nc)
+		return -ENODEV;
+
+	ncm = &nc->modes[NCSI_MODE_ENABLE];
+	if (ncm->enable)
+		return -EBUSY;
+
+	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->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->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      NULL, &nc);
+	if (!nc)
+		return -ENODEV;
+
+	ncm = &nc->modes[NCSI_MODE_ENABLE];
+	if (!ncm->enable)
+		return -EBUSY;
+
+	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->ndp;
+	struct ncsi_channel *nc;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->state == ncsi_channel_state_deselected_ready)
+		nc->state = ncsi_channel_state_deselected_initial;
+	else if (nc->state == ncsi_channel_state_selected_ready)
+		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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      NULL, &nc);
+	if (!nc)
+		return -ENODEV;
+
+	ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
+	if (ncm->enable)
+		return -EBUSY;
+
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      NULL, &nc);
+	if (!nc)
+		return -ENODEV;
+
+	ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
+	if (!ncm->enable)
+		return -EBUSY;
+
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->modes[NCSI_MODE_AEN];
+	if (ncm->enable)
+		return -EBUSY;
+
+	/* Update to AEN configuration */
+	cmd = (struct ncsi_cmd_ae_pkt *)skb_network_header(nr->cmd);
+	ncm->enable = 1;
+	ncm->data[0] = cmd->mc_id;
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->cmd);
+	ncm = &nc->modes[NCSI_MODE_LINK];
+	ncm->data[0] = ntohl(cmd->mode);
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_gls_pkt *)skb_network_header(nr->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      NULL, &nc);
+	if (!nc)
+		return -ENODEV;
+
+	ncm = &nc->modes[NCSI_MODE_LINK];
+	ncm->data[2] = ntohl(rsp->status);
+	ncm->data[3] = ntohl(rsp->other);
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_filter *ncf;
+	unsigned short vlan;
+	int ret;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->cmd);
+	ncf = nc->filters[NCSI_FILTER_VLAN];
+	if (!ncf)
+		return -ENOENT;
+	if (cmd->index >= ncf->total)
+		return -ERANGE;
+
+	/* Add or remove the VLAN filter */
+	if (!(cmd->enable & 0x1)) {
+		ret = ncsi_remove_filter(nc, NCSI_FILTER_VLAN, cmd->index);
+	} else {
+		vlan = ntohs(cmd->vlan);
+		ret = ncsi_add_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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->modes[NCSI_MODE_VLAN];
+	if (ncm->enable)
+		return -EBUSY;
+
+	/* Update to VLAN mode */
+	cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->cmd);
+	ncm->enable = 1;
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->modes[NCSI_MODE_VLAN];
+	if (!ncm->enable)
+		return -EBUSY;
+
+	/* Update to VLAN mode */
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_filter *ncf;
+	void *bitmap;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->cmd);
+	switch (cmd->at_e >> 5) {
+	case 0x0:	/* UC address */
+		ncf = nc->filters[NCSI_FILTER_UC];
+		break;
+	case 0x1:	/* MC address */
+		ncf = nc->filters[NCSI_FILTER_MC];
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	/* Sanity check on the filter */
+	if (!ncf)
+		return -ENOENT;
+	else if (cmd->index >= ncf->total)
+		return -ERANGE;
+
+	bitmap = &ncf->bitmap;
+	if (cmd->at_e & 0x1) {
+		if (test_and_set_bit(cmd->index, bitmap))
+			return -EBUSY;
+		memcpy(ncf->data + 6 * cmd->index, cmd->mac, 6);
+	} else {
+		if (!test_and_clear_bit(cmd->index, bitmap))
+			return -EBUSY;
+
+		memset(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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the package and channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->modes[NCSI_MODE_BC];
+	if (ncm->enable)
+		return -EBUSY;
+
+	/* Update to broadcast filter mode */
+	cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
+	ncm->enable = 1;
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->modes[NCSI_MODE_BC];
+	if (!ncm->enable)
+		return -EBUSY;
+
+	/* Update to broadcast filter mode */
+	ncm->enable = 0;
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->modes[NCSI_MODE_MC];
+	if (ncm->enable)
+		return -EBUSY;
+
+	/* Update to multicast filter mode */
+	cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
+	ncm->enable = 1;
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->modes[NCSI_MODE_MC];
+	if (!ncm->enable)
+		return -EBUSY;
+
+	/* Update to multicast filter mode */
+	ncm->enable = 0;
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* Find the channel */
+	rsp = (struct ncsi_rsp_pkt *)skb_network_header(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->modes[NCSI_MODE_FC];
+	if (ncm->enable)
+		return -EBUSY;
+
+	/* Update to flow control mode */
+	cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
+	ncm->enable = 1;
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_version *ncv;
+	int i;
+
+	/* Find the channel */
+	rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(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->version;
+	ncv->version = ntohl(rsp->ncsi_version);
+	ncv->alpha2 = rsp->alpha2;
+	memcpy(ncv->fw_name, rsp->fw_name, 12);
+	ncv->fw_version = ntohl(rsp->fw_version);
+	for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
+		ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_filter *ncf;
+	size_t size, entry_size;
+	int cnt, i;
+
+	/* Find the channel */
+	rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      NULL, &nc);
+	if (!nc)
+		return -ENODEV;
+
+	/* Update channel's capabilities */
+	nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
+					 NCSI_CAP_GENERIC_MASK;
+	nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
+				    NCSI_CAP_BC_MASK;
+	nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
+				    NCSI_CAP_MC_MASK;
+	nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
+	nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
+				     NCSI_CAP_AEN_MASK;
+	nc->caps[NCSI_CAP_VLAN].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->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->index = i;
+		ncf->total = cnt;
+		ncf->bitmap = 0x0ul;
+		nc->filters[i] = ncf;
+	}
+
+	return 0;
+}
+
+static int ncsi_rsp_handler_gp(struct ncsi_req *nr)
+{
+	struct ncsi_rsp_gp_pkt *rsp;
+	struct ncsi_dev_priv *ndp = nr->ndp;
+	struct ncsi_channel *nc;
+	unsigned short enable, vlan;
+	unsigned char *pdata;
+	int table, i;
+
+	/* Find the channel */
+	rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(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->modes[NCSI_MODE_BC].enable = 1;
+		nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
+	}
+	if (ntohl(rsp->valid_modes) & 0x2)	/* Channel enabled */
+		nc->modes[NCSI_MODE_ENABLE].enable = 1;
+	if (ntohl(rsp->valid_modes) & 0x4)	/* Channel Tx enabled */
+		nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
+	if (ntohl(rsp->valid_modes) & 0x8)	/* MC filter mode */
+		nc->modes[NCSI_MODE_MC].enable = 1;
+
+	/* Modes without explicit enabled indications */
+	nc->modes[NCSI_MODE_LINK].enable = 1;
+	nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
+	nc->modes[NCSI_MODE_VLAN].enable = 1;
+	nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
+	nc->modes[NCSI_MODE_FC].enable = 1;
+	nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
+	nc->modes[NCSI_MODE_AEN].enable = 1;
+	nc->modes[NCSI_MODE_AEN].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->filters[NCSI_FILTER_UC]->total +
+			  nc->filters[NCSI_FILTER_MC]->total))
+			table = NCSI_FILTER_MIXED;
+		else if (i >= nc->filters[NCSI_FILTER_UC]->total)
+			table = NCSI_FILTER_MC;
+		else
+			table = NCSI_FILTER_UC;
+
+		if (!(enable & (0x1 << i)))
+			continue;
+
+		if (ncsi_find_filter(nc, table, pdata) >= 0)
+			continue;
+
+		ncsi_add_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_filter(nc, NCSI_FILTER_VLAN, &vlan) >= 0)
+			continue;
+
+		ncsi_add_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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_stats *ncs;
+
+	/* Find the channel */
+	rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      NULL, &nc);
+	if (!nc)
+		return -ENODEV;
+
+	/* Update HNC's statistics */
+	ncs = &nc->stats;
+	ncs->hnc_cnt_hi         = ntohl(rsp->cnt_hi);
+	ncs->hnc_cnt_lo         = ntohl(rsp->cnt_lo);
+	ncs->hnc_rx_bytes       = ntohl(rsp->rx_bytes);
+	ncs->hnc_tx_bytes       = ntohl(rsp->tx_bytes);
+	ncs->hnc_rx_uc_pkts     = ntohl(rsp->rx_uc_pkts);
+	ncs->hnc_rx_mc_pkts     = ntohl(rsp->rx_mc_pkts);
+	ncs->hnc_rx_bc_pkts     = ntohl(rsp->rx_bc_pkts);
+	ncs->hnc_tx_uc_pkts     = ntohl(rsp->tx_uc_pkts);
+	ncs->hnc_tx_mc_pkts     = ntohl(rsp->tx_mc_pkts);
+	ncs->hnc_tx_bc_pkts     = ntohl(rsp->tx_bc_pkts);
+	ncs->hnc_fcs_err        = ntohl(rsp->fcs_err);
+	ncs->hnc_align_err      = ntohl(rsp->align_err);
+	ncs->hnc_false_carrier  = ntohl(rsp->false_carrier);
+	ncs->hnc_runt_pkts      = ntohl(rsp->runt_pkts);
+	ncs->hnc_jabber_pkts    = ntohl(rsp->jabber_pkts);
+	ncs->hnc_rx_pause_xon   = ntohl(rsp->rx_pause_xon);
+	ncs->hnc_rx_pause_xoff  = ntohl(rsp->rx_pause_xoff);
+	ncs->hnc_tx_pause_xon   = ntohl(rsp->tx_pause_xon);
+	ncs->hnc_tx_pause_xoff  = ntohl(rsp->tx_pause_xoff);
+	ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
+	ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
+	ncs->hnc_l_collision    = ntohl(rsp->l_collision);
+	ncs->hnc_e_collision    = ntohl(rsp->e_collision);
+	ncs->hnc_rx_ctl_frames  = ntohl(rsp->rx_ctl_frames);
+	ncs->hnc_rx_64_frames   = ntohl(rsp->rx_64_frames);
+	ncs->hnc_rx_127_frames  = ntohl(rsp->rx_127_frames);
+	ncs->hnc_rx_255_frames  = ntohl(rsp->rx_255_frames);
+	ncs->hnc_rx_511_frames  = ntohl(rsp->rx_511_frames);
+	ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
+	ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
+	ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
+	ncs->hnc_tx_64_frames   = ntohl(rsp->tx_64_frames);
+	ncs->hnc_tx_127_frames  = ntohl(rsp->tx_127_frames);
+	ncs->hnc_tx_255_frames  = ntohl(rsp->tx_255_frames);
+	ncs->hnc_tx_511_frames  = ntohl(rsp->tx_511_frames);
+	ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
+	ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
+	ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
+	ncs->hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
+	ncs->hnc_rx_runt_pkts   = ntohl(rsp->rx_runt_pkts);
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_stats *ncs;
+
+	/* Find the channel */
+	rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      NULL, &nc);
+	if (!nc)
+		return -ENODEV;
+
+	/* Update HNC's statistics */
+	ncs = &nc->stats;
+	ncs->ncsi_rx_cmds       = ntohl(rsp->rx_cmds);
+	ncs->ncsi_dropped_cmds  = ntohl(rsp->dropped_cmds);
+	ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
+	ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
+	ncs->ncsi_rx_pkts       = ntohl(rsp->rx_pkts);
+	ncs->ncsi_tx_pkts       = ntohl(rsp->tx_pkts);
+	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->ndp;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_stats *ncs;
+
+	/* Find the channel */
+	rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->rsp);
+	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
+				      NULL, &nc);
+	if (!nc)
+		return -ENODEV;
+
+	/* Update HNC's statistics */
+	ncs = &nc->stats;
+	ncs->pt_tx_pkts        = ntohl(rsp->tx_pkts);
+	ncs->pt_tx_dropped     = ntohl(rsp->tx_dropped);
+	ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
+	ncs->pt_tx_us_err      = ntohl(rsp->tx_us_err);
+	ncs->pt_rx_pkts        = ntohl(rsp->rx_pkts);
+	ncs->pt_rx_dropped     = ntohl(rsp->rx_dropped);
+	ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
+	ncs->pt_rx_us_err      = ntohl(rsp->rx_us_err);
+	ncs->pt_rx_os_err      = ntohl(rsp->rx_os_err);
+
+	return 0;
+}
+
+static struct ncsi_rsp_handler {
+	unsigned char	type;
+	int             payload;
+	int		(*handler)(struct ncsi_req *nr);
+} ncsi_rsp_handlers[] = {
+	{ NCSI_PKT_RSP_CIS,     4, ncsi_rsp_handler_cis     },
+	{ NCSI_PKT_RSP_SP,      4, ncsi_rsp_handler_sp      },
+	{ NCSI_PKT_RSP_DP,      4, ncsi_rsp_handler_dp      },
+	{ NCSI_PKT_RSP_EC,      4, ncsi_rsp_handler_ec      },
+	{ NCSI_PKT_RSP_DC,      4, ncsi_rsp_handler_dc      },
+	{ NCSI_PKT_RSP_RC,      4, ncsi_rsp_handler_rc      },
+	{ NCSI_PKT_RSP_ECNT,    4, ncsi_rsp_handler_ecnt    },
+	{ NCSI_PKT_RSP_DCNT,    4, ncsi_rsp_handler_dcnt    },
+	{ NCSI_PKT_RSP_AE,      4, ncsi_rsp_handler_ae      },
+	{ NCSI_PKT_RSP_SL,      4, ncsi_rsp_handler_sl      },
+	{ NCSI_PKT_RSP_GLS,    16, ncsi_rsp_handler_gls     },
+	{ NCSI_PKT_RSP_SVF,     4, ncsi_rsp_handler_svf     },
+	{ NCSI_PKT_RSP_EV,      4, ncsi_rsp_handler_ev      },
+	{ NCSI_PKT_RSP_DV,      4, ncsi_rsp_handler_dv      },
+	{ NCSI_PKT_RSP_SMA,     4, ncsi_rsp_handler_sma     },
+	{ NCSI_PKT_RSP_EBF,     4, ncsi_rsp_handler_ebf     },
+	{ NCSI_PKT_RSP_DBF,     4, ncsi_rsp_handler_dbf     },
+	{ NCSI_PKT_RSP_EGMF,    4, ncsi_rsp_handler_egmf    },
+	{ NCSI_PKT_RSP_DGMF,    4, ncsi_rsp_handler_dgmf    },
+	{ NCSI_PKT_RSP_SNFC,    4, ncsi_rsp_handler_snfc    },
+	{ NCSI_PKT_RSP_GVI,    36, ncsi_rsp_handler_gvi     },
+	{ NCSI_PKT_RSP_GC,     32, ncsi_rsp_handler_gc      },
+	{ NCSI_PKT_RSP_GP,     -1, ncsi_rsp_handler_gp      },
+	{ NCSI_PKT_RSP_GCPS,  172, ncsi_rsp_handler_gcps    },
+	{ NCSI_PKT_RSP_GNS,   172, ncsi_rsp_handler_gns     },
+	{ NCSI_PKT_RSP_GNPTS, 172, ncsi_rsp_handler_gnpts   },
+	{ NCSI_PKT_RSP_OEM,     0, NULL                     },
+};
+
+int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
+		 struct packet_type *pt, struct net_device *orig_dev)
+{
+	struct ncsi_rsp_handler *nrh = NULL;
+	struct ncsi_dev *nd;
+	struct ncsi_dev_priv *ndp;
+	struct ncsi_req *nr;
+	struct ncsi_pkt_hdr *hdr;
+	unsigned long flags;
+	int payload, ret;
+
+	/* Find the NCSI device */
+	nd = ncsi_find_dev(dev);
+	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
+	if (!ndp)
+		return -ENODEV;
+
+	/* Find the handler */
+	hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
+	nrh = ncsi_rsp_handlers;
+	while (nrh->handler) {
+		if (nrh->type == hdr->type)
+			break;
+
+		nrh++;
+	}
+
+	if (!nrh->handler) {
+		pr_warn("NCSI: Received unrecognized packet (0x%x)\n",
+			hdr->type);
+		return -ENOENT;
+	}
+
+	/* Associate with the request */
+	nr = &ndp->reqs[hdr->id];
+	spin_lock_irqsave(&ndp->req_lock, flags);
+	if (!nr->used) {
+		spin_unlock_irqrestore(&ndp->req_lock, flags);
+		return -ENODEV;
+	}
+
+	nr->rsp = skb;
+	if (!nr->timer_enabled) {
+		ret = -ENOENT;
+		goto out;
+	}
+
+	/* Validate the packet */
+	payload = nrh->payload;
+	if (payload < 0)
+		payload = ntohs(hdr->length);
+	ret = ncsi_validate_rsp_pkt(nr, payload);
+	if (ret)
+		goto out;
+
+	/* Process the packet */
+	ret = nrh->handler(nr);
+out:
+	spin_unlock_irqrestore(&ndp->req_lock, flags);
+	ncsi_free_req(nr, true, false);
+	return ret;
+}
-- 
2.1.0

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

* [PATCH net-next 04/10] net/ncsi: Package and channel management
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
                   ` (2 preceding siblings ...)
  2016-07-03  5:32 ` [PATCH net-next 03/10] net/ncsi: NCSI response " Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 05/10] net/ncsi: NCSI AEN packet handler Gavin Shan
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

The available NCSI packages and channels are probed with help of
NCSI command/response packets. One package and channel should be
selected as active one by which the MC (Management Controller)
can communicate with the far end and external network. The currently
active channel should be disabled and another available channel should
be selected as active one when AEN (Asychronous Event Notification)
packets received.

This introduces state machine and corresponding functions for above
purposes. ncsi_start_dev() should be called after ncsi_register_dev()
in network device driver. It probes all available NCSI packages and
channels and selects the active one to provide service. ncsi_config_dev()
and ncsi_suspend_dev() are going to be used when AEN (Asychronous
Event Notification) packets are received.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 include/net/ncsi.h     |   6 +
 net/ncsi/internal.h    |  34 ++++
 net/ncsi/ncsi-manage.c | 471 +++++++++++++++++++++++++++++++++++++++++++++++++
 net/ncsi/ncsi-rsp.c    |   6 +
 4 files changed, 517 insertions(+)

diff --git a/include/net/ncsi.h b/include/net/ncsi.h
index 70d14ee..1dbf42f 100644
--- a/include/net/ncsi.h
+++ b/include/net/ncsi.h
@@ -30,6 +30,7 @@ struct ncsi_dev {
 #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);
 void ncsi_unregister_dev(struct ncsi_dev *nd);
 #else /* !CONFIG_NET_NCSI */
 static inline struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
@@ -38,6 +39,11 @@ static inline struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
 	return NULL;
 }
 
+static inline int ncsi_start_dev(struct ncsi_dev *nd)
+{
+	return -ENOTTY;
+}
+
 static inline void ncsi_unregister_dev(struct ncsi_dev *nd)
 {
 }
diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index 508836a..e322f9e 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -205,9 +205,38 @@ struct ncsi_req {
 	bool                 timer_enabled;	/* Timer was started   */
 };
 
+enum {
+	ncsi_dev_state_major		= 0xff00,
+	ncsi_dev_state_minor		= 0x00ff,
+	ncsi_dev_state_probe_deselect	= 0x0201,
+	ncsi_dev_state_probe_package,
+	ncsi_dev_state_probe_channel,
+	ncsi_dev_state_probe_cis,
+	ncsi_dev_state_probe_gvi,
+	ncsi_dev_state_probe_gc,
+	ncsi_dev_state_probe_gls,
+	ncsi_dev_state_probe_dp,
+	ncsi_dev_state_config_sp	= 0x0301,
+	ncsi_dev_state_config_cis,
+	ncsi_dev_state_config_sma,
+	ncsi_dev_state_config_ebf,
+	ncsi_dev_state_config_ecnt,
+	ncsi_dev_state_config_ec,
+	ncsi_dev_state_config_ae,
+	ncsi_dev_state_config_gls,
+	ncsi_dev_state_config_done,
+	ncsi_dev_state_suspend_select	= 0x0401,
+	ncsi_dev_state_suspend_dcnt,
+	ncsi_dev_state_suspend_dc,
+	ncsi_dev_state_suspend_deselect,
+	ncsi_dev_state_suspend_done
+};
+
 struct ncsi_dev_priv {
 	struct ncsi_dev     ndev;		/* NCSI device              */
 	int                 flags;		/* NCSI device flags        */
+#define NCSI_DEV_FLAG_PROBED	0x1
+#define NCSI_DEV_FLAG_RESHUFFLE	0x2
 	struct ncsi_package *active_package;	/* Active NCSI package      */
 	struct ncsi_channel *active_channel;	/* Active NCSI channel      */
 	atomic_t            package_num;	/* Number of packages       */
@@ -216,6 +245,9 @@ struct ncsi_dev_priv {
 	spinlock_t          req_lock;		/* Protect request table    */
 	struct ncsi_req     reqs[256];		/* Request table            */
 	int                 last_req_idx;	/* Last used request ID     */
+	int                 pending_reqs;	/* Pending command number   */
+	struct work_struct  work;		/* For channel management   */
+	struct packet_type  ptype;		/* NCSI packet Rx handler   */
 	struct list_head    node;
 };
 
@@ -265,6 +297,8 @@ 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);
+int ncsi_suspend_dev(struct ncsi_dev *nd);
 
 /* Packet handlers */
 u32 ncsi_calculate_checksum(unsigned char *data, int len);
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index b36e93d..7c9c8ac 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -19,6 +19,7 @@
 #include <net/sock.h>
 
 #include "internal.h"
+#include "ncsi-pkt.h"
 
 LIST_HEAD(ncsi_dev_list);
 DEFINE_SPINLOCK(ncsi_dev_lock);
@@ -329,6 +330,9 @@ void ncsi_free_req(struct ncsi_req *nr, bool check, bool timeout)
 	nr->used = false;
 	spin_unlock_irqrestore(&ndp->req_lock, flags);
 
+	if (check && cmd && --ndp->pending_reqs == 0)
+		schedule_work(&ndp->work);
+
 	/* Release command and response */
 	consume_skb(cmd);
 	consume_skb(rsp);
@@ -367,6 +371,413 @@ static void ncsi_req_timeout(unsigned long data)
 	ncsi_free_req(nr, true, true);
 }
 
+static void ncsi_dev_config(struct ncsi_dev_priv *ndp)
+{
+	struct ncsi_dev *nd = &ndp->ndev;
+	struct net_device *dev = nd->dev;
+	struct ncsi_package *np = ndp->active_package;
+	struct ncsi_channel *nc = ndp->active_channel;
+	struct ncsi_cmd_arg nca;
+	unsigned char index;
+	int ret;
+
+	nca.ndp = ndp;
+	switch (nd->state) {
+	case ncsi_dev_state_config:
+	case ncsi_dev_state_config_sp:
+		ndp->pending_reqs = 1;
+
+		/* Select the specific package */
+		nca.type = NCSI_PKT_CMD_SP;
+		nca.bytes[0] = 1;
+		nca.package = np->id;
+		nca.channel = 0x1f;
+		ret = ncsi_xmit_cmd(&nca);
+		if (ret)
+			goto error;
+
+		nd->state = ncsi_dev_state_config_cis;
+		break;
+	case ncsi_dev_state_config_cis:
+		ndp->pending_reqs = 1;
+
+		/* Clear initial state */
+		nca.type = NCSI_PKT_CMD_CIS;
+		nca.package = np->id;
+		nca.channel = nc->id;
+		ret = ncsi_xmit_cmd(&nca);
+		if (ret)
+			goto error;
+
+		nd->state = ncsi_dev_state_config_sma;
+		break;
+	case ncsi_dev_state_config_sma:
+	case ncsi_dev_state_config_ebf:
+	case ncsi_dev_state_config_ecnt:
+	case ncsi_dev_state_config_ec:
+	case ncsi_dev_state_config_ae:
+	case ncsi_dev_state_config_gls:
+		ndp->pending_reqs = 1;
+
+		nca.package = np->id;
+		nca.channel = nc->id;
+
+		/* Use first entry in unicast filter table. Note that
+		 * the MAC filter table starts from entry 1 instead of
+		 * 0.
+		 */
+		if (nd->state == ncsi_dev_state_config_sma) {
+			nca.type = NCSI_PKT_CMD_SMA;
+			for (index = 0; index < 6; index++)
+				nca.bytes[index] = dev->dev_addr[index];
+			nca.bytes[6] = 0x1;
+			nca.bytes[7] = 0x1;
+			nd->state = ncsi_dev_state_config_ebf;
+		} else if (nd->state == ncsi_dev_state_config_ebf) {
+			nca.type = NCSI_PKT_CMD_EBF;
+			nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
+			nd->state = ncsi_dev_state_config_ecnt;
+		} else if (nd->state == ncsi_dev_state_config_ecnt) {
+			nca.type = NCSI_PKT_CMD_ECNT;
+			nd->state = ncsi_dev_state_config_ec;
+		} else if (nd->state == ncsi_dev_state_config_ec) {
+			nca.type = NCSI_PKT_CMD_EC;
+			nd->state = ncsi_dev_state_config_ae;
+		} else if (nd->state == ncsi_dev_state_config_ae) {
+			nca.type = NCSI_PKT_CMD_AE;
+			nca.bytes[0] = 0;
+			nca.dwords[1] = 0x7;
+			nd->state = ncsi_dev_state_config_gls;
+		} else if (nd->state == ncsi_dev_state_config_gls) {
+			nca.type = NCSI_PKT_CMD_GLS;
+			nd->state = ncsi_dev_state_config_done;
+		}
+
+		ret = ncsi_xmit_cmd(&nca);
+		if (ret)
+			goto error;
+		break;
+	case ncsi_dev_state_config_done:
+		nd->state = ncsi_dev_state_functional;
+		nd->link_up = 0;
+		if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1)
+			nd->link_up = 1;
+
+		nd->handler(nd);
+		ndp->flags &= ~NCSI_DEV_FLAG_RESHUFFLE;
+
+		break;
+	default:
+		pr_warn("NCSI: Wrong NCSI dev state 0x%x in config\n",
+			nd->state);
+	}
+
+	return;
+
+error:
+	nd->state = ncsi_dev_state_functional;
+	nd->link_up = 0;
+	ndp->flags &= ~NCSI_DEV_FLAG_RESHUFFLE;
+	nd->handler(nd);
+}
+
+static void ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
+{
+	struct ncsi_dev *nd;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	nd = &ndp->ndev;
+	ndp->active_package = NULL;
+	ndp->active_channel = NULL;
+	NCSI_FOR_EACH_PACKAGE(ndp, np) {
+		NCSI_FOR_EACH_CHANNEL(np, nc) {
+			if (!ndp->active_channel) {
+				ndp->active_package = np;
+				ndp->active_channel = nc;
+			}
+
+			ncm = &nc->modes[NCSI_MODE_LINK];
+			if (ncm->data[2] & 0x1) {
+				ndp->active_package = np;
+				ndp->active_channel = nc;
+				goto configure;
+			}
+		}
+	}
+
+configure:
+	if (!ndp->active_channel) {
+		nd->state = ncsi_dev_state_functional;
+		nd->link_up = 0;
+		nd->handler(nd);
+	} else {
+		nd->state = ncsi_dev_state_config;
+		ncsi_dev_config(ndp);
+	}
+}
+
+static void ncsi_dev_probe(struct ncsi_dev_priv *ndp)
+{
+	struct ncsi_dev *nd = &ndp->ndev;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_cmd_arg nca;
+	unsigned char index;
+	int ret;
+
+	nca.ndp = ndp;
+	switch (nd->state) {
+	case ncsi_dev_state_probe:
+		nd->state = ncsi_dev_state_probe_deselect;
+		/* Fall through */
+	case ncsi_dev_state_probe_deselect:
+		ndp->pending_reqs = 8;
+
+		/* Deselect all possible packages */
+		nca.type = NCSI_PKT_CMD_DP;
+		nca.channel = 0x1f;
+		for (index = 0; index < 8; index++) {
+			nca.package = index;
+			ret = ncsi_xmit_cmd(&nca);
+			if (ret)
+				goto error;
+		}
+
+		nd->state = ncsi_dev_state_probe_package;
+		break;
+	case ncsi_dev_state_probe_package:
+		ndp->pending_reqs = 16;
+
+		/* Select all possible packages */
+		nca.type = NCSI_PKT_CMD_SP;
+		nca.bytes[0] = 1;
+		nca.channel = 0x1f;
+		for (index = 0; index < 8; index++) {
+			nca.package = index;
+			ret = ncsi_xmit_cmd(&nca);
+			if (ret)
+				goto error;
+		}
+
+		/* Disable all possible packages */
+		nca.type = NCSI_PKT_CMD_DP;
+		for (index = 0; index < 8; index++) {
+			nca.package = index;
+			ret = ncsi_xmit_cmd(&nca);
+			if (ret)
+				goto error;
+		}
+
+		nd->state = ncsi_dev_state_probe_channel;
+		break;
+	case ncsi_dev_state_probe_channel:
+		if (!ndp->active_package)
+			ndp->active_package = list_first_or_null_rcu(
+				&ndp->packages, struct ncsi_package, node);
+		else if (list_is_last(&ndp->active_package->node,
+				      &ndp->packages))
+			ndp->active_package = NULL;
+		else
+			ndp->active_package = list_next_entry(
+				ndp->active_package, node);
+
+		/* All available packages and channels are enumerated. The
+		 * enumeration happens for once when the NCSI interface is
+		 * started. So we need continue to start the interface after
+		 * the enumeration.
+		 *
+		 * We have to choose an active channel before configuring it.
+		 * Note that we possibly don't have active channel in extreme
+		 * situation.
+		 */
+		if (!ndp->active_package) {
+			ndp->flags |= NCSI_DEV_FLAG_PROBED;
+			ncsi_choose_active_channel(ndp);
+			return;
+		}
+
+		/* Select the active package */
+		ndp->pending_reqs = 1;
+		nca.type = NCSI_PKT_CMD_SP;
+		nca.bytes[0] = 1;
+		nca.package = ndp->active_package->id;
+		nca.channel = 0x1f;
+		ret = ncsi_xmit_cmd(&nca);
+		if (ret)
+			goto error;
+
+		nd->state = ncsi_dev_state_probe_cis;
+		break;
+	case ncsi_dev_state_probe_cis:
+		ndp->pending_reqs = 32;
+
+		/* Clear initial state */
+		nca.type = NCSI_PKT_CMD_CIS;
+		nca.package = ndp->active_package->id;
+		for (index = 0; index < 0x20; index++) {
+			nca.channel = index;
+			ret = ncsi_xmit_cmd(&nca);
+			if (ret)
+				goto error;
+		}
+
+		nd->state = ncsi_dev_state_probe_gvi;
+		break;
+	case ncsi_dev_state_probe_gvi:
+	case ncsi_dev_state_probe_gc:
+	case ncsi_dev_state_probe_gls:
+		np = ndp->active_package;
+		ndp->pending_reqs = atomic_read(&np->channel_num);
+
+		/* Retrieve version, capability or link status */
+		if (nd->state == ncsi_dev_state_probe_gvi)
+			nca.type = NCSI_PKT_CMD_GVI;
+		else if (nd->state == ncsi_dev_state_probe_gc)
+			nca.type = NCSI_PKT_CMD_GC;
+		else
+			nca.type = NCSI_PKT_CMD_GLS;
+
+		nca.package = np->id;
+		NCSI_FOR_EACH_CHANNEL(np, nc) {
+			nca.channel = nc->id;
+			ret = ncsi_xmit_cmd(&nca);
+			if (ret)
+				goto error;
+		}
+
+		if (nd->state == ncsi_dev_state_probe_gvi)
+			nd->state = ncsi_dev_state_probe_gc;
+		else if (nd->state == ncsi_dev_state_probe_gc)
+			nd->state = ncsi_dev_state_probe_gls;
+		else
+			nd->state = ncsi_dev_state_probe_dp;
+		break;
+	case ncsi_dev_state_probe_dp:
+		ndp->pending_reqs = 1;
+
+		/* Deselect the active package */
+		nca.type = NCSI_PKT_CMD_DP;
+		nca.package = ndp->active_package->id;
+		nca.channel = 0x1f;
+		ret = ncsi_xmit_cmd(&nca);
+		if (ret)
+			goto error;
+
+		/* Scan channels in next package */
+		nd->state = ncsi_dev_state_probe_channel;
+		break;
+	default:
+		pr_warn("NCSI: Wrong NCSI dev state 0x%0x in enumeration\n",
+			nd->state);
+	}
+
+	return;
+error:
+	nd->state = ncsi_dev_state_functional;
+	nd->link_up = 0;
+	nd->handler(nd);
+}
+
+static void ncsi_dev_suspend(struct ncsi_dev_priv *ndp)
+{
+	struct ncsi_dev *nd = &ndp->ndev;
+	struct ncsi_package *np;
+	struct ncsi_channel *nc;
+	struct ncsi_cmd_arg nca;
+	int ret;
+
+	nca.ndp = ndp;
+	switch (nd->state) {
+	case ncsi_dev_state_suspend:
+		/* If there're no active channel, we're done */
+		if (!ndp->active_channel) {
+			nd->state = ncsi_dev_state_functional;
+
+			if (ndp->flags & NCSI_DEV_FLAG_RESHUFFLE) {
+				ndp->flags &= ~NCSI_DEV_FLAG_RESHUFFLE;
+				ncsi_choose_active_channel(ndp);
+			}
+
+			return;
+		}
+
+		nd->state = ncsi_dev_state_suspend_select;
+		/* Fall through */
+	case ncsi_dev_state_suspend_select:
+	case ncsi_dev_state_suspend_dcnt:
+	case ncsi_dev_state_suspend_dc:
+	case ncsi_dev_state_suspend_deselect:
+		ndp->pending_reqs = 1;
+
+		np = ndp->active_package;
+		nc = ndp->active_channel;
+		nca.package = np->id;
+		if (nd->state == ncsi_dev_state_suspend_select) {
+			nca.type = NCSI_PKT_CMD_SP;
+			nca.channel = 0x1f;
+			nca.bytes[0] = 1;
+			nd->state = ncsi_dev_state_suspend_dcnt;
+		} else if (nd->state == ncsi_dev_state_suspend_dcnt) {
+			nca.type = NCSI_PKT_CMD_DCNT;
+			nca.channel = nc->id;
+			nd->state = ncsi_dev_state_suspend_dc;
+		} else if (nd->state == ncsi_dev_state_suspend_dc) {
+			nca.type = NCSI_PKT_CMD_DC;
+			nca.channel = nc->id;
+			nca.bytes[0] = 1;
+			nd->state = ncsi_dev_state_suspend_deselect;
+		} else if (nd->state == ncsi_dev_state_suspend_deselect) {
+			nca.type = NCSI_PKT_CMD_DP;
+			nca.channel = 0x1f;
+			nd->state = ncsi_dev_state_suspend_done;
+		}
+
+		ret = ncsi_xmit_cmd(&nca);
+		if (ret) {
+			nd->state = ncsi_dev_state_functional;
+			return;
+		}
+
+		break;
+	case ncsi_dev_state_suspend_done:
+		nd->state = ncsi_dev_state_functional;
+
+		if (ndp->flags & NCSI_DEV_FLAG_RESHUFFLE) {
+			ndp->flags &= ~NCSI_DEV_FLAG_RESHUFFLE;
+			ncsi_choose_active_channel(ndp);
+		}
+
+		break;
+	default:
+		pr_warn("NCSI: Wrong NCSI dev state 0x%x in suspend\n",
+			nd->state);
+	}
+}
+
+static void ncsi_dev_work(struct work_struct *work)
+{
+	struct ncsi_dev_priv *ndp = container_of(work,
+			struct ncsi_dev_priv, work);
+	struct ncsi_dev *nd = &ndp->ndev;
+
+	switch (nd->state & ncsi_dev_state_major) {
+	case ncsi_dev_state_probe:
+		ncsi_dev_probe(ndp);
+		break;
+	case ncsi_dev_state_suspend:
+		ncsi_dev_suspend(ndp);
+		break;
+	case ncsi_dev_state_config:
+		ncsi_dev_config(ndp);
+		break;
+	default:
+		pr_warn("NCSI: Wrong NCSI dev state 0x%x in workqueue\n",
+			nd->state);
+	}
+}
+
 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
 				   void (*handler)(struct ncsi_dev *ndev))
 {
@@ -401,19 +812,79 @@ struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
 			    (unsigned long)&ndp->reqs[idx]);
 	}
 
+	ndp->pending_reqs = 0;
+	INIT_WORK(&ndp->work, ncsi_dev_work);
+
 	spin_lock(&ncsi_dev_lock);
 	list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
 	spin_unlock(&ncsi_dev_lock);
 
+	/* Register NCSI packet Rx handler */
+	ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
+	ndp->ptype.func = ncsi_rcv_rsp;
+	ndp->ptype.dev = dev;
+	dev_add_pack(&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->state != ncsi_dev_state_registered &&
+	    nd->state != ncsi_dev_state_functional)
+		return -ENOTTY;
+
+	if (!(ndp->flags & NCSI_DEV_FLAG_PROBED)) {
+		nd->state = ncsi_dev_state_probe;
+		schedule_work(&ndp->work);
+		return 0;
+	}
+
+	/* Choose active package and channel */
+	ncsi_choose_active_channel(ndp);
+	if (!ndp->active_channel)
+		return -ENXIO;
+
+	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->state != ncsi_dev_state_functional)
+		return -ENOTTY;
+
+	nd->state = ncsi_dev_state_config;
+	schedule_work(&ndp->work);
+
+	return 0;
+}
+
+int ncsi_suspend_dev(struct ncsi_dev *nd)
+{
+	struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
+
+	if (nd->state != ncsi_dev_state_functional)
+		return -ENOTTY;
+
+	nd->state = ncsi_dev_state_suspend;
+	schedule_work(&ndp->work);
+
+	return 0;
+}
+
 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->ptype);
+
 	spin_lock(&ndp->package_lock);
 	list_for_each_entry_safe(np, tmp, &ndp->packages, node)
 		ncsi_remove_package(np);
diff --git a/net/ncsi/ncsi-rsp.c b/net/ncsi/ncsi-rsp.c
index 20a45dc..5b81217 100644
--- a/net/ncsi/ncsi-rsp.c
+++ b/net/ncsi/ncsi-rsp.c
@@ -69,6 +69,9 @@ static int ncsi_rsp_handler_cis(struct ncsi_req *nr)
 	rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
 	if (!nc) {
+		if (ndp->flags & NCSI_DEV_FLAG_PROBED)
+			return -ENXIO;
+
 		id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
 		nc = ncsi_add_channel(np, id);
 	} else if (nc->state == ncsi_channel_state_deselected_initial) {
@@ -95,6 +98,9 @@ static int ncsi_rsp_handler_sp(struct ncsi_req *nr)
 	ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
 				      &np, NULL);
 	if (!np) {
+		if (ndp->flags & NCSI_DEV_FLAG_PROBED)
+			return -ENXIO;
+
 		id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
 		np = ncsi_add_package(ndp, id);
 		if (!np)
-- 
2.1.0

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

* [PATCH net-next 05/10] net/ncsi: NCSI AEN packet handler
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
                   ` (3 preceding siblings ...)
  2016-07-03  5:32 ` [PATCH net-next 04/10] net/ncsi: Package and channel management Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 06/10] net/farady: Helper functions to create or destroy MDIO interface Gavin Shan
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

This introduces NCSI AEN packet handlers that result in (A) the
currently active channel is reconfigured; (B) Currently active
channel is deconfigured and disabled, another channel is chosen
as active one and configured.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 net/ncsi/Makefile   |   2 +-
 net/ncsi/internal.h |   1 +
 net/ncsi/ncsi-aen.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/ncsi/ncsi-pkt.h |  36 +++++++++++
 net/ncsi/ncsi-rsp.c |   6 +-
 5 files changed, 212 insertions(+), 2 deletions(-)
 create mode 100644 net/ncsi/ncsi-aen.c

diff --git a/net/ncsi/Makefile b/net/ncsi/Makefile
index 4751819..dd12b56 100644
--- a/net/ncsi/Makefile
+++ b/net/ncsi/Makefile
@@ -1,4 +1,4 @@
 #
 # Makefile for NCSI API
 #
-obj-$(CONFIG_NET_NCSI) += ncsi-cmd.o ncsi-rsp.o 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 e322f9e..bebf699 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -305,5 +305,6 @@ u32 ncsi_calculate_checksum(unsigned char *data, int len);
 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..b14b6c4
--- /dev/null
+++ b/net/ncsi/ncsi-aen.c
@@ -0,0 +1,169 @@
+/*
+ * Copyright Gavin Shan, IBM Corporation 2016.
+ *
+ * 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)
+{
+	u32 checksum;
+	__be32 *pchecksum;
+
+	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.
+	 */
+	pchecksum = (__be32 *)((void *)(h + 1) + payload - 4);
+	if (ntohl(*pchecksum) == 0)
+		return 0;
+
+	checksum = ncsi_calculate_checksum((unsigned char *)h,
+					   sizeof(*h) + payload - 4);
+	if (*pchecksum != htonl(checksum))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int ncsi_aen_handler_lsc(struct ncsi_dev_priv *ndp,
+				struct ncsi_aen_pkt_hdr *h)
+{
+	struct ncsi_dev *nd = &ndp->ndev;
+	struct ncsi_aen_lsc_pkt *lsc;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+
+	/* 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->modes[NCSI_MODE_LINK];
+	lsc = (struct ncsi_aen_lsc_pkt *)h;
+	ncm->data[2] = ntohl(lsc->status);
+	ncm->data[4] = ntohl(lsc->oem_status);
+	if (!ndp->active_channel || ndp->active_channel != nc)
+		return 0;
+
+	ndp->flags |= NCSI_DEV_FLAG_RESHUFFLE;
+	ncsi_suspend_dev(nd);
+
+	return 0;
+}
+
+static int ncsi_aen_handler_cr(struct ncsi_dev_priv *ndp,
+			       struct ncsi_aen_pkt_hdr *h)
+{
+	struct ncsi_dev *nd = &ndp->ndev;
+	struct ncsi_channel *nc;
+
+	/* 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->active_channel || ndp->active_channel != nc)
+		return 0;
+
+	ncsi_config_dev(nd);
+
+	return 0;
+}
+
+static int ncsi_aen_handler_hncdsc(struct ncsi_dev_priv *ndp,
+				   struct ncsi_aen_pkt_hdr *h)
+{
+	struct ncsi_dev *nd = &ndp->ndev;
+	struct ncsi_channel *nc;
+	struct ncsi_channel_mode *ncm;
+	struct ncsi_aen_hncdsc_pkt *hncdsc;
+
+	/* 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->modes[NCSI_MODE_LINK];
+	hncdsc = (struct ncsi_aen_hncdsc_pkt *)h;
+	ncm->data[3] = ntohl(hncdsc->status);
+	if (ndp->active_channel != nc || 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->flags |= NCSI_DEV_FLAG_RESHUFFLE;
+	ncsi_suspend_dev(nd);
+	return 0;
+}
+
+static struct ncsi_aen_handler {
+	unsigned char type;
+	int           payload;
+	int           (*handler)(struct ncsi_dev_priv *ndp,
+				 struct ncsi_aen_pkt_hdr *h);
+} ncsi_aen_handlers[] = {
+	{ NCSI_PKT_AEN_LSC,    12, ncsi_aen_handler_lsc    },
+	{ NCSI_PKT_AEN_CR,      4, ncsi_aen_handler_cr     },
+	{ NCSI_PKT_AEN_HNCDSC,  4, ncsi_aen_handler_hncdsc },
+	{ 0,                    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->handler) {
+		if (nah->type == h->type)
+			break;
+
+		nah++;
+	}
+
+	if (!nah->handler) {
+		pr_warn("NCSI: Invalid AEN packet (0x%x) received\n",
+			h->type);
+		return -ENOENT;
+	}
+
+	ret = ncsi_validate_aen_pkt(h, nah->payload);
+	if (ret)
+		goto out;
+
+	ret = nah->handler(ndp, h);
+out:
+	consume_skb(skb);
+	return ret;
+}
diff --git a/net/ncsi/ncsi-pkt.h b/net/ncsi/ncsi-pkt.h
index 95882f3..6248835 100644
--- a/net/ncsi/ncsi-pkt.h
+++ b/net/ncsi/ncsi-pkt.h
@@ -31,6 +31,12 @@ struct ncsi_rsp_pkt_hdr {
 	__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 packet */
 struct ncsi_cmd_pkt {
 	struct ncsi_cmd_pkt_hdr cmd;      /* Command header */
@@ -282,6 +288,30 @@ struct ncsi_rsp_gnpts_pkt {
 	__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
 
@@ -356,4 +386,10 @@ struct ncsi_rsp_gnpts_pkt {
 #define NCSI_PKT_RSP_R_LENGTH		0x0005  /* Invalid payload length   */
 #define NCSI_PKT_RSP_R_UNKNOWN		0x7fff  /* Command type unsupported */
 
+/* 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 */
+
 #endif /* __NCSI_PKT_H__ */
diff --git a/net/ncsi/ncsi-rsp.c b/net/ncsi/ncsi-rsp.c
index 5b81217..09f79f8 100644
--- a/net/ncsi/ncsi-rsp.c
+++ b/net/ncsi/ncsi-rsp.c
@@ -946,8 +946,12 @@ int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
 	if (!ndp)
 		return -ENODEV;
 
-	/* Find the handler */
+	/* Check if it is 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->handler) {
 		if (nrh->type == hdr->type)
-- 
2.1.0

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

* [PATCH net-next 06/10] net/farady: Helper functions to create or destroy MDIO interface
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
                   ` (4 preceding siblings ...)
  2016-07-03  5:32 ` [PATCH net-next 05/10] net/ncsi: NCSI AEN packet handler Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 07/10] net/farady: Read MAC address from chip Gavin Shan
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

This introduces two helper functions to create or destroy MDIO
interface. No logical changes introduced except the proper MDIO
names are given when having more than one MDIO bus.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 drivers/net/ethernet/faraday/ftgmac100.c | 95 ++++++++++++++++++++------------
 1 file changed, 60 insertions(+), 35 deletions(-)

diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
index e7cf313..9b09493 100644
--- a/drivers/net/ethernet/faraday/ftgmac100.c
+++ b/drivers/net/ethernet/faraday/ftgmac100.c
@@ -1146,6 +1146,60 @@ static const struct net_device_ops ftgmac100_netdev_ops = {
 	.ndo_do_ioctl		= ftgmac100_do_ioctl,
 };
 
+static int ftgmac100_setup_mdio(struct net_device *netdev)
+{
+	struct ftgmac100 *priv = netdev_priv(netdev);
+	struct platform_device *pdev = to_platform_device(priv->dev);
+	int i, err = 0;
+
+	/* initialize mdio bus */
+	priv->mii_bus = mdiobus_alloc();
+	if (!priv->mii_bus)
+		return -EIO;
+
+	priv->mii_bus->name = "ftgmac100_mdio";
+	snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
+		 pdev->name, pdev->id);
+	priv->mii_bus->priv = priv->netdev;
+	priv->mii_bus->read = ftgmac100_mdiobus_read;
+	priv->mii_bus->write = ftgmac100_mdiobus_write;
+
+	for (i = 0; i < PHY_MAX_ADDR; i++)
+		priv->mii_bus->irq[i] = PHY_POLL;
+
+	err = mdiobus_register(priv->mii_bus);
+	if (err) {
+		dev_err(priv->dev, "Cannot register MDIO bus!\n");
+		goto err_register_mdiobus;
+	}
+
+	err = ftgmac100_mii_probe(priv);
+	if (err) {
+		dev_err(priv->dev, "MII Probe failed!\n");
+		goto err_mii_probe;
+	}
+
+	return 0;
+
+err_mii_probe:
+	mdiobus_unregister(priv->mii_bus);
+err_register_mdiobus:
+	mdiobus_free(priv->mii_bus);
+	return err;
+}
+
+static void ftgmac100_destroy_mdio(struct net_device *netdev)
+{
+	struct ftgmac100 *priv = netdev_priv(netdev);
+
+	if (!netdev->phydev)
+		return;
+
+	phy_disconnect(netdev->phydev);
+	mdiobus_unregister(priv->mii_bus);
+	mdiobus_free(priv->mii_bus);
+}
+
 /******************************************************************************
  * struct platform_driver functions
  *****************************************************************************/
@@ -1211,31 +1265,9 @@ static int ftgmac100_probe(struct platform_device *pdev)
 
 	priv->irq = irq;
 
-	/* initialize mdio bus */
-	priv->mii_bus = mdiobus_alloc();
-	if (!priv->mii_bus) {
-		err = -EIO;
-		goto err_alloc_mdiobus;
-	}
-
-	priv->mii_bus->name = "ftgmac100_mdio";
-	snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "ftgmac100_mii");
-
-	priv->mii_bus->priv = netdev;
-	priv->mii_bus->read = ftgmac100_mdiobus_read;
-	priv->mii_bus->write = ftgmac100_mdiobus_write;
-
-	err = mdiobus_register(priv->mii_bus);
-	if (err) {
-		dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
-		goto err_register_mdiobus;
-	}
-
-	err = ftgmac100_mii_probe(priv);
-	if (err) {
-		dev_err(&pdev->dev, "MII Probe failed!\n");
-		goto err_mii_probe;
-	}
+	err = ftgmac100_setup_mdio(netdev);
+	if (err)
+		goto err_setup_mdio;
 
 	/* register network device */
 	err = register_netdev(netdev);
@@ -1255,12 +1287,8 @@ static int ftgmac100_probe(struct platform_device *pdev)
 	return 0;
 
 err_register_netdev:
-	phy_disconnect(netdev->phydev);
-err_mii_probe:
-	mdiobus_unregister(priv->mii_bus);
-err_register_mdiobus:
-	mdiobus_free(priv->mii_bus);
-err_alloc_mdiobus:
+	ftgmac100_destroy_mdio(netdev);
+err_setup_mdio:
 	iounmap(priv->base);
 err_ioremap:
 	release_resource(priv->res);
@@ -1280,10 +1308,7 @@ static int __exit ftgmac100_remove(struct platform_device *pdev)
 	priv = netdev_priv(netdev);
 
 	unregister_netdev(netdev);
-
-	phy_disconnect(netdev->phydev);
-	mdiobus_unregister(priv->mii_bus);
-	mdiobus_free(priv->mii_bus);
+	ftgmac100_destroy_mdio(netdev);
 
 	iounmap(priv->base);
 	release_resource(priv->res);
-- 
2.1.0

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

* [PATCH net-next 07/10] net/farady: Read MAC address from chip
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
                   ` (5 preceding siblings ...)
  2016-07-03  5:32 ` [PATCH net-next 06/10] net/farady: Helper functions to create or destroy MDIO interface Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 08/10] net/farady: Support NCSI mode Gavin Shan
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

The device is assigned with random MAC address. It isn't reasonable.
An valid MAC address might have been provided by (uboot) firmware by
device-tree or in chip. It's reasonable to use it to maintain consistency.

This uses the MAC address from device-tree or that in the chip if it's
valid. Otherwise, a random MAC address is given as before.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 drivers/net/ethernet/faraday/ftgmac100.c | 69 ++++++++++++++++++++++++++++----
 1 file changed, 62 insertions(+), 7 deletions(-)

diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
index 9b09493..2c3f656 100644
--- a/drivers/net/ethernet/faraday/ftgmac100.c
+++ b/drivers/net/ethernet/faraday/ftgmac100.c
@@ -141,6 +141,64 @@ static void ftgmac100_set_mac(struct ftgmac100 *priv, const unsigned char *mac)
 	iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
 }
 
+static void ftgmac100_setup_mac(struct ftgmac100 *priv)
+{
+	u8 mac[ETH_ALEN];
+	unsigned int m;
+	unsigned int l;
+	void *addr;
+
+	addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
+	if (addr) {
+		ether_addr_copy(priv->netdev->dev_addr, mac);
+		dev_info(priv->dev, "Read MAC address %pM from device tree\n",
+			 mac);
+		return;
+	}
+
+	m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
+	l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
+
+	mac[0] = (m >> 8) & 0xff;
+	mac[1] = m & 0xff;
+	mac[2] = (l >> 24) & 0xff;
+	mac[3] = (l >> 16) & 0xff;
+	mac[4] = (l >> 8) & 0xff;
+	mac[5] = l & 0xff;
+
+	if (!is_valid_ether_addr(mac)) {
+		mac[5] = (m >> 8) & 0xff;
+		mac[4] = m & 0xff;
+		mac[3] = (l >> 24) & 0xff;
+		mac[2] = (l >> 16) & 0xff;
+		mac[1] = (l >>  8) & 0xff;
+		mac[0] = l & 0xff;
+	}
+
+	if (is_valid_ether_addr(mac)) {
+		ether_addr_copy(priv->netdev->dev_addr, mac);
+		dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
+	} else {
+		eth_hw_addr_random(priv->netdev);
+		dev_info(priv->dev, "Generated random MAC address %pM\n",
+			 priv->netdev->dev_addr);
+	}
+}
+
+static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
+{
+	int ret;
+
+	ret = eth_prepare_mac_addr_change(dev, p);
+	if (ret < 0)
+		return ret;
+
+	eth_commit_mac_addr_change(dev, p);
+	ftgmac100_set_mac(netdev_priv(dev), dev->dev_addr);
+
+	return 0;
+}
+
 static void ftgmac100_init_hw(struct ftgmac100 *priv)
 {
 	/* setup ring buffer base registers */
@@ -1141,7 +1199,7 @@ static const struct net_device_ops ftgmac100_netdev_ops = {
 	.ndo_open		= ftgmac100_open,
 	.ndo_stop		= ftgmac100_stop,
 	.ndo_start_xmit		= ftgmac100_hard_start_xmit,
-	.ndo_set_mac_address	= eth_mac_addr,
+	.ndo_set_mac_address	= ftgmac100_set_mac_addr,
 	.ndo_validate_addr	= eth_validate_addr,
 	.ndo_do_ioctl		= ftgmac100_do_ioctl,
 };
@@ -1265,6 +1323,9 @@ static int ftgmac100_probe(struct platform_device *pdev)
 
 	priv->irq = irq;
 
+	/* MAC address from chip or random one */
+	ftgmac100_setup_mac(priv);
+
 	err = ftgmac100_setup_mdio(netdev);
 	if (err)
 		goto err_setup_mdio;
@@ -1278,12 +1339,6 @@ static int ftgmac100_probe(struct platform_device *pdev)
 
 	netdev_info(netdev, "irq %d, mapped at %p\n", priv->irq, priv->base);
 
-	if (!is_valid_ether_addr(netdev->dev_addr)) {
-		eth_hw_addr_random(netdev);
-		netdev_info(netdev, "generated random MAC address %pM\n",
-			    netdev->dev_addr);
-	}
-
 	return 0;
 
 err_register_netdev:
-- 
2.1.0

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

* [PATCH net-next 08/10] net/farady: Support NCSI mode
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
                   ` (6 preceding siblings ...)
  2016-07-03  5:32 ` [PATCH net-next 07/10] net/farady: Read MAC address from chip Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 09/10] net/farady: Match driver according to compatible property Gavin Shan
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

This makes ftgmac100 driver support NCSI mode. The NCSI is enabled
on the interface if property "use-nc-si" or "use-ncsi" is found from
the device node in device tree.

   * No PHY device is used when NCSI mode is enabled.
   * The NCSI device (struct ncsi_dev) is created when probing the
     device while it's enabled/started when the interface is brought
     up.
   * Hardware IP checksum dosn't work when NCSI mode is enabled. It
     is disabled on enabled NCSI.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 drivers/net/ethernet/faraday/ftgmac100.c | 85 ++++++++++++++++++++++++++++----
 1 file changed, 75 insertions(+), 10 deletions(-)

diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
index 2c3f656..1cd4975 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,10 +69,13 @@ struct ftgmac100 {
 
 	struct net_device *netdev;
 	struct device *dev;
+	struct ncsi_dev *ndev;
 	struct napi_struct napi;
 
 	struct mii_bus *mii_bus;
 	int old_speed;
+	bool use_ncsi;
+	bool enabled;
 };
 
 static int ftgmac100_alloc_rx_page(struct ftgmac100 *priv,
@@ -1010,7 +1014,10 @@ 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 for
+	 * receiving or transmitting NCSI packets 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);
@@ -1123,17 +1130,33 @@ static int ftgmac100_open(struct net_device *netdev)
 		goto err_hw;
 
 	ftgmac100_init_hw(priv);
-	ftgmac100_start_hw(priv, 10);
-
-	phy_start(netdev->phydev);
+	ftgmac100_start_hw(priv, priv->use_ncsi ? 100 : 10);
+	if (netdev->phydev)
+		phy_start(netdev->phydev);
+	else if (priv->use_ncsi)
+		netif_carrier_on(netdev);
 
 	napi_enable(&priv->napi);
 	netif_start_queue(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;
+	}
+
+	priv->enabled = true;
+
 	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:
@@ -1146,12 +1169,17 @@ static int ftgmac100_stop(struct net_device *netdev)
 {
 	struct ftgmac100 *priv = netdev_priv(netdev);
 
+	if (!priv->enabled)
+		return 0;
+
 	/* disable all interrupts */
+	priv->enabled = false;
 	iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
 
 	netif_stop_queue(netdev);
 	napi_disable(&priv->napi);
-	phy_stop(netdev->phydev);
+	if (netdev->phydev)
+		phy_stop(netdev->phydev);
 
 	ftgmac100_stop_hw(priv);
 	free_irq(priv->irq, netdev);
@@ -1192,6 +1220,9 @@ static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
 /* optional */
 static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
 {
+	if (!netdev->phydev)
+		return -ENXIO;
+
 	return phy_mii_ioctl(netdev->phydev, ifr, cmd);
 }
 
@@ -1258,6 +1289,15 @@ static void ftgmac100_destroy_mdio(struct net_device *netdev)
 	mdiobus_free(priv->mii_bus);
 }
 
+static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
+{
+	if (unlikely(nd->state != ncsi_dev_state_functional))
+		return;
+
+	netdev_info(nd->dev, "NCSI interface %s\n",
+		    nd->link_up ? "up" : "down");
+}
+
 /******************************************************************************
  * struct platform_driver functions
  *****************************************************************************/
@@ -1267,7 +1307,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;
@@ -1291,7 +1331,6 @@ 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;
 
 	platform_set_drvdata(pdev, netdev);
 
@@ -1326,9 +1365,34 @@ static int ftgmac100_probe(struct platform_device *pdev)
 	/* MAC address from chip or random one */
 	ftgmac100_setup_mac(priv);
 
-	err = ftgmac100_setup_mdio(netdev);
-	if (err)
-		goto err_setup_mdio;
+	if (pdev->dev.of_node &&
+	    of_get_property(pdev->dev.of_node, "use-ncsi", NULL)) {
+		if (!IS_ENABLED(CONFIG_NET_NCSI)) {
+			dev_err(&pdev->dev, "NCSI stack not enabled\n");
+			goto err_ncsi_dev;
+		}
+
+		dev_info(&pdev->dev, "Using NCSI interface\n");
+		priv->use_ncsi = true;
+		priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
+		if (!priv->ndev)
+			goto err_ncsi_dev;
+	} else {
+		priv->use_ncsi = false;
+		err = ftgmac100_setup_mdio(netdev);
+		if (err)
+			goto err_setup_mdio;
+	}
+
+	/* We have to disable on-chip IP checksum functionality
+	 * when NCSI is enabled on the interface. It doesn't work
+	 * in that case.
+	 */
+	netdev->features = NETIF_F_IP_CSUM | NETIF_F_GRO;
+	if (priv->use_ncsi &&
+	    of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))
+		netdev->features &= ~NETIF_F_IP_CSUM;
+
 
 	/* register network device */
 	err = register_netdev(netdev);
@@ -1341,6 +1405,7 @@ static int ftgmac100_probe(struct platform_device *pdev)
 
 	return 0;
 
+err_ncsi_dev:
 err_register_netdev:
 	ftgmac100_destroy_mdio(netdev);
 err_setup_mdio:
-- 
2.1.0

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

* [PATCH net-next 09/10] net/farady: Match driver according to compatible property
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
                   ` (7 preceding siblings ...)
  2016-07-03  5:32 ` [PATCH net-next 08/10] net/farady: Support NCSI mode Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03  5:32 ` [PATCH net-next 10/10] net/farady: Mask PHY interrupt with NCSI mode Gavin Shan
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

This matches the driver with devices compatible with "faraday,ftgmac100"
declared in the device tree. Originally, device's name from device
tree for it.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 drivers/net/ethernet/faraday/ftgmac100.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
index 1cd4975..d8afa2d 100644
--- a/drivers/net/ethernet/faraday/ftgmac100.c
+++ b/drivers/net/ethernet/faraday/ftgmac100.c
@@ -1438,14 +1438,20 @@ static int __exit ftgmac100_remove(struct platform_device *pdev)
 	return 0;
 }
 
+static const struct of_device_id ftgmac100_of_match[] = {
+	{ .compatible = "faraday,ftgmac100" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
+
 static struct platform_driver ftgmac100_driver = {
-	.probe		= ftgmac100_probe,
-	.remove		= __exit_p(ftgmac100_remove),
-	.driver		= {
-		.name	= DRV_NAME,
+	.probe	= ftgmac100_probe,
+	.remove	= __exit_p(ftgmac100_remove),
+	.driver	= {
+		.name		= DRV_NAME,
+		.of_match_table	= ftgmac100_of_match,
 	},
 };
-
 module_platform_driver(ftgmac100_driver);
 
 MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
-- 
2.1.0

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

* [PATCH net-next 10/10] net/farady: Mask PHY interrupt with NCSI mode
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
                   ` (8 preceding siblings ...)
  2016-07-03  5:32 ` [PATCH net-next 09/10] net/farady: Match driver according to compatible property Gavin Shan
@ 2016-07-03  5:32 ` Gavin Shan
  2016-07-03 22:03 ` [PATCH net-next 00/10] NCSI Support Or Gerlitz
  2016-07-07 17:32 ` Florian Fainelli
  11 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-03  5:32 UTC (permalink / raw)
  To: netdev; +Cc: davem, benh, joel, weixue, Gavin Shan

Bogus PHY interrupts are observed. This masks the PHY interrupt
when the interface works in NCSI mode as there is no attached
PHY under the circumstance.

Signed-off-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
Acked-by: Joel Stanley <joel@jms.id.au>
---
 drivers/net/ethernet/faraday/ftgmac100.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/drivers/net/ethernet/faraday/ftgmac100.c b/drivers/net/ethernet/faraday/ftgmac100.c
index d8afa2d..2d4c7ea 100644
--- a/drivers/net/ethernet/faraday/ftgmac100.c
+++ b/drivers/net/ethernet/faraday/ftgmac100.c
@@ -74,6 +74,7 @@ struct ftgmac100 {
 
 	struct mii_bus *mii_bus;
 	int old_speed;
+	int int_mask_all;
 	bool use_ncsi;
 	bool enabled;
 };
@@ -84,14 +85,6 @@ static int ftgmac100_alloc_rx_page(struct ftgmac100 *priv,
 /******************************************************************************
  * internal functions (hardware register access)
  *****************************************************************************/
-#define INT_MASK_ALL_ENABLED	(FTGMAC100_INT_RPKT_LOST	| \
-				 FTGMAC100_INT_XPKT_ETH		| \
-				 FTGMAC100_INT_XPKT_LOST	| \
-				 FTGMAC100_INT_AHB_ERR		| \
-				 FTGMAC100_INT_PHYSTS_CHG	| \
-				 FTGMAC100_INT_RPKT_BUF		| \
-				 FTGMAC100_INT_NO_RXBUF)
-
 static void ftgmac100_set_rx_ring_base(struct ftgmac100 *priv, dma_addr_t addr)
 {
 	iowrite32(addr, priv->base + FTGMAC100_OFFSET_RXR_BADR);
@@ -1070,8 +1063,9 @@ static int ftgmac100_poll(struct napi_struct *napi, int budget)
 		ftgmac100_tx_complete(priv);
 	}
 
-	if (status & (FTGMAC100_INT_NO_RXBUF | FTGMAC100_INT_RPKT_LOST |
-		      FTGMAC100_INT_AHB_ERR | FTGMAC100_INT_PHYSTS_CHG)) {
+	if (status & priv->int_mask_all & (FTGMAC100_INT_NO_RXBUF |
+			FTGMAC100_INT_RPKT_LOST | FTGMAC100_INT_AHB_ERR |
+			FTGMAC100_INT_PHYSTS_CHG)) {
 		if (net_ratelimit())
 			netdev_info(netdev, "[ISR] = 0x%x: %s%s%s%s\n", status,
 				    status & FTGMAC100_INT_NO_RXBUF ? "NO_RXBUF " : "",
@@ -1094,7 +1088,8 @@ static int ftgmac100_poll(struct napi_struct *napi, int budget)
 		napi_complete(napi);
 
 		/* enable all interrupts */
-		iowrite32(INT_MASK_ALL_ENABLED, priv->base + FTGMAC100_OFFSET_IER);
+		iowrite32(priv->int_mask_all,
+			  priv->base + FTGMAC100_OFFSET_IER);
 	}
 
 	return rx;
@@ -1140,7 +1135,7 @@ static int ftgmac100_open(struct net_device *netdev)
 	netif_start_queue(netdev);
 
 	/* enable all interrupts */
-	iowrite32(INT_MASK_ALL_ENABLED, priv->base + FTGMAC100_OFFSET_IER);
+	iowrite32(priv->int_mask_all, priv->base + FTGMAC100_OFFSET_IER);
 
 	/* Start the NCSI device */
 	if (priv->use_ncsi) {
@@ -1365,6 +1360,13 @@ static int ftgmac100_probe(struct platform_device *pdev)
 	/* MAC address from chip or random one */
 	ftgmac100_setup_mac(priv);
 
+	priv->int_mask_all = (FTGMAC100_INT_RPKT_LOST |
+			      FTGMAC100_INT_XPKT_ETH |
+			      FTGMAC100_INT_XPKT_LOST |
+			      FTGMAC100_INT_AHB_ERR |
+			      FTGMAC100_INT_PHYSTS_CHG |
+			      FTGMAC100_INT_RPKT_BUF |
+			      FTGMAC100_INT_NO_RXBUF);
 	if (pdev->dev.of_node &&
 	    of_get_property(pdev->dev.of_node, "use-ncsi", NULL)) {
 		if (!IS_ENABLED(CONFIG_NET_NCSI)) {
@@ -1374,6 +1376,7 @@ static int ftgmac100_probe(struct platform_device *pdev)
 
 		dev_info(&pdev->dev, "Using NCSI interface\n");
 		priv->use_ncsi = true;
+		priv->int_mask_all &= ~FTGMAC100_INT_PHYSTS_CHG;
 		priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
 		if (!priv->ndev)
 			goto err_ncsi_dev;
-- 
2.1.0

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
                   ` (9 preceding siblings ...)
  2016-07-03  5:32 ` [PATCH net-next 10/10] net/farady: Mask PHY interrupt with NCSI mode Gavin Shan
@ 2016-07-03 22:03 ` Or Gerlitz
  2016-07-03 22:49   ` Benjamin Herrenschmidt
  2016-07-05 17:44   ` Alexei Starovoitov
  2016-07-07 17:32 ` Florian Fainelli
  11 siblings, 2 replies; 27+ messages in thread
From: Or Gerlitz @ 2016-07-03 22:03 UTC (permalink / raw)
  To: Gavin Shan; +Cc: Linux Netdev List, David Miller, benh, joel, weixue, yuvali

On Sun, Jul 3, 2016 at 8:32 AM, Gavin Shan <gwshan@linux.vnet.ibm.com> wrote:
> This series rebases on David's linux-net git repo ("master" branch). It's
> to support NCSI stack on net/farady/ftgmac100.c
>
> The following figure gives an example about how NCSI is deployed: The NCSI is
> specified by DSP0222, which can be downloaded from the following link here
> (http://www.dmtf.org/sites/default/files/standards/documents/DSP0222_1.0.0.pdf).
>
>    * The NC-SI (aka NCSI) is defined as the interface between a (Base) Management
>      Controller (BMC) and one or multiple Network Controlers (NC) on host side.
>      The interface is responsible for providing external network connectivity
>      for BMC.
>    * Each BMC can connect to multiple packages, up to 8. Each package can have
>      multiple channels, up to 32. Every package and channel are identified by
>      3-bits and 5-bits in NCSI packet. At one moment, one channel is active to
>      provide service.
>    * NCSI packet, encapsulated in ethernet frame, has 0x88F8 in the protocol
>      field. The destination MAC address should be 0xFF's while the source MAC
>      address can be arbitrary one.
>    * NCSI packets are classified to command, response, AEN (Asynchronous Event
>      Notification). Commands are sent from BMC to host for configuration and
>      information retrival. Responses, corresponding to commands, are sent from
>      host to BMC for confirmation and requested information. One command should
>      have one and only one response. AEN is sent from host to BMC for notification
>      (e.g. link down on active channel) so that BMC can take appropriate action.
>
>    +------------------+        +----------------------------------------------+
>    |                  |        |                     Host                     |
>    |        BMC       |        |                                              |
>    |                  |        | +-------------------+  +-------------------+ |
>    |    +---------+   |        | |     Package-A     |  |     Package-B     | |
>    |    |         |   |        | +---------+---------+  +-------------------+ |
>    |    |   NIC   |   |        | | Channel | Channel |  | Channel | Channel | |
>    +----+----+----+---+        +-+---------+---------+--+---------+---------+-+
>              |                             |                      |
>              |                             |                      |
>              +-----------------------------+----------------------+
>
> The design for the patchset is highlighted as below:
>
>    * The NCSI interface is abstracted with "struct ncsi_dev". It's registered
>      when net_device is created, started to work by calling ncsi_start_dev()
>      when net_device is opened (ndo_open()). For the first time, NCSI packets
>      are sent and received to/from the far end (host in above figure) to probe
>      available NCSI packages and channels. After that, one channel is chosen as
>      active one to provide service.
>    * The NCSI stack is driven by workqueue and state machine internally.
>    * AEN (Asychronous Event Notification) might be received from the far end
>      (host). The currently active NCSI channel fails over to another available
>      one if possible. Otherwise, the NCSI channel is out of service.
>    * NCSI stack should be configurable through netlink or another mechanism,
>      but it's not implemented in this patchset. It's something TBD.
>    * The first NIC driver that is aware of NCSI: drivers/net/ethernet/faraday/ftgmac100.c


FWIW, talking to a colleague, he made a comment that some of the text
above is wrong:

AENs are sent from NIC to BMC. Not from Host to BMC.

The traffic between a BMC and a NIC is over RBT if it is formatted as
NC-SI packets. This is not over network traffic....

Or.

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-03 22:03 ` [PATCH net-next 00/10] NCSI Support Or Gerlitz
@ 2016-07-03 22:49   ` Benjamin Herrenschmidt
  2016-07-04  0:24     ` Gavin Shan
  2016-07-05 17:44   ` Alexei Starovoitov
  1 sibling, 1 reply; 27+ messages in thread
From: Benjamin Herrenschmidt @ 2016-07-03 22:49 UTC (permalink / raw)
  To: Or Gerlitz, Gavin Shan
  Cc: Linux Netdev List, David Miller, joel, weixue, yuvali

On Mon, 2016-07-04 at 01:03 +0300, Or Gerlitz wrote:
> FWIW, talking to a colleague, he made a comment that some of the text
> above is wrong:
> 
> AENs are sent from NIC to BMC. Not from Host to BMC.

Just a typo in the commit message thankfully ;-)

> The traffic between a BMC and a NIC is over RBT if it is formatted as
> NC-SI packets. This is not over network traffic....

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-03 22:49   ` Benjamin Herrenschmidt
@ 2016-07-04  0:24     ` Gavin Shan
  0 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-04  0:24 UTC (permalink / raw)
  To: Benjamin Herrenschmidt
  Cc: Or Gerlitz, Gavin Shan, Linux Netdev List, David Miller, joel,
	weixue, yuvali

On Mon, Jul 04, 2016 at 08:49:31AM +1000, Benjamin Herrenschmidt wrote:
>On Mon, 2016-07-04 at 01:03 +0300, Or Gerlitz wrote:
>> FWIW, talking to a colleague, he made a comment that some of the text
>> above is wrong:
>> 
>> AENs are sent from NIC to BMC. Not from Host to BMC.
>
>Just a typo in the commit message thankfully ;-)
>

Yeah, the AENs are sent from NIC to BMC and the code follows it. The code
is tested on IBM's openPower server where a BMC (ARM system) and host (
Power system and NIC) are included. So the host, the far end and NIC can
be regarded as equivalents.

>> The traffic between a BMC and a NIC is over RBT if it is formatted as
>> NC-SI packets. This is not over network traffic....
>

It's correct. The figure included in the commit log might give the illusion
that NCSI packets are sent over network traffic.

Thanks,
Gavin

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-03 22:03 ` [PATCH net-next 00/10] NCSI Support Or Gerlitz
  2016-07-03 22:49   ` Benjamin Herrenschmidt
@ 2016-07-05 17:44   ` Alexei Starovoitov
  2016-07-05 21:42     ` Benjamin Herrenschmidt
  2016-07-07  9:12     ` Or Gerlitz
  1 sibling, 2 replies; 27+ messages in thread
From: Alexei Starovoitov @ 2016-07-05 17:44 UTC (permalink / raw)
  To: Or Gerlitz
  Cc: Gavin Shan, Linux Netdev List, David Miller, benh, joel, weixue, yuvali

On Mon, Jul 04, 2016 at 01:03:06AM +0300, Or Gerlitz wrote:
> On Sun, Jul 3, 2016 at 8:32 AM, Gavin Shan <gwshan@linux.vnet.ibm.com> wrote:
> > This series rebases on David's linux-net git repo ("master" branch). It's
> > to support NCSI stack on net/farady/ftgmac100.c
> >
> > The following figure gives an example about how NCSI is deployed: The NCSI is
> > specified by DSP0222, which can be downloaded from the following link here
> > (http://www.dmtf.org/sites/default/files/standards/documents/DSP0222_1.0.0.pdf).
> >
> >    * The NC-SI (aka NCSI) is defined as the interface between a (Base) Management
> >      Controller (BMC) and one or multiple Network Controlers (NC) on host side.
> >      The interface is responsible for providing external network connectivity
> >      for BMC.
> >    * Each BMC can connect to multiple packages, up to 8. Each package can have
> >      multiple channels, up to 32. Every package and channel are identified by
> >      3-bits and 5-bits in NCSI packet. At one moment, one channel is active to
> >      provide service.
> >    * NCSI packet, encapsulated in ethernet frame, has 0x88F8 in the protocol
> >      field. The destination MAC address should be 0xFF's while the source MAC
> >      address can be arbitrary one.
> >    * NCSI packets are classified to command, response, AEN (Asynchronous Event
> >      Notification). Commands are sent from BMC to host for configuration and
> >      information retrival. Responses, corresponding to commands, are sent from
> >      host to BMC for confirmation and requested information. One command should
> >      have one and only one response. AEN is sent from host to BMC for notification
> >      (e.g. link down on active channel) so that BMC can take appropriate action.
> >
> >    +------------------+        +----------------------------------------------+
> >    |                  |        |                     Host                     |
> >    |        BMC       |        |                                              |
> >    |                  |        | +-------------------+  +-------------------+ |
> >    |    +---------+   |        | |     Package-A     |  |     Package-B     | |
> >    |    |         |   |        | +---------+---------+  +-------------------+ |
> >    |    |   NIC   |   |        | | Channel | Channel |  | Channel | Channel | |
> >    +----+----+----+---+        +-+---------+---------+--+---------+---------+-+
> >              |                             |                      |
> >              |                             |                      |
> >              +-----------------------------+----------------------+
> >
> > The design for the patchset is highlighted as below:
> >
> >    * The NCSI interface is abstracted with "struct ncsi_dev". It's registered
> >      when net_device is created, started to work by calling ncsi_start_dev()
> >      when net_device is opened (ndo_open()). For the first time, NCSI packets
> >      are sent and received to/from the far end (host in above figure) to probe
> >      available NCSI packages and channels. After that, one channel is chosen as
> >      active one to provide service.
> >    * The NCSI stack is driven by workqueue and state machine internally.
> >    * AEN (Asychronous Event Notification) might be received from the far end
> >      (host). The currently active NCSI channel fails over to another available
> >      one if possible. Otherwise, the NCSI channel is out of service.
> >    * NCSI stack should be configurable through netlink or another mechanism,
> >      but it's not implemented in this patchset. It's something TBD.

Gavin,
what configurations do you have in mind?
For ncsi itself or to control the nic on the host?
This set of patches is for BMC side, right?
What needs to be done on the host?

> >    * The first NIC driver that is aware of NCSI: drivers/net/ethernet/faraday/ftgmac100.c
> 
> 
> FWIW, talking to a colleague, he made a comment that some of the text
> above is wrong:
> 
> AENs are sent from NIC to BMC. Not from Host to BMC.
> 
> The traffic between a BMC and a NIC is over RBT if it is formatted as
> NC-SI packets. This is not over network traffic....
> 
> Or.

Or,
since cx4 has ncsi as well, could you do a thorough review of this
to make sure that it fits mellanox nics as well?

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-05 17:44   ` Alexei Starovoitov
@ 2016-07-05 21:42     ` Benjamin Herrenschmidt
  2016-07-06  2:07       ` Alexei Starovoitov
  2016-07-07  9:12     ` Or Gerlitz
  1 sibling, 1 reply; 27+ messages in thread
From: Benjamin Herrenschmidt @ 2016-07-05 21:42 UTC (permalink / raw)
  To: Alexei Starovoitov, Or Gerlitz
  Cc: Gavin Shan, Linux Netdev List, David Miller, joel, weixue, yuvali

On Tue, 2016-07-05 at 10:44 -0700, Alexei Starovoitov wrote:
> 

 .../...

> > > The design for the patchset is highlighted as below:
> > > 
> > >    * The NCSI interface is abstracted with "struct ncsi_dev". It's registered
> > >      when net_device is created, started to work by calling ncsi_start_dev()
> > >      when net_device is opened (ndo_open()). For the first time, NCSI packets
> > >      are sent and received to/from the far end (host in above figure) to probe
> > >      available NCSI packages and channels. After that, one channel is chosen as
> > >      active one to provide service.
> > >    * The NCSI stack is driven by workqueue and state machine internally.
> > >    * AEN (Asychronous Event Notification) might be received from the far end
> > >      (host). The currently active NCSI channel fails over to another available
> > >      one if possible. Otherwise, the NCSI channel is out of service.
> > >    * NCSI stack should be configurable through netlink or another mechanism,
> > >      but it's not implemented in this patchset. It's something TBD.
> 
> Gavin,
> what configurations do you have in mind?
> For ncsi itself or to control the nic on the host?
> This set of patches is for BMC side, right?
> What needs to be done on the host?

I'll respond for Gavin since I'm awake first ;-)

We use that stack today on OpenBMC on some OpenPOWER machines.

The configuration is thus for the above stack to run on the BMC in
order to control the host NIC.

NC-SI capable host NICs operate autonomously, so there is nothing to be
done on the host OS itself, at least not with the BCM NICs that we use
today, but of course the host NIC firmware needs to have the other side
of the stack.

> > >    * The first NIC driver that is aware of NCSI: drivers/net/ethernet/faraday/ftgmac100.c
> > 
> > 
> > FWIW, talking to a colleague, he made a comment that some of the text
> > above is wrong:
> > 
> > AENs are sent from NIC to BMC. Not from Host to BMC.
> > 
> > The traffic between a BMC and a NIC is over RBT if it is formatted as
> > NC-SI packets. This is not over network traffic....
> > 
> > Or.
> 
> Or,
> since cx4 has ncsi as well, could you do a thorough review of this
> to make sure that it fits mellanox nics as well?

Cheers,
Ben.

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-05 21:42     ` Benjamin Herrenschmidt
@ 2016-07-06  2:07       ` Alexei Starovoitov
  2016-07-06  2:14         ` Benjamin Herrenschmidt
  0 siblings, 1 reply; 27+ messages in thread
From: Alexei Starovoitov @ 2016-07-06  2:07 UTC (permalink / raw)
  To: Benjamin Herrenschmidt
  Cc: Or Gerlitz, Gavin Shan, Linux Netdev List, David Miller, joel,
	weixue, yuvali

On Wed, Jul 06, 2016 at 07:42:39AM +1000, Benjamin Herrenschmidt wrote:
> On Tue, 2016-07-05 at 10:44 -0700, Alexei Starovoitov wrote:
> > 
> 
>  .../...
> 
> > > > The design for the patchset is highlighted as below:
> > > > 
> > > >    * The NCSI interface is abstracted with "struct ncsi_dev". It's registered
> > > >      when net_device is created, started to work by calling ncsi_start_dev()
> > > >      when net_device is opened (ndo_open()). For the first time, NCSI packets
> > > >      are sent and received to/from the far end (host in above figure) to probe
> > > >      available NCSI packages and channels. After that, one channel is chosen as
> > > >      active one to provide service.
> > > >    * The NCSI stack is driven by workqueue and state machine internally.
> > > >    * AEN (Asychronous Event Notification) might be received from the far end
> > > >      (host). The currently active NCSI channel fails over to another available
> > > >      one if possible. Otherwise, the NCSI channel is out of service.
> > > >    * NCSI stack should be configurable through netlink or another mechanism,
> > > >      but it's not implemented in this patchset. It's something TBD.
> > 
> > Gavin,
> > what configurations do you have in mind?
> > For ncsi itself or to control the nic on the host?
> > This set of patches is for BMC side, right?
> > What needs to be done on the host?
> 
> I'll respond for Gavin since I'm awake first ;-)
> 
> We use that stack today on OpenBMC on some OpenPOWER machines.
> 
> The configuration is thus for the above stack to run on the BMC in
> order to control the host NIC.
> 
> NC-SI capable host NICs operate autonomously, so there is nothing to be
> done on the host OS itself, at least not with the BCM NICs that we use
> today, but of course the host NIC firmware needs to have the other side
> of the stack.

Great! Thanks for clarifying.
So then future netlink api is mandatory to drive this kernel patches?
How one can use this set without it?
What is the main reason for this infra to be in the kernel instead of
userspace raw socket? Some interaction with the driver, right?
but it's not obvious from the patches.

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-06  2:07       ` Alexei Starovoitov
@ 2016-07-06  2:14         ` Benjamin Herrenschmidt
  0 siblings, 0 replies; 27+ messages in thread
From: Benjamin Herrenschmidt @ 2016-07-06  2:14 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Or Gerlitz, Gavin Shan, Linux Netdev List, David Miller, joel,
	weixue, yuvali

On Tue, 2016-07-05 at 19:07 -0700, Alexei Starovoitov wrote:
> 
> Great! Thanks for clarifying.
> So then future netlink api is mandatory to drive this kernel patches?
> How one can use this set without it?

The netlink API is to tweak things, it works reasonably well
autonomously without it.

> What is the main reason for this infra to be in the kernel instead of
> userspace raw socket? Some interaction with the driver, right?
> but it's not obvious from the patches.

There are a few reasons. One it means we can use kernel level
autoconfiguration like DHCP and NFS root which are quite handy when
developing BMC stacks :-)

Another one is that we haven't completely given up on reflecting the
state of the remote NC-SI link into the "carrier status" of the local
interface.

We can't yet do it because the link monitor would stop the driver
queues, but we could possibly invent a flag we set on the device that
prevents this from happening and causes the queues to remain up even
when the link appears down.

This will be useful as some BMCs have multiple NICs that can all do
NC-SI and thus we could have automatic fail over.

Cheers,
Ben.

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-05 17:44   ` Alexei Starovoitov
  2016-07-05 21:42     ` Benjamin Herrenschmidt
@ 2016-07-07  9:12     ` Or Gerlitz
  2016-07-07  9:17       ` Benjamin Herrenschmidt
  1 sibling, 1 reply; 27+ messages in thread
From: Or Gerlitz @ 2016-07-07  9:12 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Gavin Shan, Linux Netdev List, David Miller, benh, joel, weixue,
	Yuval Itkin

On Tue, Jul 5, 2016 at 8:44 PM, Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
> On Mon, Jul 04, 2016 at 01:03:06AM +0300, Or Gerlitz wrote:

> Or, since cx4 has ncsi as well, could you do a thorough review of this
> to make sure that it fits mellanox nics as well?

Hi Alexei, all

Yuval from our team who deals with host management did review on the
series, SB his feedback.

1. The initialization uses a single unicast MAC address which hints it
assumes that the management traffic is IPv4 only. The infrastructure
does not seem to be ready for IPv6 based management traffic.

2. The code enables AEN messages (all the 3 messages defined by the
standard are enabled). AEN support mandates readiness on the BMC side
(running the reviewed code) and mandates support on the NIC. Not every
NIC can create AEN messages therefore I would recommend that the code
will only enable AENs which are declared as supported. To query
support for AEN messages, the BMC shall interpret the response for
"Get Capabilities" command and based on the returned response it can
detect which AEN message is supported by the NIC and may be enabled.

3. When stating support for NC-SI it is important to indicate which
exact version of NC-SI is supported (or required). From my observation
this code is based on NC-SI 1.0.0 as it does not use any of the
features which were introduced in the later versions.

4. HW arbitration is disabled in this implementation. It should be
defined how to add support to system which supports HW arbitration.

5. The code has an initialization sequence which is clear. Yet there
shall be a re-initialization sequence needed if a device goes through
asynchronous entry to initial state, which may happen if the NIC goes
through reset for any reason.

Or.

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-07  9:12     ` Or Gerlitz
@ 2016-07-07  9:17       ` Benjamin Herrenschmidt
  2016-07-07  9:18         ` Benjamin Herrenschmidt
                           ` (2 more replies)
  0 siblings, 3 replies; 27+ messages in thread
From: Benjamin Herrenschmidt @ 2016-07-07  9:17 UTC (permalink / raw)
  To: Or Gerlitz, Alexei Starovoitov
  Cc: Gavin Shan, Linux Netdev List, David Miller, joel, weixue, Yuval Itkin

On Thu, 2016-07-07 at 12:12 +0300, Or Gerlitz wrote:
> On Tue, Jul 5, 2016 at 8:44 PM, Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> > On Mon, Jul 04, 2016 at 01:03:06AM +0300, Or Gerlitz wrote:
> 
> > Or, since cx4 has ncsi as well, could you do a thorough review of this
> > to make sure that it fits mellanox nics as well?
> 
> Hi Alexei, all
> 
> Yuval from our team who deals with host management did review on the
> series, SB his feedback.
> 
> 1. The initialization uses a single unicast MAC address which hints it
> assumes that the management traffic is IPv4 only. The infrastructure
> does not seem to be ready for IPv6 based management traffic.

You mean the transfer of the MAC address from the BMC to the NIC for
filtering incoming traffic ?

> 2. The code enables AEN messages (all the 3 messages defined by the
> standard are enabled). AEN support mandates readiness on the BMC side
> (running the reviewed code) and mandates support on the NIC. Not every
> NIC can create AEN messages therefore I would recommend that the code
> will only enable AENs which are declared as supported. To query
> support for AEN messages, the BMC shall interpret the response for
> "Get Capabilities" command and based on the returned response it can
> detect which AEN message is supported by the NIC and may be enabled.

Ok.

> 3. When stating support for NC-SI it is important to indicate which
> exact version of NC-SI is supported (or required). From my observation
> this code is based on NC-SI 1.0.0 as it does not use any of the
> features which were introduced in the later versions.

Probably correct, 1.0.1 I think is the only one that was available when
we started that work. I found 1.1.0 on the DMTF web site, are you aware
of anything more recent ?

Gavin, you can get it here:

 https://www.dmtf.org/sites/default/files/standards/documents/DSP0222_1.1.0.pdf

> 4. HW arbitration is disabled in this implementation. It should be
> defined how to add support to system which supports HW arbitration.
> 
> 5. The code has an initialization sequence which is clear. Yet there
> shall be a re-initialization sequence needed if a device goes through
> asynchronous entry to initial state, which may happen if the NIC goes
> through reset for any reason.

Ok.

Gavin: We can discuss these tomorrow if you need.

Cheers,
Ben.

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-07  9:17       ` Benjamin Herrenschmidt
@ 2016-07-07  9:18         ` Benjamin Herrenschmidt
  2016-07-07 13:05         ` Gavin Shan
  2016-07-07 13:44         ` Or Gerlitz
  2 siblings, 0 replies; 27+ messages in thread
From: Benjamin Herrenschmidt @ 2016-07-07  9:18 UTC (permalink / raw)
  To: Or Gerlitz, Alexei Starovoitov
  Cc: Gavin Shan, Linux Netdev List, David Miller, joel, weixue, Yuval Itkin

On Thu, 2016-07-07 at 19:17 +1000, Benjamin Herrenschmidt wrote:
> On Thu, 2016-07-07 at 12:12 +0300, Or Gerlitz wrote:
> > On Tue, Jul 5, 2016 at 8:44 PM, Alexei Starovoitov
> > <alexei.starovoitov@gmail.com> wrote:
> > > On Mon, Jul 04, 2016 at 01:03:06AM +0300, Or Gerlitz wrote:
> > 
> > > Or, since cx4 has ncsi as well, could you do a thorough review of
> > > this
> > > to make sure that it fits mellanox nics as well?
> > 
> > Hi Alexei, all
> > 
> > Yuval from our team who deals with host management did review on
> > the
> > series, SB his feedback.

Oh and thanks a lot for your review !

Cheers,
Ben.

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-07  9:17       ` Benjamin Herrenschmidt
  2016-07-07  9:18         ` Benjamin Herrenschmidt
@ 2016-07-07 13:05         ` Gavin Shan
  2016-07-07 13:44         ` Or Gerlitz
  2 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-07 13:05 UTC (permalink / raw)
  To: Benjamin Herrenschmidt
  Cc: Or Gerlitz, Alexei Starovoitov, Gavin Shan, Linux Netdev List,
	David Miller, joel, weixue, Yuval Itkin

On Thu, Jul 07, 2016 at 07:17:36PM +1000, Benjamin Herrenschmidt wrote:
>On Thu, 2016-07-07 at 12:12 +0300, Or Gerlitz wrote:
>> On Tue, Jul 5, 2016 at 8:44 PM, Alexei Starovoitov
>> <alexei.starovoitov@gmail.com> wrote:
>> > On Mon, Jul 04, 2016 at 01:03:06AM +0300, Or Gerlitz wrote:
>> 
>> > Or, since cx4 has ncsi as well, could you do a thorough review of this
>> > to make sure that it fits mellanox nics as well?
>> 
>> Hi Alexei, all
>> 
>> Yuval from our team who deals with host management did review on the
>> series, SB his feedback.
>> 

Thanks for the review and the comments.

>> 1. The initialization uses a single unicast MAC address which hints it
>> assumes that the management traffic is IPv4 only. The infrastructure
>> does not seem to be ready for IPv6 based management traffic.
>
>You mean the transfer of the MAC address from the BMC to the NIC for
>filtering incoming traffic ?
>

I think it's about global multicast filter which is used for IPv6 to
probe next neighbhour or hop. I will add it according to the retrieved
channel's capability in next revision.

>> 2. The code enables AEN messages (all the 3 messages defined by the
>> standard are enabled). AEN support mandates readiness on the BMC side
>> (running the reviewed code) and mandates support on the NIC. Not every
>> NIC can create AEN messages therefore I would recommend that the code
>> will only enable AENs which are declared as supported. To query
>> support for AEN messages, the BMC shall interpret the response for
>> "Get Capabilities" command and based on the returned response it can
>> detect which AEN message is supported by the NIC and may be enabled.
>
>Ok.
>

Yes, It makes sense. I will enable AEN messages according to channel's
capability in next revision.

>> 3. When stating support for NC-SI it is important to indicate which
>> exact version of NC-SI is supported (or required). From my observation
>> this code is based on NC-SI 1.0.0 as it does not use any of the
>> features which were introduced in the later versions.
>
>Probably correct, 1.0.1 I think is the only one that was available when
>we started that work. I found 1.1.0 on the DMTF web site, are you aware
>of anything more recent ?
>
>Gavin, you can get it here:
>
> https://www.dmtf.org/sites/default/files/standards/documents/DSP0222_1.1.0.pdf
>

Yeah, the code was implemented based on 1.0.1 specification as Ben said.
I go through 1.1.0 specification quickly. On page 65, there are more
optional commands added as below. I will define them in ncsi-pkt.h and
I don't think I need use any of them for now. Note the revision field
in NCSI packet header is still 0x1, same to the one in the code.

   Command Number           Description
   0x1B                     Get Package Status
   0x51                     PLDM
   0x52                     Get Package UUID

>> 4. HW arbitration is disabled in this implementation. It should be
>> defined how to add support to system which supports HW arbitration.
>> 

It's good point. Yes, HW arbitration is disabled in select package command
in current implementation. It can be enabled only when all available packages
and channels claim the capability in the get capability response. Currently,
there are two fields ("active_package" and "active_channel") in NCSI interface
(struct ncsi_dev_priv). I can reuse them to distinguish those cases (enabled
and disable HW arbitration):

   Enabled: active_package = first_available_package, active_channel = NULL;
   Disable: active_package = package_in_work,         active_channel = channel_in_work;

It affects the behaviour how AENs are processed. I don't think we need enable AEN
messages any more if I'm correct enough here.

>> 5. The code has an initialization sequence which is clear. Yet there
>> shall be a re-initialization sequence needed if a device goes through
>> asynchronous entry to initial state, which may happen if the NIC goes
>> through reset for any reason.
>
>Ok.
>

Yes, It makes sense as AEN isn't mandatory function supported by NIC. I will
change the code accordingly in next revision.

>Gavin: We can discuss these tomorrow if you need.
>

Thanks, Ben. I will talk to you tomorrow when you have free time. Thank you
very much for kindly helps on this.

Thanks,
Gavin

>Cheers,
>Ben.
>

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-07  9:17       ` Benjamin Herrenschmidt
  2016-07-07  9:18         ` Benjamin Herrenschmidt
  2016-07-07 13:05         ` Gavin Shan
@ 2016-07-07 13:44         ` Or Gerlitz
  2016-07-07 16:34           ` Gavin Shan
  2 siblings, 1 reply; 27+ messages in thread
From: Or Gerlitz @ 2016-07-07 13:44 UTC (permalink / raw)
  To: Benjamin Herrenschmidt
  Cc: Alexei Starovoitov, Gavin Shan, Linux Netdev List, David Miller,
	joel, weixue, Yuval Itkin

On Thu, Jul 7, 2016 at 12:17 PM, Benjamin Herrenschmidt
<benh@kernel.crashing.org> wrote:
> On Thu, 2016-07-07 at 12:12 +0300, Or Gerlitz wrote:
>> On Tue, Jul 5, 2016 at 8:44 PM, Alexei Starovoitov
>> <alexei.starovoitov@gmail.com> wrote:
>> > On Mon, Jul 04, 2016 at 01:03:06AM +0300, Or Gerlitz wrote:
>>
>> > Or, since cx4 has ncsi as well, could you do a thorough review of this
>> > to make sure that it fits mellanox nics as well?
>>
>> Hi Alexei, all
>>
>> Yuval from our team who deals with host management did review on the
>> series, SB his feedback.
>>
>> 1. The initialization uses a single unicast MAC address which hints it
>> assumes that the management traffic is IPv4 only. The infrastructure
>> does not seem to be ready for IPv6 based management traffic.
>
> You mean the transfer of the MAC address from the BMC to the NIC for
> filtering incoming traffic ?

The initialization sequence uses "Set MAC Command" once and
initializes MAC[1] in the chosen NC-SI pass-through channel.
If IPv6 is used, then the BMC multicast address (derived from the
assigned IP address to the BMC) shall also be configured (after the IP
address is assigned), and some multicast filtering shall be set as
well.

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-07 13:44         ` Or Gerlitz
@ 2016-07-07 16:34           ` Gavin Shan
  0 siblings, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-07 16:34 UTC (permalink / raw)
  To: Or Gerlitz
  Cc: Benjamin Herrenschmidt, Alexei Starovoitov, Gavin Shan,
	Linux Netdev List, David Miller, joel, weixue, Yuval Itkin

On Thu, Jul 07, 2016 at 04:44:02PM +0300, Or Gerlitz wrote:
>On Thu, Jul 7, 2016 at 12:17 PM, Benjamin Herrenschmidt
><benh@kernel.crashing.org> wrote:
>> On Thu, 2016-07-07 at 12:12 +0300, Or Gerlitz wrote:
>>> On Tue, Jul 5, 2016 at 8:44 PM, Alexei Starovoitov
>>> <alexei.starovoitov@gmail.com> wrote:
>>> > On Mon, Jul 04, 2016 at 01:03:06AM +0300, Or Gerlitz wrote:
>>>
>>> > Or, since cx4 has ncsi as well, could you do a thorough review of this
>>> > to make sure that it fits mellanox nics as well?
>>>
>>> Hi Alexei, all
>>>
>>> Yuval from our team who deals with host management did review on the
>>> series, SB his feedback.
>>>
>>> 1. The initialization uses a single unicast MAC address which hints it
>>> assumes that the management traffic is IPv4 only. The infrastructure
>>> does not seem to be ready for IPv6 based management traffic.
>>
>> You mean the transfer of the MAC address from the BMC to the NIC for
>> filtering incoming traffic ?
>
>The initialization sequence uses "Set MAC Command" once and
>initializes MAC[1] in the chosen NC-SI pass-through channel.
>If IPv6 is used, then the BMC multicast address (derived from the
>assigned IP address to the BMC) shall also be configured (after the IP
>address is assigned), and some multicast filtering shall be set as
>well.
>

Or, Thanks for the explanation. I think they are the multicast
MAC addresses (33:33:00:00:00:01/02/03)? I guess sending a Global
Multicast Filter Command is enough according to the NCSI spec
(line 2062 on page 93):

The Enable Global Multicast Filter command is used to activate global
filtering of multicast frames with optional filtering of specific multicast
protocols. Upon receiving and processing this command, the channel shall only
deliver multicast frames that match specific multicast MAC addresses enabled
for Pass through using this command ___or___ the Set MAC Address command.

Thanks,
Gavin

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
                   ` (10 preceding siblings ...)
  2016-07-03 22:03 ` [PATCH net-next 00/10] NCSI Support Or Gerlitz
@ 2016-07-07 17:32 ` Florian Fainelli
  2016-07-07 22:05   ` Benjamin Herrenschmidt
  2016-07-08  1:10   ` Gavin Shan
  11 siblings, 2 replies; 27+ messages in thread
From: Florian Fainelli @ 2016-07-07 17:32 UTC (permalink / raw)
  To: Gavin Shan, netdev; +Cc: davem, benh, joel, weixue

On 07/02/2016 10:32 PM, Gavin Shan wrote:
> This series rebases on David's linux-net git repo ("master" branch). It's
> to support NCSI stack on net/farady/ftgmac100.c
> 
> The following figure gives an example about how NCSI is deployed: The NCSI is
> specified by DSP0222, which can be downloaded from the following link here
> (http://www.dmtf.org/sites/default/files/standards/documents/DSP0222_1.0.0.pdf).
> 
>    * The NC-SI (aka NCSI) is defined as the interface between a (Base) Management
>      Controller (BMC) and one or multiple Network Controlers (NC) on host side.
>      The interface is responsible for providing external network connectivity
>      for BMC.
>    * Each BMC can connect to multiple packages, up to 8. Each package can have
>      multiple channels, up to 32. Every package and channel are identified by
>      3-bits and 5-bits in NCSI packet. At one moment, one channel is active to
>      provide service.
>    * NCSI packet, encapsulated in ethernet frame, has 0x88F8 in the protocol
>      field. The destination MAC address should be 0xFF's while the source MAC
>      address can be arbitrary one.
>    * NCSI packets are classified to command, response, AEN (Asynchronous Event
>      Notification). Commands are sent from BMC to host for configuration and
>      information retrival. Responses, corresponding to commands, are sent from
>      host to BMC for confirmation and requested information. One command should
>      have one and only one response. AEN is sent from host to BMC for notification
>      (e.g. link down on active channel) so that BMC can take appropriate action.
> 
>    +------------------+        +----------------------------------------------+
>    |                  |        |                     Host                     |
>    |        BMC       |        |                                              |
>    |                  |        | +-------------------+  +-------------------+ |
>    |    +---------+   |        | |     Package-A     |  |     Package-B     | |
>    |    |         |   |        | +---------+---------+  +-------------------+ |
>    |    |   NIC   |   |        | | Channel | Channel |  | Channel | Channel | |
>    +----+----+----+---+        +-+---------+---------+--+---------+---------+-+
>              |                             |                      |
>              |                             |                      |
>              +-----------------------------+----------------------+
> 
> The design for the patchset is highlighted as below:
> 
>    * The NCSI interface is abstracted with "struct ncsi_dev". It's registered
>      when net_device is created, started to work by calling ncsi_start_dev()
>      when net_device is opened (ndo_open()). For the first time, NCSI packets
>      are sent and received to/from the far end (host in above figure) to probe
>      available NCSI packages and channels. After that, one channel is chosen as
>      active one to provide service.
>    * The NCSI stack is driven by workqueue and state machine internally.
>    * AEN (Asychronous Event Notification) might be received from the far end
>      (host). The currently active NCSI channel fails over to another available
>      one if possible. Otherwise, the NCSI channel is out of service.
>    * NCSI stack should be configurable through netlink or another mechanism,
>      but it's not implemented in this patchset. It's something TBD.
>    * The first NIC driver that is aware of NCSI: drivers/net/ethernet/faraday/ftgmac100.c

I know nothing about NCSI, pretty much like Jon Snow, but from a cursory
look at your patches, is not there a way to make the NCSCI capable
network devices strictly adhere to the net_device APIs and calling
conventions?

Even if the data flow is a little different than normal ethernet frames,
and there is not a good way to trap to intercept the delivery of NCSI
packets, one could imagine doing something ala DSA where you register a
fake ethertype for NCSI to hook a ptype_fun packet handler, augment
struct net_device with a ncsi_dev pointer, and do processing in
net/nsci/ for this device you know where the packet came from. You don't
need to have an officially assigned ethertype for this, see
netdev_uses_dsa() which just tests whether the traffic is tagged with a
particular tag and delivers packet to a protocol specific parser in
net/dsa/.

For packets on their way out you could imagine assigning them a specific
skb->protocol value and have the driver's transmit path do specific
things based on that.

Just an idea, I am not even sure this makes sense here, but what seems
to make sens to me is that if more network device drivers end up
supporting and transporting NCSI, we barely want them to know about that.
-- 
Florian

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-07 17:32 ` Florian Fainelli
@ 2016-07-07 22:05   ` Benjamin Herrenschmidt
  2016-07-08  1:10   ` Gavin Shan
  1 sibling, 0 replies; 27+ messages in thread
From: Benjamin Herrenschmidt @ 2016-07-07 22:05 UTC (permalink / raw)
  To: Florian Fainelli, Gavin Shan, netdev; +Cc: davem, joel, weixue

On Thu, 2016-07-07 at 10:32 -0700, Florian Fainelli wrote:
> 
> I know nothing about NCSI, pretty much like Jon Snow, but from a cursory
> look at your patches, is not there a way to make the NCSCI capable
> network devices strictly adhere to the net_device APIs and calling
> conventions?

They already do.

Basically imagine NC-SI being a replacement for the PHY of the device.

Instead of having a PHY, your MII is directly connected to another NIC
(the main system NIC) which conveniently provide that sideband MII
interface for you.

You then use the NC-SI protocol which uses a specific ethernet packet
type to communicate with that other NIC as you would otherwise communicate
with a PHY using MDIO. At least conceptually. For config. Normal traffic
is still normal ethernet.

The protocol is more complex than MDIO though, you can be connected to
multiple NICs, you have to provide your MAC address to the other guy so
he can route some incoming traffic to you etc...

So in the current implementation by Gavin, all that is needed for an
existing NIC driver to support NC-SI is a few lines of code to register
with the stack, which will under the hood create the ncsi_dev associated
with the net_dev and manage the protocol.

>From a "user" point of view you still have a normal netdev doing normal
ethernet, the NC-SI protocol is transparent and strictly used to maintain
the link active between you and the other NIC thorugh which your traffic
goes.

Cheers,
Ben.

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

* Re: [PATCH net-next 00/10] NCSI Support
  2016-07-07 17:32 ` Florian Fainelli
  2016-07-07 22:05   ` Benjamin Herrenschmidt
@ 2016-07-08  1:10   ` Gavin Shan
  1 sibling, 0 replies; 27+ messages in thread
From: Gavin Shan @ 2016-07-08  1:10 UTC (permalink / raw)
  To: Florian Fainelli; +Cc: Gavin Shan, netdev, davem, benh, joel, weixue

On Thu, Jul 07, 2016 at 10:32:12AM -0700, Florian Fainelli wrote:
>On 07/02/2016 10:32 PM, Gavin Shan wrote:
>> This series rebases on David's linux-net git repo ("master" branch). It's
>> to support NCSI stack on net/farady/ftgmac100.c
>> 
>> The following figure gives an example about how NCSI is deployed: The NCSI is
>> specified by DSP0222, which can be downloaded from the following link here
>> (http://www.dmtf.org/sites/default/files/standards/documents/DSP0222_1.0.0.pdf).
>> 
>>    * The NC-SI (aka NCSI) is defined as the interface between a (Base) Management
>>      Controller (BMC) and one or multiple Network Controlers (NC) on host side.
>>      The interface is responsible for providing external network connectivity
>>      for BMC.
>>    * Each BMC can connect to multiple packages, up to 8. Each package can have
>>      multiple channels, up to 32. Every package and channel are identified by
>>      3-bits and 5-bits in NCSI packet. At one moment, one channel is active to
>>      provide service.
>>    * NCSI packet, encapsulated in ethernet frame, has 0x88F8 in the protocol
>>      field. The destination MAC address should be 0xFF's while the source MAC
>>      address can be arbitrary one.
>>    * NCSI packets are classified to command, response, AEN (Asynchronous Event
>>      Notification). Commands are sent from BMC to host for configuration and
>>      information retrival. Responses, corresponding to commands, are sent from
>>      host to BMC for confirmation and requested information. One command should
>>      have one and only one response. AEN is sent from host to BMC for notification
>>      (e.g. link down on active channel) so that BMC can take appropriate action.
>> 
>>    +------------------+        +----------------------------------------------+
>>    |                  |        |                     Host                     |
>>    |        BMC       |        |                                              |
>>    |                  |        | +-------------------+  +-------------------+ |
>>    |    +---------+   |        | |     Package-A     |  |     Package-B     | |
>>    |    |         |   |        | +---------+---------+  +-------------------+ |
>>    |    |   NIC   |   |        | | Channel | Channel |  | Channel | Channel | |
>>    +----+----+----+---+        +-+---------+---------+--+---------+---------+-+
>>              |                             |                      |
>>              |                             |                      |
>>              +-----------------------------+----------------------+
>> 
>> The design for the patchset is highlighted as below:
>> 
>>    * The NCSI interface is abstracted with "struct ncsi_dev". It's registered
>>      when net_device is created, started to work by calling ncsi_start_dev()
>>      when net_device is opened (ndo_open()). For the first time, NCSI packets
>>      are sent and received to/from the far end (host in above figure) to probe
>>      available NCSI packages and channels. After that, one channel is chosen as
>>      active one to provide service.
>>    * The NCSI stack is driven by workqueue and state machine internally.
>>    * AEN (Asychronous Event Notification) might be received from the far end
>>      (host). The currently active NCSI channel fails over to another available
>>      one if possible. Otherwise, the NCSI channel is out of service.
>>    * NCSI stack should be configurable through netlink or another mechanism,
>>      but it's not implemented in this patchset. It's something TBD.
>>    * The first NIC driver that is aware of NCSI: drivers/net/ethernet/faraday/ftgmac100.c

Florian, thanks for your comments.

>I know nothing about NCSI, pretty much like Jon Snow, but from a cursory
>look at your patches, is not there a way to make the NCSCI capable
>network devices strictly adhere to the net_device APIs and calling
>conventions?
>

Please refer to Ben's reply and it's well explained.

>Even if the data flow is a little different than normal ethernet frames,
>and there is not a good way to trap to intercept the delivery of NCSI
>packets, one could imagine doing something ala DSA where you register a
>fake ethertype for NCSI to hook a ptype_fun packet handler, augment
>struct net_device with a ncsi_dev pointer, and do processing in
>net/nsci/ for this device you know where the packet came from. You don't
>need to have an officially assigned ethertype for this, see
>netdev_uses_dsa() which just tests whether the traffic is tagged with a
>particular tag and delivers packet to a protocol specific parser in
>net/dsa/.
>
>For packets on their way out you could imagine assigning them a specific
>skb->protocol value and have the driver's transmit path do specific
>things based on that.
>
>Just an idea, I am not even sure this makes sense here, but what seems
>to make sens to me is that if more network device drivers end up
>supporting and transporting NCSI, we barely want them to know about that.

NCSI packets are encapsulated in ethernet frames those protocol field
is set to 0x88F8 as the NCSI specification states. I guess it's different
from DSA after having quick scan on the code you pointed. It seems DSA
is using software objects (struct dsa_switch_tree) to supersede the bridge
information contained in ethernet frame (e.g. destination MAC address).
The ingress ethernet frames are switched to destination port based the
tag then. NCSI isn't switching frames and all packets originate from
NCSI stack regardless of NIC on the far end. In most cases, the NCSI
packets show up in pairs (command/response). The command is always
originated from NCSI stack and response (from NIC on the far end)
terminates in NCSI stack. I think a device specific ptype works fine.

Thank you again for the idea and comments :-)

Thanks,
Gavin
 
>-- 
>Florian
>

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

end of thread, other threads:[~2016-07-08  1:11 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-03  5:32 [PATCH net-next 00/10] NCSI Support Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 01/10] net/ncsi: Resource management Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 02/10] net/ncsi: NCSI command packet handler Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 03/10] net/ncsi: NCSI response " Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 04/10] net/ncsi: Package and channel management Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 05/10] net/ncsi: NCSI AEN packet handler Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 06/10] net/farady: Helper functions to create or destroy MDIO interface Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 07/10] net/farady: Read MAC address from chip Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 08/10] net/farady: Support NCSI mode Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 09/10] net/farady: Match driver according to compatible property Gavin Shan
2016-07-03  5:32 ` [PATCH net-next 10/10] net/farady: Mask PHY interrupt with NCSI mode Gavin Shan
2016-07-03 22:03 ` [PATCH net-next 00/10] NCSI Support Or Gerlitz
2016-07-03 22:49   ` Benjamin Herrenschmidt
2016-07-04  0:24     ` Gavin Shan
2016-07-05 17:44   ` Alexei Starovoitov
2016-07-05 21:42     ` Benjamin Herrenschmidt
2016-07-06  2:07       ` Alexei Starovoitov
2016-07-06  2:14         ` Benjamin Herrenschmidt
2016-07-07  9:12     ` Or Gerlitz
2016-07-07  9:17       ` Benjamin Herrenschmidt
2016-07-07  9:18         ` Benjamin Herrenschmidt
2016-07-07 13:05         ` Gavin Shan
2016-07-07 13:44         ` Or Gerlitz
2016-07-07 16:34           ` Gavin Shan
2016-07-07 17:32 ` Florian Fainelli
2016-07-07 22:05   ` Benjamin Herrenschmidt
2016-07-08  1:10   ` 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.