1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3 
4 #include "ice_switch.h"
5 
6 #define ICE_ETH_DA_OFFSET		0
7 #define ICE_ETH_ETHTYPE_OFFSET		12
8 #define ICE_ETH_VLAN_TCI_OFFSET		14
9 #define ICE_MAX_VLAN_ID			0xFFF
10 
11 /* Dummy ethernet header needed in the ice_aqc_sw_rules_elem
12  * struct to configure any switch filter rules.
13  * {DA (6 bytes), SA(6 bytes),
14  * Ether type (2 bytes for header without VLAN tag) OR
15  * VLAN tag (4 bytes for header with VLAN tag) }
16  *
17  * Word on Hardcoded values
18  * byte 0 = 0x2: to identify it as locally administered DA MAC
19  * byte 6 = 0x2: to identify it as locally administered SA MAC
20  * byte 12 = 0x81 & byte 13 = 0x00:
21  *	In case of VLAN filter first two bytes defines ether type (0x8100)
22  *	and remaining two bytes are placeholder for programming a given VLAN ID
23  *	In case of Ether type filter it is treated as header without VLAN tag
24  *	and byte 12 and 13 is used to program a given Ether type instead
25  */
26 #define DUMMY_ETH_HDR_LEN		16
27 static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
28 							0x2, 0, 0, 0, 0, 0,
29 							0x81, 0, 0, 0};
30 
31 #define ICE_SW_RULE_RX_TX_ETH_HDR_SIZE \
32 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.lkup_tx_rx.hdr) + \
33 	 (DUMMY_ETH_HDR_LEN * \
34 	  sizeof(((struct ice_sw_rule_lkup_rx_tx *)0)->hdr[0])))
35 #define ICE_SW_RULE_RX_TX_NO_HDR_SIZE \
36 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.lkup_tx_rx.hdr))
37 #define ICE_SW_RULE_LG_ACT_SIZE(n) \
38 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.lg_act.act) + \
39 	 ((n) * sizeof(((struct ice_sw_rule_lg_act *)0)->act[0])))
40 #define ICE_SW_RULE_VSI_LIST_SIZE(n) \
41 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.vsi_list.vsi) + \
42 	 ((n) * sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi[0])))
43 
44 /**
45  * ice_init_def_sw_recp - initialize the recipe book keeping tables
46  * @hw: pointer to the HW struct
47  *
48  * Allocate memory for the entire recipe table and initialize the structures/
49  * entries corresponding to basic recipes.
50  */
51 enum ice_status ice_init_def_sw_recp(struct ice_hw *hw)
52 {
53 	struct ice_sw_recipe *recps;
54 	u8 i;
55 
56 	recps = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_NUM_RECIPES,
57 			     sizeof(*recps), GFP_KERNEL);
58 	if (!recps)
59 		return ICE_ERR_NO_MEMORY;
60 
61 	for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
62 		recps[i].root_rid = i;
63 		INIT_LIST_HEAD(&recps[i].filt_rules);
64 		INIT_LIST_HEAD(&recps[i].filt_replay_rules);
65 		mutex_init(&recps[i].filt_rule_lock);
66 	}
67 
68 	hw->switch_info->recp_list = recps;
69 
70 	return 0;
71 }
72 
73 /**
74  * ice_aq_get_sw_cfg - get switch configuration
75  * @hw: pointer to the hardware structure
76  * @buf: pointer to the result buffer
77  * @buf_size: length of the buffer available for response
78  * @req_desc: pointer to requested descriptor
79  * @num_elems: pointer to number of elements
80  * @cd: pointer to command details structure or NULL
81  *
82  * Get switch configuration (0x0200) to be placed in buf.
83  * This admin command returns information such as initial VSI/port number
84  * and switch ID it belongs to.
85  *
86  * NOTE: *req_desc is both an input/output parameter.
87  * The caller of this function first calls this function with *request_desc set
88  * to 0. If the response from f/w has *req_desc set to 0, all the switch
89  * configuration information has been returned; if non-zero (meaning not all
90  * the information was returned), the caller should call this function again
91  * with *req_desc set to the previous value returned by f/w to get the
92  * next block of switch configuration information.
93  *
94  * *num_elems is output only parameter. This reflects the number of elements
95  * in response buffer. The caller of this function to use *num_elems while
96  * parsing the response buffer.
97  */
98 static enum ice_status
99 ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp_elem *buf,
100 		  u16 buf_size, u16 *req_desc, u16 *num_elems,
101 		  struct ice_sq_cd *cd)
102 {
103 	struct ice_aqc_get_sw_cfg *cmd;
104 	struct ice_aq_desc desc;
105 	enum ice_status status;
106 
107 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
108 	cmd = &desc.params.get_sw_conf;
109 	cmd->element = cpu_to_le16(*req_desc);
110 
111 	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
112 	if (!status) {
113 		*req_desc = le16_to_cpu(cmd->element);
114 		*num_elems = le16_to_cpu(cmd->num_elems);
115 	}
116 
117 	return status;
118 }
119 
120 /**
121  * ice_aq_add_vsi
122  * @hw: pointer to the HW struct
123  * @vsi_ctx: pointer to a VSI context struct
124  * @cd: pointer to command details structure or NULL
125  *
126  * Add a VSI context to the hardware (0x0210)
127  */
128 static enum ice_status
129 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
130 	       struct ice_sq_cd *cd)
131 {
132 	struct ice_aqc_add_update_free_vsi_resp *res;
133 	struct ice_aqc_add_get_update_free_vsi *cmd;
134 	struct ice_aq_desc desc;
135 	enum ice_status status;
136 
137 	cmd = &desc.params.vsi_cmd;
138 	res = &desc.params.add_update_free_vsi_res;
139 
140 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
141 
142 	if (!vsi_ctx->alloc_from_pool)
143 		cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num |
144 					   ICE_AQ_VSI_IS_VALID);
145 	cmd->vf_id = vsi_ctx->vf_num;
146 
147 	cmd->vsi_flags = cpu_to_le16(vsi_ctx->flags);
148 
149 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
150 
151 	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
152 				 sizeof(vsi_ctx->info), cd);
153 
154 	if (!status) {
155 		vsi_ctx->vsi_num = le16_to_cpu(res->vsi_num) & ICE_AQ_VSI_NUM_M;
156 		vsi_ctx->vsis_allocd = le16_to_cpu(res->vsi_used);
157 		vsi_ctx->vsis_unallocated = le16_to_cpu(res->vsi_free);
158 	}
159 
160 	return status;
161 }
162 
163 /**
164  * ice_aq_free_vsi
165  * @hw: pointer to the HW struct
166  * @vsi_ctx: pointer to a VSI context struct
167  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
168  * @cd: pointer to command details structure or NULL
169  *
170  * Free VSI context info from hardware (0x0213)
171  */
172 static enum ice_status
173 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
174 		bool keep_vsi_alloc, struct ice_sq_cd *cd)
175 {
176 	struct ice_aqc_add_update_free_vsi_resp *resp;
177 	struct ice_aqc_add_get_update_free_vsi *cmd;
178 	struct ice_aq_desc desc;
179 	enum ice_status status;
180 
181 	cmd = &desc.params.vsi_cmd;
182 	resp = &desc.params.add_update_free_vsi_res;
183 
184 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
185 
186 	cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
187 	if (keep_vsi_alloc)
188 		cmd->cmd_flags = cpu_to_le16(ICE_AQ_VSI_KEEP_ALLOC);
189 
190 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
191 	if (!status) {
192 		vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
193 		vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
194 	}
195 
196 	return status;
197 }
198 
199 /**
200  * ice_aq_update_vsi
201  * @hw: pointer to the HW struct
202  * @vsi_ctx: pointer to a VSI context struct
203  * @cd: pointer to command details structure or NULL
204  *
205  * Update VSI context in the hardware (0x0211)
206  */
207 static enum ice_status
208 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
209 		  struct ice_sq_cd *cd)
210 {
211 	struct ice_aqc_add_update_free_vsi_resp *resp;
212 	struct ice_aqc_add_get_update_free_vsi *cmd;
213 	struct ice_aq_desc desc;
214 	enum ice_status status;
215 
216 	cmd = &desc.params.vsi_cmd;
217 	resp = &desc.params.add_update_free_vsi_res;
218 
219 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
220 
221 	cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
222 
223 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
224 
225 	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
226 				 sizeof(vsi_ctx->info), cd);
227 
228 	if (!status) {
229 		vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
230 		vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
231 	}
232 
233 	return status;
234 }
235 
236 /**
237  * ice_is_vsi_valid - check whether the VSI is valid or not
238  * @hw: pointer to the HW struct
239  * @vsi_handle: VSI handle
240  *
241  * check whether the VSI is valid or not
242  */
243 bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
244 {
245 	return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
246 }
247 
248 /**
249  * ice_get_hw_vsi_num - return the HW VSI number
250  * @hw: pointer to the HW struct
251  * @vsi_handle: VSI handle
252  *
253  * return the HW VSI number
254  * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
255  */
256 u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
257 {
258 	return hw->vsi_ctx[vsi_handle]->vsi_num;
259 }
260 
261 /**
262  * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
263  * @hw: pointer to the HW struct
264  * @vsi_handle: VSI handle
265  *
266  * return the VSI context entry for a given VSI handle
267  */
268 struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
269 {
270 	return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
271 }
272 
273 /**
274  * ice_save_vsi_ctx - save the VSI context for a given VSI handle
275  * @hw: pointer to the HW struct
276  * @vsi_handle: VSI handle
277  * @vsi: VSI context pointer
278  *
279  * save the VSI context entry for a given VSI handle
280  */
281 static void
282 ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi)
283 {
284 	hw->vsi_ctx[vsi_handle] = vsi;
285 }
286 
287 /**
288  * ice_clear_vsi_q_ctx - clear VSI queue contexts for all TCs
289  * @hw: pointer to the HW struct
290  * @vsi_handle: VSI handle
291  */
292 static void ice_clear_vsi_q_ctx(struct ice_hw *hw, u16 vsi_handle)
293 {
294 	struct ice_vsi_ctx *vsi;
295 	u8 i;
296 
297 	vsi = ice_get_vsi_ctx(hw, vsi_handle);
298 	if (!vsi)
299 		return;
300 	ice_for_each_traffic_class(i) {
301 		if (vsi->lan_q_ctx[i]) {
302 			devm_kfree(ice_hw_to_dev(hw), vsi->lan_q_ctx[i]);
303 			vsi->lan_q_ctx[i] = NULL;
304 		}
305 	}
306 }
307 
308 /**
309  * ice_clear_vsi_ctx - clear the VSI context entry
310  * @hw: pointer to the HW struct
311  * @vsi_handle: VSI handle
312  *
313  * clear the VSI context entry
314  */
315 static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
316 {
317 	struct ice_vsi_ctx *vsi;
318 
319 	vsi = ice_get_vsi_ctx(hw, vsi_handle);
320 	if (vsi) {
321 		ice_clear_vsi_q_ctx(hw, vsi_handle);
322 		devm_kfree(ice_hw_to_dev(hw), vsi);
323 		hw->vsi_ctx[vsi_handle] = NULL;
324 	}
325 }
326 
327 /**
328  * ice_clear_all_vsi_ctx - clear all the VSI context entries
329  * @hw: pointer to the HW struct
330  */
331 void ice_clear_all_vsi_ctx(struct ice_hw *hw)
332 {
333 	u16 i;
334 
335 	for (i = 0; i < ICE_MAX_VSI; i++)
336 		ice_clear_vsi_ctx(hw, i);
337 }
338 
339 /**
340  * ice_add_vsi - add VSI context to the hardware and VSI handle list
341  * @hw: pointer to the HW struct
342  * @vsi_handle: unique VSI handle provided by drivers
343  * @vsi_ctx: pointer to a VSI context struct
344  * @cd: pointer to command details structure or NULL
345  *
346  * Add a VSI context to the hardware also add it into the VSI handle list.
347  * If this function gets called after reset for existing VSIs then update
348  * with the new HW VSI number in the corresponding VSI handle list entry.
349  */
350 enum ice_status
351 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
352 	    struct ice_sq_cd *cd)
353 {
354 	struct ice_vsi_ctx *tmp_vsi_ctx;
355 	enum ice_status status;
356 
357 	if (vsi_handle >= ICE_MAX_VSI)
358 		return ICE_ERR_PARAM;
359 	status = ice_aq_add_vsi(hw, vsi_ctx, cd);
360 	if (status)
361 		return status;
362 	tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
363 	if (!tmp_vsi_ctx) {
364 		/* Create a new VSI context */
365 		tmp_vsi_ctx = devm_kzalloc(ice_hw_to_dev(hw),
366 					   sizeof(*tmp_vsi_ctx), GFP_KERNEL);
367 		if (!tmp_vsi_ctx) {
368 			ice_aq_free_vsi(hw, vsi_ctx, false, cd);
369 			return ICE_ERR_NO_MEMORY;
370 		}
371 		*tmp_vsi_ctx = *vsi_ctx;
372 		ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
373 	} else {
374 		/* update with new HW VSI num */
375 		tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
376 	}
377 
378 	return 0;
379 }
380 
381 /**
382  * ice_free_vsi- free VSI context from hardware and VSI handle list
383  * @hw: pointer to the HW struct
384  * @vsi_handle: unique VSI handle
385  * @vsi_ctx: pointer to a VSI context struct
386  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
387  * @cd: pointer to command details structure or NULL
388  *
389  * Free VSI context info from hardware as well as from VSI handle list
390  */
391 enum ice_status
392 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
393 	     bool keep_vsi_alloc, struct ice_sq_cd *cd)
394 {
395 	enum ice_status status;
396 
397 	if (!ice_is_vsi_valid(hw, vsi_handle))
398 		return ICE_ERR_PARAM;
399 	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
400 	status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
401 	if (!status)
402 		ice_clear_vsi_ctx(hw, vsi_handle);
403 	return status;
404 }
405 
406 /**
407  * ice_update_vsi
408  * @hw: pointer to the HW struct
409  * @vsi_handle: unique VSI handle
410  * @vsi_ctx: pointer to a VSI context struct
411  * @cd: pointer to command details structure or NULL
412  *
413  * Update VSI context in the hardware
414  */
415 enum ice_status
416 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
417 	       struct ice_sq_cd *cd)
418 {
419 	if (!ice_is_vsi_valid(hw, vsi_handle))
420 		return ICE_ERR_PARAM;
421 	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
422 	return ice_aq_update_vsi(hw, vsi_ctx, cd);
423 }
424 
425 /**
426  * ice_aq_alloc_free_vsi_list
427  * @hw: pointer to the HW struct
428  * @vsi_list_id: VSI list ID returned or used for lookup
429  * @lkup_type: switch rule filter lookup type
430  * @opc: switch rules population command type - pass in the command opcode
431  *
432  * allocates or free a VSI list resource
433  */
434 static enum ice_status
435 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
436 			   enum ice_sw_lkup_type lkup_type,
437 			   enum ice_adminq_opc opc)
438 {
439 	struct ice_aqc_alloc_free_res_elem *sw_buf;
440 	struct ice_aqc_res_elem *vsi_ele;
441 	enum ice_status status;
442 	u16 buf_len;
443 
444 	buf_len = struct_size(sw_buf, elem, 1);
445 	sw_buf = devm_kzalloc(ice_hw_to_dev(hw), buf_len, GFP_KERNEL);
446 	if (!sw_buf)
447 		return ICE_ERR_NO_MEMORY;
448 	sw_buf->num_elems = cpu_to_le16(1);
449 
450 	if (lkup_type == ICE_SW_LKUP_MAC ||
451 	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
452 	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
453 	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
454 	    lkup_type == ICE_SW_LKUP_PROMISC ||
455 	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN) {
456 		sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
457 	} else if (lkup_type == ICE_SW_LKUP_VLAN) {
458 		sw_buf->res_type =
459 			cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
460 	} else {
461 		status = ICE_ERR_PARAM;
462 		goto ice_aq_alloc_free_vsi_list_exit;
463 	}
464 
465 	if (opc == ice_aqc_opc_free_res)
466 		sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
467 
468 	status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
469 	if (status)
470 		goto ice_aq_alloc_free_vsi_list_exit;
471 
472 	if (opc == ice_aqc_opc_alloc_res) {
473 		vsi_ele = &sw_buf->elem[0];
474 		*vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp);
475 	}
476 
477 ice_aq_alloc_free_vsi_list_exit:
478 	devm_kfree(ice_hw_to_dev(hw), sw_buf);
479 	return status;
480 }
481 
482 /**
483  * ice_aq_sw_rules - add/update/remove switch rules
484  * @hw: pointer to the HW struct
485  * @rule_list: pointer to switch rule population list
486  * @rule_list_sz: total size of the rule list in bytes
487  * @num_rules: number of switch rules in the rule_list
488  * @opc: switch rules population command type - pass in the command opcode
489  * @cd: pointer to command details structure or NULL
490  *
491  * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
492  */
493 static enum ice_status
494 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
495 		u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
496 {
497 	struct ice_aq_desc desc;
498 
499 	if (opc != ice_aqc_opc_add_sw_rules &&
500 	    opc != ice_aqc_opc_update_sw_rules &&
501 	    opc != ice_aqc_opc_remove_sw_rules)
502 		return ICE_ERR_PARAM;
503 
504 	ice_fill_dflt_direct_cmd_desc(&desc, opc);
505 
506 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
507 	desc.params.sw_rules.num_rules_fltr_entry_index =
508 		cpu_to_le16(num_rules);
509 	return ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
510 }
511 
512 /* ice_init_port_info - Initialize port_info with switch configuration data
513  * @pi: pointer to port_info
514  * @vsi_port_num: VSI number or port number
515  * @type: Type of switch element (port or VSI)
516  * @swid: switch ID of the switch the element is attached to
517  * @pf_vf_num: PF or VF number
518  * @is_vf: true if the element is a VF, false otherwise
519  */
520 static void
521 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
522 		   u16 swid, u16 pf_vf_num, bool is_vf)
523 {
524 	switch (type) {
525 	case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
526 		pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
527 		pi->sw_id = swid;
528 		pi->pf_vf_num = pf_vf_num;
529 		pi->is_vf = is_vf;
530 		pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
531 		pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
532 		break;
533 	default:
534 		ice_debug(pi->hw, ICE_DBG_SW,
535 			  "incorrect VSI/port type received\n");
536 		break;
537 	}
538 }
539 
540 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
541  * @hw: pointer to the hardware structure
542  */
543 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
544 {
545 	struct ice_aqc_get_sw_cfg_resp_elem *rbuf;
546 	enum ice_status status;
547 	u16 req_desc = 0;
548 	u16 num_elems;
549 	u16 i;
550 
551 	rbuf = devm_kzalloc(ice_hw_to_dev(hw), ICE_SW_CFG_MAX_BUF_LEN,
552 			    GFP_KERNEL);
553 
554 	if (!rbuf)
555 		return ICE_ERR_NO_MEMORY;
556 
557 	/* Multiple calls to ice_aq_get_sw_cfg may be required
558 	 * to get all the switch configuration information. The need
559 	 * for additional calls is indicated by ice_aq_get_sw_cfg
560 	 * writing a non-zero value in req_desc
561 	 */
562 	do {
563 		struct ice_aqc_get_sw_cfg_resp_elem *ele;
564 
565 		status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
566 					   &req_desc, &num_elems, NULL);
567 
568 		if (status)
569 			break;
570 
571 		for (i = 0, ele = rbuf; i < num_elems; i++, ele++) {
572 			u16 pf_vf_num, swid, vsi_port_num;
573 			bool is_vf = false;
574 			u8 res_type;
575 
576 			vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
577 				ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
578 
579 			pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
580 				ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
581 
582 			swid = le16_to_cpu(ele->swid);
583 
584 			if (le16_to_cpu(ele->pf_vf_num) &
585 			    ICE_AQC_GET_SW_CONF_RESP_IS_VF)
586 				is_vf = true;
587 
588 			res_type = (u8)(le16_to_cpu(ele->vsi_port_num) >>
589 					ICE_AQC_GET_SW_CONF_RESP_TYPE_S);
590 
591 			if (res_type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
592 				/* FW VSI is not needed. Just continue. */
593 				continue;
594 			}
595 
596 			ice_init_port_info(hw->port_info, vsi_port_num,
597 					   res_type, swid, pf_vf_num, is_vf);
598 		}
599 	} while (req_desc && !status);
600 
601 	devm_kfree(ice_hw_to_dev(hw), (void *)rbuf);
602 	return status;
603 }
604 
605 /**
606  * ice_fill_sw_info - Helper function to populate lb_en and lan_en
607  * @hw: pointer to the hardware structure
608  * @fi: filter info structure to fill/update
609  *
610  * This helper function populates the lb_en and lan_en elements of the provided
611  * ice_fltr_info struct using the switch's type and characteristics of the
612  * switch rule being configured.
613  */
614 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
615 {
616 	fi->lb_en = false;
617 	fi->lan_en = false;
618 	if ((fi->flag & ICE_FLTR_TX) &&
619 	    (fi->fltr_act == ICE_FWD_TO_VSI ||
620 	     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
621 	     fi->fltr_act == ICE_FWD_TO_Q ||
622 	     fi->fltr_act == ICE_FWD_TO_QGRP)) {
623 		/* Setting LB for prune actions will result in replicated
624 		 * packets to the internal switch that will be dropped.
625 		 */
626 		if (fi->lkup_type != ICE_SW_LKUP_VLAN)
627 			fi->lb_en = true;
628 
629 		/* Set lan_en to TRUE if
630 		 * 1. The switch is a VEB AND
631 		 * 2
632 		 * 2.1 The lookup is a directional lookup like ethertype,
633 		 * promiscuous, ethertype-MAC, promiscuous-VLAN
634 		 * and default-port OR
635 		 * 2.2 The lookup is VLAN, OR
636 		 * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
637 		 * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
638 		 *
639 		 * OR
640 		 *
641 		 * The switch is a VEPA.
642 		 *
643 		 * In all other cases, the LAN enable has to be set to false.
644 		 */
645 		if (hw->evb_veb) {
646 			if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
647 			    fi->lkup_type == ICE_SW_LKUP_PROMISC ||
648 			    fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
649 			    fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
650 			    fi->lkup_type == ICE_SW_LKUP_DFLT ||
651 			    fi->lkup_type == ICE_SW_LKUP_VLAN ||
652 			    (fi->lkup_type == ICE_SW_LKUP_MAC &&
653 			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)) ||
654 			    (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
655 			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)))
656 				fi->lan_en = true;
657 		} else {
658 			fi->lan_en = true;
659 		}
660 	}
661 }
662 
663 /**
664  * ice_fill_sw_rule - Helper function to fill switch rule structure
665  * @hw: pointer to the hardware structure
666  * @f_info: entry containing packet forwarding information
667  * @s_rule: switch rule structure to be filled in based on mac_entry
668  * @opc: switch rules population command type - pass in the command opcode
669  */
670 static void
671 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
672 		 struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
673 {
674 	u16 vlan_id = ICE_MAX_VLAN_ID + 1;
675 	void *daddr = NULL;
676 	u16 eth_hdr_sz;
677 	u8 *eth_hdr;
678 	u32 act = 0;
679 	__be16 *off;
680 	u8 q_rgn;
681 
682 	if (opc == ice_aqc_opc_remove_sw_rules) {
683 		s_rule->pdata.lkup_tx_rx.act = 0;
684 		s_rule->pdata.lkup_tx_rx.index =
685 			cpu_to_le16(f_info->fltr_rule_id);
686 		s_rule->pdata.lkup_tx_rx.hdr_len = 0;
687 		return;
688 	}
689 
690 	eth_hdr_sz = sizeof(dummy_eth_header);
691 	eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
692 
693 	/* initialize the ether header with a dummy header */
694 	memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz);
695 	ice_fill_sw_info(hw, f_info);
696 
697 	switch (f_info->fltr_act) {
698 	case ICE_FWD_TO_VSI:
699 		act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
700 			ICE_SINGLE_ACT_VSI_ID_M;
701 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
702 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
703 				ICE_SINGLE_ACT_VALID_BIT;
704 		break;
705 	case ICE_FWD_TO_VSI_LIST:
706 		act |= ICE_SINGLE_ACT_VSI_LIST;
707 		act |= (f_info->fwd_id.vsi_list_id <<
708 			ICE_SINGLE_ACT_VSI_LIST_ID_S) &
709 			ICE_SINGLE_ACT_VSI_LIST_ID_M;
710 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
711 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
712 				ICE_SINGLE_ACT_VALID_BIT;
713 		break;
714 	case ICE_FWD_TO_Q:
715 		act |= ICE_SINGLE_ACT_TO_Q;
716 		act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
717 			ICE_SINGLE_ACT_Q_INDEX_M;
718 		break;
719 	case ICE_DROP_PACKET:
720 		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
721 			ICE_SINGLE_ACT_VALID_BIT;
722 		break;
723 	case ICE_FWD_TO_QGRP:
724 		q_rgn = f_info->qgrp_size > 0 ?
725 			(u8)ilog2(f_info->qgrp_size) : 0;
726 		act |= ICE_SINGLE_ACT_TO_Q;
727 		act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
728 			ICE_SINGLE_ACT_Q_INDEX_M;
729 		act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
730 			ICE_SINGLE_ACT_Q_REGION_M;
731 		break;
732 	default:
733 		return;
734 	}
735 
736 	if (f_info->lb_en)
737 		act |= ICE_SINGLE_ACT_LB_ENABLE;
738 	if (f_info->lan_en)
739 		act |= ICE_SINGLE_ACT_LAN_ENABLE;
740 
741 	switch (f_info->lkup_type) {
742 	case ICE_SW_LKUP_MAC:
743 		daddr = f_info->l_data.mac.mac_addr;
744 		break;
745 	case ICE_SW_LKUP_VLAN:
746 		vlan_id = f_info->l_data.vlan.vlan_id;
747 		if (f_info->fltr_act == ICE_FWD_TO_VSI ||
748 		    f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
749 			act |= ICE_SINGLE_ACT_PRUNE;
750 			act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
751 		}
752 		break;
753 	case ICE_SW_LKUP_ETHERTYPE_MAC:
754 		daddr = f_info->l_data.ethertype_mac.mac_addr;
755 		fallthrough;
756 	case ICE_SW_LKUP_ETHERTYPE:
757 		off = (__force __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
758 		*off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
759 		break;
760 	case ICE_SW_LKUP_MAC_VLAN:
761 		daddr = f_info->l_data.mac_vlan.mac_addr;
762 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
763 		break;
764 	case ICE_SW_LKUP_PROMISC_VLAN:
765 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
766 		fallthrough;
767 	case ICE_SW_LKUP_PROMISC:
768 		daddr = f_info->l_data.mac_vlan.mac_addr;
769 		break;
770 	default:
771 		break;
772 	}
773 
774 	s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
775 		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
776 		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
777 
778 	/* Recipe set depending on lookup type */
779 	s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(f_info->lkup_type);
780 	s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(f_info->src);
781 	s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);
782 
783 	if (daddr)
784 		ether_addr_copy(eth_hdr + ICE_ETH_DA_OFFSET, daddr);
785 
786 	if (!(vlan_id > ICE_MAX_VLAN_ID)) {
787 		off = (__force __be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
788 		*off = cpu_to_be16(vlan_id);
789 	}
790 
791 	/* Create the switch rule with the final dummy Ethernet header */
792 	if (opc != ice_aqc_opc_update_sw_rules)
793 		s_rule->pdata.lkup_tx_rx.hdr_len = cpu_to_le16(eth_hdr_sz);
794 }
795 
796 /**
797  * ice_add_marker_act
798  * @hw: pointer to the hardware structure
799  * @m_ent: the management entry for which sw marker needs to be added
800  * @sw_marker: sw marker to tag the Rx descriptor with
801  * @l_id: large action resource ID
802  *
803  * Create a large action to hold software marker and update the switch rule
804  * entry pointed by m_ent with newly created large action
805  */
806 static enum ice_status
807 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
808 		   u16 sw_marker, u16 l_id)
809 {
810 	struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
811 	/* For software marker we need 3 large actions
812 	 * 1. FWD action: FWD TO VSI or VSI LIST
813 	 * 2. GENERIC VALUE action to hold the profile ID
814 	 * 3. GENERIC VALUE action to hold the software marker ID
815 	 */
816 	const u16 num_lg_acts = 3;
817 	enum ice_status status;
818 	u16 lg_act_size;
819 	u16 rules_size;
820 	u32 act;
821 	u16 id;
822 
823 	if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
824 		return ICE_ERR_PARAM;
825 
826 	/* Create two back-to-back switch rules and submit them to the HW using
827 	 * one memory buffer:
828 	 *    1. Large Action
829 	 *    2. Look up Tx Rx
830 	 */
831 	lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
832 	rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
833 	lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
834 	if (!lg_act)
835 		return ICE_ERR_NO_MEMORY;
836 
837 	rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
838 
839 	/* Fill in the first switch rule i.e. large action */
840 	lg_act->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
841 	lg_act->pdata.lg_act.index = cpu_to_le16(l_id);
842 	lg_act->pdata.lg_act.size = cpu_to_le16(num_lg_acts);
843 
844 	/* First action VSI forwarding or VSI list forwarding depending on how
845 	 * many VSIs
846 	 */
847 	id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
848 		m_ent->fltr_info.fwd_id.hw_vsi_id;
849 
850 	act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
851 	act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) & ICE_LG_ACT_VSI_LIST_ID_M;
852 	if (m_ent->vsi_count > 1)
853 		act |= ICE_LG_ACT_VSI_LIST;
854 	lg_act->pdata.lg_act.act[0] = cpu_to_le32(act);
855 
856 	/* Second action descriptor type */
857 	act = ICE_LG_ACT_GENERIC;
858 
859 	act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
860 	lg_act->pdata.lg_act.act[1] = cpu_to_le32(act);
861 
862 	act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
863 	       ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
864 
865 	/* Third action Marker value */
866 	act |= ICE_LG_ACT_GENERIC;
867 	act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
868 		ICE_LG_ACT_GENERIC_VALUE_M;
869 
870 	lg_act->pdata.lg_act.act[2] = cpu_to_le32(act);
871 
872 	/* call the fill switch rule to fill the lookup Tx Rx structure */
873 	ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
874 			 ice_aqc_opc_update_sw_rules);
875 
876 	/* Update the action to point to the large action ID */
877 	rx_tx->pdata.lkup_tx_rx.act =
878 		cpu_to_le32(ICE_SINGLE_ACT_PTR |
879 			    ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
880 			     ICE_SINGLE_ACT_PTR_VAL_M));
881 
882 	/* Use the filter rule ID of the previously created rule with single
883 	 * act. Once the update happens, hardware will treat this as large
884 	 * action
885 	 */
886 	rx_tx->pdata.lkup_tx_rx.index =
887 		cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
888 
889 	status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
890 				 ice_aqc_opc_update_sw_rules, NULL);
891 	if (!status) {
892 		m_ent->lg_act_idx = l_id;
893 		m_ent->sw_marker_id = sw_marker;
894 	}
895 
896 	devm_kfree(ice_hw_to_dev(hw), lg_act);
897 	return status;
898 }
899 
900 /**
901  * ice_create_vsi_list_map
902  * @hw: pointer to the hardware structure
903  * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
904  * @num_vsi: number of VSI handles in the array
905  * @vsi_list_id: VSI list ID generated as part of allocate resource
906  *
907  * Helper function to create a new entry of VSI list ID to VSI mapping
908  * using the given VSI list ID
909  */
910 static struct ice_vsi_list_map_info *
911 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
912 			u16 vsi_list_id)
913 {
914 	struct ice_switch_info *sw = hw->switch_info;
915 	struct ice_vsi_list_map_info *v_map;
916 	int i;
917 
918 	v_map = devm_kcalloc(ice_hw_to_dev(hw), 1, sizeof(*v_map), GFP_KERNEL);
919 	if (!v_map)
920 		return NULL;
921 
922 	v_map->vsi_list_id = vsi_list_id;
923 	v_map->ref_cnt = 1;
924 	for (i = 0; i < num_vsi; i++)
925 		set_bit(vsi_handle_arr[i], v_map->vsi_map);
926 
927 	list_add(&v_map->list_entry, &sw->vsi_list_map_head);
928 	return v_map;
929 }
930 
931 /**
932  * ice_update_vsi_list_rule
933  * @hw: pointer to the hardware structure
934  * @vsi_handle_arr: array of VSI handles to form a VSI list
935  * @num_vsi: number of VSI handles in the array
936  * @vsi_list_id: VSI list ID generated as part of allocate resource
937  * @remove: Boolean value to indicate if this is a remove action
938  * @opc: switch rules population command type - pass in the command opcode
939  * @lkup_type: lookup type of the filter
940  *
941  * Call AQ command to add a new switch rule or update existing switch rule
942  * using the given VSI list ID
943  */
944 static enum ice_status
945 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
946 			 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
947 			 enum ice_sw_lkup_type lkup_type)
948 {
949 	struct ice_aqc_sw_rules_elem *s_rule;
950 	enum ice_status status;
951 	u16 s_rule_size;
952 	u16 rule_type;
953 	int i;
954 
955 	if (!num_vsi)
956 		return ICE_ERR_PARAM;
957 
958 	if (lkup_type == ICE_SW_LKUP_MAC ||
959 	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
960 	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
961 	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
962 	    lkup_type == ICE_SW_LKUP_PROMISC ||
963 	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
964 		rule_type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
965 			ICE_AQC_SW_RULES_T_VSI_LIST_SET;
966 	else if (lkup_type == ICE_SW_LKUP_VLAN)
967 		rule_type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
968 			ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
969 	else
970 		return ICE_ERR_PARAM;
971 
972 	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
973 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
974 	if (!s_rule)
975 		return ICE_ERR_NO_MEMORY;
976 	for (i = 0; i < num_vsi; i++) {
977 		if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
978 			status = ICE_ERR_PARAM;
979 			goto exit;
980 		}
981 		/* AQ call requires hw_vsi_id(s) */
982 		s_rule->pdata.vsi_list.vsi[i] =
983 			cpu_to_le16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
984 	}
985 
986 	s_rule->type = cpu_to_le16(rule_type);
987 	s_rule->pdata.vsi_list.number_vsi = cpu_to_le16(num_vsi);
988 	s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
989 
990 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
991 
992 exit:
993 	devm_kfree(ice_hw_to_dev(hw), s_rule);
994 	return status;
995 }
996 
997 /**
998  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
999  * @hw: pointer to the HW struct
1000  * @vsi_handle_arr: array of VSI handles to form a VSI list
1001  * @num_vsi: number of VSI handles in the array
1002  * @vsi_list_id: stores the ID of the VSI list to be created
1003  * @lkup_type: switch rule filter's lookup type
1004  */
1005 static enum ice_status
1006 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1007 			 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1008 {
1009 	enum ice_status status;
1010 
1011 	status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1012 					    ice_aqc_opc_alloc_res);
1013 	if (status)
1014 		return status;
1015 
1016 	/* Update the newly created VSI list to include the specified VSIs */
1017 	return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1018 					*vsi_list_id, false,
1019 					ice_aqc_opc_add_sw_rules, lkup_type);
1020 }
1021 
1022 /**
1023  * ice_create_pkt_fwd_rule
1024  * @hw: pointer to the hardware structure
1025  * @f_entry: entry containing packet forwarding information
1026  *
1027  * Create switch rule with given filter information and add an entry
1028  * to the corresponding filter management list to track this switch rule
1029  * and VSI mapping
1030  */
1031 static enum ice_status
1032 ice_create_pkt_fwd_rule(struct ice_hw *hw,
1033 			struct ice_fltr_list_entry *f_entry)
1034 {
1035 	struct ice_fltr_mgmt_list_entry *fm_entry;
1036 	struct ice_aqc_sw_rules_elem *s_rule;
1037 	enum ice_sw_lkup_type l_type;
1038 	struct ice_sw_recipe *recp;
1039 	enum ice_status status;
1040 
1041 	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1042 			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1043 	if (!s_rule)
1044 		return ICE_ERR_NO_MEMORY;
1045 	fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
1046 				GFP_KERNEL);
1047 	if (!fm_entry) {
1048 		status = ICE_ERR_NO_MEMORY;
1049 		goto ice_create_pkt_fwd_rule_exit;
1050 	}
1051 
1052 	fm_entry->fltr_info = f_entry->fltr_info;
1053 
1054 	/* Initialize all the fields for the management entry */
1055 	fm_entry->vsi_count = 1;
1056 	fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1057 	fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1058 	fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1059 
1060 	ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1061 			 ice_aqc_opc_add_sw_rules);
1062 
1063 	status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1064 				 ice_aqc_opc_add_sw_rules, NULL);
1065 	if (status) {
1066 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
1067 		goto ice_create_pkt_fwd_rule_exit;
1068 	}
1069 
1070 	f_entry->fltr_info.fltr_rule_id =
1071 		le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1072 	fm_entry->fltr_info.fltr_rule_id =
1073 		le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1074 
1075 	/* The book keeping entries will get removed when base driver
1076 	 * calls remove filter AQ command
1077 	 */
1078 	l_type = fm_entry->fltr_info.lkup_type;
1079 	recp = &hw->switch_info->recp_list[l_type];
1080 	list_add(&fm_entry->list_entry, &recp->filt_rules);
1081 
1082 ice_create_pkt_fwd_rule_exit:
1083 	devm_kfree(ice_hw_to_dev(hw), s_rule);
1084 	return status;
1085 }
1086 
1087 /**
1088  * ice_update_pkt_fwd_rule
1089  * @hw: pointer to the hardware structure
1090  * @f_info: filter information for switch rule
1091  *
1092  * Call AQ command to update a previously created switch rule with a
1093  * VSI list ID
1094  */
1095 static enum ice_status
1096 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1097 {
1098 	struct ice_aqc_sw_rules_elem *s_rule;
1099 	enum ice_status status;
1100 
1101 	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1102 			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1103 	if (!s_rule)
1104 		return ICE_ERR_NO_MEMORY;
1105 
1106 	ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
1107 
1108 	s_rule->pdata.lkup_tx_rx.index = cpu_to_le16(f_info->fltr_rule_id);
1109 
1110 	/* Update switch rule with new rule set to forward VSI list */
1111 	status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1112 				 ice_aqc_opc_update_sw_rules, NULL);
1113 
1114 	devm_kfree(ice_hw_to_dev(hw), s_rule);
1115 	return status;
1116 }
1117 
1118 /**
1119  * ice_update_sw_rule_bridge_mode
1120  * @hw: pointer to the HW struct
1121  *
1122  * Updates unicast switch filter rules based on VEB/VEPA mode
1123  */
1124 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
1125 {
1126 	struct ice_switch_info *sw = hw->switch_info;
1127 	struct ice_fltr_mgmt_list_entry *fm_entry;
1128 	enum ice_status status = 0;
1129 	struct list_head *rule_head;
1130 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1131 
1132 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1133 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1134 
1135 	mutex_lock(rule_lock);
1136 	list_for_each_entry(fm_entry, rule_head, list_entry) {
1137 		struct ice_fltr_info *fi = &fm_entry->fltr_info;
1138 		u8 *addr = fi->l_data.mac.mac_addr;
1139 
1140 		/* Update unicast Tx rules to reflect the selected
1141 		 * VEB/VEPA mode
1142 		 */
1143 		if ((fi->flag & ICE_FLTR_TX) && is_unicast_ether_addr(addr) &&
1144 		    (fi->fltr_act == ICE_FWD_TO_VSI ||
1145 		     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1146 		     fi->fltr_act == ICE_FWD_TO_Q ||
1147 		     fi->fltr_act == ICE_FWD_TO_QGRP)) {
1148 			status = ice_update_pkt_fwd_rule(hw, fi);
1149 			if (status)
1150 				break;
1151 		}
1152 	}
1153 
1154 	mutex_unlock(rule_lock);
1155 
1156 	return status;
1157 }
1158 
1159 /**
1160  * ice_add_update_vsi_list
1161  * @hw: pointer to the hardware structure
1162  * @m_entry: pointer to current filter management list entry
1163  * @cur_fltr: filter information from the book keeping entry
1164  * @new_fltr: filter information with the new VSI to be added
1165  *
1166  * Call AQ command to add or update previously created VSI list with new VSI.
1167  *
1168  * Helper function to do book keeping associated with adding filter information
1169  * The algorithm to do the book keeping is described below :
1170  * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
1171  *	if only one VSI has been added till now
1172  *		Allocate a new VSI list and add two VSIs
1173  *		to this list using switch rule command
1174  *		Update the previously created switch rule with the
1175  *		newly created VSI list ID
1176  *	if a VSI list was previously created
1177  *		Add the new VSI to the previously created VSI list set
1178  *		using the update switch rule command
1179  */
1180 static enum ice_status
1181 ice_add_update_vsi_list(struct ice_hw *hw,
1182 			struct ice_fltr_mgmt_list_entry *m_entry,
1183 			struct ice_fltr_info *cur_fltr,
1184 			struct ice_fltr_info *new_fltr)
1185 {
1186 	enum ice_status status = 0;
1187 	u16 vsi_list_id = 0;
1188 
1189 	if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
1190 	     cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
1191 		return ICE_ERR_NOT_IMPL;
1192 
1193 	if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
1194 	     new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
1195 	    (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
1196 	     cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
1197 		return ICE_ERR_NOT_IMPL;
1198 
1199 	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
1200 		/* Only one entry existed in the mapping and it was not already
1201 		 * a part of a VSI list. So, create a VSI list with the old and
1202 		 * new VSIs.
1203 		 */
1204 		struct ice_fltr_info tmp_fltr;
1205 		u16 vsi_handle_arr[2];
1206 
1207 		/* A rule already exists with the new VSI being added */
1208 		if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
1209 			return ICE_ERR_ALREADY_EXISTS;
1210 
1211 		vsi_handle_arr[0] = cur_fltr->vsi_handle;
1212 		vsi_handle_arr[1] = new_fltr->vsi_handle;
1213 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1214 						  &vsi_list_id,
1215 						  new_fltr->lkup_type);
1216 		if (status)
1217 			return status;
1218 
1219 		tmp_fltr = *new_fltr;
1220 		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
1221 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1222 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1223 		/* Update the previous switch rule of "MAC forward to VSI" to
1224 		 * "MAC fwd to VSI list"
1225 		 */
1226 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1227 		if (status)
1228 			return status;
1229 
1230 		cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1231 		cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1232 		m_entry->vsi_list_info =
1233 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1234 						vsi_list_id);
1235 
1236 		/* If this entry was large action then the large action needs
1237 		 * to be updated to point to FWD to VSI list
1238 		 */
1239 		if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1240 			status =
1241 			    ice_add_marker_act(hw, m_entry,
1242 					       m_entry->sw_marker_id,
1243 					       m_entry->lg_act_idx);
1244 	} else {
1245 		u16 vsi_handle = new_fltr->vsi_handle;
1246 		enum ice_adminq_opc opcode;
1247 
1248 		if (!m_entry->vsi_list_info)
1249 			return ICE_ERR_CFG;
1250 
1251 		/* A rule already exists with the new VSI being added */
1252 		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
1253 			return 0;
1254 
1255 		/* Update the previously created VSI list set with
1256 		 * the new VSI ID passed in
1257 		 */
1258 		vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1259 		opcode = ice_aqc_opc_update_sw_rules;
1260 
1261 		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
1262 						  vsi_list_id, false, opcode,
1263 						  new_fltr->lkup_type);
1264 		/* update VSI list mapping info with new VSI ID */
1265 		if (!status)
1266 			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
1267 	}
1268 	if (!status)
1269 		m_entry->vsi_count++;
1270 	return status;
1271 }
1272 
1273 /**
1274  * ice_find_rule_entry - Search a rule entry
1275  * @hw: pointer to the hardware structure
1276  * @recp_id: lookup type for which the specified rule needs to be searched
1277  * @f_info: rule information
1278  *
1279  * Helper function to search for a given rule entry
1280  * Returns pointer to entry storing the rule if found
1281  */
1282 static struct ice_fltr_mgmt_list_entry *
1283 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
1284 {
1285 	struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
1286 	struct ice_switch_info *sw = hw->switch_info;
1287 	struct list_head *list_head;
1288 
1289 	list_head = &sw->recp_list[recp_id].filt_rules;
1290 	list_for_each_entry(list_itr, list_head, list_entry) {
1291 		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
1292 			    sizeof(f_info->l_data)) &&
1293 		    f_info->flag == list_itr->fltr_info.flag) {
1294 			ret = list_itr;
1295 			break;
1296 		}
1297 	}
1298 	return ret;
1299 }
1300 
1301 /**
1302  * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
1303  * @hw: pointer to the hardware structure
1304  * @recp_id: lookup type for which VSI lists needs to be searched
1305  * @vsi_handle: VSI handle to be found in VSI list
1306  * @vsi_list_id: VSI list ID found containing vsi_handle
1307  *
1308  * Helper function to search a VSI list with single entry containing given VSI
1309  * handle element. This can be extended further to search VSI list with more
1310  * than 1 vsi_count. Returns pointer to VSI list entry if found.
1311  */
1312 static struct ice_vsi_list_map_info *
1313 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
1314 			u16 *vsi_list_id)
1315 {
1316 	struct ice_vsi_list_map_info *map_info = NULL;
1317 	struct ice_switch_info *sw = hw->switch_info;
1318 	struct ice_fltr_mgmt_list_entry *list_itr;
1319 	struct list_head *list_head;
1320 
1321 	list_head = &sw->recp_list[recp_id].filt_rules;
1322 	list_for_each_entry(list_itr, list_head, list_entry) {
1323 		if (list_itr->vsi_count == 1 && list_itr->vsi_list_info) {
1324 			map_info = list_itr->vsi_list_info;
1325 			if (test_bit(vsi_handle, map_info->vsi_map)) {
1326 				*vsi_list_id = map_info->vsi_list_id;
1327 				return map_info;
1328 			}
1329 		}
1330 	}
1331 	return NULL;
1332 }
1333 
1334 /**
1335  * ice_add_rule_internal - add rule for a given lookup type
1336  * @hw: pointer to the hardware structure
1337  * @recp_id: lookup type (recipe ID) for which rule has to be added
1338  * @f_entry: structure containing MAC forwarding information
1339  *
1340  * Adds or updates the rule lists for a given recipe
1341  */
1342 static enum ice_status
1343 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
1344 		      struct ice_fltr_list_entry *f_entry)
1345 {
1346 	struct ice_switch_info *sw = hw->switch_info;
1347 	struct ice_fltr_info *new_fltr, *cur_fltr;
1348 	struct ice_fltr_mgmt_list_entry *m_entry;
1349 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1350 	enum ice_status status = 0;
1351 
1352 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1353 		return ICE_ERR_PARAM;
1354 	f_entry->fltr_info.fwd_id.hw_vsi_id =
1355 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1356 
1357 	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1358 
1359 	mutex_lock(rule_lock);
1360 	new_fltr = &f_entry->fltr_info;
1361 	if (new_fltr->flag & ICE_FLTR_RX)
1362 		new_fltr->src = hw->port_info->lport;
1363 	else if (new_fltr->flag & ICE_FLTR_TX)
1364 		new_fltr->src = f_entry->fltr_info.fwd_id.hw_vsi_id;
1365 
1366 	m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
1367 	if (!m_entry) {
1368 		mutex_unlock(rule_lock);
1369 		return ice_create_pkt_fwd_rule(hw, f_entry);
1370 	}
1371 
1372 	cur_fltr = &m_entry->fltr_info;
1373 	status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
1374 	mutex_unlock(rule_lock);
1375 
1376 	return status;
1377 }
1378 
1379 /**
1380  * ice_remove_vsi_list_rule
1381  * @hw: pointer to the hardware structure
1382  * @vsi_list_id: VSI list ID generated as part of allocate resource
1383  * @lkup_type: switch rule filter lookup type
1384  *
1385  * The VSI list should be emptied before this function is called to remove the
1386  * VSI list.
1387  */
1388 static enum ice_status
1389 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1390 			 enum ice_sw_lkup_type lkup_type)
1391 {
1392 	struct ice_aqc_sw_rules_elem *s_rule;
1393 	enum ice_status status;
1394 	u16 s_rule_size;
1395 
1396 	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
1397 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1398 	if (!s_rule)
1399 		return ICE_ERR_NO_MEMORY;
1400 
1401 	s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
1402 	s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
1403 
1404 	/* Free the vsi_list resource that we allocated. It is assumed that the
1405 	 * list is empty at this point.
1406 	 */
1407 	status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
1408 					    ice_aqc_opc_free_res);
1409 
1410 	devm_kfree(ice_hw_to_dev(hw), s_rule);
1411 	return status;
1412 }
1413 
1414 /**
1415  * ice_rem_update_vsi_list
1416  * @hw: pointer to the hardware structure
1417  * @vsi_handle: VSI handle of the VSI to remove
1418  * @fm_list: filter management entry for which the VSI list management needs to
1419  *           be done
1420  */
1421 static enum ice_status
1422 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
1423 			struct ice_fltr_mgmt_list_entry *fm_list)
1424 {
1425 	enum ice_sw_lkup_type lkup_type;
1426 	enum ice_status status = 0;
1427 	u16 vsi_list_id;
1428 
1429 	if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
1430 	    fm_list->vsi_count == 0)
1431 		return ICE_ERR_PARAM;
1432 
1433 	/* A rule with the VSI being removed does not exist */
1434 	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
1435 		return ICE_ERR_DOES_NOT_EXIST;
1436 
1437 	lkup_type = fm_list->fltr_info.lkup_type;
1438 	vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
1439 	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
1440 					  ice_aqc_opc_update_sw_rules,
1441 					  lkup_type);
1442 	if (status)
1443 		return status;
1444 
1445 	fm_list->vsi_count--;
1446 	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
1447 
1448 	if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
1449 		struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
1450 		struct ice_vsi_list_map_info *vsi_list_info =
1451 			fm_list->vsi_list_info;
1452 		u16 rem_vsi_handle;
1453 
1454 		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
1455 						ICE_MAX_VSI);
1456 		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
1457 			return ICE_ERR_OUT_OF_RANGE;
1458 
1459 		/* Make sure VSI list is empty before removing it below */
1460 		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
1461 						  vsi_list_id, true,
1462 						  ice_aqc_opc_update_sw_rules,
1463 						  lkup_type);
1464 		if (status)
1465 			return status;
1466 
1467 		tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
1468 		tmp_fltr_info.fwd_id.hw_vsi_id =
1469 			ice_get_hw_vsi_num(hw, rem_vsi_handle);
1470 		tmp_fltr_info.vsi_handle = rem_vsi_handle;
1471 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
1472 		if (status) {
1473 			ice_debug(hw, ICE_DBG_SW,
1474 				  "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
1475 				  tmp_fltr_info.fwd_id.hw_vsi_id, status);
1476 			return status;
1477 		}
1478 
1479 		fm_list->fltr_info = tmp_fltr_info;
1480 	}
1481 
1482 	if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
1483 	    (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
1484 		struct ice_vsi_list_map_info *vsi_list_info =
1485 			fm_list->vsi_list_info;
1486 
1487 		/* Remove the VSI list since it is no longer used */
1488 		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
1489 		if (status) {
1490 			ice_debug(hw, ICE_DBG_SW,
1491 				  "Failed to remove VSI list %d, error %d\n",
1492 				  vsi_list_id, status);
1493 			return status;
1494 		}
1495 
1496 		list_del(&vsi_list_info->list_entry);
1497 		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
1498 		fm_list->vsi_list_info = NULL;
1499 	}
1500 
1501 	return status;
1502 }
1503 
1504 /**
1505  * ice_remove_rule_internal - Remove a filter rule of a given type
1506  * @hw: pointer to the hardware structure
1507  * @recp_id: recipe ID for which the rule needs to removed
1508  * @f_entry: rule entry containing filter information
1509  */
1510 static enum ice_status
1511 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
1512 			 struct ice_fltr_list_entry *f_entry)
1513 {
1514 	struct ice_switch_info *sw = hw->switch_info;
1515 	struct ice_fltr_mgmt_list_entry *list_elem;
1516 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1517 	enum ice_status status = 0;
1518 	bool remove_rule = false;
1519 	u16 vsi_handle;
1520 
1521 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1522 		return ICE_ERR_PARAM;
1523 	f_entry->fltr_info.fwd_id.hw_vsi_id =
1524 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1525 
1526 	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1527 	mutex_lock(rule_lock);
1528 	list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
1529 	if (!list_elem) {
1530 		status = ICE_ERR_DOES_NOT_EXIST;
1531 		goto exit;
1532 	}
1533 
1534 	if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
1535 		remove_rule = true;
1536 	} else if (!list_elem->vsi_list_info) {
1537 		status = ICE_ERR_DOES_NOT_EXIST;
1538 		goto exit;
1539 	} else if (list_elem->vsi_list_info->ref_cnt > 1) {
1540 		/* a ref_cnt > 1 indicates that the vsi_list is being
1541 		 * shared by multiple rules. Decrement the ref_cnt and
1542 		 * remove this rule, but do not modify the list, as it
1543 		 * is in-use by other rules.
1544 		 */
1545 		list_elem->vsi_list_info->ref_cnt--;
1546 		remove_rule = true;
1547 	} else {
1548 		/* a ref_cnt of 1 indicates the vsi_list is only used
1549 		 * by one rule. However, the original removal request is only
1550 		 * for a single VSI. Update the vsi_list first, and only
1551 		 * remove the rule if there are no further VSIs in this list.
1552 		 */
1553 		vsi_handle = f_entry->fltr_info.vsi_handle;
1554 		status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
1555 		if (status)
1556 			goto exit;
1557 		/* if VSI count goes to zero after updating the VSI list */
1558 		if (list_elem->vsi_count == 0)
1559 			remove_rule = true;
1560 	}
1561 
1562 	if (remove_rule) {
1563 		/* Remove the lookup rule */
1564 		struct ice_aqc_sw_rules_elem *s_rule;
1565 
1566 		s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1567 				      ICE_SW_RULE_RX_TX_NO_HDR_SIZE,
1568 				      GFP_KERNEL);
1569 		if (!s_rule) {
1570 			status = ICE_ERR_NO_MEMORY;
1571 			goto exit;
1572 		}
1573 
1574 		ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
1575 				 ice_aqc_opc_remove_sw_rules);
1576 
1577 		status = ice_aq_sw_rules(hw, s_rule,
1578 					 ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
1579 					 ice_aqc_opc_remove_sw_rules, NULL);
1580 
1581 		/* Remove a book keeping from the list */
1582 		devm_kfree(ice_hw_to_dev(hw), s_rule);
1583 
1584 		if (status)
1585 			goto exit;
1586 
1587 		list_del(&list_elem->list_entry);
1588 		devm_kfree(ice_hw_to_dev(hw), list_elem);
1589 	}
1590 exit:
1591 	mutex_unlock(rule_lock);
1592 	return status;
1593 }
1594 
1595 /**
1596  * ice_add_mac - Add a MAC address based filter rule
1597  * @hw: pointer to the hardware structure
1598  * @m_list: list of MAC addresses and forwarding information
1599  *
1600  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
1601  * multiple unicast addresses, the function assumes that all the
1602  * addresses are unique in a given add_mac call. It doesn't
1603  * check for duplicates in this case, removing duplicates from a given
1604  * list should be taken care of in the caller of this function.
1605  */
1606 enum ice_status ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
1607 {
1608 	struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
1609 	struct ice_fltr_list_entry *m_list_itr;
1610 	struct list_head *rule_head;
1611 	u16 total_elem_left, s_rule_size;
1612 	struct ice_switch_info *sw;
1613 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1614 	enum ice_status status = 0;
1615 	u16 num_unicast = 0;
1616 	u8 elem_sent;
1617 
1618 	if (!m_list || !hw)
1619 		return ICE_ERR_PARAM;
1620 
1621 	s_rule = NULL;
1622 	sw = hw->switch_info;
1623 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1624 	list_for_each_entry(m_list_itr, m_list, list_entry) {
1625 		u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
1626 		u16 vsi_handle;
1627 		u16 hw_vsi_id;
1628 
1629 		m_list_itr->fltr_info.flag = ICE_FLTR_TX;
1630 		vsi_handle = m_list_itr->fltr_info.vsi_handle;
1631 		if (!ice_is_vsi_valid(hw, vsi_handle))
1632 			return ICE_ERR_PARAM;
1633 		hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
1634 		m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
1635 		/* update the src in case it is VSI num */
1636 		if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
1637 			return ICE_ERR_PARAM;
1638 		m_list_itr->fltr_info.src = hw_vsi_id;
1639 		if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
1640 		    is_zero_ether_addr(add))
1641 			return ICE_ERR_PARAM;
1642 		if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
1643 			/* Don't overwrite the unicast address */
1644 			mutex_lock(rule_lock);
1645 			if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
1646 						&m_list_itr->fltr_info)) {
1647 				mutex_unlock(rule_lock);
1648 				return ICE_ERR_ALREADY_EXISTS;
1649 			}
1650 			mutex_unlock(rule_lock);
1651 			num_unicast++;
1652 		} else if (is_multicast_ether_addr(add) ||
1653 			   (is_unicast_ether_addr(add) && hw->ucast_shared)) {
1654 			m_list_itr->status =
1655 				ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
1656 						      m_list_itr);
1657 			if (m_list_itr->status)
1658 				return m_list_itr->status;
1659 		}
1660 	}
1661 
1662 	mutex_lock(rule_lock);
1663 	/* Exit if no suitable entries were found for adding bulk switch rule */
1664 	if (!num_unicast) {
1665 		status = 0;
1666 		goto ice_add_mac_exit;
1667 	}
1668 
1669 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1670 
1671 	/* Allocate switch rule buffer for the bulk update for unicast */
1672 	s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1673 	s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
1674 			      GFP_KERNEL);
1675 	if (!s_rule) {
1676 		status = ICE_ERR_NO_MEMORY;
1677 		goto ice_add_mac_exit;
1678 	}
1679 
1680 	r_iter = s_rule;
1681 	list_for_each_entry(m_list_itr, m_list, list_entry) {
1682 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1683 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1684 
1685 		if (is_unicast_ether_addr(mac_addr)) {
1686 			ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
1687 					 ice_aqc_opc_add_sw_rules);
1688 			r_iter = (struct ice_aqc_sw_rules_elem *)
1689 				((u8 *)r_iter + s_rule_size);
1690 		}
1691 	}
1692 
1693 	/* Call AQ bulk switch rule update for all unicast addresses */
1694 	r_iter = s_rule;
1695 	/* Call AQ switch rule in AQ_MAX chunk */
1696 	for (total_elem_left = num_unicast; total_elem_left > 0;
1697 	     total_elem_left -= elem_sent) {
1698 		struct ice_aqc_sw_rules_elem *entry = r_iter;
1699 
1700 		elem_sent = min_t(u8, total_elem_left,
1701 				  (ICE_AQ_MAX_BUF_LEN / s_rule_size));
1702 		status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
1703 					 elem_sent, ice_aqc_opc_add_sw_rules,
1704 					 NULL);
1705 		if (status)
1706 			goto ice_add_mac_exit;
1707 		r_iter = (struct ice_aqc_sw_rules_elem *)
1708 			((u8 *)r_iter + (elem_sent * s_rule_size));
1709 	}
1710 
1711 	/* Fill up rule ID based on the value returned from FW */
1712 	r_iter = s_rule;
1713 	list_for_each_entry(m_list_itr, m_list, list_entry) {
1714 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1715 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1716 		struct ice_fltr_mgmt_list_entry *fm_entry;
1717 
1718 		if (is_unicast_ether_addr(mac_addr)) {
1719 			f_info->fltr_rule_id =
1720 				le16_to_cpu(r_iter->pdata.lkup_tx_rx.index);
1721 			f_info->fltr_act = ICE_FWD_TO_VSI;
1722 			/* Create an entry to track this MAC address */
1723 			fm_entry = devm_kzalloc(ice_hw_to_dev(hw),
1724 						sizeof(*fm_entry), GFP_KERNEL);
1725 			if (!fm_entry) {
1726 				status = ICE_ERR_NO_MEMORY;
1727 				goto ice_add_mac_exit;
1728 			}
1729 			fm_entry->fltr_info = *f_info;
1730 			fm_entry->vsi_count = 1;
1731 			/* The book keeping entries will get removed when
1732 			 * base driver calls remove filter AQ command
1733 			 */
1734 
1735 			list_add(&fm_entry->list_entry, rule_head);
1736 			r_iter = (struct ice_aqc_sw_rules_elem *)
1737 				((u8 *)r_iter + s_rule_size);
1738 		}
1739 	}
1740 
1741 ice_add_mac_exit:
1742 	mutex_unlock(rule_lock);
1743 	if (s_rule)
1744 		devm_kfree(ice_hw_to_dev(hw), s_rule);
1745 	return status;
1746 }
1747 
1748 /**
1749  * ice_add_vlan_internal - Add one VLAN based filter rule
1750  * @hw: pointer to the hardware structure
1751  * @f_entry: filter entry containing one VLAN information
1752  */
1753 static enum ice_status
1754 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1755 {
1756 	struct ice_switch_info *sw = hw->switch_info;
1757 	struct ice_fltr_mgmt_list_entry *v_list_itr;
1758 	struct ice_fltr_info *new_fltr, *cur_fltr;
1759 	enum ice_sw_lkup_type lkup_type;
1760 	u16 vsi_list_id = 0, vsi_handle;
1761 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1762 	enum ice_status status = 0;
1763 
1764 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1765 		return ICE_ERR_PARAM;
1766 
1767 	f_entry->fltr_info.fwd_id.hw_vsi_id =
1768 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1769 	new_fltr = &f_entry->fltr_info;
1770 
1771 	/* VLAN ID should only be 12 bits */
1772 	if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
1773 		return ICE_ERR_PARAM;
1774 
1775 	if (new_fltr->src_id != ICE_SRC_ID_VSI)
1776 		return ICE_ERR_PARAM;
1777 
1778 	new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
1779 	lkup_type = new_fltr->lkup_type;
1780 	vsi_handle = new_fltr->vsi_handle;
1781 	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
1782 	mutex_lock(rule_lock);
1783 	v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
1784 	if (!v_list_itr) {
1785 		struct ice_vsi_list_map_info *map_info = NULL;
1786 
1787 		if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
1788 			/* All VLAN pruning rules use a VSI list. Check if
1789 			 * there is already a VSI list containing VSI that we
1790 			 * want to add. If found, use the same vsi_list_id for
1791 			 * this new VLAN rule or else create a new list.
1792 			 */
1793 			map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
1794 							   vsi_handle,
1795 							   &vsi_list_id);
1796 			if (!map_info) {
1797 				status = ice_create_vsi_list_rule(hw,
1798 								  &vsi_handle,
1799 								  1,
1800 								  &vsi_list_id,
1801 								  lkup_type);
1802 				if (status)
1803 					goto exit;
1804 			}
1805 			/* Convert the action to forwarding to a VSI list. */
1806 			new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1807 			new_fltr->fwd_id.vsi_list_id = vsi_list_id;
1808 		}
1809 
1810 		status = ice_create_pkt_fwd_rule(hw, f_entry);
1811 		if (!status) {
1812 			v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
1813 							 new_fltr);
1814 			if (!v_list_itr) {
1815 				status = ICE_ERR_DOES_NOT_EXIST;
1816 				goto exit;
1817 			}
1818 			/* reuse VSI list for new rule and increment ref_cnt */
1819 			if (map_info) {
1820 				v_list_itr->vsi_list_info = map_info;
1821 				map_info->ref_cnt++;
1822 			} else {
1823 				v_list_itr->vsi_list_info =
1824 					ice_create_vsi_list_map(hw, &vsi_handle,
1825 								1, vsi_list_id);
1826 			}
1827 		}
1828 	} else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
1829 		/* Update existing VSI list to add new VSI ID only if it used
1830 		 * by one VLAN rule.
1831 		 */
1832 		cur_fltr = &v_list_itr->fltr_info;
1833 		status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
1834 						 new_fltr);
1835 	} else {
1836 		/* If VLAN rule exists and VSI list being used by this rule is
1837 		 * referenced by more than 1 VLAN rule. Then create a new VSI
1838 		 * list appending previous VSI with new VSI and update existing
1839 		 * VLAN rule to point to new VSI list ID
1840 		 */
1841 		struct ice_fltr_info tmp_fltr;
1842 		u16 vsi_handle_arr[2];
1843 		u16 cur_handle;
1844 
1845 		/* Current implementation only supports reusing VSI list with
1846 		 * one VSI count. We should never hit below condition
1847 		 */
1848 		if (v_list_itr->vsi_count > 1 &&
1849 		    v_list_itr->vsi_list_info->ref_cnt > 1) {
1850 			ice_debug(hw, ICE_DBG_SW,
1851 				  "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
1852 			status = ICE_ERR_CFG;
1853 			goto exit;
1854 		}
1855 
1856 		cur_handle =
1857 			find_first_bit(v_list_itr->vsi_list_info->vsi_map,
1858 				       ICE_MAX_VSI);
1859 
1860 		/* A rule already exists with the new VSI being added */
1861 		if (cur_handle == vsi_handle) {
1862 			status = ICE_ERR_ALREADY_EXISTS;
1863 			goto exit;
1864 		}
1865 
1866 		vsi_handle_arr[0] = cur_handle;
1867 		vsi_handle_arr[1] = vsi_handle;
1868 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1869 						  &vsi_list_id, lkup_type);
1870 		if (status)
1871 			goto exit;
1872 
1873 		tmp_fltr = v_list_itr->fltr_info;
1874 		tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
1875 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1876 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1877 		/* Update the previous switch rule to a new VSI list which
1878 		 * includes current VSI that is requested
1879 		 */
1880 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1881 		if (status)
1882 			goto exit;
1883 
1884 		/* before overriding VSI list map info. decrement ref_cnt of
1885 		 * previous VSI list
1886 		 */
1887 		v_list_itr->vsi_list_info->ref_cnt--;
1888 
1889 		/* now update to newly created list */
1890 		v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
1891 		v_list_itr->vsi_list_info =
1892 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1893 						vsi_list_id);
1894 		v_list_itr->vsi_count++;
1895 	}
1896 
1897 exit:
1898 	mutex_unlock(rule_lock);
1899 	return status;
1900 }
1901 
1902 /**
1903  * ice_add_vlan - Add VLAN based filter rule
1904  * @hw: pointer to the hardware structure
1905  * @v_list: list of VLAN entries and forwarding information
1906  */
1907 enum ice_status ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
1908 {
1909 	struct ice_fltr_list_entry *v_list_itr;
1910 
1911 	if (!v_list || !hw)
1912 		return ICE_ERR_PARAM;
1913 
1914 	list_for_each_entry(v_list_itr, v_list, list_entry) {
1915 		if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
1916 			return ICE_ERR_PARAM;
1917 		v_list_itr->fltr_info.flag = ICE_FLTR_TX;
1918 		v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
1919 		if (v_list_itr->status)
1920 			return v_list_itr->status;
1921 	}
1922 	return 0;
1923 }
1924 
1925 /**
1926  * ice_add_eth_mac - Add ethertype and MAC based filter rule
1927  * @hw: pointer to the hardware structure
1928  * @em_list: list of ether type MAC filter, MAC is optional
1929  *
1930  * This function requires the caller to populate the entries in
1931  * the filter list with the necessary fields (including flags to
1932  * indicate Tx or Rx rules).
1933  */
1934 enum ice_status
1935 ice_add_eth_mac(struct ice_hw *hw, struct list_head *em_list)
1936 {
1937 	struct ice_fltr_list_entry *em_list_itr;
1938 
1939 	if (!em_list || !hw)
1940 		return ICE_ERR_PARAM;
1941 
1942 	list_for_each_entry(em_list_itr, em_list, list_entry) {
1943 		enum ice_sw_lkup_type l_type =
1944 			em_list_itr->fltr_info.lkup_type;
1945 
1946 		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
1947 		    l_type != ICE_SW_LKUP_ETHERTYPE)
1948 			return ICE_ERR_PARAM;
1949 
1950 		em_list_itr->status = ice_add_rule_internal(hw, l_type,
1951 							    em_list_itr);
1952 		if (em_list_itr->status)
1953 			return em_list_itr->status;
1954 	}
1955 	return 0;
1956 }
1957 
1958 /**
1959  * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
1960  * @hw: pointer to the hardware structure
1961  * @em_list: list of ethertype or ethertype MAC entries
1962  */
1963 enum ice_status
1964 ice_remove_eth_mac(struct ice_hw *hw, struct list_head *em_list)
1965 {
1966 	struct ice_fltr_list_entry *em_list_itr, *tmp;
1967 
1968 	if (!em_list || !hw)
1969 		return ICE_ERR_PARAM;
1970 
1971 	list_for_each_entry_safe(em_list_itr, tmp, em_list, list_entry) {
1972 		enum ice_sw_lkup_type l_type =
1973 			em_list_itr->fltr_info.lkup_type;
1974 
1975 		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
1976 		    l_type != ICE_SW_LKUP_ETHERTYPE)
1977 			return ICE_ERR_PARAM;
1978 
1979 		em_list_itr->status = ice_remove_rule_internal(hw, l_type,
1980 							       em_list_itr);
1981 		if (em_list_itr->status)
1982 			return em_list_itr->status;
1983 	}
1984 	return 0;
1985 }
1986 
1987 /**
1988  * ice_rem_sw_rule_info
1989  * @hw: pointer to the hardware structure
1990  * @rule_head: pointer to the switch list structure that we want to delete
1991  */
1992 static void
1993 ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
1994 {
1995 	if (!list_empty(rule_head)) {
1996 		struct ice_fltr_mgmt_list_entry *entry;
1997 		struct ice_fltr_mgmt_list_entry *tmp;
1998 
1999 		list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
2000 			list_del(&entry->list_entry);
2001 			devm_kfree(ice_hw_to_dev(hw), entry);
2002 		}
2003 	}
2004 }
2005 
2006 /**
2007  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
2008  * @hw: pointer to the hardware structure
2009  * @vsi_handle: VSI handle to set as default
2010  * @set: true to add the above mentioned switch rule, false to remove it
2011  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
2012  *
2013  * add filter rule to set/unset given VSI as default VSI for the switch
2014  * (represented by swid)
2015  */
2016 enum ice_status
2017 ice_cfg_dflt_vsi(struct ice_hw *hw, u16 vsi_handle, bool set, u8 direction)
2018 {
2019 	struct ice_aqc_sw_rules_elem *s_rule;
2020 	struct ice_fltr_info f_info;
2021 	enum ice_adminq_opc opcode;
2022 	enum ice_status status;
2023 	u16 s_rule_size;
2024 	u16 hw_vsi_id;
2025 
2026 	if (!ice_is_vsi_valid(hw, vsi_handle))
2027 		return ICE_ERR_PARAM;
2028 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2029 
2030 	s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
2031 		ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
2032 
2033 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
2034 	if (!s_rule)
2035 		return ICE_ERR_NO_MEMORY;
2036 
2037 	memset(&f_info, 0, sizeof(f_info));
2038 
2039 	f_info.lkup_type = ICE_SW_LKUP_DFLT;
2040 	f_info.flag = direction;
2041 	f_info.fltr_act = ICE_FWD_TO_VSI;
2042 	f_info.fwd_id.hw_vsi_id = hw_vsi_id;
2043 
2044 	if (f_info.flag & ICE_FLTR_RX) {
2045 		f_info.src = hw->port_info->lport;
2046 		f_info.src_id = ICE_SRC_ID_LPORT;
2047 		if (!set)
2048 			f_info.fltr_rule_id =
2049 				hw->port_info->dflt_rx_vsi_rule_id;
2050 	} else if (f_info.flag & ICE_FLTR_TX) {
2051 		f_info.src_id = ICE_SRC_ID_VSI;
2052 		f_info.src = hw_vsi_id;
2053 		if (!set)
2054 			f_info.fltr_rule_id =
2055 				hw->port_info->dflt_tx_vsi_rule_id;
2056 	}
2057 
2058 	if (set)
2059 		opcode = ice_aqc_opc_add_sw_rules;
2060 	else
2061 		opcode = ice_aqc_opc_remove_sw_rules;
2062 
2063 	ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
2064 
2065 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
2066 	if (status || !(f_info.flag & ICE_FLTR_TX_RX))
2067 		goto out;
2068 	if (set) {
2069 		u16 index = le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
2070 
2071 		if (f_info.flag & ICE_FLTR_TX) {
2072 			hw->port_info->dflt_tx_vsi_num = hw_vsi_id;
2073 			hw->port_info->dflt_tx_vsi_rule_id = index;
2074 		} else if (f_info.flag & ICE_FLTR_RX) {
2075 			hw->port_info->dflt_rx_vsi_num = hw_vsi_id;
2076 			hw->port_info->dflt_rx_vsi_rule_id = index;
2077 		}
2078 	} else {
2079 		if (f_info.flag & ICE_FLTR_TX) {
2080 			hw->port_info->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
2081 			hw->port_info->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
2082 		} else if (f_info.flag & ICE_FLTR_RX) {
2083 			hw->port_info->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
2084 			hw->port_info->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
2085 		}
2086 	}
2087 
2088 out:
2089 	devm_kfree(ice_hw_to_dev(hw), s_rule);
2090 	return status;
2091 }
2092 
2093 /**
2094  * ice_find_ucast_rule_entry - Search for a unicast MAC filter rule entry
2095  * @hw: pointer to the hardware structure
2096  * @recp_id: lookup type for which the specified rule needs to be searched
2097  * @f_info: rule information
2098  *
2099  * Helper function to search for a unicast rule entry - this is to be used
2100  * to remove unicast MAC filter that is not shared with other VSIs on the
2101  * PF switch.
2102  *
2103  * Returns pointer to entry storing the rule if found
2104  */
2105 static struct ice_fltr_mgmt_list_entry *
2106 ice_find_ucast_rule_entry(struct ice_hw *hw, u8 recp_id,
2107 			  struct ice_fltr_info *f_info)
2108 {
2109 	struct ice_switch_info *sw = hw->switch_info;
2110 	struct ice_fltr_mgmt_list_entry *list_itr;
2111 	struct list_head *list_head;
2112 
2113 	list_head = &sw->recp_list[recp_id].filt_rules;
2114 	list_for_each_entry(list_itr, list_head, list_entry) {
2115 		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
2116 			    sizeof(f_info->l_data)) &&
2117 		    f_info->fwd_id.hw_vsi_id ==
2118 		    list_itr->fltr_info.fwd_id.hw_vsi_id &&
2119 		    f_info->flag == list_itr->fltr_info.flag)
2120 			return list_itr;
2121 	}
2122 	return NULL;
2123 }
2124 
2125 /**
2126  * ice_remove_mac - remove a MAC address based filter rule
2127  * @hw: pointer to the hardware structure
2128  * @m_list: list of MAC addresses and forwarding information
2129  *
2130  * This function removes either a MAC filter rule or a specific VSI from a
2131  * VSI list for a multicast MAC address.
2132  *
2133  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
2134  * ice_add_mac. Caller should be aware that this call will only work if all
2135  * the entries passed into m_list were added previously. It will not attempt to
2136  * do a partial remove of entries that were found.
2137  */
2138 enum ice_status ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
2139 {
2140 	struct ice_fltr_list_entry *list_itr, *tmp;
2141 	struct mutex *rule_lock; /* Lock to protect filter rule list */
2142 
2143 	if (!m_list)
2144 		return ICE_ERR_PARAM;
2145 
2146 	rule_lock = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2147 	list_for_each_entry_safe(list_itr, tmp, m_list, list_entry) {
2148 		enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2149 		u8 *add = &list_itr->fltr_info.l_data.mac.mac_addr[0];
2150 		u16 vsi_handle;
2151 
2152 		if (l_type != ICE_SW_LKUP_MAC)
2153 			return ICE_ERR_PARAM;
2154 
2155 		vsi_handle = list_itr->fltr_info.vsi_handle;
2156 		if (!ice_is_vsi_valid(hw, vsi_handle))
2157 			return ICE_ERR_PARAM;
2158 
2159 		list_itr->fltr_info.fwd_id.hw_vsi_id =
2160 					ice_get_hw_vsi_num(hw, vsi_handle);
2161 		if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
2162 			/* Don't remove the unicast address that belongs to
2163 			 * another VSI on the switch, since it is not being
2164 			 * shared...
2165 			 */
2166 			mutex_lock(rule_lock);
2167 			if (!ice_find_ucast_rule_entry(hw, ICE_SW_LKUP_MAC,
2168 						       &list_itr->fltr_info)) {
2169 				mutex_unlock(rule_lock);
2170 				return ICE_ERR_DOES_NOT_EXIST;
2171 			}
2172 			mutex_unlock(rule_lock);
2173 		}
2174 		list_itr->status = ice_remove_rule_internal(hw,
2175 							    ICE_SW_LKUP_MAC,
2176 							    list_itr);
2177 		if (list_itr->status)
2178 			return list_itr->status;
2179 	}
2180 	return 0;
2181 }
2182 
2183 /**
2184  * ice_remove_vlan - Remove VLAN based filter rule
2185  * @hw: pointer to the hardware structure
2186  * @v_list: list of VLAN entries and forwarding information
2187  */
2188 enum ice_status
2189 ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
2190 {
2191 	struct ice_fltr_list_entry *v_list_itr, *tmp;
2192 
2193 	if (!v_list || !hw)
2194 		return ICE_ERR_PARAM;
2195 
2196 	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
2197 		enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2198 
2199 		if (l_type != ICE_SW_LKUP_VLAN)
2200 			return ICE_ERR_PARAM;
2201 		v_list_itr->status = ice_remove_rule_internal(hw,
2202 							      ICE_SW_LKUP_VLAN,
2203 							      v_list_itr);
2204 		if (v_list_itr->status)
2205 			return v_list_itr->status;
2206 	}
2207 	return 0;
2208 }
2209 
2210 /**
2211  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
2212  * @fm_entry: filter entry to inspect
2213  * @vsi_handle: VSI handle to compare with filter info
2214  */
2215 static bool
2216 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
2217 {
2218 	return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
2219 		 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
2220 		(fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
2221 		 (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
2222 }
2223 
2224 /**
2225  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
2226  * @hw: pointer to the hardware structure
2227  * @vsi_handle: VSI handle to remove filters from
2228  * @vsi_list_head: pointer to the list to add entry to
2229  * @fi: pointer to fltr_info of filter entry to copy & add
2230  *
2231  * Helper function, used when creating a list of filters to remove from
2232  * a specific VSI. The entry added to vsi_list_head is a COPY of the
2233  * original filter entry, with the exception of fltr_info.fltr_act and
2234  * fltr_info.fwd_id fields. These are set such that later logic can
2235  * extract which VSI to remove the fltr from, and pass on that information.
2236  */
2237 static enum ice_status
2238 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2239 			       struct list_head *vsi_list_head,
2240 			       struct ice_fltr_info *fi)
2241 {
2242 	struct ice_fltr_list_entry *tmp;
2243 
2244 	/* this memory is freed up in the caller function
2245 	 * once filters for this VSI are removed
2246 	 */
2247 	tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
2248 	if (!tmp)
2249 		return ICE_ERR_NO_MEMORY;
2250 
2251 	tmp->fltr_info = *fi;
2252 
2253 	/* Overwrite these fields to indicate which VSI to remove filter from,
2254 	 * so find and remove logic can extract the information from the
2255 	 * list entries. Note that original entries will still have proper
2256 	 * values.
2257 	 */
2258 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2259 	tmp->fltr_info.vsi_handle = vsi_handle;
2260 	tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2261 
2262 	list_add(&tmp->list_entry, vsi_list_head);
2263 
2264 	return 0;
2265 }
2266 
2267 /**
2268  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
2269  * @hw: pointer to the hardware structure
2270  * @vsi_handle: VSI handle to remove filters from
2271  * @lkup_list_head: pointer to the list that has certain lookup type filters
2272  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
2273  *
2274  * Locates all filters in lkup_list_head that are used by the given VSI,
2275  * and adds COPIES of those entries to vsi_list_head (intended to be used
2276  * to remove the listed filters).
2277  * Note that this means all entries in vsi_list_head must be explicitly
2278  * deallocated by the caller when done with list.
2279  */
2280 static enum ice_status
2281 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2282 			 struct list_head *lkup_list_head,
2283 			 struct list_head *vsi_list_head)
2284 {
2285 	struct ice_fltr_mgmt_list_entry *fm_entry;
2286 	enum ice_status status = 0;
2287 
2288 	/* check to make sure VSI ID is valid and within boundary */
2289 	if (!ice_is_vsi_valid(hw, vsi_handle))
2290 		return ICE_ERR_PARAM;
2291 
2292 	list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
2293 		struct ice_fltr_info *fi;
2294 
2295 		fi = &fm_entry->fltr_info;
2296 		if (!fi || !ice_vsi_uses_fltr(fm_entry, vsi_handle))
2297 			continue;
2298 
2299 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2300 							vsi_list_head, fi);
2301 		if (status)
2302 			return status;
2303 	}
2304 	return status;
2305 }
2306 
2307 /**
2308  * ice_determine_promisc_mask
2309  * @fi: filter info to parse
2310  *
2311  * Helper function to determine which ICE_PROMISC_ mask corresponds
2312  * to given filter into.
2313  */
2314 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
2315 {
2316 	u16 vid = fi->l_data.mac_vlan.vlan_id;
2317 	u8 *macaddr = fi->l_data.mac.mac_addr;
2318 	bool is_tx_fltr = false;
2319 	u8 promisc_mask = 0;
2320 
2321 	if (fi->flag == ICE_FLTR_TX)
2322 		is_tx_fltr = true;
2323 
2324 	if (is_broadcast_ether_addr(macaddr))
2325 		promisc_mask |= is_tx_fltr ?
2326 			ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
2327 	else if (is_multicast_ether_addr(macaddr))
2328 		promisc_mask |= is_tx_fltr ?
2329 			ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
2330 	else if (is_unicast_ether_addr(macaddr))
2331 		promisc_mask |= is_tx_fltr ?
2332 			ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
2333 	if (vid)
2334 		promisc_mask |= is_tx_fltr ?
2335 			ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
2336 
2337 	return promisc_mask;
2338 }
2339 
2340 /**
2341  * ice_remove_promisc - Remove promisc based filter rules
2342  * @hw: pointer to the hardware structure
2343  * @recp_id: recipe ID for which the rule needs to removed
2344  * @v_list: list of promisc entries
2345  */
2346 static enum ice_status
2347 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
2348 		   struct list_head *v_list)
2349 {
2350 	struct ice_fltr_list_entry *v_list_itr, *tmp;
2351 
2352 	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
2353 		v_list_itr->status =
2354 			ice_remove_rule_internal(hw, recp_id, v_list_itr);
2355 		if (v_list_itr->status)
2356 			return v_list_itr->status;
2357 	}
2358 	return 0;
2359 }
2360 
2361 /**
2362  * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
2363  * @hw: pointer to the hardware structure
2364  * @vsi_handle: VSI handle to clear mode
2365  * @promisc_mask: mask of promiscuous config bits to clear
2366  * @vid: VLAN ID to clear VLAN promiscuous
2367  */
2368 enum ice_status
2369 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
2370 		      u16 vid)
2371 {
2372 	struct ice_switch_info *sw = hw->switch_info;
2373 	struct ice_fltr_list_entry *fm_entry, *tmp;
2374 	struct list_head remove_list_head;
2375 	struct ice_fltr_mgmt_list_entry *itr;
2376 	struct list_head *rule_head;
2377 	struct mutex *rule_lock;	/* Lock to protect filter rule list */
2378 	enum ice_status status = 0;
2379 	u8 recipe_id;
2380 
2381 	if (!ice_is_vsi_valid(hw, vsi_handle))
2382 		return ICE_ERR_PARAM;
2383 
2384 	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX))
2385 		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
2386 	else
2387 		recipe_id = ICE_SW_LKUP_PROMISC;
2388 
2389 	rule_head = &sw->recp_list[recipe_id].filt_rules;
2390 	rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
2391 
2392 	INIT_LIST_HEAD(&remove_list_head);
2393 
2394 	mutex_lock(rule_lock);
2395 	list_for_each_entry(itr, rule_head, list_entry) {
2396 		struct ice_fltr_info *fltr_info;
2397 		u8 fltr_promisc_mask = 0;
2398 
2399 		if (!ice_vsi_uses_fltr(itr, vsi_handle))
2400 			continue;
2401 		fltr_info = &itr->fltr_info;
2402 
2403 		if (recipe_id == ICE_SW_LKUP_PROMISC_VLAN &&
2404 		    vid != fltr_info->l_data.mac_vlan.vlan_id)
2405 			continue;
2406 
2407 		fltr_promisc_mask |= ice_determine_promisc_mask(fltr_info);
2408 
2409 		/* Skip if filter is not completely specified by given mask */
2410 		if (fltr_promisc_mask & ~promisc_mask)
2411 			continue;
2412 
2413 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2414 							&remove_list_head,
2415 							fltr_info);
2416 		if (status) {
2417 			mutex_unlock(rule_lock);
2418 			goto free_fltr_list;
2419 		}
2420 	}
2421 	mutex_unlock(rule_lock);
2422 
2423 	status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
2424 
2425 free_fltr_list:
2426 	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
2427 		list_del(&fm_entry->list_entry);
2428 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
2429 	}
2430 
2431 	return status;
2432 }
2433 
2434 /**
2435  * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
2436  * @hw: pointer to the hardware structure
2437  * @vsi_handle: VSI handle to configure
2438  * @promisc_mask: mask of promiscuous config bits
2439  * @vid: VLAN ID to set VLAN promiscuous
2440  */
2441 enum ice_status
2442 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
2443 {
2444 	enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
2445 	struct ice_fltr_list_entry f_list_entry;
2446 	struct ice_fltr_info new_fltr;
2447 	enum ice_status status = 0;
2448 	bool is_tx_fltr;
2449 	u16 hw_vsi_id;
2450 	int pkt_type;
2451 	u8 recipe_id;
2452 
2453 	if (!ice_is_vsi_valid(hw, vsi_handle))
2454 		return ICE_ERR_PARAM;
2455 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2456 
2457 	memset(&new_fltr, 0, sizeof(new_fltr));
2458 
2459 	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
2460 		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
2461 		new_fltr.l_data.mac_vlan.vlan_id = vid;
2462 		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
2463 	} else {
2464 		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
2465 		recipe_id = ICE_SW_LKUP_PROMISC;
2466 	}
2467 
2468 	/* Separate filters must be set for each direction/packet type
2469 	 * combination, so we will loop over the mask value, store the
2470 	 * individual type, and clear it out in the input mask as it
2471 	 * is found.
2472 	 */
2473 	while (promisc_mask) {
2474 		u8 *mac_addr;
2475 
2476 		pkt_type = 0;
2477 		is_tx_fltr = false;
2478 
2479 		if (promisc_mask & ICE_PROMISC_UCAST_RX) {
2480 			promisc_mask &= ~ICE_PROMISC_UCAST_RX;
2481 			pkt_type = UCAST_FLTR;
2482 		} else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
2483 			promisc_mask &= ~ICE_PROMISC_UCAST_TX;
2484 			pkt_type = UCAST_FLTR;
2485 			is_tx_fltr = true;
2486 		} else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
2487 			promisc_mask &= ~ICE_PROMISC_MCAST_RX;
2488 			pkt_type = MCAST_FLTR;
2489 		} else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
2490 			promisc_mask &= ~ICE_PROMISC_MCAST_TX;
2491 			pkt_type = MCAST_FLTR;
2492 			is_tx_fltr = true;
2493 		} else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
2494 			promisc_mask &= ~ICE_PROMISC_BCAST_RX;
2495 			pkt_type = BCAST_FLTR;
2496 		} else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
2497 			promisc_mask &= ~ICE_PROMISC_BCAST_TX;
2498 			pkt_type = BCAST_FLTR;
2499 			is_tx_fltr = true;
2500 		}
2501 
2502 		/* Check for VLAN promiscuous flag */
2503 		if (promisc_mask & ICE_PROMISC_VLAN_RX) {
2504 			promisc_mask &= ~ICE_PROMISC_VLAN_RX;
2505 		} else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
2506 			promisc_mask &= ~ICE_PROMISC_VLAN_TX;
2507 			is_tx_fltr = true;
2508 		}
2509 
2510 		/* Set filter DA based on packet type */
2511 		mac_addr = new_fltr.l_data.mac.mac_addr;
2512 		if (pkt_type == BCAST_FLTR) {
2513 			eth_broadcast_addr(mac_addr);
2514 		} else if (pkt_type == MCAST_FLTR ||
2515 			   pkt_type == UCAST_FLTR) {
2516 			/* Use the dummy ether header DA */
2517 			ether_addr_copy(mac_addr, dummy_eth_header);
2518 			if (pkt_type == MCAST_FLTR)
2519 				mac_addr[0] |= 0x1;	/* Set multicast bit */
2520 		}
2521 
2522 		/* Need to reset this to zero for all iterations */
2523 		new_fltr.flag = 0;
2524 		if (is_tx_fltr) {
2525 			new_fltr.flag |= ICE_FLTR_TX;
2526 			new_fltr.src = hw_vsi_id;
2527 		} else {
2528 			new_fltr.flag |= ICE_FLTR_RX;
2529 			new_fltr.src = hw->port_info->lport;
2530 		}
2531 
2532 		new_fltr.fltr_act = ICE_FWD_TO_VSI;
2533 		new_fltr.vsi_handle = vsi_handle;
2534 		new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
2535 		f_list_entry.fltr_info = new_fltr;
2536 
2537 		status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
2538 		if (status)
2539 			goto set_promisc_exit;
2540 	}
2541 
2542 set_promisc_exit:
2543 	return status;
2544 }
2545 
2546 /**
2547  * ice_set_vlan_vsi_promisc
2548  * @hw: pointer to the hardware structure
2549  * @vsi_handle: VSI handle to configure
2550  * @promisc_mask: mask of promiscuous config bits
2551  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
2552  *
2553  * Configure VSI with all associated VLANs to given promiscuous mode(s)
2554  */
2555 enum ice_status
2556 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
2557 			 bool rm_vlan_promisc)
2558 {
2559 	struct ice_switch_info *sw = hw->switch_info;
2560 	struct ice_fltr_list_entry *list_itr, *tmp;
2561 	struct list_head vsi_list_head;
2562 	struct list_head *vlan_head;
2563 	struct mutex *vlan_lock; /* Lock to protect filter rule list */
2564 	enum ice_status status;
2565 	u16 vlan_id;
2566 
2567 	INIT_LIST_HEAD(&vsi_list_head);
2568 	vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2569 	vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
2570 	mutex_lock(vlan_lock);
2571 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
2572 					  &vsi_list_head);
2573 	mutex_unlock(vlan_lock);
2574 	if (status)
2575 		goto free_fltr_list;
2576 
2577 	list_for_each_entry(list_itr, &vsi_list_head, list_entry) {
2578 		vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
2579 		if (rm_vlan_promisc)
2580 			status = ice_clear_vsi_promisc(hw, vsi_handle,
2581 						       promisc_mask, vlan_id);
2582 		else
2583 			status = ice_set_vsi_promisc(hw, vsi_handle,
2584 						     promisc_mask, vlan_id);
2585 		if (status)
2586 			break;
2587 	}
2588 
2589 free_fltr_list:
2590 	list_for_each_entry_safe(list_itr, tmp, &vsi_list_head, list_entry) {
2591 		list_del(&list_itr->list_entry);
2592 		devm_kfree(ice_hw_to_dev(hw), list_itr);
2593 	}
2594 	return status;
2595 }
2596 
2597 /**
2598  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
2599  * @hw: pointer to the hardware structure
2600  * @vsi_handle: VSI handle to remove filters from
2601  * @lkup: switch rule filter lookup type
2602  */
2603 static void
2604 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
2605 			 enum ice_sw_lkup_type lkup)
2606 {
2607 	struct ice_switch_info *sw = hw->switch_info;
2608 	struct ice_fltr_list_entry *fm_entry;
2609 	struct list_head remove_list_head;
2610 	struct list_head *rule_head;
2611 	struct ice_fltr_list_entry *tmp;
2612 	struct mutex *rule_lock;	/* Lock to protect filter rule list */
2613 	enum ice_status status;
2614 
2615 	INIT_LIST_HEAD(&remove_list_head);
2616 	rule_lock = &sw->recp_list[lkup].filt_rule_lock;
2617 	rule_head = &sw->recp_list[lkup].filt_rules;
2618 	mutex_lock(rule_lock);
2619 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
2620 					  &remove_list_head);
2621 	mutex_unlock(rule_lock);
2622 	if (status)
2623 		return;
2624 
2625 	switch (lkup) {
2626 	case ICE_SW_LKUP_MAC:
2627 		ice_remove_mac(hw, &remove_list_head);
2628 		break;
2629 	case ICE_SW_LKUP_VLAN:
2630 		ice_remove_vlan(hw, &remove_list_head);
2631 		break;
2632 	case ICE_SW_LKUP_PROMISC:
2633 	case ICE_SW_LKUP_PROMISC_VLAN:
2634 		ice_remove_promisc(hw, lkup, &remove_list_head);
2635 		break;
2636 	case ICE_SW_LKUP_MAC_VLAN:
2637 	case ICE_SW_LKUP_ETHERTYPE:
2638 	case ICE_SW_LKUP_ETHERTYPE_MAC:
2639 	case ICE_SW_LKUP_DFLT:
2640 	case ICE_SW_LKUP_LAST:
2641 	default:
2642 		ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
2643 		break;
2644 	}
2645 
2646 	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
2647 		list_del(&fm_entry->list_entry);
2648 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
2649 	}
2650 }
2651 
2652 /**
2653  * ice_remove_vsi_fltr - Remove all filters for a VSI
2654  * @hw: pointer to the hardware structure
2655  * @vsi_handle: VSI handle to remove filters from
2656  */
2657 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
2658 {
2659 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
2660 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
2661 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
2662 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
2663 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
2664 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
2665 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
2666 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
2667 }
2668 
2669 /**
2670  * ice_alloc_res_cntr - allocating resource counter
2671  * @hw: pointer to the hardware structure
2672  * @type: type of resource
2673  * @alloc_shared: if set it is shared else dedicated
2674  * @num_items: number of entries requested for FD resource type
2675  * @counter_id: counter index returned by AQ call
2676  */
2677 enum ice_status
2678 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
2679 		   u16 *counter_id)
2680 {
2681 	struct ice_aqc_alloc_free_res_elem *buf;
2682 	enum ice_status status;
2683 	u16 buf_len;
2684 
2685 	/* Allocate resource */
2686 	buf_len = struct_size(buf, elem, 1);
2687 	buf = kzalloc(buf_len, GFP_KERNEL);
2688 	if (!buf)
2689 		return ICE_ERR_NO_MEMORY;
2690 
2691 	buf->num_elems = cpu_to_le16(num_items);
2692 	buf->res_type = cpu_to_le16(((type << ICE_AQC_RES_TYPE_S) &
2693 				      ICE_AQC_RES_TYPE_M) | alloc_shared);
2694 
2695 	status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
2696 				       ice_aqc_opc_alloc_res, NULL);
2697 	if (status)
2698 		goto exit;
2699 
2700 	*counter_id = le16_to_cpu(buf->elem[0].e.sw_resp);
2701 
2702 exit:
2703 	kfree(buf);
2704 	return status;
2705 }
2706 
2707 /**
2708  * ice_free_res_cntr - free resource counter
2709  * @hw: pointer to the hardware structure
2710  * @type: type of resource
2711  * @alloc_shared: if set it is shared else dedicated
2712  * @num_items: number of entries to be freed for FD resource type
2713  * @counter_id: counter ID resource which needs to be freed
2714  */
2715 enum ice_status
2716 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
2717 		  u16 counter_id)
2718 {
2719 	struct ice_aqc_alloc_free_res_elem *buf;
2720 	enum ice_status status;
2721 	u16 buf_len;
2722 
2723 	/* Free resource */
2724 	buf_len = struct_size(buf, elem, 1);
2725 	buf = kzalloc(buf_len, GFP_KERNEL);
2726 	if (!buf)
2727 		return ICE_ERR_NO_MEMORY;
2728 
2729 	buf->num_elems = cpu_to_le16(num_items);
2730 	buf->res_type = cpu_to_le16(((type << ICE_AQC_RES_TYPE_S) &
2731 				      ICE_AQC_RES_TYPE_M) | alloc_shared);
2732 	buf->elem[0].e.sw_resp = cpu_to_le16(counter_id);
2733 
2734 	status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
2735 				       ice_aqc_opc_free_res, NULL);
2736 	if (status)
2737 		ice_debug(hw, ICE_DBG_SW,
2738 			  "counter resource could not be freed\n");
2739 
2740 	kfree(buf);
2741 	return status;
2742 }
2743 
2744 /**
2745  * ice_replay_vsi_fltr - Replay filters for requested VSI
2746  * @hw: pointer to the hardware structure
2747  * @vsi_handle: driver VSI handle
2748  * @recp_id: Recipe ID for which rules need to be replayed
2749  * @list_head: list for which filters need to be replayed
2750  *
2751  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
2752  * It is required to pass valid VSI handle.
2753  */
2754 static enum ice_status
2755 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
2756 		    struct list_head *list_head)
2757 {
2758 	struct ice_fltr_mgmt_list_entry *itr;
2759 	enum ice_status status = 0;
2760 	u16 hw_vsi_id;
2761 
2762 	if (list_empty(list_head))
2763 		return status;
2764 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2765 
2766 	list_for_each_entry(itr, list_head, list_entry) {
2767 		struct ice_fltr_list_entry f_entry;
2768 
2769 		f_entry.fltr_info = itr->fltr_info;
2770 		if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
2771 		    itr->fltr_info.vsi_handle == vsi_handle) {
2772 			/* update the src in case it is VSI num */
2773 			if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2774 				f_entry.fltr_info.src = hw_vsi_id;
2775 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
2776 			if (status)
2777 				goto end;
2778 			continue;
2779 		}
2780 		if (!itr->vsi_list_info ||
2781 		    !test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
2782 			continue;
2783 		/* Clearing it so that the logic can add it back */
2784 		clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
2785 		f_entry.fltr_info.vsi_handle = vsi_handle;
2786 		f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
2787 		/* update the src in case it is VSI num */
2788 		if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2789 			f_entry.fltr_info.src = hw_vsi_id;
2790 		if (recp_id == ICE_SW_LKUP_VLAN)
2791 			status = ice_add_vlan_internal(hw, &f_entry);
2792 		else
2793 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
2794 		if (status)
2795 			goto end;
2796 	}
2797 end:
2798 	return status;
2799 }
2800 
2801 /**
2802  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
2803  * @hw: pointer to the hardware structure
2804  * @vsi_handle: driver VSI handle
2805  *
2806  * Replays filters for requested VSI via vsi_handle.
2807  */
2808 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
2809 {
2810 	struct ice_switch_info *sw = hw->switch_info;
2811 	enum ice_status status = 0;
2812 	u8 i;
2813 
2814 	for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2815 		struct list_head *head;
2816 
2817 		head = &sw->recp_list[i].filt_replay_rules;
2818 		status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
2819 		if (status)
2820 			return status;
2821 	}
2822 	return status;
2823 }
2824 
2825 /**
2826  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
2827  * @hw: pointer to the HW struct
2828  *
2829  * Deletes the filter replay rules.
2830  */
2831 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
2832 {
2833 	struct ice_switch_info *sw = hw->switch_info;
2834 	u8 i;
2835 
2836 	if (!sw)
2837 		return;
2838 
2839 	for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2840 		if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
2841 			struct list_head *l_head;
2842 
2843 			l_head = &sw->recp_list[i].filt_replay_rules;
2844 			ice_rem_sw_rule_info(hw, l_head);
2845 		}
2846 	}
2847 }
2848