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