xref: /illumos-gate/usr/src/uts/common/io/cxgbe/t4nex/t4_l2t.c (revision d21cedec4ed074c3a6feb4a8a007a9cb83cca060)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source. A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * This file is part of the Chelsio T4 support code.
14  *
15  * Copyright (C) 2010-2013 Chelsio Communications.  All rights reserved.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the LICENSE file included in this
20  * release for licensing terms and conditions.
21  */
22 
23 #include <sys/ddi.h>
24 #include <sys/sunddi.h>
25 #include <sys/sunndi.h>
26 #include <sys/atomic.h>
27 #include <sys/dlpi.h>
28 #include <sys/pattr.h>
29 #include <sys/strsubr.h>
30 #include <sys/stream.h>
31 #include <sys/strsun.h>
32 #include <sys/ethernet.h>
33 #include <inet/ip.h>
34 #include <inet/ipclassifier.h>
35 #include <inet/tcp.h>
36 
37 #include "common/common.h"
38 #include "common/t4_msg.h"
39 #include "common/t4_regs.h"
40 #include "common/t4_regs_values.h"
41 #include "t4_l2t.h"
42 
43 /* identifies sync vs async L2T_WRITE_REQs */
44 #define	S_SYNC_WR	12
45 #define	V_SYNC_WR(x)	((x) << S_SYNC_WR)
46 #define	F_SYNC_WR	V_SYNC_WR(1)
47 #define	VLAN_NONE	0xfff
48 
49 /*
50  * jhash.h: Jenkins hash support.
51  *
52  * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
53  *
54  * http://burtleburtle.net/bob/hash/
55  *
56  * These are the credits from Bob's sources:
57  *
58  * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
59  * hash(), hash2(), hash3, and mix() are externally useful functions.
60  * Routines to test the hash are included if SELF_TEST is defined.
61  * You can use this free for any purpose.  It has no warranty.
62  */
63 
64 /* NOTE: Arguments are modified. */
65 #define	__jhash_mix(a, b, c) \
66 { \
67 	a -= b; a -= c; a ^= (c>>13); \
68 	b -= c; b -= a; b ^= (a<<8); \
69 	c -= a; c -= b; c ^= (b>>13); \
70 	a -= b; a -= c; a ^= (c>>12);  \
71 	b -= c; b -= a; b ^= (a<<16); \
72 	c -= a; c -= b; c ^= (b>>5); \
73 	a -= b; a -= c; a ^= (c>>3);  \
74 	b -= c; b -= a; b ^= (a<<10); \
75 	c -= a; c -= b; c ^= (b>>15); \
76 }
77 
78 /* The golden ration: an arbitrary value */
79 #define	JHASH_GOLDEN_RATIO	0x9e3779b9
80 
81 /*
82  * A special ultra-optimized versions that knows they are hashing exactly
83  * 3, 2 or 1 word(s).
84  *
85  * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
86  *	 done at the end is not done here.
87  */
88 static inline u32
89 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
90 {
91 	a += JHASH_GOLDEN_RATIO;
92 	b += JHASH_GOLDEN_RATIO;
93 	c += initval;
94 
95 	__jhash_mix(a, b, c);
96 
97 	return (c);
98 }
99 
100 static inline u32
101 jhash_2words(u32 a, u32 b, u32 initval)
102 {
103 	return (jhash_3words(a, b, 0, initval));
104 }
105 
106 #ifndef container_of
107 #define	container_of(p, s, f) ((s *)(((uint8_t *)(p)) - offsetof(s, f)))
108 #endif
109 
110 #if defined(__GNUC__)
111 #define	likely(x)	__builtin_expect((x), 1)
112 #define	unlikely(x)	__builtin_expect((x), 0)
113 #else
114 #define	likely(x)	(x)
115 #define	unlikely(x)	(x)
116 #endif /* defined(__GNUC__) */
117 
118 enum {
119 	L2T_STATE_VALID,	/* entry is up to date */
120 	L2T_STATE_STALE,	/* entry may be used but needs revalidation */
121 	L2T_STATE_RESOLVING,	/* entry needs address resolution */
122 	L2T_STATE_SYNC_WRITE,	/* synchronous write of entry underway */
123 
124 	/* when state is one of the below the entry is not hashed */
125 	L2T_STATE_SWITCHING,	/* entry is being used by a switching filter */
126 	L2T_STATE_UNUSED	/* entry not in use */
127 };
128 
129 struct l2t_data {
130 	krwlock_t lock;
131 	u_int l2t_size;
132 	volatile uint_t nfree;	 /* number of free entries */
133 	struct l2t_entry *rover; /* starting point for next allocation */
134 	struct l2t_entry l2tab[];
135 };
136 
137 #define	VLAN_NONE	0xfff
138 #define	SA(x)		((struct sockaddr *)(x))
139 #define	SIN(x)		((struct sockaddr_in *)(x))
140 #define	SINADDR(x)	(SIN(x)->sin_addr.s_addr)
141 #define	atomic_read(x) atomic_add_int_nv(x, 0)
142 /*
143  * Allocate a free L2T entry.
144  * Must be called with l2t_data.lockatomic_load_acq_int held.
145  */
146 static struct l2t_entry *
147 alloc_l2e(struct l2t_data *d)
148 {
149 	struct l2t_entry *end, *e, **p;
150 
151 	ASSERT(rw_write_held(&d->lock));
152 
153 	if (!atomic_read(&d->nfree))
154 		return (NULL);
155 
156 	/* there's definitely a free entry */
157 	for (e = d->rover, end = &d->l2tab[d->l2t_size]; e != end; ++e)
158 		if (atomic_read(&e->refcnt) == 0)
159 			goto found;
160 
161 	for (e = d->l2tab; atomic_read(&e->refcnt); ++e)
162 		/* */;
163 found:
164 	d->rover = e + 1;
165 	atomic_dec_uint(&d->nfree);
166 
167 	/*
168 	 * The entry we found may be an inactive entry that is
169 	 * presently in the hash table.  We need to remove it.
170 	 */
171 	if (e->state < L2T_STATE_SWITCHING) {
172 		for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next) {
173 			if (*p == e) {
174 				*p = e->next;
175 				e->next = NULL;
176 				break;
177 			}
178 		}
179 	}
180 
181 	e->state = L2T_STATE_UNUSED;
182 	return (e);
183 }
184 
185 /*
186  * Write an L2T entry.  Must be called with the entry locked.
187  * The write may be synchronous or asynchronous.
188  */
189 static int
190 write_l2e(adapter_t *sc, struct l2t_entry *e, int sync)
191 {
192 	mblk_t *m;
193 	struct cpl_l2t_write_req *req;
194 	int idx = e->idx + sc->vres.l2t.start;
195 
196 	ASSERT(MUTEX_HELD(&e->lock));
197 
198 	if ((m = allocb(sizeof (*req), BPRI_HI)) == NULL)
199 		return (ENOMEM);
200 
201 	/* LINTED: E_BAD_PTR_CAST_ALIGN */
202 	req = (struct cpl_l2t_write_req *)m->b_wptr;
203 
204 	/* LINTED: E_CONSTANT_CONDITION */
205 	INIT_TP_WR(req, 0);
206 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, idx |
207 	    V_SYNC_WR(sync) | V_TID_QID(sc->sge.fwq.abs_id)));
208 	req->params = htons(V_L2T_W_PORT(e->lport) | V_L2T_W_NOREPLY(!sync));
209 	req->l2t_idx = htons(idx);
210 	req->vlan = htons(e->vlan);
211 	(void) memcpy(req->dst_mac, e->dmac, sizeof (req->dst_mac));
212 
213 	m->b_wptr += sizeof (*req);
214 
215 	(void) t4_mgmt_tx(sc, m);
216 
217 	if (sync && e->state != L2T_STATE_SWITCHING)
218 		e->state = L2T_STATE_SYNC_WRITE;
219 
220 	return (0);
221 }
222 
223 struct l2t_data *
224 t4_init_l2t(struct adapter *sc)
225 {
226 	int i, l2t_size;
227 	struct l2t_data *d;
228 
229 	l2t_size = sc->vres.l2t.size;
230 	if(l2t_size < 1)
231 		return (NULL);
232 
233 	d = kmem_zalloc(sizeof(*d) + l2t_size * sizeof (struct l2t_entry), KM_SLEEP);
234 	if (!d)
235 		return (NULL);
236 
237 	d->l2t_size = l2t_size;
238 
239 	d->rover = d->l2tab;
240 	(void) atomic_swap_uint(&d->nfree, l2t_size);
241 	rw_init(&d->lock, NULL, RW_DRIVER, NULL);
242 
243 	for (i = 0; i < l2t_size; i++) {
244 		/* LINTED: E_ASSIGN_NARROW_CONV */
245 		d->l2tab[i].idx = i;
246 		d->l2tab[i].state = L2T_STATE_UNUSED;
247 		mutex_init(&d->l2tab[i].lock, NULL, MUTEX_DRIVER, NULL);
248 		(void) atomic_swap_uint(&d->l2tab[i].refcnt, 0);
249 	}
250 
251 	(void) t4_register_cpl_handler(sc, CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
252 
253 	return (d);
254 }
255 
256 int
257 t4_free_l2t(struct l2t_data *d)
258 {
259 	int i;
260 
261 	for (i = 0; i < L2T_SIZE; i++)
262 		mutex_destroy(&d->l2tab[i].lock);
263 	rw_destroy(&d->lock);
264 	kmem_free(d, sizeof (*d));
265 
266 	return (0);
267 }
268 
269 #ifndef TCP_OFFLOAD_DISABLE
270 static inline void
271 l2t_hold(struct l2t_data *d, struct l2t_entry *e)
272 {
273 	if (atomic_inc_uint_nv(&e->refcnt) == 1)  /* 0 -> 1 transition */
274 		atomic_dec_uint(&d->nfree);
275 }
276 
277 /*
278  * To avoid having to check address families we do not allow v4 and v6
279  * neighbors to be on the same hash chain.  We keep v4 entries in the first
280  * half of available hash buckets and v6 in the second.
281  */
282 enum {
283 	L2T_SZ_HALF = L2T_SIZE / 2,
284 	L2T_HASH_MASK = L2T_SZ_HALF - 1
285 };
286 
287 static inline unsigned int
288 arp_hash(const uint32_t *key, int ifindex)
289 {
290 	return (jhash_2words(*key, ifindex, 0) & L2T_HASH_MASK);
291 }
292 
293 static inline unsigned int
294 ipv6_hash(const uint32_t *key, int ifindex)
295 {
296 	uint32_t xor = key[0] ^ key[1] ^ key[2] ^ key[3];
297 
298 	return (L2T_SZ_HALF + (jhash_2words(xor, ifindex, 0) & L2T_HASH_MASK));
299 }
300 
301 static inline unsigned int
302 addr_hash(const uint32_t *addr, int addr_len, int ifindex)
303 {
304 	return (addr_len == 4 ? arp_hash(addr, ifindex) :
305 	    ipv6_hash(addr, ifindex));
306 }
307 
308 /*
309  * Checks if an L2T entry is for the given IP/IPv6 address.  It does not check
310  * whether the L2T entry and the address are of the same address family.
311  * Callers ensure an address is only checked against L2T entries of the same
312  * family, something made trivial by the separation of IP and IPv6 hash chains
313  * mentioned above.  Returns 0 if there's a match,
314  */
315 static inline int
316 addreq(const struct l2t_entry *e, const uint32_t *addr)
317 {
318 	if (e->v6 != 0)
319 		return ((e->addr[0] ^ addr[0]) | (e->addr[1] ^ addr[1]) |
320 		    (e->addr[2] ^ addr[2]) | (e->addr[3] ^ addr[3]));
321 	return (e->addr[0] ^ addr[0]);
322 }
323 
324 /*
325  * Add a packet to an L2T entry's queue of packets awaiting resolution.
326  * Must be called with the entry's lock held.
327  */
328 static inline void
329 arpq_enqueue(struct l2t_entry *e, mblk_t *m)
330 {
331 	ASSERT(MUTEX_HELD(&e->lock));
332 
333 	ASSERT(m->b_next == NULL);
334 	if (e->arpq_head != NULL)
335 		e->arpq_tail->b_next = m;
336 	else
337 		e->arpq_head = m;
338 	e->arpq_tail = m;
339 }
340 
341 int
342 t4_l2t_send(struct adapter *sc, mblk_t *m, struct l2t_entry *e)
343 {
344 	sin_t *sin;
345 	ip2mac_t ip2m;
346 
347 	if (e->v6 != 0)
348 		ASSERT(0);
349 again:
350 	switch (e->state) {
351 	case L2T_STATE_STALE:	/* entry is stale, kick off revalidation */
352 
353 	/* Fall through */
354 	case L2T_STATE_VALID:	/* fast-path, send the packet on */
355 		(void) t4_wrq_tx(sc, MBUF_EQ(m), m);
356 		return (0);
357 
358 	case L2T_STATE_RESOLVING:
359 	case L2T_STATE_SYNC_WRITE:
360 		mutex_enter(&e->lock);
361 		if (e->state != L2T_STATE_SYNC_WRITE &&
362 		    e->state != L2T_STATE_RESOLVING) {
363 			/* state changed by the time we got here */
364 			mutex_exit(&e->lock);
365 			goto again;
366 		}
367 		arpq_enqueue(e, m);
368 		mutex_exit(&e->lock);
369 
370 		bzero(&ip2m, sizeof (ip2m));
371 		sin = (sin_t *)&ip2m.ip2mac_pa;
372 		sin->sin_family = AF_INET;
373 		sin->sin_addr.s_addr = e->in_addr;
374 		ip2m.ip2mac_ifindex = e->ifindex;
375 
376 		if (e->state == L2T_STATE_RESOLVING) {
377 			(void) ip2mac(IP2MAC_RESOLVE, &ip2m, t4_l2t_update, e,
378 			    0);
379 			if (ip2m.ip2mac_err == EINPROGRESS)
380 				ASSERT(0);
381 			else if (ip2m.ip2mac_err == 0)
382 				t4_l2t_update(&ip2m, e);
383 			else
384 				ASSERT(0);
385 		}
386 	}
387 
388 	return (0);
389 }
390 
391 /*
392  * Called when an L2T entry has no more users.  The entry is left in the hash
393  * table since it is likely to be reused but we also bump nfree to indicate
394  * that the entry can be reallocated for a different neighbor.  We also drop
395  * the existing neighbor reference in case the neighbor is going away and is
396  * waiting on our reference.
397  *
398  * Because entries can be reallocated to other neighbors once their ref count
399  * drops to 0 we need to take the entry's lock to avoid races with a new
400  * incarnation.
401  */
402 static void
403 t4_l2e_free(struct l2t_entry *e)
404 {
405 	struct l2t_data *d;
406 
407 	mutex_enter(&e->lock);
408 	/* LINTED: E_NOP_IF_STMT */
409 	if (atomic_read(&e->refcnt) == 0) {  /* hasn't been recycled */
410 		/*
411 		 * Don't need to worry about the arpq, an L2T entry can't be
412 		 * released if any packets are waiting for resolution as we
413 		 * need to be able to communicate with the device to close a
414 		 * connection.
415 		 */
416 	}
417 	mutex_exit(&e->lock);
418 
419 	d = container_of(e, struct l2t_data, l2tab[e->idx]);
420 	atomic_inc_uint(&d->nfree);
421 
422 }
423 
424 void
425 t4_l2t_release(struct l2t_entry *e)
426 {
427 	if (atomic_dec_uint_nv(&e->refcnt) == 0)
428 		t4_l2e_free(e);
429 }
430 
431 /* ARGSUSED */
432 int
433 do_l2t_write_rpl(struct sge_iq *iq, const struct rss_header *rss, mblk_t *m)
434 {
435 	struct adapter *sc = iq->adapter;
436 	const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
437 	unsigned int tid = GET_TID(rpl);
438 	unsigned int idx = tid % L2T_SIZE;
439 
440 	if (likely(rpl->status != CPL_ERR_NONE)) {
441 		cxgb_printf(sc->dip, CE_WARN,
442 		    "Unexpected L2T_WRITE_RPL status %u for entry %u",
443 		    rpl->status, idx);
444 		return (-EINVAL);
445 	}
446 
447 	return (0);
448 }
449 
450 /*
451  * The TOE wants an L2 table entry that it can use to reach the next hop over
452  * the specified port.  Produce such an entry - create one if needed.
453  *
454  * Note that the ifnet could be a pseudo-device like if_vlan, if_lagg, etc. on
455  * top of the real cxgbe interface.
456  */
457 struct l2t_entry *
458 t4_l2t_get(struct port_info *pi, conn_t *connp)
459 {
460 	struct l2t_entry *e;
461 	struct l2t_data *d = pi->adapter->l2t;
462 	int addr_len;
463 	uint32_t *addr;
464 	int hash;
465 	int index = \
466 	    connp->conn_ixa->ixa_ire->ire_ill->ill_phyint->phyint_ifindex;
467 	unsigned int smt_idx = pi->port_id;
468 	addr = (uint32_t *)&connp->conn_faddr_v4;
469 	addr_len  = sizeof (connp->conn_faddr_v4);
470 
471 	hash = addr_hash(addr, addr_len, index);
472 
473 	rw_enter(&d->lock, RW_WRITER);
474 	for (e = d->l2tab[hash].first; e; e = e->next) {
475 		if (!addreq(e, addr) && e->smt_idx == smt_idx) {
476 			l2t_hold(d, e);
477 			goto done;
478 		}
479 	}
480 
481 	/* Need to allocate a new entry */
482 	e = alloc_l2e(d);
483 	if (e != NULL) {
484 		mutex_enter(&e->lock);	/* avoid race with t4_l2t_free */
485 		e->state = L2T_STATE_RESOLVING;
486 		(void) memcpy(e->addr, addr, addr_len);
487 		e->in_addr = connp->conn_faddr_v4;
488 		e->ifindex = index;
489 		/* LINTED: E_ASSIGN_NARROW_CONV */
490 		e->smt_idx = smt_idx;
491 		/* LINTED: E_ASSIGN_NARROW_CONV */
492 		e->hash = hash;
493 		e->lport = pi->lport;
494 		e->arpq_head = e->arpq_tail = NULL;
495 		e->v6 = (addr_len == 16);
496 		e->sc = pi->adapter;
497 		(void) atomic_swap_uint(&e->refcnt, 1);
498 		e->vlan = VLAN_NONE;
499 		e->next = d->l2tab[hash].first;
500 		d->l2tab[hash].first = e;
501 		mutex_exit(&e->lock);
502 	} else {
503 		ASSERT(0);
504 	}
505 
506 done:
507 	rw_exit(&d->lock);
508 	return (e);
509 }
510 
511 /*
512  * Called when the host's neighbor layer makes a change to some entry that is
513  * loaded into the HW L2 table.
514  */
515 void
516 t4_l2t_update(ip2mac_t *ip2macp, void *arg)
517 {
518 	struct l2t_entry *e = (struct l2t_entry *)arg;
519 	struct adapter *sc = e->sc;
520 	uchar_t *cp;
521 
522 	if (ip2macp->ip2mac_err != 0) {
523 		ASSERT(0); /* Don't know what to do. Needs to be investigated */
524 	}
525 
526 	mutex_enter(&e->lock);
527 	if (atomic_read(&e->refcnt) != 0)
528 		goto found;
529 	e->state = L2T_STATE_STALE;
530 	mutex_exit(&e->lock);
531 
532 	/* The TOE has no interest in this LLE */
533 	return;
534 
535 found:
536 	if (atomic_read(&e->refcnt) != 0) {
537 
538 		/* Entry is referenced by at least 1 offloaded connection. */
539 
540 		cp = (uchar_t *)LLADDR(&ip2macp->ip2mac_ha);
541 		bcopy(cp, e->dmac, 6);
542 		(void) write_l2e(sc, e, 1);
543 		e->state = L2T_STATE_VALID;
544 
545 	}
546 	mutex_exit(&e->lock);
547 }
548 #endif
549