summaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/chelsio/cxgb4/clip_tbl.c
blob: 163efab27e9b8e6f9fea78b63f12a49a3939ef1b (plain)
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
/*
 *  This file is part of the Chelsio T4 Ethernet driver for Linux.
 *  Copyright (C) 2003-2014 Chelsio Communications.  All rights reserved.
 *
 *  Written by Deepak (deepak.s@chelsio.com)
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the LICENSE file included in this
 *  release for licensing terms and conditions.
 */

#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/jhash.h>
#include <linux/if_vlan.h>
#include <net/addrconf.h>
#include "cxgb4.h"
#include "clip_tbl.h"

static inline unsigned int ipv4_clip_hash(struct clip_tbl *c, const u32 *key)
{
	unsigned int clipt_size_half = c->clipt_size / 2;

	return jhash_1word(*key, 0) % clipt_size_half;
}

static inline unsigned int ipv6_clip_hash(struct clip_tbl *d, const u32 *key)
{
	unsigned int clipt_size_half = d->clipt_size / 2;
	u32 xor = key[0] ^ key[1] ^ key[2] ^ key[3];

	return clipt_size_half +
		(jhash_1word(xor, 0) % clipt_size_half);
}

static unsigned int clip_addr_hash(struct clip_tbl *ctbl, const u32 *addr,
				   u8 v6)
{
	return v6 ? ipv6_clip_hash(ctbl, addr) :
			ipv4_clip_hash(ctbl, addr);
}

static int clip6_get_mbox(const struct net_device *dev,
			  const struct in6_addr *lip)
{
	struct adapter *adap = netdev2adap(dev);
	struct fw_clip_cmd c;

	memset(&c, 0, sizeof(c));
	c.op_to_write = htonl(FW_CMD_OP_V(FW_CLIP_CMD) |
			      FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
	c.alloc_to_len16 = htonl(FW_CLIP_CMD_ALLOC_F | FW_LEN16(c));
	*(__be64 *)&c.ip_hi = *(__be64 *)(lip->s6_addr);
	*(__be64 *)&c.ip_lo = *(__be64 *)(lip->s6_addr + 8);
	return t4_wr_mbox_meat(adap, adap->mbox, &c, sizeof(c), &c, false);
}

static int clip6_release_mbox(const struct net_device *dev,
			      const struct in6_addr *lip)
{
	struct adapter *adap = netdev2adap(dev);
	struct fw_clip_cmd c;

	memset(&c, 0, sizeof(c));
	c.op_to_write = htonl(FW_CMD_OP_V(FW_CLIP_CMD) |
			      FW_CMD_REQUEST_F | FW_CMD_READ_F);
	c.alloc_to_len16 = htonl(FW_CLIP_CMD_FREE_F | FW_LEN16(c));
	*(__be64 *)&c.ip_hi = *(__be64 *)(lip->s6_addr);
	*(__be64 *)&c.ip_lo = *(__be64 *)(lip->s6_addr + 8);
	return t4_wr_mbox_meat(adap, adap->mbox, &c, sizeof(c), &c, false);
}

int cxgb4_clip_get(const struct net_device *dev, const u32 *lip, u8 v6)
{
	struct adapter *adap = netdev2adap(dev);
	struct clip_tbl *ctbl = adap->clipt;
	struct clip_entry *ce, *cte;
	u32 *addr = (u32 *)lip;
	int hash;
	int ret = -1;

	if (!ctbl)
		return 0;

	hash = clip_addr_hash(ctbl, addr, v6);

	read_lock_bh(&ctbl->lock);
	list_for_each_entry(cte, &ctbl->hash_list[hash], list) {
		if (cte->addr6.sin6_family == AF_INET6 && v6)
			ret = memcmp(lip, cte->addr6.sin6_addr.s6_addr,
				     sizeof(struct in6_addr));
		else if (cte->addr.sin_family == AF_INET && !v6)
			ret = memcmp(lip, (char *)(&cte->addr.sin_addr),
				     sizeof(struct in_addr));
		if (!ret) {
			ce = cte;
			read_unlock_bh(&ctbl->lock);
			refcount_inc(&ce->refcnt);
			return 0;
		}
	}
	read_unlock_bh(&ctbl->lock);

	write_lock_bh(&ctbl->lock);
	if (!list_empty(&ctbl->ce_free_head)) {
		ce = list_first_entry(&ctbl->ce_free_head,
				      struct clip_entry, list);
		list_del_init(&ce->list);
		spin_lock_init(&ce->lock);
		refcount_set(&ce->refcnt, 0);
		atomic_dec(&ctbl->nfree);
		list_add_tail(&ce->list, &ctbl->hash_list[hash]);
		if (v6) {
			ce->addr6.sin6_family = AF_INET6;
			memcpy(ce->addr6.sin6_addr.s6_addr,
			       lip, sizeof(struct in6_addr));
			ret = clip6_get_mbox(dev, (const struct in6_addr *)lip);
			if (ret) {
				write_unlock_bh(&ctbl->lock);
				dev_err(adap->pdev_dev,
					"CLIP FW cmd failed with error %d, "
					"Connections using %pI6c wont be "
					"offloaded",
					ret, ce->addr6.sin6_addr.s6_addr);
				return ret;
			}
		} else {
			ce->addr.sin_family = AF_INET;
			memcpy((char *)(&ce->addr.sin_addr), lip,
			       sizeof(struct in_addr));
		}
	} else {
		write_unlock_bh(&ctbl->lock);
		dev_info(adap->pdev_dev, "CLIP table overflow, "
			 "Connections using %pI6c wont be offloaded",
			 (void *)lip);
		return -ENOMEM;
	}
	write_unlock_bh(&ctbl->lock);
	refcount_set(&ce->refcnt, 1);
	return 0;
}
EXPORT_SYMBOL(cxgb4_clip_get);

void cxgb4_clip_release(const struct net_device *dev, const u32 *lip, u8 v6)
{
	struct adapter *adap = netdev2adap(dev);
	struct clip_tbl *ctbl = adap->clipt;
	struct clip_entry *ce, *cte;
	u32 *addr = (u32 *)lip;
	int hash;
	int ret = -1;

	if (!ctbl)
		return;

	hash = clip_addr_hash(ctbl, addr, v6);

	read_lock_bh(&ctbl->lock);
	list_for_each_entry(cte, &ctbl->hash_list[hash], list) {
		if (cte->addr6.sin6_family == AF_INET6 && v6)
			ret = memcmp(lip, cte->addr6.sin6_addr.s6_addr,
				     sizeof(struct in6_addr));
		else if (cte->addr.sin_family == AF_INET && !v6)
			ret = memcmp(lip, (char *)(&cte->addr.sin_addr),
				     sizeof(struct in_addr));
		if (!ret) {
			ce = cte;
			read_unlock_bh(&ctbl->lock);
			goto found;
		}
	}
	read_unlock_bh(&ctbl->lock);

	return;
found:
	write_lock_bh(&ctbl->lock);
	spin_lock_bh(&ce->lock);
	if (refcount_dec_and_test(&ce->refcnt)) {
		list_del_init(&ce->list);
		list_add_tail(&ce->list, &ctbl->ce_free_head);
		atomic_inc(&ctbl->nfree);
		if (v6)
			clip6_release_mbox(dev, (const struct in6_addr *)lip);
	}
	spin_unlock_bh(&ce->lock);
	write_unlock_bh(&ctbl->lock);
}
EXPORT_SYMBOL(cxgb4_clip_release);

/* Retrieves IPv6 addresses from a root device (bond, vlan) associated with
 * a physical device.
 * The physical device reference is needed to send the actul CLIP command.
 */
static int cxgb4_update_dev_clip(struct net_device *root_dev,
				 struct net_device *dev)
{
	struct inet6_dev *idev = NULL;
	struct inet6_ifaddr *ifa;
	int ret = 0;

	idev = __in6_dev_get(root_dev);
	if (!idev)
		return ret;

	read_lock_bh(&idev->lock);
	list_for_each_entry(ifa, &idev->addr_list, if_list) {
		ret = cxgb4_clip_get(dev, (const u32 *)ifa->addr.s6_addr, 1);
		if (ret < 0)
			break;
	}
	read_unlock_bh(&idev->lock);

	return ret;
}

int cxgb4_update_root_dev_clip(struct net_device *dev)
{
	struct net_device *root_dev = NULL;
	int i, ret = 0;

	/* First populate the real net device's IPv6 addresses */
	ret = cxgb4_update_dev_clip(dev, dev);
	if (ret)
		return ret;

	/* Parse all bond and vlan devices layered on top of the physical dev */
	root_dev = netdev_master_upper_dev_get_rcu(dev);
	if (root_dev) {
		ret = cxgb4_update_dev_clip(root_dev, dev);
		if (ret)
			return ret;
	}

	for (i = 0; i < VLAN_N_VID; i++) {
		root_dev = __vlan_find_dev_deep_rcu(dev, htons(ETH_P_8021Q), i);
		if (!root_dev)
			continue;

		ret = cxgb4_update_dev_clip(root_dev, dev);
		if (ret)
			break;
	}

	return ret;
}
EXPORT_SYMBOL(cxgb4_update_root_dev_clip);

int clip_tbl_show(struct seq_file *seq, void *v)
{
	struct adapter *adapter = seq->private;
	struct clip_tbl *ctbl = adapter->clipt;
	struct clip_entry *ce;
	char ip[60];
	int i;

	read_lock_bh(&ctbl->lock);

	seq_puts(seq, "IP Address                  Users\n");
	for (i = 0 ; i < ctbl->clipt_size;  ++i) {
		list_for_each_entry(ce, &ctbl->hash_list[i], list) {
			ip[0] = '\0';
			sprintf(ip, "%pISc", &ce->addr);
			seq_printf(seq, "%-25s   %u\n", ip,
				   refcount_read(&ce->refcnt));
		}
	}
	seq_printf(seq, "Free clip entries : %d\n", atomic_read(&ctbl->nfree));

	read_unlock_bh(&ctbl->lock);

	return 0;
}

struct clip_tbl *t4_init_clip_tbl(unsigned int clipt_start,
				  unsigned int clipt_end)
{
	struct clip_entry *cl_list;
	struct clip_tbl *ctbl;
	unsigned int clipt_size;
	int i;

	if (clipt_start >= clipt_end)
		return NULL;
	clipt_size = clipt_end - clipt_start + 1;
	if (clipt_size < CLIPT_MIN_HASH_BUCKETS)
		return NULL;

	ctbl = kvzalloc(struct_size(ctbl, hash_list, clipt_size), GFP_KERNEL);
	if (!ctbl)
		return NULL;

	ctbl->clipt_start = clipt_start;
	ctbl->clipt_size = clipt_size;
	INIT_LIST_HEAD(&ctbl->ce_free_head);

	atomic_set(&ctbl->nfree, clipt_size);
	rwlock_init(&ctbl->lock);

	for (i = 0; i < ctbl->clipt_size; ++i)
		INIT_LIST_HEAD(&ctbl->hash_list[i]);

	cl_list = kvcalloc(clipt_size, sizeof(struct clip_entry), GFP_KERNEL);
	if (!cl_list) {
		kvfree(ctbl);
		return NULL;
	}
	ctbl->cl_list = (void *)cl_list;

	for (i = 0; i < clipt_size; i++) {
		INIT_LIST_HEAD(&cl_list[i].list);
		list_add_tail(&cl_list[i].list, &ctbl->ce_free_head);
	}

	return ctbl;
}

void t4_cleanup_clip_tbl(struct adapter *adap)
{
	struct clip_tbl *ctbl = adap->clipt;

	if (ctbl) {
		kvfree(ctbl->cl_list);
		kvfree(ctbl);
	}
}
EXPORT_SYMBOL(t4_cleanup_clip_tbl);