summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/engleder/tsnep_rxnfc.c
blob: 9ac2a0cf38331e7ee49f31dcd0bcd1b2d0c5c87f (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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2022 Gerhard Engleder <gerhard@engleder-embedded.com> */

#include "tsnep.h"

#define ETHER_TYPE_FULL_MASK ((__force __be16)~0)

static void tsnep_enable_rule(struct tsnep_adapter *adapter,
			      struct tsnep_rxnfc_rule *rule)
{
	u8 rx_assign;
	void __iomem *addr;

	rx_assign = TSNEP_RX_ASSIGN_ACTIVE;
	rx_assign |= (rule->queue_index << TSNEP_RX_ASSIGN_QUEUE_SHIFT) &
		     TSNEP_RX_ASSIGN_QUEUE_MASK;

	addr = adapter->addr + TSNEP_RX_ASSIGN_ETHER_TYPE +
	       TSNEP_RX_ASSIGN_ETHER_TYPE_OFFSET * rule->location;
	iowrite16(rule->filter.ether_type, addr);

	/* enable rule after all settings are done */
	addr = adapter->addr + TSNEP_RX_ASSIGN +
	       TSNEP_RX_ASSIGN_OFFSET * rule->location;
	iowrite8(rx_assign, addr);
}

static void tsnep_disable_rule(struct tsnep_adapter *adapter,
			       struct tsnep_rxnfc_rule *rule)
{
	void __iomem *addr;

	addr = adapter->addr + TSNEP_RX_ASSIGN +
	       TSNEP_RX_ASSIGN_OFFSET * rule->location;
	iowrite8(0, addr);
}

static struct tsnep_rxnfc_rule *tsnep_get_rule(struct tsnep_adapter *adapter,
					       int location)
{
	struct tsnep_rxnfc_rule *rule;

	list_for_each_entry(rule, &adapter->rxnfc_rules, list) {
		if (rule->location == location)
			return rule;
		if (rule->location > location)
			break;
	}

	return NULL;
}

static void tsnep_add_rule(struct tsnep_adapter *adapter,
			   struct tsnep_rxnfc_rule *rule)
{
	struct tsnep_rxnfc_rule *pred, *cur;

	tsnep_enable_rule(adapter, rule);

	pred = NULL;
	list_for_each_entry(cur, &adapter->rxnfc_rules, list) {
		if (cur->location >= rule->location)
			break;
		pred = cur;
	}

	list_add(&rule->list, pred ? &pred->list : &adapter->rxnfc_rules);
	adapter->rxnfc_count++;
}

static void tsnep_delete_rule(struct tsnep_adapter *adapter,
			      struct tsnep_rxnfc_rule *rule)
{
	tsnep_disable_rule(adapter, rule);

	list_del(&rule->list);
	adapter->rxnfc_count--;

	kfree(rule);
}

static void tsnep_flush_rules(struct tsnep_adapter *adapter)
{
	struct tsnep_rxnfc_rule *rule, *tmp;

	mutex_lock(&adapter->rxnfc_lock);

	list_for_each_entry_safe(rule, tmp, &adapter->rxnfc_rules, list)
		tsnep_delete_rule(adapter, rule);

	mutex_unlock(&adapter->rxnfc_lock);
}

int tsnep_rxnfc_get_rule(struct tsnep_adapter *adapter,
			 struct ethtool_rxnfc *cmd)
{
	struct ethtool_rx_flow_spec *fsp = &cmd->fs;
	struct tsnep_rxnfc_rule *rule = NULL;

	cmd->data = adapter->rxnfc_max;

	mutex_lock(&adapter->rxnfc_lock);

	rule = tsnep_get_rule(adapter, fsp->location);
	if (!rule) {
		mutex_unlock(&adapter->rxnfc_lock);

		return -ENOENT;
	}

	fsp->flow_type = ETHER_FLOW;
	fsp->ring_cookie = rule->queue_index;

	if (rule->filter.type == TSNEP_RXNFC_ETHER_TYPE) {
		fsp->h_u.ether_spec.h_proto = htons(rule->filter.ether_type);
		fsp->m_u.ether_spec.h_proto = ETHER_TYPE_FULL_MASK;
	}

	mutex_unlock(&adapter->rxnfc_lock);

	return 0;
}

int tsnep_rxnfc_get_all(struct tsnep_adapter *adapter,
			struct ethtool_rxnfc *cmd,
			u32 *rule_locs)
{
	struct tsnep_rxnfc_rule *rule;
	int count = 0;

	cmd->data = adapter->rxnfc_max;

	mutex_lock(&adapter->rxnfc_lock);

	list_for_each_entry(rule, &adapter->rxnfc_rules, list) {
		if (count == cmd->rule_cnt) {
			mutex_unlock(&adapter->rxnfc_lock);

			return -EMSGSIZE;
		}

		rule_locs[count] = rule->location;
		count++;
	}

	mutex_unlock(&adapter->rxnfc_lock);

	cmd->rule_cnt = count;

	return 0;
}

static int tsnep_rxnfc_find_location(struct tsnep_adapter *adapter)
{
	struct tsnep_rxnfc_rule *tmp;
	int location = 0;

	list_for_each_entry(tmp, &adapter->rxnfc_rules, list) {
		if (tmp->location == location)
			location++;
		else
			return location;
	}

	if (location >= adapter->rxnfc_max)
		return -ENOSPC;

	return location;
}

static void tsnep_rxnfc_init_rule(struct tsnep_rxnfc_rule *rule,
				  const struct ethtool_rx_flow_spec *fsp)
{
	INIT_LIST_HEAD(&rule->list);

	rule->queue_index = fsp->ring_cookie;
	rule->location = fsp->location;

	rule->filter.type = TSNEP_RXNFC_ETHER_TYPE;
	rule->filter.ether_type = ntohs(fsp->h_u.ether_spec.h_proto);
}

static int tsnep_rxnfc_check_rule(struct tsnep_adapter *adapter,
				  struct tsnep_rxnfc_rule *rule)
{
	struct net_device *dev = adapter->netdev;
	struct tsnep_rxnfc_rule *tmp;

	list_for_each_entry(tmp, &adapter->rxnfc_rules, list) {
		if (!memcmp(&rule->filter, &tmp->filter, sizeof(rule->filter)) &&
		    tmp->location != rule->location) {
			netdev_dbg(dev, "rule already exists\n");

			return -EEXIST;
		}
	}

	return 0;
}

int tsnep_rxnfc_add_rule(struct tsnep_adapter *adapter,
			 struct ethtool_rxnfc *cmd)
{
	struct net_device *netdev = adapter->netdev;
	struct ethtool_rx_flow_spec *fsp =
		(struct ethtool_rx_flow_spec *)&cmd->fs;
	struct tsnep_rxnfc_rule *rule, *old_rule;
	int retval;

	/* only EtherType is supported */
	if (fsp->flow_type != ETHER_FLOW ||
	    !is_zero_ether_addr(fsp->m_u.ether_spec.h_dest) ||
	    !is_zero_ether_addr(fsp->m_u.ether_spec.h_source) ||
	    fsp->m_u.ether_spec.h_proto != ETHER_TYPE_FULL_MASK) {
		netdev_dbg(netdev, "only ethernet protocol is supported\n");

		return -EOPNOTSUPP;
	}

	if (fsp->ring_cookie >
	    (TSNEP_RX_ASSIGN_QUEUE_MASK >> TSNEP_RX_ASSIGN_QUEUE_SHIFT)) {
		netdev_dbg(netdev, "invalid action\n");

		return -EINVAL;
	}

	if (fsp->location != RX_CLS_LOC_ANY &&
	    fsp->location >= adapter->rxnfc_max) {
		netdev_dbg(netdev, "invalid location\n");

		return -EINVAL;
	}

	rule = kzalloc(sizeof(*rule), GFP_KERNEL);
	if (!rule)
		return -ENOMEM;

	mutex_lock(&adapter->rxnfc_lock);

	if (fsp->location == RX_CLS_LOC_ANY) {
		retval = tsnep_rxnfc_find_location(adapter);
		if (retval < 0)
			goto failed;
		fsp->location = retval;
	}

	tsnep_rxnfc_init_rule(rule, fsp);

	retval = tsnep_rxnfc_check_rule(adapter, rule);
	if (retval)
		goto failed;

	old_rule = tsnep_get_rule(adapter, fsp->location);
	if (old_rule)
		tsnep_delete_rule(adapter, old_rule);

	tsnep_add_rule(adapter, rule);

	mutex_unlock(&adapter->rxnfc_lock);

	return 0;

failed:
	mutex_unlock(&adapter->rxnfc_lock);
	kfree(rule);
	return retval;
}

int tsnep_rxnfc_del_rule(struct tsnep_adapter *adapter,
			 struct ethtool_rxnfc *cmd)
{
	struct ethtool_rx_flow_spec *fsp =
		(struct ethtool_rx_flow_spec *)&cmd->fs;
	struct tsnep_rxnfc_rule *rule;

	mutex_lock(&adapter->rxnfc_lock);

	rule = tsnep_get_rule(adapter, fsp->location);
	if (!rule) {
		mutex_unlock(&adapter->rxnfc_lock);

		return -ENOENT;
	}

	tsnep_delete_rule(adapter, rule);

	mutex_unlock(&adapter->rxnfc_lock);

	return 0;
}

int tsnep_rxnfc_init(struct tsnep_adapter *adapter)
{
	int i;

	/* disable all rules */
	for (i = 0; i < adapter->rxnfc_max;
	     i += sizeof(u32) / TSNEP_RX_ASSIGN_OFFSET)
		iowrite32(0, adapter->addr + TSNEP_RX_ASSIGN + i);

	return 0;
}

void tsnep_rxnfc_cleanup(struct tsnep_adapter *adapter)
{
	tsnep_flush_rules(adapter);
}