2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
4 * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
38 #include <linux/if_vlan.h>
39 #include <linux/jhash.h>
40 #include <linux/module.h>
41 #include <linux/debugfs.h>
42 #include <linux/seq_file.h>
43 #include <net/neighbour.h>
49 #include "t4_values.h"
51 #define VLAN_NONE 0xfff
53 /* identifies sync vs async L2T_WRITE_REQs */
55 #define SYNC_WR_V(x) ((x) << SYNC_WR_S)
56 #define SYNC_WR_F SYNC_WR_V(1)
59 unsigned int l2t_start; /* start index of our piece of the L2T */
60 unsigned int l2t_size; /* number of entries in l2tab */
62 atomic_t nfree; /* number of free entries */
63 struct l2t_entry *rover; /* starting point for next allocation */
64 struct l2t_entry l2tab[0]; /* MUST BE LAST */
67 static inline unsigned int vlan_prio(const struct l2t_entry *e)
72 static inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
74 if (atomic_add_return(1, &e->refcnt) == 1) /* 0 -> 1 transition */
75 atomic_dec(&d->nfree);
79 * To avoid having to check address families we do not allow v4 and v6
80 * neighbors to be on the same hash chain. We keep v4 entries in the first
81 * half of available hash buckets and v6 in the second. We need at least two
82 * entries in our L2T for this scheme to work.
85 L2T_MIN_HASH_BUCKETS = 2,
88 static inline unsigned int arp_hash(struct l2t_data *d, const u32 *key,
91 unsigned int l2t_size_half = d->l2t_size / 2;
93 return jhash_2words(*key, ifindex, 0) % l2t_size_half;
96 static inline unsigned int ipv6_hash(struct l2t_data *d, const u32 *key,
99 unsigned int l2t_size_half = d->l2t_size / 2;
100 u32 xor = key[0] ^ key[1] ^ key[2] ^ key[3];
102 return (l2t_size_half +
103 (jhash_2words(xor, ifindex, 0) % l2t_size_half));
106 static unsigned int addr_hash(struct l2t_data *d, const u32 *addr,
107 int addr_len, int ifindex)
109 return addr_len == 4 ? arp_hash(d, addr, ifindex) :
110 ipv6_hash(d, addr, ifindex);
114 * Checks if an L2T entry is for the given IP/IPv6 address. It does not check
115 * whether the L2T entry and the address are of the same address family.
116 * Callers ensure an address is only checked against L2T entries of the same
117 * family, something made trivial by the separation of IP and IPv6 hash chains
118 * mentioned above. Returns 0 if there's a match,
120 static int addreq(const struct l2t_entry *e, const u32 *addr)
123 return (e->addr[0] ^ addr[0]) | (e->addr[1] ^ addr[1]) |
124 (e->addr[2] ^ addr[2]) | (e->addr[3] ^ addr[3]);
125 return e->addr[0] ^ addr[0];
128 static void neigh_replace(struct l2t_entry *e, struct neighbour *n)
132 neigh_release(e->neigh);
137 * Write an L2T entry. Must be called with the entry locked.
138 * The write may be synchronous or asynchronous.
140 static int write_l2e(struct adapter *adap, struct l2t_entry *e, int sync)
142 struct l2t_data *d = adap->l2t;
143 unsigned int l2t_idx = e->idx + d->l2t_start;
145 struct cpl_l2t_write_req *req;
147 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
151 req = (struct cpl_l2t_write_req *)__skb_put(skb, sizeof(*req));
154 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ,
155 l2t_idx | (sync ? SYNC_WR_F : 0) |
156 TID_QID_V(adap->sge.fw_evtq.abs_id)));
157 req->params = htons(L2T_W_PORT_V(e->lport) | L2T_W_NOREPLY_V(!sync));
158 req->l2t_idx = htons(l2t_idx);
159 req->vlan = htons(e->vlan);
160 if (e->neigh && !(e->neigh->dev->flags & IFF_LOOPBACK))
161 memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
162 memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
164 set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
165 t4_ofld_send(adap, skb);
167 if (sync && e->state != L2T_STATE_SWITCHING)
168 e->state = L2T_STATE_SYNC_WRITE;
173 * Send packets waiting in an L2T entry's ARP queue. Must be called with the
176 static void send_pending(struct adapter *adap, struct l2t_entry *e)
178 while (e->arpq_head) {
179 struct sk_buff *skb = e->arpq_head;
181 e->arpq_head = skb->next;
183 t4_ofld_send(adap, skb);
189 * Process a CPL_L2T_WRITE_RPL. Wake up the ARP queue if it completes a
190 * synchronous L2T_WRITE. Note that the TID in the reply is really the L2T
191 * index it refers to.
193 void do_l2t_write_rpl(struct adapter *adap, const struct cpl_l2t_write_rpl *rpl)
195 struct l2t_data *d = adap->l2t;
196 unsigned int tid = GET_TID(rpl);
197 unsigned int l2t_idx = tid % L2T_SIZE;
199 if (unlikely(rpl->status != CPL_ERR_NONE)) {
200 dev_err(adap->pdev_dev,
201 "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
202 rpl->status, l2t_idx);
206 if (tid & SYNC_WR_F) {
207 struct l2t_entry *e = &d->l2tab[l2t_idx - d->l2t_start];
210 if (e->state != L2T_STATE_SWITCHING) {
211 send_pending(adap, e);
212 e->state = (e->neigh->nud_state & NUD_STALE) ?
213 L2T_STATE_STALE : L2T_STATE_VALID;
215 spin_unlock(&e->lock);
220 * Add a packet to an L2T entry's queue of packets awaiting resolution.
221 * Must be called with the entry's lock held.
223 static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
227 e->arpq_tail->next = skb;
233 int cxgb4_l2t_send(struct net_device *dev, struct sk_buff *skb,
236 struct adapter *adap = netdev2adap(dev);
240 case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
241 neigh_event_send(e->neigh, NULL);
242 spin_lock_bh(&e->lock);
243 if (e->state == L2T_STATE_STALE)
244 e->state = L2T_STATE_VALID;
245 spin_unlock_bh(&e->lock);
246 case L2T_STATE_VALID: /* fast-path, send the packet on */
247 return t4_ofld_send(adap, skb);
248 case L2T_STATE_RESOLVING:
249 case L2T_STATE_SYNC_WRITE:
250 spin_lock_bh(&e->lock);
251 if (e->state != L2T_STATE_SYNC_WRITE &&
252 e->state != L2T_STATE_RESOLVING) {
253 spin_unlock_bh(&e->lock);
256 arpq_enqueue(e, skb);
257 spin_unlock_bh(&e->lock);
259 if (e->state == L2T_STATE_RESOLVING &&
260 !neigh_event_send(e->neigh, NULL)) {
261 spin_lock_bh(&e->lock);
262 if (e->state == L2T_STATE_RESOLVING && e->arpq_head)
263 write_l2e(adap, e, 1);
264 spin_unlock_bh(&e->lock);
269 EXPORT_SYMBOL(cxgb4_l2t_send);
272 * Allocate a free L2T entry. Must be called with l2t_data.lock held.
274 static struct l2t_entry *alloc_l2e(struct l2t_data *d)
276 struct l2t_entry *end, *e, **p;
278 if (!atomic_read(&d->nfree))
281 /* there's definitely a free entry */
282 for (e = d->rover, end = &d->l2tab[d->l2t_size]; e != end; ++e)
283 if (atomic_read(&e->refcnt) == 0)
286 for (e = d->l2tab; atomic_read(&e->refcnt); ++e)
290 atomic_dec(&d->nfree);
293 * The entry we found may be an inactive entry that is
294 * presently in the hash table. We need to remove it.
296 if (e->state < L2T_STATE_SWITCHING)
297 for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
304 e->state = L2T_STATE_UNUSED;
309 * Called when an L2T entry has no more users.
311 static void t4_l2e_free(struct l2t_entry *e)
315 spin_lock_bh(&e->lock);
316 if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
318 neigh_release(e->neigh);
321 while (e->arpq_head) {
322 struct sk_buff *skb = e->arpq_head;
324 e->arpq_head = skb->next;
329 spin_unlock_bh(&e->lock);
331 d = container_of(e, struct l2t_data, l2tab[e->idx]);
332 atomic_inc(&d->nfree);
335 void cxgb4_l2t_release(struct l2t_entry *e)
337 if (atomic_dec_and_test(&e->refcnt))
340 EXPORT_SYMBOL(cxgb4_l2t_release);
343 * Update an L2T entry that was previously used for the same next hop as neigh.
344 * Must be called with softirqs disabled.
346 static void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
348 unsigned int nud_state;
350 spin_lock(&e->lock); /* avoid race with t4_l2t_free */
351 if (neigh != e->neigh)
352 neigh_replace(e, neigh);
353 nud_state = neigh->nud_state;
354 if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
355 !(nud_state & NUD_VALID))
356 e->state = L2T_STATE_RESOLVING;
357 else if (nud_state & NUD_CONNECTED)
358 e->state = L2T_STATE_VALID;
360 e->state = L2T_STATE_STALE;
361 spin_unlock(&e->lock);
364 struct l2t_entry *cxgb4_l2t_get(struct l2t_data *d, struct neighbour *neigh,
365 const struct net_device *physdev,
366 unsigned int priority)
371 int addr_len = neigh->tbl->key_len;
372 u32 *addr = (u32 *)neigh->primary_key;
373 int ifidx = neigh->dev->ifindex;
374 int hash = addr_hash(d, addr, addr_len, ifidx);
376 if (neigh->dev->flags & IFF_LOOPBACK)
377 lport = netdev2pinfo(physdev)->tx_chan + 4;
379 lport = netdev2pinfo(physdev)->lport;
381 if (neigh->dev->priv_flags & IFF_802_1Q_VLAN)
382 vlan = vlan_dev_vlan_id(neigh->dev);
386 write_lock_bh(&d->lock);
387 for (e = d->l2tab[hash].first; e; e = e->next)
388 if (!addreq(e, addr) && e->ifindex == ifidx &&
389 e->vlan == vlan && e->lport == lport) {
391 if (atomic_read(&e->refcnt) == 1)
392 reuse_entry(e, neigh);
396 /* Need to allocate a new entry */
399 spin_lock(&e->lock); /* avoid race with t4_l2t_free */
400 e->state = L2T_STATE_RESOLVING;
401 if (neigh->dev->flags & IFF_LOOPBACK)
402 memcpy(e->dmac, physdev->dev_addr, sizeof(e->dmac));
403 memcpy(e->addr, addr, addr_len);
407 e->v6 = addr_len == 16;
408 atomic_set(&e->refcnt, 1);
409 neigh_replace(e, neigh);
411 e->next = d->l2tab[hash].first;
412 d->l2tab[hash].first = e;
413 spin_unlock(&e->lock);
416 write_unlock_bh(&d->lock);
419 EXPORT_SYMBOL(cxgb4_l2t_get);
421 u64 cxgb4_select_ntuple(struct net_device *dev,
422 const struct l2t_entry *l2t)
424 struct adapter *adap = netdev2adap(dev);
425 struct tp_params *tp = &adap->params.tp;
428 /* Initialize each of the fields which we care about which are present
429 * in the Compressed Filter Tuple.
431 if (tp->vlan_shift >= 0 && l2t->vlan != VLAN_NONE)
432 ntuple |= (u64)(FT_VLAN_VLD_F | l2t->vlan) << tp->vlan_shift;
434 if (tp->port_shift >= 0)
435 ntuple |= (u64)l2t->lport << tp->port_shift;
437 if (tp->protocol_shift >= 0)
438 ntuple |= (u64)IPPROTO_TCP << tp->protocol_shift;
440 if (tp->vnic_shift >= 0) {
441 u32 viid = cxgb4_port_viid(dev);
442 u32 vf = FW_VIID_VIN_G(viid);
443 u32 pf = FW_VIID_PFN_G(viid);
444 u32 vld = FW_VIID_VIVLD_G(viid);
446 ntuple |= (u64)(FT_VNID_ID_VF_V(vf) |
447 FT_VNID_ID_PF_V(pf) |
448 FT_VNID_ID_VLD_V(vld)) << tp->vnic_shift;
453 EXPORT_SYMBOL(cxgb4_select_ntuple);
456 * Called when address resolution fails for an L2T entry to handle packets
457 * on the arpq head. If a packet specifies a failure handler it is invoked,
458 * otherwise the packet is sent to the device.
460 static void handle_failed_resolution(struct adapter *adap, struct sk_buff *arpq)
463 struct sk_buff *skb = arpq;
464 const struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
468 if (cb->arp_err_handler)
469 cb->arp_err_handler(cb->handle, skb);
471 t4_ofld_send(adap, skb);
476 * Called when the host's neighbor layer makes a change to some entry that is
477 * loaded into the HW L2 table.
479 void t4_l2t_update(struct adapter *adap, struct neighbour *neigh)
482 struct sk_buff *arpq = NULL;
483 struct l2t_data *d = adap->l2t;
484 int addr_len = neigh->tbl->key_len;
485 u32 *addr = (u32 *) neigh->primary_key;
486 int ifidx = neigh->dev->ifindex;
487 int hash = addr_hash(d, addr, addr_len, ifidx);
489 read_lock_bh(&d->lock);
490 for (e = d->l2tab[hash].first; e; e = e->next)
491 if (!addreq(e, addr) && e->ifindex == ifidx) {
493 if (atomic_read(&e->refcnt))
495 spin_unlock(&e->lock);
498 read_unlock_bh(&d->lock);
502 read_unlock(&d->lock);
504 if (neigh != e->neigh)
505 neigh_replace(e, neigh);
507 if (e->state == L2T_STATE_RESOLVING) {
508 if (neigh->nud_state & NUD_FAILED) {
510 e->arpq_head = e->arpq_tail = NULL;
511 } else if ((neigh->nud_state & (NUD_CONNECTED | NUD_STALE)) &&
513 write_l2e(adap, e, 1);
516 e->state = neigh->nud_state & NUD_CONNECTED ?
517 L2T_STATE_VALID : L2T_STATE_STALE;
518 if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)))
519 write_l2e(adap, e, 0);
522 spin_unlock_bh(&e->lock);
525 handle_failed_resolution(adap, arpq);
528 /* Allocate an L2T entry for use by a switching rule. Such need to be
529 * explicitly freed and while busy they are not on any hash chain, so normal
530 * address resolution updates do not see them.
532 struct l2t_entry *t4_l2t_alloc_switching(struct l2t_data *d)
536 write_lock_bh(&d->lock);
539 spin_lock(&e->lock); /* avoid race with t4_l2t_free */
540 e->state = L2T_STATE_SWITCHING;
541 atomic_set(&e->refcnt, 1);
542 spin_unlock(&e->lock);
544 write_unlock_bh(&d->lock);
548 /* Sets/updates the contents of a switching L2T entry that has been allocated
549 * with an earlier call to @t4_l2t_alloc_switching.
551 int t4_l2t_set_switching(struct adapter *adap, struct l2t_entry *e, u16 vlan,
552 u8 port, u8 *eth_addr)
556 memcpy(e->dmac, eth_addr, ETH_ALEN);
557 return write_l2e(adap, e, 0);
560 struct l2t_data *t4_init_l2t(unsigned int l2t_start, unsigned int l2t_end)
562 unsigned int l2t_size;
566 if (l2t_start >= l2t_end || l2t_end >= L2T_SIZE)
568 l2t_size = l2t_end - l2t_start + 1;
569 if (l2t_size < L2T_MIN_HASH_BUCKETS)
572 d = t4_alloc_mem(sizeof(*d) + l2t_size * sizeof(struct l2t_entry));
576 d->l2t_start = l2t_start;
577 d->l2t_size = l2t_size;
580 atomic_set(&d->nfree, l2t_size);
581 rwlock_init(&d->lock);
583 for (i = 0; i < d->l2t_size; ++i) {
585 d->l2tab[i].state = L2T_STATE_UNUSED;
586 spin_lock_init(&d->l2tab[i].lock);
587 atomic_set(&d->l2tab[i].refcnt, 0);
592 static inline void *l2t_get_idx(struct seq_file *seq, loff_t pos)
594 struct l2t_data *d = seq->private;
596 return pos >= d->l2t_size ? NULL : &d->l2tab[pos];
599 static void *l2t_seq_start(struct seq_file *seq, loff_t *pos)
601 return *pos ? l2t_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
604 static void *l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
606 v = l2t_get_idx(seq, *pos);
612 static void l2t_seq_stop(struct seq_file *seq, void *v)
616 static char l2e_state(const struct l2t_entry *e)
619 case L2T_STATE_VALID: return 'V';
620 case L2T_STATE_STALE: return 'S';
621 case L2T_STATE_SYNC_WRITE: return 'W';
622 case L2T_STATE_RESOLVING: return e->arpq_head ? 'A' : 'R';
623 case L2T_STATE_SWITCHING: return 'X';
629 static int l2t_seq_show(struct seq_file *seq, void *v)
631 if (v == SEQ_START_TOKEN)
632 seq_puts(seq, " Idx IP address "
633 "Ethernet address VLAN/P LP State Users Port\n");
636 struct l2t_data *d = seq->private;
637 struct l2t_entry *e = v;
639 spin_lock_bh(&e->lock);
640 if (e->state == L2T_STATE_SWITCHING)
643 sprintf(ip, e->v6 ? "%pI6c" : "%pI4", e->addr);
644 seq_printf(seq, "%4u %-25s %17pM %4d %u %2u %c %5u %s\n",
645 e->idx + d->l2t_start, ip, e->dmac,
646 e->vlan & VLAN_VID_MASK, vlan_prio(e), e->lport,
647 l2e_state(e), atomic_read(&e->refcnt),
648 e->neigh ? e->neigh->dev->name : "");
649 spin_unlock_bh(&e->lock);
654 static const struct seq_operations l2t_seq_ops = {
655 .start = l2t_seq_start,
656 .next = l2t_seq_next,
657 .stop = l2t_seq_stop,
661 static int l2t_seq_open(struct inode *inode, struct file *file)
663 int rc = seq_open(file, &l2t_seq_ops);
666 struct adapter *adap = inode->i_private;
667 struct seq_file *seq = file->private_data;
669 seq->private = adap->l2t;
674 const struct file_operations t4_l2t_fops = {
675 .owner = THIS_MODULE,
676 .open = l2t_seq_open,
679 .release = seq_release,