18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2017 Oracle and/or its affiliates. All rights reserved. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "ixgbe.h"
58c2ecf20Sopenharmony_ci#include <net/xfrm.h>
68c2ecf20Sopenharmony_ci#include <crypto/aead.h>
78c2ecf20Sopenharmony_ci#include <linux/if_bridge.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#define IXGBE_IPSEC_KEY_BITS  160
108c2ecf20Sopenharmony_cistatic const char aes_gcm_name[] = "rfc4106(gcm(aes))";
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_del_sa(struct xfrm_state *xs);
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/**
158c2ecf20Sopenharmony_ci * ixgbe_ipsec_set_tx_sa - set the Tx SA registers
168c2ecf20Sopenharmony_ci * @hw: hw specific details
178c2ecf20Sopenharmony_ci * @idx: register index to write
188c2ecf20Sopenharmony_ci * @key: key byte array
198c2ecf20Sopenharmony_ci * @salt: salt bytes
208c2ecf20Sopenharmony_ci **/
218c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_set_tx_sa(struct ixgbe_hw *hw, u16 idx,
228c2ecf20Sopenharmony_ci				  u32 key[], u32 salt)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	u32 reg;
258c2ecf20Sopenharmony_ci	int i;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++)
288c2ecf20Sopenharmony_ci		IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(i),
298c2ecf20Sopenharmony_ci				(__force u32)cpu_to_be32(key[3 - i]));
308c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSTXSALT, (__force u32)cpu_to_be32(salt));
318c2ecf20Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	reg = IXGBE_READ_REG(hw, IXGBE_IPSTXIDX);
348c2ecf20Sopenharmony_ci	reg &= IXGBE_RXTXIDX_IPS_EN;
358c2ecf20Sopenharmony_ci	reg |= idx << IXGBE_RXTXIDX_IDX_SHIFT | IXGBE_RXTXIDX_WRITE;
368c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, reg);
378c2ecf20Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/**
418c2ecf20Sopenharmony_ci * ixgbe_ipsec_set_rx_item - set an Rx table item
428c2ecf20Sopenharmony_ci * @hw: hw specific details
438c2ecf20Sopenharmony_ci * @idx: register index to write
448c2ecf20Sopenharmony_ci * @tbl: table selector
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * Trigger the device to store into a particular Rx table the
478c2ecf20Sopenharmony_ci * data that has already been loaded into the input register
488c2ecf20Sopenharmony_ci **/
498c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_set_rx_item(struct ixgbe_hw *hw, u16 idx,
508c2ecf20Sopenharmony_ci				    enum ixgbe_ipsec_tbl_sel tbl)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	u32 reg;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	reg = IXGBE_READ_REG(hw, IXGBE_IPSRXIDX);
558c2ecf20Sopenharmony_ci	reg &= IXGBE_RXTXIDX_IPS_EN;
568c2ecf20Sopenharmony_ci	reg |= tbl << IXGBE_RXIDX_TBL_SHIFT |
578c2ecf20Sopenharmony_ci	       idx << IXGBE_RXTXIDX_IDX_SHIFT |
588c2ecf20Sopenharmony_ci	       IXGBE_RXTXIDX_WRITE;
598c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, reg);
608c2ecf20Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/**
648c2ecf20Sopenharmony_ci * ixgbe_ipsec_set_rx_sa - set up the register bits to save SA info
658c2ecf20Sopenharmony_ci * @hw: hw specific details
668c2ecf20Sopenharmony_ci * @idx: register index to write
678c2ecf20Sopenharmony_ci * @spi: security parameter index
688c2ecf20Sopenharmony_ci * @key: key byte array
698c2ecf20Sopenharmony_ci * @salt: salt bytes
708c2ecf20Sopenharmony_ci * @mode: rx decrypt control bits
718c2ecf20Sopenharmony_ci * @ip_idx: index into IP table for related IP address
728c2ecf20Sopenharmony_ci **/
738c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_set_rx_sa(struct ixgbe_hw *hw, u16 idx, __be32 spi,
748c2ecf20Sopenharmony_ci				  u32 key[], u32 salt, u32 mode, u32 ip_idx)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	int i;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* store the SPI (in bigendian) and IPidx */
798c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSRXSPI,
808c2ecf20Sopenharmony_ci			(__force u32)cpu_to_le32((__force u32)spi));
818c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPIDX, ip_idx);
828c2ecf20Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_spi_tbl);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/* store the key, salt, and mode */
878c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++)
888c2ecf20Sopenharmony_ci		IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(i),
898c2ecf20Sopenharmony_ci				(__force u32)cpu_to_be32(key[3 - i]));
908c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSRXSALT, (__force u32)cpu_to_be32(salt));
918c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSRXMOD, mode);
928c2ecf20Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_key_tbl);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/**
988c2ecf20Sopenharmony_ci * ixgbe_ipsec_set_rx_ip - set up the register bits to save SA IP addr info
998c2ecf20Sopenharmony_ci * @hw: hw specific details
1008c2ecf20Sopenharmony_ci * @idx: register index to write
1018c2ecf20Sopenharmony_ci * @addr: IP address byte array
1028c2ecf20Sopenharmony_ci **/
1038c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_set_rx_ip(struct ixgbe_hw *hw, u16 idx, __be32 addr[])
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	int i;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* store the ip address */
1088c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++)
1098c2ecf20Sopenharmony_ci		IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(i),
1108c2ecf20Sopenharmony_ci				(__force u32)cpu_to_le32((__force u32)addr[i]));
1118c2ecf20Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_ip_tbl);
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/**
1178c2ecf20Sopenharmony_ci * ixgbe_ipsec_clear_hw_tables - because some tables don't get cleared on reset
1188c2ecf20Sopenharmony_ci * @adapter: board private structure
1198c2ecf20Sopenharmony_ci **/
1208c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_clear_hw_tables(struct ixgbe_adapter *adapter)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	struct ixgbe_hw *hw = &adapter->hw;
1238c2ecf20Sopenharmony_ci	u32 buf[4] = {0, 0, 0, 0};
1248c2ecf20Sopenharmony_ci	u16 idx;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* disable Rx and Tx SA lookup */
1278c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
1288c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/* scrub the tables - split the loops for the max of the IP table */
1318c2ecf20Sopenharmony_ci	for (idx = 0; idx < IXGBE_IPSEC_MAX_RX_IP_COUNT; idx++) {
1328c2ecf20Sopenharmony_ci		ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
1338c2ecf20Sopenharmony_ci		ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
1348c2ecf20Sopenharmony_ci		ixgbe_ipsec_set_rx_ip(hw, idx, (__be32 *)buf);
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci	for (; idx < IXGBE_IPSEC_MAX_SA_COUNT; idx++) {
1378c2ecf20Sopenharmony_ci		ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
1388c2ecf20Sopenharmony_ci		ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/**
1438c2ecf20Sopenharmony_ci * ixgbe_ipsec_stop_data
1448c2ecf20Sopenharmony_ci * @adapter: board private structure
1458c2ecf20Sopenharmony_ci **/
1468c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_stop_data(struct ixgbe_adapter *adapter)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct ixgbe_hw *hw = &adapter->hw;
1498c2ecf20Sopenharmony_ci	bool link = adapter->link_up;
1508c2ecf20Sopenharmony_ci	u32 t_rdy, r_rdy;
1518c2ecf20Sopenharmony_ci	u32 limit;
1528c2ecf20Sopenharmony_ci	u32 reg;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* halt data paths */
1558c2ecf20Sopenharmony_ci	reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
1568c2ecf20Sopenharmony_ci	reg |= IXGBE_SECTXCTRL_TX_DIS;
1578c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
1608c2ecf20Sopenharmony_ci	reg |= IXGBE_SECRXCTRL_RX_DIS;
1618c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	/* If both Tx and Rx are ready there are no packets
1648c2ecf20Sopenharmony_ci	 * that we need to flush so the loopback configuration
1658c2ecf20Sopenharmony_ci	 * below is not necessary.
1668c2ecf20Sopenharmony_ci	 */
1678c2ecf20Sopenharmony_ci	t_rdy = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
1688c2ecf20Sopenharmony_ci		IXGBE_SECTXSTAT_SECTX_RDY;
1698c2ecf20Sopenharmony_ci	r_rdy = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
1708c2ecf20Sopenharmony_ci		IXGBE_SECRXSTAT_SECRX_RDY;
1718c2ecf20Sopenharmony_ci	if (t_rdy && r_rdy)
1728c2ecf20Sopenharmony_ci		return;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	/* If the tx fifo doesn't have link, but still has data,
1758c2ecf20Sopenharmony_ci	 * we can't clear the tx sec block.  Set the MAC loopback
1768c2ecf20Sopenharmony_ci	 * before block clear
1778c2ecf20Sopenharmony_ci	 */
1788c2ecf20Sopenharmony_ci	if (!link) {
1798c2ecf20Sopenharmony_ci		reg = IXGBE_READ_REG(hw, IXGBE_MACC);
1808c2ecf20Sopenharmony_ci		reg |= IXGBE_MACC_FLU;
1818c2ecf20Sopenharmony_ci		IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci		reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
1848c2ecf20Sopenharmony_ci		reg |= IXGBE_HLREG0_LPBK;
1858c2ecf20Sopenharmony_ci		IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci		IXGBE_WRITE_FLUSH(hw);
1888c2ecf20Sopenharmony_ci		mdelay(3);
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/* wait for the paths to empty */
1928c2ecf20Sopenharmony_ci	limit = 20;
1938c2ecf20Sopenharmony_ci	do {
1948c2ecf20Sopenharmony_ci		mdelay(10);
1958c2ecf20Sopenharmony_ci		t_rdy = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
1968c2ecf20Sopenharmony_ci			IXGBE_SECTXSTAT_SECTX_RDY;
1978c2ecf20Sopenharmony_ci		r_rdy = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
1988c2ecf20Sopenharmony_ci			IXGBE_SECRXSTAT_SECRX_RDY;
1998c2ecf20Sopenharmony_ci	} while (!(t_rdy && r_rdy) && limit--);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	/* undo loopback if we played with it earlier */
2028c2ecf20Sopenharmony_ci	if (!link) {
2038c2ecf20Sopenharmony_ci		reg = IXGBE_READ_REG(hw, IXGBE_MACC);
2048c2ecf20Sopenharmony_ci		reg &= ~IXGBE_MACC_FLU;
2058c2ecf20Sopenharmony_ci		IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci		reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
2088c2ecf20Sopenharmony_ci		reg &= ~IXGBE_HLREG0_LPBK;
2098c2ecf20Sopenharmony_ci		IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci		IXGBE_WRITE_FLUSH(hw);
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci/**
2168c2ecf20Sopenharmony_ci * ixgbe_ipsec_stop_engine
2178c2ecf20Sopenharmony_ci * @adapter: board private structure
2188c2ecf20Sopenharmony_ci **/
2198c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_stop_engine(struct ixgbe_adapter *adapter)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	struct ixgbe_hw *hw = &adapter->hw;
2228c2ecf20Sopenharmony_ci	u32 reg;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	ixgbe_ipsec_stop_data(adapter);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* disable Rx and Tx SA lookup */
2278c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
2288c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/* disable the Rx and Tx engines and full packet store-n-forward */
2318c2ecf20Sopenharmony_ci	reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
2328c2ecf20Sopenharmony_ci	reg |= IXGBE_SECTXCTRL_SECTX_DIS;
2338c2ecf20Sopenharmony_ci	reg &= ~IXGBE_SECTXCTRL_STORE_FORWARD;
2348c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
2378c2ecf20Sopenharmony_ci	reg |= IXGBE_SECRXCTRL_SECRX_DIS;
2388c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	/* restore the "tx security buffer almost full threshold" to 0x250 */
2418c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, 0x250);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/* Set minimum IFG between packets back to the default 0x1 */
2448c2ecf20Sopenharmony_ci	reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
2458c2ecf20Sopenharmony_ci	reg = (reg & 0xfffffff0) | 0x1;
2468c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	/* final set for normal (no ipsec offload) processing */
2498c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_SECTX_DIS);
2508c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, IXGBE_SECRXCTRL_SECRX_DIS);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci/**
2568c2ecf20Sopenharmony_ci * ixgbe_ipsec_start_engine
2578c2ecf20Sopenharmony_ci * @adapter: board private structure
2588c2ecf20Sopenharmony_ci *
2598c2ecf20Sopenharmony_ci * NOTE: this increases power consumption whether being used or not
2608c2ecf20Sopenharmony_ci **/
2618c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_start_engine(struct ixgbe_adapter *adapter)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct ixgbe_hw *hw = &adapter->hw;
2648c2ecf20Sopenharmony_ci	u32 reg;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	ixgbe_ipsec_stop_data(adapter);
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* Set minimum IFG between packets to 3 */
2698c2ecf20Sopenharmony_ci	reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
2708c2ecf20Sopenharmony_ci	reg = (reg & 0xfffffff0) | 0x3;
2718c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	/* Set "tx security buffer almost full threshold" to 0x15 so that the
2748c2ecf20Sopenharmony_ci	 * almost full indication is generated only after buffer contains at
2758c2ecf20Sopenharmony_ci	 * least an entire jumbo packet.
2768c2ecf20Sopenharmony_ci	 */
2778c2ecf20Sopenharmony_ci	reg = IXGBE_READ_REG(hw, IXGBE_SECTXBUFFAF);
2788c2ecf20Sopenharmony_ci	reg = (reg & 0xfffffc00) | 0x15;
2798c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, reg);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	/* restart the data paths by clearing the DISABLE bits */
2828c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, 0);
2838c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_STORE_FORWARD);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	/* enable Rx and Tx SA lookup */
2868c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, IXGBE_RXTXIDX_IPS_EN);
2878c2ecf20Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, IXGBE_RXTXIDX_IPS_EN);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci/**
2938c2ecf20Sopenharmony_ci * ixgbe_ipsec_restore - restore the ipsec HW settings after a reset
2948c2ecf20Sopenharmony_ci * @adapter: board private structure
2958c2ecf20Sopenharmony_ci *
2968c2ecf20Sopenharmony_ci * Reload the HW tables from the SW tables after they've been bashed
2978c2ecf20Sopenharmony_ci * by a chip reset.
2988c2ecf20Sopenharmony_ci *
2998c2ecf20Sopenharmony_ci * Any VF entries are removed from the SW and HW tables since either
3008c2ecf20Sopenharmony_ci * (a) the VF also gets reset on PF reset and will ask again for the
3018c2ecf20Sopenharmony_ci * offloads, or (b) the VF has been removed by a change in the num_vfs.
3028c2ecf20Sopenharmony_ci **/
3038c2ecf20Sopenharmony_civoid ixgbe_ipsec_restore(struct ixgbe_adapter *adapter)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec = adapter->ipsec;
3068c2ecf20Sopenharmony_ci	struct ixgbe_hw *hw = &adapter->hw;
3078c2ecf20Sopenharmony_ci	int i;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED))
3108c2ecf20Sopenharmony_ci		return;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/* clean up and restart the engine */
3138c2ecf20Sopenharmony_ci	ixgbe_ipsec_stop_engine(adapter);
3148c2ecf20Sopenharmony_ci	ixgbe_ipsec_clear_hw_tables(adapter);
3158c2ecf20Sopenharmony_ci	ixgbe_ipsec_start_engine(adapter);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* reload the Rx and Tx keys */
3188c2ecf20Sopenharmony_ci	for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
3198c2ecf20Sopenharmony_ci		struct rx_sa *r = &ipsec->rx_tbl[i];
3208c2ecf20Sopenharmony_ci		struct tx_sa *t = &ipsec->tx_tbl[i];
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci		if (r->used) {
3238c2ecf20Sopenharmony_ci			if (r->mode & IXGBE_RXTXMOD_VF)
3248c2ecf20Sopenharmony_ci				ixgbe_ipsec_del_sa(r->xs);
3258c2ecf20Sopenharmony_ci			else
3268c2ecf20Sopenharmony_ci				ixgbe_ipsec_set_rx_sa(hw, i, r->xs->id.spi,
3278c2ecf20Sopenharmony_ci						      r->key, r->salt,
3288c2ecf20Sopenharmony_ci						      r->mode, r->iptbl_ind);
3298c2ecf20Sopenharmony_ci		}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci		if (t->used) {
3328c2ecf20Sopenharmony_ci			if (t->mode & IXGBE_RXTXMOD_VF)
3338c2ecf20Sopenharmony_ci				ixgbe_ipsec_del_sa(t->xs);
3348c2ecf20Sopenharmony_ci			else
3358c2ecf20Sopenharmony_ci				ixgbe_ipsec_set_tx_sa(hw, i, t->key, t->salt);
3368c2ecf20Sopenharmony_ci		}
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	/* reload the IP addrs */
3408c2ecf20Sopenharmony_ci	for (i = 0; i < IXGBE_IPSEC_MAX_RX_IP_COUNT; i++) {
3418c2ecf20Sopenharmony_ci		struct rx_ip_sa *ipsa = &ipsec->ip_tbl[i];
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci		if (ipsa->used)
3448c2ecf20Sopenharmony_ci			ixgbe_ipsec_set_rx_ip(hw, i, ipsa->ipaddr);
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci/**
3498c2ecf20Sopenharmony_ci * ixgbe_ipsec_find_empty_idx - find the first unused security parameter index
3508c2ecf20Sopenharmony_ci * @ipsec: pointer to ipsec struct
3518c2ecf20Sopenharmony_ci * @rxtable: true if we need to look in the Rx table
3528c2ecf20Sopenharmony_ci *
3538c2ecf20Sopenharmony_ci * Returns the first unused index in either the Rx or Tx SA table
3548c2ecf20Sopenharmony_ci **/
3558c2ecf20Sopenharmony_cistatic int ixgbe_ipsec_find_empty_idx(struct ixgbe_ipsec *ipsec, bool rxtable)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	u32 i;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	if (rxtable) {
3608c2ecf20Sopenharmony_ci		if (ipsec->num_rx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
3618c2ecf20Sopenharmony_ci			return -ENOSPC;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci		/* search rx sa table */
3648c2ecf20Sopenharmony_ci		for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
3658c2ecf20Sopenharmony_ci			if (!ipsec->rx_tbl[i].used)
3668c2ecf20Sopenharmony_ci				return i;
3678c2ecf20Sopenharmony_ci		}
3688c2ecf20Sopenharmony_ci	} else {
3698c2ecf20Sopenharmony_ci		if (ipsec->num_tx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
3708c2ecf20Sopenharmony_ci			return -ENOSPC;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci		/* search tx sa table */
3738c2ecf20Sopenharmony_ci		for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
3748c2ecf20Sopenharmony_ci			if (!ipsec->tx_tbl[i].used)
3758c2ecf20Sopenharmony_ci				return i;
3768c2ecf20Sopenharmony_ci		}
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	return -ENOSPC;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci/**
3838c2ecf20Sopenharmony_ci * ixgbe_ipsec_find_rx_state - find the state that matches
3848c2ecf20Sopenharmony_ci * @ipsec: pointer to ipsec struct
3858c2ecf20Sopenharmony_ci * @daddr: inbound address to match
3868c2ecf20Sopenharmony_ci * @proto: protocol to match
3878c2ecf20Sopenharmony_ci * @spi: SPI to match
3888c2ecf20Sopenharmony_ci * @ip4: true if using an ipv4 address
3898c2ecf20Sopenharmony_ci *
3908c2ecf20Sopenharmony_ci * Returns a pointer to the matching SA state information
3918c2ecf20Sopenharmony_ci **/
3928c2ecf20Sopenharmony_cistatic struct xfrm_state *ixgbe_ipsec_find_rx_state(struct ixgbe_ipsec *ipsec,
3938c2ecf20Sopenharmony_ci						    __be32 *daddr, u8 proto,
3948c2ecf20Sopenharmony_ci						    __be32 spi, bool ip4)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	struct rx_sa *rsa;
3978c2ecf20Sopenharmony_ci	struct xfrm_state *ret = NULL;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	rcu_read_lock();
4008c2ecf20Sopenharmony_ci	hash_for_each_possible_rcu(ipsec->rx_sa_list, rsa, hlist,
4018c2ecf20Sopenharmony_ci				   (__force u32)spi) {
4028c2ecf20Sopenharmony_ci		if (rsa->mode & IXGBE_RXTXMOD_VF)
4038c2ecf20Sopenharmony_ci			continue;
4048c2ecf20Sopenharmony_ci		if (spi == rsa->xs->id.spi &&
4058c2ecf20Sopenharmony_ci		    ((ip4 && *daddr == rsa->xs->id.daddr.a4) ||
4068c2ecf20Sopenharmony_ci		      (!ip4 && !memcmp(daddr, &rsa->xs->id.daddr.a6,
4078c2ecf20Sopenharmony_ci				       sizeof(rsa->xs->id.daddr.a6)))) &&
4088c2ecf20Sopenharmony_ci		    proto == rsa->xs->id.proto) {
4098c2ecf20Sopenharmony_ci			ret = rsa->xs;
4108c2ecf20Sopenharmony_ci			xfrm_state_hold(ret);
4118c2ecf20Sopenharmony_ci			break;
4128c2ecf20Sopenharmony_ci		}
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci	rcu_read_unlock();
4158c2ecf20Sopenharmony_ci	return ret;
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci/**
4198c2ecf20Sopenharmony_ci * ixgbe_ipsec_parse_proto_keys - find the key and salt based on the protocol
4208c2ecf20Sopenharmony_ci * @xs: pointer to xfrm_state struct
4218c2ecf20Sopenharmony_ci * @mykey: pointer to key array to populate
4228c2ecf20Sopenharmony_ci * @mysalt: pointer to salt value to populate
4238c2ecf20Sopenharmony_ci *
4248c2ecf20Sopenharmony_ci * This copies the protocol keys and salt to our own data tables.  The
4258c2ecf20Sopenharmony_ci * 82599 family only supports the one algorithm.
4268c2ecf20Sopenharmony_ci **/
4278c2ecf20Sopenharmony_cistatic int ixgbe_ipsec_parse_proto_keys(struct xfrm_state *xs,
4288c2ecf20Sopenharmony_ci					u32 *mykey, u32 *mysalt)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	struct net_device *dev = xs->xso.real_dev;
4318c2ecf20Sopenharmony_ci	unsigned char *key_data;
4328c2ecf20Sopenharmony_ci	char *alg_name = NULL;
4338c2ecf20Sopenharmony_ci	int key_len;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	if (!xs->aead) {
4368c2ecf20Sopenharmony_ci		netdev_err(dev, "Unsupported IPsec algorithm\n");
4378c2ecf20Sopenharmony_ci		return -EINVAL;
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	if (xs->aead->alg_icv_len != IXGBE_IPSEC_AUTH_BITS) {
4418c2ecf20Sopenharmony_ci		netdev_err(dev, "IPsec offload requires %d bit authentication\n",
4428c2ecf20Sopenharmony_ci			   IXGBE_IPSEC_AUTH_BITS);
4438c2ecf20Sopenharmony_ci		return -EINVAL;
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	key_data = &xs->aead->alg_key[0];
4478c2ecf20Sopenharmony_ci	key_len = xs->aead->alg_key_len;
4488c2ecf20Sopenharmony_ci	alg_name = xs->aead->alg_name;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	if (strcmp(alg_name, aes_gcm_name)) {
4518c2ecf20Sopenharmony_ci		netdev_err(dev, "Unsupported IPsec algorithm - please use %s\n",
4528c2ecf20Sopenharmony_ci			   aes_gcm_name);
4538c2ecf20Sopenharmony_ci		return -EINVAL;
4548c2ecf20Sopenharmony_ci	}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	/* The key bytes come down in a bigendian array of bytes, so
4578c2ecf20Sopenharmony_ci	 * we don't need to do any byteswapping.
4588c2ecf20Sopenharmony_ci	 * 160 accounts for 16 byte key and 4 byte salt
4598c2ecf20Sopenharmony_ci	 */
4608c2ecf20Sopenharmony_ci	if (key_len == IXGBE_IPSEC_KEY_BITS) {
4618c2ecf20Sopenharmony_ci		*mysalt = ((u32 *)key_data)[4];
4628c2ecf20Sopenharmony_ci	} else if (key_len != (IXGBE_IPSEC_KEY_BITS - (sizeof(*mysalt) * 8))) {
4638c2ecf20Sopenharmony_ci		netdev_err(dev, "IPsec hw offload only supports keys up to 128 bits with a 32 bit salt\n");
4648c2ecf20Sopenharmony_ci		return -EINVAL;
4658c2ecf20Sopenharmony_ci	} else {
4668c2ecf20Sopenharmony_ci		netdev_info(dev, "IPsec hw offload parameters missing 32 bit salt value\n");
4678c2ecf20Sopenharmony_ci		*mysalt = 0;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci	memcpy(mykey, key_data, 16);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	return 0;
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci/**
4758c2ecf20Sopenharmony_ci * ixgbe_ipsec_check_mgmt_ip - make sure there is no clash with mgmt IP filters
4768c2ecf20Sopenharmony_ci * @xs: pointer to transformer state struct
4778c2ecf20Sopenharmony_ci **/
4788c2ecf20Sopenharmony_cistatic int ixgbe_ipsec_check_mgmt_ip(struct xfrm_state *xs)
4798c2ecf20Sopenharmony_ci{
4808c2ecf20Sopenharmony_ci	struct net_device *dev = xs->xso.real_dev;
4818c2ecf20Sopenharmony_ci	struct ixgbe_adapter *adapter = netdev_priv(dev);
4828c2ecf20Sopenharmony_ci	struct ixgbe_hw *hw = &adapter->hw;
4838c2ecf20Sopenharmony_ci	u32 mfval, manc, reg;
4848c2ecf20Sopenharmony_ci	int num_filters = 4;
4858c2ecf20Sopenharmony_ci	bool manc_ipv4;
4868c2ecf20Sopenharmony_ci	u32 bmcipval;
4878c2ecf20Sopenharmony_ci	int i, j;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci#define MANC_EN_IPV4_FILTER      BIT(24)
4908c2ecf20Sopenharmony_ci#define MFVAL_IPV4_FILTER_SHIFT  16
4918c2ecf20Sopenharmony_ci#define MFVAL_IPV6_FILTER_SHIFT  24
4928c2ecf20Sopenharmony_ci#define MIPAF_ARR(_m, _n)        (IXGBE_MIPAF + ((_m) * 0x10) + ((_n) * 4))
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci#define IXGBE_BMCIP(_n)          (0x5050 + ((_n) * 4))
4958c2ecf20Sopenharmony_ci#define IXGBE_BMCIPVAL           0x5060
4968c2ecf20Sopenharmony_ci#define BMCIP_V4                 0x2
4978c2ecf20Sopenharmony_ci#define BMCIP_V6                 0x3
4988c2ecf20Sopenharmony_ci#define BMCIP_MASK               0x3
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	manc = IXGBE_READ_REG(hw, IXGBE_MANC);
5018c2ecf20Sopenharmony_ci	manc_ipv4 = !!(manc & MANC_EN_IPV4_FILTER);
5028c2ecf20Sopenharmony_ci	mfval = IXGBE_READ_REG(hw, IXGBE_MFVAL);
5038c2ecf20Sopenharmony_ci	bmcipval = IXGBE_READ_REG(hw, IXGBE_BMCIPVAL);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	if (xs->props.family == AF_INET) {
5068c2ecf20Sopenharmony_ci		/* are there any IPv4 filters to check? */
5078c2ecf20Sopenharmony_ci		if (manc_ipv4) {
5088c2ecf20Sopenharmony_ci			/* the 4 ipv4 filters are all in MIPAF(3, i) */
5098c2ecf20Sopenharmony_ci			for (i = 0; i < num_filters; i++) {
5108c2ecf20Sopenharmony_ci				if (!(mfval & BIT(MFVAL_IPV4_FILTER_SHIFT + i)))
5118c2ecf20Sopenharmony_ci					continue;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci				reg = IXGBE_READ_REG(hw, MIPAF_ARR(3, i));
5148c2ecf20Sopenharmony_ci				if (reg == xs->id.daddr.a4)
5158c2ecf20Sopenharmony_ci					return 1;
5168c2ecf20Sopenharmony_ci			}
5178c2ecf20Sopenharmony_ci		}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci		if ((bmcipval & BMCIP_MASK) == BMCIP_V4) {
5208c2ecf20Sopenharmony_ci			reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(3));
5218c2ecf20Sopenharmony_ci			if (reg == xs->id.daddr.a4)
5228c2ecf20Sopenharmony_ci				return 1;
5238c2ecf20Sopenharmony_ci		}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	} else {
5268c2ecf20Sopenharmony_ci		/* if there are ipv4 filters, they are in the last ipv6 slot */
5278c2ecf20Sopenharmony_ci		if (manc_ipv4)
5288c2ecf20Sopenharmony_ci			num_filters = 3;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci		for (i = 0; i < num_filters; i++) {
5318c2ecf20Sopenharmony_ci			if (!(mfval & BIT(MFVAL_IPV6_FILTER_SHIFT + i)))
5328c2ecf20Sopenharmony_ci				continue;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci			for (j = 0; j < 4; j++) {
5358c2ecf20Sopenharmony_ci				reg = IXGBE_READ_REG(hw, MIPAF_ARR(i, j));
5368c2ecf20Sopenharmony_ci				if (reg != xs->id.daddr.a6[j])
5378c2ecf20Sopenharmony_ci					break;
5388c2ecf20Sopenharmony_ci			}
5398c2ecf20Sopenharmony_ci			if (j == 4)   /* did we match all 4 words? */
5408c2ecf20Sopenharmony_ci				return 1;
5418c2ecf20Sopenharmony_ci		}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci		if ((bmcipval & BMCIP_MASK) == BMCIP_V6) {
5448c2ecf20Sopenharmony_ci			for (j = 0; j < 4; j++) {
5458c2ecf20Sopenharmony_ci				reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(j));
5468c2ecf20Sopenharmony_ci				if (reg != xs->id.daddr.a6[j])
5478c2ecf20Sopenharmony_ci					break;
5488c2ecf20Sopenharmony_ci			}
5498c2ecf20Sopenharmony_ci			if (j == 4)   /* did we match all 4 words? */
5508c2ecf20Sopenharmony_ci				return 1;
5518c2ecf20Sopenharmony_ci		}
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	return 0;
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci/**
5588c2ecf20Sopenharmony_ci * ixgbe_ipsec_add_sa - program device with a security association
5598c2ecf20Sopenharmony_ci * @xs: pointer to transformer state struct
5608c2ecf20Sopenharmony_ci **/
5618c2ecf20Sopenharmony_cistatic int ixgbe_ipsec_add_sa(struct xfrm_state *xs)
5628c2ecf20Sopenharmony_ci{
5638c2ecf20Sopenharmony_ci	struct net_device *dev = xs->xso.real_dev;
5648c2ecf20Sopenharmony_ci	struct ixgbe_adapter *adapter = netdev_priv(dev);
5658c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec = adapter->ipsec;
5668c2ecf20Sopenharmony_ci	struct ixgbe_hw *hw = &adapter->hw;
5678c2ecf20Sopenharmony_ci	int checked, match, first;
5688c2ecf20Sopenharmony_ci	u16 sa_idx;
5698c2ecf20Sopenharmony_ci	int ret;
5708c2ecf20Sopenharmony_ci	int i;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	if (xs->id.proto != IPPROTO_ESP && xs->id.proto != IPPROTO_AH) {
5738c2ecf20Sopenharmony_ci		netdev_err(dev, "Unsupported protocol 0x%04x for ipsec offload\n",
5748c2ecf20Sopenharmony_ci			   xs->id.proto);
5758c2ecf20Sopenharmony_ci		return -EINVAL;
5768c2ecf20Sopenharmony_ci	}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	if (xs->props.mode != XFRM_MODE_TRANSPORT) {
5798c2ecf20Sopenharmony_ci		netdev_err(dev, "Unsupported mode for ipsec offload\n");
5808c2ecf20Sopenharmony_ci		return -EINVAL;
5818c2ecf20Sopenharmony_ci	}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	if (ixgbe_ipsec_check_mgmt_ip(xs)) {
5848c2ecf20Sopenharmony_ci		netdev_err(dev, "IPsec IP addr clash with mgmt filters\n");
5858c2ecf20Sopenharmony_ci		return -EINVAL;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) {
5898c2ecf20Sopenharmony_ci		struct rx_sa rsa;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci		if (xs->calg) {
5928c2ecf20Sopenharmony_ci			netdev_err(dev, "Compression offload not supported\n");
5938c2ecf20Sopenharmony_ci			return -EINVAL;
5948c2ecf20Sopenharmony_ci		}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci		/* find the first unused index */
5978c2ecf20Sopenharmony_ci		ret = ixgbe_ipsec_find_empty_idx(ipsec, true);
5988c2ecf20Sopenharmony_ci		if (ret < 0) {
5998c2ecf20Sopenharmony_ci			netdev_err(dev, "No space for SA in Rx table!\n");
6008c2ecf20Sopenharmony_ci			return ret;
6018c2ecf20Sopenharmony_ci		}
6028c2ecf20Sopenharmony_ci		sa_idx = (u16)ret;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci		memset(&rsa, 0, sizeof(rsa));
6058c2ecf20Sopenharmony_ci		rsa.used = true;
6068c2ecf20Sopenharmony_ci		rsa.xs = xs;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci		if (rsa.xs->id.proto & IPPROTO_ESP)
6098c2ecf20Sopenharmony_ci			rsa.decrypt = xs->ealg || xs->aead;
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci		/* get the key and salt */
6128c2ecf20Sopenharmony_ci		ret = ixgbe_ipsec_parse_proto_keys(xs, rsa.key, &rsa.salt);
6138c2ecf20Sopenharmony_ci		if (ret) {
6148c2ecf20Sopenharmony_ci			netdev_err(dev, "Failed to get key data for Rx SA table\n");
6158c2ecf20Sopenharmony_ci			return ret;
6168c2ecf20Sopenharmony_ci		}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci		/* get ip for rx sa table */
6198c2ecf20Sopenharmony_ci		if (xs->props.family == AF_INET6)
6208c2ecf20Sopenharmony_ci			memcpy(rsa.ipaddr, &xs->id.daddr.a6, 16);
6218c2ecf20Sopenharmony_ci		else
6228c2ecf20Sopenharmony_ci			memcpy(&rsa.ipaddr[3], &xs->id.daddr.a4, 4);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci		/* The HW does not have a 1:1 mapping from keys to IP addrs, so
6258c2ecf20Sopenharmony_ci		 * check for a matching IP addr entry in the table.  If the addr
6268c2ecf20Sopenharmony_ci		 * already exists, use it; else find an unused slot and add the
6278c2ecf20Sopenharmony_ci		 * addr.  If one does not exist and there are no unused table
6288c2ecf20Sopenharmony_ci		 * entries, fail the request.
6298c2ecf20Sopenharmony_ci		 */
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci		/* Find an existing match or first not used, and stop looking
6328c2ecf20Sopenharmony_ci		 * after we've checked all we know we have.
6338c2ecf20Sopenharmony_ci		 */
6348c2ecf20Sopenharmony_ci		checked = 0;
6358c2ecf20Sopenharmony_ci		match = -1;
6368c2ecf20Sopenharmony_ci		first = -1;
6378c2ecf20Sopenharmony_ci		for (i = 0;
6388c2ecf20Sopenharmony_ci		     i < IXGBE_IPSEC_MAX_RX_IP_COUNT &&
6398c2ecf20Sopenharmony_ci		     (checked < ipsec->num_rx_sa || first < 0);
6408c2ecf20Sopenharmony_ci		     i++) {
6418c2ecf20Sopenharmony_ci			if (ipsec->ip_tbl[i].used) {
6428c2ecf20Sopenharmony_ci				if (!memcmp(ipsec->ip_tbl[i].ipaddr,
6438c2ecf20Sopenharmony_ci					    rsa.ipaddr, sizeof(rsa.ipaddr))) {
6448c2ecf20Sopenharmony_ci					match = i;
6458c2ecf20Sopenharmony_ci					break;
6468c2ecf20Sopenharmony_ci				}
6478c2ecf20Sopenharmony_ci				checked++;
6488c2ecf20Sopenharmony_ci			} else if (first < 0) {
6498c2ecf20Sopenharmony_ci				first = i;  /* track the first empty seen */
6508c2ecf20Sopenharmony_ci			}
6518c2ecf20Sopenharmony_ci		}
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci		if (ipsec->num_rx_sa == 0)
6548c2ecf20Sopenharmony_ci			first = 0;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci		if (match >= 0) {
6578c2ecf20Sopenharmony_ci			/* addrs are the same, we should use this one */
6588c2ecf20Sopenharmony_ci			rsa.iptbl_ind = match;
6598c2ecf20Sopenharmony_ci			ipsec->ip_tbl[match].ref_cnt++;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci		} else if (first >= 0) {
6628c2ecf20Sopenharmony_ci			/* no matches, but here's an empty slot */
6638c2ecf20Sopenharmony_ci			rsa.iptbl_ind = first;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci			memcpy(ipsec->ip_tbl[first].ipaddr,
6668c2ecf20Sopenharmony_ci			       rsa.ipaddr, sizeof(rsa.ipaddr));
6678c2ecf20Sopenharmony_ci			ipsec->ip_tbl[first].ref_cnt = 1;
6688c2ecf20Sopenharmony_ci			ipsec->ip_tbl[first].used = true;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci			ixgbe_ipsec_set_rx_ip(hw, rsa.iptbl_ind, rsa.ipaddr);
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci		} else {
6738c2ecf20Sopenharmony_ci			/* no match and no empty slot */
6748c2ecf20Sopenharmony_ci			netdev_err(dev, "No space for SA in Rx IP SA table\n");
6758c2ecf20Sopenharmony_ci			memset(&rsa, 0, sizeof(rsa));
6768c2ecf20Sopenharmony_ci			return -ENOSPC;
6778c2ecf20Sopenharmony_ci		}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci		rsa.mode = IXGBE_RXMOD_VALID;
6808c2ecf20Sopenharmony_ci		if (rsa.xs->id.proto & IPPROTO_ESP)
6818c2ecf20Sopenharmony_ci			rsa.mode |= IXGBE_RXMOD_PROTO_ESP;
6828c2ecf20Sopenharmony_ci		if (rsa.decrypt)
6838c2ecf20Sopenharmony_ci			rsa.mode |= IXGBE_RXMOD_DECRYPT;
6848c2ecf20Sopenharmony_ci		if (rsa.xs->props.family == AF_INET6)
6858c2ecf20Sopenharmony_ci			rsa.mode |= IXGBE_RXMOD_IPV6;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci		/* the preparations worked, so save the info */
6888c2ecf20Sopenharmony_ci		memcpy(&ipsec->rx_tbl[sa_idx], &rsa, sizeof(rsa));
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci		ixgbe_ipsec_set_rx_sa(hw, sa_idx, rsa.xs->id.spi, rsa.key,
6918c2ecf20Sopenharmony_ci				      rsa.salt, rsa.mode, rsa.iptbl_ind);
6928c2ecf20Sopenharmony_ci		xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_RX_INDEX;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci		ipsec->num_rx_sa++;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci		/* hash the new entry for faster search in Rx path */
6978c2ecf20Sopenharmony_ci		hash_add_rcu(ipsec->rx_sa_list, &ipsec->rx_tbl[sa_idx].hlist,
6988c2ecf20Sopenharmony_ci			     (__force u32)rsa.xs->id.spi);
6998c2ecf20Sopenharmony_ci	} else {
7008c2ecf20Sopenharmony_ci		struct tx_sa tsa;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci		if (adapter->num_vfs &&
7038c2ecf20Sopenharmony_ci		    adapter->bridge_mode != BRIDGE_MODE_VEPA)
7048c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci		/* find the first unused index */
7078c2ecf20Sopenharmony_ci		ret = ixgbe_ipsec_find_empty_idx(ipsec, false);
7088c2ecf20Sopenharmony_ci		if (ret < 0) {
7098c2ecf20Sopenharmony_ci			netdev_err(dev, "No space for SA in Tx table\n");
7108c2ecf20Sopenharmony_ci			return ret;
7118c2ecf20Sopenharmony_ci		}
7128c2ecf20Sopenharmony_ci		sa_idx = (u16)ret;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci		memset(&tsa, 0, sizeof(tsa));
7158c2ecf20Sopenharmony_ci		tsa.used = true;
7168c2ecf20Sopenharmony_ci		tsa.xs = xs;
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci		if (xs->id.proto & IPPROTO_ESP)
7198c2ecf20Sopenharmony_ci			tsa.encrypt = xs->ealg || xs->aead;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci		ret = ixgbe_ipsec_parse_proto_keys(xs, tsa.key, &tsa.salt);
7228c2ecf20Sopenharmony_ci		if (ret) {
7238c2ecf20Sopenharmony_ci			netdev_err(dev, "Failed to get key data for Tx SA table\n");
7248c2ecf20Sopenharmony_ci			memset(&tsa, 0, sizeof(tsa));
7258c2ecf20Sopenharmony_ci			return ret;
7268c2ecf20Sopenharmony_ci		}
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci		/* the preparations worked, so save the info */
7298c2ecf20Sopenharmony_ci		memcpy(&ipsec->tx_tbl[sa_idx], &tsa, sizeof(tsa));
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci		ixgbe_ipsec_set_tx_sa(hw, sa_idx, tsa.key, tsa.salt);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_TX_INDEX;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci		ipsec->num_tx_sa++;
7368c2ecf20Sopenharmony_ci	}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	/* enable the engine if not already warmed up */
7398c2ecf20Sopenharmony_ci	if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED)) {
7408c2ecf20Sopenharmony_ci		ixgbe_ipsec_start_engine(adapter);
7418c2ecf20Sopenharmony_ci		adapter->flags2 |= IXGBE_FLAG2_IPSEC_ENABLED;
7428c2ecf20Sopenharmony_ci	}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	return 0;
7458c2ecf20Sopenharmony_ci}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci/**
7488c2ecf20Sopenharmony_ci * ixgbe_ipsec_del_sa - clear out this specific SA
7498c2ecf20Sopenharmony_ci * @xs: pointer to transformer state struct
7508c2ecf20Sopenharmony_ci **/
7518c2ecf20Sopenharmony_cistatic void ixgbe_ipsec_del_sa(struct xfrm_state *xs)
7528c2ecf20Sopenharmony_ci{
7538c2ecf20Sopenharmony_ci	struct net_device *dev = xs->xso.real_dev;
7548c2ecf20Sopenharmony_ci	struct ixgbe_adapter *adapter = netdev_priv(dev);
7558c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec = adapter->ipsec;
7568c2ecf20Sopenharmony_ci	struct ixgbe_hw *hw = &adapter->hw;
7578c2ecf20Sopenharmony_ci	u32 zerobuf[4] = {0, 0, 0, 0};
7588c2ecf20Sopenharmony_ci	u16 sa_idx;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) {
7618c2ecf20Sopenharmony_ci		struct rx_sa *rsa;
7628c2ecf20Sopenharmony_ci		u8 ipi;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci		sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_RX_INDEX;
7658c2ecf20Sopenharmony_ci		rsa = &ipsec->rx_tbl[sa_idx];
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci		if (!rsa->used) {
7688c2ecf20Sopenharmony_ci			netdev_err(dev, "Invalid Rx SA selected sa_idx=%d offload_handle=%lu\n",
7698c2ecf20Sopenharmony_ci				   sa_idx, xs->xso.offload_handle);
7708c2ecf20Sopenharmony_ci			return;
7718c2ecf20Sopenharmony_ci		}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci		ixgbe_ipsec_set_rx_sa(hw, sa_idx, 0, zerobuf, 0, 0, 0);
7748c2ecf20Sopenharmony_ci		hash_del_rcu(&rsa->hlist);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci		/* if the IP table entry is referenced by only this SA,
7778c2ecf20Sopenharmony_ci		 * i.e. ref_cnt is only 1, clear the IP table entry as well
7788c2ecf20Sopenharmony_ci		 */
7798c2ecf20Sopenharmony_ci		ipi = rsa->iptbl_ind;
7808c2ecf20Sopenharmony_ci		if (ipsec->ip_tbl[ipi].ref_cnt > 0) {
7818c2ecf20Sopenharmony_ci			ipsec->ip_tbl[ipi].ref_cnt--;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci			if (!ipsec->ip_tbl[ipi].ref_cnt) {
7848c2ecf20Sopenharmony_ci				memset(&ipsec->ip_tbl[ipi], 0,
7858c2ecf20Sopenharmony_ci				       sizeof(struct rx_ip_sa));
7868c2ecf20Sopenharmony_ci				ixgbe_ipsec_set_rx_ip(hw, ipi,
7878c2ecf20Sopenharmony_ci						      (__force __be32 *)zerobuf);
7888c2ecf20Sopenharmony_ci			}
7898c2ecf20Sopenharmony_ci		}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci		memset(rsa, 0, sizeof(struct rx_sa));
7928c2ecf20Sopenharmony_ci		ipsec->num_rx_sa--;
7938c2ecf20Sopenharmony_ci	} else {
7948c2ecf20Sopenharmony_ci		sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci		if (!ipsec->tx_tbl[sa_idx].used) {
7978c2ecf20Sopenharmony_ci			netdev_err(dev, "Invalid Tx SA selected sa_idx=%d offload_handle=%lu\n",
7988c2ecf20Sopenharmony_ci				   sa_idx, xs->xso.offload_handle);
7998c2ecf20Sopenharmony_ci			return;
8008c2ecf20Sopenharmony_ci		}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci		ixgbe_ipsec_set_tx_sa(hw, sa_idx, zerobuf, 0);
8038c2ecf20Sopenharmony_ci		memset(&ipsec->tx_tbl[sa_idx], 0, sizeof(struct tx_sa));
8048c2ecf20Sopenharmony_ci		ipsec->num_tx_sa--;
8058c2ecf20Sopenharmony_ci	}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	/* if there are no SAs left, stop the engine to save energy */
8088c2ecf20Sopenharmony_ci	if (ipsec->num_rx_sa == 0 && ipsec->num_tx_sa == 0) {
8098c2ecf20Sopenharmony_ci		adapter->flags2 &= ~IXGBE_FLAG2_IPSEC_ENABLED;
8108c2ecf20Sopenharmony_ci		ixgbe_ipsec_stop_engine(adapter);
8118c2ecf20Sopenharmony_ci	}
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci/**
8158c2ecf20Sopenharmony_ci * ixgbe_ipsec_offload_ok - can this packet use the xfrm hw offload
8168c2ecf20Sopenharmony_ci * @skb: current data packet
8178c2ecf20Sopenharmony_ci * @xs: pointer to transformer state struct
8188c2ecf20Sopenharmony_ci **/
8198c2ecf20Sopenharmony_cistatic bool ixgbe_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
8208c2ecf20Sopenharmony_ci{
8218c2ecf20Sopenharmony_ci	if (xs->props.family == AF_INET) {
8228c2ecf20Sopenharmony_ci		/* Offload with IPv4 options is not supported yet */
8238c2ecf20Sopenharmony_ci		if (ip_hdr(skb)->ihl != 5)
8248c2ecf20Sopenharmony_ci			return false;
8258c2ecf20Sopenharmony_ci	} else {
8268c2ecf20Sopenharmony_ci		/* Offload with IPv6 extension headers is not support yet */
8278c2ecf20Sopenharmony_ci		if (ipv6_ext_hdr(ipv6_hdr(skb)->nexthdr))
8288c2ecf20Sopenharmony_ci			return false;
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	return true;
8328c2ecf20Sopenharmony_ci}
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_cistatic const struct xfrmdev_ops ixgbe_xfrmdev_ops = {
8358c2ecf20Sopenharmony_ci	.xdo_dev_state_add = ixgbe_ipsec_add_sa,
8368c2ecf20Sopenharmony_ci	.xdo_dev_state_delete = ixgbe_ipsec_del_sa,
8378c2ecf20Sopenharmony_ci	.xdo_dev_offload_ok = ixgbe_ipsec_offload_ok,
8388c2ecf20Sopenharmony_ci};
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci/**
8418c2ecf20Sopenharmony_ci * ixgbe_ipsec_vf_clear - clear the tables of data for a VF
8428c2ecf20Sopenharmony_ci * @adapter: board private structure
8438c2ecf20Sopenharmony_ci * @vf: VF id to be removed
8448c2ecf20Sopenharmony_ci **/
8458c2ecf20Sopenharmony_civoid ixgbe_ipsec_vf_clear(struct ixgbe_adapter *adapter, u32 vf)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec = adapter->ipsec;
8488c2ecf20Sopenharmony_ci	int i;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	if (!ipsec)
8518c2ecf20Sopenharmony_ci		return;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	/* search rx sa table */
8548c2ecf20Sopenharmony_ci	for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_rx_sa; i++) {
8558c2ecf20Sopenharmony_ci		if (!ipsec->rx_tbl[i].used)
8568c2ecf20Sopenharmony_ci			continue;
8578c2ecf20Sopenharmony_ci		if (ipsec->rx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
8588c2ecf20Sopenharmony_ci		    ipsec->rx_tbl[i].vf == vf)
8598c2ecf20Sopenharmony_ci			ixgbe_ipsec_del_sa(ipsec->rx_tbl[i].xs);
8608c2ecf20Sopenharmony_ci	}
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	/* search tx sa table */
8638c2ecf20Sopenharmony_ci	for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_tx_sa; i++) {
8648c2ecf20Sopenharmony_ci		if (!ipsec->tx_tbl[i].used)
8658c2ecf20Sopenharmony_ci			continue;
8668c2ecf20Sopenharmony_ci		if (ipsec->tx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
8678c2ecf20Sopenharmony_ci		    ipsec->tx_tbl[i].vf == vf)
8688c2ecf20Sopenharmony_ci			ixgbe_ipsec_del_sa(ipsec->tx_tbl[i].xs);
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci/**
8738c2ecf20Sopenharmony_ci * ixgbe_ipsec_vf_add_sa - translate VF request to SA add
8748c2ecf20Sopenharmony_ci * @adapter: board private structure
8758c2ecf20Sopenharmony_ci * @msgbuf: The message buffer
8768c2ecf20Sopenharmony_ci * @vf: the VF index
8778c2ecf20Sopenharmony_ci *
8788c2ecf20Sopenharmony_ci * Make up a new xs and algorithm info from the data sent by the VF.
8798c2ecf20Sopenharmony_ci * We only need to sketch in just enough to set up the HW offload.
8808c2ecf20Sopenharmony_ci * Put the resulting offload_handle into the return message to the VF.
8818c2ecf20Sopenharmony_ci *
8828c2ecf20Sopenharmony_ci * Returns 0 or error value
8838c2ecf20Sopenharmony_ci **/
8848c2ecf20Sopenharmony_ciint ixgbe_ipsec_vf_add_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
8858c2ecf20Sopenharmony_ci{
8868c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec = adapter->ipsec;
8878c2ecf20Sopenharmony_ci	struct xfrm_algo_desc *algo;
8888c2ecf20Sopenharmony_ci	struct sa_mbx_msg *sam;
8898c2ecf20Sopenharmony_ci	struct xfrm_state *xs;
8908c2ecf20Sopenharmony_ci	size_t aead_len;
8918c2ecf20Sopenharmony_ci	u16 sa_idx;
8928c2ecf20Sopenharmony_ci	u32 pfsa;
8938c2ecf20Sopenharmony_ci	int err;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	sam = (struct sa_mbx_msg *)(&msgbuf[1]);
8968c2ecf20Sopenharmony_ci	if (!adapter->vfinfo[vf].trusted ||
8978c2ecf20Sopenharmony_ci	    !(adapter->flags2 & IXGBE_FLAG2_VF_IPSEC_ENABLED)) {
8988c2ecf20Sopenharmony_ci		e_warn(drv, "VF %d attempted to add an IPsec SA\n", vf);
8998c2ecf20Sopenharmony_ci		err = -EACCES;
9008c2ecf20Sopenharmony_ci		goto err_out;
9018c2ecf20Sopenharmony_ci	}
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	/* Tx IPsec offload doesn't seem to work on this
9048c2ecf20Sopenharmony_ci	 * device, so block these requests for now.
9058c2ecf20Sopenharmony_ci	 */
9068c2ecf20Sopenharmony_ci	sam->flags = sam->flags & ~XFRM_OFFLOAD_IPV6;
9078c2ecf20Sopenharmony_ci	if (sam->flags != XFRM_OFFLOAD_INBOUND) {
9088c2ecf20Sopenharmony_ci		err = -EOPNOTSUPP;
9098c2ecf20Sopenharmony_ci		goto err_out;
9108c2ecf20Sopenharmony_ci	}
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	xs = kzalloc(sizeof(*xs), GFP_KERNEL);
9138c2ecf20Sopenharmony_ci	if (unlikely(!xs)) {
9148c2ecf20Sopenharmony_ci		err = -ENOMEM;
9158c2ecf20Sopenharmony_ci		goto err_out;
9168c2ecf20Sopenharmony_ci	}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	xs->xso.flags = sam->flags;
9198c2ecf20Sopenharmony_ci	xs->id.spi = sam->spi;
9208c2ecf20Sopenharmony_ci	xs->id.proto = sam->proto;
9218c2ecf20Sopenharmony_ci	xs->props.family = sam->family;
9228c2ecf20Sopenharmony_ci	if (xs->props.family == AF_INET6)
9238c2ecf20Sopenharmony_ci		memcpy(&xs->id.daddr.a6, sam->addr, sizeof(xs->id.daddr.a6));
9248c2ecf20Sopenharmony_ci	else
9258c2ecf20Sopenharmony_ci		memcpy(&xs->id.daddr.a4, sam->addr, sizeof(xs->id.daddr.a4));
9268c2ecf20Sopenharmony_ci	xs->xso.dev = adapter->netdev;
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	algo = xfrm_aead_get_byname(aes_gcm_name, IXGBE_IPSEC_AUTH_BITS, 1);
9298c2ecf20Sopenharmony_ci	if (unlikely(!algo)) {
9308c2ecf20Sopenharmony_ci		err = -ENOENT;
9318c2ecf20Sopenharmony_ci		goto err_xs;
9328c2ecf20Sopenharmony_ci	}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	aead_len = sizeof(*xs->aead) + IXGBE_IPSEC_KEY_BITS / 8;
9358c2ecf20Sopenharmony_ci	xs->aead = kzalloc(aead_len, GFP_KERNEL);
9368c2ecf20Sopenharmony_ci	if (unlikely(!xs->aead)) {
9378c2ecf20Sopenharmony_ci		err = -ENOMEM;
9388c2ecf20Sopenharmony_ci		goto err_xs;
9398c2ecf20Sopenharmony_ci	}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	xs->props.ealgo = algo->desc.sadb_alg_id;
9428c2ecf20Sopenharmony_ci	xs->geniv = algo->uinfo.aead.geniv;
9438c2ecf20Sopenharmony_ci	xs->aead->alg_icv_len = IXGBE_IPSEC_AUTH_BITS;
9448c2ecf20Sopenharmony_ci	xs->aead->alg_key_len = IXGBE_IPSEC_KEY_BITS;
9458c2ecf20Sopenharmony_ci	memcpy(xs->aead->alg_key, sam->key, sizeof(sam->key));
9468c2ecf20Sopenharmony_ci	memcpy(xs->aead->alg_name, aes_gcm_name, sizeof(aes_gcm_name));
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	/* set up the HW offload */
9498c2ecf20Sopenharmony_ci	err = ixgbe_ipsec_add_sa(xs);
9508c2ecf20Sopenharmony_ci	if (err)
9518c2ecf20Sopenharmony_ci		goto err_aead;
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	pfsa = xs->xso.offload_handle;
9548c2ecf20Sopenharmony_ci	if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
9558c2ecf20Sopenharmony_ci		sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
9568c2ecf20Sopenharmony_ci		ipsec->rx_tbl[sa_idx].vf = vf;
9578c2ecf20Sopenharmony_ci		ipsec->rx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
9588c2ecf20Sopenharmony_ci	} else {
9598c2ecf20Sopenharmony_ci		sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
9608c2ecf20Sopenharmony_ci		ipsec->tx_tbl[sa_idx].vf = vf;
9618c2ecf20Sopenharmony_ci		ipsec->tx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
9628c2ecf20Sopenharmony_ci	}
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	msgbuf[1] = xs->xso.offload_handle;
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	return 0;
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_cierr_aead:
9698c2ecf20Sopenharmony_ci	kfree_sensitive(xs->aead);
9708c2ecf20Sopenharmony_cierr_xs:
9718c2ecf20Sopenharmony_ci	kfree_sensitive(xs);
9728c2ecf20Sopenharmony_cierr_out:
9738c2ecf20Sopenharmony_ci	msgbuf[1] = err;
9748c2ecf20Sopenharmony_ci	return err;
9758c2ecf20Sopenharmony_ci}
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci/**
9788c2ecf20Sopenharmony_ci * ixgbe_ipsec_vf_del_sa - translate VF request to SA delete
9798c2ecf20Sopenharmony_ci * @adapter: board private structure
9808c2ecf20Sopenharmony_ci * @msgbuf: The message buffer
9818c2ecf20Sopenharmony_ci * @vf: the VF index
9828c2ecf20Sopenharmony_ci *
9838c2ecf20Sopenharmony_ci * Given the offload_handle sent by the VF, look for the related SA table
9848c2ecf20Sopenharmony_ci * entry and use its xs field to call for a delete of the SA.
9858c2ecf20Sopenharmony_ci *
9868c2ecf20Sopenharmony_ci * Note: We silently ignore requests to delete entries that are already
9878c2ecf20Sopenharmony_ci *       set to unused because when a VF is set to "DOWN", the PF first
9888c2ecf20Sopenharmony_ci *       gets a reset and clears all the VF's entries; then the VF's
9898c2ecf20Sopenharmony_ci *       XFRM stack sends individual deletes for each entry, which the
9908c2ecf20Sopenharmony_ci *       reset already removed.  In the future it might be good to try to
9918c2ecf20Sopenharmony_ci *       optimize this so not so many unnecessary delete messages are sent.
9928c2ecf20Sopenharmony_ci *
9938c2ecf20Sopenharmony_ci * Returns 0 or error value
9948c2ecf20Sopenharmony_ci **/
9958c2ecf20Sopenharmony_ciint ixgbe_ipsec_vf_del_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
9968c2ecf20Sopenharmony_ci{
9978c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec = adapter->ipsec;
9988c2ecf20Sopenharmony_ci	struct xfrm_state *xs;
9998c2ecf20Sopenharmony_ci	u32 pfsa = msgbuf[1];
10008c2ecf20Sopenharmony_ci	u16 sa_idx;
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	if (!adapter->vfinfo[vf].trusted) {
10038c2ecf20Sopenharmony_ci		e_err(drv, "vf %d attempted to delete an SA\n", vf);
10048c2ecf20Sopenharmony_ci		return -EPERM;
10058c2ecf20Sopenharmony_ci	}
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
10088c2ecf20Sopenharmony_ci		struct rx_sa *rsa;
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci		sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
10118c2ecf20Sopenharmony_ci		if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
10128c2ecf20Sopenharmony_ci			e_err(drv, "vf %d SA index %d out of range\n",
10138c2ecf20Sopenharmony_ci			      vf, sa_idx);
10148c2ecf20Sopenharmony_ci			return -EINVAL;
10158c2ecf20Sopenharmony_ci		}
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci		rsa = &ipsec->rx_tbl[sa_idx];
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci		if (!rsa->used)
10208c2ecf20Sopenharmony_ci			return 0;
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci		if (!(rsa->mode & IXGBE_RXTXMOD_VF) ||
10238c2ecf20Sopenharmony_ci		    rsa->vf != vf) {
10248c2ecf20Sopenharmony_ci			e_err(drv, "vf %d bad Rx SA index %d\n", vf, sa_idx);
10258c2ecf20Sopenharmony_ci			return -ENOENT;
10268c2ecf20Sopenharmony_ci		}
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci		xs = ipsec->rx_tbl[sa_idx].xs;
10298c2ecf20Sopenharmony_ci	} else {
10308c2ecf20Sopenharmony_ci		struct tx_sa *tsa;
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci		sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
10338c2ecf20Sopenharmony_ci		if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
10348c2ecf20Sopenharmony_ci			e_err(drv, "vf %d SA index %d out of range\n",
10358c2ecf20Sopenharmony_ci			      vf, sa_idx);
10368c2ecf20Sopenharmony_ci			return -EINVAL;
10378c2ecf20Sopenharmony_ci		}
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci		tsa = &ipsec->tx_tbl[sa_idx];
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci		if (!tsa->used)
10428c2ecf20Sopenharmony_ci			return 0;
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci		if (!(tsa->mode & IXGBE_RXTXMOD_VF) ||
10458c2ecf20Sopenharmony_ci		    tsa->vf != vf) {
10468c2ecf20Sopenharmony_ci			e_err(drv, "vf %d bad Tx SA index %d\n", vf, sa_idx);
10478c2ecf20Sopenharmony_ci			return -ENOENT;
10488c2ecf20Sopenharmony_ci		}
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci		xs = ipsec->tx_tbl[sa_idx].xs;
10518c2ecf20Sopenharmony_ci	}
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	ixgbe_ipsec_del_sa(xs);
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	/* remove the xs that was made-up in the add request */
10568c2ecf20Sopenharmony_ci	kfree_sensitive(xs);
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	return 0;
10598c2ecf20Sopenharmony_ci}
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci/**
10628c2ecf20Sopenharmony_ci * ixgbe_ipsec_tx - setup Tx flags for ipsec offload
10638c2ecf20Sopenharmony_ci * @tx_ring: outgoing context
10648c2ecf20Sopenharmony_ci * @first: current data packet
10658c2ecf20Sopenharmony_ci * @itd: ipsec Tx data for later use in building context descriptor
10668c2ecf20Sopenharmony_ci **/
10678c2ecf20Sopenharmony_ciint ixgbe_ipsec_tx(struct ixgbe_ring *tx_ring,
10688c2ecf20Sopenharmony_ci		   struct ixgbe_tx_buffer *first,
10698c2ecf20Sopenharmony_ci		   struct ixgbe_ipsec_tx_data *itd)
10708c2ecf20Sopenharmony_ci{
10718c2ecf20Sopenharmony_ci	struct ixgbe_adapter *adapter = netdev_priv(tx_ring->netdev);
10728c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec = adapter->ipsec;
10738c2ecf20Sopenharmony_ci	struct xfrm_state *xs;
10748c2ecf20Sopenharmony_ci	struct sec_path *sp;
10758c2ecf20Sopenharmony_ci	struct tx_sa *tsa;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	sp = skb_sec_path(first->skb);
10788c2ecf20Sopenharmony_ci	if (unlikely(!sp->len)) {
10798c2ecf20Sopenharmony_ci		netdev_err(tx_ring->netdev, "%s: no xfrm state len = %d\n",
10808c2ecf20Sopenharmony_ci			   __func__, sp->len);
10818c2ecf20Sopenharmony_ci		return 0;
10828c2ecf20Sopenharmony_ci	}
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	xs = xfrm_input_state(first->skb);
10858c2ecf20Sopenharmony_ci	if (unlikely(!xs)) {
10868c2ecf20Sopenharmony_ci		netdev_err(tx_ring->netdev, "%s: no xfrm_input_state() xs = %p\n",
10878c2ecf20Sopenharmony_ci			   __func__, xs);
10888c2ecf20Sopenharmony_ci		return 0;
10898c2ecf20Sopenharmony_ci	}
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	itd->sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
10928c2ecf20Sopenharmony_ci	if (unlikely(itd->sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT)) {
10938c2ecf20Sopenharmony_ci		netdev_err(tx_ring->netdev, "%s: bad sa_idx=%d handle=%lu\n",
10948c2ecf20Sopenharmony_ci			   __func__, itd->sa_idx, xs->xso.offload_handle);
10958c2ecf20Sopenharmony_ci		return 0;
10968c2ecf20Sopenharmony_ci	}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci	tsa = &ipsec->tx_tbl[itd->sa_idx];
10998c2ecf20Sopenharmony_ci	if (unlikely(!tsa->used)) {
11008c2ecf20Sopenharmony_ci		netdev_err(tx_ring->netdev, "%s: unused sa_idx=%d\n",
11018c2ecf20Sopenharmony_ci			   __func__, itd->sa_idx);
11028c2ecf20Sopenharmony_ci		return 0;
11038c2ecf20Sopenharmony_ci	}
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	first->tx_flags |= IXGBE_TX_FLAGS_IPSEC | IXGBE_TX_FLAGS_CC;
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	if (xs->id.proto == IPPROTO_ESP) {
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci		itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_TYPE_ESP |
11108c2ecf20Sopenharmony_ci			      IXGBE_ADVTXD_TUCMD_L4T_TCP;
11118c2ecf20Sopenharmony_ci		if (first->protocol == htons(ETH_P_IP))
11128c2ecf20Sopenharmony_ci			itd->flags |= IXGBE_ADVTXD_TUCMD_IPV4;
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci		/* The actual trailer length is authlen (16 bytes) plus
11158c2ecf20Sopenharmony_ci		 * 2 bytes for the proto and the padlen values, plus
11168c2ecf20Sopenharmony_ci		 * padlen bytes of padding.  This ends up not the same
11178c2ecf20Sopenharmony_ci		 * as the static value found in xs->props.trailer_len (21).
11188c2ecf20Sopenharmony_ci		 *
11198c2ecf20Sopenharmony_ci		 * ... but if we're doing GSO, don't bother as the stack
11208c2ecf20Sopenharmony_ci		 * doesn't add a trailer for those.
11218c2ecf20Sopenharmony_ci		 */
11228c2ecf20Sopenharmony_ci		if (!skb_is_gso(first->skb)) {
11238c2ecf20Sopenharmony_ci			/* The "correct" way to get the auth length would be
11248c2ecf20Sopenharmony_ci			 * to use
11258c2ecf20Sopenharmony_ci			 *    authlen = crypto_aead_authsize(xs->data);
11268c2ecf20Sopenharmony_ci			 * but since we know we only have one size to worry
11278c2ecf20Sopenharmony_ci			 * about * we can let the compiler use the constant
11288c2ecf20Sopenharmony_ci			 * and save us a few CPU cycles.
11298c2ecf20Sopenharmony_ci			 */
11308c2ecf20Sopenharmony_ci			const int authlen = IXGBE_IPSEC_AUTH_BITS / 8;
11318c2ecf20Sopenharmony_ci			struct sk_buff *skb = first->skb;
11328c2ecf20Sopenharmony_ci			u8 padlen;
11338c2ecf20Sopenharmony_ci			int ret;
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci			ret = skb_copy_bits(skb, skb->len - (authlen + 2),
11368c2ecf20Sopenharmony_ci					    &padlen, 1);
11378c2ecf20Sopenharmony_ci			if (unlikely(ret))
11388c2ecf20Sopenharmony_ci				return 0;
11398c2ecf20Sopenharmony_ci			itd->trailer_len = authlen + 2 + padlen;
11408c2ecf20Sopenharmony_ci		}
11418c2ecf20Sopenharmony_ci	}
11428c2ecf20Sopenharmony_ci	if (tsa->encrypt)
11438c2ecf20Sopenharmony_ci		itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_ENCRYPT_EN;
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	return 1;
11468c2ecf20Sopenharmony_ci}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci/**
11498c2ecf20Sopenharmony_ci * ixgbe_ipsec_rx - decode ipsec bits from Rx descriptor
11508c2ecf20Sopenharmony_ci * @rx_ring: receiving ring
11518c2ecf20Sopenharmony_ci * @rx_desc: receive data descriptor
11528c2ecf20Sopenharmony_ci * @skb: current data packet
11538c2ecf20Sopenharmony_ci *
11548c2ecf20Sopenharmony_ci * Determine if there was an ipsec encapsulation noticed, and if so set up
11558c2ecf20Sopenharmony_ci * the resulting status for later in the receive stack.
11568c2ecf20Sopenharmony_ci **/
11578c2ecf20Sopenharmony_civoid ixgbe_ipsec_rx(struct ixgbe_ring *rx_ring,
11588c2ecf20Sopenharmony_ci		    union ixgbe_adv_rx_desc *rx_desc,
11598c2ecf20Sopenharmony_ci		    struct sk_buff *skb)
11608c2ecf20Sopenharmony_ci{
11618c2ecf20Sopenharmony_ci	struct ixgbe_adapter *adapter = netdev_priv(rx_ring->netdev);
11628c2ecf20Sopenharmony_ci	__le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
11638c2ecf20Sopenharmony_ci	__le16 ipsec_pkt_types = cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH |
11648c2ecf20Sopenharmony_ci					     IXGBE_RXDADV_PKTTYPE_IPSEC_ESP);
11658c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec = adapter->ipsec;
11668c2ecf20Sopenharmony_ci	struct xfrm_offload *xo = NULL;
11678c2ecf20Sopenharmony_ci	struct xfrm_state *xs = NULL;
11688c2ecf20Sopenharmony_ci	struct ipv6hdr *ip6 = NULL;
11698c2ecf20Sopenharmony_ci	struct iphdr *ip4 = NULL;
11708c2ecf20Sopenharmony_ci	struct sec_path *sp;
11718c2ecf20Sopenharmony_ci	void *daddr;
11728c2ecf20Sopenharmony_ci	__be32 spi;
11738c2ecf20Sopenharmony_ci	u8 *c_hdr;
11748c2ecf20Sopenharmony_ci	u8 proto;
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_ci	/* Find the ip and crypto headers in the data.
11778c2ecf20Sopenharmony_ci	 * We can assume no vlan header in the way, b/c the
11788c2ecf20Sopenharmony_ci	 * hw won't recognize the IPsec packet and anyway the
11798c2ecf20Sopenharmony_ci	 * currently vlan device doesn't support xfrm offload.
11808c2ecf20Sopenharmony_ci	 */
11818c2ecf20Sopenharmony_ci	if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV4)) {
11828c2ecf20Sopenharmony_ci		ip4 = (struct iphdr *)(skb->data + ETH_HLEN);
11838c2ecf20Sopenharmony_ci		daddr = &ip4->daddr;
11848c2ecf20Sopenharmony_ci		c_hdr = (u8 *)ip4 + ip4->ihl * 4;
11858c2ecf20Sopenharmony_ci	} else if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV6)) {
11868c2ecf20Sopenharmony_ci		ip6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
11878c2ecf20Sopenharmony_ci		daddr = &ip6->daddr;
11888c2ecf20Sopenharmony_ci		c_hdr = (u8 *)ip6 + sizeof(struct ipv6hdr);
11898c2ecf20Sopenharmony_ci	} else {
11908c2ecf20Sopenharmony_ci		return;
11918c2ecf20Sopenharmony_ci	}
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	switch (pkt_info & ipsec_pkt_types) {
11948c2ecf20Sopenharmony_ci	case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH):
11958c2ecf20Sopenharmony_ci		spi = ((struct ip_auth_hdr *)c_hdr)->spi;
11968c2ecf20Sopenharmony_ci		proto = IPPROTO_AH;
11978c2ecf20Sopenharmony_ci		break;
11988c2ecf20Sopenharmony_ci	case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_ESP):
11998c2ecf20Sopenharmony_ci		spi = ((struct ip_esp_hdr *)c_hdr)->spi;
12008c2ecf20Sopenharmony_ci		proto = IPPROTO_ESP;
12018c2ecf20Sopenharmony_ci		break;
12028c2ecf20Sopenharmony_ci	default:
12038c2ecf20Sopenharmony_ci		return;
12048c2ecf20Sopenharmony_ci	}
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	xs = ixgbe_ipsec_find_rx_state(ipsec, daddr, proto, spi, !!ip4);
12078c2ecf20Sopenharmony_ci	if (unlikely(!xs))
12088c2ecf20Sopenharmony_ci		return;
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	sp = secpath_set(skb);
12118c2ecf20Sopenharmony_ci	if (unlikely(!sp))
12128c2ecf20Sopenharmony_ci		return;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	sp->xvec[sp->len++] = xs;
12158c2ecf20Sopenharmony_ci	sp->olen++;
12168c2ecf20Sopenharmony_ci	xo = xfrm_offload(skb);
12178c2ecf20Sopenharmony_ci	xo->flags = CRYPTO_DONE;
12188c2ecf20Sopenharmony_ci	xo->status = CRYPTO_SUCCESS;
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	adapter->rx_ipsec++;
12218c2ecf20Sopenharmony_ci}
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci/**
12248c2ecf20Sopenharmony_ci * ixgbe_init_ipsec_offload - initialize security registers for IPSec operation
12258c2ecf20Sopenharmony_ci * @adapter: board private structure
12268c2ecf20Sopenharmony_ci **/
12278c2ecf20Sopenharmony_civoid ixgbe_init_ipsec_offload(struct ixgbe_adapter *adapter)
12288c2ecf20Sopenharmony_ci{
12298c2ecf20Sopenharmony_ci	struct ixgbe_hw *hw = &adapter->hw;
12308c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec;
12318c2ecf20Sopenharmony_ci	u32 t_dis, r_dis;
12328c2ecf20Sopenharmony_ci	size_t size;
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	if (hw->mac.type == ixgbe_mac_82598EB)
12358c2ecf20Sopenharmony_ci		return;
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	/* If there is no support for either Tx or Rx offload
12388c2ecf20Sopenharmony_ci	 * we should not be advertising support for IPsec.
12398c2ecf20Sopenharmony_ci	 */
12408c2ecf20Sopenharmony_ci	t_dis = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
12418c2ecf20Sopenharmony_ci		IXGBE_SECTXSTAT_SECTX_OFF_DIS;
12428c2ecf20Sopenharmony_ci	r_dis = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
12438c2ecf20Sopenharmony_ci		IXGBE_SECRXSTAT_SECRX_OFF_DIS;
12448c2ecf20Sopenharmony_ci	if (t_dis || r_dis)
12458c2ecf20Sopenharmony_ci		return;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
12488c2ecf20Sopenharmony_ci	if (!ipsec)
12498c2ecf20Sopenharmony_ci		goto err1;
12508c2ecf20Sopenharmony_ci	hash_init(ipsec->rx_sa_list);
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	size = sizeof(struct rx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
12538c2ecf20Sopenharmony_ci	ipsec->rx_tbl = kzalloc(size, GFP_KERNEL);
12548c2ecf20Sopenharmony_ci	if (!ipsec->rx_tbl)
12558c2ecf20Sopenharmony_ci		goto err2;
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	size = sizeof(struct tx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
12588c2ecf20Sopenharmony_ci	ipsec->tx_tbl = kzalloc(size, GFP_KERNEL);
12598c2ecf20Sopenharmony_ci	if (!ipsec->tx_tbl)
12608c2ecf20Sopenharmony_ci		goto err2;
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	size = sizeof(struct rx_ip_sa) * IXGBE_IPSEC_MAX_RX_IP_COUNT;
12638c2ecf20Sopenharmony_ci	ipsec->ip_tbl = kzalloc(size, GFP_KERNEL);
12648c2ecf20Sopenharmony_ci	if (!ipsec->ip_tbl)
12658c2ecf20Sopenharmony_ci		goto err2;
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	ipsec->num_rx_sa = 0;
12688c2ecf20Sopenharmony_ci	ipsec->num_tx_sa = 0;
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	adapter->ipsec = ipsec;
12718c2ecf20Sopenharmony_ci	ixgbe_ipsec_stop_engine(adapter);
12728c2ecf20Sopenharmony_ci	ixgbe_ipsec_clear_hw_tables(adapter);
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	adapter->netdev->xfrmdev_ops = &ixgbe_xfrmdev_ops;
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	return;
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_cierr2:
12798c2ecf20Sopenharmony_ci	kfree(ipsec->ip_tbl);
12808c2ecf20Sopenharmony_ci	kfree(ipsec->rx_tbl);
12818c2ecf20Sopenharmony_ci	kfree(ipsec->tx_tbl);
12828c2ecf20Sopenharmony_ci	kfree(ipsec);
12838c2ecf20Sopenharmony_cierr1:
12848c2ecf20Sopenharmony_ci	netdev_err(adapter->netdev, "Unable to allocate memory for SA tables");
12858c2ecf20Sopenharmony_ci}
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci/**
12888c2ecf20Sopenharmony_ci * ixgbe_stop_ipsec_offload - tear down the ipsec offload
12898c2ecf20Sopenharmony_ci * @adapter: board private structure
12908c2ecf20Sopenharmony_ci **/
12918c2ecf20Sopenharmony_civoid ixgbe_stop_ipsec_offload(struct ixgbe_adapter *adapter)
12928c2ecf20Sopenharmony_ci{
12938c2ecf20Sopenharmony_ci	struct ixgbe_ipsec *ipsec = adapter->ipsec;
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	adapter->ipsec = NULL;
12968c2ecf20Sopenharmony_ci	if (ipsec) {
12978c2ecf20Sopenharmony_ci		kfree(ipsec->ip_tbl);
12988c2ecf20Sopenharmony_ci		kfree(ipsec->rx_tbl);
12998c2ecf20Sopenharmony_ci		kfree(ipsec->tx_tbl);
13008c2ecf20Sopenharmony_ci		kfree(ipsec);
13018c2ecf20Sopenharmony_ci	}
13028c2ecf20Sopenharmony_ci}
1303