Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright(c) 2017 Oracle and/or its affiliates. All rights reserved. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   3
   4#include "ixgbe.h"
   5#include <net/xfrm.h>
   6#include <crypto/aead.h>
   7#include <linux/if_bridge.h>
   8
   9#define IXGBE_IPSEC_KEY_BITS  160
  10static const char aes_gcm_name[] = "rfc4106(gcm(aes))";
  11
  12static void ixgbe_ipsec_del_sa(struct xfrm_state *xs);
  13
  14/**
  15 * ixgbe_ipsec_set_tx_sa - set the Tx SA registers
  16 * @hw: hw specific details
  17 * @idx: register index to write
  18 * @key: key byte array
  19 * @salt: salt bytes
  20 **/
  21static void ixgbe_ipsec_set_tx_sa(struct ixgbe_hw *hw, u16 idx,
  22				  u32 key[], u32 salt)
  23{
  24	u32 reg;
  25	int i;
  26
  27	for (i = 0; i < 4; i++)
  28		IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(i),
  29				(__force u32)cpu_to_be32(key[3 - i]));
  30	IXGBE_WRITE_REG(hw, IXGBE_IPSTXSALT, (__force u32)cpu_to_be32(salt));
  31	IXGBE_WRITE_FLUSH(hw);
  32
  33	reg = IXGBE_READ_REG(hw, IXGBE_IPSTXIDX);
  34	reg &= IXGBE_RXTXIDX_IPS_EN;
  35	reg |= idx << IXGBE_RXTXIDX_IDX_SHIFT | IXGBE_RXTXIDX_WRITE;
  36	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, reg);
  37	IXGBE_WRITE_FLUSH(hw);
  38}
  39
  40/**
  41 * ixgbe_ipsec_set_rx_item - set an Rx table item
  42 * @hw: hw specific details
  43 * @idx: register index to write
  44 * @tbl: table selector
  45 *
  46 * Trigger the device to store into a particular Rx table the
  47 * data that has already been loaded into the input register
  48 **/
  49static void ixgbe_ipsec_set_rx_item(struct ixgbe_hw *hw, u16 idx,
  50				    enum ixgbe_ipsec_tbl_sel tbl)
  51{
  52	u32 reg;
  53
  54	reg = IXGBE_READ_REG(hw, IXGBE_IPSRXIDX);
  55	reg &= IXGBE_RXTXIDX_IPS_EN;
  56	reg |= tbl << IXGBE_RXIDX_TBL_SHIFT |
  57	       idx << IXGBE_RXTXIDX_IDX_SHIFT |
  58	       IXGBE_RXTXIDX_WRITE;
  59	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, reg);
  60	IXGBE_WRITE_FLUSH(hw);
  61}
  62
  63/**
  64 * ixgbe_ipsec_set_rx_sa - set up the register bits to save SA info
  65 * @hw: hw specific details
  66 * @idx: register index to write
  67 * @spi: security parameter index
  68 * @key: key byte array
  69 * @salt: salt bytes
  70 * @mode: rx decrypt control bits
  71 * @ip_idx: index into IP table for related IP address
  72 **/
  73static void ixgbe_ipsec_set_rx_sa(struct ixgbe_hw *hw, u16 idx, __be32 spi,
  74				  u32 key[], u32 salt, u32 mode, u32 ip_idx)
  75{
  76	int i;
  77
  78	/* store the SPI (in bigendian) and IPidx */
  79	IXGBE_WRITE_REG(hw, IXGBE_IPSRXSPI,
  80			(__force u32)cpu_to_le32((__force u32)spi));
  81	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPIDX, ip_idx);
  82	IXGBE_WRITE_FLUSH(hw);
  83
  84	ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_spi_tbl);
  85
  86	/* store the key, salt, and mode */
  87	for (i = 0; i < 4; i++)
  88		IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(i),
  89				(__force u32)cpu_to_be32(key[3 - i]));
  90	IXGBE_WRITE_REG(hw, IXGBE_IPSRXSALT, (__force u32)cpu_to_be32(salt));
  91	IXGBE_WRITE_REG(hw, IXGBE_IPSRXMOD, mode);
  92	IXGBE_WRITE_FLUSH(hw);
  93
  94	ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_key_tbl);
  95}
  96
  97/**
  98 * ixgbe_ipsec_set_rx_ip - set up the register bits to save SA IP addr info
  99 * @hw: hw specific details
 100 * @idx: register index to write
 101 * @addr: IP address byte array
 102 **/
 103static void ixgbe_ipsec_set_rx_ip(struct ixgbe_hw *hw, u16 idx, __be32 addr[])
 104{
 105	int i;
 106
 107	/* store the ip address */
 108	for (i = 0; i < 4; i++)
 109		IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(i),
 110				(__force u32)cpu_to_le32((__force u32)addr[i]));
 111	IXGBE_WRITE_FLUSH(hw);
 112
 113	ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_ip_tbl);
 114}
 115
 116/**
 117 * ixgbe_ipsec_clear_hw_tables - because some tables don't get cleared on reset
 118 * @adapter: board private structure
 119 **/
 120static void ixgbe_ipsec_clear_hw_tables(struct ixgbe_adapter *adapter)
 121{
 
 122	struct ixgbe_hw *hw = &adapter->hw;
 123	u32 buf[4] = {0, 0, 0, 0};
 124	u16 idx;
 125
 126	/* disable Rx and Tx SA lookup */
 127	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
 128	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
 129
 130	/* scrub the tables - split the loops for the max of the IP table */
 131	for (idx = 0; idx < IXGBE_IPSEC_MAX_RX_IP_COUNT; idx++) {
 132		ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
 133		ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
 134		ixgbe_ipsec_set_rx_ip(hw, idx, (__be32 *)buf);
 135	}
 136	for (; idx < IXGBE_IPSEC_MAX_SA_COUNT; idx++) {
 137		ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
 138		ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
 139	}
 
 
 
 140}
 141
 142/**
 143 * ixgbe_ipsec_stop_data
 144 * @adapter: board private structure
 145 **/
 146static void ixgbe_ipsec_stop_data(struct ixgbe_adapter *adapter)
 147{
 148	struct ixgbe_hw *hw = &adapter->hw;
 149	bool link = adapter->link_up;
 150	u32 t_rdy, r_rdy;
 151	u32 limit;
 152	u32 reg;
 153
 154	/* halt data paths */
 155	reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
 156	reg |= IXGBE_SECTXCTRL_TX_DIS;
 157	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
 158
 159	reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
 160	reg |= IXGBE_SECRXCTRL_RX_DIS;
 161	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
 162
 163	/* If both Tx and Rx are ready there are no packets
 164	 * that we need to flush so the loopback configuration
 165	 * below is not necessary.
 166	 */
 167	t_rdy = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
 168		IXGBE_SECTXSTAT_SECTX_RDY;
 169	r_rdy = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
 170		IXGBE_SECRXSTAT_SECRX_RDY;
 171	if (t_rdy && r_rdy)
 172		return;
 173
 174	/* If the tx fifo doesn't have link, but still has data,
 175	 * we can't clear the tx sec block.  Set the MAC loopback
 176	 * before block clear
 177	 */
 178	if (!link) {
 179		reg = IXGBE_READ_REG(hw, IXGBE_MACC);
 180		reg |= IXGBE_MACC_FLU;
 181		IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
 182
 183		reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
 184		reg |= IXGBE_HLREG0_LPBK;
 185		IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
 186
 187		IXGBE_WRITE_FLUSH(hw);
 188		mdelay(3);
 189	}
 190
 191	/* wait for the paths to empty */
 192	limit = 20;
 193	do {
 194		mdelay(10);
 195		t_rdy = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
 196			IXGBE_SECTXSTAT_SECTX_RDY;
 197		r_rdy = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
 198			IXGBE_SECRXSTAT_SECRX_RDY;
 199	} while (!(t_rdy && r_rdy) && limit--);
 200
 201	/* undo loopback if we played with it earlier */
 202	if (!link) {
 203		reg = IXGBE_READ_REG(hw, IXGBE_MACC);
 204		reg &= ~IXGBE_MACC_FLU;
 205		IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
 206
 207		reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
 208		reg &= ~IXGBE_HLREG0_LPBK;
 209		IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
 210
 211		IXGBE_WRITE_FLUSH(hw);
 212	}
 213}
 214
 215/**
 216 * ixgbe_ipsec_stop_engine
 217 * @adapter: board private structure
 218 **/
 219static void ixgbe_ipsec_stop_engine(struct ixgbe_adapter *adapter)
 220{
 221	struct ixgbe_hw *hw = &adapter->hw;
 222	u32 reg;
 223
 224	ixgbe_ipsec_stop_data(adapter);
 225
 226	/* disable Rx and Tx SA lookup */
 227	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
 228	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
 229
 230	/* disable the Rx and Tx engines and full packet store-n-forward */
 231	reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
 232	reg |= IXGBE_SECTXCTRL_SECTX_DIS;
 233	reg &= ~IXGBE_SECTXCTRL_STORE_FORWARD;
 234	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
 235
 236	reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
 237	reg |= IXGBE_SECRXCTRL_SECRX_DIS;
 238	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
 239
 240	/* restore the "tx security buffer almost full threshold" to 0x250 */
 241	IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, 0x250);
 242
 243	/* Set minimum IFG between packets back to the default 0x1 */
 244	reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
 245	reg = (reg & 0xfffffff0) | 0x1;
 246	IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
 247
 248	/* final set for normal (no ipsec offload) processing */
 249	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_SECTX_DIS);
 250	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, IXGBE_SECRXCTRL_SECRX_DIS);
 251
 252	IXGBE_WRITE_FLUSH(hw);
 253}
 254
 255/**
 256 * ixgbe_ipsec_start_engine
 257 * @adapter: board private structure
 258 *
 259 * NOTE: this increases power consumption whether being used or not
 260 **/
 261static void ixgbe_ipsec_start_engine(struct ixgbe_adapter *adapter)
 262{
 263	struct ixgbe_hw *hw = &adapter->hw;
 264	u32 reg;
 265
 266	ixgbe_ipsec_stop_data(adapter);
 267
 268	/* Set minimum IFG between packets to 3 */
 269	reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
 270	reg = (reg & 0xfffffff0) | 0x3;
 271	IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
 272
 273	/* Set "tx security buffer almost full threshold" to 0x15 so that the
 274	 * almost full indication is generated only after buffer contains at
 275	 * least an entire jumbo packet.
 276	 */
 277	reg = IXGBE_READ_REG(hw, IXGBE_SECTXBUFFAF);
 278	reg = (reg & 0xfffffc00) | 0x15;
 279	IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, reg);
 280
 281	/* restart the data paths by clearing the DISABLE bits */
 282	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, 0);
 283	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_STORE_FORWARD);
 284
 285	/* enable Rx and Tx SA lookup */
 286	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, IXGBE_RXTXIDX_IPS_EN);
 287	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, IXGBE_RXTXIDX_IPS_EN);
 288
 289	IXGBE_WRITE_FLUSH(hw);
 290}
 291
 292/**
 293 * ixgbe_ipsec_restore - restore the ipsec HW settings after a reset
 294 * @adapter: board private structure
 295 *
 296 * Reload the HW tables from the SW tables after they've been bashed
 297 * by a chip reset.
 298 *
 299 * Any VF entries are removed from the SW and HW tables since either
 300 * (a) the VF also gets reset on PF reset and will ask again for the
 301 * offloads, or (b) the VF has been removed by a change in the num_vfs.
 302 **/
 303void ixgbe_ipsec_restore(struct ixgbe_adapter *adapter)
 304{
 305	struct ixgbe_ipsec *ipsec = adapter->ipsec;
 306	struct ixgbe_hw *hw = &adapter->hw;
 307	int i;
 308
 309	if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED))
 310		return;
 311
 312	/* clean up and restart the engine */
 313	ixgbe_ipsec_stop_engine(adapter);
 314	ixgbe_ipsec_clear_hw_tables(adapter);
 315	ixgbe_ipsec_start_engine(adapter);
 316
 317	/* reload the Rx and Tx keys */
 318	for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
 319		struct rx_sa *r = &ipsec->rx_tbl[i];
 320		struct tx_sa *t = &ipsec->tx_tbl[i];
 321
 322		if (r->used) {
 323			if (r->mode & IXGBE_RXTXMOD_VF)
 324				ixgbe_ipsec_del_sa(r->xs);
 325			else
 326				ixgbe_ipsec_set_rx_sa(hw, i, r->xs->id.spi,
 327						      r->key, r->salt,
 328						      r->mode, r->iptbl_ind);
 329		}
 330
 331		if (t->used) {
 332			if (t->mode & IXGBE_RXTXMOD_VF)
 333				ixgbe_ipsec_del_sa(t->xs);
 334			else
 335				ixgbe_ipsec_set_tx_sa(hw, i, t->key, t->salt);
 336		}
 337	}
 338
 339	/* reload the IP addrs */
 340	for (i = 0; i < IXGBE_IPSEC_MAX_RX_IP_COUNT; i++) {
 341		struct rx_ip_sa *ipsa = &ipsec->ip_tbl[i];
 342
 343		if (ipsa->used)
 344			ixgbe_ipsec_set_rx_ip(hw, i, ipsa->ipaddr);
 345	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 346}
 347
 348/**
 349 * ixgbe_ipsec_find_empty_idx - find the first unused security parameter index
 350 * @ipsec: pointer to ipsec struct
 351 * @rxtable: true if we need to look in the Rx table
 352 *
 353 * Returns the first unused index in either the Rx or Tx SA table
 354 **/
 355static int ixgbe_ipsec_find_empty_idx(struct ixgbe_ipsec *ipsec, bool rxtable)
 356{
 357	u32 i;
 358
 359	if (rxtable) {
 360		if (ipsec->num_rx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
 361			return -ENOSPC;
 362
 363		/* search rx sa table */
 364		for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
 365			if (!ipsec->rx_tbl[i].used)
 366				return i;
 367		}
 368	} else {
 369		if (ipsec->num_tx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
 370			return -ENOSPC;
 371
 372		/* search tx sa table */
 373		for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
 374			if (!ipsec->tx_tbl[i].used)
 375				return i;
 376		}
 377	}
 378
 379	return -ENOSPC;
 380}
 381
 382/**
 383 * ixgbe_ipsec_find_rx_state - find the state that matches
 384 * @ipsec: pointer to ipsec struct
 385 * @daddr: inbound address to match
 386 * @proto: protocol to match
 387 * @spi: SPI to match
 388 * @ip4: true if using an ipv4 address
 389 *
 390 * Returns a pointer to the matching SA state information
 391 **/
 392static struct xfrm_state *ixgbe_ipsec_find_rx_state(struct ixgbe_ipsec *ipsec,
 393						    __be32 *daddr, u8 proto,
 394						    __be32 spi, bool ip4)
 395{
 396	struct rx_sa *rsa;
 397	struct xfrm_state *ret = NULL;
 398
 399	rcu_read_lock();
 400	hash_for_each_possible_rcu(ipsec->rx_sa_list, rsa, hlist,
 401				   (__force u32)spi) {
 402		if (rsa->mode & IXGBE_RXTXMOD_VF)
 403			continue;
 404		if (spi == rsa->xs->id.spi &&
 405		    ((ip4 && *daddr == rsa->xs->id.daddr.a4) ||
 406		      (!ip4 && !memcmp(daddr, &rsa->xs->id.daddr.a6,
 407				       sizeof(rsa->xs->id.daddr.a6)))) &&
 408		    proto == rsa->xs->id.proto) {
 409			ret = rsa->xs;
 410			xfrm_state_hold(ret);
 411			break;
 412		}
 413	}
 414	rcu_read_unlock();
 415	return ret;
 416}
 417
 418/**
 419 * ixgbe_ipsec_parse_proto_keys - find the key and salt based on the protocol
 420 * @xs: pointer to xfrm_state struct
 421 * @mykey: pointer to key array to populate
 422 * @mysalt: pointer to salt value to populate
 423 *
 424 * This copies the protocol keys and salt to our own data tables.  The
 425 * 82599 family only supports the one algorithm.
 426 **/
 427static int ixgbe_ipsec_parse_proto_keys(struct xfrm_state *xs,
 428					u32 *mykey, u32 *mysalt)
 429{
 430	struct net_device *dev = xs->xso.real_dev;
 431	unsigned char *key_data;
 432	char *alg_name = NULL;
 
 433	int key_len;
 434
 435	if (!xs->aead) {
 436		netdev_err(dev, "Unsupported IPsec algorithm\n");
 437		return -EINVAL;
 438	}
 439
 440	if (xs->aead->alg_icv_len != IXGBE_IPSEC_AUTH_BITS) {
 441		netdev_err(dev, "IPsec offload requires %d bit authentication\n",
 442			   IXGBE_IPSEC_AUTH_BITS);
 443		return -EINVAL;
 444	}
 445
 446	key_data = &xs->aead->alg_key[0];
 447	key_len = xs->aead->alg_key_len;
 448	alg_name = xs->aead->alg_name;
 449
 450	if (strcmp(alg_name, aes_gcm_name)) {
 451		netdev_err(dev, "Unsupported IPsec algorithm - please use %s\n",
 452			   aes_gcm_name);
 453		return -EINVAL;
 454	}
 455
 456	/* The key bytes come down in a bigendian array of bytes, so
 457	 * we don't need to do any byteswapping.
 458	 * 160 accounts for 16 byte key and 4 byte salt
 459	 */
 460	if (key_len == IXGBE_IPSEC_KEY_BITS) {
 461		*mysalt = ((u32 *)key_data)[4];
 462	} else if (key_len != (IXGBE_IPSEC_KEY_BITS - (sizeof(*mysalt) * 8))) {
 463		netdev_err(dev, "IPsec hw offload only supports keys up to 128 bits with a 32 bit salt\n");
 464		return -EINVAL;
 465	} else {
 466		netdev_info(dev, "IPsec hw offload parameters missing 32 bit salt value\n");
 467		*mysalt = 0;
 468	}
 469	memcpy(mykey, key_data, 16);
 470
 471	return 0;
 472}
 473
 474/**
 475 * ixgbe_ipsec_check_mgmt_ip - make sure there is no clash with mgmt IP filters
 476 * @xs: pointer to transformer state struct
 477 **/
 478static int ixgbe_ipsec_check_mgmt_ip(struct xfrm_state *xs)
 479{
 480	struct net_device *dev = xs->xso.real_dev;
 481	struct ixgbe_adapter *adapter = netdev_priv(dev);
 482	struct ixgbe_hw *hw = &adapter->hw;
 483	u32 mfval, manc, reg;
 484	int num_filters = 4;
 485	bool manc_ipv4;
 486	u32 bmcipval;
 487	int i, j;
 488
 489#define MANC_EN_IPV4_FILTER      BIT(24)
 490#define MFVAL_IPV4_FILTER_SHIFT  16
 491#define MFVAL_IPV6_FILTER_SHIFT  24
 492#define MIPAF_ARR(_m, _n)        (IXGBE_MIPAF + ((_m) * 0x10) + ((_n) * 4))
 493
 494#define IXGBE_BMCIP(_n)          (0x5050 + ((_n) * 4))
 495#define IXGBE_BMCIPVAL           0x5060
 496#define BMCIP_V4                 0x2
 497#define BMCIP_V6                 0x3
 498#define BMCIP_MASK               0x3
 499
 500	manc = IXGBE_READ_REG(hw, IXGBE_MANC);
 501	manc_ipv4 = !!(manc & MANC_EN_IPV4_FILTER);
 502	mfval = IXGBE_READ_REG(hw, IXGBE_MFVAL);
 503	bmcipval = IXGBE_READ_REG(hw, IXGBE_BMCIPVAL);
 504
 505	if (xs->props.family == AF_INET) {
 506		/* are there any IPv4 filters to check? */
 507		if (manc_ipv4) {
 508			/* the 4 ipv4 filters are all in MIPAF(3, i) */
 509			for (i = 0; i < num_filters; i++) {
 510				if (!(mfval & BIT(MFVAL_IPV4_FILTER_SHIFT + i)))
 511					continue;
 512
 513				reg = IXGBE_READ_REG(hw, MIPAF_ARR(3, i));
 514				if (reg == (__force u32)xs->id.daddr.a4)
 515					return 1;
 516			}
 517		}
 518
 519		if ((bmcipval & BMCIP_MASK) == BMCIP_V4) {
 520			reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(3));
 521			if (reg == (__force u32)xs->id.daddr.a4)
 522				return 1;
 523		}
 524
 525	} else {
 526		/* if there are ipv4 filters, they are in the last ipv6 slot */
 527		if (manc_ipv4)
 528			num_filters = 3;
 529
 530		for (i = 0; i < num_filters; i++) {
 531			if (!(mfval & BIT(MFVAL_IPV6_FILTER_SHIFT + i)))
 532				continue;
 533
 534			for (j = 0; j < 4; j++) {
 535				reg = IXGBE_READ_REG(hw, MIPAF_ARR(i, j));
 536				if (reg != (__force u32)xs->id.daddr.a6[j])
 537					break;
 538			}
 539			if (j == 4)   /* did we match all 4 words? */
 540				return 1;
 541		}
 542
 543		if ((bmcipval & BMCIP_MASK) == BMCIP_V6) {
 544			for (j = 0; j < 4; j++) {
 545				reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(j));
 546				if (reg != (__force u32)xs->id.daddr.a6[j])
 547					break;
 548			}
 549			if (j == 4)   /* did we match all 4 words? */
 550				return 1;
 551		}
 552	}
 553
 554	return 0;
 555}
 556
 557/**
 558 * ixgbe_ipsec_add_sa - program device with a security association
 559 * @xs: pointer to transformer state struct
 560 * @extack: extack point to fill failure reason
 561 **/
 562static int ixgbe_ipsec_add_sa(struct xfrm_state *xs,
 563			      struct netlink_ext_ack *extack)
 564{
 565	struct net_device *dev = xs->xso.real_dev;
 566	struct ixgbe_adapter *adapter = netdev_priv(dev);
 567	struct ixgbe_ipsec *ipsec = adapter->ipsec;
 568	struct ixgbe_hw *hw = &adapter->hw;
 569	int checked, match, first;
 570	u16 sa_idx;
 571	int ret;
 572	int i;
 573
 574	if (xs->id.proto != IPPROTO_ESP && xs->id.proto != IPPROTO_AH) {
 575		NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol for ipsec offload");
 
 576		return -EINVAL;
 577	}
 578
 579	if (xs->props.mode != XFRM_MODE_TRANSPORT) {
 580		NL_SET_ERR_MSG_MOD(extack, "Unsupported mode for ipsec offload");
 581		return -EINVAL;
 582	}
 583
 584	if (ixgbe_ipsec_check_mgmt_ip(xs)) {
 585		NL_SET_ERR_MSG_MOD(extack, "IPsec IP addr clash with mgmt filters");
 586		return -EINVAL;
 587	}
 588
 589	if (xs->xso.type != XFRM_DEV_OFFLOAD_CRYPTO) {
 590		NL_SET_ERR_MSG_MOD(extack, "Unsupported ipsec offload type");
 591		return -EINVAL;
 592	}
 593
 594	if (xs->xso.dir == XFRM_DEV_OFFLOAD_IN) {
 595		struct rx_sa rsa;
 596
 597		if (xs->calg) {
 598			NL_SET_ERR_MSG_MOD(extack, "Compression offload not supported");
 599			return -EINVAL;
 600		}
 601
 602		/* find the first unused index */
 603		ret = ixgbe_ipsec_find_empty_idx(ipsec, true);
 604		if (ret < 0) {
 605			NL_SET_ERR_MSG_MOD(extack, "No space for SA in Rx table!");
 606			return ret;
 607		}
 608		sa_idx = (u16)ret;
 609
 610		memset(&rsa, 0, sizeof(rsa));
 611		rsa.used = true;
 612		rsa.xs = xs;
 613
 614		if (rsa.xs->id.proto & IPPROTO_ESP)
 615			rsa.decrypt = xs->ealg || xs->aead;
 616
 617		/* get the key and salt */
 618		ret = ixgbe_ipsec_parse_proto_keys(xs, rsa.key, &rsa.salt);
 619		if (ret) {
 620			NL_SET_ERR_MSG_MOD(extack, "Failed to get key data for Rx SA table");
 621			return ret;
 622		}
 623
 624		/* get ip for rx sa table */
 625		if (xs->props.family == AF_INET6)
 626			memcpy(rsa.ipaddr, &xs->id.daddr.a6, 16);
 627		else
 628			memcpy(&rsa.ipaddr[3], &xs->id.daddr.a4, 4);
 629
 630		/* The HW does not have a 1:1 mapping from keys to IP addrs, so
 631		 * check for a matching IP addr entry in the table.  If the addr
 632		 * already exists, use it; else find an unused slot and add the
 633		 * addr.  If one does not exist and there are no unused table
 634		 * entries, fail the request.
 635		 */
 636
 637		/* Find an existing match or first not used, and stop looking
 638		 * after we've checked all we know we have.
 639		 */
 640		checked = 0;
 641		match = -1;
 642		first = -1;
 643		for (i = 0;
 644		     i < IXGBE_IPSEC_MAX_RX_IP_COUNT &&
 645		     (checked < ipsec->num_rx_sa || first < 0);
 646		     i++) {
 647			if (ipsec->ip_tbl[i].used) {
 648				if (!memcmp(ipsec->ip_tbl[i].ipaddr,
 649					    rsa.ipaddr, sizeof(rsa.ipaddr))) {
 650					match = i;
 651					break;
 652				}
 653				checked++;
 654			} else if (first < 0) {
 655				first = i;  /* track the first empty seen */
 656			}
 657		}
 658
 659		if (ipsec->num_rx_sa == 0)
 660			first = 0;
 661
 662		if (match >= 0) {
 663			/* addrs are the same, we should use this one */
 664			rsa.iptbl_ind = match;
 665			ipsec->ip_tbl[match].ref_cnt++;
 666
 667		} else if (first >= 0) {
 668			/* no matches, but here's an empty slot */
 669			rsa.iptbl_ind = first;
 670
 671			memcpy(ipsec->ip_tbl[first].ipaddr,
 672			       rsa.ipaddr, sizeof(rsa.ipaddr));
 673			ipsec->ip_tbl[first].ref_cnt = 1;
 674			ipsec->ip_tbl[first].used = true;
 675
 676			ixgbe_ipsec_set_rx_ip(hw, rsa.iptbl_ind, rsa.ipaddr);
 677
 678		} else {
 679			/* no match and no empty slot */
 680			NL_SET_ERR_MSG_MOD(extack, "No space for SA in Rx IP SA table");
 681			memset(&rsa, 0, sizeof(rsa));
 682			return -ENOSPC;
 683		}
 684
 685		rsa.mode = IXGBE_RXMOD_VALID;
 686		if (rsa.xs->id.proto & IPPROTO_ESP)
 687			rsa.mode |= IXGBE_RXMOD_PROTO_ESP;
 688		if (rsa.decrypt)
 689			rsa.mode |= IXGBE_RXMOD_DECRYPT;
 690		if (rsa.xs->props.family == AF_INET6)
 691			rsa.mode |= IXGBE_RXMOD_IPV6;
 692
 693		/* the preparations worked, so save the info */
 694		memcpy(&ipsec->rx_tbl[sa_idx], &rsa, sizeof(rsa));
 695
 696		ixgbe_ipsec_set_rx_sa(hw, sa_idx, rsa.xs->id.spi, rsa.key,
 697				      rsa.salt, rsa.mode, rsa.iptbl_ind);
 698		xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_RX_INDEX;
 699
 700		ipsec->num_rx_sa++;
 701
 702		/* hash the new entry for faster search in Rx path */
 703		hash_add_rcu(ipsec->rx_sa_list, &ipsec->rx_tbl[sa_idx].hlist,
 704			     (__force u32)rsa.xs->id.spi);
 705	} else {
 706		struct tx_sa tsa;
 707
 708		if (adapter->num_vfs &&
 709		    adapter->bridge_mode != BRIDGE_MODE_VEPA)
 710			return -EOPNOTSUPP;
 711
 712		/* find the first unused index */
 713		ret = ixgbe_ipsec_find_empty_idx(ipsec, false);
 714		if (ret < 0) {
 715			NL_SET_ERR_MSG_MOD(extack, "No space for SA in Tx table");
 716			return ret;
 717		}
 718		sa_idx = (u16)ret;
 719
 720		memset(&tsa, 0, sizeof(tsa));
 721		tsa.used = true;
 722		tsa.xs = xs;
 723
 724		if (xs->id.proto & IPPROTO_ESP)
 725			tsa.encrypt = xs->ealg || xs->aead;
 726
 727		ret = ixgbe_ipsec_parse_proto_keys(xs, tsa.key, &tsa.salt);
 728		if (ret) {
 729			NL_SET_ERR_MSG_MOD(extack, "Failed to get key data for Tx SA table");
 730			memset(&tsa, 0, sizeof(tsa));
 731			return ret;
 732		}
 733
 734		/* the preparations worked, so save the info */
 735		memcpy(&ipsec->tx_tbl[sa_idx], &tsa, sizeof(tsa));
 736
 737		ixgbe_ipsec_set_tx_sa(hw, sa_idx, tsa.key, tsa.salt);
 738
 739		xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_TX_INDEX;
 740
 741		ipsec->num_tx_sa++;
 742	}
 743
 744	/* enable the engine if not already warmed up */
 745	if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED)) {
 746		ixgbe_ipsec_start_engine(adapter);
 747		adapter->flags2 |= IXGBE_FLAG2_IPSEC_ENABLED;
 748	}
 749
 750	return 0;
 751}
 752
 753/**
 754 * ixgbe_ipsec_del_sa - clear out this specific SA
 755 * @xs: pointer to transformer state struct
 756 **/
 757static void ixgbe_ipsec_del_sa(struct xfrm_state *xs)
 758{
 759	struct net_device *dev = xs->xso.real_dev;
 760	struct ixgbe_adapter *adapter = netdev_priv(dev);
 761	struct ixgbe_ipsec *ipsec = adapter->ipsec;
 762	struct ixgbe_hw *hw = &adapter->hw;
 763	u32 zerobuf[4] = {0, 0, 0, 0};
 764	u16 sa_idx;
 765
 766	if (xs->xso.dir == XFRM_DEV_OFFLOAD_IN) {
 767		struct rx_sa *rsa;
 768		u8 ipi;
 769
 770		sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_RX_INDEX;
 771		rsa = &ipsec->rx_tbl[sa_idx];
 772
 773		if (!rsa->used) {
 774			netdev_err(dev, "Invalid Rx SA selected sa_idx=%d offload_handle=%lu\n",
 775				   sa_idx, xs->xso.offload_handle);
 776			return;
 777		}
 778
 779		ixgbe_ipsec_set_rx_sa(hw, sa_idx, 0, zerobuf, 0, 0, 0);
 780		hash_del_rcu(&rsa->hlist);
 781
 782		/* if the IP table entry is referenced by only this SA,
 783		 * i.e. ref_cnt is only 1, clear the IP table entry as well
 784		 */
 785		ipi = rsa->iptbl_ind;
 786		if (ipsec->ip_tbl[ipi].ref_cnt > 0) {
 787			ipsec->ip_tbl[ipi].ref_cnt--;
 788
 789			if (!ipsec->ip_tbl[ipi].ref_cnt) {
 790				memset(&ipsec->ip_tbl[ipi], 0,
 791				       sizeof(struct rx_ip_sa));
 792				ixgbe_ipsec_set_rx_ip(hw, ipi,
 793						      (__force __be32 *)zerobuf);
 794			}
 795		}
 796
 797		memset(rsa, 0, sizeof(struct rx_sa));
 798		ipsec->num_rx_sa--;
 799	} else {
 800		sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
 801
 802		if (!ipsec->tx_tbl[sa_idx].used) {
 803			netdev_err(dev, "Invalid Tx SA selected sa_idx=%d offload_handle=%lu\n",
 804				   sa_idx, xs->xso.offload_handle);
 805			return;
 806		}
 807
 808		ixgbe_ipsec_set_tx_sa(hw, sa_idx, zerobuf, 0);
 809		memset(&ipsec->tx_tbl[sa_idx], 0, sizeof(struct tx_sa));
 810		ipsec->num_tx_sa--;
 811	}
 812
 813	/* if there are no SAs left, stop the engine to save energy */
 814	if (ipsec->num_rx_sa == 0 && ipsec->num_tx_sa == 0) {
 815		adapter->flags2 &= ~IXGBE_FLAG2_IPSEC_ENABLED;
 816		ixgbe_ipsec_stop_engine(adapter);
 817	}
 818}
 819
 820/**
 821 * ixgbe_ipsec_offload_ok - can this packet use the xfrm hw offload
 822 * @skb: current data packet
 823 * @xs: pointer to transformer state struct
 824 **/
 825static bool ixgbe_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
 826{
 827	if (xs->props.family == AF_INET) {
 828		/* Offload with IPv4 options is not supported yet */
 829		if (ip_hdr(skb)->ihl != 5)
 830			return false;
 831	} else {
 832		/* Offload with IPv6 extension headers is not support yet */
 833		if (ipv6_ext_hdr(ipv6_hdr(skb)->nexthdr))
 834			return false;
 835	}
 836
 837	return true;
 838}
 839
 840static const struct xfrmdev_ops ixgbe_xfrmdev_ops = {
 841	.xdo_dev_state_add = ixgbe_ipsec_add_sa,
 842	.xdo_dev_state_delete = ixgbe_ipsec_del_sa,
 843	.xdo_dev_offload_ok = ixgbe_ipsec_offload_ok,
 844};
 845
 846/**
 847 * ixgbe_ipsec_vf_clear - clear the tables of data for a VF
 848 * @adapter: board private structure
 849 * @vf: VF id to be removed
 850 **/
 851void ixgbe_ipsec_vf_clear(struct ixgbe_adapter *adapter, u32 vf)
 852{
 853	struct ixgbe_ipsec *ipsec = adapter->ipsec;
 854	int i;
 855
 856	if (!ipsec)
 857		return;
 858
 859	/* search rx sa table */
 860	for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_rx_sa; i++) {
 861		if (!ipsec->rx_tbl[i].used)
 862			continue;
 863		if (ipsec->rx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
 864		    ipsec->rx_tbl[i].vf == vf)
 865			ixgbe_ipsec_del_sa(ipsec->rx_tbl[i].xs);
 866	}
 867
 868	/* search tx sa table */
 869	for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_tx_sa; i++) {
 870		if (!ipsec->tx_tbl[i].used)
 871			continue;
 872		if (ipsec->tx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
 873		    ipsec->tx_tbl[i].vf == vf)
 874			ixgbe_ipsec_del_sa(ipsec->tx_tbl[i].xs);
 875	}
 876}
 877
 878/**
 879 * ixgbe_ipsec_vf_add_sa - translate VF request to SA add
 880 * @adapter: board private structure
 881 * @msgbuf: The message buffer
 882 * @vf: the VF index
 883 *
 884 * Make up a new xs and algorithm info from the data sent by the VF.
 885 * We only need to sketch in just enough to set up the HW offload.
 886 * Put the resulting offload_handle into the return message to the VF.
 887 *
 888 * Returns 0 or error value
 889 **/
 890int ixgbe_ipsec_vf_add_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
 891{
 892	struct ixgbe_ipsec *ipsec = adapter->ipsec;
 893	struct xfrm_algo_desc *algo;
 894	struct sa_mbx_msg *sam;
 895	struct xfrm_state *xs;
 896	size_t aead_len;
 897	u16 sa_idx;
 898	u32 pfsa;
 899	int err;
 900
 901	sam = (struct sa_mbx_msg *)(&msgbuf[1]);
 902	if (!adapter->vfinfo[vf].trusted ||
 903	    !(adapter->flags2 & IXGBE_FLAG2_VF_IPSEC_ENABLED)) {
 904		e_warn(drv, "VF %d attempted to add an IPsec SA\n", vf);
 905		err = -EACCES;
 906		goto err_out;
 907	}
 908
 909	/* Tx IPsec offload doesn't seem to work on this
 910	 * device, so block these requests for now.
 911	 */
 912	if (sam->dir != XFRM_DEV_OFFLOAD_IN) {
 913		err = -EOPNOTSUPP;
 914		goto err_out;
 915	}
 916
 917	algo = xfrm_aead_get_byname(aes_gcm_name, IXGBE_IPSEC_AUTH_BITS, 1);
 918	if (unlikely(!algo)) {
 919		err = -ENOENT;
 920		goto err_out;
 921	}
 922
 923	xs = kzalloc(sizeof(*xs), GFP_ATOMIC);
 924	if (unlikely(!xs)) {
 925		err = -ENOMEM;
 926		goto err_out;
 927	}
 928
 929	xs->xso.dir = sam->dir;
 930	xs->id.spi = sam->spi;
 931	xs->id.proto = sam->proto;
 932	xs->props.family = sam->family;
 933	if (xs->props.family == AF_INET6)
 934		memcpy(&xs->id.daddr.a6, sam->addr, sizeof(xs->id.daddr.a6));
 935	else
 936		memcpy(&xs->id.daddr.a4, sam->addr, sizeof(xs->id.daddr.a4));
 937	xs->xso.dev = adapter->netdev;
 938
 939	aead_len = sizeof(*xs->aead) + IXGBE_IPSEC_KEY_BITS / 8;
 940	xs->aead = kzalloc(aead_len, GFP_ATOMIC);
 941	if (unlikely(!xs->aead)) {
 942		err = -ENOMEM;
 943		goto err_xs;
 944	}
 945
 946	xs->props.ealgo = algo->desc.sadb_alg_id;
 947	xs->geniv = algo->uinfo.aead.geniv;
 948	xs->aead->alg_icv_len = IXGBE_IPSEC_AUTH_BITS;
 949	xs->aead->alg_key_len = IXGBE_IPSEC_KEY_BITS;
 950	memcpy(xs->aead->alg_key, sam->key, sizeof(sam->key));
 951	memcpy(xs->aead->alg_name, aes_gcm_name, sizeof(aes_gcm_name));
 952
 953	/* set up the HW offload */
 954	err = ixgbe_ipsec_add_sa(xs, NULL);
 955	if (err)
 956		goto err_aead;
 957
 958	pfsa = xs->xso.offload_handle;
 959	if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
 960		sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
 961		ipsec->rx_tbl[sa_idx].vf = vf;
 962		ipsec->rx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
 963	} else {
 964		sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
 965		ipsec->tx_tbl[sa_idx].vf = vf;
 966		ipsec->tx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
 967	}
 968
 969	msgbuf[1] = xs->xso.offload_handle;
 970
 971	return 0;
 972
 973err_aead:
 974	kfree_sensitive(xs->aead);
 975err_xs:
 976	kfree_sensitive(xs);
 977err_out:
 978	msgbuf[1] = err;
 979	return err;
 980}
 981
 982/**
 983 * ixgbe_ipsec_vf_del_sa - translate VF request to SA delete
 984 * @adapter: board private structure
 985 * @msgbuf: The message buffer
 986 * @vf: the VF index
 987 *
 988 * Given the offload_handle sent by the VF, look for the related SA table
 989 * entry and use its xs field to call for a delete of the SA.
 990 *
 991 * Note: We silently ignore requests to delete entries that are already
 992 *       set to unused because when a VF is set to "DOWN", the PF first
 993 *       gets a reset and clears all the VF's entries; then the VF's
 994 *       XFRM stack sends individual deletes for each entry, which the
 995 *       reset already removed.  In the future it might be good to try to
 996 *       optimize this so not so many unnecessary delete messages are sent.
 997 *
 998 * Returns 0 or error value
 999 **/
1000int ixgbe_ipsec_vf_del_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
1001{
1002	struct ixgbe_ipsec *ipsec = adapter->ipsec;
1003	struct xfrm_state *xs;
1004	u32 pfsa = msgbuf[1];
1005	u16 sa_idx;
1006
1007	if (!adapter->vfinfo[vf].trusted) {
1008		e_err(drv, "vf %d attempted to delete an SA\n", vf);
1009		return -EPERM;
1010	}
1011
1012	if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
1013		struct rx_sa *rsa;
1014
1015		sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
1016		if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
1017			e_err(drv, "vf %d SA index %d out of range\n",
1018			      vf, sa_idx);
1019			return -EINVAL;
1020		}
1021
1022		rsa = &ipsec->rx_tbl[sa_idx];
1023
1024		if (!rsa->used)
1025			return 0;
1026
1027		if (!(rsa->mode & IXGBE_RXTXMOD_VF) ||
1028		    rsa->vf != vf) {
1029			e_err(drv, "vf %d bad Rx SA index %d\n", vf, sa_idx);
1030			return -ENOENT;
1031		}
1032
1033		xs = ipsec->rx_tbl[sa_idx].xs;
1034	} else {
1035		struct tx_sa *tsa;
1036
1037		sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
1038		if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
1039			e_err(drv, "vf %d SA index %d out of range\n",
1040			      vf, sa_idx);
1041			return -EINVAL;
1042		}
1043
1044		tsa = &ipsec->tx_tbl[sa_idx];
1045
1046		if (!tsa->used)
1047			return 0;
1048
1049		if (!(tsa->mode & IXGBE_RXTXMOD_VF) ||
1050		    tsa->vf != vf) {
1051			e_err(drv, "vf %d bad Tx SA index %d\n", vf, sa_idx);
1052			return -ENOENT;
1053		}
1054
1055		xs = ipsec->tx_tbl[sa_idx].xs;
1056	}
1057
1058	ixgbe_ipsec_del_sa(xs);
1059
1060	/* remove the xs that was made-up in the add request */
1061	kfree_sensitive(xs);
1062
1063	return 0;
1064}
1065
1066/**
1067 * ixgbe_ipsec_tx - setup Tx flags for ipsec offload
1068 * @tx_ring: outgoing context
1069 * @first: current data packet
1070 * @itd: ipsec Tx data for later use in building context descriptor
1071 **/
1072int ixgbe_ipsec_tx(struct ixgbe_ring *tx_ring,
1073		   struct ixgbe_tx_buffer *first,
1074		   struct ixgbe_ipsec_tx_data *itd)
1075{
1076	struct ixgbe_adapter *adapter = netdev_priv(tx_ring->netdev);
1077	struct ixgbe_ipsec *ipsec = adapter->ipsec;
1078	struct xfrm_state *xs;
1079	struct sec_path *sp;
1080	struct tx_sa *tsa;
1081
1082	sp = skb_sec_path(first->skb);
1083	if (unlikely(!sp->len)) {
1084		netdev_err(tx_ring->netdev, "%s: no xfrm state len = %d\n",
1085			   __func__, sp->len);
1086		return 0;
1087	}
1088
1089	xs = xfrm_input_state(first->skb);
1090	if (unlikely(!xs)) {
1091		netdev_err(tx_ring->netdev, "%s: no xfrm_input_state() xs = %p\n",
1092			   __func__, xs);
1093		return 0;
1094	}
1095
1096	itd->sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
1097	if (unlikely(itd->sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT)) {
1098		netdev_err(tx_ring->netdev, "%s: bad sa_idx=%d handle=%lu\n",
1099			   __func__, itd->sa_idx, xs->xso.offload_handle);
1100		return 0;
1101	}
1102
1103	tsa = &ipsec->tx_tbl[itd->sa_idx];
1104	if (unlikely(!tsa->used)) {
1105		netdev_err(tx_ring->netdev, "%s: unused sa_idx=%d\n",
1106			   __func__, itd->sa_idx);
1107		return 0;
1108	}
1109
1110	first->tx_flags |= IXGBE_TX_FLAGS_IPSEC | IXGBE_TX_FLAGS_CC;
1111
1112	if (xs->id.proto == IPPROTO_ESP) {
1113
1114		itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_TYPE_ESP |
1115			      IXGBE_ADVTXD_TUCMD_L4T_TCP;
1116		if (first->protocol == htons(ETH_P_IP))
1117			itd->flags |= IXGBE_ADVTXD_TUCMD_IPV4;
1118
1119		/* The actual trailer length is authlen (16 bytes) plus
1120		 * 2 bytes for the proto and the padlen values, plus
1121		 * padlen bytes of padding.  This ends up not the same
1122		 * as the static value found in xs->props.trailer_len (21).
1123		 *
1124		 * ... but if we're doing GSO, don't bother as the stack
1125		 * doesn't add a trailer for those.
1126		 */
1127		if (!skb_is_gso(first->skb)) {
1128			/* The "correct" way to get the auth length would be
1129			 * to use
1130			 *    authlen = crypto_aead_authsize(xs->data);
1131			 * but since we know we only have one size to worry
1132			 * about * we can let the compiler use the constant
1133			 * and save us a few CPU cycles.
1134			 */
1135			const int authlen = IXGBE_IPSEC_AUTH_BITS / 8;
1136			struct sk_buff *skb = first->skb;
1137			u8 padlen;
1138			int ret;
1139
1140			ret = skb_copy_bits(skb, skb->len - (authlen + 2),
1141					    &padlen, 1);
1142			if (unlikely(ret))
1143				return 0;
1144			itd->trailer_len = authlen + 2 + padlen;
1145		}
1146	}
1147	if (tsa->encrypt)
1148		itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_ENCRYPT_EN;
1149
1150	return 1;
1151}
1152
1153/**
1154 * ixgbe_ipsec_rx - decode ipsec bits from Rx descriptor
1155 * @rx_ring: receiving ring
1156 * @rx_desc: receive data descriptor
1157 * @skb: current data packet
1158 *
1159 * Determine if there was an ipsec encapsulation noticed, and if so set up
1160 * the resulting status for later in the receive stack.
1161 **/
1162void ixgbe_ipsec_rx(struct ixgbe_ring *rx_ring,
1163		    union ixgbe_adv_rx_desc *rx_desc,
1164		    struct sk_buff *skb)
1165{
1166	struct ixgbe_adapter *adapter = netdev_priv(rx_ring->netdev);
1167	__le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1168	__le16 ipsec_pkt_types = cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH |
1169					     IXGBE_RXDADV_PKTTYPE_IPSEC_ESP);
1170	struct ixgbe_ipsec *ipsec = adapter->ipsec;
1171	struct xfrm_offload *xo = NULL;
1172	struct xfrm_state *xs = NULL;
1173	struct ipv6hdr *ip6 = NULL;
1174	struct iphdr *ip4 = NULL;
1175	struct sec_path *sp;
1176	void *daddr;
1177	__be32 spi;
1178	u8 *c_hdr;
1179	u8 proto;
1180
1181	/* Find the ip and crypto headers in the data.
1182	 * We can assume no vlan header in the way, b/c the
1183	 * hw won't recognize the IPsec packet and anyway the
1184	 * currently vlan device doesn't support xfrm offload.
1185	 */
1186	if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV4)) {
1187		ip4 = (struct iphdr *)(skb->data + ETH_HLEN);
1188		daddr = &ip4->daddr;
1189		c_hdr = (u8 *)ip4 + ip4->ihl * 4;
1190	} else if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV6)) {
1191		ip6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
1192		daddr = &ip6->daddr;
1193		c_hdr = (u8 *)ip6 + sizeof(struct ipv6hdr);
1194	} else {
1195		return;
1196	}
1197
1198	switch (pkt_info & ipsec_pkt_types) {
1199	case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH):
1200		spi = ((struct ip_auth_hdr *)c_hdr)->spi;
1201		proto = IPPROTO_AH;
1202		break;
1203	case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_ESP):
1204		spi = ((struct ip_esp_hdr *)c_hdr)->spi;
1205		proto = IPPROTO_ESP;
1206		break;
1207	default:
1208		return;
1209	}
1210
1211	xs = ixgbe_ipsec_find_rx_state(ipsec, daddr, proto, spi, !!ip4);
1212	if (unlikely(!xs))
1213		return;
1214
1215	sp = secpath_set(skb);
1216	if (unlikely(!sp))
1217		return;
1218
1219	sp->xvec[sp->len++] = xs;
1220	sp->olen++;
1221	xo = xfrm_offload(skb);
1222	xo->flags = CRYPTO_DONE;
1223	xo->status = CRYPTO_SUCCESS;
1224
1225	adapter->rx_ipsec++;
1226}
1227
1228/**
1229 * ixgbe_init_ipsec_offload - initialize security registers for IPSec operation
1230 * @adapter: board private structure
1231 **/
1232void ixgbe_init_ipsec_offload(struct ixgbe_adapter *adapter)
1233{
1234	struct ixgbe_hw *hw = &adapter->hw;
1235	struct ixgbe_ipsec *ipsec;
1236	u32 t_dis, r_dis;
1237	size_t size;
1238
1239	if (hw->mac.type == ixgbe_mac_82598EB)
1240		return;
1241
1242	/* If there is no support for either Tx or Rx offload
1243	 * we should not be advertising support for IPsec.
1244	 */
1245	t_dis = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
1246		IXGBE_SECTXSTAT_SECTX_OFF_DIS;
1247	r_dis = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
1248		IXGBE_SECRXSTAT_SECRX_OFF_DIS;
1249	if (t_dis || r_dis)
1250		return;
1251
1252	ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
1253	if (!ipsec)
1254		goto err1;
1255	hash_init(ipsec->rx_sa_list);
1256
1257	size = sizeof(struct rx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
1258	ipsec->rx_tbl = kzalloc(size, GFP_KERNEL);
1259	if (!ipsec->rx_tbl)
1260		goto err2;
1261
1262	size = sizeof(struct tx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
1263	ipsec->tx_tbl = kzalloc(size, GFP_KERNEL);
1264	if (!ipsec->tx_tbl)
1265		goto err2;
1266
1267	size = sizeof(struct rx_ip_sa) * IXGBE_IPSEC_MAX_RX_IP_COUNT;
1268	ipsec->ip_tbl = kzalloc(size, GFP_KERNEL);
1269	if (!ipsec->ip_tbl)
1270		goto err2;
1271
1272	ipsec->num_rx_sa = 0;
1273	ipsec->num_tx_sa = 0;
1274
1275	adapter->ipsec = ipsec;
1276	ixgbe_ipsec_stop_engine(adapter);
1277	ixgbe_ipsec_clear_hw_tables(adapter);
1278
1279	adapter->netdev->xfrmdev_ops = &ixgbe_xfrmdev_ops;
 
 
 
 
 
 
 
1280
1281	return;
1282
1283err2:
1284	kfree(ipsec->ip_tbl);
1285	kfree(ipsec->rx_tbl);
1286	kfree(ipsec->tx_tbl);
1287	kfree(ipsec);
1288err1:
1289	netdev_err(adapter->netdev, "Unable to allocate memory for SA tables");
1290}
1291
1292/**
1293 * ixgbe_stop_ipsec_offload - tear down the ipsec offload
1294 * @adapter: board private structure
1295 **/
1296void ixgbe_stop_ipsec_offload(struct ixgbe_adapter *adapter)
1297{
1298	struct ixgbe_ipsec *ipsec = adapter->ipsec;
1299
1300	adapter->ipsec = NULL;
1301	if (ipsec) {
1302		kfree(ipsec->ip_tbl);
1303		kfree(ipsec->rx_tbl);
1304		kfree(ipsec->tx_tbl);
1305		kfree(ipsec);
1306	}
1307}
v4.17
  1/*******************************************************************************
  2 *
  3 * Intel 10 Gigabit PCI Express Linux driver
  4 * Copyright(c) 2017 Oracle and/or its affiliates. All rights reserved.
  5 *
  6 * This program is free software; you can redistribute it and/or modify it
  7 * under the terms and conditions of the GNU General Public License,
  8 * version 2, as published by the Free Software Foundation.
  9 *
 10 * This program is distributed in the hope it will be useful, but WITHOUT
 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 13 * more details.
 14 *
 15 * You should have received a copy of the GNU General Public License along with
 16 * this program.  If not, see <http://www.gnu.org/licenses/>.
 17 *
 18 * The full GNU General Public License is included in this distribution in
 19 * the file called "COPYING".
 20 *
 21 * Contact Information:
 22 * Linux NICS <linux.nics@intel.com>
 23 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
 24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 25 *
 26 ******************************************************************************/
 27
 28#include "ixgbe.h"
 29#include <net/xfrm.h>
 30#include <crypto/aead.h>
 
 
 
 
 
 
 31
 32/**
 33 * ixgbe_ipsec_set_tx_sa - set the Tx SA registers
 34 * @hw: hw specific details
 35 * @idx: register index to write
 36 * @key: key byte array
 37 * @salt: salt bytes
 38 **/
 39static void ixgbe_ipsec_set_tx_sa(struct ixgbe_hw *hw, u16 idx,
 40				  u32 key[], u32 salt)
 41{
 42	u32 reg;
 43	int i;
 44
 45	for (i = 0; i < 4; i++)
 46		IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(i), cpu_to_be32(key[3 - i]));
 47	IXGBE_WRITE_REG(hw, IXGBE_IPSTXSALT, cpu_to_be32(salt));
 
 48	IXGBE_WRITE_FLUSH(hw);
 49
 50	reg = IXGBE_READ_REG(hw, IXGBE_IPSTXIDX);
 51	reg &= IXGBE_RXTXIDX_IPS_EN;
 52	reg |= idx << IXGBE_RXTXIDX_IDX_SHIFT | IXGBE_RXTXIDX_WRITE;
 53	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, reg);
 54	IXGBE_WRITE_FLUSH(hw);
 55}
 56
 57/**
 58 * ixgbe_ipsec_set_rx_item - set an Rx table item
 59 * @hw: hw specific details
 60 * @idx: register index to write
 61 * @tbl: table selector
 62 *
 63 * Trigger the device to store into a particular Rx table the
 64 * data that has already been loaded into the input register
 65 **/
 66static void ixgbe_ipsec_set_rx_item(struct ixgbe_hw *hw, u16 idx,
 67				    enum ixgbe_ipsec_tbl_sel tbl)
 68{
 69	u32 reg;
 70
 71	reg = IXGBE_READ_REG(hw, IXGBE_IPSRXIDX);
 72	reg &= IXGBE_RXTXIDX_IPS_EN;
 73	reg |= tbl << IXGBE_RXIDX_TBL_SHIFT |
 74	       idx << IXGBE_RXTXIDX_IDX_SHIFT |
 75	       IXGBE_RXTXIDX_WRITE;
 76	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, reg);
 77	IXGBE_WRITE_FLUSH(hw);
 78}
 79
 80/**
 81 * ixgbe_ipsec_set_rx_sa - set up the register bits to save SA info
 82 * @hw: hw specific details
 83 * @idx: register index to write
 84 * @spi: security parameter index
 85 * @key: key byte array
 86 * @salt: salt bytes
 87 * @mode: rx decrypt control bits
 88 * @ip_idx: index into IP table for related IP address
 89 **/
 90static void ixgbe_ipsec_set_rx_sa(struct ixgbe_hw *hw, u16 idx, __be32 spi,
 91				  u32 key[], u32 salt, u32 mode, u32 ip_idx)
 92{
 93	int i;
 94
 95	/* store the SPI (in bigendian) and IPidx */
 96	IXGBE_WRITE_REG(hw, IXGBE_IPSRXSPI, cpu_to_le32(spi));
 
 97	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPIDX, ip_idx);
 98	IXGBE_WRITE_FLUSH(hw);
 99
100	ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_spi_tbl);
101
102	/* store the key, salt, and mode */
103	for (i = 0; i < 4; i++)
104		IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(i), cpu_to_be32(key[3 - i]));
105	IXGBE_WRITE_REG(hw, IXGBE_IPSRXSALT, cpu_to_be32(salt));
 
106	IXGBE_WRITE_REG(hw, IXGBE_IPSRXMOD, mode);
107	IXGBE_WRITE_FLUSH(hw);
108
109	ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_key_tbl);
110}
111
112/**
113 * ixgbe_ipsec_set_rx_ip - set up the register bits to save SA IP addr info
114 * @hw: hw specific details
115 * @idx: register index to write
116 * @addr: IP address byte array
117 **/
118static void ixgbe_ipsec_set_rx_ip(struct ixgbe_hw *hw, u16 idx, __be32 addr[])
119{
120	int i;
121
122	/* store the ip address */
123	for (i = 0; i < 4; i++)
124		IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(i), cpu_to_le32(addr[i]));
 
125	IXGBE_WRITE_FLUSH(hw);
126
127	ixgbe_ipsec_set_rx_item(hw, idx, ips_rx_ip_tbl);
128}
129
130/**
131 * ixgbe_ipsec_clear_hw_tables - because some tables don't get cleared on reset
132 * @adapter: board private structure
133 **/
134static void ixgbe_ipsec_clear_hw_tables(struct ixgbe_adapter *adapter)
135{
136	struct ixgbe_ipsec *ipsec = adapter->ipsec;
137	struct ixgbe_hw *hw = &adapter->hw;
138	u32 buf[4] = {0, 0, 0, 0};
139	u16 idx;
140
141	/* disable Rx and Tx SA lookup */
142	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
143	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
144
145	/* scrub the tables - split the loops for the max of the IP table */
146	for (idx = 0; idx < IXGBE_IPSEC_MAX_RX_IP_COUNT; idx++) {
147		ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
148		ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
149		ixgbe_ipsec_set_rx_ip(hw, idx, (__be32 *)buf);
150	}
151	for (; idx < IXGBE_IPSEC_MAX_SA_COUNT; idx++) {
152		ixgbe_ipsec_set_tx_sa(hw, idx, buf, 0);
153		ixgbe_ipsec_set_rx_sa(hw, idx, 0, buf, 0, 0, 0);
154	}
155
156	ipsec->num_rx_sa = 0;
157	ipsec->num_tx_sa = 0;
158}
159
160/**
161 * ixgbe_ipsec_stop_data
162 * @adapter: board private structure
163 **/
164static void ixgbe_ipsec_stop_data(struct ixgbe_adapter *adapter)
165{
166	struct ixgbe_hw *hw = &adapter->hw;
167	bool link = adapter->link_up;
168	u32 t_rdy, r_rdy;
169	u32 limit;
170	u32 reg;
171
172	/* halt data paths */
173	reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
174	reg |= IXGBE_SECTXCTRL_TX_DIS;
175	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
176
177	reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
178	reg |= IXGBE_SECRXCTRL_RX_DIS;
179	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
180
181	IXGBE_WRITE_FLUSH(hw);
 
 
 
 
 
 
 
 
 
182
183	/* If the tx fifo doesn't have link, but still has data,
184	 * we can't clear the tx sec block.  Set the MAC loopback
185	 * before block clear
186	 */
187	if (!link) {
188		reg = IXGBE_READ_REG(hw, IXGBE_MACC);
189		reg |= IXGBE_MACC_FLU;
190		IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
191
192		reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
193		reg |= IXGBE_HLREG0_LPBK;
194		IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
195
196		IXGBE_WRITE_FLUSH(hw);
197		mdelay(3);
198	}
199
200	/* wait for the paths to empty */
201	limit = 20;
202	do {
203		mdelay(10);
204		t_rdy = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
205			IXGBE_SECTXSTAT_SECTX_RDY;
206		r_rdy = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
207			IXGBE_SECRXSTAT_SECRX_RDY;
208	} while (!t_rdy && !r_rdy && limit--);
209
210	/* undo loopback if we played with it earlier */
211	if (!link) {
212		reg = IXGBE_READ_REG(hw, IXGBE_MACC);
213		reg &= ~IXGBE_MACC_FLU;
214		IXGBE_WRITE_REG(hw, IXGBE_MACC, reg);
215
216		reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
217		reg &= ~IXGBE_HLREG0_LPBK;
218		IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
219
220		IXGBE_WRITE_FLUSH(hw);
221	}
222}
223
224/**
225 * ixgbe_ipsec_stop_engine
226 * @adapter: board private structure
227 **/
228static void ixgbe_ipsec_stop_engine(struct ixgbe_adapter *adapter)
229{
230	struct ixgbe_hw *hw = &adapter->hw;
231	u32 reg;
232
233	ixgbe_ipsec_stop_data(adapter);
234
235	/* disable Rx and Tx SA lookup */
236	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, 0);
237	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, 0);
238
239	/* disable the Rx and Tx engines and full packet store-n-forward */
240	reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
241	reg |= IXGBE_SECTXCTRL_SECTX_DIS;
242	reg &= ~IXGBE_SECTXCTRL_STORE_FORWARD;
243	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, reg);
244
245	reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
246	reg |= IXGBE_SECRXCTRL_SECRX_DIS;
247	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg);
248
249	/* restore the "tx security buffer almost full threshold" to 0x250 */
250	IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, 0x250);
251
252	/* Set minimum IFG between packets back to the default 0x1 */
253	reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
254	reg = (reg & 0xfffffff0) | 0x1;
255	IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
256
257	/* final set for normal (no ipsec offload) processing */
258	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_SECTX_DIS);
259	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, IXGBE_SECRXCTRL_SECRX_DIS);
260
261	IXGBE_WRITE_FLUSH(hw);
262}
263
264/**
265 * ixgbe_ipsec_start_engine
266 * @adapter: board private structure
267 *
268 * NOTE: this increases power consumption whether being used or not
269 **/
270static void ixgbe_ipsec_start_engine(struct ixgbe_adapter *adapter)
271{
272	struct ixgbe_hw *hw = &adapter->hw;
273	u32 reg;
274
275	ixgbe_ipsec_stop_data(adapter);
276
277	/* Set minimum IFG between packets to 3 */
278	reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
279	reg = (reg & 0xfffffff0) | 0x3;
280	IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
281
282	/* Set "tx security buffer almost full threshold" to 0x15 so that the
283	 * almost full indication is generated only after buffer contains at
284	 * least an entire jumbo packet.
285	 */
286	reg = IXGBE_READ_REG(hw, IXGBE_SECTXBUFFAF);
287	reg = (reg & 0xfffffc00) | 0x15;
288	IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, reg);
289
290	/* restart the data paths by clearing the DISABLE bits */
291	IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, 0);
292	IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL, IXGBE_SECTXCTRL_STORE_FORWARD);
293
294	/* enable Rx and Tx SA lookup */
295	IXGBE_WRITE_REG(hw, IXGBE_IPSTXIDX, IXGBE_RXTXIDX_IPS_EN);
296	IXGBE_WRITE_REG(hw, IXGBE_IPSRXIDX, IXGBE_RXTXIDX_IPS_EN);
297
298	IXGBE_WRITE_FLUSH(hw);
299}
300
301/**
302 * ixgbe_ipsec_restore - restore the ipsec HW settings after a reset
303 * @adapter: board private structure
 
 
 
 
 
 
 
304 **/
305void ixgbe_ipsec_restore(struct ixgbe_adapter *adapter)
306{
307	struct ixgbe_ipsec *ipsec = adapter->ipsec;
308	struct ixgbe_hw *hw = &adapter->hw;
309	int i;
310
311	if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED))
312		return;
313
314	/* clean up and restart the engine */
315	ixgbe_ipsec_stop_engine(adapter);
316	ixgbe_ipsec_clear_hw_tables(adapter);
317	ixgbe_ipsec_start_engine(adapter);
318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319	/* reload the IP addrs */
320	for (i = 0; i < IXGBE_IPSEC_MAX_RX_IP_COUNT; i++) {
321		struct rx_ip_sa *ipsa = &ipsec->ip_tbl[i];
322
323		if (ipsa->used)
324			ixgbe_ipsec_set_rx_ip(hw, i, ipsa->ipaddr);
325	}
326
327	/* reload the Rx and Tx keys */
328	for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
329		struct rx_sa *rsa = &ipsec->rx_tbl[i];
330		struct tx_sa *tsa = &ipsec->tx_tbl[i];
331
332		if (rsa->used)
333			ixgbe_ipsec_set_rx_sa(hw, i, rsa->xs->id.spi,
334					      rsa->key, rsa->salt,
335					      rsa->mode, rsa->iptbl_ind);
336
337		if (tsa->used)
338			ixgbe_ipsec_set_tx_sa(hw, i, tsa->key, tsa->salt);
339	}
340}
341
342/**
343 * ixgbe_ipsec_find_empty_idx - find the first unused security parameter index
344 * @ipsec: pointer to ipsec struct
345 * @rxtable: true if we need to look in the Rx table
346 *
347 * Returns the first unused index in either the Rx or Tx SA table
348 **/
349static int ixgbe_ipsec_find_empty_idx(struct ixgbe_ipsec *ipsec, bool rxtable)
350{
351	u32 i;
352
353	if (rxtable) {
354		if (ipsec->num_rx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
355			return -ENOSPC;
356
357		/* search rx sa table */
358		for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
359			if (!ipsec->rx_tbl[i].used)
360				return i;
361		}
362	} else {
363		if (ipsec->num_tx_sa == IXGBE_IPSEC_MAX_SA_COUNT)
364			return -ENOSPC;
365
366		/* search tx sa table */
367		for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT; i++) {
368			if (!ipsec->tx_tbl[i].used)
369				return i;
370		}
371	}
372
373	return -ENOSPC;
374}
375
376/**
377 * ixgbe_ipsec_find_rx_state - find the state that matches
378 * @ipsec: pointer to ipsec struct
379 * @daddr: inbound address to match
380 * @proto: protocol to match
381 * @spi: SPI to match
382 * @ip4: true if using an ipv4 address
383 *
384 * Returns a pointer to the matching SA state information
385 **/
386static struct xfrm_state *ixgbe_ipsec_find_rx_state(struct ixgbe_ipsec *ipsec,
387						    __be32 *daddr, u8 proto,
388						    __be32 spi, bool ip4)
389{
390	struct rx_sa *rsa;
391	struct xfrm_state *ret = NULL;
392
393	rcu_read_lock();
394	hash_for_each_possible_rcu(ipsec->rx_sa_list, rsa, hlist, spi)
 
 
 
395		if (spi == rsa->xs->id.spi &&
396		    ((ip4 && *daddr == rsa->xs->id.daddr.a4) ||
397		      (!ip4 && !memcmp(daddr, &rsa->xs->id.daddr.a6,
398				       sizeof(rsa->xs->id.daddr.a6)))) &&
399		    proto == rsa->xs->id.proto) {
400			ret = rsa->xs;
401			xfrm_state_hold(ret);
402			break;
403		}
 
404	rcu_read_unlock();
405	return ret;
406}
407
408/**
409 * ixgbe_ipsec_parse_proto_keys - find the key and salt based on the protocol
410 * @xs: pointer to xfrm_state struct
411 * @mykey: pointer to key array to populate
412 * @mysalt: pointer to salt value to populate
413 *
414 * This copies the protocol keys and salt to our own data tables.  The
415 * 82599 family only supports the one algorithm.
416 **/
417static int ixgbe_ipsec_parse_proto_keys(struct xfrm_state *xs,
418					u32 *mykey, u32 *mysalt)
419{
420	struct net_device *dev = xs->xso.dev;
421	unsigned char *key_data;
422	char *alg_name = NULL;
423	const char aes_gcm_name[] = "rfc4106(gcm(aes))";
424	int key_len;
425
426	if (!xs->aead) {
427		netdev_err(dev, "Unsupported IPsec algorithm\n");
428		return -EINVAL;
429	}
430
431	if (xs->aead->alg_icv_len != IXGBE_IPSEC_AUTH_BITS) {
432		netdev_err(dev, "IPsec offload requires %d bit authentication\n",
433			   IXGBE_IPSEC_AUTH_BITS);
434		return -EINVAL;
435	}
436
437	key_data = &xs->aead->alg_key[0];
438	key_len = xs->aead->alg_key_len;
439	alg_name = xs->aead->alg_name;
440
441	if (strcmp(alg_name, aes_gcm_name)) {
442		netdev_err(dev, "Unsupported IPsec algorithm - please use %s\n",
443			   aes_gcm_name);
444		return -EINVAL;
445	}
446
447	/* The key bytes come down in a bigendian array of bytes, so
448	 * we don't need to do any byteswapping.
449	 * 160 accounts for 16 byte key and 4 byte salt
450	 */
451	if (key_len == 160) {
452		*mysalt = ((u32 *)key_data)[4];
453	} else if (key_len != 128) {
454		netdev_err(dev, "IPsec hw offload only supports keys up to 128 bits with a 32 bit salt\n");
455		return -EINVAL;
456	} else {
457		netdev_info(dev, "IPsec hw offload parameters missing 32 bit salt value\n");
458		*mysalt = 0;
459	}
460	memcpy(mykey, key_data, 16);
461
462	return 0;
463}
464
465/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466 * ixgbe_ipsec_add_sa - program device with a security association
467 * @xs: pointer to transformer state struct
 
468 **/
469static int ixgbe_ipsec_add_sa(struct xfrm_state *xs)
 
470{
471	struct net_device *dev = xs->xso.dev;
472	struct ixgbe_adapter *adapter = netdev_priv(dev);
473	struct ixgbe_ipsec *ipsec = adapter->ipsec;
474	struct ixgbe_hw *hw = &adapter->hw;
475	int checked, match, first;
476	u16 sa_idx;
477	int ret;
478	int i;
479
480	if (xs->id.proto != IPPROTO_ESP && xs->id.proto != IPPROTO_AH) {
481		netdev_err(dev, "Unsupported protocol 0x%04x for ipsec offload\n",
482			   xs->id.proto);
483		return -EINVAL;
484	}
485
486	if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
487		struct rx_sa rsa;
488
489		if (xs->calg) {
490			netdev_err(dev, "Compression offload not supported\n");
491			return -EINVAL;
492		}
493
494		/* find the first unused index */
495		ret = ixgbe_ipsec_find_empty_idx(ipsec, true);
496		if (ret < 0) {
497			netdev_err(dev, "No space for SA in Rx table!\n");
498			return ret;
499		}
500		sa_idx = (u16)ret;
501
502		memset(&rsa, 0, sizeof(rsa));
503		rsa.used = true;
504		rsa.xs = xs;
505
506		if (rsa.xs->id.proto & IPPROTO_ESP)
507			rsa.decrypt = xs->ealg || xs->aead;
508
509		/* get the key and salt */
510		ret = ixgbe_ipsec_parse_proto_keys(xs, rsa.key, &rsa.salt);
511		if (ret) {
512			netdev_err(dev, "Failed to get key data for Rx SA table\n");
513			return ret;
514		}
515
516		/* get ip for rx sa table */
517		if (xs->props.family == AF_INET6)
518			memcpy(rsa.ipaddr, &xs->id.daddr.a6, 16);
519		else
520			memcpy(&rsa.ipaddr[3], &xs->id.daddr.a4, 4);
521
522		/* The HW does not have a 1:1 mapping from keys to IP addrs, so
523		 * check for a matching IP addr entry in the table.  If the addr
524		 * already exists, use it; else find an unused slot and add the
525		 * addr.  If one does not exist and there are no unused table
526		 * entries, fail the request.
527		 */
528
529		/* Find an existing match or first not used, and stop looking
530		 * after we've checked all we know we have.
531		 */
532		checked = 0;
533		match = -1;
534		first = -1;
535		for (i = 0;
536		     i < IXGBE_IPSEC_MAX_RX_IP_COUNT &&
537		     (checked < ipsec->num_rx_sa || first < 0);
538		     i++) {
539			if (ipsec->ip_tbl[i].used) {
540				if (!memcmp(ipsec->ip_tbl[i].ipaddr,
541					    rsa.ipaddr, sizeof(rsa.ipaddr))) {
542					match = i;
543					break;
544				}
545				checked++;
546			} else if (first < 0) {
547				first = i;  /* track the first empty seen */
548			}
549		}
550
551		if (ipsec->num_rx_sa == 0)
552			first = 0;
553
554		if (match >= 0) {
555			/* addrs are the same, we should use this one */
556			rsa.iptbl_ind = match;
557			ipsec->ip_tbl[match].ref_cnt++;
558
559		} else if (first >= 0) {
560			/* no matches, but here's an empty slot */
561			rsa.iptbl_ind = first;
562
563			memcpy(ipsec->ip_tbl[first].ipaddr,
564			       rsa.ipaddr, sizeof(rsa.ipaddr));
565			ipsec->ip_tbl[first].ref_cnt = 1;
566			ipsec->ip_tbl[first].used = true;
567
568			ixgbe_ipsec_set_rx_ip(hw, rsa.iptbl_ind, rsa.ipaddr);
569
570		} else {
571			/* no match and no empty slot */
572			netdev_err(dev, "No space for SA in Rx IP SA table\n");
573			memset(&rsa, 0, sizeof(rsa));
574			return -ENOSPC;
575		}
576
577		rsa.mode = IXGBE_RXMOD_VALID;
578		if (rsa.xs->id.proto & IPPROTO_ESP)
579			rsa.mode |= IXGBE_RXMOD_PROTO_ESP;
580		if (rsa.decrypt)
581			rsa.mode |= IXGBE_RXMOD_DECRYPT;
582		if (rsa.xs->props.family == AF_INET6)
583			rsa.mode |= IXGBE_RXMOD_IPV6;
584
585		/* the preparations worked, so save the info */
586		memcpy(&ipsec->rx_tbl[sa_idx], &rsa, sizeof(rsa));
587
588		ixgbe_ipsec_set_rx_sa(hw, sa_idx, rsa.xs->id.spi, rsa.key,
589				      rsa.salt, rsa.mode, rsa.iptbl_ind);
590		xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_RX_INDEX;
591
592		ipsec->num_rx_sa++;
593
594		/* hash the new entry for faster search in Rx path */
595		hash_add_rcu(ipsec->rx_sa_list, &ipsec->rx_tbl[sa_idx].hlist,
596			     rsa.xs->id.spi);
597	} else {
598		struct tx_sa tsa;
599
 
 
 
 
600		/* find the first unused index */
601		ret = ixgbe_ipsec_find_empty_idx(ipsec, false);
602		if (ret < 0) {
603			netdev_err(dev, "No space for SA in Tx table\n");
604			return ret;
605		}
606		sa_idx = (u16)ret;
607
608		memset(&tsa, 0, sizeof(tsa));
609		tsa.used = true;
610		tsa.xs = xs;
611
612		if (xs->id.proto & IPPROTO_ESP)
613			tsa.encrypt = xs->ealg || xs->aead;
614
615		ret = ixgbe_ipsec_parse_proto_keys(xs, tsa.key, &tsa.salt);
616		if (ret) {
617			netdev_err(dev, "Failed to get key data for Tx SA table\n");
618			memset(&tsa, 0, sizeof(tsa));
619			return ret;
620		}
621
622		/* the preparations worked, so save the info */
623		memcpy(&ipsec->tx_tbl[sa_idx], &tsa, sizeof(tsa));
624
625		ixgbe_ipsec_set_tx_sa(hw, sa_idx, tsa.key, tsa.salt);
626
627		xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_TX_INDEX;
628
629		ipsec->num_tx_sa++;
630	}
631
632	/* enable the engine if not already warmed up */
633	if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED)) {
634		ixgbe_ipsec_start_engine(adapter);
635		adapter->flags2 |= IXGBE_FLAG2_IPSEC_ENABLED;
636	}
637
638	return 0;
639}
640
641/**
642 * ixgbe_ipsec_del_sa - clear out this specific SA
643 * @xs: pointer to transformer state struct
644 **/
645static void ixgbe_ipsec_del_sa(struct xfrm_state *xs)
646{
647	struct net_device *dev = xs->xso.dev;
648	struct ixgbe_adapter *adapter = netdev_priv(dev);
649	struct ixgbe_ipsec *ipsec = adapter->ipsec;
650	struct ixgbe_hw *hw = &adapter->hw;
651	u32 zerobuf[4] = {0, 0, 0, 0};
652	u16 sa_idx;
653
654	if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) {
655		struct rx_sa *rsa;
656		u8 ipi;
657
658		sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_RX_INDEX;
659		rsa = &ipsec->rx_tbl[sa_idx];
660
661		if (!rsa->used) {
662			netdev_err(dev, "Invalid Rx SA selected sa_idx=%d offload_handle=%lu\n",
663				   sa_idx, xs->xso.offload_handle);
664			return;
665		}
666
667		ixgbe_ipsec_set_rx_sa(hw, sa_idx, 0, zerobuf, 0, 0, 0);
668		hash_del_rcu(&rsa->hlist);
669
670		/* if the IP table entry is referenced by only this SA,
671		 * i.e. ref_cnt is only 1, clear the IP table entry as well
672		 */
673		ipi = rsa->iptbl_ind;
674		if (ipsec->ip_tbl[ipi].ref_cnt > 0) {
675			ipsec->ip_tbl[ipi].ref_cnt--;
676
677			if (!ipsec->ip_tbl[ipi].ref_cnt) {
678				memset(&ipsec->ip_tbl[ipi], 0,
679				       sizeof(struct rx_ip_sa));
680				ixgbe_ipsec_set_rx_ip(hw, ipi, zerobuf);
 
681			}
682		}
683
684		memset(rsa, 0, sizeof(struct rx_sa));
685		ipsec->num_rx_sa--;
686	} else {
687		sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
688
689		if (!ipsec->tx_tbl[sa_idx].used) {
690			netdev_err(dev, "Invalid Tx SA selected sa_idx=%d offload_handle=%lu\n",
691				   sa_idx, xs->xso.offload_handle);
692			return;
693		}
694
695		ixgbe_ipsec_set_tx_sa(hw, sa_idx, zerobuf, 0);
696		memset(&ipsec->tx_tbl[sa_idx], 0, sizeof(struct tx_sa));
697		ipsec->num_tx_sa--;
698	}
699
700	/* if there are no SAs left, stop the engine to save energy */
701	if (ipsec->num_rx_sa == 0 && ipsec->num_tx_sa == 0) {
702		adapter->flags2 &= ~IXGBE_FLAG2_IPSEC_ENABLED;
703		ixgbe_ipsec_stop_engine(adapter);
704	}
705}
706
707/**
708 * ixgbe_ipsec_offload_ok - can this packet use the xfrm hw offload
709 * @skb: current data packet
710 * @xs: pointer to transformer state struct
711 **/
712static bool ixgbe_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
713{
714	if (xs->props.family == AF_INET) {
715		/* Offload with IPv4 options is not supported yet */
716		if (ip_hdr(skb)->ihl != 5)
717			return false;
718	} else {
719		/* Offload with IPv6 extension headers is not support yet */
720		if (ipv6_ext_hdr(ipv6_hdr(skb)->nexthdr))
721			return false;
722	}
723
724	return true;
725}
726
727static const struct xfrmdev_ops ixgbe_xfrmdev_ops = {
728	.xdo_dev_state_add = ixgbe_ipsec_add_sa,
729	.xdo_dev_state_delete = ixgbe_ipsec_del_sa,
730	.xdo_dev_offload_ok = ixgbe_ipsec_offload_ok,
731};
732
733/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
734 * ixgbe_ipsec_tx - setup Tx flags for ipsec offload
735 * @tx_ring: outgoing context
736 * @first: current data packet
737 * @itd: ipsec Tx data for later use in building context descriptor
738 **/
739int ixgbe_ipsec_tx(struct ixgbe_ring *tx_ring,
740		   struct ixgbe_tx_buffer *first,
741		   struct ixgbe_ipsec_tx_data *itd)
742{
743	struct ixgbe_adapter *adapter = netdev_priv(tx_ring->netdev);
744	struct ixgbe_ipsec *ipsec = adapter->ipsec;
745	struct xfrm_state *xs;
 
746	struct tx_sa *tsa;
747
748	if (unlikely(!first->skb->sp->len)) {
 
749		netdev_err(tx_ring->netdev, "%s: no xfrm state len = %d\n",
750			   __func__, first->skb->sp->len);
751		return 0;
752	}
753
754	xs = xfrm_input_state(first->skb);
755	if (unlikely(!xs)) {
756		netdev_err(tx_ring->netdev, "%s: no xfrm_input_state() xs = %p\n",
757			   __func__, xs);
758		return 0;
759	}
760
761	itd->sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
762	if (unlikely(itd->sa_idx > IXGBE_IPSEC_MAX_SA_COUNT)) {
763		netdev_err(tx_ring->netdev, "%s: bad sa_idx=%d handle=%lu\n",
764			   __func__, itd->sa_idx, xs->xso.offload_handle);
765		return 0;
766	}
767
768	tsa = &ipsec->tx_tbl[itd->sa_idx];
769	if (unlikely(!tsa->used)) {
770		netdev_err(tx_ring->netdev, "%s: unused sa_idx=%d\n",
771			   __func__, itd->sa_idx);
772		return 0;
773	}
774
775	first->tx_flags |= IXGBE_TX_FLAGS_IPSEC | IXGBE_TX_FLAGS_CC;
776
777	if (xs->id.proto == IPPROTO_ESP) {
778
779		itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_TYPE_ESP |
780			      IXGBE_ADVTXD_TUCMD_L4T_TCP;
781		if (first->protocol == htons(ETH_P_IP))
782			itd->flags |= IXGBE_ADVTXD_TUCMD_IPV4;
783
784		/* The actual trailer length is authlen (16 bytes) plus
785		 * 2 bytes for the proto and the padlen values, plus
786		 * padlen bytes of padding.  This ends up not the same
787		 * as the static value found in xs->props.trailer_len (21).
788		 *
789		 * ... but if we're doing GSO, don't bother as the stack
790		 * doesn't add a trailer for those.
791		 */
792		if (!skb_is_gso(first->skb)) {
793			/* The "correct" way to get the auth length would be
794			 * to use
795			 *    authlen = crypto_aead_authsize(xs->data);
796			 * but since we know we only have one size to worry
797			 * about * we can let the compiler use the constant
798			 * and save us a few CPU cycles.
799			 */
800			const int authlen = IXGBE_IPSEC_AUTH_BITS / 8;
801			struct sk_buff *skb = first->skb;
802			u8 padlen;
803			int ret;
804
805			ret = skb_copy_bits(skb, skb->len - (authlen + 2),
806					    &padlen, 1);
807			if (unlikely(ret))
808				return 0;
809			itd->trailer_len = authlen + 2 + padlen;
810		}
811	}
812	if (tsa->encrypt)
813		itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_ENCRYPT_EN;
814
815	return 1;
816}
817
818/**
819 * ixgbe_ipsec_rx - decode ipsec bits from Rx descriptor
820 * @rx_ring: receiving ring
821 * @rx_desc: receive data descriptor
822 * @skb: current data packet
823 *
824 * Determine if there was an ipsec encapsulation noticed, and if so set up
825 * the resulting status for later in the receive stack.
826 **/
827void ixgbe_ipsec_rx(struct ixgbe_ring *rx_ring,
828		    union ixgbe_adv_rx_desc *rx_desc,
829		    struct sk_buff *skb)
830{
831	struct ixgbe_adapter *adapter = netdev_priv(rx_ring->netdev);
832	__le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
833	__le16 ipsec_pkt_types = cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH |
834					     IXGBE_RXDADV_PKTTYPE_IPSEC_ESP);
835	struct ixgbe_ipsec *ipsec = adapter->ipsec;
836	struct xfrm_offload *xo = NULL;
837	struct xfrm_state *xs = NULL;
838	struct ipv6hdr *ip6 = NULL;
839	struct iphdr *ip4 = NULL;
 
840	void *daddr;
841	__be32 spi;
842	u8 *c_hdr;
843	u8 proto;
844
845	/* Find the ip and crypto headers in the data.
846	 * We can assume no vlan header in the way, b/c the
847	 * hw won't recognize the IPsec packet and anyway the
848	 * currently vlan device doesn't support xfrm offload.
849	 */
850	if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV4)) {
851		ip4 = (struct iphdr *)(skb->data + ETH_HLEN);
852		daddr = &ip4->daddr;
853		c_hdr = (u8 *)ip4 + ip4->ihl * 4;
854	} else if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV6)) {
855		ip6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
856		daddr = &ip6->daddr;
857		c_hdr = (u8 *)ip6 + sizeof(struct ipv6hdr);
858	} else {
859		return;
860	}
861
862	switch (pkt_info & ipsec_pkt_types) {
863	case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH):
864		spi = ((struct ip_auth_hdr *)c_hdr)->spi;
865		proto = IPPROTO_AH;
866		break;
867	case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_ESP):
868		spi = ((struct ip_esp_hdr *)c_hdr)->spi;
869		proto = IPPROTO_ESP;
870		break;
871	default:
872		return;
873	}
874
875	xs = ixgbe_ipsec_find_rx_state(ipsec, daddr, proto, spi, !!ip4);
876	if (unlikely(!xs))
877		return;
878
879	skb->sp = secpath_dup(skb->sp);
880	if (unlikely(!skb->sp))
881		return;
882
883	skb->sp->xvec[skb->sp->len++] = xs;
884	skb->sp->olen++;
885	xo = xfrm_offload(skb);
886	xo->flags = CRYPTO_DONE;
887	xo->status = CRYPTO_SUCCESS;
888
889	adapter->rx_ipsec++;
890}
891
892/**
893 * ixgbe_init_ipsec_offload - initialize security registers for IPSec operation
894 * @adapter: board private structure
895 **/
896void ixgbe_init_ipsec_offload(struct ixgbe_adapter *adapter)
897{
 
898	struct ixgbe_ipsec *ipsec;
 
899	size_t size;
900
901	if (adapter->hw.mac.type == ixgbe_mac_82598EB)
 
 
 
 
 
 
 
 
 
 
902		return;
903
904	ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
905	if (!ipsec)
906		goto err1;
907	hash_init(ipsec->rx_sa_list);
908
909	size = sizeof(struct rx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
910	ipsec->rx_tbl = kzalloc(size, GFP_KERNEL);
911	if (!ipsec->rx_tbl)
912		goto err2;
913
914	size = sizeof(struct tx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
915	ipsec->tx_tbl = kzalloc(size, GFP_KERNEL);
916	if (!ipsec->tx_tbl)
917		goto err2;
918
919	size = sizeof(struct rx_ip_sa) * IXGBE_IPSEC_MAX_RX_IP_COUNT;
920	ipsec->ip_tbl = kzalloc(size, GFP_KERNEL);
921	if (!ipsec->ip_tbl)
922		goto err2;
923
924	ipsec->num_rx_sa = 0;
925	ipsec->num_tx_sa = 0;
926
927	adapter->ipsec = ipsec;
928	ixgbe_ipsec_stop_engine(adapter);
929	ixgbe_ipsec_clear_hw_tables(adapter);
930
931	adapter->netdev->xfrmdev_ops = &ixgbe_xfrmdev_ops;
932
933#define IXGBE_ESP_FEATURES	(NETIF_F_HW_ESP | \
934				 NETIF_F_HW_ESP_TX_CSUM | \
935				 NETIF_F_GSO_ESP)
936
937	adapter->netdev->features |= IXGBE_ESP_FEATURES;
938	adapter->netdev->hw_enc_features |= IXGBE_ESP_FEATURES;
939
940	return;
941
942err2:
943	kfree(ipsec->ip_tbl);
944	kfree(ipsec->rx_tbl);
945	kfree(ipsec->tx_tbl);
946	kfree(ipsec);
947err1:
948	netdev_err(adapter->netdev, "Unable to allocate memory for SA tables");
949}
950
951/**
952 * ixgbe_stop_ipsec_offload - tear down the ipsec offload
953 * @adapter: board private structure
954 **/
955void ixgbe_stop_ipsec_offload(struct ixgbe_adapter *adapter)
956{
957	struct ixgbe_ipsec *ipsec = adapter->ipsec;
958
959	adapter->ipsec = NULL;
960	if (ipsec) {
961		kfree(ipsec->ip_tbl);
962		kfree(ipsec->rx_tbl);
963		kfree(ipsec->tx_tbl);
964		kfree(ipsec);
965	}
966}