net/ice/base: eliminate semantic parser warnings
[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 = (_FORCE_ __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  * ice_fill_sw_rule - Helper function to fill switch rule structure
1465  * @hw: pointer to the hardware structure
1466  * @f_info: entry containing packet forwarding information
1467  * @s_rule: switch rule structure to be filled in based on mac_entry
1468  * @opc: switch rules population command type - pass in the command opcode
1469  */
1470 static void
1471 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
1472                  struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
1473 {
1474         u16 vlan_id = ICE_MAX_VLAN_ID + 1;
1475         void *daddr = NULL;
1476         u16 eth_hdr_sz;
1477         u8 *eth_hdr;
1478         u32 act = 0;
1479         __be16 *off;
1480         u8 q_rgn;
1481
1482         if (opc == ice_aqc_opc_remove_sw_rules) {
1483                 s_rule->pdata.lkup_tx_rx.act = 0;
1484                 s_rule->pdata.lkup_tx_rx.index =
1485                         CPU_TO_LE16(f_info->fltr_rule_id);
1486                 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
1487                 return;
1488         }
1489
1490         eth_hdr_sz = sizeof(dummy_eth_header);
1491         eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
1492
1493         /* initialize the ether header with a dummy header */
1494         ice_memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz, ICE_NONDMA_TO_NONDMA);
1495         ice_fill_sw_info(hw, f_info);
1496
1497         switch (f_info->fltr_act) {
1498         case ICE_FWD_TO_VSI:
1499                 act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
1500                         ICE_SINGLE_ACT_VSI_ID_M;
1501                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1502                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1503                                 ICE_SINGLE_ACT_VALID_BIT;
1504                 break;
1505         case ICE_FWD_TO_VSI_LIST:
1506                 act |= ICE_SINGLE_ACT_VSI_LIST;
1507                 act |= (f_info->fwd_id.vsi_list_id <<
1508                         ICE_SINGLE_ACT_VSI_LIST_ID_S) &
1509                         ICE_SINGLE_ACT_VSI_LIST_ID_M;
1510                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1511                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1512                                 ICE_SINGLE_ACT_VALID_BIT;
1513                 break;
1514         case ICE_FWD_TO_Q:
1515                 act |= ICE_SINGLE_ACT_TO_Q;
1516                 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1517                         ICE_SINGLE_ACT_Q_INDEX_M;
1518                 break;
1519         case ICE_DROP_PACKET:
1520                 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
1521                         ICE_SINGLE_ACT_VALID_BIT;
1522                 break;
1523         case ICE_FWD_TO_QGRP:
1524                 q_rgn = f_info->qgrp_size > 0 ?
1525                         (u8)ice_ilog2(f_info->qgrp_size) : 0;
1526                 act |= ICE_SINGLE_ACT_TO_Q;
1527                 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1528                         ICE_SINGLE_ACT_Q_INDEX_M;
1529                 act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
1530                         ICE_SINGLE_ACT_Q_REGION_M;
1531                 break;
1532         default:
1533                 return;
1534         }
1535
1536         if (f_info->lb_en)
1537                 act |= ICE_SINGLE_ACT_LB_ENABLE;
1538         if (f_info->lan_en)
1539                 act |= ICE_SINGLE_ACT_LAN_ENABLE;
1540
1541         switch (f_info->lkup_type) {
1542         case ICE_SW_LKUP_MAC:
1543                 daddr = f_info->l_data.mac.mac_addr;
1544                 break;
1545         case ICE_SW_LKUP_VLAN:
1546                 vlan_id = f_info->l_data.vlan.vlan_id;
1547                 if (f_info->fltr_act == ICE_FWD_TO_VSI ||
1548                     f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
1549                         act |= ICE_SINGLE_ACT_PRUNE;
1550                         act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
1551                 }
1552                 break;
1553         case ICE_SW_LKUP_ETHERTYPE_MAC:
1554                 daddr = f_info->l_data.ethertype_mac.mac_addr;
1555                 /* fall-through */
1556         case ICE_SW_LKUP_ETHERTYPE:
1557                 off = (_FORCE_ __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
1558                 *off = CPU_TO_BE16(f_info->l_data.ethertype_mac.ethertype);
1559                 break;
1560         case ICE_SW_LKUP_MAC_VLAN:
1561                 daddr = f_info->l_data.mac_vlan.mac_addr;
1562                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
1563                 break;
1564         case ICE_SW_LKUP_PROMISC_VLAN:
1565                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
1566                 /* fall-through */
1567         case ICE_SW_LKUP_PROMISC:
1568                 daddr = f_info->l_data.mac_vlan.mac_addr;
1569                 break;
1570         default:
1571                 break;
1572         }
1573
1574         s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
1575                 CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_RX) :
1576                 CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_TX);
1577
1578         /* Recipe set depending on lookup type */
1579         s_rule->pdata.lkup_tx_rx.recipe_id = CPU_TO_LE16(f_info->lkup_type);
1580         s_rule->pdata.lkup_tx_rx.src = CPU_TO_LE16(f_info->src);
1581         s_rule->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1582
1583         if (daddr)
1584                 ice_memcpy(eth_hdr + ICE_ETH_DA_OFFSET, daddr, ETH_ALEN,
1585                            ICE_NONDMA_TO_NONDMA);
1586
1587         if (!(vlan_id > ICE_MAX_VLAN_ID)) {
1588                 off = (_FORCE_ __be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
1589                 *off = CPU_TO_BE16(vlan_id);
1590         }
1591
1592         /* Create the switch rule with the final dummy Ethernet header */
1593         if (opc != ice_aqc_opc_update_sw_rules)
1594                 s_rule->pdata.lkup_tx_rx.hdr_len = CPU_TO_LE16(eth_hdr_sz);
1595 }
1596
1597 /**
1598  * ice_add_marker_act
1599  * @hw: pointer to the hardware structure
1600  * @m_ent: the management entry for which sw marker needs to be added
1601  * @sw_marker: sw marker to tag the Rx descriptor with
1602  * @l_id: large action resource ID
1603  *
1604  * Create a large action to hold software marker and update the switch rule
1605  * entry pointed by m_ent with newly created large action
1606  */
1607 static enum ice_status
1608 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1609                    u16 sw_marker, u16 l_id)
1610 {
1611         struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
1612         /* For software marker we need 3 large actions
1613          * 1. FWD action: FWD TO VSI or VSI LIST
1614          * 2. GENERIC VALUE action to hold the profile ID
1615          * 3. GENERIC VALUE action to hold the software marker ID
1616          */
1617         const u16 num_lg_acts = 3;
1618         enum ice_status status;
1619         u16 lg_act_size;
1620         u16 rules_size;
1621         u32 act;
1622         u16 id;
1623
1624         if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1625                 return ICE_ERR_PARAM;
1626
1627         /* Create two back-to-back switch rules and submit them to the HW using
1628          * one memory buffer:
1629          *    1. Large Action
1630          *    2. Look up Tx Rx
1631          */
1632         lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
1633         rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1634         lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, rules_size);
1635         if (!lg_act)
1636                 return ICE_ERR_NO_MEMORY;
1637
1638         rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
1639
1640         /* Fill in the first switch rule i.e. large action */
1641         lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1642         lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1643         lg_act->pdata.lg_act.size = CPU_TO_LE16(num_lg_acts);
1644
1645         /* First action VSI forwarding or VSI list forwarding depending on how
1646          * many VSIs
1647          */
1648         id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
1649                 m_ent->fltr_info.fwd_id.hw_vsi_id;
1650
1651         act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1652         act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
1653                 ICE_LG_ACT_VSI_LIST_ID_M;
1654         if (m_ent->vsi_count > 1)
1655                 act |= ICE_LG_ACT_VSI_LIST;
1656         lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1657
1658         /* Second action descriptor type */
1659         act = ICE_LG_ACT_GENERIC;
1660
1661         act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
1662         lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1663
1664         act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
1665                ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
1666
1667         /* Third action Marker value */
1668         act |= ICE_LG_ACT_GENERIC;
1669         act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
1670                 ICE_LG_ACT_GENERIC_VALUE_M;
1671
1672         lg_act->pdata.lg_act.act[2] = CPU_TO_LE32(act);
1673
1674         /* call the fill switch rule to fill the lookup Tx Rx structure */
1675         ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1676                          ice_aqc_opc_update_sw_rules);
1677
1678         /* Update the action to point to the large action ID */
1679         rx_tx->pdata.lkup_tx_rx.act =
1680                 CPU_TO_LE32(ICE_SINGLE_ACT_PTR |
1681                             ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
1682                              ICE_SINGLE_ACT_PTR_VAL_M));
1683
1684         /* Use the filter rule ID of the previously created rule with single
1685          * act. Once the update happens, hardware will treat this as large
1686          * action
1687          */
1688         rx_tx->pdata.lkup_tx_rx.index =
1689                 CPU_TO_LE16(m_ent->fltr_info.fltr_rule_id);
1690
1691         status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1692                                  ice_aqc_opc_update_sw_rules, NULL);
1693         if (!status) {
1694                 m_ent->lg_act_idx = l_id;
1695                 m_ent->sw_marker_id = sw_marker;
1696         }
1697
1698         ice_free(hw, lg_act);
1699         return status;
1700 }
1701
1702 /**
1703  * ice_add_counter_act - add/update filter rule with counter action
1704  * @hw: pointer to the hardware structure
1705  * @m_ent: the management entry for which counter needs to be added
1706  * @counter_id: VLAN counter ID returned as part of allocate resource
1707  * @l_id: large action resource ID
1708  */
1709 static enum ice_status
1710 ice_add_counter_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1711                     u16 counter_id, u16 l_id)
1712 {
1713         struct ice_aqc_sw_rules_elem *lg_act;
1714         struct ice_aqc_sw_rules_elem *rx_tx;
1715         enum ice_status status;
1716         /* 2 actions will be added while adding a large action counter */
1717         const int num_acts = 2;
1718         u16 lg_act_size;
1719         u16 rules_size;
1720         u16 f_rule_id;
1721         u32 act;
1722         u16 id;
1723
1724         if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1725                 return ICE_ERR_PARAM;
1726
1727         /* Create two back-to-back switch rules and submit them to the HW using
1728          * one memory buffer:
1729          * 1. Large Action
1730          * 2. Look up Tx Rx
1731          */
1732         lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_acts);
1733         rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1734         lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw,
1735                                                                  rules_size);
1736         if (!lg_act)
1737                 return ICE_ERR_NO_MEMORY;
1738
1739         rx_tx = (struct ice_aqc_sw_rules_elem *)
1740                 ((u8 *)lg_act + lg_act_size);
1741
1742         /* Fill in the first switch rule i.e. large action */
1743         lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1744         lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1745         lg_act->pdata.lg_act.size = CPU_TO_LE16(num_acts);
1746
1747         /* First action VSI forwarding or VSI list forwarding depending on how
1748          * many VSIs
1749          */
1750         id = (m_ent->vsi_count > 1) ?  m_ent->fltr_info.fwd_id.vsi_list_id :
1751                 m_ent->fltr_info.fwd_id.hw_vsi_id;
1752
1753         act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1754         act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
1755                 ICE_LG_ACT_VSI_LIST_ID_M;
1756         if (m_ent->vsi_count > 1)
1757                 act |= ICE_LG_ACT_VSI_LIST;
1758         lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1759
1760         /* Second action counter ID */
1761         act = ICE_LG_ACT_STAT_COUNT;
1762         act |= (counter_id << ICE_LG_ACT_STAT_COUNT_S) &
1763                 ICE_LG_ACT_STAT_COUNT_M;
1764         lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1765
1766         /* call the fill switch rule to fill the lookup Tx Rx structure */
1767         ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1768                          ice_aqc_opc_update_sw_rules);
1769
1770         act = ICE_SINGLE_ACT_PTR;
1771         act |= (l_id << ICE_SINGLE_ACT_PTR_VAL_S) & ICE_SINGLE_ACT_PTR_VAL_M;
1772         rx_tx->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1773
1774         /* Use the filter rule ID of the previously created rule with single
1775          * act. Once the update happens, hardware will treat this as large
1776          * action
1777          */
1778         f_rule_id = m_ent->fltr_info.fltr_rule_id;
1779         rx_tx->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_rule_id);
1780
1781         status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1782                                  ice_aqc_opc_update_sw_rules, NULL);
1783         if (!status) {
1784                 m_ent->lg_act_idx = l_id;
1785                 m_ent->counter_index = counter_id;
1786         }
1787
1788         ice_free(hw, lg_act);
1789         return status;
1790 }
1791
1792 /**
1793  * ice_create_vsi_list_map
1794  * @hw: pointer to the hardware structure
1795  * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
1796  * @num_vsi: number of VSI handles in the array
1797  * @vsi_list_id: VSI list ID generated as part of allocate resource
1798  *
1799  * Helper function to create a new entry of VSI list ID to VSI mapping
1800  * using the given VSI list ID
1801  */
1802 static struct ice_vsi_list_map_info *
1803 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1804                         u16 vsi_list_id)
1805 {
1806         struct ice_switch_info *sw = hw->switch_info;
1807         struct ice_vsi_list_map_info *v_map;
1808         int i;
1809
1810         v_map = (struct ice_vsi_list_map_info *)ice_calloc(hw, 1,
1811                 sizeof(*v_map));
1812         if (!v_map)
1813                 return NULL;
1814
1815         v_map->vsi_list_id = vsi_list_id;
1816         v_map->ref_cnt = 1;
1817         for (i = 0; i < num_vsi; i++)
1818                 ice_set_bit(vsi_handle_arr[i], v_map->vsi_map);
1819
1820         LIST_ADD(&v_map->list_entry, &sw->vsi_list_map_head);
1821         return v_map;
1822 }
1823
1824 /**
1825  * ice_update_vsi_list_rule
1826  * @hw: pointer to the hardware structure
1827  * @vsi_handle_arr: array of VSI handles to form a VSI list
1828  * @num_vsi: number of VSI handles in the array
1829  * @vsi_list_id: VSI list ID generated as part of allocate resource
1830  * @remove: Boolean value to indicate if this is a remove action
1831  * @opc: switch rules population command type - pass in the command opcode
1832  * @lkup_type: lookup type of the filter
1833  *
1834  * Call AQ command to add a new switch rule or update existing switch rule
1835  * using the given VSI list ID
1836  */
1837 static enum ice_status
1838 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1839                          u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
1840                          enum ice_sw_lkup_type lkup_type)
1841 {
1842         struct ice_aqc_sw_rules_elem *s_rule;
1843         enum ice_status status;
1844         u16 s_rule_size;
1845         u16 type;
1846         int i;
1847
1848         if (!num_vsi)
1849                 return ICE_ERR_PARAM;
1850
1851         if (lkup_type == ICE_SW_LKUP_MAC ||
1852             lkup_type == ICE_SW_LKUP_MAC_VLAN ||
1853             lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1854             lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1855             lkup_type == ICE_SW_LKUP_PROMISC ||
1856             lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1857             lkup_type == ICE_SW_LKUP_LAST)
1858                 type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
1859                                 ICE_AQC_SW_RULES_T_VSI_LIST_SET;
1860         else if (lkup_type == ICE_SW_LKUP_VLAN)
1861                 type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
1862                                 ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
1863         else
1864                 return ICE_ERR_PARAM;
1865
1866         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
1867         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
1868         if (!s_rule)
1869                 return ICE_ERR_NO_MEMORY;
1870         for (i = 0; i < num_vsi; i++) {
1871                 if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
1872                         status = ICE_ERR_PARAM;
1873                         goto exit;
1874                 }
1875                 /* AQ call requires hw_vsi_id(s) */
1876                 s_rule->pdata.vsi_list.vsi[i] =
1877                         CPU_TO_LE16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
1878         }
1879
1880         s_rule->type = CPU_TO_LE16(type);
1881         s_rule->pdata.vsi_list.number_vsi = CPU_TO_LE16(num_vsi);
1882         s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
1883
1884         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
1885
1886 exit:
1887         ice_free(hw, s_rule);
1888         return status;
1889 }
1890
1891 /**
1892  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
1893  * @hw: pointer to the HW struct
1894  * @vsi_handle_arr: array of VSI handles to form a VSI list
1895  * @num_vsi: number of VSI handles in the array
1896  * @vsi_list_id: stores the ID of the VSI list to be created
1897  * @lkup_type: switch rule filter's lookup type
1898  */
1899 static enum ice_status
1900 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1901                          u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1902 {
1903         enum ice_status status;
1904
1905         status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1906                                             ice_aqc_opc_alloc_res);
1907         if (status)
1908                 return status;
1909
1910         /* Update the newly created VSI list to include the specified VSIs */
1911         return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1912                                         *vsi_list_id, false,
1913                                         ice_aqc_opc_add_sw_rules, lkup_type);
1914 }
1915
1916 /**
1917  * ice_create_pkt_fwd_rule
1918  * @hw: pointer to the hardware structure
1919  * @f_entry: entry containing packet forwarding information
1920  *
1921  * Create switch rule with given filter information and add an entry
1922  * to the corresponding filter management list to track this switch rule
1923  * and VSI mapping
1924  */
1925 static enum ice_status
1926 ice_create_pkt_fwd_rule(struct ice_hw *hw,
1927                         struct ice_fltr_list_entry *f_entry)
1928 {
1929         struct ice_fltr_mgmt_list_entry *fm_entry;
1930         struct ice_aqc_sw_rules_elem *s_rule;
1931         enum ice_sw_lkup_type l_type;
1932         struct ice_sw_recipe *recp;
1933         enum ice_status status;
1934
1935         s_rule = (struct ice_aqc_sw_rules_elem *)
1936                 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1937         if (!s_rule)
1938                 return ICE_ERR_NO_MEMORY;
1939         fm_entry = (struct ice_fltr_mgmt_list_entry *)
1940                    ice_malloc(hw, sizeof(*fm_entry));
1941         if (!fm_entry) {
1942                 status = ICE_ERR_NO_MEMORY;
1943                 goto ice_create_pkt_fwd_rule_exit;
1944         }
1945
1946         fm_entry->fltr_info = f_entry->fltr_info;
1947
1948         /* Initialize all the fields for the management entry */
1949         fm_entry->vsi_count = 1;
1950         fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1951         fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1952         fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1953
1954         ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1955                          ice_aqc_opc_add_sw_rules);
1956
1957         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1958                                  ice_aqc_opc_add_sw_rules, NULL);
1959         if (status) {
1960                 ice_free(hw, fm_entry);
1961                 goto ice_create_pkt_fwd_rule_exit;
1962         }
1963
1964         f_entry->fltr_info.fltr_rule_id =
1965                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1966         fm_entry->fltr_info.fltr_rule_id =
1967                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1968
1969         /* The book keeping entries will get removed when base driver
1970          * calls remove filter AQ command
1971          */
1972         l_type = fm_entry->fltr_info.lkup_type;
1973         recp = &hw->switch_info->recp_list[l_type];
1974         LIST_ADD(&fm_entry->list_entry, &recp->filt_rules);
1975
1976 ice_create_pkt_fwd_rule_exit:
1977         ice_free(hw, s_rule);
1978         return status;
1979 }
1980
1981 /**
1982  * ice_update_pkt_fwd_rule
1983  * @hw: pointer to the hardware structure
1984  * @f_info: filter information for switch rule
1985  *
1986  * Call AQ command to update a previously created switch rule with a
1987  * VSI list ID
1988  */
1989 static enum ice_status
1990 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1991 {
1992         struct ice_aqc_sw_rules_elem *s_rule;
1993         enum ice_status status;
1994
1995         s_rule = (struct ice_aqc_sw_rules_elem *)
1996                 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1997         if (!s_rule)
1998                 return ICE_ERR_NO_MEMORY;
1999
2000         ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
2001
2002         s_rule->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_info->fltr_rule_id);
2003
2004         /* Update switch rule with new rule set to forward VSI list */
2005         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
2006                                  ice_aqc_opc_update_sw_rules, NULL);
2007
2008         ice_free(hw, s_rule);
2009         return status;
2010 }
2011
2012 /**
2013  * ice_update_sw_rule_bridge_mode
2014  * @hw: pointer to the HW struct
2015  *
2016  * Updates unicast switch filter rules based on VEB/VEPA mode
2017  */
2018 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
2019 {
2020         struct ice_switch_info *sw = hw->switch_info;
2021         struct ice_fltr_mgmt_list_entry *fm_entry;
2022         enum ice_status status = ICE_SUCCESS;
2023         struct LIST_HEAD_TYPE *rule_head;
2024         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2025
2026         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2027         rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2028
2029         ice_acquire_lock(rule_lock);
2030         LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry,
2031                             list_entry) {
2032                 struct ice_fltr_info *fi = &fm_entry->fltr_info;
2033                 u8 *addr = fi->l_data.mac.mac_addr;
2034
2035                 /* Update unicast Tx rules to reflect the selected
2036                  * VEB/VEPA mode
2037                  */
2038                 if ((fi->flag & ICE_FLTR_TX) && IS_UNICAST_ETHER_ADDR(addr) &&
2039                     (fi->fltr_act == ICE_FWD_TO_VSI ||
2040                      fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
2041                      fi->fltr_act == ICE_FWD_TO_Q ||
2042                      fi->fltr_act == ICE_FWD_TO_QGRP)) {
2043                         status = ice_update_pkt_fwd_rule(hw, fi);
2044                         if (status)
2045                                 break;
2046                 }
2047         }
2048
2049         ice_release_lock(rule_lock);
2050
2051         return status;
2052 }
2053
2054 /**
2055  * ice_add_update_vsi_list
2056  * @hw: pointer to the hardware structure
2057  * @m_entry: pointer to current filter management list entry
2058  * @cur_fltr: filter information from the book keeping entry
2059  * @new_fltr: filter information with the new VSI to be added
2060  *
2061  * Call AQ command to add or update previously created VSI list with new VSI.
2062  *
2063  * Helper function to do book keeping associated with adding filter information
2064  * The algorithm to do the book keeping is described below :
2065  * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
2066  *      if only one VSI has been added till now
2067  *              Allocate a new VSI list and add two VSIs
2068  *              to this list using switch rule command
2069  *              Update the previously created switch rule with the
2070  *              newly created VSI list ID
2071  *      if a VSI list was previously created
2072  *              Add the new VSI to the previously created VSI list set
2073  *              using the update switch rule command
2074  */
2075 static enum ice_status
2076 ice_add_update_vsi_list(struct ice_hw *hw,
2077                         struct ice_fltr_mgmt_list_entry *m_entry,
2078                         struct ice_fltr_info *cur_fltr,
2079                         struct ice_fltr_info *new_fltr)
2080 {
2081         enum ice_status status = ICE_SUCCESS;
2082         u16 vsi_list_id = 0;
2083
2084         if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
2085              cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
2086                 return ICE_ERR_NOT_IMPL;
2087
2088         if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
2089              new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
2090             (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
2091              cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
2092                 return ICE_ERR_NOT_IMPL;
2093
2094         if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
2095                 /* Only one entry existed in the mapping and it was not already
2096                  * a part of a VSI list. So, create a VSI list with the old and
2097                  * new VSIs.
2098                  */
2099                 struct ice_fltr_info tmp_fltr;
2100                 u16 vsi_handle_arr[2];
2101
2102                 /* A rule already exists with the new VSI being added */
2103                 if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
2104                         return ICE_ERR_ALREADY_EXISTS;
2105
2106                 vsi_handle_arr[0] = cur_fltr->vsi_handle;
2107                 vsi_handle_arr[1] = new_fltr->vsi_handle;
2108                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2109                                                   &vsi_list_id,
2110                                                   new_fltr->lkup_type);
2111                 if (status)
2112                         return status;
2113
2114                 tmp_fltr = *new_fltr;
2115                 tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
2116                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2117                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2118                 /* Update the previous switch rule of "MAC forward to VSI" to
2119                  * "MAC fwd to VSI list"
2120                  */
2121                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2122                 if (status)
2123                         return status;
2124
2125                 cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
2126                 cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2127                 m_entry->vsi_list_info =
2128                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2129                                                 vsi_list_id);
2130
2131                 /* If this entry was large action then the large action needs
2132                  * to be updated to point to FWD to VSI list
2133                  */
2134                 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
2135                         status =
2136                             ice_add_marker_act(hw, m_entry,
2137                                                m_entry->sw_marker_id,
2138                                                m_entry->lg_act_idx);
2139         } else {
2140                 u16 vsi_handle = new_fltr->vsi_handle;
2141                 enum ice_adminq_opc opcode;
2142
2143                 if (!m_entry->vsi_list_info)
2144                         return ICE_ERR_CFG;
2145
2146                 /* A rule already exists with the new VSI being added */
2147                 if (ice_is_bit_set(m_entry->vsi_list_info->vsi_map, vsi_handle))
2148                         return ICE_SUCCESS;
2149
2150                 /* Update the previously created VSI list set with
2151                  * the new VSI ID passed in
2152                  */
2153                 vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
2154                 opcode = ice_aqc_opc_update_sw_rules;
2155
2156                 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
2157                                                   vsi_list_id, false, opcode,
2158                                                   new_fltr->lkup_type);
2159                 /* update VSI list mapping info with new VSI ID */
2160                 if (!status)
2161                         ice_set_bit(vsi_handle,
2162                                     m_entry->vsi_list_info->vsi_map);
2163         }
2164         if (!status)
2165                 m_entry->vsi_count++;
2166         return status;
2167 }
2168
2169 /**
2170  * ice_find_rule_entry - Search a rule entry
2171  * @hw: pointer to the hardware structure
2172  * @recp_id: lookup type for which the specified rule needs to be searched
2173  * @f_info: rule information
2174  *
2175  * Helper function to search for a given rule entry
2176  * Returns pointer to entry storing the rule if found
2177  */
2178 static struct ice_fltr_mgmt_list_entry *
2179 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
2180 {
2181         struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
2182         struct ice_switch_info *sw = hw->switch_info;
2183         struct LIST_HEAD_TYPE *list_head;
2184
2185         list_head = &sw->recp_list[recp_id].filt_rules;
2186         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
2187                             list_entry) {
2188                 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
2189                             sizeof(f_info->l_data)) &&
2190                     f_info->flag == list_itr->fltr_info.flag) {
2191                         ret = list_itr;
2192                         break;
2193                 }
2194         }
2195         return ret;
2196 }
2197
2198 /**
2199  * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
2200  * @hw: pointer to the hardware structure
2201  * @recp_id: lookup type for which VSI lists needs to be searched
2202  * @vsi_handle: VSI handle to be found in VSI list
2203  * @vsi_list_id: VSI list ID found containing vsi_handle
2204  *
2205  * Helper function to search a VSI list with single entry containing given VSI
2206  * handle element. This can be extended further to search VSI list with more
2207  * than 1 vsi_count. Returns pointer to VSI list entry if found.
2208  */
2209 static struct ice_vsi_list_map_info *
2210 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
2211                         u16 *vsi_list_id)
2212 {
2213         struct ice_vsi_list_map_info *map_info = NULL;
2214         struct ice_switch_info *sw = hw->switch_info;
2215         struct LIST_HEAD_TYPE *list_head;
2216
2217         list_head = &sw->recp_list[recp_id].filt_rules;
2218         if (sw->recp_list[recp_id].adv_rule) {
2219                 struct ice_adv_fltr_mgmt_list_entry *list_itr;
2220
2221                 LIST_FOR_EACH_ENTRY(list_itr, list_head,
2222                                     ice_adv_fltr_mgmt_list_entry,
2223                                     list_entry) {
2224                         if (list_itr->vsi_list_info) {
2225                                 map_info = list_itr->vsi_list_info;
2226                                 if (ice_is_bit_set(map_info->vsi_map,
2227                                                    vsi_handle)) {
2228                                         *vsi_list_id = map_info->vsi_list_id;
2229                                         return map_info;
2230                                 }
2231                         }
2232                 }
2233         } else {
2234                 struct ice_fltr_mgmt_list_entry *list_itr;
2235
2236                 LIST_FOR_EACH_ENTRY(list_itr, list_head,
2237                                     ice_fltr_mgmt_list_entry,
2238                                     list_entry) {
2239                         if (list_itr->vsi_count == 1 &&
2240                             list_itr->vsi_list_info) {
2241                                 map_info = list_itr->vsi_list_info;
2242                                 if (ice_is_bit_set(map_info->vsi_map,
2243                                                    vsi_handle)) {
2244                                         *vsi_list_id = map_info->vsi_list_id;
2245                                         return map_info;
2246                                 }
2247                         }
2248                 }
2249         }
2250         return NULL;
2251 }
2252
2253 /**
2254  * ice_add_rule_internal - add rule for a given lookup type
2255  * @hw: pointer to the hardware structure
2256  * @recp_id: lookup type (recipe ID) for which rule has to be added
2257  * @f_entry: structure containing MAC forwarding information
2258  *
2259  * Adds or updates the rule lists for a given recipe
2260  */
2261 static enum ice_status
2262 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
2263                       struct ice_fltr_list_entry *f_entry)
2264 {
2265         struct ice_switch_info *sw = hw->switch_info;
2266         struct ice_fltr_info *new_fltr, *cur_fltr;
2267         struct ice_fltr_mgmt_list_entry *m_entry;
2268         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2269         enum ice_status status = ICE_SUCCESS;
2270
2271         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2272                 return ICE_ERR_PARAM;
2273
2274         /* Load the hw_vsi_id only if the fwd action is fwd to VSI */
2275         if (f_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI)
2276                 f_entry->fltr_info.fwd_id.hw_vsi_id =
2277                         ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2278
2279         rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
2280
2281         ice_acquire_lock(rule_lock);
2282         new_fltr = &f_entry->fltr_info;
2283         if (new_fltr->flag & ICE_FLTR_RX)
2284                 new_fltr->src = hw->port_info->lport;
2285         else if (new_fltr->flag & ICE_FLTR_TX)
2286                 new_fltr->src =
2287                         ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2288
2289         m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
2290         if (!m_entry) {
2291                 ice_release_lock(rule_lock);
2292                 return ice_create_pkt_fwd_rule(hw, f_entry);
2293         }
2294
2295         cur_fltr = &m_entry->fltr_info;
2296         status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
2297         ice_release_lock(rule_lock);
2298
2299         return status;
2300 }
2301
2302 /**
2303  * ice_remove_vsi_list_rule
2304  * @hw: pointer to the hardware structure
2305  * @vsi_list_id: VSI list ID generated as part of allocate resource
2306  * @lkup_type: switch rule filter lookup type
2307  *
2308  * The VSI list should be emptied before this function is called to remove the
2309  * VSI list.
2310  */
2311 static enum ice_status
2312 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
2313                          enum ice_sw_lkup_type lkup_type)
2314 {
2315         struct ice_aqc_sw_rules_elem *s_rule;
2316         enum ice_status status;
2317         u16 s_rule_size;
2318
2319         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
2320         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
2321         if (!s_rule)
2322                 return ICE_ERR_NO_MEMORY;
2323
2324         s_rule->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
2325         s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
2326
2327         /* Free the vsi_list resource that we allocated. It is assumed that the
2328          * list is empty at this point.
2329          */
2330         status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
2331                                             ice_aqc_opc_free_res);
2332
2333         ice_free(hw, s_rule);
2334         return status;
2335 }
2336
2337 /**
2338  * ice_rem_update_vsi_list
2339  * @hw: pointer to the hardware structure
2340  * @vsi_handle: VSI handle of the VSI to remove
2341  * @fm_list: filter management entry for which the VSI list management needs to
2342  *           be done
2343  */
2344 static enum ice_status
2345 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
2346                         struct ice_fltr_mgmt_list_entry *fm_list)
2347 {
2348         enum ice_sw_lkup_type lkup_type;
2349         enum ice_status status = ICE_SUCCESS;
2350         u16 vsi_list_id;
2351
2352         if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
2353             fm_list->vsi_count == 0)
2354                 return ICE_ERR_PARAM;
2355
2356         /* A rule with the VSI being removed does not exist */
2357         if (!ice_is_bit_set(fm_list->vsi_list_info->vsi_map, vsi_handle))
2358                 return ICE_ERR_DOES_NOT_EXIST;
2359
2360         lkup_type = fm_list->fltr_info.lkup_type;
2361         vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
2362         status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
2363                                           ice_aqc_opc_update_sw_rules,
2364                                           lkup_type);
2365         if (status)
2366                 return status;
2367
2368         fm_list->vsi_count--;
2369         ice_clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
2370
2371         if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
2372                 struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
2373                 struct ice_vsi_list_map_info *vsi_list_info =
2374                         fm_list->vsi_list_info;
2375                 u16 rem_vsi_handle;
2376
2377                 rem_vsi_handle = ice_find_first_bit(vsi_list_info->vsi_map,
2378                                                     ICE_MAX_VSI);
2379                 if (!ice_is_vsi_valid(hw, rem_vsi_handle))
2380                         return ICE_ERR_OUT_OF_RANGE;
2381
2382                 /* Make sure VSI list is empty before removing it below */
2383                 status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
2384                                                   vsi_list_id, true,
2385                                                   ice_aqc_opc_update_sw_rules,
2386                                                   lkup_type);
2387                 if (status)
2388                         return status;
2389
2390                 tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
2391                 tmp_fltr_info.fwd_id.hw_vsi_id =
2392                         ice_get_hw_vsi_num(hw, rem_vsi_handle);
2393                 tmp_fltr_info.vsi_handle = rem_vsi_handle;
2394                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
2395                 if (status) {
2396                         ice_debug(hw, ICE_DBG_SW,
2397                                   "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
2398                                   tmp_fltr_info.fwd_id.hw_vsi_id, status);
2399                         return status;
2400                 }
2401
2402                 fm_list->fltr_info = tmp_fltr_info;
2403         }
2404
2405         if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
2406             (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
2407                 struct ice_vsi_list_map_info *vsi_list_info =
2408                         fm_list->vsi_list_info;
2409
2410                 /* Remove the VSI list since it is no longer used */
2411                 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
2412                 if (status) {
2413                         ice_debug(hw, ICE_DBG_SW,
2414                                   "Failed to remove VSI list %d, error %d\n",
2415                                   vsi_list_id, status);
2416                         return status;
2417                 }
2418
2419                 LIST_DEL(&vsi_list_info->list_entry);
2420                 ice_free(hw, vsi_list_info);
2421                 fm_list->vsi_list_info = NULL;
2422         }
2423
2424         return status;
2425 }
2426
2427 /**
2428  * ice_remove_rule_internal - Remove a filter rule of a given type
2429  *
2430  * @hw: pointer to the hardware structure
2431  * @recp_id: recipe ID for which the rule needs to removed
2432  * @f_entry: rule entry containing filter information
2433  */
2434 static enum ice_status
2435 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
2436                          struct ice_fltr_list_entry *f_entry)
2437 {
2438         struct ice_switch_info *sw = hw->switch_info;
2439         struct ice_fltr_mgmt_list_entry *list_elem;
2440         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2441         enum ice_status status = ICE_SUCCESS;
2442         bool remove_rule = false;
2443         u16 vsi_handle;
2444
2445         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2446                 return ICE_ERR_PARAM;
2447         f_entry->fltr_info.fwd_id.hw_vsi_id =
2448                 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2449
2450         rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
2451         ice_acquire_lock(rule_lock);
2452         list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
2453         if (!list_elem) {
2454                 status = ICE_ERR_DOES_NOT_EXIST;
2455                 goto exit;
2456         }
2457
2458         if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
2459                 remove_rule = true;
2460         } else if (!list_elem->vsi_list_info) {
2461                 status = ICE_ERR_DOES_NOT_EXIST;
2462                 goto exit;
2463         } else if (list_elem->vsi_list_info->ref_cnt > 1) {
2464                 /* a ref_cnt > 1 indicates that the vsi_list is being
2465                  * shared by multiple rules. Decrement the ref_cnt and
2466                  * remove this rule, but do not modify the list, as it
2467                  * is in-use by other rules.
2468                  */
2469                 list_elem->vsi_list_info->ref_cnt--;
2470                 remove_rule = true;
2471         } else {
2472                 /* a ref_cnt of 1 indicates the vsi_list is only used
2473                  * by one rule. However, the original removal request is only
2474                  * for a single VSI. Update the vsi_list first, and only
2475                  * remove the rule if there are no further VSIs in this list.
2476                  */
2477                 vsi_handle = f_entry->fltr_info.vsi_handle;
2478                 status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
2479                 if (status)
2480                         goto exit;
2481                 /* if VSI count goes to zero after updating the VSI list */
2482                 if (list_elem->vsi_count == 0)
2483                         remove_rule = true;
2484         }
2485
2486         if (remove_rule) {
2487                 /* Remove the lookup rule */
2488                 struct ice_aqc_sw_rules_elem *s_rule;
2489
2490                 s_rule = (struct ice_aqc_sw_rules_elem *)
2491                         ice_malloc(hw, ICE_SW_RULE_RX_TX_NO_HDR_SIZE);
2492                 if (!s_rule) {
2493                         status = ICE_ERR_NO_MEMORY;
2494                         goto exit;
2495                 }
2496
2497                 ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
2498                                  ice_aqc_opc_remove_sw_rules);
2499
2500                 status = ice_aq_sw_rules(hw, s_rule,
2501                                          ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
2502                                          ice_aqc_opc_remove_sw_rules, NULL);
2503                 if (status)
2504                         goto exit;
2505
2506                 /* Remove a book keeping from the list */
2507                 ice_free(hw, s_rule);
2508
2509                 LIST_DEL(&list_elem->list_entry);
2510                 ice_free(hw, list_elem);
2511         }
2512 exit:
2513         ice_release_lock(rule_lock);
2514         return status;
2515 }
2516
2517 /**
2518  * ice_aq_get_res_alloc - get allocated resources
2519  * @hw: pointer to the HW struct
2520  * @num_entries: pointer to u16 to store the number of resource entries returned
2521  * @buf: pointer to user-supplied buffer
2522  * @buf_size: size of buff
2523  * @cd: pointer to command details structure or NULL
2524  *
2525  * The user-supplied buffer must be large enough to store the resource
2526  * information for all resource types. Each resource type is an
2527  * ice_aqc_get_res_resp_data_elem structure.
2528  */
2529 enum ice_status
2530 ice_aq_get_res_alloc(struct ice_hw *hw, u16 *num_entries, void *buf,
2531                      u16 buf_size, struct ice_sq_cd *cd)
2532 {
2533         struct ice_aqc_get_res_alloc *resp;
2534         enum ice_status status;
2535         struct ice_aq_desc desc;
2536
2537         if (!buf)
2538                 return ICE_ERR_BAD_PTR;
2539
2540         if (buf_size < ICE_AQ_GET_RES_ALLOC_BUF_LEN)
2541                 return ICE_ERR_INVAL_SIZE;
2542
2543         resp = &desc.params.get_res;
2544
2545         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_res_alloc);
2546         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2547
2548         if (!status && num_entries)
2549                 *num_entries = LE16_TO_CPU(resp->resp_elem_num);
2550
2551         return status;
2552 }
2553
2554 /**
2555  * ice_aq_get_res_descs - get allocated resource descriptors
2556  * @hw: pointer to the hardware structure
2557  * @num_entries: number of resource entries in buffer
2558  * @buf: Indirect buffer to hold data parameters and response
2559  * @buf_size: size of buffer for indirect commands
2560  * @res_type: resource type
2561  * @res_shared: is resource shared
2562  * @desc_id: input - first desc ID to start; output - next desc ID
2563  * @cd: pointer to command details structure or NULL
2564  */
2565 enum ice_status
2566 ice_aq_get_res_descs(struct ice_hw *hw, u16 num_entries,
2567                      struct ice_aqc_get_allocd_res_desc_resp *buf,
2568                      u16 buf_size, u16 res_type, bool res_shared, u16 *desc_id,
2569                      struct ice_sq_cd *cd)
2570 {
2571         struct ice_aqc_get_allocd_res_desc *cmd;
2572         struct ice_aq_desc desc;
2573         enum ice_status status;
2574
2575         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_get_res_descs");
2576
2577         cmd = &desc.params.get_res_desc;
2578
2579         if (!buf)
2580                 return ICE_ERR_PARAM;
2581
2582         if (buf_size != (num_entries * sizeof(*buf)))
2583                 return ICE_ERR_PARAM;
2584
2585         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_allocd_res_desc);
2586
2587         cmd->ops.cmd.res = CPU_TO_LE16(((res_type << ICE_AQC_RES_TYPE_S) &
2588                                          ICE_AQC_RES_TYPE_M) | (res_shared ?
2589                                         ICE_AQC_RES_TYPE_FLAG_SHARED : 0));
2590         cmd->ops.cmd.first_desc = CPU_TO_LE16(*desc_id);
2591
2592         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
2593
2594         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2595         if (!status)
2596                 *desc_id = LE16_TO_CPU(cmd->ops.resp.next_desc);
2597
2598         return status;
2599 }
2600
2601 /**
2602  * ice_add_mac - Add a MAC address based filter rule
2603  * @hw: pointer to the hardware structure
2604  * @m_list: list of MAC addresses and forwarding information
2605  *
2606  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
2607  * multiple unicast addresses, the function assumes that all the
2608  * addresses are unique in a given add_mac call. It doesn't
2609  * check for duplicates in this case, removing duplicates from a given
2610  * list should be taken care of in the caller of this function.
2611  */
2612 enum ice_status
2613 ice_add_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2614 {
2615         struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
2616         struct ice_fltr_list_entry *m_list_itr;
2617         struct LIST_HEAD_TYPE *rule_head;
2618         u16 elem_sent, total_elem_left;
2619         struct ice_switch_info *sw;
2620         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2621         enum ice_status status = ICE_SUCCESS;
2622         u16 num_unicast = 0;
2623         u16 s_rule_size;
2624
2625         if (!m_list || !hw)
2626                 return ICE_ERR_PARAM;
2627         s_rule = NULL;
2628         sw = hw->switch_info;
2629         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2630         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2631                             list_entry) {
2632                 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
2633                 u16 vsi_handle;
2634                 u16 hw_vsi_id;
2635
2636                 m_list_itr->fltr_info.flag = ICE_FLTR_TX;
2637                 vsi_handle = m_list_itr->fltr_info.vsi_handle;
2638                 if (!ice_is_vsi_valid(hw, vsi_handle))
2639                         return ICE_ERR_PARAM;
2640                 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2641                 m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
2642                 /* update the src in case it is VSI num */
2643                 if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
2644                         return ICE_ERR_PARAM;
2645                 m_list_itr->fltr_info.src = hw_vsi_id;
2646                 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
2647                     IS_ZERO_ETHER_ADDR(add))
2648                         return ICE_ERR_PARAM;
2649                 if (IS_UNICAST_ETHER_ADDR(add) && !hw->ucast_shared) {
2650                         /* Don't overwrite the unicast address */
2651                         ice_acquire_lock(rule_lock);
2652                         if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
2653                                                 &m_list_itr->fltr_info)) {
2654                                 ice_release_lock(rule_lock);
2655                                 return ICE_ERR_ALREADY_EXISTS;
2656                         }
2657                         ice_release_lock(rule_lock);
2658                         num_unicast++;
2659                 } else if (IS_MULTICAST_ETHER_ADDR(add) ||
2660                            (IS_UNICAST_ETHER_ADDR(add) && hw->ucast_shared)) {
2661                         m_list_itr->status =
2662                                 ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
2663                                                       m_list_itr);
2664                         if (m_list_itr->status)
2665                                 return m_list_itr->status;
2666                 }
2667         }
2668
2669         ice_acquire_lock(rule_lock);
2670         /* Exit if no suitable entries were found for adding bulk switch rule */
2671         if (!num_unicast) {
2672                 status = ICE_SUCCESS;
2673                 goto ice_add_mac_exit;
2674         }
2675
2676         rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2677
2678         /* Allocate switch rule buffer for the bulk update for unicast */
2679         s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
2680         s_rule = (struct ice_aqc_sw_rules_elem *)
2681                 ice_calloc(hw, num_unicast, s_rule_size);
2682         if (!s_rule) {
2683                 status = ICE_ERR_NO_MEMORY;
2684                 goto ice_add_mac_exit;
2685         }
2686
2687         r_iter = s_rule;
2688         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2689                             list_entry) {
2690                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2691                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2692
2693                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2694                         ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
2695                                          ice_aqc_opc_add_sw_rules);
2696                         r_iter = (struct ice_aqc_sw_rules_elem *)
2697                                 ((u8 *)r_iter + s_rule_size);
2698                 }
2699         }
2700
2701         /* Call AQ bulk switch rule update for all unicast addresses */
2702         r_iter = s_rule;
2703         /* Call AQ switch rule in AQ_MAX chunk */
2704         for (total_elem_left = num_unicast; total_elem_left > 0;
2705              total_elem_left -= elem_sent) {
2706                 struct ice_aqc_sw_rules_elem *entry = r_iter;
2707
2708                 elem_sent = min(total_elem_left,
2709                                 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
2710                 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
2711                                          elem_sent, ice_aqc_opc_add_sw_rules,
2712                                          NULL);
2713                 if (status)
2714                         goto ice_add_mac_exit;
2715                 r_iter = (struct ice_aqc_sw_rules_elem *)
2716                         ((u8 *)r_iter + (elem_sent * s_rule_size));
2717         }
2718
2719         /* Fill up rule ID based on the value returned from FW */
2720         r_iter = s_rule;
2721         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2722                             list_entry) {
2723                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2724                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2725                 struct ice_fltr_mgmt_list_entry *fm_entry;
2726
2727                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2728                         f_info->fltr_rule_id =
2729                                 LE16_TO_CPU(r_iter->pdata.lkup_tx_rx.index);
2730                         f_info->fltr_act = ICE_FWD_TO_VSI;
2731                         /* Create an entry to track this MAC address */
2732                         fm_entry = (struct ice_fltr_mgmt_list_entry *)
2733                                 ice_malloc(hw, sizeof(*fm_entry));
2734                         if (!fm_entry) {
2735                                 status = ICE_ERR_NO_MEMORY;
2736                                 goto ice_add_mac_exit;
2737                         }
2738                         fm_entry->fltr_info = *f_info;
2739                         fm_entry->vsi_count = 1;
2740                         /* The book keeping entries will get removed when
2741                          * base driver calls remove filter AQ command
2742                          */
2743
2744                         LIST_ADD(&fm_entry->list_entry, rule_head);
2745                         r_iter = (struct ice_aqc_sw_rules_elem *)
2746                                 ((u8 *)r_iter + s_rule_size);
2747                 }
2748         }
2749
2750 ice_add_mac_exit:
2751         ice_release_lock(rule_lock);
2752         if (s_rule)
2753                 ice_free(hw, s_rule);
2754         return status;
2755 }
2756
2757 /**
2758  * ice_add_vlan_internal - Add one VLAN based filter rule
2759  * @hw: pointer to the hardware structure
2760  * @f_entry: filter entry containing one VLAN information
2761  */
2762 static enum ice_status
2763 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
2764 {
2765         struct ice_switch_info *sw = hw->switch_info;
2766         struct ice_fltr_mgmt_list_entry *v_list_itr;
2767         struct ice_fltr_info *new_fltr, *cur_fltr;
2768         enum ice_sw_lkup_type lkup_type;
2769         u16 vsi_list_id = 0, vsi_handle;
2770         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2771         enum ice_status status = ICE_SUCCESS;
2772
2773         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2774                 return ICE_ERR_PARAM;
2775
2776         f_entry->fltr_info.fwd_id.hw_vsi_id =
2777                 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2778         new_fltr = &f_entry->fltr_info;
2779
2780         /* VLAN ID should only be 12 bits */
2781         if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
2782                 return ICE_ERR_PARAM;
2783
2784         if (new_fltr->src_id != ICE_SRC_ID_VSI)
2785                 return ICE_ERR_PARAM;
2786
2787         new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
2788         lkup_type = new_fltr->lkup_type;
2789         vsi_handle = new_fltr->vsi_handle;
2790         rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2791         ice_acquire_lock(rule_lock);
2792         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
2793         if (!v_list_itr) {
2794                 struct ice_vsi_list_map_info *map_info = NULL;
2795
2796                 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
2797                         /* All VLAN pruning rules use a VSI list. Check if
2798                          * there is already a VSI list containing VSI that we
2799                          * want to add. If found, use the same vsi_list_id for
2800                          * this new VLAN rule or else create a new list.
2801                          */
2802                         map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
2803                                                            vsi_handle,
2804                                                            &vsi_list_id);
2805                         if (!map_info) {
2806                                 status = ice_create_vsi_list_rule(hw,
2807                                                                   &vsi_handle,
2808                                                                   1,
2809                                                                   &vsi_list_id,
2810                                                                   lkup_type);
2811                                 if (status)
2812                                         goto exit;
2813                         }
2814                         /* Convert the action to forwarding to a VSI list. */
2815                         new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2816                         new_fltr->fwd_id.vsi_list_id = vsi_list_id;
2817                 }
2818
2819                 status = ice_create_pkt_fwd_rule(hw, f_entry);
2820                 if (!status) {
2821                         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
2822                                                          new_fltr);
2823                         if (!v_list_itr) {
2824                                 status = ICE_ERR_DOES_NOT_EXIST;
2825                                 goto exit;
2826                         }
2827                         /* reuse VSI list for new rule and increment ref_cnt */
2828                         if (map_info) {
2829                                 v_list_itr->vsi_list_info = map_info;
2830                                 map_info->ref_cnt++;
2831                         } else {
2832                                 v_list_itr->vsi_list_info =
2833                                         ice_create_vsi_list_map(hw, &vsi_handle,
2834                                                                 1, vsi_list_id);
2835                         }
2836                 }
2837         } else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
2838                 /* Update existing VSI list to add new VSI ID only if it used
2839                  * by one VLAN rule.
2840                  */
2841                 cur_fltr = &v_list_itr->fltr_info;
2842                 status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
2843                                                  new_fltr);
2844         } else {
2845                 /* If VLAN rule exists and VSI list being used by this rule is
2846                  * referenced by more than 1 VLAN rule. Then create a new VSI
2847                  * list appending previous VSI with new VSI and update existing
2848                  * VLAN rule to point to new VSI list ID
2849                  */
2850                 struct ice_fltr_info tmp_fltr;
2851                 u16 vsi_handle_arr[2];
2852                 u16 cur_handle;
2853
2854                 /* Current implementation only supports reusing VSI list with
2855                  * one VSI count. We should never hit below condition
2856                  */
2857                 if (v_list_itr->vsi_count > 1 &&
2858                     v_list_itr->vsi_list_info->ref_cnt > 1) {
2859                         ice_debug(hw, ICE_DBG_SW,
2860                                   "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
2861                         status = ICE_ERR_CFG;
2862                         goto exit;
2863                 }
2864
2865                 cur_handle =
2866                         ice_find_first_bit(v_list_itr->vsi_list_info->vsi_map,
2867                                            ICE_MAX_VSI);
2868
2869                 /* A rule already exists with the new VSI being added */
2870                 if (cur_handle == vsi_handle) {
2871                         status = ICE_ERR_ALREADY_EXISTS;
2872                         goto exit;
2873                 }
2874
2875                 vsi_handle_arr[0] = cur_handle;
2876                 vsi_handle_arr[1] = vsi_handle;
2877                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2878                                                   &vsi_list_id, lkup_type);
2879                 if (status)
2880                         goto exit;
2881
2882                 tmp_fltr = v_list_itr->fltr_info;
2883                 tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
2884                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2885                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2886                 /* Update the previous switch rule to a new VSI list which
2887                  * includes current VSI that is requested
2888                  */
2889                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2890                 if (status)
2891                         goto exit;
2892
2893                 /* before overriding VSI list map info. decrement ref_cnt of
2894                  * previous VSI list
2895                  */
2896                 v_list_itr->vsi_list_info->ref_cnt--;
2897
2898                 /* now update to newly created list */
2899                 v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
2900                 v_list_itr->vsi_list_info =
2901                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2902                                                 vsi_list_id);
2903                 v_list_itr->vsi_count++;
2904         }
2905
2906 exit:
2907         ice_release_lock(rule_lock);
2908         return status;
2909 }
2910
2911 /**
2912  * ice_add_vlan - Add VLAN based filter rule
2913  * @hw: pointer to the hardware structure
2914  * @v_list: list of VLAN entries and forwarding information
2915  */
2916 enum ice_status
2917 ice_add_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2918 {
2919         struct ice_fltr_list_entry *v_list_itr;
2920
2921         if (!v_list || !hw)
2922                 return ICE_ERR_PARAM;
2923
2924         LIST_FOR_EACH_ENTRY(v_list_itr, v_list, ice_fltr_list_entry,
2925                             list_entry) {
2926                 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
2927                         return ICE_ERR_PARAM;
2928                 v_list_itr->fltr_info.flag = ICE_FLTR_TX;
2929                 v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
2930                 if (v_list_itr->status)
2931                         return v_list_itr->status;
2932         }
2933         return ICE_SUCCESS;
2934 }
2935
2936 #ifndef NO_MACVLAN_SUPPORT
2937 /**
2938  * ice_add_mac_vlan - Add MAC and VLAN pair based filter rule
2939  * @hw: pointer to the hardware structure
2940  * @mv_list: list of MAC and VLAN filters
2941  *
2942  * If the VSI on which the MAC-VLAN pair has to be added has Rx and Tx VLAN
2943  * pruning bits enabled, then it is the responsibility of the caller to make
2944  * sure to add a VLAN only filter on the same VSI. Packets belonging to that
2945  * VLAN won't be received on that VSI otherwise.
2946  */
2947 enum ice_status
2948 ice_add_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *mv_list)
2949 {
2950         struct ice_fltr_list_entry *mv_list_itr;
2951
2952         if (!mv_list || !hw)
2953                 return ICE_ERR_PARAM;
2954
2955         LIST_FOR_EACH_ENTRY(mv_list_itr, mv_list, ice_fltr_list_entry,
2956                             list_entry) {
2957                 enum ice_sw_lkup_type l_type =
2958                         mv_list_itr->fltr_info.lkup_type;
2959
2960                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
2961                         return ICE_ERR_PARAM;
2962                 mv_list_itr->fltr_info.flag = ICE_FLTR_TX;
2963                 mv_list_itr->status =
2964                         ice_add_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
2965                                               mv_list_itr);
2966                 if (mv_list_itr->status)
2967                         return mv_list_itr->status;
2968         }
2969         return ICE_SUCCESS;
2970 }
2971 #endif
2972
2973 /**
2974  * ice_add_eth_mac - Add ethertype and MAC based filter rule
2975  * @hw: pointer to the hardware structure
2976  * @em_list: list of ether type MAC filter, MAC is optional
2977  */
2978 enum ice_status
2979 ice_add_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2980 {
2981         struct ice_fltr_list_entry *em_list_itr;
2982
2983         LIST_FOR_EACH_ENTRY(em_list_itr, em_list, ice_fltr_list_entry,
2984                             list_entry) {
2985                 enum ice_sw_lkup_type l_type =
2986                         em_list_itr->fltr_info.lkup_type;
2987
2988                 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2989                     l_type != ICE_SW_LKUP_ETHERTYPE)
2990                         return ICE_ERR_PARAM;
2991
2992                 em_list_itr->fltr_info.flag = ICE_FLTR_TX;
2993                 em_list_itr->status = ice_add_rule_internal(hw, l_type,
2994                                                             em_list_itr);
2995                 if (em_list_itr->status)
2996                         return em_list_itr->status;
2997         }
2998         return ICE_SUCCESS;
2999 }
3000
3001 /**
3002  * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
3003  * @hw: pointer to the hardware structure
3004  * @em_list: list of ethertype or ethertype MAC entries
3005  */
3006 enum ice_status
3007 ice_remove_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
3008 {
3009         struct ice_fltr_list_entry *em_list_itr, *tmp;
3010
3011         if (!em_list || !hw)
3012                 return ICE_ERR_PARAM;
3013
3014         LIST_FOR_EACH_ENTRY_SAFE(em_list_itr, tmp, em_list, ice_fltr_list_entry,
3015                                  list_entry) {
3016                 enum ice_sw_lkup_type l_type =
3017                         em_list_itr->fltr_info.lkup_type;
3018
3019                 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
3020                     l_type != ICE_SW_LKUP_ETHERTYPE)
3021                         return ICE_ERR_PARAM;
3022
3023                 em_list_itr->status = ice_remove_rule_internal(hw, l_type,
3024                                                                em_list_itr);
3025                 if (em_list_itr->status)
3026                         return em_list_itr->status;
3027         }
3028         return ICE_SUCCESS;
3029 }
3030
3031
3032 /**
3033  * ice_rem_sw_rule_info
3034  * @hw: pointer to the hardware structure
3035  * @rule_head: pointer to the switch list structure that we want to delete
3036  */
3037 static void
3038 ice_rem_sw_rule_info(struct ice_hw *hw, struct LIST_HEAD_TYPE *rule_head)
3039 {
3040         if (!LIST_EMPTY(rule_head)) {
3041                 struct ice_fltr_mgmt_list_entry *entry;
3042                 struct ice_fltr_mgmt_list_entry *tmp;
3043
3044                 LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, rule_head,
3045                                          ice_fltr_mgmt_list_entry, list_entry) {
3046                         LIST_DEL(&entry->list_entry);
3047                         ice_free(hw, entry);
3048                 }
3049         }
3050 }
3051
3052 /**
3053  * ice_rem_adv_rule_info
3054  * @hw: pointer to the hardware structure
3055  * @rule_head: pointer to the switch list structure that we want to delete
3056  */
3057 static void
3058 ice_rem_adv_rule_info(struct ice_hw *hw, struct LIST_HEAD_TYPE *rule_head)
3059 {
3060         struct ice_adv_fltr_mgmt_list_entry *tmp_entry;
3061         struct ice_adv_fltr_mgmt_list_entry *lst_itr;
3062
3063         if (LIST_EMPTY(rule_head))
3064                 return;
3065
3066         LIST_FOR_EACH_ENTRY_SAFE(lst_itr, tmp_entry, rule_head,
3067                                  ice_adv_fltr_mgmt_list_entry, list_entry) {
3068                 LIST_DEL(&lst_itr->list_entry);
3069                 ice_free(hw, lst_itr->lkups);
3070                 ice_free(hw, lst_itr);
3071         }
3072 }
3073
3074 /**
3075  * ice_rem_all_sw_rules_info
3076  * @hw: pointer to the hardware structure
3077  */
3078 void ice_rem_all_sw_rules_info(struct ice_hw *hw)
3079 {
3080         struct ice_switch_info *sw = hw->switch_info;
3081         u8 i;
3082
3083         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3084                 struct LIST_HEAD_TYPE *rule_head;
3085
3086                 rule_head = &sw->recp_list[i].filt_rules;
3087                 if (!sw->recp_list[i].adv_rule)
3088                         ice_rem_sw_rule_info(hw, rule_head);
3089                 else
3090                         ice_rem_adv_rule_info(hw, rule_head);
3091         }
3092 }
3093
3094 /**
3095  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
3096  * @pi: pointer to the port_info structure
3097  * @vsi_handle: VSI handle to set as default
3098  * @set: true to add the above mentioned switch rule, false to remove it
3099  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
3100  *
3101  * add filter rule to set/unset given VSI as default VSI for the switch
3102  * (represented by swid)
3103  */
3104 enum ice_status
3105 ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
3106                  u8 direction)
3107 {
3108         struct ice_aqc_sw_rules_elem *s_rule;
3109         struct ice_fltr_info f_info;
3110         struct ice_hw *hw = pi->hw;
3111         enum ice_adminq_opc opcode;
3112         enum ice_status status;
3113         u16 s_rule_size;
3114         u16 hw_vsi_id;
3115
3116         if (!ice_is_vsi_valid(hw, vsi_handle))
3117                 return ICE_ERR_PARAM;
3118         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3119
3120         s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
3121                             ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
3122         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
3123         if (!s_rule)
3124                 return ICE_ERR_NO_MEMORY;
3125
3126         ice_memset(&f_info, 0, sizeof(f_info), ICE_NONDMA_MEM);
3127
3128         f_info.lkup_type = ICE_SW_LKUP_DFLT;
3129         f_info.flag = direction;
3130         f_info.fltr_act = ICE_FWD_TO_VSI;
3131         f_info.fwd_id.hw_vsi_id = hw_vsi_id;
3132
3133         if (f_info.flag & ICE_FLTR_RX) {
3134                 f_info.src = pi->lport;
3135                 f_info.src_id = ICE_SRC_ID_LPORT;
3136                 if (!set)
3137                         f_info.fltr_rule_id =
3138                                 pi->dflt_rx_vsi_rule_id;
3139         } else if (f_info.flag & ICE_FLTR_TX) {
3140                 f_info.src_id = ICE_SRC_ID_VSI;
3141                 f_info.src = hw_vsi_id;
3142                 if (!set)
3143                         f_info.fltr_rule_id =
3144                                 pi->dflt_tx_vsi_rule_id;
3145         }
3146
3147         if (set)
3148                 opcode = ice_aqc_opc_add_sw_rules;
3149         else
3150                 opcode = ice_aqc_opc_remove_sw_rules;
3151
3152         ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
3153
3154         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
3155         if (status || !(f_info.flag & ICE_FLTR_TX_RX))
3156                 goto out;
3157         if (set) {
3158                 u16 index = LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
3159
3160                 if (f_info.flag & ICE_FLTR_TX) {
3161                         pi->dflt_tx_vsi_num = hw_vsi_id;
3162                         pi->dflt_tx_vsi_rule_id = index;
3163                 } else if (f_info.flag & ICE_FLTR_RX) {
3164                         pi->dflt_rx_vsi_num = hw_vsi_id;
3165                         pi->dflt_rx_vsi_rule_id = index;
3166                 }
3167         } else {
3168                 if (f_info.flag & ICE_FLTR_TX) {
3169                         pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
3170                         pi->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
3171                 } else if (f_info.flag & ICE_FLTR_RX) {
3172                         pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
3173                         pi->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
3174                 }
3175         }
3176
3177 out:
3178         ice_free(hw, s_rule);
3179         return status;
3180 }
3181
3182 /**
3183  * ice_find_ucast_rule_entry - Search for a unicast MAC filter rule entry
3184  * @hw: pointer to the hardware structure
3185  * @recp_id: lookup type for which the specified rule needs to be searched
3186  * @f_info: rule information
3187  *
3188  * Helper function to search for a unicast rule entry - this is to be used
3189  * to remove unicast MAC filter that is not shared with other VSIs on the
3190  * PF switch.
3191  *
3192  * Returns pointer to entry storing the rule if found
3193  */
3194 static struct ice_fltr_mgmt_list_entry *
3195 ice_find_ucast_rule_entry(struct ice_hw *hw, u8 recp_id,
3196                           struct ice_fltr_info *f_info)
3197 {
3198         struct ice_switch_info *sw = hw->switch_info;
3199         struct ice_fltr_mgmt_list_entry *list_itr;
3200         struct LIST_HEAD_TYPE *list_head;
3201
3202         list_head = &sw->recp_list[recp_id].filt_rules;
3203         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
3204                             list_entry) {
3205                 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
3206                             sizeof(f_info->l_data)) &&
3207                     f_info->fwd_id.hw_vsi_id ==
3208                     list_itr->fltr_info.fwd_id.hw_vsi_id &&
3209                     f_info->flag == list_itr->fltr_info.flag)
3210                         return list_itr;
3211         }
3212         return NULL;
3213 }
3214
3215 /**
3216  * ice_remove_mac - remove a MAC address based filter rule
3217  * @hw: pointer to the hardware structure
3218  * @m_list: list of MAC addresses and forwarding information
3219  *
3220  * This function removes either a MAC filter rule or a specific VSI from a
3221  * VSI list for a multicast MAC address.
3222  *
3223  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
3224  * ice_add_mac. Caller should be aware that this call will only work if all
3225  * the entries passed into m_list were added previously. It will not attempt to
3226  * do a partial remove of entries that were found.
3227  */
3228 enum ice_status
3229 ice_remove_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
3230 {
3231         struct ice_fltr_list_entry *list_itr, *tmp;
3232         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
3233
3234         if (!m_list)
3235                 return ICE_ERR_PARAM;
3236
3237         rule_lock = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
3238         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, m_list, ice_fltr_list_entry,
3239                                  list_entry) {
3240                 enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
3241                 u8 *add = &list_itr->fltr_info.l_data.mac.mac_addr[0];
3242                 u16 vsi_handle;
3243
3244                 if (l_type != ICE_SW_LKUP_MAC)
3245                         return ICE_ERR_PARAM;
3246
3247                 vsi_handle = list_itr->fltr_info.vsi_handle;
3248                 if (!ice_is_vsi_valid(hw, vsi_handle))
3249                         return ICE_ERR_PARAM;
3250
3251                 list_itr->fltr_info.fwd_id.hw_vsi_id =
3252                                         ice_get_hw_vsi_num(hw, vsi_handle);
3253                 if (IS_UNICAST_ETHER_ADDR(add) && !hw->ucast_shared) {
3254                         /* Don't remove the unicast address that belongs to
3255                          * another VSI on the switch, since it is not being
3256                          * shared...
3257                          */
3258                         ice_acquire_lock(rule_lock);
3259                         if (!ice_find_ucast_rule_entry(hw, ICE_SW_LKUP_MAC,
3260                                                        &list_itr->fltr_info)) {
3261                                 ice_release_lock(rule_lock);
3262                                 return ICE_ERR_DOES_NOT_EXIST;
3263                         }
3264                         ice_release_lock(rule_lock);
3265                 }
3266                 list_itr->status = ice_remove_rule_internal(hw,
3267                                                             ICE_SW_LKUP_MAC,
3268                                                             list_itr);
3269                 if (list_itr->status)
3270                         return list_itr->status;
3271         }
3272         return ICE_SUCCESS;
3273 }
3274
3275 /**
3276  * ice_remove_vlan - Remove VLAN based filter rule
3277  * @hw: pointer to the hardware structure
3278  * @v_list: list of VLAN entries and forwarding information
3279  */
3280 enum ice_status
3281 ice_remove_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
3282 {
3283         struct ice_fltr_list_entry *v_list_itr, *tmp;
3284
3285         if (!v_list || !hw)
3286                 return ICE_ERR_PARAM;
3287
3288         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
3289                                  list_entry) {
3290                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
3291
3292                 if (l_type != ICE_SW_LKUP_VLAN)
3293                         return ICE_ERR_PARAM;
3294                 v_list_itr->status = ice_remove_rule_internal(hw,
3295                                                               ICE_SW_LKUP_VLAN,
3296                                                               v_list_itr);
3297                 if (v_list_itr->status)
3298                         return v_list_itr->status;
3299         }
3300         return ICE_SUCCESS;
3301 }
3302
3303 #ifndef NO_MACVLAN_SUPPORT
3304 /**
3305  * ice_remove_mac_vlan - Remove MAC VLAN based filter rule
3306  * @hw: pointer to the hardware structure
3307  * @v_list: list of MAC VLAN entries and forwarding information
3308  */
3309 enum ice_status
3310 ice_remove_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
3311 {
3312         struct ice_fltr_list_entry *v_list_itr, *tmp;
3313
3314         if (!v_list || !hw)
3315                 return ICE_ERR_PARAM;
3316
3317         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
3318                                  list_entry) {
3319                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
3320
3321                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
3322                         return ICE_ERR_PARAM;
3323                 v_list_itr->status =
3324                         ice_remove_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
3325                                                  v_list_itr);
3326                 if (v_list_itr->status)
3327                         return v_list_itr->status;
3328         }
3329         return ICE_SUCCESS;
3330 }
3331 #endif /* !NO_MACVLAN_SUPPORT */
3332
3333 /**
3334  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
3335  * @fm_entry: filter entry to inspect
3336  * @vsi_handle: VSI handle to compare with filter info
3337  */
3338 static bool
3339 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
3340 {
3341         return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
3342                  fm_entry->fltr_info.vsi_handle == vsi_handle) ||
3343                 (fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
3344                  (ice_is_bit_set(fm_entry->vsi_list_info->vsi_map,
3345                                  vsi_handle))));
3346 }
3347
3348 /**
3349  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
3350  * @hw: pointer to the hardware structure
3351  * @vsi_handle: VSI handle to remove filters from
3352  * @vsi_list_head: pointer to the list to add entry to
3353  * @fi: pointer to fltr_info of filter entry to copy & add
3354  *
3355  * Helper function, used when creating a list of filters to remove from
3356  * a specific VSI. The entry added to vsi_list_head is a COPY of the
3357  * original filter entry, with the exception of fltr_info.fltr_act and
3358  * fltr_info.fwd_id fields. These are set such that later logic can
3359  * extract which VSI to remove the fltr from, and pass on that information.
3360  */
3361 static enum ice_status
3362 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3363                                struct LIST_HEAD_TYPE *vsi_list_head,
3364                                struct ice_fltr_info *fi)
3365 {
3366         struct ice_fltr_list_entry *tmp;
3367
3368         /* this memory is freed up in the caller function
3369          * once filters for this VSI are removed
3370          */
3371         tmp = (struct ice_fltr_list_entry *)ice_malloc(hw, sizeof(*tmp));
3372         if (!tmp)
3373                 return ICE_ERR_NO_MEMORY;
3374
3375         tmp->fltr_info = *fi;
3376
3377         /* Overwrite these fields to indicate which VSI to remove filter from,
3378          * so find and remove logic can extract the information from the
3379          * list entries. Note that original entries will still have proper
3380          * values.
3381          */
3382         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
3383         tmp->fltr_info.vsi_handle = vsi_handle;
3384         tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3385
3386         LIST_ADD(&tmp->list_entry, vsi_list_head);
3387
3388         return ICE_SUCCESS;
3389 }
3390
3391 /**
3392  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
3393  * @hw: pointer to the hardware structure
3394  * @vsi_handle: VSI handle to remove filters from
3395  * @lkup_list_head: pointer to the list that has certain lookup type filters
3396  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
3397  *
3398  * Locates all filters in lkup_list_head that are used by the given VSI,
3399  * and adds COPIES of those entries to vsi_list_head (intended to be used
3400  * to remove the listed filters).
3401  * Note that this means all entries in vsi_list_head must be explicitly
3402  * deallocated by the caller when done with list.
3403  */
3404 static enum ice_status
3405 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3406                          struct LIST_HEAD_TYPE *lkup_list_head,
3407                          struct LIST_HEAD_TYPE *vsi_list_head)
3408 {
3409         struct ice_fltr_mgmt_list_entry *fm_entry;
3410         enum ice_status status = ICE_SUCCESS;
3411
3412         /* check to make sure VSI ID is valid and within boundary */
3413         if (!ice_is_vsi_valid(hw, vsi_handle))
3414                 return ICE_ERR_PARAM;
3415
3416         LIST_FOR_EACH_ENTRY(fm_entry, lkup_list_head,
3417                             ice_fltr_mgmt_list_entry, list_entry) {
3418                 struct ice_fltr_info *fi;
3419
3420                 fi = &fm_entry->fltr_info;
3421                 if (!fi || !ice_vsi_uses_fltr(fm_entry, vsi_handle))
3422                         continue;
3423
3424                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3425                                                         vsi_list_head, fi);
3426                 if (status)
3427                         return status;
3428         }
3429         return status;
3430 }
3431
3432
3433 /**
3434  * ice_determine_promisc_mask
3435  * @fi: filter info to parse
3436  *
3437  * Helper function to determine which ICE_PROMISC_ mask corresponds
3438  * to given filter into.
3439  */
3440 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
3441 {
3442         u16 vid = fi->l_data.mac_vlan.vlan_id;
3443         u8 *macaddr = fi->l_data.mac.mac_addr;
3444         bool is_tx_fltr = false;
3445         u8 promisc_mask = 0;
3446
3447         if (fi->flag == ICE_FLTR_TX)
3448                 is_tx_fltr = true;
3449
3450         if (IS_BROADCAST_ETHER_ADDR(macaddr))
3451                 promisc_mask |= is_tx_fltr ?
3452                         ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
3453         else if (IS_MULTICAST_ETHER_ADDR(macaddr))
3454                 promisc_mask |= is_tx_fltr ?
3455                         ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
3456         else if (IS_UNICAST_ETHER_ADDR(macaddr))
3457                 promisc_mask |= is_tx_fltr ?
3458                         ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
3459         if (vid)
3460                 promisc_mask |= is_tx_fltr ?
3461                         ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
3462
3463         return promisc_mask;
3464 }
3465
3466 /**
3467  * ice_get_vsi_promisc - get promiscuous mode of given VSI
3468  * @hw: pointer to the hardware structure
3469  * @vsi_handle: VSI handle to retrieve info from
3470  * @promisc_mask: pointer to mask to be filled in
3471  * @vid: VLAN ID of promisc VLAN VSI
3472  */
3473 enum ice_status
3474 ice_get_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3475                     u16 *vid)
3476 {
3477         struct ice_switch_info *sw = hw->switch_info;
3478         struct ice_fltr_mgmt_list_entry *itr;
3479         struct LIST_HEAD_TYPE *rule_head;
3480         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3481
3482         if (!ice_is_vsi_valid(hw, vsi_handle))
3483                 return ICE_ERR_PARAM;
3484
3485         *vid = 0;
3486         *promisc_mask = 0;
3487         rule_head = &sw->recp_list[ICE_SW_LKUP_PROMISC].filt_rules;
3488         rule_lock = &sw->recp_list[ICE_SW_LKUP_PROMISC].filt_rule_lock;
3489
3490         ice_acquire_lock(rule_lock);
3491         LIST_FOR_EACH_ENTRY(itr, rule_head,
3492                             ice_fltr_mgmt_list_entry, list_entry) {
3493                 /* Continue if this filter doesn't apply to this VSI or the
3494                  * VSI ID is not in the VSI map for this filter
3495                  */
3496                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3497                         continue;
3498
3499                 *promisc_mask |= ice_determine_promisc_mask(&itr->fltr_info);
3500         }
3501         ice_release_lock(rule_lock);
3502
3503         return ICE_SUCCESS;
3504 }
3505
3506 /**
3507  * ice_get_vsi_vlan_promisc - get VLAN promiscuous mode of given VSI
3508  * @hw: pointer to the hardware structure
3509  * @vsi_handle: VSI handle to retrieve info from
3510  * @promisc_mask: pointer to mask to be filled in
3511  * @vid: VLAN ID of promisc VLAN VSI
3512  */
3513 enum ice_status
3514 ice_get_vsi_vlan_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3515                          u16 *vid)
3516 {
3517         struct ice_switch_info *sw = hw->switch_info;
3518         struct ice_fltr_mgmt_list_entry *itr;
3519         struct LIST_HEAD_TYPE *rule_head;
3520         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3521
3522         if (!ice_is_vsi_valid(hw, vsi_handle))
3523                 return ICE_ERR_PARAM;
3524
3525         *vid = 0;
3526         *promisc_mask = 0;
3527         rule_head = &sw->recp_list[ICE_SW_LKUP_PROMISC_VLAN].filt_rules;
3528         rule_lock = &sw->recp_list[ICE_SW_LKUP_PROMISC_VLAN].filt_rule_lock;
3529
3530         ice_acquire_lock(rule_lock);
3531         LIST_FOR_EACH_ENTRY(itr, rule_head, ice_fltr_mgmt_list_entry,
3532                             list_entry) {
3533                 /* Continue if this filter doesn't apply to this VSI or the
3534                  * VSI ID is not in the VSI map for this filter
3535                  */
3536                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3537                         continue;
3538
3539                 *promisc_mask |= ice_determine_promisc_mask(&itr->fltr_info);
3540         }
3541         ice_release_lock(rule_lock);
3542
3543         return ICE_SUCCESS;
3544 }
3545
3546 /**
3547  * ice_remove_promisc - Remove promisc based filter rules
3548  * @hw: pointer to the hardware structure
3549  * @recp_id: recipe ID for which the rule needs to removed
3550  * @v_list: list of promisc entries
3551  */
3552 static enum ice_status
3553 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
3554                    struct LIST_HEAD_TYPE *v_list)
3555 {
3556         struct ice_fltr_list_entry *v_list_itr, *tmp;
3557
3558         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
3559                                  list_entry) {
3560                 v_list_itr->status =
3561                         ice_remove_rule_internal(hw, recp_id, v_list_itr);
3562                 if (v_list_itr->status)
3563                         return v_list_itr->status;
3564         }
3565         return ICE_SUCCESS;
3566 }
3567
3568 /**
3569  * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
3570  * @hw: pointer to the hardware structure
3571  * @vsi_handle: VSI handle to clear mode
3572  * @promisc_mask: mask of promiscuous config bits to clear
3573  * @vid: VLAN ID to clear VLAN promiscuous
3574  */
3575 enum ice_status
3576 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3577                       u16 vid)
3578 {
3579         struct ice_switch_info *sw = hw->switch_info;
3580         struct ice_fltr_list_entry *fm_entry, *tmp;
3581         struct LIST_HEAD_TYPE remove_list_head;
3582         struct ice_fltr_mgmt_list_entry *itr;
3583         struct LIST_HEAD_TYPE *rule_head;
3584         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3585         enum ice_status status = ICE_SUCCESS;
3586         u8 recipe_id;
3587
3588         if (!ice_is_vsi_valid(hw, vsi_handle))
3589                 return ICE_ERR_PARAM;
3590
3591         if (vid)
3592                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3593         else
3594                 recipe_id = ICE_SW_LKUP_PROMISC;
3595
3596         rule_head = &sw->recp_list[recipe_id].filt_rules;
3597         rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
3598
3599         INIT_LIST_HEAD(&remove_list_head);
3600
3601         ice_acquire_lock(rule_lock);
3602         LIST_FOR_EACH_ENTRY(itr, rule_head,
3603                             ice_fltr_mgmt_list_entry, list_entry) {
3604                 u8 fltr_promisc_mask = 0;
3605
3606                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3607                         continue;
3608
3609                 fltr_promisc_mask |=
3610                         ice_determine_promisc_mask(&itr->fltr_info);
3611
3612                 /* Skip if filter is not completely specified by given mask */
3613                 if (fltr_promisc_mask & ~promisc_mask)
3614                         continue;
3615
3616                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3617                                                         &remove_list_head,
3618                                                         &itr->fltr_info);
3619                 if (status) {
3620                         ice_release_lock(rule_lock);
3621                         goto free_fltr_list;
3622                 }
3623         }
3624         ice_release_lock(rule_lock);
3625
3626         status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
3627
3628 free_fltr_list:
3629         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3630                                  ice_fltr_list_entry, list_entry) {
3631                 LIST_DEL(&fm_entry->list_entry);
3632                 ice_free(hw, fm_entry);
3633         }
3634
3635         return status;
3636 }
3637
3638 /**
3639  * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
3640  * @hw: pointer to the hardware structure
3641  * @vsi_handle: VSI handle to configure
3642  * @promisc_mask: mask of promiscuous config bits
3643  * @vid: VLAN ID to set VLAN promiscuous
3644  */
3645 enum ice_status
3646 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
3647 {
3648         enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
3649         struct ice_fltr_list_entry f_list_entry;
3650         struct ice_fltr_info new_fltr;
3651         enum ice_status status = ICE_SUCCESS;
3652         bool is_tx_fltr;
3653         u16 hw_vsi_id;
3654         int pkt_type;
3655         u8 recipe_id;
3656
3657         ice_debug(hw, ICE_DBG_TRACE, "ice_set_vsi_promisc\n");
3658
3659         if (!ice_is_vsi_valid(hw, vsi_handle))
3660                 return ICE_ERR_PARAM;
3661         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3662
3663         ice_memset(&new_fltr, 0, sizeof(new_fltr), ICE_NONDMA_MEM);
3664
3665         if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
3666                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
3667                 new_fltr.l_data.mac_vlan.vlan_id = vid;
3668                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3669         } else {
3670                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
3671                 recipe_id = ICE_SW_LKUP_PROMISC;
3672         }
3673
3674         /* Separate filters must be set for each direction/packet type
3675          * combination, so we will loop over the mask value, store the
3676          * individual type, and clear it out in the input mask as it
3677          * is found.
3678          */
3679         while (promisc_mask) {
3680                 u8 *mac_addr;
3681
3682                 pkt_type = 0;
3683                 is_tx_fltr = false;
3684
3685                 if (promisc_mask & ICE_PROMISC_UCAST_RX) {
3686                         promisc_mask &= ~ICE_PROMISC_UCAST_RX;
3687                         pkt_type = UCAST_FLTR;
3688                 } else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
3689                         promisc_mask &= ~ICE_PROMISC_UCAST_TX;
3690                         pkt_type = UCAST_FLTR;
3691                         is_tx_fltr = true;
3692                 } else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
3693                         promisc_mask &= ~ICE_PROMISC_MCAST_RX;
3694                         pkt_type = MCAST_FLTR;
3695                 } else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
3696                         promisc_mask &= ~ICE_PROMISC_MCAST_TX;
3697                         pkt_type = MCAST_FLTR;
3698                         is_tx_fltr = true;
3699                 } else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
3700                         promisc_mask &= ~ICE_PROMISC_BCAST_RX;
3701                         pkt_type = BCAST_FLTR;
3702                 } else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
3703                         promisc_mask &= ~ICE_PROMISC_BCAST_TX;
3704                         pkt_type = BCAST_FLTR;
3705                         is_tx_fltr = true;
3706                 }
3707
3708                 /* Check for VLAN promiscuous flag */
3709                 if (promisc_mask & ICE_PROMISC_VLAN_RX) {
3710                         promisc_mask &= ~ICE_PROMISC_VLAN_RX;
3711                 } else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
3712                         promisc_mask &= ~ICE_PROMISC_VLAN_TX;
3713                         is_tx_fltr = true;
3714                 }
3715
3716                 /* Set filter DA based on packet type */
3717                 mac_addr = new_fltr.l_data.mac.mac_addr;
3718                 if (pkt_type == BCAST_FLTR) {
3719                         ice_memset(mac_addr, 0xff, ETH_ALEN, ICE_NONDMA_MEM);
3720                 } else if (pkt_type == MCAST_FLTR ||
3721                            pkt_type == UCAST_FLTR) {
3722                         /* Use the dummy ether header DA */
3723                         ice_memcpy(mac_addr, dummy_eth_header, ETH_ALEN,
3724                                    ICE_NONDMA_TO_NONDMA);
3725                         if (pkt_type == MCAST_FLTR)
3726                                 mac_addr[0] |= 0x1;     /* Set multicast bit */
3727                 }
3728
3729                 /* Need to reset this to zero for all iterations */
3730                 new_fltr.flag = 0;
3731                 if (is_tx_fltr) {
3732                         new_fltr.flag |= ICE_FLTR_TX;
3733                         new_fltr.src = hw_vsi_id;
3734                 } else {
3735                         new_fltr.flag |= ICE_FLTR_RX;
3736                         new_fltr.src = hw->port_info->lport;
3737                 }
3738
3739                 new_fltr.fltr_act = ICE_FWD_TO_VSI;
3740                 new_fltr.vsi_handle = vsi_handle;
3741                 new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
3742                 f_list_entry.fltr_info = new_fltr;
3743
3744                 status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
3745                 if (status != ICE_SUCCESS)
3746                         goto set_promisc_exit;
3747         }
3748
3749 set_promisc_exit:
3750         return status;
3751 }
3752
3753 /**
3754  * ice_set_vlan_vsi_promisc
3755  * @hw: pointer to the hardware structure
3756  * @vsi_handle: VSI handle to configure
3757  * @promisc_mask: mask of promiscuous config bits
3758  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
3759  *
3760  * Configure VSI with all associated VLANs to given promiscuous mode(s)
3761  */
3762 enum ice_status
3763 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3764                          bool rm_vlan_promisc)
3765 {
3766         struct ice_switch_info *sw = hw->switch_info;
3767         struct ice_fltr_list_entry *list_itr, *tmp;
3768         struct LIST_HEAD_TYPE vsi_list_head;
3769         struct LIST_HEAD_TYPE *vlan_head;
3770         struct ice_lock *vlan_lock; /* Lock to protect filter rule list */
3771         enum ice_status status;
3772         u16 vlan_id;
3773
3774         INIT_LIST_HEAD(&vsi_list_head);
3775         vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3776         vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3777         ice_acquire_lock(vlan_lock);
3778         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
3779                                           &vsi_list_head);
3780         ice_release_lock(vlan_lock);
3781         if (status)
3782                 goto free_fltr_list;
3783
3784         LIST_FOR_EACH_ENTRY(list_itr, &vsi_list_head, ice_fltr_list_entry,
3785                             list_entry) {
3786                 vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
3787                 if (rm_vlan_promisc)
3788                         status = ice_clear_vsi_promisc(hw, vsi_handle,
3789                                                        promisc_mask, vlan_id);
3790                 else
3791                         status = ice_set_vsi_promisc(hw, vsi_handle,
3792                                                      promisc_mask, vlan_id);
3793                 if (status)
3794                         break;
3795         }
3796
3797 free_fltr_list:
3798         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, &vsi_list_head,
3799                                  ice_fltr_list_entry, list_entry) {
3800                 LIST_DEL(&list_itr->list_entry);
3801                 ice_free(hw, list_itr);
3802         }
3803         return status;
3804 }
3805
3806 /**
3807  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
3808  * @hw: pointer to the hardware structure
3809  * @vsi_handle: VSI handle to remove filters from
3810  * @lkup: switch rule filter lookup type
3811  */
3812 static void
3813 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
3814                          enum ice_sw_lkup_type lkup)
3815 {
3816         struct ice_switch_info *sw = hw->switch_info;
3817         struct ice_fltr_list_entry *fm_entry;
3818         struct LIST_HEAD_TYPE remove_list_head;
3819         struct LIST_HEAD_TYPE *rule_head;
3820         struct ice_fltr_list_entry *tmp;
3821         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3822         enum ice_status status;
3823
3824         INIT_LIST_HEAD(&remove_list_head);
3825         rule_lock = &sw->recp_list[lkup].filt_rule_lock;
3826         rule_head = &sw->recp_list[lkup].filt_rules;
3827         ice_acquire_lock(rule_lock);
3828         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
3829                                           &remove_list_head);
3830         ice_release_lock(rule_lock);
3831         if (status)
3832                 return;
3833
3834         switch (lkup) {
3835         case ICE_SW_LKUP_MAC:
3836                 ice_remove_mac(hw, &remove_list_head);
3837                 break;
3838         case ICE_SW_LKUP_VLAN:
3839                 ice_remove_vlan(hw, &remove_list_head);
3840                 break;
3841         case ICE_SW_LKUP_PROMISC:
3842         case ICE_SW_LKUP_PROMISC_VLAN:
3843                 ice_remove_promisc(hw, lkup, &remove_list_head);
3844                 break;
3845         case ICE_SW_LKUP_MAC_VLAN:
3846 #ifndef NO_MACVLAN_SUPPORT
3847                 ice_remove_mac_vlan(hw, &remove_list_head);
3848 #else
3849                 ice_debug(hw, ICE_DBG_SW, "MAC VLAN look up is not supported yet\n");
3850 #endif /* !NO_MACVLAN_SUPPORT */
3851                 break;
3852         case ICE_SW_LKUP_ETHERTYPE:
3853         case ICE_SW_LKUP_ETHERTYPE_MAC:
3854                 ice_remove_eth_mac(hw, &remove_list_head);
3855                 break;
3856         case ICE_SW_LKUP_DFLT:
3857                 ice_debug(hw, ICE_DBG_SW,
3858                           "Remove filters for this lookup type hasn't been implemented yet\n");
3859                 break;
3860         case ICE_SW_LKUP_LAST:
3861                 ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type\n");
3862                 break;
3863         }
3864
3865         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3866                                  ice_fltr_list_entry, list_entry) {
3867                 LIST_DEL(&fm_entry->list_entry);
3868                 ice_free(hw, fm_entry);
3869         }
3870 }
3871
3872 /**
3873  * ice_remove_vsi_fltr - Remove all filters for a VSI
3874  * @hw: pointer to the hardware structure
3875  * @vsi_handle: VSI handle to remove filters from
3876  */
3877 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
3878 {
3879         ice_debug(hw, ICE_DBG_TRACE, "ice_remove_vsi_fltr\n");
3880
3881         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
3882         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
3883         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
3884         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
3885         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
3886         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
3887         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
3888         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
3889 }
3890
3891 /**
3892  * ice_alloc_res_cntr - allocating resource counter
3893  * @hw: pointer to the hardware structure
3894  * @type: type of resource
3895  * @alloc_shared: if set it is shared else dedicated
3896  * @num_items: number of entries requested for FD resource type
3897  * @counter_id: counter index returned by AQ call
3898  */
3899 enum ice_status
3900 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3901                    u16 *counter_id)
3902 {
3903         struct ice_aqc_alloc_free_res_elem *buf;
3904         enum ice_status status;
3905         u16 buf_len;
3906
3907         /* Allocate resource */
3908         buf_len = sizeof(*buf);
3909         buf = (struct ice_aqc_alloc_free_res_elem *)
3910                 ice_malloc(hw, buf_len);
3911         if (!buf)
3912                 return ICE_ERR_NO_MEMORY;
3913
3914         buf->num_elems = CPU_TO_LE16(num_items);
3915         buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3916                                       ICE_AQC_RES_TYPE_M) | alloc_shared);
3917
3918         status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3919                                        ice_aqc_opc_alloc_res, NULL);
3920         if (status)
3921                 goto exit;
3922
3923         *counter_id = LE16_TO_CPU(buf->elem[0].e.sw_resp);
3924
3925 exit:
3926         ice_free(hw, buf);
3927         return status;
3928 }
3929
3930 /**
3931  * ice_free_res_cntr - free resource counter
3932  * @hw: pointer to the hardware structure
3933  * @type: type of resource
3934  * @alloc_shared: if set it is shared else dedicated
3935  * @num_items: number of entries to be freed for FD resource type
3936  * @counter_id: counter ID resource which needs to be freed
3937  */
3938 enum ice_status
3939 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3940                   u16 counter_id)
3941 {
3942         struct ice_aqc_alloc_free_res_elem *buf;
3943         enum ice_status status;
3944         u16 buf_len;
3945
3946         /* Free resource */
3947         buf_len = sizeof(*buf);
3948         buf = (struct ice_aqc_alloc_free_res_elem *)
3949                 ice_malloc(hw, buf_len);
3950         if (!buf)
3951                 return ICE_ERR_NO_MEMORY;
3952
3953         buf->num_elems = CPU_TO_LE16(num_items);
3954         buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3955                                       ICE_AQC_RES_TYPE_M) | alloc_shared);
3956         buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
3957
3958         status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3959                                        ice_aqc_opc_free_res, NULL);
3960         if (status)
3961                 ice_debug(hw, ICE_DBG_SW,
3962                           "counter resource could not be freed\n");
3963
3964         ice_free(hw, buf);
3965         return status;
3966 }
3967
3968 /**
3969  * ice_alloc_vlan_res_counter - obtain counter resource for VLAN type
3970  * @hw: pointer to the hardware structure
3971  * @counter_id: returns counter index
3972  */
3973 enum ice_status ice_alloc_vlan_res_counter(struct ice_hw *hw, u16 *counter_id)
3974 {
3975         return ice_alloc_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3976                                   ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3977                                   counter_id);
3978 }
3979
3980 /**
3981  * ice_free_vlan_res_counter - Free counter resource for VLAN type
3982  * @hw: pointer to the hardware structure
3983  * @counter_id: counter index to be freed
3984  */
3985 enum ice_status ice_free_vlan_res_counter(struct ice_hw *hw, u16 counter_id)
3986 {
3987         return ice_free_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3988                                  ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3989                                  counter_id);
3990 }
3991
3992 /**
3993  * ice_alloc_res_lg_act - add large action resource
3994  * @hw: pointer to the hardware structure
3995  * @l_id: large action ID to fill it in
3996  * @num_acts: number of actions to hold with a large action entry
3997  */
3998 static enum ice_status
3999 ice_alloc_res_lg_act(struct ice_hw *hw, u16 *l_id, u16 num_acts)
4000 {
4001         struct ice_aqc_alloc_free_res_elem *sw_buf;
4002         enum ice_status status;
4003         u16 buf_len;
4004
4005         if (num_acts > ICE_MAX_LG_ACT || num_acts == 0)
4006                 return ICE_ERR_PARAM;
4007
4008         /* Allocate resource for large action */
4009         buf_len = sizeof(*sw_buf);
4010         sw_buf = (struct ice_aqc_alloc_free_res_elem *)
4011                 ice_malloc(hw, buf_len);
4012         if (!sw_buf)
4013                 return ICE_ERR_NO_MEMORY;
4014
4015         sw_buf->num_elems = CPU_TO_LE16(1);
4016
4017         /* If num_acts is 1, use ICE_AQC_RES_TYPE_WIDE_TABLE_1.
4018          * If num_acts is 2, use ICE_AQC_RES_TYPE_WIDE_TABLE_3.
4019          * If num_acts is greater than 2, then use
4020          * ICE_AQC_RES_TYPE_WIDE_TABLE_4.
4021          * The num_acts cannot exceed 4. This was ensured at the
4022          * beginning of the function.
4023          */
4024         if (num_acts == 1)
4025                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_1);
4026         else if (num_acts == 2)
4027                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_2);
4028         else
4029                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_4);
4030
4031         status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
4032                                        ice_aqc_opc_alloc_res, NULL);
4033         if (!status)
4034                 *l_id = LE16_TO_CPU(sw_buf->elem[0].e.sw_resp);
4035
4036         ice_free(hw, sw_buf);
4037         return status;
4038 }
4039
4040 /**
4041  * ice_add_mac_with_sw_marker - add filter with sw marker
4042  * @hw: pointer to the hardware structure
4043  * @f_info: filter info structure containing the MAC filter information
4044  * @sw_marker: sw marker to tag the Rx descriptor with
4045  */
4046 enum ice_status
4047 ice_add_mac_with_sw_marker(struct ice_hw *hw, struct ice_fltr_info *f_info,
4048                            u16 sw_marker)
4049 {
4050         struct ice_switch_info *sw = hw->switch_info;
4051         struct ice_fltr_mgmt_list_entry *m_entry;
4052         struct ice_fltr_list_entry fl_info;
4053         struct LIST_HEAD_TYPE l_head;
4054         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
4055         enum ice_status ret;
4056         bool entry_exists;
4057         u16 lg_act_id;
4058
4059         if (f_info->fltr_act != ICE_FWD_TO_VSI)
4060                 return ICE_ERR_PARAM;
4061
4062         if (f_info->lkup_type != ICE_SW_LKUP_MAC)
4063                 return ICE_ERR_PARAM;
4064
4065         if (sw_marker == ICE_INVAL_SW_MARKER_ID)
4066                 return ICE_ERR_PARAM;
4067
4068         if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
4069                 return ICE_ERR_PARAM;
4070         f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
4071
4072         /* Add filter if it doesn't exist so then the adding of large
4073          * action always results in update
4074          */
4075
4076         INIT_LIST_HEAD(&l_head);
4077         fl_info.fltr_info = *f_info;
4078         LIST_ADD(&fl_info.list_entry, &l_head);
4079
4080         entry_exists = false;
4081         ret = ice_add_mac(hw, &l_head);
4082         if (ret == ICE_ERR_ALREADY_EXISTS)
4083                 entry_exists = true;
4084         else if (ret)
4085                 return ret;
4086
4087         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
4088         ice_acquire_lock(rule_lock);
4089         /* Get the book keeping entry for the filter */
4090         m_entry = ice_find_rule_entry(hw, ICE_SW_LKUP_MAC, f_info);
4091         if (!m_entry)
4092                 goto exit_error;
4093
4094         /* If counter action was enabled for this rule then don't enable
4095          * sw marker large action
4096          */
4097         if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
4098                 ret = ICE_ERR_PARAM;
4099                 goto exit_error;
4100         }
4101
4102         /* if same marker was added before */
4103         if (m_entry->sw_marker_id == sw_marker) {
4104                 ret = ICE_ERR_ALREADY_EXISTS;
4105                 goto exit_error;
4106         }
4107
4108         /* Allocate a hardware table entry to hold large act. Three actions
4109          * for marker based large action
4110          */
4111         ret = ice_alloc_res_lg_act(hw, &lg_act_id, 3);
4112         if (ret)
4113                 goto exit_error;
4114
4115         if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
4116                 goto exit_error;
4117
4118         /* Update the switch rule to add the marker action */
4119         ret = ice_add_marker_act(hw, m_entry, sw_marker, lg_act_id);
4120         if (!ret) {
4121                 ice_release_lock(rule_lock);
4122                 return ret;
4123         }
4124
4125 exit_error:
4126         ice_release_lock(rule_lock);
4127         /* only remove entry if it did not exist previously */
4128         if (!entry_exists)
4129                 ret = ice_remove_mac(hw, &l_head);
4130
4131         return ret;
4132 }
4133
4134 /**
4135  * ice_add_mac_with_counter - add filter with counter enabled
4136  * @hw: pointer to the hardware structure
4137  * @f_info: pointer to filter info structure containing the MAC filter
4138  *          information
4139  */
4140 enum ice_status
4141 ice_add_mac_with_counter(struct ice_hw *hw, struct ice_fltr_info *f_info)
4142 {
4143         struct ice_switch_info *sw = hw->switch_info;
4144         struct ice_fltr_mgmt_list_entry *m_entry;
4145         struct ice_fltr_list_entry fl_info;
4146         struct LIST_HEAD_TYPE l_head;
4147         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
4148         enum ice_status ret;
4149         bool entry_exist;
4150         u16 counter_id;
4151         u16 lg_act_id;
4152
4153         if (f_info->fltr_act != ICE_FWD_TO_VSI)
4154                 return ICE_ERR_PARAM;
4155
4156         if (f_info->lkup_type != ICE_SW_LKUP_MAC)
4157                 return ICE_ERR_PARAM;
4158
4159         if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
4160                 return ICE_ERR_PARAM;
4161         f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
4162
4163         entry_exist = false;
4164
4165         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
4166
4167         /* Add filter if it doesn't exist so then the adding of large
4168          * action always results in update
4169          */
4170         INIT_LIST_HEAD(&l_head);
4171
4172         fl_info.fltr_info = *f_info;
4173         LIST_ADD(&fl_info.list_entry, &l_head);
4174
4175         ret = ice_add_mac(hw, &l_head);
4176         if (ret == ICE_ERR_ALREADY_EXISTS)
4177                 entry_exist = true;
4178         else if (ret)
4179                 return ret;
4180
4181         ice_acquire_lock(rule_lock);
4182         m_entry = ice_find_rule_entry(hw, ICE_SW_LKUP_MAC, f_info);
4183         if (!m_entry) {
4184                 ret = ICE_ERR_BAD_PTR;
4185                 goto exit_error;
4186         }
4187
4188         /* Don't enable counter for a filter for which sw marker was enabled */
4189         if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID) {
4190                 ret = ICE_ERR_PARAM;
4191                 goto exit_error;
4192         }
4193
4194         /* If a counter was already enabled then don't need to add again */
4195         if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
4196                 ret = ICE_ERR_ALREADY_EXISTS;
4197                 goto exit_error;
4198         }
4199
4200         /* Allocate a hardware table entry to VLAN counter */
4201         ret = ice_alloc_vlan_res_counter(hw, &counter_id);
4202         if (ret)
4203                 goto exit_error;
4204
4205         /* Allocate a hardware table entry to hold large act. Two actions for
4206          * counter based large action
4207          */
4208         ret = ice_alloc_res_lg_act(hw, &lg_act_id, 2);
4209         if (ret)
4210                 goto exit_error;
4211
4212         if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
4213                 goto exit_error;
4214
4215         /* Update the switch rule to add the counter action */
4216         ret = ice_add_counter_act(hw, m_entry, counter_id, lg_act_id);
4217         if (!ret) {
4218                 ice_release_lock(rule_lock);
4219                 return ret;
4220         }
4221
4222 exit_error:
4223         ice_release_lock(rule_lock);
4224         /* only remove entry if it did not exist previously */
4225         if (!entry_exist)
4226                 ret = ice_remove_mac(hw, &l_head);
4227
4228         return ret;
4229 }
4230
4231 /* This is mapping table entry that maps every word within a given protocol
4232  * structure to the real byte offset as per the specification of that
4233  * protocol header.
4234  * for example dst address is 3 words in ethertype header and corresponding
4235  * bytes are 0, 2, 3 in the actual packet header and src address is at 4, 6, 8
4236  * IMPORTANT: Every structure part of "ice_prot_hdr" union should have a
4237  * matching entry describing its field. This needs to be updated if new
4238  * structure is added to that union.
4239  */
4240 static const struct ice_prot_ext_tbl_entry ice_prot_ext[] = {
4241         { ICE_MAC_OFOS,         { 0, 2, 4, 6, 8, 10, 12 } },
4242         { ICE_MAC_IL,           { 0, 2, 4, 6, 8, 10, 12 } },
4243         { ICE_IPV4_OFOS,        { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 } },
4244         { ICE_IPV4_IL,          { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 } },
4245         { ICE_IPV6_IL,          { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
4246                                  26, 28, 30, 32, 34, 36, 38 } },
4247         { ICE_IPV6_OFOS,        { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
4248                                  26, 28, 30, 32, 34, 36, 38 } },
4249         { ICE_TCP_IL,           { 0, 2 } },
4250         { ICE_UDP_ILOS,         { 0, 2 } },
4251         { ICE_SCTP_IL,          { 0, 2 } },
4252         { ICE_VXLAN,            { 8, 10, 12 } },
4253         { ICE_GENEVE,           { 8, 10, 12 } },
4254         { ICE_VXLAN_GPE,        { 0, 2, 4 } },
4255         { ICE_NVGRE,            { 0, 2 } },
4256         { ICE_PROTOCOL_LAST,    { 0 } }
4257 };
4258
4259 /* The following table describes preferred grouping of recipes.
4260  * If a recipe that needs to be programmed is a superset or matches one of the
4261  * following combinations, then the recipe needs to be chained as per the
4262  * following policy.
4263  */
4264 static const struct ice_pref_recipe_group ice_recipe_pack[] = {
4265         {3, { { ICE_MAC_OFOS_HW, 0, 0 }, { ICE_MAC_OFOS_HW, 2, 0 },
4266               { ICE_MAC_OFOS_HW, 4, 0 } } },
4267         {4, { { ICE_MAC_IL_HW, 0, 0 }, { ICE_MAC_IL_HW, 2, 0 },
4268               { ICE_MAC_IL_HW, 4, 0 }, { ICE_META_DATA_ID_HW, 44, 0 } } },
4269         {2, { { ICE_IPV4_IL_HW, 0, 0 }, { ICE_IPV4_IL_HW, 2, 0 } } },
4270         {2, { { ICE_IPV4_IL_HW, 12, 0 }, { ICE_IPV4_IL_HW, 14, 0 } } },
4271 };
4272
4273 static const struct ice_protocol_entry ice_prot_id_tbl[] = {
4274         { ICE_MAC_OFOS,         ICE_MAC_OFOS_HW },
4275         { ICE_MAC_IL,           ICE_MAC_IL_HW },
4276         { ICE_IPV4_OFOS,        ICE_IPV4_OFOS_HW },
4277         { ICE_IPV4_IL,          ICE_IPV4_IL_HW },
4278         { ICE_IPV6_OFOS,        ICE_IPV6_OFOS_HW },
4279         { ICE_IPV6_IL,          ICE_IPV6_IL_HW },
4280         { ICE_TCP_IL,           ICE_TCP_IL_HW },
4281         { ICE_UDP_ILOS,         ICE_UDP_ILOS_HW },
4282         { ICE_SCTP_IL,          ICE_SCTP_IL_HW },
4283         { ICE_VXLAN,            ICE_UDP_OF_HW },
4284         { ICE_GENEVE,           ICE_UDP_OF_HW },
4285         { ICE_VXLAN_GPE,        ICE_UDP_OF_HW },
4286         { ICE_NVGRE,            ICE_GRE_OF_HW },
4287         { ICE_PROTOCOL_LAST,    0 }
4288 };
4289
4290 /**
4291  * ice_find_recp - find a recipe
4292  * @hw: pointer to the hardware structure
4293  * @lkup_exts: extension sequence to match
4294  *
4295  * Returns index of matching recipe, or ICE_MAX_NUM_RECIPES if not found.
4296  */
4297 static u16 ice_find_recp(struct ice_hw *hw, struct ice_prot_lkup_ext *lkup_exts)
4298 {
4299         struct ice_sw_recipe *recp;
4300         u16 i;
4301
4302         ice_get_recp_to_prof_map(hw);
4303         /* Initialize available_result_ids which tracks available result idx */
4304         for (i = 0; i <= ICE_CHAIN_FV_INDEX_START; i++)
4305                 ice_set_bit(ICE_CHAIN_FV_INDEX_START - i,
4306                             available_result_ids);
4307
4308         /* Walk through existing recipes to find a match */
4309         recp = hw->switch_info->recp_list;
4310         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4311                 /* If recipe was not created for this ID, in SW bookkeeping,
4312                  * check if FW has an entry for this recipe. If the FW has an
4313                  * entry update it in our SW bookkeeping and continue with the
4314                  * matching.
4315                  */
4316                 if (!recp[i].recp_created)
4317                         if (ice_get_recp_frm_fw(hw,
4318                                                 hw->switch_info->recp_list, i))
4319                                 continue;
4320
4321                 /* if number of words we are looking for match */
4322                 if (lkup_exts->n_val_words == recp[i].lkup_exts.n_val_words) {
4323                         struct ice_fv_word *a = lkup_exts->fv_words;
4324                         struct ice_fv_word *b = recp[i].lkup_exts.fv_words;
4325                         bool found = true;
4326                         u8 p, q;
4327
4328                         for (p = 0; p < lkup_exts->n_val_words; p++) {
4329                                 for (q = 0; q < recp[i].lkup_exts.n_val_words;
4330                                      q++) {
4331                                         if (a[p].off == b[q].off &&
4332                                             a[p].prot_id == b[q].prot_id)
4333                                                 /* Found the "p"th word in the
4334                                                  * given recipe
4335                                                  */
4336                                                 break;
4337                                 }
4338                                 /* After walking through all the words in the
4339                                  * "i"th recipe if "p"th word was not found then
4340                                  * this recipe is not what we are looking for.
4341                                  * So break out from this loop and try the next
4342                                  * recipe
4343                                  */
4344                                 if (q >= recp[i].lkup_exts.n_val_words) {
4345                                         found = false;
4346                                         break;
4347                                 }
4348                         }
4349                         /* If for "i"th recipe the found was never set to false
4350                          * then it means we found our match
4351                          */
4352                         if (found)
4353                                 return i; /* Return the recipe ID */
4354                 }
4355         }
4356         return ICE_MAX_NUM_RECIPES;
4357 }
4358
4359 /**
4360  * ice_prot_type_to_id - get protocol ID from protocol type
4361  * @type: protocol type
4362  * @id: pointer to variable that will receive the ID
4363  *
4364  * Returns true if found, false otherwise
4365  */
4366 static bool ice_prot_type_to_id(enum ice_protocol_type type, u16 *id)
4367 {
4368         u16 i;
4369
4370         for (i = 0; ice_prot_id_tbl[i].type != ICE_PROTOCOL_LAST; i++)
4371                 if (ice_prot_id_tbl[i].type == type) {
4372                         *id = ice_prot_id_tbl[i].protocol_id;
4373                         return true;
4374                 }
4375         return false;
4376 }
4377
4378 /**
4379  * ice_find_valid_words - count valid words
4380  * @rule: advanced rule with lookup information
4381  * @lkup_exts: byte offset extractions of the words that are valid
4382  *
4383  * calculate valid words in a lookup rule using mask value
4384  */
4385 static u16
4386 ice_fill_valid_words(struct ice_adv_lkup_elem *rule,
4387                      struct ice_prot_lkup_ext *lkup_exts)
4388 {
4389         u16 j, word = 0;
4390         u16 prot_id;
4391         u16 ret_val;
4392
4393         if (!ice_prot_type_to_id(rule->type, &prot_id))
4394                 return 0;
4395
4396         word = lkup_exts->n_val_words;
4397
4398         for (j = 0; j < sizeof(rule->m_u) / sizeof(u16); j++)
4399                 if (((u16 *)&rule->m_u)[j] == 0xffff &&
4400                     rule->type < ARRAY_SIZE(ice_prot_ext)) {
4401                         /* No more space to accommodate */
4402                         if (word >= ICE_MAX_CHAIN_WORDS)
4403                                 return 0;
4404                         lkup_exts->fv_words[word].off =
4405                                 ice_prot_ext[rule->type].offs[j];
4406                         lkup_exts->fv_words[word].prot_id =
4407                                 ice_prot_id_tbl[rule->type].protocol_id;
4408                         word++;
4409                 }
4410
4411         ret_val = word - lkup_exts->n_val_words;
4412         lkup_exts->n_val_words = word;
4413
4414         return ret_val;
4415 }
4416
4417 /**
4418  * ice_find_prot_off_ind - check for specific ID and offset in rule
4419  * @lkup_exts: an array of protocol header extractions
4420  * @prot_type: protocol type to check
4421  * @off: expected offset of the extraction
4422  *
4423  * Check if the prot_ext has given protocol ID and offset
4424  */
4425 static u8
4426 ice_find_prot_off_ind(struct ice_prot_lkup_ext *lkup_exts, u8 prot_type,
4427                       u16 off)
4428 {
4429         u8 j;
4430
4431         for (j = 0; j < lkup_exts->n_val_words; j++)
4432                 if (lkup_exts->fv_words[j].off == off &&
4433                     lkup_exts->fv_words[j].prot_id == prot_type)
4434                         return j;
4435
4436         return ICE_MAX_CHAIN_WORDS;
4437 }
4438
4439 /**
4440  * ice_is_recipe_subset - check if recipe group policy is a subset of lookup
4441  * @lkup_exts: an array of protocol header extractions
4442  * @r_policy: preferred recipe grouping policy
4443  *
4444  * Helper function to check if given recipe group is subset we need to check if
4445  * all the words described by the given recipe group exist in the advanced rule
4446  * look up information
4447  */
4448 static bool
4449 ice_is_recipe_subset(struct ice_prot_lkup_ext *lkup_exts,
4450                      const struct ice_pref_recipe_group *r_policy)
4451 {
4452         u8 ind[ICE_NUM_WORDS_RECIPE];
4453         u8 count = 0;
4454         u8 i;
4455
4456         /* check if everything in the r_policy is part of the entire rule */
4457         for (i = 0; i < r_policy->n_val_pairs; i++) {
4458                 u8 j;
4459
4460                 j = ice_find_prot_off_ind(lkup_exts, r_policy->pairs[i].prot_id,
4461                                           r_policy->pairs[i].off);
4462                 if (j >= ICE_MAX_CHAIN_WORDS)
4463                         return false;
4464
4465                 /* store the indexes temporarily found by the find function
4466                  * this will be used to mark the words as 'done'
4467                  */
4468                 ind[count++] = j;
4469         }
4470
4471         /* If the entire policy recipe was a true match, then mark the fields
4472          * that are covered by the recipe as 'done' meaning that these words
4473          * will be clumped together in one recipe.
4474          * "Done" here means in our searching if certain recipe group
4475          * matches or is subset of the given rule, then we mark all
4476          * the corresponding offsets as found. So the remaining recipes should
4477          * be created with whatever words that were left.
4478          */
4479         for (i = 0; i < count; i++) {
4480                 u8 in = ind[i];
4481
4482                 ice_set_bit(in, lkup_exts->done);
4483         }
4484         return true;
4485 }
4486
4487 /**
4488  * ice_create_first_fit_recp_def - Create a recipe grouping
4489  * @hw: pointer to the hardware structure
4490  * @lkup_exts: an array of protocol header extractions
4491  * @rg_list: pointer to a list that stores new recipe groups
4492  * @recp_cnt: pointer to a variable that stores returned number of recipe groups
4493  *
4494  * Using first fit algorithm, take all the words that are still not done
4495  * and start grouping them in 4-word groups. Each group makes up one
4496  * recipe.
4497  */
4498 static enum ice_status
4499 ice_create_first_fit_recp_def(struct ice_hw *hw,
4500                               struct ice_prot_lkup_ext *lkup_exts,
4501                               struct LIST_HEAD_TYPE *rg_list,
4502                               u8 *recp_cnt)
4503 {
4504         struct ice_pref_recipe_group *grp = NULL;
4505         u8 j;
4506
4507         *recp_cnt = 0;
4508
4509         /* Walk through every word in the rule to check if it is not done. If so
4510          * then this word needs to be part of a new recipe.
4511          */
4512         for (j = 0; j < lkup_exts->n_val_words; j++)
4513                 if (!ice_is_bit_set(lkup_exts->done, j)) {
4514                         if (!grp ||
4515                             grp->n_val_pairs == ICE_NUM_WORDS_RECIPE) {
4516                                 struct ice_recp_grp_entry *entry;
4517
4518                                 entry = (struct ice_recp_grp_entry *)
4519                                         ice_malloc(hw, sizeof(*entry));
4520                                 if (!entry)
4521                                         return ICE_ERR_NO_MEMORY;
4522                                 LIST_ADD(&entry->l_entry, rg_list);
4523                                 grp = &entry->r_group;
4524                                 (*recp_cnt)++;
4525                         }
4526
4527                         grp->pairs[grp->n_val_pairs].prot_id =
4528                                 lkup_exts->fv_words[j].prot_id;
4529                         grp->pairs[grp->n_val_pairs].off =
4530                                 lkup_exts->fv_words[j].off;
4531                         grp->n_val_pairs++;
4532                 }
4533
4534         return ICE_SUCCESS;
4535 }
4536
4537 /**
4538  * ice_fill_fv_word_index - fill in the field vector indices for a recipe group
4539  * @hw: pointer to the hardware structure
4540  * @fv_list: field vector with the extraction sequence information
4541  * @rg_list: recipe groupings with protocol-offset pairs
4542  *
4543  * Helper function to fill in the field vector indices for protocol-offset
4544  * pairs. These indexes are then ultimately programmed into a recipe.
4545  */
4546 static void
4547 ice_fill_fv_word_index(struct ice_hw *hw, struct LIST_HEAD_TYPE *fv_list,
4548                        struct LIST_HEAD_TYPE *rg_list)
4549 {
4550         struct ice_sw_fv_list_entry *fv;
4551         struct ice_recp_grp_entry *rg;
4552         struct ice_fv_word *fv_ext;
4553
4554         if (LIST_EMPTY(fv_list))
4555                 return;
4556
4557         fv = LIST_FIRST_ENTRY(fv_list, struct ice_sw_fv_list_entry, list_entry);
4558         fv_ext = fv->fv_ptr->ew;
4559
4560         LIST_FOR_EACH_ENTRY(rg, rg_list, ice_recp_grp_entry, l_entry) {
4561                 u8 i;
4562
4563                 for (i = 0; i < rg->r_group.n_val_pairs; i++) {
4564                         struct ice_fv_word *pr;
4565                         u8 j;
4566
4567                         pr = &rg->r_group.pairs[i];
4568                         for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
4569                                 if (fv_ext[j].prot_id == pr->prot_id &&
4570                                     fv_ext[j].off == pr->off) {
4571                                         /* Store index of field vector */
4572                                         rg->fv_idx[i] = j;
4573                                         break;
4574                                 }
4575                 }
4576         }
4577 }
4578
4579 /**
4580  * ice_add_sw_recipe - function to call AQ calls to create switch recipe
4581  * @hw: pointer to hardware structure
4582  * @rm: recipe management list entry
4583  * @match_tun: if field vector index for tunnel needs to be programmed
4584  */
4585 static enum ice_status
4586 ice_add_sw_recipe(struct ice_hw *hw, struct ice_sw_recipe *rm,
4587                   bool match_tun)
4588 {
4589         struct ice_aqc_recipe_data_elem *tmp;
4590         struct ice_aqc_recipe_data_elem *buf;
4591         struct ice_recp_grp_entry *entry;
4592         enum ice_status status;
4593         u16 recipe_count;
4594         u8 chain_idx;
4595         u8 recps = 0;
4596
4597         /* When more than one recipe are required, another recipe is needed to
4598          * chain them together. Matching a tunnel metadata ID takes up one of
4599          * the match fields in the chaining recipe reducing the number of
4600          * chained recipes by one.
4601          */
4602         if (rm->n_grp_count > 1)
4603                 rm->n_grp_count++;
4604         if (rm->n_grp_count > ICE_MAX_CHAIN_RECIPE ||
4605             (match_tun && rm->n_grp_count > (ICE_MAX_CHAIN_RECIPE - 1)))
4606                 return ICE_ERR_MAX_LIMIT;
4607
4608         tmp = (struct ice_aqc_recipe_data_elem *)ice_calloc(hw,
4609                                                             ICE_MAX_NUM_RECIPES,
4610                                                             sizeof(*tmp));
4611         if (!tmp)
4612                 return ICE_ERR_NO_MEMORY;
4613
4614         buf = (struct ice_aqc_recipe_data_elem *)
4615                 ice_calloc(hw, rm->n_grp_count, sizeof(*buf));
4616         if (!buf) {
4617                 status = ICE_ERR_NO_MEMORY;
4618                 goto err_mem;
4619         }
4620
4621         ice_zero_bitmap(rm->r_bitmap, ICE_MAX_NUM_RECIPES);
4622         recipe_count = ICE_MAX_NUM_RECIPES;
4623         status = ice_aq_get_recipe(hw, tmp, &recipe_count, ICE_SW_LKUP_MAC,
4624                                    NULL);
4625         if (status || recipe_count == 0)
4626                 goto err_unroll;
4627
4628         /* Allocate the recipe resources, and configure them according to the
4629          * match fields from protocol headers and extracted field vectors.
4630          */
4631         chain_idx = ICE_CHAIN_FV_INDEX_START -
4632                 ice_find_first_bit(available_result_ids,
4633                                    ICE_CHAIN_FV_INDEX_START + 1);
4634         LIST_FOR_EACH_ENTRY(entry, &rm->rg_list, ice_recp_grp_entry, l_entry) {
4635                 u8 i;
4636
4637                 status = ice_alloc_recipe(hw, &entry->rid);
4638                 if (status)
4639                         goto err_unroll;
4640
4641                 /* Clear the result index of the located recipe, as this will be
4642                  * updated, if needed, later in the recipe creation process.
4643                  */
4644                 tmp[0].content.result_indx = 0;
4645
4646                 buf[recps] = tmp[0];
4647                 buf[recps].recipe_indx = (u8)entry->rid;
4648                 /* if the recipe is a non-root recipe RID should be programmed
4649                  * as 0 for the rules to be applied correctly.
4650                  */
4651                 buf[recps].content.rid = 0;
4652                 ice_memset(&buf[recps].content.lkup_indx, 0,
4653                            sizeof(buf[recps].content.lkup_indx),
4654                            ICE_NONDMA_MEM);
4655
4656                 /* All recipes use look-up index 0 to match switch ID. */
4657                 buf[recps].content.lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
4658                 buf[recps].content.mask[0] =
4659                         CPU_TO_LE16(ICE_AQ_SW_ID_LKUP_MASK);
4660                 /* Setup lkup_indx 1..4 to INVALID/ignore and set the mask
4661                  * to be 0
4662                  */
4663                 for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
4664                         buf[recps].content.lkup_indx[i] = 0x80;
4665                         buf[recps].content.mask[i] = 0;
4666                 }
4667
4668                 for (i = 0; i < entry->r_group.n_val_pairs; i++) {
4669                         buf[recps].content.lkup_indx[i + 1] = entry->fv_idx[i];
4670                         buf[recps].content.mask[i + 1] = CPU_TO_LE16(0xFFFF);
4671                 }
4672
4673                 if (rm->n_grp_count > 1) {
4674                         entry->chain_idx = chain_idx;
4675                         buf[recps].content.result_indx =
4676                                 ICE_AQ_RECIPE_RESULT_EN |
4677                                 ((chain_idx << ICE_AQ_RECIPE_RESULT_DATA_S) &
4678                                  ICE_AQ_RECIPE_RESULT_DATA_M);
4679                         ice_clear_bit(ICE_CHAIN_FV_INDEX_START - chain_idx,
4680                                       available_result_ids);
4681                         chain_idx = ICE_CHAIN_FV_INDEX_START -
4682                                 ice_find_first_bit(available_result_ids,
4683                                                    ICE_CHAIN_FV_INDEX_START +
4684                                                    1);
4685                 }
4686
4687                 /* fill recipe dependencies */
4688                 ice_zero_bitmap((ice_bitmap_t *)buf[recps].recipe_bitmap,
4689                                 ICE_MAX_NUM_RECIPES);
4690                 ice_set_bit(buf[recps].recipe_indx,
4691                             (ice_bitmap_t *)buf[recps].recipe_bitmap);
4692                 buf[recps].content.act_ctrl_fwd_priority = rm->priority;
4693                 recps++;
4694         }
4695
4696         if (rm->n_grp_count == 1) {
4697                 rm->root_rid = buf[0].recipe_indx;
4698                 ice_set_bit(buf[0].recipe_indx, rm->r_bitmap);
4699                 buf[0].content.rid = rm->root_rid | ICE_AQ_RECIPE_ID_IS_ROOT;
4700                 if (sizeof(buf[0].recipe_bitmap) >= sizeof(rm->r_bitmap)) {
4701                         ice_memcpy(buf[0].recipe_bitmap, rm->r_bitmap,
4702                                    sizeof(buf[0].recipe_bitmap),
4703                                    ICE_NONDMA_TO_NONDMA);
4704                 } else {
4705                         status = ICE_ERR_BAD_PTR;
4706                         goto err_unroll;
4707                 }
4708                 /* Applicable only for ROOT_RECIPE, set the fwd_priority for
4709                  * the recipe which is getting created if specified
4710                  * by user. Usually any advanced switch filter, which results
4711                  * into new extraction sequence, ended up creating a new recipe
4712                  * of type ROOT and usually recipes are associated with profiles
4713                  * Switch rule referreing newly created recipe, needs to have
4714                  * either/or 'fwd' or 'join' priority, otherwise switch rule
4715                  * evaluation will not happen correctly. In other words, if
4716                  * switch rule to be evaluated on priority basis, then recipe
4717                  * needs to have priority, otherwise it will be evaluated last.
4718                  */
4719                 buf[0].content.act_ctrl_fwd_priority = rm->priority;
4720         } else {
4721                 struct ice_recp_grp_entry *last_chain_entry;
4722                 u16 rid, i;
4723
4724                 /* Allocate the last recipe that will chain the outcomes of the
4725                  * other recipes together
4726                  */
4727                 status = ice_alloc_recipe(hw, &rid);
4728                 if (status)
4729                         goto err_unroll;
4730
4731                 buf[recps].recipe_indx = (u8)rid;
4732                 buf[recps].content.rid = (u8)rid;
4733                 buf[recps].content.rid |= ICE_AQ_RECIPE_ID_IS_ROOT;
4734                 /* the new entry created should also be part of rg_list to
4735                  * make sure we have complete recipe
4736                  */
4737                 last_chain_entry = (struct ice_recp_grp_entry *)ice_malloc(hw,
4738                         sizeof(*last_chain_entry));
4739                 if (!last_chain_entry) {
4740                         status = ICE_ERR_NO_MEMORY;
4741                         goto err_unroll;
4742                 }
4743                 last_chain_entry->rid = rid;
4744                 ice_memset(&buf[recps].content.lkup_indx, 0,
4745                            sizeof(buf[recps].content.lkup_indx),
4746                            ICE_NONDMA_MEM);
4747                 /* All recipes use look-up index 0 to match switch ID. */
4748                 buf[recps].content.lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
4749                 buf[recps].content.mask[0] =
4750                         CPU_TO_LE16(ICE_AQ_SW_ID_LKUP_MASK);
4751                 for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
4752                         buf[recps].content.lkup_indx[i] =
4753                                 ICE_AQ_RECIPE_LKUP_IGNORE;
4754                         buf[recps].content.mask[i] = 0;
4755                 }
4756
4757                 i = 1;
4758                 /* update r_bitmap with the recp that is used for chaining */
4759                 ice_set_bit(rid, rm->r_bitmap);
4760                 /* this is the recipe that chains all the other recipes so it
4761                  * should not have a chaining ID to indicate the same
4762                  */
4763                 last_chain_entry->chain_idx = ICE_INVAL_CHAIN_IND;
4764                 LIST_FOR_EACH_ENTRY(entry, &rm->rg_list, ice_recp_grp_entry,
4765                                     l_entry) {
4766                         last_chain_entry->fv_idx[i] = entry->chain_idx;
4767                         buf[recps].content.lkup_indx[i] = entry->chain_idx;
4768                         buf[recps].content.mask[i++] = CPU_TO_LE16(0xFFFF);
4769                         ice_set_bit(entry->rid, rm->r_bitmap);
4770                 }
4771                 LIST_ADD(&last_chain_entry->l_entry, &rm->rg_list);
4772                 if (sizeof(buf[recps].recipe_bitmap) >=
4773                     sizeof(rm->r_bitmap)) {
4774                         ice_memcpy(buf[recps].recipe_bitmap, rm->r_bitmap,
4775                                    sizeof(buf[recps].recipe_bitmap),
4776                                    ICE_NONDMA_TO_NONDMA);
4777                 } else {
4778                         status = ICE_ERR_BAD_PTR;
4779                         goto err_unroll;
4780                 }
4781                 buf[recps].content.act_ctrl_fwd_priority = rm->priority;
4782
4783                 /* To differentiate among different UDP tunnels, a meta data ID
4784                  * flag is used.
4785                  */
4786                 if (match_tun) {
4787                         buf[recps].content.lkup_indx[i] = ICE_TUN_FLAG_FV_IND;
4788                         buf[recps].content.mask[i] =
4789                                 CPU_TO_LE16(ICE_TUN_FLAG_MASK);
4790                 }
4791
4792                 recps++;
4793                 rm->root_rid = (u8)rid;
4794         }
4795         status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
4796         if (status)
4797                 goto err_unroll;
4798
4799         status = ice_aq_add_recipe(hw, buf, rm->n_grp_count, NULL);
4800         ice_release_change_lock(hw);
4801         if (status)
4802                 goto err_unroll;
4803
4804         /* Every recipe that just got created add it to the recipe
4805          * book keeping list
4806          */
4807         LIST_FOR_EACH_ENTRY(entry, &rm->rg_list, ice_recp_grp_entry, l_entry) {
4808                 struct ice_switch_info *sw = hw->switch_info;
4809                 struct ice_sw_recipe *recp;
4810
4811                 recp = &sw->recp_list[entry->rid];
4812                 recp->root_rid = entry->rid;
4813                 ice_memcpy(&recp->ext_words, entry->r_group.pairs,
4814                            entry->r_group.n_val_pairs *
4815                            sizeof(struct ice_fv_word),
4816                            ICE_NONDMA_TO_NONDMA);
4817
4818                 recp->n_ext_words = entry->r_group.n_val_pairs;
4819                 recp->chain_idx = entry->chain_idx;
4820                 recp->recp_created = true;
4821                 recp->big_recp = false;
4822         }
4823         rm->root_buf = buf;
4824         ice_free(hw, tmp);
4825         return status;
4826
4827 err_unroll:
4828 err_mem:
4829         ice_free(hw, tmp);
4830         ice_free(hw, buf);
4831         return status;
4832 }
4833
4834 /**
4835  * ice_create_recipe_group - creates recipe group
4836  * @hw: pointer to hardware structure
4837  * @rm: recipe management list entry
4838  * @lkup_exts: lookup elements
4839  */
4840 static enum ice_status
4841 ice_create_recipe_group(struct ice_hw *hw, struct ice_sw_recipe *rm,
4842                         struct ice_prot_lkup_ext *lkup_exts)
4843 {
4844         struct ice_recp_grp_entry *entry;
4845         struct ice_recp_grp_entry *tmp;
4846         enum ice_status status;
4847         u8 recp_count = 0;
4848         u16 groups, i;
4849
4850         rm->n_grp_count = 0;
4851
4852         /* Each switch recipe can match up to 5 words or metadata. One word in
4853          * each recipe is used to match the switch ID. Four words are left for
4854          * matching other values. If the new advanced recipe requires more than
4855          * 4 words, it needs to be split into multiple recipes which are chained
4856          * together using the intermediate result that each produces as input to
4857          * the other recipes in the sequence.
4858          */
4859         groups = ARRAY_SIZE(ice_recipe_pack);
4860
4861         /* Check if any of the preferred recipes from the grouping policy
4862          * matches.
4863          */
4864         for (i = 0; i < groups; i++)
4865                 /* Check if the recipe from the preferred grouping matches
4866                  * or is a subset of the fields that needs to be looked up.
4867                  */
4868                 if (ice_is_recipe_subset(lkup_exts, &ice_recipe_pack[i])) {
4869                         /* This recipe can be used by itself or grouped with
4870                          * other recipes.
4871                          */
4872                         entry = (struct ice_recp_grp_entry *)
4873                                 ice_malloc(hw, sizeof(*entry));
4874                         if (!entry) {
4875                                 status = ICE_ERR_NO_MEMORY;
4876                                 goto err_unroll;
4877                         }
4878                         entry->r_group = ice_recipe_pack[i];
4879                         LIST_ADD(&entry->l_entry, &rm->rg_list);
4880                         rm->n_grp_count++;
4881                 }
4882
4883         /* Create recipes for words that are marked not done by packing them
4884          * as best fit.
4885          */
4886         status = ice_create_first_fit_recp_def(hw, lkup_exts,
4887                                                &rm->rg_list, &recp_count);
4888         if (!status) {
4889                 rm->n_grp_count += recp_count;
4890                 rm->n_ext_words = lkup_exts->n_val_words;
4891                 ice_memcpy(&rm->ext_words, lkup_exts->fv_words,
4892                            sizeof(rm->ext_words), ICE_NONDMA_TO_NONDMA);
4893                 goto out;
4894         }
4895
4896 err_unroll:
4897         LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, &rm->rg_list, ice_recp_grp_entry,
4898                                  l_entry) {
4899                 LIST_DEL(&entry->l_entry);
4900                 ice_free(hw, entry);
4901         }
4902
4903 out:
4904         return status;
4905 }
4906
4907 /**
4908  * ice_get_fv - get field vectors/extraction sequences for spec. lookup types
4909  * @hw: pointer to hardware structure
4910  * @lkups: lookup elements or match criteria for the advanced recipe, one
4911  *         structure per protocol header
4912  * @lkups_cnt: number of protocols
4913  * @fv_list: pointer to a list that holds the returned field vectors
4914  */
4915 static enum ice_status
4916 ice_get_fv(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
4917            struct LIST_HEAD_TYPE *fv_list)
4918 {
4919         enum ice_status status;
4920         u16 *prot_ids;
4921         u16 i;
4922
4923         prot_ids = (u16 *)ice_calloc(hw, lkups_cnt, sizeof(*prot_ids));
4924         if (!prot_ids)
4925                 return ICE_ERR_NO_MEMORY;
4926
4927         for (i = 0; i < lkups_cnt; i++)
4928                 if (!ice_prot_type_to_id(lkups[i].type, &prot_ids[i])) {
4929                         status = ICE_ERR_CFG;
4930                         goto free_mem;
4931                 }
4932
4933         /* Find field vectors that include all specified protocol types */
4934         status = ice_get_sw_fv_list(hw, prot_ids, lkups_cnt, fv_list);
4935
4936 free_mem:
4937         ice_free(hw, prot_ids);
4938         return status;
4939 }
4940
4941 /**
4942  * ice_add_adv_recipe - Add an advanced recipe that is not part of the default
4943  * @hw: pointer to hardware structure
4944  * @lkups: lookup elements or match criteria for the advanced recipe, one
4945  *  structure per protocol header
4946  * @lkups_cnt: number of protocols
4947  * @rinfo: other information regarding the rule e.g. priority and action info
4948  * @rid: return the recipe ID of the recipe created
4949  */
4950 static enum ice_status
4951 ice_add_adv_recipe(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
4952                    u16 lkups_cnt, struct ice_adv_rule_info *rinfo, u16 *rid)
4953 {
4954         struct ice_prot_lkup_ext *lkup_exts;
4955         struct ice_recp_grp_entry *r_entry;
4956         struct ice_sw_fv_list_entry *fvit;
4957         struct ice_recp_grp_entry *r_tmp;
4958         struct ice_sw_fv_list_entry *tmp;
4959         enum ice_status status = ICE_SUCCESS;
4960         struct ice_sw_recipe *rm;
4961         bool match_tun = false;
4962         u8 i;
4963
4964         if (!lkups_cnt)
4965                 return ICE_ERR_PARAM;
4966
4967         lkup_exts = (struct ice_prot_lkup_ext *)
4968                 ice_malloc(hw, sizeof(*lkup_exts));
4969         if (!lkup_exts)
4970                 return ICE_ERR_NO_MEMORY;
4971
4972         /* Determine the number of words to be matched and if it exceeds a
4973          * recipe's restrictions
4974          */
4975         for (i = 0; i < lkups_cnt; i++) {
4976                 u16 count;
4977
4978                 if (lkups[i].type >= ICE_PROTOCOL_LAST) {
4979                         status = ICE_ERR_CFG;
4980                         goto err_free_lkup_exts;
4981                 }
4982
4983                 count = ice_fill_valid_words(&lkups[i], lkup_exts);
4984                 if (!count) {
4985                         status = ICE_ERR_CFG;
4986                         goto err_free_lkup_exts;
4987                 }
4988         }
4989
4990         *rid = ice_find_recp(hw, lkup_exts);
4991         if (*rid < ICE_MAX_NUM_RECIPES)
4992                 /* Success if found a recipe that match the existing criteria */
4993                 goto err_free_lkup_exts;
4994
4995         /* Recipe we need does not exist, add a recipe */
4996
4997         rm = (struct ice_sw_recipe *)ice_malloc(hw, sizeof(*rm));
4998         if (!rm) {
4999                 status = ICE_ERR_NO_MEMORY;
5000                 goto err_free_lkup_exts;
5001         }
5002
5003         /* Get field vectors that contain fields extracted from all the protocol
5004          * headers being programmed.
5005          */
5006         INIT_LIST_HEAD(&rm->fv_list);
5007         INIT_LIST_HEAD(&rm->rg_list);
5008
5009         status = ice_get_fv(hw, lkups, lkups_cnt, &rm->fv_list);
5010         if (status)
5011                 goto err_unroll;
5012
5013         /* Group match words into recipes using preferred recipe grouping
5014          * criteria.
5015          */
5016         status = ice_create_recipe_group(hw, rm, lkup_exts);
5017         if (status)
5018                 goto err_unroll;
5019
5020         /* There is only profile for UDP tunnels. So, it is necessary to use a
5021          * metadata ID flag to differentiate different tunnel types. A separate
5022          * recipe needs to be used for the metadata.
5023          */
5024         if ((rinfo->tun_type == ICE_SW_TUN_VXLAN_GPE ||
5025              rinfo->tun_type == ICE_SW_TUN_GENEVE ||
5026              rinfo->tun_type == ICE_SW_TUN_VXLAN) && rm->n_grp_count > 1)
5027                 match_tun = true;
5028
5029         /* set the recipe priority if specified */
5030         rm->priority = rinfo->priority ? rinfo->priority : 0;
5031
5032         /* Find offsets from the field vector. Pick the first one for all the
5033          * recipes.
5034          */
5035         ice_fill_fv_word_index(hw, &rm->fv_list, &rm->rg_list);
5036         status = ice_add_sw_recipe(hw, rm, match_tun);
5037         if (status)
5038                 goto err_unroll;
5039
5040         /* Associate all the recipes created with all the profiles in the
5041          * common field vector.
5042          */
5043         LIST_FOR_EACH_ENTRY(fvit, &rm->fv_list, ice_sw_fv_list_entry,
5044                             list_entry) {
5045                 ice_declare_bitmap(r_bitmap, ICE_MAX_NUM_RECIPES);
5046
5047                 status = ice_aq_get_recipe_to_profile(hw, fvit->profile_id,
5048                                                       (u8 *)r_bitmap, NULL);
5049                 if (status)
5050                         goto err_unroll;
5051
5052                 ice_or_bitmap(rm->r_bitmap, r_bitmap, rm->r_bitmap,
5053                               ICE_MAX_NUM_RECIPES);
5054                 status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
5055                 if (status)
5056                         goto err_unroll;
5057
5058                 status = ice_aq_map_recipe_to_profile(hw, fvit->profile_id,
5059                                                       (u8 *)rm->r_bitmap,
5060                                                       NULL);
5061                 ice_release_change_lock(hw);
5062
5063                 if (status)
5064                         goto err_unroll;
5065         }
5066
5067         *rid = rm->root_rid;
5068         ice_memcpy(&hw->switch_info->recp_list[*rid].lkup_exts,
5069                    lkup_exts, sizeof(*lkup_exts), ICE_NONDMA_TO_NONDMA);
5070 err_unroll:
5071         LIST_FOR_EACH_ENTRY_SAFE(r_entry, r_tmp, &rm->rg_list,
5072                                  ice_recp_grp_entry, l_entry) {
5073                 LIST_DEL(&r_entry->l_entry);
5074                 ice_free(hw, r_entry);
5075         }
5076
5077         LIST_FOR_EACH_ENTRY_SAFE(fvit, tmp, &rm->fv_list, ice_sw_fv_list_entry,
5078                                  list_entry) {
5079                 LIST_DEL(&fvit->list_entry);
5080                 ice_free(hw, fvit);
5081         }
5082
5083         if (rm->root_buf)
5084                 ice_free(hw, rm->root_buf);
5085
5086         ice_free(hw, rm);
5087
5088 err_free_lkup_exts:
5089         ice_free(hw, lkup_exts);
5090
5091         return status;
5092 }
5093
5094 #define ICE_MAC_HDR_OFFSET      0
5095 #define ICE_IP_HDR_OFFSET       14
5096 #define ICE_GRE_HDR_OFFSET      34
5097 #define ICE_MAC_IL_HDR_OFFSET   42
5098 #define ICE_IP_IL_HDR_OFFSET    56
5099 #define ICE_L4_HDR_OFFSET       34
5100 #define ICE_UDP_TUN_HDR_OFFSET  42
5101
5102 /**
5103  * ice_find_dummy_packet - find dummy packet with given match criteria
5104  *
5105  * @lkups: lookup elements or match criteria for the advanced recipe, one
5106  *         structure per protocol header
5107  * @lkups_cnt: number of protocols
5108  * @tun_type: tunnel type from the match criteria
5109  * @pkt: dummy packet to fill according to filter match criteria
5110  * @pkt_len: packet length of dummy packet
5111  */
5112 static void
5113 ice_find_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5114                       enum ice_sw_tunnel_type tun_type, const u8 **pkt,
5115                       u16 *pkt_len)
5116 {
5117         u16 i;
5118
5119         if (tun_type == ICE_SW_TUN_NVGRE || tun_type == ICE_ALL_TUNNELS) {
5120                 *pkt = dummy_gre_packet;
5121                 *pkt_len = sizeof(dummy_gre_packet);
5122                 return;
5123         }
5124
5125         if (tun_type == ICE_SW_TUN_VXLAN || tun_type == ICE_SW_TUN_GENEVE ||
5126             tun_type == ICE_SW_TUN_VXLAN_GPE) {
5127                 *pkt = dummy_udp_tun_packet;
5128                 *pkt_len = sizeof(dummy_udp_tun_packet);
5129                 return;
5130         }
5131
5132         for (i = 0; i < lkups_cnt; i++) {
5133                 if (lkups[i].type == ICE_UDP_ILOS) {
5134                         *pkt = dummy_udp_tun_packet;
5135                         *pkt_len = sizeof(dummy_udp_tun_packet);
5136                         return;
5137                 }
5138         }
5139
5140         *pkt = dummy_tcp_tun_packet;
5141         *pkt_len = sizeof(dummy_tcp_tun_packet);
5142 }
5143
5144 /**
5145  * ice_fill_adv_dummy_packet - fill a dummy packet with given match criteria
5146  *
5147  * @lkups: lookup elements or match criteria for the advanced recipe, one
5148  *         structure per protocol header
5149  * @lkups_cnt: number of protocols
5150  * @tun_type: to know if the dummy packet is supposed to be tunnel packet
5151  * @s_rule: stores rule information from the match criteria
5152  * @dummy_pkt: dummy packet to fill according to filter match criteria
5153  * @pkt_len: packet length of dummy packet
5154  */
5155 static void
5156 ice_fill_adv_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5157                           enum ice_sw_tunnel_type tun_type,
5158                           struct ice_aqc_sw_rules_elem *s_rule,
5159                           const u8 *dummy_pkt, u16 pkt_len)
5160 {
5161         u8 *pkt;
5162         u16 i;
5163
5164         /* Start with a packet with a pre-defined/dummy content. Then, fill
5165          * in the header values to be looked up or matched.
5166          */
5167         pkt = s_rule->pdata.lkup_tx_rx.hdr;
5168
5169         ice_memcpy(pkt, dummy_pkt, pkt_len, ICE_NONDMA_TO_NONDMA);
5170
5171         for (i = 0; i < lkups_cnt; i++) {
5172                 u32 len, pkt_off, hdr_size, field_off;
5173
5174                 switch (lkups[i].type) {
5175                 case ICE_MAC_OFOS:
5176                 case ICE_MAC_IL:
5177                         pkt_off = offsetof(struct ice_ether_hdr, dst_addr) +
5178                                 ((lkups[i].type == ICE_MAC_IL) ?
5179                                  ICE_MAC_IL_HDR_OFFSET : 0);
5180                         len = sizeof(lkups[i].h_u.eth_hdr.dst_addr);
5181                         if ((tun_type == ICE_SW_TUN_VXLAN ||
5182                              tun_type == ICE_SW_TUN_GENEVE ||
5183                              tun_type == ICE_SW_TUN_VXLAN_GPE) &&
5184                              lkups[i].type == ICE_MAC_IL) {
5185                                 pkt_off += sizeof(struct ice_udp_tnl_hdr);
5186                         }
5187
5188                         ice_memcpy(&pkt[pkt_off],
5189                                    &lkups[i].h_u.eth_hdr.dst_addr, len,
5190                                    ICE_NONDMA_TO_NONDMA);
5191                         pkt_off = offsetof(struct ice_ether_hdr, src_addr) +
5192                                 ((lkups[i].type == ICE_MAC_IL) ?
5193                                  ICE_MAC_IL_HDR_OFFSET : 0);
5194                         len = sizeof(lkups[i].h_u.eth_hdr.src_addr);
5195                         if ((tun_type == ICE_SW_TUN_VXLAN ||
5196                              tun_type == ICE_SW_TUN_GENEVE ||
5197                              tun_type == ICE_SW_TUN_VXLAN_GPE) &&
5198                              lkups[i].type == ICE_MAC_IL) {
5199                                 pkt_off += sizeof(struct ice_udp_tnl_hdr);
5200                         }
5201                         ice_memcpy(&pkt[pkt_off],
5202                                    &lkups[i].h_u.eth_hdr.src_addr, len,
5203                                    ICE_NONDMA_TO_NONDMA);
5204                         if (lkups[i].h_u.eth_hdr.ethtype_id) {
5205                                 pkt_off = offsetof(struct ice_ether_hdr,
5206                                                    ethtype_id) +
5207                                         ((lkups[i].type == ICE_MAC_IL) ?
5208                                          ICE_MAC_IL_HDR_OFFSET : 0);
5209                                 len = sizeof(lkups[i].h_u.eth_hdr.ethtype_id);
5210                                 if ((tun_type == ICE_SW_TUN_VXLAN ||
5211                                      tun_type == ICE_SW_TUN_GENEVE ||
5212                                      tun_type == ICE_SW_TUN_VXLAN_GPE) &&
5213                                      lkups[i].type == ICE_MAC_IL) {
5214                                         pkt_off +=
5215                                                 sizeof(struct ice_udp_tnl_hdr);
5216                                 }
5217                                 ice_memcpy(&pkt[pkt_off],
5218                                            &lkups[i].h_u.eth_hdr.ethtype_id,
5219                                            len, ICE_NONDMA_TO_NONDMA);
5220                         }
5221                         break;
5222                 case ICE_IPV4_OFOS:
5223                         hdr_size = sizeof(struct ice_ipv4_hdr);
5224                         if (lkups[i].h_u.ipv4_hdr.dst_addr) {
5225                                 pkt_off = ICE_IP_HDR_OFFSET +
5226                                            offsetof(struct ice_ipv4_hdr,
5227                                                     dst_addr);
5228                                 field_off = offsetof(struct ice_ipv4_hdr,
5229                                                      dst_addr);
5230                                 len = hdr_size - field_off;
5231                                 ice_memcpy(&pkt[pkt_off],
5232                                            &lkups[i].h_u.ipv4_hdr.dst_addr,
5233                                            len, ICE_NONDMA_TO_NONDMA);
5234                         }
5235                         if (lkups[i].h_u.ipv4_hdr.src_addr) {
5236                                 pkt_off = ICE_IP_HDR_OFFSET +
5237                                            offsetof(struct ice_ipv4_hdr,
5238                                                     src_addr);
5239                                 field_off = offsetof(struct ice_ipv4_hdr,
5240                                                      src_addr);
5241                                 len = hdr_size - field_off;
5242                                 ice_memcpy(&pkt[pkt_off],
5243                                            &lkups[i].h_u.ipv4_hdr.src_addr,
5244                                            len, ICE_NONDMA_TO_NONDMA);
5245                         }
5246                         break;
5247                 case ICE_IPV4_IL:
5248                         break;
5249                 case ICE_TCP_IL:
5250                 case ICE_UDP_ILOS:
5251                 case ICE_SCTP_IL:
5252                         hdr_size = sizeof(struct ice_udp_tnl_hdr);
5253                         if (lkups[i].h_u.l4_hdr.dst_port) {
5254                                 pkt_off = ICE_L4_HDR_OFFSET +
5255                                            offsetof(struct ice_l4_hdr,
5256                                                     dst_port);
5257                                 field_off = offsetof(struct ice_l4_hdr,
5258                                                      dst_port);
5259                                 len =  hdr_size - field_off;
5260                                 ice_memcpy(&pkt[pkt_off],
5261                                            &lkups[i].h_u.l4_hdr.dst_port,
5262                                            len, ICE_NONDMA_TO_NONDMA);
5263                         }
5264                         if (lkups[i].h_u.l4_hdr.src_port) {
5265                                 pkt_off = ICE_L4_HDR_OFFSET +
5266                                         offsetof(struct ice_l4_hdr, src_port);
5267                                 field_off = offsetof(struct ice_l4_hdr,
5268                                                      src_port);
5269                                 len =  hdr_size - field_off;
5270                                 ice_memcpy(&pkt[pkt_off],
5271                                            &lkups[i].h_u.l4_hdr.src_port,
5272                                            len, ICE_NONDMA_TO_NONDMA);
5273                         }
5274                         break;
5275                 case ICE_VXLAN:
5276                 case ICE_GENEVE:
5277                 case ICE_VXLAN_GPE:
5278                         pkt_off = ICE_UDP_TUN_HDR_OFFSET +
5279                                    offsetof(struct ice_udp_tnl_hdr, vni);
5280                         field_off = offsetof(struct ice_udp_tnl_hdr, vni);
5281                         len =  sizeof(struct ice_udp_tnl_hdr) - field_off;
5282                         ice_memcpy(&pkt[pkt_off], &lkups[i].h_u.tnl_hdr.vni,
5283                                    len, ICE_NONDMA_TO_NONDMA);
5284                         break;
5285                 default:
5286                         break;
5287                 }
5288         }
5289         s_rule->pdata.lkup_tx_rx.hdr_len = CPU_TO_LE16(pkt_len);
5290 }
5291
5292 /**
5293  * ice_find_adv_rule_entry - Search a rule entry
5294  * @hw: pointer to the hardware structure
5295  * @lkups: lookup elements or match criteria for the advanced recipe, one
5296  *         structure per protocol header
5297  * @lkups_cnt: number of protocols
5298  * @recp_id: recipe ID for which we are finding the rule
5299  * @rinfo: other information regarding the rule e.g. priority and action info
5300  *
5301  * Helper function to search for a given advance rule entry
5302  * Returns pointer to entry storing the rule if found
5303  */
5304 static struct ice_adv_fltr_mgmt_list_entry *
5305 ice_find_adv_rule_entry(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5306                         u16 lkups_cnt, u8 recp_id,
5307                         struct ice_adv_rule_info *rinfo)
5308 {
5309         struct ice_adv_fltr_mgmt_list_entry *list_itr;
5310         struct ice_switch_info *sw = hw->switch_info;
5311         int i;
5312
5313         LIST_FOR_EACH_ENTRY(list_itr, &sw->recp_list[recp_id].filt_rules,
5314                             ice_adv_fltr_mgmt_list_entry, list_entry) {
5315                 bool lkups_matched = true;
5316
5317                 if (lkups_cnt != list_itr->lkups_cnt)
5318                         continue;
5319                 for (i = 0; i < list_itr->lkups_cnt; i++)
5320                         if (memcmp(&list_itr->lkups[i], &lkups[i],
5321                                    sizeof(*lkups))) {
5322                                 lkups_matched = false;
5323                                 break;
5324                         }
5325                 if (rinfo->sw_act.flag == list_itr->rule_info.sw_act.flag &&
5326                     rinfo->tun_type == list_itr->rule_info.tun_type &&
5327                     lkups_matched)
5328                         return list_itr;
5329         }
5330         return NULL;
5331 }
5332
5333 /**
5334  * ice_adv_add_update_vsi_list
5335  * @hw: pointer to the hardware structure
5336  * @m_entry: pointer to current adv filter management list entry
5337  * @cur_fltr: filter information from the book keeping entry
5338  * @new_fltr: filter information with the new VSI to be added
5339  *
5340  * Call AQ command to add or update previously created VSI list with new VSI.
5341  *
5342  * Helper function to do book keeping associated with adding filter information
5343  * The algorithm to do the booking keeping is described below :
5344  * When a VSI needs to subscribe to a given advanced filter
5345  *      if only one VSI has been added till now
5346  *              Allocate a new VSI list and add two VSIs
5347  *              to this list using switch rule command
5348  *              Update the previously created switch rule with the
5349  *              newly created VSI list ID
5350  *      if a VSI list was previously created
5351  *              Add the new VSI to the previously created VSI list set
5352  *              using the update switch rule command
5353  */
5354 static enum ice_status
5355 ice_adv_add_update_vsi_list(struct ice_hw *hw,
5356                             struct ice_adv_fltr_mgmt_list_entry *m_entry,
5357                             struct ice_adv_rule_info *cur_fltr,
5358                             struct ice_adv_rule_info *new_fltr)
5359 {
5360         enum ice_status status;
5361         u16 vsi_list_id = 0;
5362
5363         if (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5364             cur_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP)
5365                 return ICE_ERR_NOT_IMPL;
5366
5367         if (cur_fltr->sw_act.fltr_act == ICE_DROP_PACKET &&
5368             new_fltr->sw_act.fltr_act == ICE_DROP_PACKET)
5369                 return ICE_ERR_ALREADY_EXISTS;
5370
5371         if ((new_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5372              new_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP) &&
5373             (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI ||
5374              cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI_LIST))
5375                 return ICE_ERR_NOT_IMPL;
5376
5377         if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
5378                  /* Only one entry existed in the mapping and it was not already
5379                   * a part of a VSI list. So, create a VSI list with the old and
5380                   * new VSIs.
5381                   */
5382                 struct ice_fltr_info tmp_fltr;
5383                 u16 vsi_handle_arr[2];
5384
5385                 /* A rule already exists with the new VSI being added */
5386                 if (cur_fltr->sw_act.fwd_id.hw_vsi_id ==
5387                     new_fltr->sw_act.fwd_id.hw_vsi_id)
5388                         return ICE_ERR_ALREADY_EXISTS;
5389
5390                 vsi_handle_arr[0] = cur_fltr->sw_act.vsi_handle;
5391                 vsi_handle_arr[1] = new_fltr->sw_act.vsi_handle;
5392                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
5393                                                   &vsi_list_id,
5394                                                   ICE_SW_LKUP_LAST);
5395                 if (status)
5396                         return status;
5397
5398                 tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
5399                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
5400                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
5401                 /* Update the previous switch rule of "forward to VSI" to
5402                  * "fwd to VSI list"
5403                  */
5404                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5405                 if (status)
5406                         return status;
5407
5408                 cur_fltr->sw_act.fwd_id.vsi_list_id = vsi_list_id;
5409                 cur_fltr->sw_act.fltr_act = ICE_FWD_TO_VSI_LIST;
5410                 m_entry->vsi_list_info =
5411                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
5412                                                 vsi_list_id);
5413         } else {
5414                 u16 vsi_handle = new_fltr->sw_act.vsi_handle;
5415
5416                 if (!m_entry->vsi_list_info)
5417                         return ICE_ERR_CFG;
5418
5419                 /* A rule already exists with the new VSI being added */
5420                 if (ice_is_bit_set(m_entry->vsi_list_info->vsi_map, vsi_handle))
5421                         return ICE_SUCCESS;
5422
5423                 /* Update the previously created VSI list set with
5424                  * the new VSI ID passed in
5425                  */
5426                 vsi_list_id = cur_fltr->sw_act.fwd_id.vsi_list_id;
5427
5428                 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
5429                                                   vsi_list_id, false,
5430                                                   ice_aqc_opc_update_sw_rules,
5431                                                   ICE_SW_LKUP_LAST);
5432                 /* update VSI list mapping info with new VSI ID */
5433                 if (!status)
5434                         ice_set_bit(vsi_handle,
5435                                     m_entry->vsi_list_info->vsi_map);
5436         }
5437         if (!status)
5438                 m_entry->vsi_count++;
5439         return status;
5440 }
5441
5442 /**
5443  * ice_add_adv_rule - create an advanced switch rule
5444  * @hw: pointer to the hardware structure
5445  * @lkups: information on the words that needs to be looked up. All words
5446  * together makes one recipe
5447  * @lkups_cnt: num of entries in the lkups array
5448  * @rinfo: other information related to the rule that needs to be programmed
5449  * @added_entry: this will return recipe_id, rule_id and vsi_handle. should be
5450  *               ignored is case of error.
5451  *
5452  * This function can program only 1 rule at a time. The lkups is used to
5453  * describe the all the words that forms the "lookup" portion of the recipe.
5454  * These words can span multiple protocols. Callers to this function need to
5455  * pass in a list of protocol headers with lookup information along and mask
5456  * that determines which words are valid from the given protocol header.
5457  * rinfo describes other information related to this rule such as forwarding
5458  * IDs, priority of this rule, etc.
5459  */
5460 enum ice_status
5461 ice_add_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5462                  u16 lkups_cnt, struct ice_adv_rule_info *rinfo,
5463                  struct ice_rule_query_data *added_entry)
5464 {
5465         struct ice_adv_fltr_mgmt_list_entry *m_entry, *adv_fltr = NULL;
5466         u16 rid = 0, i, pkt_len, rule_buf_sz, vsi_handle;
5467         struct ice_aqc_sw_rules_elem *s_rule;
5468         struct LIST_HEAD_TYPE *rule_head;
5469         struct ice_switch_info *sw;
5470         enum ice_status status;
5471         const u8 *pkt = NULL;
5472         u32 act = 0;
5473
5474         if (!lkups_cnt)
5475                 return ICE_ERR_PARAM;
5476
5477         for (i = 0; i < lkups_cnt; i++) {
5478                 u16 j, *ptr;
5479
5480                 /* Validate match masks to make sure they match complete 16-bit
5481                  * words.
5482                  */
5483                 ptr = (u16 *)&lkups->m_u;
5484                 for (j = 0; j < sizeof(lkups->m_u) / sizeof(u16); j++)
5485                         if (ptr[j] != 0 && ptr[j] != 0xffff)
5486                                 return ICE_ERR_PARAM;
5487         }
5488
5489         if (!(rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI ||
5490               rinfo->sw_act.fltr_act == ICE_FWD_TO_Q ||
5491               rinfo->sw_act.fltr_act == ICE_DROP_PACKET))
5492                 return ICE_ERR_CFG;
5493
5494         vsi_handle = rinfo->sw_act.vsi_handle;
5495         if (!ice_is_vsi_valid(hw, vsi_handle))
5496                 return ICE_ERR_PARAM;
5497
5498         if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI)
5499                 rinfo->sw_act.fwd_id.hw_vsi_id =
5500                         ice_get_hw_vsi_num(hw, vsi_handle);
5501         if (rinfo->sw_act.flag & ICE_FLTR_TX)
5502                 rinfo->sw_act.src = ice_get_hw_vsi_num(hw, vsi_handle);
5503
5504         status = ice_add_adv_recipe(hw, lkups, lkups_cnt, rinfo, &rid);
5505         if (status)
5506                 return status;
5507         m_entry = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
5508         if (m_entry) {
5509                 /* we have to add VSI to VSI_LIST and increment vsi_count.
5510                  * Also Update VSI list so that we can change forwarding rule
5511                  * if the rule already exists, we will check if it exists with
5512                  * same vsi_id, if not then add it to the VSI list if it already
5513                  * exists if not then create a VSI list and add the existing VSI
5514                  * ID and the new VSI ID to the list
5515                  * We will add that VSI to the list
5516                  */
5517                 status = ice_adv_add_update_vsi_list(hw, m_entry,
5518                                                      &m_entry->rule_info,
5519                                                      rinfo);
5520                 if (added_entry) {
5521                         added_entry->rid = rid;
5522                         added_entry->rule_id = m_entry->rule_info.fltr_rule_id;
5523                         added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
5524                 }
5525                 return status;
5526         }
5527         ice_find_dummy_packet(lkups, lkups_cnt, rinfo->tun_type, &pkt,
5528                               &pkt_len);
5529         rule_buf_sz = ICE_SW_RULE_RX_TX_NO_HDR_SIZE + pkt_len;
5530         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, rule_buf_sz);
5531         if (!s_rule)
5532                 return ICE_ERR_NO_MEMORY;
5533         act |= ICE_SINGLE_ACT_LB_ENABLE | ICE_SINGLE_ACT_LAN_ENABLE;
5534         switch (rinfo->sw_act.fltr_act) {
5535         case ICE_FWD_TO_VSI:
5536                 act |= (rinfo->sw_act.fwd_id.hw_vsi_id <<
5537                         ICE_SINGLE_ACT_VSI_ID_S) & ICE_SINGLE_ACT_VSI_ID_M;
5538                 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_VALID_BIT;
5539                 break;
5540         case ICE_FWD_TO_Q:
5541                 act |= ICE_SINGLE_ACT_TO_Q;
5542                 act |= (rinfo->sw_act.fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
5543                        ICE_SINGLE_ACT_Q_INDEX_M;
5544                 break;
5545         case ICE_DROP_PACKET:
5546                 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
5547                        ICE_SINGLE_ACT_VALID_BIT;
5548                 break;
5549         default:
5550                 status = ICE_ERR_CFG;
5551                 goto err_ice_add_adv_rule;
5552         }
5553
5554         /* set the rule LOOKUP type based on caller specified 'RX'
5555          * instead of hardcoding it to be either LOOKUP_TX/RX
5556          *
5557          * for 'RX' set the source to be the port number
5558          * for 'TX' set the source to be the source HW VSI number (determined
5559          * by caller)
5560          */
5561         if (rinfo->rx) {
5562                 s_rule->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_RX);
5563                 s_rule->pdata.lkup_tx_rx.src =
5564                         CPU_TO_LE16(hw->port_info->lport);
5565         } else {
5566                 s_rule->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_TX);
5567                 s_rule->pdata.lkup_tx_rx.src = CPU_TO_LE16(rinfo->sw_act.src);
5568         }
5569
5570         s_rule->pdata.lkup_tx_rx.recipe_id = CPU_TO_LE16(rid);
5571         s_rule->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
5572
5573         ice_fill_adv_dummy_packet(lkups, lkups_cnt, rinfo->tun_type, s_rule,
5574                                   pkt, pkt_len);
5575
5576         status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
5577                                  rule_buf_sz, 1, ice_aqc_opc_add_sw_rules,
5578                                  NULL);
5579         if (status)
5580                 goto err_ice_add_adv_rule;
5581         adv_fltr = (struct ice_adv_fltr_mgmt_list_entry *)
5582                 ice_malloc(hw, sizeof(struct ice_adv_fltr_mgmt_list_entry));
5583         if (!adv_fltr) {
5584                 status = ICE_ERR_NO_MEMORY;
5585                 goto err_ice_add_adv_rule;
5586         }
5587
5588         adv_fltr->lkups = (struct ice_adv_lkup_elem *)
5589                 ice_memdup(hw, lkups, lkups_cnt * sizeof(*lkups),
5590                            ICE_NONDMA_TO_NONDMA);
5591         if (!adv_fltr->lkups) {
5592                 status = ICE_ERR_NO_MEMORY;
5593                 goto err_ice_add_adv_rule;
5594         }
5595
5596         adv_fltr->lkups_cnt = lkups_cnt;
5597         adv_fltr->rule_info = *rinfo;
5598         adv_fltr->rule_info.fltr_rule_id =
5599                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
5600         sw = hw->switch_info;
5601         sw->recp_list[rid].adv_rule = true;
5602         rule_head = &sw->recp_list[rid].filt_rules;
5603
5604         if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI) {
5605                 struct ice_fltr_info tmp_fltr;
5606
5607                 tmp_fltr.fltr_rule_id =
5608                         LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
5609                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI;
5610                 tmp_fltr.fwd_id.hw_vsi_id =
5611                         ice_get_hw_vsi_num(hw, vsi_handle);
5612                 tmp_fltr.vsi_handle = vsi_handle;
5613                 /* Update the previous switch rule of "forward to VSI" to
5614                  * "fwd to VSI list"
5615                  */
5616                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5617                 if (status)
5618                         goto err_ice_add_adv_rule;
5619                 adv_fltr->vsi_count = 1;
5620         }
5621
5622         /* Add rule entry to book keeping list */
5623         LIST_ADD(&adv_fltr->list_entry, rule_head);
5624         if (added_entry) {
5625                 added_entry->rid = rid;
5626                 added_entry->rule_id = adv_fltr->rule_info.fltr_rule_id;
5627                 added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
5628         }
5629 err_ice_add_adv_rule:
5630         if (status && adv_fltr) {
5631                 ice_free(hw, adv_fltr->lkups);
5632                 ice_free(hw, adv_fltr);
5633         }
5634
5635         ice_free(hw, s_rule);
5636
5637         return status;
5638 }
5639
5640 /**
5641  * ice_adv_rem_update_vsi_list
5642  * @hw: pointer to the hardware structure
5643  * @vsi_handle: VSI handle of the VSI to remove
5644  * @fm_list: filter management entry for which the VSI list management needs to
5645  *           be done
5646  */
5647 static enum ice_status
5648 ice_adv_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
5649                             struct ice_adv_fltr_mgmt_list_entry *fm_list)
5650 {
5651         struct ice_vsi_list_map_info *vsi_list_info;
5652         enum ice_sw_lkup_type lkup_type;
5653         enum ice_status status;
5654         u16 vsi_list_id;
5655
5656         if (fm_list->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST ||
5657             fm_list->vsi_count == 0)
5658                 return ICE_ERR_PARAM;
5659
5660         /* A rule with the VSI being removed does not exist */
5661         if (!ice_is_bit_set(fm_list->vsi_list_info->vsi_map, vsi_handle))
5662                 return ICE_ERR_DOES_NOT_EXIST;
5663
5664         lkup_type = ICE_SW_LKUP_LAST;
5665         vsi_list_id = fm_list->rule_info.sw_act.fwd_id.vsi_list_id;
5666         status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
5667                                           ice_aqc_opc_update_sw_rules,
5668                                           lkup_type);
5669         if (status)
5670                 return status;
5671
5672         fm_list->vsi_count--;
5673         ice_clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
5674         vsi_list_info = fm_list->vsi_list_info;
5675         if (fm_list->vsi_count == 1) {
5676                 struct ice_fltr_info tmp_fltr;
5677                 u16 rem_vsi_handle;
5678
5679                 rem_vsi_handle = ice_find_first_bit(vsi_list_info->vsi_map,
5680                                                     ICE_MAX_VSI);
5681                 if (!ice_is_vsi_valid(hw, rem_vsi_handle))
5682                         return ICE_ERR_OUT_OF_RANGE;
5683
5684                 /* Make sure VSI list is empty before removing it below */
5685                 status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
5686                                                   vsi_list_id, true,
5687                                                   ice_aqc_opc_update_sw_rules,
5688                                                   lkup_type);
5689                 if (status)
5690                         return status;
5691                 tmp_fltr.fltr_rule_id = fm_list->rule_info.fltr_rule_id;
5692                 fm_list->rule_info.sw_act.fltr_act = ICE_FWD_TO_VSI;
5693                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI;
5694                 tmp_fltr.fwd_id.hw_vsi_id =
5695                         ice_get_hw_vsi_num(hw, rem_vsi_handle);
5696                 fm_list->rule_info.sw_act.fwd_id.hw_vsi_id =
5697                         ice_get_hw_vsi_num(hw, rem_vsi_handle);
5698
5699                 /* Update the previous switch rule of "MAC forward to VSI" to
5700                  * "MAC fwd to VSI list"
5701                  */
5702                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5703                 if (status) {
5704                         ice_debug(hw, ICE_DBG_SW,
5705                                   "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
5706                                   tmp_fltr.fwd_id.hw_vsi_id, status);
5707                         return status;
5708                 }
5709         }
5710
5711         if (fm_list->vsi_count == 1) {
5712                 /* Remove the VSI list since it is no longer used */
5713                 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
5714                 if (status) {
5715                         ice_debug(hw, ICE_DBG_SW,
5716                                   "Failed to remove VSI list %d, error %d\n",
5717                                   vsi_list_id, status);
5718                         return status;
5719                 }
5720
5721                 LIST_DEL(&vsi_list_info->list_entry);
5722                 ice_free(hw, vsi_list_info);
5723                 fm_list->vsi_list_info = NULL;
5724         }
5725
5726         return status;
5727 }
5728
5729 /**
5730  * ice_rem_adv_rule - removes existing advanced switch rule
5731  * @hw: pointer to the hardware structure
5732  * @lkups: information on the words that needs to be looked up. All words
5733  *         together makes one recipe
5734  * @lkups_cnt: num of entries in the lkups array
5735  * @rinfo: Its the pointer to the rule information for the rule
5736  *
5737  * This function can be used to remove 1 rule at a time. The lkups is
5738  * used to describe all the words that forms the "lookup" portion of the
5739  * rule. These words can span multiple protocols. Callers to this function
5740  * need to pass in a list of protocol headers with lookup information along
5741  * and mask that determines which words are valid from the given protocol
5742  * header. rinfo describes other information related to this rule such as
5743  * forwarding IDs, priority of this rule, etc.
5744  */
5745 enum ice_status
5746 ice_rem_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5747                  u16 lkups_cnt, struct ice_adv_rule_info *rinfo)
5748 {
5749         struct ice_adv_fltr_mgmt_list_entry *list_elem;
5750         struct ice_prot_lkup_ext lkup_exts;
5751         u16 rule_buf_sz, pkt_len, i, rid;
5752         enum ice_status status = ICE_SUCCESS;
5753         bool remove_rule = false;
5754         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
5755         const u8 *pkt = NULL;
5756         u16 vsi_handle;
5757
5758         ice_memset(&lkup_exts, 0, sizeof(lkup_exts), ICE_NONDMA_MEM);
5759         for (i = 0; i < lkups_cnt; i++) {
5760                 u16 count;
5761
5762                 if (lkups[i].type >= ICE_PROTOCOL_LAST)
5763                         return ICE_ERR_CFG;
5764
5765                 count = ice_fill_valid_words(&lkups[i], &lkup_exts);
5766                 if (!count)
5767                         return ICE_ERR_CFG;
5768         }
5769         rid = ice_find_recp(hw, &lkup_exts);
5770         /* If did not find a recipe that match the existing criteria */
5771         if (rid == ICE_MAX_NUM_RECIPES)
5772                 return ICE_ERR_PARAM;
5773
5774         rule_lock = &hw->switch_info->recp_list[rid].filt_rule_lock;
5775         list_elem = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
5776         /* the rule is already removed */
5777         if (!list_elem)
5778                 return ICE_SUCCESS;
5779         ice_acquire_lock(rule_lock);
5780         if (list_elem->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST) {
5781                 remove_rule = true;
5782         } else if (list_elem->vsi_count > 1) {
5783                 list_elem->vsi_list_info->ref_cnt--;
5784                 remove_rule = false;
5785                 vsi_handle = rinfo->sw_act.vsi_handle;
5786                 status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
5787         } else {
5788                 vsi_handle = rinfo->sw_act.vsi_handle;
5789                 status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
5790                 if (status) {
5791                         ice_release_lock(rule_lock);
5792                         return status;
5793                 }
5794                 if (list_elem->vsi_count == 0)
5795                         remove_rule = true;
5796         }
5797         ice_release_lock(rule_lock);
5798         if (remove_rule) {
5799                 struct ice_aqc_sw_rules_elem *s_rule;
5800
5801                 ice_find_dummy_packet(lkups, lkups_cnt, rinfo->tun_type, &pkt,
5802                                       &pkt_len);
5803                 rule_buf_sz = ICE_SW_RULE_RX_TX_NO_HDR_SIZE + pkt_len;
5804                 s_rule =
5805                         (struct ice_aqc_sw_rules_elem *)ice_malloc(hw,
5806                                                                    rule_buf_sz);
5807                 if (!s_rule)
5808                         return ICE_ERR_NO_MEMORY;
5809                 s_rule->pdata.lkup_tx_rx.act = 0;
5810                 s_rule->pdata.lkup_tx_rx.index =
5811                         CPU_TO_LE16(list_elem->rule_info.fltr_rule_id);
5812                 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
5813                 status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
5814                                          rule_buf_sz, 1,
5815                                          ice_aqc_opc_remove_sw_rules, NULL);
5816                 if (status == ICE_SUCCESS) {
5817                         ice_acquire_lock(rule_lock);
5818                         LIST_DEL(&list_elem->list_entry);
5819                         ice_free(hw, list_elem->lkups);
5820                         ice_free(hw, list_elem);
5821                         ice_release_lock(rule_lock);
5822                 }
5823                 ice_free(hw, s_rule);
5824         }
5825         return status;
5826 }
5827
5828 /**
5829  * ice_rem_adv_rule_by_id - removes existing advanced switch rule by ID
5830  * @hw: pointer to the hardware structure
5831  * @remove_entry: data struct which holds rule_id, VSI handle and recipe ID
5832  *
5833  * This function is used to remove 1 rule at a time. The removal is based on
5834  * the remove_entry parameter. This function will remove rule for a given
5835  * vsi_handle with a given rule_id which is passed as parameter in remove_entry
5836  */
5837 enum ice_status
5838 ice_rem_adv_rule_by_id(struct ice_hw *hw,
5839                        struct ice_rule_query_data *remove_entry)
5840 {
5841         struct ice_adv_fltr_mgmt_list_entry *list_itr;
5842         struct LIST_HEAD_TYPE *list_head;
5843         struct ice_adv_rule_info rinfo;
5844         struct ice_switch_info *sw;
5845
5846         sw = hw->switch_info;
5847         if (!sw->recp_list[remove_entry->rid].recp_created)
5848                 return ICE_ERR_PARAM;
5849         list_head = &sw->recp_list[remove_entry->rid].filt_rules;
5850         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_adv_fltr_mgmt_list_entry,
5851                             list_entry) {
5852                 if (list_itr->rule_info.fltr_rule_id ==
5853                     remove_entry->rule_id) {
5854                         rinfo = list_itr->rule_info;
5855                         rinfo.sw_act.vsi_handle = remove_entry->vsi_handle;
5856                         return ice_rem_adv_rule(hw, list_itr->lkups,
5857                                                 list_itr->lkups_cnt, &rinfo);
5858                 }
5859         }
5860         return ICE_ERR_PARAM;
5861 }
5862
5863 /**
5864  * ice_rem_adv_for_vsi - removes existing advanced switch rules for a
5865  *                       given VSI handle
5866  * @hw: pointer to the hardware structure
5867  * @vsi_handle: VSI handle for which we are supposed to remove all the rules.
5868  *
5869  * This function is used to remove all the rules for a given VSI and as soon
5870  * as removing a rule fails, it will return immediately with the error code,
5871  * else it will return ICE_SUCCESS
5872  */
5873 enum ice_status
5874 ice_rem_adv_rule_for_vsi(struct ice_hw *hw, u16 vsi_handle)
5875 {
5876         struct ice_adv_fltr_mgmt_list_entry *list_itr;
5877         struct ice_vsi_list_map_info *map_info;
5878         struct LIST_HEAD_TYPE *list_head;
5879         struct ice_adv_rule_info rinfo;
5880         struct ice_switch_info *sw;
5881         enum ice_status status;
5882         u16 vsi_list_id = 0;
5883         u8 rid;
5884
5885         sw = hw->switch_info;
5886         for (rid = 0; rid < ICE_MAX_NUM_RECIPES; rid++) {
5887                 if (!sw->recp_list[rid].recp_created)
5888                         continue;
5889                 if (!sw->recp_list[rid].adv_rule)
5890                         continue;
5891                 list_head = &sw->recp_list[rid].filt_rules;
5892                 map_info = NULL;
5893                 LIST_FOR_EACH_ENTRY(list_itr, list_head,
5894                                     ice_adv_fltr_mgmt_list_entry, list_entry) {
5895                         map_info = ice_find_vsi_list_entry(hw, rid, vsi_handle,
5896                                                            &vsi_list_id);
5897                         if (!map_info)
5898                                 continue;
5899                         rinfo = list_itr->rule_info;
5900                         rinfo.sw_act.vsi_handle = vsi_handle;
5901                         status = ice_rem_adv_rule(hw, list_itr->lkups,
5902                                                   list_itr->lkups_cnt, &rinfo);
5903                         if (status)
5904                                 return status;
5905                         map_info = NULL;
5906                 }
5907         }
5908         return ICE_SUCCESS;
5909 }
5910
5911 /**
5912  * ice_replay_fltr - Replay all the filters stored by a specific list head
5913  * @hw: pointer to the hardware structure
5914  * @list_head: list for which filters needs to be replayed
5915  * @recp_id: Recipe ID for which rules need to be replayed
5916  */
5917 static enum ice_status
5918 ice_replay_fltr(struct ice_hw *hw, u8 recp_id, struct LIST_HEAD_TYPE *list_head)
5919 {
5920         struct ice_fltr_mgmt_list_entry *itr;
5921         struct LIST_HEAD_TYPE l_head;
5922         enum ice_status status = ICE_SUCCESS;
5923
5924         if (LIST_EMPTY(list_head))
5925                 return status;
5926
5927         /* Move entries from the given list_head to a temporary l_head so that
5928          * they can be replayed. Otherwise when trying to re-add the same
5929          * filter, the function will return already exists
5930          */
5931         LIST_REPLACE_INIT(list_head, &l_head);
5932
5933         /* Mark the given list_head empty by reinitializing it so filters
5934          * could be added again by *handler
5935          */
5936         LIST_FOR_EACH_ENTRY(itr, &l_head, ice_fltr_mgmt_list_entry,
5937                             list_entry) {
5938                 struct ice_fltr_list_entry f_entry;
5939
5940                 f_entry.fltr_info = itr->fltr_info;
5941                 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN) {
5942                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
5943                         if (status != ICE_SUCCESS)
5944                                 goto end;
5945                         continue;
5946                 }
5947
5948                 /* Add a filter per VSI separately */
5949                 while (1) {
5950                         u16 vsi_handle;
5951
5952                         vsi_handle =
5953                                 ice_find_first_bit(itr->vsi_list_info->vsi_map,
5954                                                    ICE_MAX_VSI);
5955                         if (!ice_is_vsi_valid(hw, vsi_handle))
5956                                 break;
5957
5958                         ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
5959                         f_entry.fltr_info.vsi_handle = vsi_handle;
5960                         f_entry.fltr_info.fwd_id.hw_vsi_id =
5961                                 ice_get_hw_vsi_num(hw, vsi_handle);
5962                         f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
5963                         if (recp_id == ICE_SW_LKUP_VLAN)
5964                                 status = ice_add_vlan_internal(hw, &f_entry);
5965                         else
5966                                 status = ice_add_rule_internal(hw, recp_id,
5967                                                                &f_entry);
5968                         if (status != ICE_SUCCESS)
5969                                 goto end;
5970                 }
5971         }
5972 end:
5973         /* Clear the filter management list */
5974         ice_rem_sw_rule_info(hw, &l_head);
5975         return status;
5976 }
5977
5978 /**
5979  * ice_replay_all_fltr - replay all filters stored in bookkeeping lists
5980  * @hw: pointer to the hardware structure
5981  *
5982  * NOTE: This function does not clean up partially added filters on error.
5983  * It is up to caller of the function to issue a reset or fail early.
5984  */
5985 enum ice_status ice_replay_all_fltr(struct ice_hw *hw)
5986 {
5987         struct ice_switch_info *sw = hw->switch_info;
5988         enum ice_status status = ICE_SUCCESS;
5989         u8 i;
5990
5991         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
5992                 struct LIST_HEAD_TYPE *head = &sw->recp_list[i].filt_rules;
5993
5994                 status = ice_replay_fltr(hw, i, head);
5995                 if (status != ICE_SUCCESS)
5996                         return status;
5997         }
5998         return status;
5999 }
6000
6001 /**
6002  * ice_replay_vsi_fltr - Replay filters for requested VSI
6003  * @hw: pointer to the hardware structure
6004  * @vsi_handle: driver VSI handle
6005  * @recp_id: Recipe ID for which rules need to be replayed
6006  * @list_head: list for which filters need to be replayed
6007  *
6008  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
6009  * It is required to pass valid VSI handle.
6010  */
6011 static enum ice_status
6012 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
6013                     struct LIST_HEAD_TYPE *list_head)
6014 {
6015         struct ice_fltr_mgmt_list_entry *itr;
6016         enum ice_status status = ICE_SUCCESS;
6017         u16 hw_vsi_id;
6018
6019         if (LIST_EMPTY(list_head))
6020                 return status;
6021         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
6022
6023         LIST_FOR_EACH_ENTRY(itr, list_head, ice_fltr_mgmt_list_entry,
6024                             list_entry) {
6025                 struct ice_fltr_list_entry f_entry;
6026
6027                 f_entry.fltr_info = itr->fltr_info;
6028                 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
6029                     itr->fltr_info.vsi_handle == vsi_handle) {
6030                         /* update the src in case it is VSI num */
6031                         if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6032                                 f_entry.fltr_info.src = hw_vsi_id;
6033                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
6034                         if (status != ICE_SUCCESS)
6035                                 goto end;
6036                         continue;
6037                 }
6038                 if (!itr->vsi_list_info ||
6039                     !ice_is_bit_set(itr->vsi_list_info->vsi_map, vsi_handle))
6040                         continue;
6041                 /* Clearing it so that the logic can add it back */
6042                 ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
6043                 f_entry.fltr_info.vsi_handle = vsi_handle;
6044                 f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
6045                 /* update the src in case it is VSI num */
6046                 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6047                         f_entry.fltr_info.src = hw_vsi_id;
6048                 if (recp_id == ICE_SW_LKUP_VLAN)
6049                         status = ice_add_vlan_internal(hw, &f_entry);
6050                 else
6051                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
6052                 if (status != ICE_SUCCESS)
6053                         goto end;
6054         }
6055 end:
6056         return status;
6057 }
6058
6059 /**
6060  * ice_replay_vsi_adv_rule - Replay advanced rule for requested VSI
6061  * @hw: pointer to the hardware structure
6062  * @vsi_handle: driver VSI handle
6063  * @list_head: list for which filters need to be replayed
6064  *
6065  * Replay the advanced rule for the given VSI.
6066  */
6067 static enum ice_status
6068 ice_replay_vsi_adv_rule(struct ice_hw *hw, u16 vsi_handle,
6069                         struct LIST_HEAD_TYPE *list_head)
6070 {
6071         struct ice_rule_query_data added_entry = { 0 };
6072         struct ice_adv_fltr_mgmt_list_entry *adv_fltr;
6073         enum ice_status status = ICE_SUCCESS;
6074
6075         if (LIST_EMPTY(list_head))
6076                 return status;
6077         LIST_FOR_EACH_ENTRY(adv_fltr, list_head, ice_adv_fltr_mgmt_list_entry,
6078                             list_entry) {
6079                 struct ice_adv_rule_info *rinfo = &adv_fltr->rule_info;
6080                 u16 lk_cnt = adv_fltr->lkups_cnt;
6081
6082                 if (vsi_handle != rinfo->sw_act.vsi_handle)
6083                         continue;
6084                 status = ice_add_adv_rule(hw, adv_fltr->lkups, lk_cnt, rinfo,
6085                                           &added_entry);
6086                 if (status)
6087                         break;
6088         }
6089         return status;
6090 }
6091
6092 /**
6093  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
6094  * @hw: pointer to the hardware structure
6095  * @vsi_handle: driver VSI handle
6096  *
6097  * Replays filters for requested VSI via vsi_handle.
6098  */
6099 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
6100 {
6101         struct ice_switch_info *sw = hw->switch_info;
6102         enum ice_status status;
6103         u8 i;
6104
6105         /* Update the recipes that were created */
6106         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6107                 struct LIST_HEAD_TYPE *head;
6108
6109                 head = &sw->recp_list[i].filt_replay_rules;
6110                 if (!sw->recp_list[i].adv_rule)
6111                         status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
6112                 else
6113                         status = ice_replay_vsi_adv_rule(hw, vsi_handle, head);
6114                 if (status != ICE_SUCCESS)
6115                         return status;
6116         }
6117
6118         return ICE_SUCCESS;
6119 }
6120
6121 /**
6122  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
6123  * @hw: pointer to the HW struct
6124  *
6125  * Deletes the filter replay rules.
6126  */
6127 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
6128 {
6129         struct ice_switch_info *sw = hw->switch_info;
6130         u8 i;
6131
6132         if (!sw)
6133                 return;
6134
6135         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6136                 if (!LIST_EMPTY(&sw->recp_list[i].filt_replay_rules)) {
6137                         struct LIST_HEAD_TYPE *l_head;
6138
6139                         l_head = &sw->recp_list[i].filt_replay_rules;
6140                         if (!sw->recp_list[i].adv_rule)
6141                                 ice_rem_sw_rule_info(hw, l_head);
6142                         else
6143                                 ice_rem_adv_rule_info(hw, l_head);
6144                 }
6145         }
6146 }