[][kernel][common][hnat][Fix Coverity defects in HNAT]

[Description]
Fix Coverity defects in HNAT.

[Release-log]
N/A


Change-Id: I3dc1ef8c98af3164f9b667cde65a395a8f3a4245
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/9313938
diff --git a/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_soc.h b/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_soc.h
index 94efe7f..198cd0e 100644
--- a/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_soc.h
+++ b/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_soc.h
@@ -531,6 +531,7 @@
 
 /* QDMA TX Scheduler Rate Control Register */
 #define MTK_QDMA_TX_4SCH_BASE(x)	(QDMA_BASE + 0x398 + (((x) >> 1) * 0x4))
+#define MTK_QDMA_TX_SCH_MASK		GENMASK(15, 0)
 
 /* WDMA Registers */
 #define MTK_WDMA_CTX_PTR(x)	(WDMA_BASE(x) + 0x8)
diff --git a/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_hnat/hnat_debugfs.c b/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_hnat/hnat_debugfs.c
index d111cb3..7725756 100644
--- a/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_hnat/hnat_debugfs.c
+++ b/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_hnat/hnat_debugfs.c
@@ -2129,7 +2129,7 @@
 
 	if (id & 0x1)
 		qdma_tx_sch >>= 16;
-	qdma_tx_sch &= 0xffff;
+	qdma_tx_sch &= MTK_QDMA_TX_SCH_MASK;
 	enable = !!(qdma_tx_sch & BIT(11));
 	scheduling = !!(qdma_tx_sch & BIT(15));
 	max_rate = ((qdma_tx_sch >> 4) & 0x7f);
@@ -2212,7 +2212,7 @@
 	else
 		qdma_tx_sch = readl(h->fe_base + QDMA_TX_2SCH_BASE);
 
-	qdma_tx_sch &= ~(0xffff << shift);
+	qdma_tx_sch &= ~(MTK_QDMA_TX_SCH_MASK << shift);
 	qdma_tx_sch |= val << shift;
 	if (hnat_priv->data->num_of_sch == 4)
 		writel(qdma_tx_sch, h->fe_base + QDMA_TX_4SCH_BASE(id));
diff --git a/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_hnat/hnat_nf_hook.c b/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_hnat/hnat_nf_hook.c
index 9b31e66..834a15a 100644
--- a/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_hnat/hnat_nf_hook.c
+++ b/21.02/files/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_hnat/hnat_nf_hook.c
@@ -1171,10 +1171,11 @@
 		} else {
 			entry.ipv4_hnapt.iblk2.mcast = 0;
 		}
-
-		entry.ipv4_hnapt.iblk2.port_ag =
-			(hnat_priv->data->version == MTK_HNAT_V2 ||
-			 hnat_priv->data->version == MTK_HNAT_V3) ? 0xf : 0x3f;
+#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
+		entry.ipv4_hnapt.iblk2.port_ag = 0xf;
+#else
+		entry.ipv4_hnapt.iblk2.port_ag = 0x3f;
+#endif
 		break;
 	case IPV4_DSLITE:
 	case IPV4_MAP_E:
@@ -1194,9 +1195,11 @@
 			entry.ipv6_5t_route.iblk2.mcast = 0;
 		}
 
-		entry.ipv6_5t_route.iblk2.port_ag =
-			(hnat_priv->data->version == MTK_HNAT_V2 ||
-			 hnat_priv->data->version == MTK_HNAT_V3) ? 0xf : 0x3f;
+#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
+		entry.ipv6_5t_route.iblk2.port_ag = 0xf;
+#else
+		entry.ipv6_5t_route.iblk2.port_ag = 0x3f;
+#endif
 		break;
 	}
 	return entry;
@@ -1794,10 +1797,13 @@
 	}
 
 	if (IS_IPV4_GRP(&entry)) {
-		entry.ipv4_hnapt.iblk2.dp = gmac;
+		entry.ipv4_hnapt.iblk2.dp = gmac & 0xf;
+#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
+		entry.ipv4_hnapt.iblk2.port_mg = 0;
+#else
 		entry.ipv4_hnapt.iblk2.port_mg =
 			(hnat_priv->data->version == MTK_HNAT_V1_1) ? 0x3f : 0;
-
+#endif
 		if (qos_toggle) {
 			if (hnat_priv->data->version == MTK_HNAT_V2 ||
 			    hnat_priv->data->version == MTK_HNAT_V3) {
@@ -1832,9 +1838,13 @@
 			entry.ipv4_hnapt.iblk2.fqos = 0;
 		}
 	} else {
-		entry.ipv6_5t_route.iblk2.dp = gmac;
+		entry.ipv6_5t_route.iblk2.dp = gmac & 0xf;
+#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
+		entry.ipv6_5t_route.iblk2.port_mg = 0;
+#else
 		entry.ipv6_5t_route.iblk2.port_mg =
 			(hnat_priv->data->version == MTK_HNAT_V1_1) ? 0x3f : 0;
+#endif
 
 		if (qos_toggle) {
 			if (hnat_priv->data->version == MTK_HNAT_V2 ||
@@ -1932,12 +1942,12 @@
 	/* Before entry enter BIND state, write other fields first,
 	 * prevent racing with hardware accesses.
 	 */
-	memcpy(&foe->ipv4_hnapt.sip, &entry.ipv4_hnapt.sip,
-	       sizeof(entry) - sizeof(entry.bfib1));
+	memcpy(&(foe->ipv6_hnapt.ipv6_sip0), &(entry.ipv6_hnapt.ipv6_sip0),
+	       sizeof(struct foe_entry) - sizeof(entry.bfib1));
 	/* We must ensure all info has been updated before set to hw */
 	wmb();
 	/* After other fields have been written, write info1 to BIND the entry */
-	memcpy(foe, &entry, sizeof(entry.bfib1));
+	foe->bfib1 = entry.bfib1;
 
 	/* reset statistic for this entry */
 	if (hnat_priv->data->per_flow_accounting &&
@@ -2108,10 +2118,10 @@
 				entry.ipv4_hnapt.iblk2.fqos = 1;
 			}
 		}
-		entry.ipv4_hnapt.iblk2.dp = gmac_no;
+		entry.ipv4_hnapt.iblk2.dp = gmac_no & 0xf;
 #if defined(CONFIG_MEDIATEK_NETSYS_V3)
 	} else if (IS_IPV6_HNAPT(&entry) || IS_IPV6_HNAT(&entry)) {
-		entry.ipv6_hnapt.iblk2.dp = gmac_no;
+		entry.ipv6_hnapt.iblk2.dp = gmac_no & 0xf;
 		entry.ipv6_hnapt.iblk2.rxid = skb_hnat_rx_id(skb);
 		entry.ipv6_hnapt.iblk2.winfoi = 1;
 
@@ -2213,7 +2223,7 @@
 				entry.ipv6_5t_route.iblk2.fqos = 1;
 			}
 		}
-		entry.ipv6_5t_route.iblk2.dp = gmac_no;
+		entry.ipv6_5t_route.iblk2.dp = gmac_no & 0xf;
 	}
 
 	entry.bfib1.ttl = 1;
@@ -2230,12 +2240,12 @@
 	/* Before entry enter BIND state, write other fields first,
 	 * prevent racing with hardware accesses.
 	 */
-	memcpy(&hw_entry->ipv4_hnapt.sip, &entry.ipv4_hnapt.sip,
-	       sizeof(entry) - sizeof(entry.bfib1));
+	memcpy(&(hw_entry->ipv6_hnapt.ipv6_sip0), &(entry.ipv6_hnapt.ipv6_sip0),
+	       sizeof(struct foe_entry) - sizeof(entry.bfib1));
 	/* We must ensure all info has been updated before set to hw */
 	wmb();
 	/* After other fields have been written, write info1 to BIND the entry */
-	memcpy(hw_entry, &entry, sizeof(entry.bfib1));
+	hw_entry->bfib1 = entry.bfib1;
 
 	hnat_set_entry_lock(hw_entry, false);