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