Skip to contentSkip to conversation

mips: octeon: Add ethernet support

PATCH Superseded Stefan Roese 2 participants 52 messages Mar 30, 2022 – Mar 31, 2022 Page 2 of 3

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#16 lore raw

PATCH 17/52 mips: octeon: Add cvmx-helper-pki.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-helper-pki.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-helper-pki.c | 2156 +++++++++++++++++++++++
 1 file changed, 2156 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-helper-pki.c
arch/mips/mach-octeon/cvmx-helper-pki.c new file +2156 -0
new file mode 100644
index 000000000000..d68c7dac0087
@@ -0,0 +1,2156 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * PKI helper functions.
+ */
+
+#include <time.h>
+#include <log.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pexp-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-sli-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-pki.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+#include <mach/cvmx-helper-pki.h>
+
+#include <mach/cvmx-global-resources.h>
+#include <mach/cvmx-pko-internal-ports-range.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-pip.h>
+
+static int pki_helper_debug;
+
+bool cvmx_pki_dflt_init[CVMX_MAX_NODES] = { [0 ... CVMX_MAX_NODES - 1] = 1 };
+
+static bool cvmx_pki_dflt_bp_en[CVMX_MAX_NODES] = { [0 ... CVMX_MAX_NODES - 1] =
+							    true };
+static struct cvmx_pki_cluster_grp_config pki_dflt_clgrp[CVMX_MAX_NODES] = {
+	{ 0, 0xf },
+	{ 0, 0xf }
+};
+
+struct cvmx_pki_pool_config pki_dflt_pool[CVMX_MAX_NODES] = {
+	[0 ... CVMX_MAX_NODES -
+	 1] = { .pool_num = -1, .buffer_size = 2048, .buffer_count = 0 }
+};
+
+struct cvmx_pki_aura_config pki_dflt_aura[CVMX_MAX_NODES] = {
+	[0 ... CVMX_MAX_NODES -
+	 1] = { .aura_num = 0, .pool_num = -1, .buffer_count = 0 }
+};
+
+struct cvmx_pki_style_config pki_dflt_style[CVMX_MAX_NODES] = {
+	[0 ... CVMX_MAX_NODES - 1] = { .parm_cfg = { .lenerr_en = 1,
+						     .maxerr_en = 1,
+						     .minerr_en = 1,
+						     .fcs_strip = 1,
+						     .fcs_chk = 1,
+						     .first_skip = 40,
+						     .mbuff_size = 2048 } }
+};
+
+struct cvmx_pki_sso_grp_config pki_dflt_sso_grp[CVMX_MAX_NODES];
+struct cvmx_pki_qpg_config pki_dflt_qpg[CVMX_MAX_NODES];
+struct cvmx_pki_pkind_config pki_dflt_pkind[CVMX_MAX_NODES];
+u64 pkind_style_map[CVMX_MAX_NODES][CVMX_PKI_NUM_PKIND] = {
+	[0 ... CVMX_MAX_NODES -
+	 1] = { 0,  1,	2,  3,	4,  5,	6,  7,	8,  9,	10, 11, 12, 13, 14, 15,
+		16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
+		32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
+		48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 }
+};
+
+/* To store the qos watcher values before they are written to pcam when watcher is enabled
+There is no cvmx-pip.c file exist so it ended up here */
+struct cvmx_pki_legacy_qos_watcher qos_watcher[8];
+u64 pcam_dmach[CVMX_PKI_NUM_PCAM_ENTRY] = { -1 };
+u64 pcam_dmacl[CVMX_PKI_NUM_PCAM_ENTRY] = { -1 };
+
+/** @INTERNAL
+ * This function setsup default ltype map
+ * @param node    node number
+ */
+void __cvmx_helper_pki_set_dflt_ltype_map(int node)
+{
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_NONE,
+				 CVMX_PKI_BELTYPE_NONE);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_ENET,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_VLAN,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_SNAP_PAYLD,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_ARP,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_RARP,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_IP4,
+				 CVMX_PKI_BELTYPE_IP4);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_IP4_OPT,
+				 CVMX_PKI_BELTYPE_IP4);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_IP6,
+				 CVMX_PKI_BELTYPE_IP6);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_IP6_OPT,
+				 CVMX_PKI_BELTYPE_IP6);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_IPSEC_ESP,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_IPFRAG,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_IPCOMP,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_TCP,
+				 CVMX_PKI_BELTYPE_TCP);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_UDP,
+				 CVMX_PKI_BELTYPE_UDP);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_SCTP,
+				 CVMX_PKI_BELTYPE_SCTP);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_UDP_VXLAN,
+				 CVMX_PKI_BELTYPE_UDP);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_GRE,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_NVGRE,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_GTP,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_SW28,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_SW29,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_SW30,
+				 CVMX_PKI_BELTYPE_MISC);
+	cvmx_pki_write_ltype_map(node, CVMX_PKI_LTYPE_E_SW31,
+				 CVMX_PKI_BELTYPE_MISC);
+}
+
+/** @INTERNAL
+ * This function installs the default VLAN entries to identify
+ * the VLAN and set WQE[vv], WQE[vs] if VLAN is found. In 78XX
+ * hardware (PKI) is not hardwired to recognize any 802.1Q VLAN
+ * Ethertypes
+ *
+ * @param node    node number
+ */
+int __cvmx_helper_pki_install_dflt_vlan(int node)
+{
+	struct cvmx_pki_pcam_input pcam_input;
+	struct cvmx_pki_pcam_action pcam_action;
+	enum cvmx_pki_term field;
+	int index;
+	int bank;
+	u64 cl_mask = CVMX_PKI_CLUSTER_ALL;
+
+	memset(&pcam_input, 0, sizeof(pcam_input));
+	memset(&pcam_action, 0, sizeof(pcam_action));
+
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_X)) {
+		/* PKI-20858 */
+		int i;
+
+		for (i = 0; i < 4; i++) {
+			union cvmx_pki_clx_ecc_ctl ecc_ctl;
+
+			ecc_ctl.u64 =
+				csr_rd_node(node, CVMX_PKI_CLX_ECC_CTL(i));
+			ecc_ctl.s.pcam_en = 0;
+			ecc_ctl.s.pcam0_cdis = 1;
+			ecc_ctl.s.pcam1_cdis = 1;
+			csr_wr_node(node, CVMX_PKI_CLX_ECC_CTL(i), ecc_ctl.u64);
+		}
+	}
+
+	for (field = CVMX_PKI_PCAM_TERM_ETHTYPE0;
+	     field < CVMX_PKI_PCAM_TERM_ETHTYPE2; field++) {
+		bank = field & 0x01;
+
+		index = cvmx_pki_pcam_entry_alloc(
+			node, CVMX_PKI_FIND_AVAL_ENTRY, bank, cl_mask);
+		if (index < 0) {
+			debug("ERROR: Allocating pcam entry node=%d bank=%d\n",
+			      node, bank);
+			return -1;
+		}
+		pcam_input.style = 0;
+		pcam_input.style_mask = 0;
+		pcam_input.field = field;
+		pcam_input.field_mask = 0xfd;
+		pcam_input.data = 0x81000000;
+		pcam_input.data_mask = 0xffff0000;
+		pcam_action.parse_mode_chg = CVMX_PKI_PARSE_NO_CHG;
+		pcam_action.layer_type_set = CVMX_PKI_LTYPE_E_VLAN;
+		pcam_action.style_add = 0;
+		pcam_action.pointer_advance = 4;
+		cvmx_pki_pcam_write_entry(
+			node, index, cl_mask, pcam_input,
+			pcam_action); /*cluster_mask in pass2*/
+
+		index = cvmx_pki_pcam_entry_alloc(
+			node, CVMX_PKI_FIND_AVAL_ENTRY, bank, cl_mask);
+		if (index < 0) {
+			debug("ERROR: Allocating pcam entry node=%d bank=%d\n",
+			      node, bank);
+			return -1;
+		}
+		pcam_input.data = 0x88a80000;
+		cvmx_pki_pcam_write_entry(node, index, cl_mask, pcam_input,
+					  pcam_action);
+
+		index = cvmx_pki_pcam_entry_alloc(
+			node, CVMX_PKI_FIND_AVAL_ENTRY, bank, cl_mask);
+		if (index < 0) {
+			debug("ERROR: Allocating pcam entry node=%d bank=%d\n",
+			      node, bank);
+			return -1;
+		}
+		pcam_input.data = 0x92000000;
+		cvmx_pki_pcam_write_entry(
+			node, index, cl_mask, pcam_input,
+			pcam_action); /* cluster_mask in pass2*/
+
+		index = cvmx_pki_pcam_entry_alloc(
+			node, CVMX_PKI_FIND_AVAL_ENTRY, bank, cl_mask);
+		if (index < 0) {
+			debug("ERROR: Allocating pcam entry node=%d bank=%d\n",
+			      node, bank);
+			return -1;
+		}
+		pcam_input.data = 0x91000000;
+		cvmx_pki_pcam_write_entry(node, index, cl_mask, pcam_input,
+					  pcam_action);
+	}
+	return 0;
+}
+
+static int __cvmx_helper_setup_pki_cluster_groups(int node)
+{
+	u64 cl_mask;
+	int cl_group;
+
+	cl_group =
+		cvmx_pki_cluster_grp_alloc(node, pki_dflt_clgrp[node].grp_num);
+	if (cl_group == CVMX_RESOURCE_ALLOC_FAILED)
+		return -1;
+	else if (cl_group == CVMX_RESOURCE_ALREADY_RESERVED) {
+		if (pki_dflt_clgrp[node].grp_num == -1)
+			return -1;
+		else
+			return 0; /* cluster already configured, share it */
+	}
+	cl_mask = pki_dflt_clgrp[node].cluster_mask;
+	if (pki_helper_debug)
+		debug("pki-helper: setup pki cluster grp %d with cl_mask 0x%llx\n",
+		      (int)cl_group, (unsigned long long)cl_mask);
+	cvmx_pki_attach_cluster_to_group(node, cl_group, cl_mask);
+	return 0;
+}
+
+/**
+ * This function sets up pools/auras to be used by PKI
+ * @param node    node number
+ */
+int __cvmx_helper_pki_setup_sso_groups(int node)
+{
+	struct cvmx_coremask core_mask = CVMX_COREMASK_EMPTY;
+	cvmx_xgrp_t xgrp;
+	int grp;
+	int priority;
+	int weight;
+	int affinity;
+	u64 modify_mask;
+	u8 core_mask_set;
+
+	/* try to reserve sso groups and configure them if they are not configured */
+	grp = cvmx_sso_reserve_group_range(node, &pki_dflt_sso_grp[node].group,
+					   1);
+	if (grp == CVMX_RESOURCE_ALLOC_FAILED)
+		return -1;
+	else if (grp == CVMX_RESOURCE_ALREADY_RESERVED)
+		return 0; /* sso group already configured, share it */
+
+	xgrp.xgrp = grp;
+	priority = pki_dflt_sso_grp[node].priority;
+	weight = pki_dflt_sso_grp[node].weight;
+	affinity = pki_dflt_sso_grp[node].affinity;
+	core_mask_set = pki_dflt_sso_grp[node].core_mask_set;
+	cvmx_coremask_set64_node(&core_mask, node,
+				 pki_dflt_sso_grp[node].core_mask);
+	modify_mask = CVMX_SSO_MODIFY_GROUP_PRIORITY |
+		      CVMX_SSO_MODIFY_GROUP_WEIGHT |
+		      CVMX_SSO_MODIFY_GROUP_AFFINITY;
+	if (pki_helper_debug)
+		debug("pki-helper: set sso grp %d with priority %d weight %d core_mask 0x%llx\n",
+		      grp, priority, weight,
+		      (unsigned long long)pki_dflt_sso_grp[node].core_mask);
+	cvmx_sso_set_group_priority(node, xgrp, priority, weight, affinity,
+				    modify_mask);
+	cvmx_sso_set_group_core_affinity(xgrp, &core_mask, core_mask_set);
+	return 0;
+}
+
+/**
+ * This function sets up pools/auras to be used by PKI
+ * @param node    node number
+ */
+static int __cvmx_helper_pki_setup_fpa_pools(int node)
+{
+	u64 buffer_count;
+	u64 buffer_size;
+
+	if (__cvmx_fpa3_aura_valid(pki_dflt_aura[node].aura))
+		return 0; /* aura already configured, share it */
+
+	buffer_count = pki_dflt_pool[node].buffer_count;
+	buffer_size = pki_dflt_pool[node].buffer_size;
+
+	if (buffer_count != 0) {
+		pki_dflt_pool[node].pool = cvmx_fpa3_setup_fill_pool(
+			node, pki_dflt_pool[node].pool_num, "PKI POOL DFLT",
+			buffer_size, buffer_count, NULL);
+		if (!__cvmx_fpa3_pool_valid(pki_dflt_pool[node].pool)) {
+			cvmx_printf("ERROR: %s: Failed to allocate pool %d\n",
+				    __func__, pki_dflt_pool[node].pool_num);
+			return -1;
+		}
+		pki_dflt_pool[node].pool_num = pki_dflt_pool[node].pool.lpool;
+
+		if (pki_helper_debug)
+			debug("%s pool %d with buffer size %d cnt %d\n",
+			      __func__, pki_dflt_pool[node].pool_num,
+			      (int)buffer_size, (int)buffer_count);
+
+		pki_dflt_aura[node].pool_num = pki_dflt_pool[node].pool_num;
+		pki_dflt_aura[node].pool = pki_dflt_pool[node].pool;
+	}
+
+	buffer_count = pki_dflt_aura[node].buffer_count;
+
+	if (buffer_count != 0) {
+		pki_dflt_aura[node].aura = cvmx_fpa3_set_aura_for_pool(
+			pki_dflt_aura[node].pool, pki_dflt_aura[node].aura_num,
+			"PKI DFLT AURA", buffer_size, buffer_count);
+
+		if (!__cvmx_fpa3_aura_valid(pki_dflt_aura[node].aura)) {
+			debug("ERROR: %sL Failed to allocate aura %d\n",
+			      __func__, pki_dflt_aura[node].aura_num);
+			return -1;
+		}
+	}
+	return 0;
+}
+
+static int __cvmx_helper_setup_pki_qpg_table(int node)
+{
+	int offset;
+
+	offset = cvmx_pki_qpg_entry_alloc(node, pki_dflt_qpg[node].qpg_base, 1);
+	if (offset == CVMX_RESOURCE_ALLOC_FAILED)
+		return -1;
+	else if (offset == CVMX_RESOURCE_ALREADY_RESERVED)
+		return 0; /* share the qpg table entry */
+	if (pki_helper_debug)
+		debug("pki-helper: set qpg entry at offset %d with port add %d aura %d grp_ok %d grp_bad %d\n",
+		      offset, pki_dflt_qpg[node].port_add,
+		      pki_dflt_qpg[node].aura_num, pki_dflt_qpg[node].grp_ok,
+		      pki_dflt_qpg[node].grp_bad);
+	cvmx_pki_write_qpg_entry(node, offset, &pki_dflt_qpg[node]);
+	return 0;
+}
+
+int __cvmx_helper_pki_port_setup(int node, int ipd_port)
+{
+	int xiface, index;
+	int pknd, style_num;
+	int rs;
+	struct cvmx_pki_pkind_config pkind_cfg;
+
+	if (!cvmx_pki_dflt_init[node])
+		return 0;
+	xiface = cvmx_helper_get_interface_num(ipd_port);
+	index = cvmx_helper_get_interface_index_num(ipd_port);
+
+	pknd = cvmx_helper_get_pknd(xiface, index);
+	style_num = pkind_style_map[node][pknd];
+
+	/* try to reserve the style, if it is not configured already, reserve
+	and configure it */
+	rs = cvmx_pki_style_alloc(node, style_num);
+	if (rs < 0) {
+		if (rs == CVMX_RESOURCE_ALLOC_FAILED)
+			return -1;
+	} else {
+		if (pki_helper_debug)
+			debug("pki-helper: set style %d with default parameters\n",
+			      style_num);
+		pkind_style_map[node][pknd] = style_num;
+		/* configure style with default parameters */
+		cvmx_pki_write_style_config(node, style_num,
+					    CVMX_PKI_CLUSTER_ALL,
+					    &pki_dflt_style[node]);
+	}
+	if (pki_helper_debug)
+		debug("pki-helper: set pkind %d with initial style %d\n", pknd,
+		      style_num);
+	/* write pkind configuration */
+	pkind_cfg = pki_dflt_pkind[node];
+	pkind_cfg.initial_style = style_num;
+	cvmx_pki_write_pkind_config(node, pknd, &pkind_cfg);
+	return 0;
+}
+
+int __cvmx_helper_pki_global_setup(int node)
+{
+	__cvmx_helper_pki_set_dflt_ltype_map(node);
+	if (!cvmx_pki_dflt_init[node])
+		return 0;
+	/* Setup the packet pools*/
+	__cvmx_helper_pki_setup_fpa_pools(node);
+	/*set up default cluster*/
+	__cvmx_helper_setup_pki_cluster_groups(node);
+	//__cvmx_helper_pki_setup_sso_groups(node);
+	__cvmx_helper_setup_pki_qpg_table(node);
+	/*
+	 * errata PKI-19103 backward compat has only 1 aura
+	 * no head line blocking
+	 */
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_X)) {
+		cvmx_pki_buf_ctl_t buf_ctl;
+
+		buf_ctl.u64 = csr_rd_node(node, CVMX_PKI_BUF_CTL);
+		buf_ctl.s.fpa_wait = 1;
+		csr_wr_node(node, CVMX_PKI_BUF_CTL, buf_ctl.u64);
+	}
+	return 0;
+}
+
+void cvmx_helper_pki_set_dflt_pool(int node, int pool, int buffer_size,
+				   int buffer_count)
+{
+	if (pool == 0)
+		pool = -1;
+	pki_dflt_pool[node].pool_num = pool;
+	pki_dflt_pool[node].buffer_size = buffer_size;
+	pki_dflt_pool[node].buffer_count = buffer_count;
+}
+
+void cvmx_helper_pki_set_dflt_aura(int node, int aura, int pool,
+				   int buffer_count)
+{
+	pki_dflt_aura[node].aura_num = aura;
+	pki_dflt_aura[node].pool_num = pool;
+	pki_dflt_aura[node].buffer_count = buffer_count;
+}
+
+void cvmx_helper_pki_set_dflt_pool_buffer(int node, int buffer_count)
+{
+	pki_dflt_pool[node].buffer_count = buffer_count;
+}
+
+void cvmx_helper_pki_set_dflt_aura_buffer(int node, int buffer_count)
+{
+	pki_dflt_aura[node].buffer_count = buffer_count;
+}
+
+void cvmx_helper_pki_set_dflt_style(int node,
+				    struct cvmx_pki_style_config *style_cfg)
+{
+	pki_dflt_style[node] = *style_cfg;
+}
+
+void cvmx_helper_pki_get_dflt_style(int node,
+				    struct cvmx_pki_style_config *style_cfg)
+{
+	*style_cfg = pki_dflt_style[node];
+}
+
+void cvmx_helper_pki_set_dflt_qpg(int node, struct cvmx_pki_qpg_config *qpg_cfg)
+{
+	pki_dflt_qpg[node] = *qpg_cfg;
+}
+
+void cvmx_helper_pki_get_dflt_qpg(int node, struct cvmx_pki_qpg_config *qpg_cfg)
+{
+	*qpg_cfg = pki_dflt_qpg[node];
+}
+
+void cvmx_helper_pki_set_dflt_pkind_map(int node, int pkind, int style)
+{
+	pkind_style_map[node][pkind] = style;
+}
+
+void cvmx_helper_pki_no_dflt_init(int node)
+{
+	cvmx_pki_dflt_init[node] = 0;
+}
+
+void cvmx_helper_pki_set_dflt_bp_en(int node, bool bp_en)
+{
+	cvmx_pki_dflt_bp_en[node] = bp_en;
+}
+
+/**
+ * This function Enabled the PKI hardware to
+ * start accepting/processing packets.
+ *
+ * @param node    node number
+ */
+void cvmx_helper_pki_enable(int node)
+{
+	if (pki_helper_debug)
+		debug("enable PKI on node %d\n", node);
+	__cvmx_helper_pki_install_dflt_vlan(node);
+	cvmx_pki_setup_clusters(node);
+	if (cvmx_pki_dflt_bp_en[node])
+		cvmx_pki_enable_backpressure(node);
+	cvmx_pki_parse_enable(node, 0);
+	cvmx_pki_enable(node);
+}
+
+/**
+ * This function frees up PKI resources consumed by that port.
+ * This function should only be called if port resources
+ * (fpa pools aura, style qpg entry pcam entry etc.) are not shared
+ * @param ipd_port      ipd port number for which resources need to
+ *		      be freed.
+ */
+int cvmx_helper_pki_port_shutdown(int ipd_port)
+{
+	/* remove pcam entries */
+	/* implemet if needed */
+	/* __cvmx_pki_port_rsrc_free(node); */
+	return 0;
+}
+
+/**
+ * This function shuts down complete PKI hardware
+ * and software resources.
+ * @param node	  node number where PKI needs to shutdown.
+ */
+void cvmx_helper_pki_shutdown(int node)
+{
+	int i, k;
+	/* remove pcam entries */
+	/* Disable PKI */
+	cvmx_pki_disable(node);
+	/* Free all prefetched buffers */
+	__cvmx_pki_free_ptr(node);
+	/* Reset PKI */
+	cvmx_pki_reset(node);
+	/* Free all the allocated PKI resources
+	except fpa pools & aura which will be done in fpa block */
+	__cvmx_pki_global_rsrc_free(node);
+	/* Setup some configuration registers to the reset state.*/
+	for (i = 0; i < CVMX_PKI_NUM_PKIND; i++) {
+		for (k = 0; k < (int)CVMX_PKI_NUM_CLUSTER; k++) {
+			csr_wr_node(node, CVMX_PKI_CLX_PKINDX_CFG(i, k), 0);
+			csr_wr_node(node, CVMX_PKI_CLX_PKINDX_STYLE(i, k), 0);
+			csr_wr_node(node, CVMX_PKI_CLX_PKINDX_SKIP(i, k), 0);
+			csr_wr_node(node, CVMX_PKI_CLX_PKINDX_L2_CUSTOM(i, k),
+				    0);
+			csr_wr_node(node, CVMX_PKI_CLX_PKINDX_LG_CUSTOM(i, k),
+				    0);
+		}
+		csr_wr_node(node, CVMX_PKI_PKINDX_ICGSEL(k), 0);
+	}
+	for (i = 0; i < CVMX_PKI_NUM_FINAL_STYLE; i++) {
+		for (k = 0; k < (int)CVMX_PKI_NUM_CLUSTER; k++) {
+			csr_wr_node(node, CVMX_PKI_CLX_STYLEX_CFG(i, k), 0);
+			csr_wr_node(node, CVMX_PKI_CLX_STYLEX_CFG2(i, k), 0);
+			csr_wr_node(node, CVMX_PKI_CLX_STYLEX_ALG(i, k), 0);
+		}
+		csr_wr_node(node, CVMX_PKI_STYLEX_BUF(k), (0x5 << 22) | 0x20);
+	}
+}
+
+/**
+ * This function calculates how mant qpf entries will be needed for
+ * a particular QOS.
+ * @param qpg_qos       qos value for which entries need to be calculated.
+ */
+int cvmx_helper_pki_get_num_qpg_entry(enum cvmx_pki_qpg_qos qpg_qos)
+{
+	if (qpg_qos == CVMX_PKI_QPG_QOS_NONE)
+		return 1;
+	else if (qpg_qos == CVMX_PKI_QPG_QOS_VLAN ||
+		 qpg_qos == CVMX_PKI_QPG_QOS_MPLS)
+		return 8;
+	else if (qpg_qos == CVMX_PKI_QPG_QOS_DSA_SRC)
+		return 32;
+	else if (qpg_qos == CVMX_PKI_QPG_QOS_DIFFSERV ||
+		 qpg_qos == CVMX_PKI_QPG_QOS_HIGIG)
+		return 64;
+	else {
+		debug("ERROR: unrecognized qpg_qos = %d", qpg_qos);
+		return 0;
+	}
+}
+
+/**
+ * This function setups the qos table by allocating qpg entry and writing
+ * the provided parameters to that entry (offset).
+ * @param node	  node number.
+ * @param qpg_cfg       pointer to struct containing qpg configuration
+ */
+int cvmx_helper_pki_set_qpg_entry(int node, struct cvmx_pki_qpg_config *qpg_cfg)
+{
+	int offset;
+
+	offset = cvmx_pki_qpg_entry_alloc(node, qpg_cfg->qpg_base, 1);
+	if (pki_helper_debug)
+		debug("pki-helper:set qpg entry at offset %d\n", offset);
+	if (offset == CVMX_RESOURCE_ALREADY_RESERVED) {
+		debug("INFO:setup_qpg_table: offset %d already reserved\n",
+		      qpg_cfg->qpg_base);
+		return CVMX_RESOURCE_ALREADY_RESERVED;
+	} else if (offset == CVMX_RESOURCE_ALLOC_FAILED) {
+		debug("ERROR:setup_qpg_table: no more entries available\n");
+		return CVMX_RESOURCE_ALLOC_FAILED;
+	}
+	qpg_cfg->qpg_base = offset;
+	cvmx_pki_write_qpg_entry(node, offset, qpg_cfg);
+	return offset;
+}
+
+/**
+ * This function sets up aura QOS for RED, backpressure and tail-drop.
+ *
+ * @param node       node number.
+ * @param aura       aura to configure.
+ * @param ena_red       enable RED based on [DROP] and [PASS] levels
+ *			1: enable 0:disable
+ * @param pass_thresh   pass threshold for RED.
+ * @param drop_thresh   drop threshold for RED
+ * @param ena_bp	enable backpressure based on [BP] level.
+ *			1:enable 0:disable
+ * @param bp_thresh     backpressure threshold.
+ * @param ena_drop      enable tail drop.
+ *			1:enable 0:disable
+ * @return Zero on success. Negative on failure
+ * @note the 'node' and 'aura' arguments may be combined in the future
+ * to use a compaund cvmx_fpa3_gaura_t structure argument.
+ */
+int cvmx_helper_setup_aura_qos(int node, int aura, bool ena_red, bool ena_drop,
+			       u64 pass_thresh, u64 drop_thresh, bool ena_bp,
+			       u64 bp_thresh)
+{
+	cvmx_fpa3_gaura_t gaura;
+
+	gaura = __cvmx_fpa3_gaura(node, aura);
+
+	ena_red = ena_red | ena_drop;
+	cvmx_fpa3_setup_aura_qos(gaura, ena_red, pass_thresh, drop_thresh,
+				 ena_bp, bp_thresh);
+	cvmx_pki_enable_aura_qos(node, aura, ena_red, ena_drop, ena_bp);
+	return 0;
+}
+
+/**
+ * This function maps specified bpid to all the auras from which it can receive bp and
+ * then maps that bpid to all the channels, that bpid can asserrt bp on.
+ *
+ * @param node	  node number.
+ * @param aura	  aura number which will back pressure specified bpid.
+ * @param bpid	  bpid to map.
+ * @param chl_map       array of channels to map to that bpid.
+ * @param chl_cnt	number of channel/ports to map to that bpid.
+ * @return Zero on success. Negative on failure
+ */
+int cvmx_helper_pki_map_aura_chl_bpid(int node, uint16_t aura, uint16_t bpid,
+				      u16 chl_map[], uint16_t chl_cnt)
+{
+	u16 channel;
+
+	if (aura >= CVMX_PKI_NUM_AURA) {
+		debug("ERROR: aura %d is > supported in hw\n", aura);
+		return -1;
+	}
+	if (bpid >= CVMX_PKI_NUM_BPID) {
+		debug("ERROR: bpid %d is > supported in hw\n", bpid);
+		return -1;
+	}
+	cvmx_pki_write_aura_bpid(node, aura, bpid);
+	while (chl_cnt--) {
+		channel = chl_map[chl_cnt];
+		if (channel >= CVMX_PKI_NUM_CHANNEL) {
+			debug("ERROR: channel %d is > supported in hw\n",
+			      channel);
+			return -1;
+		}
+		cvmx_pki_write_channel_bpid(node, channel, bpid);
+	}
+	return 0;
+}
+
+/** @INTERNAL
+ * This function returns the value of port shift required
+ * if all the ports on that interface are using same style and
+ * configuring qpg_qos != NONE
+ */
+int __cvmx_helper_pki_port_shift(int xiface, enum cvmx_pki_qpg_qos qpg_qos)
+{
+	u8 num_qos;
+	cvmx_helper_interface_mode_t mode =
+		cvmx_helper_interface_get_mode(xiface);
+
+	num_qos = cvmx_helper_pki_get_num_qpg_entry(qpg_qos);
+	if ((mode != CVMX_HELPER_INTERFACE_MODE_SGMII) &&
+	    (mode != CVMX_HELPER_INTERFACE_MODE_NPI) &&
+	    (mode != CVMX_HELPER_INTERFACE_MODE_LOOP)) {
+		return ffs(num_qos) - 1;
+	} else if (num_qos <= 16)
+		return 0;
+	else if (num_qos <= 32)
+		return 1;
+	else
+		return 2;
+}
+
+int __cvmx_helper_pki_qos_rsrcs(int node, struct cvmx_pki_qos_schd *qossch)
+{
+	int rs;
+
+	/* Reserve pool resources */
+	if (qossch->pool_per_qos && qossch->pool_num < 0) {
+		if (pki_helper_debug)
+			debug("pki-helper:qos-rsrc: setup pool %d buff_size %d blocks %d\n",
+			      qossch->pool_num, (int)qossch->pool_buff_size,
+			      (int)qossch->pool_max_buff);
+
+		qossch->_pool = cvmx_fpa3_setup_fill_pool(
+			node, qossch->pool_num, qossch->pool_name,
+			qossch->pool_buff_size, qossch->pool_max_buff, NULL);
+
+		if (!__cvmx_fpa3_pool_valid(qossch->_pool)) {
+			cvmx_printf("ERROR: %s POOL %d init failed\n", __func__,
+				    qossch->pool_num);
+			return -1;
+		}
+
+		qossch->pool_num = qossch->_pool.lpool;
+		if (pki_helper_debug)
+			debug("pool alloced is %d\n", qossch->pool_num);
+	}
+	/* Reserve aura resources */
+	if (qossch->aura_per_qos && qossch->aura_num < 0) {
+		if (pki_helper_debug)
+			debug("pki-helper:qos-rsrc: setup aura %d pool %d blocks %d\n",
+			      qossch->aura_num, qossch->pool_num,
+			      (int)qossch->aura_buff_cnt);
+
+		qossch->_aura = cvmx_fpa3_set_aura_for_pool(
+			qossch->_pool, qossch->aura_num, qossch->aura_name,
+			qossch->pool_buff_size, qossch->aura_buff_cnt);
+
+		if (!__cvmx_fpa3_aura_valid(qossch->_aura)) {
+			cvmx_printf("ERROR: %s AURA %d init failed\n", __func__,
+				    qossch->aura_num);
+			return -1;
+		}
+
+		qossch->aura_num = qossch->_aura.laura;
+		if (pki_helper_debug)
+			debug("aura alloced is %d\n", qossch->aura_num);
+	}
+	/* Reserve sso group resources */
+	/* Find which node work needs to be schedules vinita_to_do to extract node*/
+	if (qossch->sso_grp_per_qos && qossch->sso_grp < 0) {
+		//unsigned grp_node;
+		//grp_node = (abs)(qossch->sso_grp + CVMX_PKI_FIND_AVAILABLE_RSRC);
+		rs = cvmx_sso_reserve_group(node);
+		if (rs < 0) {
+			debug("pki-helper:qos-rsrc: ERROR: sso grp not available\n");
+			return rs;
+		}
+		qossch->sso_grp = rs | (node << 8);
+		if (pki_helper_debug)
+			debug("pki-helper:qos-rsrc: sso grp alloced is %d\n",
+			      qossch->sso_grp);
+	}
+	return 0;
+}
+
+int __cvmx_helper_pki_port_rsrcs(int node, struct cvmx_pki_prt_schd *prtsch)
+{
+	int rs;
+
+	/* Erratum 22557: Disable per-port allocation for CN78XX pass 1.X */
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_X)) {
+		static bool warned;
+
+		prtsch->pool_per_prt = 0;
+		if (!warned)
+			cvmx_printf(
+				"WARNING: %s: Ports configured in single-pool mode per erratum 22557.\n",
+				__func__);
+		warned = true;
+	}
+	/* Reserve pool resources */
+	if (prtsch->pool_per_prt && prtsch->pool_num < 0) {
+		if (pki_helper_debug)
+			debug("pki-helper:port-rsrc: setup pool %d buff_size %d blocks %d\n",
+			      prtsch->pool_num, (int)prtsch->pool_buff_size,
+			      (int)prtsch->pool_max_buff);
+
+		prtsch->_pool = cvmx_fpa3_setup_fill_pool(
+			node, prtsch->pool_num, prtsch->pool_name,
+			prtsch->pool_buff_size, prtsch->pool_max_buff, NULL);
+
+		if (!__cvmx_fpa3_pool_valid(prtsch->_pool)) {
+			cvmx_printf("ERROR: %s: POOL %d init failed\n",
+				    __func__, prtsch->pool_num);
+			return -1;
+		}
+		prtsch->pool_num = prtsch->_pool.lpool;
+		if (pki_helper_debug)
+			debug("pool alloced is %d\n", prtsch->pool_num);
+	}
+	/* Reserve aura resources */
+	if (prtsch->aura_per_prt && prtsch->aura_num < 0) {
+		if (pki_helper_debug)
+			debug("pki-helper:port-rsrc; setup aura %d pool %d blocks %d\n",
+			      prtsch->aura_num, prtsch->pool_num,
+			      (int)prtsch->aura_buff_cnt);
+		prtsch->_aura = cvmx_fpa3_set_aura_for_pool(
+			prtsch->_pool, prtsch->aura_num, prtsch->aura_name,
+			prtsch->pool_buff_size, prtsch->aura_buff_cnt);
+
+		if (!__cvmx_fpa3_aura_valid(prtsch->_aura)) {
+			cvmx_printf("ERROR: %s: AURA %d init failed\n",
+				    __func__, prtsch->aura_num);
+			return -1;
+		}
+		prtsch->aura_num = prtsch->_aura.laura;
+
+		if (pki_helper_debug)
+			debug("aura alloced is %d\n", prtsch->aura_num);
+	}
+	/* Reserve sso group resources , vinita_to_do to extract node*/
+	if (prtsch->sso_grp_per_prt && prtsch->sso_grp < 0) {
+		//unsigned grp_node;
+		//grp_node = (abs)(prtsch->sso_grp + CVMX_PKI_FIND_AVAILABLE_RSRC);
+		rs = cvmx_sso_reserve_group(node);
+		if (rs < 0) {
+			cvmx_printf("ERROR: %s: sso grp not available\n",
+				    __func__);
+			return rs;
+		}
+		prtsch->sso_grp = rs | (node << 8);
+		if (pki_helper_debug)
+			debug("pki-helper:port-rsrc: sso grp alloced is %d\n",
+			      prtsch->sso_grp);
+	}
+	return 0;
+}
+
+int __cvmx_helper_pki_intf_rsrcs(int node, struct cvmx_pki_intf_schd *intf)
+{
+	int rs;
+
+	if (intf->pool_per_intf && intf->pool_num < 0) {
+		if (pki_helper_debug)
+			debug("pki-helper:intf-rsrc: setup pool %d buff_size %d blocks %d\n",
+			      intf->pool_num, (int)intf->pool_buff_size,
+			      (int)intf->pool_max_buff);
+		intf->_pool = cvmx_fpa3_setup_fill_pool(
+			node, intf->pool_num, intf->pool_name,
+			intf->pool_buff_size, intf->pool_max_buff, NULL);
+
+		if (!__cvmx_fpa3_pool_valid(intf->_pool)) {
+			cvmx_printf("ERROR: %s: POOL %d init failed\n",
+				    __func__, intf->pool_num);
+			return -1;
+		}
+		intf->pool_num = intf->_pool.lpool;
+
+		if (pki_helper_debug)
+			debug("pool alloced is %d\n", intf->pool_num);
+	}
+	if (intf->aura_per_intf && intf->aura_num < 0) {
+		if (pki_helper_debug)
+			debug("pki-helper:intf-rsrc: setup aura %d pool %d blocks %d\n",
+			      intf->aura_num, intf->pool_num,
+			      (int)intf->aura_buff_cnt);
+		intf->_aura = cvmx_fpa3_set_aura_for_pool(
+			intf->_pool, intf->aura_num, intf->aura_name,
+			intf->pool_buff_size, intf->aura_buff_cnt);
+
+		if (!__cvmx_fpa3_aura_valid(intf->_aura)) {
+			cvmx_printf("ERROR: %s: AURA %d init failed\n",
+				    __func__, intf->aura_num);
+
+			return -1;
+		}
+
+		intf->aura_num = intf->_aura.laura;
+
+		if (pki_helper_debug)
+			debug("aura alloced is %d\n", intf->aura_num);
+	}
+	/* vinita_to_do to extract node */
+	if (intf->sso_grp_per_intf && intf->sso_grp < 0) {
+		//unsigned grp_node;
+		//grp_node = (abs)(intf->sso_grp + CVMX_PKI_FIND_AVAILABLE_RSRC);
+		rs = cvmx_sso_reserve_group(node);
+		if (rs < 0) {
+			cvmx_printf("ERROR: %s: sso grp not available\n",
+				    __func__);
+			return rs;
+		}
+		intf->sso_grp = rs | (node << 8);
+		if (pki_helper_debug)
+			debug("pki-helper:intf-rsrc: sso grp alloced is %d\n",
+			      intf->sso_grp);
+	}
+	return 0;
+}
+
+int __cvmx_helper_pki_set_intf_qpg(int node, int port, int qpg_base,
+				   int num_entry,
+				   struct cvmx_pki_intf_schd *intfsch)
+{
+	int offset;
+	int entry;
+	struct cvmx_pki_qpg_config qpg_cfg;
+
+	memset(&qpg_cfg, 0, sizeof(qpg_cfg));
+	if (pki_helper_debug)
+		debug("pki-helper:intf_qpg port %d qpg_base %d num_entry %d",
+		      port, qpg_base, num_entry);
+	offset = cvmx_pki_qpg_entry_alloc(node, qpg_base, num_entry);
+	if (offset == CVMX_RESOURCE_ALREADY_RESERVED) {
+		debug("pki-helper: INFO: qpg entries will be shared\n");
+		return offset;
+	} else if (offset == CVMX_RESOURCE_ALLOC_FAILED) {
+		debug("pki-helper: ERROR: qpg entries not available\n");
+		return offset;
+	} else if (intfsch->qpg_base < 0) {
+		intfsch->qpg_base = offset;
+	}
+	if (pki_helper_debug)
+		debug("qpg_base allocated is %d\n", offset);
+	for (entry = 0; entry < num_entry; entry++) {
+		qpg_cfg.port_add = intfsch->prt_s[port].qos_s[entry].port_add;
+		qpg_cfg.aura_num = intfsch->prt_s[port].qos_s[entry].aura_num;
+		qpg_cfg.grp_ok = intfsch->prt_s[port].qos_s[entry].sso_grp;
+		qpg_cfg.grp_bad = intfsch->prt_s[port].qos_s[entry].sso_grp;
+		cvmx_pki_write_qpg_entry(node, (offset + entry), &qpg_cfg);
+	}
+	return offset;
+}
+
+/**
+ * This function sets up the global pool, aura and sso group
+ * resources which application can use between any interfaces
+ * and ports.
+ * @param node          node number
+ * @param gblsch        pointer to struct containing global
+ *                      scheduling parameters.
+ */
+int cvmx_helper_pki_set_gbl_schd(int node, struct cvmx_pki_global_schd *gblsch)
+{
+	int rs;
+
+	if (gblsch->setup_pool && gblsch->pool_num < 0) {
+		if (pki_helper_debug)
+			debug("%s: gbl setup global pool %d buff_size %d blocks %d\n",
+			      __func__, gblsch->pool_num,
+			      (int)gblsch->pool_buff_size,
+			      (int)gblsch->pool_max_buff);
+
+		gblsch->_pool = cvmx_fpa3_setup_fill_pool(
+			node, gblsch->pool_num, gblsch->pool_name,
+			gblsch->pool_buff_size, gblsch->pool_max_buff, NULL);
+
+		if (!__cvmx_fpa3_pool_valid(gblsch->_pool)) {
+			cvmx_printf("ERROR: %s: POOL %u:%d unavailable\n",
+				    __func__, node, gblsch->pool_num);
+			return -1;
+		}
+
+		gblsch->pool_num = gblsch->_pool.lpool;
+
+		if (pki_helper_debug)
+			debug("pool alloced is %d\n", gblsch->pool_num);
+	}
+	if (gblsch->setup_aura && gblsch->aura_num < 0) {
+		if (pki_helper_debug)
+			debug("%s: gbl setup global aura %d pool %d blocks %d\n",
+			      __func__, gblsch->aura_num, gblsch->pool_num,
+			      (int)gblsch->aura_buff_cnt);
+
+		gblsch->_aura = cvmx_fpa3_set_aura_for_pool(
+			gblsch->_pool, gblsch->aura_num, gblsch->aura_name,
+			gblsch->pool_buff_size, gblsch->aura_buff_cnt);
+
+		if (!__cvmx_fpa3_aura_valid(gblsch->_aura)) {
+			cvmx_printf("ERROR: %s: AURA %u:%d unavailable\n",
+				    __func__, node, gblsch->aura_num);
+			return -1;
+		}
+
+		gblsch->aura_num = gblsch->_aura.laura;
+
+		if (pki_helper_debug)
+			debug("aura alloced is %d\n", gblsch->aura_num);
+	}
+	if (gblsch->setup_sso_grp && gblsch->sso_grp < 0) {
+		rs = cvmx_sso_reserve_group(node);
+		if (rs < 0) {
+			debug("pki-helper:gbl: ERROR: sso grp not available\n");
+			return rs;
+		}
+		gblsch->sso_grp = rs | (node << 8);
+		if (pki_helper_debug)
+			debug("pki-helper:gbl: sso grp alloced is %d\n",
+			      gblsch->sso_grp);
+	}
+	return 0;
+}
+
+/**
+ * This function sets up scheduling parameters (pool, aura, sso group etc)
+ * of an ipd port.
+ * @param ipd_port      ipd port number
+ * @param prtsch        pointer to struct containing port's
+ *                      scheduling parameters.
+ */
+int cvmx_helper_pki_init_port(int ipd_port, struct cvmx_pki_prt_schd *prtsch)
+{
+	int num_qos;
+	int qos;
+	struct cvmx_pki_qpg_config qpg_cfg;
+	struct cvmx_pki_qos_schd *qossch;
+	struct cvmx_pki_style_config style_cfg;
+	struct cvmx_pki_pkind_config pknd_cfg;
+	int xiface = cvmx_helper_get_interface_num(ipd_port);
+	int pknd;
+	u16 mbuff_size;
+	int rs;
+
+	struct cvmx_xport xp = cvmx_helper_ipd_port_to_xport(ipd_port);
+
+	num_qos = cvmx_helper_pki_get_num_qpg_entry(prtsch->qpg_qos);
+	mbuff_size = prtsch->pool_buff_size;
+	memset(&qpg_cfg, 0, sizeof(qpg_cfg));
+
+	/* Reserve port resources */
+	rs = __cvmx_helper_pki_port_rsrcs(xp.node, prtsch);
+	if (rs)
+		return rs;
+	/* Reserve qpg resources */
+	if (prtsch->qpg_base < 0) {
+		rs = cvmx_pki_qpg_entry_alloc(xp.node, prtsch->qpg_base,
+					      num_qos);
+		if (rs < 0) {
+			debug("pki-helper:port%d:ERROR: qpg entries not available\n",
+			      ipd_port);
+			return CVMX_RESOURCE_ALLOC_FAILED;
+		}
+		prtsch->qpg_base = rs;
+		if (pki_helper_debug)
+			debug("pki-helper:port-init: to port %d, qpg_base %d allocated\n",
+			      ipd_port, prtsch->qpg_base);
+	}
+
+	if (prtsch->qpg_qos) {
+		for (qos = 0; qos < num_qos; qos++) {
+			qossch = &prtsch->qos_s[qos];
+			if (!qossch->pool_per_qos)
+				qossch->pool_num = prtsch->pool_num;
+			else if (qossch->pool_buff_size < mbuff_size)
+				mbuff_size = qossch->pool_buff_size;
+			if (!qossch->aura_per_qos)
+				qossch->aura_num = prtsch->aura_num;
+			if (!qossch->sso_grp_per_qos)
+				qossch->sso_grp = prtsch->sso_grp;
+
+			/* Reserve qos resources */
+			rs = __cvmx_helper_pki_qos_rsrcs(xp.node, qossch);
+			if (rs)
+				return rs;
+			qpg_cfg.port_add = qossch->port_add;
+			qpg_cfg.aura_num = qossch->aura_num;
+			qpg_cfg.grp_ok = qossch->sso_grp;
+			qpg_cfg.grp_bad = qossch->sso_grp;
+			cvmx_pki_write_qpg_entry(
+				xp.node, prtsch->qpg_base + qos, &qpg_cfg);
+			if (pki_helper_debug)
+				debug("%s: port %d qos %d has port_add %d aura %d grp %d\n",
+				      __func__, ipd_port, qos, qossch->port_add,
+				      qossch->aura_num, qossch->sso_grp);
+		} /* for qos 0 ... num_qos */
+	} else {
+		qpg_cfg.port_add = 0;
+		qpg_cfg.aura_num = prtsch->aura_num;
+		qpg_cfg.grp_ok = prtsch->sso_grp;
+		qpg_cfg.grp_bad = prtsch->sso_grp;
+		cvmx_pki_write_qpg_entry(xp.node, prtsch->qpg_base, &qpg_cfg);
+
+		if (pki_helper_debug)
+			debug("%s: non-qos port %d has aura %d grp %d\n",
+			      __func__, ipd_port, prtsch->aura_num,
+			      prtsch->sso_grp);
+	}
+
+	/* LR: The rest of code is common for qos and non-qos ports */
+
+	/* Allocate style here and map it to the port */
+	rs = cvmx_pki_style_alloc(xp.node, prtsch->style);
+	if (rs == CVMX_RESOURCE_ALREADY_RESERVED) {
+		debug("%s INFO: style will be shared\n", __func__);
+	} else if (rs == CVMX_RESOURCE_ALLOC_FAILED) {
+		debug("%s ERROR: style not available\n", __func__);
+		return CVMX_RESOURCE_ALLOC_FAILED;
+	}
+	prtsch->style = rs;
+
+	if (pki_helper_debug)
+		debug("%s: port %d has style %d\n", __func__, ipd_port,
+		      prtsch->style);
+
+	/* Config STYLE to above QPG table base entry */
+	style_cfg = pki_dflt_style[xp.node];
+	style_cfg.parm_cfg.qpg_qos = prtsch->qpg_qos;
+	style_cfg.parm_cfg.qpg_base = prtsch->qpg_base;
+	style_cfg.parm_cfg.qpg_port_msb = 0;
+	style_cfg.parm_cfg.qpg_port_sh = 0;
+	style_cfg.parm_cfg.mbuff_size = mbuff_size;
+	cvmx_pki_write_style_config(xp.node, prtsch->style,
+				    CVMX_PKI_CLUSTER_ALL, &style_cfg);
+
+	/* Update PKND with initial STYLE */
+	pknd = cvmx_helper_get_pknd(
+		xiface, cvmx_helper_get_interface_index_num(ipd_port));
+	cvmx_pki_read_pkind_config(xp.node, pknd, &pknd_cfg);
+	pknd_cfg.initial_style = prtsch->style;
+	pknd_cfg.fcs_pres = __cvmx_helper_get_has_fcs(xiface);
+	cvmx_pki_write_pkind_config(xp.node, pknd, &pknd_cfg);
+
+	return 0;
+}
+
+/**
+ * This function sets up scheduling parameters (pool, aura, sso group etc)
+ * of an interface (all ports/channels on that interface).
+ * @param xiface        interface number with node.
+ * @param intfsch      pointer to struct containing interface
+ *                      scheduling parameters.
+ * @param gblsch       pointer to struct containing global scheduling parameters
+ *                      (can be NULL if not used)
+ */
+int cvmx_helper_pki_init_interface(const int xiface,
+				   struct cvmx_pki_intf_schd *intfsch,
+				   struct cvmx_pki_global_schd *gblsch)
+{
+	const u16 num_ports = cvmx_helper_ports_on_interface(xiface);
+	u8 qos;
+	u16 port = num_ports;
+	u8 port_msb = 0;
+	u8 port_shift = 0;
+	u16 num_entry = 0;
+	u8 num_qos;
+	int pknd;
+	int rs;
+	int has_fcs;
+	int ipd_port;
+	int qpg_base;
+	u64 mbuff_size = 0;
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	enum cvmx_pki_qpg_qos qpg_qos = CVMX_PKI_QPG_QOS_NONE;
+	struct cvmx_pki_qpg_config qpg_cfg;
+	struct cvmx_pki_prt_schd *prtsch;
+	struct cvmx_pki_qos_schd *qossch;
+	struct cvmx_pki_style_config style_cfg;
+	struct cvmx_pki_pkind_config pknd_cfg;
+
+	has_fcs = __cvmx_helper_get_has_fcs(xiface);
+	memset(&qpg_cfg, 0, sizeof(qpg_cfg));
+
+	if (pki_helper_debug)
+		debug("pki-helper:intf-init:intf0x%x initialize--------------------------------\n",
+		      xiface);
+
+	if (!intfsch->pool_per_intf) {
+		if (gblsch) {
+			intfsch->_pool = gblsch->_pool;
+			intfsch->pool_num = gblsch->pool_num;
+		} else {
+			debug("ERROR:pki-helper:intf-init:intf0x%x: global scheduling is in use but is NULL\n",
+			      xiface);
+			return -1;
+		}
+	} else {
+		if (!intfsch) {
+			debug("ERROR:pki-helper:intf-init:intf0x%x: interface scheduling pointer is NULL\n",
+			      xiface);
+			return -1;
+		}
+		mbuff_size = intfsch->pool_buff_size;
+	}
+	if (!intfsch->aura_per_intf) {
+		intfsch->_aura = gblsch->_aura;
+		intfsch->aura_num = gblsch->aura_num;
+	}
+	if (!intfsch->sso_grp_per_intf)
+		intfsch->sso_grp = gblsch->sso_grp;
+
+	/* Allocate interface resources */
+	rs = __cvmx_helper_pki_intf_rsrcs(xi.node, intfsch);
+	if (rs)
+		return rs;
+
+	for (port = 0; port < num_ports; port++) {
+		prtsch = &intfsch->prt_s[port];
+
+		/* Skip invalid/disabled ports */
+		if (!cvmx_helper_is_port_valid(xiface, port) ||
+		    prtsch->cfg_port)
+			continue;
+
+		if (!prtsch->pool_per_prt) {
+			prtsch->pool_num = intfsch->pool_num;
+			prtsch->_pool = intfsch->_pool;
+			prtsch->pool_buff_size = intfsch->pool_buff_size;
+		} else if (prtsch->pool_buff_size < mbuff_size || !mbuff_size) {
+			mbuff_size = prtsch->pool_buff_size;
+		}
+		if (!prtsch->aura_per_prt) {
+			prtsch->aura_num = intfsch->aura_num;
+			prtsch->_aura = intfsch->_aura;
+		}
+		if (!prtsch->sso_grp_per_prt)
+			prtsch->sso_grp = intfsch->sso_grp;
+
+		rs = __cvmx_helper_pki_port_rsrcs(xi.node, prtsch);
+		if (rs)
+			return rs;
+
+		/* Port is using qpg qos to schedule packets to differnet aura or sso group */
+		num_qos = cvmx_helper_pki_get_num_qpg_entry(prtsch->qpg_qos);
+		if (pki_helper_debug)
+			debug("pki-helper:intf-init:intf%d: port %d used qpg_qos=%d\n",
+			      xiface, port, prtsch->qpg_qos);
+
+		/* All ports will share the aura from port 0 for the respective qos */
+		/* Port 0 should never have this set to TRUE **/
+		if (intfsch->qos_share_aura && (port != 0)) {
+			if (pki_helper_debug)
+				debug("pki-helper:intf-init:intf0x%x All ports will share same aura for all qos\n",
+				      xiface);
+			for (qos = 0; qos < num_qos; qos++) {
+				qossch = &prtsch->qos_s[qos];
+				prtsch->qpg_qos = intfsch->prt_s[0].qpg_qos;
+				qossch->pool_per_qos = intfsch->prt_s[0]
+							       .qos_s[qos]
+							       .pool_per_qos;
+				qossch->aura_per_qos = intfsch->prt_s[0]
+							       .qos_s[qos]
+							       .aura_per_qos;
+				qossch->pool_num =
+					intfsch->prt_s[0].qos_s[qos].pool_num;
+				qossch->_pool =
+					intfsch->prt_s[0].qos_s[qos]._pool;
+				qossch->aura_num =
+					intfsch->prt_s[0].qos_s[qos].aura_num;
+				qossch->_aura =
+					intfsch->prt_s[0].qos_s[qos]._aura;
+			}
+		}
+		if (intfsch->qos_share_grp && port != 0) {
+			if (pki_helper_debug)
+				debug("pki-helper:intf-init:intf0x%x: All ports will share same sso group for all qos\n",
+				      xiface);
+			for (qos = 0; qos < num_qos; qos++) {
+				qossch = &prtsch->qos_s[qos];
+				qossch->sso_grp_per_qos =
+					intfsch->prt_s[0]
+						.qos_s[qos]
+						.sso_grp_per_qos;
+				qossch->sso_grp =
+					intfsch->prt_s[0].qos_s[qos].sso_grp;
+			}
+		}
+		for (qos = 0; qos < num_qos; qos++) {
+			qossch = &prtsch->qos_s[qos];
+			if (!qossch->pool_per_qos) {
+				qossch->pool_num = prtsch->pool_num;
+				qossch->_pool = prtsch->_pool;
+				if (pki_helper_debug)
+					debug("pki-helper:intf-init:intf0x%x: qos %d has pool %d\n",
+					      xiface, qos, prtsch->pool_num);
+			} else if (qossch->pool_buff_size < mbuff_size ||
+				   !mbuff_size)
+				mbuff_size = qossch->pool_buff_size;
+			if (!qossch->aura_per_qos) {
+				qossch->aura_num = prtsch->aura_num;
+				qossch->_aura = prtsch->_aura;
+			}
+			if (!qossch->sso_grp_per_qos)
+				qossch->sso_grp = prtsch->sso_grp;
+			rs = __cvmx_helper_pki_qos_rsrcs(xi.node, qossch);
+			if (rs)
+				return rs;
+		}
+	}
+	/* Using port shift and port msb to schedule packets from differnt
+	 * port to differnt auras and different sso group
+	 */
+	/* Using QPG_QOS to schedule packets to different aura and sso group */
+	/* If ports needs to send packets to different aura and sso group
+	 * depending on packet qos
+	 */
+	/* We will need to set up aura and sso group for each port and each qos
+	 */
+	/* If all ports are using same style, they will be using same qpg_qos
+	 * so check only for port 0
+	 */
+	if (intfsch->style_per_intf) {
+		if (intfsch->prt_s[0].qpg_qos) {
+			/* all ports using same style will use same qos
+			 * defined in port 0 config
+			 */
+			qpg_qos = intfsch->prt_s[0].qpg_qos;
+			num_qos = cvmx_helper_pki_get_num_qpg_entry(
+				intfsch->prt_s[0].qpg_qos);
+			if (intfsch->qos_share_aura && intfsch->qos_share_grp) {
+				/* All ports will use same qpg offset so no
+				 * need for port_msb or port shift
+				 */
+				port_msb = 0;
+				port_shift = 0;
+				num_entry = num_qos;
+				qpg_base = intfsch->qpg_base;
+				rs = __cvmx_helper_pki_set_intf_qpg(xi.node, 0,
+								    qpg_base,
+								    num_entry,
+								    intfsch);
+				if (rs == -1)
+					return rs;
+				intfsch->qpg_base = rs;
+			} else {
+				port_msb = 8;
+				port_shift = __cvmx_helper_pki_port_shift(
+					xiface, intfsch->prt_s[0].qpg_qos);
+				if (pki_helper_debug) {
+					debug("pki-helper: num qpg entry needed %d\n",
+					      (int)num_entry);
+					debug("pki-helper:port_msb=%d port_shift=%d\n",
+					      port_msb, port_shift);
+				}
+				num_entry = num_qos;
+				for (port = 0; port < num_ports; port++) {
+					/* Skip invalid/disabled ports */
+					prtsch = &intfsch->prt_s[port];
+					if (!cvmx_helper_is_port_valid(xiface,
+								       port) ||
+					    prtsch->cfg_port)
+						continue;
+					ipd_port = cvmx_helper_get_ipd_port(
+						xiface, port);
+					qpg_base = intfsch->qpg_base +
+						   ((ipd_port & 0xff)
+						    << port_shift);
+					rs = __cvmx_helper_pki_set_intf_qpg(
+						xi.node, port, qpg_base,
+						num_entry, intfsch);
+					if (rs == -1)
+						return rs;
+					prtsch->qpg_base = rs;
+				}
+				intfsch->qpg_base = intfsch->prt_s[0].qpg_base;
+			}
+		} else if (intfsch->prt_s[0].aura_per_prt ||
+			   intfsch->prt_s[0].sso_grp_per_prt) {
+			/* Every port is using their own aura or group but no qos */
+			port_msb = 8;
+			port_shift = 0;
+			num_entry = 1;
+			if (pki_helper_debug)
+				debug("pki-helper: aura/grp_per_prt: num qpg entry needed %d\n",
+				      (int)num_entry);
+			for (port = 0; port < num_ports; port++) {
+				prtsch = &intfsch->prt_s[port];
+				/* Skip invalid/disabled ports */
+				if (!cvmx_helper_is_port_valid(xiface, port) ||
+				    prtsch->cfg_port)
+					continue;
+				ipd_port =
+					cvmx_helper_get_ipd_port(xiface, port);
+				qpg_base = intfsch->qpg_base +
+					   ((ipd_port & 0xff) << port_shift);
+				if (pki_helper_debug)
+					debug("port %d intf_q_base=%d q_base= %d\n",
+					      port, intfsch->qpg_base,
+					      qpg_base);
+				qpg_base = cvmx_pki_qpg_entry_alloc(
+					xi.node, qpg_base, num_entry);
+				if (qpg_base ==
+				    CVMX_RESOURCE_ALREADY_RESERVED) {
+					debug("pki-helper: INFO: qpg entries will be shared\n");
+				} else if (qpg_base ==
+					   CVMX_RESOURCE_ALLOC_FAILED) {
+					debug("pki-helper: ERROR: qpg entries not available\n");
+					return qpg_base;
+				}
+
+				if (intfsch->qpg_base < 0)
+					intfsch->qpg_base = qpg_base;
+				prtsch->qpg_base = qpg_base;
+
+				qpg_cfg.port_add = 0;
+				qpg_cfg.aura_num = prtsch->aura_num;
+				qpg_cfg.grp_ok = prtsch->sso_grp;
+				qpg_cfg.grp_bad = prtsch->sso_grp;
+				cvmx_pki_write_qpg_entry(xi.node, qpg_base,
+							 &qpg_cfg);
+			}
+			intfsch->qpg_base = intfsch->prt_s[0].qpg_base;
+		} else {
+			/* All ports on that intf use same port_add,
+			 * aura & sso grps
+			 */
+			/* All ports will use same qpg offset so no need for
+			 * port_msb or port shift
+			 */
+			port_msb = 0;
+			port_shift = 0;
+			num_entry = 1;
+			qpg_base = intfsch->qpg_base;
+			qpg_base = cvmx_pki_qpg_entry_alloc(xi.node, qpg_base,
+							    num_entry);
+			if (qpg_base == CVMX_RESOURCE_ALREADY_RESERVED) {
+				debug("pki-helper: INFO: qpg entries will be shared\n");
+			} else if (qpg_base == CVMX_RESOURCE_ALLOC_FAILED) {
+				debug("pki-helper: ERROR: qpg entries not available\n");
+				return qpg_base;
+			}
+			intfsch->qpg_base = qpg_base;
+
+			qpg_cfg.port_add = 0;
+			qpg_cfg.aura_num = intfsch->aura_num;
+			qpg_cfg.grp_ok = intfsch->sso_grp;
+			qpg_cfg.grp_bad = intfsch->sso_grp;
+			cvmx_pki_write_qpg_entry(xi.node, qpg_base, &qpg_cfg);
+		}
+		if (!mbuff_size) {
+			if (!gblsch->setup_pool) {
+				debug("No pool has setup for intf 0x%x\n",
+				      xiface);
+				return -1;
+			}
+			mbuff_size = gblsch->pool_buff_size;
+			debug("interface %d on node %d is using global pool\n",
+			      xi.interface, xi.node);
+		}
+		/* Allocate style here and map it to all ports on interface */
+		rs = cvmx_pki_style_alloc(xi.node, intfsch->style);
+		if (rs == CVMX_RESOURCE_ALREADY_RESERVED) {
+			debug("passthrough: INFO: style will be shared\n");
+		} else if (rs == CVMX_RESOURCE_ALLOC_FAILED) {
+			debug("passthrough: ERROR: style not available\n");
+			return CVMX_RESOURCE_ALLOC_FAILED;
+		}
+
+		intfsch->style = rs;
+		if (pki_helper_debug)
+			debug("style %d allocated intf 0x%x qpg_base %d\n",
+			      intfsch->style, xiface, intfsch->qpg_base);
+		style_cfg = pki_dflt_style[xi.node];
+		style_cfg.parm_cfg.qpg_qos = qpg_qos;
+		style_cfg.parm_cfg.qpg_base = intfsch->qpg_base;
+		style_cfg.parm_cfg.qpg_port_msb = port_msb;
+		style_cfg.parm_cfg.qpg_port_sh = port_shift;
+		style_cfg.parm_cfg.mbuff_size = mbuff_size;
+		cvmx_pki_write_style_config(xi.node, intfsch->style,
+					    CVMX_PKI_CLUSTER_ALL, &style_cfg);
+
+		for (port = 0; port < num_ports; port++) {
+			prtsch = &intfsch->prt_s[port];
+			/* Skip invalid/disabled ports */
+			if (!cvmx_helper_is_port_valid(xiface, port) ||
+			    prtsch->cfg_port)
+				continue;
+			prtsch->style = intfsch->style;
+			pknd = cvmx_helper_get_pknd(xiface, port);
+			cvmx_pki_read_pkind_config(xi.node, pknd, &pknd_cfg);
+			pknd_cfg.initial_style = intfsch->style;
+			pknd_cfg.fcs_pres = has_fcs;
+			cvmx_pki_write_pkind_config(xi.node, pknd, &pknd_cfg);
+		}
+	} else {
+		port_msb = 0;
+		port_shift = 0;
+		for (port = 0; port < num_ports; port++) {
+			prtsch = &intfsch->prt_s[port];
+			/* Skip invalid/disabled ports */
+			if (!cvmx_helper_is_port_valid(xiface, port) ||
+			    prtsch->cfg_port)
+				continue;
+			if (prtsch->qpg_qos && intfsch->qos_share_aura &&
+			    intfsch->qos_share_grp && port != 0) {
+				if (pki_helper_debug)
+					debug("intf 0x%x has all ports share qos aura n grps\n",
+					      xiface);
+				/* Ports have differnet styles but want
+				 * to share same qpg entries.
+				 * this might never be the case
+				 */
+				prtsch->qpg_base = intfsch->prt_s[0].qpg_base;
+			}
+			ipd_port = cvmx_helper_get_ipd_port(xiface, port);
+			cvmx_helper_pki_init_port(ipd_port, prtsch);
+		}
+	}
+	return 0;
+}
+
+/**
+ * This function gets all the PKI parameters related to that
+ * particular port from hardware.
+ * @param xipd_port	xipd_port port number with node to get parameter of
+ * @param port_cfg	pointer to structure where to store read parameters
+ */
+void cvmx_pki_get_port_config(int xipd_port,
+			      struct cvmx_pki_port_config *port_cfg)
+{
+	int xiface, index, pknd;
+	int style, cl_mask;
+	cvmx_pki_icgx_cfg_t pki_cl_msk;
+	struct cvmx_xport xp = cvmx_helper_ipd_port_to_xport(xipd_port);
+
+	/* get the pkind used by this ipd port */
+	xiface = cvmx_helper_get_interface_num(xipd_port);
+	index = cvmx_helper_get_interface_index_num(xipd_port);
+	pknd = cvmx_helper_get_pknd(xiface, index);
+
+	cvmx_pki_read_pkind_config(xp.node, pknd, &port_cfg->pkind_cfg);
+	style = port_cfg->pkind_cfg.initial_style;
+	pki_cl_msk.u64 = csr_rd_node(
+		xp.node, CVMX_PKI_ICGX_CFG(port_cfg->pkind_cfg.cluster_grp));
+	cl_mask = pki_cl_msk.s.clusters;
+	cvmx_pki_read_style_config(xp.node, style, cl_mask,
+				   &port_cfg->style_cfg);
+}
+
+/**
+ * This function sets all the PKI parameters related to that
+ * particular port in hardware.
+ * @param xipd_port	ipd port number with node to get parameter of
+ * @param port_cfg	pointer to structure containing port parameters
+ */
+void cvmx_pki_set_port_config(int xipd_port,
+			      struct cvmx_pki_port_config *port_cfg)
+{
+	int xiface, index, pknd;
+	int style, cl_mask;
+	cvmx_pki_icgx_cfg_t pki_cl_msk;
+	struct cvmx_xport xp = cvmx_helper_ipd_port_to_xport(xipd_port);
+
+	/* get the pkind used by this ipd port */
+	xiface = cvmx_helper_get_interface_num(xipd_port);
+	index = cvmx_helper_get_interface_index_num(xipd_port);
+	pknd = cvmx_helper_get_pknd(xiface, index);
+
+	if (cvmx_pki_write_pkind_config(xp.node, pknd, &port_cfg->pkind_cfg))
+		return;
+	style = port_cfg->pkind_cfg.initial_style;
+	pki_cl_msk.u64 = csr_rd_node(
+		xp.node, CVMX_PKI_ICGX_CFG(port_cfg->pkind_cfg.cluster_grp));
+	cl_mask = pki_cl_msk.s.clusters;
+	cvmx_pki_write_style_config(xp.node, style, cl_mask,
+				    &port_cfg->style_cfg);
+}
+
+/**
+ * This function displays all the PKI parameters related to that
+ * particular port.
+ * @param xipd_port	ipd port number to display parameter of
+ */
+void cvmx_helper_pki_show_port_config(int xipd_port)
+{
+	int xiface, index, pknd;
+	struct cvmx_xport xp = cvmx_helper_ipd_port_to_xport(xipd_port);
+
+	xiface = cvmx_helper_get_interface_num(xipd_port);
+	index = cvmx_helper_get_interface_index_num(xipd_port);
+	pknd = cvmx_helper_get_pknd(xiface, index);
+	debug("Showing stats for intf 0x%x port %d------------------\n", xiface,
+	      index);
+	cvmx_pki_show_pkind_attributes(xp.node, pknd);
+	debug("END STAUS------------------------\n\n");
+}
+
+void cvmx_helper_pki_errata(int node)
+{
+	struct cvmx_pki_global_config gbl_cfg;
+
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_X)) {
+		cvmx_pki_read_global_config(node, &gbl_cfg);
+		gbl_cfg.fpa_wait = CVMX_PKI_WAIT_PKT;
+		cvmx_pki_write_global_config(node, &gbl_cfg);
+	}
+}
+
+static const char *pki_ltype_sprint(int ltype)
+{
+	switch (ltype) {
+	case CVMX_PKI_LTYPE_E_ENET:
+		return "(ENET)";
+	case CVMX_PKI_LTYPE_E_VLAN:
+		return "(VLAN)";
+	case CVMX_PKI_LTYPE_E_SNAP_PAYLD:
+		return "(SNAP_PAYLD)";
+	case CVMX_PKI_LTYPE_E_ARP:
+		return "(ARP)";
+	case CVMX_PKI_LTYPE_E_RARP:
+		return "(RARP)";
+	case CVMX_PKI_LTYPE_E_IP4:
+		return "(IP4)";
+	case CVMX_PKI_LTYPE_E_IP4_OPT:
+		return "(IP4_OPT)";
+	case CVMX_PKI_LTYPE_E_IP6:
+		return "(IP6)";
+	case CVMX_PKI_LTYPE_E_IP6_OPT:
+		return "(IP6_OPT)";
+	case CVMX_PKI_LTYPE_E_IPSEC_ESP:
+		return "(IPSEC_ESP)";
+	case CVMX_PKI_LTYPE_E_IPFRAG:
+		return "(IPFRAG)";
+	case CVMX_PKI_LTYPE_E_IPCOMP:
+		return "(IPCOMP)";
+	case CVMX_PKI_LTYPE_E_TCP:
+		return "(TCP)";
+	case CVMX_PKI_LTYPE_E_UDP:
+		return "(UDP)";
+	case CVMX_PKI_LTYPE_E_SCTP:
+		return "(SCTP)";
+	case CVMX_PKI_LTYPE_E_UDP_VXLAN:
+		return "(UDP_VXLAN)";
+	case CVMX_PKI_LTYPE_E_GRE:
+		return "(GRE)";
+	case CVMX_PKI_LTYPE_E_NVGRE:
+		return "(NVGRE)";
+	case CVMX_PKI_LTYPE_E_GTP:
+		return "(GTP)";
+	default:
+		return "";
+	}
+}
+
+void cvmx_pki_dump_wqe(const cvmx_wqe_78xx_t *wqp)
+{
+	int i;
+	/* it is not cvmx_shared so per core only */
+	static u64 count;
+
+	debug("Wqe entry for packet %lld\n", (unsigned long long)count++);
+	debug("    WORD%02d: %016llx", 0, (unsigned long long)wqp->word0.u64);
+	debug(" aura=0x%x", wqp->word0.aura);
+	debug(" apad=%d", wqp->word0.apad);
+	debug(" chan=0x%x", wqp->word0.channel);
+	debug(" bufs=%d", wqp->word0.bufs);
+	debug(" style=0x%x", wqp->word0.style);
+	debug(" pknd=0x%x", wqp->word0.pknd);
+	debug("\n");
+	debug("    WORD%02d: %016llx", 1, (unsigned long long)wqp->word1.u64);
+	debug(" len=%d", wqp->word1.len);
+	debug(" grp=0x%x", wqp->word1.grp);
+	debug(" tt=%s", OCT_TAG_TYPE_STRING(wqp->word1.tag_type));
+	debug(" tag=0x%08x", wqp->word1.tag);
+	debug("\n");
+	if (wqp->word2.u64) {
+		debug("    WORD%02d: %016llx", 2,
+		      (unsigned long long)wqp->word2.u64);
+		if (wqp->word2.le_hdr_type)
+			debug(" [LAE]");
+		if (wqp->word2.lb_hdr_type)
+			debug(" lbty=%d%s", wqp->word2.lb_hdr_type,
+			      pki_ltype_sprint(wqp->word2.lb_hdr_type));
+		if (wqp->word2.lc_hdr_type)
+			debug(" lcty=%d%s", wqp->word2.lc_hdr_type,
+			      pki_ltype_sprint(wqp->word2.lc_hdr_type));
+		if (wqp->word2.ld_hdr_type)
+			debug(" ldty=%d%s", wqp->word2.ld_hdr_type,
+			      pki_ltype_sprint(wqp->word2.ld_hdr_type));
+		if (wqp->word2.le_hdr_type)
+			debug(" lety=%d%s", wqp->word2.le_hdr_type,
+			      pki_ltype_sprint(wqp->word2.le_hdr_type));
+		if (wqp->word2.lf_hdr_type)
+			debug(" lfty=%d%s", wqp->word2.lf_hdr_type,
+			      pki_ltype_sprint(wqp->word2.lf_hdr_type));
+		if (wqp->word2.lg_hdr_type)
+			debug(" lgty=%d%s", wqp->word2.lg_hdr_type,
+			      pki_ltype_sprint(wqp->word2.lg_hdr_type));
+		if (wqp->word2.pcam_flag1)
+			debug(" PF1");
+		if (wqp->word2.pcam_flag2)
+			debug(" PF2");
+		if (wqp->word2.pcam_flag3)
+			debug(" PF3");
+		if (wqp->word2.pcam_flag4)
+			debug(" PF4");
+		if (wqp->word2.vlan_valid || wqp->word2.vlan_stacked) {
+			if (wqp->word2.vlan_valid)
+				debug(" vlan valid");
+			if (wqp->word2.vlan_stacked)
+				debug(" vlan stacked");
+			debug(" ");
+		}
+		if (wqp->word2.stat_inc)
+			debug(" stat_inc");
+		if (wqp->word2.is_frag)
+			debug(" L3 Fragment");
+		if (wqp->word2.is_l3_bcast)
+			debug(" L3 Broadcast");
+		if (wqp->word2.is_l3_mcast)
+			debug(" L3 Multicast");
+		if (wqp->word2.is_l2_bcast)
+			debug(" L2 Broadcast");
+		if (wqp->word2.is_l2_mcast)
+			debug(" L2 Multicast");
+		if (wqp->word2.is_raw)
+			debug(" RAW");
+		if (wqp->word2.err_level || wqp->word2.err_code) {
+			debug(" errlev=%d", wqp->word2.err_level);
+			debug(" opcode=0x%x", wqp->word2.err_code);
+		}
+		debug("\n");
+	}
+	debug("    WORD%02d: %016llx", 3,
+	      (unsigned long long)wqp->packet_ptr.u64);
+
+	debug(" size=%d", wqp->packet_ptr.size);
+	debug(" addr=0x%llx", (unsigned long long)wqp->packet_ptr.addr);
+
+	debug("\n");
+	if (wqp->word4.u64) {
+		debug("    WORD%02d: %016llx", 4,
+		      (unsigned long long)wqp->word4.u64);
+		if (wqp->word4.ptr_layer_a)
+			debug(" laptr=%d", wqp->word4.ptr_layer_a);
+		if (wqp->word4.ptr_layer_b)
+			debug(" lbptr=%d", wqp->word4.ptr_layer_b);
+		if (wqp->word4.ptr_layer_c)
+			debug(" lcptr=%d", wqp->word4.ptr_layer_c);
+		if (wqp->word4.ptr_layer_d)
+			debug(" ldptr=%d", wqp->word4.ptr_layer_d);
+		if (wqp->word4.ptr_layer_e)
+			debug(" leptr=%d", wqp->word4.ptr_layer_e);
+		if (wqp->word4.ptr_layer_f)
+			debug(" lfptr=%d", wqp->word4.ptr_layer_f);
+		if (wqp->word4.ptr_layer_g)
+			debug(" lgptr=%d", wqp->word4.ptr_layer_g);
+		if (wqp->word4.ptr_vlan)
+			debug(" vlptr=%d", wqp->word4.ptr_vlan);
+		debug("\n");
+	}
+	for (i = 0; i < 10; ++i) {
+		if (wqp->wqe_data[i])
+			debug("    WORD%02d: %016llx\n", i + 5,
+			      (unsigned long long)wqp->wqe_data[i]);
+	}
+}
+
+/**
+ * Modifies maximum frame length to check.
+ * It modifies the global frame length set used by this port, any other
+ * port using the same set will get affected too.
+ * @param xipd_port	ipd port for which to modify max len.
+ * @param max_size	maximum frame length
+ */
+void cvmx_pki_set_max_frm_len(int ipd_port, uint32_t max_size)
+{
+	/* On CN78XX frame check is enabled for a style n and
+	 * PKI_CLX_STYLE_CFG[minmax_sel] selects which set of
+	 * MAXLEN/MINLEN to use.
+	 */
+	int xiface, index, pknd;
+	cvmx_pki_clx_stylex_cfg_t style_cfg;
+	cvmx_pki_frm_len_chkx_t frame_len;
+	struct cvmx_xport xp = cvmx_helper_ipd_port_to_xport(ipd_port);
+	int cluster = 0;
+	int style;
+	int sel;
+
+	/* get the pkind used by this ipd port */
+	xiface = cvmx_helper_get_interface_num(ipd_port);
+	index = cvmx_helper_get_interface_index_num(ipd_port);
+	pknd = cvmx_helper_get_pknd(xiface, index);
+
+	style = cvmx_pki_get_pkind_style(xp.node, pknd);
+	style_cfg.u64 =
+		csr_rd_node(xp.node, CVMX_PKI_CLX_STYLEX_CFG(style, cluster));
+	sel = style_cfg.s.minmax_sel;
+	frame_len.u64 = csr_rd_node(xp.node, CVMX_PKI_FRM_LEN_CHKX(sel));
+	frame_len.s.maxlen = max_size;
+	csr_wr_node(xp.node, CVMX_PKI_FRM_LEN_CHKX(sel), frame_len.u64);
+}
+
+/**
+ * This function sets up all th eports of particular interface
+ * for chosen fcs mode. (only use for backward compatibility).
+ * New application can control it via init_interface calls.
+ * @param node		node number.
+ * @param interface	interface number.
+ * @param nports	number of ports
+ * @param has_fcs	1 -- enable fcs check and fcs strip.
+ *			0 -- disable fcs check.
+ */
+void cvmx_helper_pki_set_fcs_op(int node, int interface, int nports,
+				int has_fcs)
+{
+	int xiface, index;
+	int pknd;
+	unsigned int cluster = 0;
+	cvmx_pki_clx_pkindx_cfg_t pkind_cfg;
+
+	xiface = cvmx_helper_node_interface_to_xiface(node, interface);
+	for (index = 0; index < nports; index++) {
+		pknd = cvmx_helper_get_pknd(xiface, index);
+		while (cluster < CVMX_PKI_NUM_CLUSTER) {
+			/*find the cluster in use pass2*/
+			pkind_cfg.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_PKINDX_CFG(pknd, cluster));
+			pkind_cfg.s.fcs_pres = has_fcs;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PKINDX_CFG(pknd, cluster),
+				    pkind_cfg.u64);
+			cluster++;
+		}
+		/* make sure fcs_strip and fcs_check is also enable/disable
+		 * for the style used by that port
+		 */
+		cvmx_pki_endis_fcs_check(node, pknd, has_fcs, has_fcs);
+		cluster = 0;
+	}
+}
+
+/**
+ * This function sets the wqe buffer mode of all ports. First packet data buffer can reside
+ * either in same buffer as wqe OR it can go in separate buffer. If used the later mode,
+ * make sure software allocate enough buffers to now have wqe separate from packet data.
+ * @param node			node number.
+ * @param pkt_outside_wqe	0 = The packet link pointer will be at word [FIRST_SKIP]
+ *				immediately followed by packet data, in the same buffer
+ *				as the work queue entry.
+ *				1 = The packet link pointer will be at word [FIRST_SKIP] in a new
+ *				buffer separate from the work queue entry. Words following the
+ *				WQE in the same cache line will be zeroed, other lines in the
+ *				buffer will not be modified and will retain stale data (from the
+ *				buffer’s previous use). This setting may decrease the peak PKI
+ *				performance by up to half on small packets.
+ */
+void cvmx_helper_pki_set_wqe_mode(int node, bool pkt_outside_wqe)
+{
+	int interface, xiface, port, pknd;
+	int num_intf, num_ports;
+	u64 style;
+
+	/* get the pkind used by this ipd port */
+	num_intf = cvmx_helper_get_number_of_interfaces();
+	for (interface = 0; interface < num_intf; interface++) {
+		num_ports = cvmx_helper_ports_on_interface(interface);
+		/*Skip invalid/disabled interfaces */
+		if (num_ports <= 0)
+			continue;
+		xiface = cvmx_helper_node_interface_to_xiface(node, interface);
+		for (port = 0; port < num_ports; port++) {
+			pknd = cvmx_helper_get_pknd(xiface, port);
+			style = cvmx_pki_get_pkind_style(node, pknd);
+			cvmx_pki_set_wqe_mode(node, style, pkt_outside_wqe);
+		}
+	}
+}
+
+/**
+ * This function sets the Packet mode of all ports and styles to little-endian.
+ * It Changes write operations of packet data to L2C to
+ * be in little-endian. Does not change the WQE header format, which is
+ * properly endian neutral.
+ * @param node		node number.
+ */
+void cvmx_helper_pki_set_little_endian(int node)
+{
+	int interface, xiface, port, pknd;
+	int num_intf, num_ports;
+	u64 style;
+
+	/* get the pkind used by this ipd port */
+	num_intf = cvmx_helper_get_number_of_interfaces();
+	for (interface = 0; interface < num_intf; interface++) {
+		num_ports = cvmx_helper_ports_on_interface(interface);
+		/*Skip invalid/disabled interfaces */
+		if (num_ports <= 0)
+			continue;
+		xiface = cvmx_helper_node_interface_to_xiface(node, interface);
+		for (port = 0; port < num_ports; port++) {
+			pknd = cvmx_helper_get_pknd(xiface, port);
+			style = cvmx_pki_get_pkind_style(node, pknd);
+			cvmx_pki_set_little_endian(node, style);
+		}
+	}
+}
+
+/**
+ * This function modifies the sso group where packets from specified port needs to be routed
+ * @param ipd_port			pki port number.
+ * @param grp_ok			sso group where good packets are routed
+ * @param grp_bad			sso group where errored packets are routed
+ * NOTE: This function assumes that each port has its own style/profile and is not using qpg qos
+ */
+void cvmx_helper_pki_modify_prtgrp(int xipd_port, int grp_ok, int grp_bad)
+{
+	struct cvmx_xport xp = cvmx_helper_ipd_port_to_xport(xipd_port);
+	struct cvmx_pki_port_config port_cfg;
+	struct cvmx_pki_qpg_config qpg_cfg;
+	int index;
+
+	cvmx_pki_get_port_config(xipd_port, &port_cfg);
+	/* TODO: expand it to calculate index in other cases hrm:10.5.3*/
+	index = port_cfg.style_cfg.parm_cfg.qpg_base;
+	cvmx_pki_read_qpg_entry(xp.node, index, &qpg_cfg);
+	qpg_cfg.grp_ok = grp_ok;
+	qpg_cfg.grp_bad = grp_bad;
+	cvmx_pki_write_qpg_entry(xp.node, index, &qpg_cfg);
+}
+
+int cvmx_pki_clone_style(int node, int style, u64 cluster_mask)
+{
+	int new_style;
+	struct cvmx_pki_style_config style_cfg;
+
+	cvmx_pki_read_style_config(node, style, cluster_mask, &style_cfg);
+	new_style = cvmx_pki_style_alloc(node, CVMX_PKI_FIND_AVAL_ENTRY);
+	if (new_style < 0)
+		return -1;
+	cvmx_pki_write_style_config(node, new_style, cluster_mask, &style_cfg);
+	return new_style;
+}
+
+/* Optimize if use at runtime */
+int cvmx_pki_add_entry(u64 *array, u64 match, int index, int num_entries)
+{
+	if (index >= num_entries)
+		return -1;
+	array[index] = match;
+	return 0;
+}
+
+/* Optimize if use at runtime */
+int cvmx_pki_find_entry(u64 *array, u64 match, int num_entries)
+{
+	int i;
+
+	for (i = 0; i < num_entries; i++) {
+		if ((array[i] & 0xffffffffff) == match)
+			return i;
+	}
+	return -1;
+}
+
+/**
+ * This function send the packets to specified style/profile if
+ * specified mac address and specified input style/profile matches.
+ * @param node			node number.
+ * @param style			style/profile to match against
+ * @param mac_addr		mac address to match
+ * @param mac_addr_mask		mask of mac address bits
+ *                              1: exact match
+ *				0: don't care
+ *				ex: to exactly match mac address 0x0a0203040506
+ *                                  mask = 0xffffffffffff
+ *                                  to match only first 2 bytes  0x0a02xxxxxxxx
+ *                                  mask = 0xffff00000000
+ * @param final_style		final style (contains aura/sso_grp etc) to
+ *                              route matched packet to.
+ */
+int cvmx_helper_pki_route_dmac(int node, int style, u64 mac_addr,
+			       u64 mac_addr_mask, int final_style)
+{
+	struct cvmx_pki_pcam_input pcam_input;
+	struct cvmx_pki_pcam_action pcam_action;
+	int bank;
+	int index;
+	int interim_style = style;
+	u64 cl_mask = CVMX_PKI_CLUSTER_ALL;
+	u32 data_to_match;
+	u32 data_to_mask;
+	u64 match_h;
+	u64 match_l;
+
+	memset(&pcam_input, 0, sizeof(pcam_input));
+	memset(&pcam_action, 0, sizeof(pcam_action));
+	data_to_match =
+		(mac_addr >> CVMX_PKI_DMACH_SHIFT) & CVMX_PKI_DMACH_MASK;
+	data_to_mask =
+		(mac_addr_mask >> CVMX_PKI_DMACH_SHIFT) & CVMX_PKI_DMACH_MASK;
+	match_h = (u64)(data_to_match & data_to_mask) | (u64)(style << 16);
+	if (!data_to_mask)
+		goto pcam_dmacl;
+	index = cvmx_pki_find_entry(pcam_dmach, match_h,
+				    CVMX_PKI_NUM_PCAM_ENTRY);
+
+	if (index >= 0) {
+		interim_style = (pcam_dmach[index] >> 40) & 0xffffffffff;
+		goto pcam_dmacl;
+	}
+	bank = 0;
+	index = cvmx_pki_pcam_entry_alloc(node, CVMX_PKI_FIND_AVAL_ENTRY, bank,
+					  cl_mask);
+	if (index < 0) {
+		debug("ERROR: Allocating pcam entry node=%d bank=%d\n", node,
+		      bank);
+		return -1;
+	}
+	pcam_input.style = style;
+	pcam_input.style_mask = 0xffffffffffffffff;
+	pcam_input.field = CVMX_PKI_PCAM_TERM_DMACH;
+	pcam_input.field_mask = 0xff;
+	pcam_input.data = data_to_match;
+	pcam_input.data_mask = data_to_mask;
+	pcam_action.parse_mode_chg = CVMX_PKI_PARSE_NO_CHG;
+	pcam_action.parse_flag_set = 0;
+	pcam_action.layer_type_set = CVMX_PKI_LTYPE_E_NONE;
+	interim_style = cvmx_pki_clone_style(node, style, cl_mask);
+	if (interim_style < 0) {
+		debug("ERROR: Failed to allocate interim style\n");
+		return -1;
+	}
+	pcam_action.style_add = interim_style - style;
+	pcam_action.pointer_advance = 0;
+	cvmx_pki_pcam_write_entry(node, index, cl_mask, pcam_input,
+				  pcam_action); /*cluster_mask in pass2*/
+	match_h |= (u64)(((u64)interim_style << 40) & 0xff0000000000);
+	cvmx_pki_add_entry(pcam_dmach, match_h, index, CVMX_PKI_NUM_PCAM_ENTRY);
+pcam_dmacl:
+	bank = 1;
+	data_to_match = (mac_addr & CVMX_PKI_DMACL_MASK);
+	data_to_mask = (mac_addr_mask & CVMX_PKI_DMACL_MASK);
+	if (!data_to_mask)
+		return 0;
+	match_l = (u64)(data_to_match & data_to_mask) |
+		  ((u64)interim_style << 32);
+	if (cvmx_pki_find_entry(pcam_dmacl, match_l, CVMX_PKI_NUM_PCAM_ENTRY) >=
+	    0)
+		return 0;
+	index = cvmx_pki_pcam_entry_alloc(node, CVMX_PKI_FIND_AVAL_ENTRY, bank,
+					  cl_mask);
+	if (index < 0) {
+		debug("ERROR: Allocating pcam entry node=%d bank=%d\n", node,
+		      bank);
+		return -1;
+	}
+	cvmx_pki_add_entry(pcam_dmacl, match_l, index, CVMX_PKI_NUM_PCAM_ENTRY);
+	pcam_input.style = interim_style;
+	pcam_input.style_mask = 0xffffffffffffffff;
+	pcam_input.field = CVMX_PKI_PCAM_TERM_DMACL;
+	pcam_input.field_mask = 0xff;
+	pcam_input.data = data_to_match;
+	pcam_input.data_mask = data_to_mask;
+	/* customer need to decide if they want to resume parsing or terminate
+	 * it, if further match found in pcam it will take precedence
+	 */
+	pcam_action.parse_mode_chg = CVMX_PKI_PARSE_NO_CHG;
+	pcam_action.parse_flag_set = 0;
+	pcam_action.layer_type_set = CVMX_PKI_LTYPE_E_NONE;
+	pcam_action.style_add = final_style - interim_style;
+	pcam_action.pointer_advance = 0;
+	cvmx_pki_pcam_write_entry(node, index, cl_mask, pcam_input,
+				  pcam_action); /*cluster_mask in pass2*/
+
+	return 0;
+}
+
+/**
+ * This function send the packets to specified sso group if
+ * specified mac address and specified input port matches.
+ * NOTE: This function will always create a new style/profile for the specified
+ * sso group even if style/profile already exist and if the style used by this ipd port is
+ * shared all the ports using that style will get affected.
+ * similar function to use: cvmx_helper_pki_route_dmac()
+ * @param node			node number.
+ * @param ipd_port		ipd port on which mac address match needs to be performed.
+ * @param mac_addr		mac address to match
+ * @param mac_addr_mask		mask of mac address bits
+ *                              1: exact match
+ *				0: don't care
+ *				ex: to exactly match mac address 0x0a0203040506
+ *                                  mask = 0xffffffffffff
+ *                                  to match only first 2 bytes  0x0a02xxxxxxxx
+ *                                  mask = 0xffff00000000
+ * @param grp			sso group to route matched packet to.
+ * @return			success: final style containing routed sso group
+ *				fail: -1
+ */
+int cvmx_helper_pki_route_prt_dmac(int xipd_port, u64 mac_addr,
+				   u64 mac_addr_mask, int grp)
+{
+	int style;
+	int new_style;
+	int offset, index;
+	struct cvmx_pki_style_config st_cfg;
+	struct cvmx_pki_port_config port_cfg;
+	struct cvmx_pki_qpg_config qpg_cfg;
+	struct cvmx_xport xp = cvmx_helper_ipd_port_to_xport(xipd_port);
+	int node = xp.node;
+
+	/* 1. Get the current/initial style config used by this port */
+	cvmx_pki_get_port_config(xipd_port, &port_cfg);
+	style = port_cfg.pkind_cfg.initial_style;
+	st_cfg = port_cfg.style_cfg;
+
+	/* 2. Create new style/profile from current and modify it to steer
+	 * traffic to specified grp
+	 */
+	new_style = cvmx_pki_style_alloc(node, CVMX_PKI_FIND_AVAL_ENTRY);
+	if (new_style < 0) {
+		cvmx_printf("ERROR: %s: new style not available\n", __func__);
+		return -1;
+	}
+	offset = st_cfg.parm_cfg.qpg_base;
+	cvmx_pki_read_qpg_entry(node, offset, &qpg_cfg);
+	qpg_cfg.qpg_base = CVMX_PKI_FIND_AVAL_ENTRY;
+	qpg_cfg.grp_ok = grp;
+	qpg_cfg.grp_bad = grp;
+	index = cvmx_helper_pki_set_qpg_entry(node, &qpg_cfg);
+	if (index < 0) {
+		cvmx_printf("ERROR: %s: new qpg entry not available\n",
+			    __func__);
+		return -1;
+	}
+	st_cfg.parm_cfg.qpg_base = index;
+	cvmx_pki_write_style_config(node, new_style, CVMX_PKI_CLUSTER_ALL,
+				    &st_cfg);
+	cvmx_helper_pki_route_dmac(node, style, mac_addr, mac_addr_mask,
+				   new_style);
+	return new_style;
+}
Signature · 1 line
2.35.1
#17 lore raw

PATCH 18/52 mips: octeon: Add cvmx-helper-pko.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-helper-pko.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-helper-pko.c | 312 ++++++++++++++++++++++++
 1 file changed, 312 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-helper-pko.c
arch/mips/mach-octeon/cvmx-helper-pko.c new file +312 -0
new file mode 100644
index 000000000000..0dc7980b25f2
@@ -0,0 +1,312 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * Helper Functions for the PKO
+ */
+
+#include <errno.h>
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+#include <mach/cvmx-range.h>
+#include <mach/cvmx-global-resources.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-ipd.h>
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-pko3.h>
+#include <mach/cvmx-pko3-queue.h>
+#include <mach/cvmx-pko3-resources.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#include <mach/cvmx-helper-bgx.h>
+#include <mach/cvmx-helper-cfg.h>
+#include <mach/cvmx-helper-util.h>
+#include <mach/cvmx-helper-pki.h>
+
+static s64 pko_fpa_config_pool = -1;
+static u64 pko_fpa_config_size = 1024;
+static u64 pko_fpa_config_count;
+
+/**
+ * cvmx_override_pko_queue_priority(int pko_port, u64
+ * priorities[16]) is a function pointer. It is meant to allow
+ * customization of the PKO queue priorities based on the port
+ * number. Users should set this pointer to a function before
+ * calling any cvmx-helper operations.
+ */
+void (*cvmx_override_pko_queue_priority)(int ipd_port,
+					 uint8_t *priorities) = NULL;
+
+void cvmx_pko_set_cmd_que_pool_config(s64 pool, u64 buffer_size,
+				      u64 buffer_count)
+{
+	pko_fpa_config_pool = pool;
+	pko_fpa_config_size = buffer_size;
+	pko_fpa_config_count = buffer_count;
+}
+
+void cvmx_pko_set_cmd_queue_pool_buffer_count(u64 buffer_count)
+{
+	pko_fpa_config_count = buffer_count;
+}
+
+void cvmx_pko_get_cmd_que_pool_config(cvmx_fpa_pool_config_t *pko_pool)
+{
+	pko_pool->pool_num = pko_fpa_config_pool;
+	pko_pool->buffer_size = pko_fpa_config_size;
+	pko_pool->buffer_count = pko_fpa_config_count;
+}
+
+int64_t cvmx_fpa_get_pko_pool(void)
+{
+	return pko_fpa_config_pool;
+}
+
+/**
+ * Gets the buffer size of pko pool
+ */
+u64 cvmx_fpa_get_pko_pool_block_size(void)
+{
+	return pko_fpa_config_size;
+}
+
+/**
+ * Gets the buffer size  of pko pool
+ */
+u64 cvmx_fpa_get_pko_pool_buffer_count(void)
+{
+	return pko_fpa_config_count;
+}
+
+/**
+ * Initialize PKO command queue buffer pool
+ */
+static int cvmx_helper_pko_pool_init(void)
+{
+	u8 pool;
+	unsigned int buf_count;
+	unsigned int pkt_buf_count;
+	int rc;
+
+	/* Reserve pool */
+	pool = cvmx_fpa_get_pko_pool();
+
+	/* Avoid redundant pool creation */
+	if (cvmx_fpa_get_block_size(pool) > 0) {
+#ifdef DEBUG
+		debug("WARNING: %s: pool %d already initialized\n", __func__,
+		      pool);
+#endif
+		/* It is up to the app to have sufficient buffer count */
+		return pool;
+	}
+
+	/* Calculate buffer count: one per queue + 3-word-cmds * max_pkts */
+	pkt_buf_count = cvmx_fpa_get_packet_pool_buffer_count();
+	buf_count = CVMX_PKO_MAX_OUTPUT_QUEUES + (pkt_buf_count * 3) / 8;
+
+	/* Allocate pools for pko command queues */
+	rc = __cvmx_helper_initialize_fpa_pool(pool,
+					       cvmx_fpa_get_pko_pool_block_size(),
+					       buf_count, "PKO Cmd-bufs");
+
+	if (rc < 0)
+		debug("%s: ERROR: in PKO buffer pool\n", __func__);
+
+	pool = rc;
+	return pool;
+}
+
+/**
+ * Initialize the PKO
+ *
+ */
+int cvmx_helper_pko_init(void)
+{
+	int rc;
+
+	rc = cvmx_helper_pko_pool_init();
+	if (rc < 0)
+		return rc;
+
+	__cvmx_helper_init_port_config_data(0);
+
+	cvmx_pko_hw_init(cvmx_fpa_get_pko_pool(),
+			 cvmx_fpa_get_pko_pool_block_size());
+	return 0;
+}
+
+/**
+ * @INTERNAL
+ * Setup the PKO for the ports on an interface. The number of
+ * queues per port and the priority of each PKO output queue
+ * is set here. PKO must be disabled when this function is called.
+ *
+ * @param interface to setup PKO for
+ *
+ * @return Zero on success, negative on failure
+ *
+ * @note This is for PKO1/PKO2, and is not used for PKO3.
+ */
+int __cvmx_helper_interface_setup_pko(int interface)
+{
+	/*
+	 * Each packet output queue has an associated priority. The
+	 * higher the priority, the more often it can send a packet. A
+	 * priority of 8 means it can send in all 8 rounds of
+	 * contention. We're going to make each queue one less than
+	 * the last.  The vector of priorities has been extended to
+	 * support CN5xxx CPUs, where up to 16 queues can be
+	 * associated to a port.  To keep backward compatibility we
+	 * don't change the initial 8 priorities and replicate them in
+	 * the second half.  With per-core PKO queues (PKO lockless
+	 * operation) all queues have the same priority.
+	 */
+	/* uint8_t priorities[16] = {8,7,6,5,4,3,2,1,8,7,6,5,4,3,2,1}; */
+	u8 priorities[16] = { [0 ... 15] = 8 };
+
+	/*
+	 * Setup the IPD/PIP and PKO for the ports discovered
+	 * above. Here packet classification, tagging and output
+	 * priorities are set.
+	 */
+	int num_ports = cvmx_helper_ports_on_interface(interface);
+
+	while (num_ports--) {
+		int ipd_port;
+
+		if (!cvmx_helper_is_port_valid(interface, num_ports))
+			continue;
+
+		ipd_port = cvmx_helper_get_ipd_port(interface, num_ports);
+		/*
+		 * Give the user a chance to override the per queue
+		 * priorities.
+		 */
+		if (cvmx_override_pko_queue_priority)
+			cvmx_override_pko_queue_priority(ipd_port, priorities);
+
+		cvmx_pko_config_port(ipd_port,
+				     cvmx_pko_get_base_queue(ipd_port),
+				     cvmx_pko_get_num_queues(ipd_port),
+				     priorities);
+		ipd_port++;
+	}
+	return 0;
+	/* NOTE:
+	 * Now this function is called for all chips including 68xx,
+	 * but on the 68xx it does not enable multiple pko_iports per
+	 * eport, while before it was doing 3 pko_iport per eport
+	 * buf the reason for that is not clear.
+	 */
+}
+
+/**
+ * wait for the pko queue to drain
+ *
+ * @param queue a valid pko queue
+ * @return count is the length of the queue after calling this
+ * function
+ */
+static int cvmx_helper_wait_pko_queue_drain(int queue)
+{
+	const int timeout = 5; /* Wait up to 5 seconds for timeouts */
+	int count;
+	u64 start_cycle;
+
+	count = cvmx_pko_queue_pend_count(queue);
+	if (count < 0)
+		return count;
+
+	start_cycle = get_timer(0);
+
+	while (count > 0 && (get_timer(start_cycle) < timeout * 1000)) {
+		mdelay(1);
+		count = cvmx_pko_queue_pend_count(queue);
+	}
+
+	return count;
+}
+
+/**
+ * @INTERNAL
+ *
+ * Drain and wait until all PKO queues are empty.
+ */
+int __cvmx_helper_pko_drain(void)
+{
+	int result = 0;
+
+	if (octeon_has_feature(OCTEON_FEATURE_PKND)) {
+		int queue, max_queue;
+
+		/* PKO2 */
+		max_queue = __cvmx_helper_cfg_pko_max_queue();
+		for (queue = 0; queue < max_queue; queue++) {
+			if (cvmx_helper_wait_pko_queue_drain(queue)) {
+				result = -1;
+				return result;
+			}
+		}
+	} else {
+		int num_interfaces = cvmx_helper_get_number_of_interfaces();
+		int interface, num_ports, index;
+
+		/* PKO1 */
+		for (interface = 0; interface < num_interfaces; interface++) {
+			num_ports = cvmx_helper_ports_on_interface(interface);
+			for (index = 0; index < num_ports; index++) {
+				int pko_port;
+				int queue;
+				int max_queue;
+
+				if (!cvmx_helper_is_port_valid(interface,
+							       index))
+					continue;
+				pko_port = cvmx_helper_get_ipd_port(interface,
+								    index);
+				queue = cvmx_pko_get_base_queue(pko_port);
+				max_queue = queue +
+					    cvmx_pko_get_num_queues(pko_port);
+				while (queue < max_queue) {
+					if (cvmx_helper_wait_pko_queue_drain(queue)) {
+						result = -1;
+						return result;
+					}
+					queue++;
+				}
+			}
+		}
+	}
+	return result;
+}
Signature · 1 line
2.35.1
#18 lore raw

PATCH 19/52 mips: octeon: Add cvmx-helper-pko3.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-helper-pko3.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-helper-pko3.c | 1252 ++++++++++++++++++++++
 1 file changed, 1252 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-helper-pko3.c
arch/mips/mach-octeon/cvmx-helper-pko3.c new file +1252 -0
new file mode 100644
index 000000000000..de9b2462111d
@@ -0,0 +1,1252 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * PKOv3 helper file
+ */
+
+#include <errno.h>
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+#include <mach/cvmx-range.h>
+#include <mach/cvmx-global-resources.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-ipd.h>
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-pko3.h>
+#include <mach/cvmx-pko3-queue.h>
+#include <mach/cvmx-pko3-resources.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#include <mach/cvmx-helper-bgx.h>
+#include <mach/cvmx-helper-cfg.h>
+#include <mach/cvmx-helper-util.h>
+#include <mach/cvmx-helper-pki.h>
+
+/* channels are present at L2 queue level by default */
+static const enum cvmx_pko3_level_e cvmx_pko_default_channel_level =
+	CVMX_PKO_L2_QUEUES;
+
+static const int debug;
+
+static int __pko_pkt_budget, __pko_pkt_quota;
+
+/* These global variables are relevant for boot CPU only */
+static cvmx_fpa3_gaura_t __cvmx_pko3_aura[CVMX_MAX_NODES];
+
+/* This constant can not be modified, defined here for clarity only */
+#define CVMX_PKO3_POOL_BUFFER_SIZE 4096 /* 78XX PKO requires 4KB */
+
+/**
+ * @INTERNAL
+ *
+ * Build an owner tag based on interface/port
+ */
+static int __cvmx_helper_pko3_res_owner(int ipd_port)
+{
+	int res_owner;
+	const int res_owner_pfix = 0x19d0 << 14;
+
+	ipd_port &= 0x3fff; /* 12-bit for local CHAN_E value + node */
+
+	res_owner = res_owner_pfix | ipd_port;
+
+	return res_owner;
+}
+
+/**
+ * Configure an AURA/POOL designated for PKO internal use.
+ *
+ * This pool is used for (a) memory buffers that store PKO descriptor queues,
+ * (b) buffers for use with PKO_SEND_JUMP_S sub-header.
+ *
+ * The buffers of type (a) are never accessed by software, and their number
+ * should be at least equal to 4 times the number of descriptor queues
+ * in use.
+ *
+ * Type (b) buffers are consumed by PKO3 command-composition code,
+ * and are released by the hardware upon completion of transmission.
+ *
+ * @returns -1 if the pool could not be established or 12-bit AURA
+ * that includes the node number for use in PKO3 initialization call.
+ *
+ * NOTE: Linux kernel should pass its own aura to PKO3 initialization
+ * function so that the buffers can be mapped into kernel space
+ * for when software needs to adccess their contents.
+ *
+ */
+static int __cvmx_pko3_config_memory(unsigned int node)
+{
+	cvmx_fpa3_gaura_t aura;
+	int aura_num;
+	unsigned int buf_count;
+	bool small_mem;
+	int i, num_intf = 0;
+	const unsigned int pkt_per_buf =
+		(CVMX_PKO3_POOL_BUFFER_SIZE / sizeof(u64) / 16);
+	const unsigned int base_buf_count = 1024 * 4;
+
+	/* Simulator has limited memory, but uses one interface at a time */
+	//	small_mem = cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM;
+	small_mem = false;
+
+	/* Count the number of live interfaces */
+	for (i = 0; i < cvmx_helper_get_number_of_interfaces(); i++) {
+		int xiface = cvmx_helper_node_interface_to_xiface(node, i);
+
+		if (CVMX_HELPER_INTERFACE_MODE_DISABLED !=
+		    cvmx_helper_interface_get_mode(xiface))
+			num_intf++;
+	}
+
+	buf_count = 1024;
+	__pko_pkt_quota = buf_count * pkt_per_buf;
+	__pko_pkt_budget = __pko_pkt_quota * num_intf;
+	(void)small_mem;
+	(void)base_buf_count;
+
+	if (debug)
+		debug("%s: Creating AURA with %u buffers for up to %d total packets, %d packets per interface\n",
+		      __func__, buf_count, __pko_pkt_budget, __pko_pkt_quota);
+
+	aura = cvmx_fpa3_setup_aura_and_pool(node, -1, "PKO3 AURA", NULL,
+					     CVMX_PKO3_POOL_BUFFER_SIZE,
+					     buf_count);
+
+	if (!__cvmx_fpa3_aura_valid(aura)) {
+		printf("ERROR: %s AURA create failed\n", __func__);
+		return -1;
+	}
+
+	aura_num = aura.node << 10 | aura.laura;
+
+	/* Store handle for destruction */
+	__cvmx_pko3_aura[node] = aura;
+
+	return aura_num;
+}
+
+/** Initialize a channelized port
+ * This is intended for LOOP, ILK and NPI interfaces which have one MAC
+ * per interface and need a channel per subinterface (e.g. ring).
+ * Each channel then may have 'num_queues' descriptor queues
+ * attached to it, which can also be prioritized or fair.
+ */
+static int __cvmx_pko3_config_chan_interface(int xiface, unsigned int num_chans,
+					     u8 num_queues, bool prioritized)
+{
+	int l1_q_num;
+	int l2_q_base;
+	enum cvmx_pko3_level_e level;
+	int res;
+	int parent_q, child_q;
+	unsigned int chan, dq;
+	int pko_mac_num;
+	u16 ipd_port;
+	int res_owner, prio;
+	unsigned int i;
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	unsigned int node = xi.node;
+	char b1[12];
+
+	if (num_queues == 0)
+		num_queues = 1;
+	if ((cvmx_pko3_num_level_queues(CVMX_PKO_DESCR_QUEUES) / num_chans) < 3)
+		num_queues = 1;
+
+	if (prioritized && num_queues > 1)
+		prio = num_queues;
+	else
+		prio = -1;
+
+	if (debug)
+		debug("%s: configuring xiface %u:%u with %u chans %u queues each\n",
+		      __func__, xi.node, xi.interface, num_chans, num_queues);
+
+	/* all channels all go to the same mac */
+	pko_mac_num = __cvmx_pko3_get_mac_num(xiface, 0);
+	if (pko_mac_num < 0) {
+		printf("ERROR: %s: Invalid interface\n", __func__);
+		return -1;
+	}
+
+	/* Resources of all channels on this port have common owner */
+	ipd_port = cvmx_helper_get_ipd_port(xiface, 0);
+
+	/* Build an identifiable owner */
+	res_owner = __cvmx_helper_pko3_res_owner(ipd_port);
+
+	/* Start configuration at L1/PQ */
+	level = CVMX_PKO_PORT_QUEUES;
+
+	/* Reserve port queue to make sure the MAC is not already configured */
+	l1_q_num = cvmx_pko_alloc_queues(node, level, res_owner, -1, 1);
+
+	if (l1_q_num < 0) {
+		printf("ERROR: %s: Reserving L1 PQ\n", __func__);
+		return -1;
+	}
+
+	res = cvmx_pko3_pq_config(node, pko_mac_num, l1_q_num);
+	if (res < 0) {
+		printf("ERROR: %s: Configuring L1 PQ\n", __func__);
+		return -1;
+	}
+
+	/* next queue level = L2/SQ */
+	level = __cvmx_pko3_sq_lvl_next(level);
+
+	/* allocate level 2 queues, one per channel */
+	l2_q_base =
+		cvmx_pko_alloc_queues(node, level, res_owner, -1, num_chans);
+	if (l2_q_base < 0) {
+		printf("ERROR: %s: allocation L2 SQ\n", __func__);
+		return -1;
+	}
+
+	/* Configre <num_chans> L2 children for PQ, non-prioritized */
+	res = cvmx_pko3_sq_config_children(node, level, l1_q_num, l2_q_base,
+					   num_chans, -1);
+
+	if (res < 0) {
+		printf("ERROR: %s: Failed channel queues\n", __func__);
+		return -1;
+	}
+
+	/* map channels to l2 queues */
+	for (chan = 0; chan < num_chans; chan++) {
+		ipd_port = cvmx_helper_get_ipd_port(xiface, chan);
+		cvmx_pko3_map_channel(node, l1_q_num, l2_q_base + chan,
+				      ipd_port);
+	}
+
+	/* next queue level = L3/SQ */
+	level = __cvmx_pko3_sq_lvl_next(level);
+	parent_q = l2_q_base;
+
+	do {
+		child_q = cvmx_pko_alloc_queues(node, level, res_owner, -1,
+						num_chans);
+
+		if (child_q < 0) {
+			printf("ERROR: %s: allocating %s\n", __func__,
+			       __cvmx_pko3_sq_str(b1, level, child_q));
+			return -1;
+		}
+
+		for (i = 0; i < num_chans; i++) {
+			res = cvmx_pko3_sq_config_children(
+				node, level, parent_q + i, child_q + i, 1, 1);
+
+			if (res < 0) {
+				printf("ERROR: %s: configuring %s\n", __func__,
+				       __cvmx_pko3_sq_str(b1, level, child_q));
+				return -1;
+			}
+
+		} /* for i */
+
+		parent_q = child_q;
+		level = __cvmx_pko3_sq_lvl_next(level);
+
+		/* Terminate loop on DQ level, it has special handling */
+	} while (level != CVMX_PKO_DESCR_QUEUES &&
+		 level != CVMX_PKO_LEVEL_INVAL);
+
+	if (level != CVMX_PKO_DESCR_QUEUES) {
+		printf("ERROR: %s: level sequence error\n", __func__);
+		return -1;
+	}
+
+	/* Configure DQs, num_dqs per chan */
+	for (chan = 0; chan < num_chans; chan++) {
+		res = cvmx_pko_alloc_queues(node, level, res_owner, -1,
+					    num_queues);
+
+		if (res < 0)
+			goto _fail;
+		dq = res;
+
+		if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_0) && (dq & 7))
+			debug("WARNING: %s: DQ# %u not integral of 8\n",
+			      __func__, dq);
+
+		res = cvmx_pko3_sq_config_children(node, level, parent_q + chan,
+						   dq, num_queues, prio);
+		if (res < 0)
+			goto _fail;
+
+		/* register DQ range with the translation table */
+		res = __cvmx_pko3_ipd_dq_register(xiface, chan, dq, num_queues);
+		if (res < 0)
+			goto _fail;
+	}
+
+	return 0;
+_fail:
+	debug("ERROR: %s: configuring queues for xiface %u:%u chan %u\n",
+	      __func__, xi.node, xi.interface, i);
+	return -1;
+}
+
+/** Initialize a single Ethernet port with PFC-style channels
+ *
+ * One interface can contain multiple ports, this function is per-port
+ * Here, a physical port is allocated 8 logical channel, one per VLAN
+ * tag priority, one DQ is assigned to each channel, and all 8 DQs
+ * are registered for that IPD port.
+ * Note that the DQs are arrange such that the Ethernet QoS/PCP field
+ * can be used as an offset to the value returned by cvmx_pko_base_queue_get().
+ *
+ * For HighGig2 mode, 16 channels may be desired, instead of 8,
+ * but this function does not support that.
+ */
+static int __cvmx_pko3_config_pfc_interface(int xiface, unsigned int port)
+{
+	enum cvmx_pko3_level_e level;
+	int pko_mac_num;
+	int l1_q_num, l2_q_base;
+	int child_q, parent_q;
+	int dq_base;
+	int res;
+	const unsigned int num_chans = 8;
+	cvmx_xiface_t xi = cvmx_helper_xiface_to_node_interface(xiface);
+	unsigned int node = xi.node;
+	u16 ipd_port;
+	int res_owner;
+	char b1[12];
+	unsigned int i;
+
+	if (debug)
+		debug("%s: configuring xiface %u:%u port %u with %u PFC channels\n",
+		      __func__, node, xi.interface, port, num_chans);
+
+	/* Get MAC number for the iface/port */
+	pko_mac_num = __cvmx_pko3_get_mac_num(xiface, port);
+	if (pko_mac_num < 0) {
+		printf("ERROR: %s: Invalid interface\n", __func__);
+		return -1;
+	}
+
+	ipd_port = cvmx_helper_get_ipd_port(xiface, port);
+
+	/* Build an identifiable owner identifier */
+	res_owner = __cvmx_helper_pko3_res_owner(ipd_port);
+
+	level = CVMX_PKO_PORT_QUEUES;
+
+	/* Allocate port queue to make sure the MAC is not already configured */
+	l1_q_num = cvmx_pko_alloc_queues(node, level, res_owner, -1, 1);
+
+	if (l1_q_num < 0) {
+		printf("ERROR: %s: allocation L1 PQ\n", __func__);
+		return -1;
+	}
+
+	res = cvmx_pko3_pq_config(xi.node, pko_mac_num, l1_q_num);
+	if (res < 0) {
+		printf("ERROR: %s: Configuring %s\n", __func__,
+		       __cvmx_pko3_sq_str(b1, level, l1_q_num));
+		return -1;
+	}
+
+	/* Determine the next queue level */
+	level = __cvmx_pko3_sq_lvl_next(level);
+
+	/* Allocate 'num_chans' L2 queues, one per channel */
+	l2_q_base =
+		cvmx_pko_alloc_queues(node, level, res_owner, -1, num_chans);
+	if (l2_q_base < 0) {
+		printf("ERROR: %s: allocation L2 SQ\n", __func__);
+		return -1;
+	}
+
+	/* Configre <num_chans> L2 children for PQ, with static priority */
+	res = cvmx_pko3_sq_config_children(node, level, l1_q_num, l2_q_base,
+					   num_chans, num_chans);
+
+	if (res < 0) {
+		printf("ERROR: %s: Configuring %s for PFC\n", __func__,
+		       __cvmx_pko3_sq_str(b1, level, l1_q_num));
+		return -1;
+	}
+
+	/* Map each of the allocated channels */
+	for (i = 0; i < num_chans; i++) {
+		u16 chan;
+
+		/* Get CHAN_E value for this PFC channel, PCP in low 3 bits */
+		chan = ipd_port | cvmx_helper_prio2qos(i);
+
+		cvmx_pko3_map_channel(node, l1_q_num, l2_q_base + i, chan);
+	}
+
+	/* Iterate through the levels until DQ and allocate 'num_chans'
+	 * consecutive queues at each level and hook them up
+	 * one-to-one with the parent level queues
+	 */
+
+	parent_q = l2_q_base;
+	level = __cvmx_pko3_sq_lvl_next(level);
+
+	do {
+		child_q = cvmx_pko_alloc_queues(node, level, res_owner, -1,
+						num_chans);
+
+		if (child_q < 0) {
+			printf("ERROR: %s: allocating %s\n", __func__,
+			       __cvmx_pko3_sq_str(b1, level, child_q));
+			return -1;
+		}
+
+		for (i = 0; i < num_chans; i++) {
+			res = cvmx_pko3_sq_config_children(
+				node, level, parent_q + i, child_q + i, 1, 1);
+
+			if (res < 0) {
+				printf("ERROR: %s: configuring %s\n", __func__,
+				       __cvmx_pko3_sq_str(b1, level, child_q));
+				return -1;
+			}
+
+		} /* for i */
+
+		parent_q = child_q;
+		level = __cvmx_pko3_sq_lvl_next(level);
+
+		/* Terminate loop on DQ level, it has special handling */
+	} while (level != CVMX_PKO_DESCR_QUEUES &&
+		 level != CVMX_PKO_LEVEL_INVAL);
+
+	if (level != CVMX_PKO_DESCR_QUEUES) {
+		printf("ERROR: %s: level sequence error\n", __func__);
+		return -1;
+	}
+
+	dq_base = cvmx_pko_alloc_queues(node, level, res_owner, -1, num_chans);
+	if (dq_base < 0) {
+		printf("ERROR: %s: allocating %s\n", __func__,
+		       __cvmx_pko3_sq_str(b1, level, dq_base));
+		return -1;
+	}
+
+	/* Configure DQs in QoS order, so that QoS/PCP can be index */
+	for (i = 0; i < num_chans; i++) {
+		int dq_num = dq_base + cvmx_helper_prio2qos(i);
+
+		res = cvmx_pko3_sq_config_children(node, level, parent_q + i,
+						   dq_num, 1, 1);
+		if (res < 0) {
+			printf("ERROR: %s: configuring %s\n", __func__,
+			       __cvmx_pko3_sq_str(b1, level, dq_num));
+			return -1;
+		}
+	}
+
+	/* register entire DQ range with the IPD translation table */
+	__cvmx_pko3_ipd_dq_register(xiface, port, dq_base, num_chans);
+
+	return 0;
+}
+
+/**
+ * Initialize a simple interface with a a given number of
+ * fair or prioritized queues.
+ * This function will assign one channel per sub-interface.
+ */
+int __cvmx_pko3_config_gen_interface(int xiface, uint8_t subif, u8 num_queues,
+				     bool prioritized)
+{
+	cvmx_xiface_t xi = cvmx_helper_xiface_to_node_interface(xiface);
+	u8 node = xi.node;
+	int l1_q_num;
+	int parent_q, child_q;
+	int dq;
+	int res, res_owner;
+	int pko_mac_num;
+	enum cvmx_pko3_level_e level;
+	u16 ipd_port;
+	int static_pri;
+	char b1[12];
+
+	num_queues = 1;
+
+	if (num_queues == 0) {
+		num_queues = 1;
+		printf("WARNING: %s: xiface %#x misconfigured\n", __func__,
+		       xiface);
+	}
+
+	/* Configure DQs relative priority (a.k.a. scheduling) */
+	if (prioritized) {
+		/* With 8 queues or fewer, use static priority, else WRR */
+		static_pri = (num_queues < 9) ? num_queues : 0;
+	} else {
+		/* Set equal-RR scheduling among queues */
+		static_pri = -1;
+	}
+
+	if (debug)
+		debug("%s: configuring xiface %u:%u/%u nq=%u %s\n", __func__,
+		      xi.node, xi.interface, subif, num_queues,
+		      (prioritized) ? "qos" : "fair");
+
+	/* Get MAC number for the iface/port */
+	pko_mac_num = __cvmx_pko3_get_mac_num(xiface, subif);
+	if (pko_mac_num < 0) {
+		printf("ERROR: %s: Invalid interface %u:%u\n", __func__,
+		       xi.node, xi.interface);
+		return -1;
+	}
+
+	ipd_port = cvmx_helper_get_ipd_port(xiface, subif);
+
+	if (debug)
+		debug("%s: xiface %u:%u/%u ipd_port=%#03x\n", __func__, xi.node,
+		      xi.interface, subif, ipd_port);
+
+	/* Build an identifiable owner identifier */
+	res_owner = __cvmx_helper_pko3_res_owner(ipd_port);
+
+	level = CVMX_PKO_PORT_QUEUES;
+
+	/* Reserve port queue to make sure the MAC is not already configured */
+	l1_q_num = cvmx_pko_alloc_queues(node, level, res_owner, -1, 1);
+
+	if (l1_q_num < 0) {
+		printf("ERROR %s: xiface %u:%u/%u failed allocation L1 PQ\n",
+		       __func__, xi.node, xi.interface, subif);
+		return -1;
+	}
+
+	res = cvmx_pko3_pq_config(node, pko_mac_num, l1_q_num);
+	if (res < 0) {
+		printf("ERROR %s: Configuring L1 PQ\n", __func__);
+		return -1;
+	}
+
+	parent_q = l1_q_num;
+
+	/* Determine the next queue level */
+	level = __cvmx_pko3_sq_lvl_next(level);
+
+	/* Simply chain queues 1-to-1 from L2 to one before DQ level */
+	do {
+		/* allocate next level queue */
+		child_q = cvmx_pko_alloc_queues(node, level, res_owner, -1, 1);
+
+		if (child_q < 0) {
+			printf("ERROR: %s: allocating %s\n", __func__,
+			       __cvmx_pko3_sq_str(b1, level, child_q));
+			return -1;
+		}
+
+		/* Configre newly allocated queue */
+		res = cvmx_pko3_sq_config_children(node, level, parent_q,
+						   child_q, 1, 1);
+
+		if (res < 0) {
+			printf("ERROR: %s: configuring %s\n", __func__,
+			       __cvmx_pko3_sq_str(b1, level, child_q));
+			return -1;
+		}
+
+		/* map IPD/channel to L2/L3 queues */
+		if (level == cvmx_pko_default_channel_level)
+			cvmx_pko3_map_channel(node, l1_q_num, child_q,
+					      ipd_port);
+
+		/* Prepare for next level */
+		level = __cvmx_pko3_sq_lvl_next(level);
+		parent_q = child_q;
+
+		/* Terminate loop on DQ level, it has special handling */
+	} while (level != CVMX_PKO_DESCR_QUEUES &&
+		 level != CVMX_PKO_LEVEL_INVAL);
+
+	if (level != CVMX_PKO_DESCR_QUEUES) {
+		printf("ERROR: %s: level sequence error\n", __func__);
+		return -1;
+	}
+
+	/* Allocate descriptor queues for the port */
+	dq = cvmx_pko_alloc_queues(node, level, res_owner, -1, num_queues);
+	if (dq < 0) {
+		printf("ERROR: %s: could not reserve DQs\n", __func__);
+		return -1;
+	}
+
+	res = cvmx_pko3_sq_config_children(node, level, parent_q, dq,
+					   num_queues, static_pri);
+	if (res < 0) {
+		printf("ERROR: %s: configuring %s\n", __func__,
+		       __cvmx_pko3_sq_str(b1, level, dq));
+		return -1;
+	}
+
+	/* register DQ/IPD translation */
+	__cvmx_pko3_ipd_dq_register(xiface, subif, dq, num_queues);
+
+	if (debug)
+		debug("%s: xiface %u:%u/%u qs %u-%u\n", __func__, xi.node,
+		      xi.interface, subif, dq, dq + num_queues - 1);
+	return 0;
+}
+
+/** Initialize the NULL interface
+ *
+ * A NULL interface is a special case in that it is not
+ * one of the enumerated interfaces in the system, and does
+ * not apply to input either. Still, it can be very handy
+ * for dealing with packets that should be discarded in
+ * a generic, streamlined way.
+ *
+ * The Descriptor Queue 0 will be reserved for the NULL interface
+ * and the normalized (i.e. IPD) port number has the all-ones value.
+ */
+static int __cvmx_pko3_config_null_interface(unsigned int node)
+{
+	int l1_q_num;
+	int parent_q, child_q;
+	enum cvmx_pko3_level_e level;
+	int i, res, res_owner;
+	int xiface, ipd_port;
+	int num_dq = 1;	  /* # of DQs for NULL */
+	const int dq = 0; /* Reserve DQ#0 for NULL */
+	char pko_mac_num;
+	char b1[12];
+
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX))
+		pko_mac_num = 0x1C; /* MAC# 28 virtual MAC for NULL */
+	else if (OCTEON_IS_MODEL(OCTEON_CN73XX))
+		pko_mac_num = 0x0F; /* MAC# 16 virtual MAC for NULL */
+	else if (OCTEON_IS_MODEL(OCTEON_CNF75XX))
+		pko_mac_num = 0x0A; /* MAC# 10 virtual MAC for NULL */
+	else
+		return -1;
+
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_0))
+		num_dq = 8;
+
+	if (debug)
+		debug("%s: null iface dq=%u-%u\n", __func__, dq,
+		      dq + num_dq - 1);
+
+	ipd_port = cvmx_helper_node_to_ipd_port(node, CVMX_PKO3_IPD_PORT_NULL);
+
+	/* Build an identifiable owner identifier by MAC# for easy release */
+	res_owner = __cvmx_helper_pko3_res_owner(ipd_port);
+	if (res_owner < 0) {
+		debug("%s: ERROR Invalid interface\n", __func__);
+		return -1;
+	}
+
+	level = CVMX_PKO_PORT_QUEUES;
+
+	/* Allocate a port queue */
+	l1_q_num = cvmx_pko_alloc_queues(node, level, res_owner, -1, 1);
+
+	if (l1_q_num < 0) {
+		debug("%s: ERROR reserving L1 SQ\n", __func__);
+		return -1;
+	}
+
+	res = cvmx_pko3_pq_config(node, pko_mac_num, l1_q_num);
+	if (res < 0) {
+		printf("ERROR: %s: PQ/L1 queue configuration\n", __func__);
+		return -1;
+	}
+
+	parent_q = l1_q_num;
+
+	/* Determine the next queue level */
+	level = __cvmx_pko3_sq_lvl_next(level);
+
+	/* Simply chain queues 1-to-1 from L2 to one before DQ level */
+	do {
+		/* allocate next level queue */
+		child_q = cvmx_pko_alloc_queues(node, level, res_owner, -1, 1);
+
+		if (child_q < 0) {
+			printf("ERROR: %s: allocating %s\n", __func__,
+			       __cvmx_pko3_sq_str(b1, level, child_q));
+			return -1;
+		}
+
+		/* Configre newly allocated queue */
+		res = cvmx_pko3_sq_config_children(node, level, parent_q,
+						   child_q, 1, 1);
+
+		if (res < 0) {
+			printf("ERROR: %s: configuring %s\n", __func__,
+			       __cvmx_pko3_sq_str(b1, level, child_q));
+			return -1;
+		}
+
+		/* Prepare for next level */
+		level = __cvmx_pko3_sq_lvl_next(level);
+		parent_q = child_q;
+
+		/* Terminate loop on DQ level, it has special handling */
+	} while (level != CVMX_PKO_DESCR_QUEUES &&
+		 level != CVMX_PKO_LEVEL_INVAL);
+
+	if (level != CVMX_PKO_DESCR_QUEUES) {
+		printf("ERROR: %s: level sequence error\n", __func__);
+		return -1;
+	}
+
+	/* Reserve 'num_dq' DQ's at 0 by convention */
+	res = cvmx_pko_alloc_queues(node, level, res_owner, dq, num_dq);
+	if (dq != res) {
+		debug("%s: ERROR: could not reserve DQs\n", __func__);
+		return -1;
+	}
+
+	res = cvmx_pko3_sq_config_children(node, level, parent_q, dq, num_dq,
+					   num_dq);
+	if (res < 0) {
+		printf("ERROR: %s: configuring %s\n", __func__,
+		       __cvmx_pko3_sq_str(b1, level, dq));
+		return -1;
+	}
+
+	/* NULL interface does not need to map to a CHAN_E */
+
+	/* register DQ/IPD translation */
+	xiface = cvmx_helper_node_interface_to_xiface(node, __CVMX_XIFACE_NULL);
+	__cvmx_pko3_ipd_dq_register(xiface, 0, dq, num_dq);
+
+	/* open the null DQs here */
+	for (i = 0; i < num_dq; i++) {
+		unsigned int limit = 128; /* NULL never really uses much */
+
+		cvmx_pko_dq_open(node, dq + i);
+		cvmx_pko3_dq_set_limit(node, dq + i, limit);
+	}
+
+	return 0;
+}
+
+/** Open all descriptor queues belonging to an interface/port
+ * @INTERNAL
+ */
+int __cvmx_pko3_helper_dqs_activate(int xiface, int index, bool min_pad)
+{
+	int ipd_port, dq_base, dq_count, i;
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	unsigned int limit;
+
+	/* Get local IPD port for the interface */
+	ipd_port = cvmx_helper_get_ipd_port(xiface, index);
+	if (ipd_port < 0) {
+		printf("ERROR: %s: No IPD port for interface %d port %d\n",
+		       __func__, xiface, index);
+		return -1;
+	}
+
+	/* Get DQ# range for the IPD port */
+	dq_base = cvmx_pko3_get_queue_base(ipd_port);
+	dq_count = cvmx_pko3_get_queue_num(ipd_port);
+	if (dq_base < 0 || dq_count <= 0) {
+		printf("ERROR: %s: No descriptor queues for interface %d port %d\n",
+		       __func__, xiface, index);
+		return -1;
+	}
+
+	/* Mask out node from global DQ# */
+	dq_base &= (1 << 10) - 1;
+
+	limit = __pko_pkt_quota / dq_count /
+		cvmx_helper_interface_enumerate(xiface);
+
+	for (i = 0; i < dq_count; i++) {
+		/* FIXME: 2ms at 1Gbps max packet rate, make speed dependent */
+		cvmx_pko_dq_open(xi.node, dq_base + i);
+		cvmx_pko3_dq_options(xi.node, dq_base + i, min_pad);
+
+		if (debug)
+			debug("%s: DQ%u limit %d\n", __func__, dq_base + i,
+			      limit);
+
+		cvmx_pko3_dq_set_limit(xi.node, dq_base + i, limit);
+		__pko_pkt_budget -= limit;
+	}
+
+	if (__pko_pkt_budget < 0)
+		printf("WARNING: %s: PKO buffer deficit %d\n", __func__,
+		       __pko_pkt_budget);
+	else if (debug)
+		debug("%s: PKO remaining packet budget: %d\n", __func__,
+		      __pko_pkt_budget);
+
+	return i;
+}
+
+/** Configure and initialize PKO3 for an interface
+ *
+ * @param xiface is the interface number to configure
+ * @return 0 on success.
+ */
+int cvmx_helper_pko3_init_interface(int xiface)
+{
+	cvmx_helper_interface_mode_t mode;
+	int node, iface, subif, num_ports;
+	bool fcs_enable, pad_enable, pad_enable_pko;
+	u8 fcs_sof_off = 0;
+	u8 num_queues = 1;
+	bool qos = false, pfc = false;
+	int res = -1;
+	cvmx_xiface_t xi = cvmx_helper_xiface_to_node_interface(xiface);
+
+	node = xi.node;
+	iface = xi.interface;
+	mode = cvmx_helper_interface_get_mode(xiface);
+	num_ports = cvmx_helper_interface_enumerate(xiface);
+	subif = 0;
+
+	if ((unsigned int)iface <
+	    NUM_ELEMENTS(__cvmx_pko_queue_static_config[node].pknd.pko_cfg_iface)) {
+		pfc = __cvmx_pko_queue_static_config[node]
+			      .pknd.pko_cfg_iface[iface]
+			      .pfc_enable;
+		num_queues = __cvmx_pko_queue_static_config[node]
+				     .pknd.pko_cfg_iface[iface]
+				     .queues_per_port;
+		qos = __cvmx_pko_queue_static_config[node]
+			      .pknd.pko_cfg_iface[iface]
+			      .qos_enable;
+	}
+
+	/* Force 8 DQs per port for pass 1.0 to circumvent limitations */
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_0))
+		num_queues = 8;
+
+	/* For ILK there is one IPD port per channel */
+	if (mode == CVMX_HELPER_INTERFACE_MODE_ILK)
+		num_ports = __cvmx_helper_ilk_enumerate(xiface);
+
+	/* Skip non-existent interfaces */
+	if (num_ports < 1) {
+		debug("ERROR: %s: invalid iface %u:%u\n", __func__, node,
+		      iface);
+		return -1;
+	}
+
+	if (mode == CVMX_HELPER_INTERFACE_MODE_LOOP) {
+		num_queues = __cvmx_pko_queue_static_config[node]
+				     .pknd.pko_cfg_loop.queues_per_port;
+		qos = __cvmx_pko_queue_static_config[node]
+			      .pknd.pko_cfg_loop.qos_enable;
+
+		if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_0))
+			num_queues = 8;
+
+		res = __cvmx_pko3_config_chan_interface(xiface, num_ports,
+							num_queues, qos);
+		if (res < 0)
+			goto __cfg_error;
+	} else if (mode == CVMX_HELPER_INTERFACE_MODE_NPI) {
+		num_queues = __cvmx_pko_queue_static_config[node]
+				     .pknd.pko_cfg_npi.queues_per_port;
+		qos = __cvmx_pko_queue_static_config[node]
+			      .pknd.pko_cfg_npi.qos_enable;
+
+		if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_0))
+			num_queues = 8;
+
+		res = __cvmx_pko3_config_chan_interface(xiface, num_ports,
+							num_queues, qos);
+		if (res < 0)
+			goto __cfg_error;
+	}
+	/* ILK-specific queue configuration */
+	else if (mode == CVMX_HELPER_INTERFACE_MODE_ILK) {
+		unsigned int num_chans = __cvmx_helper_ilk_enumerate(xiface);
+
+		num_queues = 8;
+		qos = true;
+		pfc = false;
+
+		if (num_chans >= 128)
+			num_queues = 1;
+		else if (num_chans >= 64)
+			num_queues = 2;
+		else if (num_chans >= 32)
+			num_queues = 4;
+		else
+			num_queues = 8;
+
+		res = __cvmx_pko3_config_chan_interface(xiface, num_chans,
+							num_queues, qos);
+	}
+	/* Setup all ethernet configured for PFC */
+	else if (pfc) {
+		/* PFC interfaces have 8 prioritized queues */
+		for (subif = 0; subif < num_ports; subif++) {
+			res = __cvmx_pko3_config_pfc_interface(xiface, subif);
+			if (res < 0)
+				goto __cfg_error;
+
+			/* Enable PFC/CBFC on BGX */
+			__cvmx_helper_bgx_xaui_config_pfc(node, iface, subif,
+							  true);
+		}
+	} else {
+		/* All other interfaces follow static configuration */
+		for (subif = 0; subif < num_ports; subif++) {
+			res = __cvmx_pko3_config_gen_interface(xiface, subif,
+							       num_queues, qos);
+			if (res < 0)
+				goto __cfg_error;
+		}
+	}
+
+	fcs_enable = __cvmx_helper_get_has_fcs(xiface);
+	pad_enable = __cvmx_helper_get_pko_padding(xiface);
+
+	/* Do not use PKO PAD/FCS generation on o78p1.x on BGX interfaces */
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_X))
+		pad_enable_pko = false;
+	else
+		pad_enable_pko = pad_enable;
+
+	if (debug)
+		debug("%s: iface %u:%u FCS=%d pad=%d pko=%d\n", __func__, node,
+		      iface, fcs_enable, pad_enable, pad_enable_pko);
+
+	/* Setup interface options */
+	for (subif = 0; subif < num_ports; subif++) {
+		/* Open interface/port DQs to allow transmission to begin */
+		res = __cvmx_pko3_helper_dqs_activate(xiface, subif,
+						      pad_enable_pko);
+
+		if (res < 0)
+			goto __cfg_error;
+
+		/* ILK has only one MAC, subif == logical-channel */
+		if (mode == CVMX_HELPER_INTERFACE_MODE_ILK && subif > 0)
+			continue;
+
+		/* LOOP has only one MAC, subif == logical-channel */
+		if (mode == CVMX_HELPER_INTERFACE_MODE_LOOP && subif > 0)
+			continue;
+
+		/* NPI has only one MAC, subif == 'ring' */
+		if (mode == CVMX_HELPER_INTERFACE_MODE_NPI && subif > 0)
+			continue;
+
+		/* for sRIO there is 16 byte sRIO header, outside of FCS */
+		if (mode == CVMX_HELPER_INTERFACE_MODE_SRIO)
+			fcs_sof_off = 16;
+
+		if (iface >= CVMX_HELPER_MAX_GMX) {
+			/* Non-BGX interface, use PKO for FCS/PAD */
+			res = cvmx_pko3_interface_options(xiface, subif,
+							  fcs_enable,
+							  pad_enable_pko,
+							  fcs_sof_off);
+		} else if (pad_enable == pad_enable_pko) {
+			/* BGX interface: FCS/PAD done by PKO */
+			res = cvmx_pko3_interface_options(xiface, subif,
+							  fcs_enable,
+							  pad_enable,
+							  fcs_sof_off);
+			cvmx_helper_bgx_tx_options(node, iface, subif, false,
+						   false);
+		} else {
+			/* BGX interface: FCS/PAD done by BGX */
+			res = cvmx_pko3_interface_options(xiface, subif, false,
+							  false, fcs_sof_off);
+			cvmx_helper_bgx_tx_options(node, iface, subif,
+						   fcs_enable, pad_enable);
+		}
+
+		if (res < 0)
+			debug("WARNING: %s: option set failed on iface %u:%u/%u\n",
+			      __func__, node, iface, subif);
+		if (debug)
+			debug("%s: face %u:%u/%u fifo size %d\n", __func__,
+			      node, iface, subif,
+			      cvmx_pko3_port_fifo_size(xiface, subif));
+	}
+	return 0;
+
+__cfg_error:
+	debug("ERROR: %s: failed on iface %u:%u/%u\n", __func__, node, iface,
+	      subif);
+	return -1;
+}
+
+/**
+ * Global initialization for PKO3
+ *
+ * Should only be called once on each node
+ *
+ * TBD: Resolve the kernel case.
+ * When Linux eats up the entire memory, bootmem will be unable to
+ * satisfy our request, and the memory needs to come from Linux free pages.
+ */
+int __cvmx_helper_pko3_init_global(unsigned int node, uint16_t gaura)
+{
+	int res;
+
+	res = cvmx_pko3_hw_init_global(node, gaura);
+	if (res < 0) {
+		debug("ERROR: %s:failed block initialization\n", __func__);
+		return res;
+	}
+
+	/* configure channel level */
+	cvmx_pko3_channel_credit_level(node, cvmx_pko_default_channel_level);
+
+	/* add NULL MAC/DQ setup */
+	res = __cvmx_pko3_config_null_interface(node);
+	if (res < 0)
+		debug("ERROR: %s: creating NULL interface\n", __func__);
+
+	return res;
+}
+
+/**
+ * Global initialization for PKO3
+ *
+ * Should only be called once on each node
+ *
+ * When Linux eats up the entire memory, bootmem will be unable to
+ * satisfy our request, and the memory needs to come from Linux free pages.
+ */
+int cvmx_helper_pko3_init_global(unsigned int node)
+{
+	void *ptr;
+	int res = -1;
+	unsigned int aura_num = ~0;
+	cvmx_fpa3_gaura_t aura;
+
+	/* Allocate memory required by PKO3 */
+	res = __cvmx_pko3_config_memory(node);
+	if (res < 0) {
+		debug("ERROR: %s: PKO3 memory allocation error\n", __func__);
+		return res;
+	}
+
+	aura_num = res;
+	aura = __cvmx_pko3_aura[node];
+
+	/* Exercise the FPA to make sure the AURA is functional */
+	ptr = cvmx_fpa3_alloc(aura);
+
+	if (!ptr) {
+		res = -1;
+	} else {
+		cvmx_fpa3_free_nosync(ptr, aura, 0);
+		res = 0;
+	}
+
+	if (res < 0) {
+		debug("ERROR: %s: FPA failure AURA=%u:%d\n", __func__,
+		      aura.node, aura.laura);
+		return -1;
+	}
+
+	res = __cvmx_helper_pko3_init_global(node, aura_num);
+
+	if (res < 0)
+		debug("ERROR: %s: failed to start PPKO\n", __func__);
+
+	return res;
+}
+
+/**
+ * Uninitialize PKO3 interface
+ *
+ * Release all resources held by PKO for an interface.
+ * The shutdown code is the same for all supported interfaces.
+ *
+ * NOTE: The NULL virtual interface is identified by interface
+ * number -1, which translates into IPD port 0xfff, MAC#28. [Kludge]
+ */
+int cvmx_helper_pko3_shut_interface(int xiface)
+{
+	int index, num_ports;
+	int dq_base, dq_count;
+	u16 ipd_port;
+	int i, res_owner, res;
+	enum cvmx_pko3_level_e level;
+	cvmx_pko3_dq_params_t *p_param;
+	const unsigned int timeout = 10; /* milliseconds */
+	cvmx_xiface_t xi = cvmx_helper_xiface_to_node_interface(xiface);
+
+	if (__cvmx_helper_xiface_is_null(xiface)) {
+		/* Special case for NULL interface */
+		num_ports = 1;
+	} else {
+		cvmx_helper_interface_mode_t mode;
+
+		mode = cvmx_helper_interface_get_mode(xiface);
+		num_ports = cvmx_helper_interface_enumerate(xiface);
+		(void)mode;
+	}
+
+	/* Skip non-existent interfaces silently */
+	if (num_ports < 1)
+		return -1;
+
+	if (debug)
+		debug("%s: xiface %u:%d ports %d\n", __func__, xi.node,
+		      xi.interface, num_ports);
+
+	for (index = 0; index < num_ports; index++) {
+		if (__cvmx_helper_xiface_is_null(xiface))
+			ipd_port = cvmx_helper_node_to_ipd_port(
+				xi.node, CVMX_PKO3_IPD_PORT_NULL);
+		else
+			ipd_port = cvmx_helper_get_ipd_port(xiface, index);
+
+		/* Retrieve DQ range for the index */
+		dq_base = cvmx_pko3_get_queue_base(ipd_port);
+		dq_count = cvmx_pko3_get_queue_num(ipd_port);
+
+		if (dq_base < 0 || dq_count < 0) {
+			debug("ERROR: %s: No DQs for iface %u:%d/%u\n",
+			      __func__, xi.node, xi.interface, index);
+			continue;
+		}
+
+		/* Get rid of node-number in DQ# */
+		dq_base &= (1 << 10) - 1;
+
+		if (debug)
+			debug("%s: xiface %u:%d/%d dq %u-%u\n", __func__,
+			      xi.node, xi.interface, index, dq_base,
+			      dq_base + dq_count - 1);
+
+		/* Unregister the DQs for the port, should stop traffic */
+		res = __cvmx_pko3_ipd_dq_unregister(xiface, index);
+		if (res < 0) {
+			debug("ERROR: %s: failed to unregister DQs iface %u/%d/%u\n",
+			      __func__, xi.node, xi.interface, index);
+			continue;
+		}
+
+		/* Begin draining all queues */
+		for (i = 0; i < dq_count; i++)
+			cvmx_pko3_dq_drain(xi.node, dq_base + i);
+
+		/* Wait for all queues to drain, and close them */
+		for (i = 0; i < dq_count; i++) {
+			/* Prepare timeout */
+			u64 start = get_timer(0);
+
+			/* Wait for queue to drain */
+			do {
+				res = cvmx_pko3_dq_query(xi.node, dq_base + i);
+				if (get_timer(start) > timeout)
+					break;
+			} while (res > 0);
+
+			if (res != 0)
+				debug("ERROR: %s: querying queue %u\n",
+				      __func__, dq_base + i);
+
+			/* Close the queue, free internal buffers */
+			res = cvmx_pko3_dq_close(xi.node, dq_base + i);
+
+			if (res < 0)
+				debug("ERROR: %s: closing queue %u\n", __func__,
+				      dq_base + i);
+
+			/* Return DQ packet budget */
+			p_param = cvmx_pko3_dq_parameters(xi.node, dq_base + i);
+			__pko_pkt_budget += p_param->limit;
+			p_param->limit = 0;
+		}
+
+		/* Release all global resources owned by this interface/port */
+
+		res_owner = __cvmx_helper_pko3_res_owner(ipd_port);
+		if (res_owner < 0) {
+			debug("ERROR: %s: no resource owner ticket\n",
+			      __func__);
+			continue;
+		}
+
+		/* Actuall PQ/SQ/DQ associations left intact */
+		for (level = CVMX_PKO_PORT_QUEUES;
+		     level != CVMX_PKO_LEVEL_INVAL;
+		     level = __cvmx_pko3_sq_lvl_next(level)) {
+			cvmx_pko_free_queues(xi.node, level, res_owner);
+		}
+
+	} /* for port */
+
+	return 0;
+}
+
+/**
+ * Shutdown PKO3
+ *
+ * Should be called after all interfaces have been shut down on the PKO3.
+ *
+ * Disables the PKO, frees all its buffers.
+ */
+int cvmx_helper_pko3_shutdown(unsigned int node)
+{
+	unsigned int dq;
+	int res;
+
+	/* destroy NULL interface here, only PKO knows about it */
+	cvmx_helper_pko3_shut_interface(
+		cvmx_helper_node_interface_to_xiface(node, __CVMX_XIFACE_NULL));
+
+#ifdef __PKO_DQ_CLOSE_ERRATA_FIXED
+	/* Check that all DQs are closed */
+	/* this seems to cause issue on HW:
+	 * the error code differs from expected
+	 */
+	for (dq = 0; dq < (1 << 10); dq++) {
+		res = cvmx_pko3_dq_close(node, dq);
+		if (res != 0) {
+			debug("ERROR: %s: PKO3 descriptor queue %u could not be closed\n",
+			      __func__, dq);
+			return -1;
+		}
+	}
+#endif
+	(void)dq;
+	res = cvmx_pko3_hw_disable(node);
+
+	/* shut down AURA/POOL we created, and free its resources */
+	cvmx_fpa3_shutdown_aura_and_pool(__cvmx_pko3_aura[node]);
+	return res;
+}
Signature · 1 line
2.35.1
#19 lore raw

PATCH 20/52 mips: octeon: Add cvmx-helper-rgmii.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-helper-rgmii.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-helper-rgmii.c | 431 ++++++++++++++++++++++
 1 file changed, 431 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-helper-rgmii.c
arch/mips/mach-octeon/cvmx-helper-rgmii.c new file +431 -0
new file mode 100644
index 000000000000..a85b6a3cd4e8
@@ -0,0 +1,431 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * Functions for RGMII/GMII/MII initialization, configuration,
+ * and monitoring.
+ */
+
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+
+#include <mach/cvmx-hwpko.h>
+
+#include <mach/cvmx-asxx-defs.h>
+#include <mach/cvmx-dbg-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-npi-defs.h>
+#include <mach/cvmx-pko-defs.h>
+
+/**
+ * @INTERNAL
+ * Probe RGMII ports and determine the number present
+ *
+ * @param xiface Interface to probe
+ *
+ * @return Number of RGMII/GMII/MII ports (0-4).
+ */
+int __cvmx_helper_rgmii_probe(int xiface)
+{
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int num_ports = 0;
+	union cvmx_gmxx_inf_mode mode;
+
+	mode.u64 = csr_rd(CVMX_GMXX_INF_MODE(xi.interface));
+
+	if (mode.s.type)
+		debug("ERROR: Unsupported Octeon model in %s\n", __func__);
+	else
+		debug("ERROR: Unsupported Octeon model in %s\n", __func__);
+	return num_ports;
+}
+
+/**
+ * Put an RGMII interface in loopback mode. Internal packets sent
+ * out will be received back again on the same port. Externally
+ * received packets will echo back out.
+ *
+ * @param port   IPD port number to loop.
+ */
+void cvmx_helper_rgmii_internal_loopback(int port)
+{
+	int interface = (port >> 4) & 1;
+	int index = port & 0xf;
+	u64 tmp;
+
+	union cvmx_gmxx_prtx_cfg gmx_cfg;
+
+	gmx_cfg.u64 = 0;
+	gmx_cfg.s.duplex = 1;
+	gmx_cfg.s.slottime = 1;
+	gmx_cfg.s.speed = 1;
+	csr_wr(CVMX_GMXX_TXX_CLK(index, interface), 1);
+	csr_wr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);
+	csr_wr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
+	tmp = csr_rd(CVMX_ASXX_PRT_LOOP(interface));
+	csr_wr(CVMX_ASXX_PRT_LOOP(interface), (1 << index) | tmp);
+	tmp = csr_rd(CVMX_ASXX_TX_PRT_EN(interface));
+	csr_wr(CVMX_ASXX_TX_PRT_EN(interface), (1 << index) | tmp);
+	tmp = csr_rd(CVMX_ASXX_RX_PRT_EN(interface));
+	csr_wr(CVMX_ASXX_RX_PRT_EN(interface), (1 << index) | tmp);
+	gmx_cfg.s.en = 1;
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
+}
+
+/**
+ * @INTERNAL
+ * Configure all of the ASX, GMX, and PKO regsiters required
+ * to get RGMII to function on the supplied interface.
+ *
+ * @param xiface PKO Interface to configure (0 or 1)
+ *
+ * @return Zero on success
+ */
+int __cvmx_helper_rgmii_enable(int xiface)
+{
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int interface = xi.interface;
+	int num_ports = cvmx_helper_ports_on_interface(interface);
+	int port;
+	union cvmx_gmxx_inf_mode mode;
+	union cvmx_asxx_tx_prt_en asx_tx;
+	union cvmx_asxx_rx_prt_en asx_rx;
+
+	mode.u64 = csr_rd(CVMX_GMXX_INF_MODE(interface));
+
+	if (num_ports == -1)
+		return -1;
+	if (mode.s.en == 0)
+		return -1;
+
+	/* Configure the ASX registers needed to use the RGMII ports */
+	asx_tx.u64 = 0;
+	asx_tx.s.prt_en = cvmx_build_mask(num_ports);
+	csr_wr(CVMX_ASXX_TX_PRT_EN(interface), asx_tx.u64);
+
+	asx_rx.u64 = 0;
+	asx_rx.s.prt_en = cvmx_build_mask(num_ports);
+	csr_wr(CVMX_ASXX_RX_PRT_EN(interface), asx_rx.u64);
+
+	/* Configure the GMX registers needed to use the RGMII ports */
+	for (port = 0; port < num_ports; port++) {
+		/*
+		 * Configure more flexible RGMII preamble
+		 * checking. Pass 1 doesn't support this feature.
+		 */
+		union cvmx_gmxx_rxx_frm_ctl frm_ctl;
+
+		frm_ctl.u64 = csr_rd(CVMX_GMXX_RXX_FRM_CTL(port, interface));
+		/* New field, so must be compile time */
+		frm_ctl.s.pre_free = 1;
+		csr_wr(CVMX_GMXX_RXX_FRM_CTL(port, interface), frm_ctl.u64);
+
+		/*
+		 * Each pause frame transmitted will ask for about 10M
+		 * bit times before resume.  If buffer space comes
+		 * available before that time has expired, an XON
+		 * pause frame (0 time) will be transmitted to restart
+		 * the flow.
+		 */
+		csr_wr(CVMX_GMXX_TXX_PAUSE_PKT_TIME(port, interface), 20000);
+		csr_wr(CVMX_GMXX_TXX_PAUSE_PKT_INTERVAL(port, interface),
+		       19000);
+
+		csr_wr(CVMX_ASXX_TX_CLK_SETX(port, interface), 24);
+		csr_wr(CVMX_ASXX_RX_CLK_SETX(port, interface), 24);
+	}
+
+	__cvmx_helper_setup_gmx(interface, num_ports);
+
+	/* enable the ports now */
+	for (port = 0; port < num_ports; port++) {
+		union cvmx_gmxx_prtx_cfg gmx_cfg;
+
+		cvmx_helper_link_autoconf(
+			cvmx_helper_get_ipd_port(interface, port));
+		gmx_cfg.u64 = csr_rd(CVMX_GMXX_PRTX_CFG(port, interface));
+		gmx_cfg.s.en = 1;
+		csr_wr(CVMX_GMXX_PRTX_CFG(port, interface), gmx_cfg.u64);
+	}
+	return 0;
+}
+
+/**
+ * @INTERNAL
+ * Return the link state of an IPD/PKO port as returned by
+ * auto negotiation. The result of this function may not match
+ * Octeon's link config if auto negotiation has changed since
+ * the last call to cvmx_helper_link_set().
+ *
+ * @param ipd_port IPD/PKO port to query
+ *
+ * @return Link state
+ */
+cvmx_helper_link_info_t __cvmx_helper_rgmii_link_get(int ipd_port)
+{
+	int interface = cvmx_helper_get_interface_num(ipd_port);
+	int index = cvmx_helper_get_interface_index_num(ipd_port);
+	union cvmx_asxx_prt_loop asxx_prt_loop;
+
+	asxx_prt_loop.u64 = csr_rd(CVMX_ASXX_PRT_LOOP(interface));
+	if (asxx_prt_loop.s.int_loop & (1 << index)) {
+		/* Force 1Gbps full duplex on internal loopback */
+		cvmx_helper_link_info_t result;
+
+		result.u64 = 0;
+		result.s.full_duplex = 1;
+		result.s.link_up = 1;
+		result.s.speed = 1000;
+		return result;
+	} else {
+		return __cvmx_helper_board_link_get(ipd_port);
+	}
+}
+
+/**
+ * @INTERNAL
+ * Return the link state of an IPD/PKO port as returned by
+ * auto negotiation. The result of this function may not match
+ * Octeon's link config if auto negotiation has changed since
+ * the last call to cvmx_helper_link_set().
+ *
+ * @param ipd_port IPD/PKO port to query
+ *
+ * @return Link state
+ */
+cvmx_helper_link_info_t __cvmx_helper_gmii_link_get(int ipd_port)
+{
+	cvmx_helper_link_info_t result;
+	int index = cvmx_helper_get_interface_index_num(ipd_port);
+
+	if (index == 0) {
+		result = __cvmx_helper_rgmii_link_get(ipd_port);
+	} else {
+		result.s.full_duplex = 1;
+		result.s.link_up = 1;
+		result.s.speed = 1000;
+	}
+
+	return result;
+}
+
+/**
+ * @INTERNAL
+ * Configure an IPD/PKO port for the specified link state. This
+ * function does not influence auto negotiation at the PHY level.
+ * The passed link state must always match the link state returned
+ * by cvmx_helper_link_get(). It is normally best to use
+ * cvmx_helper_link_autoconf() instead.
+ *
+ * @param ipd_port  IPD/PKO port to configure
+ * @param link_info The new link state
+ *
+ * @return Zero on success, negative on failure
+ */
+int __cvmx_helper_rgmii_link_set(int ipd_port,
+				 cvmx_helper_link_info_t link_info)
+{
+	int result = 0;
+	int interface = cvmx_helper_get_interface_num(ipd_port);
+	int index = cvmx_helper_get_interface_index_num(ipd_port);
+	union cvmx_gmxx_prtx_cfg original_gmx_cfg;
+	union cvmx_gmxx_prtx_cfg new_gmx_cfg;
+	union cvmx_pko_mem_queue_qos pko_mem_queue_qos;
+	union cvmx_pko_mem_queue_qos pko_mem_queue_qos_save[16];
+	union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp;
+	union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp_save;
+	int i;
+
+	/* Read the current settings so we know the current enable state */
+	original_gmx_cfg.u64 = csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+	new_gmx_cfg = original_gmx_cfg;
+
+	/* Disable the lowest level RX */
+	csr_wr(CVMX_ASXX_RX_PRT_EN(interface),
+	       csr_rd(CVMX_ASXX_RX_PRT_EN(interface)) & ~(1 << index));
+
+	memset(pko_mem_queue_qos_save, 0, sizeof(pko_mem_queue_qos_save));
+	/* Disable all queues so that TX should become idle */
+	for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {
+		int queue = cvmx_pko_get_base_queue(ipd_port) + i;
+
+		csr_wr(CVMX_PKO_REG_READ_IDX, queue);
+		pko_mem_queue_qos.u64 = csr_rd(CVMX_PKO_MEM_QUEUE_QOS);
+		pko_mem_queue_qos.s.pid = ipd_port;
+		pko_mem_queue_qos.s.qid = queue;
+		pko_mem_queue_qos_save[i] = pko_mem_queue_qos;
+		pko_mem_queue_qos.s.qos_mask = 0;
+		csr_wr(CVMX_PKO_MEM_QUEUE_QOS, pko_mem_queue_qos.u64);
+	}
+
+	/* Disable backpressure */
+	gmx_tx_ovr_bp.u64 = csr_rd(CVMX_GMXX_TX_OVR_BP(interface));
+	gmx_tx_ovr_bp_save = gmx_tx_ovr_bp;
+	gmx_tx_ovr_bp.s.bp &= ~(1 << index);
+	gmx_tx_ovr_bp.s.en |= 1 << index;
+	csr_wr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp.u64);
+	csr_rd(CVMX_GMXX_TX_OVR_BP(interface));
+
+	/*
+	 * Poll the GMX state machine waiting for it to become
+	 * idle. Preferably we should only change speed when it is
+	 * idle. If it doesn't become idle we will still do the speed
+	 * change, but there is a slight chance that GMX will
+	 * lockup.
+	 */
+	csr_wr(CVMX_NPI_DBG_SELECT, interface * 0x800 + index * 0x100 + 0x880);
+	CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, cvmx_dbg_data_t, data & 7, ==, 0,
+			      10000);
+	CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, cvmx_dbg_data_t, data & 0xf, ==, 0,
+			      10000);
+
+	/* Disable the port before we make any changes */
+	new_gmx_cfg.s.en = 0;
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
+	csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+
+	/* Set full/half duplex */
+	if (!link_info.s.link_up)
+		/* Force full duplex on down links */
+		new_gmx_cfg.s.duplex = 1;
+	else
+		new_gmx_cfg.s.duplex = link_info.s.full_duplex;
+
+	/* Set the link speed. Anything unknown is set to 1Gbps */
+	if (link_info.s.speed == 10) {
+		new_gmx_cfg.s.slottime = 0;
+		new_gmx_cfg.s.speed = 0;
+	} else if (link_info.s.speed == 100) {
+		new_gmx_cfg.s.slottime = 0;
+		new_gmx_cfg.s.speed = 0;
+	} else {
+		new_gmx_cfg.s.slottime = 1;
+		new_gmx_cfg.s.speed = 1;
+	}
+
+	/* Adjust the clocks */
+	if (link_info.s.speed == 10) {
+		csr_wr(CVMX_GMXX_TXX_CLK(index, interface), 50);
+		csr_wr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);
+		csr_wr(CVMX_GMXX_TXX_BURST(index, interface), 0);
+	} else if (link_info.s.speed == 100) {
+		csr_wr(CVMX_GMXX_TXX_CLK(index, interface), 5);
+		csr_wr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);
+		csr_wr(CVMX_GMXX_TXX_BURST(index, interface), 0);
+	} else {
+		csr_wr(CVMX_GMXX_TXX_CLK(index, interface), 1);
+		csr_wr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);
+		csr_wr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);
+	}
+
+	/* Do a read to make sure all setup stuff is complete */
+	csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+
+	/* Save the new GMX setting without enabling the port */
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
+
+	/* Enable the lowest level RX */
+	if (link_info.s.link_up)
+		csr_wr(CVMX_ASXX_RX_PRT_EN(interface),
+		       csr_rd(CVMX_ASXX_RX_PRT_EN(interface)) | (1 << index));
+
+	/* Re-enable the TX path */
+	for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {
+		int queue = cvmx_pko_get_base_queue(ipd_port) + i;
+
+		csr_wr(CVMX_PKO_REG_READ_IDX, queue);
+		csr_wr(CVMX_PKO_MEM_QUEUE_QOS, pko_mem_queue_qos_save[i].u64);
+	}
+
+	/* Restore backpressure */
+	csr_wr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp_save.u64);
+
+	/* Restore the GMX enable state. Port config is complete */
+	new_gmx_cfg.s.en = original_gmx_cfg.s.en;
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
+
+	return result;
+}
+
+/**
+ * @INTERNAL
+ * Configure a port for internal and/or external loopback. Internal loopback
+ * causes packets sent by the port to be received by Octeon. External loopback
+ * causes packets received from the wire to sent out again.
+ *
+ * @param ipd_port IPD/PKO port to loopback.
+ * @param enable_internal
+ *                 Non zero if you want internal loopback
+ * @param enable_external
+ *                 Non zero if you want external loopback
+ *
+ * @return Zero on success, negative on failure.
+ */
+int __cvmx_helper_rgmii_configure_loopback(int ipd_port, int enable_internal,
+					   int enable_external)
+{
+	int interface = cvmx_helper_get_interface_num(ipd_port);
+	int index = cvmx_helper_get_interface_index_num(ipd_port);
+	int original_enable;
+	union cvmx_gmxx_prtx_cfg gmx_cfg;
+	union cvmx_asxx_prt_loop asxx_prt_loop;
+
+	/* Read the current enable state and save it */
+	gmx_cfg.u64 = csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+	original_enable = gmx_cfg.s.en;
+	/* Force port to be disabled */
+	gmx_cfg.s.en = 0;
+	if (enable_internal) {
+		/* Force speed if we're doing internal loopback */
+		gmx_cfg.s.duplex = 1;
+		gmx_cfg.s.slottime = 1;
+		gmx_cfg.s.speed = 1;
+		csr_wr(CVMX_GMXX_TXX_CLK(index, interface), 1);
+		csr_wr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);
+		csr_wr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);
+	}
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
+
+	/* Set the loopback bits */
+	asxx_prt_loop.u64 = csr_rd(CVMX_ASXX_PRT_LOOP(interface));
+	if (enable_internal)
+		asxx_prt_loop.s.int_loop |= 1 << index;
+	else
+		asxx_prt_loop.s.int_loop &= ~(1 << index);
+	if (enable_external)
+		asxx_prt_loop.s.ext_loop |= 1 << index;
+	else
+		asxx_prt_loop.s.ext_loop &= ~(1 << index);
+	csr_wr(CVMX_ASXX_PRT_LOOP(interface), asxx_prt_loop.u64);
+
+	/* Force enables in internal loopback */
+	if (enable_internal) {
+		u64 tmp;
+
+		tmp = csr_rd(CVMX_ASXX_TX_PRT_EN(interface));
+		csr_wr(CVMX_ASXX_TX_PRT_EN(interface), (1 << index) | tmp);
+		tmp = csr_rd(CVMX_ASXX_RX_PRT_EN(interface));
+		csr_wr(CVMX_ASXX_RX_PRT_EN(interface), (1 << index) | tmp);
+		original_enable = 1;
+	}
+
+	/* Restore the enable state */
+	gmx_cfg.s.en = original_enable;
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
+	return 0;
+}
Signature · 1 line
2.35.1
#20 lore raw

PATCH 21/52 mips: octeon: Add cvmx-helper-sgmii.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-helper-sgmii.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-helper-sgmii.c | 781 ++++++++++++++++++++++
 1 file changed, 781 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-helper-sgmii.c
arch/mips/mach-octeon/cvmx-helper-sgmii.c new file +781 -0
new file mode 100644
index 000000000000..b789ad5d1913
@@ -0,0 +1,781 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * Functions for SGMII initialization, configuration,
+ * and monitoring.
+ */
+
+#include <time.h>
+#include <log.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+/**
+ * @INTERNAL
+ * Perform initialization required only once for an SGMII port.
+ *
+ * @param interface to init
+ * @param index     Index of prot on the interface
+ *
+ * @return Zero on success, negative on failure
+ */
+static int __cvmx_helper_sgmii_hardware_init_one_time(int interface, int index)
+{
+	const u64 clock_mhz = 1200; /* todo: fixme */
+	union cvmx_pcsx_miscx_ctl_reg pcsx_miscx_ctl_reg;
+	union cvmx_pcsx_linkx_timer_count_reg pcsx_linkx_timer_count_reg;
+	union cvmx_gmxx_prtx_cfg gmxx_prtx_cfg;
+
+	if (!cvmx_helper_is_port_valid(interface, index))
+		return 0;
+
+	/* Disable GMX */
+	gmxx_prtx_cfg.u64 = csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+	gmxx_prtx_cfg.s.en = 0;
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), gmxx_prtx_cfg.u64);
+
+	/*
+	 * Write PCS*_LINK*_TIMER_COUNT_REG[COUNT] with the
+	 * appropriate value. 1000BASE-X specifies a 10ms
+	 * interval. SGMII specifies a 1.6ms interval.
+	 */
+	pcsx_miscx_ctl_reg.u64 =
+		csr_rd(CVMX_PCSX_MISCX_CTL_REG(index, interface));
+	/* Adjust the MAC mode if requested by device tree */
+	pcsx_miscx_ctl_reg.s.mac_phy =
+		cvmx_helper_get_mac_phy_mode(interface, index);
+	pcsx_miscx_ctl_reg.s.mode =
+		cvmx_helper_get_1000x_mode(interface, index);
+	csr_wr(CVMX_PCSX_MISCX_CTL_REG(index, interface),
+	       pcsx_miscx_ctl_reg.u64);
+
+	pcsx_linkx_timer_count_reg.u64 =
+		csr_rd(CVMX_PCSX_LINKX_TIMER_COUNT_REG(index, interface));
+	if (pcsx_miscx_ctl_reg.s.mode)
+		/* 1000BASE-X */
+		pcsx_linkx_timer_count_reg.s.count =
+			(10000ull * clock_mhz) >> 10;
+	else
+		/* SGMII */
+		pcsx_linkx_timer_count_reg.s.count =
+			(1600ull * clock_mhz) >> 10;
+
+	csr_wr(CVMX_PCSX_LINKX_TIMER_COUNT_REG(index, interface),
+	       pcsx_linkx_timer_count_reg.u64);
+
+	/*
+	 * Write the advertisement register to be used as the
+	 * tx_Config_Reg<D15:D0> of the autonegotiation.  In
+	 * 1000BASE-X mode, tx_Config_Reg<D15:D0> is PCS*_AN*_ADV_REG.
+	 * In SGMII PHY mode, tx_Config_Reg<D15:D0> is
+	 * PCS*_SGM*_AN_ADV_REG.  In SGMII MAC mode,
+	 * tx_Config_Reg<D15:D0> is the fixed value 0x4001, so this
+	 * step can be skipped.
+	 */
+	if (pcsx_miscx_ctl_reg.s.mode) {
+		/* 1000BASE-X */
+		union cvmx_pcsx_anx_adv_reg pcsx_anx_adv_reg;
+
+		pcsx_anx_adv_reg.u64 =
+			csr_rd(CVMX_PCSX_ANX_ADV_REG(index, interface));
+		pcsx_anx_adv_reg.s.rem_flt = 0;
+		pcsx_anx_adv_reg.s.pause = 3;
+		pcsx_anx_adv_reg.s.hfd = 1;
+		pcsx_anx_adv_reg.s.fd = 1;
+		csr_wr(CVMX_PCSX_ANX_ADV_REG(index, interface),
+		       pcsx_anx_adv_reg.u64);
+	} else {
+		if (pcsx_miscx_ctl_reg.s.mac_phy) {
+			/* PHY Mode */
+			union cvmx_pcsx_sgmx_an_adv_reg pcsx_sgmx_an_adv_reg;
+
+			pcsx_sgmx_an_adv_reg.u64 = csr_rd(
+				CVMX_PCSX_SGMX_AN_ADV_REG(index, interface));
+			pcsx_sgmx_an_adv_reg.s.dup = 1;
+			pcsx_sgmx_an_adv_reg.s.speed = 2;
+			csr_wr(CVMX_PCSX_SGMX_AN_ADV_REG(index, interface),
+			       pcsx_sgmx_an_adv_reg.u64);
+		} else {
+			/* MAC Mode - Nothing to do */
+		}
+	}
+	return 0;
+}
+
+static int __cvmx_helper_need_g15618(void)
+{
+	if (OCTEON_IS_MODEL(OCTEON_CN63XX) ||
+	    OCTEON_IS_MODEL(OCTEON_CN66XX_PASS1_X) ||
+	    OCTEON_IS_MODEL(OCTEON_CN68XX))
+		return 1;
+	else
+		return 0;
+}
+
+/**
+ * @INTERNAL
+ * Initialize the SERTES link for the first time or after a loss
+ * of link.
+ *
+ * @param interface to init
+ * @param index     Index of prot on the interface
+ *
+ * @return Zero on success, negative on failure
+ */
+static int __cvmx_helper_sgmii_hardware_init_link(int interface, int index)
+{
+	union cvmx_pcsx_mrx_control_reg control_reg;
+	union cvmx_pcsx_miscx_ctl_reg pcsx_miscx_ctl_reg;
+	bool phy_mode;
+	bool an_disable; /** Disable autonegotiation */
+	bool mode_1000x; /** 1000Base-X mode */
+
+	if (!cvmx_helper_is_port_valid(interface, index))
+		return 0;
+
+	/*
+	 * Take PCS through a reset sequence.
+	 * PCS*_MR*_CONTROL_REG[PWR_DN] should be cleared to zero.
+	 * Write PCS*_MR*_CONTROL_REG[RESET]=1 (while not changing the
+	 * value of the other PCS*_MR*_CONTROL_REG bits).  Read
+	 * PCS*_MR*_CONTROL_REG[RESET] until it changes value to
+	 * zero.
+	 */
+	control_reg.u64 = csr_rd(CVMX_PCSX_MRX_CONTROL_REG(index, interface));
+
+	/*
+	 * Errata G-15618 requires disabling PCS soft reset in CN63XX
+	 * pass upto 2.1.
+	 */
+	if (!__cvmx_helper_need_g15618()) {
+		control_reg.s.reset = 1;
+		csr_wr(CVMX_PCSX_MRX_CONTROL_REG(index, interface),
+		       control_reg.u64);
+		if (CVMX_WAIT_FOR_FIELD64(
+			    CVMX_PCSX_MRX_CONTROL_REG(index, interface),
+			    cvmx_pcsx_mrx_control_reg_t, reset, ==, 0, 10000)) {
+			debug("SGMII%x: Timeout waiting for port %d to finish reset\n",
+			      interface, index);
+			return -1;
+		}
+	}
+
+	/*
+	 * Write PCS*_MR*_CONTROL_REG[RST_AN]=1 to ensure a fresh
+	 * sgmii negotiation starts.
+	 */
+	phy_mode = cvmx_helper_get_mac_phy_mode(interface, index);
+	an_disable = (phy_mode ||
+		      !cvmx_helper_get_port_autonegotiation(interface, index));
+
+	control_reg.s.an_en = !an_disable;
+
+	/* Force a PCS reset by powering down the PCS interface
+	 * This is needed to deal with broken Qualcomm/Atheros PHYs and switches
+	 * which never recover if PCS is not power cycled.  The alternative
+	 * is to power cycle or hardware reset the Qualcomm devices whenever
+	 * SGMII is initialized.
+	 *
+	 * This is needed for the QCA8033 PHYs as well as the QCA833X switches
+	 * to work.  The QCA8337 switch has additional SGMII problems and is
+	 * best avoided if at all possible.  Failure to power cycle PCS prevents
+	 * any traffic from flowing between Octeon and Qualcomm devices if there
+	 * is a warm reset.  Even a software reset to the Qualcomm device will
+	 * not work.
+	 *
+	 * Note that this problem has been reported between Qualcomm and other
+	 * vendor's processors as well so this problem is not unique to
+	 * Qualcomm and Octeon.
+	 *
+	 * Power cycling PCS doesn't hurt anything with non-Qualcomm devices
+	 * other than adding a 25ms delay during initialization.
+	 */
+	control_reg.s.pwr_dn = 1;
+	csr_wr(CVMX_PCSX_MRX_CONTROL_REG(index, interface), control_reg.u64);
+	csr_rd(CVMX_PCSX_MRX_CONTROL_REG(index, interface));
+
+	/* 25ms should be enough, 10ms is too short */
+	mdelay(25);
+
+	control_reg.s.pwr_dn = 0;
+	csr_wr(CVMX_PCSX_MRX_CONTROL_REG(index, interface), control_reg.u64);
+
+	/* The Cortina PHY runs in 1000base-X mode */
+	mode_1000x = cvmx_helper_get_1000x_mode(interface, index);
+	pcsx_miscx_ctl_reg.u64 =
+		csr_rd(CVMX_PCSX_MISCX_CTL_REG(index, interface));
+	pcsx_miscx_ctl_reg.s.mode = mode_1000x;
+	pcsx_miscx_ctl_reg.s.mac_phy = phy_mode;
+	csr_wr(CVMX_PCSX_MISCX_CTL_REG(index, interface),
+	       pcsx_miscx_ctl_reg.u64);
+	if (an_disable)
+		/* In PHY mode we can't query the link status so we just
+		 * assume that the link is up.
+		 */
+		return 0;
+
+	/*
+	 * Wait for PCS*_MR*_STATUS_REG[AN_CPT] to be set, indicating
+	 * that sgmii autonegotiation is complete. In MAC mode this
+	 * isn't an ethernet link, but a link between Octeon and the
+	 * PHY.
+	 */
+	if (CVMX_WAIT_FOR_FIELD64(CVMX_PCSX_MRX_STATUS_REG(index, interface),
+				  union cvmx_pcsx_mrx_status_reg, an_cpt, ==, 1,
+				  10000)) {
+		debug("SGMII%x: Port %d link timeout\n", interface, index);
+		return -1;
+	}
+	return 0;
+}
+
+/**
+ * @INTERNAL
+ * Configure an SGMII link to the specified speed after the SERTES
+ * link is up.
+ *
+ * @param interface to init
+ * @param index     Index of prot on the interface
+ * @param link_info Link state to configure
+ *
+ * @return Zero on success, negative on failure
+ */
+static int
+__cvmx_helper_sgmii_hardware_init_link_speed(int interface, int index,
+					     cvmx_helper_link_info_t link_info)
+{
+	int is_enabled;
+	union cvmx_gmxx_prtx_cfg gmxx_prtx_cfg;
+	union cvmx_pcsx_miscx_ctl_reg pcsx_miscx_ctl_reg;
+
+	if (!cvmx_helper_is_port_valid(interface, index))
+		return 0;
+
+	/* Disable GMX before we make any changes. Remember the enable state */
+	gmxx_prtx_cfg.u64 = csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+	is_enabled = gmxx_prtx_cfg.s.en;
+	gmxx_prtx_cfg.s.en = 0;
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), gmxx_prtx_cfg.u64);
+
+	/* Wait for GMX to be idle */
+	if (CVMX_WAIT_FOR_FIELD64(CVMX_GMXX_PRTX_CFG(index, interface),
+				  cvmx_gmxx_prtx_cfg_t, rx_idle, ==, 1,
+				  10000) ||
+	    CVMX_WAIT_FOR_FIELD64(CVMX_GMXX_PRTX_CFG(index, interface),
+				  cvmx_gmxx_prtx_cfg_t, tx_idle, ==, 1,
+				  10000)) {
+		debug("SGMII%d: Timeout waiting for port %d to be idle\n",
+		      interface, index);
+		return -1;
+	}
+
+	/* Read GMX CFG again to make sure the disable completed */
+	gmxx_prtx_cfg.u64 = csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+
+	/*
+	 * Get the misc control for PCS. We will need to set the
+	 * duplication amount.
+	 */
+	pcsx_miscx_ctl_reg.u64 =
+		csr_rd(CVMX_PCSX_MISCX_CTL_REG(index, interface));
+
+	/*
+	 * Use GMXENO to force the link down if the status we get says
+	 * it should be down.
+	 */
+	pcsx_miscx_ctl_reg.s.gmxeno = !link_info.s.link_up;
+
+	/* Only change the duplex setting if the link is up */
+	if (link_info.s.link_up)
+		gmxx_prtx_cfg.s.duplex = link_info.s.full_duplex;
+
+	/* Do speed based setting for GMX */
+	switch (link_info.s.speed) {
+	case 10:
+		gmxx_prtx_cfg.s.speed = 0;
+		gmxx_prtx_cfg.s.speed_msb = 1;
+		gmxx_prtx_cfg.s.slottime = 0;
+		/* Setting from GMX-603 */
+		pcsx_miscx_ctl_reg.s.samp_pt = 25;
+		csr_wr(CVMX_GMXX_TXX_SLOT(index, interface), 64);
+		csr_wr(CVMX_GMXX_TXX_BURST(index, interface), 0);
+		break;
+	case 100:
+		gmxx_prtx_cfg.s.speed = 0;
+		gmxx_prtx_cfg.s.speed_msb = 0;
+		gmxx_prtx_cfg.s.slottime = 0;
+		pcsx_miscx_ctl_reg.s.samp_pt = 0x5;
+		csr_wr(CVMX_GMXX_TXX_SLOT(index, interface), 64);
+		csr_wr(CVMX_GMXX_TXX_BURST(index, interface), 0);
+		break;
+	case 1000:
+		gmxx_prtx_cfg.s.speed = 1;
+		gmxx_prtx_cfg.s.speed_msb = 0;
+		gmxx_prtx_cfg.s.slottime = 1;
+		pcsx_miscx_ctl_reg.s.samp_pt = 1;
+		csr_wr(CVMX_GMXX_TXX_SLOT(index, interface), 512);
+		if (gmxx_prtx_cfg.s.duplex)
+			/* full duplex */
+			csr_wr(CVMX_GMXX_TXX_BURST(index, interface), 0);
+		else
+			/* half duplex */
+			csr_wr(CVMX_GMXX_TXX_BURST(index, interface), 8192);
+		break;
+	default:
+		break;
+	}
+
+	/* Write the new misc control for PCS */
+	csr_wr(CVMX_PCSX_MISCX_CTL_REG(index, interface),
+	       pcsx_miscx_ctl_reg.u64);
+
+	/* Write the new GMX settings with the port still disabled */
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), gmxx_prtx_cfg.u64);
+
+	/* Read GMX CFG again to make sure the config completed */
+	gmxx_prtx_cfg.u64 = csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+
+	/* Restore the enabled / disabled state */
+	gmxx_prtx_cfg.s.en = is_enabled;
+	csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), gmxx_prtx_cfg.u64);
+
+	return 0;
+}
+
+/**
+ * @INTERNAL
+ * Bring up the SGMII interface to be ready for packet I/O but
+ * leave I/O disabled using the GMX override. This function
+ * follows the bringup documented in 10.6.3 of the manual.
+ *
+ * @param interface to bringup
+ * @param num_ports Number of ports on the interface
+ *
+ * @return Zero on success, negative on failure
+ */
+static int __cvmx_helper_sgmii_hardware_init(int interface, int num_ports)
+{
+	int index;
+	int do_link_set = 1;
+
+	/*
+	 * CN63XX Pass 1.0 errata G-14395 requires the QLM De-emphasis
+	 * be programmed.
+	 */
+	if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_0)) {
+		union cvmx_ciu_qlm2 ciu_qlm;
+
+		ciu_qlm.u64 = csr_rd(CVMX_CIU_QLM2);
+		ciu_qlm.s.txbypass = 1;
+		ciu_qlm.s.txdeemph = 0xf;
+		ciu_qlm.s.txmargin = 0xd;
+		csr_wr(CVMX_CIU_QLM2, ciu_qlm.u64);
+	}
+
+	/*
+	 * CN63XX Pass 2.x errata G-15273 requires the QLM De-emphasis
+	 * be programmed when using a 156.25Mhz ref clock.
+	 */
+	if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS2_X)) {
+		/* Read the QLM speed pins */
+		union cvmx_mio_rst_boot mio_rst_boot;
+
+		mio_rst_boot.u64 = csr_rd(CVMX_MIO_RST_BOOT);
+
+		if (mio_rst_boot.cn63xx.qlm2_spd == 4) {
+			union cvmx_ciu_qlm2 ciu_qlm;
+
+			ciu_qlm.u64 = csr_rd(CVMX_CIU_QLM2);
+			ciu_qlm.s.txbypass = 1;
+			ciu_qlm.s.txdeemph = 0x0;
+			ciu_qlm.s.txmargin = 0xf;
+			csr_wr(CVMX_CIU_QLM2, ciu_qlm.u64);
+		}
+	}
+
+	__cvmx_helper_setup_gmx(interface, num_ports);
+
+	for (index = 0; index < num_ports; index++) {
+		int ipd_port = cvmx_helper_get_ipd_port(interface, index);
+
+		if (!cvmx_helper_is_port_valid(interface, index))
+			continue;
+		__cvmx_helper_sgmii_hardware_init_one_time(interface, index);
+		if (do_link_set)
+			__cvmx_helper_sgmii_link_set(ipd_port,
+						     __cvmx_helper_sgmii_link_get(ipd_port));
+	}
+
+	return 0;
+}
+
+int __cvmx_helper_sgmii_enumerate(int xiface)
+{
+	if (OCTEON_IS_MODEL(OCTEON_CNF71XX))
+		return 2;
+	if (OCTEON_IS_MODEL(OCTEON_CN70XX)) {
+		struct cvmx_xiface xi =
+			cvmx_helper_xiface_to_node_interface(xiface);
+		enum cvmx_qlm_mode qlm_mode =
+			cvmx_qlm_get_dlm_mode(0, xi.interface);
+
+		if (qlm_mode == CVMX_QLM_MODE_SGMII)
+			return 1;
+		else if (qlm_mode == CVMX_QLM_MODE_QSGMII)
+			return 4;
+		return 0;
+	}
+	return 4;
+}
+
+/**
+ * @INTERNAL
+ * Probe a SGMII interface and determine the number of ports
+ * connected to it. The SGMII interface should still be down after
+ * this call.
+ *
+ * @param xiface Interface to probe
+ *
+ * @return Number of ports on the interface. Zero to disable.
+ */
+int __cvmx_helper_sgmii_probe(int xiface)
+{
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int interface = xi.interface;
+	union cvmx_gmxx_inf_mode mode;
+	int ports;
+
+	/*
+	 * Check if QLM is configured correct for SGMII, verify the
+	 * speed as well as mode.
+	 */
+	if (OCTEON_IS_OCTEON2()) {
+		int qlm = cvmx_qlm_interface(xiface);
+
+		if (cvmx_qlm_get_mode(qlm) != CVMX_QLM_MODE_SGMII)
+			return 0;
+	}
+
+	/* Do not enable the interface if is not in SGMII mode */
+	ports = __cvmx_helper_sgmii_enumerate(xiface);
+
+	if (ports <= 0)
+		return 0;
+
+	/*
+	 * Due to errata GMX-700 on CN56XXp1.x and CN52XXp1.x, the
+	 * interface needs to be enabled before IPD otherwise per port
+	 * backpressure may not work properly.
+	 */
+	mode.u64 = csr_rd(CVMX_GMXX_INF_MODE(interface));
+	mode.s.en = 1;
+	csr_wr(CVMX_GMXX_INF_MODE(interface), mode.u64);
+
+	return ports;
+}
+
+/**
+ * @INTERNAL
+ * Bringup and enable a SGMII interface. After this call packet
+ * I/O should be fully functional. This is called with IPD
+ * enabled but PKO disabled.
+ *
+ * @param xiface Interface to bring up
+ *
+ * @return Zero on success, negative on failure
+ */
+int __cvmx_helper_sgmii_enable(int xiface)
+{
+	int num_ports = cvmx_helper_ports_on_interface(xiface);
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int interface = xi.interface;
+	int index;
+
+	/* Setup PKND and BPID */
+	if (octeon_has_feature(OCTEON_FEATURE_PKND)) {
+		for (index = 0; index < num_ports; index++) {
+			union cvmx_gmxx_bpid_msk bpid_msk;
+			union cvmx_gmxx_bpid_mapx bpid_map;
+			union cvmx_gmxx_prtx_cfg gmxx_prtx_cfg;
+
+			if (!cvmx_helper_is_port_valid(interface, index))
+				continue;
+			/* Setup PKIND */
+			gmxx_prtx_cfg.u64 =
+				csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+			gmxx_prtx_cfg.s.pknd =
+				cvmx_helper_get_pknd(interface, index);
+			csr_wr(CVMX_GMXX_PRTX_CFG(index, interface),
+			       gmxx_prtx_cfg.u64);
+
+			/* Setup BPID */
+			bpid_map.u64 =
+				csr_rd(CVMX_GMXX_BPID_MAPX(index, interface));
+			bpid_map.s.val = 1;
+			bpid_map.s.bpid =
+				cvmx_helper_get_bpid(interface, index);
+			csr_wr(CVMX_GMXX_BPID_MAPX(index, interface),
+			       bpid_map.u64);
+
+			bpid_msk.u64 = csr_rd(CVMX_GMXX_BPID_MSK(interface));
+			bpid_msk.s.msk_or |= (1 << index);
+			bpid_msk.s.msk_and &= ~(1 << index);
+			csr_wr(CVMX_GMXX_BPID_MSK(interface), bpid_msk.u64);
+		}
+	}
+
+	__cvmx_helper_sgmii_hardware_init(interface, num_ports);
+
+	/* CN68XX adds the padding and FCS in PKO, not GMX */
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		union cvmx_gmxx_txx_append gmxx_txx_append_cfg;
+
+		for (index = 0; index < num_ports; index++) {
+			if (!cvmx_helper_is_port_valid(interface, index))
+				continue;
+			gmxx_txx_append_cfg.u64 =
+				csr_rd(CVMX_GMXX_TXX_APPEND(index, interface));
+			gmxx_txx_append_cfg.s.fcs = 0;
+			gmxx_txx_append_cfg.s.pad = 0;
+			csr_wr(CVMX_GMXX_TXX_APPEND(index, interface),
+			       gmxx_txx_append_cfg.u64);
+		}
+	}
+
+	/* Enable running disparity check for QSGMII interface */
+	if (OCTEON_IS_MODEL(OCTEON_CN70XX) && num_ports > 1) {
+		union cvmx_gmxx_qsgmii_ctl qsgmii_ctl;
+
+		qsgmii_ctl.u64 = 0;
+		qsgmii_ctl.s.disparity = 1;
+		csr_wr(CVMX_GMXX_QSGMII_CTL(interface), qsgmii_ctl.u64);
+	}
+
+	for (index = 0; index < num_ports; index++) {
+		union cvmx_gmxx_txx_append append_cfg;
+		union cvmx_gmxx_txx_sgmii_ctl sgmii_ctl;
+		union cvmx_gmxx_prtx_cfg gmxx_prtx_cfg;
+
+		if (!cvmx_helper_is_port_valid(interface, index))
+			continue;
+		/*
+		 * Clear the align bit if preamble is set to attain
+		 * maximum tx rate.
+		 */
+		append_cfg.u64 = csr_rd(CVMX_GMXX_TXX_APPEND(index, interface));
+		sgmii_ctl.u64 =
+			csr_rd(CVMX_GMXX_TXX_SGMII_CTL(index, interface));
+		sgmii_ctl.s.align = append_cfg.s.preamble ? 0 : 1;
+		csr_wr(CVMX_GMXX_TXX_SGMII_CTL(index, interface),
+		       sgmii_ctl.u64);
+
+		gmxx_prtx_cfg.u64 =
+			csr_rd(CVMX_GMXX_PRTX_CFG(index, interface));
+		gmxx_prtx_cfg.s.en = 1;
+		csr_wr(CVMX_GMXX_PRTX_CFG(index, interface), gmxx_prtx_cfg.u64);
+	}
+	return 0;
+}
+
+/**
+ * @INTERNAL
+ * Return the link state of an IPD/PKO port as returned by
+ * auto negotiation. The result of this function may not match
+ * Octeon's link config if auto negotiation has changed since
+ * the last call to cvmx_helper_link_set().
+ *
+ * @param ipd_port IPD/PKO port to query
+ *
+ * @return Link state
+ */
+cvmx_helper_link_info_t __cvmx_helper_sgmii_link_get(int ipd_port)
+{
+	cvmx_helper_link_info_t result;
+	union cvmx_pcsx_miscx_ctl_reg pcsx_miscx_ctl_reg;
+	int interface = cvmx_helper_get_interface_num(ipd_port);
+	int index = cvmx_helper_get_interface_index_num(ipd_port);
+	union cvmx_pcsx_mrx_control_reg pcsx_mrx_control_reg;
+	int speed = 1000;
+	int qlm;
+
+	result.u64 = 0;
+
+	if (!cvmx_helper_is_port_valid(interface, index))
+		return result;
+
+	if (OCTEON_IS_MODEL(OCTEON_CN66XX)) {
+		union cvmx_gmxx_inf_mode inf_mode;
+
+		inf_mode.u64 = csr_rd(CVMX_GMXX_INF_MODE(interface));
+		if (inf_mode.s.rate & (1 << index))
+			speed = 2500;
+		else
+			speed = 1000;
+	} else if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
+		qlm = cvmx_qlm_interface(interface);
+		speed = cvmx_qlm_get_gbaud_mhz(qlm) * 8 / 10;
+	} else if (OCTEON_IS_MODEL(OCTEON_CNF71XX)) {
+		speed = cvmx_qlm_get_gbaud_mhz(0) * 8 / 10;
+	} else if (OCTEON_IS_MODEL(OCTEON_CN70XX)) {
+		speed = cvmx_qlm_get_gbaud_mhz(0) * 8 / 10;
+		if (cvmx_qlm_get_dlm_mode(0, interface) == CVMX_QLM_MODE_SGMII)
+			speed >>= 1;
+		else
+			speed >>= 2;
+	}
+
+	pcsx_mrx_control_reg.u64 =
+		csr_rd(CVMX_PCSX_MRX_CONTROL_REG(index, interface));
+	if (pcsx_mrx_control_reg.s.loopbck1) {
+		/* Force 1Gbps full duplex link for internal loopback */
+		result.s.link_up = 1;
+		result.s.full_duplex = 1;
+		result.s.speed = speed;
+		return result;
+	}
+
+	pcsx_miscx_ctl_reg.u64 =
+		csr_rd(CVMX_PCSX_MISCX_CTL_REG(index, interface));
+	if (pcsx_miscx_ctl_reg.s.mac_phy ||
+	    cvmx_helper_get_port_force_link_up(interface, index)) {
+		/* PHY Mode */
+		/* Note that this also works for 1000base-X mode */
+
+		result.s.speed = speed;
+		result.s.full_duplex = 1;
+		result.s.link_up = 1;
+		return result;
+	}
+
+	/* MAC Mode */
+	return __cvmx_helper_board_link_get(ipd_port);
+}
+
+/**
+ * @INTERNAL
+ * Configure an IPD/PKO port for the specified link state. This
+ * function does not influence auto negotiation at the PHY level.
+ * The passed link state must always match the link state returned
+ * by cvmx_helper_link_get(). It is normally best to use
+ * cvmx_helper_link_autoconf() instead.
+ *
+ * @param ipd_port  IPD/PKO port to configure
+ * @param link_info The new link state
+ *
+ * @return Zero on success, negative on failure
+ */
+int __cvmx_helper_sgmii_link_set(int ipd_port,
+				 cvmx_helper_link_info_t link_info)
+{
+	union cvmx_pcsx_mrx_control_reg control_reg;
+	int interface = cvmx_helper_get_interface_num(ipd_port);
+	int index = cvmx_helper_get_interface_index_num(ipd_port);
+
+	if (!cvmx_helper_is_port_valid(interface, index))
+		return 0;
+
+	/* For some devices, i.e. the Qualcomm QCA8337 switch we need to power
+	 * down the PCS interface when the link goes down and power it back
+	 * up when the link returns.
+	 */
+	if (link_info.s.link_up || !__cvmx_helper_need_g15618()) {
+		__cvmx_helper_sgmii_hardware_init_link(interface, index);
+	} else {
+		union cvmx_pcsx_miscx_ctl_reg pcsx_miscx_ctl_reg;
+
+		pcsx_miscx_ctl_reg.u64 =
+			csr_rd(CVMX_PCSX_MISCX_CTL_REG(index, interface));
+
+		/* Disable autonegotiation when MAC mode is enabled or
+		 * autonegotiation is disabled.
+		 */
+		control_reg.u64 =
+			csr_rd(CVMX_PCSX_MRX_CONTROL_REG(index, interface));
+		if (pcsx_miscx_ctl_reg.s.mac_phy == 0 ||
+		    !cvmx_helper_get_port_autonegotiation(interface, index)) {
+			control_reg.s.an_en = 0;
+			control_reg.s.spdmsb = 1;
+			control_reg.s.spdlsb = 0;
+			control_reg.s.dup = 1;
+		}
+		csr_wr(CVMX_PCSX_MRX_CONTROL_REG(index, interface),
+		       control_reg.u64);
+		csr_rd(CVMX_PCSX_MRX_CONTROL_REG(index, interface));
+		/*
+		 * Use GMXENO to force the link down it will get
+		 * reenabled later...
+		 */
+		pcsx_miscx_ctl_reg.s.gmxeno = 1;
+		csr_wr(CVMX_PCSX_MISCX_CTL_REG(index, interface),
+		       pcsx_miscx_ctl_reg.u64);
+		csr_rd(CVMX_PCSX_MISCX_CTL_REG(index, interface));
+		return 0;
+	}
+	return __cvmx_helper_sgmii_hardware_init_link_speed(interface, index,
+							    link_info);
+}
+
+/**
+ * @INTERNAL
+ * Configure a port for internal and/or external loopback. Internal loopback
+ * causes packets sent by the port to be received by Octeon. External loopback
+ * causes packets received from the wire to sent out again.
+ *
+ * @param ipd_port IPD/PKO port to loopback.
+ * @param enable_internal
+ *                 Non zero if you want internal loopback
+ * @param enable_external
+ *                 Non zero if you want external loopback
+ *
+ * @return Zero on success, negative on failure.
+ */
+int __cvmx_helper_sgmii_configure_loopback(int ipd_port, int enable_internal,
+					   int enable_external)
+{
+	int interface = cvmx_helper_get_interface_num(ipd_port);
+	int index = cvmx_helper_get_interface_index_num(ipd_port);
+	union cvmx_pcsx_mrx_control_reg pcsx_mrx_control_reg;
+	union cvmx_pcsx_miscx_ctl_reg pcsx_miscx_ctl_reg;
+
+	if (!cvmx_helper_is_port_valid(interface, index))
+		return 0;
+
+	pcsx_mrx_control_reg.u64 =
+		csr_rd(CVMX_PCSX_MRX_CONTROL_REG(index, interface));
+	pcsx_mrx_control_reg.s.loopbck1 = enable_internal;
+	csr_wr(CVMX_PCSX_MRX_CONTROL_REG(index, interface),
+	       pcsx_mrx_control_reg.u64);
+
+	pcsx_miscx_ctl_reg.u64 =
+		csr_rd(CVMX_PCSX_MISCX_CTL_REG(index, interface));
+	pcsx_miscx_ctl_reg.s.loopbck2 = enable_external;
+	csr_wr(CVMX_PCSX_MISCX_CTL_REG(index, interface),
+	       pcsx_miscx_ctl_reg.u64);
+
+	__cvmx_helper_sgmii_hardware_init_link(interface, index);
+	return 0;
+}
Signature · 1 line
2.35.1
#21 lore raw

PATCH 22/52 mips: octeon: Add cvmx-helper-sfp.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-helper-sfp.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-helper-sfp.c | 1877 +++++++++++++++++++++++
 1 file changed, 1877 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-helper-sfp.c
arch/mips/mach-octeon/cvmx-helper-sfp.c new file +1877 -0
new file mode 100644
index 000000000000..a17ac542fcf8
@@ -0,0 +1,1877 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ */
+
+#include <errno.h>
+#include <i2c.h>
+#include <log.h>
+#include <malloc.h>
+#include <linux/delay.h>
+#include <display_options.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-fdt.h>
+#include <mach/cvmx-helper-cfg.h>
+#include <mach/cvmx-helper-gpio.h>
+#include <mach/cvmx-helper-util.h>
+
+extern void octeon_i2c_unblock(int bus);
+
+static struct cvmx_fdt_sfp_info *sfp_list;
+
+/**
+ * Local allocator to handle both SE and U-Boot that also zeroes out memory
+ *
+ * @param	size	number of bytes to allocate
+ *
+ * @return	pointer to allocated memory or NULL if out of memory.
+ *		Alignment is set to 8-bytes.
+ */
+static void *cvm_sfp_alloc(size_t size)
+{
+	return calloc(size, 1);
+}
+
+/**
+ * Free allocated memory.
+ *
+ * @param	ptr	pointer to memory to free
+ *
+ * NOTE: This only works in U-Boot since SE does not really have a freeing
+ *	 mechanism.  In SE the memory is zeroed out and not freed so this
+ *	 is a memory leak if errors occur.
+ */
+static inline void cvm_sfp_free(void *ptr, size_t size)
+{
+	free(ptr);
+}
+
+/**
+ * Select a QSFP device before accessing the EEPROM
+ *
+ * @param	sfp	handle for sfp/qsfp connector
+ * @param	enable	Set true to select, false to deselect
+ *
+ * @return	0 on success or if SFP or no select GPIO, -1 on GPIO error
+ */
+static int cvmx_qsfp_select(const struct cvmx_fdt_sfp_info *sfp, bool enable)
+{
+	/* Select is only needed for QSFP modules */
+	if (!sfp->is_qsfp) {
+		debug("%s(%s, %d): not QSFP\n", __func__, sfp->name, enable);
+		return 0;
+	}
+
+	if (dm_gpio_is_valid(&sfp->select)) {
+		/* Note that select is active low */
+		return dm_gpio_set_value(&sfp->select, !enable);
+	}
+
+	debug("%s: select GPIO unknown\n", __func__);
+	return 0;
+}
+
+static int cvmx_sfp_parse_sfp_buffer(struct cvmx_sfp_mod_info *sfp_info,
+				     const uint8_t *buffer)
+{
+	u8 csum = 0;
+	bool csum_good = false;
+	int i;
+
+	/* Validate the checksum */
+	for (i = 0; i < 0x3f; i++)
+		csum += buffer[i];
+	csum_good = csum == buffer[0x3f];
+	debug("%s: Lower checksum: 0x%02x, expected: 0x%02x\n", __func__, csum,
+	      buffer[0x3f]);
+	csum = 0;
+	for (i = 0x40; i < 0x5f; i++)
+		csum += buffer[i];
+	debug("%s: Upper checksum: 0x%02x, expected: 0x%02x\n", __func__, csum,
+	      buffer[0x5f]);
+	if (csum != buffer[0x5f] || !csum_good) {
+		debug("Error: SFP EEPROM checksum information is incorrect\n");
+		return -1;
+	}
+
+	sfp_info->conn_type = buffer[0];
+	if (buffer[1] < 1 || buffer[1] > 7) { /* Extended ID */
+		debug("Error: Unknown SFP extended identifier 0x%x\n",
+		      buffer[1]);
+		return -1;
+	}
+	if (buffer[1] != 4) {
+		debug("Module is not SFP/SFP+/SFP28/QSFP+\n");
+		return -1;
+	}
+	sfp_info->mod_type = buffer[2];
+	sfp_info->eth_comp = buffer[3] & 0xf0;
+	sfp_info->cable_comp = buffer[0x24];
+
+	/* There are several ways a cable can be marked as active or
+	 * passive.  8.[2-3] specify the SFP+ cable technology.  Some
+	 * modules also use 3.[0-1] for Infiniband, though it's
+	 * redundant.
+	 */
+	if ((buffer[8] & 0x0C) == 0x08) {
+		sfp_info->limiting = true;
+		sfp_info->active_cable = true;
+	} else if ((buffer[8] & 0xC) == 0x4) {
+		sfp_info->limiting = false;
+		sfp_info->active_cable = false;
+	}
+	if ((buffer[3] & 3) == 2) {
+		sfp_info->active_cable = true;
+		sfp_info->limiting = true;
+	}
+
+	switch (sfp_info->mod_type) {
+	case CVMX_SFP_MOD_OPTICAL_LC:
+	case CVMX_SFP_MOD_OPTICAL_PIGTAIL:
+		sfp_info->copper_cable = false;
+		break;
+	case CVMX_SFP_MOD_COPPER_PIGTAIL:
+		sfp_info->copper_cable = true;
+		break;
+	case CVMX_SFP_MOD_NO_SEP_CONN:
+		switch (sfp_info->cable_comp) {
+		case CVMX_SFP_CABLE_100G_25GAUI_C2M_AOC_HIGH_BER:
+		case CVMX_SFP_CABLE_100G_25GAUI_C2M_AOC_LOW_BER:
+		case CVMX_SFP_CABLE_100G_25GAUI_C2M_ACC_LOW_BER:
+			sfp_info->copper_cable = false;
+			sfp_info->limiting = true;
+			sfp_info->active_cable = true;
+			break;
+
+		case CVMX_SFP_CABLE_100G_SR4_25G_SR:
+		case CVMX_SFP_CABLE_100G_LR4_25G_LR:
+		case CVMX_SFP_CABLE_100G_ER4_25G_ER:
+		case CVMX_SFP_CABLE_100G_SR10:
+		case CVMX_SFP_CABLE_100G_CWDM4_MSA:
+		case CVMX_SFP_CABLE_100G_PSM4:
+		case CVMX_SFP_CABLE_100G_CWDM4:
+		case CVMX_SFP_CABLE_40G_ER4:
+		case CVMX_SFP_CABLE_4X10G_SR:
+		case CVMX_SFP_CABLE_G959_1_P1I1_2D1:
+		case CVMX_SFP_CABLE_G959_1_P1S1_2D2:
+		case CVMX_SFP_CABLE_G959_1_P1L1_2D2:
+		case CVMX_SFP_CABLE_100G_CLR4:
+		case CVMX_SFP_CABLE_100G_2_LAMBDA_DWDM:
+		case CVMX_SFP_CABLE_40G_SWDM4:
+		case CVMX_SFP_CABLE_100G_SWDM4:
+		case CVMX_SFP_CABLE_100G_PAM4_BIDI:
+			sfp_info->copper_cable = false;
+			break;
+
+		case CVMX_SFP_CABLE_100G_25GAUI_C2M_ACC_HIGH_BER:
+		case CVMX_SFP_CABLE_10GBASE_T:
+		case CVMX_SFP_CABLE_10GBASE_T_SR:
+		case CVMX_SFP_CABLE_5GBASE_T:
+		case CVMX_SFP_CABLE_2_5GBASE_T:
+			sfp_info->copper_cable = true;
+			sfp_info->limiting = true;
+			sfp_info->active_cable = true;
+			break;
+
+		case CVMX_SFP_CABLE_100G_CR4_25G_CR_CA_L:
+		case CVMX_SFP_CABLE_25G_CR_CA_S:
+		case CVMX_SFP_CABLE_25G_CR_CA_N:
+		case CVMX_SFP_CABLE_40G_PSM4:
+			sfp_info->copper_cable = true;
+			break;
+
+		default:
+			switch (sfp_info->eth_comp) {
+			case CVMX_SFP_CABLE_10GBASE_ER:
+			case CVMX_SFP_CABLE_10GBASE_LRM:
+			case CVMX_SFP_CABLE_10GBASE_LR:
+			case CVMX_SFP_CABLE_10GBASE_SR:
+				sfp_info->copper_cable = false;
+				break;
+			}
+			break;
+		}
+		break;
+
+	case CVMX_SFP_MOD_RJ45:
+		debug("%s: RJ45 adapter\n", __func__);
+		sfp_info->copper_cable = true;
+		sfp_info->active_cable = true;
+		sfp_info->limiting = true;
+		break;
+	case CVMX_SFP_MOD_UNKNOWN:
+		/* The Avago 1000Base-X to 1000Base-T module reports that it
+		 * is an unknown module type but the Ethernet compliance code
+		 * says it is 1000Base-T.  We'll change the reporting to RJ45.
+		 */
+		if (buffer[6] & 8) {
+			debug("RJ45 gigabit module detected\n");
+			sfp_info->mod_type = CVMX_SFP_MOD_RJ45;
+			sfp_info->copper_cable = false;
+			sfp_info->limiting = true;
+			sfp_info->active_cable = true;
+			sfp_info->max_copper_cable_len = buffer[0x12];
+			sfp_info->rate = CVMX_SFP_RATE_1G;
+		} else {
+			debug("Unknown module type 0x%x\n", sfp_info->mod_type);
+		}
+		sfp_info->limiting = true;
+		break;
+	case CVMX_SFP_MOD_MXC_2X16:
+		debug("%s: MXC 2X16\n", __func__);
+		break;
+	default:
+		sfp_info->limiting = true;
+		break;
+	}
+
+	if (sfp_info->copper_cable)
+		sfp_info->max_copper_cable_len = buffer[0x12];
+	else
+		sfp_info->max_50um_om4_cable_length = buffer[0x12] * 10;
+
+	if (buffer[0xe])
+		sfp_info->max_single_mode_cable_length = buffer[0xe] * 1000;
+	else
+		sfp_info->max_single_mode_cable_length = buffer[0xf] * 100000;
+
+	sfp_info->max_50um_om2_cable_length = buffer[0x10] * 10;
+	sfp_info->max_62_5um_om1_cable_length = buffer[0x11] * 10;
+	sfp_info->max_50um_om3_cable_length = buffer[0x13] * 10;
+
+	if (buffer[0xc] == 0xff) {
+		if (buffer[0x42] >= 255)
+			sfp_info->rate = CVMX_SFP_RATE_100G;
+		else if (buffer[0x42] >= 160)
+			sfp_info->rate = CVMX_SFP_RATE_40G;
+		else if (buffer[0x42] >= 100)
+			sfp_info->rate = CVMX_SFP_RATE_25G;
+		else
+			sfp_info->rate = CVMX_SFP_RATE_UNKNOWN;
+	} else if (buffer[0xc] >= 100) {
+		sfp_info->rate = CVMX_SFP_RATE_10G;
+	} else if (buffer[0xc] >= 10) {
+		sfp_info->rate = CVMX_SFP_RATE_1G;
+	} else {
+		sfp_info->rate = CVMX_SFP_RATE_UNKNOWN;
+	}
+
+	if (sfp_info->rate == CVMX_SFP_RATE_UNKNOWN) {
+		switch (sfp_info->cable_comp) {
+		case CVMX_SFP_CABLE_100G_SR10:
+		case CVMX_SFP_CABLE_100G_CWDM4_MSA:
+		case CVMX_SFP_CABLE_100G_PSM4:
+		case CVMX_SFP_CABLE_100G_CWDM4:
+		case CVMX_SFP_CABLE_100G_CLR4:
+		case CVMX_SFP_CABLE_100G_2_LAMBDA_DWDM:
+		case CVMX_SFP_CABLE_100G_SWDM4:
+		case CVMX_SFP_CABLE_100G_PAM4_BIDI:
+			sfp_info->rate = CVMX_SFP_RATE_100G;
+			break;
+		case CVMX_SFP_CABLE_100G_25GAUI_C2M_AOC_HIGH_BER:
+		case CVMX_SFP_CABLE_100G_SR4_25G_SR:
+		case CVMX_SFP_CABLE_100G_LR4_25G_LR:
+		case CVMX_SFP_CABLE_100G_ER4_25G_ER:
+		case CVMX_SFP_CABLE_100G_25GAUI_C2M_ACC_HIGH_BER:
+		case CVMX_SFP_CABLE_100G_CR4_25G_CR_CA_L:
+		case CVMX_SFP_CABLE_25G_CR_CA_S:
+		case CVMX_SFP_CABLE_25G_CR_CA_N:
+		case CVMX_SFP_CABLE_100G_25GAUI_C2M_AOC_LOW_BER:
+		case CVMX_SFP_CABLE_100G_25GAUI_C2M_ACC_LOW_BER:
+			sfp_info->rate = CVMX_SFP_RATE_25G;
+			break;
+		case CVMX_SFP_CABLE_40G_ER4:
+		case CVMX_SFP_CABLE_4X10G_SR:
+		case CVMX_SFP_CABLE_40G_PSM4:
+		case CVMX_SFP_CABLE_40G_SWDM4:
+			sfp_info->rate = CVMX_SFP_RATE_40G;
+			break;
+		case CVMX_SFP_CABLE_G959_1_P1I1_2D1:
+		case CVMX_SFP_CABLE_G959_1_P1S1_2D2:
+		case CVMX_SFP_CABLE_G959_1_P1L1_2D2:
+		case CVMX_SFP_CABLE_10GBASE_T:
+		case CVMX_SFP_CABLE_10GBASE_T_SR:
+		case CVMX_SFP_CABLE_5GBASE_T:
+		case CVMX_SFP_CABLE_2_5GBASE_T:
+			sfp_info->rate = CVMX_SFP_RATE_10G;
+			break;
+		default:
+			switch (sfp_info->eth_comp) {
+			case CVMX_SFP_CABLE_10GBASE_ER:
+			case CVMX_SFP_CABLE_10GBASE_LRM:
+			case CVMX_SFP_CABLE_10GBASE_LR:
+			case CVMX_SFP_CABLE_10GBASE_SR:
+				sfp_info->rate = CVMX_SFP_RATE_10G;
+				break;
+			default:
+				sfp_info->rate = CVMX_SFP_RATE_UNKNOWN;
+				break;
+			}
+			break;
+		}
+	}
+
+	if (buffer[0xc] < 0xff)
+		sfp_info->bitrate_max = buffer[0xc] * 100;
+	else
+		sfp_info->bitrate_max = buffer[0x42] * 250;
+
+	if ((buffer[8] & 0xc) == 8) {
+		if (buffer[0x3c] & 0x4)
+			sfp_info->limiting = true;
+	}
+
+	/* Currently we only set this for 25G.  FEC is required for CA-S cables
+	 * and for cable lengths >= 5M as of this writing.
+	 */
+	if ((sfp_info->rate == CVMX_SFP_RATE_25G &&
+	     sfp_info->copper_cable) &&
+	    (sfp_info->cable_comp == CVMX_SFP_CABLE_25G_CR_CA_S ||
+	     sfp_info->max_copper_cable_len >= 5))
+		sfp_info->fec_required = true;
+
+	/* copy strings and vendor info, strings will be automatically NUL
+	 * terminated.
+	 */
+	memcpy(sfp_info->vendor_name, &buffer[0x14], 16);
+	memcpy(sfp_info->vendor_oui, &buffer[0x25], 3);
+	memcpy(sfp_info->vendor_pn, &buffer[0x28], 16);
+	memcpy(sfp_info->vendor_rev, &buffer[0x38], 4);
+	memcpy(sfp_info->vendor_sn, &buffer[0x44], 16);
+	memcpy(sfp_info->date_code, &buffer[0x54], 8);
+
+	sfp_info->cooled_laser = !!(buffer[0x40] & 4);
+	sfp_info->internal_cdr = !!(buffer[0x40] & 8);
+
+	if (buffer[0x40] & 0x20)
+		sfp_info->power_level = 3;
+	else
+		sfp_info->power_level = (buffer[0x40] & 2) ? 2 : 1;
+
+	sfp_info->diag_paging = !!(buffer[0x40] & 0x10);
+	sfp_info->linear_rx_output = !(buffer[0x40] & 1);
+	sfp_info->los_implemented = !!(buffer[0x41] & 2);
+	sfp_info->los_inverted = !!(buffer[0x41] & 4);
+	sfp_info->tx_fault_implemented = !!(buffer[0x41] & 8);
+	sfp_info->tx_disable_implemented = !!(buffer[0x41] & 0x10);
+	sfp_info->rate_select_implemented = !!(buffer[0x41] & 0x20);
+	sfp_info->tuneable_transmitter = !!(buffer[0x41] & 0x40);
+	sfp_info->rx_decision_threshold_implemented = !!(buffer[0x41] & 0x80);
+
+	sfp_info->diag_monitoring = !!(buffer[0x5c] & 0x40);
+	sfp_info->diag_rx_power_averaged = !!(buffer[0x5c] & 0x8);
+	sfp_info->diag_externally_calibrated = !!(buffer[0x5c] & 0x10);
+	sfp_info->diag_internally_calibrated = !!(buffer[0x5c] & 0x20);
+	sfp_info->diag_addr_change_required = !!(buffer[0x5c] & 0x4);
+	sfp_info->diag_soft_rate_select_control = !!(buffer[0x5d] & 2);
+	sfp_info->diag_app_select_control = !!(buffer[0x5d] & 4);
+	sfp_info->diag_soft_rate_select_control = !!(buffer[0x5d] & 8);
+	sfp_info->diag_soft_rx_los_implemented = !!(buffer[0x5d] & 0x10);
+	sfp_info->diag_soft_tx_fault_implemented = !!(buffer[0x5d] & 0x20);
+	sfp_info->diag_soft_tx_disable_implemented = !!(buffer[0x5d] & 0x40);
+	sfp_info->diag_alarm_warning_flags_implemented =
+		!!(buffer[0x5d] & 0x80);
+	sfp_info->diag_rev = buffer[0x5e];
+
+	return 0;
+}
+
+static int cvmx_sfp_parse_qsfp_buffer(struct cvmx_sfp_mod_info *sfp_info,
+				      const uint8_t *buffer)
+{
+	u8 csum = 0;
+	bool csum_good = false;
+	int i;
+
+	/* Validate the checksum */
+	for (i = 0x80; i < 0xbf; i++)
+		csum += buffer[i];
+	csum_good = csum == buffer[0xbf];
+	debug("%s: Lower checksum: 0x%02x, expected: 0x%02x\n", __func__, csum,
+	      buffer[0xbf]);
+	csum = 0;
+	for (i = 0xc0; i < 0xdf; i++)
+		csum += buffer[i];
+	debug("%s: Upper checksum: 0x%02x, expected: 0x%02x\n", __func__, csum,
+	      buffer[0xdf]);
+	if (csum != buffer[0xdf] || !csum_good) {
+		debug("Error: SFP EEPROM checksum information is incorrect\n");
+		return -1;
+	}
+
+	sfp_info->conn_type = buffer[0x80];
+	sfp_info->mod_type = buffer[0x82];
+	sfp_info->eth_comp = buffer[0x83] & 0xf0;
+	sfp_info->cable_comp = buffer[0xa4];
+
+	switch (sfp_info->mod_type) {
+	case CVMX_SFP_MOD_COPPER_PIGTAIL:
+	case CVMX_SFP_MOD_NO_SEP_CONN:
+		debug("%s: copper pigtail or no separable cable\n", __func__);
+		/* There are several ways a cable can be marked as active or
+		 * passive.  8.[2-3] specify the SFP+ cable technology.  Some
+		 * modules also use 3.[0-1] for Infiniband, though it's
+		 * redundant.
+		 */
+		sfp_info->copper_cable = true;
+		if ((buffer[0x88] & 0x0C) == 0x08) {
+			sfp_info->limiting = true;
+			sfp_info->active_cable = true;
+		} else if ((buffer[0x88] & 0xC) == 0x4) {
+			sfp_info->limiting = false;
+			sfp_info->active_cable = false;
+		}
+		if ((buffer[0x83] & 3) == 2) {
+			sfp_info->active_cable = true;
+			sfp_info->limiting = true;
+		}
+		break;
+	case CVMX_SFP_MOD_RJ45:
+		debug("%s: RJ45 adapter\n", __func__);
+		sfp_info->copper_cable = true;
+		sfp_info->active_cable = true;
+		sfp_info->limiting = true;
+		break;
+	case CVMX_SFP_MOD_UNKNOWN:
+		debug("Unknown module type\n");
+		/* The Avago 1000Base-X to 1000Base-T module reports that it
+		 * is an unknown module type but the Ethernet compliance code
+		 * says it is 1000Base-T.  We'll change the reporting to RJ45.
+		 */
+		if (buffer[0x86] & 8) {
+			sfp_info->mod_type = CVMX_SFP_MOD_RJ45;
+			sfp_info->copper_cable = false;
+			sfp_info->limiting = true;
+			sfp_info->active_cable = true;
+			sfp_info->max_copper_cable_len = buffer[0x92];
+			sfp_info->rate = CVMX_SFP_RATE_1G;
+		}
+		fallthrough;
+	default:
+		sfp_info->limiting = true;
+		break;
+	}
+
+	if (sfp_info->copper_cable)
+		sfp_info->max_copper_cable_len = buffer[0x92];
+	else
+		sfp_info->max_50um_om4_cable_length = buffer[0x92] * 10;
+
+	debug("%s: copper cable: %d, max copper cable len: %d\n", __func__,
+	      sfp_info->copper_cable, sfp_info->max_copper_cable_len);
+	if (buffer[0xe])
+		sfp_info->max_single_mode_cable_length = buffer[0x8e] * 1000;
+	else
+		sfp_info->max_single_mode_cable_length = buffer[0x8f] * 100000;
+
+	sfp_info->max_50um_om2_cable_length = buffer[0x90] * 10;
+	sfp_info->max_62_5um_om1_cable_length = buffer[0x91] * 10;
+	sfp_info->max_50um_om3_cable_length = buffer[0x93] * 10;
+
+	if (buffer[0x8c] == 12) {
+		sfp_info->rate = CVMX_SFP_RATE_1G;
+	} else if (buffer[0x8c] == 103) {
+		sfp_info->rate = CVMX_SFP_RATE_10G;
+	} else if (buffer[0x8c] == 0xff) {
+		if (buffer[0xc2] == 103)
+			sfp_info->rate = CVMX_SFP_RATE_100G;
+	}
+
+	if (buffer[0x8c] < 0xff)
+		sfp_info->bitrate_max = buffer[0x8c] * 100;
+	else
+		sfp_info->bitrate_max = buffer[0xc2] * 250;
+
+	if ((buffer[0x88] & 0xc) == 8) {
+		if (buffer[0xbc] & 0x4)
+			sfp_info->limiting = true;
+	}
+
+	/* Currently we only set this for 25G.  FEC is required for CA-S cables
+	 * and for cable lengths >= 5M as of this writing.
+	 */
+	/* copy strings and vendor info, strings will be automatically NUL
+	 * terminated.
+	 */
+	memcpy(sfp_info->vendor_name, &buffer[0x94], 16);
+	memcpy(sfp_info->vendor_oui, &buffer[0xa5], 3);
+	memcpy(sfp_info->vendor_pn, &buffer[0xa8], 16);
+	memcpy(sfp_info->vendor_rev, &buffer[0xb8], 4);
+	memcpy(sfp_info->vendor_sn, &buffer[0xc4], 16);
+	memcpy(sfp_info->date_code, &buffer[0xd4], 8);
+
+	sfp_info->linear_rx_output = !!(buffer[0xc0] & 1);
+	sfp_info->cooled_laser = !!(buffer[0xc0] & 4);
+	sfp_info->internal_cdr = !!(buffer[0xc0] & 8);
+
+	if (buffer[0xc0] & 0x20)
+		sfp_info->power_level = 3;
+	else
+		sfp_info->power_level = (buffer[0xc0] & 2) ? 2 : 1;
+
+	sfp_info->diag_paging = !!(buffer[0xc0] & 0x10);
+	sfp_info->los_implemented = !!(buffer[0xc1] & 2);
+	sfp_info->los_inverted = !!(buffer[0xc1] & 4);
+	sfp_info->tx_fault_implemented = !!(buffer[0xc1] & 8);
+	sfp_info->tx_disable_implemented = !!(buffer[0xc1] & 0x10);
+	sfp_info->rate_select_implemented = !!(buffer[0xc1] & 0x20);
+	sfp_info->tuneable_transmitter = !!(buffer[0xc1] & 0x40);
+	sfp_info->rx_decision_threshold_implemented = !!(buffer[0xc1] & 0x80);
+
+	sfp_info->diag_monitoring = !!(buffer[0xdc] & 0x40);
+	sfp_info->diag_rx_power_averaged = !!(buffer[0xdc] & 0x8);
+	sfp_info->diag_externally_calibrated = !!(buffer[0xdc] & 0x10);
+	sfp_info->diag_internally_calibrated = !!(buffer[0xdc] & 0x20);
+	sfp_info->diag_addr_change_required = !!(buffer[0xdc] & 0x4);
+	sfp_info->diag_soft_rate_select_control = !!(buffer[0xdd] & 2);
+	sfp_info->diag_app_select_control = !!(buffer[0xdd] & 4);
+	sfp_info->diag_soft_rate_select_control = !!(buffer[0xdd] & 8);
+	sfp_info->diag_soft_rx_los_implemented = !!(buffer[0xdd] & 0x10);
+	sfp_info->diag_soft_tx_fault_implemented = !!(buffer[0xdd] & 0x20);
+	sfp_info->diag_soft_tx_disable_implemented = !!(buffer[0xdd] & 0x40);
+	sfp_info->diag_alarm_warning_flags_implemented =
+		!!(buffer[0xdd] & 0x80);
+	sfp_info->diag_rev = buffer[0xde];
+
+	return 0;
+}
+
+static bool sfp_verify_checksum(const uint8_t *buffer)
+{
+	u8 csum = 0;
+	u8 offset;
+	bool csum_good = false;
+	int i;
+
+	switch (buffer[0]) {
+	case CVMX_SFP_CONN_QSFP:
+	case CVMX_SFP_CONN_QSFPP:
+	case CVMX_SFP_CONN_QSFP28:
+	case CVMX_SFP_CONN_MICRO_QSFP:
+	case CVMX_SFP_CONN_QSFP_DD:
+		offset = 0x80;
+		break;
+	default:
+		offset = 0;
+		break;
+	}
+	for (i = offset; i < offset + 0x3f; i++)
+		csum += buffer[i];
+	csum_good = csum == buffer[offset + 0x3f];
+	if (!csum_good) {
+		debug("%s: Lower checksum bad, got 0x%x, expected 0x%x\n",
+		      __func__, csum, buffer[offset + 0x3f]);
+		return false;
+	}
+	csum = 0;
+	for (i = offset + 0x40; i < offset + 0x5f; i++)
+		csum += buffer[i];
+	if (csum != buffer[offset + 0x5f]) {
+		debug("%s: Upper checksum bad, got 0x%x, expected 0x%x\n",
+		      __func__, csum, buffer[offset + 0x5f]);
+		return false;
+	}
+	return true;
+}
+
+/**
+ * Reads and parses SFP/QSFP EEPROM
+ *
+ * @param	sfp	sfp handle to read
+ *
+ * @return	0 for success, -1 on error.
+ */
+int cvmx_sfp_read_i2c_eeprom(struct cvmx_fdt_sfp_info *sfp)
+{
+	const struct cvmx_fdt_i2c_bus_info *bus = sfp->i2c_bus;
+	int oct_bus = cvmx_fdt_i2c_get_root_bus(bus);
+	struct udevice *dev;
+	u8 buffer[256];
+	bool is_qsfp;
+	int retry;
+	int err;
+
+	if (!bus) {
+		debug("%s(%s): Error: i2c bus undefined for eeprom\n", __func__,
+		      sfp->name);
+		return -1;
+	}
+
+	is_qsfp = (sfp->sfp_info.conn_type == CVMX_SFP_CONN_QSFP ||
+		   sfp->sfp_info.conn_type == CVMX_SFP_CONN_QSFPP ||
+		   sfp->sfp_info.conn_type == CVMX_SFP_CONN_QSFP28 ||
+		   sfp->sfp_info.conn_type == CVMX_SFP_CONN_MICRO_QSFP) ||
+		  sfp->is_qsfp;
+
+	err = cvmx_qsfp_select(sfp, true);
+	if (err) {
+		debug("%s: Error selecting SFP/QSFP slot\n", __func__);
+		return err;
+	}
+
+	debug("%s: Reading eeprom from i2c address %d:0x%x\n", __func__,
+	      oct_bus, sfp->i2c_eeprom_addr);
+	for (retry = 0; retry < 3; retry++) {
+		err = i2c_get_chip(bus->i2c_bus, sfp->i2c_eeprom_addr, 1, &dev);
+		if (err) {
+			debug("Cannot find I2C device: %d\n", err);
+			goto error;
+		}
+
+		err = dm_i2c_read(dev, 0, buffer, 256);
+		if (err || !sfp_verify_checksum(buffer)) {
+			debug("%s: Error %d reading eeprom at 0x%x, bus %d\n",
+			      __func__, err, sfp->i2c_eeprom_addr, oct_bus);
+			debug("%s: Retry %d\n", __func__, retry + 1);
+			mdelay(1000);
+		} else {
+			break;
+		}
+	}
+	if (err) {
+		debug("%s: Error reading eeprom from SFP %s\n", __func__,
+		      sfp->name);
+		return -1;
+	}
+#ifdef DEBUG
+	print_buffer(0, buffer, 1, 256, 0);
+#endif
+	memset(&sfp->sfp_info, 0, sizeof(struct cvmx_sfp_mod_info));
+
+	switch (buffer[0]) {
+	case CVMX_SFP_CONN_SFP:
+		err = cvmx_sfp_parse_sfp_buffer(&sfp->sfp_info, buffer);
+		break;
+	case CVMX_SFP_CONN_QSFP:
+	case CVMX_SFP_CONN_QSFPP:
+	case CVMX_SFP_CONN_QSFP28:
+	case CVMX_SFP_CONN_MICRO_QSFP:
+		err = cvmx_sfp_parse_qsfp_buffer(&sfp->sfp_info, buffer);
+		break;
+	default:
+		debug("%s: Unknown SFP transceiver type 0x%x\n", __func__,
+		      buffer[0]);
+		err = -1;
+		break;
+	}
+
+error:
+	if (is_qsfp)
+		err |= cvmx_qsfp_select(sfp, false);
+
+	if (!err) {
+		sfp->valid = true;
+		sfp->sfp_info.valid = true;
+	} else {
+		sfp->valid = false;
+		sfp->sfp_info.valid = false;
+	}
+
+	return err;
+}
+
+/**
+ * Returns the information about a SFP/QSFP device
+ *
+ * @param       sfp             sfp handle
+ *
+ * @return      sfp_info        Pointer sfp mod info data structure
+ */
+const struct cvmx_sfp_mod_info *
+cvmx_phy_get_sfp_mod_info(const struct cvmx_fdt_sfp_info *sfp)
+{
+	return (sfp) ? &sfp->sfp_info : NULL;
+}
+
+/**
+ * Function called to check and return the status of the mod_abs pin or
+ * mod_pres pin for QSFPs.
+ *
+ * @param	sfp	Handle to SFP information.
+ * @param	data	User-defined data passed to the function
+ *
+ * @return	0 if absent, 1 if present, -1 on error
+ */
+int cvmx_sfp_check_mod_abs(struct cvmx_fdt_sfp_info *sfp, void *data)
+{
+	int val;
+	int err = 0;
+	int mode;
+
+	if (!dm_gpio_is_valid(&sfp->mod_abs)) {
+		debug("%s: Error: mod_abs not set for %s\n", __func__,
+		      sfp->name);
+		return -1;
+	}
+	val = dm_gpio_get_value(&sfp->mod_abs);
+	debug("%s(%s, %p) mod_abs: %d\n", __func__, sfp->name, data, val);
+	if (val >= 0 && val != sfp->last_mod_abs && sfp->mod_abs_changed) {
+		err = 0;
+		if (!val) {
+			err = cvmx_sfp_read_i2c_eeprom(sfp);
+			if (err)
+				debug("%s: Error reading SFP %s EEPROM\n",
+				      __func__, sfp->name);
+		}
+		err = sfp->mod_abs_changed(sfp, val, sfp->mod_abs_changed_data);
+	}
+	debug("%s(%s (%p)): Last mod_abs: %d, current: %d, changed: %p, rc: %d, next: %p, caller: %p\n",
+	      __func__, sfp->name, sfp, sfp->last_mod_abs, val,
+	      sfp->mod_abs_changed, err, sfp->next_iface_sfp,
+	      __builtin_return_address(0));
+
+	if (err >= 0) {
+		sfp->last_mod_abs = val;
+		mode = cvmx_helper_interface_get_mode(sfp->xiface);
+		cvmx_sfp_validate_module(sfp, mode);
+	} else {
+		debug("%s: mod_abs_changed for %s returned error\n", __func__,
+		      sfp->name);
+	}
+
+	return err < 0 ? err : val;
+}
+
+/**
+ * Reads the EEPROMs of all SFP modules.
+ *
+ * @return 0 for success
+ */
+int cvmx_sfp_read_all_modules(void)
+{
+	struct cvmx_fdt_sfp_info *sfp;
+	int val;
+	bool error = false;
+	int rc;
+
+	for (sfp = sfp_list; sfp; sfp = sfp->next) {
+		if (dm_gpio_is_valid(&sfp->mod_abs)) {
+			/* Check if module absent */
+			val = dm_gpio_get_value(&sfp->mod_abs);
+			sfp->last_mod_abs = val;
+			if (val)
+				continue;
+		}
+		rc = cvmx_sfp_read_i2c_eeprom(sfp);
+		if (rc) {
+			debug("%s: Error reading eeprom from SFP %s\n",
+			      __func__, sfp->name);
+			error = true;
+		}
+	}
+
+	return error ? -1 : 0;
+}
+
+/**
+ * Registers a function to be called to check mod_abs/mod_pres for a SFP/QSFP
+ * slot.
+ *
+ * @param	sfp		Handle to SFP data structure
+ * @param	check_mod_abs	Function to be called or NULL to remove
+ * @param	mod_abs_data	User-defined data to be passed to check_mod_abs
+ *
+ * @return	0 for success
+ */
+int cvmx_sfp_register_check_mod_abs(struct cvmx_fdt_sfp_info *sfp,
+				    int (*check_mod_abs)(struct cvmx_fdt_sfp_info *sfp,
+							 void *data),
+				    void *mod_abs_data)
+{
+	struct cvmx_fdt_sfp_info *nsfp = sfp; /** For walking list */
+
+	do {
+		if (nsfp->xiface == sfp->xiface && nsfp->index == sfp->index) {
+			nsfp->check_mod_abs = check_mod_abs;
+			nsfp->mod_abs_data = mod_abs_data;
+		}
+		nsfp = nsfp->next;
+	} while (nsfp);
+
+	return 0;
+}
+
+/**
+ * Registers a function to be called whenever the mod_abs/mod_pres signal
+ * changes.
+ *
+ * @param	sfp		Handle to SFP data structure
+ * @param	mod_abs_changed	Function called whenever mod_abs is changed
+ *				or NULL to remove.
+ * @param	mod_abs_changed_data	User-defined data passed to
+ *					mod_abs_changed
+ *
+ * @return	0 for success
+ *
+ * @NOTE: If multiple SFP slots are linked together, all subsequent slots
+ *	  will also be registered for the same handler.
+ */
+int cvmx_sfp_register_mod_abs_changed(struct cvmx_fdt_sfp_info *sfp,
+				      int (*mod_abs_changed)(struct cvmx_fdt_sfp_info *sfp,
+							     int val, void *data),
+				      void *mod_abs_changed_data)
+{
+	sfp->mod_abs_changed = mod_abs_changed;
+	sfp->mod_abs_changed_data = mod_abs_changed_data;
+
+	sfp->last_mod_abs = -2; /* undefined */
+
+	return 0;
+}
+
+/**
+ * Function called to check and return the status of the tx_fault pin
+ *
+ * @param	sfp	Handle to SFP information.
+ * @param	data	User-defined data passed to the function
+ *
+ * @return	0 if signal present, 1 if signal absent, -1 on error
+ */
+int cvmx_sfp_check_tx_fault(struct cvmx_fdt_sfp_info *sfp, void *data)
+{
+	int val;
+
+	debug("%s(%s, %p)\n", __func__, sfp->name, data);
+	if (!dm_gpio_is_valid(&sfp->tx_error)) {
+		printf("%s: Error: tx_error not set for %s\n", __func__,
+		       sfp->name);
+		return -1;
+	}
+	val = dm_gpio_get_value(&sfp->tx_error);
+	debug("%s: tx_fault: %d\n", __func__, val);
+
+	return val;
+}
+
+/**
+ * Function called to check and return the status of the rx_los pin
+ *
+ * @param	sfp	Handle to SFP information.
+ * @param	data	User-defined data passed to the function
+ *
+ * @return	0 if signal present, 1 if signal absent, -1 on error
+ */
+int cvmx_sfp_check_rx_los(struct cvmx_fdt_sfp_info *sfp, void *data)
+{
+	int val;
+	int err;
+
+	debug("%s(%s, %p)\n", __func__, sfp->name, data);
+	if (!dm_gpio_is_valid(&sfp->rx_los)) {
+		printf("%s: Error: rx_los not set for %s\n", __func__,
+		       sfp->name);
+		return -1;
+	}
+	val = dm_gpio_get_value(&sfp->rx_los);
+	if (val >= 0 && val != sfp->last_rx_los && sfp->rx_los_changed)
+		err = sfp->rx_los_changed(sfp, val, sfp->rx_los_changed_data);
+	debug("%s: Last rx_los: %d, current: %d, changed: %p, rc: %d\n",
+	      __func__, sfp->last_rx_los, val, sfp->rx_los_changed, err);
+	sfp->last_rx_los = val;
+
+	return val;
+}
+
+/**
+ * Registers a function to be called whenever rx_los changes
+ *
+ * @param	sfp		Handle to SFP data structure
+ * @param	rx_los_changed	Function to be called when rx_los changes
+ *				or NULL to remove the function
+ * @param	rx_los_changed_data	User-defined data passed to
+ *					rx_los_changed
+ *
+ * @return	0 for success
+ */
+int cvmx_sfp_register_rx_los_changed(struct cvmx_fdt_sfp_info *sfp,
+				     int (*rx_los_changed)(struct cvmx_fdt_sfp_info *sfp,
+							   int val, void *data),
+				     void *rx_los_changed_data)
+{
+	sfp->rx_los_changed = rx_los_changed;
+	sfp->rx_los_changed_data = rx_los_changed_data;
+	sfp->last_rx_los = -2;
+
+	return 0;
+}
+
+/**
+ * Parses a SFP slot from the device tree
+ *
+ * @param	sfp		SFP handle to store data in
+ * @param	fdt_addr	Address of flat device tree
+ * @param	of_offset	Node in device tree for SFP slot
+ *
+ * @return	0 on success, -1 on error
+ */
+static int cvmx_sfp_parse_sfp(struct cvmx_fdt_sfp_info *sfp, ofnode node)
+{
+	struct ofnode_phandle_args phandle;
+	int err;
+
+	sfp->name = ofnode_get_name(node);
+	sfp->of_offset = ofnode_to_offset(node);
+
+	err = gpio_request_by_name_nodev(node, "tx_disable", 0,
+					 &sfp->tx_disable, GPIOD_IS_OUT);
+	if (err) {
+		printf("%s: tx_disable not found in DT!\n", __func__);
+		return -ENODEV;
+	}
+	dm_gpio_set_value(&sfp->tx_disable, 0);
+
+	err = gpio_request_by_name_nodev(node, "mod_abs", 0,
+					 &sfp->mod_abs, GPIOD_IS_IN);
+	if (err) {
+		printf("%s: mod_abs not found in DT!\n", __func__);
+		return -ENODEV;
+	}
+
+	err = gpio_request_by_name_nodev(node, "tx_error", 0,
+					 &sfp->tx_error, GPIOD_IS_IN);
+	if (err) {
+		printf("%s: tx_error not found in DT!\n", __func__);
+		return -ENODEV;
+	}
+
+	err = gpio_request_by_name_nodev(node, "rx_los", 0,
+					 &sfp->rx_los, GPIOD_IS_IN);
+	if (err) {
+		printf("%s: rx_los not found in DT!\n", __func__);
+		return -ENODEV;
+	}
+
+	err = ofnode_parse_phandle_with_args(node, "eeprom", NULL, 0, 0,
+					     &phandle);
+	if (!err) {
+		sfp->i2c_eeprom_addr = ofnode_get_addr(phandle.node);
+		debug("%s: eeprom address: 0x%x\n", __func__,
+		      sfp->i2c_eeprom_addr);
+
+		debug("%s: Getting eeprom i2c bus for %s\n", __func__,
+		      sfp->name);
+		sfp->i2c_bus = cvmx_ofnode_get_i2c_bus(ofnode_get_parent(phandle.node));
+	}
+
+	err = ofnode_parse_phandle_with_args(node, "diag", NULL, 0, 0,
+					     &phandle);
+	if (!err) {
+		sfp->i2c_diag_addr = ofnode_get_addr(phandle.node);
+		if (!sfp->i2c_bus)
+			sfp->i2c_bus = cvmx_ofnode_get_i2c_bus(ofnode_get_parent(phandle.node));
+	}
+
+	sfp->last_mod_abs = -2;
+	sfp->last_rx_los = -2;
+
+	if (!sfp->i2c_bus) {
+		debug("%s(%s): Error: could not get i2c bus from device tree\n",
+		      __func__, sfp->name);
+		err = -1;
+	}
+
+	if (err) {
+		dm_gpio_free(sfp->tx_disable.dev, &sfp->tx_disable);
+		dm_gpio_free(sfp->mod_abs.dev, &sfp->mod_abs);
+		dm_gpio_free(sfp->tx_error.dev, &sfp->tx_error);
+		dm_gpio_free(sfp->rx_los.dev, &sfp->rx_los);
+	} else {
+		sfp->valid = true;
+	}
+
+	return err;
+}
+
+/**
+ * Parses a QSFP slot from the device tree
+ *
+ * @param	sfp		SFP handle to store data in
+ * @param	fdt_addr	Address of flat device tree
+ * @param	of_offset	Node in device tree for SFP slot
+ *
+ * @return	0 on success, -1 on error
+ */
+static int cvmx_sfp_parse_qsfp(struct cvmx_fdt_sfp_info *sfp, ofnode node)
+{
+	struct ofnode_phandle_args phandle;
+	int err;
+
+	sfp->is_qsfp = true;
+	sfp->name = ofnode_get_name(node);
+	sfp->of_offset = ofnode_to_offset(node);
+
+	err = gpio_request_by_name_nodev(node, "lp_mode", 0,
+					 &sfp->lp_mode, GPIOD_IS_OUT);
+	if (err) {
+		printf("%s: lp_mode not found in DT!\n", __func__);
+		return -ENODEV;
+	}
+
+	err = gpio_request_by_name_nodev(node, "mod_prs", 0,
+					 &sfp->mod_abs, GPIOD_IS_IN);
+	if (err) {
+		printf("%s: mod_prs not found in DT!\n", __func__);
+		return -ENODEV;
+	}
+
+	err = gpio_request_by_name_nodev(node, "select", 0,
+					 &sfp->select, GPIOD_IS_IN);
+	if (err) {
+		printf("%s: select not found in DT!\n", __func__);
+		return -ENODEV;
+	}
+
+	err = gpio_request_by_name_nodev(node, "reset", 0,
+					 &sfp->reset, GPIOD_IS_OUT);
+	if (err) {
+		printf("%s: reset not found in DT!\n", __func__);
+		return -ENODEV;
+	}
+
+	err = gpio_request_by_name_nodev(node, "interrupt", 0,
+					 &sfp->interrupt, GPIOD_IS_IN);
+	if (err) {
+		printf("%s: interrupt not found in DT!\n", __func__);
+		return -ENODEV;
+	}
+
+	err = ofnode_parse_phandle_with_args(node, "eeprom", NULL, 0, 0,
+					     &phandle);
+	if (!err) {
+		sfp->i2c_eeprom_addr = ofnode_get_addr(phandle.node);
+		sfp->i2c_bus = cvmx_ofnode_get_i2c_bus(ofnode_get_parent(phandle.node));
+	}
+
+	err = ofnode_parse_phandle_with_args(node, "diag", NULL, 0, 0,
+					     &phandle);
+	if (!err) {
+		sfp->i2c_diag_addr = ofnode_get_addr(phandle.node);
+		if (!sfp->i2c_bus)
+			sfp->i2c_bus = cvmx_ofnode_get_i2c_bus(ofnode_get_parent(phandle.node));
+	}
+
+	sfp->last_mod_abs = -2;
+	sfp->last_rx_los = -2;
+
+	if (!sfp->i2c_bus) {
+		cvmx_printf("%s(%s): Error: could not get i2c bus from device tree\n",
+			    __func__, sfp->name);
+		err = -1;
+	}
+
+	if (err) {
+		dm_gpio_free(sfp->lp_mode.dev, &sfp->lp_mode);
+		dm_gpio_free(sfp->mod_abs.dev, &sfp->mod_abs);
+		dm_gpio_free(sfp->select.dev, &sfp->select);
+		dm_gpio_free(sfp->reset.dev, &sfp->reset);
+		dm_gpio_free(sfp->interrupt.dev, &sfp->interrupt);
+	} else {
+		sfp->valid = true;
+	}
+
+	return err;
+}
+
+/**
+ * Parses the device tree for SFP and QSFP slots
+ *
+ * @param	fdt_addr	Address of flat device-tree
+ *
+ * @return	0 for success, -1 on error
+ */
+int cvmx_sfp_parse_device_tree(const void *fdt_addr)
+{
+	struct cvmx_fdt_sfp_info *sfp, *first_sfp = NULL, *last_sfp = NULL;
+	ofnode node;
+	int err = 0;
+	int reg;
+	static bool parsed;
+
+	debug("%s(%p): Parsing...\n", __func__, fdt_addr);
+	if (parsed) {
+		debug("%s(%p): Already parsed\n", __func__, fdt_addr);
+		return 0;
+	}
+
+	ofnode_for_each_compatible_node(node, "ethernet,sfp-slot") {
+		if (!ofnode_valid(node))
+			continue;
+
+		sfp = cvm_sfp_alloc(sizeof(*sfp));
+		if (!sfp)
+			return -1;
+
+		err = cvmx_sfp_parse_sfp(sfp, node);
+		if (!err) {
+			if (!sfp_list)
+				sfp_list = sfp;
+			if (last_sfp)
+				last_sfp->next = sfp;
+			sfp->prev = last_sfp;
+			last_sfp = sfp;
+			debug("%s: parsed %s\n", __func__, sfp->name);
+		} else {
+			debug("%s: Error parsing SFP at node %s\n",
+			      __func__, ofnode_get_name(node));
+			return err;
+		}
+	}
+
+	ofnode_for_each_compatible_node(node, "ethernet,qsfp-slot") {
+		if (!ofnode_valid(node))
+			continue;
+
+		sfp = cvm_sfp_alloc(sizeof(*sfp));
+		if (!sfp)
+			return -1;
+
+		err = cvmx_sfp_parse_qsfp(sfp, node);
+		if (!err) {
+			if (!sfp_list)
+				sfp_list = sfp;
+			if (last_sfp)
+				last_sfp->next = sfp;
+			sfp->prev = last_sfp;
+			last_sfp = sfp;
+			debug("%s: parsed %s\n", __func__, sfp->name);
+		} else {
+			debug("%s: Error parsing QSFP at node %s\n",
+			      __func__, ofnode_get_name(node));
+			return err;
+		}
+	}
+
+	if (!octeon_has_feature(OCTEON_FEATURE_BGX))
+		return 0;
+
+	err = 0;
+	ofnode_for_each_compatible_node(node, "cavium,octeon-7890-bgx-port") {
+		int sfp_nodes[4];
+		ofnode sfp_ofnodes[4];
+		int num_sfp_nodes;
+		u64 reg_addr;
+		struct cvmx_xiface xi;
+		int xiface, index;
+		cvmx_helper_interface_mode_t mode;
+		int i;
+		int rc;
+
+		if (!ofnode_valid(node))
+			break;
+
+		num_sfp_nodes = ARRAY_SIZE(sfp_nodes);
+		rc = cvmx_ofnode_lookup_phandles(node, "sfp-slot",
+						 &num_sfp_nodes, sfp_ofnodes);
+		if (rc != 0 || num_sfp_nodes < 1)
+			rc = cvmx_ofnode_lookup_phandles(node, "qsfp-slot",
+							 &num_sfp_nodes,
+							 sfp_ofnodes);
+		/* If no SFP or QSFP slot found, go to next port */
+		if (rc < 0)
+			continue;
+
+		last_sfp = NULL;
+		for (i = 0; i < num_sfp_nodes; i++) {
+			sfp = cvmx_sfp_find_slot_by_fdt_node(ofnode_to_offset(sfp_ofnodes[i]));
+			debug("%s: Adding sfp %s (%p) to BGX port\n",
+			      __func__, sfp->name, sfp);
+			if (last_sfp)
+				last_sfp->next_iface_sfp = sfp;
+			else
+				first_sfp = sfp;
+			last_sfp = sfp;
+		}
+		if (!first_sfp) {
+			debug("%s: Error: could not find SFP slot for BGX port %s\n",
+			      __func__,
+			      fdt_get_name(fdt_addr, sfp_nodes[0],
+					   NULL));
+			err = -1;
+			break;
+		}
+
+		/* Get the port index */
+		reg = ofnode_get_addr(node);
+		if (reg < 0) {
+			debug("%s: Error: could not get BGX port reg value\n",
+			      __func__);
+			err = -1;
+			break;
+		}
+		index = reg;
+
+		/* Get BGX node and address */
+		reg_addr = ofnode_get_addr(ofnode_get_parent(node));
+		/* Extrace node */
+		xi.node = cvmx_csr_addr_to_node(reg_addr);
+		/* Extract reg address */
+		reg_addr = cvmx_csr_addr_strip_node(reg_addr);
+		if ((reg_addr & 0xFFFFFFFFF0000000) !=
+		    0x00011800E0000000) {
+			debug("%s: Invalid BGX address 0x%llx\n",
+			      __func__, (unsigned long long)reg_addr);
+			xi.node = -1;
+			err = -1;
+			break;
+		}
+
+		/* Extract interface from address */
+		xi.interface = (reg_addr >> 24) & 0x0F;
+		/* Convert to xiface */
+		xiface = cvmx_helper_node_interface_to_xiface(xi.node,
+							      xi.interface);
+		debug("%s: Parsed %d SFP slots for interface 0x%x, index %d\n",
+		      __func__, num_sfp_nodes, xiface, index);
+
+		mode = cvmx_helper_interface_get_mode(xiface);
+		for (sfp = first_sfp; sfp; sfp = sfp->next_iface_sfp) {
+			sfp->xiface = xiface;
+			sfp->index = index;
+			/* Convert to IPD port */
+			sfp->ipd_port[0] =
+				cvmx_helper_get_ipd_port(xiface, index);
+			debug("%s: sfp %s (%p) xi: 0x%x, index: 0x%x, node: %d, mode: 0x%x, next: %p\n",
+			      __func__, sfp->name, sfp, sfp->xiface,
+			      sfp->index, xi.node, mode,
+			      sfp->next_iface_sfp);
+			if (mode == CVMX_HELPER_INTERFACE_MODE_XLAUI ||
+			    mode == CVMX_HELPER_INTERFACE_MODE_40G_KR4)
+				for (i = 1; i < 4; i++)
+					sfp->ipd_port[i] = -1;
+			else
+				for (i = 1; i < 4; i++)
+					sfp->ipd_port[i] =
+						cvmx_helper_get_ipd_port(
+							xiface, i);
+		}
+		cvmx_helper_cfg_set_sfp_info(xiface, index, first_sfp);
+	}
+
+	if (!err) {
+		parsed = true;
+		cvmx_sfp_read_all_modules();
+	}
+
+	return err;
+}
+
+/**
+ * Given an IPD port number find the corresponding SFP or QSFP slot
+ *
+ * @param	ipd_port	IPD port number to search for
+ *
+ * @return	pointer to SFP data structure or NULL if not found
+ */
+struct cvmx_fdt_sfp_info *cvmx_sfp_find_slot_by_port(int ipd_port)
+{
+	struct cvmx_fdt_sfp_info *sfp = sfp_list;
+	int i;
+
+	while (sfp) {
+		for (i = 0; i < 4; i++)
+			if (sfp->ipd_port[i] == ipd_port)
+				return sfp;
+		sfp = sfp->next;
+	}
+	return NULL;
+}
+
+/**
+ * Given a fdt node offset find the corresponding SFP or QSFP slot
+ *
+ * @param	of_offset	flat device tree node offset
+ *
+ * @return	pointer to SFP data structure or NULL if not found
+ */
+struct cvmx_fdt_sfp_info *cvmx_sfp_find_slot_by_fdt_node(int of_offset)
+{
+	struct cvmx_fdt_sfp_info *sfp = sfp_list;
+
+	while (sfp) {
+		if (sfp->of_offset == of_offset)
+			return sfp;
+		sfp = sfp->next;
+	}
+	return NULL;
+}
+
+static bool cvmx_sfp_validate_quad(struct cvmx_fdt_sfp_info *sfp,
+				   struct cvmx_phy_gpio_leds *leds)
+{
+	bool multi_led = leds && (leds->next);
+	bool error = false;
+	int mod_abs;
+
+	do {
+		/* Skip missing modules */
+		if (dm_gpio_is_valid(&sfp->mod_abs))
+			mod_abs = dm_gpio_get_value(&sfp->mod_abs);
+		else
+			mod_abs = 0;
+		if (!mod_abs) {
+			if (cvmx_sfp_read_i2c_eeprom(sfp)) {
+				debug("%s: Error reading eeprom for %s\n",
+				      __func__, sfp->name);
+			}
+			if (sfp->sfp_info.rate < CVMX_SFP_RATE_10G) {
+				cvmx_helper_leds_show_error(leds, true);
+				error = true;
+			} else if (sfp->sfp_info.rate >= CVMX_SFP_RATE_10G) {
+				/* We don't support 10GBase-T modules in
+				 * this mode.
+				 */
+				switch (sfp->sfp_info.cable_comp) {
+				case CVMX_SFP_CABLE_10GBASE_T:
+				case CVMX_SFP_CABLE_10GBASE_T_SR:
+				case CVMX_SFP_CABLE_5GBASE_T:
+				case CVMX_SFP_CABLE_2_5GBASE_T:
+					cvmx_helper_leds_show_error(leds, true);
+					error = true;
+					break;
+				default:
+					break;
+				}
+			}
+		} else if (multi_led) {
+			cvmx_helper_leds_show_error(leds, false);
+		}
+
+		if (multi_led && leds->next)
+			leds = leds->next;
+		sfp = sfp->next_iface_sfp;
+	} while (sfp);
+
+	if (!multi_led)
+		cvmx_helper_leds_show_error(leds, error);
+
+	return error;
+}
+
+/**
+ * Validates if the module is correct for the specified port
+ *
+ * @param[in]	sfp	SFP port to check
+ * @param	xiface	interface
+ * @param	index	port index
+ * @param	speed	link speed, -1 if unknown
+ * @param	mode	interface mode
+ *
+ * @return	true if module is valid, false if invalid
+ * NOTE: This will also toggle the error LED, if present
+ */
+bool cvmx_sfp_validate_module(struct cvmx_fdt_sfp_info *sfp, int mode)
+{
+	const struct cvmx_sfp_mod_info *mod_info = &sfp->sfp_info;
+	int xiface = sfp->xiface;
+	int index = sfp->index;
+	struct cvmx_phy_gpio_leds *leds;
+	bool error = false;
+	bool quad_mode = false;
+
+	debug("%s(%s, 0x%x, 0x%x, 0x%x)\n", __func__, sfp->name, xiface, index,
+	      mode);
+	if (!sfp) {
+		debug("%s: Error: sfp is NULL\n", __func__);
+		return false;
+	}
+	/* No module is valid */
+	leds = cvmx_helper_get_port_phy_leds(xiface, index);
+	if (!leds)
+		debug("%s: No leds for 0x%x:0x%x\n", __func__, xiface, index);
+
+	if (mode != CVMX_HELPER_INTERFACE_MODE_XLAUI &&
+	    mode != CVMX_HELPER_INTERFACE_MODE_40G_KR4 && !sfp->is_qsfp &&
+	    sfp->last_mod_abs && leds) {
+		cvmx_helper_leds_show_error(leds, false);
+		debug("%s: %s: last_mod_abs: %d, no error\n", __func__,
+		      sfp->name, sfp->last_mod_abs);
+		return true;
+	}
+
+	switch (mode) {
+	case CVMX_HELPER_INTERFACE_MODE_RGMII:
+	case CVMX_HELPER_INTERFACE_MODE_GMII:
+	case CVMX_HELPER_INTERFACE_MODE_SGMII:
+	case CVMX_HELPER_INTERFACE_MODE_QSGMII:
+	case CVMX_HELPER_INTERFACE_MODE_AGL:
+	case CVMX_HELPER_INTERFACE_MODE_SPI:
+		if ((mod_info->active_cable &&
+		     mod_info->rate != CVMX_SFP_RATE_1G) ||
+		    mod_info->rate < CVMX_SFP_RATE_1G)
+			error = true;
+		break;
+	case CVMX_HELPER_INTERFACE_MODE_RXAUI:
+	case CVMX_HELPER_INTERFACE_MODE_XAUI:
+	case CVMX_HELPER_INTERFACE_MODE_10G_KR:
+	case CVMX_HELPER_INTERFACE_MODE_XFI:
+		if ((mod_info->active_cable &&
+		     mod_info->rate != CVMX_SFP_RATE_10G) ||
+		    mod_info->rate < CVMX_SFP_RATE_10G)
+			error = true;
+		break;
+	case CVMX_HELPER_INTERFACE_MODE_XLAUI:
+	case CVMX_HELPER_INTERFACE_MODE_40G_KR4:
+		if (!sfp->is_qsfp) {
+			quad_mode = true;
+			error = cvmx_sfp_validate_quad(sfp, leds);
+		} else {
+			if ((mod_info->active_cable &&
+			     mod_info->rate != CVMX_SFP_RATE_40G) ||
+			    mod_info->rate < CVMX_SFP_RATE_25G)
+				error = true;
+		}
+		break;
+	default:
+		debug("%s: Unsupported interface mode %d on xiface 0x%x\n",
+		      __func__, mode, xiface);
+		return false;
+	}
+	debug("%s: %s: error: %d\n", __func__, sfp->name, error);
+	if (leds && !quad_mode)
+		cvmx_helper_leds_show_error(leds, error);
+
+	return !error;
+}
+
+/**
+ * Prints information about the SFP module
+ *
+ * @param[in]	sfp	sfp data structure
+ */
+void cvmx_sfp_print_info(const struct cvmx_fdt_sfp_info *sfp)
+{
+	const struct cvmx_sfp_mod_info *mi = &sfp->sfp_info;
+	const char *conn_type;
+	const char *mod_type;
+	const char *rate_str;
+	const char *cable_comp;
+
+	if (!sfp) {
+		printf("Invalid SFP (NULL)\n");
+		return;
+	}
+
+	/* Please refer to the SFF-8024 and SFF-8472 documents */
+	switch (mi->conn_type) {
+	case CVMX_SFP_CONN_GBIC:
+		conn_type = "GBIC";
+		break;
+	case CVMX_SFP_CONN_SFP:
+		conn_type = "SFP/SFP+/SFP28";
+		break;
+	case CVMX_SFP_CONN_QSFP:
+		conn_type = "QSFP";
+		break;
+	case CVMX_SFP_CONN_QSFPP:
+		conn_type = "QSFP+";
+		break;
+	case CVMX_SFP_CONN_QSFP28:
+		conn_type = "QSFP28";
+		break;
+	case CVMX_SFP_CONN_MICRO_QSFP:
+		conn_type = "Micro QSFP";
+		break;
+	case CVMX_SFP_CONN_QSFP_DD:
+		conn_type = "QSFP-DD";
+		break;
+	case CVMX_SFP_CONN_SFP_DD:
+		conn_type = "SFP-DD";
+		break;
+	default:
+		conn_type = "Unknown";
+		break;
+	}
+
+	switch (mi->mod_type) {
+	case CVMX_SFP_MOD_UNKNOWN:
+		mod_type = "Unknown";
+		break;
+	case CVMX_SFP_MOD_OPTICAL_LC:
+		mod_type = "Optical LC";
+		break;
+	case CVMX_SFP_MOD_MULTIPLE_OPTICAL:
+		mod_type = "Multiple Optical";
+		break;
+	case CVMX_SFP_MOD_OPTICAL_PIGTAIL:
+		mod_type = "Optical Pigtail";
+		break;
+	case CVMX_SFP_MOD_COPPER_PIGTAIL:
+		mod_type = "Copper Pigtail";
+		break;
+	case CVMX_SFP_MOD_RJ45:
+		mod_type = "Copper RJ45";
+		break;
+	case CVMX_SFP_MOD_NO_SEP_CONN:
+		mod_type = "No Separable Connector";
+		break;
+	case CVMX_SFP_MOD_MXC_2X16:
+		mod_type = "MXC 2X16";
+		break;
+	case CVMX_SFP_MOD_CS_OPTICAL:
+		mod_type = "CS Optical";
+		break;
+	case CVMX_SFP_MOD_MINI_CS_OPTICAL:
+		mod_type = "Mini CS Optical";
+		break;
+	case CVMX_SFP_MOD_OTHER:
+		mod_type = "Unknown/Other";
+		break;
+	default:
+		mod_type = "Undefined";
+		break;
+	}
+
+	switch (mi->cable_comp) {
+	case CVMX_SFP_CABLE_UNSPEC:
+		cable_comp = "";
+		break;
+	case CVMX_SFP_CABLE_100G_25GAUI_C2M_AOC_HIGH_BER:
+		if (mi->rate == CVMX_SFP_RATE_25G)
+			cable_comp = " 25GAUI-C2M AOC HIGH BER";
+		else
+			cable_comp = " 100G AOC HIGH BER";
+		break;
+	case CVMX_SFP_CABLE_100G_SR4_25G_SR:
+		if (mi->rate == CVMX_SFP_RATE_25G)
+			cable_comp = " 25GBASE-SR";
+		else
+			cable_comp = " 100GBASE-SR4";
+		break;
+	case CVMX_SFP_CABLE_100G_LR4_25G_LR:
+		if (mi->rate == CVMX_SFP_RATE_25G)
+			cable_comp = " 25GBASE-LR";
+		else
+			cable_comp = " 100GBASE-LR4";
+		break;
+	case CVMX_SFP_CABLE_100G_ER4_25G_ER:
+		if (mi->rate == CVMX_SFP_RATE_25G)
+			cable_comp = " 25GBASE-ER";
+		else
+			cable_comp = " 100GBASE-ER4";
+		break;
+	case CVMX_SFP_CABLE_100G_SR10:
+		cable_comp = " 100GBASE-SR10";
+		break;
+	case CVMX_SFP_CABLE_100G_CWDM4_MSA:
+		cable_comp = " 100G CWDM4";
+		break;
+	case CVMX_SFP_CABLE_100G_PSM4:
+		cable_comp = " 100G PSM4 Parallel SMF";
+		break;
+	case CVMX_SFP_CABLE_100G_25GAUI_C2M_ACC_HIGH_BER:
+		if (mi->rate == CVMX_SFP_RATE_25G)
+			cable_comp = " 25GAUI C2M ACC";
+		else
+			cable_comp = " 100G ACC";
+		break;
+	case CVMX_SFP_CABLE_100G_CWDM4:
+		cable_comp = " 100G CWDM4 MSA";
+		break;
+	case CVMX_SFP_CABLE_100G_CR4_25G_CR_CA_L:
+		if (mi->rate == CVMX_SFP_RATE_25G)
+			cable_comp = " 25GBASE-CR CA-L";
+		else
+			cable_comp = " 100GBASE-CR4";
+		break;
+	case CVMX_SFP_CABLE_25G_CR_CA_S:
+		cable_comp = " 25GBase-CR CA-S";
+		break;
+	case CVMX_SFP_CABLE_25G_CR_CA_N:
+		cable_comp = " 25GBase-CR CA-N";
+		break;
+	case CVMX_SFP_CABLE_40G_ER4:
+		cable_comp = " 40GBASE-ER4";
+		break;
+	case CVMX_SFP_CABLE_4X10G_SR:
+		cable_comp = " 4 X 10GBASE-T SR";
+		break;
+	case CVMX_SFP_CABLE_40G_PSM4:
+		cable_comp = " 40G PSM4 Parallel SMF";
+		break;
+	case CVMX_SFP_CABLE_G959_1_P1I1_2D1:
+		cable_comp = " G959.1 profile P1I1-2D1";
+		break;
+	case CVMX_SFP_CABLE_G959_1_P1S1_2D2:
+		cable_comp = " G959.1 profile P1S1-2D2";
+		break;
+	case CVMX_SFP_CABLE_G959_1_P1L1_2D2:
+		cable_comp = " G959.1 profile P1L1-2D2";
+		break;
+	case CVMX_SFP_CABLE_10GBASE_T:
+		cable_comp = " 10GBase-T";
+		break;
+	case CVMX_SFP_CABLE_100G_CLR4:
+		cable_comp = " 10G CLR4";
+		break;
+	case CVMX_SFP_CABLE_100G_25GAUI_C2M_AOC_LOW_BER:
+		if (mi->rate == CVMX_SFP_RATE_25G)
+			cable_comp = " 25GAUI C2M AOC LOW BER";
+		else
+			cable_comp = " 100G AOC LOW BER";
+		break;
+	case CVMX_SFP_CABLE_100G_25GAUI_C2M_ACC_LOW_BER:
+		if (mi->rate == CVMX_SFP_RATE_25G)
+			cable_comp = " 25GAUI C2M ACC LOW BER";
+		else
+			cable_comp = " 100G ACC LOW BER";
+		break;
+	case CVMX_SFP_CABLE_100G_2_LAMBDA_DWDM:
+		cable_comp = " 100GE-DWDM2";
+		break;
+	case CVMX_SFP_CABLE_100G_1550NM_WDM:
+		cable_comp = " 100GE 1550nm WDM";
+		break;
+	case CVMX_SFP_CABLE_10GBASE_T_SR:
+		cable_comp = " 10GBase-T short reach (30 meters)";
+		break;
+	case CVMX_SFP_CABLE_5GBASE_T:
+		cable_comp = " 5GBase-T";
+		break;
+	case CVMX_SFP_CABLE_2_5GBASE_T:
+		cable_comp = " 2.5GBase-T";
+		break;
+	case CVMX_SFP_CABLE_40G_SWDM4:
+		cable_comp = " 40G SWDM4";
+		break;
+	case CVMX_SFP_CABLE_100G_SWDM4:
+		cable_comp = " 100G SWDM4";
+		break;
+	case CVMX_SFP_CABLE_100G_PAM4_BIDI:
+		cable_comp = " 100G SWDM4";
+		break;
+	case CVMX_SFP_CABLE_100G_4WDM_10_FEC_HOST:
+		cable_comp = " 4WDM-10 MSA (10KM CWDM4) FEC in host";
+		break;
+	case CVMX_SFP_CABLE_100G_4WDM_20_FEC_HOST:
+		cable_comp = " 4WDM-20 MSA (20KM 100GBASE-LR4) FEC in host";
+		break;
+	case CVMX_SFP_CABLE_100G_4WDM_40_FEC_HOST:
+		cable_comp = " 4WDM-40 MSA (40KM APD receiver) FEC in host";
+		break;
+	case CVMX_SFP_CABLE_100GBASE_DR_CAUI4_NO_FEC:
+		cable_comp = " 100GBASE-DR with CAUI-4, no FEC";
+		break;
+	case CVMX_SFP_CABLE_100G_FR_CAUI4_NO_FEC:
+		cable_comp = " 100G-FR with CAUI-4, no FEC";
+		break;
+	case CVMX_SFP_CABLE_100G_LR_CAUI4_NO_FEC:
+		cable_comp = " 100G-LR with CAUI-4, no FEC";
+		break;
+	case CVMX_SFP_CABLE_ACTIVE_COPPER_50_100_200GAUI_LOW_BER:
+		cable_comp = " Active Copper Cable with 50GAUI, 100GAUI-2 or 200GAUI-4 C2M, BER 10^(-6) or lower";
+		break;
+	case CVMX_SFP_CABLE_ACTIVE_OPTICAL_50_100_200GAUI_LOW_BER:
+		cable_comp = " Active Optical Cable with 50GAUI, 100GAUI-2 or 200GAUI-4 C2M, BER 10^(-6) or lower";
+		break;
+	case CVMX_SFP_CABLE_ACTIVE_COPPER_50_100_200GAUI_HI_BER:
+		cable_comp = " Active Copper Cable with 50GAUI, 100GAUI-2 or 200GAUI-4 AUI, BER 2.6 * 10^(-4) or lower";
+		break;
+	case CVMX_SFP_CABLE_ACTIVE_OPTICAL_50_100_200GAUI_HI_BER:
+		cable_comp = " Active Optical Cable with 50GAUI, 100GAUI-2 or 200GAUI-4 AUI, BER 2.6 10^(-4) or lower";
+		break;
+	case CVMX_SFP_CABLE_50_100_200G_CR:
+		cable_comp = " 50GBASE-CR, 100G-CR2 or 200G-CR4";
+		break;
+	case CVMX_SFP_CABLE_50_100_200G_SR:
+		cable_comp = " 50GBASE-SR, 100G-SR2 or 200G-SR4";
+		break;
+	case CVMX_SFP_CABLE_50GBASE_FR_200GBASE_DR4:
+		cable_comp = " 50GBASE-FR or 200GBASE-DR4";
+		break;
+	case CVMX_SFP_CABLE_200GBASE_FR4:
+		cable_comp = " 200GBASE-FR4";
+		break;
+	case CVMX_SFP_CABLE_200G_1550NM_PSM4:
+		cable_comp = " 200G 1550nm PSM4";
+		break;
+	case CVMX_SFP_CABLE_50GBASE_LR:
+		cable_comp = "50GBASE-LR";
+		break;
+	case CVMX_SFP_CABLE_200GBASE_LR4:
+		cable_comp = " 200GBASE-LR4";
+		break;
+	case CVMX_SFP_CABLE_64GFC_EA:
+		cable_comp = " 64GFC EA";
+		break;
+	case CVMX_SFP_CABLE_64GFC_SW:
+		cable_comp = " 64GFC SW";
+		break;
+	case CVMX_SFP_CABLE_64GFC_LW:
+		cable_comp = " 64GFC LW";
+		break;
+	case CVMX_SFP_CABLE_128GFC_EA:
+		cable_comp = " 64GFC EA";
+		break;
+	case CVMX_SFP_CABLE_128GFC_SW:
+		cable_comp = " 64GFC SW";
+		break;
+	case CVMX_SFP_CABLE_128GFC_LW:
+		cable_comp = " 64GFC LW";
+		break;
+	default:
+		cable_comp = " Unknown or Unsupported";
+		break;
+	}
+
+	switch (mi->rate) {
+	case CVMX_SFP_RATE_UNKNOWN:
+	default:
+		rate_str = "Unknown";
+		break;
+	case CVMX_SFP_RATE_1G:
+		rate_str = "1G";
+		break;
+	case CVMX_SFP_RATE_10G:
+		switch (mi->cable_comp) {
+		case CVMX_SFP_CABLE_5GBASE_T:
+			rate_str = "5G";
+			break;
+		case CVMX_SFP_CABLE_2_5GBASE_T:
+			rate_str = "2.5G";
+			break;
+		default:
+			rate_str = "10G";
+			break;
+		}
+		break;
+	case CVMX_SFP_RATE_25G:
+		rate_str = "25G";
+		break;
+	case CVMX_SFP_RATE_40G:
+		rate_str = "40G";
+		break;
+	case CVMX_SFP_RATE_100G:
+		rate_str = "100G";
+		break;
+	}
+
+	cvmx_printf("%s %s%s module detected\n", mod_type, conn_type,
+		    cable_comp);
+	debug("Vendor: %s\n", mi->vendor_name);
+	debug("Vendor OUI: %02X:%02X:%02X\n", mi->vendor_oui[0],
+	      mi->vendor_oui[1], mi->vendor_oui[2]);
+	debug("Vendor part number: %s Revision: %s\n", mi->vendor_pn,
+	      mi->vendor_rev);
+	debug("Manufacturing date code: %s\n", mi->date_code);
+	debug("Rate: %s\n", rate_str);
+	if (mi->copper_cable) {
+		debug("Copper cable type: %s\n",
+		      mi->active_cable ? "Active" : "Passive");
+		debug("Cable length: %u meters\n", mi->max_copper_cable_len);
+		if (mi->rate == CVMX_SFP_RATE_25G)
+			debug("Forward error correction is%s %s\n",
+			      mi->fec_required ? "" : " not",
+			      (mi->max_copper_cable_len >= 5 ||
+			       !mi->fec_required) ?
+				      "required" :
+					    "suggested");
+	} else {
+		bool more = false;
+
+		debug("Supported optical types: ");
+		if (mi->eth_comp & CVMX_SFP_CABLE_10GBASE_ER) {
+			more = !!(mi->eth_comp & ~CVMX_SFP_CABLE_10GBASE_ER);
+			debug("10GBase-ER%s", more ? ", " : "");
+		}
+		if (mi->eth_comp & CVMX_SFP_CABLE_10GBASE_LRM) {
+			more = !!(mi->eth_comp & ~(CVMX_SFP_CABLE_10GBASE_ER |
+						   CVMX_SFP_CABLE_10GBASE_LRM));
+			debug("10GBase-LRM%s", more ? ", " : "");
+		}
+		if (mi->eth_comp & CVMX_SFP_CABLE_10GBASE_LR) {
+			more = !!(mi->eth_comp & ~(CVMX_SFP_CABLE_10GBASE_ER |
+						   CVMX_SFP_CABLE_10GBASE_LRM |
+						   CVMX_SFP_CABLE_10GBASE_LR));
+			debug("10GBase-LR%s", more ? ", " : "");
+		}
+		if (mi->eth_comp & CVMX_SFP_CABLE_10GBASE_SR)
+			debug("10GBase-SR");
+		debug("\nMaximum single mode cable length: %d meters\n",
+		      mi->max_single_mode_cable_length);
+		debug("Maximum 62.5um OM1 cable length:  %d meters\n",
+		      mi->max_62_5um_om1_cable_length);
+		debug("Maximum 50um OM2 cable length:    %d meters\n",
+		      mi->max_50um_om2_cable_length);
+		debug("Maximum 50um OM3 cable length:    %d meters\n",
+		      mi->max_50um_om3_cable_length);
+		debug("Maximum 50um OM4 cable length:    %d meters\n",
+		      mi->max_50um_om4_cable_length);
+		debug("Laser is%s cooled\n", mi->cooled_laser ? "" : " not");
+	}
+	debug("Limiting is %sabled\n", mi->limiting ? "en" : "dis");
+	debug("Power level: %d\n", mi->power_level);
+	debug("RX LoS is%s implemented and is%s inverted\n",
+	      mi->los_implemented ? "" : " not",
+	      mi->los_inverted ? "" : " not");
+	debug("TX fault is%s implemented\n",
+	      mi->tx_fault_implemented ? "" : " not");
+	debug("TX disable is%s implemented\n",
+	      mi->tx_disable_implemented ? "" : " not");
+	debug("Rate select is%s implemented\n",
+	      mi->rate_select_implemented ? "" : " not");
+	debug("RX output is %s\n",
+	      mi->linear_rx_output ? "linear" : "limiting");
+	debug("Diagnostic monitoring is%s implemented\n",
+	      mi->diag_monitoring ? "" : " not");
+	if (mi->diag_monitoring) {
+		const char *diag_rev;
+
+		switch (mi->diag_rev) {
+		case CVMX_SFP_SFF_8472_NO_DIAG:
+			diag_rev = "none";
+			break;
+		case CVMX_SFP_SFF_8472_REV_9_3:
+			diag_rev = "9.3";
+			break;
+		case CVMX_SFP_SFF_8472_REV_9_5:
+			diag_rev = "9.5";
+			break;
+		case CVMX_SFP_SFF_8472_REV_10_2:
+			diag_rev = "10.2";
+			break;
+		case CVMX_SFP_SFF_8472_REV_10_4:
+			diag_rev = "10.4";
+			break;
+		case CVMX_SFP_SFF_8472_REV_11_0:
+			diag_rev = "11.0";
+			break;
+		case CVMX_SFP_SFF_8472_REV_11_3:
+			diag_rev = "11.3";
+			break;
+		case CVMX_SFP_SFF_8472_REV_11_4:
+			diag_rev = "11.4";
+			break;
+		case CVMX_SFP_SFF_8472_REV_12_0:
+			diag_rev = "12.0";
+			break;
+		case CVMX_SFP_SFF_8472_REV_UNALLOCATED:
+			diag_rev = "Unallocated";
+			break;
+		default:
+			diag_rev = "Unknown";
+			break;
+		}
+		debug("Diagnostics revision: %s\n", diag_rev);
+		debug("Diagnostic address change is%s required\n",
+		      mi->diag_addr_change_required ? "" : " not");
+		debug("Diagnostics are%s internally calibrated\n",
+		      mi->diag_internally_calibrated ? "" : " not");
+		debug("Diagnostics are%s externally calibrated\n",
+		      mi->diag_externally_calibrated ? "" : " not");
+		debug("Receive power measurement type: %s\n",
+		      mi->diag_rx_power_averaged ? "Averaged" : "OMA");
+	}
+}
Signature · 1 line
2.35.1
#22 lore raw

PATCH 24/52 mips: octeon: Add cvmx-agl.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-agl.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-agl.c | 216 +++++++++++++++++++++++++++++++
 1 file changed, 216 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-agl.c
arch/mips/mach-octeon/cvmx-agl.c new file +216 -0
new file mode 100644
index 000000000000..9eea857e47c7
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * Functions for RGMII (MGMT) initialization, configuration,
+ * and monitoring.
+ */
+
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#include <mach/cvmx-agl.h>
+#include <mach/cvmx-agl-defs.h>
+
+/*
+ * @param port to enable
+ *
+ * @return Zero on success, negative on failure
+ */
+int cvmx_agl_enable(int port)
+{
+	cvmx_agl_gmx_rxx_frm_ctl_t rxx_frm_ctl;
+
+	rxx_frm_ctl.u64 = 0;
+	rxx_frm_ctl.s.pre_align = 1;
+	/* When set, disables the length check for non-min sized pkts with
+	 * padding in the client data
+	 */
+	rxx_frm_ctl.s.pad_len = 1;
+	/* When set, disables the length check for VLAN pkts */
+	rxx_frm_ctl.s.vlan_len = 1;
+	/* When set, PREAMBLE checking is  less strict */
+	rxx_frm_ctl.s.pre_free = 1;
+	/* Control Pause Frames can match station SMAC */
+	rxx_frm_ctl.s.ctl_smac = 0;
+	/* Control Pause Frames can match globally assign Multicast address */
+	rxx_frm_ctl.s.ctl_mcst = 1;
+	rxx_frm_ctl.s.ctl_bck = 1;  /* Forward pause information to TX block */
+	rxx_frm_ctl.s.ctl_drp = 1;  /* Drop Control Pause Frames */
+	rxx_frm_ctl.s.pre_strp = 1; /* Strip off the preamble */
+	/* This port is configured to send PREAMBLE+SFD to begin every frame.
+	 * GMX checks that the PREAMBLE is sent correctly
+	 */
+	rxx_frm_ctl.s.pre_chk = 1;
+	csr_wr(CVMX_AGL_GMX_RXX_FRM_CTL(port), rxx_frm_ctl.u64);
+
+	return 0;
+}
+
+cvmx_helper_link_info_t cvmx_agl_link_get(int port)
+{
+	cvmx_helper_link_info_t result;
+	int interface, port_index;
+
+	/* Fake IPD port is used on some older models. */
+	if (port < 0)
+		return __cvmx_helper_board_link_get(port);
+
+	/* Simulator does not have PHY, use some defaults. */
+	interface = cvmx_helper_get_interface_num(port);
+	port_index = cvmx_helper_get_interface_index_num(port);
+	if (cvmx_helper_get_port_force_link_up(interface, port_index)) {
+		result.u64 = 0;
+		result.s.full_duplex = 1;
+		result.s.link_up = 1;
+		result.s.speed = 1000;
+		return result;
+	}
+
+	return __cvmx_helper_board_link_get(port);
+}
+
+/*
+ * Set MII/RGMII link based on mode.
+ *
+ * @param port   interface port to set the link.
+ * @param link_info  Link status
+ *
+ * @return       0 on success and 1 on failure
+ */
+int cvmx_agl_link_set(int port, cvmx_helper_link_info_t link_info)
+{
+	cvmx_agl_gmx_prtx_cfg_t agl_gmx_prtx;
+
+	/* Disable GMX before we make any changes. */
+	agl_gmx_prtx.u64 = csr_rd(CVMX_AGL_GMX_PRTX_CFG(port));
+	agl_gmx_prtx.s.en = 0;
+	agl_gmx_prtx.s.tx_en = 0;
+	agl_gmx_prtx.s.rx_en = 0;
+	csr_wr(CVMX_AGL_GMX_PRTX_CFG(port), agl_gmx_prtx.u64);
+
+	if (OCTEON_IS_MODEL(OCTEON_CN6XXX) || OCTEON_IS_OCTEON3()) {
+		u64 one_second = 0x1000000; /* todo: this needs checking */
+
+		/* Wait for GMX to be idle */
+		if (CVMX_WAIT_FOR_FIELD64(CVMX_AGL_GMX_PRTX_CFG(port),
+					  cvmx_agl_gmx_prtx_cfg_t, rx_idle, ==,
+					  1, one_second) ||
+		    CVMX_WAIT_FOR_FIELD64(CVMX_AGL_GMX_PRTX_CFG(port),
+					  cvmx_agl_gmx_prtx_cfg_t, tx_idle, ==,
+					  1, one_second)) {
+			debug("AGL%d: Timeout waiting for GMX to be idle\n",
+			      port);
+			return -1;
+		}
+	}
+
+	agl_gmx_prtx.u64 = csr_rd(CVMX_AGL_GMX_PRTX_CFG(port));
+
+	/* Set duplex mode */
+	if (!link_info.s.link_up)
+		agl_gmx_prtx.s.duplex = 1; /* Force full duplex on down links */
+	else
+		agl_gmx_prtx.s.duplex = link_info.s.full_duplex;
+
+	switch (link_info.s.speed) {
+	case 10:
+		agl_gmx_prtx.s.speed = 0;
+		agl_gmx_prtx.s.slottime = 0;
+		if (OCTEON_IS_MODEL(OCTEON_CN6XXX) || OCTEON_IS_OCTEON3()) {
+			agl_gmx_prtx.s.speed_msb = 1;
+			agl_gmx_prtx.s.burst = 1;
+		}
+		break;
+
+	case 100:
+		agl_gmx_prtx.s.speed = 0;
+		agl_gmx_prtx.s.slottime = 0;
+		if (OCTEON_IS_MODEL(OCTEON_CN6XXX) || OCTEON_IS_OCTEON3()) {
+			agl_gmx_prtx.s.speed_msb = 0;
+			agl_gmx_prtx.s.burst = 1;
+		}
+		break;
+
+	case 1000:
+		/* 1000 MBits is only supported on 6XXX chips */
+		if (OCTEON_IS_MODEL(OCTEON_CN6XXX) || OCTEON_IS_OCTEON3()) {
+			agl_gmx_prtx.s.speed_msb = 0;
+			agl_gmx_prtx.s.speed = 1;
+			agl_gmx_prtx.s.slottime =
+				1; /* Only matters for half-duplex */
+			agl_gmx_prtx.s.burst = agl_gmx_prtx.s.duplex;
+		}
+		break;
+
+		/* No link */
+	case 0:
+	default:
+		break;
+	}
+
+	/* Write the new GMX setting with the port still disabled. */
+	csr_wr(CVMX_AGL_GMX_PRTX_CFG(port), agl_gmx_prtx.u64);
+
+	/* Read GMX CFG again to make sure the config is completed. */
+	agl_gmx_prtx.u64 = csr_rd(CVMX_AGL_GMX_PRTX_CFG(port));
+
+	if (OCTEON_IS_MODEL(OCTEON_CN6XXX) || OCTEON_IS_OCTEON3()) {
+		cvmx_agl_gmx_txx_clk_t agl_clk;
+		cvmx_agl_prtx_ctl_t prt_ctl;
+
+		prt_ctl.u64 = csr_rd(CVMX_AGL_PRTX_CTL(port));
+		agl_clk.u64 = csr_rd(CVMX_AGL_GMX_TXX_CLK(port));
+		/* MII (both speeds) and RGMII 1000 setting */
+		agl_clk.s.clk_cnt = 1;
+		/* Check other speeds for RGMII mode */
+		if (prt_ctl.s.mode == 0 || OCTEON_IS_OCTEON3()) {
+			if (link_info.s.speed == 10)
+				agl_clk.s.clk_cnt = 50;
+			else if (link_info.s.speed == 100)
+				agl_clk.s.clk_cnt = 5;
+		}
+		csr_wr(CVMX_AGL_GMX_TXX_CLK(port), agl_clk.u64);
+	}
+
+	/* Enable transmit and receive ports */
+	agl_gmx_prtx.s.tx_en = 1;
+	agl_gmx_prtx.s.rx_en = 1;
+	csr_wr(CVMX_AGL_GMX_PRTX_CFG(port), agl_gmx_prtx.u64);
+
+	/* Enable the link. */
+	agl_gmx_prtx.s.en = 1;
+	csr_wr(CVMX_AGL_GMX_PRTX_CFG(port), agl_gmx_prtx.u64);
+
+	if (OCTEON_IS_OCTEON3()) {
+		union cvmx_agl_prtx_ctl agl_prtx_ctl;
+		/* Enable the interface, set clkrst */
+		agl_prtx_ctl.u64 = csr_rd(CVMX_AGL_PRTX_CTL(port));
+		agl_prtx_ctl.s.clkrst = 1;
+		csr_wr(CVMX_AGL_PRTX_CTL(port), agl_prtx_ctl.u64);
+		csr_rd(CVMX_AGL_PRTX_CTL(port));
+		agl_prtx_ctl.s.enable = 1;
+		csr_wr(CVMX_AGL_PRTX_CTL(port), agl_prtx_ctl.u64);
+		/* Read the value back to force the previous write */
+		csr_rd(CVMX_AGL_PRTX_CTL(port));
+	}
+
+	return 0;
+}
Signature · 1 line
2.35.1
#23 lore raw

PATCH 25/52 mips: octeon: Add cvmx-cmd-queue.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-cmd-queue.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-cmd-queue.c | 449 +++++++++++++++++++++++++
 1 file changed, 449 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-cmd-queue.c
arch/mips/mach-octeon/cvmx-cmd-queue.c new file +449 -0
new file mode 100644
index 000000000000..041f2390ca18
@@ -0,0 +1,449 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * Support functions for managing command queues used for
+ * various hardware blocks.
+ */
+
+#include <errno.h>
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-fpa.h>
+#include <mach/cvmx-cmd-queue.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-pko3.h>
+#include <mach/cvmx-pko3-queue.h>
+#include <mach/cvmx-pko3-resources.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#include <mach/cvmx-helper-bgx.h>
+#include <mach/cvmx-helper-cfg.h>
+#include <mach/cvmx-helper-util.h>
+#include <mach/cvmx-helper-pki.h>
+
+#include <mach/cvmx-helper-util.h>
+#include <mach/cvmx-dpi-defs.h>
+#include <mach/cvmx-npei-defs.h>
+#include <mach/cvmx-pexp-defs.h>
+
+/**
+ * This application uses this pointer to access the global queue
+ * state. It points to a bootmem named block.
+ */
+__cvmx_cmd_queue_all_state_t *__cvmx_cmd_queue_state_ptrs[CVMX_MAX_NODES];
+
+/**
+ * @INTERNAL
+ * Initialize the Global queue state pointer.
+ *
+ * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
+ */
+cvmx_cmd_queue_result_t __cvmx_cmd_queue_init_state_ptr(unsigned int node)
+{
+	const char *alloc_name = "cvmx_cmd_queues\0\0";
+	char s[4] = "_0";
+	const struct cvmx_bootmem_named_block_desc *block_desc = NULL;
+	unsigned int size;
+	u64 paddr_min = 0, paddr_max = 0;
+	void *ptr;
+
+	if (cvmx_likely(__cvmx_cmd_queue_state_ptrs[node]))
+		return CVMX_CMD_QUEUE_SUCCESS;
+
+	/* Add node# to block name */
+	if (node > 0) {
+		s[1] += node;
+		strcat((char *)alloc_name, s);
+	}
+
+	/* Find the named block in case it has been created already */
+	block_desc = cvmx_bootmem_find_named_block(alloc_name);
+	if (block_desc) {
+		__cvmx_cmd_queue_state_ptrs[node] =
+			(__cvmx_cmd_queue_all_state_t *)cvmx_phys_to_ptr(
+				block_desc->base_addr);
+		return CVMX_CMD_QUEUE_SUCCESS;
+	}
+
+	size = sizeof(*__cvmx_cmd_queue_state_ptrs[node]);
+
+	/* Rest f the code is to allocate a new named block */
+
+	/* Atomically allocate named block once, and zero it by default */
+	ptr = cvmx_bootmem_alloc_named_range_once(size, paddr_min, paddr_max,
+						  128, alloc_name, NULL);
+
+	if (ptr) {
+		__cvmx_cmd_queue_state_ptrs[node] =
+			(__cvmx_cmd_queue_all_state_t *)ptr;
+	} else {
+		debug("ERROR: %s: Unable to get named block %s.\n", __func__,
+		      alloc_name);
+		return CVMX_CMD_QUEUE_NO_MEMORY;
+	}
+	return CVMX_CMD_QUEUE_SUCCESS;
+}
+
+/**
+ * Initialize a command queue for use. The initial FPA buffer is
+ * allocated and the hardware unit is configured to point to the
+ * new command queue.
+ *
+ * @param queue_id  Hardware command queue to initialize.
+ * @param max_depth Maximum outstanding commands that can be queued.
+ * @param fpa_pool  FPA pool the command queues should come from.
+ * @param pool_size Size of each buffer in the FPA pool (bytes)
+ *
+ * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
+ */
+cvmx_cmd_queue_result_t cvmx_cmd_queue_initialize(cvmx_cmd_queue_id_t queue_id,
+						  int max_depth, int fpa_pool,
+						  int pool_size)
+{
+	__cvmx_cmd_queue_state_t *qstate;
+	cvmx_cmd_queue_result_t result;
+	unsigned int node;
+	unsigned int index;
+	int fpa_pool_min, fpa_pool_max;
+	union cvmx_fpa_ctl_status status;
+	void *buffer;
+
+	node = __cvmx_cmd_queue_get_node(queue_id);
+
+	index = __cvmx_cmd_queue_get_index(queue_id);
+	if (index >= NUM_ELEMENTS(__cvmx_cmd_queue_state_ptrs[node]->state)) {
+		printf("ERROR: %s: queue %#x out of range\n", __func__,
+		       queue_id);
+		return CVMX_CMD_QUEUE_INVALID_PARAM;
+	}
+
+	result = __cvmx_cmd_queue_init_state_ptr(node);
+	if (result != CVMX_CMD_QUEUE_SUCCESS)
+		return result;
+
+	qstate = __cvmx_cmd_queue_get_state(queue_id);
+	if (!qstate)
+		return CVMX_CMD_QUEUE_INVALID_PARAM;
+
+	/*
+	 * We artificially limit max_depth to 1<<20 words. It is an
+	 * arbitrary limit.
+	 */
+	if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH) {
+		if (max_depth < 0 || max_depth > 1 << 20)
+			return CVMX_CMD_QUEUE_INVALID_PARAM;
+	} else if (max_depth != 0) {
+		return CVMX_CMD_QUEUE_INVALID_PARAM;
+	}
+
+	/* CVMX_FPA_NUM_POOLS maps to cvmx_fpa3_num_auras for FPA3 */
+	fpa_pool_min = node << 10;
+	fpa_pool_max = fpa_pool_min + CVMX_FPA_NUM_POOLS;
+
+	if (fpa_pool < fpa_pool_min || fpa_pool >= fpa_pool_max)
+		return CVMX_CMD_QUEUE_INVALID_PARAM;
+
+	if (pool_size < 128 || pool_size > (1 << 17))
+		return CVMX_CMD_QUEUE_INVALID_PARAM;
+
+	if (pool_size & 3)
+		debug("WARNING: %s: pool_size %d not multiple of 8\n", __func__,
+		      pool_size);
+
+	/* See if someone else has already initialized the queue */
+	if (qstate->base_paddr) {
+		int depth;
+		static const char emsg[] = /* Common error message part */
+			"Queue already initialized with different ";
+
+		depth = (max_depth + qstate->pool_size_m1 - 1) /
+			qstate->pool_size_m1;
+		if (depth != qstate->max_depth) {
+			depth = qstate->max_depth * qstate->pool_size_m1;
+			debug("ERROR: %s: %s max_depth (%d).\n", __func__, emsg,
+			      depth);
+			return CVMX_CMD_QUEUE_INVALID_PARAM;
+		}
+		if (fpa_pool != qstate->fpa_pool) {
+			debug("ERROR: %s: %s FPA pool (%d).\n", __func__, emsg,
+			      (int)qstate->fpa_pool);
+			return CVMX_CMD_QUEUE_INVALID_PARAM;
+		}
+		if ((pool_size >> 3) - 1 != qstate->pool_size_m1) {
+			debug("ERROR: %s: %s FPA pool size (%u).\n", __func__,
+			      emsg, (qstate->pool_size_m1 + 1) << 3);
+			return CVMX_CMD_QUEUE_INVALID_PARAM;
+		}
+		return CVMX_CMD_QUEUE_ALREADY_SETUP;
+	}
+
+	if (!(octeon_has_feature(OCTEON_FEATURE_FPA3))) {
+		status.u64 = csr_rd(CVMX_FPA_CTL_STATUS);
+		if (!status.s.enb) {
+			debug("ERROR: %s: FPA is not enabled.\n",
+			      __func__);
+			return CVMX_CMD_QUEUE_NO_MEMORY;
+		}
+	}
+	buffer = cvmx_fpa_alloc(fpa_pool);
+	if (!buffer) {
+		debug("ERROR: %s: allocating first buffer.\n", __func__);
+		return CVMX_CMD_QUEUE_NO_MEMORY;
+	}
+
+	index = (pool_size >> 3) - 1;
+	qstate->pool_size_m1 = index;
+	qstate->max_depth = (max_depth + index - 1) / index;
+	qstate->index = 0;
+	qstate->fpa_pool = fpa_pool;
+	qstate->base_paddr = cvmx_ptr_to_phys(buffer);
+
+	/* Initialize lock */
+	__cvmx_cmd_queue_lock_init(queue_id);
+	return CVMX_CMD_QUEUE_SUCCESS;
+}
+
+/**
+ * Shutdown a queue a free it's command buffers to the FPA. The
+ * hardware connected to the queue must be stopped before this
+ * function is called.
+ *
+ * @param queue_id Queue to shutdown
+ *
+ * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
+ */
+cvmx_cmd_queue_result_t cvmx_cmd_queue_shutdown(cvmx_cmd_queue_id_t queue_id)
+{
+	__cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
+
+	/* FIXME: This will not complain if the queue was never initialized */
+	if (!qptr) {
+		debug("ERROR: %s: Unable to get queue information.\n",
+		      __func__);
+		return CVMX_CMD_QUEUE_INVALID_PARAM;
+	}
+
+	if (cvmx_cmd_queue_length(queue_id) > 0) {
+		debug("ERROR: %s: Queue still has data in it.\n", __func__);
+		return CVMX_CMD_QUEUE_FULL;
+	}
+
+	__cvmx_cmd_queue_lock(queue_id);
+	if (qptr->base_paddr) {
+		cvmx_fpa_free(cvmx_phys_to_ptr((u64)qptr->base_paddr),
+			      qptr->fpa_pool, 0);
+		qptr->base_paddr = 0;
+	}
+	__cvmx_cmd_queue_unlock(queue_id);
+
+	return CVMX_CMD_QUEUE_SUCCESS;
+}
+
+/**
+ * Return the number of command words pending in the queue. This
+ * function may be relatively slow for some hardware units.
+ *
+ * @param queue_id Hardware command queue to query
+ *
+ * @return Number of outstanding commands
+ */
+int cvmx_cmd_queue_length(cvmx_cmd_queue_id_t queue_id)
+{
+	union cvmx_dpi_dmax_counts dmax_counts;
+	union cvmx_pko_mem_debug8 debug8;
+
+	if (!__cvmx_cmd_queue_get_state(queue_id))
+		return CVMX_CMD_QUEUE_INVALID_PARAM;
+
+	/*
+	 * The cast is here so gcc with check that all values in the
+	 * cvmx_cmd_queue_id_t enumeration are here.
+	 */
+	switch ((cvmx_cmd_queue_id_t)(queue_id & 0xff0000)) {
+	case CVMX_CMD_QUEUE_PKO_BASE:
+		/*
+		 * Really need atomic lock on
+		 * CVMX_PKO_REG_READ_IDX. Right now we are normally
+		 * called with the queue lock, so that is a SLIGHT
+		 * amount of protection.
+		 */
+		csr_wr(CVMX_PKO_REG_READ_IDX, queue_id & 0xffff);
+		debug8.u64 = csr_rd(CVMX_PKO_MEM_DEBUG8);
+		if (octeon_has_feature(OCTEON_FEATURE_PKND))
+			return debug8.cn68xx.doorbell;
+		else
+			return debug8.cn58xx.doorbell;
+	case CVMX_CMD_QUEUE_ZIP:
+	case CVMX_CMD_QUEUE_DFA:
+	case CVMX_CMD_QUEUE_HNA:
+	case CVMX_CMD_QUEUE_RAID:
+		/* Still need to implement other lengths */
+		return 0;
+	case CVMX_CMD_QUEUE_DMA_BASE:
+		if (octeon_has_feature(OCTEON_FEATURE_NPEI)) {
+			dmax_counts.u64 = csr_rd(
+				CVMX_PEXP_NPEI_DMAX_COUNTS(queue_id & 0x7));
+			return dmax_counts.s.dbell;
+		}
+
+		dmax_counts.u64 = csr_rd(CVMX_DPI_DMAX_COUNTS(queue_id & 0x7));
+		return dmax_counts.s.dbell;
+	case CVMX_CMD_QUEUE_BCH:
+		/* Not available */
+		return 0;
+	case CVMX_CMD_QUEUE_END:
+		return CVMX_CMD_QUEUE_INVALID_PARAM;
+	}
+	return CVMX_CMD_QUEUE_INVALID_PARAM;
+}
+
+/**
+ * Return the command buffer to be written to. The purpose of this
+ * function is to allow CVMX routine access to the low level buffer
+ * for initial hardware setup. User applications should not call this
+ * function directly.
+ *
+ * @param queue_id Command queue to query
+ *
+ * @return Command buffer or NULL on failure
+ */
+void *cvmx_cmd_queue_buffer(cvmx_cmd_queue_id_t queue_id)
+{
+	__cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
+
+	if (qptr && qptr->base_paddr)
+		return cvmx_phys_to_ptr((u64)qptr->base_paddr);
+	else
+		return NULL;
+}
+
+static u64 *__cvmx_cmd_queue_add_blk(__cvmx_cmd_queue_state_t *qptr)
+{
+	u64 *cmd_ptr;
+	u64 *new_buffer;
+	u64 new_paddr;
+
+	/* Get base vaddr of current (full) block */
+	cmd_ptr = (u64 *)cvmx_phys_to_ptr((u64)qptr->base_paddr);
+
+	/* Allocate a new block from the per-queue pool */
+	new_buffer = (u64 *)cvmx_fpa_alloc(qptr->fpa_pool);
+
+	/* Check for allocation failure */
+	if (cvmx_unlikely(!new_buffer))
+		return NULL;
+
+	/* Zero out the new block link pointer,
+	 * in case this block will be filled to the rim
+	 */
+	new_buffer[qptr->pool_size_m1] = ~0ull;
+
+	/* Get physical address of the new buffer */
+	new_paddr = cvmx_ptr_to_phys(new_buffer);
+
+	/* Store the physical link address at the end of current full block */
+	cmd_ptr[qptr->pool_size_m1] = new_paddr;
+
+	/* Store the physical address in the queue state structure */
+	qptr->base_paddr = new_paddr;
+	qptr->index = 0;
+
+	/* Return the virtual base of the new block */
+	return new_buffer;
+}
+
+/**
+ * @INTERNAL
+ * Add command words into a queue, handles all the corener cases
+ * where only some of the words might fit into the current block,
+ * and a new block may need to be allocated.
+ * Locking and argument checks are done in the front-end in-line
+ * functions that call this one for the rare corner cases.
+ */
+cvmx_cmd_queue_result_t
+__cvmx_cmd_queue_write_raw(cvmx_cmd_queue_id_t queue_id,
+			   __cvmx_cmd_queue_state_t *qptr, int cmd_count,
+			   const u64 *cmds)
+{
+	u64 *cmd_ptr;
+	unsigned int index;
+
+	cmd_ptr = (u64 *)cvmx_phys_to_ptr((u64)qptr->base_paddr);
+	index = qptr->index;
+
+	/* Enforce queue depth limit, if enabled, once per block */
+	if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && cvmx_unlikely(qptr->max_depth)) {
+		unsigned int depth = cvmx_cmd_queue_length(queue_id);
+
+		depth /= qptr->pool_size_m1;
+
+		if (cvmx_unlikely(depth > qptr->max_depth))
+			return CVMX_CMD_QUEUE_FULL;
+	}
+
+	/*
+	 * If the block allocation fails, even the words that we wrote
+	 * to the current block will not count because the 'index' will
+	 * not be comitted.
+	 * The loop is run 'count + 1' times to take care of the tail
+	 * case, where the buffer is full to the rim, so the link
+	 * pointer must be filled with a valid address.
+	 */
+	while (cmd_count >= 0) {
+		if (index >= qptr->pool_size_m1) {
+			/* Block is full, get another one and proceed */
+			cmd_ptr = __cvmx_cmd_queue_add_blk(qptr);
+
+			/* Baul on allocation error w/o comitting anything */
+			if (cvmx_unlikely(!cmd_ptr))
+				return CVMX_CMD_QUEUE_NO_MEMORY;
+
+			/* Reset index for start of new block */
+			index = 0;
+		}
+		/* Exit Loop on 'count + 1' iterations */
+		if (cmd_count <= 0)
+			break;
+		/* Store commands into queue block while there is space */
+		cmd_ptr[index++] = *cmds++;
+		cmd_count--;
+	} /* while cmd_count */
+
+	/* Commit added words if all is well */
+	qptr->index = index;
+
+	return CVMX_CMD_QUEUE_SUCCESS;
+}
Signature · 1 line
2.35.1
#24 lore raw

PATCH 26/52 mips: octeon: Add cvmx-fau-compat.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-fau-compat.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-fau-compat.c | 53 +++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-fau-compat.c
arch/mips/mach-octeon/cvmx-fau-compat.c new file +53 -0
new file mode 100644
index 000000000000..9c2ff763ad53
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ */
+
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-global-resources.h>
+
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#include <mach/cvmx-hwfau.h>
+
+u8 *cvmx_fau_regs_ptr;
+
+void cvmx_fau_bootmem_init(void *bootmem)
+{
+	memset(bootmem, 0, CVMX_FAU_MAX_REGISTERS_8);
+}
+
+/**
+ * Initializes FAU region for devices without FAU unit.
+ * @return 0 on success -1 on failure
+ */
+int cvmx_fau_init(void)
+{
+	cvmx_fau_regs_ptr = (u8 *)cvmx_bootmem_alloc_named_range_once(
+		CVMX_FAU_MAX_REGISTERS_8, 0, 1ull << 31, 128,
+		"cvmx_fau_registers", cvmx_fau_bootmem_init);
+
+	if (cvmx_fau_regs_ptr == 0ull) {
+		debug("ERROR: Failed to alloc named block for software FAU.\n");
+		return -1;
+	}
+
+	return 0;
+}
Signature · 1 line
2.35.1
#25 lore raw

PATCH 28/52 mips: octeon: Add cvmx-fpa-resource.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-fpa-resource.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-fpa-resource.c | 305 ++++++++++++++++++++++
 1 file changed, 305 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-fpa-resource.c
arch/mips/mach-octeon/cvmx-fpa-resource.c new file +305 -0
new file mode 100644
index 000000000000..743518850088
@@ -0,0 +1,305 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ */
+
+#include <errno.h>
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+#include <mach/cvmx-range.h>
+#include <mach/cvmx-global-resources.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-ipd.h>
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-pko3.h>
+#include <mach/cvmx-pko3-queue.h>
+#include <mach/cvmx-pko3-resources.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#include <mach/cvmx-helper-bgx.h>
+#include <mach/cvmx-helper-cfg.h>
+#include <mach/cvmx-helper-util.h>
+#include <mach/cvmx-helper-pki.h>
+
+static struct global_resource_tag get_fpa1_resource_tag(void)
+{
+	return CVMX_GR_TAG_FPA;
+}
+
+static struct global_resource_tag get_fpa3_aura_resource_tag(int node)
+{
+	return cvmx_get_gr_tag('c', 'v', 'm', '_', 'a', 'u', 'r', 'a', '_',
+			       node + '0', '.', '.', '.', '.', '.', '.');
+}
+
+static struct global_resource_tag get_fpa3_pool_resource_tag(int node)
+{
+	return cvmx_get_gr_tag('c', 'v', 'm', '_', 'p', 'o', 'o', 'l', '_',
+			       node + '0', '.', '.', '.', '.', '.', '.');
+}
+
+int cvmx_fpa_get_max_pools(void)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_FPA3))
+		return cvmx_fpa3_num_auras();
+	else if (OCTEON_IS_MODEL(OCTEON_CN68XX))
+		/* 68xx pool 8 is not available via API */
+		return CVMX_FPA1_NUM_POOLS;
+	else
+		return CVMX_FPA1_NUM_POOLS;
+}
+
+u64 cvmx_fpa3_get_aura_owner(cvmx_fpa3_gaura_t aura)
+{
+	return cvmx_get_global_resource_owner(
+		get_fpa3_aura_resource_tag(aura.node), aura.laura);
+}
+
+u64 cvmx_fpa1_get_pool_owner(cvmx_fpa1_pool_t pool)
+{
+	return cvmx_get_global_resource_owner(get_fpa1_resource_tag(), pool);
+}
+
+u64 cvmx_fpa_get_pool_owner(int pool_num)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_FPA3))
+		return cvmx_fpa3_get_aura_owner(
+			cvmx_fpa1_pool_to_fpa3_aura(pool_num));
+	else
+		return cvmx_fpa1_get_pool_owner(pool_num);
+}
+
+/**
+ */
+cvmx_fpa3_gaura_t cvmx_fpa3_reserve_aura(int node, int desired_aura_num)
+{
+	u64 owner = cvmx_get_app_id();
+	int rv = 0;
+	struct global_resource_tag tag;
+	cvmx_fpa3_gaura_t aura;
+
+	if (node == -1)
+		node = cvmx_get_node_num();
+
+	tag = get_fpa3_aura_resource_tag(node);
+
+	if (cvmx_create_global_resource_range(tag, cvmx_fpa3_num_auras()) !=
+	    0) {
+		printf("ERROR: %s: global resource create node=%u\n", __func__,
+		       node);
+		return CVMX_FPA3_INVALID_GAURA;
+	}
+
+	if (desired_aura_num >= 0)
+		rv = cvmx_reserve_global_resource_range(tag, owner,
+							desired_aura_num, 1);
+	else
+		rv = cvmx_resource_alloc_reverse(tag, owner);
+
+	if (rv < 0) {
+		printf("ERROR: %s: node=%u desired aura=%d\n", __func__, node,
+		       desired_aura_num);
+		return CVMX_FPA3_INVALID_GAURA;
+	}
+
+	aura = __cvmx_fpa3_gaura(node, rv);
+
+	return aura;
+}
+
+int cvmx_fpa3_release_aura(cvmx_fpa3_gaura_t aura)
+{
+	struct global_resource_tag tag = get_fpa3_aura_resource_tag(aura.node);
+	int laura = aura.laura;
+
+	if (!__cvmx_fpa3_aura_valid(aura))
+		return -1;
+
+	return cvmx_free_global_resource_range_multiple(tag, &laura, 1);
+}
+
+/**
+ */
+cvmx_fpa3_pool_t cvmx_fpa3_reserve_pool(int node, int desired_pool_num)
+{
+	u64 owner = cvmx_get_app_id();
+	int rv = 0;
+	struct global_resource_tag tag;
+	cvmx_fpa3_pool_t pool;
+
+	if (node == -1)
+		node = cvmx_get_node_num();
+
+	tag = get_fpa3_pool_resource_tag(node);
+
+	if (cvmx_create_global_resource_range(tag, cvmx_fpa3_num_pools()) !=
+	    0) {
+		printf("ERROR: %s: global resource create node=%u\n", __func__,
+		       node);
+		return CVMX_FPA3_INVALID_POOL;
+	}
+
+	if (desired_pool_num >= 0)
+		rv = cvmx_reserve_global_resource_range(tag, owner,
+							desired_pool_num, 1);
+	else
+		rv = cvmx_resource_alloc_reverse(tag, owner);
+
+	if (rv < 0) {
+		/* Desired pool is already in use */
+		return CVMX_FPA3_INVALID_POOL;
+	}
+
+	pool = __cvmx_fpa3_pool(node, rv);
+
+	return pool;
+}
+
+int cvmx_fpa3_release_pool(cvmx_fpa3_pool_t pool)
+{
+	struct global_resource_tag tag = get_fpa3_pool_resource_tag(pool.node);
+	int lpool = pool.lpool;
+
+	if (!__cvmx_fpa3_pool_valid(pool))
+		return -1;
+
+	if (cvmx_create_global_resource_range(tag, cvmx_fpa3_num_pools()) !=
+	    0) {
+		printf("ERROR: %s: global resource create node=%u\n", __func__,
+		       pool.node);
+		return -1;
+	}
+
+	return cvmx_free_global_resource_range_multiple(tag, &lpool, 1);
+}
+
+cvmx_fpa1_pool_t cvmx_fpa1_reserve_pool(int desired_pool_num)
+{
+	u64 owner = cvmx_get_app_id();
+	struct global_resource_tag tag;
+	int rv;
+
+	tag = get_fpa1_resource_tag();
+
+	if (cvmx_create_global_resource_range(tag, CVMX_FPA1_NUM_POOLS) != 0) {
+		printf("ERROR: %s: global resource not created\n", __func__);
+		return -1;
+	}
+
+	if (desired_pool_num >= 0) {
+		rv = cvmx_reserve_global_resource_range(tag, owner,
+							desired_pool_num, 1);
+	} else {
+		rv = cvmx_resource_alloc_reverse(tag, owner);
+	}
+
+	if (rv < 0) {
+		printf("ERROR: %s: FPA_POOL %d unavailable\n", __func__,
+		       desired_pool_num);
+		return CVMX_RESOURCE_ALREADY_RESERVED;
+	}
+	return (cvmx_fpa1_pool_t)rv;
+}
+
+int cvmx_fpa1_release_pool(cvmx_fpa1_pool_t pool)
+{
+	struct global_resource_tag tag;
+
+	tag = get_fpa1_resource_tag();
+
+	return cvmx_free_global_resource_range_multiple(tag, &pool, 1);
+}
+
+/**
+ * Query if an FPA pool is available for reservation
+ * using global resources
+ * @note This function is no longer in use, and will be removed in a future release
+ */
+int cvmx_fpa1_is_pool_available(cvmx_fpa1_pool_t pool)
+{
+	if (cvmx_fpa1_reserve_pool(pool) == -1)
+		return 0;
+	cvmx_fpa1_release_pool(pool);
+	return 1;
+}
+
+/**
+ * @INTERNAL
+ *
+ * This function is no longer in use, and will be removed in a future release
+ */
+int cvmx_fpa3_is_pool_available(int node, int lpool)
+{
+	cvmx_fpa3_pool_t pool;
+
+	pool = cvmx_fpa3_reserve_pool(node, lpool);
+
+	if (!__cvmx_fpa3_pool_valid(pool))
+		return 0;
+
+	cvmx_fpa3_release_pool(pool);
+	return 1;
+}
+
+/**
+ * @INTERNAL
+ *
+ * This function is no longer in use, and will be removed in a future release
+ */
+int cvmx_fpa3_is_aura_available(int node, int laura)
+{
+	cvmx_fpa3_gaura_t aura;
+
+	aura = cvmx_fpa3_reserve_aura(node, laura);
+
+	if (!__cvmx_fpa3_aura_valid(aura))
+		return 0;
+
+	cvmx_fpa3_release_aura(aura);
+	return 1;
+}
+
+/**
+ * Return if aura/pool is already reserved
+ * @param pool_num - pool to check (aura for o78+)
+ * @return 0 if reserved, 1 if available
+ *
+ * @note This function is no longer in use, and will be removed in a future release
+ */
+int cvmx_fpa_is_pool_available(int pool_num)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_FPA3))
+		return cvmx_fpa3_is_aura_available(0, pool_num);
+	else
+		return cvmx_fpa1_is_pool_available(pool_num);
+}
Signature · 1 line
2.35.1
#26 lore raw

PATCH 29/52 mips: octeon: Add cvmx-global-resource.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-global-resource.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-global-resources.c | 639 ++++++++++++++++++
 1 file changed, 639 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-global-resources.c
arch/mips/mach-octeon/cvmx-global-resources.c new file +639 -0
new file mode 100644
index 000000000000..14a2ed524714
@@ -0,0 +1,639 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ */
+
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+#include <mach/cvmx-range.h>
+
+#include <mach/cvmx-global-resources.h>
+#include <mach/cvmx-bootmem.h>
+
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#define CVMX_MAX_GLOBAL_RESOURCES 128
+#define CVMX_RESOURCES_ENTRIES_SIZE                                            \
+	(sizeof(struct cvmx_global_resource_entry) * CVMX_MAX_GLOBAL_RESOURCES)
+
+/**
+ * This macro returns a member of the data
+ * structure. The argument "field" is the member name of the
+ * structure to read. The return type is a u64.
+ */
+#define CVMX_GLOBAL_RESOURCES_GET_FIELD(field)                                 \
+	__cvmx_struct_get_unsigned_field(                                      \
+		__cvmx_global_resources_addr,                                  \
+		offsetof(struct cvmx_global_resources, field),                 \
+		SIZEOF_FIELD(struct cvmx_global_resources, field))
+
+/**
+ * This macro writes a member of the struct cvmx_global_resourcest
+ * structure. The argument "field" is the member name of the
+ * struct cvmx_global_resources to write.
+ */
+#define CVMX_GLOBAL_RESOURCES_SET_FIELD(field, value)                          \
+	__cvmx_struct_set_unsigned_field(                                      \
+		__cvmx_global_resources_addr,                                  \
+		offsetof(struct cvmx_global_resources, field),                 \
+		SIZEOF_FIELD(struct cvmx_global_resources, field), value)
+
+/**
+ * This macro returns a member of the struct cvmx_global_resource_entry.
+ * The argument "field" is the member name of this structure.
+ * the return type is a u64. The "addr" parameter is the physical
+ * address of the structure.
+ */
+#define CVMX_RESOURCE_ENTRY_GET_FIELD(addr, field)                             \
+	__cvmx_struct_get_unsigned_field(                                      \
+		addr, offsetof(struct cvmx_global_resource_entry, field),      \
+		SIZEOF_FIELD(struct cvmx_global_resource_entry, field))
+
+/**
+ * This macro writes a member of the struct cvmx_global_resource_entry
+ * structure. The argument "field" is the member name of the
+ * struct cvmx_global_resource_entry to write. The "addr" parameter
+ * is the physical address of the structure.
+ */
+#define CVMX_RESOURCE_ENTRY_SET_FIELD(addr, field, value)                      \
+	__cvmx_struct_set_unsigned_field(                                      \
+		addr, offsetof(struct cvmx_global_resource_entry, field),      \
+		SIZEOF_FIELD(struct cvmx_global_resource_entry, field), value)
+
+#define CVMX_GET_RESOURCE_ENTRY(count)                                         \
+	(__cvmx_global_resources_addr +                                        \
+	 offsetof(struct cvmx_global_resources, resource_entry) +              \
+	 (count * sizeof(struct cvmx_global_resource_entry)))
+
+#define CVMX_RESOURCE_TAG_SET_FIELD(addr, field, value)                        \
+	__cvmx_struct_set_unsigned_field(                                      \
+		addr, offsetof(struct global_resource_tag, field),             \
+		SIZEOF_FIELD(struct global_resource_tag, field), value)
+
+#define CVMX_RESOURCE_TAG_GET_FIELD(addr, field)                               \
+	__cvmx_struct_get_unsigned_field(                                      \
+		addr, offsetof(struct global_resource_tag, field),             \
+		SIZEOF_FIELD(struct global_resource_tag, field))
+
+#define MAX_RESOURCE_TAG_LEN		16
+#define CVMX_GLOBAL_RESOURCE_NO_LOCKING (1)
+
+struct cvmx_global_resource_entry {
+	struct global_resource_tag tag;
+	u64 phys_addr;
+	u64 size;
+};
+
+struct cvmx_global_resources {
+	u32 pad;
+	u32 rlock;
+	u64 entry_cnt;
+	struct cvmx_global_resource_entry resource_entry[];
+};
+
+/* Not the right place, putting it here for now */
+int cvmx_enable_helper_flag;
+u64 cvmx_app_id;
+
+/*
+ * Global named memory can be accessed anywhere even in 32-bit mode
+ */
+static u64 __cvmx_global_resources_addr;
+
+/**
+ * This macro returns the size of a member of a structure.
+ */
+#define SIZEOF_FIELD(s, field) sizeof(((s *)NULL)->field)
+
+/**
+ * This function is the implementation of the get macros defined
+ * for individual structure members. The argument are generated
+ * by the macros inorder to read only the needed memory.
+ *
+ * @param base   64bit physical address of the complete structure
+ * @param offset from the beginning of the structure to the member being
+ *               accessed.
+ * @param size   Size of the structure member.
+ *
+ * @return Value of the structure member promoted into a u64.
+ */
+static inline u64 __cvmx_struct_get_unsigned_field(u64 base, int offset,
+						   int size)
+{
+	base = (1ull << 63) | (base + offset);
+	switch (size) {
+	case 4:
+		return cvmx_read64_uint32(base);
+	case 8:
+		return cvmx_read64_uint64(base);
+	default:
+		return 0;
+	}
+}
+
+/**
+ * This function is the implementation of the set macros defined
+ * for individual structure members. The argument are generated
+ * by the macros in order to write only the needed memory.
+ *
+ * @param base   64bit physical address of the complete structure
+ * @param offset from the beginning of the structure to the member being
+ *               accessed.
+ * @param size   Size of the structure member.
+ * @param value  Value to write into the structure
+ */
+static inline void __cvmx_struct_set_unsigned_field(u64 base, int offset,
+						    int size, u64 value)
+{
+	base = (1ull << 63) | (base + offset);
+	switch (size) {
+	case 4:
+		cvmx_write64_uint32(base, value);
+		break;
+	case 8:
+		cvmx_write64_uint64(base, value);
+		break;
+	default:
+		break;
+	}
+}
+
+/* Get the global resource lock. */
+static inline void __cvmx_global_resource_lock(void)
+{
+	u64 lock_addr =
+		(1ull << 63) | (__cvmx_global_resources_addr +
+				offsetof(struct cvmx_global_resources, rlock));
+	unsigned int tmp;
+
+	__asm__ __volatile__(".set noreorder\n"
+			     "1: ll   %[tmp], 0(%[addr])\n"
+			     "   bnez %[tmp], 1b\n"
+			     "   li   %[tmp], 1\n"
+			     "   sc   %[tmp], 0(%[addr])\n"
+			     "   beqz %[tmp], 1b\n"
+			     "   nop\n"
+			     ".set reorder\n"
+			     : [tmp] "=&r"(tmp)
+			     : [addr] "r"(lock_addr)
+			     : "memory");
+}
+
+/* Release the global resource lock. */
+static inline void __cvmx_global_resource_unlock(void)
+{
+	u64 lock_addr =
+		(1ull << 63) | (__cvmx_global_resources_addr +
+				offsetof(struct cvmx_global_resources, rlock));
+	CVMX_SYNCW;
+	__asm__ __volatile__("sw $0, 0(%[addr])\n"
+			     :
+			     : [addr] "r"(lock_addr)
+			     : "memory");
+	CVMX_SYNCW;
+}
+
+static u64 __cvmx_alloc_bootmem_for_global_resources(int sz)
+{
+	void *tmp;
+
+	tmp = cvmx_bootmem_alloc_range(sz, CVMX_CACHE_LINE_SIZE, 0, 0);
+	return cvmx_ptr_to_phys(tmp);
+}
+
+static inline void __cvmx_get_tagname(struct global_resource_tag *rtag,
+				      char *tagname)
+{
+	int i, j, k;
+
+	j = 0;
+	k = 8;
+	for (i = 7; i >= 0; i--, j++, k++) {
+		tagname[j] = (rtag->lo >> (i * 8)) & 0xff;
+		tagname[k] = (rtag->hi >> (i * 8)) & 0xff;
+	}
+}
+
+static u64 __cvmx_global_resources_init(void)
+{
+	struct cvmx_bootmem_named_block_desc *block_desc;
+	int sz = sizeof(struct cvmx_global_resources) +
+		 CVMX_RESOURCES_ENTRIES_SIZE;
+	s64 tmp_phys;
+	int count = 0;
+	u64 base = 0;
+
+	cvmx_bootmem_lock();
+
+	block_desc = (struct cvmx_bootmem_named_block_desc *)
+		__cvmx_bootmem_find_named_block_flags(
+			CVMX_GLOBAL_RESOURCES_DATA_NAME,
+			CVMX_BOOTMEM_FLAG_NO_LOCKING);
+	if (!block_desc) {
+		debug("%s: allocating global resources\n", __func__);
+
+		tmp_phys = cvmx_bootmem_phy_named_block_alloc(
+			sz, 0, 0, CVMX_CACHE_LINE_SIZE,
+			CVMX_GLOBAL_RESOURCES_DATA_NAME,
+			CVMX_BOOTMEM_FLAG_NO_LOCKING);
+		if (tmp_phys < 0) {
+			cvmx_printf(
+				"ERROR: %s: failed to allocate global resource name block. sz=%d\n",
+				__func__, sz);
+			goto end;
+		}
+		__cvmx_global_resources_addr = (u64)tmp_phys;
+
+		debug("%s: memset global resources %llu\n", __func__,
+		      CAST_ULL(__cvmx_global_resources_addr));
+
+		base = (1ull << 63) | __cvmx_global_resources_addr;
+		for (count = 0; count < (sz / 8); count++) {
+			cvmx_write64_uint64(base, 0);
+			base += 8;
+		}
+	} else {
+		debug("%s:found global resource\n", __func__);
+		__cvmx_global_resources_addr = block_desc->base_addr;
+	}
+end:
+	cvmx_bootmem_unlock();
+	debug("__cvmx_global_resources_addr=%llu sz=%d\n",
+	      CAST_ULL(__cvmx_global_resources_addr), sz);
+	return __cvmx_global_resources_addr;
+}
+
+u64 cvmx_get_global_resource(struct global_resource_tag tag, int no_lock)
+{
+	u64 entry_cnt = 0;
+	u64 resource_entry_addr = 0;
+	int count = 0;
+	u64 rphys_addr = 0;
+	u64 tag_lo = 0, tag_hi = 0;
+
+	if (__cvmx_global_resources_addr == 0)
+		__cvmx_global_resources_init();
+	if (!no_lock)
+		__cvmx_global_resource_lock();
+
+	entry_cnt = CVMX_GLOBAL_RESOURCES_GET_FIELD(entry_cnt);
+	while (entry_cnt > 0) {
+		resource_entry_addr = CVMX_GET_RESOURCE_ENTRY(count);
+		tag_lo = CVMX_RESOURCE_TAG_GET_FIELD(resource_entry_addr, lo);
+		tag_hi = CVMX_RESOURCE_TAG_GET_FIELD(resource_entry_addr, hi);
+
+		if (tag_lo == tag.lo && tag_hi == tag.hi) {
+			debug("%s: Found global resource entry\n", __func__);
+			break;
+		}
+		entry_cnt--;
+		count++;
+	}
+
+	if (entry_cnt == 0) {
+		debug("%s: no matching global resource entry found\n",
+		      __func__);
+		if (!no_lock)
+			__cvmx_global_resource_unlock();
+		return 0;
+	}
+	rphys_addr =
+		CVMX_RESOURCE_ENTRY_GET_FIELD(resource_entry_addr, phys_addr);
+	if (!no_lock)
+		__cvmx_global_resource_unlock();
+
+	return rphys_addr;
+}
+
+u64 cvmx_create_global_resource(struct global_resource_tag tag, u64 size,
+				int no_lock, int *_new_)
+{
+	u64 entry_count = 0;
+	u64 resource_entry_addr = 0;
+	u64 phys_addr;
+
+	if (__cvmx_global_resources_addr == 0)
+		__cvmx_global_resources_init();
+
+	if (!no_lock)
+		__cvmx_global_resource_lock();
+
+	phys_addr =
+		cvmx_get_global_resource(tag, CVMX_GLOBAL_RESOURCE_NO_LOCKING);
+	if (phys_addr != 0) {
+		/* we already have the resource, return it */
+		*_new_ = 0;
+		goto end;
+	}
+
+	*_new_ = 1;
+	entry_count = CVMX_GLOBAL_RESOURCES_GET_FIELD(entry_cnt);
+	if (entry_count >= CVMX_MAX_GLOBAL_RESOURCES) {
+		char tagname[MAX_RESOURCE_TAG_LEN + 1];
+
+		__cvmx_get_tagname(&tag, tagname);
+		cvmx_printf(
+			"ERROR: %s: reached global resources limit for %s\n",
+			__func__, tagname);
+		phys_addr = 0;
+		goto end;
+	}
+
+	/* Allocate bootmem for the resource*/
+	phys_addr = __cvmx_alloc_bootmem_for_global_resources(size);
+	if (!phys_addr) {
+		char tagname[MAX_RESOURCE_TAG_LEN + 1];
+
+		__cvmx_get_tagname(&tag, tagname);
+		debug("ERROR: %s: out of memory %s, size=%d\n", __func__,
+		      tagname, (int)size);
+		goto end;
+	}
+
+	resource_entry_addr = CVMX_GET_RESOURCE_ENTRY(entry_count);
+	CVMX_RESOURCE_ENTRY_SET_FIELD(resource_entry_addr, phys_addr,
+				      phys_addr);
+	CVMX_RESOURCE_ENTRY_SET_FIELD(resource_entry_addr, size, size);
+	CVMX_RESOURCE_TAG_SET_FIELD(resource_entry_addr, lo, tag.lo);
+	CVMX_RESOURCE_TAG_SET_FIELD(resource_entry_addr, hi, tag.hi);
+	/* update entry_cnt */
+	entry_count += 1;
+	CVMX_GLOBAL_RESOURCES_SET_FIELD(entry_cnt, entry_count);
+
+end:
+	if (!no_lock)
+		__cvmx_global_resource_unlock();
+
+	return phys_addr;
+}
+
+int cvmx_create_global_resource_range(struct global_resource_tag tag,
+				      int nelements)
+{
+	int sz = cvmx_range_memory_size(nelements);
+	int _new_;
+	u64 addr;
+	int rv = 0;
+
+	if (__cvmx_global_resources_addr == 0)
+		__cvmx_global_resources_init();
+
+	__cvmx_global_resource_lock();
+	addr = cvmx_create_global_resource(tag, sz, 1, &_new_);
+	if (!addr) {
+		__cvmx_global_resource_unlock();
+		return -1;
+	}
+	if (_new_)
+		rv = cvmx_range_init(addr, nelements);
+	__cvmx_global_resource_unlock();
+	return rv;
+}
+
+int cvmx_allocate_global_resource_range(struct global_resource_tag tag,
+					u64 owner, int nelements, int alignment)
+{
+	u64 addr = cvmx_get_global_resource(tag, 1);
+	int base;
+
+	if (addr == 0) {
+		char tagname[256];
+
+		__cvmx_get_tagname(&tag, tagname);
+		cvmx_printf("ERROR: %s: cannot find resource %s\n", __func__,
+			    tagname);
+		return -1;
+	}
+	__cvmx_global_resource_lock();
+	base = cvmx_range_alloc(addr, owner, nelements, alignment);
+	__cvmx_global_resource_unlock();
+	return base;
+}
+
+int cvmx_resource_alloc_many(struct global_resource_tag tag, u64 owner,
+			     int nelements, int allocated_elements[])
+{
+	u64 addr = cvmx_get_global_resource(tag, 1);
+	int rv;
+
+	if (addr == 0) {
+		char tagname[256];
+
+		__cvmx_get_tagname(&tag, tagname);
+		debug("ERROR: cannot find resource %s\n", tagname);
+		return -1;
+	}
+	__cvmx_global_resource_lock();
+	rv = cvmx_range_alloc_non_contiguos(addr, owner, nelements,
+					    allocated_elements);
+	__cvmx_global_resource_unlock();
+	return rv;
+}
+
+int cvmx_resource_alloc_reverse(struct global_resource_tag tag, u64 owner)
+{
+	u64 addr = cvmx_get_global_resource(tag, 1);
+	int rv;
+
+	if (addr == 0) {
+		char tagname[256];
+
+		__cvmx_get_tagname(&tag, tagname);
+		debug("ERROR: cannot find resource %s\n", tagname);
+		return -1;
+	}
+	__cvmx_global_resource_lock();
+	rv = cvmx_range_alloc_ordered(addr, owner, 1, 1, 1);
+	__cvmx_global_resource_unlock();
+	return rv;
+}
+
+int cvmx_reserve_global_resource_range(struct global_resource_tag tag,
+				       u64 owner, int base, int nelements)
+{
+	u64 addr = cvmx_get_global_resource(tag, 1);
+	int start;
+
+	__cvmx_global_resource_lock();
+	start = cvmx_range_reserve(addr, owner, base, nelements);
+	__cvmx_global_resource_unlock();
+	return start;
+}
+
+int cvmx_free_global_resource_range_with_base(struct global_resource_tag tag,
+					      int base, int nelements)
+{
+	u64 addr = cvmx_get_global_resource(tag, 1);
+	int rv;
+
+	/* Resource was not created, nothing to release */
+	if (addr == 0)
+		return 0;
+
+	__cvmx_global_resource_lock();
+	rv = cvmx_range_free_with_base(addr, base, nelements);
+	__cvmx_global_resource_unlock();
+	return rv;
+}
+
+int cvmx_free_global_resource_range_multiple(struct global_resource_tag tag,
+					     int bases[], int nelements)
+{
+	u64 addr = cvmx_get_global_resource(tag, 1);
+	int rv;
+
+	/* Resource was not created, nothing to release */
+	if (addr == 0)
+		return 0;
+
+	__cvmx_global_resource_lock();
+	rv = cvmx_range_free_mutiple(addr, bases, nelements);
+	__cvmx_global_resource_unlock();
+	return rv;
+}
+
+int cvmx_free_global_resource_range_with_owner(struct global_resource_tag tag,
+					       int owner)
+{
+	u64 addr = cvmx_get_global_resource(tag, 1);
+	int rv;
+
+	/* Resource was not created, nothing to release */
+	if (addr == 0)
+		return 0;
+
+	__cvmx_global_resource_lock();
+	rv = cvmx_range_free_with_owner(addr, owner);
+	__cvmx_global_resource_unlock();
+	return rv;
+}
+
+void cvmx_show_global_resource_range(struct global_resource_tag tag)
+{
+	u64 addr = cvmx_get_global_resource(tag, 1);
+
+	cvmx_range_show(addr);
+}
+
+int free_global_resources(void)
+{
+	int rc;
+	int i, entry_cnt;
+	u64 resource_entry_addr, phys_addr, size;
+
+	if (__cvmx_global_resources_addr == 0)
+		__cvmx_global_resources_init();
+
+	__cvmx_global_resource_lock();
+
+	entry_cnt = CVMX_GLOBAL_RESOURCES_GET_FIELD(entry_cnt);
+
+	/* get and free all the global resources */
+	for (i = 0; i < entry_cnt; i++) {
+		resource_entry_addr = CVMX_GET_RESOURCE_ENTRY(i);
+		phys_addr = CVMX_RESOURCE_ENTRY_GET_FIELD(resource_entry_addr,
+							  phys_addr);
+		size = CVMX_RESOURCE_ENTRY_GET_FIELD(resource_entry_addr, size);
+		/* free the resource */
+		rc = __cvmx_bootmem_phy_free(phys_addr, size, 0);
+		if (!rc) {
+			debug("ERROR: %s: could not free memory to bootmem\n",
+			      __func__);
+		}
+	}
+
+	__cvmx_global_resource_unlock();
+
+	rc = cvmx_bootmem_free_named(CVMX_GLOBAL_RESOURCES_DATA_NAME);
+	debug("freed global resources named block rc=%d\n", rc);
+
+	__cvmx_global_resources_addr = 0;
+
+	return 0;
+}
+
+u64 cvmx_get_global_resource_owner(struct global_resource_tag tag, int base)
+{
+	u64 addr = cvmx_get_global_resource(tag, 1);
+
+	/* Resource was not created, return "available" special owner code */
+	if (addr == 0)
+		return -88LL;
+
+	return cvmx_range_get_owner(addr, base);
+}
+
+void cvmx_global_resources_show(void)
+{
+	u64 entry_cnt;
+	u64 p;
+	char tagname[MAX_RESOURCE_TAG_LEN + 1];
+	struct global_resource_tag rtag;
+	u64 count;
+	u64 phys_addr;
+
+	if (__cvmx_global_resources_addr == 0)
+		__cvmx_global_resources_init();
+
+	__cvmx_global_resource_lock();
+
+	entry_cnt = CVMX_GLOBAL_RESOURCES_GET_FIELD(entry_cnt);
+	memset(tagname, 0, MAX_RESOURCE_TAG_LEN + 1);
+
+	for (count = 0; count < entry_cnt; count++) {
+		p = CVMX_GET_RESOURCE_ENTRY(count);
+		phys_addr = CVMX_RESOURCE_ENTRY_GET_FIELD(p, phys_addr);
+		rtag.lo = CVMX_RESOURCE_TAG_GET_FIELD(p, lo);
+		rtag.hi = CVMX_RESOURCE_TAG_GET_FIELD(p, hi);
+		__cvmx_get_tagname(&rtag, tagname);
+		debug("Global Resource tag name: %s Resource Address: %llx\n",
+		      tagname, CAST_ULL(phys_addr));
+	}
+	debug("<End of Global Resources>\n");
+	__cvmx_global_resource_unlock();
+}
+
+void cvmx_app_id_init(void *bootmem)
+{
+	u64 *p = (u64 *)bootmem;
+
+	*p = 0;
+}
+
+u64 cvmx_allocate_app_id(void)
+{
+	u64 *vptr;
+
+	vptr = (u64 *)cvmx_bootmem_alloc_named_range_once(sizeof(cvmx_app_id),
+							  0, 1 << 31, 128,
+							  "cvmx_app_id",
+							  cvmx_app_id_init);
+
+	cvmx_app_id = __atomic_add_fetch(vptr, 1, __ATOMIC_SEQ_CST);
+
+	debug("CVMX_APP_ID = %lx\n", (unsigned long)cvmx_app_id);
+	return cvmx_app_id;
+}
+
+u64 cvmx_get_app_id(void)
+{
+	if (cvmx_app_id == 0)
+		cvmx_allocate_app_id();
+	return cvmx_app_id;
+}
Signature · 1 line
2.35.1
#27 lore raw

PATCH 30/52 mips: octeon: Add cvmx-ilk.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-ilk.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-ilk.c | 1618 ++++++++++++++++++++++++++++++
 1 file changed, 1618 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-ilk.c
arch/mips/mach-octeon/cvmx-ilk.c new file +1618 -0
new file mode 100644
index 000000000000..e9d0cfc2413f
@@ -0,0 +1,1618 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * Support library for the ILK
+ */
+
+#include <time.h>
+#include <log.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-pki.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+/*
+ * global configurations.
+ *
+ * for cn68, the default is {0xf, 0xf0}. to disable the 2nd ILK, set
+ * cvmx_ilk_lane_mask[CVMX_NUM_ILK_INTF] = {0xff, 0x0} and
+ * cvmx_ilk_chans[CVMX_NUM_ILK_INTF] = {8, 0}
+ */
+unsigned short cvmx_ilk_lane_mask[CVMX_MAX_NODES][CVMX_NUM_ILK_INTF] = {
+	[0 ... CVMX_MAX_NODES - 1] = { 0x000f, 0x00f0 }
+};
+
+int cvmx_ilk_chans[CVMX_MAX_NODES][CVMX_NUM_ILK_INTF] = {
+	[0 ... CVMX_MAX_NODES - 1] = { 8, 8 }
+};
+
+static cvmx_ilk_intf_t cvmx_ilk_intf_cfg[CVMX_MAX_NODES][CVMX_NUM_ILK_INTF];
+
+cvmx_ilk_LA_mode_t cvmx_ilk_LA_mode[CVMX_NUM_ILK_INTF] = { { 0, 0 }, { 0, 0 } };
+/**
+ * User-overrideable callback function that returns whether or not an interface
+ * should use look-aside mode.
+ *
+ * @param interface - interface being checked
+ * @param channel - channel number, can be 0 or 1 or -1 to see if LA mode
+ *                  should be enabled for the interface.
+ * @return 0 to not use LA-mode, 1 to use LA-mode.
+ */
+int cvmx_ilk_use_la_mode(int interface, int channel)
+{
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX_PASS1_0))
+		return 0;
+
+	if (interface >= CVMX_NUM_ILK_INTF) {
+		debug("ERROR: invalid interface=%d in %s\n",
+		      interface, __func__);
+		return -1;
+	}
+	return cvmx_ilk_LA_mode[interface].ilk_LA_mode;
+}
+
+/**
+ * User-overrideable callback function that returns whether or not an interface
+ * in look-aside mode should enable the RX calendar.
+ *
+ * @param interface - interface to check
+ * @return 1 to enable RX calendar, 0 to disable RX calendar.
+ *
+ * NOTE: For the CN68XX pass 2.0 this will enable the RX calendar for interface
+ * 0 and not interface 1.  It is up to the customer to override this behavior.
+ */
+int cvmx_ilk_la_mode_enable_rx_calendar(int interface)
+{
+	/* There is an errata in the CN68XX pass 2.0 where if connected
+	 * in a loopback configuration or back to back then only one interface
+	 * can have the RX calendar enabled.
+	 */
+	if (interface >= CVMX_NUM_ILK_INTF) {
+		debug("ERROR: invalid interface=%d in %s\n",
+		      interface, __func__);
+		return -1;
+	}
+	return cvmx_ilk_LA_mode[interface].ilk_LA_mode_cal_ena;
+}
+
+/**
+ * Initialize and start the ILK interface.
+ *
+ * @param interface The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param lane_mask the lane group for this interface
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_start_interface(int interface, unsigned short lane_mask)
+{
+	int res = -1;
+	int other_intf, this_qlm, other_qlm;
+	unsigned short uni_mask;
+	cvmx_ilk_txx_cfg0_t ilk_txx_cfg0;
+	cvmx_ilk_rxx_cfg0_t ilk_rxx_cfg0;
+	cvmx_ilk_ser_cfg_t ilk_ser_cfg;
+	int node = (interface >> 4) & 0xf;
+
+	interface &= 0xf;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	if (lane_mask == 0)
+		return res;
+
+	/* check conflicts between 2 ilk interfaces. 1 lane can be assigned to 1
+	 * interface only
+	 */
+	other_intf = !interface;
+	if (cvmx_ilk_lane_mask[node][other_intf] & lane_mask) {
+		debug("ILK%d:%d: %s: lane assignment conflict\n", node,
+		      interface, __func__);
+		return res;
+	}
+
+	/* check the legality of the lane mask. interface 0 can have 8 lanes,
+	 * while interface 1 can have 4 lanes at most
+	 */
+	uni_mask = lane_mask >> (interface * 4);
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		cvmx_mio_qlmx_cfg_t mio_qlmx_cfg, other_mio_qlmx_cfg;
+
+		if ((uni_mask != 0x1 && uni_mask != 0x3 && uni_mask != 0xf &&
+		     uni_mask != 0xff) ||
+		    (interface == 1 && lane_mask > 0xf0)) {
+			debug("ILK%d: %s: incorrect lane mask: 0x%x\n",
+			      interface, __func__, uni_mask);
+			return res;
+		}
+		/* check the availability of qlms. qlm_cfg = 001 means the chip
+		 * is fused to give this qlm to ilk
+		 */
+		this_qlm = interface + CVMX_ILK_QLM_BASE();
+		other_qlm = other_intf + CVMX_ILK_QLM_BASE();
+		mio_qlmx_cfg.u64 = csr_rd(CVMX_MIO_QLMX_CFG(this_qlm));
+		other_mio_qlmx_cfg.u64 = csr_rd(CVMX_MIO_QLMX_CFG(other_qlm));
+		if (mio_qlmx_cfg.s.qlm_cfg != 1 ||
+		    (uni_mask == 0xff && other_mio_qlmx_cfg.s.qlm_cfg != 1)) {
+			debug("ILK%d: %s: qlm unavailable\n", interface,
+			      __func__);
+			return res;
+		}
+		/* Has 8 lanes */
+		lane_mask &= 0xff;
+	}
+
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		int qlm;
+		unsigned short lane_mask_all = 0;
+
+		/* QLM 4 - QLM 7 can be configured for ILK. Get the lane mask
+		 * of all the qlms that are configured for ilk
+		 */
+		for (qlm = 4; qlm < 8; qlm++) {
+			cvmx_gserx_cfg_t gserx_cfg;
+			cvmx_gserx_phy_ctl_t phy_ctl;
+
+			/* Make sure QLM is powered and out of reset */
+			phy_ctl.u64 =
+				csr_rd_node(node, CVMX_GSERX_PHY_CTL(qlm));
+			if (phy_ctl.s.phy_pd || phy_ctl.s.phy_reset)
+				continue;
+
+			/* Make sure QLM is in ILK mode */
+			gserx_cfg.u64 = csr_rd_node(node, CVMX_GSERX_CFG(qlm));
+			if (gserx_cfg.s.ila)
+				lane_mask_all |= ((1 << 4) - 1)
+						 << (4 * (qlm - 4));
+		}
+
+		if ((lane_mask_all & lane_mask) != lane_mask) {
+			debug("ILK%d: %s: incorrect lane mask: 0x%x\n",
+			      interface, __func__, lane_mask);
+			return res;
+		}
+	}
+
+	/* power up the serdes */
+	ilk_ser_cfg.u64 = csr_rd_node(node, CVMX_ILK_SER_CFG);
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		if (ilk_ser_cfg.cn68xx.ser_pwrup == 0) {
+			ilk_ser_cfg.cn68xx.ser_rxpol_auto = 1;
+			ilk_ser_cfg.cn68xx.ser_rxpol = 0;
+			ilk_ser_cfg.cn68xx.ser_txpol = 0;
+			ilk_ser_cfg.cn68xx.ser_reset_n = 0xff;
+			ilk_ser_cfg.cn68xx.ser_haul = 0;
+		}
+		ilk_ser_cfg.cn68xx.ser_pwrup |=
+			((interface == 0) && (lane_mask > 0xf)) ?
+				0x3 :
+				      (1 << interface);
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		ilk_ser_cfg.cn78xx.ser_rxpol_auto = 1;
+		ilk_ser_cfg.cn78xx.ser_rxpol = 0;
+		ilk_ser_cfg.cn78xx.ser_txpol = 0;
+		ilk_ser_cfg.cn78xx.ser_reset_n = 0xffff;
+	}
+	csr_wr_node(node, CVMX_ILK_SER_CFG, ilk_ser_cfg.u64);
+
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX_PASS2_X)) {
+		/* Workaround for Errata (G-16467) */
+		int qlm = (interface) ? 2 : 1;
+		int start_qlm, end_qlm;
+
+		/* Apply the workaround to both the QLMs if configured for x8 lanes */
+		if (cvmx_pop(lane_mask) > 4) {
+			start_qlm = 1;
+			end_qlm = 2;
+		} else {
+			start_qlm = qlm;
+			end_qlm = qlm;
+		}
+
+		for (qlm = start_qlm; qlm <= end_qlm; qlm++) {
+#ifdef CVMX_QLM_DUMP_STATE
+			debug("%s:%d: ILK%d: Applying workaround for Errata G-16467\n",
+			      __func__, __LINE__, qlm);
+			cvmx_qlm_display_registers(qlm);
+			debug("\n");
+#endif
+			/* This workaround only applies to QLMs running ILK at 6.25Ghz */
+			if ((cvmx_qlm_get_gbaud_mhz(qlm) == 6250) &&
+			    (cvmx_qlm_jtag_get(qlm, 0, "clkf_byp") != 20)) {
+				udelay(100); /* Wait 100us for links to stabalize */
+				cvmx_qlm_jtag_set(qlm, -1, "clkf_byp", 20);
+				/* Allow the QLM to exit reset */
+				cvmx_qlm_jtag_set(qlm, -1, "cfg_rst_n_clr", 0);
+				udelay(100); /* Wait 100us for links to stabalize */
+				/* Allow TX on QLM */
+				cvmx_qlm_jtag_set(qlm, -1, "cfg_tx_idle_set",
+						  0);
+			}
+#ifdef CVMX_QLM_DUMP_STATE
+			debug("%s:%d: ILK%d: Done applying workaround for Errata G-16467\n",
+			      __func__, __LINE__, qlm);
+			cvmx_qlm_display_registers(qlm);
+			debug("\n\n");
+#endif
+		}
+	}
+
+	/* Initialize all calendar entries to xoff state */
+	__cvmx_ilk_clear_cal((node << 4) | interface);
+
+	/* Enable ILK LA mode if configured. */
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		if (cvmx_ilk_use_la_mode(interface, 0)) {
+			cvmx_ilk_txx_cfg1_t ilk_txx_cfg1;
+			cvmx_ilk_rxx_cfg1_t ilk_rxx_cfg1;
+
+			ilk_txx_cfg1.u64 = csr_rd(CVMX_ILK_TXX_CFG1(interface));
+			ilk_rxx_cfg1.u64 = csr_rd(CVMX_ILK_RXX_CFG1(interface));
+			ilk_txx_cfg1.s.la_mode = 1;
+			ilk_txx_cfg1.s.tx_link_fc_jam = 1;
+			ilk_txx_cfg1.s.rx_link_fc_ign = 1;
+			ilk_rxx_cfg1.s.la_mode = 1;
+			csr_wr(CVMX_ILK_TXX_CFG1(interface), ilk_txx_cfg1.u64);
+			csr_wr(CVMX_ILK_RXX_CFG1(interface), ilk_rxx_cfg1.u64);
+			cvmx_ilk_intf_cfg[node][interface].la_mode =
+				1; /* Enable look-aside mode */
+		} else {
+			cvmx_ilk_intf_cfg[node][interface].la_mode =
+				0; /* Disable look-aside mode */
+		}
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX))
+		cvmx_ilk_intf_cfg[node][interface].la_mode = 0;
+
+	/* configure the lane enable of the interface */
+	ilk_txx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+	ilk_rxx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+	ilk_rxx_cfg0.s.lane_ena = lane_mask;
+	ilk_txx_cfg0.s.lane_ena = lane_mask;
+	csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface), ilk_txx_cfg0.u64);
+	csr_wr_node(node, CVMX_ILK_RXX_CFG0(interface), ilk_rxx_cfg0.u64);
+
+	/* For 10.3125Gbs data rate, set SER_LIMIT to 0x3ff for x8 & x12 mode */
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		cvmx_gserx_lane_mode_t lmode0, lmode1;
+
+		lmode0.u64 = csr_rd_node(node, CVMX_GSERX_LANE_MODE(5));
+		lmode1.u64 = csr_rd_node(node, CVMX_GSERX_LANE_MODE(7));
+		if ((lmode0.s.lmode == 5 || lmode1.s.lmode == 5) &&
+		    (lane_mask == 0xfff || lane_mask == 0xfff0 ||
+		     lane_mask == 0xff || lane_mask == 0xff00)) {
+			cvmx_ilk_txx_cfg1_t ilk_txx_cfg1;
+
+			ilk_txx_cfg1.u64 =
+				csr_rd_node(node, CVMX_ILK_TXX_CFG1(interface));
+			ilk_txx_cfg1.s.ser_limit = 0x3ff;
+			csr_wr_node(node, CVMX_ILK_TXX_CFG1(interface),
+				    ilk_txx_cfg1.u64);
+		}
+	}
+
+	/* write to local cache. for lane speed, if interface 0 has 8 lanes,
+	 * assume both qlms have the same speed
+	 */
+	cvmx_ilk_intf_cfg[node][interface].intf_en = 1;
+	res = 0;
+
+	return res;
+}
+
+/**
+ * set pipe group base and length for the interface
+ *
+ * @param xiface    The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param pipe_base the base of the pipe group
+ * @param pipe_len  the length of the pipe group
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_set_pipe(int xiface, int pipe_base, unsigned int pipe_len)
+{
+	int res = -1;
+	cvmx_ilk_txx_pipe_t ilk_txx_pipe;
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int interface = xi.interface - CVMX_ILK_GBL_BASE();
+
+	if (!(OCTEON_IS_MODEL(OCTEON_CN68XX)))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	/* set them in ilk tx section */
+	ilk_txx_pipe.u64 = csr_rd_node(xi.node, CVMX_ILK_TXX_PIPE(interface));
+	ilk_txx_pipe.s.base = pipe_base;
+	ilk_txx_pipe.s.nump = pipe_len;
+	csr_wr_node(xi.node, CVMX_ILK_TXX_PIPE(interface), ilk_txx_pipe.u64);
+	res = 0;
+
+	return res;
+}
+
+/**
+ * set logical channels for tx
+ *
+ * @param interface The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param pch     pointer to an array of pipe-channel pair
+ * @param num_chs the number of entries in the pipe-channel array
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_tx_set_channel(int interface, cvmx_ilk_pipe_chan_t *pch,
+			    unsigned int num_chs)
+{
+	int res = -1;
+	cvmx_ilk_txx_idx_pmap_t ilk_txx_idx_pmap;
+	cvmx_ilk_txx_mem_pmap_t ilk_txx_mem_pmap;
+	unsigned int i;
+
+	if (!(OCTEON_IS_MODEL(OCTEON_CN68XX)))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	if (!pch || num_chs > CVMX_ILK_MAX_PIPES)
+		return res;
+
+	if (cvmx_ilk_use_la_mode(interface, 0)) {
+		ilk_txx_idx_pmap.u64 = 0;
+		ilk_txx_mem_pmap.u64 = 0;
+		for (i = 0; i < num_chs; i++) {
+			ilk_txx_idx_pmap.s.index = pch->pipe;
+			ilk_txx_mem_pmap.s.channel = pch->chan;
+			ilk_txx_mem_pmap.s.remap = 1;
+			csr_wr(CVMX_ILK_TXX_IDX_PMAP(interface),
+			       ilk_txx_idx_pmap.u64);
+			csr_wr(CVMX_ILK_TXX_MEM_PMAP(interface),
+			       ilk_txx_mem_pmap.u64);
+			pch++;
+		}
+	} else {
+		/* write the pair to ilk tx */
+		ilk_txx_mem_pmap.u64 = 0;
+		ilk_txx_idx_pmap.u64 = 0;
+		for (i = 0; i < num_chs; i++) {
+			ilk_txx_idx_pmap.s.index = pch->pipe;
+			ilk_txx_mem_pmap.s.channel = pch->chan;
+			csr_wr(CVMX_ILK_TXX_IDX_PMAP(interface),
+			       ilk_txx_idx_pmap.u64);
+			csr_wr(CVMX_ILK_TXX_MEM_PMAP(interface),
+			       ilk_txx_mem_pmap.u64);
+			pch++;
+		}
+	}
+	res = 0;
+
+	return res;
+}
+
+/**
+ * set pkind for rx
+ *
+ * @param xiface    The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param chpknd    pointer to an array of channel-pkind pair
+ * @param num_pknd the number of entries in the channel-pkind array
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_rx_set_pknd(int xiface, cvmx_ilk_chan_pknd_t *chpknd,
+			 unsigned int num_pknd)
+{
+	int res = -1;
+	cvmx_ilk_rxf_idx_pmap_t ilk_rxf_idx_pmap;
+	unsigned int i;
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int interface = xi.interface - CVMX_ILK_GBL_BASE();
+
+	if (!(OCTEON_IS_MODEL(OCTEON_CN68XX)))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	if (!chpknd || num_pknd > CVMX_ILK_MAX_PKNDS)
+		return res;
+
+	res = 0;
+
+	for (i = 0; i < num_pknd; i++) {
+		ilk_rxf_idx_pmap.u64 = 0;
+		/* write the pair to ilk rx. note the channels for different
+		 * interfaces are given in *chpknd and interface is not used
+		 * as a param
+		 */
+		if (chpknd->chan < 2 &&
+		    cvmx_ilk_use_la_mode(interface, chpknd->chan)) {
+			ilk_rxf_idx_pmap.s.index =
+				interface * 256 + 128 + chpknd->chan;
+			csr_wr(CVMX_ILK_RXF_IDX_PMAP, ilk_rxf_idx_pmap.u64);
+			csr_wr(CVMX_ILK_RXF_MEM_PMAP, chpknd->pknd);
+		}
+		ilk_rxf_idx_pmap.s.index = interface * 256 + chpknd->chan;
+		csr_wr(CVMX_ILK_RXF_IDX_PMAP, ilk_rxf_idx_pmap.u64);
+		csr_wr(CVMX_ILK_RXF_MEM_PMAP, chpknd->pknd);
+		chpknd++;
+	}
+
+	return res;
+}
+
+/**
+ * configure calendar for rx
+ *
+ * @param intf The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param cal_depth the number of calendar entries
+ * @param pent      pointer to calendar entries
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_rx_cal_conf(int intf, int cal_depth, cvmx_ilk_cal_entry_t *pent)
+{
+	int res = -1, i;
+	cvmx_ilk_rxx_cfg0_t ilk_rxx_cfg0;
+	int num_entries;
+	int node = (intf >> 4) & 0xf;
+	int interface = intf & 0xf;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	if (cal_depth < CVMX_ILK_RX_MIN_CAL || cal_depth > CVMX_ILK_MAX_CAL ||
+	    (OCTEON_IS_MODEL(OCTEON_CN68XX) && !pent))
+		return res;
+
+	/* mandatory link-level fc as workarounds for ILK-15397 and
+	 * ILK-15479
+	 */
+	/* TODO: test effectiveness */
+
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		/* Update the calendar for each channel */
+		if ((cvmx_ilk_use_la_mode(interface, 0) == 0) ||
+		    (cvmx_ilk_use_la_mode(interface, 0) &&
+		     cvmx_ilk_la_mode_enable_rx_calendar(interface))) {
+			for (i = 0; i < cal_depth; i++) {
+				__cvmx_ilk_write_rx_cal_entry(
+					interface, i, pent[i].pipe_bpid);
+			}
+		}
+
+		/* Update the depth */
+		ilk_rxx_cfg0.u64 = csr_rd(CVMX_ILK_RXX_CFG0(interface));
+		num_entries = 1 + cal_depth + (cal_depth - 1) / 15;
+		ilk_rxx_cfg0.s.cal_depth = num_entries;
+		if (cvmx_ilk_use_la_mode(interface, 0)) {
+			ilk_rxx_cfg0.s.mproto_ign = 1;
+			ilk_rxx_cfg0.s.lnk_stats_ena = 1;
+			ilk_rxx_cfg0.s.lnk_stats_wrap = 1;
+		}
+		csr_wr(CVMX_ILK_RXX_CFG0(interface), ilk_rxx_cfg0.u64);
+	}
+
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		ilk_rxx_cfg0.u64 =
+			csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+		/*
+		 * Make sure cal_ena is 0 for programming the calendar table,
+		 * as per Errata ILK-19398
+		 */
+		ilk_rxx_cfg0.s.cal_ena = 0;
+		csr_wr_node(node, CVMX_ILK_RXX_CFG0(interface),
+			    ilk_rxx_cfg0.u64);
+
+		for (i = 0; i < cal_depth; i++)
+			__cvmx_ilk_write_rx_cal_entry(intf, i, 0);
+
+		ilk_rxx_cfg0.u64 =
+			csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+		num_entries = 1 + cal_depth + (cal_depth - 1) / 15;
+		ilk_rxx_cfg0.s.cal_depth = num_entries;
+		csr_wr_node(node, CVMX_ILK_RXX_CFG0(interface),
+			    ilk_rxx_cfg0.u64);
+	}
+
+	return 0;
+}
+
+/**
+ * set high water mark for rx
+ *
+ * @param intf      The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param hi_wm     high water mark for this interface
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_rx_set_hwm(int intf, int hi_wm)
+{
+	int res = -1;
+	cvmx_ilk_rxx_cfg1_t ilk_rxx_cfg1;
+	int node = (intf >> 4) & 0xf;
+	int interface = intf & 0xf;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	if (hi_wm <= 0)
+		return res;
+
+	/* set the hwm */
+	ilk_rxx_cfg1.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG1(interface));
+	ilk_rxx_cfg1.s.rx_fifo_hwm = hi_wm;
+	csr_wr_node(node, CVMX_ILK_RXX_CFG1(interface), ilk_rxx_cfg1.u64);
+	res = 0;
+
+	return res;
+}
+
+/**
+ * enable calendar for rx
+ *
+ * @param intf      The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param cal_ena   enable or disable calendar
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_rx_cal_ena(int intf, unsigned char cal_ena)
+{
+	int res = -1;
+	cvmx_ilk_rxx_cfg0_t ilk_rxx_cfg0;
+	int node = (intf >> 4) & 0xf;
+	int interface = intf & 0xf;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	if (cvmx_ilk_use_la_mode(interface, 0) &&
+	    !cvmx_ilk_la_mode_enable_rx_calendar(interface))
+		return 0;
+
+	/* set the enable */
+	ilk_rxx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+	ilk_rxx_cfg0.s.cal_ena = cal_ena;
+	csr_wr_node(node, CVMX_ILK_RXX_CFG0(interface), ilk_rxx_cfg0.u64);
+	csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+	res = 0;
+
+	return res;
+}
+
+/**
+ * set up calendar for rx
+ *
+ * @param intf      The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param cal_depth the number of calendar entries
+ * @param pent      pointer to calendar entries
+ * @param hi_wm     high water mark for this interface
+ * @param cal_ena   enable or disable calendar
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_cal_setup_rx(int intf, int cal_depth, cvmx_ilk_cal_entry_t *pent,
+			  int hi_wm, unsigned char cal_ena)
+{
+	int res = -1;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	res = cvmx_ilk_rx_cal_conf(intf, cal_depth, pent);
+	if (res < 0)
+		return res;
+
+	res = cvmx_ilk_rx_set_hwm(intf, hi_wm);
+	if (res < 0)
+		return res;
+
+	res = cvmx_ilk_rx_cal_ena(intf, cal_ena);
+	return res;
+}
+
+/**
+ * configure calendar for tx
+ *
+ * @param intf      The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param cal_depth the number of calendar entries
+ * @param pent      pointer to calendar entries
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_tx_cal_conf(int intf, int cal_depth, cvmx_ilk_cal_entry_t *pent)
+{
+	int res = -1, i;
+	cvmx_ilk_txx_cfg0_t ilk_txx_cfg0;
+	int num_entries;
+	int node = (intf >> 4) & 0xf;
+	int interface = intf & 0xf;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	if (cal_depth < CVMX_ILK_TX_MIN_CAL || cal_depth > CVMX_ILK_MAX_CAL ||
+	    (OCTEON_IS_MODEL(OCTEON_CN68XX) && !pent))
+		return res;
+
+	/* mandatory link-level fc as workarounds for ILK-15397 and
+	 * ILK-15479
+	 */
+	/* TODO: test effectiveness */
+
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		/* Update the calendar for each channel */
+		for (i = 0; i < cal_depth; i++) {
+			__cvmx_ilk_write_tx_cal_entry(interface, i,
+						      pent[i].pipe_bpid);
+		}
+
+		/* Set the depth (must be multiple of 8)*/
+		ilk_txx_cfg0.u64 = csr_rd(CVMX_ILK_TXX_CFG0(interface));
+		num_entries = 1 + cal_depth + (cal_depth - 1) / 15;
+		ilk_txx_cfg0.s.cal_depth = (num_entries + 7) & ~7;
+		csr_wr(CVMX_ILK_TXX_CFG0(interface), ilk_txx_cfg0.u64);
+	}
+
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		ilk_txx_cfg0.u64 =
+			csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+		/*
+		 * Make sure cal_ena is 0 for programming the calendar table,
+		 * as per Errata ILK-19398
+		 */
+		ilk_txx_cfg0.s.cal_ena = 0;
+		csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface),
+			    ilk_txx_cfg0.u64);
+
+		for (i = 0; i < cal_depth; i++)
+			__cvmx_ilk_write_tx_cal_entry(intf, i, 0);
+
+		ilk_txx_cfg0.u64 =
+			csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+		num_entries = 1 + cal_depth + (cal_depth - 1) / 15;
+		/* cal_depth[2:0] needs to be zero, round up */
+		ilk_txx_cfg0.s.cal_depth = (num_entries + 7) & 0x1f8;
+		csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface),
+			    ilk_txx_cfg0.u64);
+	}
+
+	return 0;
+}
+
+/**
+ * enable calendar for tx
+ *
+ * @param intf	    The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param cal_ena   enable or disable calendar
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_tx_cal_ena(int intf, unsigned char cal_ena)
+{
+	int res = -1;
+	cvmx_ilk_txx_cfg0_t ilk_txx_cfg0;
+	int node = (intf >> 4) & 0xf;
+	int interface = intf & 0xf;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	/* set the enable */
+	ilk_txx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+	ilk_txx_cfg0.s.cal_ena = cal_ena;
+	csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface), ilk_txx_cfg0.u64);
+	csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+	res = 0;
+
+	return res;
+}
+
+/**
+ * set up calendar for tx
+ *
+ * @param intf      The identifier of the packet interface to configure and
+ *                  use as a ILK interface. cn68xx has 2 interfaces: ilk0 and
+ *                  ilk1.
+ *
+ * @param cal_depth the number of calendar entries
+ * @param pent      pointer to calendar entries
+ * @param cal_ena   enable or disable calendar
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_cal_setup_tx(int intf, int cal_depth, cvmx_ilk_cal_entry_t *pent,
+			  unsigned char cal_ena)
+{
+	int res = -1;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	res = cvmx_ilk_tx_cal_conf(intf, cal_depth, pent);
+	if (res < 0)
+		return res;
+
+	res = cvmx_ilk_tx_cal_ena(intf, cal_ena);
+	return res;
+}
+
+/* #define CVMX_ILK_STATS_ENA 1 */
+#ifdef CVMX_ILK_STATS_ENA
+static void cvmx_ilk_reg_dump_rx(int intf)
+{
+	int i;
+	cvmx_ilk_rxx_cfg0_t ilk_rxx_cfg0;
+	cvmx_ilk_rxx_cfg1_t ilk_rxx_cfg1;
+	cvmx_ilk_rxx_int_t ilk_rxx_int;
+	cvmx_ilk_rxx_jabber_t ilk_rxx_jabber;
+	cvmx_ilk_rx_lnex_cfg_t ilk_rx_lnex_cfg;
+	cvmx_ilk_rx_lnex_int_t ilk_rx_lnex_int;
+	cvmx_ilk_gbl_cfg_t ilk_gbl_cfg;
+	cvmx_ilk_ser_cfg_t ilk_ser_cfg;
+	cvmx_ilk_rxf_idx_pmap_t ilk_rxf_idx_pmap;
+	cvmx_ilk_rxf_mem_pmap_t ilk_rxf_mem_pmap;
+	cvmx_ilk_rxx_idx_cal_t ilk_rxx_idx_cal;
+	cvmx_ilk_rxx_mem_cal0_t ilk_rxx_mem_cal0;
+	cvmx_ilk_rxx_mem_cal1_t ilk_rxx_mem_cal1;
+	int node = (intf >> 4) & 0xf;
+	int interface = intf & 0xf;
+
+	ilk_rxx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+	debug("ilk rxx cfg0: 0x%16lx\n", ilk_rxx_cfg0.u64);
+
+	ilk_rxx_cfg1.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG1(interface));
+	debug("ilk rxx cfg1: 0x%16lx\n", ilk_rxx_cfg1.u64);
+
+	ilk_rxx_int.u64 = csr_rd_node(node, CVMX_ILK_RXX_INT(interface));
+	debug("ilk rxx int: 0x%16lx\n", ilk_rxx_int.u64);
+	csr_wr_node(node, CVMX_ILK_RXX_INT(interface), ilk_rxx_int.u64);
+
+	ilk_rxx_jabber.u64 = csr_rd_node(node, CVMX_ILK_RXX_JABBER(interface));
+	debug("ilk rxx jabber: 0x%16lx\n", ilk_rxx_jabber.u64);
+
+#define LNE_NUM_DBG 4
+	for (i = 0; i < LNE_NUM_DBG; i++) {
+		ilk_rx_lnex_cfg.u64 =
+			csr_rd_node(node, CVMX_ILK_RX_LNEX_CFG(i));
+		debug("ilk rx lnex cfg lane: %d  0x%16lx\n", i,
+		      ilk_rx_lnex_cfg.u64);
+	}
+
+	for (i = 0; i < LNE_NUM_DBG; i++) {
+		ilk_rx_lnex_int.u64 =
+			csr_rd_node(node, CVMX_ILK_RX_LNEX_INT(i));
+		debug("ilk rx lnex int lane: %d  0x%16lx\n", i,
+		      ilk_rx_lnex_int.u64);
+		csr_wr_node(node, CVMX_ILK_RX_LNEX_INT(i), ilk_rx_lnex_int.u64);
+	}
+
+	ilk_gbl_cfg.u64 = csr_rd_node(node, CVMX_ILK_GBL_CFG);
+	debug("ilk gbl cfg: 0x%16lx\n", ilk_gbl_cfg.u64);
+
+	ilk_ser_cfg.u64 = csr_rd_node(node, CVMX_ILK_SER_CFG);
+	debug("ilk ser cfg: 0x%16lx\n", ilk_ser_cfg.u64);
+
+#define CHAN_NUM_DBG 8
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		ilk_rxf_idx_pmap.u64 = 0;
+		ilk_rxf_idx_pmap.s.index = interface * 256;
+		ilk_rxf_idx_pmap.s.inc = 1;
+		csr_wr(CVMX_ILK_RXF_IDX_PMAP, ilk_rxf_idx_pmap.u64);
+		for (i = 0; i < CHAN_NUM_DBG; i++) {
+			ilk_rxf_mem_pmap.u64 = csr_rd(CVMX_ILK_RXF_MEM_PMAP);
+			debug("ilk rxf mem pmap chan: %3d  0x%16lx\n", i,
+			      ilk_rxf_mem_pmap.u64);
+		}
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		cvmx_ilk_rxx_chax_t rxx_chax;
+
+		for (i = 0; i < CHAN_NUM_DBG; i++) {
+			rxx_chax.u64 = csr_rd_node(
+				node, CVMX_ILK_RXX_CHAX(i, interface));
+			debug("ilk chan: %d  pki chan: 0x%x\n", i,
+			      rxx_chax.s.port_kind);
+		}
+	}
+
+#define CAL_NUM_DBG 2
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		ilk_rxx_idx_cal.u64 = 0;
+		ilk_rxx_idx_cal.s.inc = 1;
+		csr_wr(CVMX_ILK_RXX_IDX_CAL(interface), ilk_rxx_idx_cal.u64);
+		for (i = 0; i < CAL_NUM_DBG; i++) {
+			ilk_rxx_idx_cal.u64 =
+				csr_rd(CVMX_ILK_RXX_IDX_CAL(interface));
+			debug("ilk rxx idx cal: 0x%16lx\n",
+			      ilk_rxx_idx_cal.u64);
+
+			ilk_rxx_mem_cal0.u64 =
+				csr_rd(CVMX_ILK_RXX_MEM_CAL0(interface));
+			debug("ilk rxx mem cal0: 0x%16lx\n",
+			      ilk_rxx_mem_cal0.u64);
+			ilk_rxx_mem_cal1.u64 =
+				csr_rd(CVMX_ILK_RXX_MEM_CAL1(interface));
+			debug("ilk rxx mem cal1: 0x%16lx\n",
+			      ilk_rxx_mem_cal1.u64);
+		}
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		cvmx_ilk_rxx_cal_entryx_t rxx_cal_entryx;
+
+		for (i = 0; i < CAL_NUM_DBG; i++) {
+			rxx_cal_entryx.u64 = csr_rd_node(
+				node, CVMX_ILK_RXX_CAL_ENTRYX(i, interface));
+			debug("ilk rxx cal idx: %d\n", i);
+			debug("ilk rxx cal ctl: 0x%x\n", rxx_cal_entryx.s.ctl);
+			debug("ilk rxx cal pko chan: 0x%x\n",
+			      rxx_cal_entryx.s.channel);
+		}
+	}
+}
+
+static void cvmx_ilk_reg_dump_tx(int intf)
+{
+	int i;
+	cvmx_ilk_txx_cfg0_t ilk_txx_cfg0;
+	cvmx_ilk_txx_cfg1_t ilk_txx_cfg1;
+	cvmx_ilk_txx_idx_pmap_t ilk_txx_idx_pmap;
+	cvmx_ilk_txx_mem_pmap_t ilk_txx_mem_pmap;
+	cvmx_ilk_txx_int_t ilk_txx_int;
+	cvmx_ilk_txx_pipe_t ilk_txx_pipe;
+	cvmx_ilk_txx_idx_cal_t ilk_txx_idx_cal;
+	cvmx_ilk_txx_mem_cal0_t ilk_txx_mem_cal0;
+	cvmx_ilk_txx_mem_cal1_t ilk_txx_mem_cal1;
+	int node = (intf >> 4) & 0xf;
+	int interface = intf & 0xf;
+
+	ilk_txx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+	debug("ilk txx cfg0: 0x%16lx\n", ilk_txx_cfg0.u64);
+
+	ilk_txx_cfg1.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG1(interface));
+	debug("ilk txx cfg1: 0x%16lx\n", ilk_txx_cfg1.u64);
+
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		ilk_txx_pipe.u64 = csr_rd(CVMX_ILK_TXX_PIPE(interface));
+		debug("ilk txx pipe: 0x%16lx\n", ilk_txx_pipe.u64);
+
+		ilk_txx_idx_pmap.u64 = 0;
+		ilk_txx_idx_pmap.s.index = ilk_txx_pipe.s.base;
+		ilk_txx_idx_pmap.s.inc = 1;
+		csr_wr(CVMX_ILK_TXX_IDX_PMAP(interface), ilk_txx_idx_pmap.u64);
+		for (i = 0; i < CHAN_NUM_DBG; i++) {
+			ilk_txx_mem_pmap.u64 =
+				csr_rd(CVMX_ILK_TXX_MEM_PMAP(interface));
+			debug("ilk txx mem pmap pipe: %3d  0x%16lx\n",
+			      ilk_txx_pipe.s.base + i, ilk_txx_mem_pmap.u64);
+		}
+	}
+
+	ilk_txx_int.u64 = csr_rd_node(node, CVMX_ILK_TXX_INT(interface));
+	debug("ilk txx int: 0x%16lx\n", ilk_txx_int.u64);
+
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		ilk_txx_idx_cal.u64 = 0;
+		ilk_txx_idx_cal.s.inc = 1;
+		csr_wr(CVMX_ILK_TXX_IDX_CAL(interface), ilk_txx_idx_cal.u64);
+		for (i = 0; i < CAL_NUM_DBG; i++) {
+			ilk_txx_idx_cal.u64 =
+				csr_rd(CVMX_ILK_TXX_IDX_CAL(interface));
+			debug("ilk txx idx cal: 0x%16lx\n",
+			      ilk_txx_idx_cal.u64);
+
+			ilk_txx_mem_cal0.u64 =
+				csr_rd(CVMX_ILK_TXX_MEM_CAL0(interface));
+			debug("ilk txx mem cal0: 0x%16lx\n",
+			      ilk_txx_mem_cal0.u64);
+			ilk_txx_mem_cal1.u64 =
+				csr_rd(CVMX_ILK_TXX_MEM_CAL1(interface));
+			debug("ilk txx mem cal1: 0x%16lx\n",
+			      ilk_txx_mem_cal1.u64);
+		}
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		cvmx_ilk_txx_cal_entryx_t txx_cal_entryx;
+
+		for (i = 0; i < CAL_NUM_DBG; i++) {
+			txx_cal_entryx.u64 = csr_rd_node(
+				node, CVMX_ILK_TXX_CAL_ENTRYX(i, interface));
+			debug("ilk txx cal idx: %d\n", i);
+			debug("ilk txx cal ctl: 0x%x\n", txx_cal_entryx.s.ctl);
+			debug("ilk txx cal pki chan: 0x%x\n",
+			      txx_cal_entryx.s.channel);
+		}
+	}
+}
+#endif
+
+/**
+ * show run time status
+ *
+ * @param interface The identifier of the packet interface to enable. cn68xx
+ *                  has 2 interfaces: ilk0 and ilk1.
+ *
+ * @return nothing
+ */
+#ifdef CVMX_ILK_RUNTIME_DBG
+void cvmx_ilk_runtime_status(int interface)
+{
+	cvmx_ilk_txx_cfg1_t ilk_txx_cfg1;
+	cvmx_ilk_txx_flow_ctl0_t ilk_txx_flow_ctl0;
+	cvmx_ilk_rxx_cfg1_t ilk_rxx_cfg1;
+	cvmx_ilk_rxx_int_t ilk_rxx_int;
+	cvmx_ilk_rxx_flow_ctl0_t ilk_rxx_flow_ctl0;
+	cvmx_ilk_rxx_flow_ctl1_t ilk_rxx_flow_ctl1;
+	cvmx_ilk_gbl_int_t ilk_gbl_int;
+
+	debug("\nilk run-time status: interface: %d\n", interface);
+
+	ilk_txx_cfg1.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG1(interface));
+	debug("\nilk txx cfg1: 0x%16lx\n", ilk_txx_cfg1.u64);
+	if (ilk_txx_cfg1.s.rx_link_fc)
+		debug("link flow control received\n");
+	if (ilk_txx_cfg1.s.tx_link_fc)
+		debug("link flow control sent\n");
+
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		ilk_txx_flow_ctl0.u64 =
+			csr_rd(CVMX_ILK_TXX_FLOW_CTL0(interface));
+		debug("\nilk txx flow ctl0: 0x%16lx\n", ilk_txx_flow_ctl0.u64);
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		int i;
+		cvmx_ilk_txx_cha_xonx_t txx_cha_xonx;
+
+		for (i = 0; i < 4; i++) {
+			txx_cha_xonx.u64 = csr_rd_node(
+				node, CVMX_ILK_TXX_CHA_XONX(i, interface));
+			debug("\nilk txx cha xon: 0x%16lx\n", txx_cha_xonx.u64);
+		}
+	}
+
+	ilk_rxx_cfg1.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG1(interface));
+	debug("\nilk rxx cfg1: 0x%16lx\n", ilk_rxx_cfg1.u64);
+	debug("rx fifo count: %d\n", ilk_rxx_cfg1.s.rx_fifo_cnt);
+
+	ilk_rxx_int.u64 = csr_rd_node(node, CVMX_ILK_RXX_INT(interface));
+	debug("\nilk rxx int: 0x%16lx\n", ilk_rxx_int.u64);
+	if (ilk_rxx_int.s.pkt_drop_rxf)
+		debug("rx fifo packet drop\n");
+	if (ilk_rxx_int.u64)
+		csr_wr_node(node, CVMX_ILK_RXX_INT(interface), ilk_rxx_int.u64);
+
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		ilk_rxx_flow_ctl0.u64 =
+			csr_rd(CVMX_ILK_RXX_FLOW_CTL0(interface));
+		debug("\nilk rxx flow ctl0: 0x%16lx\n", ilk_rxx_flow_ctl0.u64);
+
+		ilk_rxx_flow_ctl1.u64 =
+			csr_rd(CVMX_ILK_RXX_FLOW_CTL1(interface));
+		debug("\nilk rxx flow ctl1: 0x%16lx\n", ilk_rxx_flow_ctl1.u64);
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		int i;
+		cvmx_ilk_rxx_cha_xonx_t rxx_cha_xonx;
+
+		for (i = 0; i < 4; i++) {
+			rxx_cha_xonx.u64 = csr_rd_node(
+				node, CVMX_ILK_RXX_CHA_XONX(i, interface));
+			debug("\nilk rxx cha xon: 0x%16lx\n", rxx_cha_xonx.u64);
+		}
+	}
+
+	ilk_gbl_int.u64 = csr_rd_node(node, CVMX_ILK_GBL_INT);
+	debug("\nilk gbl int: 0x%16lx\n", ilk_gbl_int.u64);
+	if (ilk_gbl_int.s.rxf_push_full)
+		debug("rx fifo overflow\n");
+	if (ilk_gbl_int.u64)
+		csr_wr_node(node, CVMX_ILK_GBL_INT, ilk_gbl_int.u64);
+}
+#endif
+
+/**
+ * enable interface
+ *
+ * @param xiface    The identifier of the packet interface to enable. cn68xx
+ *                  has 2 interfaces: ilk0 and ilk1.
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_enable(int xiface)
+{
+	int res = -1;
+	int retry_count = 0;
+	cvmx_helper_link_info_t result;
+	cvmx_ilk_txx_cfg1_t ilk_txx_cfg1;
+	cvmx_ilk_rxx_cfg1_t ilk_rxx_cfg1;
+#ifdef CVMX_ILK_STATS_ENA
+	cvmx_ilk_rxx_cfg0_t ilk_rxx_cfg0;
+	cvmx_ilk_txx_cfg0_t ilk_txx_cfg0;
+#endif
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int node = xi.node;
+	int interface = xi.interface - CVMX_ILK_GBL_BASE();
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	result.u64 = 0;
+
+#ifdef CVMX_ILK_STATS_ENA
+	debug("\n");
+	debug("<<<< ILK%d: Before enabling ilk\n", interface);
+	cvmx_ilk_reg_dump_rx(intf);
+	cvmx_ilk_reg_dump_tx(intf);
+#endif
+
+	/* RX packet will be enabled only if link is up */
+
+	/* TX side */
+	ilk_txx_cfg1.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG1(interface));
+	ilk_txx_cfg1.s.pkt_ena = 1;
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		if (cvmx_ilk_use_la_mode(interface, 0)) {
+			ilk_txx_cfg1.s.la_mode = 1;
+			ilk_txx_cfg1.s.tx_link_fc_jam = 1;
+		}
+	}
+	csr_wr_node(node, CVMX_ILK_TXX_CFG1(interface), ilk_txx_cfg1.u64);
+	csr_rd_node(node, CVMX_ILK_TXX_CFG1(interface));
+
+#ifdef CVMX_ILK_STATS_ENA
+	/* RX side stats */
+	ilk_rxx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+	ilk_rxx_cfg0.s.lnk_stats_ena = 1;
+	csr_wr_node(node, CVMX_ILK_RXX_CFG0(interface), ilk_rxx_cfg0.u64);
+
+	/* TX side stats */
+	ilk_txx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+	ilk_txx_cfg0.s.lnk_stats_ena = 1;
+	csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface), ilk_txx_cfg0.u64);
+#endif
+
+retry:
+	retry_count++;
+	if (retry_count > 10)
+		goto out;
+
+	/* Make sure the link is up, so that packets can be sent. */
+	result = __cvmx_helper_ilk_link_get(
+		cvmx_helper_get_ipd_port((interface + CVMX_ILK_GBL_BASE()), 0));
+
+	/* Small delay before another retry. */
+	udelay(100);
+
+	ilk_rxx_cfg1.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG1(interface));
+	if (ilk_rxx_cfg1.s.pkt_ena == 0)
+		goto retry;
+
+out:
+
+#ifdef CVMX_ILK_STATS_ENA
+	debug(">>>> ILK%d: After ILK is enabled\n", interface);
+	cvmx_ilk_reg_dump_rx(intf);
+	cvmx_ilk_reg_dump_tx(intf);
+#endif
+
+	if (result.s.link_up)
+		return 0;
+
+	return -1;
+}
+
+/**
+ * Disable interface
+ *
+ * @param intf      The identifier of the packet interface to disable. cn68xx
+ *                  has 2 interfaces: ilk0 and ilk1.
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_disable(int intf)
+{
+	int res = -1;
+	cvmx_ilk_txx_cfg1_t ilk_txx_cfg1;
+	cvmx_ilk_rxx_cfg1_t ilk_rxx_cfg1;
+#ifdef CVMX_ILK_STATS_ENA
+	cvmx_ilk_rxx_cfg0_t ilk_rxx_cfg0;
+	cvmx_ilk_txx_cfg0_t ilk_txx_cfg0;
+#endif
+	int node = (intf >> 4) & 0xf;
+	int interface = intf & 0xf;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	/* TX side */
+	ilk_txx_cfg1.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG1(interface));
+	ilk_txx_cfg1.s.pkt_ena = 0;
+	csr_wr_node(node, CVMX_ILK_TXX_CFG1(interface), ilk_txx_cfg1.u64);
+
+	/* RX side */
+	ilk_rxx_cfg1.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG1(interface));
+	ilk_rxx_cfg1.s.pkt_ena = 0;
+	csr_wr_node(node, CVMX_ILK_RXX_CFG1(interface), ilk_rxx_cfg1.u64);
+
+#ifdef CVMX_ILK_STATS_ENA
+	/* RX side stats */
+	ilk_rxx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+	ilk_rxx_cfg0.s.lnk_stats_ena = 0;
+	csr_wr_node(node, CVMX_ILK_RXX_CFG0(interface), ilk_rxx_cfg0.u64);
+
+	/* RX side stats */
+	ilk_txx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+	ilk_txx_cfg0.s.lnk_stats_ena = 0;
+	csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface), ilk_txx_cfg0.u64);
+#endif
+
+	return 0;
+}
+
+/**
+ * Provide interface enable status
+ *
+ * @param xiface The identifier of the packet xiface to disable. cn68xx
+ *                  has 2 interfaces: ilk0 and ilk1.
+ *
+ * @return Zero, not enabled; One, enabled.
+ */
+int cvmx_ilk_get_intf_ena(int xiface)
+{
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int interface = xi.interface - CVMX_ILK_GBL_BASE();
+	return cvmx_ilk_intf_cfg[xi.node][interface].intf_en;
+}
+
+/**
+ * Enable or disable LA mode in ILK header.
+ * For normal ILK mode, enable CRC and skip = 0.
+ * For ILK LA mode, disable CRC and set skip to size of ILK header.
+ *
+ * @param ipd_port   IPD port of the ILK header
+ * @param mode   If set, enable LA mode in ILK header, else disable
+ *
+ * @return ILK header
+ */
+cvmx_ilk_la_nsp_compact_hdr_t cvmx_ilk_enable_la_header(int ipd_port, int mode)
+{
+	cvmx_ilk_la_nsp_compact_hdr_t ilk_header;
+	cvmx_pip_prt_cfgx_t pip_config;
+	struct cvmx_xport xp = cvmx_helper_ipd_port_to_xport(ipd_port);
+	int xiface = cvmx_helper_get_interface_num(ipd_port);
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+
+	int ilk_interface = xi.interface - CVMX_ILK_GBL_BASE();
+	int skip = 0;
+	int crc = 1;
+	int len_chk = 1;
+
+	ilk_header.u64 = 0;
+
+	if (ilk_interface >= CVMX_NUM_ILK_INTF)
+		debug("ERROR: Invalid interface %d\n", ilk_interface);
+	if (!cvmx_ilk_use_la_mode(ilk_interface, 0))
+		return ilk_header;
+
+	if (mode) {
+		ilk_header.s.la_mode = 1;
+		ilk_header.s.ilk_channel = xp.port & 1;
+		skip = sizeof(ilk_header);
+		crc = 0;
+	}
+	/* There is a bug in the CN68XX pass 2.x where the CRC erroneously is
+	 * computed over the ILK header when it should not be so we ignore it.
+	 */
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX_PASS2_X)) {
+		crc = 0;
+		if (!mode)
+			len_chk = 0;
+		skip = sizeof(ilk_header);
+	}
+
+	/* SKIP ILK header only for first 2 ports */
+	if ((xp.port & 0x7) < 2) {
+		int pknd = cvmx_helper_get_pknd(xiface, xp.port & 1);
+		int ipko_port;
+		cvmx_pko_reg_read_idx_t pko_reg;
+		cvmx_pko_mem_iport_ptrs_t pko_mem_iport;
+		cvmx_pip_sub_pkind_fcsx_t pknd_fcs;
+
+		/* Enable/Disable CRC in IPD and set skip */
+		pip_config.u64 = csr_rd(CVMX_PIP_PRT_CFGX(pknd));
+		pip_config.s.skip = skip;
+		pip_config.s.crc_en = crc;
+		pip_config.s.lenerr_en = len_chk;
+		pip_config.s.minerr_en = (len_chk && !mode);
+		csr_wr(CVMX_PIP_PRT_CFGX(pknd), pip_config.u64);
+
+		pknd_fcs.u64 = csr_rd(CVMX_PIP_SUB_PKIND_FCSX(0));
+		pknd_fcs.s.port_bit &= ~(1ull << pknd);
+		csr_wr(CVMX_PIP_SUB_PKIND_FCSX(0), pknd_fcs.u64);
+
+		/* Enable/Disable CRC in PKO */
+
+		/* Get PKO internal port */
+		ipko_port = ilk_interface + 0x1c;
+
+		pko_reg.u64 = csr_rd(CVMX_PKO_REG_READ_IDX);
+		pko_reg.s.index = cvmx_helper_get_pko_port(xiface, xp.port & 1);
+		csr_wr(CVMX_PKO_REG_READ_IDX, pko_reg.u64);
+
+		pko_mem_iport.u64 = csr_rd(CVMX_PKO_MEM_IPORT_PTRS);
+		pko_mem_iport.s.crc = crc;
+		pko_mem_iport.s.intr = ipko_port;
+		csr_wr(CVMX_PKO_MEM_IPORT_PTRS, pko_mem_iport.u64);
+	}
+
+	return ilk_header;
+}
+
+/**
+ * Show channel statistics
+ *
+ * @param interface The identifier of the packet interface to disable. cn68xx
+ *                  has 2 interfaces: ilk0 and ilk1.
+ * @param pstats A pointer to cvmx_ilk_stats_ctrl_t that specifies which
+ *               logical channels to access
+ *
+ * @return nothing
+ */
+void cvmx_ilk_show_stats(int interface, cvmx_ilk_stats_ctrl_t *pstats)
+{
+	unsigned int i;
+	cvmx_ilk_rxx_idx_stat0_t ilk_rxx_idx_stat0;
+	cvmx_ilk_rxx_idx_stat1_t ilk_rxx_idx_stat1;
+	cvmx_ilk_rxx_mem_stat0_t ilk_rxx_mem_stat0;
+	cvmx_ilk_rxx_mem_stat1_t ilk_rxx_mem_stat1;
+
+	cvmx_ilk_txx_idx_stat0_t ilk_txx_idx_stat0;
+	cvmx_ilk_txx_idx_stat1_t ilk_txx_idx_stat1;
+	cvmx_ilk_txx_mem_stat0_t ilk_txx_mem_stat0;
+	cvmx_ilk_txx_mem_stat1_t ilk_txx_mem_stat1;
+	int node = 0;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return;
+
+	if (!pstats)
+		return;
+
+	/* discrete channels */
+	if (pstats->chan_list) {
+		int *chan_list = pstats->chan_list;
+
+		for (i = 0; i < pstats->num_chans; i++) {
+			if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+				/* get the number of rx packets */
+				ilk_rxx_idx_stat0.u64 = 0;
+				ilk_rxx_idx_stat0.s.index = *pstats->chan_list;
+				ilk_rxx_idx_stat0.s.clr = pstats->clr_on_rd;
+				csr_wr(CVMX_ILK_RXX_IDX_STAT0(interface),
+				       ilk_rxx_idx_stat0.u64);
+				ilk_rxx_mem_stat0.u64 = csr_rd(
+					CVMX_ILK_RXX_MEM_STAT0(interface));
+
+				/* get the number of rx bytes */
+				ilk_rxx_idx_stat1.u64 = 0;
+				ilk_rxx_idx_stat1.s.index = *chan_list;
+				ilk_rxx_idx_stat1.s.clr = pstats->clr_on_rd;
+				csr_wr(CVMX_ILK_RXX_IDX_STAT1(interface),
+				       ilk_rxx_idx_stat1.u64);
+				ilk_rxx_mem_stat1.u64 = csr_rd(
+					CVMX_ILK_RXX_MEM_STAT1(interface));
+
+				debug("ILK%d Channel%d Rx: %d packets %d bytes\n",
+				      interface, *chan_list,
+				      ilk_rxx_mem_stat0.s.rx_pkt,
+				      (unsigned int)
+					      ilk_rxx_mem_stat1.s.rx_bytes);
+			}
+			if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+				cvmx_ilk_rxx_pkt_cntx_t rxx_pkt_cntx;
+				cvmx_ilk_rxx_byte_cntx_t rxx_byte_cntx;
+
+				rxx_pkt_cntx.u64 = csr_rd_node(
+					node, CVMX_ILK_RXX_PKT_CNTX(*chan_list,
+								    interface));
+				rxx_byte_cntx.u64 = csr_rd_node(
+					node, CVMX_ILK_RXX_BYTE_CNTX(
+						      *chan_list, interface));
+				debug("ILK%d Channel%d Rx: %llu packets %llu bytes\n",
+				      interface, *chan_list,
+				      (unsigned long long)rxx_pkt_cntx.s.rx_pkt,
+				      (unsigned long long)
+					      rxx_byte_cntx.s.rx_bytes);
+			}
+
+			if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+				/* get the number of tx packets */
+				ilk_txx_idx_stat0.u64 = 0;
+				ilk_txx_idx_stat0.s.index = *chan_list;
+				ilk_txx_idx_stat0.s.clr = pstats->clr_on_rd;
+				csr_wr(CVMX_ILK_TXX_IDX_STAT0(interface),
+				       ilk_txx_idx_stat0.u64);
+				ilk_txx_mem_stat0.u64 = csr_rd(
+					CVMX_ILK_TXX_MEM_STAT0(interface));
+
+				/* get the number of tx bytes */
+				ilk_txx_idx_stat1.u64 = 0;
+				ilk_txx_idx_stat1.s.index = *pstats->chan_list;
+				ilk_txx_idx_stat1.s.clr = pstats->clr_on_rd;
+				csr_wr(CVMX_ILK_TXX_IDX_STAT1(interface),
+				       ilk_txx_idx_stat1.u64);
+				ilk_txx_mem_stat1.u64 = csr_rd(
+					CVMX_ILK_TXX_MEM_STAT1(interface));
+
+				debug("ILK%d Channel%d Tx: %d packets %d bytes\n",
+				      interface, *chan_list,
+				      ilk_txx_mem_stat0.s.tx_pkt,
+				      (unsigned int)
+					      ilk_txx_mem_stat1.s.tx_bytes);
+			}
+			if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+				cvmx_ilk_txx_pkt_cntx_t txx_pkt_cntx;
+				cvmx_ilk_txx_byte_cntx_t txx_byte_cntx;
+
+				txx_pkt_cntx.u64 = csr_rd_node(
+					node, CVMX_ILK_TXX_PKT_CNTX(*chan_list,
+								    interface));
+				txx_byte_cntx.u64 = csr_rd_node(
+					node, CVMX_ILK_TXX_BYTE_CNTX(
+						      *chan_list, interface));
+				debug("ILK%d Channel%d Tx: %llu packets %llu bytes\n",
+				      interface, *chan_list,
+				      (unsigned long long)txx_pkt_cntx.s.tx_pkt,
+				      (unsigned long long)
+					      txx_byte_cntx.s.tx_bytes);
+			}
+
+			chan_list++;
+		}
+		return;
+	}
+
+	/* continuous channels */
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		ilk_rxx_idx_stat0.u64 = 0;
+		ilk_rxx_idx_stat0.s.index = pstats->chan_start;
+		ilk_rxx_idx_stat0.s.inc = pstats->chan_step;
+		ilk_rxx_idx_stat0.s.clr = pstats->clr_on_rd;
+		csr_wr(CVMX_ILK_RXX_IDX_STAT0(interface),
+		       ilk_rxx_idx_stat0.u64);
+
+		ilk_rxx_idx_stat1.u64 = 0;
+		ilk_rxx_idx_stat1.s.index = pstats->chan_start;
+		ilk_rxx_idx_stat1.s.inc = pstats->chan_step;
+		ilk_rxx_idx_stat1.s.clr = pstats->clr_on_rd;
+		csr_wr(CVMX_ILK_RXX_IDX_STAT1(interface),
+		       ilk_rxx_idx_stat1.u64);
+
+		ilk_txx_idx_stat0.u64 = 0;
+		ilk_txx_idx_stat0.s.index = pstats->chan_start;
+		ilk_txx_idx_stat0.s.inc = pstats->chan_step;
+		ilk_txx_idx_stat0.s.clr = pstats->clr_on_rd;
+		csr_wr(CVMX_ILK_TXX_IDX_STAT0(interface),
+		       ilk_txx_idx_stat0.u64);
+
+		ilk_txx_idx_stat1.u64 = 0;
+		ilk_txx_idx_stat1.s.index = pstats->chan_start;
+		ilk_txx_idx_stat1.s.inc = pstats->chan_step;
+		ilk_txx_idx_stat1.s.clr = pstats->clr_on_rd;
+		csr_wr(CVMX_ILK_TXX_IDX_STAT1(interface),
+		       ilk_txx_idx_stat1.u64);
+
+		for (i = pstats->chan_start; i <= pstats->chan_end;
+		     i += pstats->chan_step) {
+			ilk_rxx_mem_stat0.u64 =
+				csr_rd(CVMX_ILK_RXX_MEM_STAT0(interface));
+			ilk_rxx_mem_stat1.u64 =
+				csr_rd(CVMX_ILK_RXX_MEM_STAT1(interface));
+			debug("ILK%d Channel%d Rx: %d packets %d bytes\n",
+			      interface, i, ilk_rxx_mem_stat0.s.rx_pkt,
+			      (unsigned int)ilk_rxx_mem_stat1.s.rx_bytes);
+
+			ilk_txx_mem_stat0.u64 =
+				csr_rd(CVMX_ILK_TXX_MEM_STAT0(interface));
+			ilk_txx_mem_stat1.u64 =
+				csr_rd(CVMX_ILK_TXX_MEM_STAT1(interface));
+			debug("ILK%d Channel%d Tx: %d packets %d bytes\n",
+			      interface, i, ilk_rxx_mem_stat0.s.rx_pkt,
+			      (unsigned int)ilk_rxx_mem_stat1.s.rx_bytes);
+		}
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		for (i = pstats->chan_start; i <= pstats->chan_end;
+		     i += pstats->chan_step) {
+			cvmx_ilk_rxx_pkt_cntx_t rxx_pkt_cntx;
+			cvmx_ilk_rxx_byte_cntx_t rxx_byte_cntx;
+			cvmx_ilk_txx_pkt_cntx_t txx_pkt_cntx;
+			cvmx_ilk_txx_byte_cntx_t txx_byte_cntx;
+
+			rxx_pkt_cntx.u64 = csr_rd_node(
+				node, CVMX_ILK_RXX_PKT_CNTX(i, interface));
+			rxx_byte_cntx.u64 = csr_rd_node(
+				node, CVMX_ILK_RXX_BYTE_CNTX(i, interface));
+			debug("ILK%d Channel%d Rx: %llu packets %llu bytes\n",
+			      interface, i,
+			      (unsigned long long)rxx_pkt_cntx.s.rx_pkt,
+			      (unsigned long long)rxx_byte_cntx.s.rx_bytes);
+
+			txx_pkt_cntx.u64 = csr_rd_node(
+				node, CVMX_ILK_TXX_PKT_CNTX(i, interface));
+			txx_byte_cntx.u64 = csr_rd_node(
+				node, CVMX_ILK_TXX_BYTE_CNTX(i, interface));
+			debug("ILK%d Channel%d Tx: %llu packets %llu bytes\n",
+			      interface, i,
+			      (unsigned long long)txx_pkt_cntx.s.tx_pkt,
+			      (unsigned long long)txx_byte_cntx.s.tx_bytes);
+		}
+	}
+}
+
+/**
+ * enable or disable loopbacks
+ *
+ * @param xiface The identifier of the packet xiface to disable. cn68xx
+ *                  has 2 interfaces: ilk0 and ilk1.
+ * @param enable    Enable or disable loopback
+ * @param mode      Internal or external loopback
+ *
+ * @return Zero on success, negative on failure.
+ */
+int cvmx_ilk_lpbk(int xiface, cvmx_ilk_lpbk_ena_t enable,
+		  cvmx_ilk_lpbk_mode_t mode)
+{
+	int res = -1;
+	cvmx_ilk_txx_cfg0_t ilk_txx_cfg0;
+	cvmx_ilk_rxx_cfg0_t ilk_rxx_cfg0;
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int node = xi.node;
+	int interface = xi.interface - CVMX_ILK_GBL_BASE();
+
+	;
+
+	if (!octeon_has_feature(OCTEON_FEATURE_ILK))
+		return res;
+
+	if (interface >= CVMX_NUM_ILK_INTF)
+		return res;
+
+	/* internal loopback. only 1 type of loopback can be on at 1 time */
+	if (mode == CVMX_ILK_LPBK_INT) {
+		if (enable == CVMX_ILK_LPBK_ENA) {
+			ilk_txx_cfg0.u64 =
+				csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+			ilk_txx_cfg0.s.ext_lpbk = CVMX_ILK_LPBK_DISA;
+			ilk_txx_cfg0.s.ext_lpbk_fc = CVMX_ILK_LPBK_DISA;
+			csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface),
+				    ilk_txx_cfg0.u64);
+
+			ilk_rxx_cfg0.u64 =
+				csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+			ilk_rxx_cfg0.s.ext_lpbk = CVMX_ILK_LPBK_DISA;
+			ilk_rxx_cfg0.s.ext_lpbk_fc = CVMX_ILK_LPBK_DISA;
+			csr_wr_node(node, CVMX_ILK_RXX_CFG0(interface),
+				    ilk_rxx_cfg0.u64);
+		}
+
+		ilk_txx_cfg0.u64 =
+			csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+		ilk_txx_cfg0.s.int_lpbk = enable;
+		csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface),
+			    ilk_txx_cfg0.u64);
+
+		res = 0;
+		return res;
+	}
+
+	/* external loopback. only 1 type of loopback can be on at 1 time */
+	if (enable == CVMX_ILK_LPBK_ENA) {
+		ilk_txx_cfg0.u64 =
+			csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+		ilk_txx_cfg0.s.int_lpbk = CVMX_ILK_LPBK_DISA;
+		csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface),
+			    ilk_txx_cfg0.u64);
+	}
+
+	ilk_txx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_TXX_CFG0(interface));
+	ilk_txx_cfg0.s.ext_lpbk = enable;
+	ilk_txx_cfg0.s.ext_lpbk_fc = enable;
+	csr_wr_node(node, CVMX_ILK_TXX_CFG0(interface), ilk_txx_cfg0.u64);
+
+	ilk_rxx_cfg0.u64 = csr_rd_node(node, CVMX_ILK_RXX_CFG0(interface));
+	ilk_rxx_cfg0.s.ext_lpbk = enable;
+	ilk_rxx_cfg0.s.ext_lpbk_fc = enable;
+	csr_wr_node(node, CVMX_ILK_RXX_CFG0(interface), ilk_rxx_cfg0.u64);
+
+	res = 0;
+	return res;
+}
Signature · 1 line
2.35.1
#28 lore raw

PATCH 31/52 mips: octeon: Add cvmx-ipd.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-ipd.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-ipd.c | 690 +++++++++++++++++++++++++++++++
 1 file changed, 690 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-ipd.c
arch/mips/mach-octeon/cvmx-ipd.c new file +690 -0
new file mode 100644
index 000000000000..0b5aca56e399
@@ -0,0 +1,690 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * IPD Support.
+ */
+
+#include <errno.h>
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+#include <mach/cvmx-range.h>
+#include <mach/cvmx-global-resources.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-ipd.h>
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-pko3.h>
+#include <mach/cvmx-pko3-queue.h>
+#include <mach/cvmx-pko3-resources.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#include <mach/cvmx-helper-bgx.h>
+#include <mach/cvmx-helper-cfg.h>
+#include <mach/cvmx-helper-util.h>
+#include <mach/cvmx-helper-pki.h>
+
+cvmx_ipd_config_t cvmx_ipd_cfg = {
+	.first_mbuf_skip = 184,
+	.ipd_enable = 1,
+	.cache_mode = CVMX_IPD_OPC_MODE_STT,
+	.packet_pool = { 0, 2048, 0 },
+	.wqe_pool = { 1, 128, 0 },
+	.port_config = { CVMX_PIP_PORT_CFG_MODE_SKIPL2,
+			 CVMX_POW_TAG_TYPE_ORDERED, CVMX_PIP_TAG_MODE_TUPLE,
+			 .tag_fields = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } }
+};
+
+#define IPD_RED_AVG_DLY 1000
+#define IPD_RED_PRB_DLY 1000
+
+void cvmx_ipd_convert_to_newcfg(cvmx_ipd_config_t ipd_config)
+{
+	int pkind;
+	unsigned int node = cvmx_get_node_num();
+
+	/*Set all the styles to same parameters since old config does not have per port config*/
+	pki_dflt_style[node].parm_cfg.cache_mode = ipd_config.cache_mode;
+	pki_dflt_style[node].parm_cfg.first_skip = ipd_config.first_mbuf_skip;
+	pki_dflt_style[node].parm_cfg.later_skip =
+		ipd_config.not_first_mbuf_skip;
+	pki_dflt_style[node].parm_cfg.mbuff_size =
+		ipd_config.packet_pool.buffer_size;
+	pki_dflt_style[node].parm_cfg.tag_type =
+		ipd_config.port_config.tag_type;
+
+	pki_dflt_style[node].tag_cfg.tag_fields.layer_c_src =
+		ipd_config.port_config.tag_fields.ipv6_src_ip |
+		ipd_config.port_config.tag_fields.ipv4_src_ip;
+	pki_dflt_style[node].tag_cfg.tag_fields.layer_c_dst =
+		ipd_config.port_config.tag_fields.ipv6_dst_ip |
+		ipd_config.port_config.tag_fields.ipv4_dst_ip;
+	pki_dflt_style[node].tag_cfg.tag_fields.ip_prot_nexthdr =
+		ipd_config.port_config.tag_fields.ipv6_next_header |
+		ipd_config.port_config.tag_fields.ipv4_protocol;
+	pki_dflt_style[node].tag_cfg.tag_fields.layer_f_src =
+		ipd_config.port_config.tag_fields.ipv6_src_port |
+		ipd_config.port_config.tag_fields.ipv4_src_port;
+	pki_dflt_style[node].tag_cfg.tag_fields.layer_f_dst =
+		ipd_config.port_config.tag_fields.ipv6_dst_port |
+		ipd_config.port_config.tag_fields.ipv4_dst_port;
+	pki_dflt_style[node].tag_cfg.tag_fields.input_port =
+		ipd_config.port_config.tag_fields.input_port;
+
+	if (ipd_config.port_config.parse_mode == 0x1)
+		pki_dflt_pkind[node].initial_parse_mode =
+			CVMX_PKI_PARSE_LA_TO_LG;
+	else if (ipd_config.port_config.parse_mode == 0x2)
+		pki_dflt_pkind[node].initial_parse_mode =
+			CVMX_PKI_PARSE_LC_TO_LG;
+	else
+		pki_dflt_pkind[node].initial_parse_mode =
+			CVMX_PKI_PARSE_NOTHING;
+
+	/* For compatibility make style = pkind so old software can modify style */
+	for (pkind = 0; pkind < CVMX_PKI_NUM_PKIND; pkind++)
+		pkind_style_map[node][pkind] = pkind;
+	/*setup packet pool*/
+	cvmx_helper_pki_set_dflt_pool(node, ipd_config.packet_pool.pool_num,
+				      ipd_config.packet_pool.buffer_size,
+				      ipd_config.packet_pool.buffer_count);
+	cvmx_helper_pki_set_dflt_aura(node, ipd_config.packet_pool.pool_num,
+				      ipd_config.packet_pool.pool_num,
+				      ipd_config.packet_pool.buffer_count);
+}
+
+int cvmx_ipd_set_config(cvmx_ipd_config_t ipd_config)
+{
+	cvmx_ipd_cfg = ipd_config;
+	if (octeon_has_feature(OCTEON_FEATURE_PKI))
+		cvmx_ipd_convert_to_newcfg(ipd_config);
+	return 0;
+}
+
+void cvmx_ipd_get_config(cvmx_ipd_config_t *ipd_config)
+{
+	*ipd_config = cvmx_ipd_cfg;
+}
+
+void cvmx_ipd_set_packet_pool_buffer_count(u64 buffer_count)
+{
+	cvmx_ipd_cfg.packet_pool.buffer_count = buffer_count;
+}
+
+void cvmx_ipd_set_packet_pool_config(s64 pool, u64 buffer_size,
+				     u64 buffer_count)
+{
+	cvmx_ipd_cfg.packet_pool.pool_num = pool;
+	cvmx_ipd_cfg.packet_pool.buffer_size = buffer_size;
+	cvmx_ipd_cfg.packet_pool.buffer_count = buffer_count;
+	if (octeon_has_feature(OCTEON_FEATURE_PKI)) {
+		int node = cvmx_get_node_num();
+		s64 aura = pool;
+
+		cvmx_helper_pki_set_dflt_pool(node, pool, buffer_size,
+					      buffer_count);
+		cvmx_helper_pki_set_dflt_aura(node, aura, pool, buffer_count);
+	}
+}
+
+void cvmx_ipd_set_wqe_pool_buffer_count(u64 buffer_count)
+{
+	cvmx_ipd_cfg.wqe_pool.buffer_count = buffer_count;
+}
+
+void cvmx_ipd_set_wqe_pool_config(s64 pool, u64 buffer_size, u64 buffer_count)
+{
+	cvmx_ipd_cfg.wqe_pool.pool_num = pool;
+	cvmx_ipd_cfg.wqe_pool.buffer_size = buffer_size;
+	cvmx_ipd_cfg.wqe_pool.buffer_count = buffer_count;
+}
+
+static void __cvmx_ipd_free_ptr_v1(void)
+{
+	unsigned int wqe_pool = cvmx_fpa_get_wqe_pool();
+	int i;
+	union cvmx_ipd_ptr_count ptr_count;
+	union cvmx_ipd_prc_port_ptr_fifo_ctl prc_port_fifo;
+	int packet_pool = (int)cvmx_fpa_get_packet_pool();
+
+	ptr_count.u64 = csr_rd(CVMX_IPD_PTR_COUNT);
+
+	/* Handle Work Queue Entry in cn56xx and cn52xx */
+	if (octeon_has_feature(OCTEON_FEATURE_NO_WPTR)) {
+		union cvmx_ipd_ctl_status ctl_status;
+
+		ctl_status.u64 = csr_rd(CVMX_IPD_CTL_STATUS);
+		if (ctl_status.s.no_wptr)
+			wqe_pool = packet_pool;
+	}
+
+	/* Free the prefetched WQE */
+	if (ptr_count.s.wqev_cnt) {
+		union cvmx_ipd_wqe_ptr_valid wqe_ptr_valid;
+
+		wqe_ptr_valid.u64 = csr_rd(CVMX_IPD_WQE_PTR_VALID);
+		cvmx_fpa1_free(cvmx_phys_to_ptr((u64)wqe_ptr_valid.s.ptr << 7),
+			       wqe_pool, 0);
+	}
+
+	/* Free all WQE in the fifo */
+	if (ptr_count.s.wqe_pcnt) {
+		int i;
+		union cvmx_ipd_pwp_ptr_fifo_ctl pwp_fifo;
+
+		pwp_fifo.u64 = csr_rd(CVMX_IPD_PWP_PTR_FIFO_CTL);
+		for (i = 0; i < ptr_count.s.wqe_pcnt; i++) {
+			pwp_fifo.s.cena = 0;
+			pwp_fifo.s.raddr =
+				pwp_fifo.s.max_cnts +
+				(pwp_fifo.s.wraddr + i) % pwp_fifo.s.max_cnts;
+			csr_wr(CVMX_IPD_PWP_PTR_FIFO_CTL, pwp_fifo.u64);
+			pwp_fifo.u64 = csr_rd(CVMX_IPD_PWP_PTR_FIFO_CTL);
+			cvmx_fpa1_free(cvmx_phys_to_ptr((u64)pwp_fifo.s.ptr
+							<< 7),
+				       wqe_pool, 0);
+		}
+		pwp_fifo.s.cena = 1;
+		csr_wr(CVMX_IPD_PWP_PTR_FIFO_CTL, pwp_fifo.u64);
+	}
+
+	/* Free the prefetched packet */
+	if (ptr_count.s.pktv_cnt) {
+		union cvmx_ipd_pkt_ptr_valid pkt_ptr_valid;
+
+		pkt_ptr_valid.u64 = csr_rd(CVMX_IPD_PKT_PTR_VALID);
+		cvmx_fpa1_free(cvmx_phys_to_ptr((u64)pkt_ptr_valid.s.ptr << 7),
+			       packet_pool, 0);
+	}
+
+	/* Free the per port prefetched packets */
+	prc_port_fifo.u64 = csr_rd(CVMX_IPD_PRC_PORT_PTR_FIFO_CTL);
+
+	for (i = 0; i < prc_port_fifo.s.max_pkt; i++) {
+		prc_port_fifo.s.cena = 0;
+		prc_port_fifo.s.raddr = i % prc_port_fifo.s.max_pkt;
+		csr_wr(CVMX_IPD_PRC_PORT_PTR_FIFO_CTL, prc_port_fifo.u64);
+		prc_port_fifo.u64 = csr_rd(CVMX_IPD_PRC_PORT_PTR_FIFO_CTL);
+		cvmx_fpa1_free(cvmx_phys_to_ptr((u64)prc_port_fifo.s.ptr << 7),
+			       packet_pool, 0);
+	}
+	prc_port_fifo.s.cena = 1;
+	csr_wr(CVMX_IPD_PRC_PORT_PTR_FIFO_CTL, prc_port_fifo.u64);
+
+	/* Free all packets in the holding fifo */
+	if (ptr_count.s.pfif_cnt) {
+		int i;
+		union cvmx_ipd_prc_hold_ptr_fifo_ctl prc_hold_fifo;
+
+		prc_hold_fifo.u64 = csr_rd(CVMX_IPD_PRC_HOLD_PTR_FIFO_CTL);
+
+		for (i = 0; i < ptr_count.s.pfif_cnt; i++) {
+			prc_hold_fifo.s.cena = 0;
+			prc_hold_fifo.s.raddr = (prc_hold_fifo.s.praddr + i) %
+						prc_hold_fifo.s.max_pkt;
+			csr_wr(CVMX_IPD_PRC_HOLD_PTR_FIFO_CTL,
+			       prc_hold_fifo.u64);
+			prc_hold_fifo.u64 =
+				csr_rd(CVMX_IPD_PRC_HOLD_PTR_FIFO_CTL);
+			cvmx_fpa1_free(cvmx_phys_to_ptr((u64)prc_hold_fifo.s.ptr
+							<< 7),
+				       packet_pool, 0);
+		}
+		prc_hold_fifo.s.cena = 1;
+		csr_wr(CVMX_IPD_PRC_HOLD_PTR_FIFO_CTL, prc_hold_fifo.u64);
+	}
+
+	/* Free all packets in the fifo */
+	if (ptr_count.s.pkt_pcnt) {
+		int i;
+		union cvmx_ipd_pwp_ptr_fifo_ctl pwp_fifo;
+
+		pwp_fifo.u64 = csr_rd(CVMX_IPD_PWP_PTR_FIFO_CTL);
+
+		for (i = 0; i < ptr_count.s.pkt_pcnt; i++) {
+			pwp_fifo.s.cena = 0;
+			pwp_fifo.s.raddr =
+				(pwp_fifo.s.praddr + i) % pwp_fifo.s.max_cnts;
+			csr_wr(CVMX_IPD_PWP_PTR_FIFO_CTL, pwp_fifo.u64);
+			pwp_fifo.u64 = csr_rd(CVMX_IPD_PWP_PTR_FIFO_CTL);
+			cvmx_fpa1_free(cvmx_phys_to_ptr((u64)pwp_fifo.s.ptr
+							<< 7),
+				       packet_pool, 0);
+		}
+		pwp_fifo.s.cena = 1;
+		csr_wr(CVMX_IPD_PWP_PTR_FIFO_CTL, pwp_fifo.u64);
+	}
+}
+
+static void __cvmx_ipd_free_ptr_v2(void)
+{
+	int no_wptr = 0;
+	int i;
+	union cvmx_ipd_port_ptr_fifo_ctl port_ptr_fifo;
+	union cvmx_ipd_ptr_count ptr_count;
+	int packet_pool = (int)cvmx_fpa_get_packet_pool();
+	int wqe_pool = (int)cvmx_fpa_get_wqe_pool();
+
+	ptr_count.u64 = csr_rd(CVMX_IPD_PTR_COUNT);
+
+	/* Handle Work Queue Entry in cn68xx */
+	if (octeon_has_feature(OCTEON_FEATURE_NO_WPTR)) {
+		union cvmx_ipd_ctl_status ctl_status;
+
+		ctl_status.u64 = csr_rd(CVMX_IPD_CTL_STATUS);
+		if (ctl_status.s.no_wptr)
+			no_wptr = 1;
+	}
+
+	/* Free the prefetched WQE */
+	if (ptr_count.s.wqev_cnt) {
+		union cvmx_ipd_next_wqe_ptr next_wqe_ptr;
+
+		next_wqe_ptr.u64 = csr_rd(CVMX_IPD_NEXT_WQE_PTR);
+		if (no_wptr)
+			cvmx_fpa1_free(cvmx_phys_to_ptr((u64)next_wqe_ptr.s.ptr
+							<< 7),
+				       packet_pool, 0);
+		else
+			cvmx_fpa1_free(cvmx_phys_to_ptr((u64)next_wqe_ptr.s.ptr
+							<< 7),
+				       wqe_pool, 0);
+	}
+
+	/* Free all WQE in the fifo */
+	if (ptr_count.s.wqe_pcnt) {
+		union cvmx_ipd_free_ptr_fifo_ctl free_fifo;
+		union cvmx_ipd_free_ptr_value free_ptr_value;
+
+		free_fifo.u64 = csr_rd(CVMX_IPD_FREE_PTR_FIFO_CTL);
+		for (i = 0; i < ptr_count.s.wqe_pcnt; i++) {
+			free_fifo.s.cena = 0;
+			free_fifo.s.raddr =
+				free_fifo.s.max_cnts +
+				(free_fifo.s.wraddr + i) % free_fifo.s.max_cnts;
+			csr_wr(CVMX_IPD_FREE_PTR_FIFO_CTL, free_fifo.u64);
+			free_fifo.u64 = csr_rd(CVMX_IPD_FREE_PTR_FIFO_CTL);
+			free_ptr_value.u64 = csr_rd(CVMX_IPD_FREE_PTR_VALUE);
+			if (no_wptr)
+				cvmx_fpa1_free(cvmx_phys_to_ptr(
+						       (u64)free_ptr_value.s.ptr
+						       << 7),
+					       packet_pool, 0);
+			else
+				cvmx_fpa1_free(cvmx_phys_to_ptr(
+						       (u64)free_ptr_value.s.ptr
+						       << 7),
+					       wqe_pool, 0);
+		}
+		free_fifo.s.cena = 1;
+		csr_wr(CVMX_IPD_FREE_PTR_FIFO_CTL, free_fifo.u64);
+	}
+
+	/* Free the prefetched packet */
+	if (ptr_count.s.pktv_cnt) {
+		union cvmx_ipd_next_pkt_ptr next_pkt_ptr;
+
+		next_pkt_ptr.u64 = csr_rd(CVMX_IPD_NEXT_PKT_PTR);
+		cvmx_fpa1_free(cvmx_phys_to_ptr((u64)next_pkt_ptr.s.ptr << 7),
+			       packet_pool, 0);
+	}
+
+	/* Free the per port prefetched packets */
+	port_ptr_fifo.u64 = csr_rd(CVMX_IPD_PORT_PTR_FIFO_CTL);
+
+	for (i = 0; i < port_ptr_fifo.s.max_pkt; i++) {
+		port_ptr_fifo.s.cena = 0;
+		port_ptr_fifo.s.raddr = i % port_ptr_fifo.s.max_pkt;
+		csr_wr(CVMX_IPD_PORT_PTR_FIFO_CTL, port_ptr_fifo.u64);
+		port_ptr_fifo.u64 = csr_rd(CVMX_IPD_PORT_PTR_FIFO_CTL);
+		cvmx_fpa1_free(cvmx_phys_to_ptr((u64)port_ptr_fifo.s.ptr << 7),
+			       packet_pool, 0);
+	}
+	port_ptr_fifo.s.cena = 1;
+	csr_wr(CVMX_IPD_PORT_PTR_FIFO_CTL, port_ptr_fifo.u64);
+
+	/* Free all packets in the holding fifo */
+	if (ptr_count.s.pfif_cnt) {
+		union cvmx_ipd_hold_ptr_fifo_ctl hold_ptr_fifo;
+
+		hold_ptr_fifo.u64 = csr_rd(CVMX_IPD_HOLD_PTR_FIFO_CTL);
+
+		for (i = 0; i < ptr_count.s.pfif_cnt; i++) {
+			hold_ptr_fifo.s.cena = 0;
+			hold_ptr_fifo.s.raddr = (hold_ptr_fifo.s.praddr + i) %
+						hold_ptr_fifo.s.max_pkt;
+			csr_wr(CVMX_IPD_HOLD_PTR_FIFO_CTL, hold_ptr_fifo.u64);
+			hold_ptr_fifo.u64 = csr_rd(CVMX_IPD_HOLD_PTR_FIFO_CTL);
+			cvmx_fpa1_free(cvmx_phys_to_ptr((u64)hold_ptr_fifo.s.ptr
+							<< 7),
+				       packet_pool, 0);
+		}
+		hold_ptr_fifo.s.cena = 1;
+		csr_wr(CVMX_IPD_HOLD_PTR_FIFO_CTL, hold_ptr_fifo.u64);
+	}
+
+	/* Free all packets in the fifo */
+	if (ptr_count.s.pkt_pcnt) {
+		union cvmx_ipd_free_ptr_fifo_ctl free_fifo;
+		union cvmx_ipd_free_ptr_value free_ptr_value;
+
+		free_fifo.u64 = csr_rd(CVMX_IPD_FREE_PTR_FIFO_CTL);
+
+		for (i = 0; i < ptr_count.s.pkt_pcnt; i++) {
+			free_fifo.s.cena = 0;
+			free_fifo.s.raddr =
+				(free_fifo.s.praddr + i) % free_fifo.s.max_cnts;
+			csr_wr(CVMX_IPD_FREE_PTR_FIFO_CTL, free_fifo.u64);
+			free_fifo.u64 = csr_rd(CVMX_IPD_FREE_PTR_FIFO_CTL);
+			free_ptr_value.u64 = csr_rd(CVMX_IPD_FREE_PTR_VALUE);
+			cvmx_fpa1_free(cvmx_phys_to_ptr(
+					       (u64)free_ptr_value.s.ptr << 7),
+				       packet_pool, 0);
+		}
+		free_fifo.s.cena = 1;
+		csr_wr(CVMX_IPD_FREE_PTR_FIFO_CTL, free_fifo.u64);
+	}
+}
+
+/**
+ * @INTERNAL
+ * This function is called by cvmx_helper_shutdown() to extract
+ * all FPA buffers out of the IPD and PIP. After this function
+ * completes, all FPA buffers that were prefetched by IPD and PIP
+ * wil be in the appropriate FPA pool. This functions does not reset
+ * PIP or IPD as FPA pool zero must be empty before the reset can
+ * be performed. WARNING: It is very important that IPD and PIP be
+ * reset soon after a call to this function.
+ */
+void __cvmx_ipd_free_ptr(void)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_PKND))
+		__cvmx_ipd_free_ptr_v2();
+	else
+		__cvmx_ipd_free_ptr_v1();
+}
+
+void cvmx_ipd_config(u64 mbuff_size, u64 first_mbuff_skip,
+		     u64 not_first_mbuff_skip, u64 first_back, u64 second_back,
+		     u64 wqe_fpa_pool, cvmx_ipd_mode_t cache_mode,
+		     u64 back_pres_enable_flag)
+{
+	cvmx_ipd_1st_mbuff_skip_t first_skip;
+	cvmx_ipd_mbuff_not_first_skip_t not_first_skip;
+	cvmx_ipd_packet_mbuff_size_t size;
+	cvmx_ipd_1st_next_ptr_back_t first_back_struct;
+	cvmx_ipd_second_next_ptr_back_t second_back_struct;
+	cvmx_ipd_wqe_fpa_queue_t wqe_pool;
+	cvmx_ipd_ctl_status_t ipd_ctl_reg;
+
+	/* Enforce 1st skip minimum if WQE shares the buffer with packet */
+	if (octeon_has_feature(OCTEON_FEATURE_NO_WPTR)) {
+		union cvmx_ipd_ctl_status ctl_status;
+
+		ctl_status.u64 = csr_rd(CVMX_IPD_CTL_STATUS);
+		if (ctl_status.s.no_wptr != 0 && first_mbuff_skip < 16)
+			first_mbuff_skip = 16;
+	}
+
+	first_skip.u64 = 0;
+	first_skip.s.skip_sz = first_mbuff_skip;
+	csr_wr(CVMX_IPD_1ST_MBUFF_SKIP, first_skip.u64);
+
+	not_first_skip.u64 = 0;
+	not_first_skip.s.skip_sz = not_first_mbuff_skip;
+	csr_wr(CVMX_IPD_NOT_1ST_MBUFF_SKIP, not_first_skip.u64);
+
+	size.u64 = 0;
+	size.s.mb_size = mbuff_size;
+	csr_wr(CVMX_IPD_PACKET_MBUFF_SIZE, size.u64);
+
+	first_back_struct.u64 = 0;
+	first_back_struct.s.back = first_back;
+	csr_wr(CVMX_IPD_1st_NEXT_PTR_BACK, first_back_struct.u64);
+
+	second_back_struct.u64 = 0;
+	second_back_struct.s.back = second_back;
+	csr_wr(CVMX_IPD_2nd_NEXT_PTR_BACK, second_back_struct.u64);
+
+	wqe_pool.u64 = 0;
+	wqe_pool.s.wqe_pool = wqe_fpa_pool;
+	csr_wr(CVMX_IPD_WQE_FPA_QUEUE, wqe_pool.u64);
+
+	ipd_ctl_reg.u64 = csr_rd(CVMX_IPD_CTL_STATUS);
+	ipd_ctl_reg.s.opc_mode = cache_mode;
+	ipd_ctl_reg.s.pbp_en = back_pres_enable_flag;
+	csr_wr(CVMX_IPD_CTL_STATUS, ipd_ctl_reg.u64);
+
+	/* Note: the example RED code is below */
+}
+
+/**
+ * Setup Random Early Drop on a specific input queue
+ *
+ * @param queue  Input queue to setup RED on (0-7)
+ * @param pass_thresh
+ *               Packets will begin slowly dropping when there are less than
+ *               this many packet buffers free in FPA 0.
+ * @param drop_thresh
+ *               All incoming packets will be dropped when there are less
+ *               than this many free packet buffers in FPA 0.
+ * @return Zero on success. Negative on failure
+ */
+int cvmx_ipd_setup_red_queue(int queue, int pass_thresh, int drop_thresh)
+{
+	union cvmx_ipd_qosx_red_marks red_marks;
+	union cvmx_ipd_red_quex_param red_param;
+
+	/*
+	 * Set RED to begin dropping packets when there are
+	 * pass_thresh buffers left. It will linearly drop more
+	 * packets until reaching drop_thresh buffers.
+	 */
+	red_marks.u64 = 0;
+	red_marks.s.drop = drop_thresh;
+	red_marks.s.pass = pass_thresh;
+	csr_wr(CVMX_IPD_QOSX_RED_MARKS(queue), red_marks.u64);
+
+	/* Use the actual queue 0 counter, not the average */
+	red_param.u64 = 0;
+	red_param.s.prb_con =
+		(255ul << 24) / (red_marks.s.pass - red_marks.s.drop);
+	red_param.s.avg_con = 1;
+	red_param.s.new_con = 255;
+	red_param.s.use_pcnt = 1;
+	csr_wr(CVMX_IPD_RED_QUEX_PARAM(queue), red_param.u64);
+	return 0;
+}
+
+/**
+ * Setup Random Early Drop to automatically begin dropping packets.
+ *
+ * @param pass_thresh
+ *               Packets will begin slowly dropping when there are less than
+ *               this many packet buffers free in FPA 0.
+ * @param drop_thresh
+ *               All incoming packets will be dropped when there are less
+ *               than this many free packet buffers in FPA 0.
+ * @return Zero on success. Negative on failure
+ */
+int cvmx_ipd_setup_red(int pass_thresh, int drop_thresh)
+{
+	int queue;
+	int interface;
+	int port;
+
+	if (octeon_has_feature(OCTEON_FEATURE_PKI))
+		return -1;
+	/*
+	 * Disable backpressure based on queued buffers. It needs SW support
+	 */
+	if (octeon_has_feature(OCTEON_FEATURE_PKND)) {
+		int bpid;
+
+		for (interface = 0; interface < CVMX_HELPER_MAX_GMX;
+		     interface++) {
+			int num_ports;
+
+			num_ports = cvmx_helper_ports_on_interface(interface);
+			for (port = 0; port < num_ports; port++) {
+				bpid = cvmx_helper_get_bpid(interface, port);
+				if (bpid == CVMX_INVALID_BPID)
+					debug("setup_red: cvmx_helper_get_bpid(%d, %d) = %d\n",
+					      interface, port,
+					      cvmx_helper_get_bpid(interface,
+								   port));
+				else
+					csr_wr(CVMX_IPD_BPIDX_MBUF_TH(bpid), 0);
+			}
+		}
+	} else {
+		union cvmx_ipd_portx_bp_page_cnt page_cnt;
+
+		page_cnt.u64 = 0;
+		page_cnt.s.bp_enb = 0;
+		page_cnt.s.page_cnt = 100;
+		for (interface = 0; interface < CVMX_HELPER_MAX_GMX;
+		     interface++) {
+			for (port = cvmx_helper_get_first_ipd_port(interface);
+			     port < cvmx_helper_get_last_ipd_port(interface);
+			     port++)
+				csr_wr(CVMX_IPD_PORTX_BP_PAGE_CNT(port),
+				       page_cnt.u64);
+		}
+	}
+
+	for (queue = 0; queue < 8; queue++)
+		cvmx_ipd_setup_red_queue(queue, pass_thresh, drop_thresh);
+
+	/*
+	 * Shutoff the dropping based on the per port page count. SW isn't
+	 * decrementing it right now
+	 */
+	if (octeon_has_feature(OCTEON_FEATURE_PKND))
+		csr_wr(CVMX_IPD_ON_BP_DROP_PKTX(0), 0);
+	else
+		csr_wr(CVMX_IPD_BP_PRT_RED_END, 0);
+
+	/*
+	 * Setting up avg_dly and prb_dly, enable bits
+	 */
+	if (octeon_has_feature(OCTEON_FEATURE_PKND)) {
+		union cvmx_ipd_red_delay red_delay;
+		union cvmx_ipd_red_bpid_enablex red_bpid_enable;
+
+		red_delay.u64 = 0;
+		red_delay.s.avg_dly = IPD_RED_AVG_DLY;
+		red_delay.s.prb_dly = IPD_RED_PRB_DLY;
+		csr_wr(CVMX_IPD_RED_DELAY, red_delay.u64);
+
+		/*
+		 * Only enable the gmx ports
+		 */
+		red_bpid_enable.u64 = 0;
+		for (interface = 0; interface < CVMX_HELPER_MAX_GMX;
+		     interface++) {
+			int num_ports =
+				cvmx_helper_ports_on_interface(interface);
+			for (port = 0; port < num_ports; port++)
+				red_bpid_enable.u64 |=
+					(((u64)1) << cvmx_helper_get_bpid(
+						 interface, port));
+		}
+		csr_wr(CVMX_IPD_RED_BPID_ENABLEX(0), red_bpid_enable.u64);
+	} else {
+		union cvmx_ipd_red_port_enable red_port_enable;
+
+		red_port_enable.u64 = 0;
+		red_port_enable.s.prt_enb = 0xfffffffffull;
+		red_port_enable.s.avg_dly = IPD_RED_AVG_DLY;
+		red_port_enable.s.prb_dly = IPD_RED_PRB_DLY;
+		csr_wr(CVMX_IPD_RED_PORT_ENABLE, red_port_enable.u64);
+
+		/*
+		 * Shutoff the dropping of packets based on RED for SRIO ports
+		 */
+		if (octeon_has_feature(OCTEON_FEATURE_SRIO)) {
+			union cvmx_ipd_red_port_enable2 red_port_enable2;
+
+			red_port_enable2.u64 = 0;
+			red_port_enable2.s.prt_enb = 0xf0;
+			csr_wr(CVMX_IPD_RED_PORT_ENABLE2, red_port_enable2.u64);
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * Enable IPD
+ */
+void cvmx_ipd_enable(void)
+{
+	cvmx_ipd_ctl_status_t ipd_reg;
+
+	ipd_reg.u64 = csr_rd(CVMX_IPD_CTL_STATUS);
+
+	/*
+	 * busy-waiting for rst_done in o68
+	 */
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX))
+		while (ipd_reg.s.rst_done != 0)
+			ipd_reg.u64 = csr_rd(CVMX_IPD_CTL_STATUS);
+
+	if (ipd_reg.s.ipd_en)
+		debug("Warning: Enabling IPD when IPD already enabled.\n");
+
+	ipd_reg.s.ipd_en = 1;
+
+	if (cvmx_ipd_cfg.enable_len_M8_fix)
+		ipd_reg.s.len_m8 = 1;
+
+	csr_wr(CVMX_IPD_CTL_STATUS, ipd_reg.u64);
+}
+
+/**
+ * Disable IPD
+ */
+void cvmx_ipd_disable(void)
+{
+	cvmx_ipd_ctl_status_t ipd_reg;
+
+	if (octeon_has_feature(OCTEON_FEATURE_PKI)) {
+		unsigned int node = cvmx_get_node_num();
+
+		cvmx_pki_disable(node);
+		return;
+	}
+	ipd_reg.u64 = csr_rd(CVMX_IPD_CTL_STATUS);
+	ipd_reg.s.ipd_en = 0;
+	csr_wr(CVMX_IPD_CTL_STATUS, ipd_reg.u64);
+}
Signature · 1 line
2.35.1
#29 lore raw

PATCH 32/52 mips: octeon: Add cvmx-pki.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-pki.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-pki.c | 1619 ++++++++++++++++++++++++++++++
 1 file changed, 1619 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-pki.c
arch/mips/mach-octeon/cvmx-pki.c new file +1619 -0
new file mode 100644
index 000000000000..136e17966b18
@@ -0,0 +1,1619 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * PKI Support.
+ */
+
+#include <time.h>
+#include <log.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-pki-cluster.h>
+#include <mach/cvmx-pki-resources.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+/**
+ * This function enables PKI
+ *
+ * @param node  Node to enable PKI.
+ */
+void cvmx_pki_enable(int node)
+{
+	cvmx_pki_sft_rst_t sft_rst;
+	cvmx_pki_buf_ctl_t buf_ctl;
+
+	sft_rst.u64 = csr_rd_node(node, CVMX_PKI_SFT_RST);
+	while (sft_rst.s.busy != 0)
+		sft_rst.u64 = csr_rd_node(node, CVMX_PKI_SFT_RST);
+
+	buf_ctl.u64 = csr_rd_node(node, CVMX_PKI_BUF_CTL);
+	if (buf_ctl.s.pki_en)
+		debug("Warning: Enabling PKI when PKI already enabled.\n");
+
+	buf_ctl.s.pki_en = 1;
+	csr_wr_node(node, CVMX_PKI_BUF_CTL, buf_ctl.u64);
+}
+
+/**
+ * This function disables PKI.
+ *
+ * @param node  Node to disable PKI.
+ */
+void cvmx_pki_disable(int node)
+{
+	cvmx_pki_buf_ctl_t buf_ctl;
+
+	buf_ctl.u64 = csr_rd_node(node, CVMX_PKI_BUF_CTL);
+	buf_ctl.s.pki_en = 0;
+	csr_wr_node(node, CVMX_PKI_BUF_CTL, buf_ctl.u64);
+}
+
+/**
+ * This function soft resets PKI.
+ *
+ * @param node  Node to enable PKI.
+ */
+void cvmx_pki_reset(int node)
+{
+	cvmx_pki_sft_rst_t sft_rst;
+
+	sft_rst.u64 = csr_rd_node(node, CVMX_PKI_SFT_RST);
+	if (CVMX_WAIT_FOR_FIELD64_NODE(node, CVMX_PKI_SFT_RST,
+				       cvmx_pki_sft_rst_t, active, ==, 0,
+				       10000)) {
+		debug("PKI_SFT_RST is not active\n");
+	}
+
+	sft_rst.s.rst = 1;
+	csr_wr_node(node, CVMX_PKI_SFT_RST, sft_rst.u64);
+	if (CVMX_WAIT_FOR_FIELD64_NODE(node, CVMX_PKI_SFT_RST,
+				       cvmx_pki_sft_rst_t, busy, ==, 0,
+				       10000)) {
+		debug("PKI_SFT_RST is busy\n");
+	}
+}
+
+/**
+ * This function sets the clusters in PKI.
+ *
+ * @param node  Node to set clusters.
+ */
+int cvmx_pki_setup_clusters(int node)
+{
+	int i;
+
+	for (i = 0; i < cvmx_pki_cluster_code_length; i++)
+		csr_wr_node(node, CVMX_PKI_IMEMX(i),
+			    cvmx_pki_cluster_code_default[i]);
+
+	return 0;
+}
+
+/**
+ * @INTERNAL
+ * This function is called by cvmx_helper_shutdown() to extract all FPA buffers
+ * out of the PKI. After this function completes, all FPA buffers that were
+ * prefetched by PKI will be in the appropriate FPA pool. This functions does
+ * not reset PKI.
+ * WARNING: It is very important that PKI be reset soon after a call to this function.
+ */
+void __cvmx_pki_free_ptr(int node)
+{
+	cvmx_pki_buf_ctl_t buf_ctl;
+
+	buf_ctl.u64 = csr_rd_node(node, CVMX_PKI_BUF_CTL);
+	/* Disable buffering any data. */
+	buf_ctl.s.pkt_off = 1;
+	/* Disable caching of any data and return all the prefetched buffers to FPA. */
+	buf_ctl.s.fpa_cac_dis = 1;
+	csr_wr_node(node, CVMX_PKI_BUF_CTL, buf_ctl.u64);
+	buf_ctl.u64 = csr_rd_node(node, CVMX_PKI_BUF_CTL);
+}
+
+/**
+ * This function reads global configuration of PKI block.
+ *
+ * @param node  Node number.
+ * @param gbl_cfg  Pointer to struct to read global configuration.
+ */
+void cvmx_pki_read_global_config(int node,
+				 struct cvmx_pki_global_config *gbl_cfg)
+{
+	cvmx_pki_stat_ctl_t stat_ctl;
+	cvmx_pki_icgx_cfg_t icg_cfg;
+	cvmx_pki_gbl_pen_t gbl_pen;
+	cvmx_pki_tag_secret_t tag_secret;
+	cvmx_pki_frm_len_chkx_t frm_len_chk;
+	cvmx_pki_buf_ctl_t buf_ctl;
+	unsigned int cl_grp;
+	int id;
+
+	stat_ctl.u64 = csr_rd_node(node, CVMX_PKI_STAT_CTL);
+	gbl_cfg->stat_mode = stat_ctl.s.mode;
+
+	for (cl_grp = 0; cl_grp < CVMX_PKI_NUM_CLUSTER_GROUP; cl_grp++) {
+		icg_cfg.u64 = csr_rd_node(node, CVMX_PKI_ICGX_CFG(cl_grp));
+		gbl_cfg->cluster_mask[cl_grp] = icg_cfg.s.clusters;
+	}
+	gbl_pen.u64 = csr_rd_node(node, CVMX_PKI_GBL_PEN);
+	gbl_cfg->gbl_pen.virt_pen = gbl_pen.s.virt_pen;
+	gbl_cfg->gbl_pen.clg_pen = gbl_pen.s.clg_pen;
+	gbl_cfg->gbl_pen.cl2_pen = gbl_pen.s.cl2_pen;
+	gbl_cfg->gbl_pen.l4_pen = gbl_pen.s.l4_pen;
+	gbl_cfg->gbl_pen.il3_pen = gbl_pen.s.il3_pen;
+	gbl_cfg->gbl_pen.l3_pen = gbl_pen.s.l3_pen;
+	gbl_cfg->gbl_pen.mpls_pen = gbl_pen.s.mpls_pen;
+	gbl_cfg->gbl_pen.fulc_pen = gbl_pen.s.fulc_pen;
+	gbl_cfg->gbl_pen.dsa_pen = gbl_pen.s.dsa_pen;
+	gbl_cfg->gbl_pen.hg_pen = gbl_pen.s.hg_pen;
+
+	tag_secret.u64 = csr_rd_node(node, CVMX_PKI_TAG_SECRET);
+	gbl_cfg->tag_secret.dst6 = tag_secret.s.dst6;
+	gbl_cfg->tag_secret.src6 = tag_secret.s.src6;
+	gbl_cfg->tag_secret.dst = tag_secret.s.dst;
+	gbl_cfg->tag_secret.src = tag_secret.s.src;
+
+	for (id = 0; id < CVMX_PKI_NUM_FRAME_CHECK; id++) {
+		frm_len_chk.u64 = csr_rd_node(node, CVMX_PKI_FRM_LEN_CHKX(id));
+		gbl_cfg->frm_len[id].maxlen = frm_len_chk.s.maxlen;
+		gbl_cfg->frm_len[id].minlen = frm_len_chk.s.minlen;
+	}
+	buf_ctl.u64 = csr_rd_node(node, CVMX_PKI_BUF_CTL);
+	gbl_cfg->fpa_wait = buf_ctl.s.fpa_wait;
+}
+
+/**
+ * This function writes max and min frame lengths to hardware which can be used
+ * to check the size of frame arrived.There are 2 possible combination which are
+ * indicated by id field.
+ *
+ * @param node  Node number.
+ * @param id  Choose which frame len register to write to
+ * @param len_chk  Struct containing byte count for max-sized/min-sized frame check.
+ */
+static void cvmx_pki_write_frame_len(int node, int id,
+				     struct cvmx_pki_frame_len len_chk)
+{
+	cvmx_pki_frm_len_chkx_t frm_len_chk;
+
+	frm_len_chk.u64 = csr_rd_node(node, CVMX_PKI_FRM_LEN_CHKX(id));
+	frm_len_chk.s.maxlen = len_chk.maxlen;
+	frm_len_chk.s.minlen = len_chk.minlen;
+	csr_wr_node(node, CVMX_PKI_FRM_LEN_CHKX(id), frm_len_chk.u64);
+}
+
+/**
+ * This function writes global configuration of PKI into hw.
+ *
+ * @param node  Node number.
+ * @param gbl_cfg  Pointer to struct to global configuration.
+ */
+void cvmx_pki_write_global_config(int node,
+				  struct cvmx_pki_global_config *gbl_cfg)
+{
+	cvmx_pki_stat_ctl_t stat_ctl;
+	cvmx_pki_buf_ctl_t buf_ctl;
+	unsigned int cl_grp;
+
+	for (cl_grp = 0; cl_grp < CVMX_PKI_NUM_CLUSTER_GROUP; cl_grp++)
+		cvmx_pki_attach_cluster_to_group(node, cl_grp,
+						 gbl_cfg->cluster_mask[cl_grp]);
+
+	stat_ctl.u64 = 0;
+	stat_ctl.s.mode = gbl_cfg->stat_mode;
+	csr_wr_node(node, CVMX_PKI_STAT_CTL, stat_ctl.u64);
+
+	buf_ctl.u64 = csr_rd_node(node, CVMX_PKI_BUF_CTL);
+	buf_ctl.s.fpa_wait = gbl_cfg->fpa_wait;
+	csr_wr_node(node, CVMX_PKI_BUF_CTL, buf_ctl.u64);
+
+	cvmx_pki_write_global_parse(node, gbl_cfg->gbl_pen);
+	cvmx_pki_write_tag_secret(node, gbl_cfg->tag_secret);
+	cvmx_pki_write_frame_len(node, 0, gbl_cfg->frm_len[0]);
+	cvmx_pki_write_frame_len(node, 1, gbl_cfg->frm_len[1]);
+}
+
+/**
+ * This function reads per pkind parameters in hardware which defines how
+ * the incoming packet is processed.
+ *
+ * @param node  Node number.
+ * @param pkind  PKI supports a large number of incoming interfaces and packets
+ *     arriving on different interfaces or channels may want to be processed
+ *     differently. PKI uses the pkind to determine how the incoming packet
+ *     is processed.
+ * @param pkind_cfg  Pointer to struct conatining pkind configuration read
+ *     from the hardware.
+ */
+int cvmx_pki_read_pkind_config(int node, int pkind,
+			       struct cvmx_pki_pkind_config *pkind_cfg)
+{
+	int cluster = 0;
+	u64 cl_mask;
+	cvmx_pki_pkindx_icgsel_t icgsel;
+	cvmx_pki_clx_pkindx_style_t pstyle;
+	cvmx_pki_icgx_cfg_t icg_cfg;
+	cvmx_pki_clx_pkindx_cfg_t pcfg;
+	cvmx_pki_clx_pkindx_skip_t skip;
+	cvmx_pki_clx_pkindx_l2_custom_t l2cust;
+	cvmx_pki_clx_pkindx_lg_custom_t lgcust;
+
+	icgsel.u64 = csr_rd_node(node, CVMX_PKI_PKINDX_ICGSEL(pkind));
+	icg_cfg.u64 = csr_rd_node(node, CVMX_PKI_ICGX_CFG(icgsel.s.icg));
+	pkind_cfg->cluster_grp = (uint8_t)icgsel.s.icg;
+	cl_mask = (uint64_t)icg_cfg.s.clusters;
+	cluster = __builtin_ffsll(cl_mask) - 1;
+
+	pstyle.u64 =
+		csr_rd_node(node, CVMX_PKI_CLX_PKINDX_STYLE(pkind, cluster));
+	pkind_cfg->initial_parse_mode = pstyle.s.pm;
+	pkind_cfg->initial_style = pstyle.s.style;
+
+	pcfg.u64 = csr_rd_node(node, CVMX_PKI_CLX_PKINDX_CFG(pkind, cluster));
+	pkind_cfg->fcs_pres = pcfg.s.fcs_pres;
+	pkind_cfg->parse_en.inst_hdr = pcfg.s.inst_hdr;
+	pkind_cfg->parse_en.mpls_en = pcfg.s.mpls_en;
+	pkind_cfg->parse_en.lg_custom = pcfg.s.lg_custom;
+	pkind_cfg->parse_en.fulc_en = pcfg.s.fulc_en;
+	pkind_cfg->parse_en.dsa_en = pcfg.s.dsa_en;
+	pkind_cfg->parse_en.hg2_en = pcfg.s.hg2_en;
+	pkind_cfg->parse_en.hg_en = pcfg.s.hg_en;
+
+	skip.u64 = csr_rd_node(node, CVMX_PKI_CLX_PKINDX_SKIP(pkind, cluster));
+	pkind_cfg->fcs_skip = skip.s.fcs_skip;
+	pkind_cfg->inst_skip = skip.s.inst_skip;
+
+	l2cust.u64 = csr_rd_node(node,
+				 CVMX_PKI_CLX_PKINDX_L2_CUSTOM(pkind, cluster));
+	pkind_cfg->l2_scan_offset = l2cust.s.offset;
+
+	lgcust.u64 = csr_rd_node(node,
+				 CVMX_PKI_CLX_PKINDX_LG_CUSTOM(pkind, cluster));
+	pkind_cfg->lg_scan_offset = lgcust.s.offset;
+	return 0;
+}
+
+/**
+ * This function writes per pkind parameters in hardware which defines how
+ * the incoming packet is processed.
+ *
+ * @param node  Node number.
+ * @param pkind  PKI supports a large number of incoming interfaces and packets
+ *     arriving on different interfaces or channels may want to be processed
+ *     differently. PKI uses the pkind to determine how the incoming
+ *     packet is processed.
+ * @param pkind_cfg  Pointer to struct conatining pkind configuration need
+ *     to be written in the hardware.
+ */
+int cvmx_pki_write_pkind_config(int node, int pkind,
+				struct cvmx_pki_pkind_config *pkind_cfg)
+{
+	unsigned int cluster = 0;
+	u64 cluster_mask;
+	cvmx_pki_pkindx_icgsel_t icgsel;
+	cvmx_pki_clx_pkindx_style_t pstyle;
+	cvmx_pki_icgx_cfg_t icg_cfg;
+	cvmx_pki_clx_pkindx_cfg_t pcfg;
+	cvmx_pki_clx_pkindx_skip_t skip;
+	cvmx_pki_clx_pkindx_l2_custom_t l2cust;
+	cvmx_pki_clx_pkindx_lg_custom_t lgcust;
+
+	if (pkind >= CVMX_PKI_NUM_PKIND ||
+	    pkind_cfg->cluster_grp >= CVMX_PKI_NUM_CLUSTER_GROUP ||
+	    pkind_cfg->initial_style >= CVMX_PKI_NUM_FINAL_STYLE) {
+		debug("ERROR: Configuring PKIND pkind = %d cluster_group = %d style = %d\n",
+		      pkind, pkind_cfg->cluster_grp, pkind_cfg->initial_style);
+		return -1;
+	}
+	icgsel.u64 = csr_rd_node(node, CVMX_PKI_PKINDX_ICGSEL(pkind));
+	icgsel.s.icg = pkind_cfg->cluster_grp;
+	csr_wr_node(node, CVMX_PKI_PKINDX_ICGSEL(pkind), icgsel.u64);
+
+	icg_cfg.u64 =
+		csr_rd_node(node, CVMX_PKI_ICGX_CFG(pkind_cfg->cluster_grp));
+	cluster_mask = (uint64_t)icg_cfg.s.clusters;
+	while (cluster < CVMX_PKI_NUM_CLUSTER) {
+		if (cluster_mask & (0x01L << cluster)) {
+			pstyle.u64 = csr_rd_node(
+				node,
+				CVMX_PKI_CLX_PKINDX_STYLE(pkind, cluster));
+			pstyle.s.pm = pkind_cfg->initial_parse_mode;
+			pstyle.s.style = pkind_cfg->initial_style;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PKINDX_STYLE(pkind, cluster),
+				    pstyle.u64);
+
+			pcfg.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_PKINDX_CFG(pkind, cluster));
+			pcfg.s.fcs_pres = pkind_cfg->fcs_pres;
+			pcfg.s.inst_hdr = pkind_cfg->parse_en.inst_hdr;
+			pcfg.s.mpls_en = pkind_cfg->parse_en.mpls_en;
+			pcfg.s.lg_custom = pkind_cfg->parse_en.lg_custom;
+			pcfg.s.fulc_en = pkind_cfg->parse_en.fulc_en;
+			pcfg.s.dsa_en = pkind_cfg->parse_en.dsa_en;
+			pcfg.s.hg2_en = pkind_cfg->parse_en.hg2_en;
+			pcfg.s.hg_en = pkind_cfg->parse_en.hg_en;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PKINDX_CFG(pkind, cluster),
+				    pcfg.u64);
+
+			skip.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_PKINDX_SKIP(pkind, cluster));
+			skip.s.fcs_skip = pkind_cfg->fcs_skip;
+			skip.s.inst_skip = pkind_cfg->inst_skip;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PKINDX_SKIP(pkind, cluster),
+				    skip.u64);
+
+			l2cust.u64 = csr_rd_node(
+				node,
+				CVMX_PKI_CLX_PKINDX_L2_CUSTOM(pkind, cluster));
+			l2cust.s.offset = pkind_cfg->l2_scan_offset;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PKINDX_L2_CUSTOM(pkind,
+								  cluster),
+				    l2cust.u64);
+
+			lgcust.u64 = csr_rd_node(
+				node,
+				CVMX_PKI_CLX_PKINDX_LG_CUSTOM(pkind, cluster));
+			lgcust.s.offset = pkind_cfg->lg_scan_offset;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PKINDX_LG_CUSTOM(pkind,
+								  cluster),
+				    lgcust.u64);
+		}
+		cluster++;
+	}
+	return 0;
+}
+
+/**
+ * This function reads parameters associated with tag configuration in hardware.
+ * Only first cluster in the group is used.
+ *
+ * @param node  Node number.
+ * @param style  Style to configure tag for.
+ * @param cluster_mask	Mask of clusters to configure the style for.
+ * @param tag_cfg  Pointer to tag configuration struct.
+ */
+void cvmx_pki_read_tag_config(int node, int style, uint64_t cluster_mask,
+			      struct cvmx_pki_style_tag_cfg *tag_cfg)
+{
+	int mask, tag_idx, index;
+	cvmx_pki_clx_stylex_cfg2_t style_cfg2;
+	cvmx_pki_clx_stylex_alg_t style_alg;
+	cvmx_pki_stylex_tag_sel_t tag_sel;
+	cvmx_pki_tag_incx_ctl_t tag_ctl;
+	cvmx_pki_tag_incx_mask_t tag_mask;
+	int cluster = __builtin_ffsll(cluster_mask) - 1;
+
+	style_cfg2.u64 =
+		csr_rd_node(node, CVMX_PKI_CLX_STYLEX_CFG2(style, cluster));
+	style_alg.u64 =
+		csr_rd_node(node, CVMX_PKI_CLX_STYLEX_ALG(style, cluster));
+
+	/* 7-Tuple Tag: */
+	tag_cfg->tag_fields.layer_g_src = style_cfg2.s.tag_src_lg;
+	tag_cfg->tag_fields.layer_f_src = style_cfg2.s.tag_src_lf;
+	tag_cfg->tag_fields.layer_e_src = style_cfg2.s.tag_src_le;
+	tag_cfg->tag_fields.layer_d_src = style_cfg2.s.tag_src_ld;
+	tag_cfg->tag_fields.layer_c_src = style_cfg2.s.tag_src_lc;
+	tag_cfg->tag_fields.layer_b_src = style_cfg2.s.tag_src_lb;
+	tag_cfg->tag_fields.layer_g_dst = style_cfg2.s.tag_dst_lg;
+	tag_cfg->tag_fields.layer_f_dst = style_cfg2.s.tag_dst_lf;
+	tag_cfg->tag_fields.layer_e_dst = style_cfg2.s.tag_dst_le;
+	tag_cfg->tag_fields.layer_d_dst = style_cfg2.s.tag_dst_ld;
+	tag_cfg->tag_fields.layer_c_dst = style_cfg2.s.tag_dst_lc;
+	tag_cfg->tag_fields.layer_b_dst = style_cfg2.s.tag_dst_lb;
+	tag_cfg->tag_fields.tag_vni = style_alg.s.tag_vni;
+	tag_cfg->tag_fields.tag_gtp = style_alg.s.tag_gtp;
+	tag_cfg->tag_fields.tag_spi = style_alg.s.tag_spi;
+	tag_cfg->tag_fields.tag_sync = style_alg.s.tag_syn;
+	tag_cfg->tag_fields.ip_prot_nexthdr = style_alg.s.tag_pctl;
+	tag_cfg->tag_fields.second_vlan = style_alg.s.tag_vs1;
+	tag_cfg->tag_fields.first_vlan = style_alg.s.tag_vs0;
+	tag_cfg->tag_fields.mpls_label = style_alg.s.tag_mpls0;
+	tag_cfg->tag_fields.input_port = style_alg.s.tag_prt;
+
+	/* Custom-Mask Tag: */
+	tag_sel.u64 = csr_rd_node(node, CVMX_PKI_STYLEX_TAG_SEL(style));
+	for (mask = 0; mask < 4; mask++) {
+		tag_cfg->mask_tag[mask].enable =
+			(style_cfg2.s.tag_inc & (1 << mask)) != 0;
+		switch (mask) {
+		case 0:
+			tag_idx = tag_sel.s.tag_idx0;
+			break;
+		case 1:
+			tag_idx = tag_sel.s.tag_idx1;
+			break;
+		case 2:
+			tag_idx = tag_sel.s.tag_idx2;
+			break;
+		case 3:
+			tag_idx = tag_sel.s.tag_idx3;
+			break;
+		}
+		index = tag_idx * 4 + mask;
+		tag_mask.u64 = csr_rd_node(node, CVMX_PKI_TAG_INCX_MASK(index));
+		tag_cfg->mask_tag[mask].val = tag_mask.s.en;
+		tag_ctl.u64 = csr_rd_node(node, CVMX_PKI_TAG_INCX_CTL(index));
+		tag_cfg->mask_tag[mask].base = tag_ctl.s.ptr_sel;
+		tag_cfg->mask_tag[mask].offset = tag_ctl.s.offset;
+	}
+}
+
+/**
+ * This function writes/configures parameters associated with tag configuration in
+ * hardware. In Custom-Mask Tagging, all four masks use the same base index
+ * to access Tag Control and Tag Mask registers.
+ *
+ * @param node  Node number.
+ * @param style  Style to configure tag for.
+ * @param cluster_mask  Mask of clusters to configure the style for.
+ * @param tag_cfg  Pointer to taf configuration struct.
+ */
+void cvmx_pki_write_tag_config(int node, int style, uint64_t cluster_mask,
+			       struct cvmx_pki_style_tag_cfg *tag_cfg)
+{
+	int mask, index, tag_idx, mtag_en = 0;
+	unsigned int cluster = 0;
+	cvmx_pki_clx_stylex_cfg2_t scfg2;
+	cvmx_pki_clx_stylex_alg_t style_alg;
+	cvmx_pki_tag_incx_ctl_t tag_ctl;
+	cvmx_pki_tag_incx_mask_t tag_mask;
+	cvmx_pki_stylex_tag_sel_t tag_sel;
+
+	while (cluster < CVMX_PKI_NUM_CLUSTER) {
+		if (cluster_mask & (0x01L << cluster)) {
+			/* 7-Tuple Tag: */
+			scfg2.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_STYLEX_CFG2(style, cluster));
+			scfg2.s.tag_src_lg = tag_cfg->tag_fields.layer_g_src;
+			scfg2.s.tag_src_lf = tag_cfg->tag_fields.layer_f_src;
+			scfg2.s.tag_src_le = tag_cfg->tag_fields.layer_e_src;
+			scfg2.s.tag_src_ld = tag_cfg->tag_fields.layer_d_src;
+			scfg2.s.tag_src_lc = tag_cfg->tag_fields.layer_c_src;
+			scfg2.s.tag_src_lb = tag_cfg->tag_fields.layer_b_src;
+			scfg2.s.tag_dst_lg = tag_cfg->tag_fields.layer_g_dst;
+			scfg2.s.tag_dst_lf = tag_cfg->tag_fields.layer_f_dst;
+			scfg2.s.tag_dst_le = tag_cfg->tag_fields.layer_e_dst;
+			scfg2.s.tag_dst_ld = tag_cfg->tag_fields.layer_d_dst;
+			scfg2.s.tag_dst_lc = tag_cfg->tag_fields.layer_c_dst;
+			scfg2.s.tag_dst_lb = tag_cfg->tag_fields.layer_b_dst;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_STYLEX_CFG2(style, cluster),
+				    scfg2.u64);
+
+			style_alg.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_STYLEX_ALG(style, cluster));
+			style_alg.s.tag_vni = tag_cfg->tag_fields.tag_vni;
+			style_alg.s.tag_gtp = tag_cfg->tag_fields.tag_gtp;
+			style_alg.s.tag_spi = tag_cfg->tag_fields.tag_spi;
+			style_alg.s.tag_syn = tag_cfg->tag_fields.tag_sync;
+			style_alg.s.tag_pctl =
+				tag_cfg->tag_fields.ip_prot_nexthdr;
+			style_alg.s.tag_vs1 = tag_cfg->tag_fields.second_vlan;
+			style_alg.s.tag_vs0 = tag_cfg->tag_fields.first_vlan;
+			style_alg.s.tag_mpls0 = tag_cfg->tag_fields.mpls_label;
+			style_alg.s.tag_prt = tag_cfg->tag_fields.input_port;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_STYLEX_ALG(style, cluster),
+				    style_alg.u64);
+
+			/* Custom-Mask Tag (Part 1): */
+			for (mask = 0; mask < 4; mask++) {
+				if (tag_cfg->mask_tag[mask].enable)
+					mtag_en++;
+			}
+			if (mtag_en) {
+				scfg2.u64 = csr_rd_node(
+					node, CVMX_PKI_CLX_STYLEX_CFG2(
+						      style, cluster));
+				scfg2.s.tag_inc = 0;
+				for (mask = 0; mask < 4; mask++) {
+					if (tag_cfg->mask_tag[mask].enable)
+						scfg2.s.tag_inc |= 1 << mask;
+				}
+				csr_wr_node(node,
+					    CVMX_PKI_CLX_STYLEX_CFG2(style,
+								     cluster),
+					    scfg2.u64);
+			}
+		}
+		cluster++;
+	}
+	/* Custom-Mask Tag (Part 2): */
+	if (mtag_en) {
+		tag_idx = cvmx_pki_mtag_idx_alloc(node, -1);
+		if (tag_idx < 0)
+			return;
+
+		tag_sel.u64 = csr_rd_node(node, CVMX_PKI_STYLEX_TAG_SEL(style));
+		for (mask = 0; mask < 4; mask++) {
+			if (tag_cfg->mask_tag[mask].enable) {
+				switch (mask) {
+				case 0:
+					tag_sel.s.tag_idx0 = tag_idx;
+					break;
+				case 1:
+					tag_sel.s.tag_idx1 = tag_idx;
+					break;
+				case 2:
+					tag_sel.s.tag_idx2 = tag_idx;
+					break;
+				case 3:
+					tag_sel.s.tag_idx3 = tag_idx;
+					break;
+				}
+				index = tag_idx * 4 + mask;
+				tag_mask.u64 = csr_rd_node(
+					node, CVMX_PKI_TAG_INCX_MASK(index));
+				tag_mask.s.en = tag_cfg->mask_tag[mask].val;
+				csr_wr_node(node, CVMX_PKI_TAG_INCX_MASK(index),
+					    tag_mask.u64);
+
+				tag_ctl.u64 = csr_rd_node(
+					node, CVMX_PKI_TAG_INCX_CTL(index));
+				tag_ctl.s.ptr_sel =
+					tag_cfg->mask_tag[mask].base;
+				tag_ctl.s.offset =
+					tag_cfg->mask_tag[mask].offset;
+				csr_wr_node(node, CVMX_PKI_TAG_INCX_CTL(index),
+					    tag_ctl.u64);
+			}
+		}
+		csr_wr_node(node, CVMX_PKI_STYLEX_TAG_SEL(style), tag_sel.u64);
+	}
+}
+
+/**
+ * This function reads parameters associated with style in hardware.
+ *
+ * @param node  Node number.
+ * @param style	Style to read from.
+ * @param cluster_mask	Mask of clusters style belongs to.
+ * @param style_cfg	 Pointer to style config struct.
+ */
+void cvmx_pki_read_style_config(int node, int style, uint64_t cluster_mask,
+				struct cvmx_pki_style_config *style_cfg)
+{
+	cvmx_pki_clx_stylex_cfg_t scfg;
+	cvmx_pki_clx_stylex_cfg2_t scfg2;
+	cvmx_pki_clx_stylex_alg_t style_alg;
+	cvmx_pki_stylex_buf_t style_buf;
+	int cluster = __builtin_ffsll(cluster_mask) - 1;
+
+	scfg.u64 = csr_rd_node(node, CVMX_PKI_CLX_STYLEX_CFG(style, cluster));
+	scfg2.u64 = csr_rd_node(node, CVMX_PKI_CLX_STYLEX_CFG2(style, cluster));
+	style_alg.u64 =
+		csr_rd_node(node, CVMX_PKI_CLX_STYLEX_ALG(style, cluster));
+	style_buf.u64 = csr_rd_node(node, CVMX_PKI_STYLEX_BUF(style));
+
+	style_cfg->parm_cfg.ip6_udp_opt = scfg.s.ip6_udp_opt;
+	style_cfg->parm_cfg.lenerr_en = scfg.s.lenerr_en;
+	style_cfg->parm_cfg.lenerr_eqpad = scfg.s.lenerr_eqpad;
+	style_cfg->parm_cfg.maxerr_en = scfg.s.maxerr_en;
+	style_cfg->parm_cfg.minerr_en = scfg.s.minerr_en;
+	style_cfg->parm_cfg.fcs_chk = scfg.s.fcs_chk;
+	style_cfg->parm_cfg.fcs_strip = scfg.s.fcs_strip;
+	style_cfg->parm_cfg.minmax_sel = scfg.s.minmax_sel;
+	style_cfg->parm_cfg.qpg_base = scfg.s.qpg_base;
+	style_cfg->parm_cfg.qpg_dis_padd = scfg.s.qpg_dis_padd;
+	style_cfg->parm_cfg.qpg_dis_aura = scfg.s.qpg_dis_aura;
+	style_cfg->parm_cfg.qpg_dis_grp = scfg.s.qpg_dis_grp;
+	style_cfg->parm_cfg.qpg_dis_grptag = scfg.s.qpg_dis_grptag;
+	style_cfg->parm_cfg.rawdrp = scfg.s.rawdrp;
+	style_cfg->parm_cfg.force_drop = scfg.s.drop;
+	style_cfg->parm_cfg.nodrop = scfg.s.nodrop;
+
+	style_cfg->parm_cfg.len_lg = scfg2.s.len_lg;
+	style_cfg->parm_cfg.len_lf = scfg2.s.len_lf;
+	style_cfg->parm_cfg.len_le = scfg2.s.len_le;
+	style_cfg->parm_cfg.len_ld = scfg2.s.len_ld;
+	style_cfg->parm_cfg.len_lc = scfg2.s.len_lc;
+	style_cfg->parm_cfg.len_lb = scfg2.s.len_lb;
+	style_cfg->parm_cfg.csum_lg = scfg2.s.csum_lg;
+	style_cfg->parm_cfg.csum_lf = scfg2.s.csum_lf;
+	style_cfg->parm_cfg.csum_le = scfg2.s.csum_le;
+	style_cfg->parm_cfg.csum_ld = scfg2.s.csum_ld;
+	style_cfg->parm_cfg.csum_lc = scfg2.s.csum_lc;
+	style_cfg->parm_cfg.csum_lb = scfg2.s.csum_lb;
+
+	style_cfg->parm_cfg.qpg_qos = style_alg.s.qpg_qos;
+	style_cfg->parm_cfg.tag_type = style_alg.s.tt;
+	style_cfg->parm_cfg.apad_nip = style_alg.s.apad_nip;
+	style_cfg->parm_cfg.qpg_port_sh = style_alg.s.qpg_port_sh;
+	style_cfg->parm_cfg.qpg_port_msb = style_alg.s.qpg_port_msb;
+	style_cfg->parm_cfg.wqe_vs = style_alg.s.wqe_vs;
+
+	style_cfg->parm_cfg.pkt_lend = style_buf.s.pkt_lend;
+	style_cfg->parm_cfg.wqe_hsz = style_buf.s.wqe_hsz;
+	style_cfg->parm_cfg.wqe_skip = style_buf.s.wqe_skip * 128;
+	style_cfg->parm_cfg.first_skip = style_buf.s.first_skip * 8;
+	style_cfg->parm_cfg.later_skip = style_buf.s.later_skip * 8;
+	style_cfg->parm_cfg.cache_mode = style_buf.s.opc_mode;
+	style_cfg->parm_cfg.mbuff_size = style_buf.s.mb_size * 8;
+	style_cfg->parm_cfg.dis_wq_dat = style_buf.s.dis_wq_dat;
+
+	cvmx_pki_read_tag_config(node, style, cluster_mask,
+				 &style_cfg->tag_cfg);
+}
+
+/**
+ * This function writes/configures parameters associated with style in hardware.
+ *
+ * @param node  Node number.
+ * @param style  Style to configure.
+ * @param cluster_mask  Mask of clusters to configure the style for.
+ * @param style_cfg	 Pointer to style config struct.
+ */
+void cvmx_pki_write_style_config(int node, uint64_t style, u64 cluster_mask,
+				 struct cvmx_pki_style_config *style_cfg)
+{
+	cvmx_pki_clx_stylex_cfg_t scfg;
+	cvmx_pki_clx_stylex_cfg2_t scfg2;
+	cvmx_pki_clx_stylex_alg_t style_alg;
+	cvmx_pki_stylex_buf_t style_buf;
+	unsigned int cluster = 0;
+
+	while (cluster < CVMX_PKI_NUM_CLUSTER) {
+		if (cluster_mask & (0x01L << cluster)) {
+			scfg.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_STYLEX_CFG(style, cluster));
+			scfg.s.ip6_udp_opt = style_cfg->parm_cfg.ip6_udp_opt;
+			scfg.s.lenerr_en = style_cfg->parm_cfg.lenerr_en;
+			scfg.s.lenerr_eqpad = style_cfg->parm_cfg.lenerr_eqpad;
+			scfg.s.maxerr_en = style_cfg->parm_cfg.maxerr_en;
+			scfg.s.minerr_en = style_cfg->parm_cfg.minerr_en;
+			scfg.s.fcs_chk = style_cfg->parm_cfg.fcs_chk;
+			scfg.s.fcs_strip = style_cfg->parm_cfg.fcs_strip;
+			scfg.s.minmax_sel = style_cfg->parm_cfg.minmax_sel;
+			scfg.s.qpg_base = style_cfg->parm_cfg.qpg_base;
+			scfg.s.qpg_dis_padd = style_cfg->parm_cfg.qpg_dis_padd;
+			scfg.s.qpg_dis_aura = style_cfg->parm_cfg.qpg_dis_aura;
+			scfg.s.qpg_dis_grp = style_cfg->parm_cfg.qpg_dis_grp;
+			scfg.s.qpg_dis_grptag =
+				style_cfg->parm_cfg.qpg_dis_grptag;
+			scfg.s.rawdrp = style_cfg->parm_cfg.rawdrp;
+			scfg.s.drop = style_cfg->parm_cfg.force_drop;
+			scfg.s.nodrop = style_cfg->parm_cfg.nodrop;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_STYLEX_CFG(style, cluster),
+				    scfg.u64);
+
+			scfg2.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_STYLEX_CFG2(style, cluster));
+			scfg2.s.len_lg = style_cfg->parm_cfg.len_lg;
+			scfg2.s.len_lf = style_cfg->parm_cfg.len_lf;
+			scfg2.s.len_le = style_cfg->parm_cfg.len_le;
+			scfg2.s.len_ld = style_cfg->parm_cfg.len_ld;
+			scfg2.s.len_lc = style_cfg->parm_cfg.len_lc;
+			scfg2.s.len_lb = style_cfg->parm_cfg.len_lb;
+			scfg2.s.csum_lg = style_cfg->parm_cfg.csum_lg;
+			scfg2.s.csum_lf = style_cfg->parm_cfg.csum_lf;
+			scfg2.s.csum_le = style_cfg->parm_cfg.csum_le;
+			scfg2.s.csum_ld = style_cfg->parm_cfg.csum_ld;
+			scfg2.s.csum_lc = style_cfg->parm_cfg.csum_lc;
+			scfg2.s.csum_lb = style_cfg->parm_cfg.csum_lb;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_STYLEX_CFG2(style, cluster),
+				    scfg2.u64);
+
+			style_alg.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_STYLEX_ALG(style, cluster));
+			style_alg.s.qpg_qos = style_cfg->parm_cfg.qpg_qos;
+			style_alg.s.tt = style_cfg->parm_cfg.tag_type;
+			style_alg.s.apad_nip = style_cfg->parm_cfg.apad_nip;
+			style_alg.s.qpg_port_sh =
+				style_cfg->parm_cfg.qpg_port_sh;
+			style_alg.s.qpg_port_msb =
+				style_cfg->parm_cfg.qpg_port_msb;
+			style_alg.s.wqe_vs = style_cfg->parm_cfg.wqe_vs;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_STYLEX_ALG(style, cluster),
+				    style_alg.u64);
+		}
+		cluster++;
+	}
+	style_buf.u64 = csr_rd_node(node, CVMX_PKI_STYLEX_BUF(style));
+	style_buf.s.pkt_lend = style_cfg->parm_cfg.pkt_lend;
+	style_buf.s.wqe_hsz = style_cfg->parm_cfg.wqe_hsz;
+	style_buf.s.wqe_skip = (style_cfg->parm_cfg.wqe_skip) / 128;
+	style_buf.s.first_skip = (style_cfg->parm_cfg.first_skip) / 8;
+	style_buf.s.later_skip = style_cfg->parm_cfg.later_skip / 8;
+	style_buf.s.opc_mode = style_cfg->parm_cfg.cache_mode;
+	style_buf.s.mb_size = (style_cfg->parm_cfg.mbuff_size) / 8;
+	style_buf.s.dis_wq_dat = style_cfg->parm_cfg.dis_wq_dat;
+	csr_wr_node(node, CVMX_PKI_STYLEX_BUF(style), style_buf.u64);
+
+	cvmx_pki_write_tag_config(node, style, cluster_mask,
+				  &style_cfg->tag_cfg);
+}
+
+/**
+ * This function reads qpg entry at specified offset from qpg table.
+ *
+ * @param node  Node number.
+ * @param offset  Offset in qpg table to read from.
+ * @param qpg_cfg  Pointer to structure containing qpg values.
+ */
+int cvmx_pki_read_qpg_entry(int node, int offset,
+			    struct cvmx_pki_qpg_config *qpg_cfg)
+{
+	cvmx_pki_qpg_tblx_t qpg_tbl;
+
+	if (offset >= CVMX_PKI_NUM_QPG_ENTRY) {
+		debug("ERROR: qpg offset %d is >= 2048\n", offset);
+		return -1;
+	}
+	qpg_tbl.u64 = csr_rd_node(node, CVMX_PKI_QPG_TBLX(offset));
+	qpg_cfg->aura_num = qpg_tbl.s.laura;
+	qpg_cfg->port_add = qpg_tbl.s.padd;
+	qpg_cfg->grp_ok = qpg_tbl.s.grp_ok;
+	qpg_cfg->grp_bad = qpg_tbl.s.grp_bad;
+	qpg_cfg->grptag_ok = qpg_tbl.s.grptag_ok;
+	qpg_cfg->grptag_bad = qpg_tbl.s.grptag_bad;
+	return 0;
+}
+
+/**
+ * This function writes qpg entry at specified offset in qpg table.
+ *
+ * @param node  Node number.
+ * @param offset  Offset in qpg table to read from.
+ * @param qpg_cfg  Pointer to structure containing qpg values.
+ */
+void cvmx_pki_write_qpg_entry(int node, int offset,
+			      struct cvmx_pki_qpg_config *qpg_cfg)
+{
+	cvmx_pki_qpg_tblx_t qpg_tbl;
+
+	qpg_tbl.u64 = csr_rd_node(node, CVMX_PKI_QPG_TBLX(offset));
+	qpg_tbl.s.padd = qpg_cfg->port_add;
+	qpg_tbl.s.laura = qpg_cfg->aura_num;
+	qpg_tbl.s.grp_ok = qpg_cfg->grp_ok;
+	qpg_tbl.s.grp_bad = qpg_cfg->grp_bad;
+	qpg_tbl.s.grptag_ok = qpg_cfg->grptag_ok;
+	qpg_tbl.s.grptag_bad = qpg_cfg->grptag_bad;
+	csr_wr_node(node, CVMX_PKI_QPG_TBLX(offset), qpg_tbl.u64);
+}
+
+/**
+ * This function writes pcam entry at given offset in pcam table in hardware
+ *
+ * @param node  Node number.
+ * @param index  Offset in pcam table.
+ * @param cluster_mask	Mask of clusters in which to write pcam entry.
+ * @param input  Input keys to pcam match passed as struct.
+ * @param action  PCAM match action passed as struct.
+ */
+int cvmx_pki_pcam_write_entry(int node, int index, uint64_t cluster_mask,
+			      struct cvmx_pki_pcam_input input,
+			      struct cvmx_pki_pcam_action action)
+{
+	int bank;
+	unsigned int cluster = 0;
+	cvmx_pki_clx_pcamx_termx_t term;
+	cvmx_pki_clx_pcamx_matchx_t match;
+	cvmx_pki_clx_pcamx_actionx_t act;
+
+	if (index >= CVMX_PKI_TOTAL_PCAM_ENTRY) {
+		debug("\nERROR: Invalid pcam entry %d\n", index);
+		return -1;
+	}
+	bank = (int)(input.field & 0x01);
+	while (cluster < CVMX_PKI_NUM_CLUSTER) {
+		if (cluster_mask & (0x01L << cluster)) {
+			term.u64 = csr_rd_node(
+				node,
+				CVMX_PKI_CLX_PCAMX_TERMX(cluster, bank, index));
+			term.s.valid = 0;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PCAMX_TERMX(cluster, bank,
+							     index),
+				    term.u64);
+			match.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_PCAMX_MATCHX(cluster, bank,
+								index));
+			match.s.data1 = input.data & input.data_mask;
+			match.s.data0 = (~input.data) & input.data_mask;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PCAMX_MATCHX(cluster, bank,
+							      index),
+				    match.u64);
+
+			act.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_PCAMX_ACTIONX(cluster, bank,
+								 index));
+			act.s.pmc = action.parse_mode_chg;
+			act.s.style_add = action.style_add;
+			act.s.pf = action.parse_flag_set;
+			act.s.setty = action.layer_type_set;
+			act.s.advance = action.pointer_advance;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PCAMX_ACTIONX(cluster, bank,
+							       index),
+				    act.u64);
+
+			term.u64 = csr_rd_node(
+				node,
+				CVMX_PKI_CLX_PCAMX_TERMX(cluster, bank, index));
+			term.s.term1 = input.field & input.field_mask;
+			term.s.term0 = (~input.field) & input.field_mask;
+			term.s.style1 = input.style & input.style_mask;
+			term.s.style0 = (~input.style) & input.style_mask;
+			term.s.valid = 1;
+			csr_wr_node(node,
+				    CVMX_PKI_CLX_PCAMX_TERMX(cluster, bank,
+							     index),
+				    term.u64);
+		}
+		cluster++;
+	}
+	return 0;
+}
+
+/**
+ * Enables/Disabled QoS (RED Drop, Tail Drop & backpressure) for the PKI aura.
+ *
+ * @param node  Node number
+ * @param aura  To enable/disable QoS on.
+ * @param ena_red  Enable/Disable RED drop between pass and drop level
+ *    1-enable 0-disable
+ * @param ena_drop  Enable/disable tail drop when max drop level exceeds
+ *    1-enable 0-disable
+ * @param ena_bp  Enable/Disable asserting backpressure on bpid when max
+ *    DROP level exceeds.
+ *    1-enable 0-disable
+ */
+int cvmx_pki_enable_aura_qos(int node, int aura, bool ena_red, bool ena_drop,
+			     bool ena_bp)
+{
+	cvmx_pki_aurax_cfg_t aura_cfg;
+
+	if (aura >= CVMX_PKI_NUM_AURA) {
+		debug("ERROR: %s aura = %d\n", __func__, aura);
+		return -1;
+	}
+	aura_cfg.u64 = csr_rd_node(node, CVMX_PKI_AURAX_CFG(aura));
+	aura_cfg.s.ena_red = ena_red;
+	aura_cfg.s.ena_drop = ena_drop;
+	aura_cfg.s.ena_bp = ena_bp;
+	csr_wr_node(node, CVMX_PKI_AURAX_CFG(aura), aura_cfg.u64);
+	return 0;
+}
+
+/**
+ * Configures the bpid on which, specified aura will assert backpressure.
+ * Each bpid receives backpressure from auras.
+ * Multiple auras can backpressure single bpid.
+ *
+ * @param node  Node number.
+ * @param aura  Number which will assert backpressure on that bpid.
+ * @param bpid  To assert backpressure on.
+ */
+int cvmx_pki_write_aura_bpid(int node, int aura, int bpid)
+{
+	int i, cnt, ena_bp;
+	cvmx_pki_aurax_cfg_t aura_cfg;
+
+	if (aura >= CVMX_PKI_NUM_AURA || bpid >= CVMX_PKI_NUM_BPID) {
+		debug("ERROR: aura=%d or bpid=%d is out or range\n", aura,
+		      bpid);
+		return -1;
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		/* Workaround for Errata PKI-24364:
+		 * Inform about assigning the same BPID to multiple auras
+		 * having different ENA_BP.
+		 */
+		aura_cfg.u64 = csr_rd_node(node, CVMX_PKI_AURAX_CFG(aura));
+		ena_bp = aura_cfg.s.ena_bp;
+		for (i = 0, cnt = 1; i < CVMX_PKI_NUM_AURA; i++) {
+			if (i == aura)
+				continue;
+			aura_cfg.u64 = csr_rd_node(node, CVMX_PKI_AURAX_CFG(i));
+			if (aura_cfg.s.bpid == bpid &&
+			    aura_cfg.s.ena_bp != ena_bp)
+				cnt++;
+		}
+		if (cnt > 1)
+			debug("WARNING: BPID(%d) is used by %d AURAs.\n"
+			      "\tEnable|disable backpressure for all AURAs on this BPID.\n",
+			      bpid, cnt);
+	}
+	aura_cfg.u64 = csr_rd_node(node, CVMX_PKI_AURAX_CFG(aura));
+	aura_cfg.s.bpid = bpid;
+	csr_wr_node(node, CVMX_PKI_AURAX_CFG(aura), aura_cfg.u64);
+	return 0;
+}
+
+/**
+ * Configures the channel which will receive backpressure
+ * from the specified bpid.
+ * Each channel listens for backpressure on a specific bpid.
+ * Each bpid can backpressure multiple channels.
+ *
+ * @param node  Node number.
+ * @param bpid  BPID from which, channel will receive backpressure.
+ * @param channel  Channel number to receive backpressue.
+ */
+int cvmx_pki_write_channel_bpid(int node, int channel, int bpid)
+{
+	cvmx_pki_chanx_cfg_t chan_cfg;
+
+	if (channel >= CVMX_PKI_NUM_CHANNEL || bpid >= CVMX_PKI_NUM_BPID) {
+		debug("ERROR: %s channel = %d bpid = %d\n", __func__, channel,
+		      bpid);
+		return -1;
+	}
+	chan_cfg.u64 = csr_rd_node(node, CVMX_PKI_CHANX_CFG(channel));
+	chan_cfg.s.bpid = bpid;
+	csr_wr_node(node, CVMX_PKI_CHANX_CFG(channel), chan_cfg.u64);
+	return 0;
+}
+
+/**
+ * This function gives the initial style used by that pkind.
+ *
+ * @param node	Node number.
+ * @param pkind	PKIND number.
+ */
+int cvmx_pki_get_pkind_style(int node, int pkind)
+{
+	int cluster = 0;
+	cvmx_pki_clx_pkindx_style_t style;
+
+	style.u64 =
+		csr_rd_node(node, CVMX_PKI_CLX_PKINDX_STYLE(pkind, cluster));
+	return style.s.style;
+}
+
+/**
+ * This function sets the wqe buffer mode. First packet data buffer can
+ * reside either in same buffer as wqe OR it can go in separate buffer.
+ * If used the later mode, make sure software allocate enough buffers to
+ * now have wqe separate from packet data.
+ *
+ * @param node  Node number.
+ * @param style	 Style to configure.
+ * @param pkt_outside_wqe	0 = The packet link pointer will be at word [FIRST_SKIP]
+ *    immediately followed by packet data, in the same buffer as the work queue entry.
+ *    1 = The packet link pointer will be at word [FIRST_SKIP] in a new buffer
+ *    separate from the work queue entry. Words following the WQE in the same
+ *    cache line will be zeroed, other lines in the buffer will not be modified
+ *    and will retain stale data (from the buffer’s previous use). This setting may
+ *    decrease the peak PKI performance by up to half on small packets.
+ */
+void cvmx_pki_set_wqe_mode(int node, uint64_t style, bool pkt_outside_wqe)
+{
+	cvmx_pki_stylex_buf_t style_buf;
+
+	style_buf.u64 = csr_rd_node(node, CVMX_PKI_STYLEX_BUF(style));
+	style_buf.s.dis_wq_dat = pkt_outside_wqe;
+	csr_wr_node(node, CVMX_PKI_STYLEX_BUF(style), style_buf.u64);
+}
+
+/**
+ * This function sets the Packet mode of all ports and styles to little-endian.
+ * It Changes write operations of packet data to L2C to be in little-endian.
+ * Does not change the WQE header format, which is properly endian neutral.
+ *
+ * @param node  Node number.
+ * @param style  Style to configure.
+ */
+void cvmx_pki_set_little_endian(int node, uint64_t style)
+{
+	cvmx_pki_stylex_buf_t style_buf;
+
+	style_buf.u64 = csr_rd_node(node, CVMX_PKI_STYLEX_BUF(style));
+	style_buf.s.pkt_lend = 1;
+	csr_wr_node(node, CVMX_PKI_STYLEX_BUF(style), style_buf.u64);
+}
+
+/**
+ * Enables/Disables fcs check and fcs stripping on the pkind.
+ *
+ * @param node  Node number
+ * @param pknd  PKIND to apply settings on.
+ * @param fcs_chk  Enable/disable fcs check.
+ *    1 = enable fcs error check.
+ *    0 = disable fcs error check.
+ * @param fcs_strip	 Strip L2 FCS bytes from packet, decrease WQE[LEN] by 4 bytes
+ *    1 = strip L2 FCS.
+ *    0 = Do not strip L2 FCS.
+ */
+void cvmx_pki_endis_fcs_check(int node, int pknd, bool fcs_chk, bool fcs_strip)
+{
+	int style;
+	unsigned int cluster;
+	cvmx_pki_clx_pkindx_style_t pstyle;
+	cvmx_pki_clx_stylex_cfg_t style_cfg;
+
+	/* Valudate PKIND # */
+	if (pknd >= CVMX_PKI_NUM_PKIND) {
+		printf("%s: PKIND %d out of range\n", __func__, pknd);
+		return;
+	}
+
+	for (cluster = 0; cluster < CVMX_PKI_NUM_CLUSTER; cluster++) {
+		pstyle.u64 = csr_rd_node(
+			node, CVMX_PKI_CLX_PKINDX_STYLE(pknd, cluster));
+		style = pstyle.s.style;
+		/* Validate STYLE # */
+		if (style >= CVMX_PKI_NUM_INTERNAL_STYLE)
+			continue;
+		style_cfg.u64 = csr_rd_node(
+			node, CVMX_PKI_CLX_STYLEX_CFG(style, cluster));
+		style_cfg.s.fcs_chk = fcs_chk;
+		style_cfg.s.fcs_strip = fcs_strip;
+		csr_wr_node(node, CVMX_PKI_CLX_STYLEX_CFG(style, cluster),
+			    style_cfg.u64);
+	}
+}
+
+/**
+ * Enables/Disables l2 length error check and max & min frame length checks
+ *
+ * @param node  Node number
+ * @param pknd  PKIND to disable error for.
+ * @param l2len_err  L2 length error check enable.
+ * @param maxframe_err  Max frame error check enable.
+ * @param minframe_err  Min frame error check enable.
+ *    1 = Enabel err checks
+ *    0 = Disable error checks
+ */
+void cvmx_pki_endis_l2_errs(int node, int pknd, bool l2len_err,
+			    bool maxframe_err, bool minframe_err)
+{
+	int style;
+	unsigned int cluster;
+	cvmx_pki_clx_pkindx_style_t pstyle;
+	cvmx_pki_clx_stylex_cfg_t style_cfg;
+
+	/* Valudate PKIND # */
+	if (pknd >= CVMX_PKI_NUM_PKIND) {
+		printf("%s: PKIND %d out of range\n", __func__, pknd);
+		return;
+	}
+
+	for (cluster = 0; cluster < CVMX_PKI_NUM_CLUSTER; cluster++) {
+		pstyle.u64 = csr_rd_node(
+			node, CVMX_PKI_CLX_PKINDX_STYLE(pknd, cluster));
+		style = pstyle.s.style;
+		/* Validate STYLE # */
+		if (style >= CVMX_PKI_NUM_INTERNAL_STYLE)
+			continue;
+		style_cfg.u64 = csr_rd_node(
+			node, CVMX_PKI_CLX_STYLEX_CFG(style, cluster));
+		style_cfg.s.lenerr_en = l2len_err;
+		style_cfg.s.maxerr_en = maxframe_err;
+		style_cfg.s.minerr_en = minframe_err;
+		csr_wr_node(node, CVMX_PKI_CLX_STYLEX_CFG(style, cluster),
+			    style_cfg.u64);
+	}
+}
+
+/**
+ * Disables maximum & minimum frame length checks
+ *
+ * @param node  Node number.
+ * @param pknd  PKIND to disable error for.
+ */
+void cvmx_pki_dis_frame_len_chk(int node, int pknd)
+{
+	int style;
+	unsigned int cluster = 0;
+	cvmx_pki_clx_pkindx_style_t pstyle;
+	cvmx_pki_clx_stylex_cfg_t style_cfg;
+
+	while (cluster < CVMX_PKI_NUM_CLUSTER) {
+		pstyle.u64 = csr_rd_node(
+			node, CVMX_PKI_CLX_PKINDX_STYLE(pknd, cluster));
+		style = pstyle.s.style;
+		style_cfg.u64 = csr_rd_node(
+			node, CVMX_PKI_CLX_STYLEX_CFG(style, cluster));
+		style_cfg.s.maxerr_en = 0;
+		style_cfg.s.minerr_en = 0;
+		csr_wr_node(node, CVMX_PKI_CLX_STYLEX_CFG(style, cluster),
+			    style_cfg.u64);
+		cluster++;
+	}
+}
+
+/**
+ * This function shows the qpg table entries, read directly from hardware.
+ *
+ * @param node	Node number
+ * @param num_entry  Number of entries to show
+ */
+void cvmx_pki_show_qpg_entries(int node, uint16_t num_entry)
+{
+	int index;
+	cvmx_pki_qpg_tblx_t qpg_tbl;
+
+	if (num_entry > CVMX_PKI_NUM_QPG_ENTRY)
+		num_entry = CVMX_PKI_NUM_QPG_ENTRY;
+	for (index = 0; index < num_entry; index++) {
+		qpg_tbl.u64 = csr_rd_node(node, CVMX_PKI_QPG_TBLX(index));
+		debug("\n%d	", index);
+		debug("PADD %-16lu", (unsigned long)qpg_tbl.s.padd);
+		debug("GRP_OK %-16lu", (unsigned long)qpg_tbl.s.grp_ok);
+		debug("GRP_BAD %-16lu", (unsigned long)qpg_tbl.s.grp_bad);
+		debug("LAURA %-16lu", (unsigned long)qpg_tbl.s.laura);
+	}
+}
+
+/**
+ * This function shows the pcam table in raw format,
+ * read directly from hardware.
+ *
+ * @param node  Node number
+ */
+void cvmx_pki_show_pcam_entries(int node)
+{
+	int cluster;
+	int index;
+	int bank;
+
+	for (cluster = 0; cluster < (int)CVMX_PKI_NUM_CLUSTER; cluster++) {
+		for (bank = 0; bank < 2; bank++) {
+			debug("\n--------------Cluster %1d Bank %1d-------------\n",
+			      cluster, bank);
+			debug("index         TERM                 DATA,                ACTION");
+			for (index = 0; index < CVMX_PKI_NUM_PCAM_ENTRY;
+			     index++) {
+				debug("\n%d", index);
+				debug("             %-16lx",
+				      (unsigned long)csr_rd_node(
+					      node,
+					      CVMX_PKI_CLX_PCAMX_TERMX(
+						      cluster, bank, index)));
+				debug("     %-16lx",
+				      (unsigned long)csr_rd_node(
+					      node,
+					      CVMX_PKI_CLX_PCAMX_MATCHX(
+						      cluster, bank, index)));
+				debug("     %-16lx",
+				      (unsigned long)csr_rd_node(
+					      node,
+					      CVMX_PKI_CLX_PCAMX_ACTIONX(
+						      cluster, bank, index)));
+			}
+		}
+	}
+}
+
+/**
+ * This function shows the valid entries in readable format,
+ * read directly from hardware.
+ *
+ * @param node  Node number.
+ */
+void cvmx_pki_show_valid_pcam_entries(int node)
+{
+	int cluster;
+	int index;
+	int bank;
+	cvmx_pki_clx_pcamx_termx_t term;
+	cvmx_pki_clx_pcamx_matchx_t match;
+	cvmx_pki_clx_pcamx_actionx_t act;
+
+	for (cluster = 0; cluster < (int)CVMX_PKI_NUM_CLUSTER; cluster++) {
+		for (bank = 0; bank < 2; bank++) {
+			debug("\n--------------Cluster %1d Bank %1d---------------------\n",
+			      cluster, bank);
+			debug("%-10s%-17s%-19s%-18s", "index", "TERM1:TERM0",
+			      "Style1:Style0", "Data1:Data0");
+			debug("%-6s", "ACTION[pmc:style_add:pf:setty:advance]");
+			for (index = 0; index < CVMX_PKI_NUM_PCAM_ENTRY;
+			     index++) {
+				term.u64 = csr_rd_node(
+					node, CVMX_PKI_CLX_PCAMX_TERMX(
+						      cluster, bank, index));
+				if (term.s.valid) {
+					match.u64 = csr_rd_node(
+						node,
+						CVMX_PKI_CLX_PCAMX_MATCHX(
+							cluster, bank, index));
+					act.u64 = csr_rd_node(
+						node,
+						CVMX_PKI_CLX_PCAMX_ACTIONX(
+							cluster, bank, index));
+					debug("\n%-13d", index);
+					debug("%-2x:%x", term.s.term1,
+					      term.s.term0);
+					debug("	      %-2x:%x", term.s.style1,
+					      term.s.style0);
+					debug("        %-8x:%x", match.s.data1,
+					      match.s.data0);
+					debug("            %-2x:%-2x       :%-1x :%2x   :%-2x",
+					      act.s.pmc, act.s.style_add,
+					      act.s.pf, act.s.setty,
+					      act.s.advance);
+				}
+			}
+		}
+	}
+}
+
+/**
+ * This function shows the pkind attributes in readable format,
+ * read directly from hardware.
+ *
+ * @param node  Node number
+ * @param pkind  PKIND info to print
+ */
+void cvmx_pki_show_pkind_attributes(int node, int pkind)
+{
+	unsigned int cluster = 0;
+	int index;
+	cvmx_pki_pkindx_icgsel_t icgsel;
+	cvmx_pki_clx_pkindx_style_t pstyle;
+	cvmx_pki_icgx_cfg_t icg_cfg;
+	cvmx_pki_clx_stylex_cfg_t style_cfg;
+	cvmx_pki_clx_stylex_alg_t style_alg;
+
+	if (pkind >= CVMX_PKI_NUM_PKIND) {
+		debug("ERROR: PKIND %d is beyond range\n", pkind);
+		return;
+	}
+	debug("Showing stats for pkind %d------------------\n", pkind);
+	icgsel.u64 = csr_rd_node(node, CVMX_PKI_PKINDX_ICGSEL(pkind));
+	debug("cluster group:	%d\n", icgsel.s.icg);
+	icg_cfg.u64 = csr_rd_node(node, CVMX_PKI_ICGX_CFG(icgsel.s.icg));
+	debug("cluster mask of the group:	0x%x\n", icg_cfg.s.clusters);
+
+	while (cluster < CVMX_PKI_NUM_CLUSTER) {
+		if (icg_cfg.s.clusters & (0x01L << cluster)) {
+			debug("pkind %d config 0x%llx\n", pkind,
+			      (unsigned long long)csr_rd_node(
+				      node,
+				      CVMX_PKI_CLX_PKINDX_CFG(pkind, cluster)));
+			pstyle.u64 = csr_rd_node(
+				node,
+				CVMX_PKI_CLX_PKINDX_STYLE(pkind, cluster));
+			debug("initial parse Mode: %d\n", pstyle.s.pm);
+			debug("initial_style: %d\n", pstyle.s.style);
+			style_alg.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_STYLEX_ALG(pstyle.s.style,
+							      cluster));
+			debug("style_alg: 0x%llx\n",
+			      (unsigned long long)style_alg.u64);
+			style_cfg.u64 = csr_rd_node(
+				node, CVMX_PKI_CLX_STYLEX_CFG(pstyle.s.style,
+							      cluster));
+			debug("style_cfg: 0x%llx\n",
+			      (unsigned long long)style_cfg.u64);
+			debug("style_cfg2: 0x%llx\n",
+			      (unsigned long long)csr_rd_node(
+				      node, CVMX_PKI_CLX_STYLEX_CFG2(
+						    pstyle.s.style, cluster)));
+			debug("style_buf: 0x%llx\n",
+			      (unsigned long long)csr_rd_node(
+				      node,
+				      CVMX_PKI_STYLEX_BUF(pstyle.s.style)));
+			break;
+		}
+	}
+	debug("qpg base: %d\n", style_cfg.s.qpg_base);
+	debug("qpg qos: %d\n", style_alg.s.qpg_qos);
+	for (index = 0; index < 8; index++) {
+		debug("qpg index %d: 0x%llx\n", (index + style_cfg.s.qpg_base),
+		      (unsigned long long)csr_rd_node(
+			      node,
+			      CVMX_PKI_QPG_TBLX(style_cfg.s.qpg_base + index)));
+	}
+}
+
+static void readcorrect(int node, u64 value, u64 addr)
+{
+	int cnt = 0;
+
+	while (value >= (1ull << 48) && cnt++ < 20)
+		value = csr_rd_node(node, addr);
+	if (cnt >= 20)
+		debug("count stuck for 0x%llx\n", (unsigned long long)addr);
+}
+
+/**
+ * Get the status counters for index from PKI.
+ *
+ * @param node  Node number
+ * @param index  PKIND number (if PKI_STATS_CTL:mode=0) or
+ *    style(flow) number (if PKI_STATS_CTL:mode=1)
+ * @param status   Where to put the results.
+ */
+void cvmx_pki_get_stats(int node, int index, struct cvmx_pki_port_stats *status)
+{
+	cvmx_pki_statx_stat0_t stat0;
+	cvmx_pki_statx_stat1_t stat1;
+	cvmx_pki_statx_stat2_t stat2;
+	cvmx_pki_statx_stat3_t stat3;
+	cvmx_pki_statx_stat4_t stat4;
+	cvmx_pki_statx_stat5_t stat5;
+	cvmx_pki_statx_stat6_t stat6;
+	cvmx_pki_statx_stat7_t stat7;
+	cvmx_pki_statx_stat8_t stat8;
+	cvmx_pki_statx_stat9_t stat9;
+	cvmx_pki_statx_stat10_t stat10;
+	cvmx_pki_statx_stat11_t stat11;
+	cvmx_pki_statx_stat14_t stat14;
+	cvmx_pki_statx_stat15_t stat15;
+	cvmx_pki_statx_stat16_t stat16;
+	cvmx_pki_statx_stat17_t stat17;
+	cvmx_pki_statx_hist0_t hist0;
+	cvmx_pki_statx_hist1_t hist1;
+	cvmx_pki_statx_hist2_t hist2;
+	cvmx_pki_statx_hist3_t hist3;
+	cvmx_pki_statx_hist4_t hist4;
+	cvmx_pki_statx_hist5_t hist5;
+	cvmx_pki_statx_hist6_t hist6;
+	cvmx_pki_pkndx_inb_stat0_t inb_stat0;
+	cvmx_pki_pkndx_inb_stat1_t inb_stat1;
+	cvmx_pki_pkndx_inb_stat2_t inb_stat2;
+
+	/* Accessing PKI stat registers can timeout based on the Errata
+	 * PKI-20775, disable SLI_INT_SUM[RML_TO] before reading the stats
+	 * enable back after clearing the interrupt.
+	 */
+	cvmx_error_intsn_disable_v3(node, 0x1f000);
+	stat0.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT0(index));
+	readcorrect(node, stat0.u64, CVMX_PKI_STATX_STAT0(index));
+
+	stat1.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT1(index));
+	readcorrect(node, stat1.u64, CVMX_PKI_STATX_STAT1(index));
+
+	stat2.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT2(index));
+	readcorrect(node, stat2.u64, CVMX_PKI_STATX_STAT2(index));
+
+	stat3.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT3(index));
+	readcorrect(node, stat3.u64, CVMX_PKI_STATX_STAT3(index));
+
+	stat4.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT4(index));
+	readcorrect(node, stat4.u64, CVMX_PKI_STATX_STAT4(index));
+
+	stat5.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT5(index));
+	readcorrect(node, stat5.u64, CVMX_PKI_STATX_STAT5(index));
+
+	stat6.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT6(index));
+	readcorrect(node, stat6.u64, CVMX_PKI_STATX_STAT6(index));
+
+	stat7.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT7(index));
+	readcorrect(node, stat7.u64, CVMX_PKI_STATX_STAT7(index));
+
+	stat8.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT8(index));
+	readcorrect(node, stat8.u64, CVMX_PKI_STATX_STAT8(index));
+
+	stat9.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT9(index));
+	readcorrect(node, stat9.u64, CVMX_PKI_STATX_STAT9(index));
+
+	stat10.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT10(index));
+	readcorrect(node, stat10.u64, CVMX_PKI_STATX_STAT10(index));
+
+	stat11.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT11(index));
+	readcorrect(node, stat11.u64, CVMX_PKI_STATX_STAT11(index));
+
+	stat14.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT14(index));
+	readcorrect(node, stat14.u64, CVMX_PKI_STATX_STAT14(index));
+
+	stat15.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT15(index));
+	readcorrect(node, stat15.u64, CVMX_PKI_STATX_STAT15(index));
+
+	stat16.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT16(index));
+	readcorrect(node, stat16.u64, CVMX_PKI_STATX_STAT16(index));
+
+	stat17.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT17(index));
+	readcorrect(node, stat17.u64, CVMX_PKI_STATX_STAT17(index));
+
+	hist0.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST0(index));
+	readcorrect(node, hist0.u64, CVMX_PKI_STATX_HIST0(index));
+
+	hist1.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST1(index));
+	readcorrect(node, hist1.u64, CVMX_PKI_STATX_HIST1(index));
+
+	hist2.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST2(index));
+	readcorrect(node, hist2.u64, CVMX_PKI_STATX_HIST2(index));
+
+	hist3.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST3(index));
+	readcorrect(node, hist3.u64, CVMX_PKI_STATX_HIST3(index));
+
+	hist4.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST4(index));
+	readcorrect(node, hist4.u64, CVMX_PKI_STATX_HIST4(index));
+
+	hist5.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST5(index));
+	readcorrect(node, hist5.u64, CVMX_PKI_STATX_HIST5(index));
+
+	hist6.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST6(index));
+	readcorrect(node, hist6.u64, CVMX_PKI_STATX_HIST6(index));
+
+	inb_stat0.u64 = csr_rd_node(node, CVMX_PKI_PKNDX_INB_STAT0(index));
+	inb_stat1.u64 = csr_rd_node(node, CVMX_PKI_PKNDX_INB_STAT1(index));
+	inb_stat2.u64 = csr_rd_node(node, CVMX_PKI_PKNDX_INB_STAT2(index));
+
+	status->dropped_octets = stat4.s.drp_octs;
+	status->dropped_packets = stat3.s.drp_pkts;
+	status->octets = stat1.s.octs;
+	status->pci_raw_packets = stat2.s.raw;
+	status->packets = stat0.s.pkts;
+	status->multicast_packets = stat6.s.mcast;
+	status->broadcast_packets = stat5.s.bcast;
+	status->len_64_packets = hist0.s.h1to63;
+	status->len_65_127_packets = hist1.s.h64to127;
+	status->len_128_255_packets = hist2.s.h128to255;
+	status->len_256_511_packets = hist3.s.h256to511;
+	status->len_512_1023_packets = hist4.s.h512to1023;
+	status->len_1024_1518_packets = hist5.s.h1024to1518;
+	status->len_1519_max_packets = hist6.s.h1519;
+	status->fcs_align_err_packets = stat7.s.fcs;
+	status->runt_packets = stat9.s.undersz;
+	status->runt_crc_packets = stat8.s.frag;
+	status->oversize_packets = stat11.s.oversz;
+	status->oversize_crc_packets = stat10.s.jabber;
+	status->mcast_l2_red_packets = stat15.s.drp_mcast;
+	status->bcast_l2_red_packets = stat14.s.drp_bcast;
+	status->mcast_l3_red_packets = stat17.s.drp_mcast;
+	status->bcast_l3_red_packets = stat16.s.drp_bcast;
+	status->inb_packets = inb_stat0.s.pkts;
+	status->inb_octets = inb_stat1.s.octs;
+	status->inb_errors = inb_stat2.s.errs;
+	/* Enable SLI_INT_SUM[RML_TO] interrupt after clear the pending interrupt. */
+	csr_wr_node(node, CVMX_CIU3_ISCX_W1C(0x1f000), 1);
+	cvmx_error_intsn_enable_v3(node, 0x1f000);
+}
+
+/**
+ * Clear the statistics counters for a port.
+ *
+ * @param node  Node number
+ * @param port  Port number (ipd_port) to get statistics for.
+ *    Make sure PKI_STATS_CTL:mode is set to 0 for collecting per port/pkind stats.
+ */
+void cvmx_pki_clear_port_stats(int node, uint64_t port)
+{
+	int xipd = cvmx_helper_node_to_ipd_port(node, port);
+	int xiface = cvmx_helper_get_interface_num(xipd);
+	int index = cvmx_helper_get_interface_index_num(port);
+	int pknd = cvmx_helper_get_pknd(xiface, index);
+
+	cvmx_pki_statx_stat0_t stat0;
+	cvmx_pki_statx_stat1_t stat1;
+	cvmx_pki_statx_stat2_t stat2;
+	cvmx_pki_statx_stat3_t stat3;
+	cvmx_pki_statx_stat4_t stat4;
+	cvmx_pki_statx_stat5_t stat5;
+	cvmx_pki_statx_stat6_t stat6;
+	cvmx_pki_statx_stat7_t stat7;
+	cvmx_pki_statx_stat8_t stat8;
+	cvmx_pki_statx_stat9_t stat9;
+	cvmx_pki_statx_stat10_t stat10;
+	cvmx_pki_statx_stat11_t stat11;
+	cvmx_pki_statx_stat14_t stat14;
+	cvmx_pki_statx_stat15_t stat15;
+	cvmx_pki_statx_stat16_t stat16;
+	cvmx_pki_statx_stat17_t stat17;
+	cvmx_pki_statx_hist0_t hist0;
+	cvmx_pki_statx_hist1_t hist1;
+	cvmx_pki_statx_hist2_t hist2;
+	cvmx_pki_statx_hist3_t hist3;
+	cvmx_pki_statx_hist4_t hist4;
+	cvmx_pki_statx_hist5_t hist5;
+	cvmx_pki_statx_hist6_t hist6;
+	cvmx_pki_pkndx_inb_stat0_t inb_stat0;
+	cvmx_pki_pkndx_inb_stat1_t inb_stat1;
+	cvmx_pki_pkndx_inb_stat2_t inb_stat2;
+
+	stat0.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT0(pknd));
+	stat1.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT1(pknd));
+	stat2.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT2(pknd));
+	stat3.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT3(pknd));
+	stat4.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT4(pknd));
+	stat5.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT5(pknd));
+	stat6.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT6(pknd));
+	stat7.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT7(pknd));
+	stat8.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT8(pknd));
+	stat9.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT9(pknd));
+	stat10.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT10(pknd));
+	stat11.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT11(pknd));
+	stat14.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT14(pknd));
+	stat15.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT15(pknd));
+	stat16.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT16(pknd));
+	stat17.u64 = csr_rd_node(node, CVMX_PKI_STATX_STAT17(pknd));
+	hist0.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST0(pknd));
+	hist1.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST1(pknd));
+	hist2.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST2(pknd));
+	hist3.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST3(pknd));
+	hist4.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST4(pknd));
+	hist5.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST5(pknd));
+	hist6.u64 = csr_rd_node(node, CVMX_PKI_STATX_HIST6(pknd));
+	inb_stat0.u64 = csr_rd_node(node, CVMX_PKI_PKNDX_INB_STAT0(pknd));
+	inb_stat1.u64 = csr_rd_node(node, CVMX_PKI_PKNDX_INB_STAT1(pknd));
+	inb_stat2.u64 = csr_rd_node(node, CVMX_PKI_PKNDX_INB_STAT2(pknd));
+
+	stat4.s.drp_octs = 0;
+	stat3.s.drp_pkts = 0;
+	stat1.s.octs = 0;
+	stat2.s.raw = 0;
+	stat0.s.pkts = 0;
+	stat6.s.mcast = 0;
+	stat5.s.bcast = 0;
+	hist0.s.h1to63 = 0;
+	hist1.s.h64to127 = 0;
+	hist2.s.h128to255 = 0;
+	hist3.s.h256to511 = 0;
+	hist4.s.h512to1023 = 0;
+	hist5.s.h1024to1518 = 0;
+	hist6.s.h1519 = 0;
+	stat7.s.fcs = 0;
+	stat9.s.undersz = 0;
+	stat8.s.frag = 0;
+	stat11.s.oversz = 0;
+	stat10.s.jabber = 0;
+	stat15.s.drp_mcast = 0;
+	stat14.s.drp_bcast = 0;
+	stat17.s.drp_mcast = 0;
+	stat16.s.drp_bcast = 0;
+	inb_stat0.s.pkts = 0;
+	inb_stat1.s.octs = 0;
+	inb_stat2.s.errs = 0;
+
+	csr_wr_node(node, CVMX_PKI_STATX_STAT0(pknd), stat0.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT1(pknd), stat1.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT2(pknd), stat2.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT3(pknd), stat3.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT4(pknd), stat4.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT5(pknd), stat5.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT6(pknd), stat6.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT7(pknd), stat7.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT8(pknd), stat8.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT9(pknd), stat9.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT10(pknd), stat10.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT11(pknd), stat11.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT14(pknd), stat14.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT15(pknd), stat15.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT16(pknd), stat16.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_STAT17(pknd), stat17.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_HIST0(pknd), hist0.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_HIST1(pknd), hist1.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_HIST2(pknd), hist2.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_HIST3(pknd), hist3.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_HIST4(pknd), hist4.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_HIST5(pknd), hist5.u64);
+	csr_wr_node(node, CVMX_PKI_STATX_HIST6(pknd), hist6.u64);
+	csr_wr_node(node, CVMX_PKI_PKNDX_INB_STAT0(pknd), inb_stat0.u64);
+	csr_wr_node(node, CVMX_PKI_PKNDX_INB_STAT1(pknd), inb_stat1.u64);
+	csr_wr_node(node, CVMX_PKI_PKNDX_INB_STAT2(pknd), inb_stat2.u64);
+}
Signature · 1 line
2.35.1
#30 lore raw

PATCH 34/52 mips: octeon: Add cvmx-pko.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-pko.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-pko.c | 1110 ++++++++++++++++++++++++++++++
 1 file changed, 1110 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-pko.c
arch/mips/mach-octeon/cvmx-pko.c new file +1110 -0
new file mode 100644
index 000000000000..6340020410b0
@@ -0,0 +1,1110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ *
+ * Support library for the hardware Packet Output unit.
+ */
+
+#include <errno.h>
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-iob-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-pko3.h>
+#include <mach/cvmx-pko3-queue.h>
+#include <mach/cvmx-pko3-resources.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#include <mach/cvmx-helper-bgx.h>
+#include <mach/cvmx-helper-cfg.h>
+#include <mach/cvmx-helper-util.h>
+#include <mach/cvmx-helper-pki.h>
+#include <mach/cvmx-helper-pko.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define CVMX_PKO_NQ_PER_PORT_MAX 32
+
+static cvmx_pko_return_value_t cvmx_pko2_config_port(short ipd_port,
+						     int base_queue,
+						     int num_queues,
+						     const u8 priority[]);
+
+static const int debug;
+
+/**
+ * Internal state of packet output
+ */
+
+/*
+ * PKO port iterator
+ * XXX this macro only works for 68XX
+ */
+
+#define pko_for_each_port(__p)                                                 \
+	for (__p = 0; __p < CVMX_HELPER_CFG_MAX_PKO_PORT; __p++)               \
+		if (__cvmx_helper_cfg_pko_queue_base(__p) !=                   \
+		    CVMX_HELPER_CFG_INVALID_VALUE)
+
+/*
+ * @INTERNAL
+ *
+ * Get INT for a port
+ *
+ * @param interface
+ * @param index
+ * @return the INT value on success and -1 on error
+ *
+ * This function is only for CN68XX.
+ */
+static int __cvmx_pko_int(int interface, int index)
+{
+	cvmx_helper_cfg_assert(interface < CVMX_HELPER_MAX_IFACE);
+	cvmx_helper_cfg_assert(index >= 0);
+
+	switch (interface) {
+	case 0:
+		cvmx_helper_cfg_assert(index < 4);
+		return index;
+	case 1:
+		cvmx_helper_cfg_assert(index == 0);
+		return 4;
+	case 2:
+		cvmx_helper_cfg_assert(index < 4);
+		return index + 8;
+	case 3:
+		cvmx_helper_cfg_assert(index < 4);
+		return index + 0xC;
+	case 4:
+		cvmx_helper_cfg_assert(index < 4);
+		return index + 0x10;
+	case 5:
+		cvmx_helper_cfg_assert(index < 256);
+		return 0x1C;
+	case 6:
+		cvmx_helper_cfg_assert(index < 256);
+		return 0x1D;
+	case 7:
+		cvmx_helper_cfg_assert(index < 32);
+		return 0x1E;
+	case 8:
+		cvmx_helper_cfg_assert(index < 8);
+		return 0x1F;
+	}
+
+	return -1;
+}
+
+int cvmx_pko_get_base_pko_port(int interface, int index)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE))
+		return cvmx_helper_get_ipd_port(interface, index);
+	else if (octeon_has_feature(OCTEON_FEATURE_PKND))
+		return __cvmx_helper_cfg_pko_port_base(interface, index);
+	else
+		return cvmx_helper_get_ipd_port(interface, index);
+}
+
+int cvmx_pko_get_num_pko_ports(int interface, int index)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE))
+		return 1;
+	if (octeon_has_feature(OCTEON_FEATURE_PKND))
+		return __cvmx_helper_cfg_pko_port_num(interface, index);
+	else
+		return 1;
+}
+
+int cvmx_pko_get_base_queue(int port)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE)) {
+		return cvmx_pko3_get_queue_base(port);
+	} else if (octeon_has_feature(OCTEON_FEATURE_PKND)) {
+		return __cvmx_helper_cfg_pko_queue_base(
+			cvmx_helper_cfg_ipd2pko_port_base(port));
+	} else {
+		if (port < 48)
+			return cvmx_pko_queue_table[port].ccppp_queue_base;
+		else
+			return CVMX_PKO_ILLEGAL_QUEUE;
+	}
+}
+
+/**
+ * For a given PKO port number, return the base output queue
+ * for the port.
+ *
+ * @param pko_port   PKO port number
+ * @return           Base output queue
+ */
+int cvmx_pko_get_base_queue_pkoid(int pko_port)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE))
+		return cvmx_pko3_get_queue_base(pko_port);
+	return __cvmx_helper_cfg_pko_queue_base(pko_port);
+}
+
+/**
+ * For a given PKO port number, return the number of output queues
+ * for the port.
+ *
+ * @param pko_port	PKO port number
+ * @return		the number of output queues
+ */
+int cvmx_pko_get_num_queues_pkoid(int pko_port)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE))
+		return cvmx_pko3_get_queue_num(pko_port);
+	return __cvmx_helper_cfg_pko_queue_num(pko_port);
+}
+
+int cvmx_pko_get_num_queues(int port)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE)) {
+		return cvmx_pko3_get_queue_num(port);
+	} else if (octeon_has_feature(OCTEON_FEATURE_PKND)) {
+		return __cvmx_helper_cfg_pko_queue_num(
+			cvmx_helper_cfg_ipd2pko_port_base(port));
+	} else {
+		if (port < 48)
+			return cvmx_pko_queue_table[port].ccppp_num_queues;
+	}
+	return 0;
+}
+
+/**
+ * Show queues for the internal ports
+ */
+void cvmx_pko_show_queue_map(void)
+{
+	if (octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE)) {
+		debug("%s: not supported on this chip\n", __func__);
+		return;
+	} else if (octeon_has_feature(OCTEON_FEATURE_PKND)) {
+		int port;
+
+		pko_for_each_port(port) {
+			debug("pko_port %d (interface%d index%d) has %d queues (queue base = %d)\n",
+			      port, __cvmx_helper_cfg_pko_port_interface(port),
+			      __cvmx_helper_cfg_pko_port_index(port),
+			      __cvmx_helper_cfg_pko_queue_num(port),
+			      __cvmx_helper_cfg_pko_queue_base(port));
+		}
+	} else {
+		int port;
+		int pko_output_ports;
+
+		pko_output_ports = 40;
+		debug("pko queue info\n");
+		for (port = 0; port < pko_output_ports; port++) {
+			debug("%3d=%3d-%3d ", port,
+			      cvmx_pko_get_base_queue(port),
+			      cvmx_pko_get_base_queue(port) +
+				      cvmx_pko_get_num_queues(port) - 1);
+			if (((port + 1) % 4) == 0)
+				debug("\n");
+		}
+		debug("\n");
+	}
+}
+
+/*
+ * Allocate memory for PKO engines.
+ *
+ * @param engine is the PKO engine ID.
+ * @return # of 2KB-chunks allocated to this PKO engine.
+ */
+static int __cvmx_pko_memory_per_engine_o68(int engine)
+{
+	/* CN68XX has 40KB to devide between the engines in 2KB chunks */
+	int max_engine;
+	int size_per_engine;
+	int size;
+
+	max_engine = __cvmx_helper_cfg_pko_max_engine();
+	size_per_engine = 40 / 2 / max_engine;
+
+	if (engine >= max_engine)
+		/* Unused engines get no space */
+		size = 0;
+	else if (engine == max_engine - 1)
+		/*
+		 * The last engine gets all the space lost by rounding. This means
+		 * the ILK gets the most space
+		 */
+		size = 40 / 2 - engine * size_per_engine;
+	else
+		/* All other engines get the same space */
+		size = size_per_engine;
+
+	return size;
+}
+
+/*
+ * Setup one-to-one mapping between PKO2 iport and eport.
+ * @INTERNAL
+ */
+static void __cvmx_pko2_chip_init(void)
+{
+	int i;
+	int interface, index, port;
+	cvmx_helper_interface_mode_t mode;
+	union cvmx_pko_mem_iport_ptrs config;
+
+	/*
+	 * Initialize every iport with the invalid eid.
+	 */
+#define CVMX_O68_PKO2_INVALID_EID 31
+	config.u64 = 0;
+	config.s.eid = CVMX_O68_PKO2_INVALID_EID;
+	for (i = 0; i < CVMX_HELPER_CFG_MAX_PKO_PORT; i++) {
+		config.s.ipid = i;
+		csr_wr(CVMX_PKO_MEM_IPORT_PTRS, config.u64);
+	}
+
+	/*
+	 * Set up PKO_MEM_IPORT_PTRS
+	 */
+	pko_for_each_port(port) {
+		interface = __cvmx_helper_cfg_pko_port_interface(port);
+		index = __cvmx_helper_cfg_pko_port_index(port);
+		mode = cvmx_helper_interface_get_mode(interface);
+
+		if (mode == CVMX_HELPER_INTERFACE_MODE_DISABLED)
+			continue;
+
+		config.s.ipid = port;
+		config.s.qos_mask = 0xff;
+		config.s.crc = __cvmx_helper_get_has_fcs(interface);
+		config.s.min_pkt = __cvmx_helper_get_pko_padding(interface);
+		config.s.intr = __cvmx_pko_int(interface, index);
+		config.s.eid = __cvmx_helper_cfg_pko_port_eid(port);
+		config.s.pipe = (mode == CVMX_HELPER_INTERFACE_MODE_LOOP) ?
+					index :
+					      port;
+		csr_wr(CVMX_PKO_MEM_IPORT_PTRS, config.u64);
+	}
+}
+
+int __cvmx_pko_get_pipe(int interface, int index)
+{
+	/* The loopback ports do not have pipes */
+	if (cvmx_helper_interface_get_mode(interface) ==
+	    CVMX_HELPER_INTERFACE_MODE_LOOP)
+		return -1;
+	/* We use pko_port as the pipe. See __cvmx_pko_port_map_o68(). */
+	return cvmx_helper_get_pko_port(interface, index);
+}
+
+static void __cvmx_pko1_chip_init(void)
+{
+	int queue;
+	union cvmx_pko_mem_queue_ptrs config;
+	union cvmx_pko_reg_queue_ptrs1 config1;
+	const int port = CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID;
+
+	/* Initialize all queues to connect to port 63 (ILLEGAL_PID) */
+	for (queue = 0; queue < CVMX_PKO_MAX_OUTPUT_QUEUES; queue++) {
+		config1.u64 = 0;
+		config1.s.idx3 = 0;
+		config1.s.qid7 = queue >> 7;
+
+		config.u64 = 0;
+		config.s.tail = 1;
+		config.s.index = 0;
+		config.s.port = port;
+		config.s.queue = queue;
+		config.s.buf_ptr = 0;
+
+		csr_wr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);
+		csr_wr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);
+	}
+}
+
+/**
+ * Call before any other calls to initialize the packet
+ * output system.  This does chip global config, and should only be
+ * done by one core.
+ */
+void cvmx_pko_hw_init(u8 pool, unsigned int bufsize)
+{
+	union cvmx_pko_reg_cmd_buf config;
+	union cvmx_iob_fau_timeout fau_to;
+	int i;
+
+	if (debug)
+		debug("%s: pool=%u bufsz=%u\n", __func__, pool, bufsize);
+
+	/* chip-specific setup. */
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX))
+		__cvmx_pko2_chip_init();
+	else
+		__cvmx_pko1_chip_init();
+
+	/*
+	 * Set the size of the PKO command buffers to an odd number of
+	 * 64bit words. This allows the normal two word send to stay
+	 * aligned and never span a command word buffer.
+	 */
+	config.u64 = 0;
+	config.s.pool = pool;
+	config.s.size = bufsize / 8 - 1;
+	csr_wr(CVMX_PKO_REG_CMD_BUF, config.u64);
+
+	/*
+	 * Disable tagwait FAU timeout. This needs to be done before
+	 * anyone might start packet output using tags.
+	 */
+	fau_to.u64 = 0;
+	fau_to.s.tout_val = 0xfff;
+	fau_to.s.tout_enb = 0;
+	csr_wr(CVMX_IOB_FAU_TIMEOUT, fau_to.u64);
+
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		union cvmx_pko_reg_min_pkt min_pkt;
+
+		min_pkt.u64 = 0;
+		min_pkt.s.size1 = 59;
+		min_pkt.s.size2 = 59;
+		min_pkt.s.size3 = 59;
+		min_pkt.s.size4 = 59;
+		min_pkt.s.size5 = 59;
+		min_pkt.s.size6 = 59;
+		min_pkt.s.size7 = 59;
+		csr_wr(CVMX_PKO_REG_MIN_PKT, min_pkt.u64);
+	}
+
+	/*
+	 * If we aren't using all of the queues optimize PKO's
+	 * internal memory.
+	 */
+	if (OCTEON_IS_OCTEON2() || OCTEON_IS_MODEL(OCTEON_CN70XX)) {
+		int max_queues = __cvmx_helper_cfg_pko_max_queue();
+
+		if (OCTEON_IS_MODEL(OCTEON_CN68XX) && max_queues <= 32)
+			csr_wr(CVMX_PKO_REG_QUEUE_MODE, 3);
+		else if (max_queues <= 64)
+			csr_wr(CVMX_PKO_REG_QUEUE_MODE, 2);
+		else if (max_queues <= 128)
+			csr_wr(CVMX_PKO_REG_QUEUE_MODE, 1);
+		else
+			csr_wr(CVMX_PKO_REG_QUEUE_MODE, 0);
+		if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+			for (i = 0; i < 2; i++) {
+				union cvmx_pko_reg_engine_storagex
+					engine_storage;
+
+#define PKO_ASSIGN_ENGINE_STORAGE(index)                                       \
+	engine_storage.s.engine##index =                                       \
+		__cvmx_pko_memory_per_engine_o68(16 * i + (index))
+
+				engine_storage.u64 = 0;
+				PKO_ASSIGN_ENGINE_STORAGE(0);
+				PKO_ASSIGN_ENGINE_STORAGE(1);
+				PKO_ASSIGN_ENGINE_STORAGE(2);
+				PKO_ASSIGN_ENGINE_STORAGE(3);
+				PKO_ASSIGN_ENGINE_STORAGE(4);
+				PKO_ASSIGN_ENGINE_STORAGE(5);
+				PKO_ASSIGN_ENGINE_STORAGE(6);
+				PKO_ASSIGN_ENGINE_STORAGE(7);
+				PKO_ASSIGN_ENGINE_STORAGE(8);
+				PKO_ASSIGN_ENGINE_STORAGE(9);
+				PKO_ASSIGN_ENGINE_STORAGE(10);
+				PKO_ASSIGN_ENGINE_STORAGE(11);
+				PKO_ASSIGN_ENGINE_STORAGE(12);
+				PKO_ASSIGN_ENGINE_STORAGE(13);
+				PKO_ASSIGN_ENGINE_STORAGE(14);
+				PKO_ASSIGN_ENGINE_STORAGE(15);
+				csr_wr(CVMX_PKO_REG_ENGINE_STORAGEX(i),
+				       engine_storage.u64);
+			}
+		}
+	}
+}
+
+/**
+ * Enables the packet output hardware. It must already be
+ * configured.
+ */
+void cvmx_pko_enable(void)
+{
+	union cvmx_pko_reg_flags flags;
+
+	flags.u64 = csr_rd(CVMX_PKO_REG_FLAGS);
+	if (flags.s.ena_pko)
+		debug("Warning: Enabling PKO when PKO already enabled.\n");
+
+	flags.s.ena_dwb = cvmx_helper_cfg_opt_get(CVMX_HELPER_CFG_OPT_USE_DWB);
+	flags.s.ena_pko = 1;
+	/*
+	 * always enable big endian for 3-word command.  Does nothing
+	 * for 2-word.
+	 */
+	flags.s.store_be = 1;
+	csr_wr(CVMX_PKO_REG_FLAGS, flags.u64);
+}
+
+/**
+ * Disables the packet output. Does not affect any configuration.
+ */
+void cvmx_pko_disable(void)
+{
+	union cvmx_pko_reg_flags pko_reg_flags;
+
+	pko_reg_flags.u64 = csr_rd(CVMX_PKO_REG_FLAGS);
+	pko_reg_flags.s.ena_pko = 0;
+	csr_wr(CVMX_PKO_REG_FLAGS, pko_reg_flags.u64);
+}
+
+/**
+ * @INTERNAL
+ * Reset the packet output.
+ */
+static void __cvmx_pko_reset(void)
+{
+	union cvmx_pko_reg_flags pko_reg_flags;
+
+	pko_reg_flags.u64 = csr_rd(CVMX_PKO_REG_FLAGS);
+	pko_reg_flags.s.reset = 1;
+	csr_wr(CVMX_PKO_REG_FLAGS, pko_reg_flags.u64);
+}
+
+/**
+ * Shutdown and free resources required by packet output.
+ */
+void cvmx_pko_shutdown(void)
+{
+	int queue;
+
+	cvmx_pko_disable();
+
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
+		union cvmx_pko_mem_iqueue_ptrs config;
+
+		config.u64 = 0;
+		for (queue = 0; queue < CVMX_PKO_MAX_OUTPUT_QUEUES; queue++) {
+			config.s.qid = queue;
+			csr_wr(CVMX_PKO_MEM_IQUEUE_PTRS, config.u64);
+			cvmx_cmd_queue_shutdown(CVMX_CMD_QUEUE_PKO(queue));
+		}
+	} else {
+		union cvmx_pko_mem_queue_ptrs config;
+
+		for (queue = 0; queue < CVMX_PKO_MAX_OUTPUT_QUEUES; queue++) {
+			union cvmx_pko_reg_queue_ptrs1 config1;
+
+			config.u64 = 0;
+			config.s.tail = 1;
+			config.s.index = 0;
+			config.s.port = CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID;
+			config.s.queue = queue & 0x7f;
+			config.s.qos_mask = 0;
+			config.s.buf_ptr = 0;
+
+			config1.u64 = 0;
+			config1.s.qid7 = queue >> 7;
+			csr_wr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);
+			csr_wr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);
+			cvmx_cmd_queue_shutdown(CVMX_CMD_QUEUE_PKO(queue));
+		}
+	}
+
+	__cvmx_pko_reset();
+	cvmx_pko_queue_free_all();
+}
+
+/**
+ * Configure a output port and the associated queues for use.
+ *
+ * @param port       Port to configure.
+ * @param base_queue First queue number to associate with this port.
+ * @param num_queues Number of queues to associate with this port
+ * @param priority   Array of priority levels for each queue. Values are
+ *                   allowed to be 0-8. A value of 8 get 8 times the traffic
+ *                   of a value of 1.  A value of 0 indicates that no rounds
+ *                   will be participated in. These priorities can be changed
+ *                   on the fly while the pko is enabled. A priority of 9
+ *                   indicates that static priority should be used.  If static
+ *                   priority is used all queues with static priority must be
+ *                   contiguous starting at the base_queue, and lower numbered
+ *                   queues have higher priority than higher numbered queues.
+ *                   There must be num_queues elements in the array.
+ */
+cvmx_pko_return_value_t cvmx_pko_config_port(int port, int base_queue,
+					     int num_queues,
+					     const u8 priority[])
+{
+	cvmx_pko_return_value_t result_code;
+	int queue;
+	union cvmx_pko_mem_queue_ptrs config;
+	union cvmx_pko_reg_queue_ptrs1 config1;
+	int static_priority_base = -1;
+	int static_priority_end = -1;
+	int outputbuffer_pool = (int)cvmx_fpa_get_pko_pool();
+	u64 outputbuffer_pool_size = cvmx_fpa_get_pko_pool_block_size();
+
+	/* This function is not used for CN68XX */
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX))
+		return cvmx_pko2_config_port(port, base_queue, num_queues,
+					     priority);
+
+	if (debug)
+		debug("%s: port=%d queue=%d-%d pri %#x %#x %#x %#x\n", __func__,
+		      port, base_queue, (base_queue + num_queues - 1),
+		      priority[0], priority[1], priority[2], priority[3]);
+
+	/* The need to handle ILLEGAL_PID port argument
+	 * is obsolete now, the code here can be simplified.
+	 */
+
+	if (port >= CVMX_PKO_NUM_OUTPUT_PORTS &&
+	    port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {
+		debug("ERROR: %s: Invalid port %llu\n", __func__,
+		      (unsigned long long)port);
+		return CVMX_PKO_INVALID_PORT;
+	}
+
+	if (base_queue + num_queues > CVMX_PKO_MAX_OUTPUT_QUEUES) {
+		debug("ERROR: %s: Invalid queue range port = %lld base=%llu numques=%lld\n",
+		      __func__, (unsigned long long)port,
+		      (unsigned long long)base_queue,
+		      (unsigned long long)num_queues);
+		return CVMX_PKO_INVALID_QUEUE;
+	}
+
+	if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {
+		/*
+		 * Validate the static queue priority setup and set
+		 * static_priority_base and static_priority_end
+		 * accordingly.
+		 */
+		for (queue = 0; queue < num_queues; queue++) {
+			/* Find first queue of static priority */
+			int p_queue = queue % 16;
+
+			if (static_priority_base == -1 &&
+			    priority[p_queue] == CVMX_PKO_QUEUE_STATIC_PRIORITY)
+				static_priority_base = queue;
+			/* Find last queue of static priority */
+			if (static_priority_base != -1 &&
+			    static_priority_end == -1 &&
+			    priority[p_queue] !=
+				    CVMX_PKO_QUEUE_STATIC_PRIORITY &&
+			    queue)
+				static_priority_end = queue - 1;
+			else if (static_priority_base != -1 &&
+				 static_priority_end == -1 &&
+				 queue == num_queues - 1)
+				/* all queues're static priority */
+				static_priority_end = queue;
+
+			/*
+			 * Check to make sure all static priority
+			 * queues are contiguous.  Also catches some
+			 * cases of static priorites not starting at
+			 * queue 0.
+			 */
+			if (static_priority_end != -1 &&
+			    (int)queue > static_priority_end &&
+			    priority[p_queue] ==
+				    CVMX_PKO_QUEUE_STATIC_PRIORITY) {
+				debug("ERROR: %s: Static priority queues aren't contiguous or don't start at base queue. q: %d, eq: %d\n",
+				      __func__, (int)queue, static_priority_end);
+				return CVMX_PKO_INVALID_PRIORITY;
+			}
+		}
+		if (static_priority_base > 0) {
+			debug("ERROR: %s: Static priority queues don't start at base queue. sq: %d\n",
+			      __func__, static_priority_base);
+			return CVMX_PKO_INVALID_PRIORITY;
+		}
+	}
+
+	/*
+	 * At this point, static_priority_base and static_priority_end
+	 * are either both -1, or are valid start/end queue numbers
+	 */
+
+	result_code = CVMX_PKO_SUCCESS;
+
+	for (queue = 0; queue < num_queues; queue++) {
+		u64 *buf_ptr = NULL;
+		int p_queue = queue % 16;
+
+		config1.u64 = 0;
+		config1.s.idx3 = queue >> 3;
+		config1.s.qid7 = (base_queue + queue) >> 7;
+
+		config.u64 = 0;
+		config.s.tail = queue == (num_queues - 1);
+		config.s.index = queue;
+		config.s.port = port;
+		config.s.queue = base_queue + queue;
+
+		config.s.static_p = static_priority_base >= 0;
+		config.s.static_q = (int)queue <= static_priority_end;
+		config.s.s_tail = (int)queue == static_priority_end;
+		/*
+		 * Convert the priority into an enable bit field. Try
+		 * to space the bits out evenly so the packet don't
+		 * get grouped up.
+		 */
+		switch ((int)priority[p_queue]) {
+		case 0:
+			config.s.qos_mask = 0x00;
+			break;
+		case 1:
+			config.s.qos_mask = 0x01;
+			break;
+		case 2:
+			config.s.qos_mask = 0x11;
+			break;
+		case 3:
+			config.s.qos_mask = 0x49;
+			break;
+		case 4:
+			config.s.qos_mask = 0x55;
+			break;
+		case 5:
+			config.s.qos_mask = 0x57;
+			break;
+		case 6:
+			config.s.qos_mask = 0x77;
+			break;
+		case 7:
+			config.s.qos_mask = 0x7f;
+			break;
+		case 8:
+			config.s.qos_mask = 0xff;
+			break;
+		case CVMX_PKO_QUEUE_STATIC_PRIORITY:
+			config.s.qos_mask = 0xff;
+			break;
+		default:
+			debug("ERROR: %s: Invalid priority %llu\n", __func__,
+			      (unsigned long long)priority[p_queue]);
+			config.s.qos_mask = 0xff;
+			result_code = CVMX_PKO_INVALID_PRIORITY;
+			break;
+		}
+
+		if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {
+			cvmx_cmd_queue_result_t cmd_res;
+
+			cmd_res = cvmx_cmd_queue_initialize(
+				CVMX_CMD_QUEUE_PKO(base_queue + queue),
+				CVMX_PKO_MAX_QUEUE_DEPTH, outputbuffer_pool,
+				outputbuffer_pool_size -
+					CVMX_PKO_COMMAND_BUFFER_SIZE_ADJUST *
+						8);
+			if (cmd_res != CVMX_CMD_QUEUE_SUCCESS) {
+				switch (cmd_res) {
+				case CVMX_CMD_QUEUE_NO_MEMORY:
+					debug("ERROR: %s: Unable to allocate output buffer\n",
+					      __func__);
+					return CVMX_PKO_NO_MEMORY;
+				case CVMX_CMD_QUEUE_ALREADY_SETUP:
+					debug("ERROR: %s: Port already setup. port=%d\n",
+					      __func__, (int)port);
+					return CVMX_PKO_PORT_ALREADY_SETUP;
+				case CVMX_CMD_QUEUE_INVALID_PARAM:
+				default:
+					debug("ERROR: %s: Command queue initialization failed.\n",
+					      __func__);
+					return CVMX_PKO_CMD_QUEUE_INIT_ERROR;
+				}
+			}
+
+			buf_ptr = (u64 *)cvmx_cmd_queue_buffer(
+				CVMX_CMD_QUEUE_PKO(base_queue + queue));
+			config.s.buf_ptr = cvmx_ptr_to_phys(buf_ptr);
+		} else {
+			config.s.buf_ptr = 0;
+		}
+
+		CVMX_SYNCWS;
+
+		csr_wr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);
+		csr_wr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);
+	}
+
+	return result_code;
+}
+
+/*
+ * Configure queues for an internal port.
+ * @INTERNAL
+ * @param pko_port PKO internal port number
+ * @note this is the PKO2 equivalent to cvmx_pko_config_port()
+ */
+static cvmx_pko_return_value_t cvmx_pko2_config_port(short ipd_port,
+						     int base_queue,
+						     int num_queues,
+						     const u8 priority[])
+{
+	int queue, pko_port;
+	int static_priority_base;
+	int static_priority_end;
+	union cvmx_pko_mem_iqueue_ptrs config;
+	u64 *buf_ptr = NULL;
+	int outputbuffer_pool = (int)cvmx_fpa_get_pko_pool();
+	u64 outputbuffer_pool_size = cvmx_fpa_get_pko_pool_block_size();
+
+	pko_port = cvmx_helper_cfg_ipd2pko_port_base(ipd_port);
+
+	if (debug)
+		debug("%s: ipd_port %d pko_iport %d qbase %d qnum %d\n",
+		      __func__, ipd_port, pko_port, base_queue, num_queues);
+
+	static_priority_base = -1;
+	static_priority_end = -1;
+
+	/*
+	 * static queue priority validation
+	 */
+	for (queue = 0; queue < num_queues; queue++) {
+		int p_queue = queue % 16;
+
+		if (static_priority_base == -1 &&
+		    priority[p_queue] == CVMX_PKO_QUEUE_STATIC_PRIORITY)
+			static_priority_base = queue;
+
+		if (static_priority_base != -1 && static_priority_end == -1 &&
+		    priority[p_queue] != CVMX_PKO_QUEUE_STATIC_PRIORITY &&
+		    queue)
+			static_priority_end = queue - 1;
+		else if (static_priority_base != -1 &&
+			 static_priority_end == -1 && queue == num_queues - 1)
+			static_priority_end =
+				queue; /* all queues are static priority */
+
+		/*
+		 * Check to make sure all static priority queues are contiguous.
+		 * Also catches some cases of static priorites not starting from
+		 * queue 0.
+		 */
+		if (static_priority_end != -1 &&
+		    (int)queue > static_priority_end &&
+		    priority[p_queue] == CVMX_PKO_QUEUE_STATIC_PRIORITY) {
+			debug("ERROR: %s: Static priority queues aren't contiguous or don't start at base queue. q: %d, eq: %d\n",
+			      __func__, (int)queue, static_priority_end);
+		}
+		if (static_priority_base > 0) {
+			debug("ERROR: %s: Static priority queues don't start at base queue. sq: %d\n",
+			      __func__, static_priority_base);
+		}
+	}
+
+	/*
+	 * main loop to set the fields of CVMX_PKO_MEM_IQUEUE_PTRS for
+	 * each queue
+	 */
+	for (queue = 0; queue < num_queues; queue++) {
+		int p_queue = queue % 8;
+
+		config.u64 = 0;
+		config.s.index = queue;
+		config.s.qid = base_queue + queue;
+		config.s.ipid = pko_port;
+		config.s.tail = (queue == (num_queues - 1));
+		config.s.s_tail = (queue == static_priority_end);
+		config.s.static_p = (static_priority_base >= 0);
+		config.s.static_q = (queue <= static_priority_end);
+
+		/*
+		 * Convert the priority into an enable bit field.
+		 * Try to space the bits out evenly so the packet
+		 * don't get grouped up.
+		 */
+		switch ((int)priority[p_queue]) {
+		case 0:
+			config.s.qos_mask = 0x00;
+			break;
+		case 1:
+			config.s.qos_mask = 0x01;
+			break;
+		case 2:
+			config.s.qos_mask = 0x11;
+			break;
+		case 3:
+			config.s.qos_mask = 0x49;
+			break;
+		case 4:
+			config.s.qos_mask = 0x55;
+			break;
+		case 5:
+			config.s.qos_mask = 0x57;
+			break;
+		case 6:
+			config.s.qos_mask = 0x77;
+			break;
+		case 7:
+			config.s.qos_mask = 0x7f;
+			break;
+		case 8:
+			config.s.qos_mask = 0xff;
+			break;
+		case CVMX_PKO_QUEUE_STATIC_PRIORITY:
+			config.s.qos_mask = 0xff;
+			break;
+		default:
+			debug("ERROR: %s: Invalid priority %llu\n", __func__,
+			      (unsigned long long)priority[p_queue]);
+			config.s.qos_mask = 0xff;
+			break;
+		}
+
+		/*
+		 * The command queues
+		 */
+		{
+			cvmx_cmd_queue_result_t cmd_res;
+
+			cmd_res = cvmx_cmd_queue_initialize(
+				CVMX_CMD_QUEUE_PKO(base_queue + queue),
+				CVMX_PKO_MAX_QUEUE_DEPTH, outputbuffer_pool,
+				(outputbuffer_pool_size -
+				 CVMX_PKO_COMMAND_BUFFER_SIZE_ADJUST * 8));
+
+			if (cmd_res != CVMX_CMD_QUEUE_SUCCESS) {
+				switch (cmd_res) {
+				case CVMX_CMD_QUEUE_NO_MEMORY:
+					debug("ERROR: %s: Unable to allocate output buffer\n",
+					      __func__);
+					break;
+				case CVMX_CMD_QUEUE_ALREADY_SETUP:
+					debug("ERROR: %s: Port already setup\n",
+					      __func__);
+					break;
+				case CVMX_CMD_QUEUE_INVALID_PARAM:
+				default:
+					debug("ERROR: %s: Command queue initialization failed.",
+					      __func__);
+					break;
+				}
+				debug(" pko_port%d base_queue%d num_queues%d queue%d.\n",
+				      pko_port, base_queue, num_queues, queue);
+			}
+
+			buf_ptr = (u64 *)cvmx_cmd_queue_buffer(
+				CVMX_CMD_QUEUE_PKO(base_queue + queue));
+			config.s.buf_ptr = cvmx_ptr_to_phys(buf_ptr) >> 7;
+		}
+
+		CVMX_SYNCWS;
+		csr_wr(CVMX_PKO_MEM_IQUEUE_PTRS, config.u64);
+	}
+
+	/* Error detection is resirable here */
+	return 0;
+}
+
+/**
+ * Rate limit a PKO port to a max packets/sec. This function is only
+ * supported on CN51XX and higher, excluding CN58XX.
+ *
+ * @param port      Port to rate limit
+ * @param packets_s Maximum packet/sec
+ * @param burst     Maximum number of packets to burst in a row before rate
+ *                  limiting cuts in.
+ *
+ * @return Zero on success, negative on failure
+ */
+int cvmx_pko_rate_limit_packets(int port, int packets_s, int burst)
+{
+	union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;
+	union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;
+
+	pko_mem_port_rate0.u64 = 0;
+	pko_mem_port_rate0.s.pid = port;
+	pko_mem_port_rate0.s.rate_pkt = gd->bus_clk / packets_s / 16;
+	/* No cost per word since we are limited by packets/sec, not bits/sec */
+	pko_mem_port_rate0.s.rate_word = 0;
+
+	pko_mem_port_rate1.u64 = 0;
+	pko_mem_port_rate1.s.pid = port;
+	pko_mem_port_rate1.s.rate_lim =
+		((u64)pko_mem_port_rate0.s.rate_pkt * burst) >> 8;
+
+	csr_wr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);
+	csr_wr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);
+	return 0;
+}
+
+/**
+ * Rate limit a PKO port to a max bits/sec. This function is only
+ * supported on CN51XX and higher, excluding CN58XX.
+ *
+ * @param port   Port to rate limit
+ * @param bits_s PKO rate limit in bits/sec
+ * @param burst  Maximum number of bits to burst before rate
+ *               limiting cuts in.
+ *
+ * @return Zero on success, negative on failure
+ */
+int cvmx_pko_rate_limit_bits(int port, u64 bits_s, int burst)
+{
+	union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;
+	union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;
+	//	u64 clock_rate = cvmx_clock_get_rate(CVMX_CLOCK_SCLK);
+	u64 clock_rate = gd->bus_clk;
+	u64 tokens_per_bit = clock_rate * 16 / bits_s;
+
+	pko_mem_port_rate0.u64 = 0;
+	pko_mem_port_rate0.s.pid = port;
+	/*
+	 * Each packet has a 12 bytes of interframe gap, an 8 byte
+	 * preamble, and a 4 byte CRC. These are not included in the
+	 * per word count. Multiply by 8 to covert to bits and divide
+	 * by 256 for limit granularity.
+	 */
+	pko_mem_port_rate0.s.rate_pkt = (12 + 8 + 4) * 8 * tokens_per_bit / 256;
+	/* Each 8 byte word has 64bits */
+	pko_mem_port_rate0.s.rate_word = 64 * tokens_per_bit;
+
+	pko_mem_port_rate1.u64 = 0;
+	pko_mem_port_rate1.s.pid = port;
+	pko_mem_port_rate1.s.rate_lim = tokens_per_bit * burst / 256;
+
+	csr_wr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);
+	csr_wr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);
+	return 0;
+}
+
+/**
+ * Get the status counters for a port.
+ *
+ * @param ipd_port Port number (ipd_port) to get statistics for.
+ * @param clear    Set to 1 to clear the counters after they are read
+ * @param status   Where to put the results.
+ *
+ * Note:
+ *     - Only the doorbell for the base queue of the ipd_port is
+ *       collected.
+ *     - Retrieving the stats involves writing the index through
+ *       CVMX_PKO_REG_READ_IDX and reading the stat CSRs, in that
+ *       order. It is not MP-safe and caller should guarantee
+ *       atomicity.
+ */
+void cvmx_pko_get_port_status(u64 ipd_port, u64 clear,
+			      cvmx_pko_port_status_t *status)
+{
+	cvmx_pko_reg_read_idx_t pko_reg_read_idx;
+	cvmx_pko_mem_count0_t pko_mem_count0;
+	cvmx_pko_mem_count1_t pko_mem_count1;
+	int pko_port, port_base, port_limit;
+
+	if (octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE)) {
+		int xipd = cvmx_helper_node_to_ipd_port(cvmx_get_node_num(),
+							ipd_port);
+		cvmx_pko3_get_legacy_port_stats(xipd, clear, status);
+		return;
+	} else if (octeon_has_feature(OCTEON_FEATURE_PKND)) {
+		int interface = cvmx_helper_get_interface_num(ipd_port);
+		int index = cvmx_helper_get_interface_index_num(ipd_port);
+
+		port_base = cvmx_helper_get_pko_port(interface, index);
+		if (port_base == -1)
+			debug("Warning: Invalid port_base\n");
+		port_limit = port_base +
+			     cvmx_pko_get_num_pko_ports(interface, index);
+	} else {
+		port_base = ipd_port;
+		port_limit = port_base + 1;
+	}
+
+	/*
+	 * status->packets and status->octets
+	 */
+	status->packets = 0;
+	status->octets = 0;
+	pko_reg_read_idx.u64 = 0;
+
+	for (pko_port = port_base; pko_port < port_limit; pko_port++) {
+		/*
+		 * In theory, one doesn't need to write the index csr every
+		 * time as he can set pko_reg_read_idx.s.inc to increment
+		 * the index automatically. Need to find out exactly how XXX.
+		 */
+		pko_reg_read_idx.s.index = pko_port;
+		csr_wr(CVMX_PKO_REG_READ_IDX, pko_reg_read_idx.u64);
+
+		pko_mem_count0.u64 = csr_rd(CVMX_PKO_MEM_COUNT0);
+		status->packets += pko_mem_count0.s.count;
+		if (clear) {
+			pko_mem_count0.s.count = pko_port;
+			csr_wr(CVMX_PKO_MEM_COUNT0, pko_mem_count0.u64);
+		}
+
+		pko_mem_count1.u64 = csr_rd(CVMX_PKO_MEM_COUNT1);
+		status->octets += pko_mem_count1.s.count;
+		if (clear) {
+			pko_mem_count1.s.count = pko_port;
+			csr_wr(CVMX_PKO_MEM_COUNT1, pko_mem_count1.u64);
+		}
+	}
+
+	/*
+	 * status->doorbell
+	 */
+	cvmx_pko_mem_debug8_t debug8;
+
+	pko_reg_read_idx.s.index = cvmx_pko_get_base_queue(ipd_port);
+	csr_wr(CVMX_PKO_REG_READ_IDX, pko_reg_read_idx.u64);
+	debug8.u64 = csr_rd(CVMX_PKO_MEM_DEBUG8);
+	if (OCTEON_IS_MODEL(OCTEON_CN68XX))
+		status->doorbell = debug8.cn68xx.doorbell;
+	else
+		status->doorbell = debug8.cn58xx.doorbell;
+}
+
+/*
+ * Obtain the number of PKO commands pending in a queue
+ *
+ * @param queue is the queue identifier to be queried
+ * @return the number of commands pending transmission or -1 on error
+ */
+int cvmx_pko_queue_pend_count(cvmx_cmd_queue_id_t queue)
+{
+	int count;
+
+	if (octeon_has_feature(OCTEON_FEATURE_CN78XX_WQE)) {
+		int node = cvmx_get_node_num();
+
+		count = cvmx_pko3_dq_query(node, queue);
+	} else {
+		count = cvmx_cmd_queue_length(CVMX_CMD_QUEUE_PKO(queue));
+	}
+	return count;
+}
Signature · 1 line
2.35.1
#31 lore raw

PATCH 35/52 mips: octeon: Add cvmx-pko3.c

In reply to Stefan Roese

From: Aaron Williams <[email protected]>

Import cvmx-pko3.c from 2013 U-Boot. It will be used by the later added drivers to support networking on the MIPS Octeon II / III platforms.

---
 arch/mips/mach-octeon/cvmx-pko3.c | 2143 +++++++++++++++++++++++++++++
 1 file changed, 2143 insertions(+)
 create mode 100644 arch/mips/mach-octeon/cvmx-pko3.c
arch/mips/mach-octeon/cvmx-pko3.c new file +2143 -0
new file mode 100644
index 000000000000..dc180ea83ac3
@@ -0,0 +1,2143 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018-2022 Marvell International Ltd.
+ */
+
+#include <errno.h>
+#include <log.h>
+#include <time.h>
+#include <linux/delay.h>
+
+#include <mach/cvmx-regs.h>
+#include <mach/cvmx-csr.h>
+#include <mach/cvmx-bootmem.h>
+#include <mach/octeon-model.h>
+#include <mach/cvmx-fuse.h>
+#include <mach/octeon-feature.h>
+#include <mach/cvmx-qlm.h>
+#include <mach/octeon_qlm.h>
+#include <mach/cvmx-pcie.h>
+#include <mach/cvmx-coremask.h>
+
+#include <mach/cvmx-agl-defs.h>
+#include <mach/cvmx-bgxx-defs.h>
+#include <mach/cvmx-ciu-defs.h>
+#include <mach/cvmx-gmxx-defs.h>
+#include <mach/cvmx-gserx-defs.h>
+#include <mach/cvmx-ilk-defs.h>
+#include <mach/cvmx-ipd-defs.h>
+#include <mach/cvmx-pcsx-defs.h>
+#include <mach/cvmx-pcsxx-defs.h>
+#include <mach/cvmx-pki-defs.h>
+#include <mach/cvmx-pko-defs.h>
+#include <mach/cvmx-xcv-defs.h>
+
+#include <mach/cvmx-hwpko.h>
+#include <mach/cvmx-ilk.h>
+#include <mach/cvmx-pki.h>
+#include <mach/cvmx-pko3.h>
+#include <mach/cvmx-pko3-queue.h>
+#include <mach/cvmx-pko3-resources.h>
+
+#include <mach/cvmx-helper.h>
+#include <mach/cvmx-helper-board.h>
+#include <mach/cvmx-helper-cfg.h>
+
+#include <mach/cvmx-helper-bgx.h>
+#include <mach/cvmx-helper-cfg.h>
+#include <mach/cvmx-helper-util.h>
+#include <mach/cvmx-helper-pki.h>
+
+static const int debug;
+static const bool __native_le;
+
+#define CVMX_DUMP_REGX(reg)						\
+	if (debug)							\
+		debug("%s=%#llx\n", #reg, (long long)csr_rd_node(node, reg))
+
+static int cvmx_pko_setup_macs(int node);
+
+/*
+ * PKO descriptor queue operation error string
+ *
+ * @param dqstatus is the enumeration returned from hardware,
+ *	  PKO_QUERY_RTN_S[DQSTATUS].
+ *
+ * @return static constant string error description
+ */
+const char *pko_dqstatus_error(pko_query_dqstatus_t dqstatus)
+{
+	char *str = "PKO Undefined error";
+
+	switch (dqstatus) {
+	case PKO_DQSTATUS_PASS:
+		str = "No error";
+		break;
+	case PKO_DQSTATUS_BADSTATE:
+		str = "PKO queue not ready";
+		break;
+	case PKO_DQSTATUS_NOFPABUF:
+		str = "PKO failed to allocate buffer from FPA";
+		break;
+	case PKO_DQSTATUS_NOPKOBUF:
+		str = "PKO out of buffers";
+		break;
+	case PKO_DQSTATUS_FAILRTNPTR:
+		str = "PKO failed to return buffer to FPA";
+		break;
+	case PKO_DQSTATUS_ALREADY:
+		str = "PKO queue already opened";
+		break;
+	case PKO_DQSTATUS_NOTCREATED:
+		str = "PKO queue has not been created";
+		break;
+	case PKO_DQSTATUS_NOTEMPTY:
+		str = "PKO queue is not empty";
+		break;
+	case PKO_DQSTATUS_SENDPKTDROP:
+		str = "Illegal PKO command construct";
+		break;
+	}
+	return str;
+}
+
+/*
+ * PKO global initialization for 78XX.
+ *
+ * @param node is the node on which PKO block is initialized.
+ * @return none.
+ */
+int cvmx_pko3_hw_init_global(int node, uint16_t aura)
+{
+	cvmx_pko_dpfi_flush_t pko_flush;
+	cvmx_pko_dpfi_fpa_aura_t pko_aura;
+	cvmx_pko_dpfi_ena_t dpfi_enable;
+	cvmx_pko_ptf_iobp_cfg_t ptf_iobp_cfg;
+	cvmx_pko_pdm_cfg_t pko_pdm_cfg;
+	cvmx_pko_enable_t pko_enable;
+	cvmx_pko_dpfi_status_t dpfi_status;
+	cvmx_pko_status_t pko_status;
+	cvmx_pko_shaper_cfg_t shaper_cfg;
+	u64 cycles;
+	const unsigned int timeout = 100; /* 100 milliseconds */
+
+	if (node != (aura >> 10))
+		cvmx_printf("WARNING: AURA vs PKO node mismatch\n");
+
+	pko_enable.u64 = csr_rd_node(node, CVMX_PKO_ENABLE);
+	if (pko_enable.s.enable) {
+		cvmx_printf("WARNING: %s: PKO already enabled on node %u\n",
+			    __func__, node);
+		return 0;
+	}
+	/* Enable color awareness. */
+	shaper_cfg.u64 = csr_rd_node(node, CVMX_PKO_SHAPER_CFG);
+	shaper_cfg.s.color_aware = 1;
+	csr_wr_node(node, CVMX_PKO_SHAPER_CFG, shaper_cfg.u64);
+
+	/* Clear FLUSH command to be sure */
+	pko_flush.u64 = 0;
+	pko_flush.s.flush_en = 0;
+	csr_wr_node(node, CVMX_PKO_DPFI_FLUSH, pko_flush.u64);
+
+	/* set the aura number in pko, use aura node from parameter */
+	pko_aura.u64 = 0;
+	pko_aura.s.node = aura >> 10;
+	pko_aura.s.laura = aura;
+	csr_wr_node(node, CVMX_PKO_DPFI_FPA_AURA, pko_aura.u64);
+
+	CVMX_DUMP_REGX(CVMX_PKO_DPFI_FPA_AURA);
+
+	dpfi_enable.u64 = 0;
+	dpfi_enable.s.enable = 1;
+	csr_wr_node(node, CVMX_PKO_DPFI_ENA, dpfi_enable.u64);
+
+	/* Prepare timeout */
+	cycles = get_timer(0);
+
+	/* Wait until all pointers have been returned */
+	do {
+		pko_status.u64 = csr_rd_node(node, CVMX_PKO_STATUS);
+		if (get_timer(cycles) > timeout)
+			break;
+	} while (!pko_status.s.pko_rdy);
+
+	if (!pko_status.s.pko_rdy) {
+		dpfi_status.u64 = csr_rd_node(node, CVMX_PKO_DPFI_STATUS);
+		cvmx_printf("ERROR: %s: PKO DFPI failed, PKO_STATUS=%#llx DPFI_STATUS=%#llx\n",
+			    __func__, (unsigned long long)pko_status.u64,
+			    (unsigned long long)dpfi_status.u64);
+		return -1;
+	}
+
+	/* Set max outstanding requests in IOBP for any FIFO.*/
+	ptf_iobp_cfg.u64 = csr_rd_node(node, CVMX_PKO_PTF_IOBP_CFG);
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX))
+		ptf_iobp_cfg.s.max_read_size = 0x10; /* Recommended by HRM.*/
+	else
+		/* Reduce the value from recommended 0x10 to avoid
+		 * getting "underflow" condition in the BGX TX FIFO.
+		 */
+		ptf_iobp_cfg.s.max_read_size = 3;
+	csr_wr_node(node, CVMX_PKO_PTF_IOBP_CFG, ptf_iobp_cfg.u64);
+
+	/* Set minimum packet size per Ethernet standard */
+	pko_pdm_cfg.u64 = 0;
+	pko_pdm_cfg.s.pko_pad_minlen = 0x3c; /* 60 bytes before FCS */
+	csr_wr_node(node, CVMX_PKO_PDM_CFG, pko_pdm_cfg.u64);
+
+	/* Initialize MACs and FIFOs */
+	cvmx_pko_setup_macs(node);
+
+	/* enable PKO, although interfaces and queues are not up yet */
+	pko_enable.u64 = 0;
+	pko_enable.s.enable = 1;
+	csr_wr_node(node, CVMX_PKO_ENABLE, pko_enable.u64);
+
+	/* PKO_RDY set indicates successful initialization */
+	pko_status.u64 = csr_rd_node(node, CVMX_PKO_STATUS);
+	if (pko_status.s.pko_rdy)
+		return 0;
+
+	cvmx_printf("ERROR: %s: failed, PKO_STATUS=%#llx\n", __func__,
+		    (unsigned long long)pko_status.u64);
+	return -1;
+}
+
+/**
+ * Shutdown the entire PKO
+ */
+int cvmx_pko3_hw_disable(int node)
+{
+	cvmx_pko_dpfi_flush_t pko_flush;
+	cvmx_pko_dpfi_status_t dpfi_status;
+	cvmx_pko_dpfi_ena_t dpfi_enable;
+	cvmx_pko_enable_t pko_enable;
+	cvmx_pko_status_t pko_status;
+	u64 cycles;
+	const unsigned int timeout = 10; /* 10 milliseconds */
+	unsigned int mac_num, fifo, i;
+	unsigned int null_mac_num, null_fifo_num, fifo_grp_count, pq_count;
+
+	(void)pko_status;
+
+	/* Wait until there are no in-flight packets */
+	for (i = mac_num = 0; mac_num < __cvmx_pko3_num_macs(); mac_num++) {
+		cvmx_pko_ptfx_status_t ptf_status;
+
+		ptf_status.u64 =
+			csr_rd_node(node, CVMX_PKO_PTFX_STATUS(mac_num));
+		if (debug)
+			debug("%s: MAC %u in-flight %u total %u\n", __func__,
+			      mac_num, ptf_status.s.in_flight_cnt,
+			      ptf_status.s.total_in_flight_cnt);
+		if (ptf_status.s.mac_num == 0x1f)
+			continue;
+		if (ptf_status.s.in_flight_cnt != 0) {
+			cvmx_printf("WARNING: %s: MAC %d in-flight %d\n",
+				    __func__, mac_num,
+				    ptf_status.s.in_flight_cnt);
+			mac_num--;
+			udelay(1000);
+		}
+	}
+
+	/* disable PKO - all packets should be out by now */
+	pko_enable.u64 = 0;
+	pko_enable.s.enable = 0;
+	csr_wr_node(node, CVMX_PKO_ENABLE, pko_enable.u64);
+
+	/* Assign NULL MAC# for L1/SQ disabled state */
+	if (OCTEON_IS_MODEL(OCTEON_CN73XX)) {
+		null_mac_num = 0x0f;
+		null_fifo_num = 0x1f;
+		fifo_grp_count = 4;
+		pq_count = 16;
+	} else if (OCTEON_IS_MODEL(OCTEON_CNF75XX)) {
+		null_mac_num = 0x0a;
+		null_fifo_num = 0x1f;
+		fifo_grp_count = 4;
+		pq_count = 16;
+	} else {
+		null_mac_num = 0x1c;
+		null_fifo_num = 0x1f;
+		fifo_grp_count = 8;
+		pq_count = 32;
+	}
+
+	/* Reset L1_PQ */
+	for (i = 0; i < pq_count; i++) {
+		cvmx_pko_l1_sqx_topology_t pko_l1_topology;
+		cvmx_pko_l1_sqx_shape_t pko_l1_shape;
+		cvmx_pko_l1_sqx_link_t pko_l1_link;
+
+		pko_l1_topology.u64 = 0;
+		pko_l1_topology.s.link = null_mac_num;
+		csr_wr_node(node, CVMX_PKO_L1_SQX_TOPOLOGY(i),
+			    pko_l1_topology.u64);
+
+		pko_l1_shape.u64 = 0;
+		pko_l1_shape.s.link = null_mac_num;
+		csr_wr_node(node, CVMX_PKO_L1_SQX_SHAPE(i), pko_l1_shape.u64);
+
+		pko_l1_link.u64 = 0;
+		pko_l1_link.s.link = null_mac_num;
+		csr_wr_node(node, CVMX_PKO_L1_SQX_LINK(i), pko_l1_link.u64);
+	}
+
+	/* Reset all MAC configurations */
+	for (mac_num = 0; mac_num < __cvmx_pko3_num_macs(); mac_num++) {
+		cvmx_pko_macx_cfg_t pko_mac_cfg;
+
+		pko_mac_cfg.u64 = 0;
+		pko_mac_cfg.s.fifo_num = null_fifo_num;
+		csr_wr_node(node, CVMX_PKO_MACX_CFG(mac_num), pko_mac_cfg.u64);
+	}
+
+	/* Reset all FIFO groups */
+	for (fifo = 0; fifo < fifo_grp_count; fifo++) {
+		cvmx_pko_ptgfx_cfg_t pko_ptgfx_cfg;
+
+		pko_ptgfx_cfg.u64 = csr_rd_node(node, CVMX_PKO_PTGFX_CFG(fifo));
+		/* Simulator asserts if an unused group is reset */
+		if (pko_ptgfx_cfg.u64 == 0)
+			continue;
+		pko_ptgfx_cfg.u64 = 0;
+		pko_ptgfx_cfg.s.reset = 1;
+		csr_wr_node(node, CVMX_PKO_PTGFX_CFG(fifo), pko_ptgfx_cfg.u64);
+	}
+
+	/* Set FLUSH_EN to return cached pointers to FPA */
+	pko_flush.u64 = 0;
+	pko_flush.s.flush_en = 1;
+	csr_wr_node(node, CVMX_PKO_DPFI_FLUSH, pko_flush.u64);
+
+	/* Prepare timeout */
+	//	cycles = cvmx_get_cycle();
+	//	cycles += cvmx_clock_get_rate(CVMX_CLOCK_CORE)/1000 * timeout;
+	cycles = get_timer(0);
+
+	/* Wait until all pointers have been returned */
+	do {
+		dpfi_status.u64 = csr_rd_node(node, CVMX_PKO_DPFI_STATUS);
+		//		if (cycles < cvmx_get_cycle())
+		if (get_timer(cycles) > timeout)
+			break;
+	} while (!dpfi_status.s.cache_flushed);
+
+	/* disable PKO buffer manager, should return all buffers to FPA */
+	dpfi_enable.u64 = 0;
+	dpfi_enable.s.enable = 0;
+	csr_wr_node(node, CVMX_PKO_DPFI_ENA, dpfi_enable.u64);
+
+	CVMX_DUMP_REGX(CVMX_PKO_DPFI_ENA);
+	CVMX_DUMP_REGX(CVMX_PKO_DPFI_STATUS);
+	CVMX_DUMP_REGX(CVMX_PKO_STATUS);
+
+	/* Clear the FLUSH_EN bit, as we are done */
+	pko_flush.u64 = 0;
+	csr_wr_node(node, CVMX_PKO_DPFI_FLUSH, pko_flush.u64);
+	CVMX_DUMP_REGX(CVMX_PKO_DPFI_FLUSH);
+
+	if (dpfi_status.s.cache_flushed == 0) {
+		cvmx_printf("%s: ERROR: timeout waiting for PKO3 ptr flush\n",
+			    __func__);
+		return -1;
+	}
+
+	return 0;
+}
+
+/*
+ * Configure Channel credit level in PKO.
+ *
+ * @param node is to specify the node to which this configuration is applied.
+ * @param level specifies the level at which pko channel queues will be configured,
+ * @return returns 0 if successful and -1 on failure.
+ */
+int cvmx_pko3_channel_credit_level(int node, enum cvmx_pko3_level_e level)
+{
+	union cvmx_pko_channel_level channel_level;
+
+	channel_level.u64 = 0;
+
+	if (level == CVMX_PKO_L2_QUEUES)
+		channel_level.s.cc_level = 0;
+	else if (level == CVMX_PKO_L3_QUEUES)
+		channel_level.s.cc_level = 1;
+	else
+		return -1;
+
+	csr_wr_node(node, CVMX_PKO_CHANNEL_LEVEL, channel_level.u64);
+
+	return 0;
+}
+
+/** Open configured descriptor queues before queueing packets into them.
+ *
+ * @param node is to specify the node to which this configuration is applied.
+ * @param dq is the descriptor queue number to be opened.
+ * @return returns 0 on success or -1 on failure.
+ */
+int cvmx_pko_dq_open(int node, int dq)
+{
+	cvmx_pko_query_rtn_t pko_status;
+	pko_query_dqstatus_t dqstatus;
+	cvmx_pko3_dq_params_t *p_param;
+
+	if (debug)
+		debug("%s: DEBUG: dq %u\n", __func__, dq);
+
+	__cvmx_pko3_dq_param_setup(node);
+
+	pko_status = __cvmx_pko3_do_dma(node, dq, NULL, 0, CVMX_PKO_DQ_OPEN);
+
+	dqstatus = pko_status.s.dqstatus;
+
+	if (dqstatus == PKO_DQSTATUS_ALREADY)
+		return 0;
+	if (dqstatus != PKO_DQSTATUS_PASS) {
+		cvmx_printf("%s: ERROR: Failed to open dq :%u: %s\n", __func__,
+			    dq, pko_dqstatus_error(dqstatus));
+		return -1;
+	}
+
+	/* Setup the descriptor queue software parameters */
+	p_param = cvmx_pko3_dq_parameters(node, dq);
+	if (p_param) {
+		p_param->depth = pko_status.s.depth;
+		if (p_param->limit == 0)
+			p_param->limit = 1024; /* last-resort default */
+	}
+
+	return 0;
+}
+
+/**
+ * Close a descriptor queue
+ *
+ * @param node is to specify the node to which this configuration is applied.
+ * @param dq is the descriptor queue number to be opened.
+ * @return returns 0 on success or -1 on failure.
+ *
+ * This should be called before changing the DQ parent link, topology,
+ * or when shutting down the PKO.
+ */
+int cvmx_pko3_dq_close(int node, int dq)
+{
+	cvmx_pko_query_rtn_t pko_status;
+	pko_query_dqstatus_t dqstatus;
+
+	if (debug)
+		debug("%s: DEBUG: dq %u\n", __func__, dq);
+
+	pko_status = __cvmx_pko3_do_dma(node, dq, NULL, 0, CVMX_PKO_DQ_CLOSE);
+
+	dqstatus = pko_status.s.dqstatus;
+
+	if (dqstatus == PKO_DQSTATUS_NOTCREATED)
+		return 0;
+
+	if (dqstatus != PKO_DQSTATUS_PASS) {
+		cvmx_printf("WARNING: %s: Failed to close dq :%u: %s\n",
+			    __func__, dq, pko_dqstatus_error(dqstatus));
+		debug("DEBUG: %s: dq %u depth %u\n", __func__, dq,
+		      (unsigned int)pko_status.s.depth);
+	}
+
+	return 0;
+}
+
+/**
+ * Drain a descriptor queue
+ *
+ * Before closing a DQ, this call will drain all pending traffic
+ * on the DQ to the NULL MAC, which will circumvent any traffic
+ * shaping and flow control to quickly reclaim all packet buffers.
+ *
+ * @param node is to specify the node to which this configuration is applied.
+ * @param dq is the descriptor queue number to be drained.
+ */
+void cvmx_pko3_dq_drain(int node, int dq)
+{
+	cvmx_pko_dqx_sw_xoff_t rxoff;
+
+	rxoff.u64 = 0;
+	rxoff.s.drain_null_link = 1;
+	rxoff.s.drain = 1;
+	rxoff.s.xoff = 0;
+
+	csr_wr_node(node, CVMX_PKO_DQX_SW_XOFF(dq), rxoff.u64);
+
+	udelay(100);
+
+	rxoff.u64 = 0;
+	csr_wr_node(node, CVMX_PKO_DQX_SW_XOFF(dq), rxoff.u64);
+}
+
+/**
+ * Query a descriptor queue
+ *
+ * @param node is to specify the node to which this configuration is applied.
+ * @param dq is the descriptor queue number to be opened.
+ * @return returns the descriptor queue depth on success or -1 on failure.
+ *
+ * This should be called before changing the DQ parent link, topology,
+ * or when shutting down the PKO.
+ */
+int cvmx_pko3_dq_query(int node, int dq)
+{
+	cvmx_pko_query_rtn_t pko_status;
+	pko_query_dqstatus_t dqstatus;
+
+	pko_status = __cvmx_pko3_do_dma(node, dq, NULL, 0, CVMX_PKO_DQ_QUERY);
+
+	dqstatus = pko_status.s.dqstatus;
+
+	if (dqstatus != PKO_DQSTATUS_PASS) {
+		cvmx_printf("%s: ERROR: Failed to query dq :%u: %s\n", __func__,
+			    dq, pko_dqstatus_error(dqstatus));
+		return -1;
+	}
+
+	/* Temp: debug for HW */
+	if (pko_status.s.depth > 0)
+		debug("%s: DEBUG: dq %u depth %u\n", __func__, dq,
+		      (unsigned int)pko_status.s.depth);
+
+	return pko_status.s.depth;
+}
+
+/*
+ * PKO initialization of MACs and FIFOs
+ *
+ * All MACs are configured and assigned a specific FIFO,
+ * and each FIFO is configured with size for a best utilization
+ * of available FIFO resources.
+ *
+ * @param node is to specify which node's pko block for this setup.
+ * @return returns 0 if successful and -1 on failure.
+ *
+ * Note: This function contains model-specific code.
+ */
+static int cvmx_pko_setup_macs(int node)
+{
+	unsigned int interface;
+	unsigned int port, num_ports;
+	unsigned int mac_num, fifo, pri, cnt;
+	cvmx_helper_interface_mode_t mode;
+	const unsigned int num_interfaces =
+		cvmx_helper_get_number_of_interfaces();
+	u8 fifo_group_cfg[8];
+	u8 fifo_group_spd[8];
+	unsigned int fifo_count = 0;
+	unsigned int max_fifos = 0, fifo_groups = 0;
+	struct {
+		u8 fifo_cnt;
+		u8 fifo_id;
+		u8 pri;
+		u8 spd;
+		u8 mac_fifo_cnt;
+	} cvmx_pko3_mac_table[32];
+
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX)) {
+		max_fifos = 28;	 /* exclusive of NULL FIFO */
+		fifo_groups = 8; /* inclusive of NULL PTGF */
+	}
+	if (OCTEON_IS_MODEL(OCTEON_CN73XX) || OCTEON_IS_MODEL(OCTEON_CNF75XX)) {
+		max_fifos = 16;
+		fifo_groups = 5;
+	}
+
+	/* Initialize FIFO allocation table */
+	memset(&fifo_group_cfg, 0, sizeof(fifo_group_cfg));
+	memset(&fifo_group_spd, 0, sizeof(fifo_group_spd));
+	memset(cvmx_pko3_mac_table, 0, sizeof(cvmx_pko3_mac_table));
+
+	/* Initialize all MACs as disabled */
+	for (mac_num = 0; mac_num < __cvmx_pko3_num_macs(); mac_num++) {
+		cvmx_pko3_mac_table[mac_num].pri = 0;
+		cvmx_pko3_mac_table[mac_num].fifo_cnt = 0;
+		cvmx_pko3_mac_table[mac_num].fifo_id = 0x1f;
+	}
+
+	for (interface = 0; interface < num_interfaces; interface++) {
+		int xiface =
+			cvmx_helper_node_interface_to_xiface(node, interface);
+		/* Interface type for ALL interfaces */
+		mode = cvmx_helper_interface_get_mode(xiface);
+		num_ports = cvmx_helper_interface_enumerate(xiface);
+
+		if (mode == CVMX_HELPER_INTERFACE_MODE_DISABLED)
+			continue;
+		/*
+		 * Non-BGX interfaces:
+		 * Each of these interfaces has a single MAC really.
+		 */
+		if (mode == CVMX_HELPER_INTERFACE_MODE_ILK ||
+		    mode == CVMX_HELPER_INTERFACE_MODE_NPI ||
+		    mode == CVMX_HELPER_INTERFACE_MODE_LOOP)
+			num_ports = 1;
+
+		for (port = 0; port < num_ports; port++) {
+			int i;
+
+			/* Get the per-port mode for BGX-interfaces */
+			if (interface < CVMX_HELPER_MAX_GMX)
+				mode = cvmx_helper_bgx_get_mode(xiface, port);
+			/* In MIXED mode, LMACs can run different protocols */
+
+			/* convert interface/port to mac number */
+			i = __cvmx_pko3_get_mac_num(xiface, port);
+			if (i < 0 || i >= (int)__cvmx_pko3_num_macs()) {
+				cvmx_printf("%s: ERROR: interface %d:%u port %d has no MAC %d/%d\n",
+					    __func__, node, interface, port, i,
+					    __cvmx_pko3_num_macs());
+				continue;
+			}
+
+			if (mode == CVMX_HELPER_INTERFACE_MODE_RXAUI) {
+				unsigned int bgx_fifo_size =
+					__cvmx_helper_bgx_fifo_size(xiface,
+								    port);
+
+				cvmx_pko3_mac_table[i].mac_fifo_cnt =
+					bgx_fifo_size /
+					(CVMX_BGX_TX_FIFO_SIZE / 4);
+				cvmx_pko3_mac_table[i].pri = 2;
+				cvmx_pko3_mac_table[i].spd = 10;
+				cvmx_pko3_mac_table[i].fifo_cnt = 2;
+			} else if (mode == CVMX_HELPER_INTERFACE_MODE_XLAUI) {
+				unsigned int bgx_fifo_size =
+					__cvmx_helper_bgx_fifo_size(xiface,
+								    port);
+
+				cvmx_pko3_mac_table[i].mac_fifo_cnt =
+					bgx_fifo_size /
+					(CVMX_BGX_TX_FIFO_SIZE / 4);
+				cvmx_pko3_mac_table[i].pri = 4;
+				cvmx_pko3_mac_table[i].spd = 40;
+				cvmx_pko3_mac_table[i].fifo_cnt = 4;
+			} else if (mode == CVMX_HELPER_INTERFACE_MODE_XAUI) {
+				unsigned int bgx_fifo_size =
+					__cvmx_helper_bgx_fifo_size(xiface,
+								    port);
+
+				cvmx_pko3_mac_table[i].mac_fifo_cnt =
+					bgx_fifo_size /
+					(CVMX_BGX_TX_FIFO_SIZE / 4);
+				cvmx_pko3_mac_table[i].pri = 3;
+				cvmx_pko3_mac_table[i].fifo_cnt = 4;
+				/* DXAUI at 20G, or XAU at 10G */
+				cvmx_pko3_mac_table[i].spd = 20;
+			} else if (mode == CVMX_HELPER_INTERFACE_MODE_XFI) {
+				unsigned int bgx_fifo_size =
+					__cvmx_helper_bgx_fifo_size(xiface,
+								    port);
+
+				cvmx_pko3_mac_table[i].mac_fifo_cnt =
+					bgx_fifo_size /
+					(CVMX_BGX_TX_FIFO_SIZE / 4);
+				cvmx_pko3_mac_table[i].pri = 3;
+				cvmx_pko3_mac_table[i].fifo_cnt = 4;
+				cvmx_pko3_mac_table[i].spd = 10;
+			} else if (mode == CVMX_HELPER_INTERFACE_MODE_LOOP) {
+				cvmx_pko3_mac_table[i].fifo_cnt = 1;
+				cvmx_pko3_mac_table[i].pri = 1;
+				cvmx_pko3_mac_table[i].spd = 1;
+				cvmx_pko3_mac_table[i].mac_fifo_cnt = 1;
+			} else if (mode == CVMX_HELPER_INTERFACE_MODE_ILK ||
+				   mode == CVMX_HELPER_INTERFACE_MODE_SRIO) {
+				cvmx_pko3_mac_table[i].fifo_cnt = 4;
+				cvmx_pko3_mac_table[i].pri = 3;
+				/* ILK/SRIO: speed depends on lane count */
+				cvmx_pko3_mac_table[i].spd = 40;
+				cvmx_pko3_mac_table[i].mac_fifo_cnt = 4;
+			} else if (mode == CVMX_HELPER_INTERFACE_MODE_NPI) {
+				cvmx_pko3_mac_table[i].fifo_cnt = 4;
+				cvmx_pko3_mac_table[i].pri = 2;
+				/* Actual speed depends on PCIe lanes/mode */
+				cvmx_pko3_mac_table[i].spd = 50;
+				/* SLI Tx FIFO size to be revisitted */
+				cvmx_pko3_mac_table[i].mac_fifo_cnt = 1;
+			} else {
+				/* Other BGX interface modes: SGMII/RGMII */
+				unsigned int bgx_fifo_size =
+					__cvmx_helper_bgx_fifo_size(xiface,
+								    port);
+
+				cvmx_pko3_mac_table[i].mac_fifo_cnt =
+					bgx_fifo_size /
+					(CVMX_BGX_TX_FIFO_SIZE / 4);
+				cvmx_pko3_mac_table[i].fifo_cnt = 1;
+				cvmx_pko3_mac_table[i].pri = 1;
+				cvmx_pko3_mac_table[i].spd = 1;
+			}
+
+			if (debug)
+				debug("%s: intf %d:%u port %u %s mac %02u cnt %u macfifo %uk spd %u\n",
+				      __func__, node, interface, port,
+				      cvmx_helper_interface_mode_to_string(mode),
+				      i, cvmx_pko3_mac_table[i].fifo_cnt,
+				      cvmx_pko3_mac_table[i].mac_fifo_cnt * 8,
+				      cvmx_pko3_mac_table[i].spd);
+
+		} /* for port */
+	}	  /* for interface */
+
+	/* Count the number of requested FIFOs */
+	for (fifo_count = mac_num = 0; mac_num < __cvmx_pko3_num_macs();
+	     mac_num++)
+		fifo_count += cvmx_pko3_mac_table[mac_num].fifo_cnt;
+
+	if (debug)
+		debug("%s: initially requested FIFO count %u\n", __func__,
+		      fifo_count);
+
+	/* Heuristically trim FIFO count to fit in available number */
+	pri = 1;
+	cnt = 4;
+	while (fifo_count > max_fifos) {
+		for (mac_num = 0; mac_num < __cvmx_pko3_num_macs(); mac_num++) {
+			if (cvmx_pko3_mac_table[mac_num].fifo_cnt == cnt &&
+			    cvmx_pko3_mac_table[mac_num].pri <= pri) {
+				cvmx_pko3_mac_table[mac_num].fifo_cnt >>= 1;
+				fifo_count -=
+					cvmx_pko3_mac_table[mac_num].fifo_cnt;
+			}
+			if (fifo_count <= max_fifos)
+				break;
+		}
+		if (pri >= 4) {
+			pri = 1;
+			cnt >>= 1;
+		} else {
+			pri++;
+		}
+		if (cnt == 0)
+			break;
+	}
+
+	if (debug)
+		debug("%s: adjusted FIFO count %u\n", __func__, fifo_count);
+
+	/* Special case for NULL Virtual FIFO */
+	fifo_group_cfg[fifo_groups - 1] = 0;
+	/* there is no MAC connected to NULL FIFO */
+
+	/* Configure MAC units, and attach a FIFO to each */
+	for (fifo = 0, cnt = 4; cnt > 0; cnt >>= 1) {
+		unsigned int g;
+
+		for (mac_num = 0; mac_num < __cvmx_pko3_num_macs(); mac_num++) {
+			if (cvmx_pko3_mac_table[mac_num].fifo_cnt < cnt ||
+			    cvmx_pko3_mac_table[mac_num].fifo_id != 0x1f)
+				continue;
+
+			/* Attach FIFO to MAC */
+			cvmx_pko3_mac_table[mac_num].fifo_id = fifo;
+			g = fifo >> 2;
+			/* Sum speed for FIFO group */
+			fifo_group_spd[g] += cvmx_pko3_mac_table[mac_num].spd;
+
+			if (cnt == 4)
+				fifo_group_cfg[g] = 4; /* 10k,0,0,0 */
+			else if (cnt == 2 && (fifo & 0x3) == 0)
+				fifo_group_cfg[g] = 3; /* 5k,0,5k,0 */
+			else if (cnt == 2 && fifo_group_cfg[g] == 3)
+				/* no change */;
+			else if (cnt == 1 && (fifo & 0x2) &&
+				 fifo_group_cfg[g] == 3)
+				fifo_group_cfg[g] = 1; /* 5k,0,2.5k 2.5k*/
+			else if (cnt == 1 && (fifo & 0x3) == 0x3)
+				/* no change */;
+			else if (cnt == 1)
+				fifo_group_cfg[g] = 0; /* 2.5k x 4 */
+			else
+				cvmx_printf("ERROR: %s: internal error\n",
+					    __func__);
+
+			fifo += cnt;
+		}
+	}
+
+	/* Check if there was no error in FIFO allocation */
+	if (fifo > max_fifos) {
+		cvmx_printf("ERROR: %s: Internal error FIFO %u\n", __func__,
+			    fifo);
+		return -1;
+	}
+
+	if (debug)
+		debug("%s: used %u of FIFOs\n", __func__, fifo);
+
+	/* Now configure all FIFO groups */
+	for (fifo = 0; fifo < fifo_groups; fifo++) {
+		cvmx_pko_ptgfx_cfg_t pko_ptgfx_cfg;
+
+		pko_ptgfx_cfg.u64 = csr_rd_node(node, CVMX_PKO_PTGFX_CFG(fifo));
+		if (pko_ptgfx_cfg.s.size != fifo_group_cfg[fifo])
+			pko_ptgfx_cfg.s.reset = 1;
+		pko_ptgfx_cfg.s.size = fifo_group_cfg[fifo];
+		if (fifo_group_spd[fifo] >= 40)
+			if (pko_ptgfx_cfg.s.size >= 3)
+				pko_ptgfx_cfg.s.rate = 3; /* 50 Gbps */
+			else
+				pko_ptgfx_cfg.s.rate = 2; /* 25 Gbps */
+		else if (fifo_group_spd[fifo] >= 20)
+			pko_ptgfx_cfg.s.rate = 2; /* 25 Gbps */
+		else if (fifo_group_spd[fifo] >= 10)
+			pko_ptgfx_cfg.s.rate = 1; /* 12.5 Gbps */
+		else
+			pko_ptgfx_cfg.s.rate = 0; /* 6.25 Gbps */
+
+		if (debug)
+			debug("%s: FIFO %#x-%#x size=%u speed=%d rate=%d\n",
+			      __func__, fifo * 4, fifo * 4 + 3,
+			      pko_ptgfx_cfg.s.size, fifo_group_spd[fifo],
+			      pko_ptgfx_cfg.s.rate);
+
+		csr_wr_node(node, CVMX_PKO_PTGFX_CFG(fifo), pko_ptgfx_cfg.u64);
+		pko_ptgfx_cfg.s.reset = 0;
+		csr_wr_node(node, CVMX_PKO_PTGFX_CFG(fifo), pko_ptgfx_cfg.u64);
+	}
+
+	/* Configure all MACs assigned FIFO number */
+	for (mac_num = 0; mac_num < __cvmx_pko3_num_macs(); mac_num++) {
+		cvmx_pko_macx_cfg_t pko_mac_cfg;
+
+		if (debug)
+			debug("%s: mac#%02u: fifo=%#x cnt=%u speed=%d\n",
+			      __func__, mac_num,
+			      cvmx_pko3_mac_table[mac_num].fifo_id,
+			      cvmx_pko3_mac_table[mac_num].fifo_cnt,
+			      cvmx_pko3_mac_table[mac_num].spd);
+
+		pko_mac_cfg.u64 = csr_rd_node(node, CVMX_PKO_MACX_CFG(mac_num));
+		pko_mac_cfg.s.fifo_num = cvmx_pko3_mac_table[mac_num].fifo_id;
+		csr_wr_node(node, CVMX_PKO_MACX_CFG(mac_num), pko_mac_cfg.u64);
+	}
+
+	/* Setup PKO MCI0/MCI1/SKID credits */
+	for (mac_num = 0; mac_num < __cvmx_pko3_num_macs(); mac_num++) {
+		cvmx_pko_mci0_max_credx_t pko_mci0_max_cred;
+		cvmx_pko_mci1_max_credx_t pko_mci1_max_cred;
+		cvmx_pko_macx_cfg_t pko_mac_cfg;
+		unsigned int fifo_credit, mac_credit, skid_credit;
+		unsigned int pko_fifo_cnt, fifo_size;
+		unsigned int mac_fifo_cnt;
+		unsigned int tmp;
+		int saved_fifo_num;
+
+		pko_fifo_cnt = cvmx_pko3_mac_table[mac_num].fifo_cnt;
+		mac_fifo_cnt = cvmx_pko3_mac_table[mac_num].mac_fifo_cnt;
+
+		/* Skip unused MACs */
+		if (pko_fifo_cnt == 0)
+			continue;
+
+		/* Check for sanity */
+		if (pko_fifo_cnt > 4)
+			pko_fifo_cnt = 1;
+
+		fifo_size = (2 * 1024) + (1024 / 2); /* 2.5KiB */
+		fifo_credit = pko_fifo_cnt * fifo_size;
+
+		if (mac_num == 0) {
+			/* loopback */
+			mac_credit = 4096; /* From HRM Sec 13.0 */
+			skid_credit = 0;
+		} else if (mac_num == 1) {
+			/* DPI */
+			mac_credit = 2 * 1024;
+			skid_credit = 0;
+		} else if (octeon_has_feature(OCTEON_FEATURE_ILK) &&
+			   (mac_num & 0xfe) == 2) {
+			/* ILK0, ILK1: MAC 2,3 */
+			mac_credit = 4 * 1024; /* 4KB fifo */
+			skid_credit = 0;
+		} else if (octeon_has_feature(OCTEON_FEATURE_SRIO) &&
+			   (mac_num >= 6) && (mac_num <= 9)) {
+			/* SRIO0, SRIO1: MAC 6..9 */
+			mac_credit = 1024 / 2;
+			skid_credit = 0;
+		} else {
+			/* BGX */
+			mac_credit = mac_fifo_cnt * 8 * 1024;
+			skid_credit = mac_fifo_cnt * 256;
+		}
+
+		if (debug)
+			debug("%s: mac %u pko_fifo_credit=%u mac_credit=%u\n",
+			      __func__, mac_num, fifo_credit, mac_credit);
+
+		tmp = (fifo_credit + mac_credit) / 16;
+		pko_mci0_max_cred.u64 = 0;
+		pko_mci0_max_cred.s.max_cred_lim = tmp;
+
+		/* Check for overflow */
+		if (pko_mci0_max_cred.s.max_cred_lim != tmp) {
+			cvmx_printf("WARNING: %s: MCI0 credit overflow\n",
+				    __func__);
+			pko_mci0_max_cred.s.max_cred_lim = 0xfff;
+		}
+
+		/* Pass 2 PKO hardware does not use the MCI0 credits */
+		if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_X))
+			csr_wr_node(node, CVMX_PKO_MCI0_MAX_CREDX(mac_num),
+				    pko_mci0_max_cred.u64);
+
+		/* The original CSR formula is the correct one after all */
+		tmp = (mac_credit) / 16;
+		pko_mci1_max_cred.u64 = 0;
+		pko_mci1_max_cred.s.max_cred_lim = tmp;
+
+		/* Check for overflow */
+		if (pko_mci1_max_cred.s.max_cred_lim != tmp) {
+			cvmx_printf("WARNING: %s: MCI1 credit overflow\n",
+				    __func__);
+			pko_mci1_max_cred.s.max_cred_lim = 0xfff;
+		}
+
+		csr_wr_node(node, CVMX_PKO_MCI1_MAX_CREDX(mac_num),
+			    pko_mci1_max_cred.u64);
+
+		tmp = (skid_credit / 256) >> 1; /* valid 0,1,2 */
+		pko_mac_cfg.u64 = csr_rd_node(node, CVMX_PKO_MACX_CFG(mac_num));
+
+		/* The PKO_MACX_CFG bits cannot be changed unless FIFO_MUM=0x1f (unused fifo) */
+		saved_fifo_num = pko_mac_cfg.s.fifo_num;
+		pko_mac_cfg.s.fifo_num = 0x1f;
+		pko_mac_cfg.s.skid_max_cnt = tmp;
+		csr_wr_node(node, CVMX_PKO_MACX_CFG(mac_num), pko_mac_cfg.u64);
+
+		pko_mac_cfg.u64 = csr_rd_node(node, CVMX_PKO_MACX_CFG(mac_num));
+		pko_mac_cfg.s.fifo_num = saved_fifo_num;
+		csr_wr_node(node, CVMX_PKO_MACX_CFG(mac_num), pko_mac_cfg.u64);
+
+		if (debug) {
+			pko_mci0_max_cred.u64 =
+				csr_rd_node(node, CVMX_PKO_MCI0_MAX_CREDX(mac_num));
+			pko_mci1_max_cred.u64 =
+				csr_rd_node(node, CVMX_PKO_MCI1_MAX_CREDX(mac_num));
+			pko_mac_cfg.u64 =
+				csr_rd_node(node, CVMX_PKO_MACX_CFG(mac_num));
+			debug("%s: mac %u PKO_MCI0_MAX_CREDX=%u PKO_MCI1_MAX_CREDX=%u PKO_MACX_CFG[SKID_MAX_CNT]=%u\n",
+			      __func__, mac_num,
+			      pko_mci0_max_cred.s.max_cred_lim,
+			      pko_mci1_max_cred.s.max_cred_lim,
+			      pko_mac_cfg.s.skid_max_cnt);
+		}
+	} /* for mac_num */
+
+	return 0;
+}
+
+/**
+ * @INTERNAL
+ * Backward compatibility for collecting statistics from PKO3
+ *
+ * NOTE:
+ * The good stats are in BGX block.
+ */
+void cvmx_pko3_get_legacy_port_stats(u16 ipd_port, unsigned int clear,
+				     cvmx_pko_port_status_t *stats)
+{
+	unsigned int dq, dq_base, dq_num;
+	unsigned int node = cvmx_get_node_num();
+
+	dq_base = cvmx_pko3_get_queue_base(ipd_port);
+	dq_num = cvmx_pko3_get_queue_num(ipd_port);
+
+	stats->packets = 0;
+	stats->octets = 0;
+	stats->doorbell = 0; /* NOTE: PKO3 does not have a doorbell */
+
+	for (dq = dq_base; dq < (dq_base + dq_num); dq++) {
+		cvmx_pko_dqx_packets_t pkts;
+		cvmx_pko_dqx_bytes_t byts;
+
+		/* NOTE: clearing of these counters is non-atomic */
+		pkts.u64 = csr_rd_node(node, CVMX_PKO_DQX_PACKETS(dq));
+		if (clear)
+			csr_wr_node(node, CVMX_PKO_DQX_PACKETS(dq), 0ull);
+
+		byts.u64 = csr_rd_node(node, CVMX_PKO_DQX_BYTES(dq));
+		if (clear)
+			csr_wr_node(node, CVMX_PKO_DQX_BYTES(dq), 0ull);
+
+		stats->packets += pkts.s.count;
+		stats->octets += byts.s.count;
+	} /* for dq */
+}
+
+/** Set MAC options
+ *
+ * The options supported are the parameters below:
+ *
+ * @param xiface The physical interface number
+ * @param index The physical sub-interface port
+ * @param fcs_enable Enable FCS generation
+ * @param pad_enable Enable padding to minimum packet size
+ * @param fcs_sop_off Number of bytes at start of packet to exclude from FCS
+ *
+ * The typical use for `fcs_sop_off` is when the interface is configured
+ * to use a header such as HighGig to precede every Ethernet packet,
+ * such a header usually does not partake in the CRC32 computation stream,
+ * and its size must be set with this parameter.
+ *
+ * @return Returns 0 on success, -1 if interface/port is invalid.
+ */
+int cvmx_pko3_interface_options(int xiface, int index, bool fcs_enable,
+				bool pad_enable, unsigned int fcs_sop_off)
+{
+	int mac_num;
+	cvmx_pko_macx_cfg_t pko_mac_cfg;
+	unsigned int fifo_num;
+	struct cvmx_xiface xi = cvmx_helper_xiface_to_node_interface(xiface);
+
+	if (debug)
+		debug("%s: intf %u:%u/%u fcs=%d pad=%d\n", __func__, xi.node,
+		      xi.interface, index, fcs_enable, pad_enable);
+
+	mac_num = __cvmx_pko3_get_mac_num(xiface, index);
+	if (mac_num < 0) {
+		cvmx_printf("ERROR: %s: invalid interface %u:%u/%u\n", __func__,
+			    xi.node, xi.interface, index);
+		return -1;
+	}
+
+	pko_mac_cfg.u64 = csr_rd_node(xi.node, CVMX_PKO_MACX_CFG(mac_num));
+
+	/* If MAC is not assigned, return an error */
+	if (pko_mac_cfg.s.fifo_num == 0x1f) {
+		cvmx_printf("ERROR: %s: unused interface %u:%u/%u\n", __func__,
+			    xi.node, xi.interface, index);
+		return -1;
+	}
+
+	if (pko_mac_cfg.s.min_pad_ena == pad_enable &&
+	    pko_mac_cfg.s.fcs_ena == fcs_enable) {
+		if (debug)
+			debug("%s: mac %#x unchanged\n", __func__, mac_num);
+		return 0;
+	}
+
+	/* WORKAROUND: Pass1 won't allow change any bits unless FIFO_NUM=0x1f */
+	fifo_num = pko_mac_cfg.s.fifo_num;
+	pko_mac_cfg.s.fifo_num = 0x1f;
+
+	pko_mac_cfg.s.min_pad_ena = pad_enable;
+	pko_mac_cfg.s.fcs_ena = fcs_enable;
+	pko_mac_cfg.s.fcs_sop_off = fcs_sop_off;
+
+	csr_wr_node(xi.node, CVMX_PKO_MACX_CFG(mac_num), pko_mac_cfg.u64);
+
+	pko_mac_cfg.s.fifo_num = fifo_num;
+	csr_wr_node(xi.node, CVMX_PKO_MACX_CFG(mac_num), pko_mac_cfg.u64);
+
+	if (debug)
+		debug("%s: PKO_MAC[%u]CFG=%#llx\n", __func__, mac_num,
+		      (unsigned long long)csr_rd_node(xi.node, CVMX_PKO_MACX_CFG(mac_num)));
+
+	return 0;
+}
+
+/** Set Descriptor Queue options
+ *
+ * The `min_pad` parameter must be in agreement with the interface-level
+ * padding option for all descriptor queues assigned to that particular
+ * interface/port.
+ *
+ * @param node on which to operate
+ * @param dq descriptor queue to set
+ * @param min_pad minimum padding to set for dq
+ */
+void cvmx_pko3_dq_options(unsigned int node, unsigned int dq, bool min_pad)
+{
+	cvmx_pko_pdm_dqx_minpad_t reg;
+
+	dq &= (1 << 10) - 1;
+	reg.u64 = csr_rd_node(node, CVMX_PKO_PDM_DQX_MINPAD(dq));
+	reg.s.minpad = min_pad;
+	csr_wr_node(node, CVMX_PKO_PDM_DQX_MINPAD(dq), reg.u64);
+}
+
+/**
+ * Get number of PKO internal buffers available
+ *
+ * This function may be used to throttle output processing
+ * when the PKO runs out of internal buffers, to avoid discarding
+ * of packets or returning error results from transmission function.
+ *
+ * Returns negative numbers on error, positive number ti nidicate the
+ * number of buffers available, or 0 when no more buffers are available.
+ *
+ * @INTERNAL
+ */
+int cvmx_pko3_internal_buffer_count(unsigned int node)
+{
+	cvmx_pko_dpfi_fpa_aura_t pko_aura;
+	unsigned int laura, pool;
+	long long avail1, avail2;
+
+	/* get the aura number in pko, use aura node from parameter */
+	pko_aura.u64 = csr_rd_node(node, CVMX_PKO_DPFI_FPA_AURA);
+	laura = pko_aura.s.laura;
+
+	/* form here on, node is the AURA node */
+	node = pko_aura.s.node;
+
+	/* get the POOL number for this AURA */
+	pool = csr_rd_node(node, CVMX_FPA_AURAX_POOL(laura));
+
+	avail1 = csr_rd_node(node, CVMX_FPA_POOLX_AVAILABLE(pool));
+
+	avail2 = csr_rd_node(node, CVMX_FPA_AURAX_CNT_LIMIT(laura)) -
+		 csr_rd_node(node, CVMX_FPA_AURAX_CNT(laura));
+
+	if (avail1 < avail2)
+		return avail1;
+
+	return avail2;
+}
+
+/**
+ * @INTERNAL
+ *
+ * Get actual PKO FIFO buffer size for a given port
+ *
+ * Since the FIFOs are allocated dynamically based on supply/demand
+ * heuristics, it may be useful in some instances to know the
+ * actual FIFO size allocated to any specific port at run time.
+ *
+ * @param xiface global interface number
+ * @param index port index on interface
+ * @return Returns the per-port FIFO size in bytes, or 0 if the port
+ * has not been configured, or a negative number if the interface and
+ * index numbers are not valid.
+ */
+int cvmx_pko3_port_fifo_size(unsigned int xiface, unsigned int index)
+{
+	unsigned int node;
+	unsigned int mac_num;
+	unsigned int fifo_grp, fifo_off;
+	cvmx_pko_macx_cfg_t pko_mac_cfg;
+	cvmx_pko_ptgfx_cfg_t pko_ptgfx_cfg;
+	cvmx_xiface_t xi = cvmx_helper_xiface_to_node_interface(xiface);
+	int ret;
+
+	node = xi.node;
+	ret = __cvmx_pko3_get_mac_num(xiface, index);
+	if (ret < 0)
+		return ret;
+
+	if (debug)
+		debug("%s: iface=%u:%u/%u mac %d\n", __func__, xi.node,
+		      xi.interface, index, ret);
+
+	mac_num = ret;
+
+	/* Check for the special value on onused MACs */
+	if (mac_num == 0x1f)
+		return 0;
+
+	pko_mac_cfg.u64 = csr_rd_node(node, CVMX_PKO_MACX_CFG(mac_num));
+
+	fifo_grp = pko_mac_cfg.s.fifo_num >> 2;
+	fifo_off = pko_mac_cfg.s.fifo_num & 0x3;
+	pko_ptgfx_cfg.u64 = csr_rd_node(node, CVMX_PKO_PTGFX_CFG(fifo_grp));
+
+	ret = (2 << 10) + (1 << 9); /* set 2.5KBytes base FIFO size */
+
+	switch (pko_ptgfx_cfg.s.size) {
+	case 0:
+		/* 2.5l, 2.5k, 2.5k, 2.5k */
+		break;
+	case 1:
+		/* 5.0k, 0.0k, 2.5k, 2.5k */
+		if (fifo_off == 1)
+			ret = 0;
+		if (fifo_off == 0)
+			ret *= 2;
+		break;
+	case 2:
+		/* 2.5k, 2.5k, 5.0k, 0.0k */
+		if (fifo_off == 3)
+			ret = 0;
+		if (fifo_off == 2)
+			ret *= 2;
+		break;
+	case 3:
+		/* 5k, 0, 5k, 0 */
+		if ((fifo_off & 1) != 0)
+			ret = 0;
+		ret *= 2;
+		break;
+	case 4:
+		/* 10k, 0, 0, 0 */
+		if (fifo_off != 0)
+			ret = 0;
+		ret *= 4;
+		break;
+	default:
+		ret = -1;
+	}
+	return ret;
+}
+
+/**
+ * @INTERNAL
+ *
+ * Stop an interface port transmission and wait until its FIFO is empty.
+ *
+ */
+int cvmx_pko3_port_xoff(unsigned int xiface, unsigned int index)
+{
+	cvmx_pko_l1_sqx_topology_t pko_l1_topology;
+	cvmx_pko_l1_sqx_sw_xoff_t pko_l1_xoff;
+	cvmx_pko_ptfx_status_t pko_ptfx_status;
+	cvmx_pko_macx_cfg_t pko_mac_cfg;
+	cvmx_pko_mci1_cred_cntx_t cred_cnt;
+	unsigned int node, pq, num_pq, mac_num, fifo_num;
+	int ret;
+	cvmx_xiface_t xi = cvmx_helper_xiface_to_node_interface(xiface);
+
+	node = xi.node;
+	ret = __cvmx_pko3_get_mac_num(xiface, index);
+
+	if (debug)
+		debug("%s: iface=%u:%u/%u mac %d\n", __func__, xi.node,
+		      xi.interface, index, ret);
+
+	if (ret < 0)
+		return ret;
+
+	mac_num = ret;
+
+	if (mac_num == 0x1f)
+		return 0;
+
+	pko_mac_cfg.u64 = csr_rd_node(node, CVMX_PKO_MACX_CFG(mac_num));
+	fifo_num = pko_mac_cfg.s.fifo_num;
+
+	/* Verify the FIFO number is correct */
+	pko_ptfx_status.u64 = csr_rd_node(node, CVMX_PKO_PTFX_STATUS(fifo_num));
+
+	if (debug)
+		debug("%s: mac %d fifo %d, fifo mac %d\n", __func__, mac_num,
+		      fifo_num, pko_ptfx_status.s.mac_num);
+
+	cvmx_warn_if(pko_ptfx_status.s.mac_num != mac_num,
+		     "PKO3 FIFO number does not match MAC\n");
+
+	num_pq = cvmx_pko3_num_level_queues(CVMX_PKO_PORT_QUEUES);
+	/* Find the L1/PQ connected to the MAC for this interface */
+	for (pq = 0; pq < num_pq; pq++) {
+		pko_l1_topology.u64 =
+			csr_rd_node(node, CVMX_PKO_L1_SQX_TOPOLOGY(pq));
+		if (pko_l1_topology.s.link == mac_num)
+			break;
+	}
+
+	if (debug)
+		debug("%s: L1_PQ%u LINK %d MAC_NUM %d\n", __func__, pq,
+		      pko_l1_topology.s.link, mac_num);
+
+	if (pq >= num_pq)
+		return -1;
+
+	if (debug) {
+		pko_ptfx_status.u64 =
+			csr_rd_node(node, CVMX_PKO_PTFX_STATUS(fifo_num));
+		ret = pko_ptfx_status.s.in_flight_cnt;
+		debug("%s: FIFO %d in-flight %d packets\n", __func__, fifo_num,
+		      ret);
+	}
+
+	/* Turn the XOFF bit on */
+	pko_l1_xoff.u64 = csr_rd_node(node, CVMX_PKO_L1_SQX_SW_XOFF(pq));
+	pko_l1_xoff.s.xoff = 1;
+	csr_wr_node(node, CVMX_PKO_L1_SQX_SW_XOFF(pq), pko_l1_xoff.u64);
+
+	ret = 1 << 22;
+	/* Wait for PKO TX FIFO to drain */
+	do {
+		CVMX_SYNC;
+		pko_ptfx_status.u64 =
+			csr_rd_node(node, CVMX_PKO_PTFX_STATUS(fifo_num));
+	} while (pko_ptfx_status.s.in_flight_cnt != 0 && ret--);
+
+	if (pko_ptfx_status.s.in_flight_cnt != 0)
+		cvmx_warn("%s: FIFO %d failed to drain\n", __func__, fifo_num);
+
+	if (debug)
+		debug("%s: FIFO %d drained in %d cycles\n", __func__, fifo_num,
+		      (1 << 22) - ret);
+
+	/* Wait for MAC TX FIFO to drain. */
+	do {
+		cred_cnt.u64 =
+			csr_rd_node(node, CVMX_PKO_MCI1_CRED_CNTX(mac_num));
+	} while (cred_cnt.s.cred_cnt != 0);
+
+	return 0;
+}
+
+/**
+ * @INTERNAL
+ *
+ * Resume transmission on an interface port.
+ *
+ */
+int cvmx_pko3_port_xon(unsigned int xiface, unsigned int index)
+{
+	cvmx_pko_l1_sqx_topology_t pko_l1_topology;
+	cvmx_pko_l1_sqx_sw_xoff_t pko_l1_xoff;
+	unsigned int node, pq, num_pq, mac_num;
+	int ret;
+	cvmx_xiface_t xi = cvmx_helper_xiface_to_node_interface(xiface);
+
+	num_pq = cvmx_pko3_num_level_queues(CVMX_PKO_PORT_QUEUES);
+	node = xi.node;
+	ret = __cvmx_pko3_get_mac_num(xiface, index);
+
+	if (debug)
+		debug("%s: iface=%u:%u/%u mac %d\n", __func__, xi.node,
+		      xi.interface, index, ret);
+
+	if (ret < 0)
+		return ret;
+
+	mac_num = ret;
+
+	if (mac_num == 0x1f)
+		return 0;
+
+	/* Find the L1/PQ connected to the MAC for this interface */
+	for (pq = 0; pq < num_pq; pq++) {
+		pko_l1_topology.u64 =
+			csr_rd_node(node, CVMX_PKO_L1_SQX_TOPOLOGY(pq));
+		if (pko_l1_topology.s.link == mac_num)
+			break;
+	}
+
+	if (debug)
+		debug("%s: L1_PQ%u LINK %d MAC_NUM %d\n", __func__, pq,
+		      pko_l1_topology.s.link, mac_num);
+
+	if (pq >= num_pq)
+		return -1;
+
+	/* Turn the XOFF bit off */
+	pko_l1_xoff.u64 = csr_rd_node(node, CVMX_PKO_L1_SQX_SW_XOFF(pq));
+	ret = pko_l1_xoff.s.xoff;
+	pko_l1_xoff.s.xoff = 0;
+	csr_wr_node(node, CVMX_PKO_L1_SQX_SW_XOFF(pq), pko_l1_xoff.u64);
+
+	return ret;
+}
+
+/******************************************************************************
+ * New PKO3 API - Experimental
+ ******************************************************************************/
+
+/**
+ * Initialize packet descriptor
+ *
+ * Descriptor storage is provided by the caller,
+ * use this function to initialize the descriptor to a known
+ * empty state.
+ *
+ * @param pdesc Packet Descriptor.
+ *
+ * Do not use this function when creating a descriptor from a
+ * Work Queue Entry.
+ *
+ * The default setting of the 'free_bufs' attribute is 'false'.
+ */
+void cvmx_pko3_pdesc_init(cvmx_pko3_pdesc_t *pdesc)
+{
+	cvmx_pko_send_aura_t *ext_s;
+
+	memset(pdesc, 0, sizeof(*pdesc));
+
+	/* Start with HDR_S and HDR_EXT_S in first two words, all 0's */
+	pdesc->num_words = 2;
+
+	pdesc->hdr_s = (void *)&pdesc->word[0];
+	ext_s = (void *)&pdesc->word[1];
+	ext_s->s.subdc4 = CVMX_PKO_SENDSUBDC_EXT;
+
+	pdesc->last_aura = -1;
+	pdesc->jb_aura = -1;
+
+	/* Empty packets, can not decode header offsets (yet) */
+	pdesc->hdr_offsets = 1;
+}
+
+/**
+ * @INTERNAL
+ *
+ * Add arbitrary subcommand to a packet descriptor.
+ *
+ * This function will also allocate a jump buffer when
+ * the primary LMTDMA buffer is exhausted.
+ * The jump buffer is allocated from the internal PKO3 aura
+ * on the node where this function is running.
+ */
+static int cvmx_pko3_pdesc_subdc_add(cvmx_pko3_pdesc_t *pdesc, uint64_t subdc)
+{
+	cvmx_pko_send_hdr_t *hdr_s;
+	cvmx_pko_send_aura_t *ext_s;
+	cvmx_pko_buf_ptr_t *jump_s;
+	const unsigned int jump_buf_size = 4 * 1024 / sizeof(uint64_t);
+	unsigned int i;
+
+	/* Simple handling while fitting the command buffer */
+	if (cvmx_likely(pdesc->num_words < 15 && !pdesc->jump_buf)) {
+		pdesc->word[pdesc->num_words] = subdc;
+		pdesc->num_words++;
+		return pdesc->num_words;
+	}
+
+	/* SEND_JUMP_S missing on Pass1 */
+	if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_X)) {
+		cvmx_printf("%s: ERROR: too many segments\n", __func__);
+		return -E2BIG;
+	}
+
+	hdr_s = (void *)&pdesc->word[0];
+	ext_s = (void *)&pdesc->word[1];
+
+	/* Allocate jump buffer */
+	if (cvmx_unlikely(!pdesc->jump_buf)) {
+		u16 pko_gaura;
+		cvmx_fpa3_gaura_t aura;
+		unsigned int fpa_node = cvmx_get_node_num();
+
+		/* Allocate jump buffer from PKO internal FPA AURA, size=4KiB */
+		pko_gaura = __cvmx_pko3_aura_get(fpa_node);
+		aura = __cvmx_fpa3_gaura(pko_gaura >> 10, pko_gaura & 0x3ff);
+
+		pdesc->jump_buf = cvmx_fpa3_alloc(aura);
+		if (!pdesc->jump_buf)
+			return -EINVAL;
+
+		/* Save the JB aura for later */
+		pdesc->jb_aura = pko_gaura;
+
+		/* Move most of the command to the jump buffer */
+		memcpy(pdesc->jump_buf, &pdesc->word[2],
+		       (pdesc->num_words - 2) * sizeof(uint64_t));
+		jump_s = (void *)&pdesc->word[2];
+		jump_s->u64 = 0;
+		jump_s->s.addr = cvmx_ptr_to_phys(pdesc->jump_buf);
+		jump_s->s.i = !hdr_s->s.df; /* F= ~DF */
+		jump_s->s.size = pdesc->num_words - 2;
+		jump_s->s.subdc3 = CVMX_PKO_SENDSUBDC_JUMP;
+
+		/* Now the LMTDMA buffer has only HDR_S, EXT_S, JUMP_S */
+		pdesc->num_words = 3;
+	}
+
+	/* Add the new subcommand to the jump buffer */
+	jump_s = (void *)&pdesc->word[2];
+	i = jump_s->s.size;
+
+	/* Avoid overrunning jump buffer */
+	if (i >= (jump_buf_size - 2)) {
+		cvmx_printf("%s: ERROR: too many segments\n", __func__);
+		return -E2BIG;
+	}
+
+	pdesc->jump_buf[i] = subdc;
+	jump_s->s.size++;
+
+	(void)ext_s;
+
+	return (i + pdesc->num_words);
+}
+
+/**
+ * Send a packet in a descriptor to an output port via an output queue.
+ *
+ * A call to this function must follow all other functions that
+ * create a packet descriptor from WQE, or after initializing an
+ * empty descriptor and filling it with one or more data fragments.
+ * After this function is called, the content of the packet descriptor
+ * can no longer be used, and are undefined.
+ *
+ * @param pdesc Packet Descriptor.
+ * @param dq Descriptor Queue associated with the desired output port
+ * @param tag Flow Tag pointer for packet ordering or NULL
+ * @return Returns 0 on success, -1 on error.
+ *
+ */
+int cvmx_pko3_pdesc_transmit(cvmx_pko3_pdesc_t *pdesc, uint16_t dq,
+			     uint32_t *tag)
+{
+	cvmx_pko_query_rtn_t pko_status;
+	cvmx_pko_send_aura_t aura_s;
+	u8 port_node;
+	int rc;
+
+	/* Add last AURA_S for jump_buf, if present */
+	if (cvmx_unlikely(pdesc->jump_buf) &&
+	    pdesc->last_aura != pdesc->jb_aura) {
+		/* The last AURA_S subdc refers to the jump_buf itself */
+		aura_s.s.aura = pdesc->jb_aura;
+		aura_s.s.offset = 0;
+		aura_s.s.alg = AURAALG_NOP;
+		aura_s.s.subdc4 = CVMX_PKO_SENDSUBDC_AURA;
+		pdesc->last_aura = pdesc->jb_aura;
+
+		rc = cvmx_pko3_pdesc_subdc_add(pdesc, aura_s.u64);
+		if (rc < 0)
+			return -1;
+	}
+
+	/* SEND_WORK_S must be the very last subdc */
+	if (cvmx_unlikely(pdesc->send_work_s != 0ULL)) {
+		rc = cvmx_pko3_pdesc_subdc_add(pdesc, pdesc->send_work_s);
+		if (rc < 0)
+			return -1;
+		pdesc->send_work_s = 0ULL;
+	}
+
+	/* Derive destination node from dq */
+	port_node = dq >> 10;
+	dq &= (1 << 10) - 1;
+
+	/* To preserve packet order, go atomic with DQ-specific tag */
+	if (tag)
+		cvmx_pow_tag_sw(*tag ^ dq, CVMX_POW_TAG_TYPE_ATOMIC);
+
+	/* Send the PKO3 command into the Descriptor Queue */
+	pko_status = __cvmx_pko3_do_dma(port_node, dq, pdesc->word,
+					pdesc->num_words, CVMX_PKO_DQ_SEND);
+
+	/* Map PKO3 result codes to legacy return values */
+	if (pko_status.s.dqstatus == PKO_DQSTATUS_PASS)
+		return 0;
+
+	return -1;
+}
+
+int cvmx_pko3_pdesc_append_free(cvmx_pko3_pdesc_t *pdesc, uint64_t addr,
+				unsigned int gaura)
+{
+	cvmx_pko_send_hdr_t *hdr_s;
+	cvmx_pko_send_free_t free_s;
+	cvmx_pko_send_aura_t aura_s;
+
+	hdr_s = (void *)&pdesc->word[0];
+
+	if (pdesc->last_aura == -1) {
+		hdr_s->s.aura = gaura;
+		pdesc->last_aura = hdr_s->s.aura;
+	} else if (pdesc->last_aura != (short)gaura) {
+		aura_s.s.aura = gaura;
+		aura_s.s.offset = 0;
+		aura_s.s.alg = AURAALG_NOP;
+		aura_s.s.subdc4 = CVMX_PKO_SENDSUBDC_AURA;
+		pdesc->last_aura = gaura;
+		if (cvmx_pko3_pdesc_subdc_add(pdesc, aura_s.u64) < 0)
+			return -1;
+	}
+
+	free_s.u64 = 0;
+	free_s.s.subdc4 = CVMX_PKO_SENDSUBDC_FREE;
+	free_s.s.addr = addr;
+
+	return cvmx_pko3_pdesc_subdc_add(pdesc, free_s.u64);
+}
+
+/**
+ * Append a packet segment to a packet descriptor
+ *
+ * After a packet descriptor is initialized, one or more
+ * packet data segments can be added to the packet,
+ * in the order in which they should be transmitted.
+ *
+ * The size of the resulting packet will be equal to the
+ * sum of the segments appended by this function.
+ * Every segment may be contained in a buffer that belongs
+ * to a different FPA 'aura', and may be automatically
+ * released back to that aura, if required.
+ *
+ * @param pdesc Packet Descriptor.
+ * @param p_data Address of the segment first byte (virtual).
+ * @param data_bytes Size of the data segment (in bytes).
+ * @param gaura A global FPA 'aura' where the packet buffer was allocated from.
+ *
+ * The 'gaura' parameter contains the node number where the buffer pool
+ * is located, and has only a meaning if the 'free_buf' argument is 'true'.
+ * The buffer being added will be automatically freed upon transmission
+ * along with all other buffers in this descriptor, or not, depending
+ * on the descriptor 'free_bufs' attribute that is set during
+ * descriptor creation, or changed subsequently with a call to
+ * 'cvmx_pko3_pdesc_set_free()'.
+ *
+ * @return Returns 0 on success, -1 on error.
+ */
+int cvmx_pko3_pdesc_buf_append(cvmx_pko3_pdesc_t *pdesc, void *p_data,
+			       unsigned int data_bytes, unsigned int gaura)
+{
+	cvmx_pko_send_hdr_t *hdr_s;
+	cvmx_pko_buf_ptr_t gather_s;
+	cvmx_pko_send_aura_t aura_s;
+	int rc;
+
+	if (pdesc->mem_s_ix > 0) {
+		cvmx_printf("ERROR: %s: subcommand restriction violated\n",
+			    __func__);
+		return -1;
+	}
+
+	hdr_s = (void *)&pdesc->word[0];
+
+	if (gaura != (unsigned int)-1) {
+		if (pdesc->last_aura == -1) {
+			unsigned int buf_sz = 128;
+
+			/* First mbuf, calculate headroom */
+			cvmx_fpa3_gaura_t aura;
+
+			aura = __cvmx_fpa3_gaura(gaura >> 10, gaura & 0x3ff);
+			buf_sz = cvmx_fpa3_get_aura_buf_size(aura);
+			pdesc->headroom = (unsigned long)p_data & (buf_sz - 1);
+			hdr_s->s.aura = gaura;
+			pdesc->last_aura = hdr_s->s.aura;
+		} else if (pdesc->last_aura != (short)gaura) {
+			aura_s.s.aura = gaura;
+			aura_s.s.offset = 0;
+			aura_s.s.alg = AURAALG_NOP;
+			aura_s.s.subdc4 = CVMX_PKO_SENDSUBDC_AURA;
+			pdesc->last_aura = gaura;
+
+			rc = cvmx_pko3_pdesc_subdc_add(pdesc, aura_s.u64);
+			if (rc < 0)
+				return -1;
+		}
+	}
+
+	gather_s.u64 = 0;
+	gather_s.s.addr = cvmx_ptr_to_phys(p_data);
+	gather_s.s.size = data_bytes;
+	hdr_s->s.total += data_bytes;
+	gather_s.s.i = 0; /* follow HDR_S[DF] setting */
+	gather_s.s.subdc3 = CVMX_PKO_SENDSUBDC_GATHER;
+
+	rc = cvmx_pko3_pdesc_subdc_add(pdesc, gather_s.u64);
+	if (rc < 0)
+		return -1;
+
+	return rc;
+}
+
+/**
+ * Add a Work Entry for packet transmission notification
+ *
+ * Add a subcommand to notify of packet transmission completion
+ * via a Work Queue entry over the SSO.
+ * The Work Queue entry may be a 'software' event, or the content
+ * of a packet.
+ *
+ * @param pdesc Packet Descriptor, memory provided by caller.
+ * @param wqe Work Queue Entry in a model-native format.
+ * @param node The OCI node of the SSO where the WQE will be delivered.
+ * @param group The SSO group where the WQE is delivered.
+ * @param tt The SSO Tag Type for the WQE. If tt is not NULL, tag should be a
+ * valid tag value.
+ * @param tag Valid tag value to assign to WQE
+ *
+ * @return Returns 0 on success, -1 on error.
+ *
+ * Restrictions:
+ * There can be only one such notification per packet descriptor,
+ * but this function may be called at any time after the descriptor
+ * is first created from WQE or initialized, and before
+ * starting transmission.
+ *
+ */
+int cvmx_pko3_pdesc_notify_wqe(cvmx_pko3_pdesc_t *pdesc, cvmx_wqe_78xx_t *wqe,
+			       u8 node, u8 group, uint8_t tt, uint32_t tag)
+{
+	cvmx_pko_send_work_t work_s;
+
+	/*
+	 * There can be only one SEND_WORK_S entry in the command
+	 * and it must be the very last subcommand
+	 */
+	if (pdesc->send_work_s != 0) {
+		cvmx_printf("ERROR: %s: Only one SEND_WORK_S is allowed\n",
+			    __func__);
+		return -1;
+	}
+
+	work_s.u64 = 0;
+	work_s.s.subdc4 = CVMX_PKO_SENDSUBDC_WORK;
+	work_s.s.addr = cvmx_ptr_to_phys(wqe);
+	work_s.s.grp = (group & 0xff) | (node << 8);
+	work_s.s.tt = tt;
+
+	wqe->word1.rsvd_0 = 0;
+	wqe->word1.rsvd_1 = 0;
+	wqe->word1.tag = tag;
+	wqe->word1.tag_type = tt;
+	wqe->word1.grp = work_s.s.grp;
+
+	/* Store in descriptor for now, apply just before LMTDMA-ing */
+	pdesc->send_work_s = work_s.u64;
+
+	return 0;
+}
+
+/**
+ * Request atomic memory decrement at transmission completion
+ *
+ * Each packet descriptor may contain several decrement notification
+ * requests, but these requests must only be made after all of the
+ * packet data segments have been added, and before packet transmission
+ * commences.
+ *
+ * Only decrement of a 64-bit memory location is supported.
+ *
+ * @param pdesc Packet Descriptor.
+ * @param p_counter A pointer to an atomic 64-bit memory location.
+ *
+ * @return Returns 0 on success, -1 on failure.
+ */
+int cvmx_pko3_pdesc_notify_decrement(cvmx_pko3_pdesc_t *pdesc,
+				     volatile uint64_t *p_counter)
+{
+	int rc;
+	/* 64-bit decrement is the only supported operation */
+	cvmx_pko_send_mem_t mem_s = {
+		.s = { .subdc4 = CVMX_PKO_SENDSUBDC_MEM,
+		       .dsz = MEMDSZ_B64,
+		       .alg = MEMALG_SUB,
+		       .offset = 1,
+#ifdef _NOT_IN_SIM_
+		       /* Enforce MEM before SSO submission if both present */
+		       .wmem = 1
+#endif
+		}
+	};
+
+	mem_s.s.addr = cvmx_ptr_to_phys(CASTPTR(void, p_counter));
+
+	rc = cvmx_pko3_pdesc_subdc_add(pdesc, mem_s.u64);
+
+	/*
+	 * SEND_MEM_S must be after all LINK_S/FATHER_S/IMM_S
+	 * subcommands, set the index to prevent further data
+	 * subcommands.
+	 */
+	if (rc > 0)
+		pdesc->mem_s_ix = rc;
+
+	return rc;
+}
+
+/**
+ * Request atomic memory clear at transmission completion
+ *
+ * Each packet descriptor may contain several notification
+ * requests, but these request must only be made after all of the
+ * packet data segments have been added, and before packet transmission
+ * commences.
+ *
+ * Clearing of a single byte is requested by this function.
+ *
+ * @param pdesc Packet Descriptor.
+ * @param p_mem A pointer to a byte location.
+ *
+ * @return Returns 0 on success, -1 on failure.
+ */
+int cvmx_pko3_pdesc_notify_memclr(cvmx_pko3_pdesc_t *pdesc,
+				  volatile uint8_t *p_mem)
+{
+	int rc;
+	/* 640bit decrement is the only supported operation */
+	cvmx_pko_send_mem_t mem_s = { .s = {
+					      .subdc4 = CVMX_PKO_SENDSUBDC_MEM,
+					      .dsz = MEMDSZ_B8,
+					      .alg = MEMALG_SET,
+					      .offset = 0,
+				      } };
+
+	mem_s.s.addr = cvmx_ptr_to_phys(CASTPTR(void, p_mem));
+
+	rc = cvmx_pko3_pdesc_subdc_add(pdesc, mem_s.u64);
+
+	/*
+	 * SEND_MEM_S must be after all LINK_S/FATHER_S/IMM_S
+	 * subcommands, set the index to prevent further data
+	 * subcommands.
+	 */
+	if (rc > 0)
+		pdesc->mem_s_ix = rc;
+
+	return rc;
+}
+
+/**
+ * @INTERNAL
+ *
+ * Decode packet header and calculate protocol header offsets
+ *
+ * The protocol information and layer offset is derived
+ * from the results if decoding done by the PKI,
+ * and the appropriate PKO fields are filled.
+ *
+ * The function assumes the headers have not been modified
+ * since converted from WQE, and does not (yet) implement
+ * software-based decoding to handle modified or originated
+ * packets correctly.
+ *
+ * @note
+ * Need to add simple accessors to read the decoded protocol fields.
+ */
+static int cvmx_pko3_pdesc_hdr_offsets(cvmx_pko3_pdesc_t *pdesc)
+{
+	cvmx_pko_send_hdr_t *hdr_s;
+
+	if (pdesc->hdr_offsets)
+		return 0;
+
+	if (!pdesc->pki_word4_present)
+		return -EINVAL;
+
+	hdr_s = (void *)&pdesc->word[0];
+	pdesc->hdr_s = hdr_s;
+
+	/* Match IPv5/IPv6 protocols with/without options */
+	if ((pdesc->pki_word2.lc_hdr_type & 0x1c) == CVMX_PKI_LTYPE_E_IP4) {
+		hdr_s->s.l3ptr = pdesc->pki_word4.ptr_layer_c;
+
+		/* Match TCP/UDP/SCTP group */
+		if ((pdesc->pki_word2.lf_hdr_type & 0x18) ==
+		    CVMX_PKI_LTYPE_E_TCP)
+			hdr_s->s.l4ptr = pdesc->pki_word4.ptr_layer_f;
+
+		if (pdesc->pki_word2.lf_hdr_type == CVMX_PKI_LTYPE_E_UDP)
+			pdesc->ckl4_alg = CKL4ALG_UDP;
+		if (pdesc->pki_word2.lf_hdr_type == CVMX_PKI_LTYPE_E_TCP)
+			pdesc->ckl4_alg = CKL4ALG_TCP;
+		if (pdesc->pki_word2.lf_hdr_type == CVMX_PKI_LTYPE_E_SCTP)
+			pdesc->ckl4_alg = CKL4ALG_SCTP;
+	}
+	/* May need to add logic for ARP, IPfrag packets here */
+
+	pdesc->hdr_offsets = 1; /* make sure its done once */
+	return 0;
+}
+
+/*
+ * @INTERNAL
+ *
+ * memcpy() a reverse endian memory region.
+ * where both the source and destination are the reverse endianness
+ * with respect to native byte order.
+ */
+static void memcpy_swap(void *dst, const void *src, unsigned int bytes)
+{
+	u8 *d = dst;
+	const u8 *s = src;
+	unsigned int i;
+	const unsigned int swizzle = 0x7; /* 64-bit invariant endianness */
+
+	for (i = 0; i < bytes; i++)
+		d[i ^ swizzle] = s[i ^ swizzle];
+}
+
+/*
+ * @INTERNAL
+ *
+ * memcpy() with swizzling, from reverse endianness to native byte order.
+ */
+static void memcpy_from_swap(void *dst, const void *src, unsigned int bytes)
+{
+	u8 *d = dst;
+	const u8 *s = src;
+	unsigned int i;
+	const unsigned int swizzle = 0x7; /* 64-bit invariant endianness */
+
+	for (i = 0; i < bytes; i++)
+		d[i] = s[i ^ swizzle];
+}
+
+/*
+ * @INTERNAL
+ *
+ * memcpy() with swizzling, from native byte order to the reverse endianness.
+ */
+static void memcpy_to_swap(void *dst, const void *src, unsigned int bytes)
+{
+	u8 *d = dst;
+	const u8 *s = src;
+	unsigned int i;
+	const unsigned int swizzle = 0x7; /* 64-bit invariant endianness */
+
+	for (i = 0; i < bytes; i++)
+		d[i ^ swizzle] = s[i];
+}
+
+/**
+ * Prepend a data segment to the packet descriptor
+ *
+ * Useful for pushing additional headers
+ *
+ * The initial implementation is confined by the size of the
+ * "headroom" in the first packet buffer attached to the descriptor.
+ * Future version may prepend additional buffers when this head room
+ * is insufficient, but currently will return -1 when headrom is
+ * insufficient.
+ *
+ * On success, the function returns the remaining headroom in the buffer.
+ *
+ */
+int cvmx_pko3_pdesc_hdr_push(cvmx_pko3_pdesc_t *pdesc, const void *p_data,
+			     u8 data_bytes, uint8_t layer)
+{
+	cvmx_pko_send_hdr_t *hdr_s;
+	cvmx_pko_buf_ptr_t *gather_s;
+	short headroom;
+	void *p; /* old data location */
+	void *q; /* new data location */
+	bool endian_swap;
+
+	headroom = pdesc->headroom;
+
+	if ((short)data_bytes > headroom)
+		return -ENOSPC;
+
+	hdr_s = (void *)&pdesc->word[0];
+	endian_swap = (hdr_s->s.le != __native_le);
+
+	/* Get GATTHER_S/LINK_S subcommand location */
+	if (cvmx_likely(!pdesc->jump_buf))
+		/* Without JB, first data buf is in 3rd command word */
+		gather_s = (void *)&pdesc->word[2];
+	else
+		/* With JB, its first word is the first buffer */
+		gather_s = (void *)pdesc->jump_buf;
+
+	/* Verify the subcommand is of the expected type */
+	if (cvmx_unlikely(gather_s->s.subdc3 != CVMX_PKO_SENDSUBDC_LINK &&
+			  gather_s->s.subdc3 != CVMX_PKO_SENDSUBDC_GATHER))
+		return -EINVAL;
+
+	/* adjust  address and size values */
+	p = cvmx_phys_to_ptr(gather_s->s.addr);
+	q = p - data_bytes;
+	gather_s->s.addr -= data_bytes;
+	gather_s->s.size += data_bytes;
+	hdr_s->s.total += data_bytes;
+	headroom -= data_bytes;
+
+	/* Move link pointer if the descriptor is SEND_LINK_S */
+	if (gather_s->s.subdc3 == CVMX_PKO_SENDSUBDC_LINK) {
+		if (cvmx_likely(!endian_swap))
+			memcpy(q - 8, p - 8, 8);
+		else
+			memcpy_swap(q - 8, p - 8, 8);
+	}
+
+	if (cvmx_likely(!endian_swap))
+		memcpy(q, p_data, data_bytes);
+	else
+		memcpy_to_swap(q, p_data, data_bytes);
+
+	pdesc->headroom = headroom;
+
+	/* Adjust higher level protocol header offset */
+	cvmx_pko3_pdesc_hdr_offsets(pdesc);
+	if (layer <= 4)
+		pdesc->hdr_s->s.l4ptr += data_bytes;
+
+	if (layer <= 3)
+		pdesc->hdr_s->s.l3ptr += data_bytes;
+
+	if (layer >= 3) {
+		/* Set CKL3 only for IPv4 */
+		if ((pdesc->pki_word2.lc_hdr_type & 0x1e) ==
+		    CVMX_PKI_LTYPE_E_IP4)
+			hdr_s->s.ckl3 = 1;
+		hdr_s->s.ckl4 = pdesc->ckl4_alg;
+	}
+
+	return headroom;
+}
+
+/**
+ * Remove some bytes from start of packet
+ *
+ * Useful for popping a header from a packet.
+ * It only needs to find the first segment, and adjust its address,
+ * as well as segment and total sizes.
+ *
+ * Returns new packet size, or -1 if the trimmed size exceeds the
+ * size of the first data segment.
+ */
+int cvmx_pko3_pdesc_hdr_pop(cvmx_pko3_pdesc_t *pdesc, void *hdr_buf,
+			    unsigned int num_bytes)
+{
+	cvmx_pko_send_hdr_t *hdr_s;
+	cvmx_pko_buf_ptr_t *gather_s;
+	short headroom;
+	void *p;
+	void *q;
+	bool endian_swap;
+
+	headroom = pdesc->headroom;
+
+	hdr_s = (void *)&pdesc->word[0];
+	endian_swap = (hdr_s->s.le != __native_le);
+
+	if (hdr_s->s.total < num_bytes)
+		return -ENOSPC;
+
+	/* Get GATTHER_S/LINK_S subcommand location */
+	if (cvmx_likely(!pdesc->jump_buf))
+		/* Without JB, first data buf is in 3rd command word */
+		gather_s = (void *)&pdesc->word[2];
+	else
+		/* With JB, its first word is the first buffer */
+		gather_s = (void *)pdesc->jump_buf;
+
+	/* Verify the subcommand is of the expected type */
+	if (cvmx_unlikely(gather_s->s.subdc3 != CVMX_PKO_SENDSUBDC_LINK &&
+			  gather_s->s.subdc3 != CVMX_PKO_SENDSUBDC_GATHER))
+		return -EINVAL;
+
+	/* Can't trim more than the content of the first buffer */
+	if (gather_s->s.size < num_bytes)
+		return -ENOMEM;
+
+	/* adjust  address and size values */
+	p = cvmx_phys_to_ptr(gather_s->s.addr);
+	q = p + num_bytes;
+	gather_s->s.addr += num_bytes;
+	gather_s->s.size -= num_bytes;
+	hdr_s->s.total -= num_bytes;
+	headroom += num_bytes;
+
+	if (hdr_buf) {
+		/* Retrieve popped header to user buffer */
+		if (cvmx_likely(!endian_swap))
+			memcpy(hdr_buf, p, num_bytes);
+		else
+			memcpy_from_swap(hdr_buf, p, num_bytes);
+	}
+
+	/* Move link pointer if the descriptor is SEND_LINK_S */
+	if (gather_s->s.subdc3 == CVMX_PKO_SENDSUBDC_LINK) {
+		if (cvmx_likely(!endian_swap))
+			memcpy(q - 8, p - 8, 8);
+		else
+			memcpy_swap(q - 8, p - 8, 8);
+	}
+
+	pdesc->headroom = headroom;
+
+	/* Adjust higher level protocol header offset */
+	cvmx_pko3_pdesc_hdr_offsets(pdesc);
+	if (num_bytes < pdesc->hdr_s->s.l3ptr) {
+		pdesc->hdr_s->s.l3ptr -= num_bytes;
+		pdesc->hdr_s->s.l4ptr -= num_bytes;
+	} else if (num_bytes < pdesc->hdr_s->s.l4ptr) {
+		pdesc->hdr_s->s.l3ptr = 0;
+		pdesc->hdr_s->s.l4ptr -= num_bytes;
+	} else {
+		pdesc->hdr_s->s.l3ptr = 0;
+		pdesc->hdr_s->s.l4ptr = 0;
+		hdr_s->s.ckl4 = CKL4ALG_NONE;
+	}
+
+	return hdr_s->s.total;
+}
+
+/**
+ * Peek into some header field of a packet
+ *
+ * Will return a number of bytes of packet header data at an arbitrary offset
+ * which must reside within the first packet data buffer.
+ *
+ */
+int cvmx_pko3_pdesc_hdr_peek(cvmx_pko3_pdesc_t *pdesc, void *hdr_buf,
+			     unsigned int num_bytes, unsigned int offset)
+{
+	cvmx_pko_send_hdr_t *hdr_s;
+	cvmx_pko_buf_ptr_t *gather_s;
+	void *p;
+	bool endian_swap;
+
+	hdr_s = (void *)&pdesc->word[0];
+	endian_swap = (hdr_s->s.le != __native_le);
+
+	if (hdr_s->s.total < (num_bytes + offset))
+		return -ENOSPC;
+
+	/* Get GATTHER_S/LINK_S subcommand location */
+	if (cvmx_likely(!pdesc->jump_buf))
+		/* Without JB, first data buf is in 3rd command word */
+		gather_s = (void *)&pdesc->word[2];
+	else
+		/* With JB, its first word is the first buffer */
+		gather_s = (void *)pdesc->jump_buf;
+
+	/* Verify the subcommand is of the expected type */
+	if (cvmx_unlikely(gather_s->s.subdc3 != CVMX_PKO_SENDSUBDC_LINK &&
+			  gather_s->s.subdc3 != CVMX_PKO_SENDSUBDC_GATHER))
+		return -EINVAL;
+
+	/* Can't peek more than the content of the first buffer */
+	if (gather_s->s.size <= offset)
+		return -ENOMEM;
+	if ((gather_s->s.size - offset) < num_bytes)
+		num_bytes = gather_s->s.size - offset;
+
+	/* adjust address */
+	p = cvmx_phys_to_ptr(gather_s->s.addr) + offset;
+
+	if (!hdr_buf)
+		return -EINVAL;
+
+	/* Copy requested bytes */
+	if (cvmx_likely(!endian_swap))
+		memcpy(hdr_buf, p, num_bytes);
+	else
+		memcpy_from_swap(hdr_buf, p, num_bytes);
+
+	return num_bytes;
+}
+
+/**
+ * Set the packet descriptor automatic-free attribute
+ *
+ * Override the 'free_bufs' attribute that was set during
+ * packet descriptor creation, or by an earlier call to
+ * this function.
+ * Setting the 'buf_free" attribute to 'true' will cause
+ * the PKO3 to free all buffers associated with this packet
+ * descriptor to be released upon transmission complete.
+ * Setting this attribute to 'false' allows e.g. using the
+ * same descriptor to transmit a packet out of several ports
+ * with a minimum overhead.
+ */
+void cvmx_pko3_pdesc_set_free(cvmx_pko3_pdesc_t *pdesc, bool free_bufs)
+{
+	cvmx_pko_send_hdr_t *hdr_s;
+	cvmx_pko_buf_ptr_t *jump_s;
+
+	hdr_s = (void *)&pdesc->word[0];
+	hdr_s->s.df = !free_bufs;
+
+	if (cvmx_likely(!pdesc->jump_buf))
+		return;
+	jump_s = (void *)&pdesc->word[2];
+	jump_s->s.i = free_bufs; /* F=free */
+}
Signature · 1 line
2.35.1