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