Home | History | Annotate | Line # | Download | only in cxgb
cxgb_l2t.c revision 1.1.4.2
      1  1.1.4.2  rmind /**************************************************************************
      2  1.1.4.2  rmind 
      3  1.1.4.2  rmind Copyright (c) 2007, Chelsio Inc.
      4  1.1.4.2  rmind All rights reserved.
      5  1.1.4.2  rmind 
      6  1.1.4.2  rmind Redistribution and use in source and binary forms, with or without
      7  1.1.4.2  rmind modification, are permitted provided that the following conditions are met:
      8  1.1.4.2  rmind 
      9  1.1.4.2  rmind  1. Redistributions of source code must retain the above copyright notice,
     10  1.1.4.2  rmind     this list of conditions and the following disclaimer.
     11  1.1.4.2  rmind 
     12  1.1.4.2  rmind  2. Neither the name of the Chelsio Corporation nor the names of its
     13  1.1.4.2  rmind     contributors may be used to endorse or promote products derived from
     14  1.1.4.2  rmind     this software without specific prior written permission.
     15  1.1.4.2  rmind 
     16  1.1.4.2  rmind THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     17  1.1.4.2  rmind AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1.4.2  rmind IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1.4.2  rmind ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     20  1.1.4.2  rmind LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1.4.2  rmind CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1.4.2  rmind SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1.4.2  rmind INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1.4.2  rmind CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1.4.2  rmind ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1.4.2  rmind POSSIBILITY OF SUCH DAMAGE.
     27  1.1.4.2  rmind 
     28  1.1.4.2  rmind ***************************************************************************/
     29  1.1.4.2  rmind 
     30  1.1.4.2  rmind #include <sys/cdefs.h>
     31  1.1.4.2  rmind __KERNEL_RCSID(0, "$NetBSD: cxgb_l2t.c,v 1.1.4.2 2010/05/30 05:17:40 rmind Exp $");
     32  1.1.4.2  rmind 
     33  1.1.4.2  rmind #include <sys/param.h>
     34  1.1.4.2  rmind #include <sys/systm.h>
     35  1.1.4.2  rmind #include <sys/kernel.h>
     36  1.1.4.2  rmind #include <sys/lock.h>
     37  1.1.4.2  rmind #include <sys/mutex.h>
     38  1.1.4.2  rmind 
     39  1.1.4.2  rmind #include <sys/socket.h>
     40  1.1.4.2  rmind #include <sys/socketvar.h>
     41  1.1.4.2  rmind #include <net/if.h>
     42  1.1.4.2  rmind #include <netinet/in.h>
     43  1.1.4.2  rmind #include <netinet/in_var.h>
     44  1.1.4.2  rmind #include <netinet/if_inarp.h>
     45  1.1.4.2  rmind #include <net/if_dl.h>
     46  1.1.4.2  rmind #include <net/route.h>
     47  1.1.4.2  rmind #include <netinet/in.h>
     48  1.1.4.2  rmind 
     49  1.1.4.2  rmind #ifdef CONFIG_DEFINED
     50  1.1.4.2  rmind #include <cxgb_include.h>
     51  1.1.4.2  rmind #else
     52  1.1.4.2  rmind #include "cxgb_include.h"
     53  1.1.4.2  rmind #endif
     54  1.1.4.2  rmind 
     55  1.1.4.2  rmind #define VLAN_NONE 0xfff
     56  1.1.4.2  rmind #define SDL(s) ((struct sockaddr_dl *)s)
     57  1.1.4.2  rmind #define RT_ENADDR(rt)  ((u_char *)LLADDR(SDL((rt))))
     58  1.1.4.2  rmind #define rt_expire rt_rmx.rmx_expire
     59  1.1.4.2  rmind 
     60  1.1.4.2  rmind /*
     61  1.1.4.2  rmind  * Module locking notes:  There is a RW lock protecting the L2 table as a
     62  1.1.4.2  rmind  * whole plus a spinlock per L2T entry.  Entry lookups and allocations happen
     63  1.1.4.2  rmind  * under the protection of the table lock, individual entry changes happen
     64  1.1.4.2  rmind  * while holding that entry's spinlock.  The table lock nests outside the
     65  1.1.4.2  rmind  * entry locks.  Allocations of new entries take the table lock as writers so
     66  1.1.4.2  rmind  * no other lookups can happen while allocating new entries.  Entry updates
     67  1.1.4.2  rmind  * take the table lock as readers so multiple entries can be updated in
     68  1.1.4.2  rmind  * parallel.  An L2T entry can be dropped by decrementing its reference count
     69  1.1.4.2  rmind  * and therefore can happen in parallel with entry allocation but no entry
     70  1.1.4.2  rmind  * can change state or increment its ref count during allocation as both of
     71  1.1.4.2  rmind  * these perform lookups.
     72  1.1.4.2  rmind  */
     73  1.1.4.2  rmind 
     74  1.1.4.2  rmind static inline unsigned int
     75  1.1.4.2  rmind vlan_prio(const struct l2t_entry *e)
     76  1.1.4.2  rmind {
     77  1.1.4.2  rmind     return e->vlan >> 13;
     78  1.1.4.2  rmind }
     79  1.1.4.2  rmind 
     80  1.1.4.2  rmind static inline unsigned int
     81  1.1.4.2  rmind arp_hash(u32 key, int ifindex, const struct l2t_data *d)
     82  1.1.4.2  rmind {
     83  1.1.4.2  rmind     return jhash_2words(key, ifindex, 0) & (d->nentries - 1);
     84  1.1.4.2  rmind }
     85  1.1.4.2  rmind 
     86  1.1.4.2  rmind static inline void
     87  1.1.4.2  rmind neigh_replace(struct l2t_entry *e, struct rtentry *rt)
     88  1.1.4.2  rmind {
     89  1.1.4.2  rmind     RT_LOCK(rt);
     90  1.1.4.2  rmind     RT_ADDREF(rt);
     91  1.1.4.2  rmind     RT_UNLOCK(rt);
     92  1.1.4.2  rmind 
     93  1.1.4.2  rmind     if (e->neigh) {
     94  1.1.4.2  rmind         RT_LOCK(e->neigh);
     95  1.1.4.2  rmind         RT_REMREF(e->neigh);
     96  1.1.4.2  rmind         RT_UNLOCK(e->neigh);
     97  1.1.4.2  rmind     }
     98  1.1.4.2  rmind     e->neigh = rt;
     99  1.1.4.2  rmind }
    100  1.1.4.2  rmind 
    101  1.1.4.2  rmind /*
    102  1.1.4.2  rmind  * Set up an L2T entry and send any packets waiting in the arp queue.  The
    103  1.1.4.2  rmind  * supplied mbuf is used for the CPL_L2T_WRITE_REQ.  Must be called with the
    104  1.1.4.2  rmind  * entry locked.
    105  1.1.4.2  rmind  */
    106  1.1.4.2  rmind static int
    107  1.1.4.2  rmind setup_l2e_send_pending(struct toedev *dev, struct mbuf *m,
    108  1.1.4.2  rmind             struct l2t_entry *e)
    109  1.1.4.2  rmind {
    110  1.1.4.2  rmind     struct cpl_l2t_write_req *req;
    111  1.1.4.2  rmind 
    112  1.1.4.2  rmind     if (!m) {
    113  1.1.4.2  rmind         if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
    114  1.1.4.2  rmind             return (ENOMEM);
    115  1.1.4.2  rmind     }
    116  1.1.4.2  rmind     /*
    117  1.1.4.2  rmind      * XXX MH_ALIGN
    118  1.1.4.2  rmind      */
    119  1.1.4.2  rmind     req = mtod(m, struct cpl_l2t_write_req *);
    120  1.1.4.2  rmind     req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
    121  1.1.4.2  rmind     OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, e->idx));
    122  1.1.4.2  rmind     req->params = htonl(V_L2T_W_IDX(e->idx) | V_L2T_W_IFF(e->smt_idx) |
    123  1.1.4.2  rmind                 V_L2T_W_VLAN(e->vlan & EVL_VLID_MASK) |
    124  1.1.4.2  rmind                 V_L2T_W_PRIO(vlan_prio(e)));
    125  1.1.4.2  rmind 
    126  1.1.4.2  rmind     memcpy(e->dmac, RT_ENADDR(e->neigh), sizeof(e->dmac));
    127  1.1.4.2  rmind     memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
    128  1.1.4.2  rmind     m_set_priority(m, CPL_PRIORITY_CONTROL);
    129  1.1.4.2  rmind     while (e->arpq_head) {
    130  1.1.4.2  rmind         m = e->arpq_head;
    131  1.1.4.2  rmind         e->arpq_head = m->m_next;
    132  1.1.4.2  rmind         m->m_next = NULL;
    133  1.1.4.2  rmind     }
    134  1.1.4.2  rmind     e->arpq_tail = NULL;
    135  1.1.4.2  rmind     e->state = L2T_STATE_VALID;
    136  1.1.4.2  rmind 
    137  1.1.4.2  rmind     return 0;
    138  1.1.4.2  rmind }
    139  1.1.4.2  rmind 
    140  1.1.4.2  rmind /*
    141  1.1.4.2  rmind  * Add a packet to the an L2T entry's queue of packets awaiting resolution.
    142  1.1.4.2  rmind  * Must be called with the entry's lock held.
    143  1.1.4.2  rmind  */
    144  1.1.4.2  rmind static inline void
    145  1.1.4.2  rmind arpq_enqueue(struct l2t_entry *e, struct mbuf *m)
    146  1.1.4.2  rmind {
    147  1.1.4.2  rmind     m->m_next = NULL;
    148  1.1.4.2  rmind     if (e->arpq_head)
    149  1.1.4.2  rmind         e->arpq_tail->m_next = m;
    150  1.1.4.2  rmind     else
    151  1.1.4.2  rmind         e->arpq_head = m;
    152  1.1.4.2  rmind     e->arpq_tail = m;
    153  1.1.4.2  rmind }
    154  1.1.4.2  rmind 
    155  1.1.4.2  rmind int
    156  1.1.4.2  rmind t3_l2t_send_slow(struct toedev *dev, struct mbuf *m,
    157  1.1.4.2  rmind              struct l2t_entry *e)
    158  1.1.4.2  rmind {
    159  1.1.4.2  rmind     struct rtentry *rt;
    160  1.1.4.2  rmind     struct mbuf *m0;
    161  1.1.4.2  rmind 
    162  1.1.4.2  rmind     if ((m0 = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
    163  1.1.4.2  rmind         return (ENOMEM);
    164  1.1.4.2  rmind 
    165  1.1.4.2  rmind     rt = e->neigh;
    166  1.1.4.2  rmind 
    167  1.1.4.2  rmind again:
    168  1.1.4.2  rmind     switch (e->state) {
    169  1.1.4.2  rmind     case L2T_STATE_STALE:     /* entry is stale, kick off revalidation */
    170  1.1.4.2  rmind         arpresolve(rt->rt_ifp, rt, m0, rt->rt_gateway, RT_ENADDR(rt));
    171  1.1.4.2  rmind         mtx_lock(&e->lock);
    172  1.1.4.2  rmind         if (e->state == L2T_STATE_STALE)
    173  1.1.4.2  rmind             e->state = L2T_STATE_VALID;
    174  1.1.4.2  rmind         mtx_unlock(&e->lock);
    175  1.1.4.2  rmind     case L2T_STATE_VALID:     /* fast-path, send the packet on */
    176  1.1.4.2  rmind     case L2T_STATE_RESOLVING:
    177  1.1.4.2  rmind         mtx_lock(&e->lock);
    178  1.1.4.2  rmind         if (e->state != L2T_STATE_RESOLVING) { // ARP already completed
    179  1.1.4.2  rmind             mtx_unlock(&e->lock);
    180  1.1.4.2  rmind             goto again;
    181  1.1.4.2  rmind         }
    182  1.1.4.2  rmind         arpq_enqueue(e, m);
    183  1.1.4.2  rmind         mtx_unlock(&e->lock);
    184  1.1.4.2  rmind 
    185  1.1.4.2  rmind         if ((m0 = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
    186  1.1.4.2  rmind             return (ENOMEM);
    187  1.1.4.2  rmind         /*
    188  1.1.4.2  rmind          * Only the first packet added to the arpq should kick off
    189  1.1.4.2  rmind          * resolution.  However, because the m_gethdr below can fail,
    190  1.1.4.2  rmind          * we allow each packet added to the arpq to retry resolution
    191  1.1.4.2  rmind          * as a way of recovering from transient memory exhaustion.
    192  1.1.4.2  rmind          * A better way would be to use a work request to retry L2T
    193  1.1.4.2  rmind          * entries when there's no memory.
    194  1.1.4.2  rmind          */
    195  1.1.4.2  rmind         if (arpresolve(rt->rt_ifp, rt, m0, rt->rt_gateway, RT_ENADDR(rt)) == 0) {
    196  1.1.4.2  rmind 
    197  1.1.4.2  rmind             mtx_lock(&e->lock);
    198  1.1.4.2  rmind             if (e->arpq_head)
    199  1.1.4.2  rmind                 setup_l2e_send_pending(dev, m, e);
    200  1.1.4.2  rmind             else
    201  1.1.4.2  rmind                 m_freem(m);
    202  1.1.4.2  rmind             mtx_unlock(&e->lock);
    203  1.1.4.2  rmind         }
    204  1.1.4.2  rmind     }
    205  1.1.4.2  rmind     return 0;
    206  1.1.4.2  rmind }
    207  1.1.4.2  rmind 
    208  1.1.4.2  rmind void
    209  1.1.4.2  rmind t3_l2t_send_event(struct toedev *dev, struct l2t_entry *e)
    210  1.1.4.2  rmind {
    211  1.1.4.2  rmind     struct rtentry *rt;
    212  1.1.4.2  rmind     struct mbuf *m0;
    213  1.1.4.2  rmind 
    214  1.1.4.2  rmind     if ((m0 = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
    215  1.1.4.2  rmind         return;
    216  1.1.4.2  rmind 
    217  1.1.4.2  rmind     rt = e->neigh;
    218  1.1.4.2  rmind again:
    219  1.1.4.2  rmind     switch (e->state) {
    220  1.1.4.2  rmind     case L2T_STATE_STALE:     /* entry is stale, kick off revalidation */
    221  1.1.4.2  rmind         arpresolve(rt->rt_ifp, rt, m0, rt->rt_gateway, RT_ENADDR(rt));
    222  1.1.4.2  rmind         mtx_lock(&e->lock);
    223  1.1.4.2  rmind         if (e->state == L2T_STATE_STALE) {
    224  1.1.4.2  rmind             e->state = L2T_STATE_VALID;
    225  1.1.4.2  rmind         }
    226  1.1.4.2  rmind         mtx_unlock(&e->lock);
    227  1.1.4.2  rmind         return;
    228  1.1.4.2  rmind     case L2T_STATE_VALID:     /* fast-path, send the packet on */
    229  1.1.4.2  rmind         return;
    230  1.1.4.2  rmind     case L2T_STATE_RESOLVING:
    231  1.1.4.2  rmind         mtx_lock(&e->lock);
    232  1.1.4.2  rmind         if (e->state != L2T_STATE_RESOLVING) { // ARP already completed
    233  1.1.4.2  rmind             mtx_unlock(&e->lock);
    234  1.1.4.2  rmind             goto again;
    235  1.1.4.2  rmind         }
    236  1.1.4.2  rmind         mtx_unlock(&e->lock);
    237  1.1.4.2  rmind 
    238  1.1.4.2  rmind         if ((m0 = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
    239  1.1.4.2  rmind             return;
    240  1.1.4.2  rmind         /*
    241  1.1.4.2  rmind          * Only the first packet added to the arpq should kick off
    242  1.1.4.2  rmind          * resolution.  However, because the alloc_skb below can fail,
    243  1.1.4.2  rmind          * we allow each packet added to the arpq to retry resolution
    244  1.1.4.2  rmind          * as a way of recovering from transient memory exhaustion.
    245  1.1.4.2  rmind          * A better way would be to use a work request to retry L2T
    246  1.1.4.2  rmind          * entries when there's no memory.
    247  1.1.4.2  rmind          */
    248  1.1.4.2  rmind         arpresolve(rt->rt_ifp, rt, m0, rt->rt_gateway, RT_ENADDR(rt));
    249  1.1.4.2  rmind 
    250  1.1.4.2  rmind     }
    251  1.1.4.2  rmind     return;
    252  1.1.4.2  rmind }
    253  1.1.4.2  rmind /*
    254  1.1.4.2  rmind  * Allocate a free L2T entry.  Must be called with l2t_data.lock held.
    255  1.1.4.2  rmind  */
    256  1.1.4.2  rmind static struct l2t_entry *
    257  1.1.4.2  rmind alloc_l2e(struct l2t_data *d)
    258  1.1.4.2  rmind {
    259  1.1.4.2  rmind     struct l2t_entry *end, *e, **p;
    260  1.1.4.2  rmind 
    261  1.1.4.2  rmind     if (!atomic_load_acq_int(&d->nfree))
    262  1.1.4.2  rmind         return NULL;
    263  1.1.4.2  rmind 
    264  1.1.4.2  rmind     /* there's definitely a free entry */
    265  1.1.4.2  rmind     for (e = d->rover, end = &d->l2tab[d->nentries]; e != end; ++e)
    266  1.1.4.2  rmind         if (atomic_load_acq_int(&e->refcnt) == 0)
    267  1.1.4.2  rmind             goto found;
    268  1.1.4.2  rmind 
    269  1.1.4.2  rmind     for (e = &d->l2tab[1]; atomic_load_acq_int(&e->refcnt); ++e) ;
    270  1.1.4.2  rmind found:
    271  1.1.4.2  rmind     d->rover = e + 1;
    272  1.1.4.2  rmind     atomic_add_int(&d->nfree, -1);
    273  1.1.4.2  rmind 
    274  1.1.4.2  rmind     /*
    275  1.1.4.2  rmind      * The entry we found may be an inactive entry that is
    276  1.1.4.2  rmind      * presently in the hash table.  We need to remove it.
    277  1.1.4.2  rmind      */
    278  1.1.4.2  rmind     if (e->state != L2T_STATE_UNUSED) {
    279  1.1.4.2  rmind         int hash = arp_hash(e->addr, e->ifindex, d);
    280  1.1.4.2  rmind 
    281  1.1.4.2  rmind         for (p = &d->l2tab[hash].first; *p; p = &(*p)->next)
    282  1.1.4.2  rmind             if (*p == e) {
    283  1.1.4.2  rmind                 *p = e->next;
    284  1.1.4.2  rmind                 break;
    285  1.1.4.2  rmind             }
    286  1.1.4.2  rmind         e->state = L2T_STATE_UNUSED;
    287  1.1.4.2  rmind     }
    288  1.1.4.2  rmind     return e;
    289  1.1.4.2  rmind }
    290  1.1.4.2  rmind 
    291  1.1.4.2  rmind /*
    292  1.1.4.2  rmind  * Called when an L2T entry has no more users.  The entry is left in the hash
    293  1.1.4.2  rmind  * table since it is likely to be reused but we also bump nfree to indicate
    294  1.1.4.2  rmind  * that the entry can be reallocated for a different neighbor.  We also drop
    295  1.1.4.2  rmind  * the existing neighbor reference in case the neighbor is going away and is
    296  1.1.4.2  rmind  * waiting on our reference.
    297  1.1.4.2  rmind  *
    298  1.1.4.2  rmind  * Because entries can be reallocated to other neighbors once their ref count
    299  1.1.4.2  rmind  * drops to 0 we need to take the entry's lock to avoid races with a new
    300  1.1.4.2  rmind  * incarnation.
    301  1.1.4.2  rmind  */
    302  1.1.4.2  rmind void
    303  1.1.4.2  rmind t3_l2e_free(struct l2t_data *d, struct l2t_entry *e)
    304  1.1.4.2  rmind {
    305  1.1.4.2  rmind     mtx_lock(&e->lock);
    306  1.1.4.2  rmind     if (atomic_load_acq_int(&e->refcnt) == 0) {  /* hasn't been recycled */
    307  1.1.4.2  rmind         if (e->neigh) {
    308  1.1.4.2  rmind             RT_LOCK(e->neigh);
    309  1.1.4.2  rmind             RT_REMREF(e->neigh);
    310  1.1.4.2  rmind             RT_UNLOCK(e->neigh);
    311  1.1.4.2  rmind             e->neigh = NULL;
    312  1.1.4.2  rmind         }
    313  1.1.4.2  rmind     }
    314  1.1.4.2  rmind     mtx_unlock(&e->lock);
    315  1.1.4.2  rmind     atomic_add_int(&d->nfree, 1);
    316  1.1.4.2  rmind }
    317  1.1.4.2  rmind 
    318  1.1.4.2  rmind /*
    319  1.1.4.2  rmind  * Update an L2T entry that was previously used for the same next hop as neigh.
    320  1.1.4.2  rmind  * Must be called with softirqs disabled.
    321  1.1.4.2  rmind  */
    322  1.1.4.2  rmind static inline void
    323  1.1.4.2  rmind reuse_entry(struct l2t_entry *e, struct rtentry *neigh)
    324  1.1.4.2  rmind {
    325  1.1.4.2  rmind     struct llinfo_arp *la;
    326  1.1.4.2  rmind 
    327  1.1.4.2  rmind     la = (struct llinfo_arp *)neigh->rt_llinfo;
    328  1.1.4.2  rmind 
    329  1.1.4.2  rmind     mtx_lock(&e->lock);                /* avoid race with t3_l2t_free */
    330  1.1.4.2  rmind     if (neigh != e->neigh)
    331  1.1.4.2  rmind         neigh_replace(e, neigh);
    332  1.1.4.2  rmind 
    333  1.1.4.2  rmind     if (memcmp(e->dmac, RT_ENADDR(neigh), sizeof(e->dmac)) ||
    334  1.1.4.2  rmind         (neigh->rt_expire > time_uptime))
    335  1.1.4.2  rmind         e->state = L2T_STATE_RESOLVING;
    336  1.1.4.2  rmind     else if (la->la_hold == NULL)
    337  1.1.4.2  rmind         e->state = L2T_STATE_VALID;
    338  1.1.4.2  rmind     else
    339  1.1.4.2  rmind         e->state = L2T_STATE_STALE;
    340  1.1.4.2  rmind     mtx_unlock(&e->lock);
    341  1.1.4.2  rmind }
    342  1.1.4.2  rmind 
    343  1.1.4.2  rmind struct l2t_entry *
    344  1.1.4.2  rmind t3_l2t_get(struct toedev *dev, struct rtentry *neigh,
    345  1.1.4.2  rmind                  unsigned int smt_idx)
    346  1.1.4.2  rmind {
    347  1.1.4.2  rmind     struct l2t_entry *e;
    348  1.1.4.2  rmind     struct l2t_data *d = L2DATA(dev);
    349  1.1.4.2  rmind     u32 addr = *(u32 *)neigh->_rt_key;
    350  1.1.4.2  rmind     int ifidx = neigh->rt_ifp->if_index;
    351  1.1.4.2  rmind     int hash = arp_hash(addr, ifidx, d);
    352  1.1.4.2  rmind 
    353  1.1.4.2  rmind     rw_wlock(&d->lock);
    354  1.1.4.2  rmind     for (e = d->l2tab[hash].first; e; e = e->next)
    355  1.1.4.2  rmind         if (e->addr == addr && e->ifindex == ifidx &&
    356  1.1.4.2  rmind             e->smt_idx == smt_idx) {
    357  1.1.4.2  rmind             l2t_hold(d, e);
    358  1.1.4.2  rmind             if (atomic_load_acq_int(&e->refcnt) == 1)
    359  1.1.4.2  rmind                 reuse_entry(e, neigh);
    360  1.1.4.2  rmind             goto done;
    361  1.1.4.2  rmind         }
    362  1.1.4.2  rmind 
    363  1.1.4.2  rmind     /* Need to allocate a new entry */
    364  1.1.4.2  rmind     e = alloc_l2e(d);
    365  1.1.4.2  rmind     if (e) {
    366  1.1.4.2  rmind         mtx_lock(&e->lock);          /* avoid race with t3_l2t_free */
    367  1.1.4.2  rmind         e->next = d->l2tab[hash].first;
    368  1.1.4.2  rmind         d->l2tab[hash].first = e;
    369  1.1.4.2  rmind         e->state = L2T_STATE_RESOLVING;
    370  1.1.4.2  rmind         e->addr = addr;
    371  1.1.4.2  rmind         e->ifindex = ifidx;
    372  1.1.4.2  rmind         e->smt_idx = smt_idx;
    373  1.1.4.2  rmind         atomic_store_rel_int(&e->refcnt, 1);
    374  1.1.4.2  rmind         neigh_replace(e, neigh);
    375  1.1.4.2  rmind #ifdef notyet
    376  1.1.4.2  rmind         /*
    377  1.1.4.2  rmind          * XXX need to add accessor function for vlan tag
    378  1.1.4.2  rmind          */
    379  1.1.4.2  rmind         if (neigh->rt_ifp->if_vlantrunk)
    380  1.1.4.2  rmind             e->vlan = VLAN_DEV_INFO(neigh->dev)->vlan_id;
    381  1.1.4.2  rmind         else
    382  1.1.4.2  rmind #endif
    383  1.1.4.2  rmind             e->vlan = VLAN_NONE;
    384  1.1.4.2  rmind         mtx_unlock(&e->lock);
    385  1.1.4.2  rmind     }
    386  1.1.4.2  rmind done:
    387  1.1.4.2  rmind     rw_wunlock(&d->lock);
    388  1.1.4.2  rmind     return e;
    389  1.1.4.2  rmind }
    390  1.1.4.2  rmind 
    391  1.1.4.2  rmind /*
    392  1.1.4.2  rmind  * Called when address resolution fails for an L2T entry to handle packets
    393  1.1.4.2  rmind  * on the arpq head.  If a packet specifies a failure handler it is invoked,
    394  1.1.4.2  rmind  * otherwise the packets is sent to the TOE.
    395  1.1.4.2  rmind  *
    396  1.1.4.2  rmind  * XXX: maybe we should abandon the latter behavior and just require a failure
    397  1.1.4.2  rmind  * handler.
    398  1.1.4.2  rmind  */
    399  1.1.4.2  rmind static void
    400  1.1.4.2  rmind handle_failed_resolution(struct toedev *dev, struct mbuf *arpq)
    401  1.1.4.2  rmind {
    402  1.1.4.2  rmind 
    403  1.1.4.2  rmind     while (arpq) {
    404  1.1.4.2  rmind         struct mbuf *m = arpq;
    405  1.1.4.2  rmind #ifdef notyet
    406  1.1.4.2  rmind         struct l2t_mbuf_cb *cb = L2T_MBUF_CB(m);
    407  1.1.4.2  rmind #endif
    408  1.1.4.2  rmind         arpq = m->m_next;
    409  1.1.4.2  rmind         m->m_next = NULL;
    410  1.1.4.2  rmind #ifdef notyet
    411  1.1.4.2  rmind         if (cb->arp_failure_handler)
    412  1.1.4.2  rmind             cb->arp_failure_handler(dev, m);
    413  1.1.4.2  rmind         else
    414  1.1.4.2  rmind #endif
    415  1.1.4.2  rmind     }
    416  1.1.4.2  rmind 
    417  1.1.4.2  rmind }
    418  1.1.4.2  rmind 
    419  1.1.4.2  rmind #if defined(NETEVENT) || !defined(CONFIG_CHELSIO_T3_MODULE)
    420  1.1.4.2  rmind /*
    421  1.1.4.2  rmind  * Called when the host's ARP layer makes a change to some entry that is
    422  1.1.4.2  rmind  * loaded into the HW L2 table.
    423  1.1.4.2  rmind  */
    424  1.1.4.2  rmind void
    425  1.1.4.2  rmind t3_l2t_update(struct toedev *dev, struct rtentry *neigh)
    426  1.1.4.2  rmind {
    427  1.1.4.2  rmind     struct l2t_entry *e;
    428  1.1.4.2  rmind     struct mbuf *arpq = NULL;
    429  1.1.4.2  rmind     struct l2t_data *d = L2DATA(dev);
    430  1.1.4.2  rmind     u32 addr = *(u32 *)neigh->_rt_key;
    431  1.1.4.2  rmind     int ifidx = neigh->rt_ifp->if_index;
    432  1.1.4.2  rmind     int hash = arp_hash(addr, ifidx, d);
    433  1.1.4.2  rmind     struct llinfo_arp *la;
    434  1.1.4.2  rmind 
    435  1.1.4.2  rmind     rw_rlock(&d->lock);
    436  1.1.4.2  rmind     for (e = d->l2tab[hash].first; e; e = e->next)
    437  1.1.4.2  rmind         if (e->addr == addr && e->ifindex == ifidx) {
    438  1.1.4.2  rmind             mtx_lock(&e->lock);
    439  1.1.4.2  rmind             goto found;
    440  1.1.4.2  rmind         }
    441  1.1.4.2  rmind     rw_runlock(&d->lock);
    442  1.1.4.2  rmind     return;
    443  1.1.4.2  rmind 
    444  1.1.4.2  rmind found:
    445  1.1.4.2  rmind     rw_runlock(&d->lock);
    446  1.1.4.2  rmind     if (atomic_load_acq_int(&e->refcnt)) {
    447  1.1.4.2  rmind         if (neigh != e->neigh)
    448  1.1.4.2  rmind             neigh_replace(e, neigh);
    449  1.1.4.2  rmind 
    450  1.1.4.2  rmind         la = (struct llinfo_arp *)neigh->rt_llinfo;
    451  1.1.4.2  rmind         if (e->state == L2T_STATE_RESOLVING) {
    452  1.1.4.2  rmind 
    453  1.1.4.2  rmind             if (la->la_asked >= 5 /* arp_maxtries */) {
    454  1.1.4.2  rmind                 arpq = e->arpq_head;
    455  1.1.4.2  rmind                 e->arpq_head = e->arpq_tail = NULL;
    456  1.1.4.2  rmind             } else if (la->la_hold == NULL)
    457  1.1.4.2  rmind                 setup_l2e_send_pending(dev, NULL, e);
    458  1.1.4.2  rmind         } else {
    459  1.1.4.2  rmind             e->state = (la->la_hold == NULL) ?
    460  1.1.4.2  rmind                 L2T_STATE_VALID : L2T_STATE_STALE;
    461  1.1.4.2  rmind             if (memcmp(e->dmac, RT_ENADDR(neigh), 6))
    462  1.1.4.2  rmind                 setup_l2e_send_pending(dev, NULL, e);
    463  1.1.4.2  rmind         }
    464  1.1.4.2  rmind     }
    465  1.1.4.2  rmind     mtx_unlock(&e->lock);
    466  1.1.4.2  rmind 
    467  1.1.4.2  rmind     if (arpq)
    468  1.1.4.2  rmind         handle_failed_resolution(dev, arpq);
    469  1.1.4.2  rmind }
    470  1.1.4.2  rmind #else
    471  1.1.4.2  rmind /*
    472  1.1.4.2  rmind  * Called from a kprobe, interrupts are off.
    473  1.1.4.2  rmind  */
    474  1.1.4.2  rmind void
    475  1.1.4.2  rmind t3_l2t_update(struct toedev *dev, struct rtentry *neigh)
    476  1.1.4.2  rmind {
    477  1.1.4.2  rmind     struct l2t_entry *e;
    478  1.1.4.2  rmind     struct l2t_data *d = L2DATA(dev);
    479  1.1.4.2  rmind     u32 addr = *(u32 *) rt_key(neigh);
    480  1.1.4.2  rmind     int ifidx = neigh->dev->ifindex;
    481  1.1.4.2  rmind     int hash = arp_hash(addr, ifidx, d);
    482  1.1.4.2  rmind 
    483  1.1.4.2  rmind     rw_rlock(&d->lock);
    484  1.1.4.2  rmind     for (e = d->l2tab[hash].first; e; e = e->next)
    485  1.1.4.2  rmind         if (e->addr == addr && e->ifindex == ifidx) {
    486  1.1.4.2  rmind             mtx_lock(&e->lock);
    487  1.1.4.2  rmind             if (atomic_load_acq_int(&e->refcnt)) {
    488  1.1.4.2  rmind                 if (neigh != e->neigh)
    489  1.1.4.2  rmind                     neigh_replace(e, neigh);
    490  1.1.4.2  rmind                 e->tdev = dev;
    491  1.1.4.2  rmind                 mod_timer(&e->update_timer, jiffies + 1);
    492  1.1.4.2  rmind             }
    493  1.1.4.2  rmind             mtx_unlock(&e->lock);
    494  1.1.4.2  rmind             break;
    495  1.1.4.2  rmind         }
    496  1.1.4.2  rmind     rw_runlock(&d->lock);
    497  1.1.4.2  rmind }
    498  1.1.4.2  rmind 
    499  1.1.4.2  rmind static void
    500  1.1.4.2  rmind update_timer_cb(unsigned long data)
    501  1.1.4.2  rmind {
    502  1.1.4.2  rmind     struct mbuf *arpq = NULL;
    503  1.1.4.2  rmind     struct l2t_entry *e = (struct l2t_entry *)data;
    504  1.1.4.2  rmind     struct rtentry *neigh = e->neigh;
    505  1.1.4.2  rmind     struct toedev *dev = e->tdev;
    506  1.1.4.2  rmind 
    507  1.1.4.2  rmind     barrier();
    508  1.1.4.2  rmind     if (!atomic_load_acq_int(&e->refcnt))
    509  1.1.4.2  rmind         return;
    510  1.1.4.2  rmind 
    511  1.1.4.2  rmind     rw_rlock(&neigh->lock);
    512  1.1.4.2  rmind     mtx_lock(&e->lock);
    513  1.1.4.2  rmind 
    514  1.1.4.2  rmind     if (atomic_load_acq_int(&e->refcnt)) {
    515  1.1.4.2  rmind         if (e->state == L2T_STATE_RESOLVING) {
    516  1.1.4.2  rmind             if (neigh->nud_state & NUD_FAILED) {
    517  1.1.4.2  rmind                 arpq = e->arpq_head;
    518  1.1.4.2  rmind                 e->arpq_head = e->arpq_tail = NULL;
    519  1.1.4.2  rmind             } else if (neigh_is_connected(neigh) && e->arpq_head)
    520  1.1.4.2  rmind                 setup_l2e_send_pending(dev, NULL, e);
    521  1.1.4.2  rmind         } else {
    522  1.1.4.2  rmind             e->state = neigh_is_connected(neigh) ?
    523  1.1.4.2  rmind                 L2T_STATE_VALID : L2T_STATE_STALE;
    524  1.1.4.2  rmind             if (memcmp(e->dmac, RT_ENADDR(neigh), sizeof(e->dmac)))
    525  1.1.4.2  rmind                 setup_l2e_send_pending(dev, NULL, e);
    526  1.1.4.2  rmind         }
    527  1.1.4.2  rmind     }
    528  1.1.4.2  rmind     mtx_unlock(&e->lock);
    529  1.1.4.2  rmind     rw_runlock(&neigh->lock);
    530  1.1.4.2  rmind 
    531  1.1.4.2  rmind     if (arpq)
    532  1.1.4.2  rmind         handle_failed_resolution(dev, arpq);
    533  1.1.4.2  rmind }
    534  1.1.4.2  rmind #endif
    535  1.1.4.2  rmind 
    536  1.1.4.2  rmind struct l2t_data *
    537  1.1.4.2  rmind t3_init_l2t(unsigned int l2t_capacity)
    538  1.1.4.2  rmind {
    539  1.1.4.2  rmind     struct l2t_data *d;
    540  1.1.4.2  rmind     int i, size = sizeof(*d) + l2t_capacity * sizeof(struct l2t_entry);
    541  1.1.4.2  rmind 
    542  1.1.4.2  rmind     d = cxgb_alloc_mem(size);
    543  1.1.4.2  rmind     if (!d)
    544  1.1.4.2  rmind         return NULL;
    545  1.1.4.2  rmind 
    546  1.1.4.2  rmind     d->nentries = l2t_capacity;
    547  1.1.4.2  rmind     d->rover = &d->l2tab[1];    /* entry 0 is not used */
    548  1.1.4.2  rmind     atomic_store_rel_int(&d->nfree, l2t_capacity - 1);
    549  1.1.4.2  rmind     rw_init(&d->lock, "L2T");
    550  1.1.4.2  rmind 
    551  1.1.4.2  rmind     for (i = 0; i < l2t_capacity; ++i) {
    552  1.1.4.2  rmind         d->l2tab[i].idx = i;
    553  1.1.4.2  rmind         d->l2tab[i].state = L2T_STATE_UNUSED;
    554  1.1.4.2  rmind         mtx_init(&d->l2tab[i].lock, "L2TAB", NULL, MTX_DEF);
    555  1.1.4.2  rmind         atomic_store_rel_int(&d->l2tab[i].refcnt, 0);
    556  1.1.4.2  rmind #ifndef NETEVENT
    557  1.1.4.2  rmind #ifdef CONFIG_CHELSIO_T3_MODULE
    558  1.1.4.2  rmind         setup_timer(&d->l2tab[i].update_timer, update_timer_cb,
    559  1.1.4.2  rmind                 (unsigned long)&d->l2tab[i]);
    560  1.1.4.2  rmind #endif
    561  1.1.4.2  rmind #endif
    562  1.1.4.2  rmind     }
    563  1.1.4.2  rmind     return d;
    564  1.1.4.2  rmind }
    565  1.1.4.2  rmind 
    566  1.1.4.2  rmind void
    567  1.1.4.2  rmind t3_free_l2t(struct l2t_data *d)
    568  1.1.4.2  rmind {
    569  1.1.4.2  rmind #ifndef NETEVENT
    570  1.1.4.2  rmind #ifdef CONFIG_CHELSIO_T3_MODULE
    571  1.1.4.2  rmind     int i;
    572  1.1.4.2  rmind 
    573  1.1.4.2  rmind     /* Stop all L2T timers */
    574  1.1.4.2  rmind     for (i = 0; i < d->nentries; ++i)
    575  1.1.4.2  rmind         del_timer_sync(&d->l2tab[i].update_timer);
    576  1.1.4.2  rmind #endif
    577  1.1.4.2  rmind #endif
    578  1.1.4.2  rmind     cxgb_free_mem(d);
    579  1.1.4.2  rmind }
    580  1.1.4.2  rmind 
    581  1.1.4.2  rmind #ifdef CONFIG_PROC_FS
    582  1.1.4.2  rmind #include <linux/module.h>
    583  1.1.4.2  rmind #include <linux/proc_fs.h>
    584  1.1.4.2  rmind #include <linux/seq_file.h>
    585  1.1.4.2  rmind 
    586  1.1.4.2  rmind static inline void *
    587  1.1.4.2  rmind l2t_get_idx(struct seq_file *seq, loff_t pos)
    588  1.1.4.2  rmind {
    589  1.1.4.2  rmind     struct l2t_data *d = seq->private;
    590  1.1.4.2  rmind 
    591  1.1.4.2  rmind     return pos >= d->nentries ? NULL : &d->l2tab[pos];
    592  1.1.4.2  rmind }
    593  1.1.4.2  rmind 
    594  1.1.4.2  rmind static void *
    595  1.1.4.2  rmind l2t_seq_start(struct seq_file *seq, loff_t *pos)
    596  1.1.4.2  rmind {
    597  1.1.4.2  rmind     return *pos ? l2t_get_idx(seq, *pos) : SEQ_START_TOKEN;
    598  1.1.4.2  rmind }
    599  1.1.4.2  rmind 
    600  1.1.4.2  rmind static void *
    601  1.1.4.2  rmind l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
    602  1.1.4.2  rmind {
    603  1.1.4.2  rmind     v = l2t_get_idx(seq, *pos + 1);
    604  1.1.4.2  rmind     if (v)
    605  1.1.4.2  rmind         ++*pos;
    606  1.1.4.2  rmind     return v;
    607  1.1.4.2  rmind }
    608  1.1.4.2  rmind 
    609  1.1.4.2  rmind static void
    610  1.1.4.2  rmind l2t_seq_stop(struct seq_file *seq, void *v)
    611  1.1.4.2  rmind {
    612  1.1.4.2  rmind }
    613  1.1.4.2  rmind 
    614  1.1.4.2  rmind static char
    615  1.1.4.2  rmind l2e_state(const struct l2t_entry *e)
    616  1.1.4.2  rmind {
    617  1.1.4.2  rmind     switch (e->state) {
    618  1.1.4.2  rmind     case L2T_STATE_VALID: return 'V';  /* valid, fast-path entry */
    619  1.1.4.2  rmind     case L2T_STATE_STALE: return 'S';  /* needs revalidation, but usable */
    620  1.1.4.2  rmind     case L2T_STATE_RESOLVING:
    621  1.1.4.2  rmind         return e->arpq_head ? 'A' : 'R';
    622  1.1.4.2  rmind     default:
    623  1.1.4.2  rmind         return 'U';
    624  1.1.4.2  rmind     }
    625  1.1.4.2  rmind }
    626  1.1.4.2  rmind 
    627  1.1.4.2  rmind static int
    628  1.1.4.2  rmind l2t_seq_show(struct seq_file *seq, void *v)
    629  1.1.4.2  rmind {
    630  1.1.4.2  rmind     if (v == SEQ_START_TOKEN)
    631  1.1.4.2  rmind         seq_puts(seq, "Index IP address      Ethernet address   VLAN  "
    632  1.1.4.2  rmind              "Prio  State   Users SMTIDX  Port\n");
    633  1.1.4.2  rmind     else {
    634  1.1.4.2  rmind         char ip[20];
    635  1.1.4.2  rmind         struct l2t_entry *e = v;
    636  1.1.4.2  rmind 
    637  1.1.4.2  rmind         mtx_lock(&e->lock);
    638  1.1.4.2  rmind         sprintf(ip, "%u.%u.%u.%u", NIPQUAD(e->addr));
    639  1.1.4.2  rmind         seq_printf(seq, "%-5u %-15s %02x:%02x:%02x:%02x:%02x:%02x  %4d"
    640  1.1.4.2  rmind                "  %3u     %c   %7u   %4u %s\n",
    641  1.1.4.2  rmind                e->idx, ip, e->dmac[0], e->dmac[1], e->dmac[2],
    642  1.1.4.2  rmind                e->dmac[3], e->dmac[4], e->dmac[5],
    643  1.1.4.2  rmind                e->vlan & EVL_VLID_MASK, vlan_prio(e),
    644  1.1.4.2  rmind                l2e_state(e), atomic_load_acq_int(&e->refcnt), e->smt_idx,
    645  1.1.4.2  rmind                e->neigh ? e->neigh->dev->name : "");
    646  1.1.4.2  rmind         mtx_unlock(&e->lock);
    647  1.1.4.2  rmind     }
    648  1.1.4.2  rmind     return 0;
    649  1.1.4.2  rmind }
    650  1.1.4.2  rmind 
    651  1.1.4.2  rmind #endif
    652