aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/cxgbe/t4_clip.c
blob: e462a064847f24bc51ee5272adaf0a68096019d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2012-2021 Chelsio Communications, Inc.
 * All rights reserved.
 * Written by: Navdeep Parhar <np@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#include "opt_inet.h"
#include "opt_inet6.h"

#include <sys/types.h>
#include <sys/ck.h>
#include <sys/eventhandler.h>
#include <sys/malloc.h>
#include <sys/rmlock.h>
#include <sys/sbuf.h>
#include <sys/socket.h>
#include <sys/taskqueue.h>
#include <net/if.h>
#include <net/if_var.h>
#include <netinet/in.h>
#include <netinet6/in6_var.h>
#include <netinet6/scope6_var.h>

#include "common/common.h"
#include "t4_clip.h"

/*
 * Code to deal with the Compressed Local IPv6 (CLIP) table in the ASIC.
 *
 * The driver maintains a global CLIP database (clip_db) of IPv6 addresses and a
 * per-adapter CLIP table (sc->clip_table) with entries that point to an IPv6 in
 * the clip_db.  All access is protected by a single global lock (clip_db_lock).
 * The correct lock order is clip lock before synchronized op.
 *
 * By default (hw.cxgbe.clip_db_auto=1) all local IPv6 addresses are added to
 * the db.  Addresses are also added on-demand when the driver allocates an
 * entry for a filter, TOE tid, etc.  krn_ref counts the number of times an
 * address appears in the system.  adp_ref counts the number of adapters that
 * have that address in their CLIP table.  If both are 0 then the entry is
 * evicted from the db.  Consumers of the CLIP table entry (filters, TOE tids)
 * are tracked in ce->refcount.  Driver ioctls let external consumers add/remove
 * addresses from the CLIP table.
 */

#if defined(INET6)
struct clip_db_entry {
	LIST_ENTRY(clip_db_entry) link;	/* clip_db hash linkage */
	struct in6_addr lip;
	u_int krn_ref;	/* # of times this IP6 appears in list of all IP6 */
	u_int adp_ref;	/* # of adapters with this IP6 in their CLIP */
	u_int tmp_ref;	/* Used only during refresh */
};

struct clip_entry {
	LIST_ENTRY(clip_entry) link;	/* clip_table hash linkage */
	TAILQ_ENTRY(clip_entry) plink;	/* clip_pending linkage */
	struct clip_db_entry *cde;
	int16_t clip_idx;		/* index in the hw table */
	bool pending;			/* in clip_pending list */
	int refcount;
};

static eventhandler_tag ifaddr_evhandler;
static struct mtx clip_db_lock;
static LIST_HEAD(, clip_db_entry) *clip_db;
static u_long clip_db_mask;
static int clip_db_gen;
static struct task clip_db_task;

static int add_lip(struct adapter *, struct in6_addr *, int16_t *);
static int del_lip(struct adapter *, struct in6_addr *);
static void t4_clip_db_task(void *, int);
static void t4_clip_task(void *, int);
static void update_clip_db(void);
static int update_sw_clip_table(struct adapter *);
static int update_hw_clip_table(struct adapter *);
static void update_clip_table(struct adapter *, void *);
static int sysctl_clip_db(SYSCTL_HANDLER_ARGS);
static int sysctl_clip_db_auto(SYSCTL_HANDLER_ARGS);
static struct clip_db_entry *lookup_clip_db_entry(struct in6_addr *, bool);
static struct clip_entry *lookup_clip_entry(struct adapter *, struct in6_addr *,
    bool);

SYSCTL_PROC(_hw_cxgbe, OID_AUTO, clip_db, CTLTYPE_STRING | CTLFLAG_RD |
    CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, 0, sysctl_clip_db, "A",
    "CLIP database");

int t4_clip_db_auto = 1;
SYSCTL_PROC(_hw_cxgbe, OID_AUTO, clip_db_auto, CTLTYPE_INT | CTLFLAG_RWTUN |
    CTLFLAG_MPSAFE, NULL, 0, sysctl_clip_db_auto, "I",
    "Add local IPs to CLIP db automatically (0 = no, 1 = yes)");

static inline uint32_t
clip_hashfn(struct in6_addr *addr)
{
	return (fnv_32_buf(addr, sizeof(*addr), FNV1_32_INIT) & clip_db_mask);
}

static inline struct clip_db_entry *
alloc_clip_db_entry(struct in6_addr *in6)
{
	struct clip_db_entry *cde;

	cde = malloc(sizeof(*cde), M_CXGBE, M_NOWAIT | M_ZERO);
	if (__predict_true(cde != NULL))
		memcpy(&cde->lip, in6, sizeof(cde->lip));

	return (cde);
}

static inline struct clip_entry *
alloc_clip_entry(struct clip_db_entry *cde)
{
	struct clip_entry *ce;

	mtx_assert(&clip_db_lock, MA_OWNED);

	ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT | M_ZERO);
	if (__predict_true(ce != NULL)) {
		ce->cde = cde;
		cde->adp_ref++;
		ce->clip_idx = -1;
	}

	return (ce);
}

/*
 * Look up the IP6 address in the CLIP db.  If add is set then an entry for the
 * IP6 will be added to the db.
 */
static struct clip_db_entry *
lookup_clip_db_entry(struct in6_addr *in6, bool add)
{
	struct clip_db_entry *cde;
	const int bucket = clip_hashfn(in6);

	mtx_assert(&clip_db_lock, MA_OWNED);

	LIST_FOREACH(cde, &clip_db[bucket], link) {
		if (IN6_ARE_ADDR_EQUAL(&cde->lip, in6))
			return (cde);
	}

	/* Not found.  Create a new entry if requested. */
	if (add) {
		cde = alloc_clip_db_entry(in6);
		if (cde != NULL)
			LIST_INSERT_HEAD(&clip_db[bucket], cde, link);
	}

	return (cde);
}

/*
 * Look up the IP6 address in the CLIP db.  If add is set then an entry for the
 * IP6 will be added to the db.
 */
static struct clip_entry *
lookup_clip_entry(struct adapter *sc, struct in6_addr *in6, bool add)
{
	struct clip_db_entry *cde;
	struct clip_entry *ce;
	const int bucket = clip_hashfn(in6);

	mtx_assert(&clip_db_lock, MA_OWNED);

	cde = lookup_clip_db_entry(in6, add);
	if (cde == NULL)
		return (NULL);

	LIST_FOREACH(ce, &sc->clip_table[bucket], link) {
		if (ce->cde == cde)
			return (ce);
	}

	/* Not found.  Create a new entry if requested. */
	if (add) {
		ce = alloc_clip_entry(cde);
		if (ce != NULL) {
			LIST_INSERT_HEAD(&sc->clip_table[bucket], ce, link);
			TAILQ_INSERT_TAIL(&sc->clip_pending, ce, plink);
			ce->pending = true;
		}
	}

	return (ce);
}

static int
add_lip(struct adapter *sc, struct in6_addr *lip, int16_t *idx)
{
	struct fw_clip_cmd c;
	int rc;

	ASSERT_SYNCHRONIZED_OP(sc);

	memset(&c, 0, sizeof(c));
	c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
	    F_FW_CMD_WRITE);
	c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c));
	c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
	c.ip_lo = *(uint64_t *)&lip->s6_addr[8];

	rc = -t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c);
	if (rc == 0 && idx != NULL)
		*idx = G_FW_CLIP_CMD_INDEX(ntohl(c.alloc_to_len16));
	return (rc);
}

static int
del_lip(struct adapter *sc, struct in6_addr *lip)
{
	struct fw_clip_cmd c;

	ASSERT_SYNCHRONIZED_OP(sc);

	memset(&c, 0, sizeof(c));
	c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
	    F_FW_CMD_READ);
	c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c));
	c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
	c.ip_lo = *(uint64_t *)&lip->s6_addr[8];

	return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
}
#endif

struct clip_entry *
t4_get_clip_entry(struct adapter *sc, struct in6_addr *in6, bool add)
{
#ifdef INET6
	struct clip_entry *ce;
	bool schedule = false;

	mtx_lock(&clip_db_lock);
	ce = lookup_clip_entry(sc, in6, add);
	if (ce != NULL) {
		MPASS(ce->cde->adp_ref > 0);
		if (++ce->refcount == 1 && ce->pending && ce->clip_idx != -1) {
			/*
			 * Valid entry that was waiting to be deleted.  It is in
			 * use now so take it off the pending list.
			 */
			TAILQ_REMOVE(&sc->clip_pending, ce, plink);
			ce->pending = false;
		}
		if (ce->clip_idx == -1 && update_hw_clip_table(sc) != 0)
			schedule = true;
	}
	mtx_unlock(&clip_db_lock);
	if (schedule)
		taskqueue_enqueue_timeout(taskqueue_thread, &sc->clip_task, 0);

	return (ce);
#else
	return (NULL);
#endif
}

void
t4_hold_clip_entry(struct adapter *sc, struct clip_entry *ce)
{
#ifdef INET6
	MPASS(ce != NULL);
	MPASS(ce->cde->adp_ref > 0);

	mtx_lock(&clip_db_lock);
	MPASS(ce->refcount > 0); /* Caller should already have a reference */
	ce->refcount++;
	mtx_unlock(&clip_db_lock);
#endif
}

#ifdef INET6
static void
release_clip_entry_locked(struct adapter *sc, struct clip_entry *ce)
{
	struct clip_db_entry *cde;

	mtx_assert(&clip_db_lock, MA_OWNED);
	MPASS(ce->refcount > 0);
	cde = ce->cde;
	MPASS(cde->adp_ref > 0);
	if (--ce->refcount == 0 && cde->krn_ref == 0) {
		if (ce->clip_idx == -1) {
			/* Was never written to the hardware. */
			MPASS(ce->pending);
			TAILQ_REMOVE(&sc->clip_pending, ce, plink);
			LIST_REMOVE(ce, link);
			free(ce, M_CXGBE);
			if (--cde->adp_ref == 0) {
				LIST_REMOVE(cde, link);
				free(cde, M_CXGBE);
			}
		} else {
			/*
			 * Valid entry is now unused, add to the pending list
			 * for deletion.  Its refcount was 1 on entry so it
			 * can't already be pending.
			 */
			MPASS(!ce->pending);
			TAILQ_INSERT_HEAD(&sc->clip_pending, ce, plink);
			ce->pending = true;
		}
	}
}
#endif

void
t4_release_clip_entry(struct adapter *sc, struct clip_entry *ce)
{
#ifdef INET6
	MPASS(ce != NULL);

	mtx_lock(&clip_db_lock);
	release_clip_entry_locked(sc, ce);
	/*
	 * This isn't a manual release via the ioctl.  No need to update the
	 * hw right now even if the release resulted in the entry being queued
	 * for deletion.
	 */
	mtx_unlock(&clip_db_lock);
#endif
}

int
t4_release_clip_addr(struct adapter *sc, struct in6_addr *in6)
{
	int rc = ENOTSUP;
#ifdef INET6
	struct clip_entry *ce;
	bool schedule = false;

	mtx_lock(&clip_db_lock);
	ce = lookup_clip_entry(sc, in6, false);
	if (ce == NULL)
		rc = ENOENT;
	else if (ce->refcount == 0)
		rc = EIO;
	else {
		release_clip_entry_locked(sc, ce);
		if (update_hw_clip_table(sc) != 0)
			schedule = true;
		rc = 0;
	}
	mtx_unlock(&clip_db_lock);
	if (schedule)
		taskqueue_enqueue_timeout(taskqueue_thread, &sc->clip_task, 0);
#endif
	return (rc);
}

#ifdef INET6
void
t4_init_clip_table(struct adapter *sc)
{
	TAILQ_INIT(&sc->clip_pending);
	TIMEOUT_TASK_INIT(taskqueue_thread, &sc->clip_task, 0, t4_clip_task, sc);
	sc->clip_gen = -1;
	sc->clip_table = hashinit(CLIP_HASH_SIZE, M_CXGBE, &sc->clip_mask);

	/* Both the hashes must use the same bucket for the same key. */
	if (sc->clip_table != NULL)
		MPASS(sc->clip_mask == clip_db_mask);
	/*
	 * Don't bother forcing an update of the clip table when the
	 * adapter is initialized.  Before an interface can be used it
	 * must be assigned an address which will trigger the event
	 * handler to update the table.
	 */
}

/*
 * Returns true if any additions or deletions were made to the CLIP DB.
 */
static void
update_clip_db(void)
{
	VNET_ITERATOR_DECL(vnet_iter);
	struct rm_priotracker in6_ifa_tracker;
	struct in6_addr *in6, tin6;
	struct in6_ifaddr *ia;
	struct clip_db_entry *cde, *cde_tmp;
	int i, addel;

	VNET_LIST_RLOCK();
	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
	mtx_lock(&clip_db_lock);
	VNET_FOREACH(vnet_iter) {
		CURVNET_SET_QUIET(vnet_iter);
		CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
			if (if_getflags(ia->ia_ifp) & IFF_LOOPBACK)
				continue;
			in6 = &ia->ia_addr.sin6_addr;
			KASSERT(!IN6_IS_ADDR_MULTICAST(in6),
			    ("%s: mcast address in in6_ifaddr list", __func__));
			if (IN6_IS_ADDR_LOOPBACK(in6))
				continue;

			if (IN6_IS_SCOPE_EMBED(in6)) {
				tin6 = *in6;
				in6 = &tin6;
				in6_clearscope(in6);
			}
			cde = lookup_clip_db_entry(in6, true);
			if (cde == NULL)
				continue;
			cde->tmp_ref++;
		}
		CURVNET_RESTORE();
	}

	addel = 0;
	for (i = 0; i <= clip_db_mask; i++) {
		LIST_FOREACH_SAFE(cde, &clip_db[i], link, cde_tmp) {
			if (cde->krn_ref == 0 && cde->tmp_ref > 0) {
				addel++;	/* IP6 addr added. */
			} else if (cde->krn_ref > 0 && cde->tmp_ref == 0) {
				if (cde->adp_ref == 0) {
					LIST_REMOVE(cde, link);
					free(cde, M_CXGBE);
					continue;
				}
				addel++;	/* IP6 addr deleted. */
			}
			cde->krn_ref = cde->tmp_ref;
			cde->tmp_ref = 0;
		}
	}
	if (addel > 0)
		clip_db_gen++;
	mtx_unlock(&clip_db_lock);
	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
	VNET_LIST_RUNLOCK();

}

/*
 * Update the CLIP db and then update the CLIP tables on all the adapters.
 */
static void
t4_clip_db_task(void *arg, int count)
{
	update_clip_db();
	t4_iterate(update_clip_table, NULL);
}

/*
 * Refresh the sw CLIP table for this adapter from the global CLIP db.  Entries
 * that need to be added or deleted from the hardware CLIP table are placed on a
 * pending list but the hardware is not touched.  The pending list is something
 * reasonable even if this fails so it's ok to apply that to the hardware.
 */
static int
update_sw_clip_table(struct adapter *sc)
{
	struct clip_db_entry *cde;
	struct clip_entry *ce, *ce_temp;
	int i;
	bool found;

	mtx_assert(&clip_db_lock, MA_OWNED);

	/*
	 * We are about to rebuild the pending list from scratch.  Deletions are
	 * placed before additions because that's how we want to submit them to
	 * the hardware.
	 */
	TAILQ_INIT(&sc->clip_pending);

	/*
	 * Walk the sw CLIP table first.  We want to reset every entry's pending
	 * status as we're rebuilding the pending list.
	 */
	for (i = 0; i <= clip_db_mask; i++) {
		LIST_FOREACH_SAFE(ce, &sc->clip_table[i], link, ce_temp) {
			cde = ce->cde;
			MPASS(cde->adp_ref > 0);
			if (ce->refcount != 0 || cde->krn_ref != 0) {
				/*
				 * Entry should stay in the CLIP.
				 */

				if (ce->clip_idx != -1) {
					ce->pending = false;
				} else {
					/* Was never added, carry forward. */
					MPASS(ce->pending);
					TAILQ_INSERT_TAIL(&sc->clip_pending, ce,
					    plink);
				}
				continue;
			}

			/*
			 * Entry should be removed from the CLIP.
			 */

			if (ce->clip_idx != -1) {
				ce->pending = true;
				TAILQ_INSERT_HEAD(&sc->clip_pending, ce, plink);
			} else {
				/* Was never added, free right now. */
				MPASS(ce->pending);
				LIST_REMOVE(ce, link);
				free(ce, M_CXGBE);
				if (--cde->adp_ref == 0) {
					LIST_REMOVE(cde, link);
					free(cde, M_CXGBE);
				}
			}
		}
	}

	for (i = 0; i <= clip_db_mask; i++) {
		LIST_FOREACH(cde, &clip_db[i], link) {
			if (cde->krn_ref == 0)
				continue;

			found = false;
			LIST_FOREACH(ce, &sc->clip_table[i], link) {
				if (ce->cde == cde) {
					found = true;
					break;
				}
			}
			if (found)
				continue;
			ce = alloc_clip_entry(cde);
			if (ce == NULL)
				return (ENOMEM);
			LIST_INSERT_HEAD(&sc->clip_table[i], ce, link);
			TAILQ_INSERT_TAIL(&sc->clip_pending, ce, plink);
			ce->pending = true;
		}
	}

	sc->clip_gen = clip_db_gen;
	return (0);
}

static int
update_hw_clip_table(struct adapter *sc)
{
	struct clip_db_entry *cde;
	struct clip_entry *ce;
	int rc;
	char ip[INET6_ADDRSTRLEN];

	mtx_assert(&clip_db_lock, MA_OWNED);
	rc = begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4clip");
	if (rc != 0)
		return (rc);
	if (!hw_all_ok(sc))
		goto done;	/* with rc = 0, we don't want to reschedule. */
	while (!TAILQ_EMPTY(&sc->clip_pending)) {
		ce = TAILQ_FIRST(&sc->clip_pending);
		MPASS(ce->pending);
		cde = ce->cde;
		MPASS(cde->adp_ref > 0);

		if (ce->clip_idx == -1) {
			/*
			 * Entry was queued for addition to the HW CLIP.
			 */

			if (ce->refcount == 0 && cde->krn_ref == 0) {
				/* No need to add to HW CLIP. */
				TAILQ_REMOVE(&sc->clip_pending, ce, plink);
				LIST_REMOVE(ce, link);
				free(ce, M_CXGBE);
				if (--cde->adp_ref == 0) {
					LIST_REMOVE(cde, link);
					free(cde, M_CXGBE);
				}
			} else {
				/* Add to the HW CLIP. */
				rc = add_lip(sc, &cde->lip, &ce->clip_idx);
				if (rc == FW_ENOMEM) {
					/* CLIP full, no point in retrying. */
					rc = 0;
					goto done;
				}
				if (rc != 0) {
					inet_ntop(AF_INET6, &cde->lip, &ip[0],
					    sizeof(ip));
					CH_ERR(sc, "add_lip(%s) failed: %d\n",
					    ip, rc);
					goto done;
				}
				MPASS(ce->clip_idx != -1);
				TAILQ_REMOVE(&sc->clip_pending, ce, plink);
				ce->pending = false;
			}
		} else {
			/*
			 * Entry was queued for deletion from the HW CLIP.
			 */

			if (ce->refcount == 0 && cde->krn_ref == 0) {
				/*
				 * Delete from the HW CLIP.  Delete should never
				 * fail so we always log an error.  But if the
				 * failure is that the entry wasn't found in the
				 * CLIP then we carry on as if it was deleted.
				 */
				rc = del_lip(sc, &cde->lip);
				if (rc != 0)
					CH_ERR(sc, "del_lip(%s) failed: %d\n",
					    ip, rc);
				if (rc == FW_EPROTO)
					rc = 0;
				if (rc != 0)
					goto done;

				TAILQ_REMOVE(&sc->clip_pending, ce, plink);
				LIST_REMOVE(ce, link);
				free(ce, M_CXGBE);
				if (--cde->adp_ref == 0) {
					LIST_REMOVE(cde, link);
					free(cde, M_CXGBE);
				}
			} else {
				/* No need to delete from HW CLIP. */
				TAILQ_REMOVE(&sc->clip_pending, ce, plink);
				ce->pending = false;
			}
		}
	}
done:
	end_synchronized_op(sc, LOCK_HELD);
	return (rc);
}

static void
update_clip_table(struct adapter *sc, void *arg __unused)
{
	bool reschedule;

	if (sc->clip_table == NULL)
		return;

	reschedule = false;
	mtx_lock(&clip_db_lock);
	if (sc->clip_gen != clip_db_gen && update_sw_clip_table(sc) != 0)
		reschedule = true;
	if (!TAILQ_EMPTY(&sc->clip_pending) && update_hw_clip_table(sc) != 0)
		reschedule = true;
	mtx_unlock(&clip_db_lock);
	if (reschedule)
		taskqueue_enqueue_timeout(taskqueue_thread, &sc->clip_task,
		    -hz / 4);
}

/*
 * Update the CLIP table of the specified adapter.
 */
static void
t4_clip_task(void *sc, int count)
{
	update_clip_table(sc, NULL);
}

void
t4_destroy_clip_table(struct adapter *sc)
{
	struct clip_entry *ce, *ce_temp;
	int i;

	mtx_lock(&clip_db_lock);
	if (sc->clip_table == NULL)
		goto done;		/* CLIP was never initialized. */
	for (i = 0; i <= sc->clip_mask; i++) {
		LIST_FOREACH_SAFE(ce, &sc->clip_table[i], link, ce_temp) {
			MPASS(ce->refcount == 0);
			MPASS(ce->cde->adp_ref > 0);
#if 0
			del_lip(sc, &ce->lip);
#endif
			LIST_REMOVE(ce, link);
			if (--ce->cde->adp_ref == 0 && ce->cde->krn_ref == 0) {
				LIST_REMOVE(ce->cde, link);
				free(ce->cde, M_CXGBE);
			}
			free(ce, M_CXGBE);
		}
	}
	hashdestroy(sc->clip_table, M_CXGBE, sc->clip_mask);
	sc->clip_table = NULL;
done:
	mtx_unlock(&clip_db_lock);
}

static void
t4_ifaddr_event(void *arg __unused, if_t ifp, struct ifaddr *ifa,
    int event)
{
	struct in6_addr *in6;

	if (t4_clip_db_auto == 0)
		return;		/* Automatic updates not allowed. */
	if (ifa->ifa_addr->sa_family != AF_INET6)
		return;
	if (if_getflags(ifp) & IFF_LOOPBACK)
		return;
	in6 = &((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr;
	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_MULTICAST(in6))
		return;

	taskqueue_enqueue(taskqueue_thread, &clip_db_task);
}

int
sysctl_clip(SYSCTL_HANDLER_ARGS)
{
	struct adapter *sc = arg1;
	struct clip_entry *ce;
	struct sbuf *sb;
	int i, rc, header = 0;
	char ip[INET6_ADDRSTRLEN];

	rc = sysctl_wire_old_buffer(req, 0);
	if (rc != 0)
		return (rc);

	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
	if (sb == NULL)
		return (ENOMEM);

	mtx_lock(&clip_db_lock);
	for (i = 0; i <= sc->clip_mask; i++) {
		LIST_FOREACH(ce, &sc->clip_table[i], link) {
			if (header == 0) {
				sbuf_printf(sb, "%-4s %-4s %s", "Indx", "Refs",
				    "IP address");
				header = 1;
			}
			inet_ntop(AF_INET6, &ce->cde->lip, &ip[0], sizeof(ip));
			if (ce->clip_idx == -1) {
				sbuf_printf(sb, "\n%-4s %-4d %s", "-",
				    ce->refcount, ip);
			} else {
				sbuf_printf(sb, "\n%-4d %-4d %s", ce->clip_idx,
				    ce->refcount, ip);
			}
		}
	}
	mtx_unlock(&clip_db_lock);

	rc = sbuf_finish(sb);
	sbuf_delete(sb);

	return (rc);
}

static int
sysctl_clip_db(SYSCTL_HANDLER_ARGS)
{
	struct clip_db_entry *cde;
	struct sbuf *sb;
	int i, rc, header = 0;
	char ip[INET6_ADDRSTRLEN];

	rc = sysctl_wire_old_buffer(req, 0);
	if (rc != 0)
		return (rc);

	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
	if (sb == NULL)
		return (ENOMEM);

	mtx_lock(&clip_db_lock);
	for (i = 0; i <= clip_db_mask; i++) {
		LIST_FOREACH(cde, &clip_db[i], link) {
			MPASS(cde->tmp_ref == 0);
			if (header == 0) {
				sbuf_printf(sb, "%-4s %-4s %s", "Kref", "Aref",
				    "IP address");
				header = 1;
			}
			inet_ntop(AF_INET6, &cde->lip, &ip[0], sizeof(ip));
			sbuf_printf(sb, "\n%-4d %-4d %s", cde->krn_ref,
			    cde->adp_ref, ip);
		}
	}
	mtx_unlock(&clip_db_lock);

	rc = sbuf_finish(sb);
	sbuf_delete(sb);

	return (rc);
}

static int
sysctl_clip_db_auto(SYSCTL_HANDLER_ARGS)
{
	int rc, val;

	val = t4_clip_db_auto;
	rc = sysctl_handle_int(oidp, &val, 0, req);
	if (rc != 0 || req->newptr == NULL)
		return (rc);

	if (val == 0 || val == 1)
		t4_clip_db_auto = val;
	else {
		/*
		 * Writing a value other than 0 or 1 forces a one-time update of
		 * the clip_db directly in the sysctl and not in some taskqueue.
		 */
		t4_clip_db_task(NULL, 0);
	}

	return (0);
}

void
t4_clip_modload(void)
{
	mtx_init(&clip_db_lock, "clip_db", NULL, MTX_DEF);
	clip_db = hashinit(CLIP_HASH_SIZE, M_CXGBE, &clip_db_mask);
	TASK_INIT(&clip_db_task, 0, t4_clip_db_task, NULL);
	ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event_ext,
	    t4_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
}

void
t4_clip_modunload(void)
{
	struct clip_db_entry *cde;
	int i;

	EVENTHANDLER_DEREGISTER(ifaddr_event_ext, ifaddr_evhandler);
	taskqueue_drain(taskqueue_thread, &clip_db_task);
	mtx_lock(&clip_db_lock);
	for (i = 0; i <= clip_db_mask; i++) {
		while ((cde = LIST_FIRST(&clip_db[i])) != NULL) {
			MPASS(cde->tmp_ref == 0);
			MPASS(cde->adp_ref == 0);
			LIST_REMOVE(cde, link);
			free(cde, M_CXGBE);
		}
	}
	mtx_unlock(&clip_db_lock);
	hashdestroy(clip_db, M_CXGBE, clip_db_mask);
	mtx_destroy(&clip_db_lock);
}
#endif