summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/intel/ice/ice_fltr.c
blob: 52d9d78039e220a7e575156fef76c8a7a76544f6 (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
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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2018-2020, Intel Corporation. */

#include "ice.h"
#include "ice_fltr.h"

/**
 * ice_fltr_free_list - free filter lists helper
 * @dev: pointer to the device struct
 * @h: pointer to the list head to be freed
 *
 * Helper function to free filter lists previously created using
 * ice_fltr_add_mac_to_list
 */
void ice_fltr_free_list(struct device *dev, struct list_head *h)
{
	struct ice_fltr_list_entry *e, *tmp;

	list_for_each_entry_safe(e, tmp, h, list_entry) {
		list_del(&e->list_entry);
		devm_kfree(dev, e);
	}
}

/**
 * ice_fltr_add_entry_to_list - allocate and add filter entry to list
 * @dev: pointer to device needed by alloc function
 * @info: filter info struct that gets added to the passed in list
 * @list: pointer to the list which contains MAC filters entry
 */
static int
ice_fltr_add_entry_to_list(struct device *dev, struct ice_fltr_info *info,
			   struct list_head *list)
{
	struct ice_fltr_list_entry *entry;

	entry = devm_kzalloc(dev, sizeof(*entry), GFP_ATOMIC);
	if (!entry)
		return -ENOMEM;

	entry->fltr_info = *info;

	INIT_LIST_HEAD(&entry->list_entry);
	list_add(&entry->list_entry, list);

	return 0;
}

/**
 * ice_fltr_set_vlan_vsi_promisc
 * @hw: pointer to the hardware structure
 * @vsi: the VSI being configured
 * @promisc_mask: mask of promiscuous config bits
 *
 * Set VSI with all associated VLANs to given promiscuous mode(s)
 */
int
ice_fltr_set_vlan_vsi_promisc(struct ice_hw *hw, struct ice_vsi *vsi,
			      u8 promisc_mask)
{
	return ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_mask, false);
}

/**
 * ice_fltr_clear_vlan_vsi_promisc
 * @hw: pointer to the hardware structure
 * @vsi: the VSI being configured
 * @promisc_mask: mask of promiscuous config bits
 *
 * Clear VSI with all associated VLANs to given promiscuous mode(s)
 */
int
ice_fltr_clear_vlan_vsi_promisc(struct ice_hw *hw, struct ice_vsi *vsi,
				u8 promisc_mask)
{
	return ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_mask, true);
}

/**
 * ice_fltr_clear_vsi_promisc - clear specified promiscuous mode(s)
 * @hw: pointer to the hardware structure
 * @vsi_handle: VSI handle to clear mode
 * @promisc_mask: mask of promiscuous config bits to clear
 * @vid: VLAN ID to clear VLAN promiscuous
 */
int
ice_fltr_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
			   u16 vid)
{
	return ice_clear_vsi_promisc(hw, vsi_handle, promisc_mask, vid);
}

/**
 * ice_fltr_set_vsi_promisc - set given VSI to given promiscuous mode(s)
 * @hw: pointer to the hardware structure
 * @vsi_handle: VSI handle to configure
 * @promisc_mask: mask of promiscuous config bits
 * @vid: VLAN ID to set VLAN promiscuous
 */
int
ice_fltr_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
			 u16 vid)
{
	return ice_set_vsi_promisc(hw, vsi_handle, promisc_mask, vid);
}

/**
 * ice_fltr_add_mac_list - add list of MAC filters
 * @vsi: pointer to VSI struct
 * @list: list of filters
 */
int
ice_fltr_add_mac_list(struct ice_vsi *vsi, struct list_head *list)
{
	return ice_add_mac(&vsi->back->hw, list);
}

/**
 * ice_fltr_remove_mac_list - remove list of MAC filters
 * @vsi: pointer to VSI struct
 * @list: list of filters
 */
int
ice_fltr_remove_mac_list(struct ice_vsi *vsi, struct list_head *list)
{
	return ice_remove_mac(&vsi->back->hw, list);
}

/**
 * ice_fltr_add_vlan_list - add list of VLAN filters
 * @vsi: pointer to VSI struct
 * @list: list of filters
 */
static int
ice_fltr_add_vlan_list(struct ice_vsi *vsi, struct list_head *list)
{
	return ice_add_vlan(&vsi->back->hw, list);
}

/**
 * ice_fltr_remove_vlan_list - remove list of VLAN filters
 * @vsi: pointer to VSI struct
 * @list: list of filters
 */
static int
ice_fltr_remove_vlan_list(struct ice_vsi *vsi, struct list_head *list)
{
	return ice_remove_vlan(&vsi->back->hw, list);
}

/**
 * ice_fltr_add_eth_list - add list of ethertype filters
 * @vsi: pointer to VSI struct
 * @list: list of filters
 */
static int
ice_fltr_add_eth_list(struct ice_vsi *vsi, struct list_head *list)
{
	return ice_add_eth_mac(&vsi->back->hw, list);
}

/**
 * ice_fltr_remove_eth_list - remove list of ethertype filters
 * @vsi: pointer to VSI struct
 * @list: list of filters
 */
static int
ice_fltr_remove_eth_list(struct ice_vsi *vsi, struct list_head *list)
{
	return ice_remove_eth_mac(&vsi->back->hw, list);
}

/**
 * ice_fltr_remove_all - remove all filters associated with VSI
 * @vsi: pointer to VSI struct
 */
void ice_fltr_remove_all(struct ice_vsi *vsi)
{
	ice_remove_vsi_fltr(&vsi->back->hw, vsi->idx);
}

/**
 * ice_fltr_add_mac_to_list - add MAC filter info to exsisting list
 * @vsi: pointer to VSI struct
 * @list: list to add filter info to
 * @mac: MAC address to add
 * @action: filter action
 */
int
ice_fltr_add_mac_to_list(struct ice_vsi *vsi, struct list_head *list,
			 const u8 *mac, enum ice_sw_fwd_act_type action)
{
	struct ice_fltr_info info = { 0 };

	info.flag = ICE_FLTR_TX;
	info.src_id = ICE_SRC_ID_VSI;
	info.lkup_type = ICE_SW_LKUP_MAC;
	info.fltr_act = action;
	info.vsi_handle = vsi->idx;

	ether_addr_copy(info.l_data.mac.mac_addr, mac);

	return ice_fltr_add_entry_to_list(ice_pf_to_dev(vsi->back), &info,
					  list);
}

/**
 * ice_fltr_add_vlan_to_list - add VLAN filter info to exsisting list
 * @vsi: pointer to VSI struct
 * @list: list to add filter info to
 * @vlan_id: VLAN ID to add
 * @action: filter action
 */
static int
ice_fltr_add_vlan_to_list(struct ice_vsi *vsi, struct list_head *list,
			  u16 vlan_id, enum ice_sw_fwd_act_type action)
{
	struct ice_fltr_info info = { 0 };

	info.flag = ICE_FLTR_TX;
	info.src_id = ICE_SRC_ID_VSI;
	info.lkup_type = ICE_SW_LKUP_VLAN;
	info.fltr_act = action;
	info.vsi_handle = vsi->idx;
	info.l_data.vlan.vlan_id = vlan_id;

	return ice_fltr_add_entry_to_list(ice_pf_to_dev(vsi->back), &info,
					  list);
}

/**
 * ice_fltr_add_eth_to_list - add ethertype filter info to exsisting list
 * @vsi: pointer to VSI struct
 * @list: list to add filter info to
 * @ethertype: ethertype of packet that matches filter
 * @flag: filter direction, Tx or Rx
 * @action: filter action
 */
static int
ice_fltr_add_eth_to_list(struct ice_vsi *vsi, struct list_head *list,
			 u16 ethertype, u16 flag,
			 enum ice_sw_fwd_act_type action)
{
	struct ice_fltr_info info = { 0 };

	info.flag = flag;
	info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
	info.fltr_act = action;
	info.vsi_handle = vsi->idx;
	info.l_data.ethertype_mac.ethertype = ethertype;

	if (flag == ICE_FLTR_TX)
		info.src_id = ICE_SRC_ID_VSI;
	else
		info.src_id = ICE_SRC_ID_LPORT;

	return ice_fltr_add_entry_to_list(ice_pf_to_dev(vsi->back), &info,
					  list);
}

/**
 * ice_fltr_prepare_mac - add or remove MAC rule
 * @vsi: pointer to VSI struct
 * @mac: MAC address to add
 * @action: action to be performed on filter match
 * @mac_action: pointer to add or remove MAC function
 */
static int
ice_fltr_prepare_mac(struct ice_vsi *vsi, const u8 *mac,
		     enum ice_sw_fwd_act_type action,
		     int (*mac_action)(struct ice_vsi *, struct list_head *))
{
	int result;
	LIST_HEAD(tmp_list);

	if (ice_fltr_add_mac_to_list(vsi, &tmp_list, mac, action)) {
		ice_fltr_free_list(ice_pf_to_dev(vsi->back), &tmp_list);
		return -ENOMEM;
	}

	result = mac_action(vsi, &tmp_list);
	ice_fltr_free_list(ice_pf_to_dev(vsi->back), &tmp_list);
	return result;
}

/**
 * ice_fltr_prepare_mac_and_broadcast - add or remove MAC and broadcast filter
 * @vsi: pointer to VSI struct
 * @mac: MAC address to add
 * @action: action to be performed on filter match
 * @mac_action: pointer to add or remove MAC function
 */
static int
ice_fltr_prepare_mac_and_broadcast(struct ice_vsi *vsi, const u8 *mac,
				   enum ice_sw_fwd_act_type action,
				   int(*mac_action)
				   (struct ice_vsi *, struct list_head *))
{
	u8 broadcast[ETH_ALEN];
	int result;
	LIST_HEAD(tmp_list);

	eth_broadcast_addr(broadcast);
	if (ice_fltr_add_mac_to_list(vsi, &tmp_list, mac, action) ||
	    ice_fltr_add_mac_to_list(vsi, &tmp_list, broadcast, action)) {
		ice_fltr_free_list(ice_pf_to_dev(vsi->back), &tmp_list);
		return -ENOMEM;
	}

	result = mac_action(vsi, &tmp_list);
	ice_fltr_free_list(ice_pf_to_dev(vsi->back), &tmp_list);
	return result;
}

/**
 * ice_fltr_prepare_vlan - add or remove VLAN filter
 * @vsi: pointer to VSI struct
 * @vlan_id: VLAN ID to add
 * @action: action to be performed on filter match
 * @vlan_action: pointer to add or remove VLAN function
 */
static int
ice_fltr_prepare_vlan(struct ice_vsi *vsi, u16 vlan_id,
		      enum ice_sw_fwd_act_type action,
		      int (*vlan_action)(struct ice_vsi *, struct list_head *))
{
	int result;
	LIST_HEAD(tmp_list);

	if (ice_fltr_add_vlan_to_list(vsi, &tmp_list, vlan_id, action))
		return -ENOMEM;

	result = vlan_action(vsi, &tmp_list);
	ice_fltr_free_list(ice_pf_to_dev(vsi->back), &tmp_list);
	return result;
}

/**
 * ice_fltr_prepare_eth - add or remove ethertype filter
 * @vsi: pointer to VSI struct
 * @ethertype: ethertype of packet to be filtered
 * @flag: direction of packet, Tx or Rx
 * @action: action to be performed on filter match
 * @eth_action: pointer to add or remove ethertype function
 */
static int
ice_fltr_prepare_eth(struct ice_vsi *vsi, u16 ethertype, u16 flag,
		     enum ice_sw_fwd_act_type action,
		     int (*eth_action)(struct ice_vsi *, struct list_head *))
{
	int result;
	LIST_HEAD(tmp_list);

	if (ice_fltr_add_eth_to_list(vsi, &tmp_list, ethertype, flag, action))
		return -ENOMEM;

	result = eth_action(vsi, &tmp_list);
	ice_fltr_free_list(ice_pf_to_dev(vsi->back), &tmp_list);
	return result;
}

/**
 * ice_fltr_add_mac - add single MAC filter
 * @vsi: pointer to VSI struct
 * @mac: MAC to add
 * @action: action to be performed on filter match
 */
int ice_fltr_add_mac(struct ice_vsi *vsi, const u8 *mac,
		     enum ice_sw_fwd_act_type action)
{
	return ice_fltr_prepare_mac(vsi, mac, action, ice_fltr_add_mac_list);
}

/**
 * ice_fltr_add_mac_and_broadcast - add single MAC and broadcast
 * @vsi: pointer to VSI struct
 * @mac: MAC to add
 * @action: action to be performed on filter match
 */
int
ice_fltr_add_mac_and_broadcast(struct ice_vsi *vsi, const u8 *mac,
			       enum ice_sw_fwd_act_type action)
{
	return ice_fltr_prepare_mac_and_broadcast(vsi, mac, action,
						  ice_fltr_add_mac_list);
}

/**
 * ice_fltr_remove_mac - remove MAC filter
 * @vsi: pointer to VSI struct
 * @mac: filter MAC to remove
 * @action: action to remove
 */
int ice_fltr_remove_mac(struct ice_vsi *vsi, const u8 *mac,
			enum ice_sw_fwd_act_type action)
{
	return ice_fltr_prepare_mac(vsi, mac, action, ice_fltr_remove_mac_list);
}

/**
 * ice_fltr_add_vlan - add single VLAN filter
 * @vsi: pointer to VSI struct
 * @vlan_id: VLAN ID to add
 * @action: action to be performed on filter match
 */
int ice_fltr_add_vlan(struct ice_vsi *vsi, u16 vlan_id,
		      enum ice_sw_fwd_act_type action)
{
	return ice_fltr_prepare_vlan(vsi, vlan_id, action,
				     ice_fltr_add_vlan_list);
}

/**
 * ice_fltr_remove_vlan - remove VLAN filter
 * @vsi: pointer to VSI struct
 * @vlan_id: filter VLAN to remove
 * @action: action to remove
 */
int ice_fltr_remove_vlan(struct ice_vsi *vsi, u16 vlan_id,
			 enum ice_sw_fwd_act_type action)
{
	return ice_fltr_prepare_vlan(vsi, vlan_id, action,
				     ice_fltr_remove_vlan_list);
}

/**
 * ice_fltr_add_eth - add specyfic ethertype filter
 * @vsi: pointer to VSI struct
 * @ethertype: ethertype of filter
 * @flag: direction of packet to be filtered, Tx or Rx
 * @action: action to be performed on filter match
 */
int ice_fltr_add_eth(struct ice_vsi *vsi, u16 ethertype, u16 flag,
		     enum ice_sw_fwd_act_type action)
{
	return ice_fltr_prepare_eth(vsi, ethertype, flag, action,
				    ice_fltr_add_eth_list);
}

/**
 * ice_fltr_remove_eth - remove ethertype filter
 * @vsi: pointer to VSI struct
 * @ethertype: ethertype of filter
 * @flag: direction of filter
 * @action: action to remove
 */
int ice_fltr_remove_eth(struct ice_vsi *vsi, u16 ethertype, u16 flag,
			enum ice_sw_fwd_act_type action)
{
	return ice_fltr_prepare_eth(vsi, ethertype, flag, action,
				    ice_fltr_remove_eth_list);
}

/**
 * ice_fltr_update_rule_flags - update lan_en/lb_en flags
 * @hw: pointer to hw
 * @rule_id: id of rule being updated
 * @recipe_id: recipe id of rule
 * @act: current action field
 * @type: Rx or Tx
 * @src: source VSI
 * @new_flags: combinations of lb_en and lan_en
 */
static int
ice_fltr_update_rule_flags(struct ice_hw *hw, u16 rule_id, u16 recipe_id,
			   u32 act, u16 type, u16 src, u32 new_flags)
{
	struct ice_aqc_sw_rules_elem *s_rule;
	int err;
	u32 flags_mask;

	s_rule = kzalloc(ICE_SW_RULE_RX_TX_NO_HDR_SIZE, GFP_KERNEL);
	if (!s_rule)
		return -ENOMEM;

	flags_mask = ICE_SINGLE_ACT_LB_ENABLE | ICE_SINGLE_ACT_LAN_ENABLE;
	act &= ~flags_mask;
	act |= (flags_mask & new_flags);

	s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(recipe_id);
	s_rule->pdata.lkup_tx_rx.index = cpu_to_le16(rule_id);
	s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);

	if (type & ICE_FLTR_RX) {
		s_rule->pdata.lkup_tx_rx.src =
			cpu_to_le16(hw->port_info->lport);
		s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX);

	} else {
		s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(src);
		s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
	}

	err = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
			      ice_aqc_opc_update_sw_rules, NULL);

	kfree(s_rule);
	return err;
}

/**
 * ice_fltr_build_action - build action for rule
 * @vsi_id: id of VSI which is use to build action
 */
static u32 ice_fltr_build_action(u16 vsi_id)
{
	return ((vsi_id << ICE_SINGLE_ACT_VSI_ID_S) & ICE_SINGLE_ACT_VSI_ID_M) |
		ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_VALID_BIT;
}

/**
 * ice_fltr_update_flags_dflt_rule - update flags on default rule
 * @vsi: pointer to VSI
 * @rule_id: id of rule
 * @direction: Tx or Rx
 * @new_flags: flags to update
 *
 * Function updates flags on default rule with ICE_SW_LKUP_DFLT.
 *
 * Flags should be a combination of ICE_SINGLE_ACT_LB_ENABLE and
 * ICE_SINGLE_ACT_LAN_ENABLE.
 */
int
ice_fltr_update_flags_dflt_rule(struct ice_vsi *vsi, u16 rule_id, u8 direction,
				u32 new_flags)
{
	u32 action = ice_fltr_build_action(vsi->vsi_num);
	struct ice_hw *hw = &vsi->back->hw;

	return ice_fltr_update_rule_flags(hw, rule_id, ICE_SW_LKUP_DFLT, action,
					  direction, vsi->vsi_num, new_flags);
}