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 ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle,
326 			     struct ice_vsi_ctx *vsi)
327 {
328 	hw->vsi_ctx[vsi_handle] = vsi;
329 }
330 
331 /**
332  * ice_clear_vsi_ctx - clear the VSI context entry
333  * @hw: pointer to the hw struct
334  * @vsi_handle: VSI handle
335  *
336  * clear the VSI context entry
337  */
338 static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
339 {
340 	struct ice_vsi_ctx *vsi;
341 
342 	vsi = ice_get_vsi_ctx(hw, vsi_handle);
343 	if (vsi) {
344 		devm_kfree(ice_hw_to_dev(hw), vsi);
345 		hw->vsi_ctx[vsi_handle] = NULL;
346 	}
347 }
348 
349 /**
350  * ice_clear_all_vsi_ctx - clear all the VSI context entries
351  * @hw: pointer to the hw struct
352  */
353 void ice_clear_all_vsi_ctx(struct ice_hw *hw)
354 {
355 	u16 i;
356 
357 	for (i = 0; i < ICE_MAX_VSI; i++)
358 		ice_clear_vsi_ctx(hw, i);
359 }
360 
361 /**
362  * ice_add_vsi - add VSI context to the hardware and VSI handle list
363  * @hw: pointer to the hw struct
364  * @vsi_handle: unique VSI handle provided by drivers
365  * @vsi_ctx: pointer to a VSI context struct
366  * @cd: pointer to command details structure or NULL
367  *
368  * Add a VSI context to the hardware also add it into the VSI handle list.
369  * If this function gets called after reset for existing VSIs then update
370  * with the new HW VSI number in the corresponding VSI handle list entry.
371  */
372 enum ice_status
373 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
374 	    struct ice_sq_cd *cd)
375 {
376 	struct ice_vsi_ctx *tmp_vsi_ctx;
377 	enum ice_status status;
378 
379 	if (vsi_handle >= ICE_MAX_VSI)
380 		return ICE_ERR_PARAM;
381 	status = ice_aq_add_vsi(hw, vsi_ctx, cd);
382 	if (status)
383 		return status;
384 	tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
385 	if (!tmp_vsi_ctx) {
386 		/* Create a new vsi context */
387 		tmp_vsi_ctx = devm_kzalloc(ice_hw_to_dev(hw),
388 					   sizeof(*tmp_vsi_ctx), GFP_KERNEL);
389 		if (!tmp_vsi_ctx) {
390 			ice_aq_free_vsi(hw, vsi_ctx, false, cd);
391 			return ICE_ERR_NO_MEMORY;
392 		}
393 		*tmp_vsi_ctx = *vsi_ctx;
394 		ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
395 	} else {
396 		/* update with new HW VSI num */
397 		if (tmp_vsi_ctx->vsi_num != vsi_ctx->vsi_num)
398 			tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
399 	}
400 
401 	return status;
402 }
403 
404 /**
405  * ice_free_vsi- free VSI context from hardware and VSI handle list
406  * @hw: pointer to the hw struct
407  * @vsi_handle: unique VSI handle
408  * @vsi_ctx: pointer to a VSI context struct
409  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
410  * @cd: pointer to command details structure or NULL
411  *
412  * Free VSI context info from hardware as well as from VSI handle list
413  */
414 enum ice_status
415 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
416 	     bool keep_vsi_alloc, struct ice_sq_cd *cd)
417 {
418 	enum ice_status status;
419 
420 	if (!ice_is_vsi_valid(hw, vsi_handle))
421 		return ICE_ERR_PARAM;
422 	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
423 	status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
424 	if (!status)
425 		ice_clear_vsi_ctx(hw, vsi_handle);
426 	return status;
427 }
428 
429 /**
430  * ice_update_vsi
431  * @hw: pointer to the hw struct
432  * @vsi_handle: unique VSI handle
433  * @vsi_ctx: pointer to a VSI context struct
434  * @cd: pointer to command details structure or NULL
435  *
436  * Update VSI context in the hardware
437  */
438 enum ice_status
439 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
440 	       struct ice_sq_cd *cd)
441 {
442 	if (!ice_is_vsi_valid(hw, vsi_handle))
443 		return ICE_ERR_PARAM;
444 	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
445 	return ice_aq_update_vsi(hw, vsi_ctx, cd);
446 }
447 
448 /**
449  * ice_aq_alloc_free_vsi_list
450  * @hw: pointer to the hw struct
451  * @vsi_list_id: VSI list id returned or used for lookup
452  * @lkup_type: switch rule filter lookup type
453  * @opc: switch rules population command type - pass in the command opcode
454  *
455  * allocates or free a VSI list resource
456  */
457 static enum ice_status
458 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
459 			   enum ice_sw_lkup_type lkup_type,
460 			   enum ice_adminq_opc opc)
461 {
462 	struct ice_aqc_alloc_free_res_elem *sw_buf;
463 	struct ice_aqc_res_elem *vsi_ele;
464 	enum ice_status status;
465 	u16 buf_len;
466 
467 	buf_len = sizeof(*sw_buf);
468 	sw_buf = devm_kzalloc(ice_hw_to_dev(hw), buf_len, GFP_KERNEL);
469 	if (!sw_buf)
470 		return ICE_ERR_NO_MEMORY;
471 	sw_buf->num_elems = cpu_to_le16(1);
472 
473 	if (lkup_type == ICE_SW_LKUP_MAC ||
474 	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
475 	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
476 	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
477 	    lkup_type == ICE_SW_LKUP_PROMISC ||
478 	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN) {
479 		sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
480 	} else if (lkup_type == ICE_SW_LKUP_VLAN) {
481 		sw_buf->res_type =
482 			cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
483 	} else {
484 		status = ICE_ERR_PARAM;
485 		goto ice_aq_alloc_free_vsi_list_exit;
486 	}
487 
488 	if (opc == ice_aqc_opc_free_res)
489 		sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
490 
491 	status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
492 	if (status)
493 		goto ice_aq_alloc_free_vsi_list_exit;
494 
495 	if (opc == ice_aqc_opc_alloc_res) {
496 		vsi_ele = &sw_buf->elem[0];
497 		*vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp);
498 	}
499 
500 ice_aq_alloc_free_vsi_list_exit:
501 	devm_kfree(ice_hw_to_dev(hw), sw_buf);
502 	return status;
503 }
504 
505 /**
506  * ice_aq_sw_rules - add/update/remove switch rules
507  * @hw: pointer to the hw struct
508  * @rule_list: pointer to switch rule population list
509  * @rule_list_sz: total size of the rule list in bytes
510  * @num_rules: number of switch rules in the rule_list
511  * @opc: switch rules population command type - pass in the command opcode
512  * @cd: pointer to command details structure or NULL
513  *
514  * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
515  */
516 static enum ice_status
517 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
518 		u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
519 {
520 	struct ice_aq_desc desc;
521 
522 	if (opc != ice_aqc_opc_add_sw_rules &&
523 	    opc != ice_aqc_opc_update_sw_rules &&
524 	    opc != ice_aqc_opc_remove_sw_rules)
525 		return ICE_ERR_PARAM;
526 
527 	ice_fill_dflt_direct_cmd_desc(&desc, opc);
528 
529 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
530 	desc.params.sw_rules.num_rules_fltr_entry_index =
531 		cpu_to_le16(num_rules);
532 	return ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
533 }
534 
535 /* ice_init_port_info - Initialize port_info with switch configuration data
536  * @pi: pointer to port_info
537  * @vsi_port_num: VSI number or port number
538  * @type: Type of switch element (port or VSI)
539  * @swid: switch ID of the switch the element is attached to
540  * @pf_vf_num: PF or VF number
541  * @is_vf: true if the element is a VF, false otherwise
542  */
543 static void
544 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
545 		   u16 swid, u16 pf_vf_num, bool is_vf)
546 {
547 	switch (type) {
548 	case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
549 		pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
550 		pi->sw_id = swid;
551 		pi->pf_vf_num = pf_vf_num;
552 		pi->is_vf = is_vf;
553 		pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
554 		pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
555 		break;
556 	default:
557 		ice_debug(pi->hw, ICE_DBG_SW,
558 			  "incorrect VSI/port type received\n");
559 		break;
560 	}
561 }
562 
563 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
564  * @hw: pointer to the hardware structure
565  */
566 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
567 {
568 	struct ice_aqc_get_sw_cfg_resp *rbuf;
569 	enum ice_status status;
570 	u16 req_desc = 0;
571 	u16 num_elems;
572 	u16 i;
573 
574 	rbuf = devm_kzalloc(ice_hw_to_dev(hw), ICE_SW_CFG_MAX_BUF_LEN,
575 			    GFP_KERNEL);
576 
577 	if (!rbuf)
578 		return ICE_ERR_NO_MEMORY;
579 
580 	/* Multiple calls to ice_aq_get_sw_cfg may be required
581 	 * to get all the switch configuration information. The need
582 	 * for additional calls is indicated by ice_aq_get_sw_cfg
583 	 * writing a non-zero value in req_desc
584 	 */
585 	do {
586 		status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
587 					   &req_desc, &num_elems, NULL);
588 
589 		if (status)
590 			break;
591 
592 		for (i = 0; i < num_elems; i++) {
593 			struct ice_aqc_get_sw_cfg_resp_elem *ele;
594 			u16 pf_vf_num, swid, vsi_port_num;
595 			bool is_vf = false;
596 			u8 type;
597 
598 			ele = rbuf[i].elements;
599 			vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
600 				ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
601 
602 			pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
603 				ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
604 
605 			swid = le16_to_cpu(ele->swid);
606 
607 			if (le16_to_cpu(ele->pf_vf_num) &
608 			    ICE_AQC_GET_SW_CONF_RESP_IS_VF)
609 				is_vf = true;
610 
611 			type = le16_to_cpu(ele->vsi_port_num) >>
612 				ICE_AQC_GET_SW_CONF_RESP_TYPE_S;
613 
614 			if (type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
615 				/* FW VSI is not needed. Just continue. */
616 				continue;
617 			}
618 
619 			ice_init_port_info(hw->port_info, vsi_port_num,
620 					   type, swid, pf_vf_num, is_vf);
621 		}
622 	} while (req_desc && !status);
623 
624 	devm_kfree(ice_hw_to_dev(hw), (void *)rbuf);
625 	return status;
626 }
627 
628 /**
629  * ice_fill_sw_info - Helper function to populate lb_en and lan_en
630  * @hw: pointer to the hardware structure
631  * @fi: filter info structure to fill/update
632  *
633  * This helper function populates the lb_en and lan_en elements of the provided
634  * ice_fltr_info struct using the switch's type and characteristics of the
635  * switch rule being configured.
636  */
637 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
638 {
639 	fi->lb_en = false;
640 	fi->lan_en = false;
641 	if ((fi->flag & ICE_FLTR_TX) &&
642 	    (fi->fltr_act == ICE_FWD_TO_VSI ||
643 	     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
644 	     fi->fltr_act == ICE_FWD_TO_Q ||
645 	     fi->fltr_act == ICE_FWD_TO_QGRP)) {
646 		fi->lb_en = true;
647 		/* Do not set lan_en to TRUE if
648 		 * 1. The switch is a VEB AND
649 		 * 2
650 		 * 2.1 The lookup is MAC with unicast addr for MAC, OR
651 		 * 2.2 The lookup is MAC_VLAN with unicast addr for MAC
652 		 *
653 		 * In all other cases, the LAN enable has to be set to true.
654 		 */
655 		if (!(hw->evb_veb &&
656 		      ((fi->lkup_type == ICE_SW_LKUP_MAC &&
657 			is_unicast_ether_addr(fi->l_data.mac.mac_addr)) ||
658 		       (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
659 			is_unicast_ether_addr(fi->l_data.mac_vlan.mac_addr)))))
660 			fi->lan_en = true;
661 	}
662 }
663 
664 /**
665  * ice_fill_sw_rule - Helper function to fill switch rule structure
666  * @hw: pointer to the hardware structure
667  * @f_info: entry containing packet forwarding information
668  * @s_rule: switch rule structure to be filled in based on mac_entry
669  * @opc: switch rules population command type - pass in the command opcode
670  */
671 static void
672 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
673 		 struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
674 {
675 	u16 vlan_id = ICE_MAX_VLAN_ID + 1;
676 	void *daddr = NULL;
677 	u16 eth_hdr_sz;
678 	u8 *eth_hdr;
679 	u32 act = 0;
680 	__be16 *off;
681 	u8 q_rgn;
682 
683 	if (opc == ice_aqc_opc_remove_sw_rules) {
684 		s_rule->pdata.lkup_tx_rx.act = 0;
685 		s_rule->pdata.lkup_tx_rx.index =
686 			cpu_to_le16(f_info->fltr_rule_id);
687 		s_rule->pdata.lkup_tx_rx.hdr_len = 0;
688 		return;
689 	}
690 
691 	eth_hdr_sz = sizeof(dummy_eth_header);
692 	eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
693 
694 	/* initialize the ether header with a dummy header */
695 	memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz);
696 	ice_fill_sw_info(hw, f_info);
697 
698 	switch (f_info->fltr_act) {
699 	case ICE_FWD_TO_VSI:
700 		act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
701 			ICE_SINGLE_ACT_VSI_ID_M;
702 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
703 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
704 				ICE_SINGLE_ACT_VALID_BIT;
705 		break;
706 	case ICE_FWD_TO_VSI_LIST:
707 		act |= ICE_SINGLE_ACT_VSI_LIST;
708 		act |= (f_info->fwd_id.vsi_list_id <<
709 			ICE_SINGLE_ACT_VSI_LIST_ID_S) &
710 			ICE_SINGLE_ACT_VSI_LIST_ID_M;
711 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
712 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
713 				ICE_SINGLE_ACT_VALID_BIT;
714 		break;
715 	case ICE_FWD_TO_Q:
716 		act |= ICE_SINGLE_ACT_TO_Q;
717 		act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
718 			ICE_SINGLE_ACT_Q_INDEX_M;
719 		break;
720 	case ICE_DROP_PACKET:
721 		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
722 			ICE_SINGLE_ACT_VALID_BIT;
723 		break;
724 	case ICE_FWD_TO_QGRP:
725 		q_rgn = f_info->qgrp_size > 0 ?
726 			(u8)ilog2(f_info->qgrp_size) : 0;
727 		act |= ICE_SINGLE_ACT_TO_Q;
728 		act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
729 			ICE_SINGLE_ACT_Q_INDEX_M;
730 		act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
731 			ICE_SINGLE_ACT_Q_REGION_M;
732 		break;
733 	default:
734 		return;
735 	}
736 
737 	if (f_info->lb_en)
738 		act |= ICE_SINGLE_ACT_LB_ENABLE;
739 	if (f_info->lan_en)
740 		act |= ICE_SINGLE_ACT_LAN_ENABLE;
741 
742 	switch (f_info->lkup_type) {
743 	case ICE_SW_LKUP_MAC:
744 		daddr = f_info->l_data.mac.mac_addr;
745 		break;
746 	case ICE_SW_LKUP_VLAN:
747 		vlan_id = f_info->l_data.vlan.vlan_id;
748 		if (f_info->fltr_act == ICE_FWD_TO_VSI ||
749 		    f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
750 			act |= ICE_SINGLE_ACT_PRUNE;
751 			act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
752 		}
753 		break;
754 	case ICE_SW_LKUP_ETHERTYPE_MAC:
755 		daddr = f_info->l_data.ethertype_mac.mac_addr;
756 		/* fall-through */
757 	case ICE_SW_LKUP_ETHERTYPE:
758 		off = (__be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
759 		*off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
760 		break;
761 	case ICE_SW_LKUP_MAC_VLAN:
762 		daddr = f_info->l_data.mac_vlan.mac_addr;
763 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
764 		break;
765 	case ICE_SW_LKUP_PROMISC_VLAN:
766 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
767 		/* fall-through */
768 	case ICE_SW_LKUP_PROMISC:
769 		daddr = f_info->l_data.mac_vlan.mac_addr;
770 		break;
771 	default:
772 		break;
773 	}
774 
775 	s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
776 		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
777 		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
778 
779 	/* Recipe set depending on lookup type */
780 	s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(f_info->lkup_type);
781 	s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(f_info->src);
782 	s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);
783 
784 	if (daddr)
785 		ether_addr_copy(eth_hdr + ICE_ETH_DA_OFFSET, daddr);
786 
787 	if (!(vlan_id > ICE_MAX_VLAN_ID)) {
788 		off = (__be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
789 		*off = cpu_to_be16(vlan_id);
790 	}
791 
792 	/* Create the switch rule with the final dummy Ethernet header */
793 	if (opc != ice_aqc_opc_update_sw_rules)
794 		s_rule->pdata.lkup_tx_rx.hdr_len = cpu_to_le16(eth_hdr_sz);
795 }
796 
797 /**
798  * ice_add_marker_act
799  * @hw: pointer to the hardware structure
800  * @m_ent: the management entry for which sw marker needs to be added
801  * @sw_marker: sw marker to tag the Rx descriptor with
802  * @l_id: large action resource id
803  *
804  * Create a large action to hold software marker and update the switch rule
805  * entry pointed by m_ent with newly created large action
806  */
807 static enum ice_status
808 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
809 		   u16 sw_marker, u16 l_id)
810 {
811 	struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
812 	/* For software marker we need 3 large actions
813 	 * 1. FWD action: FWD TO VSI or VSI LIST
814 	 * 2. GENERIC VALUE action to hold the profile id
815 	 * 3. GENERIC VALUE action to hold the software marker id
816 	 */
817 	const u16 num_lg_acts = 3;
818 	enum ice_status status;
819 	u16 lg_act_size;
820 	u16 rules_size;
821 	u32 act;
822 	u16 id;
823 
824 	if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
825 		return ICE_ERR_PARAM;
826 
827 	/* Create two back-to-back switch rules and submit them to the HW using
828 	 * one memory buffer:
829 	 *    1. Large Action
830 	 *    2. Look up Tx Rx
831 	 */
832 	lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
833 	rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
834 	lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
835 	if (!lg_act)
836 		return ICE_ERR_NO_MEMORY;
837 
838 	rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
839 
840 	/* Fill in the first switch rule i.e. large action */
841 	lg_act->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
842 	lg_act->pdata.lg_act.index = cpu_to_le16(l_id);
843 	lg_act->pdata.lg_act.size = cpu_to_le16(num_lg_acts);
844 
845 	/* First action VSI forwarding or VSI list forwarding depending on how
846 	 * many VSIs
847 	 */
848 	id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
849 		m_ent->fltr_info.fwd_id.hw_vsi_id;
850 
851 	act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
852 	act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
853 		ICE_LG_ACT_VSI_LIST_ID_M;
854 	if (m_ent->vsi_count > 1)
855 		act |= ICE_LG_ACT_VSI_LIST;
856 	lg_act->pdata.lg_act.act[0] = cpu_to_le32(act);
857 
858 	/* Second action descriptor type */
859 	act = ICE_LG_ACT_GENERIC;
860 
861 	act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
862 	lg_act->pdata.lg_act.act[1] = cpu_to_le32(act);
863 
864 	act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
865 	       ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
866 
867 	/* Third action Marker value */
868 	act |= ICE_LG_ACT_GENERIC;
869 	act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
870 		ICE_LG_ACT_GENERIC_VALUE_M;
871 
872 	lg_act->pdata.lg_act.act[2] = cpu_to_le32(act);
873 
874 	/* call the fill switch rule to fill the lookup Tx Rx structure */
875 	ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
876 			 ice_aqc_opc_update_sw_rules);
877 
878 	/* Update the action to point to the large action id */
879 	rx_tx->pdata.lkup_tx_rx.act =
880 		cpu_to_le32(ICE_SINGLE_ACT_PTR |
881 			    ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
882 			     ICE_SINGLE_ACT_PTR_VAL_M));
883 
884 	/* Use the filter rule id of the previously created rule with single
885 	 * act. Once the update happens, hardware will treat this as large
886 	 * action
887 	 */
888 	rx_tx->pdata.lkup_tx_rx.index =
889 		cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
890 
891 	status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
892 				 ice_aqc_opc_update_sw_rules, NULL);
893 	if (!status) {
894 		m_ent->lg_act_idx = l_id;
895 		m_ent->sw_marker_id = sw_marker;
896 	}
897 
898 	devm_kfree(ice_hw_to_dev(hw), lg_act);
899 	return status;
900 }
901 
902 /**
903  * ice_create_vsi_list_map
904  * @hw: pointer to the hardware structure
905  * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
906  * @num_vsi: number of VSI handles in the array
907  * @vsi_list_id: VSI list id generated as part of allocate resource
908  *
909  * Helper function to create a new entry of VSI list id to VSI mapping
910  * using the given VSI list id
911  */
912 static struct ice_vsi_list_map_info *
913 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
914 			u16 vsi_list_id)
915 {
916 	struct ice_switch_info *sw = hw->switch_info;
917 	struct ice_vsi_list_map_info *v_map;
918 	int i;
919 
920 	v_map = devm_kcalloc(ice_hw_to_dev(hw), 1, sizeof(*v_map), GFP_KERNEL);
921 	if (!v_map)
922 		return NULL;
923 
924 	v_map->vsi_list_id = vsi_list_id;
925 	v_map->ref_cnt = 1;
926 	for (i = 0; i < num_vsi; i++)
927 		set_bit(vsi_handle_arr[i], v_map->vsi_map);
928 
929 	list_add(&v_map->list_entry, &sw->vsi_list_map_head);
930 	return v_map;
931 }
932 
933 /**
934  * ice_update_vsi_list_rule
935  * @hw: pointer to the hardware structure
936  * @vsi_handle_arr: array of VSI handles to form a VSI list
937  * @num_vsi: number of VSI handles in the array
938  * @vsi_list_id: VSI list id generated as part of allocate resource
939  * @remove: Boolean value to indicate if this is a remove action
940  * @opc: switch rules population command type - pass in the command opcode
941  * @lkup_type: lookup type of the filter
942  *
943  * Call AQ command to add a new switch rule or update existing switch rule
944  * using the given VSI list id
945  */
946 static enum ice_status
947 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
948 			 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
949 			 enum ice_sw_lkup_type lkup_type)
950 {
951 	struct ice_aqc_sw_rules_elem *s_rule;
952 	enum ice_status status;
953 	u16 s_rule_size;
954 	u16 type;
955 	int i;
956 
957 	if (!num_vsi)
958 		return ICE_ERR_PARAM;
959 
960 	if (lkup_type == ICE_SW_LKUP_MAC ||
961 	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
962 	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
963 	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
964 	    lkup_type == ICE_SW_LKUP_PROMISC ||
965 	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
966 		type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
967 				ICE_AQC_SW_RULES_T_VSI_LIST_SET;
968 	else if (lkup_type == ICE_SW_LKUP_VLAN)
969 		type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
970 				ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
971 	else
972 		return ICE_ERR_PARAM;
973 
974 	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
975 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
976 	if (!s_rule)
977 		return ICE_ERR_NO_MEMORY;
978 	for (i = 0; i < num_vsi; i++) {
979 		if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
980 			status = ICE_ERR_PARAM;
981 			goto exit;
982 		}
983 		/* AQ call requires hw_vsi_id(s) */
984 		s_rule->pdata.vsi_list.vsi[i] =
985 			cpu_to_le16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
986 	}
987 
988 	s_rule->type = cpu_to_le16(type);
989 	s_rule->pdata.vsi_list.number_vsi = cpu_to_le16(num_vsi);
990 	s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
991 
992 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
993 
994 exit:
995 	devm_kfree(ice_hw_to_dev(hw), s_rule);
996 	return status;
997 }
998 
999 /**
1000  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
1001  * @hw: pointer to the hw struct
1002  * @vsi_handle_arr: array of VSI handles to form a VSI list
1003  * @num_vsi: number of VSI handles in the array
1004  * @vsi_list_id: stores the ID of the VSI list to be created
1005  * @lkup_type: switch rule filter's lookup type
1006  */
1007 static enum ice_status
1008 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1009 			 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1010 {
1011 	enum ice_status status;
1012 
1013 	status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1014 					    ice_aqc_opc_alloc_res);
1015 	if (status)
1016 		return status;
1017 
1018 	/* Update the newly created VSI list to include the specified VSIs */
1019 	return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1020 					*vsi_list_id, false,
1021 					ice_aqc_opc_add_sw_rules, lkup_type);
1022 }
1023 
1024 /**
1025  * ice_create_pkt_fwd_rule
1026  * @hw: pointer to the hardware structure
1027  * @f_entry: entry containing packet forwarding information
1028  *
1029  * Create switch rule with given filter information and add an entry
1030  * to the corresponding filter management list to track this switch rule
1031  * and VSI mapping
1032  */
1033 static enum ice_status
1034 ice_create_pkt_fwd_rule(struct ice_hw *hw,
1035 			struct ice_fltr_list_entry *f_entry)
1036 {
1037 	struct ice_fltr_mgmt_list_entry *fm_entry;
1038 	struct ice_aqc_sw_rules_elem *s_rule;
1039 	enum ice_sw_lkup_type l_type;
1040 	struct ice_sw_recipe *recp;
1041 	enum ice_status status;
1042 
1043 	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1044 			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1045 	if (!s_rule)
1046 		return ICE_ERR_NO_MEMORY;
1047 	fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
1048 				GFP_KERNEL);
1049 	if (!fm_entry) {
1050 		status = ICE_ERR_NO_MEMORY;
1051 		goto ice_create_pkt_fwd_rule_exit;
1052 	}
1053 
1054 	fm_entry->fltr_info = f_entry->fltr_info;
1055 
1056 	/* Initialize all the fields for the management entry */
1057 	fm_entry->vsi_count = 1;
1058 	fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1059 	fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1060 	fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1061 
1062 	ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1063 			 ice_aqc_opc_add_sw_rules);
1064 
1065 	status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1066 				 ice_aqc_opc_add_sw_rules, NULL);
1067 	if (status) {
1068 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
1069 		goto ice_create_pkt_fwd_rule_exit;
1070 	}
1071 
1072 	f_entry->fltr_info.fltr_rule_id =
1073 		le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1074 	fm_entry->fltr_info.fltr_rule_id =
1075 		le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1076 
1077 	/* The book keeping entries will get removed when base driver
1078 	 * calls remove filter AQ command
1079 	 */
1080 	l_type = fm_entry->fltr_info.lkup_type;
1081 	recp = &hw->switch_info->recp_list[l_type];
1082 	list_add(&fm_entry->list_entry, &recp->filt_rules);
1083 
1084 ice_create_pkt_fwd_rule_exit:
1085 	devm_kfree(ice_hw_to_dev(hw), s_rule);
1086 	return status;
1087 }
1088 
1089 /**
1090  * ice_update_pkt_fwd_rule
1091  * @hw: pointer to the hardware structure
1092  * @f_info: filter information for switch rule
1093  *
1094  * Call AQ command to update a previously created switch rule with a
1095  * VSI list id
1096  */
1097 static enum ice_status
1098 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1099 {
1100 	struct ice_aqc_sw_rules_elem *s_rule;
1101 	enum ice_status status;
1102 
1103 	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1104 			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1105 	if (!s_rule)
1106 		return ICE_ERR_NO_MEMORY;
1107 
1108 	ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
1109 
1110 	s_rule->pdata.lkup_tx_rx.index = cpu_to_le16(f_info->fltr_rule_id);
1111 
1112 	/* Update switch rule with new rule set to forward VSI list */
1113 	status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1114 				 ice_aqc_opc_update_sw_rules, NULL);
1115 
1116 	devm_kfree(ice_hw_to_dev(hw), s_rule);
1117 	return status;
1118 }
1119 
1120 /**
1121  * ice_update_sw_rule_bridge_mode
1122  * @hw: pointer to the hw struct
1123  *
1124  * Updates unicast switch filter rules based on VEB/VEPA mode
1125  */
1126 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
1127 {
1128 	struct ice_switch_info *sw = hw->switch_info;
1129 	struct ice_fltr_mgmt_list_entry *fm_entry;
1130 	enum ice_status status = 0;
1131 	struct list_head *rule_head;
1132 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1133 
1134 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1135 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1136 
1137 	mutex_lock(rule_lock);
1138 	list_for_each_entry(fm_entry, rule_head, list_entry) {
1139 		struct ice_fltr_info *fi = &fm_entry->fltr_info;
1140 		u8 *addr = fi->l_data.mac.mac_addr;
1141 
1142 		/* Update unicast Tx rules to reflect the selected
1143 		 * VEB/VEPA mode
1144 		 */
1145 		if ((fi->flag & ICE_FLTR_TX) && is_unicast_ether_addr(addr) &&
1146 		    (fi->fltr_act == ICE_FWD_TO_VSI ||
1147 		     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1148 		     fi->fltr_act == ICE_FWD_TO_Q ||
1149 		     fi->fltr_act == ICE_FWD_TO_QGRP)) {
1150 			status = ice_update_pkt_fwd_rule(hw, fi);
1151 			if (status)
1152 				break;
1153 		}
1154 	}
1155 
1156 	mutex_unlock(rule_lock);
1157 
1158 	return status;
1159 }
1160 
1161 /**
1162  * ice_add_update_vsi_list
1163  * @hw: pointer to the hardware structure
1164  * @m_entry: pointer to current filter management list entry
1165  * @cur_fltr: filter information from the book keeping entry
1166  * @new_fltr: filter information with the new VSI to be added
1167  *
1168  * Call AQ command to add or update previously created VSI list with new VSI.
1169  *
1170  * Helper function to do book keeping associated with adding filter information
1171  * The algorithm to do the book keeping is described below :
1172  * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
1173  *	if only one VSI has been added till now
1174  *		Allocate a new VSI list and add two VSIs
1175  *		to this list using switch rule command
1176  *		Update the previously created switch rule with the
1177  *		newly created VSI list id
1178  *	if a VSI list was previously created
1179  *		Add the new VSI to the previously created VSI list set
1180  *		using the update switch rule command
1181  */
1182 static enum ice_status
1183 ice_add_update_vsi_list(struct ice_hw *hw,
1184 			struct ice_fltr_mgmt_list_entry *m_entry,
1185 			struct ice_fltr_info *cur_fltr,
1186 			struct ice_fltr_info *new_fltr)
1187 {
1188 	enum ice_status status = 0;
1189 	u16 vsi_list_id = 0;
1190 
1191 	if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
1192 	     cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
1193 		return ICE_ERR_NOT_IMPL;
1194 
1195 	if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
1196 	     new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
1197 	    (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
1198 	     cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
1199 		return ICE_ERR_NOT_IMPL;
1200 
1201 	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
1202 		/* Only one entry existed in the mapping and it was not already
1203 		 * a part of a VSI list. So, create a VSI list with the old and
1204 		 * new VSIs.
1205 		 */
1206 		struct ice_fltr_info tmp_fltr;
1207 		u16 vsi_handle_arr[2];
1208 
1209 		/* A rule already exists with the new VSI being added */
1210 		if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
1211 			return ICE_ERR_ALREADY_EXISTS;
1212 
1213 		vsi_handle_arr[0] = cur_fltr->vsi_handle;
1214 		vsi_handle_arr[1] = new_fltr->vsi_handle;
1215 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1216 						  &vsi_list_id,
1217 						  new_fltr->lkup_type);
1218 		if (status)
1219 			return status;
1220 
1221 		tmp_fltr = *new_fltr;
1222 		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
1223 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1224 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1225 		/* Update the previous switch rule of "MAC forward to VSI" to
1226 		 * "MAC fwd to VSI list"
1227 		 */
1228 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1229 		if (status)
1230 			return status;
1231 
1232 		cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1233 		cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1234 		m_entry->vsi_list_info =
1235 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1236 						vsi_list_id);
1237 
1238 		/* If this entry was large action then the large action needs
1239 		 * to be updated to point to FWD to VSI list
1240 		 */
1241 		if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1242 			status =
1243 			    ice_add_marker_act(hw, m_entry,
1244 					       m_entry->sw_marker_id,
1245 					       m_entry->lg_act_idx);
1246 	} else {
1247 		u16 vsi_handle = new_fltr->vsi_handle;
1248 		enum ice_adminq_opc opcode;
1249 
1250 		if (!m_entry->vsi_list_info)
1251 			return ICE_ERR_CFG;
1252 
1253 		/* A rule already exists with the new VSI being added */
1254 		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
1255 			return 0;
1256 
1257 		/* Update the previously created VSI list set with
1258 		 * the new VSI id passed in
1259 		 */
1260 		vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1261 		opcode = ice_aqc_opc_update_sw_rules;
1262 
1263 		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
1264 						  vsi_list_id, false, opcode,
1265 						  new_fltr->lkup_type);
1266 		/* update VSI list mapping info with new VSI id */
1267 		if (!status)
1268 			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
1269 	}
1270 	if (!status)
1271 		m_entry->vsi_count++;
1272 	return status;
1273 }
1274 
1275 /**
1276  * ice_find_rule_entry - Search a rule entry
1277  * @hw: pointer to the hardware structure
1278  * @recp_id: lookup type for which the specified rule needs to be searched
1279  * @f_info: rule information
1280  *
1281  * Helper function to search for a given rule entry
1282  * Returns pointer to entry storing the rule if found
1283  */
1284 static struct ice_fltr_mgmt_list_entry *
1285 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
1286 {
1287 	struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
1288 	struct ice_switch_info *sw = hw->switch_info;
1289 	struct list_head *list_head;
1290 
1291 	list_head = &sw->recp_list[recp_id].filt_rules;
1292 	list_for_each_entry(list_itr, list_head, list_entry) {
1293 		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
1294 			    sizeof(f_info->l_data)) &&
1295 		    f_info->flag == list_itr->fltr_info.flag) {
1296 			ret = list_itr;
1297 			break;
1298 		}
1299 	}
1300 	return ret;
1301 }
1302 
1303 /**
1304  * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
1305  * @hw: pointer to the hardware structure
1306  * @recp_id: lookup type for which VSI lists needs to be searched
1307  * @vsi_handle: VSI handle to be found in VSI list
1308  * @vsi_list_id: VSI list id found containing vsi_handle
1309  *
1310  * Helper function to search a VSI list with single entry containing given VSI
1311  * handle element. This can be extended further to search VSI list with more
1312  * than 1 vsi_count. Returns pointer to VSI list entry if found.
1313  */
1314 static struct ice_vsi_list_map_info *
1315 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
1316 			u16 *vsi_list_id)
1317 {
1318 	struct ice_vsi_list_map_info *map_info = NULL;
1319 	struct ice_switch_info *sw = hw->switch_info;
1320 	struct ice_fltr_mgmt_list_entry *list_itr;
1321 	struct list_head *list_head;
1322 
1323 	list_head = &sw->recp_list[recp_id].filt_rules;
1324 	list_for_each_entry(list_itr, list_head, list_entry) {
1325 		if (list_itr->vsi_count == 1 && list_itr->vsi_list_info) {
1326 			map_info = list_itr->vsi_list_info;
1327 			if (test_bit(vsi_handle, map_info->vsi_map)) {
1328 				*vsi_list_id = map_info->vsi_list_id;
1329 				return map_info;
1330 			}
1331 		}
1332 	}
1333 	return NULL;
1334 }
1335 
1336 /**
1337  * ice_add_rule_internal - add rule for a given lookup type
1338  * @hw: pointer to the hardware structure
1339  * @recp_id: lookup type (recipe id) for which rule has to be added
1340  * @f_entry: structure containing MAC forwarding information
1341  *
1342  * Adds or updates the rule lists for a given recipe
1343  */
1344 static enum ice_status
1345 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
1346 		      struct ice_fltr_list_entry *f_entry)
1347 {
1348 	struct ice_switch_info *sw = hw->switch_info;
1349 	struct ice_fltr_info *new_fltr, *cur_fltr;
1350 	struct ice_fltr_mgmt_list_entry *m_entry;
1351 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1352 	enum ice_status status = 0;
1353 
1354 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1355 		return ICE_ERR_PARAM;
1356 	f_entry->fltr_info.fwd_id.hw_vsi_id =
1357 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1358 
1359 	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1360 
1361 	mutex_lock(rule_lock);
1362 	new_fltr = &f_entry->fltr_info;
1363 	if (new_fltr->flag & ICE_FLTR_RX)
1364 		new_fltr->src = hw->port_info->lport;
1365 	else if (new_fltr->flag & ICE_FLTR_TX)
1366 		new_fltr->src = f_entry->fltr_info.fwd_id.hw_vsi_id;
1367 
1368 	m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
1369 	if (!m_entry) {
1370 		mutex_unlock(rule_lock);
1371 		return ice_create_pkt_fwd_rule(hw, f_entry);
1372 	}
1373 
1374 	cur_fltr = &m_entry->fltr_info;
1375 	status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
1376 	mutex_unlock(rule_lock);
1377 
1378 	return status;
1379 }
1380 
1381 /**
1382  * ice_remove_vsi_list_rule
1383  * @hw: pointer to the hardware structure
1384  * @vsi_list_id: VSI list id generated as part of allocate resource
1385  * @lkup_type: switch rule filter lookup type
1386  *
1387  * The VSI list should be emptied before this function is called to remove the
1388  * VSI list.
1389  */
1390 static enum ice_status
1391 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1392 			 enum ice_sw_lkup_type lkup_type)
1393 {
1394 	struct ice_aqc_sw_rules_elem *s_rule;
1395 	enum ice_status status;
1396 	u16 s_rule_size;
1397 
1398 	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
1399 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1400 	if (!s_rule)
1401 		return ICE_ERR_NO_MEMORY;
1402 
1403 	s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
1404 	s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
1405 
1406 	/* Free the vsi_list resource that we allocated. It is assumed that the
1407 	 * list is empty at this point.
1408 	 */
1409 	status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
1410 					    ice_aqc_opc_free_res);
1411 
1412 	devm_kfree(ice_hw_to_dev(hw), s_rule);
1413 	return status;
1414 }
1415 
1416 /**
1417  * ice_rem_update_vsi_list
1418  * @hw: pointer to the hardware structure
1419  * @vsi_handle: VSI handle of the VSI to remove
1420  * @fm_list: filter management entry for which the VSI list management needs to
1421  *           be done
1422  */
1423 static enum ice_status
1424 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
1425 			struct ice_fltr_mgmt_list_entry *fm_list)
1426 {
1427 	enum ice_sw_lkup_type lkup_type;
1428 	enum ice_status status = 0;
1429 	u16 vsi_list_id;
1430 
1431 	if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
1432 	    fm_list->vsi_count == 0)
1433 		return ICE_ERR_PARAM;
1434 
1435 	/* A rule with the VSI being removed does not exist */
1436 	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
1437 		return ICE_ERR_DOES_NOT_EXIST;
1438 
1439 	lkup_type = fm_list->fltr_info.lkup_type;
1440 	vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
1441 	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
1442 					  ice_aqc_opc_update_sw_rules,
1443 					  lkup_type);
1444 	if (status)
1445 		return status;
1446 
1447 	fm_list->vsi_count--;
1448 	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
1449 
1450 	if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
1451 		struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
1452 		struct ice_vsi_list_map_info *vsi_list_info =
1453 			fm_list->vsi_list_info;
1454 		u16 rem_vsi_handle;
1455 
1456 		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
1457 						ICE_MAX_VSI);
1458 		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
1459 			return ICE_ERR_OUT_OF_RANGE;
1460 
1461 		/* Make sure VSI list is empty before removing it below */
1462 		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
1463 						  vsi_list_id, true,
1464 						  ice_aqc_opc_update_sw_rules,
1465 						  lkup_type);
1466 		if (status)
1467 			return status;
1468 
1469 		tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
1470 		tmp_fltr_info.fwd_id.hw_vsi_id =
1471 			ice_get_hw_vsi_num(hw, rem_vsi_handle);
1472 		tmp_fltr_info.vsi_handle = rem_vsi_handle;
1473 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
1474 		if (status) {
1475 			ice_debug(hw, ICE_DBG_SW,
1476 				  "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
1477 				  tmp_fltr_info.fwd_id.hw_vsi_id, status);
1478 			return status;
1479 		}
1480 
1481 		fm_list->fltr_info = tmp_fltr_info;
1482 	}
1483 
1484 	if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
1485 	    (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
1486 		struct ice_vsi_list_map_info *vsi_list_info =
1487 			fm_list->vsi_list_info;
1488 
1489 		/* Remove the VSI list since it is no longer used */
1490 		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
1491 		if (status) {
1492 			ice_debug(hw, ICE_DBG_SW,
1493 				  "Failed to remove VSI list %d, error %d\n",
1494 				  vsi_list_id, status);
1495 			return status;
1496 		}
1497 
1498 		list_del(&vsi_list_info->list_entry);
1499 		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
1500 		fm_list->vsi_list_info = NULL;
1501 	}
1502 
1503 	return status;
1504 }
1505 
1506 /**
1507  * ice_remove_rule_internal - Remove a filter rule of a given type
1508  * @hw: pointer to the hardware structure
1509  * @recp_id: recipe id for which the rule needs to removed
1510  * @f_entry: rule entry containing filter information
1511  */
1512 static enum ice_status
1513 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
1514 			 struct ice_fltr_list_entry *f_entry)
1515 {
1516 	struct ice_switch_info *sw = hw->switch_info;
1517 	struct ice_fltr_mgmt_list_entry *list_elem;
1518 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1519 	enum ice_status status = 0;
1520 	bool remove_rule = false;
1521 	u16 vsi_handle;
1522 
1523 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1524 		return ICE_ERR_PARAM;
1525 	f_entry->fltr_info.fwd_id.hw_vsi_id =
1526 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1527 
1528 	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1529 	mutex_lock(rule_lock);
1530 	list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
1531 	if (!list_elem) {
1532 		status = ICE_ERR_DOES_NOT_EXIST;
1533 		goto exit;
1534 	}
1535 
1536 	if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
1537 		remove_rule = true;
1538 	} else if (!list_elem->vsi_list_info) {
1539 		status = ICE_ERR_DOES_NOT_EXIST;
1540 		goto exit;
1541 	} else if (list_elem->vsi_list_info->ref_cnt > 1) {
1542 		/* a ref_cnt > 1 indicates that the vsi_list is being
1543 		 * shared by multiple rules. Decrement the ref_cnt and
1544 		 * remove this rule, but do not modify the list, as it
1545 		 * is in-use by other rules.
1546 		 */
1547 		list_elem->vsi_list_info->ref_cnt--;
1548 		remove_rule = true;
1549 	} else {
1550 		/* a ref_cnt of 1 indicates the vsi_list is only used
1551 		 * by one rule. However, the original removal request is only
1552 		 * for a single VSI. Update the vsi_list first, and only
1553 		 * remove the rule if there are no further VSIs in this list.
1554 		 */
1555 		vsi_handle = f_entry->fltr_info.vsi_handle;
1556 		status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
1557 		if (status)
1558 			goto exit;
1559 		/* if vsi count goes to zero after updating the vsi list */
1560 		if (list_elem->vsi_count == 0)
1561 			remove_rule = true;
1562 	}
1563 
1564 	if (remove_rule) {
1565 		/* Remove the lookup rule */
1566 		struct ice_aqc_sw_rules_elem *s_rule;
1567 
1568 		s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1569 				      ICE_SW_RULE_RX_TX_NO_HDR_SIZE,
1570 				      GFP_KERNEL);
1571 		if (!s_rule) {
1572 			status = ICE_ERR_NO_MEMORY;
1573 			goto exit;
1574 		}
1575 
1576 		ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
1577 				 ice_aqc_opc_remove_sw_rules);
1578 
1579 		status = ice_aq_sw_rules(hw, s_rule,
1580 					 ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
1581 					 ice_aqc_opc_remove_sw_rules, NULL);
1582 		if (status)
1583 			goto exit;
1584 
1585 		/* Remove a book keeping from the list */
1586 		devm_kfree(ice_hw_to_dev(hw), s_rule);
1587 
1588 		list_del(&list_elem->list_entry);
1589 		devm_kfree(ice_hw_to_dev(hw), list_elem);
1590 	}
1591 exit:
1592 	mutex_unlock(rule_lock);
1593 	return status;
1594 }
1595 
1596 /**
1597  * ice_add_mac - Add a MAC address based filter rule
1598  * @hw: pointer to the hardware structure
1599  * @m_list: list of MAC addresses and forwarding information
1600  *
1601  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
1602  * multiple unicast addresses, the function assumes that all the
1603  * addresses are unique in a given add_mac call. It doesn't
1604  * check for duplicates in this case, removing duplicates from a given
1605  * list should be taken care of in the caller of this function.
1606  */
1607 enum ice_status
1608 ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
1609 {
1610 	struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
1611 	struct ice_fltr_list_entry *m_list_itr;
1612 	struct list_head *rule_head;
1613 	u16 elem_sent, total_elem_left;
1614 	struct ice_switch_info *sw;
1615 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1616 	enum ice_status status = 0;
1617 	u16 num_unicast = 0;
1618 	u16 s_rule_size;
1619 
1620 	if (!m_list || !hw)
1621 		return ICE_ERR_PARAM;
1622 
1623 	s_rule = NULL;
1624 	sw = hw->switch_info;
1625 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1626 	list_for_each_entry(m_list_itr, m_list, list_entry) {
1627 		u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
1628 		u16 vsi_handle;
1629 		u16 hw_vsi_id;
1630 
1631 		m_list_itr->fltr_info.flag = ICE_FLTR_TX;
1632 		vsi_handle = m_list_itr->fltr_info.vsi_handle;
1633 		if (!ice_is_vsi_valid(hw, vsi_handle))
1634 			return ICE_ERR_PARAM;
1635 		hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
1636 		m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
1637 		/* update the src in case it is vsi num */
1638 		if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
1639 			return ICE_ERR_PARAM;
1640 		m_list_itr->fltr_info.src = hw_vsi_id;
1641 		if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
1642 		    is_zero_ether_addr(add))
1643 			return ICE_ERR_PARAM;
1644 		if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
1645 			/* Don't overwrite the unicast address */
1646 			mutex_lock(rule_lock);
1647 			if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
1648 						&m_list_itr->fltr_info)) {
1649 				mutex_unlock(rule_lock);
1650 				return ICE_ERR_ALREADY_EXISTS;
1651 			}
1652 			mutex_unlock(rule_lock);
1653 			num_unicast++;
1654 		} else if (is_multicast_ether_addr(add) ||
1655 			   (is_unicast_ether_addr(add) && hw->ucast_shared)) {
1656 			m_list_itr->status =
1657 				ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
1658 						      m_list_itr);
1659 			if (m_list_itr->status)
1660 				return m_list_itr->status;
1661 		}
1662 	}
1663 
1664 	mutex_lock(rule_lock);
1665 	/* Exit if no suitable entries were found for adding bulk switch rule */
1666 	if (!num_unicast) {
1667 		status = 0;
1668 		goto ice_add_mac_exit;
1669 	}
1670 
1671 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1672 
1673 	/* Allocate switch rule buffer for the bulk update for unicast */
1674 	s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1675 	s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
1676 			      GFP_KERNEL);
1677 	if (!s_rule) {
1678 		status = ICE_ERR_NO_MEMORY;
1679 		goto ice_add_mac_exit;
1680 	}
1681 
1682 	r_iter = s_rule;
1683 	list_for_each_entry(m_list_itr, m_list, list_entry) {
1684 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1685 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1686 
1687 		if (is_unicast_ether_addr(mac_addr)) {
1688 			ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
1689 					 ice_aqc_opc_add_sw_rules);
1690 			r_iter = (struct ice_aqc_sw_rules_elem *)
1691 				((u8 *)r_iter + s_rule_size);
1692 		}
1693 	}
1694 
1695 	/* Call AQ bulk switch rule update for all unicast addresses */
1696 	r_iter = s_rule;
1697 	/* Call AQ switch rule in AQ_MAX chunk */
1698 	for (total_elem_left = num_unicast; total_elem_left > 0;
1699 	     total_elem_left -= elem_sent) {
1700 		struct ice_aqc_sw_rules_elem *entry = r_iter;
1701 
1702 		elem_sent = min(total_elem_left,
1703 				(u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
1704 		status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
1705 					 elem_sent, ice_aqc_opc_add_sw_rules,
1706 					 NULL);
1707 		if (status)
1708 			goto ice_add_mac_exit;
1709 		r_iter = (struct ice_aqc_sw_rules_elem *)
1710 			((u8 *)r_iter + (elem_sent * s_rule_size));
1711 	}
1712 
1713 	/* Fill up rule id based on the value returned from FW */
1714 	r_iter = s_rule;
1715 	list_for_each_entry(m_list_itr, m_list, list_entry) {
1716 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1717 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1718 		struct ice_fltr_mgmt_list_entry *fm_entry;
1719 
1720 		if (is_unicast_ether_addr(mac_addr)) {
1721 			f_info->fltr_rule_id =
1722 				le16_to_cpu(r_iter->pdata.lkup_tx_rx.index);
1723 			f_info->fltr_act = ICE_FWD_TO_VSI;
1724 			/* Create an entry to track this MAC address */
1725 			fm_entry = devm_kzalloc(ice_hw_to_dev(hw),
1726 						sizeof(*fm_entry), GFP_KERNEL);
1727 			if (!fm_entry) {
1728 				status = ICE_ERR_NO_MEMORY;
1729 				goto ice_add_mac_exit;
1730 			}
1731 			fm_entry->fltr_info = *f_info;
1732 			fm_entry->vsi_count = 1;
1733 			/* The book keeping entries will get removed when
1734 			 * base driver calls remove filter AQ command
1735 			 */
1736 
1737 			list_add(&fm_entry->list_entry, rule_head);
1738 			r_iter = (struct ice_aqc_sw_rules_elem *)
1739 				((u8 *)r_iter + s_rule_size);
1740 		}
1741 	}
1742 
1743 ice_add_mac_exit:
1744 	mutex_unlock(rule_lock);
1745 	if (s_rule)
1746 		devm_kfree(ice_hw_to_dev(hw), s_rule);
1747 	return status;
1748 }
1749 
1750 /**
1751  * ice_add_vlan_internal - Add one VLAN based filter rule
1752  * @hw: pointer to the hardware structure
1753  * @f_entry: filter entry containing one VLAN information
1754  */
1755 static enum ice_status
1756 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1757 {
1758 	struct ice_switch_info *sw = hw->switch_info;
1759 	struct ice_fltr_mgmt_list_entry *v_list_itr;
1760 	struct ice_fltr_info *new_fltr, *cur_fltr;
1761 	enum ice_sw_lkup_type lkup_type;
1762 	u16 vsi_list_id = 0, vsi_handle;
1763 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1764 	enum ice_status status = 0;
1765 
1766 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1767 		return ICE_ERR_PARAM;
1768 
1769 	f_entry->fltr_info.fwd_id.hw_vsi_id =
1770 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1771 	new_fltr = &f_entry->fltr_info;
1772 
1773 	/* VLAN id should only be 12 bits */
1774 	if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
1775 		return ICE_ERR_PARAM;
1776 
1777 	if (new_fltr->src_id != ICE_SRC_ID_VSI)
1778 		return ICE_ERR_PARAM;
1779 
1780 	new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
1781 	lkup_type = new_fltr->lkup_type;
1782 	vsi_handle = new_fltr->vsi_handle;
1783 	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
1784 	mutex_lock(rule_lock);
1785 	v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
1786 	if (!v_list_itr) {
1787 		struct ice_vsi_list_map_info *map_info = NULL;
1788 
1789 		if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
1790 			/* All VLAN pruning rules use a VSI list. Check if
1791 			 * there is already a VSI list containing VSI that we
1792 			 * want to add. If found, use the same vsi_list_id for
1793 			 * this new VLAN rule or else create a new list.
1794 			 */
1795 			map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
1796 							   vsi_handle,
1797 							   &vsi_list_id);
1798 			if (!map_info) {
1799 				status = ice_create_vsi_list_rule(hw,
1800 								  &vsi_handle,
1801 								  1,
1802 								  &vsi_list_id,
1803 								  lkup_type);
1804 				if (status)
1805 					goto exit;
1806 			}
1807 			/* Convert the action to forwarding to a VSI list. */
1808 			new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1809 			new_fltr->fwd_id.vsi_list_id = vsi_list_id;
1810 		}
1811 
1812 		status = ice_create_pkt_fwd_rule(hw, f_entry);
1813 		if (!status) {
1814 			v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
1815 							 new_fltr);
1816 			if (!v_list_itr) {
1817 				status = ICE_ERR_DOES_NOT_EXIST;
1818 				goto exit;
1819 			}
1820 			/* reuse VSI list for new rule and increment ref_cnt */
1821 			if (map_info) {
1822 				v_list_itr->vsi_list_info = map_info;
1823 				map_info->ref_cnt++;
1824 			} else {
1825 				v_list_itr->vsi_list_info =
1826 					ice_create_vsi_list_map(hw, &vsi_handle,
1827 								1, vsi_list_id);
1828 			}
1829 		}
1830 	} else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
1831 		/* Update existing VSI list to add new VSI id only if it used
1832 		 * by one VLAN rule.
1833 		 */
1834 		cur_fltr = &v_list_itr->fltr_info;
1835 		status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
1836 						 new_fltr);
1837 	} else {
1838 		/* If VLAN rule exists and VSI list being used by this rule is
1839 		 * referenced by more than 1 VLAN rule. Then create a new VSI
1840 		 * list appending previous VSI with new VSI and update existing
1841 		 * VLAN rule to point to new VSI list id
1842 		 */
1843 		struct ice_fltr_info tmp_fltr;
1844 		u16 vsi_handle_arr[2];
1845 		u16 cur_handle;
1846 
1847 		/* Current implementation only supports reusing VSI list with
1848 		 * one VSI count. We should never hit below condition
1849 		 */
1850 		if (v_list_itr->vsi_count > 1 &&
1851 		    v_list_itr->vsi_list_info->ref_cnt > 1) {
1852 			ice_debug(hw, ICE_DBG_SW,
1853 				  "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
1854 			status = ICE_ERR_CFG;
1855 			goto exit;
1856 		}
1857 
1858 		cur_handle =
1859 			find_first_bit(v_list_itr->vsi_list_info->vsi_map,
1860 				       ICE_MAX_VSI);
1861 
1862 		/* A rule already exists with the new VSI being added */
1863 		if (cur_handle == vsi_handle) {
1864 			status = ICE_ERR_ALREADY_EXISTS;
1865 			goto exit;
1866 		}
1867 
1868 		vsi_handle_arr[0] = cur_handle;
1869 		vsi_handle_arr[1] = vsi_handle;
1870 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1871 						  &vsi_list_id, lkup_type);
1872 		if (status)
1873 			goto exit;
1874 
1875 		tmp_fltr = v_list_itr->fltr_info;
1876 		tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
1877 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1878 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1879 		/* Update the previous switch rule to a new VSI list which
1880 		 * includes current VSI that is requested
1881 		 */
1882 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1883 		if (status)
1884 			goto exit;
1885 
1886 		/* before overriding VSI list map info. decrement ref_cnt of
1887 		 * previous VSI list
1888 		 */
1889 		v_list_itr->vsi_list_info->ref_cnt--;
1890 
1891 		/* now update to newly created list */
1892 		v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
1893 		v_list_itr->vsi_list_info =
1894 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1895 						vsi_list_id);
1896 		v_list_itr->vsi_count++;
1897 	}
1898 
1899 exit:
1900 	mutex_unlock(rule_lock);
1901 	return status;
1902 }
1903 
1904 /**
1905  * ice_add_vlan - Add VLAN based filter rule
1906  * @hw: pointer to the hardware structure
1907  * @v_list: list of VLAN entries and forwarding information
1908  */
1909 enum ice_status
1910 ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
1911 {
1912 	struct ice_fltr_list_entry *v_list_itr;
1913 
1914 	if (!v_list || !hw)
1915 		return ICE_ERR_PARAM;
1916 
1917 	list_for_each_entry(v_list_itr, v_list, list_entry) {
1918 		if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
1919 			return ICE_ERR_PARAM;
1920 		v_list_itr->fltr_info.flag = ICE_FLTR_TX;
1921 		v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
1922 		if (v_list_itr->status)
1923 			return v_list_itr->status;
1924 	}
1925 	return 0;
1926 }
1927 
1928 /**
1929  * ice_rem_sw_rule_info
1930  * @hw: pointer to the hardware structure
1931  * @rule_head: pointer to the switch list structure that we want to delete
1932  */
1933 static void
1934 ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
1935 {
1936 	if (!list_empty(rule_head)) {
1937 		struct ice_fltr_mgmt_list_entry *entry;
1938 		struct ice_fltr_mgmt_list_entry *tmp;
1939 
1940 		list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
1941 			list_del(&entry->list_entry);
1942 			devm_kfree(ice_hw_to_dev(hw), entry);
1943 		}
1944 	}
1945 }
1946 
1947 /**
1948  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
1949  * @hw: pointer to the hardware structure
1950  * @vsi_handle: VSI handle to set as default
1951  * @set: true to add the above mentioned switch rule, false to remove it
1952  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
1953  *
1954  * add filter rule to set/unset given VSI as default VSI for the switch
1955  * (represented by swid)
1956  */
1957 enum ice_status
1958 ice_cfg_dflt_vsi(struct ice_hw *hw, u16 vsi_handle, bool set, u8 direction)
1959 {
1960 	struct ice_aqc_sw_rules_elem *s_rule;
1961 	struct ice_fltr_info f_info;
1962 	enum ice_adminq_opc opcode;
1963 	enum ice_status status;
1964 	u16 s_rule_size;
1965 	u16 hw_vsi_id;
1966 
1967 	if (!ice_is_vsi_valid(hw, vsi_handle))
1968 		return ICE_ERR_PARAM;
1969 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
1970 
1971 	s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
1972 			    ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
1973 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1974 	if (!s_rule)
1975 		return ICE_ERR_NO_MEMORY;
1976 
1977 	memset(&f_info, 0, sizeof(f_info));
1978 
1979 	f_info.lkup_type = ICE_SW_LKUP_DFLT;
1980 	f_info.flag = direction;
1981 	f_info.fltr_act = ICE_FWD_TO_VSI;
1982 	f_info.fwd_id.hw_vsi_id = hw_vsi_id;
1983 
1984 	if (f_info.flag & ICE_FLTR_RX) {
1985 		f_info.src = hw->port_info->lport;
1986 		f_info.src_id = ICE_SRC_ID_LPORT;
1987 		if (!set)
1988 			f_info.fltr_rule_id =
1989 				hw->port_info->dflt_rx_vsi_rule_id;
1990 	} else if (f_info.flag & ICE_FLTR_TX) {
1991 		f_info.src_id = ICE_SRC_ID_VSI;
1992 		f_info.src = hw_vsi_id;
1993 		if (!set)
1994 			f_info.fltr_rule_id =
1995 				hw->port_info->dflt_tx_vsi_rule_id;
1996 	}
1997 
1998 	if (set)
1999 		opcode = ice_aqc_opc_add_sw_rules;
2000 	else
2001 		opcode = ice_aqc_opc_remove_sw_rules;
2002 
2003 	ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
2004 
2005 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
2006 	if (status || !(f_info.flag & ICE_FLTR_TX_RX))
2007 		goto out;
2008 	if (set) {
2009 		u16 index = le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
2010 
2011 		if (f_info.flag & ICE_FLTR_TX) {
2012 			hw->port_info->dflt_tx_vsi_num = hw_vsi_id;
2013 			hw->port_info->dflt_tx_vsi_rule_id = index;
2014 		} else if (f_info.flag & ICE_FLTR_RX) {
2015 			hw->port_info->dflt_rx_vsi_num = hw_vsi_id;
2016 			hw->port_info->dflt_rx_vsi_rule_id = index;
2017 		}
2018 	} else {
2019 		if (f_info.flag & ICE_FLTR_TX) {
2020 			hw->port_info->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
2021 			hw->port_info->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
2022 		} else if (f_info.flag & ICE_FLTR_RX) {
2023 			hw->port_info->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
2024 			hw->port_info->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
2025 		}
2026 	}
2027 
2028 out:
2029 	devm_kfree(ice_hw_to_dev(hw), s_rule);
2030 	return status;
2031 }
2032 
2033 /**
2034  * ice_remove_mac - remove a MAC address based filter rule
2035  * @hw: pointer to the hardware structure
2036  * @m_list: list of MAC addresses and forwarding information
2037  *
2038  * This function removes either a MAC filter rule or a specific VSI from a
2039  * VSI list for a multicast MAC address.
2040  *
2041  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
2042  * ice_add_mac. Caller should be aware that this call will only work if all
2043  * the entries passed into m_list were added previously. It will not attempt to
2044  * do a partial remove of entries that were found.
2045  */
2046 enum ice_status
2047 ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
2048 {
2049 	struct ice_fltr_list_entry *list_itr, *tmp;
2050 
2051 	if (!m_list)
2052 		return ICE_ERR_PARAM;
2053 
2054 	list_for_each_entry_safe(list_itr, tmp, m_list, list_entry) {
2055 		enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2056 
2057 		if (l_type != ICE_SW_LKUP_MAC)
2058 			return ICE_ERR_PARAM;
2059 		list_itr->status = ice_remove_rule_internal(hw,
2060 							    ICE_SW_LKUP_MAC,
2061 							    list_itr);
2062 		if (list_itr->status)
2063 			return list_itr->status;
2064 	}
2065 	return 0;
2066 }
2067 
2068 /**
2069  * ice_remove_vlan - Remove VLAN based filter rule
2070  * @hw: pointer to the hardware structure
2071  * @v_list: list of VLAN entries and forwarding information
2072  */
2073 enum ice_status
2074 ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
2075 {
2076 	struct ice_fltr_list_entry *v_list_itr, *tmp;
2077 
2078 	if (!v_list || !hw)
2079 		return ICE_ERR_PARAM;
2080 
2081 	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
2082 		enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2083 
2084 		if (l_type != ICE_SW_LKUP_VLAN)
2085 			return ICE_ERR_PARAM;
2086 		v_list_itr->status = ice_remove_rule_internal(hw,
2087 							      ICE_SW_LKUP_VLAN,
2088 							      v_list_itr);
2089 		if (v_list_itr->status)
2090 			return v_list_itr->status;
2091 	}
2092 	return 0;
2093 }
2094 
2095 /**
2096  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
2097  * @fm_entry: filter entry to inspect
2098  * @vsi_handle: VSI handle to compare with filter info
2099  */
2100 static bool
2101 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
2102 {
2103 	return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
2104 		 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
2105 		(fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
2106 		 (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
2107 }
2108 
2109 /**
2110  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
2111  * @hw: pointer to the hardware structure
2112  * @vsi_handle: VSI handle to remove filters from
2113  * @vsi_list_head: pointer to the list to add entry to
2114  * @fi: pointer to fltr_info of filter entry to copy & add
2115  *
2116  * Helper function, used when creating a list of filters to remove from
2117  * a specific VSI. The entry added to vsi_list_head is a COPY of the
2118  * original filter entry, with the exception of fltr_info.fltr_act and
2119  * fltr_info.fwd_id fields. These are set such that later logic can
2120  * extract which VSI to remove the fltr from, and pass on that information.
2121  */
2122 static enum ice_status
2123 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2124 			       struct list_head *vsi_list_head,
2125 			       struct ice_fltr_info *fi)
2126 {
2127 	struct ice_fltr_list_entry *tmp;
2128 
2129 	/* this memory is freed up in the caller function
2130 	 * once filters for this VSI are removed
2131 	 */
2132 	tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
2133 	if (!tmp)
2134 		return ICE_ERR_NO_MEMORY;
2135 
2136 	tmp->fltr_info = *fi;
2137 
2138 	/* Overwrite these fields to indicate which VSI to remove filter from,
2139 	 * so find and remove logic can extract the information from the
2140 	 * list entries. Note that original entries will still have proper
2141 	 * values.
2142 	 */
2143 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2144 	tmp->fltr_info.vsi_handle = vsi_handle;
2145 	tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2146 
2147 	list_add(&tmp->list_entry, vsi_list_head);
2148 
2149 	return 0;
2150 }
2151 
2152 /**
2153  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
2154  * @hw: pointer to the hardware structure
2155  * @vsi_handle: VSI handle to remove filters from
2156  * @lkup_list_head: pointer to the list that has certain lookup type filters
2157  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
2158  *
2159  * Locates all filters in lkup_list_head that are used by the given VSI,
2160  * and adds COPIES of those entries to vsi_list_head (intended to be used
2161  * to remove the listed filters).
2162  * Note that this means all entries in vsi_list_head must be explicitly
2163  * deallocated by the caller when done with list.
2164  */
2165 static enum ice_status
2166 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2167 			 struct list_head *lkup_list_head,
2168 			 struct list_head *vsi_list_head)
2169 {
2170 	struct ice_fltr_mgmt_list_entry *fm_entry;
2171 	enum ice_status status = 0;
2172 
2173 	/* check to make sure VSI id is valid and within boundary */
2174 	if (!ice_is_vsi_valid(hw, vsi_handle))
2175 		return ICE_ERR_PARAM;
2176 
2177 	list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
2178 		struct ice_fltr_info *fi;
2179 
2180 		fi = &fm_entry->fltr_info;
2181 		if (!fi || !ice_vsi_uses_fltr(fm_entry, vsi_handle))
2182 			continue;
2183 
2184 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2185 							vsi_list_head, fi);
2186 		if (status)
2187 			return status;
2188 	}
2189 	return status;
2190 }
2191 
2192 /**
2193  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
2194  * @hw: pointer to the hardware structure
2195  * @vsi_handle: VSI handle to remove filters from
2196  * @lkup: switch rule filter lookup type
2197  */
2198 static void
2199 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
2200 			 enum ice_sw_lkup_type lkup)
2201 {
2202 	struct ice_switch_info *sw = hw->switch_info;
2203 	struct ice_fltr_list_entry *fm_entry;
2204 	struct list_head remove_list_head;
2205 	struct list_head *rule_head;
2206 	struct ice_fltr_list_entry *tmp;
2207 	struct mutex *rule_lock;	/* Lock to protect filter rule list */
2208 	enum ice_status status;
2209 
2210 	INIT_LIST_HEAD(&remove_list_head);
2211 	rule_lock = &sw->recp_list[lkup].filt_rule_lock;
2212 	rule_head = &sw->recp_list[lkup].filt_rules;
2213 	mutex_lock(rule_lock);
2214 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
2215 					  &remove_list_head);
2216 	mutex_unlock(rule_lock);
2217 	if (status)
2218 		return;
2219 
2220 	switch (lkup) {
2221 	case ICE_SW_LKUP_MAC:
2222 		ice_remove_mac(hw, &remove_list_head);
2223 		break;
2224 	case ICE_SW_LKUP_VLAN:
2225 		ice_remove_vlan(hw, &remove_list_head);
2226 		break;
2227 	case ICE_SW_LKUP_MAC_VLAN:
2228 	case ICE_SW_LKUP_ETHERTYPE:
2229 	case ICE_SW_LKUP_ETHERTYPE_MAC:
2230 	case ICE_SW_LKUP_PROMISC:
2231 	case ICE_SW_LKUP_DFLT:
2232 	case ICE_SW_LKUP_PROMISC_VLAN:
2233 	case ICE_SW_LKUP_LAST:
2234 	default:
2235 		ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
2236 		break;
2237 	}
2238 
2239 	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
2240 		list_del(&fm_entry->list_entry);
2241 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
2242 	}
2243 }
2244 
2245 /**
2246  * ice_remove_vsi_fltr - Remove all filters for a VSI
2247  * @hw: pointer to the hardware structure
2248  * @vsi_handle: VSI handle to remove filters from
2249  */
2250 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
2251 {
2252 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
2253 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
2254 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
2255 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
2256 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
2257 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
2258 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
2259 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
2260 }
2261 
2262 /**
2263  * ice_replay_vsi_fltr - Replay filters for requested VSI
2264  * @hw: pointer to the hardware structure
2265  * @vsi_handle: driver VSI handle
2266  * @recp_id: Recipe id for which rules need to be replayed
2267  * @list_head: list for which filters need to be replayed
2268  *
2269  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
2270  * It is required to pass valid VSI handle.
2271  */
2272 static enum ice_status
2273 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
2274 		    struct list_head *list_head)
2275 {
2276 	struct ice_fltr_mgmt_list_entry *itr;
2277 	enum ice_status status = 0;
2278 	u16 hw_vsi_id;
2279 
2280 	if (list_empty(list_head))
2281 		return status;
2282 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2283 
2284 	list_for_each_entry(itr, list_head, list_entry) {
2285 		struct ice_fltr_list_entry f_entry;
2286 
2287 		f_entry.fltr_info = itr->fltr_info;
2288 		if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
2289 		    itr->fltr_info.vsi_handle == vsi_handle) {
2290 			/* update the src in case it is vsi num */
2291 			if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2292 				f_entry.fltr_info.src = hw_vsi_id;
2293 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
2294 			if (status)
2295 				goto end;
2296 			continue;
2297 		}
2298 		if (!itr->vsi_list_info ||
2299 		    !test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
2300 			continue;
2301 		/* Clearing it so that the logic can add it back */
2302 		clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
2303 		f_entry.fltr_info.vsi_handle = vsi_handle;
2304 		f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
2305 		/* update the src in case it is vsi num */
2306 		if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2307 			f_entry.fltr_info.src = hw_vsi_id;
2308 		if (recp_id == ICE_SW_LKUP_VLAN)
2309 			status = ice_add_vlan_internal(hw, &f_entry);
2310 		else
2311 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
2312 		if (status)
2313 			goto end;
2314 	}
2315 end:
2316 	return status;
2317 }
2318 
2319 /**
2320  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
2321  * @hw: pointer to the hardware structure
2322  * @vsi_handle: driver VSI handle
2323  *
2324  * Replays filters for requested VSI via vsi_handle.
2325  */
2326 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
2327 {
2328 	struct ice_switch_info *sw = hw->switch_info;
2329 	enum ice_status status = 0;
2330 	u8 i;
2331 
2332 	for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2333 		struct list_head *head;
2334 
2335 		head = &sw->recp_list[i].filt_replay_rules;
2336 		status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
2337 		if (status)
2338 			return status;
2339 	}
2340 	return status;
2341 }
2342 
2343 /**
2344  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
2345  * @hw: pointer to the hw struct
2346  *
2347  * Deletes the filter replay rules.
2348  */
2349 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
2350 {
2351 	struct ice_switch_info *sw = hw->switch_info;
2352 	u8 i;
2353 
2354 	if (!sw)
2355 		return;
2356 
2357 	for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2358 		if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
2359 			struct list_head *l_head;
2360 
2361 			l_head = &sw->recp_list[i].filt_replay_rules;
2362 			ice_rem_sw_rule_info(hw, l_head);
2363 		}
2364 	}
2365 }
2366