aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/chelsio/cxgb4/cxgb4_mps.c
blob: a020e84906813f49d619ebe8ecfba335275bb374 (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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 Chelsio Communications, Inc. All rights reserved. */

#include "cxgb4.h"

static int cxgb4_mps_ref_dec_by_mac(struct adapter *adap,
				    const u8 *addr, const u8 *mask)
{
	u8 bitmask[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
	struct mps_entries_ref *mps_entry, *tmp;
	int ret = -EINVAL;

	spin_lock_bh(&adap->mps_ref_lock);
	list_for_each_entry_safe(mps_entry, tmp, &adap->mps_ref, list) {
		if (ether_addr_equal(mps_entry->addr, addr) &&
		    ether_addr_equal(mps_entry->mask, mask ? mask : bitmask)) {
			if (!refcount_dec_and_test(&mps_entry->refcnt)) {
				spin_unlock_bh(&adap->mps_ref_lock);
				return -EBUSY;
			}
			list_del(&mps_entry->list);
			kfree(mps_entry);
			ret = 0;
			break;
		}
	}
	spin_unlock_bh(&adap->mps_ref_lock);
	return ret;
}

static int cxgb4_mps_ref_dec(struct adapter *adap, u16 idx)
{
	struct mps_entries_ref *mps_entry, *tmp;
	int ret = -EINVAL;

	spin_lock(&adap->mps_ref_lock);
	list_for_each_entry_safe(mps_entry, tmp, &adap->mps_ref, list) {
		if (mps_entry->idx == idx) {
			if (!refcount_dec_and_test(&mps_entry->refcnt)) {
				spin_unlock(&adap->mps_ref_lock);
				return -EBUSY;
			}
			list_del(&mps_entry->list);
			kfree(mps_entry);
			ret = 0;
			break;
		}
	}
	spin_unlock(&adap->mps_ref_lock);
	return ret;
}

static int cxgb4_mps_ref_inc(struct adapter *adap, const u8 *mac_addr,
			     u16 idx, const u8 *mask)
{
	u8 bitmask[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
	struct mps_entries_ref *mps_entry;
	int ret = 0;

	spin_lock_bh(&adap->mps_ref_lock);
	list_for_each_entry(mps_entry, &adap->mps_ref, list) {
		if (mps_entry->idx == idx) {
			refcount_inc(&mps_entry->refcnt);
			goto unlock;
		}
	}
	mps_entry = kzalloc(sizeof(*mps_entry), GFP_ATOMIC);
	if (!mps_entry) {
		ret = -ENOMEM;
		goto unlock;
	}
	ether_addr_copy(mps_entry->mask, mask ? mask : bitmask);
	ether_addr_copy(mps_entry->addr, mac_addr);
	mps_entry->idx = idx;
	refcount_set(&mps_entry->refcnt, 1);
	list_add_tail(&mps_entry->list, &adap->mps_ref);
unlock:
	spin_unlock_bh(&adap->mps_ref_lock);
	return ret;
}

int cxgb4_free_mac_filt(struct adapter *adap, unsigned int viid,
			unsigned int naddr, const u8 **addr, bool sleep_ok)
{
	int ret, i;

	for (i = 0; i < naddr; i++) {
		if (!cxgb4_mps_ref_dec_by_mac(adap, addr[i], NULL)) {
			ret = t4_free_mac_filt(adap, adap->mbox, viid,
					       1, &addr[i], sleep_ok);
			if (ret < 0)
				return ret;
		}
	}

	/* return number of filters freed */
	return naddr;
}

int cxgb4_alloc_mac_filt(struct adapter *adap, unsigned int viid,
			 bool free, unsigned int naddr, const u8 **addr,
			 u16 *idx, u64 *hash, bool sleep_ok)
{
	int ret, i;

	ret = t4_alloc_mac_filt(adap, adap->mbox, viid, free,
				naddr, addr, idx, hash, sleep_ok);
	if (ret < 0)
		return ret;

	for (i = 0; i < naddr; i++) {
		if (idx[i] != 0xffff) {
			if (cxgb4_mps_ref_inc(adap, addr[i], idx[i], NULL)) {
				ret = -ENOMEM;
				goto error;
			}
		}
	}

	goto out;
error:
	cxgb4_free_mac_filt(adap, viid, naddr, addr, sleep_ok);

out:
	/* Returns a negative error number or the number of filters allocated */
	return ret;
}

int cxgb4_update_mac_filt(struct port_info *pi, unsigned int viid,
			  int *tcam_idx, const u8 *addr,
			  bool persistent, u8 *smt_idx)
{
	int ret;

	ret = cxgb4_change_mac(pi, viid, tcam_idx,
			       addr, persistent, smt_idx);
	if (ret < 0)
		return ret;

	cxgb4_mps_ref_inc(pi->adapter, addr, *tcam_idx, NULL);
	return ret;
}

int cxgb4_free_raw_mac_filt(struct adapter *adap,
			    unsigned int viid,
			    const u8 *addr,
			    const u8 *mask,
			    unsigned int idx,
			    u8 lookup_type,
			    u8 port_id,
			    bool sleep_ok)
{
	int ret = 0;

	if (!cxgb4_mps_ref_dec(adap, idx))
		ret = t4_free_raw_mac_filt(adap, viid, addr,
					   mask, idx, lookup_type,
					   port_id, sleep_ok);

	return ret;
}

int cxgb4_alloc_raw_mac_filt(struct adapter *adap,
			     unsigned int viid,
			     const u8 *addr,
			     const u8 *mask,
			     unsigned int idx,
			     u8 lookup_type,
			     u8 port_id,
			     bool sleep_ok)
{
	int ret;

	ret = t4_alloc_raw_mac_filt(adap, viid, addr,
				    mask, idx, lookup_type,
				    port_id, sleep_ok);
	if (ret < 0)
		return ret;

	if (cxgb4_mps_ref_inc(adap, addr, ret, mask)) {
		ret = -ENOMEM;
		t4_free_raw_mac_filt(adap, viid, addr,
				     mask, idx, lookup_type,
				     port_id, sleep_ok);
	}

	return ret;
}

int cxgb4_free_encap_mac_filt(struct adapter *adap, unsigned int viid,
			      int idx, bool sleep_ok)
{
	int ret = 0;

	if (!cxgb4_mps_ref_dec(adap, idx))
		ret = t4_free_encap_mac_filt(adap, viid, idx, sleep_ok);

	return ret;
}

int cxgb4_alloc_encap_mac_filt(struct adapter *adap, unsigned int viid,
			       const u8 *addr, const u8 *mask,
			       unsigned int vni, unsigned int vni_mask,
			       u8 dip_hit, u8 lookup_type, bool sleep_ok)
{
	int ret;

	ret = t4_alloc_encap_mac_filt(adap, viid, addr, mask, vni, vni_mask,
				      dip_hit, lookup_type, sleep_ok);
	if (ret < 0)
		return ret;

	if (cxgb4_mps_ref_inc(adap, addr, ret, mask)) {
		ret = -ENOMEM;
		t4_free_encap_mac_filt(adap, viid, ret, sleep_ok);
	}
	return ret;
}

int cxgb4_init_mps_ref_entries(struct adapter *adap)
{
	spin_lock_init(&adap->mps_ref_lock);
	INIT_LIST_HEAD(&adap->mps_ref);

	return 0;
}

void cxgb4_free_mps_ref_entries(struct adapter *adap)
{
	struct mps_entries_ref *mps_entry, *tmp;

	if (list_empty(&adap->mps_ref))
		return;

	spin_lock(&adap->mps_ref_lock);
	list_for_each_entry_safe(mps_entry, tmp, &adap->mps_ref, list) {
		list_del(&mps_entry->list);
		kfree(mps_entry);
	}
	spin_unlock(&adap->mps_ref_lock);
}