xref: /linux/drivers/net/ethernet/intel/ixgbe/ixgbe_ipsec.c (revision fcab107abe1ab5be9dbe874baa722372da8f4f73)
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
10 static const char aes_gcm_name[] = "rfc4106(gcm(aes))";
11 
12 static void ixgbe_ipsec_del_sa(struct net_device *dev, 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  **/
21 static 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  **/
49 static 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  **/
73 static 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  **/
103 static 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  **/
120 static 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  **/
146 static 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  **/
219 static 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  **/
261 static 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  **/
303 void 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(adapter->netdev, 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(adapter->netdev, 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  **/
355 static 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  **/
392 static 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  * @dev: pointer to net device
421  * @xs: pointer to xfrm_state struct
422  * @mykey: pointer to key array to populate
423  * @mysalt: pointer to salt value to populate
424  *
425  * This copies the protocol keys and salt to our own data tables.  The
426  * 82599 family only supports the one algorithm.
427  **/
428 static int ixgbe_ipsec_parse_proto_keys(struct net_device *dev,
429 					struct xfrm_state *xs,
430 					u32 *mykey, u32 *mysalt)
431 {
432 	unsigned char *key_data;
433 	char *alg_name = NULL;
434 	int key_len;
435 
436 	if (!xs->aead) {
437 		netdev_err(dev, "Unsupported IPsec algorithm\n");
438 		return -EINVAL;
439 	}
440 
441 	if (xs->aead->alg_icv_len != IXGBE_IPSEC_AUTH_BITS) {
442 		netdev_err(dev, "IPsec offload requires %d bit authentication\n",
443 			   IXGBE_IPSEC_AUTH_BITS);
444 		return -EINVAL;
445 	}
446 
447 	key_data = &xs->aead->alg_key[0];
448 	key_len = xs->aead->alg_key_len;
449 	alg_name = xs->aead->alg_name;
450 
451 	if (strcmp(alg_name, aes_gcm_name)) {
452 		netdev_err(dev, "Unsupported IPsec algorithm - please use %s\n",
453 			   aes_gcm_name);
454 		return -EINVAL;
455 	}
456 
457 	/* The key bytes come down in a bigendian array of bytes, so
458 	 * we don't need to do any byteswapping.
459 	 * 160 accounts for 16 byte key and 4 byte salt
460 	 */
461 	if (key_len == IXGBE_IPSEC_KEY_BITS) {
462 		*mysalt = ((u32 *)key_data)[4];
463 	} else if (key_len != (IXGBE_IPSEC_KEY_BITS - (sizeof(*mysalt) * 8))) {
464 		netdev_err(dev, "IPsec hw offload only supports keys up to 128 bits with a 32 bit salt\n");
465 		return -EINVAL;
466 	} else {
467 		netdev_info(dev, "IPsec hw offload parameters missing 32 bit salt value\n");
468 		*mysalt = 0;
469 	}
470 	memcpy(mykey, key_data, 16);
471 
472 	return 0;
473 }
474 
475 /**
476  * ixgbe_ipsec_check_mgmt_ip - make sure there is no clash with mgmt IP filters
477  * @dev: pointer to net device
478  * @xs: pointer to transformer state struct
479  **/
480 static int ixgbe_ipsec_check_mgmt_ip(struct net_device *dev,
481 				     struct xfrm_state *xs)
482 {
483 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(dev);
484 	struct ixgbe_hw *hw = &adapter->hw;
485 	u32 mfval, manc, reg;
486 	int num_filters = 4;
487 	bool manc_ipv4;
488 	u32 bmcipval;
489 	int i, j;
490 
491 #define MANC_EN_IPV4_FILTER      BIT(24)
492 #define MFVAL_IPV4_FILTER_SHIFT  16
493 #define MFVAL_IPV6_FILTER_SHIFT  24
494 #define MIPAF_ARR(_m, _n)        (IXGBE_MIPAF + ((_m) * 0x10) + ((_n) * 4))
495 
496 #define IXGBE_BMCIP(_n)          (0x5050 + ((_n) * 4))
497 #define IXGBE_BMCIPVAL           0x5060
498 #define BMCIP_V4                 0x2
499 #define BMCIP_V6                 0x3
500 #define BMCIP_MASK               0x3
501 
502 	manc = IXGBE_READ_REG(hw, IXGBE_MANC);
503 	manc_ipv4 = !!(manc & MANC_EN_IPV4_FILTER);
504 	mfval = IXGBE_READ_REG(hw, IXGBE_MFVAL);
505 	bmcipval = IXGBE_READ_REG(hw, IXGBE_BMCIPVAL);
506 
507 	if (xs->props.family == AF_INET) {
508 		/* are there any IPv4 filters to check? */
509 		if (manc_ipv4) {
510 			/* the 4 ipv4 filters are all in MIPAF(3, i) */
511 			for (i = 0; i < num_filters; i++) {
512 				if (!(mfval & BIT(MFVAL_IPV4_FILTER_SHIFT + i)))
513 					continue;
514 
515 				reg = IXGBE_READ_REG(hw, MIPAF_ARR(3, i));
516 				if (reg == (__force u32)xs->id.daddr.a4)
517 					return 1;
518 			}
519 		}
520 
521 		if ((bmcipval & BMCIP_MASK) == BMCIP_V4) {
522 			reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(3));
523 			if (reg == (__force u32)xs->id.daddr.a4)
524 				return 1;
525 		}
526 
527 	} else {
528 		/* if there are ipv4 filters, they are in the last ipv6 slot */
529 		if (manc_ipv4)
530 			num_filters = 3;
531 
532 		for (i = 0; i < num_filters; i++) {
533 			if (!(mfval & BIT(MFVAL_IPV6_FILTER_SHIFT + i)))
534 				continue;
535 
536 			for (j = 0; j < 4; j++) {
537 				reg = IXGBE_READ_REG(hw, MIPAF_ARR(i, j));
538 				if (reg != (__force u32)xs->id.daddr.a6[j])
539 					break;
540 			}
541 			if (j == 4)   /* did we match all 4 words? */
542 				return 1;
543 		}
544 
545 		if ((bmcipval & BMCIP_MASK) == BMCIP_V6) {
546 			for (j = 0; j < 4; j++) {
547 				reg = IXGBE_READ_REG(hw, IXGBE_BMCIP(j));
548 				if (reg != (__force u32)xs->id.daddr.a6[j])
549 					break;
550 			}
551 			if (j == 4)   /* did we match all 4 words? */
552 				return 1;
553 		}
554 	}
555 
556 	return 0;
557 }
558 
559 /**
560  * ixgbe_ipsec_add_sa - program device with a security association
561  * @dev: pointer to device to program
562  * @xs: pointer to transformer state struct
563  * @extack: extack point to fill failure reason
564  **/
565 static int ixgbe_ipsec_add_sa(struct net_device *dev,
566 			      struct xfrm_state *xs,
567 			      struct netlink_ext_ack *extack)
568 {
569 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(dev);
570 	struct ixgbe_ipsec *ipsec = adapter->ipsec;
571 	struct ixgbe_hw *hw = &adapter->hw;
572 	int checked, match, first;
573 	u16 sa_idx;
574 	int ret;
575 	int i;
576 
577 	if (xs->id.proto != IPPROTO_ESP && xs->id.proto != IPPROTO_AH) {
578 		NL_SET_ERR_MSG_MOD(extack, "Unsupported protocol for ipsec offload");
579 		return -EINVAL;
580 	}
581 
582 	if (xs->props.mode != XFRM_MODE_TRANSPORT) {
583 		NL_SET_ERR_MSG_MOD(extack, "Unsupported mode for ipsec offload");
584 		return -EINVAL;
585 	}
586 
587 	if (ixgbe_ipsec_check_mgmt_ip(dev, xs)) {
588 		NL_SET_ERR_MSG_MOD(extack, "IPsec IP addr clash with mgmt filters");
589 		return -EINVAL;
590 	}
591 
592 	if (xs->xso.type != XFRM_DEV_OFFLOAD_CRYPTO) {
593 		NL_SET_ERR_MSG_MOD(extack, "Unsupported ipsec offload type");
594 		return -EINVAL;
595 	}
596 
597 	if (xs->xso.dir == XFRM_DEV_OFFLOAD_IN) {
598 		struct rx_sa rsa;
599 
600 		if (xs->calg) {
601 			NL_SET_ERR_MSG_MOD(extack, "Compression offload not supported");
602 			return -EINVAL;
603 		}
604 
605 		/* find the first unused index */
606 		ret = ixgbe_ipsec_find_empty_idx(ipsec, true);
607 		if (ret < 0) {
608 			NL_SET_ERR_MSG_MOD(extack, "No space for SA in Rx table!");
609 			return ret;
610 		}
611 		sa_idx = (u16)ret;
612 
613 		memset(&rsa, 0, sizeof(rsa));
614 		rsa.used = true;
615 		rsa.xs = xs;
616 
617 		if (rsa.xs->id.proto & IPPROTO_ESP)
618 			rsa.decrypt = xs->ealg || xs->aead;
619 
620 		/* get the key and salt */
621 		ret = ixgbe_ipsec_parse_proto_keys(dev, xs, rsa.key, &rsa.salt);
622 		if (ret) {
623 			NL_SET_ERR_MSG_MOD(extack, "Failed to get key data for Rx SA table");
624 			return ret;
625 		}
626 
627 		/* get ip for rx sa table */
628 		if (xs->props.family == AF_INET6)
629 			memcpy(rsa.ipaddr, &xs->id.daddr.a6, 16);
630 		else
631 			memcpy(&rsa.ipaddr[3], &xs->id.daddr.a4, 4);
632 
633 		/* The HW does not have a 1:1 mapping from keys to IP addrs, so
634 		 * check for a matching IP addr entry in the table.  If the addr
635 		 * already exists, use it; else find an unused slot and add the
636 		 * addr.  If one does not exist and there are no unused table
637 		 * entries, fail the request.
638 		 */
639 
640 		/* Find an existing match or first not used, and stop looking
641 		 * after we've checked all we know we have.
642 		 */
643 		checked = 0;
644 		match = -1;
645 		first = -1;
646 		for (i = 0;
647 		     i < IXGBE_IPSEC_MAX_RX_IP_COUNT &&
648 		     (checked < ipsec->num_rx_sa || first < 0);
649 		     i++) {
650 			if (ipsec->ip_tbl[i].used) {
651 				if (!memcmp(ipsec->ip_tbl[i].ipaddr,
652 					    rsa.ipaddr, sizeof(rsa.ipaddr))) {
653 					match = i;
654 					break;
655 				}
656 				checked++;
657 			} else if (first < 0) {
658 				first = i;  /* track the first empty seen */
659 			}
660 		}
661 
662 		if (ipsec->num_rx_sa == 0)
663 			first = 0;
664 
665 		if (match >= 0) {
666 			/* addrs are the same, we should use this one */
667 			rsa.iptbl_ind = match;
668 			ipsec->ip_tbl[match].ref_cnt++;
669 
670 		} else if (first >= 0) {
671 			/* no matches, but here's an empty slot */
672 			rsa.iptbl_ind = first;
673 
674 			memcpy(ipsec->ip_tbl[first].ipaddr,
675 			       rsa.ipaddr, sizeof(rsa.ipaddr));
676 			ipsec->ip_tbl[first].ref_cnt = 1;
677 			ipsec->ip_tbl[first].used = true;
678 
679 			ixgbe_ipsec_set_rx_ip(hw, rsa.iptbl_ind, rsa.ipaddr);
680 
681 		} else {
682 			/* no match and no empty slot */
683 			NL_SET_ERR_MSG_MOD(extack, "No space for SA in Rx IP SA table");
684 			memset(&rsa, 0, sizeof(rsa));
685 			return -ENOSPC;
686 		}
687 
688 		rsa.mode = IXGBE_RXMOD_VALID;
689 		if (rsa.xs->id.proto & IPPROTO_ESP)
690 			rsa.mode |= IXGBE_RXMOD_PROTO_ESP;
691 		if (rsa.decrypt)
692 			rsa.mode |= IXGBE_RXMOD_DECRYPT;
693 		if (rsa.xs->props.family == AF_INET6)
694 			rsa.mode |= IXGBE_RXMOD_IPV6;
695 
696 		/* the preparations worked, so save the info */
697 		memcpy(&ipsec->rx_tbl[sa_idx], &rsa, sizeof(rsa));
698 
699 		ixgbe_ipsec_set_rx_sa(hw, sa_idx, rsa.xs->id.spi, rsa.key,
700 				      rsa.salt, rsa.mode, rsa.iptbl_ind);
701 		xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_RX_INDEX;
702 
703 		ipsec->num_rx_sa++;
704 
705 		/* hash the new entry for faster search in Rx path */
706 		hash_add_rcu(ipsec->rx_sa_list, &ipsec->rx_tbl[sa_idx].hlist,
707 			     (__force u32)rsa.xs->id.spi);
708 	} else {
709 		struct tx_sa tsa;
710 
711 		if (adapter->num_vfs &&
712 		    adapter->bridge_mode != BRIDGE_MODE_VEPA)
713 			return -EOPNOTSUPP;
714 
715 		/* find the first unused index */
716 		ret = ixgbe_ipsec_find_empty_idx(ipsec, false);
717 		if (ret < 0) {
718 			NL_SET_ERR_MSG_MOD(extack, "No space for SA in Tx table");
719 			return ret;
720 		}
721 		sa_idx = (u16)ret;
722 
723 		memset(&tsa, 0, sizeof(tsa));
724 		tsa.used = true;
725 		tsa.xs = xs;
726 
727 		if (xs->id.proto & IPPROTO_ESP)
728 			tsa.encrypt = xs->ealg || xs->aead;
729 
730 		ret = ixgbe_ipsec_parse_proto_keys(dev, xs, tsa.key, &tsa.salt);
731 		if (ret) {
732 			NL_SET_ERR_MSG_MOD(extack, "Failed to get key data for Tx SA table");
733 			memset(&tsa, 0, sizeof(tsa));
734 			return ret;
735 		}
736 
737 		/* the preparations worked, so save the info */
738 		memcpy(&ipsec->tx_tbl[sa_idx], &tsa, sizeof(tsa));
739 
740 		ixgbe_ipsec_set_tx_sa(hw, sa_idx, tsa.key, tsa.salt);
741 
742 		xs->xso.offload_handle = sa_idx + IXGBE_IPSEC_BASE_TX_INDEX;
743 
744 		ipsec->num_tx_sa++;
745 	}
746 
747 	/* enable the engine if not already warmed up */
748 	if (!(adapter->flags2 & IXGBE_FLAG2_IPSEC_ENABLED)) {
749 		ixgbe_ipsec_start_engine(adapter);
750 		adapter->flags2 |= IXGBE_FLAG2_IPSEC_ENABLED;
751 	}
752 
753 	return 0;
754 }
755 
756 /**
757  * ixgbe_ipsec_del_sa - clear out this specific SA
758  * @dev: pointer to device to program
759  * @xs: pointer to transformer state struct
760  **/
761 static void ixgbe_ipsec_del_sa(struct net_device *dev, struct xfrm_state *xs)
762 {
763 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(dev);
764 	struct ixgbe_ipsec *ipsec = adapter->ipsec;
765 	struct ixgbe_hw *hw = &adapter->hw;
766 	u32 zerobuf[4] = {0, 0, 0, 0};
767 	u16 sa_idx;
768 
769 	if (xs->xso.dir == XFRM_DEV_OFFLOAD_IN) {
770 		struct rx_sa *rsa;
771 		u8 ipi;
772 
773 		sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_RX_INDEX;
774 		rsa = &ipsec->rx_tbl[sa_idx];
775 
776 		if (!rsa->used) {
777 			netdev_err(dev, "Invalid Rx SA selected sa_idx=%d offload_handle=%lu\n",
778 				   sa_idx, xs->xso.offload_handle);
779 			return;
780 		}
781 
782 		ixgbe_ipsec_set_rx_sa(hw, sa_idx, 0, zerobuf, 0, 0, 0);
783 		hash_del_rcu(&rsa->hlist);
784 
785 		/* if the IP table entry is referenced by only this SA,
786 		 * i.e. ref_cnt is only 1, clear the IP table entry as well
787 		 */
788 		ipi = rsa->iptbl_ind;
789 		if (ipsec->ip_tbl[ipi].ref_cnt > 0) {
790 			ipsec->ip_tbl[ipi].ref_cnt--;
791 
792 			if (!ipsec->ip_tbl[ipi].ref_cnt) {
793 				memset(&ipsec->ip_tbl[ipi], 0,
794 				       sizeof(struct rx_ip_sa));
795 				ixgbe_ipsec_set_rx_ip(hw, ipi,
796 						      (__force __be32 *)zerobuf);
797 			}
798 		}
799 
800 		memset(rsa, 0, sizeof(struct rx_sa));
801 		ipsec->num_rx_sa--;
802 	} else {
803 		sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
804 
805 		if (!ipsec->tx_tbl[sa_idx].used) {
806 			netdev_err(dev, "Invalid Tx SA selected sa_idx=%d offload_handle=%lu\n",
807 				   sa_idx, xs->xso.offload_handle);
808 			return;
809 		}
810 
811 		ixgbe_ipsec_set_tx_sa(hw, sa_idx, zerobuf, 0);
812 		memset(&ipsec->tx_tbl[sa_idx], 0, sizeof(struct tx_sa));
813 		ipsec->num_tx_sa--;
814 	}
815 
816 	/* if there are no SAs left, stop the engine to save energy */
817 	if (ipsec->num_rx_sa == 0 && ipsec->num_tx_sa == 0) {
818 		adapter->flags2 &= ~IXGBE_FLAG2_IPSEC_ENABLED;
819 		ixgbe_ipsec_stop_engine(adapter);
820 	}
821 }
822 
823 static const struct xfrmdev_ops ixgbe_xfrmdev_ops = {
824 	.xdo_dev_state_add = ixgbe_ipsec_add_sa,
825 	.xdo_dev_state_delete = ixgbe_ipsec_del_sa,
826 };
827 
828 /**
829  * ixgbe_ipsec_vf_clear - clear the tables of data for a VF
830  * @adapter: board private structure
831  * @vf: VF id to be removed
832  **/
833 void ixgbe_ipsec_vf_clear(struct ixgbe_adapter *adapter, u32 vf)
834 {
835 	struct ixgbe_ipsec *ipsec = adapter->ipsec;
836 	int i;
837 
838 	if (!ipsec)
839 		return;
840 
841 	/* search rx sa table */
842 	for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_rx_sa; i++) {
843 		if (!ipsec->rx_tbl[i].used)
844 			continue;
845 		if (ipsec->rx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
846 		    ipsec->rx_tbl[i].vf == vf)
847 			ixgbe_ipsec_del_sa(adapter->netdev,
848 					   ipsec->rx_tbl[i].xs);
849 	}
850 
851 	/* search tx sa table */
852 	for (i = 0; i < IXGBE_IPSEC_MAX_SA_COUNT && ipsec->num_tx_sa; i++) {
853 		if (!ipsec->tx_tbl[i].used)
854 			continue;
855 		if (ipsec->tx_tbl[i].mode & IXGBE_RXTXMOD_VF &&
856 		    ipsec->tx_tbl[i].vf == vf)
857 			ixgbe_ipsec_del_sa(adapter->netdev,
858 					   ipsec->tx_tbl[i].xs);
859 	}
860 }
861 
862 /**
863  * ixgbe_ipsec_vf_add_sa - translate VF request to SA add
864  * @adapter: board private structure
865  * @msgbuf: The message buffer
866  * @vf: the VF index
867  *
868  * Make up a new xs and algorithm info from the data sent by the VF.
869  * We only need to sketch in just enough to set up the HW offload.
870  * Put the resulting offload_handle into the return message to the VF.
871  *
872  * Returns 0 or error value
873  **/
874 int ixgbe_ipsec_vf_add_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
875 {
876 	struct ixgbe_ipsec *ipsec = adapter->ipsec;
877 	struct xfrm_algo_desc *algo;
878 	struct sa_mbx_msg *sam;
879 	struct xfrm_state *xs;
880 	size_t aead_len;
881 	u16 sa_idx;
882 	u32 pfsa;
883 	int err;
884 
885 	sam = (struct sa_mbx_msg *)(&msgbuf[1]);
886 	if (!adapter->vfinfo[vf].trusted ||
887 	    !(adapter->flags2 & IXGBE_FLAG2_VF_IPSEC_ENABLED)) {
888 		e_warn(drv, "VF %d attempted to add an IPsec SA\n", vf);
889 		err = -EACCES;
890 		goto err_out;
891 	}
892 
893 	/* Tx IPsec offload doesn't seem to work on this
894 	 * device, so block these requests for now.
895 	 */
896 	if (sam->dir != XFRM_DEV_OFFLOAD_IN) {
897 		err = -EOPNOTSUPP;
898 		goto err_out;
899 	}
900 
901 	algo = xfrm_aead_get_byname(aes_gcm_name, IXGBE_IPSEC_AUTH_BITS, 1);
902 	if (unlikely(!algo)) {
903 		err = -ENOENT;
904 		goto err_out;
905 	}
906 
907 	xs = kzalloc(sizeof(*xs), GFP_ATOMIC);
908 	if (unlikely(!xs)) {
909 		err = -ENOMEM;
910 		goto err_out;
911 	}
912 
913 	xs->xso.dir = sam->dir;
914 	xs->id.spi = sam->spi;
915 	xs->id.proto = sam->proto;
916 	xs->props.family = sam->family;
917 	if (xs->props.family == AF_INET6)
918 		memcpy(&xs->id.daddr.a6, sam->addr, sizeof(xs->id.daddr.a6));
919 	else
920 		memcpy(&xs->id.daddr.a4, sam->addr, sizeof(xs->id.daddr.a4));
921 	xs->xso.dev = adapter->netdev;
922 
923 	aead_len = sizeof(*xs->aead) + IXGBE_IPSEC_KEY_BITS / 8;
924 	xs->aead = kzalloc(aead_len, GFP_ATOMIC);
925 	if (unlikely(!xs->aead)) {
926 		err = -ENOMEM;
927 		goto err_xs;
928 	}
929 
930 	xs->props.ealgo = algo->desc.sadb_alg_id;
931 	xs->geniv = algo->uinfo.aead.geniv;
932 	xs->aead->alg_icv_len = IXGBE_IPSEC_AUTH_BITS;
933 	xs->aead->alg_key_len = IXGBE_IPSEC_KEY_BITS;
934 	memcpy(xs->aead->alg_key, sam->key, sizeof(sam->key));
935 	memcpy(xs->aead->alg_name, aes_gcm_name, sizeof(aes_gcm_name));
936 
937 	/* set up the HW offload */
938 	err = ixgbe_ipsec_add_sa(adapter->netdev, xs, NULL);
939 	if (err)
940 		goto err_aead;
941 
942 	pfsa = xs->xso.offload_handle;
943 	if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
944 		sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
945 		ipsec->rx_tbl[sa_idx].vf = vf;
946 		ipsec->rx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
947 	} else {
948 		sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
949 		ipsec->tx_tbl[sa_idx].vf = vf;
950 		ipsec->tx_tbl[sa_idx].mode |= IXGBE_RXTXMOD_VF;
951 	}
952 
953 	msgbuf[1] = xs->xso.offload_handle;
954 
955 	return 0;
956 
957 err_aead:
958 	kfree_sensitive(xs->aead);
959 err_xs:
960 	kfree_sensitive(xs);
961 err_out:
962 	msgbuf[1] = err;
963 	return err;
964 }
965 
966 /**
967  * ixgbe_ipsec_vf_del_sa - translate VF request to SA delete
968  * @adapter: board private structure
969  * @msgbuf: The message buffer
970  * @vf: the VF index
971  *
972  * Given the offload_handle sent by the VF, look for the related SA table
973  * entry and use its xs field to call for a delete of the SA.
974  *
975  * Note: We silently ignore requests to delete entries that are already
976  *       set to unused because when a VF is set to "DOWN", the PF first
977  *       gets a reset and clears all the VF's entries; then the VF's
978  *       XFRM stack sends individual deletes for each entry, which the
979  *       reset already removed.  In the future it might be good to try to
980  *       optimize this so not so many unnecessary delete messages are sent.
981  *
982  * Returns 0 or error value
983  **/
984 int ixgbe_ipsec_vf_del_sa(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf)
985 {
986 	struct ixgbe_ipsec *ipsec = adapter->ipsec;
987 	struct xfrm_state *xs;
988 	u32 pfsa = msgbuf[1];
989 	u16 sa_idx;
990 
991 	if (!adapter->vfinfo[vf].trusted) {
992 		e_err(drv, "vf %d attempted to delete an SA\n", vf);
993 		return -EPERM;
994 	}
995 
996 	if (pfsa < IXGBE_IPSEC_BASE_TX_INDEX) {
997 		struct rx_sa *rsa;
998 
999 		sa_idx = pfsa - IXGBE_IPSEC_BASE_RX_INDEX;
1000 		if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
1001 			e_err(drv, "vf %d SA index %d out of range\n",
1002 			      vf, sa_idx);
1003 			return -EINVAL;
1004 		}
1005 
1006 		rsa = &ipsec->rx_tbl[sa_idx];
1007 
1008 		if (!rsa->used)
1009 			return 0;
1010 
1011 		if (!(rsa->mode & IXGBE_RXTXMOD_VF) ||
1012 		    rsa->vf != vf) {
1013 			e_err(drv, "vf %d bad Rx SA index %d\n", vf, sa_idx);
1014 			return -ENOENT;
1015 		}
1016 
1017 		xs = ipsec->rx_tbl[sa_idx].xs;
1018 	} else {
1019 		struct tx_sa *tsa;
1020 
1021 		sa_idx = pfsa - IXGBE_IPSEC_BASE_TX_INDEX;
1022 		if (sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT) {
1023 			e_err(drv, "vf %d SA index %d out of range\n",
1024 			      vf, sa_idx);
1025 			return -EINVAL;
1026 		}
1027 
1028 		tsa = &ipsec->tx_tbl[sa_idx];
1029 
1030 		if (!tsa->used)
1031 			return 0;
1032 
1033 		if (!(tsa->mode & IXGBE_RXTXMOD_VF) ||
1034 		    tsa->vf != vf) {
1035 			e_err(drv, "vf %d bad Tx SA index %d\n", vf, sa_idx);
1036 			return -ENOENT;
1037 		}
1038 
1039 		xs = ipsec->tx_tbl[sa_idx].xs;
1040 	}
1041 
1042 	ixgbe_ipsec_del_sa(adapter->netdev, xs);
1043 
1044 	/* remove the xs that was made-up in the add request */
1045 	kfree_sensitive(xs);
1046 
1047 	return 0;
1048 }
1049 
1050 /**
1051  * ixgbe_ipsec_tx - setup Tx flags for ipsec offload
1052  * @tx_ring: outgoing context
1053  * @first: current data packet
1054  * @itd: ipsec Tx data for later use in building context descriptor
1055  **/
1056 int ixgbe_ipsec_tx(struct ixgbe_ring *tx_ring,
1057 		   struct ixgbe_tx_buffer *first,
1058 		   struct ixgbe_ipsec_tx_data *itd)
1059 {
1060 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(tx_ring->netdev);
1061 	struct ixgbe_ipsec *ipsec = adapter->ipsec;
1062 	struct xfrm_state *xs;
1063 	struct sec_path *sp;
1064 	struct tx_sa *tsa;
1065 
1066 	sp = skb_sec_path(first->skb);
1067 	if (unlikely(!sp->len)) {
1068 		netdev_err(tx_ring->netdev, "%s: no xfrm state len = %d\n",
1069 			   __func__, sp->len);
1070 		return 0;
1071 	}
1072 
1073 	xs = xfrm_input_state(first->skb);
1074 	if (unlikely(!xs)) {
1075 		netdev_err(tx_ring->netdev, "%s: no xfrm_input_state() xs = %p\n",
1076 			   __func__, xs);
1077 		return 0;
1078 	}
1079 
1080 	itd->sa_idx = xs->xso.offload_handle - IXGBE_IPSEC_BASE_TX_INDEX;
1081 	if (unlikely(itd->sa_idx >= IXGBE_IPSEC_MAX_SA_COUNT)) {
1082 		netdev_err(tx_ring->netdev, "%s: bad sa_idx=%d handle=%lu\n",
1083 			   __func__, itd->sa_idx, xs->xso.offload_handle);
1084 		return 0;
1085 	}
1086 
1087 	tsa = &ipsec->tx_tbl[itd->sa_idx];
1088 	if (unlikely(!tsa->used)) {
1089 		netdev_err(tx_ring->netdev, "%s: unused sa_idx=%d\n",
1090 			   __func__, itd->sa_idx);
1091 		return 0;
1092 	}
1093 
1094 	first->tx_flags |= IXGBE_TX_FLAGS_IPSEC | IXGBE_TX_FLAGS_CC;
1095 
1096 	if (xs->id.proto == IPPROTO_ESP) {
1097 
1098 		itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_TYPE_ESP |
1099 			      IXGBE_ADVTXD_TUCMD_L4T_TCP;
1100 		if (first->protocol == htons(ETH_P_IP))
1101 			itd->flags |= IXGBE_ADVTXD_TUCMD_IPV4;
1102 
1103 		/* The actual trailer length is authlen (16 bytes) plus
1104 		 * 2 bytes for the proto and the padlen values, plus
1105 		 * padlen bytes of padding.  This ends up not the same
1106 		 * as the static value found in xs->props.trailer_len (21).
1107 		 *
1108 		 * ... but if we're doing GSO, don't bother as the stack
1109 		 * doesn't add a trailer for those.
1110 		 */
1111 		if (!skb_is_gso(first->skb)) {
1112 			/* The "correct" way to get the auth length would be
1113 			 * to use
1114 			 *    authlen = crypto_aead_authsize(xs->data);
1115 			 * but since we know we only have one size to worry
1116 			 * about * we can let the compiler use the constant
1117 			 * and save us a few CPU cycles.
1118 			 */
1119 			const int authlen = IXGBE_IPSEC_AUTH_BITS / 8;
1120 			struct sk_buff *skb = first->skb;
1121 			u8 padlen;
1122 			int ret;
1123 
1124 			ret = skb_copy_bits(skb, skb->len - (authlen + 2),
1125 					    &padlen, 1);
1126 			if (unlikely(ret))
1127 				return 0;
1128 			itd->trailer_len = authlen + 2 + padlen;
1129 		}
1130 	}
1131 	if (tsa->encrypt)
1132 		itd->flags |= IXGBE_ADVTXD_TUCMD_IPSEC_ENCRYPT_EN;
1133 
1134 	return 1;
1135 }
1136 
1137 /**
1138  * ixgbe_ipsec_rx - decode ipsec bits from Rx descriptor
1139  * @rx_ring: receiving ring
1140  * @rx_desc: receive data descriptor
1141  * @skb: current data packet
1142  *
1143  * Determine if there was an ipsec encapsulation noticed, and if so set up
1144  * the resulting status for later in the receive stack.
1145  **/
1146 void ixgbe_ipsec_rx(struct ixgbe_ring *rx_ring,
1147 		    union ixgbe_adv_rx_desc *rx_desc,
1148 		    struct sk_buff *skb)
1149 {
1150 	struct ixgbe_adapter *adapter = ixgbe_from_netdev(rx_ring->netdev);
1151 	__le16 pkt_info = rx_desc->wb.lower.lo_dword.hs_rss.pkt_info;
1152 	__le16 ipsec_pkt_types = cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH |
1153 					     IXGBE_RXDADV_PKTTYPE_IPSEC_ESP);
1154 	struct ixgbe_ipsec *ipsec = adapter->ipsec;
1155 	struct xfrm_offload *xo = NULL;
1156 	struct xfrm_state *xs = NULL;
1157 	struct ipv6hdr *ip6 = NULL;
1158 	struct iphdr *ip4 = NULL;
1159 	struct sec_path *sp;
1160 	void *daddr;
1161 	__be32 spi;
1162 	u8 *c_hdr;
1163 	u8 proto;
1164 
1165 	/* Find the ip and crypto headers in the data.
1166 	 * We can assume no vlan header in the way, b/c the
1167 	 * hw won't recognize the IPsec packet and anyway the
1168 	 * currently vlan device doesn't support xfrm offload.
1169 	 */
1170 	if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV4)) {
1171 		ip4 = (struct iphdr *)(skb->data + ETH_HLEN);
1172 		daddr = &ip4->daddr;
1173 		c_hdr = (u8 *)ip4 + ip4->ihl * 4;
1174 	} else if (pkt_info & cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPV6)) {
1175 		ip6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
1176 		daddr = &ip6->daddr;
1177 		c_hdr = (u8 *)ip6 + sizeof(struct ipv6hdr);
1178 	} else {
1179 		return;
1180 	}
1181 
1182 	switch (pkt_info & ipsec_pkt_types) {
1183 	case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_AH):
1184 		spi = ((struct ip_auth_hdr *)c_hdr)->spi;
1185 		proto = IPPROTO_AH;
1186 		break;
1187 	case cpu_to_le16(IXGBE_RXDADV_PKTTYPE_IPSEC_ESP):
1188 		spi = ((struct ip_esp_hdr *)c_hdr)->spi;
1189 		proto = IPPROTO_ESP;
1190 		break;
1191 	default:
1192 		return;
1193 	}
1194 
1195 	xs = ixgbe_ipsec_find_rx_state(ipsec, daddr, proto, spi, !!ip4);
1196 	if (unlikely(!xs))
1197 		return;
1198 
1199 	sp = secpath_set(skb);
1200 	if (unlikely(!sp))
1201 		return;
1202 
1203 	sp->xvec[sp->len++] = xs;
1204 	sp->olen++;
1205 	xo = xfrm_offload(skb);
1206 	xo->flags = CRYPTO_DONE;
1207 	xo->status = CRYPTO_SUCCESS;
1208 
1209 	adapter->rx_ipsec++;
1210 }
1211 
1212 /**
1213  * ixgbe_init_ipsec_offload - initialize security registers for IPSec operation
1214  * @adapter: board private structure
1215  **/
1216 void ixgbe_init_ipsec_offload(struct ixgbe_adapter *adapter)
1217 {
1218 	struct ixgbe_hw *hw = &adapter->hw;
1219 	struct ixgbe_ipsec *ipsec;
1220 	u32 t_dis, r_dis;
1221 	size_t size;
1222 
1223 	if (hw->mac.type == ixgbe_mac_82598EB)
1224 		return;
1225 
1226 	/* If there is no support for either Tx or Rx offload
1227 	 * we should not be advertising support for IPsec.
1228 	 */
1229 	t_dis = IXGBE_READ_REG(hw, IXGBE_SECTXSTAT) &
1230 		IXGBE_SECTXSTAT_SECTX_OFF_DIS;
1231 	r_dis = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT) &
1232 		IXGBE_SECRXSTAT_SECRX_OFF_DIS;
1233 	if (t_dis || r_dis)
1234 		return;
1235 
1236 	ipsec = kzalloc(sizeof(*ipsec), GFP_KERNEL);
1237 	if (!ipsec)
1238 		goto err1;
1239 	hash_init(ipsec->rx_sa_list);
1240 
1241 	size = sizeof(struct rx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
1242 	ipsec->rx_tbl = kzalloc(size, GFP_KERNEL);
1243 	if (!ipsec->rx_tbl)
1244 		goto err2;
1245 
1246 	size = sizeof(struct tx_sa) * IXGBE_IPSEC_MAX_SA_COUNT;
1247 	ipsec->tx_tbl = kzalloc(size, GFP_KERNEL);
1248 	if (!ipsec->tx_tbl)
1249 		goto err2;
1250 
1251 	size = sizeof(struct rx_ip_sa) * IXGBE_IPSEC_MAX_RX_IP_COUNT;
1252 	ipsec->ip_tbl = kzalloc(size, GFP_KERNEL);
1253 	if (!ipsec->ip_tbl)
1254 		goto err2;
1255 
1256 	ipsec->num_rx_sa = 0;
1257 	ipsec->num_tx_sa = 0;
1258 
1259 	adapter->ipsec = ipsec;
1260 	ixgbe_ipsec_stop_engine(adapter);
1261 	ixgbe_ipsec_clear_hw_tables(adapter);
1262 
1263 	adapter->netdev->xfrmdev_ops = &ixgbe_xfrmdev_ops;
1264 
1265 	return;
1266 
1267 err2:
1268 	kfree(ipsec->ip_tbl);
1269 	kfree(ipsec->rx_tbl);
1270 	kfree(ipsec->tx_tbl);
1271 	kfree(ipsec);
1272 err1:
1273 	netdev_err(adapter->netdev, "Unable to allocate memory for SA tables");
1274 }
1275 
1276 /**
1277  * ixgbe_stop_ipsec_offload - tear down the ipsec offload
1278  * @adapter: board private structure
1279  **/
1280 void ixgbe_stop_ipsec_offload(struct ixgbe_adapter *adapter)
1281 {
1282 	struct ixgbe_ipsec *ipsec = adapter->ipsec;
1283 
1284 	adapter->ipsec = NULL;
1285 	if (ipsec) {
1286 		kfree(ipsec->ip_tbl);
1287 		kfree(ipsec->rx_tbl);
1288 		kfree(ipsec->tx_tbl);
1289 		kfree(ipsec);
1290 	}
1291 }
1292