linux/drivers/net/ethernet/chelsio/cxgb4/clip_tbl.c
<<
>>
Prefs
   1/*
   2 *  This file is part of the Chelsio T4 Ethernet driver for Linux.
   3 *  Copyright (C) 2003-2014 Chelsio Communications.  All rights reserved.
   4 *
   5 *  Written by Deepak (deepak.s@chelsio.com)
   6 *
   7 *  This program is distributed in the hope that it will be useful, but WITHOUT
   8 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   9 *  FITNESS FOR A PARTICULAR PURPOSE.  See the LICENSE file included in this
  10 *  release for licensing terms and conditions.
  11 */
  12
  13#include <linux/module.h>
  14#include <linux/netdevice.h>
  15#include <linux/jhash.h>
  16#include <linux/if_vlan.h>
  17#include <net/addrconf.h>
  18#include "cxgb4.h"
  19#include "clip_tbl.h"
  20
  21static inline unsigned int ipv4_clip_hash(struct clip_tbl *c, const u32 *key)
  22{
  23        unsigned int clipt_size_half = c->clipt_size / 2;
  24
  25        return jhash_1word(*key, 0) % clipt_size_half;
  26}
  27
  28static inline unsigned int ipv6_clip_hash(struct clip_tbl *d, const u32 *key)
  29{
  30        unsigned int clipt_size_half = d->clipt_size / 2;
  31        u32 xor = key[0] ^ key[1] ^ key[2] ^ key[3];
  32
  33        return clipt_size_half +
  34                (jhash_1word(xor, 0) % clipt_size_half);
  35}
  36
  37static unsigned int clip_addr_hash(struct clip_tbl *ctbl, const u32 *addr,
  38                                   u8 v6)
  39{
  40        return v6 ? ipv6_clip_hash(ctbl, addr) :
  41                        ipv4_clip_hash(ctbl, addr);
  42}
  43
  44static int clip6_get_mbox(const struct net_device *dev,
  45                          const struct in6_addr *lip)
  46{
  47        struct adapter *adap = netdev2adap(dev);
  48        struct fw_clip_cmd c;
  49
  50        memset(&c, 0, sizeof(c));
  51        c.op_to_write = htonl(FW_CMD_OP_V(FW_CLIP_CMD) |
  52                              FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
  53        c.alloc_to_len16 = htonl(FW_CLIP_CMD_ALLOC_F | FW_LEN16(c));
  54        *(__be64 *)&c.ip_hi = *(__be64 *)(lip->s6_addr);
  55        *(__be64 *)&c.ip_lo = *(__be64 *)(lip->s6_addr + 8);
  56        return t4_wr_mbox_meat(adap, adap->mbox, &c, sizeof(c), &c, false);
  57}
  58
  59static int clip6_release_mbox(const struct net_device *dev,
  60                              const struct in6_addr *lip)
  61{
  62        struct adapter *adap = netdev2adap(dev);
  63        struct fw_clip_cmd c;
  64
  65        memset(&c, 0, sizeof(c));
  66        c.op_to_write = htonl(FW_CMD_OP_V(FW_CLIP_CMD) |
  67                              FW_CMD_REQUEST_F | FW_CMD_READ_F);
  68        c.alloc_to_len16 = htonl(FW_CLIP_CMD_FREE_F | FW_LEN16(c));
  69        *(__be64 *)&c.ip_hi = *(__be64 *)(lip->s6_addr);
  70        *(__be64 *)&c.ip_lo = *(__be64 *)(lip->s6_addr + 8);
  71        return t4_wr_mbox_meat(adap, adap->mbox, &c, sizeof(c), &c, false);
  72}
  73
  74int cxgb4_clip_get(const struct net_device *dev, const u32 *lip, u8 v6)
  75{
  76        struct adapter *adap = netdev2adap(dev);
  77        struct clip_tbl *ctbl = adap->clipt;
  78        struct clip_entry *ce, *cte;
  79        u32 *addr = (u32 *)lip;
  80        int hash;
  81        int ret = -1;
  82
  83        if (!ctbl)
  84                return 0;
  85
  86        hash = clip_addr_hash(ctbl, addr, v6);
  87
  88        read_lock_bh(&ctbl->lock);
  89        list_for_each_entry(cte, &ctbl->hash_list[hash], list) {
  90                if (cte->addr6.sin6_family == AF_INET6 && v6)
  91                        ret = memcmp(lip, cte->addr6.sin6_addr.s6_addr,
  92                                     sizeof(struct in6_addr));
  93                else if (cte->addr.sin_family == AF_INET && !v6)
  94                        ret = memcmp(lip, (char *)(&cte->addr.sin_addr),
  95                                     sizeof(struct in_addr));
  96                if (!ret) {
  97                        ce = cte;
  98                        read_unlock_bh(&ctbl->lock);
  99                        goto found;
 100                }
 101        }
 102        read_unlock_bh(&ctbl->lock);
 103
 104        write_lock_bh(&ctbl->lock);
 105        if (!list_empty(&ctbl->ce_free_head)) {
 106                ce = list_first_entry(&ctbl->ce_free_head,
 107                                      struct clip_entry, list);
 108                list_del(&ce->list);
 109                INIT_LIST_HEAD(&ce->list);
 110                spin_lock_init(&ce->lock);
 111                atomic_set(&ce->refcnt, 0);
 112                atomic_dec(&ctbl->nfree);
 113                list_add_tail(&ce->list, &ctbl->hash_list[hash]);
 114                if (v6) {
 115                        ce->addr6.sin6_family = AF_INET6;
 116                        memcpy(ce->addr6.sin6_addr.s6_addr,
 117                               lip, sizeof(struct in6_addr));
 118                        ret = clip6_get_mbox(dev, (const struct in6_addr *)lip);
 119                        if (ret) {
 120                                write_unlock_bh(&ctbl->lock);
 121                                dev_err(adap->pdev_dev,
 122                                        "CLIP FW cmd failed with error %d, "
 123                                        "Connections using %pI6c wont be "
 124                                        "offloaded",
 125                                        ret, ce->addr6.sin6_addr.s6_addr);
 126                                return ret;
 127                        }
 128                } else {
 129                        ce->addr.sin_family = AF_INET;
 130                        memcpy((char *)(&ce->addr.sin_addr), lip,
 131                               sizeof(struct in_addr));
 132                }
 133        } else {
 134                write_unlock_bh(&ctbl->lock);
 135                dev_info(adap->pdev_dev, "CLIP table overflow, "
 136                         "Connections using %pI6c wont be offloaded",
 137                         (void *)lip);
 138                return -ENOMEM;
 139        }
 140        write_unlock_bh(&ctbl->lock);
 141found:
 142        atomic_inc(&ce->refcnt);
 143
 144        return 0;
 145}
 146EXPORT_SYMBOL(cxgb4_clip_get);
 147
 148void cxgb4_clip_release(const struct net_device *dev, const u32 *lip, u8 v6)
 149{
 150        struct adapter *adap = netdev2adap(dev);
 151        struct clip_tbl *ctbl = adap->clipt;
 152        struct clip_entry *ce, *cte;
 153        u32 *addr = (u32 *)lip;
 154        int hash;
 155        int ret = -1;
 156
 157        if (!ctbl)
 158                return;
 159
 160        hash = clip_addr_hash(ctbl, addr, v6);
 161
 162        read_lock_bh(&ctbl->lock);
 163        list_for_each_entry(cte, &ctbl->hash_list[hash], list) {
 164                if (cte->addr6.sin6_family == AF_INET6 && v6)
 165                        ret = memcmp(lip, cte->addr6.sin6_addr.s6_addr,
 166                                     sizeof(struct in6_addr));
 167                else if (cte->addr.sin_family == AF_INET && !v6)
 168                        ret = memcmp(lip, (char *)(&cte->addr.sin_addr),
 169                                     sizeof(struct in_addr));
 170                if (!ret) {
 171                        ce = cte;
 172                        read_unlock_bh(&ctbl->lock);
 173                        goto found;
 174                }
 175        }
 176        read_unlock_bh(&ctbl->lock);
 177
 178        return;
 179found:
 180        write_lock_bh(&ctbl->lock);
 181        spin_lock_bh(&ce->lock);
 182        if (atomic_dec_and_test(&ce->refcnt)) {
 183                list_del(&ce->list);
 184                INIT_LIST_HEAD(&ce->list);
 185                list_add_tail(&ce->list, &ctbl->ce_free_head);
 186                atomic_inc(&ctbl->nfree);
 187                if (v6)
 188                        clip6_release_mbox(dev, (const struct in6_addr *)lip);
 189        }
 190        spin_unlock_bh(&ce->lock);
 191        write_unlock_bh(&ctbl->lock);
 192}
 193EXPORT_SYMBOL(cxgb4_clip_release);
 194
 195/* Retrieves IPv6 addresses from a root device (bond, vlan) associated with
 196 * a physical device.
 197 * The physical device reference is needed to send the actul CLIP command.
 198 */
 199static int cxgb4_update_dev_clip(struct net_device *root_dev,
 200                                 struct net_device *dev)
 201{
 202        struct inet6_dev *idev = NULL;
 203        struct inet6_ifaddr *ifa;
 204        int ret = 0;
 205
 206        idev = __in6_dev_get(root_dev);
 207        if (!idev)
 208                return ret;
 209
 210        read_lock_bh(&idev->lock);
 211        list_for_each_entry(ifa, &idev->addr_list, if_list) {
 212                ret = cxgb4_clip_get(dev, (const u32 *)ifa->addr.s6_addr, 1);
 213                if (ret < 0)
 214                        break;
 215        }
 216        read_unlock_bh(&idev->lock);
 217
 218        return ret;
 219}
 220
 221int cxgb4_update_root_dev_clip(struct net_device *dev)
 222{
 223        struct net_device *root_dev = NULL;
 224        int i, ret = 0;
 225
 226        /* First populate the real net device's IPv6 addresses */
 227        ret = cxgb4_update_dev_clip(dev, dev);
 228        if (ret)
 229                return ret;
 230
 231        /* Parse all bond and vlan devices layered on top of the physical dev */
 232        root_dev = netdev_master_upper_dev_get_rcu(dev);
 233        if (root_dev) {
 234                ret = cxgb4_update_dev_clip(root_dev, dev);
 235                if (ret)
 236                        return ret;
 237        }
 238
 239        for (i = 0; i < VLAN_N_VID; i++) {
 240                root_dev = __vlan_find_dev_deep_rcu(dev, htons(ETH_P_8021Q), i);
 241                if (!root_dev)
 242                        continue;
 243
 244                ret = cxgb4_update_dev_clip(root_dev, dev);
 245                if (ret)
 246                        break;
 247        }
 248
 249        return ret;
 250}
 251EXPORT_SYMBOL(cxgb4_update_root_dev_clip);
 252
 253int clip_tbl_show(struct seq_file *seq, void *v)
 254{
 255        struct adapter *adapter = seq->private;
 256        struct clip_tbl *ctbl = adapter->clipt;
 257        struct clip_entry *ce;
 258        char ip[60];
 259        int i;
 260
 261        read_lock_bh(&ctbl->lock);
 262
 263        seq_puts(seq, "IP Address                  Users\n");
 264        for (i = 0 ; i < ctbl->clipt_size;  ++i) {
 265                list_for_each_entry(ce, &ctbl->hash_list[i], list) {
 266                        ip[0] = '\0';
 267                        sprintf(ip, "%pISc", &ce->addr);
 268                        seq_printf(seq, "%-25s   %u\n", ip,
 269                                   atomic_read(&ce->refcnt));
 270                }
 271        }
 272        seq_printf(seq, "Free clip entries : %d\n", atomic_read(&ctbl->nfree));
 273
 274        read_unlock_bh(&ctbl->lock);
 275
 276        return 0;
 277}
 278
 279struct clip_tbl *t4_init_clip_tbl(unsigned int clipt_start,
 280                                  unsigned int clipt_end)
 281{
 282        struct clip_entry *cl_list;
 283        struct clip_tbl *ctbl;
 284        unsigned int clipt_size;
 285        int i;
 286
 287        if (clipt_start >= clipt_end)
 288                return NULL;
 289        clipt_size = clipt_end - clipt_start + 1;
 290        if (clipt_size < CLIPT_MIN_HASH_BUCKETS)
 291                return NULL;
 292
 293        ctbl = t4_alloc_mem(sizeof(*ctbl) +
 294                            clipt_size*sizeof(struct list_head));
 295        if (!ctbl)
 296                return NULL;
 297
 298        ctbl->clipt_start = clipt_start;
 299        ctbl->clipt_size = clipt_size;
 300        INIT_LIST_HEAD(&ctbl->ce_free_head);
 301
 302        atomic_set(&ctbl->nfree, clipt_size);
 303        rwlock_init(&ctbl->lock);
 304
 305        for (i = 0; i < ctbl->clipt_size; ++i)
 306                INIT_LIST_HEAD(&ctbl->hash_list[i]);
 307
 308        cl_list = t4_alloc_mem(clipt_size*sizeof(struct clip_entry));
 309        if (!cl_list) {
 310                t4_free_mem(ctbl);
 311                return NULL;
 312        }
 313        ctbl->cl_list = (void *)cl_list;
 314
 315        for (i = 0; i < clipt_size; i++) {
 316                INIT_LIST_HEAD(&cl_list[i].list);
 317                list_add_tail(&cl_list[i].list, &ctbl->ce_free_head);
 318        }
 319
 320        return ctbl;
 321}
 322
 323void t4_cleanup_clip_tbl(struct adapter *adap)
 324{
 325        struct clip_tbl *ctbl = adap->clipt;
 326
 327        if (ctbl) {
 328                if (ctbl->cl_list)
 329                        t4_free_mem(ctbl->cl_list);
 330                t4_free_mem(ctbl);
 331        }
 332}
 333EXPORT_SYMBOL(t4_cleanup_clip_tbl);
 334