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