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(struct ice_sw_recipe), 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 {
1542 		if (list_elem->vsi_list_info->ref_cnt > 1)
1543 			list_elem->vsi_list_info->ref_cnt--;
1544 		vsi_handle = f_entry->fltr_info.vsi_handle;
1545 		status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
1546 		if (status)
1547 			goto exit;
1548 		/* if vsi count goes to zero after updating the vsi list */
1549 		if (list_elem->vsi_count == 0)
1550 			remove_rule = true;
1551 	}
1552 
1553 	if (remove_rule) {
1554 		/* Remove the lookup rule */
1555 		struct ice_aqc_sw_rules_elem *s_rule;
1556 
1557 		s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1558 				      ICE_SW_RULE_RX_TX_NO_HDR_SIZE,
1559 				      GFP_KERNEL);
1560 		if (!s_rule) {
1561 			status = ICE_ERR_NO_MEMORY;
1562 			goto exit;
1563 		}
1564 
1565 		ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
1566 				 ice_aqc_opc_remove_sw_rules);
1567 
1568 		status = ice_aq_sw_rules(hw, s_rule,
1569 					 ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
1570 					 ice_aqc_opc_remove_sw_rules, NULL);
1571 		if (status)
1572 			goto exit;
1573 
1574 		/* Remove a book keeping from the list */
1575 		devm_kfree(ice_hw_to_dev(hw), s_rule);
1576 
1577 		list_del(&list_elem->list_entry);
1578 		devm_kfree(ice_hw_to_dev(hw), list_elem);
1579 	}
1580 exit:
1581 	mutex_unlock(rule_lock);
1582 	return status;
1583 }
1584 
1585 /**
1586  * ice_add_mac - Add a MAC address based filter rule
1587  * @hw: pointer to the hardware structure
1588  * @m_list: list of MAC addresses and forwarding information
1589  *
1590  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
1591  * multiple unicast addresses, the function assumes that all the
1592  * addresses are unique in a given add_mac call. It doesn't
1593  * check for duplicates in this case, removing duplicates from a given
1594  * list should be taken care of in the caller of this function.
1595  */
1596 enum ice_status
1597 ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
1598 {
1599 	struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
1600 	struct ice_fltr_list_entry *m_list_itr;
1601 	struct list_head *rule_head;
1602 	u16 elem_sent, total_elem_left;
1603 	struct ice_switch_info *sw;
1604 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1605 	enum ice_status status = 0;
1606 	u16 num_unicast = 0;
1607 	u16 s_rule_size;
1608 
1609 	if (!m_list || !hw)
1610 		return ICE_ERR_PARAM;
1611 
1612 	s_rule = NULL;
1613 	sw = hw->switch_info;
1614 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1615 	list_for_each_entry(m_list_itr, m_list, list_entry) {
1616 		u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
1617 		u16 vsi_handle;
1618 		u16 hw_vsi_id;
1619 
1620 		m_list_itr->fltr_info.flag = ICE_FLTR_TX;
1621 		vsi_handle = m_list_itr->fltr_info.vsi_handle;
1622 		if (!ice_is_vsi_valid(hw, vsi_handle))
1623 			return ICE_ERR_PARAM;
1624 		hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
1625 		m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
1626 		/* update the src in case it is vsi num */
1627 		if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
1628 			return ICE_ERR_PARAM;
1629 		m_list_itr->fltr_info.src = hw_vsi_id;
1630 		if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
1631 		    is_zero_ether_addr(add))
1632 			return ICE_ERR_PARAM;
1633 		if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
1634 			/* Don't overwrite the unicast address */
1635 			mutex_lock(rule_lock);
1636 			if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
1637 						&m_list_itr->fltr_info)) {
1638 				mutex_unlock(rule_lock);
1639 				return ICE_ERR_ALREADY_EXISTS;
1640 			}
1641 			mutex_unlock(rule_lock);
1642 			num_unicast++;
1643 		} else if (is_multicast_ether_addr(add) ||
1644 			   (is_unicast_ether_addr(add) && hw->ucast_shared)) {
1645 			m_list_itr->status =
1646 				ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
1647 						      m_list_itr);
1648 			if (m_list_itr->status)
1649 				return m_list_itr->status;
1650 		}
1651 	}
1652 
1653 	mutex_lock(rule_lock);
1654 	/* Exit if no suitable entries were found for adding bulk switch rule */
1655 	if (!num_unicast) {
1656 		status = 0;
1657 		goto ice_add_mac_exit;
1658 	}
1659 
1660 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1661 
1662 	/* Allocate switch rule buffer for the bulk update for unicast */
1663 	s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1664 	s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
1665 			      GFP_KERNEL);
1666 	if (!s_rule) {
1667 		status = ICE_ERR_NO_MEMORY;
1668 		goto ice_add_mac_exit;
1669 	}
1670 
1671 	r_iter = s_rule;
1672 	list_for_each_entry(m_list_itr, m_list, list_entry) {
1673 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1674 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1675 
1676 		if (is_unicast_ether_addr(mac_addr)) {
1677 			ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
1678 					 ice_aqc_opc_add_sw_rules);
1679 			r_iter = (struct ice_aqc_sw_rules_elem *)
1680 				((u8 *)r_iter + s_rule_size);
1681 		}
1682 	}
1683 
1684 	/* Call AQ bulk switch rule update for all unicast addresses */
1685 	r_iter = s_rule;
1686 	/* Call AQ switch rule in AQ_MAX chunk */
1687 	for (total_elem_left = num_unicast; total_elem_left > 0;
1688 	     total_elem_left -= elem_sent) {
1689 		struct ice_aqc_sw_rules_elem *entry = r_iter;
1690 
1691 		elem_sent = min(total_elem_left,
1692 				(u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
1693 		status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
1694 					 elem_sent, ice_aqc_opc_add_sw_rules,
1695 					 NULL);
1696 		if (status)
1697 			goto ice_add_mac_exit;
1698 		r_iter = (struct ice_aqc_sw_rules_elem *)
1699 			((u8 *)r_iter + (elem_sent * s_rule_size));
1700 	}
1701 
1702 	/* Fill up rule id based on the value returned from FW */
1703 	r_iter = s_rule;
1704 	list_for_each_entry(m_list_itr, m_list, list_entry) {
1705 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1706 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1707 		struct ice_fltr_mgmt_list_entry *fm_entry;
1708 
1709 		if (is_unicast_ether_addr(mac_addr)) {
1710 			f_info->fltr_rule_id =
1711 				le16_to_cpu(r_iter->pdata.lkup_tx_rx.index);
1712 			f_info->fltr_act = ICE_FWD_TO_VSI;
1713 			/* Create an entry to track this MAC address */
1714 			fm_entry = devm_kzalloc(ice_hw_to_dev(hw),
1715 						sizeof(*fm_entry), GFP_KERNEL);
1716 			if (!fm_entry) {
1717 				status = ICE_ERR_NO_MEMORY;
1718 				goto ice_add_mac_exit;
1719 			}
1720 			fm_entry->fltr_info = *f_info;
1721 			fm_entry->vsi_count = 1;
1722 			/* The book keeping entries will get removed when
1723 			 * base driver calls remove filter AQ command
1724 			 */
1725 
1726 			list_add(&fm_entry->list_entry, rule_head);
1727 			r_iter = (struct ice_aqc_sw_rules_elem *)
1728 				((u8 *)r_iter + s_rule_size);
1729 		}
1730 	}
1731 
1732 ice_add_mac_exit:
1733 	mutex_unlock(rule_lock);
1734 	if (s_rule)
1735 		devm_kfree(ice_hw_to_dev(hw), s_rule);
1736 	return status;
1737 }
1738 
1739 /**
1740  * ice_add_vlan_internal - Add one VLAN based filter rule
1741  * @hw: pointer to the hardware structure
1742  * @f_entry: filter entry containing one VLAN information
1743  */
1744 static enum ice_status
1745 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1746 {
1747 	struct ice_switch_info *sw = hw->switch_info;
1748 	struct ice_fltr_mgmt_list_entry *v_list_itr;
1749 	struct ice_fltr_info *new_fltr, *cur_fltr;
1750 	enum ice_sw_lkup_type lkup_type;
1751 	u16 vsi_list_id = 0, vsi_handle;
1752 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1753 	enum ice_status status = 0;
1754 
1755 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1756 		return ICE_ERR_PARAM;
1757 
1758 	f_entry->fltr_info.fwd_id.hw_vsi_id =
1759 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1760 	new_fltr = &f_entry->fltr_info;
1761 
1762 	/* VLAN id should only be 12 bits */
1763 	if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
1764 		return ICE_ERR_PARAM;
1765 
1766 	if (new_fltr->src_id != ICE_SRC_ID_VSI)
1767 		return ICE_ERR_PARAM;
1768 
1769 	new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
1770 	lkup_type = new_fltr->lkup_type;
1771 	vsi_handle = new_fltr->vsi_handle;
1772 	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
1773 	mutex_lock(rule_lock);
1774 	v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
1775 	if (!v_list_itr) {
1776 		struct ice_vsi_list_map_info *map_info = NULL;
1777 
1778 		if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
1779 			/* All VLAN pruning rules use a VSI list. Check if
1780 			 * there is already a VSI list containing VSI that we
1781 			 * want to add. If found, use the same vsi_list_id for
1782 			 * this new VLAN rule or else create a new list.
1783 			 */
1784 			map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
1785 							   vsi_handle,
1786 							   &vsi_list_id);
1787 			if (!map_info) {
1788 				status = ice_create_vsi_list_rule(hw,
1789 								  &vsi_handle,
1790 								  1,
1791 								  &vsi_list_id,
1792 								  lkup_type);
1793 				if (status)
1794 					goto exit;
1795 			}
1796 			/* Convert the action to forwarding to a VSI list. */
1797 			new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1798 			new_fltr->fwd_id.vsi_list_id = vsi_list_id;
1799 		}
1800 
1801 		status = ice_create_pkt_fwd_rule(hw, f_entry);
1802 		if (!status) {
1803 			v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
1804 							 new_fltr);
1805 			if (!v_list_itr) {
1806 				status = ICE_ERR_DOES_NOT_EXIST;
1807 				goto exit;
1808 			}
1809 			/* reuse VSI list for new rule and increment ref_cnt */
1810 			if (map_info) {
1811 				v_list_itr->vsi_list_info = map_info;
1812 				map_info->ref_cnt++;
1813 			} else {
1814 				v_list_itr->vsi_list_info =
1815 					ice_create_vsi_list_map(hw, &vsi_handle,
1816 								1, vsi_list_id);
1817 			}
1818 		}
1819 	} else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
1820 		/* Update existing VSI list to add new VSI id only if it used
1821 		 * by one VLAN rule.
1822 		 */
1823 		cur_fltr = &v_list_itr->fltr_info;
1824 		status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
1825 						 new_fltr);
1826 	} else {
1827 		/* If VLAN rule exists and VSI list being used by this rule is
1828 		 * referenced by more than 1 VLAN rule. Then create a new VSI
1829 		 * list appending previous VSI with new VSI and update existing
1830 		 * VLAN rule to point to new VSI list id
1831 		 */
1832 		struct ice_fltr_info tmp_fltr;
1833 		u16 vsi_handle_arr[2];
1834 		u16 cur_handle;
1835 
1836 		/* Current implementation only supports reusing VSI list with
1837 		 * one VSI count. We should never hit below condition
1838 		 */
1839 		if (v_list_itr->vsi_count > 1 &&
1840 		    v_list_itr->vsi_list_info->ref_cnt > 1) {
1841 			ice_debug(hw, ICE_DBG_SW,
1842 				  "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
1843 			status = ICE_ERR_CFG;
1844 			goto exit;
1845 		}
1846 
1847 		cur_handle =
1848 			find_first_bit(v_list_itr->vsi_list_info->vsi_map,
1849 				       ICE_MAX_VSI);
1850 
1851 		/* A rule already exists with the new VSI being added */
1852 		if (cur_handle == vsi_handle) {
1853 			status = ICE_ERR_ALREADY_EXISTS;
1854 			goto exit;
1855 		}
1856 
1857 		vsi_handle_arr[0] = cur_handle;
1858 		vsi_handle_arr[1] = vsi_handle;
1859 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1860 						  &vsi_list_id, lkup_type);
1861 		if (status)
1862 			goto exit;
1863 
1864 		tmp_fltr = v_list_itr->fltr_info;
1865 		tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
1866 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1867 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1868 		/* Update the previous switch rule to a new VSI list which
1869 		 * includes current VSI that is requested
1870 		 */
1871 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1872 		if (status)
1873 			goto exit;
1874 
1875 		/* before overriding VSI list map info. decrement ref_cnt of
1876 		 * previous VSI list
1877 		 */
1878 		v_list_itr->vsi_list_info->ref_cnt--;
1879 
1880 		/* now update to newly created list */
1881 		v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
1882 		v_list_itr->vsi_list_info =
1883 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1884 						vsi_list_id);
1885 		v_list_itr->vsi_count++;
1886 	}
1887 
1888 exit:
1889 	mutex_unlock(rule_lock);
1890 	return status;
1891 }
1892 
1893 /**
1894  * ice_add_vlan - Add VLAN based filter rule
1895  * @hw: pointer to the hardware structure
1896  * @v_list: list of VLAN entries and forwarding information
1897  */
1898 enum ice_status
1899 ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
1900 {
1901 	struct ice_fltr_list_entry *v_list_itr;
1902 
1903 	if (!v_list || !hw)
1904 		return ICE_ERR_PARAM;
1905 
1906 	list_for_each_entry(v_list_itr, v_list, list_entry) {
1907 		if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
1908 			return ICE_ERR_PARAM;
1909 		v_list_itr->fltr_info.flag = ICE_FLTR_TX;
1910 		v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
1911 		if (v_list_itr->status)
1912 			return v_list_itr->status;
1913 	}
1914 	return 0;
1915 }
1916 
1917 /**
1918  * ice_rem_sw_rule_info
1919  * @hw: pointer to the hardware structure
1920  * @rule_head: pointer to the switch list structure that we want to delete
1921  */
1922 static void
1923 ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
1924 {
1925 	if (!list_empty(rule_head)) {
1926 		struct ice_fltr_mgmt_list_entry *entry;
1927 		struct ice_fltr_mgmt_list_entry *tmp;
1928 
1929 		list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
1930 			list_del(&entry->list_entry);
1931 			devm_kfree(ice_hw_to_dev(hw), entry);
1932 		}
1933 	}
1934 }
1935 
1936 /**
1937  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
1938  * @hw: pointer to the hardware structure
1939  * @vsi_handle: VSI handle to set as default
1940  * @set: true to add the above mentioned switch rule, false to remove it
1941  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
1942  *
1943  * add filter rule to set/unset given VSI as default VSI for the switch
1944  * (represented by swid)
1945  */
1946 enum ice_status
1947 ice_cfg_dflt_vsi(struct ice_hw *hw, u16 vsi_handle, bool set, u8 direction)
1948 {
1949 	struct ice_aqc_sw_rules_elem *s_rule;
1950 	struct ice_fltr_info f_info;
1951 	enum ice_adminq_opc opcode;
1952 	enum ice_status status;
1953 	u16 s_rule_size;
1954 	u16 hw_vsi_id;
1955 
1956 	if (!ice_is_vsi_valid(hw, vsi_handle))
1957 		return ICE_ERR_PARAM;
1958 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
1959 
1960 	s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
1961 			    ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
1962 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1963 	if (!s_rule)
1964 		return ICE_ERR_NO_MEMORY;
1965 
1966 	memset(&f_info, 0, sizeof(f_info));
1967 
1968 	f_info.lkup_type = ICE_SW_LKUP_DFLT;
1969 	f_info.flag = direction;
1970 	f_info.fltr_act = ICE_FWD_TO_VSI;
1971 	f_info.fwd_id.hw_vsi_id = hw_vsi_id;
1972 
1973 	if (f_info.flag & ICE_FLTR_RX) {
1974 		f_info.src = hw->port_info->lport;
1975 		f_info.src_id = ICE_SRC_ID_LPORT;
1976 		if (!set)
1977 			f_info.fltr_rule_id =
1978 				hw->port_info->dflt_rx_vsi_rule_id;
1979 	} else if (f_info.flag & ICE_FLTR_TX) {
1980 		f_info.src_id = ICE_SRC_ID_VSI;
1981 		f_info.src = hw_vsi_id;
1982 		if (!set)
1983 			f_info.fltr_rule_id =
1984 				hw->port_info->dflt_tx_vsi_rule_id;
1985 	}
1986 
1987 	if (set)
1988 		opcode = ice_aqc_opc_add_sw_rules;
1989 	else
1990 		opcode = ice_aqc_opc_remove_sw_rules;
1991 
1992 	ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
1993 
1994 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
1995 	if (status || !(f_info.flag & ICE_FLTR_TX_RX))
1996 		goto out;
1997 	if (set) {
1998 		u16 index = le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1999 
2000 		if (f_info.flag & ICE_FLTR_TX) {
2001 			hw->port_info->dflt_tx_vsi_num = hw_vsi_id;
2002 			hw->port_info->dflt_tx_vsi_rule_id = index;
2003 		} else if (f_info.flag & ICE_FLTR_RX) {
2004 			hw->port_info->dflt_rx_vsi_num = hw_vsi_id;
2005 			hw->port_info->dflt_rx_vsi_rule_id = index;
2006 		}
2007 	} else {
2008 		if (f_info.flag & ICE_FLTR_TX) {
2009 			hw->port_info->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
2010 			hw->port_info->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
2011 		} else if (f_info.flag & ICE_FLTR_RX) {
2012 			hw->port_info->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
2013 			hw->port_info->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
2014 		}
2015 	}
2016 
2017 out:
2018 	devm_kfree(ice_hw_to_dev(hw), s_rule);
2019 	return status;
2020 }
2021 
2022 /**
2023  * ice_remove_mac - remove a MAC address based filter rule
2024  * @hw: pointer to the hardware structure
2025  * @m_list: list of MAC addresses and forwarding information
2026  *
2027  * This function removes either a MAC filter rule or a specific VSI from a
2028  * VSI list for a multicast MAC address.
2029  *
2030  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
2031  * ice_add_mac. Caller should be aware that this call will only work if all
2032  * the entries passed into m_list were added previously. It will not attempt to
2033  * do a partial remove of entries that were found.
2034  */
2035 enum ice_status
2036 ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
2037 {
2038 	struct ice_fltr_list_entry *list_itr, *tmp;
2039 
2040 	if (!m_list)
2041 		return ICE_ERR_PARAM;
2042 
2043 	list_for_each_entry_safe(list_itr, tmp, m_list, list_entry) {
2044 		enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2045 
2046 		if (l_type != ICE_SW_LKUP_MAC)
2047 			return ICE_ERR_PARAM;
2048 		list_itr->status = ice_remove_rule_internal(hw,
2049 							    ICE_SW_LKUP_MAC,
2050 							    list_itr);
2051 		if (list_itr->status)
2052 			return list_itr->status;
2053 	}
2054 	return 0;
2055 }
2056 
2057 /**
2058  * ice_remove_vlan - Remove VLAN based filter rule
2059  * @hw: pointer to the hardware structure
2060  * @v_list: list of VLAN entries and forwarding information
2061  */
2062 enum ice_status
2063 ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
2064 {
2065 	struct ice_fltr_list_entry *v_list_itr, *tmp;
2066 
2067 	if (!v_list || !hw)
2068 		return ICE_ERR_PARAM;
2069 
2070 	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
2071 		enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2072 
2073 		if (l_type != ICE_SW_LKUP_VLAN)
2074 			return ICE_ERR_PARAM;
2075 		v_list_itr->status = ice_remove_rule_internal(hw,
2076 							      ICE_SW_LKUP_VLAN,
2077 							      v_list_itr);
2078 		if (v_list_itr->status)
2079 			return v_list_itr->status;
2080 	}
2081 	return 0;
2082 }
2083 
2084 /**
2085  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
2086  * @fm_entry: filter entry to inspect
2087  * @vsi_handle: VSI handle to compare with filter info
2088  */
2089 static bool
2090 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
2091 {
2092 	return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
2093 		 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
2094 		(fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
2095 		 (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
2096 }
2097 
2098 /**
2099  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
2100  * @hw: pointer to the hardware structure
2101  * @vsi_handle: VSI handle to remove filters from
2102  * @vsi_list_head: pointer to the list to add entry to
2103  * @fi: pointer to fltr_info of filter entry to copy & add
2104  *
2105  * Helper function, used when creating a list of filters to remove from
2106  * a specific VSI. The entry added to vsi_list_head is a COPY of the
2107  * original filter entry, with the exception of fltr_info.fltr_act and
2108  * fltr_info.fwd_id fields. These are set such that later logic can
2109  * extract which VSI to remove the fltr from, and pass on that information.
2110  */
2111 static enum ice_status
2112 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2113 			       struct list_head *vsi_list_head,
2114 			       struct ice_fltr_info *fi)
2115 {
2116 	struct ice_fltr_list_entry *tmp;
2117 
2118 	/* this memory is freed up in the caller function
2119 	 * once filters for this VSI are removed
2120 	 */
2121 	tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
2122 	if (!tmp)
2123 		return ICE_ERR_NO_MEMORY;
2124 
2125 	tmp->fltr_info = *fi;
2126 
2127 	/* Overwrite these fields to indicate which VSI to remove filter from,
2128 	 * so find and remove logic can extract the information from the
2129 	 * list entries. Note that original entries will still have proper
2130 	 * values.
2131 	 */
2132 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2133 	tmp->fltr_info.vsi_handle = vsi_handle;
2134 	tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2135 
2136 	list_add(&tmp->list_entry, vsi_list_head);
2137 
2138 	return 0;
2139 }
2140 
2141 /**
2142  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
2143  * @hw: pointer to the hardware structure
2144  * @vsi_handle: VSI handle to remove filters from
2145  * @lkup_list_head: pointer to the list that has certain lookup type filters
2146  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
2147  *
2148  * Locates all filters in lkup_list_head that are used by the given VSI,
2149  * and adds COPIES of those entries to vsi_list_head (intended to be used
2150  * to remove the listed filters).
2151  * Note that this means all entries in vsi_list_head must be explicitly
2152  * deallocated by the caller when done with list.
2153  */
2154 static enum ice_status
2155 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2156 			 struct list_head *lkup_list_head,
2157 			 struct list_head *vsi_list_head)
2158 {
2159 	struct ice_fltr_mgmt_list_entry *fm_entry;
2160 	enum ice_status status = 0;
2161 
2162 	/* check to make sure VSI id is valid and within boundary */
2163 	if (!ice_is_vsi_valid(hw, vsi_handle))
2164 		return ICE_ERR_PARAM;
2165 
2166 	list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
2167 		struct ice_fltr_info *fi;
2168 
2169 		fi = &fm_entry->fltr_info;
2170 		if (!fi || !ice_vsi_uses_fltr(fm_entry, vsi_handle))
2171 			continue;
2172 
2173 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2174 							vsi_list_head, fi);
2175 		if (status)
2176 			return status;
2177 	}
2178 	return status;
2179 }
2180 
2181 /**
2182  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
2183  * @hw: pointer to the hardware structure
2184  * @vsi_handle: VSI handle to remove filters from
2185  * @lkup: switch rule filter lookup type
2186  */
2187 static void
2188 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
2189 			 enum ice_sw_lkup_type lkup)
2190 {
2191 	struct ice_switch_info *sw = hw->switch_info;
2192 	struct ice_fltr_list_entry *fm_entry;
2193 	struct list_head remove_list_head;
2194 	struct list_head *rule_head;
2195 	struct ice_fltr_list_entry *tmp;
2196 	struct mutex *rule_lock;	/* Lock to protect filter rule list */
2197 	enum ice_status status;
2198 
2199 	INIT_LIST_HEAD(&remove_list_head);
2200 	rule_lock = &sw->recp_list[lkup].filt_rule_lock;
2201 	rule_head = &sw->recp_list[lkup].filt_rules;
2202 	mutex_lock(rule_lock);
2203 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
2204 					  &remove_list_head);
2205 	mutex_unlock(rule_lock);
2206 	if (status)
2207 		return;
2208 
2209 	switch (lkup) {
2210 	case ICE_SW_LKUP_MAC:
2211 		ice_remove_mac(hw, &remove_list_head);
2212 		break;
2213 	case ICE_SW_LKUP_VLAN:
2214 		ice_remove_vlan(hw, &remove_list_head);
2215 		break;
2216 	case ICE_SW_LKUP_MAC_VLAN:
2217 	case ICE_SW_LKUP_ETHERTYPE:
2218 	case ICE_SW_LKUP_ETHERTYPE_MAC:
2219 	case ICE_SW_LKUP_PROMISC:
2220 	case ICE_SW_LKUP_DFLT:
2221 	case ICE_SW_LKUP_PROMISC_VLAN:
2222 	case ICE_SW_LKUP_LAST:
2223 	default:
2224 		ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
2225 		break;
2226 	}
2227 
2228 	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
2229 		list_del(&fm_entry->list_entry);
2230 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
2231 	}
2232 }
2233 
2234 /**
2235  * ice_remove_vsi_fltr - Remove all filters for a VSI
2236  * @hw: pointer to the hardware structure
2237  * @vsi_handle: VSI handle to remove filters from
2238  */
2239 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
2240 {
2241 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
2242 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
2243 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
2244 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
2245 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
2246 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
2247 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
2248 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
2249 }
2250 
2251 /**
2252  * ice_replay_vsi_fltr - Replay filters for requested VSI
2253  * @hw: pointer to the hardware structure
2254  * @vsi_handle: driver VSI handle
2255  * @recp_id: Recipe id for which rules need to be replayed
2256  * @list_head: list for which filters need to be replayed
2257  *
2258  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
2259  * It is required to pass valid VSI handle.
2260  */
2261 static enum ice_status
2262 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
2263 		    struct list_head *list_head)
2264 {
2265 	struct ice_fltr_mgmt_list_entry *itr;
2266 	enum ice_status status = 0;
2267 	u16 hw_vsi_id;
2268 
2269 	if (list_empty(list_head))
2270 		return status;
2271 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2272 
2273 	list_for_each_entry(itr, list_head, list_entry) {
2274 		struct ice_fltr_list_entry f_entry;
2275 
2276 		f_entry.fltr_info = itr->fltr_info;
2277 		if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
2278 		    itr->fltr_info.vsi_handle == vsi_handle) {
2279 			/* update the src in case it is vsi num */
2280 			if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2281 				f_entry.fltr_info.src = hw_vsi_id;
2282 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
2283 			if (status)
2284 				goto end;
2285 			continue;
2286 		}
2287 		if (!itr->vsi_list_info ||
2288 		    !test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
2289 			continue;
2290 		/* Clearing it so that the logic can add it back */
2291 		clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
2292 		f_entry.fltr_info.vsi_handle = vsi_handle;
2293 		f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
2294 		/* update the src in case it is vsi num */
2295 		if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2296 			f_entry.fltr_info.src = hw_vsi_id;
2297 		if (recp_id == ICE_SW_LKUP_VLAN)
2298 			status = ice_add_vlan_internal(hw, &f_entry);
2299 		else
2300 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
2301 		if (status)
2302 			goto end;
2303 	}
2304 end:
2305 	return status;
2306 }
2307 
2308 /**
2309  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
2310  * @hw: pointer to the hardware structure
2311  * @vsi_handle: driver VSI handle
2312  *
2313  * Replays filters for requested VSI via vsi_handle.
2314  */
2315 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
2316 {
2317 	struct ice_switch_info *sw = hw->switch_info;
2318 	enum ice_status status = 0;
2319 	u8 i;
2320 
2321 	for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2322 		struct list_head *head;
2323 
2324 		head = &sw->recp_list[i].filt_replay_rules;
2325 		status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
2326 		if (status)
2327 			return status;
2328 	}
2329 	return status;
2330 }
2331 
2332 /**
2333  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
2334  * @hw: pointer to the hw struct
2335  *
2336  * Deletes the filter replay rules.
2337  */
2338 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
2339 {
2340 	struct ice_switch_info *sw = hw->switch_info;
2341 	u8 i;
2342 
2343 	if (!sw)
2344 		return;
2345 
2346 	for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2347 		if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
2348 			struct list_head *l_head;
2349 
2350 			l_head = &sw->recp_list[i].filt_replay_rules;
2351 			ice_rem_sw_rule_info(hw, l_head);
2352 		}
2353 	}
2354 }
2355