151dce24bSJeff Kirsher // SPDX-License-Identifier: GPL-2.0
251dce24bSJeff Kirsher /* Copyright(c) 2017 Oracle and/or its affiliates. All rights reserved. */
38bbbc5e9SShannon Nelson
48bbbc5e9SShannon Nelson #include "ixgbe.h"
563a67fe2SShannon Nelson #include <net/xfrm.h>
663a67fe2SShannon Nelson #include <crypto/aead.h>
77fa57ca4SShannon Nelson #include <linux/if_bridge.h>
88bbbc5e9SShannon Nelson
999a7b0c1SShannon Nelson #define IXGBE_IPSEC_KEY_BITS 160
1099a7b0c1SShannon Nelson static const char aes_gcm_name[] = "rfc4106(gcm(aes))";
1199a7b0c1SShannon Nelson
12eda0333aSShannon Nelson static void ixgbe_ipsec_del_sa(struct xfrm_state *xs);
13eda0333aSShannon Nelson
148bbbc5e9SShannon Nelson /**
158bbbc5e9SShannon Nelson * ixgbe_ipsec_set_tx_sa - set the Tx SA registers
168bbbc5e9SShannon Nelson * @hw: hw specific details
178bbbc5e9SShannon Nelson * @idx: register index to write
188bbbc5e9SShannon Nelson * @key: key byte array
198bbbc5e9SShannon Nelson * @salt: salt bytes
208bbbc5e9SShannon Nelson **/
ixgbe_ipsec_set_tx_sa(struct ixgbe_hw * hw,u16 idx,u32 key[],u32 salt)218bbbc5e9SShannon Nelson static void ixgbe_ipsec_set_tx_sa(struct ixgbe_hw *hw, u16 idx,
228bbbc5e9SShannon Nelson u32 key[], u32 salt)
238bbbc5e9SShannon Nelson {
248bbbc5e9SShannon Nelson u32 reg;
258bbbc5e9SShannon Nelson int i;
268bbbc5e9SShannon Nelson
278bbbc5e9SShannon Nelson for (i = 0; i < 4; i++)
289cfbfa70SCathy Zhou IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(i),
299cfbfa70SCathy Zhou (__force u32)cpu_to_be32(key[3 - i]));
309cfbfa70SCathy Zhou IXGBE_WRITE_REG(hw, IXGBE_IPSTXSALT, (__force u32)cpu_to_be32(salt));
318bbbc5e9SShannon Nelson IXGBE_WRITE_FLUSH(hw);
328bbbc5e9SShannon Nelson
338bbbc5e9SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_IPSTXIDX);
348bbbc5e9SShannon Nelson reg &= IXGBE_RXTXIDX_IPS_EN;
358bbbc5e9SShannon Nelson reg |= idx << IXGBE_RXTXIDX_IDX_SHIFT | IXGBE_RXTXIDX_WRITE;
368bbbc5e9SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, reg);
378bbbc5e9SShannon Nelson IXGBE_WRITE_FLUSH(hw);
388bbbc5e9SShannon Nelson }
398bbbc5e9SShannon Nelson
408bbbc5e9SShannon Nelson /**
418bbbc5e9SShannon Nelson * ixgbe_ipsec_set_rx_item - set an Rx table item
428bbbc5e9SShannon Nelson * @hw: hw specific details
438bbbc5e9SShannon Nelson * @idx: register index to write
448bbbc5e9SShannon Nelson * @tbl: table selector
458bbbc5e9SShannon Nelson *
468bbbc5e9SShannon Nelson * Trigger the device to store into a particular Rx table the
478bbbc5e9SShannon Nelson * data that has already been loaded into the input register
488bbbc5e9SShannon Nelson **/
ixgbe_ipsec_set_rx_item(struct ixgbe_hw * hw,u16 idx,enum ixgbe_ipsec_tbl_sel tbl)498bbbc5e9SShannon Nelson static void ixgbe_ipsec_set_rx_item(struct ixgbe_hw *hw, u16 idx,
508bbbc5e9SShannon Nelson enum ixgbe_ipsec_tbl_sel tbl)
518bbbc5e9SShannon Nelson {
528bbbc5e9SShannon Nelson u32 reg;
538bbbc5e9SShannon Nelson
548bbbc5e9SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_IPSRXIDX);
558bbbc5e9SShannon Nelson reg &= IXGBE_RXTXIDX_IPS_EN;
568bbbc5e9SShannon Nelson reg |= tbl << IXGBE_RXIDX_TBL_SHIFT |
578bbbc5e9SShannon Nelson idx << IXGBE_RXTXIDX_IDX_SHIFT |
588bbbc5e9SShannon Nelson IXGBE_RXTXIDX_WRITE;
598bbbc5e9SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, reg);
608bbbc5e9SShannon Nelson IXGBE_WRITE_FLUSH(hw);
618bbbc5e9SShannon Nelson }
628bbbc5e9SShannon Nelson
638bbbc5e9SShannon Nelson /**
648bbbc5e9SShannon Nelson * ixgbe_ipsec_set_rx_sa - set up the register bits to save SA info
658bbbc5e9SShannon Nelson * @hw: hw specific details
668bbbc5e9SShannon Nelson * @idx: register index to write
678bbbc5e9SShannon Nelson * @spi: security parameter index
688bbbc5e9SShannon Nelson * @key: key byte array
698bbbc5e9SShannon Nelson * @salt: salt bytes
708bbbc5e9SShannon Nelson * @mode: rx decrypt control bits
718bbbc5e9SShannon Nelson * @ip_idx: index into IP table for related IP address
728bbbc5e9SShannon Nelson **/
ixgbe_ipsec_set_rx_sa(struct ixgbe_hw * hw,u16 idx,__be32 spi,u32 key[],u32 salt,u32 mode,u32 ip_idx)738bbbc5e9SShannon Nelson static void ixgbe_ipsec_set_rx_sa(struct ixgbe_hw *hw, u16 idx, __be32 spi,
748bbbc5e9SShannon Nelson u32 key[], u32 salt, u32 mode, u32 ip_idx)
758bbbc5e9SShannon Nelson {
768bbbc5e9SShannon Nelson int i;
778bbbc5e9SShannon Nelson
788bbbc5e9SShannon Nelson /* store the SPI (in bigendian) and IPidx */
799cfbfa70SCathy Zhou IXGBE_WRITE_REG(hw, IXGBE_IPSRXSPI,
809cfbfa70SCathy Zhou (__force u32)cpu_to_le32((__force u32)spi));
818bbbc5e9SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPIDX, ip_idx);
828bbbc5e9SShannon Nelson IXGBE_WRITE_FLUSH(hw);
838bbbc5e9SShannon Nelson
848bbbc5e9SShannon Nelson ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_spi_tbl);
858bbbc5e9SShannon Nelson
868bbbc5e9SShannon Nelson /* store the key, salt, and mode */
878bbbc5e9SShannon Nelson for (i = 0; i < 4; i++)
889cfbfa70SCathy Zhou IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(i),
899cfbfa70SCathy Zhou (__force u32)cpu_to_be32(key[3 - i]));
909cfbfa70SCathy Zhou IXGBE_WRITE_REG(hw, IXGBE_IPSRXSALT, (__force u32)cpu_to_be32(salt));
918bbbc5e9SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSRXMOD, mode);
928bbbc5e9SShannon Nelson IXGBE_WRITE_FLUSH(hw);
938bbbc5e9SShannon Nelson
948bbbc5e9SShannon Nelson ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_key_tbl);
958bbbc5e9SShannon Nelson }
968bbbc5e9SShannon Nelson
978bbbc5e9SShannon Nelson /**
988bbbc5e9SShannon Nelson * ixgbe_ipsec_set_rx_ip - set up the register bits to save SA IP addr info
998bbbc5e9SShannon Nelson * @hw: hw specific details
1008bbbc5e9SShannon Nelson * @idx: register index to write
1018bbbc5e9SShannon Nelson * @addr: IP address byte array
1028bbbc5e9SShannon Nelson **/
ixgbe_ipsec_set_rx_ip(struct ixgbe_hw * hw,u16 idx,__be32 addr[])1038bbbc5e9SShannon Nelson static void ixgbe_ipsec_set_rx_ip(struct ixgbe_hw *hw, u16 idx, __be32 addr[])
1048bbbc5e9SShannon Nelson {
1058bbbc5e9SShannon Nelson int i;
1068bbbc5e9SShannon Nelson
1078bbbc5e9SShannon Nelson /* store the ip address */
1088bbbc5e9SShannon Nelson for (i = 0; i < 4; i++)
1099cfbfa70SCathy Zhou IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(i),
1109cfbfa70SCathy Zhou (__force u32)cpu_to_le32((__force u32)addr[i]));
1118bbbc5e9SShannon Nelson IXGBE_WRITE_FLUSH(hw);
1128bbbc5e9SShannon Nelson
1138bbbc5e9SShannon Nelson ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_ip_tbl);
1148bbbc5e9SShannon Nelson }
1158bbbc5e9SShannon Nelson
1168bbbc5e9SShannon Nelson /**
1178bbbc5e9SShannon Nelson * ixgbe_ipsec_clear_hw_tables - because some tables don't get cleared on reset
1188bbbc5e9SShannon Nelson * @adapter: board private structure
1198bbbc5e9SShannon Nelson **/
ixgbe_ipsec_clear_hw_tables(struct ixgbe_adapter * adapter)1208bbbc5e9SShannon Nelson static void ixgbe_ipsec_clear_hw_tables(struct ixgbe_adapter *adapter)
1218bbbc5e9SShannon Nelson {
1228bbbc5e9SShannon Nelson struct ixgbe_hw *hw = &adapter->hw;
1238bbbc5e9SShannon Nelson u32 buf[4] = {0, 0, 0, 0};
1248bbbc5e9SShannon Nelson u16 idx;
1258bbbc5e9SShannon Nelson
1268bbbc5e9SShannon Nelson /* disable Rx and Tx SA lookup */
1278bbbc5e9SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
1288bbbc5e9SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
1298bbbc5e9SShannon Nelson
1308bbbc5e9SShannon Nelson /* scrub the tables - split the loops for the max of the IP table */
1318bbbc5e9SShannon Nelson for (idx = 0; idx < IXGBE_IPSEC_MAX_RX_IP_COUNT; idx++) {
1328bbbc5e9SShannon Nelson ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
1338bbbc5e9SShannon Nelson ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
1348bbbc5e9SShannon Nelson ixgbe_ipsec_set_rx_ip(hw, idx, (__be32 *)buf);
1358bbbc5e9SShannon Nelson }
1368bbbc5e9SShannon Nelson for (; idx < IXGBE_IPSEC_MAX_SA_COUNT; idx++) {
1378bbbc5e9SShannon Nelson ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
1388bbbc5e9SShannon Nelson ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
1398bbbc5e9SShannon Nelson }
1408bbbc5e9SShannon Nelson }
1418bbbc5e9SShannon Nelson
1428bbbc5e9SShannon Nelson /**
14349a94d74SShannon Nelson * ixgbe_ipsec_stop_data
14449a94d74SShannon Nelson * @adapter: board private structure
14549a94d74SShannon Nelson **/
ixgbe_ipsec_stop_data(struct ixgbe_adapter * adapter)14649a94d74SShannon Nelson static void ixgbe_ipsec_stop_data(struct ixgbe_adapter *adapter)
14749a94d74SShannon Nelson {
14849a94d74SShannon Nelson struct ixgbe_hw *hw = &adapter->hw;
14949a94d74SShannon Nelson bool link = adapter->link_up;
15049a94d74SShannon Nelson u32 t_rdy, r_rdy;
15149a94d74SShannon Nelson u32 limit;
15249a94d74SShannon Nelson u32 reg;
15349a94d74SShannon Nelson
15449a94d74SShannon Nelson /* halt data paths */
15549a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
15649a94d74SShannon Nelson reg |= IXGBE_SECTXCTRL_TX_DIS;
15749a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
15849a94d74SShannon Nelson
15949a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
16049a94d74SShannon Nelson reg |= IXGBE_SECRXCTRL_RX_DIS;
16149a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
16249a94d74SShannon Nelson
163e9f655eeSAlexander Duyck /* If both Tx and Rx are ready there are no packets
164e9f655eeSAlexander Duyck * that we need to flush so the loopback configuration
165e9f655eeSAlexander Duyck * below is not necessary.
166e9f655eeSAlexander Duyck */
167e9f655eeSAlexander Duyck t_rdy = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
168e9f655eeSAlexander Duyck IXGBE_SECTXSTAT_SECTX_RDY;
169e9f655eeSAlexander Duyck r_rdy = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
170e9f655eeSAlexander Duyck IXGBE_SECRXSTAT_SECRX_RDY;
171e9f655eeSAlexander Duyck if (t_rdy && r_rdy)
172e9f655eeSAlexander Duyck return;
17349a94d74SShannon Nelson
17449a94d74SShannon Nelson /* If the tx fifo doesn't have link, but still has data,
17549a94d74SShannon Nelson * we can't clear the tx sec block. Set the MAC loopback
17649a94d74SShannon Nelson * before block clear
17749a94d74SShannon Nelson */
17849a94d74SShannon Nelson if (!link) {
17949a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_MACC);
18049a94d74SShannon Nelson reg |= IXGBE_MACC_FLU;
18149a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
18249a94d74SShannon Nelson
18349a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
18449a94d74SShannon Nelson reg |= IXGBE_HLREG0_LPBK;
18549a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
18649a94d74SShannon Nelson
18749a94d74SShannon Nelson IXGBE_WRITE_FLUSH(hw);
18849a94d74SShannon Nelson mdelay(3);
18949a94d74SShannon Nelson }
19049a94d74SShannon Nelson
19149a94d74SShannon Nelson /* wait for the paths to empty */
19249a94d74SShannon Nelson limit = 20;
19349a94d74SShannon Nelson do {
19449a94d74SShannon Nelson mdelay(10);
19549a94d74SShannon Nelson t_rdy = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
19649a94d74SShannon Nelson IXGBE_SECTXSTAT_SECTX_RDY;
19749a94d74SShannon Nelson r_rdy = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
19849a94d74SShannon Nelson IXGBE_SECRXSTAT_SECRX_RDY;
199e9f655eeSAlexander Duyck } while (!(t_rdy && r_rdy) && limit--);
20049a94d74SShannon Nelson
20149a94d74SShannon Nelson /* undo loopback if we played with it earlier */
20249a94d74SShannon Nelson if (!link) {
20349a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_MACC);
20449a94d74SShannon Nelson reg &= ~IXGBE_MACC_FLU;
20549a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
20649a94d74SShannon Nelson
20749a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
20849a94d74SShannon Nelson reg &= ~IXGBE_HLREG0_LPBK;
20949a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
21049a94d74SShannon Nelson
21149a94d74SShannon Nelson IXGBE_WRITE_FLUSH(hw);
21249a94d74SShannon Nelson }
21349a94d74SShannon Nelson }
21449a94d74SShannon Nelson
21549a94d74SShannon Nelson /**
21649a94d74SShannon Nelson * ixgbe_ipsec_stop_engine
21749a94d74SShannon Nelson * @adapter: board private structure
21849a94d74SShannon Nelson **/
ixgbe_ipsec_stop_engine(struct ixgbe_adapter * adapter)21949a94d74SShannon Nelson static void ixgbe_ipsec_stop_engine(struct ixgbe_adapter *adapter)
22049a94d74SShannon Nelson {
22149a94d74SShannon Nelson struct ixgbe_hw *hw = &adapter->hw;
22249a94d74SShannon Nelson u32 reg;
22349a94d74SShannon Nelson
22449a94d74SShannon Nelson ixgbe_ipsec_stop_data(adapter);
22549a94d74SShannon Nelson
22649a94d74SShannon Nelson /* disable Rx and Tx SA lookup */
22749a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
22849a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
22949a94d74SShannon Nelson
23049a94d74SShannon Nelson /* disable the Rx and Tx engines and full packet store-n-forward */
23149a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
23249a94d74SShannon Nelson reg |= IXGBE_SECTXCTRL_SECTX_DIS;
23349a94d74SShannon Nelson reg &= ~IXGBE_SECTXCTRL_STORE_FORWARD;
23449a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
23549a94d74SShannon Nelson
23649a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
23749a94d74SShannon Nelson reg |= IXGBE_SECRXCTRL_SECRX_DIS;
23849a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
23949a94d74SShannon Nelson
24049a94d74SShannon Nelson /* restore the "tx security buffer almost full threshold" to 0x250 */
24149a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, 0x250);
24249a94d74SShannon Nelson
24349a94d74SShannon Nelson /* Set minimum IFG between packets back to the default 0x1 */
24449a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
24549a94d74SShannon Nelson reg = (reg & 0xfffffff0) | 0x1;
24649a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
24749a94d74SShannon Nelson
24849a94d74SShannon Nelson /* final set for normal (no ipsec offload) processing */
24949a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_SECTX_DIS);
25049a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, IXGBE_SECRXCTRL_SECRX_DIS);
25149a94d74SShannon Nelson
25249a94d74SShannon Nelson IXGBE_WRITE_FLUSH(hw);
25349a94d74SShannon Nelson }
25449a94d74SShannon Nelson
25549a94d74SShannon Nelson /**
25649a94d74SShannon Nelson * ixgbe_ipsec_start_engine
25749a94d74SShannon Nelson * @adapter: board private structure
25849a94d74SShannon Nelson *
25949a94d74SShannon Nelson * NOTE: this increases power consumption whether being used or not
26049a94d74SShannon Nelson **/
ixgbe_ipsec_start_engine(struct ixgbe_adapter * adapter)26149a94d74SShannon Nelson static void ixgbe_ipsec_start_engine(struct ixgbe_adapter *adapter)
26249a94d74SShannon Nelson {
26349a94d74SShannon Nelson struct ixgbe_hw *hw = &adapter->hw;
26449a94d74SShannon Nelson u32 reg;
26549a94d74SShannon Nelson
26649a94d74SShannon Nelson ixgbe_ipsec_stop_data(adapter);
26749a94d74SShannon Nelson
26849a94d74SShannon Nelson /* Set minimum IFG between packets to 3 */
26949a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
27049a94d74SShannon Nelson reg = (reg & 0xfffffff0) | 0x3;
27149a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
27249a94d74SShannon Nelson
27349a94d74SShannon Nelson /* Set "tx security buffer almost full threshold" to 0x15 so that the
27449a94d74SShannon Nelson * almost full indication is generated only after buffer contains at
27549a94d74SShannon Nelson * least an entire jumbo packet.
27649a94d74SShannon Nelson */
27749a94d74SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_SECTXBUFFAF);
27849a94d74SShannon Nelson reg = (reg & 0xfffffc00) | 0x15;
27949a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, reg);
28049a94d74SShannon Nelson
28149a94d74SShannon Nelson /* restart the data paths by clearing the DISABLE bits */
28249a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, 0);
28349a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_STORE_FORWARD);
28449a94d74SShannon Nelson
28549a94d74SShannon Nelson /* enable Rx and Tx SA lookup */
28649a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, IXGBE_RXTXIDX_IPS_EN);
28749a94d74SShannon Nelson IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, IXGBE_RXTXIDX_IPS_EN);
28849a94d74SShannon Nelson
28949a94d74SShannon Nelson IXGBE_WRITE_FLUSH(hw);
29049a94d74SShannon Nelson }
29149a94d74SShannon Nelson
29249a94d74SShannon Nelson /**
2936d73a154SShannon Nelson * ixgbe_ipsec_restore - restore the ipsec HW settings after a reset
2946d73a154SShannon Nelson * @adapter: board private structure
295eda0333aSShannon Nelson *
296eda0333aSShannon Nelson * Reload the HW tables from the SW tables after they've been bashed
297eda0333aSShannon Nelson * by a chip reset.
298eda0333aSShannon Nelson *
299eda0333aSShannon Nelson * Any VF entries are removed from the SW and HW tables since either
300eda0333aSShannon Nelson * (a) the VF also gets reset on PF reset and will ask again for the
301eda0333aSShannon Nelson * offloads, or (b) the VF has been removed by a change in the num_vfs.
3026d73a154SShannon Nelson **/
ixgbe_ipsec_restore(struct ixgbe_adapter * adapter)3036d73a154SShannon Nelson void ixgbe_ipsec_restore(struct ixgbe_adapter *adapter)
3046d73a154SShannon Nelson {
3056d73a154SShannon Nelson struct ixgbe_ipsec *ipsec = adapter->ipsec;
3066d73a154SShannon Nelson struct ixgbe_hw *hw = &adapter->hw;
3076d73a154SShannon Nelson int i;
3086d73a154SShannon Nelson
3096d73a154SShannon Nelson if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED))
3106d73a154SShannon Nelson return;
3116d73a154SShannon Nelson
3126d73a154SShannon Nelson /* clean up and restart the engine */
3136d73a154SShannon Nelson ixgbe_ipsec_stop_engine(adapter);
3146d73a154SShannon Nelson ixgbe_ipsec_clear_hw_tables(adapter);
3156d73a154SShannon Nelson ixgbe_ipsec_start_engine(adapter);
3166d73a154SShannon Nelson
3176d73a154SShannon Nelson /* reload the Rx and Tx keys */
3186d73a154SShannon Nelson for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
319eda0333aSShannon Nelson struct rx_sa *r = &ipsec->rx_tbl[i];
320eda0333aSShannon Nelson struct tx_sa *t = &ipsec->tx_tbl[i];
3216d73a154SShannon Nelson
322eda0333aSShannon Nelson if (r->used) {
323eda0333aSShannon Nelson if (r->mode & IXGBE_RXTXMOD_VF)
324eda0333aSShannon Nelson ixgbe_ipsec_del_sa(r->xs);
325eda0333aSShannon Nelson else
326eda0333aSShannon Nelson ixgbe_ipsec_set_rx_sa(hw, i, r->xs->id.spi,
327eda0333aSShannon Nelson r->key, r->salt,
328eda0333aSShannon Nelson r->mode, r->iptbl_ind);
329eda0333aSShannon Nelson }
3306d73a154SShannon Nelson
331eda0333aSShannon Nelson if (t->used) {
332eda0333aSShannon Nelson if (t->mode & IXGBE_RXTXMOD_VF)
333eda0333aSShannon Nelson ixgbe_ipsec_del_sa(t->xs);
334eda0333aSShannon Nelson else
335eda0333aSShannon Nelson ixgbe_ipsec_set_tx_sa(hw, i, t->key, t->salt);
336eda0333aSShannon Nelson }
3376d73a154SShannon Nelson }
338b2875fbfSShannon Nelson
339b2875fbfSShannon Nelson /* reload the IP addrs */
340b2875fbfSShannon Nelson for (i = 0; i < IXGBE_IPSEC_MAX_RX_IP_COUNT; i++) {
341b2875fbfSShannon Nelson struct rx_ip_sa *ipsa = &ipsec->ip_tbl[i];
342b2875fbfSShannon Nelson
343b2875fbfSShannon Nelson if (ipsa->used)
344b2875fbfSShannon Nelson ixgbe_ipsec_set_rx_ip(hw, i, ipsa->ipaddr);
345b2875fbfSShannon Nelson }
3466d73a154SShannon Nelson }
3476d73a154SShannon Nelson
3486d73a154SShannon Nelson /**
34963a67fe2SShannon Nelson * ixgbe_ipsec_find_empty_idx - find the first unused security parameter index
35063a67fe2SShannon Nelson * @ipsec: pointer to ipsec struct
35163a67fe2SShannon Nelson * @rxtable: true if we need to look in the Rx table
35263a67fe2SShannon Nelson *
35363a67fe2SShannon Nelson * Returns the first unused index in either the Rx or Tx SA table
35463a67fe2SShannon Nelson **/
ixgbe_ipsec_find_empty_idx(struct ixgbe_ipsec * ipsec,bool rxtable)35563a67fe2SShannon Nelson static int ixgbe_ipsec_find_empty_idx(struct ixgbe_ipsec *ipsec, bool rxtable)
35663a67fe2SShannon Nelson {
35763a67fe2SShannon Nelson u32 i;
35863a67fe2SShannon Nelson
35963a67fe2SShannon Nelson if (rxtable) {
36063a67fe2SShannon Nelson if (ipsec->num_rx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
36163a67fe2SShannon Nelson return -ENOSPC;
36263a67fe2SShannon Nelson
36363a67fe2SShannon Nelson /* search rx sa table */
36463a67fe2SShannon Nelson for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
36563a67fe2SShannon Nelson if (!ipsec->rx_tbl[i].used)
36663a67fe2SShannon Nelson return i;
36763a67fe2SShannon Nelson }
36863a67fe2SShannon Nelson } else {
36963a67fe2SShannon Nelson if (ipsec->num_tx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
37063a67fe2SShannon Nelson return -ENOSPC;
37163a67fe2SShannon Nelson
37263a67fe2SShannon Nelson /* search tx sa table */
37363a67fe2SShannon Nelson for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
37463a67fe2SShannon Nelson if (!ipsec->tx_tbl[i].used)
37563a67fe2SShannon Nelson return i;
37663a67fe2SShannon Nelson }
37763a67fe2SShannon Nelson }
37863a67fe2SShannon Nelson
37963a67fe2SShannon Nelson return -ENOSPC;
38063a67fe2SShannon Nelson }
38163a67fe2SShannon Nelson
38263a67fe2SShannon Nelson /**
38392103199SShannon Nelson * ixgbe_ipsec_find_rx_state - find the state that matches
38492103199SShannon Nelson * @ipsec: pointer to ipsec struct
38592103199SShannon Nelson * @daddr: inbound address to match
38692103199SShannon Nelson * @proto: protocol to match
38792103199SShannon Nelson * @spi: SPI to match
38892103199SShannon Nelson * @ip4: true if using an ipv4 address
38992103199SShannon Nelson *
39092103199SShannon Nelson * Returns a pointer to the matching SA state information
39192103199SShannon Nelson **/
ixgbe_ipsec_find_rx_state(struct ixgbe_ipsec * ipsec,__be32 * daddr,u8 proto,__be32 spi,bool ip4)39292103199SShannon Nelson static struct xfrm_state *ixgbe_ipsec_find_rx_state(struct ixgbe_ipsec *ipsec,
39392103199SShannon Nelson __be32 *daddr, u8 proto,
39492103199SShannon Nelson __be32 spi, bool ip4)
39592103199SShannon Nelson {
39692103199SShannon Nelson struct rx_sa *rsa;
39792103199SShannon Nelson struct xfrm_state *ret = NULL;
39892103199SShannon Nelson
39992103199SShannon Nelson rcu_read_lock();
4009cfbfa70SCathy Zhou hash_for_each_possible_rcu(ipsec->rx_sa_list, rsa, hlist,
4019cfbfa70SCathy Zhou (__force u32)spi) {
402eda0333aSShannon Nelson if (rsa->mode & IXGBE_RXTXMOD_VF)
403eda0333aSShannon Nelson continue;
40492103199SShannon Nelson if (spi == rsa->xs->id.spi &&
40592103199SShannon Nelson ((ip4 && *daddr == rsa->xs->id.daddr.a4) ||
40692103199SShannon Nelson (!ip4 && !memcmp(daddr, &rsa->xs->id.daddr.a6,
40792103199SShannon Nelson sizeof(rsa->xs->id.daddr.a6)))) &&
40892103199SShannon Nelson proto == rsa->xs->id.proto) {
40992103199SShannon Nelson ret = rsa->xs;
41092103199SShannon Nelson xfrm_state_hold(ret);
41192103199SShannon Nelson break;
41292103199SShannon Nelson }
4139cfbfa70SCathy Zhou }
41492103199SShannon Nelson rcu_read_unlock();
41592103199SShannon Nelson return ret;
41692103199SShannon Nelson }
41792103199SShannon Nelson
41892103199SShannon Nelson /**
41963a67fe2SShannon Nelson * ixgbe_ipsec_parse_proto_keys - find the key and salt based on the protocol
42063a67fe2SShannon Nelson * @xs: pointer to xfrm_state struct
42163a67fe2SShannon Nelson * @mykey: pointer to key array to populate
42263a67fe2SShannon Nelson * @mysalt: pointer to salt value to populate
42363a67fe2SShannon Nelson *
42463a67fe2SShannon Nelson * This copies the protocol keys and salt to our own data tables. The
42563a67fe2SShannon Nelson * 82599 family only supports the one algorithm.
42663a67fe2SShannon Nelson **/
ixgbe_ipsec_parse_proto_keys(struct xfrm_state * xs,u32 * mykey,u32 * mysalt)42763a67fe2SShannon Nelson static int ixgbe_ipsec_parse_proto_keys(struct xfrm_state *xs,
42863a67fe2SShannon Nelson u32 *mykey, u32 *mysalt)
42963a67fe2SShannon Nelson {
430bdfd2d1fSJarod Wilson struct net_device *dev = xs->xso.real_dev;
43163a67fe2SShannon Nelson unsigned char *key_data;
43263a67fe2SShannon Nelson char *alg_name = NULL;
43363a67fe2SShannon Nelson int key_len;
43463a67fe2SShannon Nelson
43568c1fb2dSShannon Nelson if (!xs->aead) {
43663a67fe2SShannon Nelson netdev_err(dev, "Unsupported IPsec algorithm\n");
43763a67fe2SShannon Nelson return -EINVAL;
43863a67fe2SShannon Nelson }
43963a67fe2SShannon Nelson
44068c1fb2dSShannon Nelson if (xs->aead->alg_icv_len != IXGBE_IPSEC_AUTH_BITS) {
44168c1fb2dSShannon Nelson netdev_err(dev, "IPsec offload requires %d bit authentication\n",
44268c1fb2dSShannon Nelson IXGBE_IPSEC_AUTH_BITS);
44368c1fb2dSShannon Nelson return -EINVAL;
44468c1fb2dSShannon Nelson }
44568c1fb2dSShannon Nelson
44668c1fb2dSShannon Nelson key_data = &xs->aead->alg_key[0];
44768c1fb2dSShannon Nelson key_len = xs->aead->alg_key_len;
44868c1fb2dSShannon Nelson alg_name = xs->aead->alg_name;
44968c1fb2dSShannon Nelson
45063a67fe2SShannon Nelson if (strcmp(alg_name, aes_gcm_name)) {
45163a67fe2SShannon Nelson netdev_err(dev, "Unsupported IPsec algorithm - please use %s\n",
45263a67fe2SShannon Nelson aes_gcm_name);
45363a67fe2SShannon Nelson return -EINVAL;
45463a67fe2SShannon Nelson }
45563a67fe2SShannon Nelson
45663a67fe2SShannon Nelson /* The key bytes come down in a bigendian array of bytes, so
45763a67fe2SShannon Nelson * we don't need to do any byteswapping.
45863a67fe2SShannon Nelson * 160 accounts for 16 byte key and 4 byte salt
45963a67fe2SShannon Nelson */
46099a7b0c1SShannon Nelson if (key_len == IXGBE_IPSEC_KEY_BITS) {
46163a67fe2SShannon Nelson *mysalt = ((u32 *)key_data)[4];
46299a7b0c1SShannon Nelson } else if (key_len != (IXGBE_IPSEC_KEY_BITS - (sizeof(*mysalt) * 8))) {
46363a67fe2SShannon Nelson netdev_err(dev, "IPsec hw offload only supports keys up to 128 bits with a 32 bit salt\n");
46463a67fe2SShannon Nelson return -EINVAL;
46563a67fe2SShannon Nelson } else {
46663a67fe2SShannon Nelson netdev_info(dev, "IPsec hw offload parameters missing 32 bit salt value\n");
46763a67fe2SShannon Nelson *mysalt = 0;
46863a67fe2SShannon Nelson }
46963a67fe2SShannon Nelson memcpy(mykey, key_data, 16);
47063a67fe2SShannon Nelson
47163a67fe2SShannon Nelson return 0;
47263a67fe2SShannon Nelson }
47363a67fe2SShannon Nelson
47463a67fe2SShannon Nelson /**
4752a8a1552SShannon Nelson * ixgbe_ipsec_check_mgmt_ip - make sure there is no clash with mgmt IP filters
4762a8a1552SShannon Nelson * @xs: pointer to transformer state struct
4772a8a1552SShannon Nelson **/
ixgbe_ipsec_check_mgmt_ip(struct xfrm_state * xs)4782a8a1552SShannon Nelson static int ixgbe_ipsec_check_mgmt_ip(struct xfrm_state *xs)
4792a8a1552SShannon Nelson {
480bdfd2d1fSJarod Wilson struct net_device *dev = xs->xso.real_dev;
481bdfd2d1fSJarod Wilson struct ixgbe_adapter *adapter = netdev_priv(dev);
482bdfd2d1fSJarod Wilson struct ixgbe_hw *hw = &adapter->hw;
4832a8a1552SShannon Nelson u32 mfval, manc, reg;
4842a8a1552SShannon Nelson int num_filters = 4;
4852a8a1552SShannon Nelson bool manc_ipv4;
4862a8a1552SShannon Nelson u32 bmcipval;
4872a8a1552SShannon Nelson int i, j;
4882a8a1552SShannon Nelson
4892a8a1552SShannon Nelson #define MANC_EN_IPV4_FILTER BIT(24)
4902a8a1552SShannon Nelson #define MFVAL_IPV4_FILTER_SHIFT 16
4912a8a1552SShannon Nelson #define MFVAL_IPV6_FILTER_SHIFT 24
4922a8a1552SShannon Nelson #define MIPAF_ARR(_m, _n) (IXGBE_MIPAF + ((_m) * 0x10) + ((_n) * 4))
4932a8a1552SShannon Nelson
4942a8a1552SShannon Nelson #define IXGBE_BMCIP(_n) (0x5050 + ((_n) * 4))
4952a8a1552SShannon Nelson #define IXGBE_BMCIPVAL 0x5060
4962a8a1552SShannon Nelson #define BMCIP_V4 0x2
4972a8a1552SShannon Nelson #define BMCIP_V6 0x3
4982a8a1552SShannon Nelson #define BMCIP_MASK 0x3
4992a8a1552SShannon Nelson
5002a8a1552SShannon Nelson manc = IXGBE_READ_REG(hw, IXGBE_MANC);
5012a8a1552SShannon Nelson manc_ipv4 = !!(manc & MANC_EN_IPV4_FILTER);
5022a8a1552SShannon Nelson mfval = IXGBE_READ_REG(hw, IXGBE_MFVAL);
5032a8a1552SShannon Nelson bmcipval = IXGBE_READ_REG(hw, IXGBE_BMCIPVAL);
5042a8a1552SShannon Nelson
5052a8a1552SShannon Nelson if (xs->props.family == AF_INET) {
5062a8a1552SShannon Nelson /* are there any IPv4 filters to check? */
5072a8a1552SShannon Nelson if (manc_ipv4) {
5082a8a1552SShannon Nelson /* the 4 ipv4 filters are all in MIPAF(3, i) */
5092a8a1552SShannon Nelson for (i = 0; i < num_filters; i++) {
5102a8a1552SShannon Nelson if (!(mfval & BIT(MFVAL_IPV4_FILTER_SHIFT + i)))
5112a8a1552SShannon Nelson continue;
5122a8a1552SShannon Nelson
5132a8a1552SShannon Nelson reg = IXGBE_READ_REG(hw, MIPAF_ARR(3, i));
514205523bcSJesse Brandeburg if (reg == (__force u32)xs->id.daddr.a4)
5152a8a1552SShannon Nelson return 1;
5162a8a1552SShannon Nelson }
5172a8a1552SShannon Nelson }
5182a8a1552SShannon Nelson
5192a8a1552SShannon Nelson if ((bmcipval & BMCIP_MASK) == BMCIP_V4) {
5202a8a1552SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(3));
521205523bcSJesse Brandeburg if (reg == (__force u32)xs->id.daddr.a4)
5222a8a1552SShannon Nelson return 1;
5232a8a1552SShannon Nelson }
5242a8a1552SShannon Nelson
5252a8a1552SShannon Nelson } else {
5262a8a1552SShannon Nelson /* if there are ipv4 filters, they are in the last ipv6 slot */
5272a8a1552SShannon Nelson if (manc_ipv4)
5282a8a1552SShannon Nelson num_filters = 3;
5292a8a1552SShannon Nelson
5302a8a1552SShannon Nelson for (i = 0; i < num_filters; i++) {
5312a8a1552SShannon Nelson if (!(mfval & BIT(MFVAL_IPV6_FILTER_SHIFT + i)))
5322a8a1552SShannon Nelson continue;
5332a8a1552SShannon Nelson
5342a8a1552SShannon Nelson for (j = 0; j < 4; j++) {
5352a8a1552SShannon Nelson reg = IXGBE_READ_REG(hw, MIPAF_ARR(i, j));
536205523bcSJesse Brandeburg if (reg != (__force u32)xs->id.daddr.a6[j])
5372a8a1552SShannon Nelson break;
5382a8a1552SShannon Nelson }
5392a8a1552SShannon Nelson if (j == 4) /* did we match all 4 words? */
5402a8a1552SShannon Nelson return 1;
5412a8a1552SShannon Nelson }
5422a8a1552SShannon Nelson
5432a8a1552SShannon Nelson if ((bmcipval & BMCIP_MASK) == BMCIP_V6) {
5442a8a1552SShannon Nelson for (j = 0; j < 4; j++) {
5452a8a1552SShannon Nelson reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(j));
546205523bcSJesse Brandeburg if (reg != (__force u32)xs->id.daddr.a6[j])
5472a8a1552SShannon Nelson break;
5482a8a1552SShannon Nelson }
5492a8a1552SShannon Nelson if (j == 4) /* did we match all 4 words? */
5502a8a1552SShannon Nelson return 1;
5512a8a1552SShannon Nelson }
5522a8a1552SShannon Nelson }
5532a8a1552SShannon Nelson
5542a8a1552SShannon Nelson return 0;
5552a8a1552SShannon Nelson }
5562a8a1552SShannon Nelson
5572a8a1552SShannon Nelson /**
55863a67fe2SShannon Nelson * ixgbe_ipsec_add_sa - program device with a security association
55963a67fe2SShannon Nelson * @xs: pointer to transformer state struct
5607681a4f5SLeon Romanovsky * @extack: extack point to fill failure reason
56163a67fe2SShannon Nelson **/
ixgbe_ipsec_add_sa(struct xfrm_state * xs,struct netlink_ext_ack * extack)5627681a4f5SLeon Romanovsky static int ixgbe_ipsec_add_sa(struct xfrm_state *xs,
5637681a4f5SLeon Romanovsky struct netlink_ext_ack *extack)
56463a67fe2SShannon Nelson {
565bdfd2d1fSJarod Wilson struct net_device *dev = xs->xso.real_dev;
566bdfd2d1fSJarod Wilson struct ixgbe_adapter *adapter = netdev_priv(dev);
567bdfd2d1fSJarod Wilson struct ixgbe_ipsec *ipsec = adapter->ipsec;
568bdfd2d1fSJarod Wilson struct ixgbe_hw *hw = &adapter->hw;
56963a67fe2SShannon Nelson int checked, match, first;
57063a67fe2SShannon Nelson u16 sa_idx;
57163a67fe2SShannon Nelson int ret;
57263a67fe2SShannon Nelson int i;
57363a67fe2SShannon Nelson
57463a67fe2SShannon Nelson if (xs->id.proto != IPPROTO_ESP && xs->id.proto != IPPROTO_AH) {
575505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol for ipsec offload");
57663a67fe2SShannon Nelson return -EINVAL;
57763a67fe2SShannon Nelson }
57863a67fe2SShannon Nelson
579d785e1feSAntony Antony if (xs->props.mode != XFRM_MODE_TRANSPORT) {
580505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "Unsupported mode for ipsec offload");
581d785e1feSAntony Antony return -EINVAL;
582d785e1feSAntony Antony }
583d785e1feSAntony Antony
5842a8a1552SShannon Nelson if (ixgbe_ipsec_check_mgmt_ip(xs)) {
585505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "IPsec IP addr clash with mgmt filters");
5862a8a1552SShannon Nelson return -EINVAL;
5872a8a1552SShannon Nelson }
5882a8a1552SShannon Nelson
58962f6eca5SLeon Romanovsky if (xs->xso.type != XFRM_DEV_OFFLOAD_CRYPTO) {
590505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "Unsupported ipsec offload type");
59162f6eca5SLeon Romanovsky return -EINVAL;
59262f6eca5SLeon Romanovsky }
59362f6eca5SLeon Romanovsky
5940c05ab78SLeon Romanovsky if (xs->xso.dir == XFRM_DEV_OFFLOAD_IN) {
59563a67fe2SShannon Nelson struct rx_sa rsa;
59663a67fe2SShannon Nelson
59763a67fe2SShannon Nelson if (xs->calg) {
598505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "Compression offload not supported");
59963a67fe2SShannon Nelson return -EINVAL;
60063a67fe2SShannon Nelson }
60163a67fe2SShannon Nelson
60263a67fe2SShannon Nelson /* find the first unused index */
60363a67fe2SShannon Nelson ret = ixgbe_ipsec_find_empty_idx(ipsec, true);
60463a67fe2SShannon Nelson if (ret < 0) {
605505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "No space for SA in Rx table!");
60663a67fe2SShannon Nelson return ret;
60763a67fe2SShannon Nelson }
60863a67fe2SShannon Nelson sa_idx = (u16)ret;
60963a67fe2SShannon Nelson
61063a67fe2SShannon Nelson memset(&rsa, 0, sizeof(rsa));
61163a67fe2SShannon Nelson rsa.used = true;
61263a67fe2SShannon Nelson rsa.xs = xs;
61363a67fe2SShannon Nelson
61463a67fe2SShannon Nelson if (rsa.xs->id.proto & IPPROTO_ESP)
61563a67fe2SShannon Nelson rsa.decrypt = xs->ealg || xs->aead;
61663a67fe2SShannon Nelson
61763a67fe2SShannon Nelson /* get the key and salt */
61863a67fe2SShannon Nelson ret = ixgbe_ipsec_parse_proto_keys(xs, rsa.key, &rsa.salt);
61963a67fe2SShannon Nelson if (ret) {
620505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "Failed to get key data for Rx SA table");
62163a67fe2SShannon Nelson return ret;
62263a67fe2SShannon Nelson }
62363a67fe2SShannon Nelson
62463a67fe2SShannon Nelson /* get ip for rx sa table */
62592103199SShannon Nelson if (xs->props.family == AF_INET6)
62663a67fe2SShannon Nelson memcpy(rsa.ipaddr, &xs->id.daddr.a6, 16);
62763a67fe2SShannon Nelson else
62863a67fe2SShannon Nelson memcpy(&rsa.ipaddr[3], &xs->id.daddr.a4, 4);
62963a67fe2SShannon Nelson
63063a67fe2SShannon Nelson /* The HW does not have a 1:1 mapping from keys to IP addrs, so
63163a67fe2SShannon Nelson * check for a matching IP addr entry in the table. If the addr
63263a67fe2SShannon Nelson * already exists, use it; else find an unused slot and add the
63363a67fe2SShannon Nelson * addr. If one does not exist and there are no unused table
63463a67fe2SShannon Nelson * entries, fail the request.
63563a67fe2SShannon Nelson */
63663a67fe2SShannon Nelson
63763a67fe2SShannon Nelson /* Find an existing match or first not used, and stop looking
63863a67fe2SShannon Nelson * after we've checked all we know we have.
63963a67fe2SShannon Nelson */
64063a67fe2SShannon Nelson checked = 0;
64163a67fe2SShannon Nelson match = -1;
64263a67fe2SShannon Nelson first = -1;
64363a67fe2SShannon Nelson for (i = 0;
64463a67fe2SShannon Nelson i < IXGBE_IPSEC_MAX_RX_IP_COUNT &&
64563a67fe2SShannon Nelson (checked < ipsec->num_rx_sa || first < 0);
64663a67fe2SShannon Nelson i++) {
64763a67fe2SShannon Nelson if (ipsec->ip_tbl[i].used) {
64863a67fe2SShannon Nelson if (!memcmp(ipsec->ip_tbl[i].ipaddr,
64963a67fe2SShannon Nelson rsa.ipaddr, sizeof(rsa.ipaddr))) {
65063a67fe2SShannon Nelson match = i;
65163a67fe2SShannon Nelson break;
65263a67fe2SShannon Nelson }
65363a67fe2SShannon Nelson checked++;
65463a67fe2SShannon Nelson } else if (first < 0) {
65563a67fe2SShannon Nelson first = i; /* track the first empty seen */
65663a67fe2SShannon Nelson }
65763a67fe2SShannon Nelson }
65863a67fe2SShannon Nelson
65963a67fe2SShannon Nelson if (ipsec->num_rx_sa == 0)
66063a67fe2SShannon Nelson first = 0;
66163a67fe2SShannon Nelson
66263a67fe2SShannon Nelson if (match >= 0) {
66363a67fe2SShannon Nelson /* addrs are the same, we should use this one */
66463a67fe2SShannon Nelson rsa.iptbl_ind = match;
66563a67fe2SShannon Nelson ipsec->ip_tbl[match].ref_cnt++;
66663a67fe2SShannon Nelson
66763a67fe2SShannon Nelson } else if (first >= 0) {
66863a67fe2SShannon Nelson /* no matches, but here's an empty slot */
66963a67fe2SShannon Nelson rsa.iptbl_ind = first;
67063a67fe2SShannon Nelson
67163a67fe2SShannon Nelson memcpy(ipsec->ip_tbl[first].ipaddr,
67263a67fe2SShannon Nelson rsa.ipaddr, sizeof(rsa.ipaddr));
67363a67fe2SShannon Nelson ipsec->ip_tbl[first].ref_cnt = 1;
67463a67fe2SShannon Nelson ipsec->ip_tbl[first].used = true;
67563a67fe2SShannon Nelson
67663a67fe2SShannon Nelson ixgbe_ipsec_set_rx_ip(hw, rsa.iptbl_ind, rsa.ipaddr);
67763a67fe2SShannon Nelson
67863a67fe2SShannon Nelson } else {
67963a67fe2SShannon Nelson /* no match and no empty slot */
680505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "No space for SA in Rx IP SA table");
68163a67fe2SShannon Nelson memset(&rsa, 0, sizeof(rsa));
68263a67fe2SShannon Nelson return -ENOSPC;
68363a67fe2SShannon Nelson }
68463a67fe2SShannon Nelson
68563a67fe2SShannon Nelson rsa.mode = IXGBE_RXMOD_VALID;
68663a67fe2SShannon Nelson if (rsa.xs->id.proto & IPPROTO_ESP)
68763a67fe2SShannon Nelson rsa.mode |= IXGBE_RXMOD_PROTO_ESP;
68863a67fe2SShannon Nelson if (rsa.decrypt)
68963a67fe2SShannon Nelson rsa.mode |= IXGBE_RXMOD_DECRYPT;
69092103199SShannon Nelson if (rsa.xs->props.family == AF_INET6)
69163a67fe2SShannon Nelson rsa.mode |= IXGBE_RXMOD_IPV6;
69263a67fe2SShannon Nelson
69363a67fe2SShannon Nelson /* the preparations worked, so save the info */
69463a67fe2SShannon Nelson memcpy(&ipsec->rx_tbl[sa_idx], &rsa, sizeof(rsa));
69563a67fe2SShannon Nelson
69663a67fe2SShannon Nelson ixgbe_ipsec_set_rx_sa(hw, sa_idx, rsa.xs->id.spi, rsa.key,
69763a67fe2SShannon Nelson rsa.salt, rsa.mode, rsa.iptbl_ind);
69863a67fe2SShannon Nelson xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_RX_INDEX;
69963a67fe2SShannon Nelson
70063a67fe2SShannon Nelson ipsec->num_rx_sa++;
70163a67fe2SShannon Nelson
70263a67fe2SShannon Nelson /* hash the new entry for faster search in Rx path */
70363a67fe2SShannon Nelson hash_add_rcu(ipsec->rx_sa_list, &ipsec->rx_tbl[sa_idx].hlist,
7049a75fa5cSShannon Nelson (__force u32)rsa.xs->id.spi);
70563a67fe2SShannon Nelson } else {
70663a67fe2SShannon Nelson struct tx_sa tsa;
70763a67fe2SShannon Nelson
7087fa57ca4SShannon Nelson if (adapter->num_vfs &&
7097fa57ca4SShannon Nelson adapter->bridge_mode != BRIDGE_MODE_VEPA)
71047b6f500SShannon Nelson return -EOPNOTSUPP;
71147b6f500SShannon Nelson
71263a67fe2SShannon Nelson /* find the first unused index */
71363a67fe2SShannon Nelson ret = ixgbe_ipsec_find_empty_idx(ipsec, false);
71463a67fe2SShannon Nelson if (ret < 0) {
715505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "No space for SA in Tx table");
71663a67fe2SShannon Nelson return ret;
71763a67fe2SShannon Nelson }
71863a67fe2SShannon Nelson sa_idx = (u16)ret;
71963a67fe2SShannon Nelson
72063a67fe2SShannon Nelson memset(&tsa, 0, sizeof(tsa));
72163a67fe2SShannon Nelson tsa.used = true;
72263a67fe2SShannon Nelson tsa.xs = xs;
72363a67fe2SShannon Nelson
72463a67fe2SShannon Nelson if (xs->id.proto & IPPROTO_ESP)
72563a67fe2SShannon Nelson tsa.encrypt = xs->ealg || xs->aead;
72663a67fe2SShannon Nelson
72763a67fe2SShannon Nelson ret = ixgbe_ipsec_parse_proto_keys(xs, tsa.key, &tsa.salt);
72863a67fe2SShannon Nelson if (ret) {
729505c500cSLeon Romanovsky NL_SET_ERR_MSG_MOD(extack, "Failed to get key data for Tx SA table");
73063a67fe2SShannon Nelson memset(&tsa, 0, sizeof(tsa));
73163a67fe2SShannon Nelson return ret;
73263a67fe2SShannon Nelson }
73363a67fe2SShannon Nelson
73463a67fe2SShannon Nelson /* the preparations worked, so save the info */
73563a67fe2SShannon Nelson memcpy(&ipsec->tx_tbl[sa_idx], &tsa, sizeof(tsa));
73663a67fe2SShannon Nelson
73763a67fe2SShannon Nelson ixgbe_ipsec_set_tx_sa(hw, sa_idx, tsa.key, tsa.salt);
73863a67fe2SShannon Nelson
73963a67fe2SShannon Nelson xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_TX_INDEX;
74063a67fe2SShannon Nelson
74163a67fe2SShannon Nelson ipsec->num_tx_sa++;
74263a67fe2SShannon Nelson }
74363a67fe2SShannon Nelson
74463a67fe2SShannon Nelson /* enable the engine if not already warmed up */
74563a67fe2SShannon Nelson if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED)) {
74663a67fe2SShannon Nelson ixgbe_ipsec_start_engine(adapter);
74763a67fe2SShannon Nelson adapter->flags2 |= IXGBE_FLAG2_IPSEC_ENABLED;
74863a67fe2SShannon Nelson }
74963a67fe2SShannon Nelson
75063a67fe2SShannon Nelson return 0;
75163a67fe2SShannon Nelson }
75263a67fe2SShannon Nelson
75363a67fe2SShannon Nelson /**
75463a67fe2SShannon Nelson * ixgbe_ipsec_del_sa - clear out this specific SA
75563a67fe2SShannon Nelson * @xs: pointer to transformer state struct
75663a67fe2SShannon Nelson **/
ixgbe_ipsec_del_sa(struct xfrm_state * xs)75763a67fe2SShannon Nelson static void ixgbe_ipsec_del_sa(struct xfrm_state *xs)
75863a67fe2SShannon Nelson {
759bdfd2d1fSJarod Wilson struct net_device *dev = xs->xso.real_dev;
760bdfd2d1fSJarod Wilson struct ixgbe_adapter *adapter = netdev_priv(dev);
761bdfd2d1fSJarod Wilson struct ixgbe_ipsec *ipsec = adapter->ipsec;
762bdfd2d1fSJarod Wilson struct ixgbe_hw *hw = &adapter->hw;
76363a67fe2SShannon Nelson u32 zerobuf[4] = {0, 0, 0, 0};
76463a67fe2SShannon Nelson u16 sa_idx;
76563a67fe2SShannon Nelson
7660c05ab78SLeon Romanovsky if (xs->xso.dir == XFRM_DEV_OFFLOAD_IN) {
76763a67fe2SShannon Nelson struct rx_sa *rsa;
76863a67fe2SShannon Nelson u8 ipi;
76963a67fe2SShannon Nelson
77063a67fe2SShannon Nelson sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_RX_INDEX;
77163a67fe2SShannon Nelson rsa = &ipsec->rx_tbl[sa_idx];
77263a67fe2SShannon Nelson
77363a67fe2SShannon Nelson if (!rsa->used) {
77463a67fe2SShannon Nelson netdev_err(dev, "Invalid Rx SA selected sa_idx=%d offload_handle=%lu\n",
77563a67fe2SShannon Nelson sa_idx, xs->xso.offload_handle);
77663a67fe2SShannon Nelson return;
77763a67fe2SShannon Nelson }
77863a67fe2SShannon Nelson
77963a67fe2SShannon Nelson ixgbe_ipsec_set_rx_sa(hw, sa_idx, 0, zerobuf, 0, 0, 0);
78063a67fe2SShannon Nelson hash_del_rcu(&rsa->hlist);
78163a67fe2SShannon Nelson
78263a67fe2SShannon Nelson /* if the IP table entry is referenced by only this SA,
78363a67fe2SShannon Nelson * i.e. ref_cnt is only 1, clear the IP table entry as well
78463a67fe2SShannon Nelson */
78563a67fe2SShannon Nelson ipi = rsa->iptbl_ind;
78663a67fe2SShannon Nelson if (ipsec->ip_tbl[ipi].ref_cnt > 0) {
78763a67fe2SShannon Nelson ipsec->ip_tbl[ipi].ref_cnt--;
78863a67fe2SShannon Nelson
78963a67fe2SShannon Nelson if (!ipsec->ip_tbl[ipi].ref_cnt) {
79063a67fe2SShannon Nelson memset(&ipsec->ip_tbl[ipi], 0,
79163a67fe2SShannon Nelson sizeof(struct rx_ip_sa));
7929cfbfa70SCathy Zhou ixgbe_ipsec_set_rx_ip(hw, ipi,
7939cfbfa70SCathy Zhou (__force __be32 *)zerobuf);
79463a67fe2SShannon Nelson }
79563a67fe2SShannon Nelson }
79663a67fe2SShannon Nelson
79763a67fe2SShannon Nelson memset(rsa, 0, sizeof(struct rx_sa));
79863a67fe2SShannon Nelson ipsec->num_rx_sa--;
79963a67fe2SShannon Nelson } else {
80063a67fe2SShannon Nelson sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
80163a67fe2SShannon Nelson
80263a67fe2SShannon Nelson if (!ipsec->tx_tbl[sa_idx].used) {
80363a67fe2SShannon Nelson netdev_err(dev, "Invalid Tx SA selected sa_idx=%d offload_handle=%lu\n",
80463a67fe2SShannon Nelson sa_idx, xs->xso.offload_handle);
80563a67fe2SShannon Nelson return;
80663a67fe2SShannon Nelson }
80763a67fe2SShannon Nelson
80863a67fe2SShannon Nelson ixgbe_ipsec_set_tx_sa(hw, sa_idx, zerobuf, 0);
80963a67fe2SShannon Nelson memset(&ipsec->tx_tbl[sa_idx], 0, sizeof(struct tx_sa));
81063a67fe2SShannon Nelson ipsec->num_tx_sa--;
81163a67fe2SShannon Nelson }
81263a67fe2SShannon Nelson
81363a67fe2SShannon Nelson /* if there are no SAs left, stop the engine to save energy */
81463a67fe2SShannon Nelson if (ipsec->num_rx_sa == 0 && ipsec->num_tx_sa == 0) {
81563a67fe2SShannon Nelson adapter->flags2 &= ~IXGBE_FLAG2_IPSEC_ENABLED;
81663a67fe2SShannon Nelson ixgbe_ipsec_stop_engine(adapter);
81763a67fe2SShannon Nelson }
81863a67fe2SShannon Nelson }
81963a67fe2SShannon Nelson
82059259470SShannon Nelson /**
82159259470SShannon Nelson * ixgbe_ipsec_offload_ok - can this packet use the xfrm hw offload
82259259470SShannon Nelson * @skb: current data packet
82359259470SShannon Nelson * @xs: pointer to transformer state struct
82459259470SShannon Nelson **/
ixgbe_ipsec_offload_ok(struct sk_buff * skb,struct xfrm_state * xs)82559259470SShannon Nelson static bool ixgbe_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
82659259470SShannon Nelson {
82759259470SShannon Nelson if (xs->props.family == AF_INET) {
82859259470SShannon Nelson /* Offload with IPv4 options is not supported yet */
82959259470SShannon Nelson if (ip_hdr(skb)->ihl != 5)
83059259470SShannon Nelson return false;
83159259470SShannon Nelson } else {
83259259470SShannon Nelson /* Offload with IPv6 extension headers is not support yet */
83359259470SShannon Nelson if (ipv6_ext_hdr(ipv6_hdr(skb)->nexthdr))
83459259470SShannon Nelson return false;
83559259470SShannon Nelson }
83659259470SShannon Nelson
83759259470SShannon Nelson return true;
83859259470SShannon Nelson }
83959259470SShannon Nelson
84063a67fe2SShannon Nelson static const struct xfrmdev_ops ixgbe_xfrmdev_ops = {
84163a67fe2SShannon Nelson .xdo_dev_state_add = ixgbe_ipsec_add_sa,
84263a67fe2SShannon Nelson .xdo_dev_state_delete = ixgbe_ipsec_del_sa,
84359259470SShannon Nelson .xdo_dev_offload_ok = ixgbe_ipsec_offload_ok,
84463a67fe2SShannon Nelson };
84563a67fe2SShannon Nelson
84663a67fe2SShannon Nelson /**
847eda0333aSShannon Nelson * ixgbe_ipsec_vf_clear - clear the tables of data for a VF
848eda0333aSShannon Nelson * @adapter: board private structure
849eda0333aSShannon Nelson * @vf: VF id to be removed
850eda0333aSShannon Nelson **/
ixgbe_ipsec_vf_clear(struct ixgbe_adapter * adapter,u32 vf)851eda0333aSShannon Nelson void ixgbe_ipsec_vf_clear(struct ixgbe_adapter *adapter, u32 vf)
852eda0333aSShannon Nelson {
853eda0333aSShannon Nelson struct ixgbe_ipsec *ipsec = adapter->ipsec;
854eda0333aSShannon Nelson int i;
855eda0333aSShannon Nelson
85692924064SDann Frazier if (!ipsec)
85792924064SDann Frazier return;
85892924064SDann Frazier
859eda0333aSShannon Nelson /* search rx sa table */
860eda0333aSShannon Nelson for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_rx_sa; i++) {
861eda0333aSShannon Nelson if (!ipsec->rx_tbl[i].used)
862eda0333aSShannon Nelson continue;
863eda0333aSShannon Nelson if (ipsec->rx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
864eda0333aSShannon Nelson ipsec->rx_tbl[i].vf == vf)
865eda0333aSShannon Nelson ixgbe_ipsec_del_sa(ipsec->rx_tbl[i].xs);
866eda0333aSShannon Nelson }
867eda0333aSShannon Nelson
868eda0333aSShannon Nelson /* search tx sa table */
869eda0333aSShannon Nelson for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_tx_sa; i++) {
870eda0333aSShannon Nelson if (!ipsec->tx_tbl[i].used)
871eda0333aSShannon Nelson continue;
872eda0333aSShannon Nelson if (ipsec->tx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
873eda0333aSShannon Nelson ipsec->tx_tbl[i].vf == vf)
874eda0333aSShannon Nelson ixgbe_ipsec_del_sa(ipsec->tx_tbl[i].xs);
875eda0333aSShannon Nelson }
876eda0333aSShannon Nelson }
877eda0333aSShannon Nelson
878eda0333aSShannon Nelson /**
879eda0333aSShannon Nelson * ixgbe_ipsec_vf_add_sa - translate VF request to SA add
880eda0333aSShannon Nelson * @adapter: board private structure
881eda0333aSShannon Nelson * @msgbuf: The message buffer
882eda0333aSShannon Nelson * @vf: the VF index
883eda0333aSShannon Nelson *
884eda0333aSShannon Nelson * Make up a new xs and algorithm info from the data sent by the VF.
885eda0333aSShannon Nelson * We only need to sketch in just enough to set up the HW offload.
886eda0333aSShannon Nelson * Put the resulting offload_handle into the return message to the VF.
887eda0333aSShannon Nelson *
888eda0333aSShannon Nelson * Returns 0 or error value
889eda0333aSShannon Nelson **/
ixgbe_ipsec_vf_add_sa(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)890eda0333aSShannon Nelson int ixgbe_ipsec_vf_add_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
891eda0333aSShannon Nelson {
892eda0333aSShannon Nelson struct ixgbe_ipsec *ipsec = adapter->ipsec;
893eda0333aSShannon Nelson struct xfrm_algo_desc *algo;
894eda0333aSShannon Nelson struct sa_mbx_msg *sam;
895eda0333aSShannon Nelson struct xfrm_state *xs;
896eda0333aSShannon Nelson size_t aead_len;
897eda0333aSShannon Nelson u16 sa_idx;
898eda0333aSShannon Nelson u32 pfsa;
899eda0333aSShannon Nelson int err;
900eda0333aSShannon Nelson
901eda0333aSShannon Nelson sam = (struct sa_mbx_msg *)(&msgbuf[1]);
9029e4e30ccSShannon Nelson if (!adapter->vfinfo[vf].trusted ||
9039e4e30ccSShannon Nelson !(adapter->flags2 & IXGBE_FLAG2_VF_IPSEC_ENABLED)) {
904eda0333aSShannon Nelson e_warn(drv, "VF %d attempted to add an IPsec SA\n", vf);
905eda0333aSShannon Nelson err = -EACCES;
906eda0333aSShannon Nelson goto err_out;
907eda0333aSShannon Nelson }
908eda0333aSShannon Nelson
909eda0333aSShannon Nelson /* Tx IPsec offload doesn't seem to work on this
910eda0333aSShannon Nelson * device, so block these requests for now.
911eda0333aSShannon Nelson */
9120c05ab78SLeon Romanovsky if (sam->dir != XFRM_DEV_OFFLOAD_IN) {
9135ed4e9e9SShannon Nelson err = -EOPNOTSUPP;
914eda0333aSShannon Nelson goto err_out;
915eda0333aSShannon Nelson }
916eda0333aSShannon Nelson
917*4465b15aSPrzemek Kitszel algo = xfrm_aead_get_byname(aes_gcm_name, IXGBE_IPSEC_AUTH_BITS, 1);
918*4465b15aSPrzemek Kitszel if (unlikely(!algo)) {
919*4465b15aSPrzemek Kitszel err = -ENOENT;
920*4465b15aSPrzemek Kitszel goto err_out;
921*4465b15aSPrzemek Kitszel }
922*4465b15aSPrzemek Kitszel
923*4465b15aSPrzemek Kitszel xs = kzalloc(sizeof(*xs), GFP_ATOMIC);
924eda0333aSShannon Nelson if (unlikely(!xs)) {
925eda0333aSShannon Nelson err = -ENOMEM;
926eda0333aSShannon Nelson goto err_out;
927eda0333aSShannon Nelson }
928eda0333aSShannon Nelson
9290c05ab78SLeon Romanovsky xs->xso.dir = sam->dir;
930eda0333aSShannon Nelson xs->id.spi = sam->spi;
931eda0333aSShannon Nelson xs->id.proto = sam->proto;
932eda0333aSShannon Nelson xs->props.family = sam->family;
933eda0333aSShannon Nelson if (xs->props.family == AF_INET6)
934eda0333aSShannon Nelson memcpy(&xs->id.daddr.a6, sam->addr, sizeof(xs->id.daddr.a6));
935eda0333aSShannon Nelson else
936eda0333aSShannon Nelson memcpy(&xs->id.daddr.a4, sam->addr, sizeof(xs->id.daddr.a4));
937eda0333aSShannon Nelson xs->xso.dev = adapter->netdev;
938eda0333aSShannon Nelson
939eda0333aSShannon Nelson aead_len = sizeof(*xs->aead) + IXGBE_IPSEC_KEY_BITS / 8;
940*4465b15aSPrzemek Kitszel xs->aead = kzalloc(aead_len, GFP_ATOMIC);
941eda0333aSShannon Nelson if (unlikely(!xs->aead)) {
942eda0333aSShannon Nelson err = -ENOMEM;
943eda0333aSShannon Nelson goto err_xs;
944eda0333aSShannon Nelson }
945eda0333aSShannon Nelson
946eda0333aSShannon Nelson xs->props.ealgo = algo->desc.sadb_alg_id;
947eda0333aSShannon Nelson xs->geniv = algo->uinfo.aead.geniv;
948eda0333aSShannon Nelson xs->aead->alg_icv_len = IXGBE_IPSEC_AUTH_BITS;
949eda0333aSShannon Nelson xs->aead->alg_key_len = IXGBE_IPSEC_KEY_BITS;
950eda0333aSShannon Nelson memcpy(xs->aead->alg_key, sam->key, sizeof(sam->key));
951eda0333aSShannon Nelson memcpy(xs->aead->alg_name, aes_gcm_name, sizeof(aes_gcm_name));
952eda0333aSShannon Nelson
953eda0333aSShannon Nelson /* set up the HW offload */
9547681a4f5SLeon Romanovsky err = ixgbe_ipsec_add_sa(xs, NULL);
955eda0333aSShannon Nelson if (err)
956eda0333aSShannon Nelson goto err_aead;
957eda0333aSShannon Nelson
958eda0333aSShannon Nelson pfsa = xs->xso.offload_handle;
959eda0333aSShannon Nelson if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
960eda0333aSShannon Nelson sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
961eda0333aSShannon Nelson ipsec->rx_tbl[sa_idx].vf = vf;
962eda0333aSShannon Nelson ipsec->rx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
963eda0333aSShannon Nelson } else {
964eda0333aSShannon Nelson sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
965eda0333aSShannon Nelson ipsec->tx_tbl[sa_idx].vf = vf;
966eda0333aSShannon Nelson ipsec->tx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
967eda0333aSShannon Nelson }
968eda0333aSShannon Nelson
969eda0333aSShannon Nelson msgbuf[1] = xs->xso.offload_handle;
970eda0333aSShannon Nelson
971eda0333aSShannon Nelson return 0;
972eda0333aSShannon Nelson
973eda0333aSShannon Nelson err_aead:
974453431a5SWaiman Long kfree_sensitive(xs->aead);
975eda0333aSShannon Nelson err_xs:
976453431a5SWaiman Long kfree_sensitive(xs);
977eda0333aSShannon Nelson err_out:
978eda0333aSShannon Nelson msgbuf[1] = err;
979eda0333aSShannon Nelson return err;
980eda0333aSShannon Nelson }
981eda0333aSShannon Nelson
982eda0333aSShannon Nelson /**
983eda0333aSShannon Nelson * ixgbe_ipsec_vf_del_sa - translate VF request to SA delete
984eda0333aSShannon Nelson * @adapter: board private structure
985eda0333aSShannon Nelson * @msgbuf: The message buffer
986eda0333aSShannon Nelson * @vf: the VF index
987eda0333aSShannon Nelson *
988eda0333aSShannon Nelson * Given the offload_handle sent by the VF, look for the related SA table
989eda0333aSShannon Nelson * entry and use its xs field to call for a delete of the SA.
990eda0333aSShannon Nelson *
991eda0333aSShannon Nelson * Note: We silently ignore requests to delete entries that are already
992eda0333aSShannon Nelson * set to unused because when a VF is set to "DOWN", the PF first
993eda0333aSShannon Nelson * gets a reset and clears all the VF's entries; then the VF's
994eda0333aSShannon Nelson * XFRM stack sends individual deletes for each entry, which the
995eda0333aSShannon Nelson * reset already removed. In the future it might be good to try to
996eda0333aSShannon Nelson * optimize this so not so many unnecessary delete messages are sent.
997eda0333aSShannon Nelson *
998eda0333aSShannon Nelson * Returns 0 or error value
999eda0333aSShannon Nelson **/
ixgbe_ipsec_vf_del_sa(struct ixgbe_adapter * adapter,u32 * msgbuf,u32 vf)1000eda0333aSShannon Nelson int ixgbe_ipsec_vf_del_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
1001eda0333aSShannon Nelson {
1002eda0333aSShannon Nelson struct ixgbe_ipsec *ipsec = adapter->ipsec;
1003eda0333aSShannon Nelson struct xfrm_state *xs;
1004eda0333aSShannon Nelson u32 pfsa = msgbuf[1];
1005eda0333aSShannon Nelson u16 sa_idx;
1006eda0333aSShannon Nelson
1007eda0333aSShannon Nelson if (!adapter->vfinfo[vf].trusted) {
1008eda0333aSShannon Nelson e_err(drv, "vf %d attempted to delete an SA\n", vf);
1009eda0333aSShannon Nelson return -EPERM;
1010eda0333aSShannon Nelson }
1011eda0333aSShannon Nelson
1012eda0333aSShannon Nelson if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
1013eda0333aSShannon Nelson struct rx_sa *rsa;
1014eda0333aSShannon Nelson
1015eda0333aSShannon Nelson sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
1016eda0333aSShannon Nelson if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
1017eda0333aSShannon Nelson e_err(drv, "vf %d SA index %d out of range\n",
1018eda0333aSShannon Nelson vf, sa_idx);
1019eda0333aSShannon Nelson return -EINVAL;
1020eda0333aSShannon Nelson }
1021eda0333aSShannon Nelson
1022eda0333aSShannon Nelson rsa = &ipsec->rx_tbl[sa_idx];
1023eda0333aSShannon Nelson
1024eda0333aSShannon Nelson if (!rsa->used)
1025eda0333aSShannon Nelson return 0;
1026eda0333aSShannon Nelson
1027eda0333aSShannon Nelson if (!(rsa->mode & IXGBE_RXTXMOD_VF) ||
1028eda0333aSShannon Nelson rsa->vf != vf) {
1029eda0333aSShannon Nelson e_err(drv, "vf %d bad Rx SA index %d\n", vf, sa_idx);
1030eda0333aSShannon Nelson return -ENOENT;
1031eda0333aSShannon Nelson }
1032eda0333aSShannon Nelson
1033eda0333aSShannon Nelson xs = ipsec->rx_tbl[sa_idx].xs;
1034eda0333aSShannon Nelson } else {
1035eda0333aSShannon Nelson struct tx_sa *tsa;
1036eda0333aSShannon Nelson
1037eda0333aSShannon Nelson sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
1038eda0333aSShannon Nelson if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
1039eda0333aSShannon Nelson e_err(drv, "vf %d SA index %d out of range\n",
1040eda0333aSShannon Nelson vf, sa_idx);
1041eda0333aSShannon Nelson return -EINVAL;
1042eda0333aSShannon Nelson }
1043eda0333aSShannon Nelson
1044eda0333aSShannon Nelson tsa = &ipsec->tx_tbl[sa_idx];
1045eda0333aSShannon Nelson
1046eda0333aSShannon Nelson if (!tsa->used)
1047eda0333aSShannon Nelson return 0;
1048eda0333aSShannon Nelson
1049eda0333aSShannon Nelson if (!(tsa->mode & IXGBE_RXTXMOD_VF) ||
1050eda0333aSShannon Nelson tsa->vf != vf) {
1051eda0333aSShannon Nelson e_err(drv, "vf %d bad Tx SA index %d\n", vf, sa_idx);
1052eda0333aSShannon Nelson return -ENOENT;
1053eda0333aSShannon Nelson }
1054eda0333aSShannon Nelson
1055eda0333aSShannon Nelson xs = ipsec->tx_tbl[sa_idx].xs;
1056eda0333aSShannon Nelson }
1057eda0333aSShannon Nelson
1058eda0333aSShannon Nelson ixgbe_ipsec_del_sa(xs);
1059eda0333aSShannon Nelson
1060eda0333aSShannon Nelson /* remove the xs that was made-up in the add request */
1061453431a5SWaiman Long kfree_sensitive(xs);
1062eda0333aSShannon Nelson
1063eda0333aSShannon Nelson return 0;
1064eda0333aSShannon Nelson }
1065eda0333aSShannon Nelson
1066eda0333aSShannon Nelson /**
106759259470SShannon Nelson * ixgbe_ipsec_tx - setup Tx flags for ipsec offload
106859259470SShannon Nelson * @tx_ring: outgoing context
106959259470SShannon Nelson * @first: current data packet
107059259470SShannon Nelson * @itd: ipsec Tx data for later use in building context descriptor
107159259470SShannon Nelson **/
ixgbe_ipsec_tx(struct ixgbe_ring * tx_ring,struct ixgbe_tx_buffer * first,struct ixgbe_ipsec_tx_data * itd)107259259470SShannon Nelson int ixgbe_ipsec_tx(struct ixgbe_ring *tx_ring,
107359259470SShannon Nelson struct ixgbe_tx_buffer *first,
107459259470SShannon Nelson struct ixgbe_ipsec_tx_data *itd)
107559259470SShannon Nelson {
107659259470SShannon Nelson struct ixgbe_adapter *adapter = netdev_priv(tx_ring->netdev);
107759259470SShannon Nelson struct ixgbe_ipsec *ipsec = adapter->ipsec;
107859259470SShannon Nelson struct xfrm_state *xs;
10792fdb435bSFlorian Westphal struct sec_path *sp;
108059259470SShannon Nelson struct tx_sa *tsa;
108159259470SShannon Nelson
10822fdb435bSFlorian Westphal sp = skb_sec_path(first->skb);
10832fdb435bSFlorian Westphal if (unlikely(!sp->len)) {
108459259470SShannon Nelson netdev_err(tx_ring->netdev, "%s: no xfrm state len = %d\n",
10852fdb435bSFlorian Westphal __func__, sp->len);
108659259470SShannon Nelson return 0;
108759259470SShannon Nelson }
108859259470SShannon Nelson
108959259470SShannon Nelson xs = xfrm_input_state(first->skb);
109059259470SShannon Nelson if (unlikely(!xs)) {
109159259470SShannon Nelson netdev_err(tx_ring->netdev, "%s: no xfrm_input_state() xs = %p\n",
109259259470SShannon Nelson __func__, xs);
109359259470SShannon Nelson return 0;
109459259470SShannon Nelson }
109559259470SShannon Nelson
109659259470SShannon Nelson itd->sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
1097c4111041SDan Carpenter if (unlikely(itd->sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT)) {
109859259470SShannon Nelson netdev_err(tx_ring->netdev, "%s: bad sa_idx=%d handle=%lu\n",
109959259470SShannon Nelson __func__, itd->sa_idx, xs->xso.offload_handle);
110059259470SShannon Nelson return 0;
110159259470SShannon Nelson }
110259259470SShannon Nelson
110359259470SShannon Nelson tsa = &ipsec->tx_tbl[itd->sa_idx];
110459259470SShannon Nelson if (unlikely(!tsa->used)) {
110559259470SShannon Nelson netdev_err(tx_ring->netdev, "%s: unused sa_idx=%d\n",
110659259470SShannon Nelson __func__, itd->sa_idx);
110759259470SShannon Nelson return 0;
110859259470SShannon Nelson }
110959259470SShannon Nelson
111059259470SShannon Nelson first->tx_flags |= IXGBE_TX_FLAGS_IPSEC | IXGBE_TX_FLAGS_CC;
111159259470SShannon Nelson
111259259470SShannon Nelson if (xs->id.proto == IPPROTO_ESP) {
11130ae418e6SShannon Nelson
111459259470SShannon Nelson itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_TYPE_ESP |
111559259470SShannon Nelson IXGBE_ADVTXD_TUCMD_L4T_TCP;
111659259470SShannon Nelson if (first->protocol == htons(ETH_P_IP))
111759259470SShannon Nelson itd->flags |= IXGBE_ADVTXD_TUCMD_IPV4;
11180ae418e6SShannon Nelson
11190ae418e6SShannon Nelson /* The actual trailer length is authlen (16 bytes) plus
11200ae418e6SShannon Nelson * 2 bytes for the proto and the padlen values, plus
11210ae418e6SShannon Nelson * padlen bytes of padding. This ends up not the same
11220ae418e6SShannon Nelson * as the static value found in xs->props.trailer_len (21).
11230ae418e6SShannon Nelson *
11241db685e6SShannon Nelson * ... but if we're doing GSO, don't bother as the stack
11251db685e6SShannon Nelson * doesn't add a trailer for those.
11260ae418e6SShannon Nelson */
11271db685e6SShannon Nelson if (!skb_is_gso(first->skb)) {
11281db685e6SShannon Nelson /* The "correct" way to get the auth length would be
11291db685e6SShannon Nelson * to use
11301db685e6SShannon Nelson * authlen = crypto_aead_authsize(xs->data);
11311db685e6SShannon Nelson * but since we know we only have one size to worry
11321db685e6SShannon Nelson * about * we can let the compiler use the constant
11331db685e6SShannon Nelson * and save us a few CPU cycles.
11341db685e6SShannon Nelson */
11351db685e6SShannon Nelson const int authlen = IXGBE_IPSEC_AUTH_BITS / 8;
11361db685e6SShannon Nelson struct sk_buff *skb = first->skb;
11371db685e6SShannon Nelson u8 padlen;
11381db685e6SShannon Nelson int ret;
11390ae418e6SShannon Nelson
11401db685e6SShannon Nelson ret = skb_copy_bits(skb, skb->len - (authlen + 2),
11411db685e6SShannon Nelson &padlen, 1);
11420ae418e6SShannon Nelson if (unlikely(ret))
11430ae418e6SShannon Nelson return 0;
11441db685e6SShannon Nelson itd->trailer_len = authlen + 2 + padlen;
11451db685e6SShannon Nelson }
114659259470SShannon Nelson }
114759259470SShannon Nelson if (tsa->encrypt)
114859259470SShannon Nelson itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_ENCRYPT_EN;
114959259470SShannon Nelson
115059259470SShannon Nelson return 1;
115159259470SShannon Nelson }
115259259470SShannon Nelson
115359259470SShannon Nelson /**
115492103199SShannon Nelson * ixgbe_ipsec_rx - decode ipsec bits from Rx descriptor
115592103199SShannon Nelson * @rx_ring: receiving ring
115692103199SShannon Nelson * @rx_desc: receive data descriptor
115792103199SShannon Nelson * @skb: current data packet
115892103199SShannon Nelson *
115992103199SShannon Nelson * Determine if there was an ipsec encapsulation noticed, and if so set up
116092103199SShannon Nelson * the resulting status for later in the receive stack.
116192103199SShannon Nelson **/
ixgbe_ipsec_rx(struct ixgbe_ring * rx_ring,union ixgbe_adv_rx_desc * rx_desc,struct sk_buff * skb)116292103199SShannon Nelson void ixgbe_ipsec_rx(struct ixgbe_ring *rx_ring,
116392103199SShannon Nelson union ixgbe_adv_rx_desc *rx_desc,
116492103199SShannon Nelson struct sk_buff *skb)
116592103199SShannon Nelson {
116692103199SShannon Nelson struct ixgbe_adapter *adapter = netdev_priv(rx_ring->netdev);
116792103199SShannon Nelson __le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
116892103199SShannon Nelson __le16 ipsec_pkt_types = cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH |
116992103199SShannon Nelson IXGBE_RXDADV_PKTTYPE_IPSEC_ESP);
117092103199SShannon Nelson struct ixgbe_ipsec *ipsec = adapter->ipsec;
117192103199SShannon Nelson struct xfrm_offload *xo = NULL;
117292103199SShannon Nelson struct xfrm_state *xs = NULL;
117392103199SShannon Nelson struct ipv6hdr *ip6 = NULL;
117492103199SShannon Nelson struct iphdr *ip4 = NULL;
1175a84e3f53SFlorian Westphal struct sec_path *sp;
117692103199SShannon Nelson void *daddr;
117792103199SShannon Nelson __be32 spi;
117892103199SShannon Nelson u8 *c_hdr;
117992103199SShannon Nelson u8 proto;
118092103199SShannon Nelson
118192103199SShannon Nelson /* Find the ip and crypto headers in the data.
118292103199SShannon Nelson * We can assume no vlan header in the way, b/c the
118392103199SShannon Nelson * hw won't recognize the IPsec packet and anyway the
118492103199SShannon Nelson * currently vlan device doesn't support xfrm offload.
118592103199SShannon Nelson */
118692103199SShannon Nelson if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV4)) {
118792103199SShannon Nelson ip4 = (struct iphdr *)(skb->data + ETH_HLEN);
118892103199SShannon Nelson daddr = &ip4->daddr;
118992103199SShannon Nelson c_hdr = (u8 *)ip4 + ip4->ihl * 4;
119092103199SShannon Nelson } else if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV6)) {
119192103199SShannon Nelson ip6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
119292103199SShannon Nelson daddr = &ip6->daddr;
119392103199SShannon Nelson c_hdr = (u8 *)ip6 + sizeof(struct ipv6hdr);
119492103199SShannon Nelson } else {
119592103199SShannon Nelson return;
119692103199SShannon Nelson }
119792103199SShannon Nelson
119892103199SShannon Nelson switch (pkt_info & ipsec_pkt_types) {
119992103199SShannon Nelson case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH):
120092103199SShannon Nelson spi = ((struct ip_auth_hdr *)c_hdr)->spi;
120192103199SShannon Nelson proto = IPPROTO_AH;
120292103199SShannon Nelson break;
120392103199SShannon Nelson case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_ESP):
120492103199SShannon Nelson spi = ((struct ip_esp_hdr *)c_hdr)->spi;
120592103199SShannon Nelson proto = IPPROTO_ESP;
120692103199SShannon Nelson break;
120792103199SShannon Nelson default:
120892103199SShannon Nelson return;
120992103199SShannon Nelson }
121092103199SShannon Nelson
121192103199SShannon Nelson xs = ixgbe_ipsec_find_rx_state(ipsec, daddr, proto, spi, !!ip4);
121292103199SShannon Nelson if (unlikely(!xs))
121392103199SShannon Nelson return;
121492103199SShannon Nelson
1215a84e3f53SFlorian Westphal sp = secpath_set(skb);
1216a84e3f53SFlorian Westphal if (unlikely(!sp))
121792103199SShannon Nelson return;
121892103199SShannon Nelson
1219a84e3f53SFlorian Westphal sp->xvec[sp->len++] = xs;
1220a84e3f53SFlorian Westphal sp->olen++;
122192103199SShannon Nelson xo = xfrm_offload(skb);
122292103199SShannon Nelson xo->flags = CRYPTO_DONE;
122392103199SShannon Nelson xo->status = CRYPTO_SUCCESS;
1224a8a43fdaSShannon Nelson
1225a8a43fdaSShannon Nelson adapter->rx_ipsec++;
122692103199SShannon Nelson }
122792103199SShannon Nelson
122892103199SShannon Nelson /**
12298bbbc5e9SShannon Nelson * ixgbe_init_ipsec_offload - initialize security registers for IPSec operation
12308bbbc5e9SShannon Nelson * @adapter: board private structure
12318bbbc5e9SShannon Nelson **/
ixgbe_init_ipsec_offload(struct ixgbe_adapter * adapter)12328bbbc5e9SShannon Nelson void ixgbe_init_ipsec_offload(struct ixgbe_adapter *adapter)
12338bbbc5e9SShannon Nelson {
1234421d954cSAlexander Duyck struct ixgbe_hw *hw = &adapter->hw;
123563a67fe2SShannon Nelson struct ixgbe_ipsec *ipsec;
1236421d954cSAlexander Duyck u32 t_dis, r_dis;
123763a67fe2SShannon Nelson size_t size;
123863a67fe2SShannon Nelson
1239421d954cSAlexander Duyck if (hw->mac.type == ixgbe_mac_82598EB)
1240421d954cSAlexander Duyck return;
1241421d954cSAlexander Duyck
1242421d954cSAlexander Duyck /* If there is no support for either Tx or Rx offload
1243421d954cSAlexander Duyck * we should not be advertising support for IPsec.
1244421d954cSAlexander Duyck */
1245421d954cSAlexander Duyck t_dis = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
1246421d954cSAlexander Duyck IXGBE_SECTXSTAT_SECTX_OFF_DIS;
1247421d954cSAlexander Duyck r_dis = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
1248421d954cSAlexander Duyck IXGBE_SECRXSTAT_SECRX_OFF_DIS;
1249421d954cSAlexander Duyck if (t_dis || r_dis)
125063a67fe2SShannon Nelson return;
125163a67fe2SShannon Nelson
125263a67fe2SShannon Nelson ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
125363a67fe2SShannon Nelson if (!ipsec)
125463a67fe2SShannon Nelson goto err1;
125563a67fe2SShannon Nelson hash_init(ipsec->rx_sa_list);
125663a67fe2SShannon Nelson
125763a67fe2SShannon Nelson size = sizeof(struct rx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
125863a67fe2SShannon Nelson ipsec->rx_tbl = kzalloc(size, GFP_KERNEL);
125963a67fe2SShannon Nelson if (!ipsec->rx_tbl)
126063a67fe2SShannon Nelson goto err2;
126163a67fe2SShannon Nelson
126263a67fe2SShannon Nelson size = sizeof(struct tx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
126363a67fe2SShannon Nelson ipsec->tx_tbl = kzalloc(size, GFP_KERNEL);
126463a67fe2SShannon Nelson if (!ipsec->tx_tbl)
126563a67fe2SShannon Nelson goto err2;
126663a67fe2SShannon Nelson
126763a67fe2SShannon Nelson size = sizeof(struct rx_ip_sa) * IXGBE_IPSEC_MAX_RX_IP_COUNT;
126863a67fe2SShannon Nelson ipsec->ip_tbl = kzalloc(size, GFP_KERNEL);
126963a67fe2SShannon Nelson if (!ipsec->ip_tbl)
127063a67fe2SShannon Nelson goto err2;
127163a67fe2SShannon Nelson
127263a67fe2SShannon Nelson ipsec->num_rx_sa = 0;
127363a67fe2SShannon Nelson ipsec->num_tx_sa = 0;
127463a67fe2SShannon Nelson
127563a67fe2SShannon Nelson adapter->ipsec = ipsec;
127649a94d74SShannon Nelson ixgbe_ipsec_stop_engine(adapter);
127763a67fe2SShannon Nelson ixgbe_ipsec_clear_hw_tables(adapter);
127863a67fe2SShannon Nelson
127985bc2663SShannon Nelson adapter->netdev->xfrmdev_ops = &ixgbe_xfrmdev_ops;
128070da6824SShannon Nelson
128163a67fe2SShannon Nelson return;
128263a67fe2SShannon Nelson
128363a67fe2SShannon Nelson err2:
128463a67fe2SShannon Nelson kfree(ipsec->ip_tbl);
128563a67fe2SShannon Nelson kfree(ipsec->rx_tbl);
128663a67fe2SShannon Nelson kfree(ipsec->tx_tbl);
1287c89ebb96SColin Ian King kfree(ipsec);
128863a67fe2SShannon Nelson err1:
128963a67fe2SShannon Nelson netdev_err(adapter->netdev, "Unable to allocate memory for SA tables");
129063a67fe2SShannon Nelson }
129163a67fe2SShannon Nelson
129263a67fe2SShannon Nelson /**
129363a67fe2SShannon Nelson * ixgbe_stop_ipsec_offload - tear down the ipsec offload
129463a67fe2SShannon Nelson * @adapter: board private structure
129563a67fe2SShannon Nelson **/
ixgbe_stop_ipsec_offload(struct ixgbe_adapter * adapter)129663a67fe2SShannon Nelson void ixgbe_stop_ipsec_offload(struct ixgbe_adapter *adapter)
129763a67fe2SShannon Nelson {
129863a67fe2SShannon Nelson struct ixgbe_ipsec *ipsec = adapter->ipsec;
129963a67fe2SShannon Nelson
130063a67fe2SShannon Nelson adapter->ipsec = NULL;
130163a67fe2SShannon Nelson if (ipsec) {
130263a67fe2SShannon Nelson kfree(ipsec->ip_tbl);
130363a67fe2SShannon Nelson kfree(ipsec->rx_tbl);
130463a67fe2SShannon Nelson kfree(ipsec->tx_tbl);
130563a67fe2SShannon Nelson kfree(ipsec);
130663a67fe2SShannon Nelson }
13078bbbc5e9SShannon Nelson }
1308