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