0ffd79dded838ee5dd984dffe616df4fad04b09e
[dpdk.git] / drivers / net / ice / base / ice_switch.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2019
3  */
4
5 #include "ice_switch.h"
6 #include "ice_flex_type.h"
7 #include "ice_flow.h"
8
9
10 #define ICE_ETH_DA_OFFSET               0
11 #define ICE_ETH_ETHTYPE_OFFSET          12
12 #define ICE_ETH_VLAN_TCI_OFFSET         14
13 #define ICE_MAX_VLAN_ID                 0xFFF
14
15 /* Dummy ethernet header needed in the ice_aqc_sw_rules_elem
16  * struct to configure any switch filter rules.
17  * {DA (6 bytes), SA(6 bytes),
18  * Ether type (2 bytes for header without VLAN tag) OR
19  * VLAN tag (4 bytes for header with VLAN tag) }
20  *
21  * Word on Hardcoded values
22  * byte 0 = 0x2: to identify it as locally administered DA MAC
23  * byte 6 = 0x2: to identify it as locally administered SA MAC
24  * byte 12 = 0x81 & byte 13 = 0x00:
25  *      In case of VLAN filter first two bytes defines ether type (0x8100)
26  *      and remaining two bytes are placeholder for programming a given VLAN ID
27  *      In case of Ether type filter it is treated as header without VLAN tag
28  *      and byte 12 and 13 is used to program a given Ether type instead
29  */
30 #define DUMMY_ETH_HDR_LEN               16
31 static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
32                                                         0x2, 0, 0, 0, 0, 0,
33                                                         0x81, 0, 0, 0};
34
35 #define ICE_SW_RULE_RX_TX_ETH_HDR_SIZE \
36         (sizeof(struct ice_aqc_sw_rules_elem) - \
37          sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
38          sizeof(struct ice_sw_rule_lkup_rx_tx) + DUMMY_ETH_HDR_LEN - 1)
39 #define ICE_SW_RULE_RX_TX_NO_HDR_SIZE \
40         (sizeof(struct ice_aqc_sw_rules_elem) - \
41          sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
42          sizeof(struct ice_sw_rule_lkup_rx_tx) - 1)
43 #define ICE_SW_RULE_LG_ACT_SIZE(n) \
44         (sizeof(struct ice_aqc_sw_rules_elem) - \
45          sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
46          sizeof(struct ice_sw_rule_lg_act) - \
47          sizeof(((struct ice_sw_rule_lg_act *)0)->act) + \
48          ((n) * sizeof(((struct ice_sw_rule_lg_act *)0)->act)))
49 #define ICE_SW_RULE_VSI_LIST_SIZE(n) \
50         (sizeof(struct ice_aqc_sw_rules_elem) - \
51          sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
52          sizeof(struct ice_sw_rule_vsi_list) - \
53          sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi) + \
54          ((n) * sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi)))
55
56 static const
57 u8 dummy_gre_packet[] = { 0, 0, 0, 0,           /* Ether starts */
58                           0, 0, 0, 0,
59                           0, 0, 0, 0,
60                           0x08, 0,              /* Ether ends */
61                           0x45, 0, 0, 0x3E,     /* IP starts */
62                           0, 0, 0, 0,
63                           0, 0x2F, 0, 0,
64                           0, 0, 0, 0,
65                           0, 0, 0, 0,           /* IP ends */
66                           0x80, 0, 0x65, 0x58,  /* GRE starts */
67                           0, 0, 0, 0,           /* GRE ends */
68                           0, 0, 0, 0,           /* Ether starts */
69                           0, 0, 0, 0,
70                           0, 0, 0, 0,
71                           0x08, 0,              /* Ether ends */
72                           0x45, 0, 0, 0x14,     /* IP starts */
73                           0, 0, 0, 0,
74                           0, 0, 0, 0,
75                           0, 0, 0, 0,
76                           0, 0, 0, 0            /* IP ends */
77                         };
78
79 static const u8
80 dummy_udp_tun_packet[] = {0, 0, 0, 0,           /* Ether starts */
81                           0, 0, 0, 0,
82                           0, 0, 0, 0,
83                           0x08, 0,              /* Ether ends */
84                           0x45, 0, 0, 0x32,     /* IP starts */
85                           0, 0, 0, 0,
86                           0, 0x11, 0, 0,
87                           0, 0, 0, 0,
88                           0, 0, 0, 0,           /* IP ends */
89                           0, 0, 0x12, 0xB5,     /* UDP start*/
90                           0, 0x1E, 0, 0,        /* UDP end*/
91                           0, 0, 0, 0,           /* VXLAN start */
92                           0, 0, 0, 0,           /* VXLAN end*/
93                           0, 0, 0, 0,           /* Ether starts */
94                           0, 0, 0, 0,
95                           0, 0, 0, 0,
96                           0, 0                  /* Ether ends */
97                         };
98
99 static const u8
100 dummy_tcp_tun_packet[] = {0, 0, 0, 0,           /* Ether starts */
101                           0, 0, 0, 0,
102                           0, 0, 0, 0,
103                           0x08, 0,              /* Ether ends */
104                           0x45, 0, 0, 0x28,     /* IP starts */
105                           0, 0x01, 0, 0,
106                           0x40, 0x06, 0xF5, 0x69,
107                           0, 0, 0, 0,
108                           0, 0, 0, 0,   /* IP ends */
109                           0, 0, 0, 0,
110                           0, 0, 0, 0,
111                           0, 0, 0, 0,
112                           0x50, 0x02, 0x20,
113                           0, 0x9, 0x79, 0, 0,
114                           0, 0 /* 2 bytes padding for 4 byte alignment*/
115                         };
116
117 /* this is a recipe to profile bitmap association */
118 static ice_declare_bitmap(recipe_to_profile[ICE_MAX_NUM_RECIPES],
119                           ICE_MAX_NUM_PROFILES);
120 static ice_declare_bitmap(available_result_ids, ICE_CHAIN_FV_INDEX_START + 1);
121
122 /**
123  * ice_get_recp_frm_fw - update SW bookkeeping from FW recipe entries
124  * @hw: pointer to hardware structure
125  * @recps: struct that we need to populate
126  * @rid: recipe ID that we are populating
127  *
128  * This function is used to populate all the necessary entries into our
129  * bookkeeping so that we have a current list of all the recipes that are
130  * programmed in the firmware.
131  */
132 static enum ice_status
133 ice_get_recp_frm_fw(struct ice_hw *hw, struct ice_sw_recipe *recps, u8 rid)
134 {
135         u16 i, sub_recps, fv_word_idx = 0, result_idx = 0;
136         ice_declare_bitmap(r_bitmap, ICE_MAX_NUM_PROFILES);
137         u16 result_idxs[ICE_MAX_CHAIN_RECIPE] = { 0 };
138         struct ice_aqc_recipe_data_elem *tmp;
139         u16 num_recps = ICE_MAX_NUM_RECIPES;
140         struct ice_prot_lkup_ext *lkup_exts;
141         enum ice_status status;
142
143         /* we need a buffer big enough to accommodate all the recipes */
144         tmp = (struct ice_aqc_recipe_data_elem *)ice_calloc(hw,
145                 ICE_MAX_NUM_RECIPES, sizeof(*tmp));
146         if (!tmp)
147                 return ICE_ERR_NO_MEMORY;
148
149         tmp[0].recipe_indx = rid;
150         status = ice_aq_get_recipe(hw, tmp, &num_recps, rid, NULL);
151         /* non-zero status meaning recipe doesn't exist */
152         if (status)
153                 goto err_unroll;
154         lkup_exts = &recps[rid].lkup_exts;
155         /* start populating all the entries for recps[rid] based on lkups from
156          * firmware
157          */
158         for (sub_recps = 0; sub_recps < num_recps; sub_recps++) {
159                 struct ice_aqc_recipe_data_elem root_bufs = tmp[sub_recps];
160                 struct ice_recp_grp_entry *rg_entry;
161                 u8 prof_id, prot = 0;
162                 u16 off = 0;
163
164                 rg_entry = (struct ice_recp_grp_entry *)
165                         ice_malloc(hw, sizeof(*rg_entry));
166                 if (!rg_entry) {
167                         status = ICE_ERR_NO_MEMORY;
168                         goto err_unroll;
169                 }
170                 /* Avoid 8th bit since its result enable bit */
171                 result_idxs[result_idx] = root_bufs.content.result_indx &
172                         ~ICE_AQ_RECIPE_RESULT_EN;
173                 /* Check if result enable bit is set */
174                 if (root_bufs.content.result_indx & ICE_AQ_RECIPE_RESULT_EN)
175                         ice_clear_bit(ICE_CHAIN_FV_INDEX_START -
176                                       result_idxs[result_idx++],
177                                       available_result_ids);
178                 ice_memcpy(r_bitmap,
179                            recipe_to_profile[tmp[sub_recps].recipe_indx],
180                            sizeof(r_bitmap), ICE_NONDMA_TO_NONDMA);
181                 /* get the first profile that is associated with rid */
182                 prof_id = ice_find_first_bit(r_bitmap, ICE_MAX_NUM_PROFILES);
183                 for (i = 0; i < ICE_NUM_WORDS_RECIPE; i++) {
184                         u8 lkup_indx = root_bufs.content.lkup_indx[i + 1];
185
186                         rg_entry->fv_idx[i] = lkup_indx;
187                         /* If the recipe is a chained recipe then all its
188                          * child recipe's result will have a result index.
189                          * To fill fv_words we should not use those result
190                          * index, we only need the protocol ids and offsets.
191                          * We will skip all the fv_idx which stores result
192                          * index in them. We also need to skip any fv_idx which
193                          * has ICE_AQ_RECIPE_LKUP_IGNORE or 0 since it isn't a
194                          * valid offset value.
195                          */
196                         if (result_idxs[0] == rg_entry->fv_idx[i] ||
197                             result_idxs[1] == rg_entry->fv_idx[i] ||
198                             result_idxs[2] == rg_entry->fv_idx[i] ||
199                             result_idxs[3] == rg_entry->fv_idx[i] ||
200                             result_idxs[4] == rg_entry->fv_idx[i] ||
201                             rg_entry->fv_idx[i] == ICE_AQ_RECIPE_LKUP_IGNORE ||
202                             rg_entry->fv_idx[i] == 0)
203                                 continue;
204
205                         ice_find_prot_off(hw, ICE_BLK_SW, prof_id,
206                                           rg_entry->fv_idx[i], &prot, &off);
207                         lkup_exts->fv_words[fv_word_idx].prot_id = prot;
208                         lkup_exts->fv_words[fv_word_idx].off = off;
209                         fv_word_idx++;
210                 }
211                 /* populate rg_list with the data from the child entry of this
212                  * recipe
213                  */
214                 LIST_ADD(&rg_entry->l_entry, &recps[rid].rg_list);
215         }
216         lkup_exts->n_val_words = fv_word_idx;
217         recps[rid].n_grp_count = num_recps;
218         recps[rid].root_buf = (struct ice_aqc_recipe_data_elem *)
219                 ice_calloc(hw, recps[rid].n_grp_count,
220                            sizeof(struct ice_aqc_recipe_data_elem));
221         if (!recps[rid].root_buf)
222                 goto err_unroll;
223
224         ice_memcpy(recps[rid].root_buf, tmp, recps[rid].n_grp_count *
225                    sizeof(*recps[rid].root_buf), ICE_NONDMA_TO_NONDMA);
226         recps[rid].recp_created = true;
227         if (tmp[sub_recps].content.rid & ICE_AQ_RECIPE_ID_IS_ROOT)
228                 recps[rid].root_rid = rid;
229 err_unroll:
230         ice_free(hw, tmp);
231         return status;
232 }
233
234 /**
235  * ice_get_recp_to_prof_map - updates recipe to profile mapping
236  * @hw: pointer to hardware structure
237  *
238  * This function is used to populate recipe_to_profile matrix where index to
239  * this array is the recipe ID and the element is the mapping of which profiles
240  * is this recipe mapped to.
241  */
242 static void
243 ice_get_recp_to_prof_map(struct ice_hw *hw)
244 {
245         ice_declare_bitmap(r_bitmap, ICE_MAX_NUM_RECIPES);
246         u16 i;
247
248         for (i = 0; i < ICE_MAX_NUM_PROFILES; i++) {
249                 u16 j;
250
251                 ice_zero_bitmap(r_bitmap, ICE_MAX_NUM_RECIPES);
252                 if (ice_aq_get_recipe_to_profile(hw, i, (u8 *)r_bitmap, NULL))
253                         continue;
254
255                 for (j = 0; j < ICE_MAX_NUM_RECIPES; j++)
256                         if (ice_is_bit_set(r_bitmap, j))
257                                 ice_set_bit(i, recipe_to_profile[j]);
258         }
259 }
260
261 /**
262  * ice_init_def_sw_recp - initialize the recipe book keeping tables
263  * @hw: pointer to the HW struct
264  *
265  * Allocate memory for the entire recipe table and initialize the structures/
266  * entries corresponding to basic recipes.
267  */
268 enum ice_status ice_init_def_sw_recp(struct ice_hw *hw)
269 {
270         struct ice_sw_recipe *recps;
271         u8 i;
272
273         recps = (struct ice_sw_recipe *)
274                 ice_calloc(hw, ICE_MAX_NUM_RECIPES, sizeof(*recps));
275         if (!recps)
276                 return ICE_ERR_NO_MEMORY;
277
278         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
279                 recps[i].root_rid = i;
280                 INIT_LIST_HEAD(&recps[i].filt_rules);
281                 INIT_LIST_HEAD(&recps[i].filt_replay_rules);
282                 INIT_LIST_HEAD(&recps[i].rg_list);
283                 ice_init_lock(&recps[i].filt_rule_lock);
284         }
285
286         hw->switch_info->recp_list = recps;
287
288         return ICE_SUCCESS;
289 }
290
291 /**
292  * ice_aq_get_sw_cfg - get switch configuration
293  * @hw: pointer to the hardware structure
294  * @buf: pointer to the result buffer
295  * @buf_size: length of the buffer available for response
296  * @req_desc: pointer to requested descriptor
297  * @num_elems: pointer to number of elements
298  * @cd: pointer to command details structure or NULL
299  *
300  * Get switch configuration (0x0200) to be placed in 'buff'.
301  * This admin command returns information such as initial VSI/port number
302  * and switch ID it belongs to.
303  *
304  * NOTE: *req_desc is both an input/output parameter.
305  * The caller of this function first calls this function with *request_desc set
306  * to 0. If the response from f/w has *req_desc set to 0, all the switch
307  * configuration information has been returned; if non-zero (meaning not all
308  * the information was returned), the caller should call this function again
309  * with *req_desc set to the previous value returned by f/w to get the
310  * next block of switch configuration information.
311  *
312  * *num_elems is output only parameter. This reflects the number of elements
313  * in response buffer. The caller of this function to use *num_elems while
314  * parsing the response buffer.
315  */
316 static enum ice_status
317 ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp *buf,
318                   u16 buf_size, u16 *req_desc, u16 *num_elems,
319                   struct ice_sq_cd *cd)
320 {
321         struct ice_aqc_get_sw_cfg *cmd;
322         enum ice_status status;
323         struct ice_aq_desc desc;
324
325         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
326         cmd = &desc.params.get_sw_conf;
327         cmd->element = CPU_TO_LE16(*req_desc);
328
329         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
330         if (!status) {
331                 *req_desc = LE16_TO_CPU(cmd->element);
332                 *num_elems = LE16_TO_CPU(cmd->num_elems);
333         }
334
335         return status;
336 }
337
338
339 /**
340  * ice_alloc_sw - allocate resources specific to switch
341  * @hw: pointer to the HW struct
342  * @ena_stats: true to turn on VEB stats
343  * @shared_res: true for shared resource, false for dedicated resource
344  * @sw_id: switch ID returned
345  * @counter_id: VEB counter ID returned
346  *
347  * allocates switch resources (SWID and VEB counter) (0x0208)
348  */
349 enum ice_status
350 ice_alloc_sw(struct ice_hw *hw, bool ena_stats, bool shared_res, u16 *sw_id,
351              u16 *counter_id)
352 {
353         struct ice_aqc_alloc_free_res_elem *sw_buf;
354         struct ice_aqc_res_elem *sw_ele;
355         enum ice_status status;
356         u16 buf_len;
357
358         buf_len = sizeof(*sw_buf);
359         sw_buf = (struct ice_aqc_alloc_free_res_elem *)
360                    ice_malloc(hw, buf_len);
361         if (!sw_buf)
362                 return ICE_ERR_NO_MEMORY;
363
364         /* Prepare buffer for switch ID.
365          * The number of resource entries in buffer is passed as 1 since only a
366          * single switch/VEB instance is allocated, and hence a single sw_id
367          * is requested.
368          */
369         sw_buf->num_elems = CPU_TO_LE16(1);
370         sw_buf->res_type =
371                 CPU_TO_LE16(ICE_AQC_RES_TYPE_SWID |
372                             (shared_res ? ICE_AQC_RES_TYPE_FLAG_SHARED :
373                             ICE_AQC_RES_TYPE_FLAG_DEDICATED));
374
375         status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
376                                        ice_aqc_opc_alloc_res, NULL);
377
378         if (status)
379                 goto ice_alloc_sw_exit;
380
381         sw_ele = &sw_buf->elem[0];
382         *sw_id = LE16_TO_CPU(sw_ele->e.sw_resp);
383
384         if (ena_stats) {
385                 /* Prepare buffer for VEB Counter */
386                 enum ice_adminq_opc opc = ice_aqc_opc_alloc_res;
387                 struct ice_aqc_alloc_free_res_elem *counter_buf;
388                 struct ice_aqc_res_elem *counter_ele;
389
390                 counter_buf = (struct ice_aqc_alloc_free_res_elem *)
391                                 ice_malloc(hw, buf_len);
392                 if (!counter_buf) {
393                         status = ICE_ERR_NO_MEMORY;
394                         goto ice_alloc_sw_exit;
395                 }
396
397                 /* The number of resource entries in buffer is passed as 1 since
398                  * only a single switch/VEB instance is allocated, and hence a
399                  * single VEB counter is requested.
400                  */
401                 counter_buf->num_elems = CPU_TO_LE16(1);
402                 counter_buf->res_type =
403                         CPU_TO_LE16(ICE_AQC_RES_TYPE_VEB_COUNTER |
404                                     ICE_AQC_RES_TYPE_FLAG_DEDICATED);
405                 status = ice_aq_alloc_free_res(hw, 1, counter_buf, buf_len,
406                                                opc, NULL);
407
408                 if (status) {
409                         ice_free(hw, counter_buf);
410                         goto ice_alloc_sw_exit;
411                 }
412                 counter_ele = &counter_buf->elem[0];
413                 *counter_id = LE16_TO_CPU(counter_ele->e.sw_resp);
414                 ice_free(hw, counter_buf);
415         }
416
417 ice_alloc_sw_exit:
418         ice_free(hw, sw_buf);
419         return status;
420 }
421
422 /**
423  * ice_free_sw - free resources specific to switch
424  * @hw: pointer to the HW struct
425  * @sw_id: switch ID returned
426  * @counter_id: VEB counter ID returned
427  *
428  * free switch resources (SWID and VEB counter) (0x0209)
429  *
430  * NOTE: This function frees multiple resources. It continues
431  * releasing other resources even after it encounters error.
432  * The error code returned is the last error it encountered.
433  */
434 enum ice_status ice_free_sw(struct ice_hw *hw, u16 sw_id, u16 counter_id)
435 {
436         struct ice_aqc_alloc_free_res_elem *sw_buf, *counter_buf;
437         enum ice_status status, ret_status;
438         u16 buf_len;
439
440         buf_len = sizeof(*sw_buf);
441         sw_buf = (struct ice_aqc_alloc_free_res_elem *)
442                    ice_malloc(hw, buf_len);
443         if (!sw_buf)
444                 return ICE_ERR_NO_MEMORY;
445
446         /* Prepare buffer to free for switch ID res.
447          * The number of resource entries in buffer is passed as 1 since only a
448          * single switch/VEB instance is freed, and hence a single sw_id
449          * is released.
450          */
451         sw_buf->num_elems = CPU_TO_LE16(1);
452         sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_SWID);
453         sw_buf->elem[0].e.sw_resp = CPU_TO_LE16(sw_id);
454
455         ret_status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
456                                            ice_aqc_opc_free_res, NULL);
457
458         if (ret_status)
459                 ice_debug(hw, ICE_DBG_SW, "CQ CMD Buffer:\n");
460
461         /* Prepare buffer to free for VEB Counter resource */
462         counter_buf = (struct ice_aqc_alloc_free_res_elem *)
463                         ice_malloc(hw, buf_len);
464         if (!counter_buf) {
465                 ice_free(hw, sw_buf);
466                 return ICE_ERR_NO_MEMORY;
467         }
468
469         /* The number of resource entries in buffer is passed as 1 since only a
470          * single switch/VEB instance is freed, and hence a single VEB counter
471          * is released
472          */
473         counter_buf->num_elems = CPU_TO_LE16(1);
474         counter_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_VEB_COUNTER);
475         counter_buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
476
477         status = ice_aq_alloc_free_res(hw, 1, counter_buf, buf_len,
478                                        ice_aqc_opc_free_res, NULL);
479         if (status) {
480                 ice_debug(hw, ICE_DBG_SW,
481                           "VEB counter resource could not be freed\n");
482                 ret_status = status;
483         }
484
485         ice_free(hw, counter_buf);
486         ice_free(hw, sw_buf);
487         return ret_status;
488 }
489
490 /**
491  * ice_aq_add_vsi
492  * @hw: pointer to the HW struct
493  * @vsi_ctx: pointer to a VSI context struct
494  * @cd: pointer to command details structure or NULL
495  *
496  * Add a VSI context to the hardware (0x0210)
497  */
498 enum ice_status
499 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
500                struct ice_sq_cd *cd)
501 {
502         struct ice_aqc_add_update_free_vsi_resp *res;
503         struct ice_aqc_add_get_update_free_vsi *cmd;
504         struct ice_aq_desc desc;
505         enum ice_status status;
506
507         cmd = &desc.params.vsi_cmd;
508         res = &desc.params.add_update_free_vsi_res;
509
510         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
511
512         if (!vsi_ctx->alloc_from_pool)
513                 cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num |
514                                            ICE_AQ_VSI_IS_VALID);
515
516         cmd->vsi_flags = CPU_TO_LE16(vsi_ctx->flags);
517
518         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
519
520         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
521                                  sizeof(vsi_ctx->info), cd);
522
523         if (!status) {
524                 vsi_ctx->vsi_num = LE16_TO_CPU(res->vsi_num) & ICE_AQ_VSI_NUM_M;
525                 vsi_ctx->vsis_allocd = LE16_TO_CPU(res->vsi_used);
526                 vsi_ctx->vsis_unallocated = LE16_TO_CPU(res->vsi_free);
527         }
528
529         return status;
530 }
531
532 /**
533  * ice_aq_free_vsi
534  * @hw: pointer to the HW struct
535  * @vsi_ctx: pointer to a VSI context struct
536  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
537  * @cd: pointer to command details structure or NULL
538  *
539  * Free VSI context info from hardware (0x0213)
540  */
541 enum ice_status
542 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
543                 bool keep_vsi_alloc, struct ice_sq_cd *cd)
544 {
545         struct ice_aqc_add_update_free_vsi_resp *resp;
546         struct ice_aqc_add_get_update_free_vsi *cmd;
547         struct ice_aq_desc desc;
548         enum ice_status status;
549
550         cmd = &desc.params.vsi_cmd;
551         resp = &desc.params.add_update_free_vsi_res;
552
553         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
554
555         cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
556         if (keep_vsi_alloc)
557                 cmd->cmd_flags = CPU_TO_LE16(ICE_AQ_VSI_KEEP_ALLOC);
558
559         status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
560         if (!status) {
561                 vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
562                 vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
563         }
564
565         return status;
566 }
567
568 /**
569  * ice_aq_update_vsi
570  * @hw: pointer to the HW struct
571  * @vsi_ctx: pointer to a VSI context struct
572  * @cd: pointer to command details structure or NULL
573  *
574  * Update VSI context in the hardware (0x0211)
575  */
576 enum ice_status
577 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
578                   struct ice_sq_cd *cd)
579 {
580         struct ice_aqc_add_update_free_vsi_resp *resp;
581         struct ice_aqc_add_get_update_free_vsi *cmd;
582         struct ice_aq_desc desc;
583         enum ice_status status;
584
585         cmd = &desc.params.vsi_cmd;
586         resp = &desc.params.add_update_free_vsi_res;
587
588         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
589
590         cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
591
592         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
593
594         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
595                                  sizeof(vsi_ctx->info), cd);
596
597         if (!status) {
598                 vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
599                 vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
600         }
601
602         return status;
603 }
604
605 /**
606  * ice_is_vsi_valid - check whether the VSI is valid or not
607  * @hw: pointer to the HW struct
608  * @vsi_handle: VSI handle
609  *
610  * check whether the VSI is valid or not
611  */
612 bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
613 {
614         return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
615 }
616
617 /**
618  * ice_get_hw_vsi_num - return the HW VSI number
619  * @hw: pointer to the HW struct
620  * @vsi_handle: VSI handle
621  *
622  * return the HW VSI number
623  * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
624  */
625 u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
626 {
627         return hw->vsi_ctx[vsi_handle]->vsi_num;
628 }
629
630 /**
631  * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
632  * @hw: pointer to the HW struct
633  * @vsi_handle: VSI handle
634  *
635  * return the VSI context entry for a given VSI handle
636  */
637 struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
638 {
639         return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
640 }
641
642 /**
643  * ice_save_vsi_ctx - save the VSI context for a given VSI handle
644  * @hw: pointer to the HW struct
645  * @vsi_handle: VSI handle
646  * @vsi: VSI context pointer
647  *
648  * save the VSI context entry for a given VSI handle
649  */
650 static void
651 ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi)
652 {
653         hw->vsi_ctx[vsi_handle] = vsi;
654 }
655
656 /**
657  * ice_clear_vsi_q_ctx - clear VSI queue contexts for all TCs
658  * @hw: pointer to the HW struct
659  * @vsi_handle: VSI handle
660  */
661 static void ice_clear_vsi_q_ctx(struct ice_hw *hw, u16 vsi_handle)
662 {
663         struct ice_vsi_ctx *vsi;
664         u8 i;
665
666         vsi = ice_get_vsi_ctx(hw, vsi_handle);
667         if (!vsi)
668                 return;
669         ice_for_each_traffic_class(i) {
670                 if (vsi->lan_q_ctx[i]) {
671                         ice_free(hw, vsi->lan_q_ctx[i]);
672                         vsi->lan_q_ctx[i] = NULL;
673                 }
674         }
675 }
676
677 /**
678  * ice_clear_vsi_ctx - clear the VSI context entry
679  * @hw: pointer to the HW struct
680  * @vsi_handle: VSI handle
681  *
682  * clear the VSI context entry
683  */
684 static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
685 {
686         struct ice_vsi_ctx *vsi;
687
688         vsi = ice_get_vsi_ctx(hw, vsi_handle);
689         if (vsi) {
690                 ice_clear_vsi_q_ctx(hw, vsi_handle);
691                 ice_free(hw, vsi);
692                 hw->vsi_ctx[vsi_handle] = NULL;
693         }
694 }
695
696 /**
697  * ice_clear_all_vsi_ctx - clear all the VSI context entries
698  * @hw: pointer to the HW struct
699  */
700 void ice_clear_all_vsi_ctx(struct ice_hw *hw)
701 {
702         u16 i;
703
704         for (i = 0; i < ICE_MAX_VSI; i++)
705                 ice_clear_vsi_ctx(hw, i);
706 }
707
708 /**
709  * ice_add_vsi - add VSI context to the hardware and VSI handle list
710  * @hw: pointer to the HW struct
711  * @vsi_handle: unique VSI handle provided by drivers
712  * @vsi_ctx: pointer to a VSI context struct
713  * @cd: pointer to command details structure or NULL
714  *
715  * Add a VSI context to the hardware also add it into the VSI handle list.
716  * If this function gets called after reset for existing VSIs then update
717  * with the new HW VSI number in the corresponding VSI handle list entry.
718  */
719 enum ice_status
720 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
721             struct ice_sq_cd *cd)
722 {
723         struct ice_vsi_ctx *tmp_vsi_ctx;
724         enum ice_status status;
725
726         if (vsi_handle >= ICE_MAX_VSI)
727                 return ICE_ERR_PARAM;
728         status = ice_aq_add_vsi(hw, vsi_ctx, cd);
729         if (status)
730                 return status;
731         tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
732         if (!tmp_vsi_ctx) {
733                 /* Create a new VSI context */
734                 tmp_vsi_ctx = (struct ice_vsi_ctx *)
735                         ice_malloc(hw, sizeof(*tmp_vsi_ctx));
736                 if (!tmp_vsi_ctx) {
737                         ice_aq_free_vsi(hw, vsi_ctx, false, cd);
738                         return ICE_ERR_NO_MEMORY;
739                 }
740                 *tmp_vsi_ctx = *vsi_ctx;
741
742                 ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
743         } else {
744                 /* update with new HW VSI num */
745                 if (tmp_vsi_ctx->vsi_num != vsi_ctx->vsi_num)
746                         tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
747         }
748
749         return ICE_SUCCESS;
750 }
751
752 /**
753  * ice_free_vsi- free VSI context from hardware and VSI handle list
754  * @hw: pointer to the HW struct
755  * @vsi_handle: unique VSI handle
756  * @vsi_ctx: pointer to a VSI context struct
757  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
758  * @cd: pointer to command details structure or NULL
759  *
760  * Free VSI context info from hardware as well as from VSI handle list
761  */
762 enum ice_status
763 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
764              bool keep_vsi_alloc, struct ice_sq_cd *cd)
765 {
766         enum ice_status status;
767
768         if (!ice_is_vsi_valid(hw, vsi_handle))
769                 return ICE_ERR_PARAM;
770         vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
771         status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
772         if (!status)
773                 ice_clear_vsi_ctx(hw, vsi_handle);
774         return status;
775 }
776
777 /**
778  * ice_update_vsi
779  * @hw: pointer to the HW struct
780  * @vsi_handle: unique VSI handle
781  * @vsi_ctx: pointer to a VSI context struct
782  * @cd: pointer to command details structure or NULL
783  *
784  * Update VSI context in the hardware
785  */
786 enum ice_status
787 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
788                struct ice_sq_cd *cd)
789 {
790         if (!ice_is_vsi_valid(hw, vsi_handle))
791                 return ICE_ERR_PARAM;
792         vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
793         return ice_aq_update_vsi(hw, vsi_ctx, cd);
794 }
795
796 /**
797  * ice_aq_get_vsi_params
798  * @hw: pointer to the HW struct
799  * @vsi_ctx: pointer to a VSI context struct
800  * @cd: pointer to command details structure or NULL
801  *
802  * Get VSI context info from hardware (0x0212)
803  */
804 enum ice_status
805 ice_aq_get_vsi_params(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
806                       struct ice_sq_cd *cd)
807 {
808         struct ice_aqc_add_get_update_free_vsi *cmd;
809         struct ice_aqc_get_vsi_resp *resp;
810         struct ice_aq_desc desc;
811         enum ice_status status;
812
813         cmd = &desc.params.vsi_cmd;
814         resp = &desc.params.get_vsi_resp;
815
816         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_vsi_params);
817
818         cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
819
820         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
821                                  sizeof(vsi_ctx->info), cd);
822         if (!status) {
823                 vsi_ctx->vsi_num = LE16_TO_CPU(resp->vsi_num) &
824                                         ICE_AQ_VSI_NUM_M;
825                 vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
826                 vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
827         }
828
829         return status;
830 }
831
832 /**
833  * ice_aq_add_update_mir_rule - add/update a mirror rule
834  * @hw: pointer to the HW struct
835  * @rule_type: Rule Type
836  * @dest_vsi: VSI number to which packets will be mirrored
837  * @count: length of the list
838  * @mr_buf: buffer for list of mirrored VSI numbers
839  * @cd: pointer to command details structure or NULL
840  * @rule_id: Rule ID
841  *
842  * Add/Update Mirror Rule (0x260).
843  */
844 enum ice_status
845 ice_aq_add_update_mir_rule(struct ice_hw *hw, u16 rule_type, u16 dest_vsi,
846                            u16 count, struct ice_mir_rule_buf *mr_buf,
847                            struct ice_sq_cd *cd, u16 *rule_id)
848 {
849         struct ice_aqc_add_update_mir_rule *cmd;
850         struct ice_aq_desc desc;
851         enum ice_status status;
852         __le16 *mr_list = NULL;
853         u16 buf_size = 0;
854
855         switch (rule_type) {
856         case ICE_AQC_RULE_TYPE_VPORT_INGRESS:
857         case ICE_AQC_RULE_TYPE_VPORT_EGRESS:
858                 /* Make sure count and mr_buf are set for these rule_types */
859                 if (!(count && mr_buf))
860                         return ICE_ERR_PARAM;
861
862                 buf_size = count * sizeof(__le16);
863                 mr_list = (__le16 *)ice_malloc(hw, buf_size);
864                 if (!mr_list)
865                         return ICE_ERR_NO_MEMORY;
866                 break;
867         case ICE_AQC_RULE_TYPE_PPORT_INGRESS:
868         case ICE_AQC_RULE_TYPE_PPORT_EGRESS:
869                 /* Make sure count and mr_buf are not set for these
870                  * rule_types
871                  */
872                 if (count || mr_buf)
873                         return ICE_ERR_PARAM;
874                 break;
875         default:
876                 ice_debug(hw, ICE_DBG_SW,
877                           "Error due to unsupported rule_type %u\n", rule_type);
878                 return ICE_ERR_OUT_OF_RANGE;
879         }
880
881         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_update_mir_rule);
882
883         /* Pre-process 'mr_buf' items for add/update of virtual port
884          * ingress/egress mirroring (but not physical port ingress/egress
885          * mirroring)
886          */
887         if (mr_buf) {
888                 int i;
889
890                 for (i = 0; i < count; i++) {
891                         u16 id;
892
893                         id = mr_buf[i].vsi_idx & ICE_AQC_RULE_MIRRORED_VSI_M;
894
895                         /* Validate specified VSI number, make sure it is less
896                          * than ICE_MAX_VSI, if not return with error.
897                          */
898                         if (id >= ICE_MAX_VSI) {
899                                 ice_debug(hw, ICE_DBG_SW,
900                                           "Error VSI index (%u) out-of-range\n",
901                                           id);
902                                 ice_free(hw, mr_list);
903                                 return ICE_ERR_OUT_OF_RANGE;
904                         }
905
906                         /* add VSI to mirror rule */
907                         if (mr_buf[i].add)
908                                 mr_list[i] =
909                                         CPU_TO_LE16(id | ICE_AQC_RULE_ACT_M);
910                         else /* remove VSI from mirror rule */
911                                 mr_list[i] = CPU_TO_LE16(id);
912                 }
913         }
914
915         cmd = &desc.params.add_update_rule;
916         if ((*rule_id) != ICE_INVAL_MIRROR_RULE_ID)
917                 cmd->rule_id = CPU_TO_LE16(((*rule_id) & ICE_AQC_RULE_ID_M) |
918                                            ICE_AQC_RULE_ID_VALID_M);
919         cmd->rule_type = CPU_TO_LE16(rule_type & ICE_AQC_RULE_TYPE_M);
920         cmd->num_entries = CPU_TO_LE16(count);
921         cmd->dest = CPU_TO_LE16(dest_vsi);
922
923         status = ice_aq_send_cmd(hw, &desc, mr_list, buf_size, cd);
924         if (!status)
925                 *rule_id = LE16_TO_CPU(cmd->rule_id) & ICE_AQC_RULE_ID_M;
926
927         ice_free(hw, mr_list);
928
929         return status;
930 }
931
932 /**
933  * ice_aq_delete_mir_rule - delete a mirror rule
934  * @hw: pointer to the HW struct
935  * @rule_id: Mirror rule ID (to be deleted)
936  * @keep_allocd: if set, the VSI stays part of the PF allocated res,
937  *               otherwise it is returned to the shared pool
938  * @cd: pointer to command details structure or NULL
939  *
940  * Delete Mirror Rule (0x261).
941  */
942 enum ice_status
943 ice_aq_delete_mir_rule(struct ice_hw *hw, u16 rule_id, bool keep_allocd,
944                        struct ice_sq_cd *cd)
945 {
946         struct ice_aqc_delete_mir_rule *cmd;
947         struct ice_aq_desc desc;
948
949         /* rule_id should be in the range 0...63 */
950         if (rule_id >= ICE_MAX_NUM_MIRROR_RULES)
951                 return ICE_ERR_OUT_OF_RANGE;
952
953         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_del_mir_rule);
954
955         cmd = &desc.params.del_rule;
956         rule_id |= ICE_AQC_RULE_ID_VALID_M;
957         cmd->rule_id = CPU_TO_LE16(rule_id);
958
959         if (keep_allocd)
960                 cmd->flags = CPU_TO_LE16(ICE_AQC_FLAG_KEEP_ALLOCD_M);
961
962         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
963 }
964
965 /**
966  * ice_aq_alloc_free_vsi_list
967  * @hw: pointer to the HW struct
968  * @vsi_list_id: VSI list ID returned or used for lookup
969  * @lkup_type: switch rule filter lookup type
970  * @opc: switch rules population command type - pass in the command opcode
971  *
972  * allocates or free a VSI list resource
973  */
974 static enum ice_status
975 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
976                            enum ice_sw_lkup_type lkup_type,
977                            enum ice_adminq_opc opc)
978 {
979         struct ice_aqc_alloc_free_res_elem *sw_buf;
980         struct ice_aqc_res_elem *vsi_ele;
981         enum ice_status status;
982         u16 buf_len;
983
984         buf_len = sizeof(*sw_buf);
985         sw_buf = (struct ice_aqc_alloc_free_res_elem *)
986                 ice_malloc(hw, buf_len);
987         if (!sw_buf)
988                 return ICE_ERR_NO_MEMORY;
989         sw_buf->num_elems = CPU_TO_LE16(1);
990
991         if (lkup_type == ICE_SW_LKUP_MAC ||
992             lkup_type == ICE_SW_LKUP_MAC_VLAN ||
993             lkup_type == ICE_SW_LKUP_ETHERTYPE ||
994             lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
995             lkup_type == ICE_SW_LKUP_PROMISC ||
996             lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
997             lkup_type == ICE_SW_LKUP_LAST) {
998                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
999         } else if (lkup_type == ICE_SW_LKUP_VLAN) {
1000                 sw_buf->res_type =
1001                         CPU_TO_LE16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
1002         } else {
1003                 status = ICE_ERR_PARAM;
1004                 goto ice_aq_alloc_free_vsi_list_exit;
1005         }
1006
1007         if (opc == ice_aqc_opc_free_res)
1008                 sw_buf->elem[0].e.sw_resp = CPU_TO_LE16(*vsi_list_id);
1009
1010         status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
1011         if (status)
1012                 goto ice_aq_alloc_free_vsi_list_exit;
1013
1014         if (opc == ice_aqc_opc_alloc_res) {
1015                 vsi_ele = &sw_buf->elem[0];
1016                 *vsi_list_id = LE16_TO_CPU(vsi_ele->e.sw_resp);
1017         }
1018
1019 ice_aq_alloc_free_vsi_list_exit:
1020         ice_free(hw, sw_buf);
1021         return status;
1022 }
1023
1024 /**
1025  * ice_aq_set_storm_ctrl - Sets storm control configuration
1026  * @hw: pointer to the HW struct
1027  * @bcast_thresh: represents the upper threshold for broadcast storm control
1028  * @mcast_thresh: represents the upper threshold for multicast storm control
1029  * @ctl_bitmask: storm control control knobs
1030  *
1031  * Sets the storm control configuration (0x0280)
1032  */
1033 enum ice_status
1034 ice_aq_set_storm_ctrl(struct ice_hw *hw, u32 bcast_thresh, u32 mcast_thresh,
1035                       u32 ctl_bitmask)
1036 {
1037         struct ice_aqc_storm_cfg *cmd;
1038         struct ice_aq_desc desc;
1039
1040         cmd = &desc.params.storm_conf;
1041
1042         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_storm_cfg);
1043
1044         cmd->bcast_thresh_size = CPU_TO_LE32(bcast_thresh & ICE_AQ_THRESHOLD_M);
1045         cmd->mcast_thresh_size = CPU_TO_LE32(mcast_thresh & ICE_AQ_THRESHOLD_M);
1046         cmd->storm_ctrl_ctrl = CPU_TO_LE32(ctl_bitmask);
1047
1048         return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1049 }
1050
1051 /**
1052  * ice_aq_get_storm_ctrl - gets storm control configuration
1053  * @hw: pointer to the HW struct
1054  * @bcast_thresh: represents the upper threshold for broadcast storm control
1055  * @mcast_thresh: represents the upper threshold for multicast storm control
1056  * @ctl_bitmask: storm control control knobs
1057  *
1058  * Gets the storm control configuration (0x0281)
1059  */
1060 enum ice_status
1061 ice_aq_get_storm_ctrl(struct ice_hw *hw, u32 *bcast_thresh, u32 *mcast_thresh,
1062                       u32 *ctl_bitmask)
1063 {
1064         enum ice_status status;
1065         struct ice_aq_desc desc;
1066
1067         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_storm_cfg);
1068
1069         status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1070         if (!status) {
1071                 struct ice_aqc_storm_cfg *resp = &desc.params.storm_conf;
1072
1073                 if (bcast_thresh)
1074                         *bcast_thresh = LE32_TO_CPU(resp->bcast_thresh_size) &
1075                                 ICE_AQ_THRESHOLD_M;
1076                 if (mcast_thresh)
1077                         *mcast_thresh = LE32_TO_CPU(resp->mcast_thresh_size) &
1078                                 ICE_AQ_THRESHOLD_M;
1079                 if (ctl_bitmask)
1080                         *ctl_bitmask = LE32_TO_CPU(resp->storm_ctrl_ctrl);
1081         }
1082
1083         return status;
1084 }
1085
1086 /**
1087  * ice_aq_sw_rules - add/update/remove switch rules
1088  * @hw: pointer to the HW struct
1089  * @rule_list: pointer to switch rule population list
1090  * @rule_list_sz: total size of the rule list in bytes
1091  * @num_rules: number of switch rules in the rule_list
1092  * @opc: switch rules population command type - pass in the command opcode
1093  * @cd: pointer to command details structure or NULL
1094  *
1095  * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
1096  */
1097 static enum ice_status
1098 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
1099                 u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
1100 {
1101         struct ice_aq_desc desc;
1102
1103         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_sw_rules");
1104
1105         if (opc != ice_aqc_opc_add_sw_rules &&
1106             opc != ice_aqc_opc_update_sw_rules &&
1107             opc != ice_aqc_opc_remove_sw_rules)
1108                 return ICE_ERR_PARAM;
1109
1110         ice_fill_dflt_direct_cmd_desc(&desc, opc);
1111
1112         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
1113         desc.params.sw_rules.num_rules_fltr_entry_index =
1114                 CPU_TO_LE16(num_rules);
1115         return ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
1116 }
1117
1118 /**
1119  * ice_aq_add_recipe - add switch recipe
1120  * @hw: pointer to the HW struct
1121  * @s_recipe_list: pointer to switch rule population list
1122  * @num_recipes: number of switch recipes in the list
1123  * @cd: pointer to command details structure or NULL
1124  *
1125  * Add(0x0290)
1126  */
1127 enum ice_status
1128 ice_aq_add_recipe(struct ice_hw *hw,
1129                   struct ice_aqc_recipe_data_elem *s_recipe_list,
1130                   u16 num_recipes, struct ice_sq_cd *cd)
1131 {
1132         struct ice_aqc_add_get_recipe *cmd;
1133         struct ice_aq_desc desc;
1134         u16 buf_size;
1135
1136         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_add_recipe");
1137         cmd = &desc.params.add_get_recipe;
1138         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_recipe);
1139
1140         cmd->num_sub_recipes = CPU_TO_LE16(num_recipes);
1141         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
1142
1143         buf_size = num_recipes * sizeof(*s_recipe_list);
1144
1145         return ice_aq_send_cmd(hw, &desc, s_recipe_list, buf_size, cd);
1146 }
1147
1148 /**
1149  * ice_aq_get_recipe - get switch recipe
1150  * @hw: pointer to the HW struct
1151  * @s_recipe_list: pointer to switch rule population list
1152  * @num_recipes: pointer to the number of recipes (input and output)
1153  * @recipe_root: root recipe number of recipe(s) to retrieve
1154  * @cd: pointer to command details structure or NULL
1155  *
1156  * Get(0x0292)
1157  *
1158  * On input, *num_recipes should equal the number of entries in s_recipe_list.
1159  * On output, *num_recipes will equal the number of entries returned in
1160  * s_recipe_list.
1161  *
1162  * The caller must supply enough space in s_recipe_list to hold all possible
1163  * recipes and *num_recipes must equal ICE_MAX_NUM_RECIPES.
1164  */
1165 enum ice_status
1166 ice_aq_get_recipe(struct ice_hw *hw,
1167                   struct ice_aqc_recipe_data_elem *s_recipe_list,
1168                   u16 *num_recipes, u16 recipe_root, struct ice_sq_cd *cd)
1169 {
1170         struct ice_aqc_add_get_recipe *cmd;
1171         struct ice_aq_desc desc;
1172         enum ice_status status;
1173         u16 buf_size;
1174
1175         if (*num_recipes != ICE_MAX_NUM_RECIPES)
1176                 return ICE_ERR_PARAM;
1177
1178         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_get_recipe");
1179         cmd = &desc.params.add_get_recipe;
1180         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_recipe);
1181
1182         cmd->return_index = CPU_TO_LE16(recipe_root);
1183         cmd->num_sub_recipes = 0;
1184
1185         buf_size = *num_recipes * sizeof(*s_recipe_list);
1186
1187         status = ice_aq_send_cmd(hw, &desc, s_recipe_list, buf_size, cd);
1188         /* cppcheck-suppress constArgument */
1189         *num_recipes = LE16_TO_CPU(cmd->num_sub_recipes);
1190
1191         return status;
1192 }
1193
1194 /**
1195  * ice_aq_map_recipe_to_profile - Map recipe to packet profile
1196  * @hw: pointer to the HW struct
1197  * @profile_id: package profile ID to associate the recipe with
1198  * @r_bitmap: Recipe bitmap filled in and need to be returned as response
1199  * @cd: pointer to command details structure or NULL
1200  * Recipe to profile association (0x0291)
1201  */
1202 enum ice_status
1203 ice_aq_map_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u8 *r_bitmap,
1204                              struct ice_sq_cd *cd)
1205 {
1206         struct ice_aqc_recipe_to_profile *cmd;
1207         struct ice_aq_desc desc;
1208
1209         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_assoc_recipe_to_prof");
1210         cmd = &desc.params.recipe_to_profile;
1211         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_recipe_to_profile);
1212         cmd->profile_id = CPU_TO_LE16(profile_id);
1213         /* Set the recipe ID bit in the bitmask to let the device know which
1214          * profile we are associating the recipe to
1215          */
1216         ice_memcpy(cmd->recipe_assoc, r_bitmap, sizeof(cmd->recipe_assoc),
1217                    ICE_NONDMA_TO_NONDMA);
1218
1219         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1220 }
1221
1222 /**
1223  * ice_aq_get_recipe_to_profile - Map recipe to packet profile
1224  * @hw: pointer to the HW struct
1225  * @profile_id: package profile ID to associate the recipe with
1226  * @r_bitmap: Recipe bitmap filled in and need to be returned as response
1227  * @cd: pointer to command details structure or NULL
1228  * Associate profile ID with given recipe (0x0293)
1229  */
1230 enum ice_status
1231 ice_aq_get_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u8 *r_bitmap,
1232                              struct ice_sq_cd *cd)
1233 {
1234         struct ice_aqc_recipe_to_profile *cmd;
1235         struct ice_aq_desc desc;
1236         enum ice_status status;
1237
1238         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_get_recipe_to_prof");
1239         cmd = &desc.params.recipe_to_profile;
1240         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_recipe_to_profile);
1241         cmd->profile_id = CPU_TO_LE16(profile_id);
1242
1243         status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1244         if (!status)
1245                 ice_memcpy(r_bitmap, cmd->recipe_assoc,
1246                            sizeof(cmd->recipe_assoc), ICE_NONDMA_TO_NONDMA);
1247
1248         return status;
1249 }
1250
1251 /**
1252  * ice_alloc_recipe - add recipe resource
1253  * @hw: pointer to the hardware structure
1254  * @rid: recipe ID returned as response to AQ call
1255  */
1256 enum ice_status ice_alloc_recipe(struct ice_hw *hw, u16 *rid)
1257 {
1258         struct ice_aqc_alloc_free_res_elem *sw_buf;
1259         enum ice_status status;
1260         u16 buf_len;
1261
1262         buf_len = sizeof(*sw_buf);
1263         sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
1264         if (!sw_buf)
1265                 return ICE_ERR_NO_MEMORY;
1266
1267         sw_buf->num_elems = CPU_TO_LE16(1);
1268         sw_buf->res_type = CPU_TO_LE16((ICE_AQC_RES_TYPE_RECIPE <<
1269                                         ICE_AQC_RES_TYPE_S) |
1270                                         ICE_AQC_RES_TYPE_FLAG_SHARED);
1271         status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
1272                                        ice_aqc_opc_alloc_res, NULL);
1273         if (!status)
1274                 *rid = LE16_TO_CPU(sw_buf->elem[0].e.sw_resp);
1275         ice_free(hw, sw_buf);
1276
1277         return status;
1278 }
1279
1280 /* ice_init_port_info - Initialize port_info with switch configuration data
1281  * @pi: pointer to port_info
1282  * @vsi_port_num: VSI number or port number
1283  * @type: Type of switch element (port or VSI)
1284  * @swid: switch ID of the switch the element is attached to
1285  * @pf_vf_num: PF or VF number
1286  * @is_vf: true if the element is a VF, false otherwise
1287  */
1288 static void
1289 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
1290                    u16 swid, u16 pf_vf_num, bool is_vf)
1291 {
1292         switch (type) {
1293         case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
1294                 pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
1295                 pi->sw_id = swid;
1296                 pi->pf_vf_num = pf_vf_num;
1297                 pi->is_vf = is_vf;
1298                 pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
1299                 pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
1300                 break;
1301         default:
1302                 ice_debug(pi->hw, ICE_DBG_SW,
1303                           "incorrect VSI/port type received\n");
1304                 break;
1305         }
1306 }
1307
1308 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
1309  * @hw: pointer to the hardware structure
1310  */
1311 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
1312 {
1313         struct ice_aqc_get_sw_cfg_resp *rbuf;
1314         enum ice_status status;
1315         u16 num_total_ports;
1316         u16 req_desc = 0;
1317         u16 num_elems;
1318         u16 j = 0;
1319         u16 i;
1320
1321         num_total_ports = 1;
1322
1323         rbuf = (struct ice_aqc_get_sw_cfg_resp *)
1324                 ice_malloc(hw, ICE_SW_CFG_MAX_BUF_LEN);
1325
1326         if (!rbuf)
1327                 return ICE_ERR_NO_MEMORY;
1328
1329         /* Multiple calls to ice_aq_get_sw_cfg may be required
1330          * to get all the switch configuration information. The need
1331          * for additional calls is indicated by ice_aq_get_sw_cfg
1332          * writing a non-zero value in req_desc
1333          */
1334         do {
1335                 status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
1336                                            &req_desc, &num_elems, NULL);
1337
1338                 if (status)
1339                         break;
1340
1341                 for (i = 0; i < num_elems; i++) {
1342                         struct ice_aqc_get_sw_cfg_resp_elem *ele;
1343                         u16 pf_vf_num, swid, vsi_port_num;
1344                         bool is_vf = false;
1345                         u8 type;
1346
1347                         ele = rbuf[i].elements;
1348                         vsi_port_num = LE16_TO_CPU(ele->vsi_port_num) &
1349                                 ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
1350
1351                         pf_vf_num = LE16_TO_CPU(ele->pf_vf_num) &
1352                                 ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
1353
1354                         swid = LE16_TO_CPU(ele->swid);
1355
1356                         if (LE16_TO_CPU(ele->pf_vf_num) &
1357                             ICE_AQC_GET_SW_CONF_RESP_IS_VF)
1358                                 is_vf = true;
1359
1360                         type = LE16_TO_CPU(ele->vsi_port_num) >>
1361                                 ICE_AQC_GET_SW_CONF_RESP_TYPE_S;
1362
1363                         switch (type) {
1364                         case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
1365                         case ICE_AQC_GET_SW_CONF_RESP_VIRT_PORT:
1366                                 if (j == num_total_ports) {
1367                                         ice_debug(hw, ICE_DBG_SW,
1368                                                   "more ports than expected\n");
1369                                         status = ICE_ERR_CFG;
1370                                         goto out;
1371                                 }
1372                                 ice_init_port_info(hw->port_info,
1373                                                    vsi_port_num, type, swid,
1374                                                    pf_vf_num, is_vf);
1375                                 j++;
1376                                 break;
1377                         default:
1378                                 break;
1379                         }
1380                 }
1381         } while (req_desc && !status);
1382
1383
1384 out:
1385         ice_free(hw, (void *)rbuf);
1386         return status;
1387 }
1388
1389
1390 /**
1391  * ice_fill_sw_info - Helper function to populate lb_en and lan_en
1392  * @hw: pointer to the hardware structure
1393  * @fi: filter info structure to fill/update
1394  *
1395  * This helper function populates the lb_en and lan_en elements of the provided
1396  * ice_fltr_info struct using the switch's type and characteristics of the
1397  * switch rule being configured.
1398  */
1399 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
1400 {
1401         fi->lb_en = false;
1402         fi->lan_en = false;
1403         if ((fi->flag & ICE_FLTR_TX) &&
1404             (fi->fltr_act == ICE_FWD_TO_VSI ||
1405              fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1406              fi->fltr_act == ICE_FWD_TO_Q ||
1407              fi->fltr_act == ICE_FWD_TO_QGRP)) {
1408                 /* Setting LB for prune actions will result in replicated
1409                  * packets to the internal switch that will be dropped.
1410                  */
1411                 if (fi->lkup_type != ICE_SW_LKUP_VLAN)
1412                         fi->lb_en = true;
1413
1414                 /* Set lan_en to TRUE if
1415                  * 1. The switch is a VEB AND
1416                  * 2
1417                  * 2.1 The lookup is a directional lookup like ethertype,
1418                  * promiscuous, ethertype-MAC, promiscuous-VLAN
1419                  * and default-port OR
1420                  * 2.2 The lookup is VLAN, OR
1421                  * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
1422                  * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
1423                  *
1424                  * OR
1425                  *
1426                  * The switch is a VEPA.
1427                  *
1428                  * In all other cases, the LAN enable has to be set to false.
1429                  */
1430                 if (hw->evb_veb) {
1431                         if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1432                             fi->lkup_type == ICE_SW_LKUP_PROMISC ||
1433                             fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1434                             fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1435                             fi->lkup_type == ICE_SW_LKUP_DFLT ||
1436                             fi->lkup_type == ICE_SW_LKUP_VLAN ||
1437                             (fi->lkup_type == ICE_SW_LKUP_MAC &&
1438                              !IS_UNICAST_ETHER_ADDR(fi->l_data.mac.mac_addr)) ||
1439                             (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
1440                              !IS_UNICAST_ETHER_ADDR(fi->l_data.mac.mac_addr)))
1441                                 fi->lan_en = true;
1442                 } else {
1443                         fi->lan_en = true;
1444                 }
1445         }
1446 }
1447
1448 /**
1449  * ice_ilog2 - Calculates integer log base 2 of a number
1450  * @n: number on which to perform operation
1451  */
1452 static int ice_ilog2(u64 n)
1453 {
1454         int i;
1455
1456         for (i = 63; i >= 0; i--)
1457                 if (((u64)1 << i) & n)
1458                         return i;
1459
1460         return -1;
1461 }
1462
1463
1464 /**
1465  * ice_fill_sw_rule - Helper function to fill switch rule structure
1466  * @hw: pointer to the hardware structure
1467  * @f_info: entry containing packet forwarding information
1468  * @s_rule: switch rule structure to be filled in based on mac_entry
1469  * @opc: switch rules population command type - pass in the command opcode
1470  */
1471 static void
1472 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
1473                  struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
1474 {
1475         u16 vlan_id = ICE_MAX_VLAN_ID + 1;
1476         void *daddr = NULL;
1477         u16 eth_hdr_sz;
1478         u8 *eth_hdr;
1479         u32 act = 0;
1480         __be16 *off;
1481         u8 q_rgn;
1482
1483
1484         if (opc == ice_aqc_opc_remove_sw_rules) {
1485                 s_rule->pdata.lkup_tx_rx.act = 0;
1486                 s_rule->pdata.lkup_tx_rx.index =
1487                         CPU_TO_LE16(f_info->fltr_rule_id);
1488                 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
1489                 return;
1490         }
1491
1492         eth_hdr_sz = sizeof(dummy_eth_header);
1493         eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
1494
1495         /* initialize the ether header with a dummy header */
1496         ice_memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz, ICE_NONDMA_TO_NONDMA);
1497         ice_fill_sw_info(hw, f_info);
1498
1499         switch (f_info->fltr_act) {
1500         case ICE_FWD_TO_VSI:
1501                 act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
1502                         ICE_SINGLE_ACT_VSI_ID_M;
1503                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1504                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1505                                 ICE_SINGLE_ACT_VALID_BIT;
1506                 break;
1507         case ICE_FWD_TO_VSI_LIST:
1508                 act |= ICE_SINGLE_ACT_VSI_LIST;
1509                 act |= (f_info->fwd_id.vsi_list_id <<
1510                         ICE_SINGLE_ACT_VSI_LIST_ID_S) &
1511                         ICE_SINGLE_ACT_VSI_LIST_ID_M;
1512                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1513                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1514                                 ICE_SINGLE_ACT_VALID_BIT;
1515                 break;
1516         case ICE_FWD_TO_Q:
1517                 act |= ICE_SINGLE_ACT_TO_Q;
1518                 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1519                         ICE_SINGLE_ACT_Q_INDEX_M;
1520                 break;
1521         case ICE_DROP_PACKET:
1522                 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
1523                         ICE_SINGLE_ACT_VALID_BIT;
1524                 break;
1525         case ICE_FWD_TO_QGRP:
1526                 q_rgn = f_info->qgrp_size > 0 ?
1527                         (u8)ice_ilog2(f_info->qgrp_size) : 0;
1528                 act |= ICE_SINGLE_ACT_TO_Q;
1529                 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1530                         ICE_SINGLE_ACT_Q_INDEX_M;
1531                 act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
1532                         ICE_SINGLE_ACT_Q_REGION_M;
1533                 break;
1534         default:
1535                 return;
1536         }
1537
1538         if (f_info->lb_en)
1539                 act |= ICE_SINGLE_ACT_LB_ENABLE;
1540         if (f_info->lan_en)
1541                 act |= ICE_SINGLE_ACT_LAN_ENABLE;
1542
1543         switch (f_info->lkup_type) {
1544         case ICE_SW_LKUP_MAC:
1545                 daddr = f_info->l_data.mac.mac_addr;
1546                 break;
1547         case ICE_SW_LKUP_VLAN:
1548                 vlan_id = f_info->l_data.vlan.vlan_id;
1549                 if (f_info->fltr_act == ICE_FWD_TO_VSI ||
1550                     f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
1551                         act |= ICE_SINGLE_ACT_PRUNE;
1552                         act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
1553                 }
1554                 break;
1555         case ICE_SW_LKUP_ETHERTYPE_MAC:
1556                 daddr = f_info->l_data.ethertype_mac.mac_addr;
1557                 /* fall-through */
1558         case ICE_SW_LKUP_ETHERTYPE:
1559                 off = (__be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
1560                 *off = CPU_TO_BE16(f_info->l_data.ethertype_mac.ethertype);
1561                 break;
1562         case ICE_SW_LKUP_MAC_VLAN:
1563                 daddr = f_info->l_data.mac_vlan.mac_addr;
1564                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
1565                 break;
1566         case ICE_SW_LKUP_PROMISC_VLAN:
1567                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
1568                 /* fall-through */
1569         case ICE_SW_LKUP_PROMISC:
1570                 daddr = f_info->l_data.mac_vlan.mac_addr;
1571                 break;
1572         default:
1573                 break;
1574         }
1575
1576         s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
1577                 CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_RX) :
1578                 CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_TX);
1579
1580         /* Recipe set depending on lookup type */
1581         s_rule->pdata.lkup_tx_rx.recipe_id = CPU_TO_LE16(f_info->lkup_type);
1582         s_rule->pdata.lkup_tx_rx.src = CPU_TO_LE16(f_info->src);
1583         s_rule->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1584
1585         if (daddr)
1586                 ice_memcpy(eth_hdr + ICE_ETH_DA_OFFSET, daddr, ETH_ALEN,
1587                            ICE_NONDMA_TO_NONDMA);
1588
1589         if (!(vlan_id > ICE_MAX_VLAN_ID)) {
1590                 off = (__be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
1591                 *off = CPU_TO_BE16(vlan_id);
1592         }
1593
1594         /* Create the switch rule with the final dummy Ethernet header */
1595         if (opc != ice_aqc_opc_update_sw_rules)
1596                 s_rule->pdata.lkup_tx_rx.hdr_len = CPU_TO_LE16(eth_hdr_sz);
1597 }
1598
1599 /**
1600  * ice_add_marker_act
1601  * @hw: pointer to the hardware structure
1602  * @m_ent: the management entry for which sw marker needs to be added
1603  * @sw_marker: sw marker to tag the Rx descriptor with
1604  * @l_id: large action resource ID
1605  *
1606  * Create a large action to hold software marker and update the switch rule
1607  * entry pointed by m_ent with newly created large action
1608  */
1609 static enum ice_status
1610 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1611                    u16 sw_marker, u16 l_id)
1612 {
1613         struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
1614         /* For software marker we need 3 large actions
1615          * 1. FWD action: FWD TO VSI or VSI LIST
1616          * 2. GENERIC VALUE action to hold the profile ID
1617          * 3. GENERIC VALUE action to hold the software marker ID
1618          */
1619         const u16 num_lg_acts = 3;
1620         enum ice_status status;
1621         u16 lg_act_size;
1622         u16 rules_size;
1623         u32 act;
1624         u16 id;
1625
1626         if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1627                 return ICE_ERR_PARAM;
1628
1629         /* Create two back-to-back switch rules and submit them to the HW using
1630          * one memory buffer:
1631          *    1. Large Action
1632          *    2. Look up Tx Rx
1633          */
1634         lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
1635         rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1636         lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, rules_size);
1637         if (!lg_act)
1638                 return ICE_ERR_NO_MEMORY;
1639
1640         rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
1641
1642         /* Fill in the first switch rule i.e. large action */
1643         lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1644         lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1645         lg_act->pdata.lg_act.size = CPU_TO_LE16(num_lg_acts);
1646
1647         /* First action VSI forwarding or VSI list forwarding depending on how
1648          * many VSIs
1649          */
1650         id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
1651                 m_ent->fltr_info.fwd_id.hw_vsi_id;
1652
1653         act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1654         act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
1655                 ICE_LG_ACT_VSI_LIST_ID_M;
1656         if (m_ent->vsi_count > 1)
1657                 act |= ICE_LG_ACT_VSI_LIST;
1658         lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1659
1660         /* Second action descriptor type */
1661         act = ICE_LG_ACT_GENERIC;
1662
1663         act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
1664         lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1665
1666         act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
1667                ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
1668
1669         /* Third action Marker value */
1670         act |= ICE_LG_ACT_GENERIC;
1671         act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
1672                 ICE_LG_ACT_GENERIC_VALUE_M;
1673
1674         lg_act->pdata.lg_act.act[2] = CPU_TO_LE32(act);
1675
1676         /* call the fill switch rule to fill the lookup Tx Rx structure */
1677         ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1678                          ice_aqc_opc_update_sw_rules);
1679
1680         /* Update the action to point to the large action ID */
1681         rx_tx->pdata.lkup_tx_rx.act =
1682                 CPU_TO_LE32(ICE_SINGLE_ACT_PTR |
1683                             ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
1684                              ICE_SINGLE_ACT_PTR_VAL_M));
1685
1686         /* Use the filter rule ID of the previously created rule with single
1687          * act. Once the update happens, hardware will treat this as large
1688          * action
1689          */
1690         rx_tx->pdata.lkup_tx_rx.index =
1691                 CPU_TO_LE16(m_ent->fltr_info.fltr_rule_id);
1692
1693         status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1694                                  ice_aqc_opc_update_sw_rules, NULL);
1695         if (!status) {
1696                 m_ent->lg_act_idx = l_id;
1697                 m_ent->sw_marker_id = sw_marker;
1698         }
1699
1700         ice_free(hw, lg_act);
1701         return status;
1702 }
1703
1704 /**
1705  * ice_add_counter_act - add/update filter rule with counter action
1706  * @hw: pointer to the hardware structure
1707  * @m_ent: the management entry for which counter needs to be added
1708  * @counter_id: VLAN counter ID returned as part of allocate resource
1709  * @l_id: large action resource ID
1710  */
1711 static enum ice_status
1712 ice_add_counter_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1713                     u16 counter_id, u16 l_id)
1714 {
1715         struct ice_aqc_sw_rules_elem *lg_act;
1716         struct ice_aqc_sw_rules_elem *rx_tx;
1717         enum ice_status status;
1718         /* 2 actions will be added while adding a large action counter */
1719         const int num_acts = 2;
1720         u16 lg_act_size;
1721         u16 rules_size;
1722         u16 f_rule_id;
1723         u32 act;
1724         u16 id;
1725
1726         if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1727                 return ICE_ERR_PARAM;
1728
1729         /* Create two back-to-back switch rules and submit them to the HW using
1730          * one memory buffer:
1731          * 1. Large Action
1732          * 2. Look up Tx Rx
1733          */
1734         lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_acts);
1735         rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1736         lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw,
1737                                                                  rules_size);
1738         if (!lg_act)
1739                 return ICE_ERR_NO_MEMORY;
1740
1741         rx_tx = (struct ice_aqc_sw_rules_elem *)
1742                 ((u8 *)lg_act + lg_act_size);
1743
1744         /* Fill in the first switch rule i.e. large action */
1745         lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1746         lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1747         lg_act->pdata.lg_act.size = CPU_TO_LE16(num_acts);
1748
1749         /* First action VSI forwarding or VSI list forwarding depending on how
1750          * many VSIs
1751          */
1752         id = (m_ent->vsi_count > 1) ?  m_ent->fltr_info.fwd_id.vsi_list_id :
1753                 m_ent->fltr_info.fwd_id.hw_vsi_id;
1754
1755         act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1756         act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
1757                 ICE_LG_ACT_VSI_LIST_ID_M;
1758         if (m_ent->vsi_count > 1)
1759                 act |= ICE_LG_ACT_VSI_LIST;
1760         lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1761
1762         /* Second action counter ID */
1763         act = ICE_LG_ACT_STAT_COUNT;
1764         act |= (counter_id << ICE_LG_ACT_STAT_COUNT_S) &
1765                 ICE_LG_ACT_STAT_COUNT_M;
1766         lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1767
1768         /* call the fill switch rule to fill the lookup Tx Rx structure */
1769         ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1770                          ice_aqc_opc_update_sw_rules);
1771
1772         act = ICE_SINGLE_ACT_PTR;
1773         act |= (l_id << ICE_SINGLE_ACT_PTR_VAL_S) & ICE_SINGLE_ACT_PTR_VAL_M;
1774         rx_tx->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1775
1776         /* Use the filter rule ID of the previously created rule with single
1777          * act. Once the update happens, hardware will treat this as large
1778          * action
1779          */
1780         f_rule_id = m_ent->fltr_info.fltr_rule_id;
1781         rx_tx->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_rule_id);
1782
1783         status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1784                                  ice_aqc_opc_update_sw_rules, NULL);
1785         if (!status) {
1786                 m_ent->lg_act_idx = l_id;
1787                 m_ent->counter_index = counter_id;
1788         }
1789
1790         ice_free(hw, lg_act);
1791         return status;
1792 }
1793
1794 /**
1795  * ice_create_vsi_list_map
1796  * @hw: pointer to the hardware structure
1797  * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
1798  * @num_vsi: number of VSI handles in the array
1799  * @vsi_list_id: VSI list ID generated as part of allocate resource
1800  *
1801  * Helper function to create a new entry of VSI list ID to VSI mapping
1802  * using the given VSI list ID
1803  */
1804 static struct ice_vsi_list_map_info *
1805 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1806                         u16 vsi_list_id)
1807 {
1808         struct ice_switch_info *sw = hw->switch_info;
1809         struct ice_vsi_list_map_info *v_map;
1810         int i;
1811
1812         v_map = (struct ice_vsi_list_map_info *)ice_calloc(hw, 1,
1813                 sizeof(*v_map));
1814         if (!v_map)
1815                 return NULL;
1816
1817         v_map->vsi_list_id = vsi_list_id;
1818         v_map->ref_cnt = 1;
1819         for (i = 0; i < num_vsi; i++)
1820                 ice_set_bit(vsi_handle_arr[i], v_map->vsi_map);
1821
1822         LIST_ADD(&v_map->list_entry, &sw->vsi_list_map_head);
1823         return v_map;
1824 }
1825
1826 /**
1827  * ice_update_vsi_list_rule
1828  * @hw: pointer to the hardware structure
1829  * @vsi_handle_arr: array of VSI handles to form a VSI list
1830  * @num_vsi: number of VSI handles in the array
1831  * @vsi_list_id: VSI list ID generated as part of allocate resource
1832  * @remove: Boolean value to indicate if this is a remove action
1833  * @opc: switch rules population command type - pass in the command opcode
1834  * @lkup_type: lookup type of the filter
1835  *
1836  * Call AQ command to add a new switch rule or update existing switch rule
1837  * using the given VSI list ID
1838  */
1839 static enum ice_status
1840 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1841                          u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
1842                          enum ice_sw_lkup_type lkup_type)
1843 {
1844         struct ice_aqc_sw_rules_elem *s_rule;
1845         enum ice_status status;
1846         u16 s_rule_size;
1847         u16 type;
1848         int i;
1849
1850         if (!num_vsi)
1851                 return ICE_ERR_PARAM;
1852
1853         if (lkup_type == ICE_SW_LKUP_MAC ||
1854             lkup_type == ICE_SW_LKUP_MAC_VLAN ||
1855             lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1856             lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1857             lkup_type == ICE_SW_LKUP_PROMISC ||
1858             lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1859             lkup_type == ICE_SW_LKUP_LAST)
1860                 type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
1861                                 ICE_AQC_SW_RULES_T_VSI_LIST_SET;
1862         else if (lkup_type == ICE_SW_LKUP_VLAN)
1863                 type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
1864                                 ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
1865         else
1866                 return ICE_ERR_PARAM;
1867
1868         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
1869         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
1870         if (!s_rule)
1871                 return ICE_ERR_NO_MEMORY;
1872         for (i = 0; i < num_vsi; i++) {
1873                 if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
1874                         status = ICE_ERR_PARAM;
1875                         goto exit;
1876                 }
1877                 /* AQ call requires hw_vsi_id(s) */
1878                 s_rule->pdata.vsi_list.vsi[i] =
1879                         CPU_TO_LE16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
1880         }
1881
1882         s_rule->type = CPU_TO_LE16(type);
1883         s_rule->pdata.vsi_list.number_vsi = CPU_TO_LE16(num_vsi);
1884         s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
1885
1886         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
1887
1888 exit:
1889         ice_free(hw, s_rule);
1890         return status;
1891 }
1892
1893 /**
1894  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
1895  * @hw: pointer to the HW struct
1896  * @vsi_handle_arr: array of VSI handles to form a VSI list
1897  * @num_vsi: number of VSI handles in the array
1898  * @vsi_list_id: stores the ID of the VSI list to be created
1899  * @lkup_type: switch rule filter's lookup type
1900  */
1901 static enum ice_status
1902 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1903                          u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1904 {
1905         enum ice_status status;
1906
1907         status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1908                                             ice_aqc_opc_alloc_res);
1909         if (status)
1910                 return status;
1911
1912         /* Update the newly created VSI list to include the specified VSIs */
1913         return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1914                                         *vsi_list_id, false,
1915                                         ice_aqc_opc_add_sw_rules, lkup_type);
1916 }
1917
1918 /**
1919  * ice_create_pkt_fwd_rule
1920  * @hw: pointer to the hardware structure
1921  * @f_entry: entry containing packet forwarding information
1922  *
1923  * Create switch rule with given filter information and add an entry
1924  * to the corresponding filter management list to track this switch rule
1925  * and VSI mapping
1926  */
1927 static enum ice_status
1928 ice_create_pkt_fwd_rule(struct ice_hw *hw,
1929                         struct ice_fltr_list_entry *f_entry)
1930 {
1931         struct ice_fltr_mgmt_list_entry *fm_entry;
1932         struct ice_aqc_sw_rules_elem *s_rule;
1933         enum ice_sw_lkup_type l_type;
1934         struct ice_sw_recipe *recp;
1935         enum ice_status status;
1936
1937         s_rule = (struct ice_aqc_sw_rules_elem *)
1938                 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1939         if (!s_rule)
1940                 return ICE_ERR_NO_MEMORY;
1941         fm_entry = (struct ice_fltr_mgmt_list_entry *)
1942                    ice_malloc(hw, sizeof(*fm_entry));
1943         if (!fm_entry) {
1944                 status = ICE_ERR_NO_MEMORY;
1945                 goto ice_create_pkt_fwd_rule_exit;
1946         }
1947
1948         fm_entry->fltr_info = f_entry->fltr_info;
1949
1950         /* Initialize all the fields for the management entry */
1951         fm_entry->vsi_count = 1;
1952         fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1953         fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1954         fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1955
1956         ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1957                          ice_aqc_opc_add_sw_rules);
1958
1959         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1960                                  ice_aqc_opc_add_sw_rules, NULL);
1961         if (status) {
1962                 ice_free(hw, fm_entry);
1963                 goto ice_create_pkt_fwd_rule_exit;
1964         }
1965
1966         f_entry->fltr_info.fltr_rule_id =
1967                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1968         fm_entry->fltr_info.fltr_rule_id =
1969                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1970
1971         /* The book keeping entries will get removed when base driver
1972          * calls remove filter AQ command
1973          */
1974         l_type = fm_entry->fltr_info.lkup_type;
1975         recp = &hw->switch_info->recp_list[l_type];
1976         LIST_ADD(&fm_entry->list_entry, &recp->filt_rules);
1977
1978 ice_create_pkt_fwd_rule_exit:
1979         ice_free(hw, s_rule);
1980         return status;
1981 }
1982
1983 /**
1984  * ice_update_pkt_fwd_rule
1985  * @hw: pointer to the hardware structure
1986  * @f_info: filter information for switch rule
1987  *
1988  * Call AQ command to update a previously created switch rule with a
1989  * VSI list ID
1990  */
1991 static enum ice_status
1992 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1993 {
1994         struct ice_aqc_sw_rules_elem *s_rule;
1995         enum ice_status status;
1996
1997         s_rule = (struct ice_aqc_sw_rules_elem *)
1998                 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1999         if (!s_rule)
2000                 return ICE_ERR_NO_MEMORY;
2001
2002         ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
2003
2004         s_rule->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_info->fltr_rule_id);
2005
2006         /* Update switch rule with new rule set to forward VSI list */
2007         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
2008                                  ice_aqc_opc_update_sw_rules, NULL);
2009
2010         ice_free(hw, s_rule);
2011         return status;
2012 }
2013
2014 /**
2015  * ice_update_sw_rule_bridge_mode
2016  * @hw: pointer to the HW struct
2017  *
2018  * Updates unicast switch filter rules based on VEB/VEPA mode
2019  */
2020 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
2021 {
2022         struct ice_switch_info *sw = hw->switch_info;
2023         struct ice_fltr_mgmt_list_entry *fm_entry;
2024         enum ice_status status = ICE_SUCCESS;
2025         struct LIST_HEAD_TYPE *rule_head;
2026         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2027
2028         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2029         rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2030
2031         ice_acquire_lock(rule_lock);
2032         LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry,
2033                             list_entry) {
2034                 struct ice_fltr_info *fi = &fm_entry->fltr_info;
2035                 u8 *addr = fi->l_data.mac.mac_addr;
2036
2037                 /* Update unicast Tx rules to reflect the selected
2038                  * VEB/VEPA mode
2039                  */
2040                 if ((fi->flag & ICE_FLTR_TX) && IS_UNICAST_ETHER_ADDR(addr) &&
2041                     (fi->fltr_act == ICE_FWD_TO_VSI ||
2042                      fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
2043                      fi->fltr_act == ICE_FWD_TO_Q ||
2044                      fi->fltr_act == ICE_FWD_TO_QGRP)) {
2045                         status = ice_update_pkt_fwd_rule(hw, fi);
2046                         if (status)
2047                                 break;
2048                 }
2049         }
2050
2051         ice_release_lock(rule_lock);
2052
2053         return status;
2054 }
2055
2056 /**
2057  * ice_add_update_vsi_list
2058  * @hw: pointer to the hardware structure
2059  * @m_entry: pointer to current filter management list entry
2060  * @cur_fltr: filter information from the book keeping entry
2061  * @new_fltr: filter information with the new VSI to be added
2062  *
2063  * Call AQ command to add or update previously created VSI list with new VSI.
2064  *
2065  * Helper function to do book keeping associated with adding filter information
2066  * The algorithm to do the book keeping is described below :
2067  * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
2068  *      if only one VSI has been added till now
2069  *              Allocate a new VSI list and add two VSIs
2070  *              to this list using switch rule command
2071  *              Update the previously created switch rule with the
2072  *              newly created VSI list ID
2073  *      if a VSI list was previously created
2074  *              Add the new VSI to the previously created VSI list set
2075  *              using the update switch rule command
2076  */
2077 static enum ice_status
2078 ice_add_update_vsi_list(struct ice_hw *hw,
2079                         struct ice_fltr_mgmt_list_entry *m_entry,
2080                         struct ice_fltr_info *cur_fltr,
2081                         struct ice_fltr_info *new_fltr)
2082 {
2083         enum ice_status status = ICE_SUCCESS;
2084         u16 vsi_list_id = 0;
2085
2086         if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
2087              cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
2088                 return ICE_ERR_NOT_IMPL;
2089
2090         if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
2091              new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
2092             (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
2093              cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
2094                 return ICE_ERR_NOT_IMPL;
2095
2096         if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
2097                 /* Only one entry existed in the mapping and it was not already
2098                  * a part of a VSI list. So, create a VSI list with the old and
2099                  * new VSIs.
2100                  */
2101                 struct ice_fltr_info tmp_fltr;
2102                 u16 vsi_handle_arr[2];
2103
2104                 /* A rule already exists with the new VSI being added */
2105                 if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
2106                         return ICE_ERR_ALREADY_EXISTS;
2107
2108                 vsi_handle_arr[0] = cur_fltr->vsi_handle;
2109                 vsi_handle_arr[1] = new_fltr->vsi_handle;
2110                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2111                                                   &vsi_list_id,
2112                                                   new_fltr->lkup_type);
2113                 if (status)
2114                         return status;
2115
2116                 tmp_fltr = *new_fltr;
2117                 tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
2118                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2119                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2120                 /* Update the previous switch rule of "MAC forward to VSI" to
2121                  * "MAC fwd to VSI list"
2122                  */
2123                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2124                 if (status)
2125                         return status;
2126
2127                 cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
2128                 cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2129                 m_entry->vsi_list_info =
2130                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2131                                                 vsi_list_id);
2132
2133                 /* If this entry was large action then the large action needs
2134                  * to be updated to point to FWD to VSI list
2135                  */
2136                 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
2137                         status =
2138                             ice_add_marker_act(hw, m_entry,
2139                                                m_entry->sw_marker_id,
2140                                                m_entry->lg_act_idx);
2141         } else {
2142                 u16 vsi_handle = new_fltr->vsi_handle;
2143                 enum ice_adminq_opc opcode;
2144
2145                 if (!m_entry->vsi_list_info)
2146                         return ICE_ERR_CFG;
2147
2148                 /* A rule already exists with the new VSI being added */
2149                 if (ice_is_bit_set(m_entry->vsi_list_info->vsi_map, vsi_handle))
2150                         return ICE_SUCCESS;
2151
2152                 /* Update the previously created VSI list set with
2153                  * the new VSI ID passed in
2154                  */
2155                 vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
2156                 opcode = ice_aqc_opc_update_sw_rules;
2157
2158                 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
2159                                                   vsi_list_id, false, opcode,
2160                                                   new_fltr->lkup_type);
2161                 /* update VSI list mapping info with new VSI ID */
2162                 if (!status)
2163                         ice_set_bit(vsi_handle,
2164                                     m_entry->vsi_list_info->vsi_map);
2165         }
2166         if (!status)
2167                 m_entry->vsi_count++;
2168         return status;
2169 }
2170
2171 /**
2172  * ice_find_rule_entry - Search a rule entry
2173  * @hw: pointer to the hardware structure
2174  * @recp_id: lookup type for which the specified rule needs to be searched
2175  * @f_info: rule information
2176  *
2177  * Helper function to search for a given rule entry
2178  * Returns pointer to entry storing the rule if found
2179  */
2180 static struct ice_fltr_mgmt_list_entry *
2181 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
2182 {
2183         struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
2184         struct ice_switch_info *sw = hw->switch_info;
2185         struct LIST_HEAD_TYPE *list_head;
2186
2187         list_head = &sw->recp_list[recp_id].filt_rules;
2188         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
2189                             list_entry) {
2190                 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
2191                             sizeof(f_info->l_data)) &&
2192                     f_info->flag == list_itr->fltr_info.flag) {
2193                         ret = list_itr;
2194                         break;
2195                 }
2196         }
2197         return ret;
2198 }
2199
2200 /**
2201  * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
2202  * @hw: pointer to the hardware structure
2203  * @recp_id: lookup type for which VSI lists needs to be searched
2204  * @vsi_handle: VSI handle to be found in VSI list
2205  * @vsi_list_id: VSI list ID found containing vsi_handle
2206  *
2207  * Helper function to search a VSI list with single entry containing given VSI
2208  * handle element. This can be extended further to search VSI list with more
2209  * than 1 vsi_count. Returns pointer to VSI list entry if found.
2210  */
2211 static struct ice_vsi_list_map_info *
2212 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
2213                         u16 *vsi_list_id)
2214 {
2215         struct ice_vsi_list_map_info *map_info = NULL;
2216         struct ice_switch_info *sw = hw->switch_info;
2217         struct LIST_HEAD_TYPE *list_head;
2218
2219         list_head = &sw->recp_list[recp_id].filt_rules;
2220         if (sw->recp_list[recp_id].adv_rule) {
2221                 struct ice_adv_fltr_mgmt_list_entry *list_itr;
2222
2223                 LIST_FOR_EACH_ENTRY(list_itr, list_head,
2224                                     ice_adv_fltr_mgmt_list_entry,
2225                                     list_entry) {
2226                         if (list_itr->vsi_list_info) {
2227                                 map_info = list_itr->vsi_list_info;
2228                                 if (ice_is_bit_set(map_info->vsi_map,
2229                                                    vsi_handle)) {
2230                                         *vsi_list_id = map_info->vsi_list_id;
2231                                         return map_info;
2232                                 }
2233                         }
2234                 }
2235         } else {
2236                 struct ice_fltr_mgmt_list_entry *list_itr;
2237
2238                 LIST_FOR_EACH_ENTRY(list_itr, list_head,
2239                                     ice_fltr_mgmt_list_entry,
2240                                     list_entry) {
2241                         if (list_itr->vsi_count == 1 &&
2242                             list_itr->vsi_list_info) {
2243                                 map_info = list_itr->vsi_list_info;
2244                                 if (ice_is_bit_set(map_info->vsi_map,
2245                                                    vsi_handle)) {
2246                                         *vsi_list_id = map_info->vsi_list_id;
2247                                         return map_info;
2248                                 }
2249                         }
2250                 }
2251         }
2252         return NULL;
2253 }
2254
2255 /**
2256  * ice_add_rule_internal - add rule for a given lookup type
2257  * @hw: pointer to the hardware structure
2258  * @recp_id: lookup type (recipe ID) for which rule has to be added
2259  * @f_entry: structure containing MAC forwarding information
2260  *
2261  * Adds or updates the rule lists for a given recipe
2262  */
2263 static enum ice_status
2264 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
2265                       struct ice_fltr_list_entry *f_entry)
2266 {
2267         struct ice_switch_info *sw = hw->switch_info;
2268         struct ice_fltr_info *new_fltr, *cur_fltr;
2269         struct ice_fltr_mgmt_list_entry *m_entry;
2270         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2271         enum ice_status status = ICE_SUCCESS;
2272
2273         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2274                 return ICE_ERR_PARAM;
2275
2276         /* Load the hw_vsi_id only if the fwd action is fwd to VSI */
2277         if (f_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI)
2278                 f_entry->fltr_info.fwd_id.hw_vsi_id =
2279                         ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2280
2281         rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
2282
2283         ice_acquire_lock(rule_lock);
2284         new_fltr = &f_entry->fltr_info;
2285         if (new_fltr->flag & ICE_FLTR_RX)
2286                 new_fltr->src = hw->port_info->lport;
2287         else if (new_fltr->flag & ICE_FLTR_TX)
2288                 new_fltr->src =
2289                         ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2290
2291         m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
2292         if (!m_entry) {
2293                 ice_release_lock(rule_lock);
2294                 return ice_create_pkt_fwd_rule(hw, f_entry);
2295         }
2296
2297         cur_fltr = &m_entry->fltr_info;
2298         status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
2299         ice_release_lock(rule_lock);
2300
2301         return status;
2302 }
2303
2304 /**
2305  * ice_remove_vsi_list_rule
2306  * @hw: pointer to the hardware structure
2307  * @vsi_list_id: VSI list ID generated as part of allocate resource
2308  * @lkup_type: switch rule filter lookup type
2309  *
2310  * The VSI list should be emptied before this function is called to remove the
2311  * VSI list.
2312  */
2313 static enum ice_status
2314 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
2315                          enum ice_sw_lkup_type lkup_type)
2316 {
2317         struct ice_aqc_sw_rules_elem *s_rule;
2318         enum ice_status status;
2319         u16 s_rule_size;
2320
2321         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
2322         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
2323         if (!s_rule)
2324                 return ICE_ERR_NO_MEMORY;
2325
2326         s_rule->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
2327         s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
2328
2329         /* Free the vsi_list resource that we allocated. It is assumed that the
2330          * list is empty at this point.
2331          */
2332         status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
2333                                             ice_aqc_opc_free_res);
2334
2335         ice_free(hw, s_rule);
2336         return status;
2337 }
2338
2339 /**
2340  * ice_rem_update_vsi_list
2341  * @hw: pointer to the hardware structure
2342  * @vsi_handle: VSI handle of the VSI to remove
2343  * @fm_list: filter management entry for which the VSI list management needs to
2344  *           be done
2345  */
2346 static enum ice_status
2347 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
2348                         struct ice_fltr_mgmt_list_entry *fm_list)
2349 {
2350         enum ice_sw_lkup_type lkup_type;
2351         enum ice_status status = ICE_SUCCESS;
2352         u16 vsi_list_id;
2353
2354         if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
2355             fm_list->vsi_count == 0)
2356                 return ICE_ERR_PARAM;
2357
2358         /* A rule with the VSI being removed does not exist */
2359         if (!ice_is_bit_set(fm_list->vsi_list_info->vsi_map, vsi_handle))
2360                 return ICE_ERR_DOES_NOT_EXIST;
2361
2362         lkup_type = fm_list->fltr_info.lkup_type;
2363         vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
2364         status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
2365                                           ice_aqc_opc_update_sw_rules,
2366                                           lkup_type);
2367         if (status)
2368                 return status;
2369
2370         fm_list->vsi_count--;
2371         ice_clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
2372
2373         if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
2374                 struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
2375                 struct ice_vsi_list_map_info *vsi_list_info =
2376                         fm_list->vsi_list_info;
2377                 u16 rem_vsi_handle;
2378
2379                 rem_vsi_handle = ice_find_first_bit(vsi_list_info->vsi_map,
2380                                                     ICE_MAX_VSI);
2381                 if (!ice_is_vsi_valid(hw, rem_vsi_handle))
2382                         return ICE_ERR_OUT_OF_RANGE;
2383
2384                 /* Make sure VSI list is empty before removing it below */
2385                 status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
2386                                                   vsi_list_id, true,
2387                                                   ice_aqc_opc_update_sw_rules,
2388                                                   lkup_type);
2389                 if (status)
2390                         return status;
2391
2392                 tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
2393                 tmp_fltr_info.fwd_id.hw_vsi_id =
2394                         ice_get_hw_vsi_num(hw, rem_vsi_handle);
2395                 tmp_fltr_info.vsi_handle = rem_vsi_handle;
2396                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
2397                 if (status) {
2398                         ice_debug(hw, ICE_DBG_SW,
2399                                   "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
2400                                   tmp_fltr_info.fwd_id.hw_vsi_id, status);
2401                         return status;
2402                 }
2403
2404                 fm_list->fltr_info = tmp_fltr_info;
2405         }
2406
2407         if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
2408             (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
2409                 struct ice_vsi_list_map_info *vsi_list_info =
2410                         fm_list->vsi_list_info;
2411
2412                 /* Remove the VSI list since it is no longer used */
2413                 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
2414                 if (status) {
2415                         ice_debug(hw, ICE_DBG_SW,
2416                                   "Failed to remove VSI list %d, error %d\n",
2417                                   vsi_list_id, status);
2418                         return status;
2419                 }
2420
2421                 LIST_DEL(&vsi_list_info->list_entry);
2422                 ice_free(hw, vsi_list_info);
2423                 fm_list->vsi_list_info = NULL;
2424         }
2425
2426         return status;
2427 }
2428
2429 /**
2430  * ice_remove_rule_internal - Remove a filter rule of a given type
2431  *
2432  * @hw: pointer to the hardware structure
2433  * @recp_id: recipe ID for which the rule needs to removed
2434  * @f_entry: rule entry containing filter information
2435  */
2436 static enum ice_status
2437 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
2438                          struct ice_fltr_list_entry *f_entry)
2439 {
2440         struct ice_switch_info *sw = hw->switch_info;
2441         struct ice_fltr_mgmt_list_entry *list_elem;
2442         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2443         enum ice_status status = ICE_SUCCESS;
2444         bool remove_rule = false;
2445         u16 vsi_handle;
2446
2447         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2448                 return ICE_ERR_PARAM;
2449         f_entry->fltr_info.fwd_id.hw_vsi_id =
2450                 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2451
2452         rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
2453         ice_acquire_lock(rule_lock);
2454         list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
2455         if (!list_elem) {
2456                 status = ICE_ERR_DOES_NOT_EXIST;
2457                 goto exit;
2458         }
2459
2460         if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
2461                 remove_rule = true;
2462         } else if (!list_elem->vsi_list_info) {
2463                 status = ICE_ERR_DOES_NOT_EXIST;
2464                 goto exit;
2465         } else if (list_elem->vsi_list_info->ref_cnt > 1) {
2466                 /* a ref_cnt > 1 indicates that the vsi_list is being
2467                  * shared by multiple rules. Decrement the ref_cnt and
2468                  * remove this rule, but do not modify the list, as it
2469                  * is in-use by other rules.
2470                  */
2471                 list_elem->vsi_list_info->ref_cnt--;
2472                 remove_rule = true;
2473         } else {
2474                 /* a ref_cnt of 1 indicates the vsi_list is only used
2475                  * by one rule. However, the original removal request is only
2476                  * for a single VSI. Update the vsi_list first, and only
2477                  * remove the rule if there are no further VSIs in this list.
2478                  */
2479                 vsi_handle = f_entry->fltr_info.vsi_handle;
2480                 status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
2481                 if (status)
2482                         goto exit;
2483                 /* if VSI count goes to zero after updating the VSI list */
2484                 if (list_elem->vsi_count == 0)
2485                         remove_rule = true;
2486         }
2487
2488         if (remove_rule) {
2489                 /* Remove the lookup rule */
2490                 struct ice_aqc_sw_rules_elem *s_rule;
2491
2492                 s_rule = (struct ice_aqc_sw_rules_elem *)
2493                         ice_malloc(hw, ICE_SW_RULE_RX_TX_NO_HDR_SIZE);
2494                 if (!s_rule) {
2495                         status = ICE_ERR_NO_MEMORY;
2496                         goto exit;
2497                 }
2498
2499                 ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
2500                                  ice_aqc_opc_remove_sw_rules);
2501
2502                 status = ice_aq_sw_rules(hw, s_rule,
2503                                          ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
2504                                          ice_aqc_opc_remove_sw_rules, NULL);
2505                 if (status)
2506                         goto exit;
2507
2508                 /* Remove a book keeping from the list */
2509                 ice_free(hw, s_rule);
2510
2511                 LIST_DEL(&list_elem->list_entry);
2512                 ice_free(hw, list_elem);
2513         }
2514 exit:
2515         ice_release_lock(rule_lock);
2516         return status;
2517 }
2518
2519 /**
2520  * ice_aq_get_res_alloc - get allocated resources
2521  * @hw: pointer to the HW struct
2522  * @num_entries: pointer to u16 to store the number of resource entries returned
2523  * @buf: pointer to user-supplied buffer
2524  * @buf_size: size of buff
2525  * @cd: pointer to command details structure or NULL
2526  *
2527  * The user-supplied buffer must be large enough to store the resource
2528  * information for all resource types. Each resource type is an
2529  * ice_aqc_get_res_resp_data_elem structure.
2530  */
2531 enum ice_status
2532 ice_aq_get_res_alloc(struct ice_hw *hw, u16 *num_entries, void *buf,
2533                      u16 buf_size, struct ice_sq_cd *cd)
2534 {
2535         struct ice_aqc_get_res_alloc *resp;
2536         enum ice_status status;
2537         struct ice_aq_desc desc;
2538
2539         if (!buf)
2540                 return ICE_ERR_BAD_PTR;
2541
2542         if (buf_size < ICE_AQ_GET_RES_ALLOC_BUF_LEN)
2543                 return ICE_ERR_INVAL_SIZE;
2544
2545         resp = &desc.params.get_res;
2546
2547         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_res_alloc);
2548         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2549
2550         if (!status && num_entries)
2551                 *num_entries = LE16_TO_CPU(resp->resp_elem_num);
2552
2553         return status;
2554 }
2555
2556 /**
2557  * ice_aq_get_res_descs - get allocated resource descriptors
2558  * @hw: pointer to the hardware structure
2559  * @num_entries: number of resource entries in buffer
2560  * @buf: Indirect buffer to hold data parameters and response
2561  * @buf_size: size of buffer for indirect commands
2562  * @res_type: resource type
2563  * @res_shared: is resource shared
2564  * @desc_id: input - first desc ID to start; output - next desc ID
2565  * @cd: pointer to command details structure or NULL
2566  */
2567 enum ice_status
2568 ice_aq_get_res_descs(struct ice_hw *hw, u16 num_entries,
2569                      struct ice_aqc_get_allocd_res_desc_resp *buf,
2570                      u16 buf_size, u16 res_type, bool res_shared, u16 *desc_id,
2571                      struct ice_sq_cd *cd)
2572 {
2573         struct ice_aqc_get_allocd_res_desc *cmd;
2574         struct ice_aq_desc desc;
2575         enum ice_status status;
2576
2577         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_get_res_descs");
2578
2579         cmd = &desc.params.get_res_desc;
2580
2581         if (!buf)
2582                 return ICE_ERR_PARAM;
2583
2584         if (buf_size != (num_entries * sizeof(*buf)))
2585                 return ICE_ERR_PARAM;
2586
2587         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_allocd_res_desc);
2588
2589         cmd->ops.cmd.res = CPU_TO_LE16(((res_type << ICE_AQC_RES_TYPE_S) &
2590                                          ICE_AQC_RES_TYPE_M) | (res_shared ?
2591                                         ICE_AQC_RES_TYPE_FLAG_SHARED : 0));
2592         cmd->ops.cmd.first_desc = CPU_TO_LE16(*desc_id);
2593
2594         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
2595
2596         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2597         if (!status)
2598                 *desc_id = LE16_TO_CPU(cmd->ops.resp.next_desc);
2599
2600         return status;
2601 }
2602
2603 /**
2604  * ice_add_mac - Add a MAC address based filter rule
2605  * @hw: pointer to the hardware structure
2606  * @m_list: list of MAC addresses and forwarding information
2607  *
2608  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
2609  * multiple unicast addresses, the function assumes that all the
2610  * addresses are unique in a given add_mac call. It doesn't
2611  * check for duplicates in this case, removing duplicates from a given
2612  * list should be taken care of in the caller of this function.
2613  */
2614 enum ice_status
2615 ice_add_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2616 {
2617         struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
2618         struct ice_fltr_list_entry *m_list_itr;
2619         struct LIST_HEAD_TYPE *rule_head;
2620         u16 elem_sent, total_elem_left;
2621         struct ice_switch_info *sw;
2622         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2623         enum ice_status status = ICE_SUCCESS;
2624         u16 num_unicast = 0;
2625         u16 s_rule_size;
2626
2627         if (!m_list || !hw)
2628                 return ICE_ERR_PARAM;
2629         s_rule = NULL;
2630         sw = hw->switch_info;
2631         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2632         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2633                             list_entry) {
2634                 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
2635                 u16 vsi_handle;
2636                 u16 hw_vsi_id;
2637
2638                 m_list_itr->fltr_info.flag = ICE_FLTR_TX;
2639                 vsi_handle = m_list_itr->fltr_info.vsi_handle;
2640                 if (!ice_is_vsi_valid(hw, vsi_handle))
2641                         return ICE_ERR_PARAM;
2642                 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2643                 m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
2644                 /* update the src in case it is VSI num */
2645                 if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
2646                         return ICE_ERR_PARAM;
2647                 m_list_itr->fltr_info.src = hw_vsi_id;
2648                 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
2649                     IS_ZERO_ETHER_ADDR(add))
2650                         return ICE_ERR_PARAM;
2651                 if (IS_UNICAST_ETHER_ADDR(add) && !hw->ucast_shared) {
2652                         /* Don't overwrite the unicast address */
2653                         ice_acquire_lock(rule_lock);
2654                         if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
2655                                                 &m_list_itr->fltr_info)) {
2656                                 ice_release_lock(rule_lock);
2657                                 return ICE_ERR_ALREADY_EXISTS;
2658                         }
2659                         ice_release_lock(rule_lock);
2660                         num_unicast++;
2661                 } else if (IS_MULTICAST_ETHER_ADDR(add) ||
2662                            (IS_UNICAST_ETHER_ADDR(add) && hw->ucast_shared)) {
2663                         m_list_itr->status =
2664                                 ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
2665                                                       m_list_itr);
2666                         if (m_list_itr->status)
2667                                 return m_list_itr->status;
2668                 }
2669         }
2670
2671         ice_acquire_lock(rule_lock);
2672         /* Exit if no suitable entries were found for adding bulk switch rule */
2673         if (!num_unicast) {
2674                 status = ICE_SUCCESS;
2675                 goto ice_add_mac_exit;
2676         }
2677
2678         rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2679
2680         /* Allocate switch rule buffer for the bulk update for unicast */
2681         s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
2682         s_rule = (struct ice_aqc_sw_rules_elem *)
2683                 ice_calloc(hw, num_unicast, s_rule_size);
2684         if (!s_rule) {
2685                 status = ICE_ERR_NO_MEMORY;
2686                 goto ice_add_mac_exit;
2687         }
2688
2689         r_iter = s_rule;
2690         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2691                             list_entry) {
2692                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2693                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2694
2695                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2696                         ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
2697                                          ice_aqc_opc_add_sw_rules);
2698                         r_iter = (struct ice_aqc_sw_rules_elem *)
2699                                 ((u8 *)r_iter + s_rule_size);
2700                 }
2701         }
2702
2703         /* Call AQ bulk switch rule update for all unicast addresses */
2704         r_iter = s_rule;
2705         /* Call AQ switch rule in AQ_MAX chunk */
2706         for (total_elem_left = num_unicast; total_elem_left > 0;
2707              total_elem_left -= elem_sent) {
2708                 struct ice_aqc_sw_rules_elem *entry = r_iter;
2709
2710                 elem_sent = min(total_elem_left,
2711                                 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
2712                 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
2713                                          elem_sent, ice_aqc_opc_add_sw_rules,
2714                                          NULL);
2715                 if (status)
2716                         goto ice_add_mac_exit;
2717                 r_iter = (struct ice_aqc_sw_rules_elem *)
2718                         ((u8 *)r_iter + (elem_sent * s_rule_size));
2719         }
2720
2721         /* Fill up rule ID based on the value returned from FW */
2722         r_iter = s_rule;
2723         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2724                             list_entry) {
2725                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2726                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2727                 struct ice_fltr_mgmt_list_entry *fm_entry;
2728
2729                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2730                         f_info->fltr_rule_id =
2731                                 LE16_TO_CPU(r_iter->pdata.lkup_tx_rx.index);
2732                         f_info->fltr_act = ICE_FWD_TO_VSI;
2733                         /* Create an entry to track this MAC address */
2734                         fm_entry = (struct ice_fltr_mgmt_list_entry *)
2735                                 ice_malloc(hw, sizeof(*fm_entry));
2736                         if (!fm_entry) {
2737                                 status = ICE_ERR_NO_MEMORY;
2738                                 goto ice_add_mac_exit;
2739                         }
2740                         fm_entry->fltr_info = *f_info;
2741                         fm_entry->vsi_count = 1;
2742                         /* The book keeping entries will get removed when
2743                          * base driver calls remove filter AQ command
2744                          */
2745
2746                         LIST_ADD(&fm_entry->list_entry, rule_head);
2747                         r_iter = (struct ice_aqc_sw_rules_elem *)
2748                                 ((u8 *)r_iter + s_rule_size);
2749                 }
2750         }
2751
2752 ice_add_mac_exit:
2753         ice_release_lock(rule_lock);
2754         if (s_rule)
2755                 ice_free(hw, s_rule);
2756         return status;
2757 }
2758
2759 /**
2760  * ice_add_vlan_internal - Add one VLAN based filter rule
2761  * @hw: pointer to the hardware structure
2762  * @f_entry: filter entry containing one VLAN information
2763  */
2764 static enum ice_status
2765 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
2766 {
2767         struct ice_switch_info *sw = hw->switch_info;
2768         struct ice_fltr_mgmt_list_entry *v_list_itr;
2769         struct ice_fltr_info *new_fltr, *cur_fltr;
2770         enum ice_sw_lkup_type lkup_type;
2771         u16 vsi_list_id = 0, vsi_handle;
2772         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2773         enum ice_status status = ICE_SUCCESS;
2774
2775         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2776                 return ICE_ERR_PARAM;
2777
2778         f_entry->fltr_info.fwd_id.hw_vsi_id =
2779                 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2780         new_fltr = &f_entry->fltr_info;
2781
2782         /* VLAN ID should only be 12 bits */
2783         if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
2784                 return ICE_ERR_PARAM;
2785
2786         if (new_fltr->src_id != ICE_SRC_ID_VSI)
2787                 return ICE_ERR_PARAM;
2788
2789         new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
2790         lkup_type = new_fltr->lkup_type;
2791         vsi_handle = new_fltr->vsi_handle;
2792         rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2793         ice_acquire_lock(rule_lock);
2794         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
2795         if (!v_list_itr) {
2796                 struct ice_vsi_list_map_info *map_info = NULL;
2797
2798                 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
2799                         /* All VLAN pruning rules use a VSI list. Check if
2800                          * there is already a VSI list containing VSI that we
2801                          * want to add. If found, use the same vsi_list_id for
2802                          * this new VLAN rule or else create a new list.
2803                          */
2804                         map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
2805                                                            vsi_handle,
2806                                                            &vsi_list_id);
2807                         if (!map_info) {
2808                                 status = ice_create_vsi_list_rule(hw,
2809                                                                   &vsi_handle,
2810                                                                   1,
2811                                                                   &vsi_list_id,
2812                                                                   lkup_type);
2813                                 if (status)
2814                                         goto exit;
2815                         }
2816                         /* Convert the action to forwarding to a VSI list. */
2817                         new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2818                         new_fltr->fwd_id.vsi_list_id = vsi_list_id;
2819                 }
2820
2821                 status = ice_create_pkt_fwd_rule(hw, f_entry);
2822                 if (!status) {
2823                         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
2824                                                          new_fltr);
2825                         if (!v_list_itr) {
2826                                 status = ICE_ERR_DOES_NOT_EXIST;
2827                                 goto exit;
2828                         }
2829                         /* reuse VSI list for new rule and increment ref_cnt */
2830                         if (map_info) {
2831                                 v_list_itr->vsi_list_info = map_info;
2832                                 map_info->ref_cnt++;
2833                         } else {
2834                                 v_list_itr->vsi_list_info =
2835                                         ice_create_vsi_list_map(hw, &vsi_handle,
2836                                                                 1, vsi_list_id);
2837                         }
2838                 }
2839         } else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
2840                 /* Update existing VSI list to add new VSI ID only if it used
2841                  * by one VLAN rule.
2842                  */
2843                 cur_fltr = &v_list_itr->fltr_info;
2844                 status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
2845                                                  new_fltr);
2846         } else {
2847                 /* If VLAN rule exists and VSI list being used by this rule is
2848                  * referenced by more than 1 VLAN rule. Then create a new VSI
2849                  * list appending previous VSI with new VSI and update existing
2850                  * VLAN rule to point to new VSI list ID
2851                  */
2852                 struct ice_fltr_info tmp_fltr;
2853                 u16 vsi_handle_arr[2];
2854                 u16 cur_handle;
2855
2856                 /* Current implementation only supports reusing VSI list with
2857                  * one VSI count. We should never hit below condition
2858                  */
2859                 if (v_list_itr->vsi_count > 1 &&
2860                     v_list_itr->vsi_list_info->ref_cnt > 1) {
2861                         ice_debug(hw, ICE_DBG_SW,
2862                                   "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
2863                         status = ICE_ERR_CFG;
2864                         goto exit;
2865                 }
2866
2867                 cur_handle =
2868                         ice_find_first_bit(v_list_itr->vsi_list_info->vsi_map,
2869                                            ICE_MAX_VSI);
2870
2871                 /* A rule already exists with the new VSI being added */
2872                 if (cur_handle == vsi_handle) {
2873                         status = ICE_ERR_ALREADY_EXISTS;
2874                         goto exit;
2875                 }
2876
2877                 vsi_handle_arr[0] = cur_handle;
2878                 vsi_handle_arr[1] = vsi_handle;
2879                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2880                                                   &vsi_list_id, lkup_type);
2881                 if (status)
2882                         goto exit;
2883
2884                 tmp_fltr = v_list_itr->fltr_info;
2885                 tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
2886                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2887                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2888                 /* Update the previous switch rule to a new VSI list which
2889                  * includes current VSI that is requested
2890                  */
2891                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2892                 if (status)
2893                         goto exit;
2894
2895                 /* before overriding VSI list map info. decrement ref_cnt of
2896                  * previous VSI list
2897                  */
2898                 v_list_itr->vsi_list_info->ref_cnt--;
2899
2900                 /* now update to newly created list */
2901                 v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
2902                 v_list_itr->vsi_list_info =
2903                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2904                                                 vsi_list_id);
2905                 v_list_itr->vsi_count++;
2906         }
2907
2908 exit:
2909         ice_release_lock(rule_lock);
2910         return status;
2911 }
2912
2913 /**
2914  * ice_add_vlan - Add VLAN based filter rule
2915  * @hw: pointer to the hardware structure
2916  * @v_list: list of VLAN entries and forwarding information
2917  */
2918 enum ice_status
2919 ice_add_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2920 {
2921         struct ice_fltr_list_entry *v_list_itr;
2922
2923         if (!v_list || !hw)
2924                 return ICE_ERR_PARAM;
2925
2926         LIST_FOR_EACH_ENTRY(v_list_itr, v_list, ice_fltr_list_entry,
2927                             list_entry) {
2928                 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
2929                         return ICE_ERR_PARAM;
2930                 v_list_itr->fltr_info.flag = ICE_FLTR_TX;
2931                 v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
2932                 if (v_list_itr->status)
2933                         return v_list_itr->status;
2934         }
2935         return ICE_SUCCESS;
2936 }
2937
2938 #ifndef NO_MACVLAN_SUPPORT
2939 /**
2940  * ice_add_mac_vlan - Add MAC and VLAN pair based filter rule
2941  * @hw: pointer to the hardware structure
2942  * @mv_list: list of MAC and VLAN filters
2943  *
2944  * If the VSI on which the MAC-VLAN pair has to be added has Rx and Tx VLAN
2945  * pruning bits enabled, then it is the responsibility of the caller to make
2946  * sure to add a VLAN only filter on the same VSI. Packets belonging to that
2947  * VLAN won't be received on that VSI otherwise.
2948  */
2949 enum ice_status
2950 ice_add_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *mv_list)
2951 {
2952         struct ice_fltr_list_entry *mv_list_itr;
2953
2954         if (!mv_list || !hw)
2955                 return ICE_ERR_PARAM;
2956
2957         LIST_FOR_EACH_ENTRY(mv_list_itr, mv_list, ice_fltr_list_entry,
2958                             list_entry) {
2959                 enum ice_sw_lkup_type l_type =
2960                         mv_list_itr->fltr_info.lkup_type;
2961
2962                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
2963                         return ICE_ERR_PARAM;
2964                 mv_list_itr->fltr_info.flag = ICE_FLTR_TX;
2965                 mv_list_itr->status =
2966                         ice_add_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
2967                                               mv_list_itr);
2968                 if (mv_list_itr->status)
2969                         return mv_list_itr->status;
2970         }
2971         return ICE_SUCCESS;
2972 }
2973 #endif
2974
2975 /**
2976  * ice_add_eth_mac - Add ethertype and MAC based filter rule
2977  * @hw: pointer to the hardware structure
2978  * @em_list: list of ether type MAC filter, MAC is optional
2979  */
2980 enum ice_status
2981 ice_add_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2982 {
2983         struct ice_fltr_list_entry *em_list_itr;
2984
2985         LIST_FOR_EACH_ENTRY(em_list_itr, em_list, ice_fltr_list_entry,
2986                             list_entry) {
2987                 enum ice_sw_lkup_type l_type =
2988                         em_list_itr->fltr_info.lkup_type;
2989
2990                 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2991                     l_type != ICE_SW_LKUP_ETHERTYPE)
2992                         return ICE_ERR_PARAM;
2993
2994                 em_list_itr->fltr_info.flag = ICE_FLTR_TX;
2995                 em_list_itr->status = ice_add_rule_internal(hw, l_type,
2996                                                             em_list_itr);
2997                 if (em_list_itr->status)
2998                         return em_list_itr->status;
2999         }
3000         return ICE_SUCCESS;
3001 }
3002
3003 /**
3004  * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
3005  * @hw: pointer to the hardware structure
3006  * @em_list: list of ethertype or ethertype MAC entries
3007  */
3008 enum ice_status
3009 ice_remove_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
3010 {
3011         struct ice_fltr_list_entry *em_list_itr, *tmp;
3012
3013         if (!em_list || !hw)
3014                 return ICE_ERR_PARAM;
3015
3016         LIST_FOR_EACH_ENTRY_SAFE(em_list_itr, tmp, em_list, ice_fltr_list_entry,
3017                                  list_entry) {
3018                 enum ice_sw_lkup_type l_type =
3019                         em_list_itr->fltr_info.lkup_type;
3020
3021                 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
3022                     l_type != ICE_SW_LKUP_ETHERTYPE)
3023                         return ICE_ERR_PARAM;
3024
3025                 em_list_itr->status = ice_remove_rule_internal(hw, l_type,
3026                                                                em_list_itr);
3027                 if (em_list_itr->status)
3028                         return em_list_itr->status;
3029         }
3030         return ICE_SUCCESS;
3031 }
3032
3033
3034 /**
3035  * ice_rem_sw_rule_info
3036  * @hw: pointer to the hardware structure
3037  * @rule_head: pointer to the switch list structure that we want to delete
3038  */
3039 static void
3040 ice_rem_sw_rule_info(struct ice_hw *hw, struct LIST_HEAD_TYPE *rule_head)
3041 {
3042         if (!LIST_EMPTY(rule_head)) {
3043                 struct ice_fltr_mgmt_list_entry *entry;
3044                 struct ice_fltr_mgmt_list_entry *tmp;
3045
3046                 LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, rule_head,
3047                                          ice_fltr_mgmt_list_entry, list_entry) {
3048                         LIST_DEL(&entry->list_entry);
3049                         ice_free(hw, entry);
3050                 }
3051         }
3052 }
3053
3054 /**
3055  * ice_rem_adv_rule_info
3056  * @hw: pointer to the hardware structure
3057  * @rule_head: pointer to the switch list structure that we want to delete
3058  */
3059 static void
3060 ice_rem_adv_rule_info(struct ice_hw *hw, struct LIST_HEAD_TYPE *rule_head)
3061 {
3062         struct ice_adv_fltr_mgmt_list_entry *tmp_entry;
3063         struct ice_adv_fltr_mgmt_list_entry *lst_itr;
3064
3065         if (LIST_EMPTY(rule_head))
3066                 return;
3067
3068         LIST_FOR_EACH_ENTRY_SAFE(lst_itr, tmp_entry, rule_head,
3069                                  ice_adv_fltr_mgmt_list_entry, list_entry) {
3070                 LIST_DEL(&lst_itr->list_entry);
3071                 ice_free(hw, lst_itr->lkups);
3072                 ice_free(hw, lst_itr);
3073         }
3074 }
3075
3076 /**
3077  * ice_rem_all_sw_rules_info
3078  * @hw: pointer to the hardware structure
3079  */
3080 void ice_rem_all_sw_rules_info(struct ice_hw *hw)
3081 {
3082         struct ice_switch_info *sw = hw->switch_info;
3083         u8 i;
3084
3085         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3086                 struct LIST_HEAD_TYPE *rule_head;
3087
3088                 rule_head = &sw->recp_list[i].filt_rules;
3089                 if (!sw->recp_list[i].adv_rule)
3090                         ice_rem_sw_rule_info(hw, rule_head);
3091                 else
3092                         ice_rem_adv_rule_info(hw, rule_head);
3093         }
3094 }
3095
3096 /**
3097  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
3098  * @pi: pointer to the port_info structure
3099  * @vsi_handle: VSI handle to set as default
3100  * @set: true to add the above mentioned switch rule, false to remove it
3101  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
3102  *
3103  * add filter rule to set/unset given VSI as default VSI for the switch
3104  * (represented by swid)
3105  */
3106 enum ice_status
3107 ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
3108                  u8 direction)
3109 {
3110         struct ice_aqc_sw_rules_elem *s_rule;
3111         struct ice_fltr_info f_info;
3112         struct ice_hw *hw = pi->hw;
3113         enum ice_adminq_opc opcode;
3114         enum ice_status status;
3115         u16 s_rule_size;
3116         u16 hw_vsi_id;
3117
3118         if (!ice_is_vsi_valid(hw, vsi_handle))
3119                 return ICE_ERR_PARAM;
3120         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3121
3122         s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
3123                             ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
3124         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
3125         if (!s_rule)
3126                 return ICE_ERR_NO_MEMORY;
3127
3128         ice_memset(&f_info, 0, sizeof(f_info), ICE_NONDMA_MEM);
3129
3130         f_info.lkup_type = ICE_SW_LKUP_DFLT;
3131         f_info.flag = direction;
3132         f_info.fltr_act = ICE_FWD_TO_VSI;
3133         f_info.fwd_id.hw_vsi_id = hw_vsi_id;
3134
3135         if (f_info.flag & ICE_FLTR_RX) {
3136                 f_info.src = pi->lport;
3137                 f_info.src_id = ICE_SRC_ID_LPORT;
3138                 if (!set)
3139                         f_info.fltr_rule_id =
3140                                 pi->dflt_rx_vsi_rule_id;
3141         } else if (f_info.flag & ICE_FLTR_TX) {
3142                 f_info.src_id = ICE_SRC_ID_VSI;
3143                 f_info.src = hw_vsi_id;
3144                 if (!set)
3145                         f_info.fltr_rule_id =
3146                                 pi->dflt_tx_vsi_rule_id;
3147         }
3148
3149         if (set)
3150                 opcode = ice_aqc_opc_add_sw_rules;
3151         else
3152                 opcode = ice_aqc_opc_remove_sw_rules;
3153
3154         ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
3155
3156         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
3157         if (status || !(f_info.flag & ICE_FLTR_TX_RX))
3158                 goto out;
3159         if (set) {
3160                 u16 index = LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
3161
3162                 if (f_info.flag & ICE_FLTR_TX) {
3163                         pi->dflt_tx_vsi_num = hw_vsi_id;
3164                         pi->dflt_tx_vsi_rule_id = index;
3165                 } else if (f_info.flag & ICE_FLTR_RX) {
3166                         pi->dflt_rx_vsi_num = hw_vsi_id;
3167                         pi->dflt_rx_vsi_rule_id = index;
3168                 }
3169         } else {
3170                 if (f_info.flag & ICE_FLTR_TX) {
3171                         pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
3172                         pi->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
3173                 } else if (f_info.flag & ICE_FLTR_RX) {
3174                         pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
3175                         pi->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
3176                 }
3177         }
3178
3179 out:
3180         ice_free(hw, s_rule);
3181         return status;
3182 }
3183
3184 /**
3185  * ice_find_ucast_rule_entry - Search for a unicast MAC filter rule entry
3186  * @hw: pointer to the hardware structure
3187  * @recp_id: lookup type for which the specified rule needs to be searched
3188  * @f_info: rule information
3189  *
3190  * Helper function to search for a unicast rule entry - this is to be used
3191  * to remove unicast MAC filter that is not shared with other VSIs on the
3192  * PF switch.
3193  *
3194  * Returns pointer to entry storing the rule if found
3195  */
3196 static struct ice_fltr_mgmt_list_entry *
3197 ice_find_ucast_rule_entry(struct ice_hw *hw, u8 recp_id,
3198                           struct ice_fltr_info *f_info)
3199 {
3200         struct ice_switch_info *sw = hw->switch_info;
3201         struct ice_fltr_mgmt_list_entry *list_itr;
3202         struct LIST_HEAD_TYPE *list_head;
3203
3204         list_head = &sw->recp_list[recp_id].filt_rules;
3205         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
3206                             list_entry) {
3207                 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
3208                             sizeof(f_info->l_data)) &&
3209                     f_info->fwd_id.hw_vsi_id ==
3210                     list_itr->fltr_info.fwd_id.hw_vsi_id &&
3211                     f_info->flag == list_itr->fltr_info.flag)
3212                         return list_itr;
3213         }
3214         return NULL;
3215 }
3216
3217 /**
3218  * ice_remove_mac - remove a MAC address based filter rule
3219  * @hw: pointer to the hardware structure
3220  * @m_list: list of MAC addresses and forwarding information
3221  *
3222  * This function removes either a MAC filter rule or a specific VSI from a
3223  * VSI list for a multicast MAC address.
3224  *
3225  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
3226  * ice_add_mac. Caller should be aware that this call will only work if all
3227  * the entries passed into m_list were added previously. It will not attempt to
3228  * do a partial remove of entries that were found.
3229  */
3230 enum ice_status
3231 ice_remove_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
3232 {
3233         struct ice_fltr_list_entry *list_itr, *tmp;
3234         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
3235
3236         if (!m_list)
3237                 return ICE_ERR_PARAM;
3238
3239         rule_lock = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
3240         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, m_list, ice_fltr_list_entry,
3241                                  list_entry) {
3242                 enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
3243                 u8 *add = &list_itr->fltr_info.l_data.mac.mac_addr[0];
3244                 u16 vsi_handle;
3245
3246                 if (l_type != ICE_SW_LKUP_MAC)
3247                         return ICE_ERR_PARAM;
3248
3249                 vsi_handle = list_itr->fltr_info.vsi_handle;
3250                 if (!ice_is_vsi_valid(hw, vsi_handle))
3251                         return ICE_ERR_PARAM;
3252
3253                 list_itr->fltr_info.fwd_id.hw_vsi_id =
3254                                         ice_get_hw_vsi_num(hw, vsi_handle);
3255                 if (IS_UNICAST_ETHER_ADDR(add) && !hw->ucast_shared) {
3256                         /* Don't remove the unicast address that belongs to
3257                          * another VSI on the switch, since it is not being
3258                          * shared...
3259                          */
3260                         ice_acquire_lock(rule_lock);
3261                         if (!ice_find_ucast_rule_entry(hw, ICE_SW_LKUP_MAC,
3262                                                        &list_itr->fltr_info)) {
3263                                 ice_release_lock(rule_lock);
3264                                 return ICE_ERR_DOES_NOT_EXIST;
3265                         }
3266                         ice_release_lock(rule_lock);
3267                 }
3268                 list_itr->status = ice_remove_rule_internal(hw,
3269                                                             ICE_SW_LKUP_MAC,
3270                                                             list_itr);
3271                 if (list_itr->status)
3272                         return list_itr->status;
3273         }
3274         return ICE_SUCCESS;
3275 }
3276
3277 /**
3278  * ice_remove_vlan - Remove VLAN based filter rule
3279  * @hw: pointer to the hardware structure
3280  * @v_list: list of VLAN entries and forwarding information
3281  */
3282 enum ice_status
3283 ice_remove_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
3284 {
3285         struct ice_fltr_list_entry *v_list_itr, *tmp;
3286
3287         if (!v_list || !hw)
3288                 return ICE_ERR_PARAM;
3289
3290         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
3291                                  list_entry) {
3292                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
3293
3294                 if (l_type != ICE_SW_LKUP_VLAN)
3295                         return ICE_ERR_PARAM;
3296                 v_list_itr->status = ice_remove_rule_internal(hw,
3297                                                               ICE_SW_LKUP_VLAN,
3298                                                               v_list_itr);
3299                 if (v_list_itr->status)
3300                         return v_list_itr->status;
3301         }
3302         return ICE_SUCCESS;
3303 }
3304
3305 #ifndef NO_MACVLAN_SUPPORT
3306 /**
3307  * ice_remove_mac_vlan - Remove MAC VLAN based filter rule
3308  * @hw: pointer to the hardware structure
3309  * @v_list: list of MAC VLAN entries and forwarding information
3310  */
3311 enum ice_status
3312 ice_remove_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
3313 {
3314         struct ice_fltr_list_entry *v_list_itr, *tmp;
3315
3316         if (!v_list || !hw)
3317                 return ICE_ERR_PARAM;
3318
3319         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
3320                                  list_entry) {
3321                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
3322
3323                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
3324                         return ICE_ERR_PARAM;
3325                 v_list_itr->status =
3326                         ice_remove_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
3327                                                  v_list_itr);
3328                 if (v_list_itr->status)
3329                         return v_list_itr->status;
3330         }
3331         return ICE_SUCCESS;
3332 }
3333 #endif /* !NO_MACVLAN_SUPPORT */
3334
3335 /**
3336  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
3337  * @fm_entry: filter entry to inspect
3338  * @vsi_handle: VSI handle to compare with filter info
3339  */
3340 static bool
3341 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
3342 {
3343         return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
3344                  fm_entry->fltr_info.vsi_handle == vsi_handle) ||
3345                 (fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
3346                  (ice_is_bit_set(fm_entry->vsi_list_info->vsi_map,
3347                                  vsi_handle))));
3348 }
3349
3350 /**
3351  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
3352  * @hw: pointer to the hardware structure
3353  * @vsi_handle: VSI handle to remove filters from
3354  * @vsi_list_head: pointer to the list to add entry to
3355  * @fi: pointer to fltr_info of filter entry to copy & add
3356  *
3357  * Helper function, used when creating a list of filters to remove from
3358  * a specific VSI. The entry added to vsi_list_head is a COPY of the
3359  * original filter entry, with the exception of fltr_info.fltr_act and
3360  * fltr_info.fwd_id fields. These are set such that later logic can
3361  * extract which VSI to remove the fltr from, and pass on that information.
3362  */
3363 static enum ice_status
3364 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3365                                struct LIST_HEAD_TYPE *vsi_list_head,
3366                                struct ice_fltr_info *fi)
3367 {
3368         struct ice_fltr_list_entry *tmp;
3369
3370         /* this memory is freed up in the caller function
3371          * once filters for this VSI are removed
3372          */
3373         tmp = (struct ice_fltr_list_entry *)ice_malloc(hw, sizeof(*tmp));
3374         if (!tmp)
3375                 return ICE_ERR_NO_MEMORY;
3376
3377         tmp->fltr_info = *fi;
3378
3379         /* Overwrite these fields to indicate which VSI to remove filter from,
3380          * so find and remove logic can extract the information from the
3381          * list entries. Note that original entries will still have proper
3382          * values.
3383          */
3384         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
3385         tmp->fltr_info.vsi_handle = vsi_handle;
3386         tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3387
3388         LIST_ADD(&tmp->list_entry, vsi_list_head);
3389
3390         return ICE_SUCCESS;
3391 }
3392
3393 /**
3394  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
3395  * @hw: pointer to the hardware structure
3396  * @vsi_handle: VSI handle to remove filters from
3397  * @lkup_list_head: pointer to the list that has certain lookup type filters
3398  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
3399  *
3400  * Locates all filters in lkup_list_head that are used by the given VSI,
3401  * and adds COPIES of those entries to vsi_list_head (intended to be used
3402  * to remove the listed filters).
3403  * Note that this means all entries in vsi_list_head must be explicitly
3404  * deallocated by the caller when done with list.
3405  */
3406 static enum ice_status
3407 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3408                          struct LIST_HEAD_TYPE *lkup_list_head,
3409                          struct LIST_HEAD_TYPE *vsi_list_head)
3410 {
3411         struct ice_fltr_mgmt_list_entry *fm_entry;
3412         enum ice_status status = ICE_SUCCESS;
3413
3414         /* check to make sure VSI ID is valid and within boundary */
3415         if (!ice_is_vsi_valid(hw, vsi_handle))
3416                 return ICE_ERR_PARAM;
3417
3418         LIST_FOR_EACH_ENTRY(fm_entry, lkup_list_head,
3419                             ice_fltr_mgmt_list_entry, list_entry) {
3420                 struct ice_fltr_info *fi;
3421
3422                 fi = &fm_entry->fltr_info;
3423                 if (!fi || !ice_vsi_uses_fltr(fm_entry, vsi_handle))
3424                         continue;
3425
3426                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3427                                                         vsi_list_head, fi);
3428                 if (status)
3429                         return status;
3430         }
3431         return status;
3432 }
3433
3434
3435 /**
3436  * ice_determine_promisc_mask
3437  * @fi: filter info to parse
3438  *
3439  * Helper function to determine which ICE_PROMISC_ mask corresponds
3440  * to given filter into.
3441  */
3442 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
3443 {
3444         u16 vid = fi->l_data.mac_vlan.vlan_id;
3445         u8 *macaddr = fi->l_data.mac.mac_addr;
3446         bool is_tx_fltr = false;
3447         u8 promisc_mask = 0;
3448
3449         if (fi->flag == ICE_FLTR_TX)
3450                 is_tx_fltr = true;
3451
3452         if (IS_BROADCAST_ETHER_ADDR(macaddr))
3453                 promisc_mask |= is_tx_fltr ?
3454                         ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
3455         else if (IS_MULTICAST_ETHER_ADDR(macaddr))
3456                 promisc_mask |= is_tx_fltr ?
3457                         ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
3458         else if (IS_UNICAST_ETHER_ADDR(macaddr))
3459                 promisc_mask |= is_tx_fltr ?
3460                         ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
3461         if (vid)
3462                 promisc_mask |= is_tx_fltr ?
3463                         ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
3464
3465         return promisc_mask;
3466 }
3467
3468 /**
3469  * ice_get_vsi_promisc - get promiscuous mode of given VSI
3470  * @hw: pointer to the hardware structure
3471  * @vsi_handle: VSI handle to retrieve info from
3472  * @promisc_mask: pointer to mask to be filled in
3473  * @vid: VLAN ID of promisc VLAN VSI
3474  */
3475 enum ice_status
3476 ice_get_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3477                     u16 *vid)
3478 {
3479         struct ice_switch_info *sw = hw->switch_info;
3480         struct ice_fltr_mgmt_list_entry *itr;
3481         struct LIST_HEAD_TYPE *rule_head;
3482         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3483
3484         if (!ice_is_vsi_valid(hw, vsi_handle))
3485                 return ICE_ERR_PARAM;
3486
3487         *vid = 0;
3488         *promisc_mask = 0;
3489         rule_head = &sw->recp_list[ICE_SW_LKUP_PROMISC].filt_rules;
3490         rule_lock = &sw->recp_list[ICE_SW_LKUP_PROMISC].filt_rule_lock;
3491
3492         ice_acquire_lock(rule_lock);
3493         LIST_FOR_EACH_ENTRY(itr, rule_head,
3494                             ice_fltr_mgmt_list_entry, list_entry) {
3495                 /* Continue if this filter doesn't apply to this VSI or the
3496                  * VSI ID is not in the VSI map for this filter
3497                  */
3498                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3499                         continue;
3500
3501                 *promisc_mask |= ice_determine_promisc_mask(&itr->fltr_info);
3502         }
3503         ice_release_lock(rule_lock);
3504
3505         return ICE_SUCCESS;
3506 }
3507
3508 /**
3509  * ice_get_vsi_vlan_promisc - get VLAN promiscuous mode of given VSI
3510  * @hw: pointer to the hardware structure
3511  * @vsi_handle: VSI handle to retrieve info from
3512  * @promisc_mask: pointer to mask to be filled in
3513  * @vid: VLAN ID of promisc VLAN VSI
3514  */
3515 enum ice_status
3516 ice_get_vsi_vlan_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3517                          u16 *vid)
3518 {
3519         struct ice_switch_info *sw = hw->switch_info;
3520         struct ice_fltr_mgmt_list_entry *itr;
3521         struct LIST_HEAD_TYPE *rule_head;
3522         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3523
3524         if (!ice_is_vsi_valid(hw, vsi_handle))
3525                 return ICE_ERR_PARAM;
3526
3527         *vid = 0;
3528         *promisc_mask = 0;
3529         rule_head = &sw->recp_list[ICE_SW_LKUP_PROMISC_VLAN].filt_rules;
3530         rule_lock = &sw->recp_list[ICE_SW_LKUP_PROMISC_VLAN].filt_rule_lock;
3531
3532         ice_acquire_lock(rule_lock);
3533         LIST_FOR_EACH_ENTRY(itr, rule_head, ice_fltr_mgmt_list_entry,
3534                             list_entry) {
3535                 /* Continue if this filter doesn't apply to this VSI or the
3536                  * VSI ID is not in the VSI map for this filter
3537                  */
3538                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3539                         continue;
3540
3541                 *promisc_mask |= ice_determine_promisc_mask(&itr->fltr_info);
3542         }
3543         ice_release_lock(rule_lock);
3544
3545         return ICE_SUCCESS;
3546 }
3547
3548 /**
3549  * ice_remove_promisc - Remove promisc based filter rules
3550  * @hw: pointer to the hardware structure
3551  * @recp_id: recipe ID for which the rule needs to removed
3552  * @v_list: list of promisc entries
3553  */
3554 static enum ice_status
3555 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
3556                    struct LIST_HEAD_TYPE *v_list)
3557 {
3558         struct ice_fltr_list_entry *v_list_itr, *tmp;
3559
3560         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
3561                                  list_entry) {
3562                 v_list_itr->status =
3563                         ice_remove_rule_internal(hw, recp_id, v_list_itr);
3564                 if (v_list_itr->status)
3565                         return v_list_itr->status;
3566         }
3567         return ICE_SUCCESS;
3568 }
3569
3570 /**
3571  * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
3572  * @hw: pointer to the hardware structure
3573  * @vsi_handle: VSI handle to clear mode
3574  * @promisc_mask: mask of promiscuous config bits to clear
3575  * @vid: VLAN ID to clear VLAN promiscuous
3576  */
3577 enum ice_status
3578 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3579                       u16 vid)
3580 {
3581         struct ice_switch_info *sw = hw->switch_info;
3582         struct ice_fltr_list_entry *fm_entry, *tmp;
3583         struct LIST_HEAD_TYPE remove_list_head;
3584         struct ice_fltr_mgmt_list_entry *itr;
3585         struct LIST_HEAD_TYPE *rule_head;
3586         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3587         enum ice_status status = ICE_SUCCESS;
3588         u8 recipe_id;
3589
3590         if (!ice_is_vsi_valid(hw, vsi_handle))
3591                 return ICE_ERR_PARAM;
3592
3593         if (vid)
3594                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3595         else
3596                 recipe_id = ICE_SW_LKUP_PROMISC;
3597
3598         rule_head = &sw->recp_list[recipe_id].filt_rules;
3599         rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
3600
3601         INIT_LIST_HEAD(&remove_list_head);
3602
3603         ice_acquire_lock(rule_lock);
3604         LIST_FOR_EACH_ENTRY(itr, rule_head,
3605                             ice_fltr_mgmt_list_entry, list_entry) {
3606                 u8 fltr_promisc_mask = 0;
3607
3608                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3609                         continue;
3610
3611                 fltr_promisc_mask |=
3612                         ice_determine_promisc_mask(&itr->fltr_info);
3613
3614                 /* Skip if filter is not completely specified by given mask */
3615                 if (fltr_promisc_mask & ~promisc_mask)
3616                         continue;
3617
3618                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3619                                                         &remove_list_head,
3620                                                         &itr->fltr_info);
3621                 if (status) {
3622                         ice_release_lock(rule_lock);
3623                         goto free_fltr_list;
3624                 }
3625         }
3626         ice_release_lock(rule_lock);
3627
3628         status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
3629
3630 free_fltr_list:
3631         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3632                                  ice_fltr_list_entry, list_entry) {
3633                 LIST_DEL(&fm_entry->list_entry);
3634                 ice_free(hw, fm_entry);
3635         }
3636
3637         return status;
3638 }
3639
3640 /**
3641  * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
3642  * @hw: pointer to the hardware structure
3643  * @vsi_handle: VSI handle to configure
3644  * @promisc_mask: mask of promiscuous config bits
3645  * @vid: VLAN ID to set VLAN promiscuous
3646  */
3647 enum ice_status
3648 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
3649 {
3650         enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
3651         struct ice_fltr_list_entry f_list_entry;
3652         struct ice_fltr_info new_fltr;
3653         enum ice_status status = ICE_SUCCESS;
3654         bool is_tx_fltr;
3655         u16 hw_vsi_id;
3656         int pkt_type;
3657         u8 recipe_id;
3658
3659         ice_debug(hw, ICE_DBG_TRACE, "ice_set_vsi_promisc\n");
3660
3661         if (!ice_is_vsi_valid(hw, vsi_handle))
3662                 return ICE_ERR_PARAM;
3663         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3664
3665         ice_memset(&new_fltr, 0, sizeof(new_fltr), ICE_NONDMA_MEM);
3666
3667         if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
3668                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
3669                 new_fltr.l_data.mac_vlan.vlan_id = vid;
3670                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3671         } else {
3672                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
3673                 recipe_id = ICE_SW_LKUP_PROMISC;
3674         }
3675
3676         /* Separate filters must be set for each direction/packet type
3677          * combination, so we will loop over the mask value, store the
3678          * individual type, and clear it out in the input mask as it
3679          * is found.
3680          */
3681         while (promisc_mask) {
3682                 u8 *mac_addr;
3683
3684                 pkt_type = 0;
3685                 is_tx_fltr = false;
3686
3687                 if (promisc_mask & ICE_PROMISC_UCAST_RX) {
3688                         promisc_mask &= ~ICE_PROMISC_UCAST_RX;
3689                         pkt_type = UCAST_FLTR;
3690                 } else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
3691                         promisc_mask &= ~ICE_PROMISC_UCAST_TX;
3692                         pkt_type = UCAST_FLTR;
3693                         is_tx_fltr = true;
3694                 } else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
3695                         promisc_mask &= ~ICE_PROMISC_MCAST_RX;
3696                         pkt_type = MCAST_FLTR;
3697                 } else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
3698                         promisc_mask &= ~ICE_PROMISC_MCAST_TX;
3699                         pkt_type = MCAST_FLTR;
3700                         is_tx_fltr = true;
3701                 } else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
3702                         promisc_mask &= ~ICE_PROMISC_BCAST_RX;
3703                         pkt_type = BCAST_FLTR;
3704                 } else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
3705                         promisc_mask &= ~ICE_PROMISC_BCAST_TX;
3706                         pkt_type = BCAST_FLTR;
3707                         is_tx_fltr = true;
3708                 }
3709
3710                 /* Check for VLAN promiscuous flag */
3711                 if (promisc_mask & ICE_PROMISC_VLAN_RX) {
3712                         promisc_mask &= ~ICE_PROMISC_VLAN_RX;
3713                 } else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
3714                         promisc_mask &= ~ICE_PROMISC_VLAN_TX;
3715                         is_tx_fltr = true;
3716                 }
3717
3718                 /* Set filter DA based on packet type */
3719                 mac_addr = new_fltr.l_data.mac.mac_addr;
3720                 if (pkt_type == BCAST_FLTR) {
3721                         ice_memset(mac_addr, 0xff, ETH_ALEN, ICE_NONDMA_MEM);
3722                 } else if (pkt_type == MCAST_FLTR ||
3723                            pkt_type == UCAST_FLTR) {
3724                         /* Use the dummy ether header DA */
3725                         ice_memcpy(mac_addr, dummy_eth_header, ETH_ALEN,
3726                                    ICE_NONDMA_TO_NONDMA);
3727                         if (pkt_type == MCAST_FLTR)
3728                                 mac_addr[0] |= 0x1;     /* Set multicast bit */
3729                 }
3730
3731                 /* Need to reset this to zero for all iterations */
3732                 new_fltr.flag = 0;
3733                 if (is_tx_fltr) {
3734                         new_fltr.flag |= ICE_FLTR_TX;
3735                         new_fltr.src = hw_vsi_id;
3736                 } else {
3737                         new_fltr.flag |= ICE_FLTR_RX;
3738                         new_fltr.src = hw->port_info->lport;
3739                 }
3740
3741                 new_fltr.fltr_act = ICE_FWD_TO_VSI;
3742                 new_fltr.vsi_handle = vsi_handle;
3743                 new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
3744                 f_list_entry.fltr_info = new_fltr;
3745
3746                 status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
3747                 if (status != ICE_SUCCESS)
3748                         goto set_promisc_exit;
3749         }
3750
3751 set_promisc_exit:
3752         return status;
3753 }
3754
3755 /**
3756  * ice_set_vlan_vsi_promisc
3757  * @hw: pointer to the hardware structure
3758  * @vsi_handle: VSI handle to configure
3759  * @promisc_mask: mask of promiscuous config bits
3760  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
3761  *
3762  * Configure VSI with all associated VLANs to given promiscuous mode(s)
3763  */
3764 enum ice_status
3765 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3766                          bool rm_vlan_promisc)
3767 {
3768         struct ice_switch_info *sw = hw->switch_info;
3769         struct ice_fltr_list_entry *list_itr, *tmp;
3770         struct LIST_HEAD_TYPE vsi_list_head;
3771         struct LIST_HEAD_TYPE *vlan_head;
3772         struct ice_lock *vlan_lock; /* Lock to protect filter rule list */
3773         enum ice_status status;
3774         u16 vlan_id;
3775
3776         INIT_LIST_HEAD(&vsi_list_head);
3777         vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3778         vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3779         ice_acquire_lock(vlan_lock);
3780         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
3781                                           &vsi_list_head);
3782         ice_release_lock(vlan_lock);
3783         if (status)
3784                 goto free_fltr_list;
3785
3786         LIST_FOR_EACH_ENTRY(list_itr, &vsi_list_head, ice_fltr_list_entry,
3787                             list_entry) {
3788                 vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
3789                 if (rm_vlan_promisc)
3790                         status = ice_clear_vsi_promisc(hw, vsi_handle,
3791                                                        promisc_mask, vlan_id);
3792                 else
3793                         status = ice_set_vsi_promisc(hw, vsi_handle,
3794                                                      promisc_mask, vlan_id);
3795                 if (status)
3796                         break;
3797         }
3798
3799 free_fltr_list:
3800         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, &vsi_list_head,
3801                                  ice_fltr_list_entry, list_entry) {
3802                 LIST_DEL(&list_itr->list_entry);
3803                 ice_free(hw, list_itr);
3804         }
3805         return status;
3806 }
3807
3808 /**
3809  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
3810  * @hw: pointer to the hardware structure
3811  * @vsi_handle: VSI handle to remove filters from
3812  * @lkup: switch rule filter lookup type
3813  */
3814 static void
3815 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
3816                          enum ice_sw_lkup_type lkup)
3817 {
3818         struct ice_switch_info *sw = hw->switch_info;
3819         struct ice_fltr_list_entry *fm_entry;
3820         struct LIST_HEAD_TYPE remove_list_head;
3821         struct LIST_HEAD_TYPE *rule_head;
3822         struct ice_fltr_list_entry *tmp;
3823         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3824         enum ice_status status;
3825
3826         INIT_LIST_HEAD(&remove_list_head);
3827         rule_lock = &sw->recp_list[lkup].filt_rule_lock;
3828         rule_head = &sw->recp_list[lkup].filt_rules;
3829         ice_acquire_lock(rule_lock);
3830         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
3831                                           &remove_list_head);
3832         ice_release_lock(rule_lock);
3833         if (status)
3834                 return;
3835
3836         switch (lkup) {
3837         case ICE_SW_LKUP_MAC:
3838                 ice_remove_mac(hw, &remove_list_head);
3839                 break;
3840         case ICE_SW_LKUP_VLAN:
3841                 ice_remove_vlan(hw, &remove_list_head);
3842                 break;
3843         case ICE_SW_LKUP_PROMISC:
3844         case ICE_SW_LKUP_PROMISC_VLAN:
3845                 ice_remove_promisc(hw, lkup, &remove_list_head);
3846                 break;
3847         case ICE_SW_LKUP_MAC_VLAN:
3848 #ifndef NO_MACVLAN_SUPPORT
3849                 ice_remove_mac_vlan(hw, &remove_list_head);
3850 #else
3851                 ice_debug(hw, ICE_DBG_SW, "MAC VLAN look up is not supported yet\n");
3852 #endif /* !NO_MACVLAN_SUPPORT */
3853                 break;
3854         case ICE_SW_LKUP_ETHERTYPE:
3855         case ICE_SW_LKUP_ETHERTYPE_MAC:
3856                 ice_remove_eth_mac(hw, &remove_list_head);
3857                 break;
3858         case ICE_SW_LKUP_DFLT:
3859                 ice_debug(hw, ICE_DBG_SW,
3860                           "Remove filters for this lookup type hasn't been implemented yet\n");
3861                 break;
3862         case ICE_SW_LKUP_LAST:
3863                 ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type\n");
3864                 break;
3865         }
3866
3867         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3868                                  ice_fltr_list_entry, list_entry) {
3869                 LIST_DEL(&fm_entry->list_entry);
3870                 ice_free(hw, fm_entry);
3871         }
3872 }
3873
3874 /**
3875  * ice_remove_vsi_fltr - Remove all filters for a VSI
3876  * @hw: pointer to the hardware structure
3877  * @vsi_handle: VSI handle to remove filters from
3878  */
3879 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
3880 {
3881         ice_debug(hw, ICE_DBG_TRACE, "ice_remove_vsi_fltr\n");
3882
3883         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
3884         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
3885         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
3886         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
3887         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
3888         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
3889         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
3890         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
3891 }
3892
3893 /**
3894  * ice_alloc_res_cntr - allocating resource counter
3895  * @hw: pointer to the hardware structure
3896  * @type: type of resource
3897  * @alloc_shared: if set it is shared else dedicated
3898  * @num_items: number of entries requested for FD resource type
3899  * @counter_id: counter index returned by AQ call
3900  */
3901 enum ice_status
3902 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3903                    u16 *counter_id)
3904 {
3905         struct ice_aqc_alloc_free_res_elem *buf;
3906         enum ice_status status;
3907         u16 buf_len;
3908
3909         /* Allocate resource */
3910         buf_len = sizeof(*buf);
3911         buf = (struct ice_aqc_alloc_free_res_elem *)
3912                 ice_malloc(hw, buf_len);
3913         if (!buf)
3914                 return ICE_ERR_NO_MEMORY;
3915
3916         buf->num_elems = CPU_TO_LE16(num_items);
3917         buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3918                                       ICE_AQC_RES_TYPE_M) | alloc_shared);
3919
3920         status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3921                                        ice_aqc_opc_alloc_res, NULL);
3922         if (status)
3923                 goto exit;
3924
3925         *counter_id = LE16_TO_CPU(buf->elem[0].e.sw_resp);
3926
3927 exit:
3928         ice_free(hw, buf);
3929         return status;
3930 }
3931
3932 /**
3933  * ice_free_res_cntr - free resource counter
3934  * @hw: pointer to the hardware structure
3935  * @type: type of resource
3936  * @alloc_shared: if set it is shared else dedicated
3937  * @num_items: number of entries to be freed for FD resource type
3938  * @counter_id: counter ID resource which needs to be freed
3939  */
3940 enum ice_status
3941 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3942                   u16 counter_id)
3943 {
3944         struct ice_aqc_alloc_free_res_elem *buf;
3945         enum ice_status status;
3946         u16 buf_len;
3947
3948         /* Free resource */
3949         buf_len = sizeof(*buf);
3950         buf = (struct ice_aqc_alloc_free_res_elem *)
3951                 ice_malloc(hw, buf_len);
3952         if (!buf)
3953                 return ICE_ERR_NO_MEMORY;
3954
3955         buf->num_elems = CPU_TO_LE16(num_items);
3956         buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3957                                       ICE_AQC_RES_TYPE_M) | alloc_shared);
3958         buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
3959
3960         status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3961                                        ice_aqc_opc_free_res, NULL);
3962         if (status)
3963                 ice_debug(hw, ICE_DBG_SW,
3964                           "counter resource could not be freed\n");
3965
3966         ice_free(hw, buf);
3967         return status;
3968 }
3969
3970 /**
3971  * ice_alloc_vlan_res_counter - obtain counter resource for VLAN type
3972  * @hw: pointer to the hardware structure
3973  * @counter_id: returns counter index
3974  */
3975 enum ice_status ice_alloc_vlan_res_counter(struct ice_hw *hw, u16 *counter_id)
3976 {
3977         return ice_alloc_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3978                                   ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3979                                   counter_id);
3980 }
3981
3982 /**
3983  * ice_free_vlan_res_counter - Free counter resource for VLAN type
3984  * @hw: pointer to the hardware structure
3985  * @counter_id: counter index to be freed
3986  */
3987 enum ice_status ice_free_vlan_res_counter(struct ice_hw *hw, u16 counter_id)
3988 {
3989         return ice_free_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3990                                  ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3991                                  counter_id);
3992 }
3993
3994 /**
3995  * ice_alloc_res_lg_act - add large action resource
3996  * @hw: pointer to the hardware structure
3997  * @l_id: large action ID to fill it in
3998  * @num_acts: number of actions to hold with a large action entry
3999  */
4000 static enum ice_status
4001 ice_alloc_res_lg_act(struct ice_hw *hw, u16 *l_id, u16 num_acts)
4002 {
4003         struct ice_aqc_alloc_free_res_elem *sw_buf;
4004         enum ice_status status;
4005         u16 buf_len;
4006
4007         if (num_acts > ICE_MAX_LG_ACT || num_acts == 0)
4008                 return ICE_ERR_PARAM;
4009
4010         /* Allocate resource for large action */
4011         buf_len = sizeof(*sw_buf);
4012         sw_buf = (struct ice_aqc_alloc_free_res_elem *)
4013                 ice_malloc(hw, buf_len);
4014         if (!sw_buf)
4015                 return ICE_ERR_NO_MEMORY;
4016
4017         sw_buf->num_elems = CPU_TO_LE16(1);
4018
4019         /* If num_acts is 1, use ICE_AQC_RES_TYPE_WIDE_TABLE_1.
4020          * If num_acts is 2, use ICE_AQC_RES_TYPE_WIDE_TABLE_3.
4021          * If num_acts is greater than 2, then use
4022          * ICE_AQC_RES_TYPE_WIDE_TABLE_4.
4023          * The num_acts cannot exceed 4. This was ensured at the
4024          * beginning of the function.
4025          */
4026         if (num_acts == 1)
4027                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_1);
4028         else if (num_acts == 2)
4029                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_2);
4030         else
4031                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_4);
4032
4033         status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
4034                                        ice_aqc_opc_alloc_res, NULL);
4035         if (!status)
4036                 *l_id = LE16_TO_CPU(sw_buf->elem[0].e.sw_resp);
4037
4038         ice_free(hw, sw_buf);
4039         return status;
4040 }
4041
4042 /**
4043  * ice_add_mac_with_sw_marker - add filter with sw marker
4044  * @hw: pointer to the hardware structure
4045  * @f_info: filter info structure containing the MAC filter information
4046  * @sw_marker: sw marker to tag the Rx descriptor with
4047  */
4048 enum ice_status
4049 ice_add_mac_with_sw_marker(struct ice_hw *hw, struct ice_fltr_info *f_info,
4050                            u16 sw_marker)
4051 {
4052         struct ice_switch_info *sw = hw->switch_info;
4053         struct ice_fltr_mgmt_list_entry *m_entry;
4054         struct ice_fltr_list_entry fl_info;
4055         struct LIST_HEAD_TYPE l_head;
4056         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
4057         enum ice_status ret;
4058         bool entry_exists;
4059         u16 lg_act_id;
4060
4061         if (f_info->fltr_act != ICE_FWD_TO_VSI)
4062                 return ICE_ERR_PARAM;
4063
4064         if (f_info->lkup_type != ICE_SW_LKUP_MAC)
4065                 return ICE_ERR_PARAM;
4066
4067         if (sw_marker == ICE_INVAL_SW_MARKER_ID)
4068                 return ICE_ERR_PARAM;
4069
4070         if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
4071                 return ICE_ERR_PARAM;
4072         f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
4073
4074         /* Add filter if it doesn't exist so then the adding of large
4075          * action always results in update
4076          */
4077
4078         INIT_LIST_HEAD(&l_head);
4079         fl_info.fltr_info = *f_info;
4080         LIST_ADD(&fl_info.list_entry, &l_head);
4081
4082         entry_exists = false;
4083         ret = ice_add_mac(hw, &l_head);
4084         if (ret == ICE_ERR_ALREADY_EXISTS)
4085                 entry_exists = true;
4086         else if (ret)
4087                 return ret;
4088
4089         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
4090         ice_acquire_lock(rule_lock);
4091         /* Get the book keeping entry for the filter */
4092         m_entry = ice_find_rule_entry(hw, ICE_SW_LKUP_MAC, f_info);
4093         if (!m_entry)
4094                 goto exit_error;
4095
4096         /* If counter action was enabled for this rule then don't enable
4097          * sw marker large action
4098          */
4099         if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
4100                 ret = ICE_ERR_PARAM;
4101                 goto exit_error;
4102         }
4103
4104         /* if same marker was added before */
4105         if (m_entry->sw_marker_id == sw_marker) {
4106                 ret = ICE_ERR_ALREADY_EXISTS;
4107                 goto exit_error;
4108         }
4109
4110         /* Allocate a hardware table entry to hold large act. Three actions
4111          * for marker based large action
4112          */
4113         ret = ice_alloc_res_lg_act(hw, &lg_act_id, 3);
4114         if (ret)
4115                 goto exit_error;
4116
4117         if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
4118                 goto exit_error;
4119
4120         /* Update the switch rule to add the marker action */
4121         ret = ice_add_marker_act(hw, m_entry, sw_marker, lg_act_id);
4122         if (!ret) {
4123                 ice_release_lock(rule_lock);
4124                 return ret;
4125         }
4126
4127 exit_error:
4128         ice_release_lock(rule_lock);
4129         /* only remove entry if it did not exist previously */
4130         if (!entry_exists)
4131                 ret = ice_remove_mac(hw, &l_head);
4132
4133         return ret;
4134 }
4135
4136 /**
4137  * ice_add_mac_with_counter - add filter with counter enabled
4138  * @hw: pointer to the hardware structure
4139  * @f_info: pointer to filter info structure containing the MAC filter
4140  *          information
4141  */
4142 enum ice_status
4143 ice_add_mac_with_counter(struct ice_hw *hw, struct ice_fltr_info *f_info)
4144 {
4145         struct ice_switch_info *sw = hw->switch_info;
4146         struct ice_fltr_mgmt_list_entry *m_entry;
4147         struct ice_fltr_list_entry fl_info;
4148         struct LIST_HEAD_TYPE l_head;
4149         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
4150         enum ice_status ret;
4151         bool entry_exist;
4152         u16 counter_id;
4153         u16 lg_act_id;
4154
4155         if (f_info->fltr_act != ICE_FWD_TO_VSI)
4156                 return ICE_ERR_PARAM;
4157
4158         if (f_info->lkup_type != ICE_SW_LKUP_MAC)
4159                 return ICE_ERR_PARAM;
4160
4161         if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
4162                 return ICE_ERR_PARAM;
4163         f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
4164
4165         entry_exist = false;
4166
4167         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
4168
4169         /* Add filter if it doesn't exist so then the adding of large
4170          * action always results in update
4171          */
4172         INIT_LIST_HEAD(&l_head);
4173
4174         fl_info.fltr_info = *f_info;
4175         LIST_ADD(&fl_info.list_entry, &l_head);
4176
4177         ret = ice_add_mac(hw, &l_head);
4178         if (ret == ICE_ERR_ALREADY_EXISTS)
4179                 entry_exist = true;
4180         else if (ret)
4181                 return ret;
4182
4183         ice_acquire_lock(rule_lock);
4184         m_entry = ice_find_rule_entry(hw, ICE_SW_LKUP_MAC, f_info);
4185         if (!m_entry) {
4186                 ret = ICE_ERR_BAD_PTR;
4187                 goto exit_error;
4188         }
4189
4190         /* Don't enable counter for a filter for which sw marker was enabled */
4191         if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID) {
4192                 ret = ICE_ERR_PARAM;
4193                 goto exit_error;
4194         }
4195
4196         /* If a counter was already enabled then don't need to add again */
4197         if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
4198                 ret = ICE_ERR_ALREADY_EXISTS;
4199                 goto exit_error;
4200         }
4201
4202         /* Allocate a hardware table entry to VLAN counter */
4203         ret = ice_alloc_vlan_res_counter(hw, &counter_id);
4204         if (ret)
4205                 goto exit_error;
4206
4207         /* Allocate a hardware table entry to hold large act. Two actions for
4208          * counter based large action
4209          */
4210         ret = ice_alloc_res_lg_act(hw, &lg_act_id, 2);
4211         if (ret)
4212                 goto exit_error;
4213
4214         if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
4215                 goto exit_error;
4216
4217         /* Update the switch rule to add the counter action */
4218         ret = ice_add_counter_act(hw, m_entry, counter_id, lg_act_id);
4219         if (!ret) {
4220                 ice_release_lock(rule_lock);
4221                 return ret;
4222         }
4223
4224 exit_error:
4225         ice_release_lock(rule_lock);
4226         /* only remove entry if it did not exist previously */
4227         if (!entry_exist)
4228                 ret = ice_remove_mac(hw, &l_head);
4229
4230         return ret;
4231 }
4232
4233 /* This is mapping table entry that maps every word within a given protocol
4234  * structure to the real byte offset as per the specification of that
4235  * protocol header.
4236  * for example dst address is 3 words in ethertype header and corresponding
4237  * bytes are 0, 2, 3 in the actual packet header and src address is at 4, 6, 8
4238  * IMPORTANT: Every structure part of "ice_prot_hdr" union should have a
4239  * matching entry describing its field. This needs to be updated if new
4240  * structure is added to that union.
4241  */
4242 static const struct ice_prot_ext_tbl_entry ice_prot_ext[] = {
4243         { ICE_MAC_OFOS,         { 0, 2, 4, 6, 8, 10, 12 } },
4244         { ICE_MAC_IL,           { 0, 2, 4, 6, 8, 10, 12 } },
4245         { ICE_IPV4_OFOS,        { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 } },
4246         { ICE_IPV4_IL,          { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 } },
4247         { ICE_IPV6_IL,          { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
4248                                  26, 28, 30, 32, 34, 36, 38 } },
4249         { ICE_IPV6_OFOS,        { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
4250                                  26, 28, 30, 32, 34, 36, 38 } },
4251         { ICE_TCP_IL,           { 0, 2 } },
4252         { ICE_UDP_ILOS,         { 0, 2 } },
4253         { ICE_SCTP_IL,          { 0, 2 } },
4254         { ICE_VXLAN,            { 8, 10, 12 } },
4255         { ICE_GENEVE,           { 8, 10, 12 } },
4256         { ICE_VXLAN_GPE,        { 0, 2, 4 } },
4257         { ICE_NVGRE,            { 0, 2 } },
4258         { ICE_PROTOCOL_LAST,    { 0 } }
4259 };
4260
4261 /* The following table describes preferred grouping of recipes.
4262  * If a recipe that needs to be programmed is a superset or matches one of the
4263  * following combinations, then the recipe needs to be chained as per the
4264  * following policy.
4265  */
4266 static const struct ice_pref_recipe_group ice_recipe_pack[] = {
4267         {3, { { ICE_MAC_OFOS_HW, 0, 0 }, { ICE_MAC_OFOS_HW, 2, 0 },
4268               { ICE_MAC_OFOS_HW, 4, 0 } } },
4269         {4, { { ICE_MAC_IL_HW, 0, 0 }, { ICE_MAC_IL_HW, 2, 0 },
4270               { ICE_MAC_IL_HW, 4, 0 }, { ICE_META_DATA_ID_HW, 44, 0 } } },
4271         {2, { { ICE_IPV4_IL_HW, 0, 0 }, { ICE_IPV4_IL_HW, 2, 0 } } },
4272         {2, { { ICE_IPV4_IL_HW, 12, 0 }, { ICE_IPV4_IL_HW, 14, 0 } } },
4273 };
4274
4275 static const struct ice_protocol_entry ice_prot_id_tbl[] = {
4276         { ICE_MAC_OFOS,         ICE_MAC_OFOS_HW },
4277         { ICE_MAC_IL,           ICE_MAC_IL_HW },
4278         { ICE_IPV4_OFOS,        ICE_IPV4_OFOS_HW },
4279         { ICE_IPV4_IL,          ICE_IPV4_IL_HW },
4280         { ICE_IPV6_OFOS,        ICE_IPV6_OFOS_HW },
4281         { ICE_IPV6_IL,          ICE_IPV6_IL_HW },
4282         { ICE_TCP_IL,           ICE_TCP_IL_HW },
4283         { ICE_UDP_ILOS,         ICE_UDP_ILOS_HW },
4284         { ICE_SCTP_IL,          ICE_SCTP_IL_HW },
4285         { ICE_VXLAN,            ICE_UDP_OF_HW },
4286         { ICE_GENEVE,           ICE_UDP_OF_HW },
4287         { ICE_VXLAN_GPE,        ICE_UDP_OF_HW },
4288         { ICE_NVGRE,            ICE_GRE_OF_HW },
4289         { ICE_PROTOCOL_LAST,    0 }
4290 };
4291
4292 /**
4293  * ice_find_recp - find a recipe
4294  * @hw: pointer to the hardware structure
4295  * @lkup_exts: extension sequence to match
4296  *
4297  * Returns index of matching recipe, or ICE_MAX_NUM_RECIPES if not found.
4298  */
4299 static u16 ice_find_recp(struct ice_hw *hw, struct ice_prot_lkup_ext *lkup_exts)
4300 {
4301         struct ice_sw_recipe *recp;
4302         u16 i;
4303
4304         ice_get_recp_to_prof_map(hw);
4305         /* Initialize available_result_ids which tracks available result idx */
4306         for (i = 0; i <= ICE_CHAIN_FV_INDEX_START; i++)
4307                 ice_set_bit(ICE_CHAIN_FV_INDEX_START - i,
4308                             available_result_ids);
4309
4310         /* Walk through existing recipes to find a match */
4311         recp = hw->switch_info->recp_list;
4312         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4313                 /* If recipe was not created for this ID, in SW bookkeeping,
4314                  * check if FW has an entry for this recipe. If the FW has an
4315                  * entry update it in our SW bookkeeping and continue with the
4316                  * matching.
4317                  */
4318                 if (!recp[i].recp_created)
4319                         if (ice_get_recp_frm_fw(hw,
4320                                                 hw->switch_info->recp_list, i))
4321                                 continue;
4322
4323                 /* if number of words we are looking for match */
4324                 if (lkup_exts->n_val_words == recp[i].lkup_exts.n_val_words) {
4325                         struct ice_fv_word *a = lkup_exts->fv_words;
4326                         struct ice_fv_word *b = recp[i].lkup_exts.fv_words;
4327                         bool found = true;
4328                         u8 p, q;
4329
4330                         for (p = 0; p < lkup_exts->n_val_words; p++) {
4331                                 for (q = 0; q < recp[i].lkup_exts.n_val_words;
4332                                      q++) {
4333                                         if (a[p].off == b[q].off &&
4334                                             a[p].prot_id == b[q].prot_id)
4335                                                 /* Found the "p"th word in the
4336                                                  * given recipe
4337                                                  */
4338                                                 break;
4339                                 }
4340                                 /* After walking through all the words in the
4341                                  * "i"th recipe if "p"th word was not found then
4342                                  * this recipe is not what we are looking for.
4343                                  * So break out from this loop and try the next
4344                                  * recipe
4345                                  */
4346                                 if (q >= recp[i].lkup_exts.n_val_words) {
4347                                         found = false;
4348                                         break;
4349                                 }
4350                         }
4351                         /* If for "i"th recipe the found was never set to false
4352                          * then it means we found our match
4353                          */
4354                         if (found)
4355                                 return i; /* Return the recipe ID */
4356                 }
4357         }
4358         return ICE_MAX_NUM_RECIPES;
4359 }
4360
4361 /**
4362  * ice_prot_type_to_id - get protocol ID from protocol type
4363  * @type: protocol type
4364  * @id: pointer to variable that will receive the ID
4365  *
4366  * Returns true if found, false otherwise
4367  */
4368 static bool ice_prot_type_to_id(enum ice_protocol_type type, u16 *id)
4369 {
4370         u16 i;
4371
4372         for (i = 0; ice_prot_id_tbl[i].type != ICE_PROTOCOL_LAST; i++)
4373                 if (ice_prot_id_tbl[i].type == type) {
4374                         *id = ice_prot_id_tbl[i].protocol_id;
4375                         return true;
4376                 }
4377         return false;
4378 }
4379
4380 /**
4381  * ice_find_valid_words - count valid words
4382  * @rule: advanced rule with lookup information
4383  * @lkup_exts: byte offset extractions of the words that are valid
4384  *
4385  * calculate valid words in a lookup rule using mask value
4386  */
4387 static u16
4388 ice_fill_valid_words(struct ice_adv_lkup_elem *rule,
4389                      struct ice_prot_lkup_ext *lkup_exts)
4390 {
4391         u16 j, word = 0;
4392         u16 prot_id;
4393         u16 ret_val;
4394
4395         if (!ice_prot_type_to_id(rule->type, &prot_id))
4396                 return 0;
4397
4398         word = lkup_exts->n_val_words;
4399
4400         for (j = 0; j < sizeof(rule->m_u) / sizeof(u16); j++)
4401                 if (((u16 *)&rule->m_u)[j] == 0xffff &&
4402                     rule->type < ARRAY_SIZE(ice_prot_ext)) {
4403                         /* No more space to accommodate */
4404                         if (word >= ICE_MAX_CHAIN_WORDS)
4405                                 return 0;
4406                         lkup_exts->fv_words[word].off =
4407                                 ice_prot_ext[rule->type].offs[j];
4408                         lkup_exts->fv_words[word].prot_id =
4409                                 ice_prot_id_tbl[rule->type].protocol_id;
4410                         word++;
4411                 }
4412
4413         ret_val = word - lkup_exts->n_val_words;
4414         lkup_exts->n_val_words = word;
4415
4416         return ret_val;
4417 }
4418
4419 /**
4420  * ice_find_prot_off_ind - check for specific ID and offset in rule
4421  * @lkup_exts: an array of protocol header extractions
4422  * @prot_type: protocol type to check
4423  * @off: expected offset of the extraction
4424  *
4425  * Check if the prot_ext has given protocol ID and offset
4426  */
4427 static u8
4428 ice_find_prot_off_ind(struct ice_prot_lkup_ext *lkup_exts, u8 prot_type,
4429                       u16 off)
4430 {
4431         u8 j;
4432
4433         for (j = 0; j < lkup_exts->n_val_words; j++)
4434                 if (lkup_exts->fv_words[j].off == off &&
4435                     lkup_exts->fv_words[j].prot_id == prot_type)
4436                         return j;
4437
4438         return ICE_MAX_CHAIN_WORDS;
4439 }
4440
4441 /**
4442  * ice_is_recipe_subset - check if recipe group policy is a subset of lookup
4443  * @lkup_exts: an array of protocol header extractions
4444  * @r_policy: preferred recipe grouping policy
4445  *
4446  * Helper function to check if given recipe group is subset we need to check if
4447  * all the words described by the given recipe group exist in the advanced rule
4448  * look up information
4449  */
4450 static bool
4451 ice_is_recipe_subset(struct ice_prot_lkup_ext *lkup_exts,
4452                      const struct ice_pref_recipe_group *r_policy)
4453 {
4454         u8 ind[ICE_NUM_WORDS_RECIPE];
4455         u8 count = 0;
4456         u8 i;
4457
4458         /* check if everything in the r_policy is part of the entire rule */
4459         for (i = 0; i < r_policy->n_val_pairs; i++) {
4460                 u8 j;
4461
4462                 j = ice_find_prot_off_ind(lkup_exts, r_policy->pairs[i].prot_id,
4463                                           r_policy->pairs[i].off);
4464                 if (j >= ICE_MAX_CHAIN_WORDS)
4465                         return false;
4466
4467                 /* store the indexes temporarily found by the find function
4468                  * this will be used to mark the words as 'done'
4469                  */
4470                 ind[count++] = j;
4471         }
4472
4473         /* If the entire policy recipe was a true match, then mark the fields
4474          * that are covered by the recipe as 'done' meaning that these words
4475          * will be clumped together in one recipe.
4476          * "Done" here means in our searching if certain recipe group
4477          * matches or is subset of the given rule, then we mark all
4478          * the corresponding offsets as found. So the remaining recipes should
4479          * be created with whatever words that were left.
4480          */
4481         for (i = 0; i < count; i++) {
4482                 u8 in = ind[i];
4483
4484                 ice_set_bit(in, lkup_exts->done);
4485         }
4486         return true;
4487 }
4488
4489 /**
4490  * ice_create_first_fit_recp_def - Create a recipe grouping
4491  * @hw: pointer to the hardware structure
4492  * @lkup_exts: an array of protocol header extractions
4493  * @rg_list: pointer to a list that stores new recipe groups
4494  * @recp_cnt: pointer to a variable that stores returned number of recipe groups
4495  *
4496  * Using first fit algorithm, take all the words that are still not done
4497  * and start grouping them in 4-word groups. Each group makes up one
4498  * recipe.
4499  */
4500 static enum ice_status
4501 ice_create_first_fit_recp_def(struct ice_hw *hw,
4502                               struct ice_prot_lkup_ext *lkup_exts,
4503                               struct LIST_HEAD_TYPE *rg_list,
4504                               u8 *recp_cnt)
4505 {
4506         struct ice_pref_recipe_group *grp = NULL;
4507         u8 j;
4508
4509         *recp_cnt = 0;
4510
4511         /* Walk through every word in the rule to check if it is not done. If so
4512          * then this word needs to be part of a new recipe.
4513          */
4514         for (j = 0; j < lkup_exts->n_val_words; j++)
4515                 if (!ice_is_bit_set(lkup_exts->done, j)) {
4516                         if (!grp ||
4517                             grp->n_val_pairs == ICE_NUM_WORDS_RECIPE) {
4518                                 struct ice_recp_grp_entry *entry;
4519
4520                                 entry = (struct ice_recp_grp_entry *)
4521                                         ice_malloc(hw, sizeof(*entry));
4522                                 if (!entry)
4523                                         return ICE_ERR_NO_MEMORY;
4524                                 LIST_ADD(&entry->l_entry, rg_list);
4525                                 grp = &entry->r_group;
4526                                 (*recp_cnt)++;
4527                         }
4528
4529                         grp->pairs[grp->n_val_pairs].prot_id =
4530                                 lkup_exts->fv_words[j].prot_id;
4531                         grp->pairs[grp->n_val_pairs].off =
4532                                 lkup_exts->fv_words[j].off;
4533                         grp->n_val_pairs++;
4534                 }
4535
4536         return ICE_SUCCESS;
4537 }
4538
4539 /**
4540  * ice_fill_fv_word_index - fill in the field vector indices for a recipe group
4541  * @hw: pointer to the hardware structure
4542  * @fv_list: field vector with the extraction sequence information
4543  * @rg_list: recipe groupings with protocol-offset pairs
4544  *
4545  * Helper function to fill in the field vector indices for protocol-offset
4546  * pairs. These indexes are then ultimately programmed into a recipe.
4547  */
4548 static void
4549 ice_fill_fv_word_index(struct ice_hw *hw, struct LIST_HEAD_TYPE *fv_list,
4550                        struct LIST_HEAD_TYPE *rg_list)
4551 {
4552         struct ice_sw_fv_list_entry *fv;
4553         struct ice_recp_grp_entry *rg;
4554         struct ice_fv_word *fv_ext;
4555
4556         if (LIST_EMPTY(fv_list))
4557                 return;
4558
4559         fv = LIST_FIRST_ENTRY(fv_list, struct ice_sw_fv_list_entry, list_entry);
4560         fv_ext = fv->fv_ptr->ew;
4561
4562         LIST_FOR_EACH_ENTRY(rg, rg_list, ice_recp_grp_entry, l_entry) {
4563                 u8 i;
4564
4565                 for (i = 0; i < rg->r_group.n_val_pairs; i++) {
4566                         struct ice_fv_word *pr;
4567                         u8 j;
4568
4569                         pr = &rg->r_group.pairs[i];
4570                         for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
4571                                 if (fv_ext[j].prot_id == pr->prot_id &&
4572                                     fv_ext[j].off == pr->off) {
4573                                         /* Store index of field vector */
4574                                         rg->fv_idx[i] = j;
4575                                         break;
4576                                 }
4577                 }
4578         }
4579 }
4580
4581 /**
4582  * ice_add_sw_recipe - function to call AQ calls to create switch recipe
4583  * @hw: pointer to hardware structure
4584  * @rm: recipe management list entry
4585  * @match_tun: if field vector index for tunnel needs to be programmed
4586  */
4587 static enum ice_status
4588 ice_add_sw_recipe(struct ice_hw *hw, struct ice_sw_recipe *rm,
4589                   bool match_tun)
4590 {
4591         struct ice_aqc_recipe_data_elem *tmp;
4592         struct ice_aqc_recipe_data_elem *buf;
4593         struct ice_recp_grp_entry *entry;
4594         enum ice_status status;
4595         u16 recipe_count;
4596         u8 chain_idx;
4597         u8 recps = 0;
4598
4599         /* When more than one recipe are required, another recipe is needed to
4600          * chain them together. Matching a tunnel metadata ID takes up one of
4601          * the match fields in the chaining recipe reducing the number of
4602          * chained recipes by one.
4603          */
4604         if (rm->n_grp_count > 1)
4605                 rm->n_grp_count++;
4606         if (rm->n_grp_count > ICE_MAX_CHAIN_RECIPE ||
4607             (match_tun && rm->n_grp_count > (ICE_MAX_CHAIN_RECIPE - 1)))
4608                 return ICE_ERR_MAX_LIMIT;
4609
4610         tmp = (struct ice_aqc_recipe_data_elem *)ice_calloc(hw,
4611                                                             ICE_MAX_NUM_RECIPES,
4612                                                             sizeof(*tmp));
4613         if (!tmp)
4614                 return ICE_ERR_NO_MEMORY;
4615
4616         buf = (struct ice_aqc_recipe_data_elem *)
4617                 ice_calloc(hw, rm->n_grp_count, sizeof(*buf));
4618         if (!buf) {
4619                 status = ICE_ERR_NO_MEMORY;
4620                 goto err_mem;
4621         }
4622
4623         ice_zero_bitmap(rm->r_bitmap, ICE_MAX_NUM_RECIPES);
4624         recipe_count = ICE_MAX_NUM_RECIPES;
4625         status = ice_aq_get_recipe(hw, tmp, &recipe_count, ICE_SW_LKUP_MAC,
4626                                    NULL);
4627         if (status || recipe_count == 0)
4628                 goto err_unroll;
4629
4630         /* Allocate the recipe resources, and configure them according to the
4631          * match fields from protocol headers and extracted field vectors.
4632          */
4633         chain_idx = ICE_CHAIN_FV_INDEX_START -
4634                 ice_find_first_bit(available_result_ids,
4635                                    ICE_CHAIN_FV_INDEX_START + 1);
4636         LIST_FOR_EACH_ENTRY(entry, &rm->rg_list, ice_recp_grp_entry, l_entry) {
4637                 u8 i;
4638
4639                 status = ice_alloc_recipe(hw, &entry->rid);
4640                 if (status)
4641                         goto err_unroll;
4642
4643                 /* Clear the result index of the located recipe, as this will be
4644                  * updated, if needed, later in the recipe creation process.
4645                  */
4646                 tmp[0].content.result_indx = 0;
4647
4648                 buf[recps] = tmp[0];
4649                 buf[recps].recipe_indx = (u8)entry->rid;
4650                 /* if the recipe is a non-root recipe RID should be programmed
4651                  * as 0 for the rules to be applied correctly.
4652                  */
4653                 buf[recps].content.rid = 0;
4654                 ice_memset(&buf[recps].content.lkup_indx, 0,
4655                            sizeof(buf[recps].content.lkup_indx),
4656                            ICE_NONDMA_MEM);
4657
4658                 /* All recipes use look-up index 0 to match switch ID. */
4659                 buf[recps].content.lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
4660                 buf[recps].content.mask[0] =
4661                         CPU_TO_LE16(ICE_AQ_SW_ID_LKUP_MASK);
4662                 /* Setup lkup_indx 1..4 to INVALID/ignore and set the mask
4663                  * to be 0
4664                  */
4665                 for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
4666                         buf[recps].content.lkup_indx[i] = 0x80;
4667                         buf[recps].content.mask[i] = 0;
4668                 }
4669
4670                 for (i = 0; i < entry->r_group.n_val_pairs; i++) {
4671                         buf[recps].content.lkup_indx[i + 1] = entry->fv_idx[i];
4672                         buf[recps].content.mask[i + 1] = CPU_TO_LE16(0xFFFF);
4673                 }
4674
4675                 if (rm->n_grp_count > 1) {
4676                         entry->chain_idx = chain_idx;
4677                         buf[recps].content.result_indx =
4678                                 ICE_AQ_RECIPE_RESULT_EN |
4679                                 ((chain_idx << ICE_AQ_RECIPE_RESULT_DATA_S) &
4680                                  ICE_AQ_RECIPE_RESULT_DATA_M);
4681                         ice_clear_bit(ICE_CHAIN_FV_INDEX_START - chain_idx,
4682                                       available_result_ids);
4683                         chain_idx = ICE_CHAIN_FV_INDEX_START -
4684                                 ice_find_first_bit(available_result_ids,
4685                                                    ICE_CHAIN_FV_INDEX_START +
4686                                                    1);
4687                 }
4688
4689                 /* fill recipe dependencies */
4690                 ice_zero_bitmap((ice_bitmap_t *)buf[recps].recipe_bitmap,
4691                                 ICE_MAX_NUM_RECIPES);
4692                 ice_set_bit(buf[recps].recipe_indx,
4693                             (ice_bitmap_t *)buf[recps].recipe_bitmap);
4694                 buf[recps].content.act_ctrl_fwd_priority = rm->priority;
4695                 recps++;
4696         }
4697
4698         if (rm->n_grp_count == 1) {
4699                 rm->root_rid = buf[0].recipe_indx;
4700                 ice_set_bit(buf[0].recipe_indx, rm->r_bitmap);
4701                 buf[0].content.rid = rm->root_rid | ICE_AQ_RECIPE_ID_IS_ROOT;
4702                 if (sizeof(buf[0].recipe_bitmap) >= sizeof(rm->r_bitmap)) {
4703                         ice_memcpy(buf[0].recipe_bitmap, rm->r_bitmap,
4704                                    sizeof(buf[0].recipe_bitmap),
4705                                    ICE_NONDMA_TO_NONDMA);
4706                 } else {
4707                         status = ICE_ERR_BAD_PTR;
4708                         goto err_unroll;
4709                 }
4710                 /* Applicable only for ROOT_RECIPE, set the fwd_priority for
4711                  * the recipe which is getting created if specified
4712                  * by user. Usually any advanced switch filter, which results
4713                  * into new extraction sequence, ended up creating a new recipe
4714                  * of type ROOT and usually recipes are associated with profiles
4715                  * Switch rule referreing newly created recipe, needs to have
4716                  * either/or 'fwd' or 'join' priority, otherwise switch rule
4717                  * evaluation will not happen correctly. In other words, if
4718                  * switch rule to be evaluated on priority basis, then recipe
4719                  * needs to have priority, otherwise it will be evaluated last.
4720                  */
4721                 buf[0].content.act_ctrl_fwd_priority = rm->priority;
4722         } else {
4723                 struct ice_recp_grp_entry *last_chain_entry;
4724                 u16 rid, i;
4725
4726                 /* Allocate the last recipe that will chain the outcomes of the
4727                  * other recipes together
4728                  */
4729                 status = ice_alloc_recipe(hw, &rid);
4730                 if (status)
4731                         goto err_unroll;
4732
4733                 buf[recps].recipe_indx = (u8)rid;
4734                 buf[recps].content.rid = (u8)rid;
4735                 buf[recps].content.rid |= ICE_AQ_RECIPE_ID_IS_ROOT;
4736                 /* the new entry created should also be part of rg_list to
4737                  * make sure we have complete recipe
4738                  */
4739                 last_chain_entry = (struct ice_recp_grp_entry *)ice_malloc(hw,
4740                         sizeof(*last_chain_entry));
4741                 if (!last_chain_entry) {
4742                         status = ICE_ERR_NO_MEMORY;
4743                         goto err_unroll;
4744                 }
4745                 last_chain_entry->rid = rid;
4746                 ice_memset(&buf[recps].content.lkup_indx, 0,
4747                            sizeof(buf[recps].content.lkup_indx),
4748                            ICE_NONDMA_MEM);
4749                 /* All recipes use look-up index 0 to match switch ID. */
4750                 buf[recps].content.lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
4751                 buf[recps].content.mask[0] =
4752                         CPU_TO_LE16(ICE_AQ_SW_ID_LKUP_MASK);
4753                 for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
4754                         buf[recps].content.lkup_indx[i] =
4755                                 ICE_AQ_RECIPE_LKUP_IGNORE;
4756                         buf[recps].content.mask[i] = 0;
4757                 }
4758
4759                 i = 1;
4760                 /* update r_bitmap with the recp that is used for chaining */
4761                 ice_set_bit(rid, rm->r_bitmap);
4762                 /* this is the recipe that chains all the other recipes so it
4763                  * should not have a chaining ID to indicate the same
4764                  */
4765                 last_chain_entry->chain_idx = ICE_INVAL_CHAIN_IND;
4766                 LIST_FOR_EACH_ENTRY(entry, &rm->rg_list, ice_recp_grp_entry,
4767                                     l_entry) {
4768                         last_chain_entry->fv_idx[i] = entry->chain_idx;
4769                         buf[recps].content.lkup_indx[i] = entry->chain_idx;
4770                         buf[recps].content.mask[i++] = CPU_TO_LE16(0xFFFF);
4771                         ice_set_bit(entry->rid, rm->r_bitmap);
4772                 }
4773                 LIST_ADD(&last_chain_entry->l_entry, &rm->rg_list);
4774                 if (sizeof(buf[recps].recipe_bitmap) >=
4775                     sizeof(rm->r_bitmap)) {
4776                         ice_memcpy(buf[recps].recipe_bitmap, rm->r_bitmap,
4777                                    sizeof(buf[recps].recipe_bitmap),
4778                                    ICE_NONDMA_TO_NONDMA);
4779                 } else {
4780                         status = ICE_ERR_BAD_PTR;
4781                         goto err_unroll;
4782                 }
4783                 buf[recps].content.act_ctrl_fwd_priority = rm->priority;
4784
4785                 /* To differentiate among different UDP tunnels, a meta data ID
4786                  * flag is used.
4787                  */
4788                 if (match_tun) {
4789                         buf[recps].content.lkup_indx[i] = ICE_TUN_FLAG_FV_IND;
4790                         buf[recps].content.mask[i] =
4791                                 CPU_TO_LE16(ICE_TUN_FLAG_MASK);
4792                 }
4793
4794                 recps++;
4795                 rm->root_rid = (u8)rid;
4796         }
4797         status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
4798         if (status)
4799                 goto err_unroll;
4800
4801         status = ice_aq_add_recipe(hw, buf, rm->n_grp_count, NULL);
4802         ice_release_change_lock(hw);
4803         if (status)
4804                 goto err_unroll;
4805
4806         /* Every recipe that just got created add it to the recipe
4807          * book keeping list
4808          */
4809         LIST_FOR_EACH_ENTRY(entry, &rm->rg_list, ice_recp_grp_entry, l_entry) {
4810                 struct ice_switch_info *sw = hw->switch_info;
4811                 struct ice_sw_recipe *recp;
4812
4813                 recp = &sw->recp_list[entry->rid];
4814                 recp->root_rid = entry->rid;
4815                 ice_memcpy(&recp->ext_words, entry->r_group.pairs,
4816                            entry->r_group.n_val_pairs *
4817                            sizeof(struct ice_fv_word),
4818                            ICE_NONDMA_TO_NONDMA);
4819
4820                 recp->n_ext_words = entry->r_group.n_val_pairs;
4821                 recp->chain_idx = entry->chain_idx;
4822                 recp->recp_created = true;
4823                 recp->big_recp = false;
4824         }
4825         rm->root_buf = buf;
4826         ice_free(hw, tmp);
4827         return status;
4828
4829 err_unroll:
4830 err_mem:
4831         ice_free(hw, tmp);
4832         ice_free(hw, buf);
4833         return status;
4834 }
4835
4836 /**
4837  * ice_create_recipe_group - creates recipe group
4838  * @hw: pointer to hardware structure
4839  * @rm: recipe management list entry
4840  * @lkup_exts: lookup elements
4841  */
4842 static enum ice_status
4843 ice_create_recipe_group(struct ice_hw *hw, struct ice_sw_recipe *rm,
4844                         struct ice_prot_lkup_ext *lkup_exts)
4845 {
4846         struct ice_recp_grp_entry *entry;
4847         struct ice_recp_grp_entry *tmp;
4848         enum ice_status status;
4849         u8 recp_count = 0;
4850         u16 groups, i;
4851
4852         rm->n_grp_count = 0;
4853
4854         /* Each switch recipe can match up to 5 words or metadata. One word in
4855          * each recipe is used to match the switch ID. Four words are left for
4856          * matching other values. If the new advanced recipe requires more than
4857          * 4 words, it needs to be split into multiple recipes which are chained
4858          * together using the intermediate result that each produces as input to
4859          * the other recipes in the sequence.
4860          */
4861         groups = ARRAY_SIZE(ice_recipe_pack);
4862
4863         /* Check if any of the preferred recipes from the grouping policy
4864          * matches.
4865          */
4866         for (i = 0; i < groups; i++)
4867                 /* Check if the recipe from the preferred grouping matches
4868                  * or is a subset of the fields that needs to be looked up.
4869                  */
4870                 if (ice_is_recipe_subset(lkup_exts, &ice_recipe_pack[i])) {
4871                         /* This recipe can be used by itself or grouped with
4872                          * other recipes.
4873                          */
4874                         entry = (struct ice_recp_grp_entry *)
4875                                 ice_malloc(hw, sizeof(*entry));
4876                         if (!entry) {
4877                                 status = ICE_ERR_NO_MEMORY;
4878                                 goto err_unroll;
4879                         }
4880                         entry->r_group = ice_recipe_pack[i];
4881                         LIST_ADD(&entry->l_entry, &rm->rg_list);
4882                         rm->n_grp_count++;
4883                 }
4884
4885         /* Create recipes for words that are marked not done by packing them
4886          * as best fit.
4887          */
4888         status = ice_create_first_fit_recp_def(hw, lkup_exts,
4889                                                &rm->rg_list, &recp_count);
4890         if (!status) {
4891                 rm->n_grp_count += recp_count;
4892                 rm->n_ext_words = lkup_exts->n_val_words;
4893                 ice_memcpy(&rm->ext_words, lkup_exts->fv_words,
4894                            sizeof(rm->ext_words), ICE_NONDMA_TO_NONDMA);
4895                 goto out;
4896         }
4897
4898 err_unroll:
4899         LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, &rm->rg_list, ice_recp_grp_entry,
4900                                  l_entry) {
4901                 LIST_DEL(&entry->l_entry);
4902                 ice_free(hw, entry);
4903         }
4904
4905 out:
4906         return status;
4907 }
4908
4909 /**
4910  * ice_get_fv - get field vectors/extraction sequences for spec. lookup types
4911  * @hw: pointer to hardware structure
4912  * @lkups: lookup elements or match criteria for the advanced recipe, one
4913  *         structure per protocol header
4914  * @lkups_cnt: number of protocols
4915  * @fv_list: pointer to a list that holds the returned field vectors
4916  */
4917 static enum ice_status
4918 ice_get_fv(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
4919            struct LIST_HEAD_TYPE *fv_list)
4920 {
4921         enum ice_status status;
4922         u16 *prot_ids;
4923         u16 i;
4924
4925         prot_ids = (u16 *)ice_calloc(hw, lkups_cnt, sizeof(*prot_ids));
4926         if (!prot_ids)
4927                 return ICE_ERR_NO_MEMORY;
4928
4929         for (i = 0; i < lkups_cnt; i++)
4930                 if (!ice_prot_type_to_id(lkups[i].type, &prot_ids[i])) {
4931                         status = ICE_ERR_CFG;
4932                         goto free_mem;
4933                 }
4934
4935         /* Find field vectors that include all specified protocol types */
4936         status = ice_get_sw_fv_list(hw, prot_ids, lkups_cnt, fv_list);
4937
4938 free_mem:
4939         ice_free(hw, prot_ids);
4940         return status;
4941 }
4942
4943 /**
4944  * ice_add_adv_recipe - Add an advanced recipe that is not part of the default
4945  * @hw: pointer to hardware structure
4946  * @lkups: lookup elements or match criteria for the advanced recipe, one
4947  *  structure per protocol header
4948  * @lkups_cnt: number of protocols
4949  * @rinfo: other information regarding the rule e.g. priority and action info
4950  * @rid: return the recipe ID of the recipe created
4951  */
4952 static enum ice_status
4953 ice_add_adv_recipe(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
4954                    u16 lkups_cnt, struct ice_adv_rule_info *rinfo, u16 *rid)
4955 {
4956         struct ice_prot_lkup_ext *lkup_exts;
4957         struct ice_recp_grp_entry *r_entry;
4958         struct ice_sw_fv_list_entry *fvit;
4959         struct ice_recp_grp_entry *r_tmp;
4960         struct ice_sw_fv_list_entry *tmp;
4961         enum ice_status status = ICE_SUCCESS;
4962         struct ice_sw_recipe *rm;
4963         bool match_tun = false;
4964         u8 i;
4965
4966         if (!lkups_cnt)
4967                 return ICE_ERR_PARAM;
4968
4969         lkup_exts = (struct ice_prot_lkup_ext *)
4970                 ice_malloc(hw, sizeof(*lkup_exts));
4971         if (!lkup_exts)
4972                 return ICE_ERR_NO_MEMORY;
4973
4974         /* Determine the number of words to be matched and if it exceeds a
4975          * recipe's restrictions
4976          */
4977         for (i = 0; i < lkups_cnt; i++) {
4978                 u16 count;
4979
4980                 if (lkups[i].type >= ICE_PROTOCOL_LAST) {
4981                         status = ICE_ERR_CFG;
4982                         goto err_free_lkup_exts;
4983                 }
4984
4985                 count = ice_fill_valid_words(&lkups[i], lkup_exts);
4986                 if (!count) {
4987                         status = ICE_ERR_CFG;
4988                         goto err_free_lkup_exts;
4989                 }
4990         }
4991
4992         *rid = ice_find_recp(hw, lkup_exts);
4993         if (*rid < ICE_MAX_NUM_RECIPES)
4994                 /* Success if found a recipe that match the existing criteria */
4995                 goto err_free_lkup_exts;
4996
4997         /* Recipe we need does not exist, add a recipe */
4998
4999         rm = (struct ice_sw_recipe *)ice_malloc(hw, sizeof(*rm));
5000         if (!rm) {
5001                 status = ICE_ERR_NO_MEMORY;
5002                 goto err_free_lkup_exts;
5003         }
5004
5005         /* Get field vectors that contain fields extracted from all the protocol
5006          * headers being programmed.
5007          */
5008         INIT_LIST_HEAD(&rm->fv_list);
5009         INIT_LIST_HEAD(&rm->rg_list);
5010
5011         status = ice_get_fv(hw, lkups, lkups_cnt, &rm->fv_list);
5012         if (status)
5013                 goto err_unroll;
5014
5015         /* Group match words into recipes using preferred recipe grouping
5016          * criteria.
5017          */
5018         status = ice_create_recipe_group(hw, rm, lkup_exts);
5019         if (status)
5020                 goto err_unroll;
5021
5022         /* There is only profile for UDP tunnels. So, it is necessary to use a
5023          * metadata ID flag to differentiate different tunnel types. A separate
5024          * recipe needs to be used for the metadata.
5025          */
5026         if ((rinfo->tun_type == ICE_SW_TUN_VXLAN_GPE ||
5027              rinfo->tun_type == ICE_SW_TUN_GENEVE ||
5028              rinfo->tun_type == ICE_SW_TUN_VXLAN) && rm->n_grp_count > 1)
5029                 match_tun = true;
5030
5031         /* set the recipe priority if specified */
5032         rm->priority = rinfo->priority ? rinfo->priority : 0;
5033
5034         /* Find offsets from the field vector. Pick the first one for all the
5035          * recipes.
5036          */
5037         ice_fill_fv_word_index(hw, &rm->fv_list, &rm->rg_list);
5038         status = ice_add_sw_recipe(hw, rm, match_tun);
5039         if (status)
5040                 goto err_unroll;
5041
5042         /* Associate all the recipes created with all the profiles in the
5043          * common field vector.
5044          */
5045         LIST_FOR_EACH_ENTRY(fvit, &rm->fv_list, ice_sw_fv_list_entry,
5046                             list_entry) {
5047                 ice_declare_bitmap(r_bitmap, ICE_MAX_NUM_RECIPES);
5048
5049                 status = ice_aq_get_recipe_to_profile(hw, fvit->profile_id,
5050                                                       (u8 *)r_bitmap, NULL);
5051                 if (status)
5052                         goto err_unroll;
5053
5054                 ice_or_bitmap(rm->r_bitmap, r_bitmap, rm->r_bitmap,
5055                               ICE_MAX_NUM_RECIPES);
5056                 status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
5057                 if (status)
5058                         goto err_unroll;
5059
5060                 status = ice_aq_map_recipe_to_profile(hw, fvit->profile_id,
5061                                                       (u8 *)rm->r_bitmap,
5062                                                       NULL);
5063                 ice_release_change_lock(hw);
5064
5065                 if (status)
5066                         goto err_unroll;
5067         }
5068
5069         *rid = rm->root_rid;
5070         ice_memcpy(&hw->switch_info->recp_list[*rid].lkup_exts,
5071                    lkup_exts, sizeof(*lkup_exts), ICE_NONDMA_TO_NONDMA);
5072 err_unroll:
5073         LIST_FOR_EACH_ENTRY_SAFE(r_entry, r_tmp, &rm->rg_list,
5074                                  ice_recp_grp_entry, l_entry) {
5075                 LIST_DEL(&r_entry->l_entry);
5076                 ice_free(hw, r_entry);
5077         }
5078
5079         LIST_FOR_EACH_ENTRY_SAFE(fvit, tmp, &rm->fv_list, ice_sw_fv_list_entry,
5080                                  list_entry) {
5081                 LIST_DEL(&fvit->list_entry);
5082                 ice_free(hw, fvit);
5083         }
5084
5085         if (rm->root_buf)
5086                 ice_free(hw, rm->root_buf);
5087
5088         ice_free(hw, rm);
5089
5090 err_free_lkup_exts:
5091         ice_free(hw, lkup_exts);
5092
5093         return status;
5094 }
5095
5096 #define ICE_MAC_HDR_OFFSET      0
5097 #define ICE_IP_HDR_OFFSET       14
5098 #define ICE_GRE_HDR_OFFSET      34
5099 #define ICE_MAC_IL_HDR_OFFSET   42
5100 #define ICE_IP_IL_HDR_OFFSET    56
5101 #define ICE_L4_HDR_OFFSET       34
5102 #define ICE_UDP_TUN_HDR_OFFSET  42
5103
5104 /**
5105  * ice_find_dummy_packet - find dummy packet with given match criteria
5106  *
5107  * @lkups: lookup elements or match criteria for the advanced recipe, one
5108  *         structure per protocol header
5109  * @lkups_cnt: number of protocols
5110  * @tun_type: tunnel type from the match criteria
5111  * @pkt: dummy packet to fill according to filter match criteria
5112  * @pkt_len: packet length of dummy packet
5113  */
5114 static void
5115 ice_find_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5116                       enum ice_sw_tunnel_type tun_type, const u8 **pkt,
5117                       u16 *pkt_len)
5118 {
5119         u16 i;
5120
5121         if (tun_type == ICE_SW_TUN_NVGRE || tun_type == ICE_ALL_TUNNELS) {
5122                 *pkt = dummy_gre_packet;
5123                 *pkt_len = sizeof(dummy_gre_packet);
5124                 return;
5125         }
5126
5127         if (tun_type == ICE_SW_TUN_VXLAN || tun_type == ICE_SW_TUN_GENEVE ||
5128             tun_type == ICE_SW_TUN_VXLAN_GPE) {
5129                 *pkt = dummy_udp_tun_packet;
5130                 *pkt_len = sizeof(dummy_udp_tun_packet);
5131                 return;
5132         }
5133
5134         for (i = 0; i < lkups_cnt; i++) {
5135                 if (lkups[i].type == ICE_UDP_ILOS) {
5136                         *pkt = dummy_udp_tun_packet;
5137                         *pkt_len = sizeof(dummy_udp_tun_packet);
5138                         return;
5139                 }
5140         }
5141
5142         *pkt = dummy_tcp_tun_packet;
5143         *pkt_len = sizeof(dummy_tcp_tun_packet);
5144 }
5145
5146 /**
5147  * ice_fill_adv_dummy_packet - fill a dummy packet with given match criteria
5148  *
5149  * @lkups: lookup elements or match criteria for the advanced recipe, one
5150  *         structure per protocol header
5151  * @lkups_cnt: number of protocols
5152  * @tun_type: to know if the dummy packet is supposed to be tunnel packet
5153  * @s_rule: stores rule information from the match criteria
5154  * @dummy_pkt: dummy packet to fill according to filter match criteria
5155  * @pkt_len: packet length of dummy packet
5156  */
5157 static void
5158 ice_fill_adv_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5159                           enum ice_sw_tunnel_type tun_type,
5160                           struct ice_aqc_sw_rules_elem *s_rule,
5161                           const u8 *dummy_pkt, u16 pkt_len)
5162 {
5163         u8 *pkt;
5164         u16 i;
5165
5166         /* Start with a packet with a pre-defined/dummy content. Then, fill
5167          * in the header values to be looked up or matched.
5168          */
5169         pkt = s_rule->pdata.lkup_tx_rx.hdr;
5170
5171         ice_memcpy(pkt, dummy_pkt, pkt_len, ICE_NONDMA_TO_NONDMA);
5172
5173         for (i = 0; i < lkups_cnt; i++) {
5174                 u32 len, pkt_off, hdr_size, field_off;
5175
5176                 switch (lkups[i].type) {
5177                 case ICE_MAC_OFOS:
5178                 case ICE_MAC_IL:
5179                         pkt_off = offsetof(struct ice_ether_hdr, dst_addr) +
5180                                 ((lkups[i].type == ICE_MAC_IL) ?
5181                                  ICE_MAC_IL_HDR_OFFSET : 0);
5182                         len = sizeof(lkups[i].h_u.eth_hdr.dst_addr);
5183                         if ((tun_type == ICE_SW_TUN_VXLAN ||
5184                              tun_type == ICE_SW_TUN_GENEVE ||
5185                              tun_type == ICE_SW_TUN_VXLAN_GPE) &&
5186                              lkups[i].type == ICE_MAC_IL) {
5187                                 pkt_off += sizeof(struct ice_udp_tnl_hdr);
5188                         }
5189
5190                         ice_memcpy(&pkt[pkt_off],
5191                                    &lkups[i].h_u.eth_hdr.dst_addr, len,
5192                                    ICE_NONDMA_TO_NONDMA);
5193                         pkt_off = offsetof(struct ice_ether_hdr, src_addr) +
5194                                 ((lkups[i].type == ICE_MAC_IL) ?
5195                                  ICE_MAC_IL_HDR_OFFSET : 0);
5196                         len = sizeof(lkups[i].h_u.eth_hdr.src_addr);
5197                         if ((tun_type == ICE_SW_TUN_VXLAN ||
5198                              tun_type == ICE_SW_TUN_GENEVE ||
5199                              tun_type == ICE_SW_TUN_VXLAN_GPE) &&
5200                              lkups[i].type == ICE_MAC_IL) {
5201                                 pkt_off += sizeof(struct ice_udp_tnl_hdr);
5202                         }
5203                         ice_memcpy(&pkt[pkt_off],
5204                                    &lkups[i].h_u.eth_hdr.src_addr, len,
5205                                    ICE_NONDMA_TO_NONDMA);
5206                         if (lkups[i].h_u.eth_hdr.ethtype_id) {
5207                                 pkt_off = offsetof(struct ice_ether_hdr,
5208                                                    ethtype_id) +
5209                                         ((lkups[i].type == ICE_MAC_IL) ?
5210                                          ICE_MAC_IL_HDR_OFFSET : 0);
5211                                 len = sizeof(lkups[i].h_u.eth_hdr.ethtype_id);
5212                                 if ((tun_type == ICE_SW_TUN_VXLAN ||
5213                                      tun_type == ICE_SW_TUN_GENEVE ||
5214                                      tun_type == ICE_SW_TUN_VXLAN_GPE) &&
5215                                      lkups[i].type == ICE_MAC_IL) {
5216                                         pkt_off +=
5217                                                 sizeof(struct ice_udp_tnl_hdr);
5218                                 }
5219                                 ice_memcpy(&pkt[pkt_off],
5220                                            &lkups[i].h_u.eth_hdr.ethtype_id,
5221                                            len, ICE_NONDMA_TO_NONDMA);
5222                         }
5223                         break;
5224                 case ICE_IPV4_OFOS:
5225                         hdr_size = sizeof(struct ice_ipv4_hdr);
5226                         if (lkups[i].h_u.ipv4_hdr.dst_addr) {
5227                                 pkt_off = ICE_IP_HDR_OFFSET +
5228                                            offsetof(struct ice_ipv4_hdr,
5229                                                     dst_addr);
5230                                 field_off = offsetof(struct ice_ipv4_hdr,
5231                                                      dst_addr);
5232                                 len = hdr_size - field_off;
5233                                 ice_memcpy(&pkt[pkt_off],
5234                                            &lkups[i].h_u.ipv4_hdr.dst_addr,
5235                                            len, ICE_NONDMA_TO_NONDMA);
5236                         }
5237                         if (lkups[i].h_u.ipv4_hdr.src_addr) {
5238                                 pkt_off = ICE_IP_HDR_OFFSET +
5239                                            offsetof(struct ice_ipv4_hdr,
5240                                                     src_addr);
5241                                 field_off = offsetof(struct ice_ipv4_hdr,
5242                                                      src_addr);
5243                                 len = hdr_size - field_off;
5244                                 ice_memcpy(&pkt[pkt_off],
5245                                            &lkups[i].h_u.ipv4_hdr.src_addr,
5246                                            len, ICE_NONDMA_TO_NONDMA);
5247                         }
5248                         break;
5249                 case ICE_IPV4_IL:
5250                         break;
5251                 case ICE_TCP_IL:
5252                 case ICE_UDP_ILOS:
5253                 case ICE_SCTP_IL:
5254                         hdr_size = sizeof(struct ice_udp_tnl_hdr);
5255                         if (lkups[i].h_u.l4_hdr.dst_port) {
5256                                 pkt_off = ICE_L4_HDR_OFFSET +
5257                                            offsetof(struct ice_l4_hdr,
5258                                                     dst_port);
5259                                 field_off = offsetof(struct ice_l4_hdr,
5260                                                      dst_port);
5261                                 len =  hdr_size - field_off;
5262                                 ice_memcpy(&pkt[pkt_off],
5263                                            &lkups[i].h_u.l4_hdr.dst_port,
5264                                            len, ICE_NONDMA_TO_NONDMA);
5265                         }
5266                         if (lkups[i].h_u.l4_hdr.src_port) {
5267                                 pkt_off = ICE_L4_HDR_OFFSET +
5268                                         offsetof(struct ice_l4_hdr, src_port);
5269                                 field_off = offsetof(struct ice_l4_hdr,
5270                                                      src_port);
5271                                 len =  hdr_size - field_off;
5272                                 ice_memcpy(&pkt[pkt_off],
5273                                            &lkups[i].h_u.l4_hdr.src_port,
5274                                            len, ICE_NONDMA_TO_NONDMA);
5275                         }
5276                         break;
5277                 case ICE_VXLAN:
5278                 case ICE_GENEVE:
5279                 case ICE_VXLAN_GPE:
5280                         pkt_off = ICE_UDP_TUN_HDR_OFFSET +
5281                                    offsetof(struct ice_udp_tnl_hdr, vni);
5282                         field_off = offsetof(struct ice_udp_tnl_hdr, vni);
5283                         len =  sizeof(struct ice_udp_tnl_hdr) - field_off;
5284                         ice_memcpy(&pkt[pkt_off], &lkups[i].h_u.tnl_hdr.vni,
5285                                    len, ICE_NONDMA_TO_NONDMA);
5286                         break;
5287                 default:
5288                         break;
5289                 }
5290         }
5291         s_rule->pdata.lkup_tx_rx.hdr_len = CPU_TO_LE16(pkt_len);
5292 }
5293
5294 /**
5295  * ice_find_adv_rule_entry - Search a rule entry
5296  * @hw: pointer to the hardware structure
5297  * @lkups: lookup elements or match criteria for the advanced recipe, one
5298  *         structure per protocol header
5299  * @lkups_cnt: number of protocols
5300  * @recp_id: recipe ID for which we are finding the rule
5301  * @rinfo: other information regarding the rule e.g. priority and action info
5302  *
5303  * Helper function to search for a given advance rule entry
5304  * Returns pointer to entry storing the rule if found
5305  */
5306 static struct ice_adv_fltr_mgmt_list_entry *
5307 ice_find_adv_rule_entry(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5308                         u16 lkups_cnt, u8 recp_id,
5309                         struct ice_adv_rule_info *rinfo)
5310 {
5311         struct ice_adv_fltr_mgmt_list_entry *list_itr;
5312         struct ice_switch_info *sw = hw->switch_info;
5313         int i;
5314
5315         LIST_FOR_EACH_ENTRY(list_itr, &sw->recp_list[recp_id].filt_rules,
5316                             ice_adv_fltr_mgmt_list_entry, list_entry) {
5317                 bool lkups_matched = true;
5318
5319                 if (lkups_cnt != list_itr->lkups_cnt)
5320                         continue;
5321                 for (i = 0; i < list_itr->lkups_cnt; i++)
5322                         if (memcmp(&list_itr->lkups[i], &lkups[i],
5323                                    sizeof(*lkups))) {
5324                                 lkups_matched = false;
5325                                 break;
5326                         }
5327                 if (rinfo->sw_act.flag == list_itr->rule_info.sw_act.flag &&
5328                     rinfo->tun_type == list_itr->rule_info.tun_type &&
5329                     lkups_matched)
5330                         return list_itr;
5331         }
5332         return NULL;
5333 }
5334
5335 /**
5336  * ice_adv_add_update_vsi_list
5337  * @hw: pointer to the hardware structure
5338  * @m_entry: pointer to current adv filter management list entry
5339  * @cur_fltr: filter information from the book keeping entry
5340  * @new_fltr: filter information with the new VSI to be added
5341  *
5342  * Call AQ command to add or update previously created VSI list with new VSI.
5343  *
5344  * Helper function to do book keeping associated with adding filter information
5345  * The algorithm to do the booking keeping is described below :
5346  * When a VSI needs to subscribe to a given advanced filter
5347  *      if only one VSI has been added till now
5348  *              Allocate a new VSI list and add two VSIs
5349  *              to this list using switch rule command
5350  *              Update the previously created switch rule with the
5351  *              newly created VSI list ID
5352  *      if a VSI list was previously created
5353  *              Add the new VSI to the previously created VSI list set
5354  *              using the update switch rule command
5355  */
5356 static enum ice_status
5357 ice_adv_add_update_vsi_list(struct ice_hw *hw,
5358                             struct ice_adv_fltr_mgmt_list_entry *m_entry,
5359                             struct ice_adv_rule_info *cur_fltr,
5360                             struct ice_adv_rule_info *new_fltr)
5361 {
5362         enum ice_status status;
5363         u16 vsi_list_id = 0;
5364
5365         if (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5366             cur_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP)
5367                 return ICE_ERR_NOT_IMPL;
5368
5369         if (cur_fltr->sw_act.fltr_act == ICE_DROP_PACKET &&
5370             new_fltr->sw_act.fltr_act == ICE_DROP_PACKET)
5371                 return ICE_ERR_ALREADY_EXISTS;
5372
5373         if ((new_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5374              new_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP) &&
5375             (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI ||
5376              cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI_LIST))
5377                 return ICE_ERR_NOT_IMPL;
5378
5379         if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
5380                  /* Only one entry existed in the mapping and it was not already
5381                   * a part of a VSI list. So, create a VSI list with the old and
5382                   * new VSIs.
5383                   */
5384                 struct ice_fltr_info tmp_fltr;
5385                 u16 vsi_handle_arr[2];
5386
5387                 /* A rule already exists with the new VSI being added */
5388                 if (cur_fltr->sw_act.fwd_id.hw_vsi_id ==
5389                     new_fltr->sw_act.fwd_id.hw_vsi_id)
5390                         return ICE_ERR_ALREADY_EXISTS;
5391
5392                 vsi_handle_arr[0] = cur_fltr->sw_act.vsi_handle;
5393                 vsi_handle_arr[1] = new_fltr->sw_act.vsi_handle;
5394                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
5395                                                   &vsi_list_id,
5396                                                   ICE_SW_LKUP_LAST);
5397                 if (status)
5398                         return status;
5399
5400                 tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
5401                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
5402                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
5403                 /* Update the previous switch rule of "forward to VSI" to
5404                  * "fwd to VSI list"
5405                  */
5406                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5407                 if (status)
5408                         return status;
5409
5410                 cur_fltr->sw_act.fwd_id.vsi_list_id = vsi_list_id;
5411                 cur_fltr->sw_act.fltr_act = ICE_FWD_TO_VSI_LIST;
5412                 m_entry->vsi_list_info =
5413                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
5414                                                 vsi_list_id);
5415         } else {
5416                 u16 vsi_handle = new_fltr->sw_act.vsi_handle;
5417
5418                 if (!m_entry->vsi_list_info)
5419                         return ICE_ERR_CFG;
5420
5421                 /* A rule already exists with the new VSI being added */
5422                 if (ice_is_bit_set(m_entry->vsi_list_info->vsi_map, vsi_handle))
5423                         return ICE_SUCCESS;
5424
5425                 /* Update the previously created VSI list set with
5426                  * the new VSI ID passed in
5427                  */
5428                 vsi_list_id = cur_fltr->sw_act.fwd_id.vsi_list_id;
5429
5430                 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
5431                                                   vsi_list_id, false,
5432                                                   ice_aqc_opc_update_sw_rules,
5433                                                   ICE_SW_LKUP_LAST);
5434                 /* update VSI list mapping info with new VSI ID */
5435                 if (!status)
5436                         ice_set_bit(vsi_handle,
5437                                     m_entry->vsi_list_info->vsi_map);
5438         }
5439         if (!status)
5440                 m_entry->vsi_count++;
5441         return status;
5442 }
5443
5444 /**
5445  * ice_add_adv_rule - create an advanced switch rule
5446  * @hw: pointer to the hardware structure
5447  * @lkups: information on the words that needs to be looked up. All words
5448  * together makes one recipe
5449  * @lkups_cnt: num of entries in the lkups array
5450  * @rinfo: other information related to the rule that needs to be programmed
5451  * @added_entry: this will return recipe_id, rule_id and vsi_handle. should be
5452  *               ignored is case of error.
5453  *
5454  * This function can program only 1 rule at a time. The lkups is used to
5455  * describe the all the words that forms the "lookup" portion of the recipe.
5456  * These words can span multiple protocols. Callers to this function need to
5457  * pass in a list of protocol headers with lookup information along and mask
5458  * that determines which words are valid from the given protocol header.
5459  * rinfo describes other information related to this rule such as forwarding
5460  * IDs, priority of this rule, etc.
5461  */
5462 enum ice_status
5463 ice_add_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5464                  u16 lkups_cnt, struct ice_adv_rule_info *rinfo,
5465                  struct ice_rule_query_data *added_entry)
5466 {
5467         struct ice_adv_fltr_mgmt_list_entry *m_entry, *adv_fltr = NULL;
5468         u16 rid = 0, i, pkt_len, rule_buf_sz, vsi_handle;
5469         struct ice_aqc_sw_rules_elem *s_rule;
5470         struct LIST_HEAD_TYPE *rule_head;
5471         struct ice_switch_info *sw;
5472         enum ice_status status;
5473         const u8 *pkt = NULL;
5474         u32 act = 0;
5475
5476         if (!lkups_cnt)
5477                 return ICE_ERR_PARAM;
5478
5479         for (i = 0; i < lkups_cnt; i++) {
5480                 u16 j, *ptr;
5481
5482                 /* Validate match masks to make sure they match complete 16-bit
5483                  * words.
5484                  */
5485                 ptr = (u16 *)&lkups->m_u;
5486                 for (j = 0; j < sizeof(lkups->m_u) / sizeof(u16); j++)
5487                         if (ptr[j] != 0 && ptr[j] != 0xffff)
5488                                 return ICE_ERR_PARAM;
5489         }
5490
5491         if (!(rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI ||
5492               rinfo->sw_act.fltr_act == ICE_FWD_TO_Q ||
5493               rinfo->sw_act.fltr_act == ICE_DROP_PACKET))
5494                 return ICE_ERR_CFG;
5495
5496         vsi_handle = rinfo->sw_act.vsi_handle;
5497         if (!ice_is_vsi_valid(hw, vsi_handle))
5498                 return ICE_ERR_PARAM;
5499
5500         if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI)
5501                 rinfo->sw_act.fwd_id.hw_vsi_id =
5502                         ice_get_hw_vsi_num(hw, vsi_handle);
5503         if (rinfo->sw_act.flag & ICE_FLTR_TX)
5504                 rinfo->sw_act.src = ice_get_hw_vsi_num(hw, vsi_handle);
5505
5506         status = ice_add_adv_recipe(hw, lkups, lkups_cnt, rinfo, &rid);
5507         if (status)
5508                 return status;
5509         m_entry = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
5510         if (m_entry) {
5511                 /* we have to add VSI to VSI_LIST and increment vsi_count.
5512                  * Also Update VSI list so that we can change forwarding rule
5513                  * if the rule already exists, we will check if it exists with
5514                  * same vsi_id, if not then add it to the VSI list if it already
5515                  * exists if not then create a VSI list and add the existing VSI
5516                  * ID and the new VSI ID to the list
5517                  * We will add that VSI to the list
5518                  */
5519                 status = ice_adv_add_update_vsi_list(hw, m_entry,
5520                                                      &m_entry->rule_info,
5521                                                      rinfo);
5522                 if (added_entry) {
5523                         added_entry->rid = rid;
5524                         added_entry->rule_id = m_entry->rule_info.fltr_rule_id;
5525                         added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
5526                 }
5527                 return status;
5528         }
5529         ice_find_dummy_packet(lkups, lkups_cnt, rinfo->tun_type, &pkt,
5530                               &pkt_len);
5531         rule_buf_sz = ICE_SW_RULE_RX_TX_NO_HDR_SIZE + pkt_len;
5532         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, rule_buf_sz);
5533         if (!s_rule)
5534                 return ICE_ERR_NO_MEMORY;
5535         act |= ICE_SINGLE_ACT_LB_ENABLE | ICE_SINGLE_ACT_LAN_ENABLE;
5536         switch (rinfo->sw_act.fltr_act) {
5537         case ICE_FWD_TO_VSI:
5538                 act |= (rinfo->sw_act.fwd_id.hw_vsi_id <<
5539                         ICE_SINGLE_ACT_VSI_ID_S) & ICE_SINGLE_ACT_VSI_ID_M;
5540                 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_VALID_BIT;
5541                 break;
5542         case ICE_FWD_TO_Q:
5543                 act |= ICE_SINGLE_ACT_TO_Q;
5544                 act |= (rinfo->sw_act.fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
5545                        ICE_SINGLE_ACT_Q_INDEX_M;
5546                 break;
5547         case ICE_DROP_PACKET:
5548                 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
5549                        ICE_SINGLE_ACT_VALID_BIT;
5550                 break;
5551         default:
5552                 status = ICE_ERR_CFG;
5553                 goto err_ice_add_adv_rule;
5554         }
5555
5556         /* set the rule LOOKUP type based on caller specified 'RX'
5557          * instead of hardcoding it to be either LOOKUP_TX/RX
5558          *
5559          * for 'RX' set the source to be the port number
5560          * for 'TX' set the source to be the source HW VSI number (determined
5561          * by caller)
5562          */
5563         if (rinfo->rx) {
5564                 s_rule->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_RX);
5565                 s_rule->pdata.lkup_tx_rx.src =
5566                         CPU_TO_LE16(hw->port_info->lport);
5567         } else {
5568                 s_rule->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_TX);
5569                 s_rule->pdata.lkup_tx_rx.src = CPU_TO_LE16(rinfo->sw_act.src);
5570         }
5571
5572         s_rule->pdata.lkup_tx_rx.recipe_id = CPU_TO_LE16(rid);
5573         s_rule->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
5574
5575         ice_fill_adv_dummy_packet(lkups, lkups_cnt, rinfo->tun_type, s_rule,
5576                                   pkt, pkt_len);
5577
5578         status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
5579                                  rule_buf_sz, 1, ice_aqc_opc_add_sw_rules,
5580                                  NULL);
5581         if (status)
5582                 goto err_ice_add_adv_rule;
5583         adv_fltr = (struct ice_adv_fltr_mgmt_list_entry *)
5584                 ice_malloc(hw, sizeof(struct ice_adv_fltr_mgmt_list_entry));
5585         if (!adv_fltr) {
5586                 status = ICE_ERR_NO_MEMORY;
5587                 goto err_ice_add_adv_rule;
5588         }
5589
5590         adv_fltr->lkups = (struct ice_adv_lkup_elem *)
5591                 ice_memdup(hw, lkups, lkups_cnt * sizeof(*lkups),
5592                            ICE_NONDMA_TO_NONDMA);
5593         if (!adv_fltr->lkups) {
5594                 status = ICE_ERR_NO_MEMORY;
5595                 goto err_ice_add_adv_rule;
5596         }
5597
5598         adv_fltr->lkups_cnt = lkups_cnt;
5599         adv_fltr->rule_info = *rinfo;
5600         adv_fltr->rule_info.fltr_rule_id =
5601                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
5602         sw = hw->switch_info;
5603         sw->recp_list[rid].adv_rule = true;
5604         rule_head = &sw->recp_list[rid].filt_rules;
5605
5606         if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI) {
5607                 struct ice_fltr_info tmp_fltr;
5608
5609                 tmp_fltr.fltr_rule_id =
5610                         LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
5611                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI;
5612                 tmp_fltr.fwd_id.hw_vsi_id =
5613                         ice_get_hw_vsi_num(hw, vsi_handle);
5614                 tmp_fltr.vsi_handle = vsi_handle;
5615                 /* Update the previous switch rule of "forward to VSI" to
5616                  * "fwd to VSI list"
5617                  */
5618                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5619                 if (status)
5620                         goto err_ice_add_adv_rule;
5621                 adv_fltr->vsi_count = 1;
5622         }
5623
5624         /* Add rule entry to book keeping list */
5625         LIST_ADD(&adv_fltr->list_entry, rule_head);
5626         if (added_entry) {
5627                 added_entry->rid = rid;
5628                 added_entry->rule_id = adv_fltr->rule_info.fltr_rule_id;
5629                 added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
5630         }
5631 err_ice_add_adv_rule:
5632         if (status && adv_fltr) {
5633                 ice_free(hw, adv_fltr->lkups);
5634                 ice_free(hw, adv_fltr);
5635         }
5636
5637         ice_free(hw, s_rule);
5638
5639         return status;
5640 }
5641
5642 /**
5643  * ice_adv_rem_update_vsi_list
5644  * @hw: pointer to the hardware structure
5645  * @vsi_handle: VSI handle of the VSI to remove
5646  * @fm_list: filter management entry for which the VSI list management needs to
5647  *           be done
5648  */
5649 static enum ice_status
5650 ice_adv_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
5651                             struct ice_adv_fltr_mgmt_list_entry *fm_list)
5652 {
5653         struct ice_vsi_list_map_info *vsi_list_info;
5654         enum ice_sw_lkup_type lkup_type;
5655         enum ice_status status;
5656         u16 vsi_list_id;
5657
5658         if (fm_list->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST ||
5659             fm_list->vsi_count == 0)
5660                 return ICE_ERR_PARAM;
5661
5662         /* A rule with the VSI being removed does not exist */
5663         if (!ice_is_bit_set(fm_list->vsi_list_info->vsi_map, vsi_handle))
5664                 return ICE_ERR_DOES_NOT_EXIST;
5665
5666         lkup_type = ICE_SW_LKUP_LAST;
5667         vsi_list_id = fm_list->rule_info.sw_act.fwd_id.vsi_list_id;
5668         status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
5669                                           ice_aqc_opc_update_sw_rules,
5670                                           lkup_type);
5671         if (status)
5672                 return status;
5673
5674         fm_list->vsi_count--;
5675         ice_clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
5676         vsi_list_info = fm_list->vsi_list_info;
5677         if (fm_list->vsi_count == 1) {
5678                 struct ice_fltr_info tmp_fltr;
5679                 u16 rem_vsi_handle;
5680
5681                 rem_vsi_handle = ice_find_first_bit(vsi_list_info->vsi_map,
5682                                                     ICE_MAX_VSI);
5683                 if (!ice_is_vsi_valid(hw, rem_vsi_handle))
5684                         return ICE_ERR_OUT_OF_RANGE;
5685
5686                 /* Make sure VSI list is empty before removing it below */
5687                 status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
5688                                                   vsi_list_id, true,
5689                                                   ice_aqc_opc_update_sw_rules,
5690                                                   lkup_type);
5691                 if (status)
5692                         return status;
5693                 tmp_fltr.fltr_rule_id = fm_list->rule_info.fltr_rule_id;
5694                 fm_list->rule_info.sw_act.fltr_act = ICE_FWD_TO_VSI;
5695                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI;
5696                 tmp_fltr.fwd_id.hw_vsi_id =
5697                         ice_get_hw_vsi_num(hw, rem_vsi_handle);
5698                 fm_list->rule_info.sw_act.fwd_id.hw_vsi_id =
5699                         ice_get_hw_vsi_num(hw, rem_vsi_handle);
5700
5701                 /* Update the previous switch rule of "MAC forward to VSI" to
5702                  * "MAC fwd to VSI list"
5703                  */
5704                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5705                 if (status) {
5706                         ice_debug(hw, ICE_DBG_SW,
5707                                   "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
5708                                   tmp_fltr.fwd_id.hw_vsi_id, status);
5709                         return status;
5710                 }
5711         }
5712
5713         if (fm_list->vsi_count == 1) {
5714                 /* Remove the VSI list since it is no longer used */
5715                 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
5716                 if (status) {
5717                         ice_debug(hw, ICE_DBG_SW,
5718                                   "Failed to remove VSI list %d, error %d\n",
5719                                   vsi_list_id, status);
5720                         return status;
5721                 }
5722
5723                 LIST_DEL(&vsi_list_info->list_entry);
5724                 ice_free(hw, vsi_list_info);
5725                 fm_list->vsi_list_info = NULL;
5726         }
5727
5728         return status;
5729 }
5730
5731 /**
5732  * ice_rem_adv_rule - removes existing advanced switch rule
5733  * @hw: pointer to the hardware structure
5734  * @lkups: information on the words that needs to be looked up. All words
5735  *         together makes one recipe
5736  * @lkups_cnt: num of entries in the lkups array
5737  * @rinfo: Its the pointer to the rule information for the rule
5738  *
5739  * This function can be used to remove 1 rule at a time. The lkups is
5740  * used to describe all the words that forms the "lookup" portion of the
5741  * rule. These words can span multiple protocols. Callers to this function
5742  * need to pass in a list of protocol headers with lookup information along
5743  * and mask that determines which words are valid from the given protocol
5744  * header. rinfo describes other information related to this rule such as
5745  * forwarding IDs, priority of this rule, etc.
5746  */
5747 enum ice_status
5748 ice_rem_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5749                  u16 lkups_cnt, struct ice_adv_rule_info *rinfo)
5750 {
5751         struct ice_adv_fltr_mgmt_list_entry *list_elem;
5752         struct ice_prot_lkup_ext lkup_exts;
5753         u16 rule_buf_sz, pkt_len, i, rid;
5754         enum ice_status status = ICE_SUCCESS;
5755         bool remove_rule = false;
5756         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
5757         const u8 *pkt = NULL;
5758         u16 vsi_handle;
5759
5760         ice_memset(&lkup_exts, 0, sizeof(lkup_exts), ICE_NONDMA_MEM);
5761         for (i = 0; i < lkups_cnt; i++) {
5762                 u16 count;
5763
5764                 if (lkups[i].type >= ICE_PROTOCOL_LAST)
5765                         return ICE_ERR_CFG;
5766
5767                 count = ice_fill_valid_words(&lkups[i], &lkup_exts);
5768                 if (!count)
5769                         return ICE_ERR_CFG;
5770         }
5771         rid = ice_find_recp(hw, &lkup_exts);
5772         /* If did not find a recipe that match the existing criteria */
5773         if (rid == ICE_MAX_NUM_RECIPES)
5774                 return ICE_ERR_PARAM;
5775
5776         rule_lock = &hw->switch_info->recp_list[rid].filt_rule_lock;
5777         list_elem = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
5778         /* the rule is already removed */
5779         if (!list_elem)
5780                 return ICE_SUCCESS;
5781         ice_acquire_lock(rule_lock);
5782         if (list_elem->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST) {
5783                 remove_rule = true;
5784         } else if (list_elem->vsi_count > 1) {
5785                 list_elem->vsi_list_info->ref_cnt--;
5786                 remove_rule = false;
5787                 vsi_handle = rinfo->sw_act.vsi_handle;
5788                 status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
5789         } else {
5790                 vsi_handle = rinfo->sw_act.vsi_handle;
5791                 status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
5792                 if (status) {
5793                         ice_release_lock(rule_lock);
5794                         return status;
5795                 }
5796                 if (list_elem->vsi_count == 0)
5797                         remove_rule = true;
5798         }
5799         ice_release_lock(rule_lock);
5800         if (remove_rule) {
5801                 struct ice_aqc_sw_rules_elem *s_rule;
5802
5803                 ice_find_dummy_packet(lkups, lkups_cnt, rinfo->tun_type, &pkt,
5804                                       &pkt_len);
5805                 rule_buf_sz = ICE_SW_RULE_RX_TX_NO_HDR_SIZE + pkt_len;
5806                 s_rule =
5807                         (struct ice_aqc_sw_rules_elem *)ice_malloc(hw,
5808                                                                    rule_buf_sz);
5809                 if (!s_rule)
5810                         return ICE_ERR_NO_MEMORY;
5811                 s_rule->pdata.lkup_tx_rx.act = 0;
5812                 s_rule->pdata.lkup_tx_rx.index =
5813                         CPU_TO_LE16(list_elem->rule_info.fltr_rule_id);
5814                 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
5815                 status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
5816                                          rule_buf_sz, 1,
5817                                          ice_aqc_opc_remove_sw_rules, NULL);
5818                 if (status == ICE_SUCCESS) {
5819                         ice_acquire_lock(rule_lock);
5820                         LIST_DEL(&list_elem->list_entry);
5821                         ice_free(hw, list_elem->lkups);
5822                         ice_free(hw, list_elem);
5823                         ice_release_lock(rule_lock);
5824                 }
5825                 ice_free(hw, s_rule);
5826         }
5827         return status;
5828 }
5829
5830 /**
5831  * ice_rem_adv_rule_by_id - removes existing advanced switch rule by ID
5832  * @hw: pointer to the hardware structure
5833  * @remove_entry: data struct which holds rule_id, VSI handle and recipe ID
5834  *
5835  * This function is used to remove 1 rule at a time. The removal is based on
5836  * the remove_entry parameter. This function will remove rule for a given
5837  * vsi_handle with a given rule_id which is passed as parameter in remove_entry
5838  */
5839 enum ice_status
5840 ice_rem_adv_rule_by_id(struct ice_hw *hw,
5841                        struct ice_rule_query_data *remove_entry)
5842 {
5843         struct ice_adv_fltr_mgmt_list_entry *list_itr;
5844         struct LIST_HEAD_TYPE *list_head;
5845         struct ice_adv_rule_info rinfo;
5846         struct ice_switch_info *sw;
5847
5848         sw = hw->switch_info;
5849         if (!sw->recp_list[remove_entry->rid].recp_created)
5850                 return ICE_ERR_PARAM;
5851         list_head = &sw->recp_list[remove_entry->rid].filt_rules;
5852         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_adv_fltr_mgmt_list_entry,
5853                             list_entry) {
5854                 if (list_itr->rule_info.fltr_rule_id ==
5855                     remove_entry->rule_id) {
5856                         rinfo = list_itr->rule_info;
5857                         rinfo.sw_act.vsi_handle = remove_entry->vsi_handle;
5858                         return ice_rem_adv_rule(hw, list_itr->lkups,
5859                                                 list_itr->lkups_cnt, &rinfo);
5860                 }
5861         }
5862         return ICE_ERR_PARAM;
5863 }
5864
5865 /**
5866  * ice_rem_adv_for_vsi - removes existing advanced switch rules for a
5867  *                       given VSI handle
5868  * @hw: pointer to the hardware structure
5869  * @vsi_handle: VSI handle for which we are supposed to remove all the rules.
5870  *
5871  * This function is used to remove all the rules for a given VSI and as soon
5872  * as removing a rule fails, it will return immediately with the error code,
5873  * else it will return ICE_SUCCESS
5874  */
5875 enum ice_status
5876 ice_rem_adv_rule_for_vsi(struct ice_hw *hw, u16 vsi_handle)
5877 {
5878         struct ice_adv_fltr_mgmt_list_entry *list_itr;
5879         struct ice_vsi_list_map_info *map_info;
5880         struct LIST_HEAD_TYPE *list_head;
5881         struct ice_adv_rule_info rinfo;
5882         struct ice_switch_info *sw;
5883         enum ice_status status;
5884         u16 vsi_list_id = 0;
5885         u8 rid;
5886
5887         sw = hw->switch_info;
5888         for (rid = 0; rid < ICE_MAX_NUM_RECIPES; rid++) {
5889                 if (!sw->recp_list[rid].recp_created)
5890                         continue;
5891                 if (!sw->recp_list[rid].adv_rule)
5892                         continue;
5893                 list_head = &sw->recp_list[rid].filt_rules;
5894                 map_info = NULL;
5895                 LIST_FOR_EACH_ENTRY(list_itr, list_head,
5896                                     ice_adv_fltr_mgmt_list_entry, list_entry) {
5897                         map_info = ice_find_vsi_list_entry(hw, rid, vsi_handle,
5898                                                            &vsi_list_id);
5899                         if (!map_info)
5900                                 continue;
5901                         rinfo = list_itr->rule_info;
5902                         rinfo.sw_act.vsi_handle = vsi_handle;
5903                         status = ice_rem_adv_rule(hw, list_itr->lkups,
5904                                                   list_itr->lkups_cnt, &rinfo);
5905                         if (status)
5906                                 return status;
5907                         map_info = NULL;
5908                 }
5909         }
5910         return ICE_SUCCESS;
5911 }
5912
5913 /**
5914  * ice_replay_fltr - Replay all the filters stored by a specific list head
5915  * @hw: pointer to the hardware structure
5916  * @list_head: list for which filters needs to be replayed
5917  * @recp_id: Recipe ID for which rules need to be replayed
5918  */
5919 static enum ice_status
5920 ice_replay_fltr(struct ice_hw *hw, u8 recp_id, struct LIST_HEAD_TYPE *list_head)
5921 {
5922         struct ice_fltr_mgmt_list_entry *itr;
5923         struct LIST_HEAD_TYPE l_head;
5924         enum ice_status status = ICE_SUCCESS;
5925
5926         if (LIST_EMPTY(list_head))
5927                 return status;
5928
5929         /* Move entries from the given list_head to a temporary l_head so that
5930          * they can be replayed. Otherwise when trying to re-add the same
5931          * filter, the function will return already exists
5932          */
5933         LIST_REPLACE_INIT(list_head, &l_head);
5934
5935         /* Mark the given list_head empty by reinitializing it so filters
5936          * could be added again by *handler
5937          */
5938         LIST_FOR_EACH_ENTRY(itr, &l_head, ice_fltr_mgmt_list_entry,
5939                             list_entry) {
5940                 struct ice_fltr_list_entry f_entry;
5941
5942                 f_entry.fltr_info = itr->fltr_info;
5943                 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN) {
5944                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
5945                         if (status != ICE_SUCCESS)
5946                                 goto end;
5947                         continue;
5948                 }
5949
5950                 /* Add a filter per VSI separately */
5951                 while (1) {
5952                         u16 vsi_handle;
5953
5954                         vsi_handle =
5955                                 ice_find_first_bit(itr->vsi_list_info->vsi_map,
5956                                                    ICE_MAX_VSI);
5957                         if (!ice_is_vsi_valid(hw, vsi_handle))
5958                                 break;
5959
5960                         ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
5961                         f_entry.fltr_info.vsi_handle = vsi_handle;
5962                         f_entry.fltr_info.fwd_id.hw_vsi_id =
5963                                 ice_get_hw_vsi_num(hw, vsi_handle);
5964                         f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
5965                         if (recp_id == ICE_SW_LKUP_VLAN)
5966                                 status = ice_add_vlan_internal(hw, &f_entry);
5967                         else
5968                                 status = ice_add_rule_internal(hw, recp_id,
5969                                                                &f_entry);
5970                         if (status != ICE_SUCCESS)
5971                                 goto end;
5972                 }
5973         }
5974 end:
5975         /* Clear the filter management list */
5976         ice_rem_sw_rule_info(hw, &l_head);
5977         return status;
5978 }
5979
5980 /**
5981  * ice_replay_all_fltr - replay all filters stored in bookkeeping lists
5982  * @hw: pointer to the hardware structure
5983  *
5984  * NOTE: This function does not clean up partially added filters on error.
5985  * It is up to caller of the function to issue a reset or fail early.
5986  */
5987 enum ice_status ice_replay_all_fltr(struct ice_hw *hw)
5988 {
5989         struct ice_switch_info *sw = hw->switch_info;
5990         enum ice_status status = ICE_SUCCESS;
5991         u8 i;
5992
5993         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
5994                 struct LIST_HEAD_TYPE *head = &sw->recp_list[i].filt_rules;
5995
5996                 status = ice_replay_fltr(hw, i, head);
5997                 if (status != ICE_SUCCESS)
5998                         return status;
5999         }
6000         return status;
6001 }
6002
6003 /**
6004  * ice_replay_vsi_fltr - Replay filters for requested VSI
6005  * @hw: pointer to the hardware structure
6006  * @vsi_handle: driver VSI handle
6007  * @recp_id: Recipe ID for which rules need to be replayed
6008  * @list_head: list for which filters need to be replayed
6009  *
6010  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
6011  * It is required to pass valid VSI handle.
6012  */
6013 static enum ice_status
6014 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
6015                     struct LIST_HEAD_TYPE *list_head)
6016 {
6017         struct ice_fltr_mgmt_list_entry *itr;
6018         enum ice_status status = ICE_SUCCESS;
6019         u16 hw_vsi_id;
6020
6021         if (LIST_EMPTY(list_head))
6022                 return status;
6023         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
6024
6025         LIST_FOR_EACH_ENTRY(itr, list_head, ice_fltr_mgmt_list_entry,
6026                             list_entry) {
6027                 struct ice_fltr_list_entry f_entry;
6028
6029                 f_entry.fltr_info = itr->fltr_info;
6030                 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
6031                     itr->fltr_info.vsi_handle == vsi_handle) {
6032                         /* update the src in case it is VSI num */
6033                         if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6034                                 f_entry.fltr_info.src = hw_vsi_id;
6035                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
6036                         if (status != ICE_SUCCESS)
6037                                 goto end;
6038                         continue;
6039                 }
6040                 if (!itr->vsi_list_info ||
6041                     !ice_is_bit_set(itr->vsi_list_info->vsi_map, vsi_handle))
6042                         continue;
6043                 /* Clearing it so that the logic can add it back */
6044                 ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
6045                 f_entry.fltr_info.vsi_handle = vsi_handle;
6046                 f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
6047                 /* update the src in case it is VSI num */
6048                 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6049                         f_entry.fltr_info.src = hw_vsi_id;
6050                 if (recp_id == ICE_SW_LKUP_VLAN)
6051                         status = ice_add_vlan_internal(hw, &f_entry);
6052                 else
6053                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
6054                 if (status != ICE_SUCCESS)
6055                         goto end;
6056         }
6057 end:
6058         return status;
6059 }
6060
6061 /**
6062  * ice_replay_vsi_adv_rule - Replay advanced rule for requested VSI
6063  * @hw: pointer to the hardware structure
6064  * @vsi_handle: driver VSI handle
6065  * @list_head: list for which filters need to be replayed
6066  *
6067  * Replay the advanced rule for the given VSI.
6068  */
6069 static enum ice_status
6070 ice_replay_vsi_adv_rule(struct ice_hw *hw, u16 vsi_handle,
6071                         struct LIST_HEAD_TYPE *list_head)
6072 {
6073         struct ice_rule_query_data added_entry = { 0 };
6074         struct ice_adv_fltr_mgmt_list_entry *adv_fltr;
6075         enum ice_status status = ICE_SUCCESS;
6076
6077         if (LIST_EMPTY(list_head))
6078                 return status;
6079         LIST_FOR_EACH_ENTRY(adv_fltr, list_head, ice_adv_fltr_mgmt_list_entry,
6080                             list_entry) {
6081                 struct ice_adv_rule_info *rinfo = &adv_fltr->rule_info;
6082                 u16 lk_cnt = adv_fltr->lkups_cnt;
6083
6084                 if (vsi_handle != rinfo->sw_act.vsi_handle)
6085                         continue;
6086                 status = ice_add_adv_rule(hw, adv_fltr->lkups, lk_cnt, rinfo,
6087                                           &added_entry);
6088                 if (status)
6089                         break;
6090         }
6091         return status;
6092 }
6093
6094 /**
6095  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
6096  * @hw: pointer to the hardware structure
6097  * @vsi_handle: driver VSI handle
6098  *
6099  * Replays filters for requested VSI via vsi_handle.
6100  */
6101 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
6102 {
6103         struct ice_switch_info *sw = hw->switch_info;
6104         enum ice_status status;
6105         u8 i;
6106
6107         /* Update the recipes that were created */
6108         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6109                 struct LIST_HEAD_TYPE *head;
6110
6111                 head = &sw->recp_list[i].filt_replay_rules;
6112                 if (!sw->recp_list[i].adv_rule)
6113                         status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
6114                 else
6115                         status = ice_replay_vsi_adv_rule(hw, vsi_handle, head);
6116                 if (status != ICE_SUCCESS)
6117                         return status;
6118         }
6119
6120         return ICE_SUCCESS;
6121 }
6122
6123 /**
6124  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
6125  * @hw: pointer to the HW struct
6126  *
6127  * Deletes the filter replay rules.
6128  */
6129 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
6130 {
6131         struct ice_switch_info *sw = hw->switch_info;
6132         u8 i;
6133
6134         if (!sw)
6135                 return;
6136
6137         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6138                 if (!LIST_EMPTY(&sw->recp_list[i].filt_replay_rules)) {
6139                         struct LIST_HEAD_TYPE *l_head;
6140
6141                         l_head = &sw->recp_list[i].filt_replay_rules;
6142                         if (!sw->recp_list[i].adv_rule)
6143                                 ice_rem_sw_rule_info(hw, l_head);
6144                         else
6145                                 ice_rem_adv_rule_info(hw, l_head);
6146                 }
6147         }
6148 }