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