net/ice/base: add helper functions for flow management
[dpdk.git] / drivers / net / ice / base / ice_switch.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2018
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
57 /**
58  * ice_init_def_sw_recp - initialize the recipe book keeping tables
59  * @hw: pointer to the HW struct
60  *
61  * Allocate memory for the entire recipe table and initialize the structures/
62  * entries corresponding to basic recipes.
63  */
64 enum ice_status ice_init_def_sw_recp(struct ice_hw *hw)
65 {
66         struct ice_sw_recipe *recps;
67         u8 i;
68
69         recps = (struct ice_sw_recipe *)
70                 ice_calloc(hw, ICE_MAX_NUM_RECIPES, sizeof(*recps));
71         if (!recps)
72                 return ICE_ERR_NO_MEMORY;
73
74         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
75                 recps[i].root_rid = i;
76                 INIT_LIST_HEAD(&recps[i].filt_rules);
77                 INIT_LIST_HEAD(&recps[i].filt_replay_rules);
78                 ice_init_lock(&recps[i].filt_rule_lock);
79         }
80
81         hw->switch_info->recp_list = recps;
82
83         return ICE_SUCCESS;
84 }
85
86 /**
87  * ice_aq_get_sw_cfg - get switch configuration
88  * @hw: pointer to the hardware structure
89  * @buf: pointer to the result buffer
90  * @buf_size: length of the buffer available for response
91  * @req_desc: pointer to requested descriptor
92  * @num_elems: pointer to number of elements
93  * @cd: pointer to command details structure or NULL
94  *
95  * Get switch configuration (0x0200) to be placed in 'buff'.
96  * This admin command returns information such as initial VSI/port number
97  * and switch ID it belongs to.
98  *
99  * NOTE: *req_desc is both an input/output parameter.
100  * The caller of this function first calls this function with *request_desc set
101  * to 0. If the response from f/w has *req_desc set to 0, all the switch
102  * configuration information has been returned; if non-zero (meaning not all
103  * the information was returned), the caller should call this function again
104  * with *req_desc set to the previous value returned by f/w to get the
105  * next block of switch configuration information.
106  *
107  * *num_elems is output only parameter. This reflects the number of elements
108  * in response buffer. The caller of this function to use *num_elems while
109  * parsing the response buffer.
110  */
111 static enum ice_status
112 ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp *buf,
113                   u16 buf_size, u16 *req_desc, u16 *num_elems,
114                   struct ice_sq_cd *cd)
115 {
116         struct ice_aqc_get_sw_cfg *cmd;
117         enum ice_status status;
118         struct ice_aq_desc desc;
119
120         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
121         cmd = &desc.params.get_sw_conf;
122         cmd->element = CPU_TO_LE16(*req_desc);
123
124         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
125         if (!status) {
126                 *req_desc = LE16_TO_CPU(cmd->element);
127                 *num_elems = LE16_TO_CPU(cmd->num_elems);
128         }
129
130         return status;
131 }
132
133
134 /**
135  * ice_alloc_sw - allocate resources specific to switch
136  * @hw: pointer to the HW struct
137  * @ena_stats: true to turn on VEB stats
138  * @shared_res: true for shared resource, false for dedicated resource
139  * @sw_id: switch ID returned
140  * @counter_id: VEB counter ID returned
141  *
142  * allocates switch resources (SWID and VEB counter) (0x0208)
143  */
144 enum ice_status
145 ice_alloc_sw(struct ice_hw *hw, bool ena_stats, bool shared_res, u16 *sw_id,
146              u16 *counter_id)
147 {
148         struct ice_aqc_alloc_free_res_elem *sw_buf;
149         struct ice_aqc_res_elem *sw_ele;
150         enum ice_status status;
151         u16 buf_len;
152
153         buf_len = sizeof(*sw_buf);
154         sw_buf = (struct ice_aqc_alloc_free_res_elem *)
155                    ice_malloc(hw, buf_len);
156         if (!sw_buf)
157                 return ICE_ERR_NO_MEMORY;
158
159         /* Prepare buffer for switch ID.
160          * The number of resource entries in buffer is passed as 1 since only a
161          * single switch/VEB instance is allocated, and hence a single sw_id
162          * is requested.
163          */
164         sw_buf->num_elems = CPU_TO_LE16(1);
165         sw_buf->res_type =
166                 CPU_TO_LE16(ICE_AQC_RES_TYPE_SWID |
167                             (shared_res ? ICE_AQC_RES_TYPE_FLAG_SHARED :
168                             ICE_AQC_RES_TYPE_FLAG_DEDICATED));
169
170         status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
171                                        ice_aqc_opc_alloc_res, NULL);
172
173         if (status)
174                 goto ice_alloc_sw_exit;
175
176         sw_ele = &sw_buf->elem[0];
177         *sw_id = LE16_TO_CPU(sw_ele->e.sw_resp);
178
179         if (ena_stats) {
180                 /* Prepare buffer for VEB Counter */
181                 enum ice_adminq_opc opc = ice_aqc_opc_alloc_res;
182                 struct ice_aqc_alloc_free_res_elem *counter_buf;
183                 struct ice_aqc_res_elem *counter_ele;
184
185                 counter_buf = (struct ice_aqc_alloc_free_res_elem *)
186                                 ice_malloc(hw, buf_len);
187                 if (!counter_buf) {
188                         status = ICE_ERR_NO_MEMORY;
189                         goto ice_alloc_sw_exit;
190                 }
191
192                 /* The number of resource entries in buffer is passed as 1 since
193                  * only a single switch/VEB instance is allocated, and hence a
194                  * single VEB counter is requested.
195                  */
196                 counter_buf->num_elems = CPU_TO_LE16(1);
197                 counter_buf->res_type =
198                         CPU_TO_LE16(ICE_AQC_RES_TYPE_VEB_COUNTER |
199                                     ICE_AQC_RES_TYPE_FLAG_DEDICATED);
200                 status = ice_aq_alloc_free_res(hw, 1, counter_buf, buf_len,
201                                                opc, NULL);
202
203                 if (status) {
204                         ice_free(hw, counter_buf);
205                         goto ice_alloc_sw_exit;
206                 }
207                 counter_ele = &counter_buf->elem[0];
208                 *counter_id = LE16_TO_CPU(counter_ele->e.sw_resp);
209                 ice_free(hw, counter_buf);
210         }
211
212 ice_alloc_sw_exit:
213         ice_free(hw, sw_buf);
214         return status;
215 }
216
217 /**
218  * ice_free_sw - free resources specific to switch
219  * @hw: pointer to the HW struct
220  * @sw_id: switch ID returned
221  * @counter_id: VEB counter ID returned
222  *
223  * free switch resources (SWID and VEB counter) (0x0209)
224  *
225  * NOTE: This function frees multiple resources. It continues
226  * releasing other resources even after it encounters error.
227  * The error code returned is the last error it encountered.
228  */
229 enum ice_status ice_free_sw(struct ice_hw *hw, u16 sw_id, u16 counter_id)
230 {
231         struct ice_aqc_alloc_free_res_elem *sw_buf, *counter_buf;
232         enum ice_status status, ret_status;
233         u16 buf_len;
234
235         buf_len = sizeof(*sw_buf);
236         sw_buf = (struct ice_aqc_alloc_free_res_elem *)
237                    ice_malloc(hw, buf_len);
238         if (!sw_buf)
239                 return ICE_ERR_NO_MEMORY;
240
241         /* Prepare buffer to free for switch ID res.
242          * The number of resource entries in buffer is passed as 1 since only a
243          * single switch/VEB instance is freed, and hence a single sw_id
244          * is released.
245          */
246         sw_buf->num_elems = CPU_TO_LE16(1);
247         sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_SWID);
248         sw_buf->elem[0].e.sw_resp = CPU_TO_LE16(sw_id);
249
250         ret_status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
251                                            ice_aqc_opc_free_res, NULL);
252
253         if (ret_status)
254                 ice_debug(hw, ICE_DBG_SW, "CQ CMD Buffer:\n");
255
256         /* Prepare buffer to free for VEB Counter resource */
257         counter_buf = (struct ice_aqc_alloc_free_res_elem *)
258                         ice_malloc(hw, buf_len);
259         if (!counter_buf) {
260                 ice_free(hw, sw_buf);
261                 return ICE_ERR_NO_MEMORY;
262         }
263
264         /* The number of resource entries in buffer is passed as 1 since only a
265          * single switch/VEB instance is freed, and hence a single VEB counter
266          * is released
267          */
268         counter_buf->num_elems = CPU_TO_LE16(1);
269         counter_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_VEB_COUNTER);
270         counter_buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
271
272         status = ice_aq_alloc_free_res(hw, 1, counter_buf, buf_len,
273                                        ice_aqc_opc_free_res, NULL);
274         if (status) {
275                 ice_debug(hw, ICE_DBG_SW,
276                           "VEB counter resource could not be freed\n");
277                 ret_status = status;
278         }
279
280         ice_free(hw, counter_buf);
281         ice_free(hw, sw_buf);
282         return ret_status;
283 }
284
285 /**
286  * ice_aq_add_vsi
287  * @hw: pointer to the HW struct
288  * @vsi_ctx: pointer to a VSI context struct
289  * @cd: pointer to command details structure or NULL
290  *
291  * Add a VSI context to the hardware (0x0210)
292  */
293 enum ice_status
294 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
295                struct ice_sq_cd *cd)
296 {
297         struct ice_aqc_add_update_free_vsi_resp *res;
298         struct ice_aqc_add_get_update_free_vsi *cmd;
299         struct ice_aq_desc desc;
300         enum ice_status status;
301
302         cmd = &desc.params.vsi_cmd;
303         res = &desc.params.add_update_free_vsi_res;
304
305         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
306
307         if (!vsi_ctx->alloc_from_pool)
308                 cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num |
309                                            ICE_AQ_VSI_IS_VALID);
310
311         cmd->vsi_flags = CPU_TO_LE16(vsi_ctx->flags);
312
313         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
314
315         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
316                                  sizeof(vsi_ctx->info), cd);
317
318         if (!status) {
319                 vsi_ctx->vsi_num = LE16_TO_CPU(res->vsi_num) & ICE_AQ_VSI_NUM_M;
320                 vsi_ctx->vsis_allocd = LE16_TO_CPU(res->vsi_used);
321                 vsi_ctx->vsis_unallocated = LE16_TO_CPU(res->vsi_free);
322         }
323
324         return status;
325 }
326
327 /**
328  * ice_aq_free_vsi
329  * @hw: pointer to the HW struct
330  * @vsi_ctx: pointer to a VSI context struct
331  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
332  * @cd: pointer to command details structure or NULL
333  *
334  * Free VSI context info from hardware (0x0213)
335  */
336 enum ice_status
337 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
338                 bool keep_vsi_alloc, struct ice_sq_cd *cd)
339 {
340         struct ice_aqc_add_update_free_vsi_resp *resp;
341         struct ice_aqc_add_get_update_free_vsi *cmd;
342         struct ice_aq_desc desc;
343         enum ice_status status;
344
345         cmd = &desc.params.vsi_cmd;
346         resp = &desc.params.add_update_free_vsi_res;
347
348         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
349
350         cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
351         if (keep_vsi_alloc)
352                 cmd->cmd_flags = CPU_TO_LE16(ICE_AQ_VSI_KEEP_ALLOC);
353
354         status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
355         if (!status) {
356                 vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
357                 vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
358         }
359
360         return status;
361 }
362
363 /**
364  * ice_aq_update_vsi
365  * @hw: pointer to the HW struct
366  * @vsi_ctx: pointer to a VSI context struct
367  * @cd: pointer to command details structure or NULL
368  *
369  * Update VSI context in the hardware (0x0211)
370  */
371 enum ice_status
372 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
373                   struct ice_sq_cd *cd)
374 {
375         struct ice_aqc_add_update_free_vsi_resp *resp;
376         struct ice_aqc_add_get_update_free_vsi *cmd;
377         struct ice_aq_desc desc;
378         enum ice_status status;
379
380         cmd = &desc.params.vsi_cmd;
381         resp = &desc.params.add_update_free_vsi_res;
382
383         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
384
385         cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
386
387         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
388
389         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
390                                  sizeof(vsi_ctx->info), cd);
391
392         if (!status) {
393                 vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
394                 vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
395         }
396
397         return status;
398 }
399
400 /**
401  * ice_is_vsi_valid - check whether the VSI is valid or not
402  * @hw: pointer to the HW struct
403  * @vsi_handle: VSI handle
404  *
405  * check whether the VSI is valid or not
406  */
407 bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
408 {
409         return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
410 }
411
412 /**
413  * ice_get_hw_vsi_num - return the HW VSI number
414  * @hw: pointer to the HW struct
415  * @vsi_handle: VSI handle
416  *
417  * return the HW VSI number
418  * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
419  */
420 u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
421 {
422         return hw->vsi_ctx[vsi_handle]->vsi_num;
423 }
424
425 /**
426  * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
427  * @hw: pointer to the HW struct
428  * @vsi_handle: VSI handle
429  *
430  * return the VSI context entry for a given VSI handle
431  */
432 struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
433 {
434         return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
435 }
436
437 /**
438  * ice_save_vsi_ctx - save the VSI context for a given VSI handle
439  * @hw: pointer to the HW struct
440  * @vsi_handle: VSI handle
441  * @vsi: VSI context pointer
442  *
443  * save the VSI context entry for a given VSI handle
444  */
445 static void
446 ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi)
447 {
448         hw->vsi_ctx[vsi_handle] = vsi;
449 }
450
451 /**
452  * ice_clear_vsi_q_ctx - clear VSI queue contexts for all TCs
453  * @hw: pointer to the HW struct
454  * @vsi_handle: VSI handle
455  */
456 static void ice_clear_vsi_q_ctx(struct ice_hw *hw, u16 vsi_handle)
457 {
458         struct ice_vsi_ctx *vsi;
459         u8 i;
460
461         vsi = ice_get_vsi_ctx(hw, vsi_handle);
462         if (!vsi)
463                 return;
464         ice_for_each_traffic_class(i) {
465                 if (vsi->lan_q_ctx[i]) {
466                         ice_free(hw, vsi->lan_q_ctx[i]);
467                         vsi->lan_q_ctx[i] = NULL;
468                 }
469         }
470 }
471
472 /**
473  * ice_clear_vsi_ctx - clear the VSI context entry
474  * @hw: pointer to the HW struct
475  * @vsi_handle: VSI handle
476  *
477  * clear the VSI context entry
478  */
479 static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
480 {
481         struct ice_vsi_ctx *vsi;
482
483         vsi = ice_get_vsi_ctx(hw, vsi_handle);
484         if (vsi) {
485                 if (!LIST_EMPTY(&vsi->rss_list_head))
486                         ice_rem_all_rss_vsi_ctx(hw, vsi_handle);
487                 ice_clear_vsi_q_ctx(hw, vsi_handle);
488                 ice_destroy_lock(&vsi->rss_locks);
489                 ice_free(hw, vsi);
490                 hw->vsi_ctx[vsi_handle] = NULL;
491         }
492 }
493
494 /**
495  * ice_clear_all_vsi_ctx - clear all the VSI context entries
496  * @hw: pointer to the HW struct
497  */
498 void ice_clear_all_vsi_ctx(struct ice_hw *hw)
499 {
500         u16 i;
501
502         for (i = 0; i < ICE_MAX_VSI; i++)
503                 ice_clear_vsi_ctx(hw, i);
504 }
505
506 /**
507  * ice_add_vsi - add VSI context to the hardware and VSI handle list
508  * @hw: pointer to the HW struct
509  * @vsi_handle: unique VSI handle provided by drivers
510  * @vsi_ctx: pointer to a VSI context struct
511  * @cd: pointer to command details structure or NULL
512  *
513  * Add a VSI context to the hardware also add it into the VSI handle list.
514  * If this function gets called after reset for existing VSIs then update
515  * with the new HW VSI number in the corresponding VSI handle list entry.
516  */
517 enum ice_status
518 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
519             struct ice_sq_cd *cd)
520 {
521         struct ice_vsi_ctx *tmp_vsi_ctx;
522         enum ice_status status;
523
524         if (vsi_handle >= ICE_MAX_VSI)
525                 return ICE_ERR_PARAM;
526         status = ice_aq_add_vsi(hw, vsi_ctx, cd);
527         if (status)
528                 return status;
529         tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
530         if (!tmp_vsi_ctx) {
531                 /* Create a new VSI context */
532                 tmp_vsi_ctx = (struct ice_vsi_ctx *)
533                         ice_malloc(hw, sizeof(*tmp_vsi_ctx));
534                 if (!tmp_vsi_ctx) {
535                         ice_aq_free_vsi(hw, vsi_ctx, false, cd);
536                         return ICE_ERR_NO_MEMORY;
537                 }
538                 *tmp_vsi_ctx = *vsi_ctx;
539                 ice_init_lock(&tmp_vsi_ctx->rss_locks);
540                 INIT_LIST_HEAD(&tmp_vsi_ctx->rss_list_head);
541                 ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
542         } else {
543                 /* update with new HW VSI num */
544                 if (tmp_vsi_ctx->vsi_num != vsi_ctx->vsi_num)
545                         tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
546         }
547
548         return ICE_SUCCESS;
549 }
550
551 /**
552  * ice_free_vsi- free VSI context from hardware and VSI handle list
553  * @hw: pointer to the HW struct
554  * @vsi_handle: unique VSI handle
555  * @vsi_ctx: pointer to a VSI context struct
556  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
557  * @cd: pointer to command details structure or NULL
558  *
559  * Free VSI context info from hardware as well as from VSI handle list
560  */
561 enum ice_status
562 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
563              bool keep_vsi_alloc, struct ice_sq_cd *cd)
564 {
565         enum ice_status status;
566
567         if (!ice_is_vsi_valid(hw, vsi_handle))
568                 return ICE_ERR_PARAM;
569         vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
570         status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
571         if (!status)
572                 ice_clear_vsi_ctx(hw, vsi_handle);
573         return status;
574 }
575
576 /**
577  * ice_update_vsi
578  * @hw: pointer to the HW struct
579  * @vsi_handle: unique VSI handle
580  * @vsi_ctx: pointer to a VSI context struct
581  * @cd: pointer to command details structure or NULL
582  *
583  * Update VSI context in the hardware
584  */
585 enum ice_status
586 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
587                struct ice_sq_cd *cd)
588 {
589         if (!ice_is_vsi_valid(hw, vsi_handle))
590                 return ICE_ERR_PARAM;
591         vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
592         return ice_aq_update_vsi(hw, vsi_ctx, cd);
593 }
594
595 /**
596  * ice_aq_get_vsi_params
597  * @hw: pointer to the HW struct
598  * @vsi_ctx: pointer to a VSI context struct
599  * @cd: pointer to command details structure or NULL
600  *
601  * Get VSI context info from hardware (0x0212)
602  */
603 enum ice_status
604 ice_aq_get_vsi_params(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
605                       struct ice_sq_cd *cd)
606 {
607         struct ice_aqc_add_get_update_free_vsi *cmd;
608         struct ice_aqc_get_vsi_resp *resp;
609         struct ice_aq_desc desc;
610         enum ice_status status;
611
612         cmd = &desc.params.vsi_cmd;
613         resp = &desc.params.get_vsi_resp;
614
615         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_vsi_params);
616
617         cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
618
619         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
620                                  sizeof(vsi_ctx->info), cd);
621         if (!status) {
622                 vsi_ctx->vsi_num = LE16_TO_CPU(resp->vsi_num) &
623                                         ICE_AQ_VSI_NUM_M;
624                 vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
625                 vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
626         }
627
628         return status;
629 }
630
631 /**
632  * ice_aq_add_update_mir_rule - add/update a mirror rule
633  * @hw: pointer to the HW struct
634  * @rule_type: Rule Type
635  * @dest_vsi: VSI number to which packets will be mirrored
636  * @count: length of the list
637  * @mr_buf: buffer for list of mirrored VSI numbers
638  * @cd: pointer to command details structure or NULL
639  * @rule_id: Rule ID
640  *
641  * Add/Update Mirror Rule (0x260).
642  */
643 enum ice_status
644 ice_aq_add_update_mir_rule(struct ice_hw *hw, u16 rule_type, u16 dest_vsi,
645                            u16 count, struct ice_mir_rule_buf *mr_buf,
646                            struct ice_sq_cd *cd, u16 *rule_id)
647 {
648         struct ice_aqc_add_update_mir_rule *cmd;
649         struct ice_aq_desc desc;
650         enum ice_status status;
651         __le16 *mr_list = NULL;
652         u16 buf_size = 0;
653
654         switch (rule_type) {
655         case ICE_AQC_RULE_TYPE_VPORT_INGRESS:
656         case ICE_AQC_RULE_TYPE_VPORT_EGRESS:
657                 /* Make sure count and mr_buf are set for these rule_types */
658                 if (!(count && mr_buf))
659                         return ICE_ERR_PARAM;
660
661                 buf_size = count * sizeof(__le16);
662                 mr_list = (__le16 *)ice_malloc(hw, buf_size);
663                 if (!mr_list)
664                         return ICE_ERR_NO_MEMORY;
665                 break;
666         case ICE_AQC_RULE_TYPE_PPORT_INGRESS:
667         case ICE_AQC_RULE_TYPE_PPORT_EGRESS:
668                 /* Make sure count and mr_buf are not set for these
669                  * rule_types
670                  */
671                 if (count || mr_buf)
672                         return ICE_ERR_PARAM;
673                 break;
674         default:
675                 ice_debug(hw, ICE_DBG_SW,
676                           "Error due to unsupported rule_type %u\n", rule_type);
677                 return ICE_ERR_OUT_OF_RANGE;
678         }
679
680         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_update_mir_rule);
681
682         /* Pre-process 'mr_buf' items for add/update of virtual port
683          * ingress/egress mirroring (but not physical port ingress/egress
684          * mirroring)
685          */
686         if (mr_buf) {
687                 int i;
688
689                 for (i = 0; i < count; i++) {
690                         u16 id;
691
692                         id = mr_buf[i].vsi_idx & ICE_AQC_RULE_MIRRORED_VSI_M;
693
694                         /* Validate specified VSI number, make sure it is less
695                          * than ICE_MAX_VSI, if not return with error.
696                          */
697                         if (id >= ICE_MAX_VSI) {
698                                 ice_debug(hw, ICE_DBG_SW,
699                                           "Error VSI index (%u) out-of-range\n",
700                                           id);
701                                 ice_free(hw, mr_list);
702                                 return ICE_ERR_OUT_OF_RANGE;
703                         }
704
705                         /* add VSI to mirror rule */
706                         if (mr_buf[i].add)
707                                 mr_list[i] =
708                                         CPU_TO_LE16(id | ICE_AQC_RULE_ACT_M);
709                         else /* remove VSI from mirror rule */
710                                 mr_list[i] = CPU_TO_LE16(id);
711                 }
712         }
713
714         cmd = &desc.params.add_update_rule;
715         if ((*rule_id) != ICE_INVAL_MIRROR_RULE_ID)
716                 cmd->rule_id = CPU_TO_LE16(((*rule_id) & ICE_AQC_RULE_ID_M) |
717                                            ICE_AQC_RULE_ID_VALID_M);
718         cmd->rule_type = CPU_TO_LE16(rule_type & ICE_AQC_RULE_TYPE_M);
719         cmd->num_entries = CPU_TO_LE16(count);
720         cmd->dest = CPU_TO_LE16(dest_vsi);
721
722         status = ice_aq_send_cmd(hw, &desc, mr_list, buf_size, cd);
723         if (!status)
724                 *rule_id = LE16_TO_CPU(cmd->rule_id) & ICE_AQC_RULE_ID_M;
725
726         ice_free(hw, mr_list);
727
728         return status;
729 }
730
731 /**
732  * ice_aq_delete_mir_rule - delete a mirror rule
733  * @hw: pointer to the HW struct
734  * @rule_id: Mirror rule ID (to be deleted)
735  * @keep_allocd: if set, the VSI stays part of the PF allocated res,
736  *               otherwise it is returned to the shared pool
737  * @cd: pointer to command details structure or NULL
738  *
739  * Delete Mirror Rule (0x261).
740  */
741 enum ice_status
742 ice_aq_delete_mir_rule(struct ice_hw *hw, u16 rule_id, bool keep_allocd,
743                        struct ice_sq_cd *cd)
744 {
745         struct ice_aqc_delete_mir_rule *cmd;
746         struct ice_aq_desc desc;
747
748         /* rule_id should be in the range 0...63 */
749         if (rule_id >= ICE_MAX_NUM_MIRROR_RULES)
750                 return ICE_ERR_OUT_OF_RANGE;
751
752         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_del_mir_rule);
753
754         cmd = &desc.params.del_rule;
755         rule_id |= ICE_AQC_RULE_ID_VALID_M;
756         cmd->rule_id = CPU_TO_LE16(rule_id);
757
758         if (keep_allocd)
759                 cmd->flags = CPU_TO_LE16(ICE_AQC_FLAG_KEEP_ALLOCD_M);
760
761         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
762 }
763
764 /**
765  * ice_aq_alloc_free_vsi_list
766  * @hw: pointer to the HW struct
767  * @vsi_list_id: VSI list ID returned or used for lookup
768  * @lkup_type: switch rule filter lookup type
769  * @opc: switch rules population command type - pass in the command opcode
770  *
771  * allocates or free a VSI list resource
772  */
773 static enum ice_status
774 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
775                            enum ice_sw_lkup_type lkup_type,
776                            enum ice_adminq_opc opc)
777 {
778         struct ice_aqc_alloc_free_res_elem *sw_buf;
779         struct ice_aqc_res_elem *vsi_ele;
780         enum ice_status status;
781         u16 buf_len;
782
783         buf_len = sizeof(*sw_buf);
784         sw_buf = (struct ice_aqc_alloc_free_res_elem *)
785                 ice_malloc(hw, buf_len);
786         if (!sw_buf)
787                 return ICE_ERR_NO_MEMORY;
788         sw_buf->num_elems = CPU_TO_LE16(1);
789
790         if (lkup_type == ICE_SW_LKUP_MAC ||
791             lkup_type == ICE_SW_LKUP_MAC_VLAN ||
792             lkup_type == ICE_SW_LKUP_ETHERTYPE ||
793             lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
794             lkup_type == ICE_SW_LKUP_PROMISC ||
795             lkup_type == ICE_SW_LKUP_PROMISC_VLAN) {
796                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
797         } else if (lkup_type == ICE_SW_LKUP_VLAN) {
798                 sw_buf->res_type =
799                         CPU_TO_LE16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
800         } else {
801                 status = ICE_ERR_PARAM;
802                 goto ice_aq_alloc_free_vsi_list_exit;
803         }
804
805         if (opc == ice_aqc_opc_free_res)
806                 sw_buf->elem[0].e.sw_resp = CPU_TO_LE16(*vsi_list_id);
807
808         status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
809         if (status)
810                 goto ice_aq_alloc_free_vsi_list_exit;
811
812         if (opc == ice_aqc_opc_alloc_res) {
813                 vsi_ele = &sw_buf->elem[0];
814                 *vsi_list_id = LE16_TO_CPU(vsi_ele->e.sw_resp);
815         }
816
817 ice_aq_alloc_free_vsi_list_exit:
818         ice_free(hw, sw_buf);
819         return status;
820 }
821
822 /**
823  * ice_aq_set_storm_ctrl - Sets storm control configuration
824  * @hw: pointer to the HW struct
825  * @bcast_thresh: represents the upper threshold for broadcast storm control
826  * @mcast_thresh: represents the upper threshold for multicast storm control
827  * @ctl_bitmask: storm control control knobs
828  *
829  * Sets the storm control configuration (0x0280)
830  */
831 enum ice_status
832 ice_aq_set_storm_ctrl(struct ice_hw *hw, u32 bcast_thresh, u32 mcast_thresh,
833                       u32 ctl_bitmask)
834 {
835         struct ice_aqc_storm_cfg *cmd;
836         struct ice_aq_desc desc;
837
838         cmd = &desc.params.storm_conf;
839
840         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_storm_cfg);
841
842         cmd->bcast_thresh_size = CPU_TO_LE32(bcast_thresh & ICE_AQ_THRESHOLD_M);
843         cmd->mcast_thresh_size = CPU_TO_LE32(mcast_thresh & ICE_AQ_THRESHOLD_M);
844         cmd->storm_ctrl_ctrl = CPU_TO_LE32(ctl_bitmask);
845
846         return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
847 }
848
849 /**
850  * ice_aq_get_storm_ctrl - gets storm control configuration
851  * @hw: pointer to the HW struct
852  * @bcast_thresh: represents the upper threshold for broadcast storm control
853  * @mcast_thresh: represents the upper threshold for multicast storm control
854  * @ctl_bitmask: storm control control knobs
855  *
856  * Gets the storm control configuration (0x0281)
857  */
858 enum ice_status
859 ice_aq_get_storm_ctrl(struct ice_hw *hw, u32 *bcast_thresh, u32 *mcast_thresh,
860                       u32 *ctl_bitmask)
861 {
862         enum ice_status status;
863         struct ice_aq_desc desc;
864
865         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_storm_cfg);
866
867         status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
868         if (!status) {
869                 struct ice_aqc_storm_cfg *resp = &desc.params.storm_conf;
870
871                 if (bcast_thresh)
872                         *bcast_thresh = LE32_TO_CPU(resp->bcast_thresh_size) &
873                                 ICE_AQ_THRESHOLD_M;
874                 if (mcast_thresh)
875                         *mcast_thresh = LE32_TO_CPU(resp->mcast_thresh_size) &
876                                 ICE_AQ_THRESHOLD_M;
877                 if (ctl_bitmask)
878                         *ctl_bitmask = LE32_TO_CPU(resp->storm_ctrl_ctrl);
879         }
880
881         return status;
882 }
883
884 /**
885  * ice_aq_sw_rules - add/update/remove switch rules
886  * @hw: pointer to the HW struct
887  * @rule_list: pointer to switch rule population list
888  * @rule_list_sz: total size of the rule list in bytes
889  * @num_rules: number of switch rules in the rule_list
890  * @opc: switch rules population command type - pass in the command opcode
891  * @cd: pointer to command details structure or NULL
892  *
893  * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
894  */
895 static enum ice_status
896 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
897                 u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
898 {
899         struct ice_aq_desc desc;
900
901         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_sw_rules");
902
903         if (opc != ice_aqc_opc_add_sw_rules &&
904             opc != ice_aqc_opc_update_sw_rules &&
905             opc != ice_aqc_opc_remove_sw_rules)
906                 return ICE_ERR_PARAM;
907
908         ice_fill_dflt_direct_cmd_desc(&desc, opc);
909
910         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
911         desc.params.sw_rules.num_rules_fltr_entry_index =
912                 CPU_TO_LE16(num_rules);
913         return ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
914 }
915
916
917 /* ice_init_port_info - Initialize port_info with switch configuration data
918  * @pi: pointer to port_info
919  * @vsi_port_num: VSI number or port number
920  * @type: Type of switch element (port or VSI)
921  * @swid: switch ID of the switch the element is attached to
922  * @pf_vf_num: PF or VF number
923  * @is_vf: true if the element is a VF, false otherwise
924  */
925 static void
926 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
927                    u16 swid, u16 pf_vf_num, bool is_vf)
928 {
929         switch (type) {
930         case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
931                 pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
932                 pi->sw_id = swid;
933                 pi->pf_vf_num = pf_vf_num;
934                 pi->is_vf = is_vf;
935                 pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
936                 pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
937                 break;
938         default:
939                 ice_debug(pi->hw, ICE_DBG_SW,
940                           "incorrect VSI/port type received\n");
941                 break;
942         }
943 }
944
945 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
946  * @hw: pointer to the hardware structure
947  */
948 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
949 {
950         struct ice_aqc_get_sw_cfg_resp *rbuf;
951         enum ice_status status;
952         u16 num_total_ports;
953         u16 req_desc = 0;
954         u16 num_elems;
955         u16 j = 0;
956         u16 i;
957
958         num_total_ports = 1;
959
960         rbuf = (struct ice_aqc_get_sw_cfg_resp *)
961                 ice_malloc(hw, ICE_SW_CFG_MAX_BUF_LEN);
962
963         if (!rbuf)
964                 return ICE_ERR_NO_MEMORY;
965
966         /* Multiple calls to ice_aq_get_sw_cfg may be required
967          * to get all the switch configuration information. The need
968          * for additional calls is indicated by ice_aq_get_sw_cfg
969          * writing a non-zero value in req_desc
970          */
971         do {
972                 status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
973                                            &req_desc, &num_elems, NULL);
974
975                 if (status)
976                         break;
977
978                 for (i = 0; i < num_elems; i++) {
979                         struct ice_aqc_get_sw_cfg_resp_elem *ele;
980                         u16 pf_vf_num, swid, vsi_port_num;
981                         bool is_vf = false;
982                         u8 type;
983
984                         ele = rbuf[i].elements;
985                         vsi_port_num = LE16_TO_CPU(ele->vsi_port_num) &
986                                 ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
987
988                         pf_vf_num = LE16_TO_CPU(ele->pf_vf_num) &
989                                 ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
990
991                         swid = LE16_TO_CPU(ele->swid);
992
993                         if (LE16_TO_CPU(ele->pf_vf_num) &
994                             ICE_AQC_GET_SW_CONF_RESP_IS_VF)
995                                 is_vf = true;
996
997                         type = LE16_TO_CPU(ele->vsi_port_num) >>
998                                 ICE_AQC_GET_SW_CONF_RESP_TYPE_S;
999
1000                         switch (type) {
1001                         case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
1002                         case ICE_AQC_GET_SW_CONF_RESP_VIRT_PORT:
1003                                 if (j == num_total_ports) {
1004                                         ice_debug(hw, ICE_DBG_SW,
1005                                                   "more ports than expected\n");
1006                                         status = ICE_ERR_CFG;
1007                                         goto out;
1008                                 }
1009                                 ice_init_port_info(hw->port_info,
1010                                                    vsi_port_num, type, swid,
1011                                                    pf_vf_num, is_vf);
1012                                 j++;
1013                                 break;
1014                         default:
1015                                 break;
1016                         }
1017                 }
1018         } while (req_desc && !status);
1019
1020
1021 out:
1022         ice_free(hw, (void *)rbuf);
1023         return status;
1024 }
1025
1026
1027 /**
1028  * ice_fill_sw_info - Helper function to populate lb_en and lan_en
1029  * @hw: pointer to the hardware structure
1030  * @fi: filter info structure to fill/update
1031  *
1032  * This helper function populates the lb_en and lan_en elements of the provided
1033  * ice_fltr_info struct using the switch's type and characteristics of the
1034  * switch rule being configured.
1035  */
1036 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
1037 {
1038         fi->lb_en = false;
1039         fi->lan_en = false;
1040         if ((fi->flag & ICE_FLTR_TX) &&
1041             (fi->fltr_act == ICE_FWD_TO_VSI ||
1042              fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1043              fi->fltr_act == ICE_FWD_TO_Q ||
1044              fi->fltr_act == ICE_FWD_TO_QGRP)) {
1045                 /* Setting LB for prune actions will result in replicated
1046                  * packets to the internal switch that will be dropped.
1047                  */
1048                 if (fi->lkup_type != ICE_SW_LKUP_VLAN)
1049                         fi->lb_en = true;
1050
1051                 /* Set lan_en to TRUE if
1052                  * 1. The switch is a VEB AND
1053                  * 2
1054                  * 2.1 The lookup is a directional lookup like ethertype,
1055                  * promiscuous, ethertype-MAC, promiscuous-VLAN
1056                  * and default-port OR
1057                  * 2.2 The lookup is VLAN, OR
1058                  * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
1059                  * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
1060                  *
1061                  * OR
1062                  *
1063                  * The switch is a VEPA.
1064                  *
1065                  * In all other cases, the LAN enable has to be set to false.
1066                  */
1067                 if (hw->evb_veb) {
1068                         if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1069                             fi->lkup_type == ICE_SW_LKUP_PROMISC ||
1070                             fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1071                             fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1072                             fi->lkup_type == ICE_SW_LKUP_DFLT ||
1073                             fi->lkup_type == ICE_SW_LKUP_VLAN ||
1074                             (fi->lkup_type == ICE_SW_LKUP_MAC &&
1075                              !IS_UNICAST_ETHER_ADDR(fi->l_data.mac.mac_addr)) ||
1076                             (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
1077                              !IS_UNICAST_ETHER_ADDR(fi->l_data.mac.mac_addr)))
1078                                 fi->lan_en = true;
1079                 } else {
1080                         fi->lan_en = true;
1081                 }
1082         }
1083 }
1084
1085 /**
1086  * ice_ilog2 - Calculates integer log base 2 of a number
1087  * @n: number on which to perform operation
1088  */
1089 static int ice_ilog2(u64 n)
1090 {
1091         int i;
1092
1093         for (i = 63; i >= 0; i--)
1094                 if (((u64)1 << i) & n)
1095                         return i;
1096
1097         return -1;
1098 }
1099
1100
1101 /**
1102  * ice_fill_sw_rule - Helper function to fill switch rule structure
1103  * @hw: pointer to the hardware structure
1104  * @f_info: entry containing packet forwarding information
1105  * @s_rule: switch rule structure to be filled in based on mac_entry
1106  * @opc: switch rules population command type - pass in the command opcode
1107  */
1108 static void
1109 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
1110                  struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
1111 {
1112         u16 vlan_id = ICE_MAX_VLAN_ID + 1;
1113         void *daddr = NULL;
1114         u16 eth_hdr_sz;
1115         u8 *eth_hdr;
1116         u32 act = 0;
1117         __be16 *off;
1118         u8 q_rgn;
1119
1120
1121         if (opc == ice_aqc_opc_remove_sw_rules) {
1122                 s_rule->pdata.lkup_tx_rx.act = 0;
1123                 s_rule->pdata.lkup_tx_rx.index =
1124                         CPU_TO_LE16(f_info->fltr_rule_id);
1125                 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
1126                 return;
1127         }
1128
1129         eth_hdr_sz = sizeof(dummy_eth_header);
1130         eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
1131
1132         /* initialize the ether header with a dummy header */
1133         ice_memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz, ICE_NONDMA_TO_NONDMA);
1134         ice_fill_sw_info(hw, f_info);
1135
1136         switch (f_info->fltr_act) {
1137         case ICE_FWD_TO_VSI:
1138                 act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
1139                         ICE_SINGLE_ACT_VSI_ID_M;
1140                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1141                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1142                                 ICE_SINGLE_ACT_VALID_BIT;
1143                 break;
1144         case ICE_FWD_TO_VSI_LIST:
1145                 act |= ICE_SINGLE_ACT_VSI_LIST;
1146                 act |= (f_info->fwd_id.vsi_list_id <<
1147                         ICE_SINGLE_ACT_VSI_LIST_ID_S) &
1148                         ICE_SINGLE_ACT_VSI_LIST_ID_M;
1149                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1150                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1151                                 ICE_SINGLE_ACT_VALID_BIT;
1152                 break;
1153         case ICE_FWD_TO_Q:
1154                 act |= ICE_SINGLE_ACT_TO_Q;
1155                 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1156                         ICE_SINGLE_ACT_Q_INDEX_M;
1157                 break;
1158         case ICE_DROP_PACKET:
1159                 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
1160                         ICE_SINGLE_ACT_VALID_BIT;
1161                 break;
1162         case ICE_FWD_TO_QGRP:
1163                 q_rgn = f_info->qgrp_size > 0 ?
1164                         (u8)ice_ilog2(f_info->qgrp_size) : 0;
1165                 act |= ICE_SINGLE_ACT_TO_Q;
1166                 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1167                         ICE_SINGLE_ACT_Q_INDEX_M;
1168                 act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
1169                         ICE_SINGLE_ACT_Q_REGION_M;
1170                 break;
1171         default:
1172                 return;
1173         }
1174
1175         if (f_info->lb_en)
1176                 act |= ICE_SINGLE_ACT_LB_ENABLE;
1177         if (f_info->lan_en)
1178                 act |= ICE_SINGLE_ACT_LAN_ENABLE;
1179
1180         switch (f_info->lkup_type) {
1181         case ICE_SW_LKUP_MAC:
1182                 daddr = f_info->l_data.mac.mac_addr;
1183                 break;
1184         case ICE_SW_LKUP_VLAN:
1185                 vlan_id = f_info->l_data.vlan.vlan_id;
1186                 if (f_info->fltr_act == ICE_FWD_TO_VSI ||
1187                     f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
1188                         act |= ICE_SINGLE_ACT_PRUNE;
1189                         act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
1190                 }
1191                 break;
1192         case ICE_SW_LKUP_ETHERTYPE_MAC:
1193                 daddr = f_info->l_data.ethertype_mac.mac_addr;
1194                 /* fall-through */
1195         case ICE_SW_LKUP_ETHERTYPE:
1196                 off = (__be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
1197                 *off = CPU_TO_BE16(f_info->l_data.ethertype_mac.ethertype);
1198                 break;
1199         case ICE_SW_LKUP_MAC_VLAN:
1200                 daddr = f_info->l_data.mac_vlan.mac_addr;
1201                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
1202                 break;
1203         case ICE_SW_LKUP_PROMISC_VLAN:
1204                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
1205                 /* fall-through */
1206         case ICE_SW_LKUP_PROMISC:
1207                 daddr = f_info->l_data.mac_vlan.mac_addr;
1208                 break;
1209         default:
1210                 break;
1211         }
1212
1213         s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
1214                 CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_RX) :
1215                 CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_TX);
1216
1217         /* Recipe set depending on lookup type */
1218         s_rule->pdata.lkup_tx_rx.recipe_id = CPU_TO_LE16(f_info->lkup_type);
1219         s_rule->pdata.lkup_tx_rx.src = CPU_TO_LE16(f_info->src);
1220         s_rule->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1221
1222         if (daddr)
1223                 ice_memcpy(eth_hdr + ICE_ETH_DA_OFFSET, daddr, ETH_ALEN,
1224                            ICE_NONDMA_TO_NONDMA);
1225
1226         if (!(vlan_id > ICE_MAX_VLAN_ID)) {
1227                 off = (__be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
1228                 *off = CPU_TO_BE16(vlan_id);
1229         }
1230
1231         /* Create the switch rule with the final dummy Ethernet header */
1232         if (opc != ice_aqc_opc_update_sw_rules)
1233                 s_rule->pdata.lkup_tx_rx.hdr_len = CPU_TO_LE16(eth_hdr_sz);
1234 }
1235
1236 /**
1237  * ice_add_marker_act
1238  * @hw: pointer to the hardware structure
1239  * @m_ent: the management entry for which sw marker needs to be added
1240  * @sw_marker: sw marker to tag the Rx descriptor with
1241  * @l_id: large action resource ID
1242  *
1243  * Create a large action to hold software marker and update the switch rule
1244  * entry pointed by m_ent with newly created large action
1245  */
1246 static enum ice_status
1247 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1248                    u16 sw_marker, u16 l_id)
1249 {
1250         struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
1251         /* For software marker we need 3 large actions
1252          * 1. FWD action: FWD TO VSI or VSI LIST
1253          * 2. GENERIC VALUE action to hold the profile ID
1254          * 3. GENERIC VALUE action to hold the software marker ID
1255          */
1256         const u16 num_lg_acts = 3;
1257         enum ice_status status;
1258         u16 lg_act_size;
1259         u16 rules_size;
1260         u32 act;
1261         u16 id;
1262
1263         if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1264                 return ICE_ERR_PARAM;
1265
1266         /* Create two back-to-back switch rules and submit them to the HW using
1267          * one memory buffer:
1268          *    1. Large Action
1269          *    2. Look up Tx Rx
1270          */
1271         lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
1272         rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1273         lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, rules_size);
1274         if (!lg_act)
1275                 return ICE_ERR_NO_MEMORY;
1276
1277         rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
1278
1279         /* Fill in the first switch rule i.e. large action */
1280         lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1281         lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1282         lg_act->pdata.lg_act.size = CPU_TO_LE16(num_lg_acts);
1283
1284         /* First action VSI forwarding or VSI list forwarding depending on how
1285          * many VSIs
1286          */
1287         id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
1288                 m_ent->fltr_info.fwd_id.hw_vsi_id;
1289
1290         act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1291         act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
1292                 ICE_LG_ACT_VSI_LIST_ID_M;
1293         if (m_ent->vsi_count > 1)
1294                 act |= ICE_LG_ACT_VSI_LIST;
1295         lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1296
1297         /* Second action descriptor type */
1298         act = ICE_LG_ACT_GENERIC;
1299
1300         act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
1301         lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1302
1303         act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
1304                ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
1305
1306         /* Third action Marker value */
1307         act |= ICE_LG_ACT_GENERIC;
1308         act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
1309                 ICE_LG_ACT_GENERIC_VALUE_M;
1310
1311         lg_act->pdata.lg_act.act[2] = CPU_TO_LE32(act);
1312
1313         /* call the fill switch rule to fill the lookup Tx Rx structure */
1314         ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1315                          ice_aqc_opc_update_sw_rules);
1316
1317         /* Update the action to point to the large action ID */
1318         rx_tx->pdata.lkup_tx_rx.act =
1319                 CPU_TO_LE32(ICE_SINGLE_ACT_PTR |
1320                             ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
1321                              ICE_SINGLE_ACT_PTR_VAL_M));
1322
1323         /* Use the filter rule ID of the previously created rule with single
1324          * act. Once the update happens, hardware will treat this as large
1325          * action
1326          */
1327         rx_tx->pdata.lkup_tx_rx.index =
1328                 CPU_TO_LE16(m_ent->fltr_info.fltr_rule_id);
1329
1330         status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1331                                  ice_aqc_opc_update_sw_rules, NULL);
1332         if (!status) {
1333                 m_ent->lg_act_idx = l_id;
1334                 m_ent->sw_marker_id = sw_marker;
1335         }
1336
1337         ice_free(hw, lg_act);
1338         return status;
1339 }
1340
1341 /**
1342  * ice_add_counter_act - add/update filter rule with counter action
1343  * @hw: pointer to the hardware structure
1344  * @m_ent: the management entry for which counter needs to be added
1345  * @counter_id: VLAN counter ID returned as part of allocate resource
1346  * @l_id: large action resource ID
1347  */
1348 static enum ice_status
1349 ice_add_counter_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1350                     u16 counter_id, u16 l_id)
1351 {
1352         struct ice_aqc_sw_rules_elem *lg_act;
1353         struct ice_aqc_sw_rules_elem *rx_tx;
1354         enum ice_status status;
1355         /* 2 actions will be added while adding a large action counter */
1356         const int num_acts = 2;
1357         u16 lg_act_size;
1358         u16 rules_size;
1359         u16 f_rule_id;
1360         u32 act;
1361         u16 id;
1362
1363         if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1364                 return ICE_ERR_PARAM;
1365
1366         /* Create two back-to-back switch rules and submit them to the HW using
1367          * one memory buffer:
1368          * 1. Large Action
1369          * 2. Look up Tx Rx
1370          */
1371         lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_acts);
1372         rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1373         lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw,
1374                                                                  rules_size);
1375         if (!lg_act)
1376                 return ICE_ERR_NO_MEMORY;
1377
1378         rx_tx = (struct ice_aqc_sw_rules_elem *)
1379                 ((u8 *)lg_act + lg_act_size);
1380
1381         /* Fill in the first switch rule i.e. large action */
1382         lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1383         lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1384         lg_act->pdata.lg_act.size = CPU_TO_LE16(num_acts);
1385
1386         /* First action VSI forwarding or VSI list forwarding depending on how
1387          * many VSIs
1388          */
1389         id = (m_ent->vsi_count > 1) ?  m_ent->fltr_info.fwd_id.vsi_list_id :
1390                 m_ent->fltr_info.fwd_id.hw_vsi_id;
1391
1392         act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1393         act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
1394                 ICE_LG_ACT_VSI_LIST_ID_M;
1395         if (m_ent->vsi_count > 1)
1396                 act |= ICE_LG_ACT_VSI_LIST;
1397         lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1398
1399         /* Second action counter ID */
1400         act = ICE_LG_ACT_STAT_COUNT;
1401         act |= (counter_id << ICE_LG_ACT_STAT_COUNT_S) &
1402                 ICE_LG_ACT_STAT_COUNT_M;
1403         lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1404
1405         /* call the fill switch rule to fill the lookup Tx Rx structure */
1406         ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1407                          ice_aqc_opc_update_sw_rules);
1408
1409         act = ICE_SINGLE_ACT_PTR;
1410         act |= (l_id << ICE_SINGLE_ACT_PTR_VAL_S) & ICE_SINGLE_ACT_PTR_VAL_M;
1411         rx_tx->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1412
1413         /* Use the filter rule ID of the previously created rule with single
1414          * act. Once the update happens, hardware will treat this as large
1415          * action
1416          */
1417         f_rule_id = m_ent->fltr_info.fltr_rule_id;
1418         rx_tx->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_rule_id);
1419
1420         status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1421                                  ice_aqc_opc_update_sw_rules, NULL);
1422         if (!status) {
1423                 m_ent->lg_act_idx = l_id;
1424                 m_ent->counter_index = counter_id;
1425         }
1426
1427         ice_free(hw, lg_act);
1428         return status;
1429 }
1430
1431 /**
1432  * ice_create_vsi_list_map
1433  * @hw: pointer to the hardware structure
1434  * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
1435  * @num_vsi: number of VSI handles in the array
1436  * @vsi_list_id: VSI list ID generated as part of allocate resource
1437  *
1438  * Helper function to create a new entry of VSI list ID to VSI mapping
1439  * using the given VSI list ID
1440  */
1441 static struct ice_vsi_list_map_info *
1442 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1443                         u16 vsi_list_id)
1444 {
1445         struct ice_switch_info *sw = hw->switch_info;
1446         struct ice_vsi_list_map_info *v_map;
1447         int i;
1448
1449         v_map = (struct ice_vsi_list_map_info *)ice_calloc(hw, 1,
1450                 sizeof(*v_map));
1451         if (!v_map)
1452                 return NULL;
1453
1454         v_map->vsi_list_id = vsi_list_id;
1455         v_map->ref_cnt = 1;
1456         for (i = 0; i < num_vsi; i++)
1457                 ice_set_bit(vsi_handle_arr[i], v_map->vsi_map);
1458
1459         LIST_ADD(&v_map->list_entry, &sw->vsi_list_map_head);
1460         return v_map;
1461 }
1462
1463 /**
1464  * ice_update_vsi_list_rule
1465  * @hw: pointer to the hardware structure
1466  * @vsi_handle_arr: array of VSI handles to form a VSI list
1467  * @num_vsi: number of VSI handles in the array
1468  * @vsi_list_id: VSI list ID generated as part of allocate resource
1469  * @remove: Boolean value to indicate if this is a remove action
1470  * @opc: switch rules population command type - pass in the command opcode
1471  * @lkup_type: lookup type of the filter
1472  *
1473  * Call AQ command to add a new switch rule or update existing switch rule
1474  * using the given VSI list ID
1475  */
1476 static enum ice_status
1477 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1478                          u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
1479                          enum ice_sw_lkup_type lkup_type)
1480 {
1481         struct ice_aqc_sw_rules_elem *s_rule;
1482         enum ice_status status;
1483         u16 s_rule_size;
1484         u16 type;
1485         int i;
1486
1487         if (!num_vsi)
1488                 return ICE_ERR_PARAM;
1489
1490         if (lkup_type == ICE_SW_LKUP_MAC ||
1491             lkup_type == ICE_SW_LKUP_MAC_VLAN ||
1492             lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1493             lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1494             lkup_type == ICE_SW_LKUP_PROMISC ||
1495             lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
1496                 type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
1497                                 ICE_AQC_SW_RULES_T_VSI_LIST_SET;
1498         else if (lkup_type == ICE_SW_LKUP_VLAN)
1499                 type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
1500                                 ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
1501         else
1502                 return ICE_ERR_PARAM;
1503
1504         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
1505         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
1506         if (!s_rule)
1507                 return ICE_ERR_NO_MEMORY;
1508         for (i = 0; i < num_vsi; i++) {
1509                 if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
1510                         status = ICE_ERR_PARAM;
1511                         goto exit;
1512                 }
1513                 /* AQ call requires hw_vsi_id(s) */
1514                 s_rule->pdata.vsi_list.vsi[i] =
1515                         CPU_TO_LE16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
1516         }
1517
1518         s_rule->type = CPU_TO_LE16(type);
1519         s_rule->pdata.vsi_list.number_vsi = CPU_TO_LE16(num_vsi);
1520         s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
1521
1522         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
1523
1524 exit:
1525         ice_free(hw, s_rule);
1526         return status;
1527 }
1528
1529 /**
1530  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
1531  * @hw: pointer to the HW struct
1532  * @vsi_handle_arr: array of VSI handles to form a VSI list
1533  * @num_vsi: number of VSI handles in the array
1534  * @vsi_list_id: stores the ID of the VSI list to be created
1535  * @lkup_type: switch rule filter's lookup type
1536  */
1537 static enum ice_status
1538 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1539                          u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1540 {
1541         enum ice_status status;
1542
1543         status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1544                                             ice_aqc_opc_alloc_res);
1545         if (status)
1546                 return status;
1547
1548         /* Update the newly created VSI list to include the specified VSIs */
1549         return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1550                                         *vsi_list_id, false,
1551                                         ice_aqc_opc_add_sw_rules, lkup_type);
1552 }
1553
1554 /**
1555  * ice_create_pkt_fwd_rule
1556  * @hw: pointer to the hardware structure
1557  * @f_entry: entry containing packet forwarding information
1558  *
1559  * Create switch rule with given filter information and add an entry
1560  * to the corresponding filter management list to track this switch rule
1561  * and VSI mapping
1562  */
1563 static enum ice_status
1564 ice_create_pkt_fwd_rule(struct ice_hw *hw,
1565                         struct ice_fltr_list_entry *f_entry)
1566 {
1567         struct ice_fltr_mgmt_list_entry *fm_entry;
1568         struct ice_aqc_sw_rules_elem *s_rule;
1569         enum ice_sw_lkup_type l_type;
1570         struct ice_sw_recipe *recp;
1571         enum ice_status status;
1572
1573         s_rule = (struct ice_aqc_sw_rules_elem *)
1574                 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1575         if (!s_rule)
1576                 return ICE_ERR_NO_MEMORY;
1577         fm_entry = (struct ice_fltr_mgmt_list_entry *)
1578                    ice_malloc(hw, sizeof(*fm_entry));
1579         if (!fm_entry) {
1580                 status = ICE_ERR_NO_MEMORY;
1581                 goto ice_create_pkt_fwd_rule_exit;
1582         }
1583
1584         fm_entry->fltr_info = f_entry->fltr_info;
1585
1586         /* Initialize all the fields for the management entry */
1587         fm_entry->vsi_count = 1;
1588         fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1589         fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1590         fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1591
1592         ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1593                          ice_aqc_opc_add_sw_rules);
1594
1595         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1596                                  ice_aqc_opc_add_sw_rules, NULL);
1597         if (status) {
1598                 ice_free(hw, fm_entry);
1599                 goto ice_create_pkt_fwd_rule_exit;
1600         }
1601
1602         f_entry->fltr_info.fltr_rule_id =
1603                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1604         fm_entry->fltr_info.fltr_rule_id =
1605                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1606
1607         /* The book keeping entries will get removed when base driver
1608          * calls remove filter AQ command
1609          */
1610         l_type = fm_entry->fltr_info.lkup_type;
1611         recp = &hw->switch_info->recp_list[l_type];
1612         LIST_ADD(&fm_entry->list_entry, &recp->filt_rules);
1613
1614 ice_create_pkt_fwd_rule_exit:
1615         ice_free(hw, s_rule);
1616         return status;
1617 }
1618
1619 /**
1620  * ice_update_pkt_fwd_rule
1621  * @hw: pointer to the hardware structure
1622  * @f_info: filter information for switch rule
1623  *
1624  * Call AQ command to update a previously created switch rule with a
1625  * VSI list ID
1626  */
1627 static enum ice_status
1628 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1629 {
1630         struct ice_aqc_sw_rules_elem *s_rule;
1631         enum ice_status status;
1632
1633         s_rule = (struct ice_aqc_sw_rules_elem *)
1634                 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1635         if (!s_rule)
1636                 return ICE_ERR_NO_MEMORY;
1637
1638         ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
1639
1640         s_rule->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_info->fltr_rule_id);
1641
1642         /* Update switch rule with new rule set to forward VSI list */
1643         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1644                                  ice_aqc_opc_update_sw_rules, NULL);
1645
1646         ice_free(hw, s_rule);
1647         return status;
1648 }
1649
1650 /**
1651  * ice_update_sw_rule_bridge_mode
1652  * @hw: pointer to the HW struct
1653  *
1654  * Updates unicast switch filter rules based on VEB/VEPA mode
1655  */
1656 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
1657 {
1658         struct ice_switch_info *sw = hw->switch_info;
1659         struct ice_fltr_mgmt_list_entry *fm_entry;
1660         enum ice_status status = ICE_SUCCESS;
1661         struct LIST_HEAD_TYPE *rule_head;
1662         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
1663
1664         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1665         rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1666
1667         ice_acquire_lock(rule_lock);
1668         LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry,
1669                             list_entry) {
1670                 struct ice_fltr_info *fi = &fm_entry->fltr_info;
1671                 u8 *addr = fi->l_data.mac.mac_addr;
1672
1673                 /* Update unicast Tx rules to reflect the selected
1674                  * VEB/VEPA mode
1675                  */
1676                 if ((fi->flag & ICE_FLTR_TX) && IS_UNICAST_ETHER_ADDR(addr) &&
1677                     (fi->fltr_act == ICE_FWD_TO_VSI ||
1678                      fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1679                      fi->fltr_act == ICE_FWD_TO_Q ||
1680                      fi->fltr_act == ICE_FWD_TO_QGRP)) {
1681                         status = ice_update_pkt_fwd_rule(hw, fi);
1682                         if (status)
1683                                 break;
1684                 }
1685         }
1686
1687         ice_release_lock(rule_lock);
1688
1689         return status;
1690 }
1691
1692 /**
1693  * ice_add_update_vsi_list
1694  * @hw: pointer to the hardware structure
1695  * @m_entry: pointer to current filter management list entry
1696  * @cur_fltr: filter information from the book keeping entry
1697  * @new_fltr: filter information with the new VSI to be added
1698  *
1699  * Call AQ command to add or update previously created VSI list with new VSI.
1700  *
1701  * Helper function to do book keeping associated with adding filter information
1702  * The algorithm to do the book keeping is described below :
1703  * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
1704  *      if only one VSI has been added till now
1705  *              Allocate a new VSI list and add two VSIs
1706  *              to this list using switch rule command
1707  *              Update the previously created switch rule with the
1708  *              newly created VSI list ID
1709  *      if a VSI list was previously created
1710  *              Add the new VSI to the previously created VSI list set
1711  *              using the update switch rule command
1712  */
1713 static enum ice_status
1714 ice_add_update_vsi_list(struct ice_hw *hw,
1715                         struct ice_fltr_mgmt_list_entry *m_entry,
1716                         struct ice_fltr_info *cur_fltr,
1717                         struct ice_fltr_info *new_fltr)
1718 {
1719         enum ice_status status = ICE_SUCCESS;
1720         u16 vsi_list_id = 0;
1721
1722         if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
1723              cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
1724                 return ICE_ERR_NOT_IMPL;
1725
1726         if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
1727              new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
1728             (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
1729              cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
1730                 return ICE_ERR_NOT_IMPL;
1731
1732         if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
1733                 /* Only one entry existed in the mapping and it was not already
1734                  * a part of a VSI list. So, create a VSI list with the old and
1735                  * new VSIs.
1736                  */
1737                 struct ice_fltr_info tmp_fltr;
1738                 u16 vsi_handle_arr[2];
1739
1740                 /* A rule already exists with the new VSI being added */
1741                 if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
1742                         return ICE_ERR_ALREADY_EXISTS;
1743
1744                 vsi_handle_arr[0] = cur_fltr->vsi_handle;
1745                 vsi_handle_arr[1] = new_fltr->vsi_handle;
1746                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1747                                                   &vsi_list_id,
1748                                                   new_fltr->lkup_type);
1749                 if (status)
1750                         return status;
1751
1752                 tmp_fltr = *new_fltr;
1753                 tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
1754                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1755                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1756                 /* Update the previous switch rule of "MAC forward to VSI" to
1757                  * "MAC fwd to VSI list"
1758                  */
1759                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1760                 if (status)
1761                         return status;
1762
1763                 cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1764                 cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1765                 m_entry->vsi_list_info =
1766                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1767                                                 vsi_list_id);
1768
1769                 /* If this entry was large action then the large action needs
1770                  * to be updated to point to FWD to VSI list
1771                  */
1772                 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1773                         status =
1774                             ice_add_marker_act(hw, m_entry,
1775                                                m_entry->sw_marker_id,
1776                                                m_entry->lg_act_idx);
1777         } else {
1778                 u16 vsi_handle = new_fltr->vsi_handle;
1779                 enum ice_adminq_opc opcode;
1780
1781                 if (!m_entry->vsi_list_info)
1782                         return ICE_ERR_CFG;
1783
1784                 /* A rule already exists with the new VSI being added */
1785                 if (ice_is_bit_set(m_entry->vsi_list_info->vsi_map, vsi_handle))
1786                         return ICE_SUCCESS;
1787
1788                 /* Update the previously created VSI list set with
1789                  * the new VSI ID passed in
1790                  */
1791                 vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1792                 opcode = ice_aqc_opc_update_sw_rules;
1793
1794                 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
1795                                                   vsi_list_id, false, opcode,
1796                                                   new_fltr->lkup_type);
1797                 /* update VSI list mapping info with new VSI ID */
1798                 if (!status)
1799                         ice_set_bit(vsi_handle,
1800                                     m_entry->vsi_list_info->vsi_map);
1801         }
1802         if (!status)
1803                 m_entry->vsi_count++;
1804         return status;
1805 }
1806
1807 /**
1808  * ice_find_rule_entry - Search a rule entry
1809  * @hw: pointer to the hardware structure
1810  * @recp_id: lookup type for which the specified rule needs to be searched
1811  * @f_info: rule information
1812  *
1813  * Helper function to search for a given rule entry
1814  * Returns pointer to entry storing the rule if found
1815  */
1816 static struct ice_fltr_mgmt_list_entry *
1817 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
1818 {
1819         struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
1820         struct ice_switch_info *sw = hw->switch_info;
1821         struct LIST_HEAD_TYPE *list_head;
1822
1823         list_head = &sw->recp_list[recp_id].filt_rules;
1824         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
1825                             list_entry) {
1826                 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
1827                             sizeof(f_info->l_data)) &&
1828                     f_info->flag == list_itr->fltr_info.flag) {
1829                         ret = list_itr;
1830                         break;
1831                 }
1832         }
1833         return ret;
1834 }
1835
1836 /**
1837  * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
1838  * @hw: pointer to the hardware structure
1839  * @recp_id: lookup type for which VSI lists needs to be searched
1840  * @vsi_handle: VSI handle to be found in VSI list
1841  * @vsi_list_id: VSI list ID found containing vsi_handle
1842  *
1843  * Helper function to search a VSI list with single entry containing given VSI
1844  * handle element. This can be extended further to search VSI list with more
1845  * than 1 vsi_count. Returns pointer to VSI list entry if found.
1846  */
1847 static struct ice_vsi_list_map_info *
1848 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
1849                         u16 *vsi_list_id)
1850 {
1851         struct ice_vsi_list_map_info *map_info = NULL;
1852         struct ice_switch_info *sw = hw->switch_info;
1853         struct ice_fltr_mgmt_list_entry *list_itr;
1854         struct LIST_HEAD_TYPE *list_head;
1855
1856         list_head = &sw->recp_list[recp_id].filt_rules;
1857         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
1858                             list_entry) {
1859                 if (list_itr->vsi_count == 1 && list_itr->vsi_list_info) {
1860                         map_info = list_itr->vsi_list_info;
1861                         if (ice_is_bit_set(map_info->vsi_map, vsi_handle)) {
1862                                 *vsi_list_id = map_info->vsi_list_id;
1863                                 return map_info;
1864                         }
1865                 }
1866         }
1867         return NULL;
1868 }
1869
1870 /**
1871  * ice_add_rule_internal - add rule for a given lookup type
1872  * @hw: pointer to the hardware structure
1873  * @recp_id: lookup type (recipe ID) for which rule has to be added
1874  * @f_entry: structure containing MAC forwarding information
1875  *
1876  * Adds or updates the rule lists for a given recipe
1877  */
1878 static enum ice_status
1879 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
1880                       struct ice_fltr_list_entry *f_entry)
1881 {
1882         struct ice_switch_info *sw = hw->switch_info;
1883         struct ice_fltr_info *new_fltr, *cur_fltr;
1884         struct ice_fltr_mgmt_list_entry *m_entry;
1885         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
1886         enum ice_status status = ICE_SUCCESS;
1887
1888         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1889                 return ICE_ERR_PARAM;
1890
1891         /* Load the hw_vsi_id only if the fwd action is fwd to VSI */
1892         if (f_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI)
1893                 f_entry->fltr_info.fwd_id.hw_vsi_id =
1894                         ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1895
1896         rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1897
1898         ice_acquire_lock(rule_lock);
1899         new_fltr = &f_entry->fltr_info;
1900         if (new_fltr->flag & ICE_FLTR_RX)
1901                 new_fltr->src = hw->port_info->lport;
1902         else if (new_fltr->flag & ICE_FLTR_TX)
1903                 new_fltr->src =
1904                         ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1905
1906         m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
1907         if (!m_entry) {
1908                 ice_release_lock(rule_lock);
1909                 return ice_create_pkt_fwd_rule(hw, f_entry);
1910         }
1911
1912         cur_fltr = &m_entry->fltr_info;
1913         status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
1914         ice_release_lock(rule_lock);
1915
1916         return status;
1917 }
1918
1919 /**
1920  * ice_remove_vsi_list_rule
1921  * @hw: pointer to the hardware structure
1922  * @vsi_list_id: VSI list ID generated as part of allocate resource
1923  * @lkup_type: switch rule filter lookup type
1924  *
1925  * The VSI list should be emptied before this function is called to remove the
1926  * VSI list.
1927  */
1928 static enum ice_status
1929 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1930                          enum ice_sw_lkup_type lkup_type)
1931 {
1932         struct ice_aqc_sw_rules_elem *s_rule;
1933         enum ice_status status;
1934         u16 s_rule_size;
1935
1936         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
1937         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
1938         if (!s_rule)
1939                 return ICE_ERR_NO_MEMORY;
1940
1941         s_rule->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
1942         s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
1943
1944         /* Free the vsi_list resource that we allocated. It is assumed that the
1945          * list is empty at this point.
1946          */
1947         status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
1948                                             ice_aqc_opc_free_res);
1949
1950         ice_free(hw, s_rule);
1951         return status;
1952 }
1953
1954 /**
1955  * ice_rem_update_vsi_list
1956  * @hw: pointer to the hardware structure
1957  * @vsi_handle: VSI handle of the VSI to remove
1958  * @fm_list: filter management entry for which the VSI list management needs to
1959  *           be done
1960  */
1961 static enum ice_status
1962 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
1963                         struct ice_fltr_mgmt_list_entry *fm_list)
1964 {
1965         enum ice_sw_lkup_type lkup_type;
1966         enum ice_status status = ICE_SUCCESS;
1967         u16 vsi_list_id;
1968
1969         if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
1970             fm_list->vsi_count == 0)
1971                 return ICE_ERR_PARAM;
1972
1973         /* A rule with the VSI being removed does not exist */
1974         if (!ice_is_bit_set(fm_list->vsi_list_info->vsi_map, vsi_handle))
1975                 return ICE_ERR_DOES_NOT_EXIST;
1976
1977         lkup_type = fm_list->fltr_info.lkup_type;
1978         vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
1979         status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
1980                                           ice_aqc_opc_update_sw_rules,
1981                                           lkup_type);
1982         if (status)
1983                 return status;
1984
1985         fm_list->vsi_count--;
1986         ice_clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
1987
1988         if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
1989                 struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
1990                 struct ice_vsi_list_map_info *vsi_list_info =
1991                         fm_list->vsi_list_info;
1992                 u16 rem_vsi_handle;
1993
1994                 rem_vsi_handle = ice_find_first_bit(vsi_list_info->vsi_map,
1995                                                     ICE_MAX_VSI);
1996                 if (!ice_is_vsi_valid(hw, rem_vsi_handle))
1997                         return ICE_ERR_OUT_OF_RANGE;
1998
1999                 /* Make sure VSI list is empty before removing it below */
2000                 status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
2001                                                   vsi_list_id, true,
2002                                                   ice_aqc_opc_update_sw_rules,
2003                                                   lkup_type);
2004                 if (status)
2005                         return status;
2006
2007                 tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
2008                 tmp_fltr_info.fwd_id.hw_vsi_id =
2009                         ice_get_hw_vsi_num(hw, rem_vsi_handle);
2010                 tmp_fltr_info.vsi_handle = rem_vsi_handle;
2011                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
2012                 if (status) {
2013                         ice_debug(hw, ICE_DBG_SW,
2014                                   "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
2015                                   tmp_fltr_info.fwd_id.hw_vsi_id, status);
2016                         return status;
2017                 }
2018
2019                 fm_list->fltr_info = tmp_fltr_info;
2020         }
2021
2022         if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
2023             (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
2024                 struct ice_vsi_list_map_info *vsi_list_info =
2025                         fm_list->vsi_list_info;
2026
2027                 /* Remove the VSI list since it is no longer used */
2028                 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
2029                 if (status) {
2030                         ice_debug(hw, ICE_DBG_SW,
2031                                   "Failed to remove VSI list %d, error %d\n",
2032                                   vsi_list_id, status);
2033                         return status;
2034                 }
2035
2036                 LIST_DEL(&vsi_list_info->list_entry);
2037                 ice_free(hw, vsi_list_info);
2038                 fm_list->vsi_list_info = NULL;
2039         }
2040
2041         return status;
2042 }
2043
2044 /**
2045  * ice_remove_rule_internal - Remove a filter rule of a given type
2046  *
2047  * @hw: pointer to the hardware structure
2048  * @recp_id: recipe ID for which the rule needs to removed
2049  * @f_entry: rule entry containing filter information
2050  */
2051 static enum ice_status
2052 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
2053                          struct ice_fltr_list_entry *f_entry)
2054 {
2055         struct ice_switch_info *sw = hw->switch_info;
2056         struct ice_fltr_mgmt_list_entry *list_elem;
2057         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2058         enum ice_status status = ICE_SUCCESS;
2059         bool remove_rule = false;
2060         u16 vsi_handle;
2061
2062         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2063                 return ICE_ERR_PARAM;
2064         f_entry->fltr_info.fwd_id.hw_vsi_id =
2065                 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2066
2067         rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
2068         ice_acquire_lock(rule_lock);
2069         list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
2070         if (!list_elem) {
2071                 status = ICE_ERR_DOES_NOT_EXIST;
2072                 goto exit;
2073         }
2074
2075         if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
2076                 remove_rule = true;
2077         } else if (!list_elem->vsi_list_info) {
2078                 status = ICE_ERR_DOES_NOT_EXIST;
2079                 goto exit;
2080         } else if (list_elem->vsi_list_info->ref_cnt > 1) {
2081                 /* a ref_cnt > 1 indicates that the vsi_list is being
2082                  * shared by multiple rules. Decrement the ref_cnt and
2083                  * remove this rule, but do not modify the list, as it
2084                  * is in-use by other rules.
2085                  */
2086                 list_elem->vsi_list_info->ref_cnt--;
2087                 remove_rule = true;
2088         } else {
2089                 /* a ref_cnt of 1 indicates the vsi_list is only used
2090                  * by one rule. However, the original removal request is only
2091                  * for a single VSI. Update the vsi_list first, and only
2092                  * remove the rule if there are no further VSIs in this list.
2093                  */
2094                 vsi_handle = f_entry->fltr_info.vsi_handle;
2095                 status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
2096                 if (status)
2097                         goto exit;
2098                 /* if VSI count goes to zero after updating the VSI list */
2099                 if (list_elem->vsi_count == 0)
2100                         remove_rule = true;
2101         }
2102
2103         if (remove_rule) {
2104                 /* Remove the lookup rule */
2105                 struct ice_aqc_sw_rules_elem *s_rule;
2106
2107                 s_rule = (struct ice_aqc_sw_rules_elem *)
2108                         ice_malloc(hw, ICE_SW_RULE_RX_TX_NO_HDR_SIZE);
2109                 if (!s_rule) {
2110                         status = ICE_ERR_NO_MEMORY;
2111                         goto exit;
2112                 }
2113
2114                 ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
2115                                  ice_aqc_opc_remove_sw_rules);
2116
2117                 status = ice_aq_sw_rules(hw, s_rule,
2118                                          ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
2119                                          ice_aqc_opc_remove_sw_rules, NULL);
2120                 if (status)
2121                         goto exit;
2122
2123                 /* Remove a book keeping from the list */
2124                 ice_free(hw, s_rule);
2125
2126                 LIST_DEL(&list_elem->list_entry);
2127                 ice_free(hw, list_elem);
2128         }
2129 exit:
2130         ice_release_lock(rule_lock);
2131         return status;
2132 }
2133
2134 /**
2135  * ice_aq_get_res_alloc - get allocated resources
2136  * @hw: pointer to the HW struct
2137  * @num_entries: pointer to u16 to store the number of resource entries returned
2138  * @buf: pointer to user-supplied buffer
2139  * @buf_size: size of buff
2140  * @cd: pointer to command details structure or NULL
2141  *
2142  * The user-supplied buffer must be large enough to store the resource
2143  * information for all resource types. Each resource type is an
2144  * ice_aqc_get_res_resp_data_elem structure.
2145  */
2146 enum ice_status
2147 ice_aq_get_res_alloc(struct ice_hw *hw, u16 *num_entries, void *buf,
2148                      u16 buf_size, struct ice_sq_cd *cd)
2149 {
2150         struct ice_aqc_get_res_alloc *resp;
2151         enum ice_status status;
2152         struct ice_aq_desc desc;
2153
2154         if (!buf)
2155                 return ICE_ERR_BAD_PTR;
2156
2157         if (buf_size < ICE_AQ_GET_RES_ALLOC_BUF_LEN)
2158                 return ICE_ERR_INVAL_SIZE;
2159
2160         resp = &desc.params.get_res;
2161
2162         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_res_alloc);
2163         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2164
2165         if (!status && num_entries)
2166                 *num_entries = LE16_TO_CPU(resp->resp_elem_num);
2167
2168         return status;
2169 }
2170
2171 /**
2172  * ice_aq_get_res_descs - get allocated resource descriptors
2173  * @hw: pointer to the hardware structure
2174  * @num_entries: number of resource entries in buffer
2175  * @buf: Indirect buffer to hold data parameters and response
2176  * @buf_size: size of buffer for indirect commands
2177  * @res_type: resource type
2178  * @res_shared: is resource shared
2179  * @desc_id: input - first desc ID to start; output - next desc ID
2180  * @cd: pointer to command details structure or NULL
2181  */
2182 enum ice_status
2183 ice_aq_get_res_descs(struct ice_hw *hw, u16 num_entries,
2184                      struct ice_aqc_get_allocd_res_desc_resp *buf,
2185                      u16 buf_size, u16 res_type, bool res_shared, u16 *desc_id,
2186                      struct ice_sq_cd *cd)
2187 {
2188         struct ice_aqc_get_allocd_res_desc *cmd;
2189         struct ice_aq_desc desc;
2190         enum ice_status status;
2191
2192         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_get_res_descs");
2193
2194         cmd = &desc.params.get_res_desc;
2195
2196         if (!buf)
2197                 return ICE_ERR_PARAM;
2198
2199         if (buf_size != (num_entries * sizeof(*buf)))
2200                 return ICE_ERR_PARAM;
2201
2202         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_allocd_res_desc);
2203
2204         cmd->ops.cmd.res = CPU_TO_LE16(((res_type << ICE_AQC_RES_TYPE_S) &
2205                                          ICE_AQC_RES_TYPE_M) | (res_shared ?
2206                                         ICE_AQC_RES_TYPE_FLAG_SHARED : 0));
2207         cmd->ops.cmd.first_desc = CPU_TO_LE16(*desc_id);
2208
2209         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
2210
2211         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2212         if (!status)
2213                 *desc_id = LE16_TO_CPU(cmd->ops.resp.next_desc);
2214
2215         return status;
2216 }
2217
2218 /**
2219  * ice_add_mac - Add a MAC address based filter rule
2220  * @hw: pointer to the hardware structure
2221  * @m_list: list of MAC addresses and forwarding information
2222  *
2223  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
2224  * multiple unicast addresses, the function assumes that all the
2225  * addresses are unique in a given add_mac call. It doesn't
2226  * check for duplicates in this case, removing duplicates from a given
2227  * list should be taken care of in the caller of this function.
2228  */
2229 enum ice_status
2230 ice_add_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2231 {
2232         struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
2233         struct ice_fltr_list_entry *m_list_itr;
2234         struct LIST_HEAD_TYPE *rule_head;
2235         u16 elem_sent, total_elem_left;
2236         struct ice_switch_info *sw;
2237         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2238         enum ice_status status = ICE_SUCCESS;
2239         u16 num_unicast = 0;
2240         u16 s_rule_size;
2241
2242         if (!m_list || !hw)
2243                 return ICE_ERR_PARAM;
2244         s_rule = NULL;
2245         sw = hw->switch_info;
2246         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2247         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2248                             list_entry) {
2249                 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
2250                 u16 vsi_handle;
2251                 u16 hw_vsi_id;
2252
2253                 m_list_itr->fltr_info.flag = ICE_FLTR_TX;
2254                 vsi_handle = m_list_itr->fltr_info.vsi_handle;
2255                 if (!ice_is_vsi_valid(hw, vsi_handle))
2256                         return ICE_ERR_PARAM;
2257                 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2258                 m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
2259                 /* update the src in case it is VSI num */
2260                 if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
2261                         return ICE_ERR_PARAM;
2262                 m_list_itr->fltr_info.src = hw_vsi_id;
2263                 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
2264                     IS_ZERO_ETHER_ADDR(add))
2265                         return ICE_ERR_PARAM;
2266                 if (IS_UNICAST_ETHER_ADDR(add) && !hw->ucast_shared) {
2267                         /* Don't overwrite the unicast address */
2268                         ice_acquire_lock(rule_lock);
2269                         if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
2270                                                 &m_list_itr->fltr_info)) {
2271                                 ice_release_lock(rule_lock);
2272                                 return ICE_ERR_ALREADY_EXISTS;
2273                         }
2274                         ice_release_lock(rule_lock);
2275                         num_unicast++;
2276                 } else if (IS_MULTICAST_ETHER_ADDR(add) ||
2277                            (IS_UNICAST_ETHER_ADDR(add) && hw->ucast_shared)) {
2278                         m_list_itr->status =
2279                                 ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
2280                                                       m_list_itr);
2281                         if (m_list_itr->status)
2282                                 return m_list_itr->status;
2283                 }
2284         }
2285
2286         ice_acquire_lock(rule_lock);
2287         /* Exit if no suitable entries were found for adding bulk switch rule */
2288         if (!num_unicast) {
2289                 status = ICE_SUCCESS;
2290                 goto ice_add_mac_exit;
2291         }
2292
2293         rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2294
2295         /* Allocate switch rule buffer for the bulk update for unicast */
2296         s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
2297         s_rule = (struct ice_aqc_sw_rules_elem *)
2298                 ice_calloc(hw, num_unicast, s_rule_size);
2299         if (!s_rule) {
2300                 status = ICE_ERR_NO_MEMORY;
2301                 goto ice_add_mac_exit;
2302         }
2303
2304         r_iter = s_rule;
2305         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2306                             list_entry) {
2307                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2308                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2309
2310                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2311                         ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
2312                                          ice_aqc_opc_add_sw_rules);
2313                         r_iter = (struct ice_aqc_sw_rules_elem *)
2314                                 ((u8 *)r_iter + s_rule_size);
2315                 }
2316         }
2317
2318         /* Call AQ bulk switch rule update for all unicast addresses */
2319         r_iter = s_rule;
2320         /* Call AQ switch rule in AQ_MAX chunk */
2321         for (total_elem_left = num_unicast; total_elem_left > 0;
2322              total_elem_left -= elem_sent) {
2323                 struct ice_aqc_sw_rules_elem *entry = r_iter;
2324
2325                 elem_sent = min(total_elem_left,
2326                                 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
2327                 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
2328                                          elem_sent, ice_aqc_opc_add_sw_rules,
2329                                          NULL);
2330                 if (status)
2331                         goto ice_add_mac_exit;
2332                 r_iter = (struct ice_aqc_sw_rules_elem *)
2333                         ((u8 *)r_iter + (elem_sent * s_rule_size));
2334         }
2335
2336         /* Fill up rule ID based on the value returned from FW */
2337         r_iter = s_rule;
2338         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2339                             list_entry) {
2340                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2341                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2342                 struct ice_fltr_mgmt_list_entry *fm_entry;
2343
2344                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2345                         f_info->fltr_rule_id =
2346                                 LE16_TO_CPU(r_iter->pdata.lkup_tx_rx.index);
2347                         f_info->fltr_act = ICE_FWD_TO_VSI;
2348                         /* Create an entry to track this MAC address */
2349                         fm_entry = (struct ice_fltr_mgmt_list_entry *)
2350                                 ice_malloc(hw, sizeof(*fm_entry));
2351                         if (!fm_entry) {
2352                                 status = ICE_ERR_NO_MEMORY;
2353                                 goto ice_add_mac_exit;
2354                         }
2355                         fm_entry->fltr_info = *f_info;
2356                         fm_entry->vsi_count = 1;
2357                         /* The book keeping entries will get removed when
2358                          * base driver calls remove filter AQ command
2359                          */
2360
2361                         LIST_ADD(&fm_entry->list_entry, rule_head);
2362                         r_iter = (struct ice_aqc_sw_rules_elem *)
2363                                 ((u8 *)r_iter + s_rule_size);
2364                 }
2365         }
2366
2367 ice_add_mac_exit:
2368         ice_release_lock(rule_lock);
2369         if (s_rule)
2370                 ice_free(hw, s_rule);
2371         return status;
2372 }
2373
2374 /**
2375  * ice_add_vlan_internal - Add one VLAN based filter rule
2376  * @hw: pointer to the hardware structure
2377  * @f_entry: filter entry containing one VLAN information
2378  */
2379 static enum ice_status
2380 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
2381 {
2382         struct ice_switch_info *sw = hw->switch_info;
2383         struct ice_fltr_mgmt_list_entry *v_list_itr;
2384         struct ice_fltr_info *new_fltr, *cur_fltr;
2385         enum ice_sw_lkup_type lkup_type;
2386         u16 vsi_list_id = 0, vsi_handle;
2387         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2388         enum ice_status status = ICE_SUCCESS;
2389
2390         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2391                 return ICE_ERR_PARAM;
2392
2393         f_entry->fltr_info.fwd_id.hw_vsi_id =
2394                 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2395         new_fltr = &f_entry->fltr_info;
2396
2397         /* VLAN ID should only be 12 bits */
2398         if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
2399                 return ICE_ERR_PARAM;
2400
2401         if (new_fltr->src_id != ICE_SRC_ID_VSI)
2402                 return ICE_ERR_PARAM;
2403
2404         new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
2405         lkup_type = new_fltr->lkup_type;
2406         vsi_handle = new_fltr->vsi_handle;
2407         rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2408         ice_acquire_lock(rule_lock);
2409         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
2410         if (!v_list_itr) {
2411                 struct ice_vsi_list_map_info *map_info = NULL;
2412
2413                 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
2414                         /* All VLAN pruning rules use a VSI list. Check if
2415                          * there is already a VSI list containing VSI that we
2416                          * want to add. If found, use the same vsi_list_id for
2417                          * this new VLAN rule or else create a new list.
2418                          */
2419                         map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
2420                                                            vsi_handle,
2421                                                            &vsi_list_id);
2422                         if (!map_info) {
2423                                 status = ice_create_vsi_list_rule(hw,
2424                                                                   &vsi_handle,
2425                                                                   1,
2426                                                                   &vsi_list_id,
2427                                                                   lkup_type);
2428                                 if (status)
2429                                         goto exit;
2430                         }
2431                         /* Convert the action to forwarding to a VSI list. */
2432                         new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2433                         new_fltr->fwd_id.vsi_list_id = vsi_list_id;
2434                 }
2435
2436                 status = ice_create_pkt_fwd_rule(hw, f_entry);
2437                 if (!status) {
2438                         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
2439                                                          new_fltr);
2440                         if (!v_list_itr) {
2441                                 status = ICE_ERR_DOES_NOT_EXIST;
2442                                 goto exit;
2443                         }
2444                         /* reuse VSI list for new rule and increment ref_cnt */
2445                         if (map_info) {
2446                                 v_list_itr->vsi_list_info = map_info;
2447                                 map_info->ref_cnt++;
2448                         } else {
2449                                 v_list_itr->vsi_list_info =
2450                                         ice_create_vsi_list_map(hw, &vsi_handle,
2451                                                                 1, vsi_list_id);
2452                         }
2453                 }
2454         } else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
2455                 /* Update existing VSI list to add new VSI ID only if it used
2456                  * by one VLAN rule.
2457                  */
2458                 cur_fltr = &v_list_itr->fltr_info;
2459                 status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
2460                                                  new_fltr);
2461         } else {
2462                 /* If VLAN rule exists and VSI list being used by this rule is
2463                  * referenced by more than 1 VLAN rule. Then create a new VSI
2464                  * list appending previous VSI with new VSI and update existing
2465                  * VLAN rule to point to new VSI list ID
2466                  */
2467                 struct ice_fltr_info tmp_fltr;
2468                 u16 vsi_handle_arr[2];
2469                 u16 cur_handle;
2470
2471                 /* Current implementation only supports reusing VSI list with
2472                  * one VSI count. We should never hit below condition
2473                  */
2474                 if (v_list_itr->vsi_count > 1 &&
2475                     v_list_itr->vsi_list_info->ref_cnt > 1) {
2476                         ice_debug(hw, ICE_DBG_SW,
2477                                   "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
2478                         status = ICE_ERR_CFG;
2479                         goto exit;
2480                 }
2481
2482                 cur_handle =
2483                         ice_find_first_bit(v_list_itr->vsi_list_info->vsi_map,
2484                                            ICE_MAX_VSI);
2485
2486                 /* A rule already exists with the new VSI being added */
2487                 if (cur_handle == vsi_handle) {
2488                         status = ICE_ERR_ALREADY_EXISTS;
2489                         goto exit;
2490                 }
2491
2492                 vsi_handle_arr[0] = cur_handle;
2493                 vsi_handle_arr[1] = vsi_handle;
2494                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2495                                                   &vsi_list_id, lkup_type);
2496                 if (status)
2497                         goto exit;
2498
2499                 tmp_fltr = v_list_itr->fltr_info;
2500                 tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
2501                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2502                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2503                 /* Update the previous switch rule to a new VSI list which
2504                  * includes current VSI that is requested
2505                  */
2506                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2507                 if (status)
2508                         goto exit;
2509
2510                 /* before overriding VSI list map info. decrement ref_cnt of
2511                  * previous VSI list
2512                  */
2513                 v_list_itr->vsi_list_info->ref_cnt--;
2514
2515                 /* now update to newly created list */
2516                 v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
2517                 v_list_itr->vsi_list_info =
2518                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2519                                                 vsi_list_id);
2520                 v_list_itr->vsi_count++;
2521         }
2522
2523 exit:
2524         ice_release_lock(rule_lock);
2525         return status;
2526 }
2527
2528 /**
2529  * ice_add_vlan - Add VLAN based filter rule
2530  * @hw: pointer to the hardware structure
2531  * @v_list: list of VLAN entries and forwarding information
2532  */
2533 enum ice_status
2534 ice_add_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2535 {
2536         struct ice_fltr_list_entry *v_list_itr;
2537
2538         if (!v_list || !hw)
2539                 return ICE_ERR_PARAM;
2540
2541         LIST_FOR_EACH_ENTRY(v_list_itr, v_list, ice_fltr_list_entry,
2542                             list_entry) {
2543                 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
2544                         return ICE_ERR_PARAM;
2545                 v_list_itr->fltr_info.flag = ICE_FLTR_TX;
2546                 v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
2547                 if (v_list_itr->status)
2548                         return v_list_itr->status;
2549         }
2550         return ICE_SUCCESS;
2551 }
2552
2553 #ifndef NO_MACVLAN_SUPPORT
2554 /**
2555  * ice_add_mac_vlan - Add MAC and VLAN pair based filter rule
2556  * @hw: pointer to the hardware structure
2557  * @mv_list: list of MAC and VLAN filters
2558  *
2559  * If the VSI on which the MAC-VLAN pair has to be added has Rx and Tx VLAN
2560  * pruning bits enabled, then it is the responsibility of the caller to make
2561  * sure to add a VLAN only filter on the same VSI. Packets belonging to that
2562  * VLAN won't be received on that VSI otherwise.
2563  */
2564 enum ice_status
2565 ice_add_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *mv_list)
2566 {
2567         struct ice_fltr_list_entry *mv_list_itr;
2568
2569         if (!mv_list || !hw)
2570                 return ICE_ERR_PARAM;
2571
2572         LIST_FOR_EACH_ENTRY(mv_list_itr, mv_list, ice_fltr_list_entry,
2573                             list_entry) {
2574                 enum ice_sw_lkup_type l_type =
2575                         mv_list_itr->fltr_info.lkup_type;
2576
2577                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
2578                         return ICE_ERR_PARAM;
2579                 mv_list_itr->fltr_info.flag = ICE_FLTR_TX;
2580                 mv_list_itr->status =
2581                         ice_add_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
2582                                               mv_list_itr);
2583                 if (mv_list_itr->status)
2584                         return mv_list_itr->status;
2585         }
2586         return ICE_SUCCESS;
2587 }
2588 #endif
2589
2590 /**
2591  * ice_add_eth_mac - Add ethertype and MAC based filter rule
2592  * @hw: pointer to the hardware structure
2593  * @em_list: list of ether type MAC filter, MAC is optional
2594  */
2595 enum ice_status
2596 ice_add_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2597 {
2598         struct ice_fltr_list_entry *em_list_itr;
2599
2600         LIST_FOR_EACH_ENTRY(em_list_itr, em_list, ice_fltr_list_entry,
2601                             list_entry) {
2602                 enum ice_sw_lkup_type l_type =
2603                         em_list_itr->fltr_info.lkup_type;
2604
2605                 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2606                     l_type != ICE_SW_LKUP_ETHERTYPE)
2607                         return ICE_ERR_PARAM;
2608
2609                 em_list_itr->fltr_info.flag = ICE_FLTR_TX;
2610                 em_list_itr->status = ice_add_rule_internal(hw, l_type,
2611                                                             em_list_itr);
2612                 if (em_list_itr->status)
2613                         return em_list_itr->status;
2614         }
2615         return ICE_SUCCESS;
2616 }
2617
2618 /**
2619  * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
2620  * @hw: pointer to the hardware structure
2621  * @em_list: list of ethertype or ethertype MAC entries
2622  */
2623 enum ice_status
2624 ice_remove_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2625 {
2626         struct ice_fltr_list_entry *em_list_itr, *tmp;
2627
2628         if (!em_list || !hw)
2629                 return ICE_ERR_PARAM;
2630
2631         LIST_FOR_EACH_ENTRY_SAFE(em_list_itr, tmp, em_list, ice_fltr_list_entry,
2632                                  list_entry) {
2633                 enum ice_sw_lkup_type l_type =
2634                         em_list_itr->fltr_info.lkup_type;
2635
2636                 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2637                     l_type != ICE_SW_LKUP_ETHERTYPE)
2638                         return ICE_ERR_PARAM;
2639
2640                 em_list_itr->status = ice_remove_rule_internal(hw, l_type,
2641                                                                em_list_itr);
2642                 if (em_list_itr->status)
2643                         return em_list_itr->status;
2644         }
2645         return ICE_SUCCESS;
2646 }
2647
2648
2649 /**
2650  * ice_rem_sw_rule_info
2651  * @hw: pointer to the hardware structure
2652  * @rule_head: pointer to the switch list structure that we want to delete
2653  */
2654 static void
2655 ice_rem_sw_rule_info(struct ice_hw *hw, struct LIST_HEAD_TYPE *rule_head)
2656 {
2657         if (!LIST_EMPTY(rule_head)) {
2658                 struct ice_fltr_mgmt_list_entry *entry;
2659                 struct ice_fltr_mgmt_list_entry *tmp;
2660
2661                 LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, rule_head,
2662                                          ice_fltr_mgmt_list_entry, list_entry) {
2663                         LIST_DEL(&entry->list_entry);
2664                         ice_free(hw, entry);
2665                 }
2666         }
2667 }
2668
2669
2670 /**
2671  * ice_rem_all_sw_rules_info
2672  * @hw: pointer to the hardware structure
2673  */
2674 void ice_rem_all_sw_rules_info(struct ice_hw *hw)
2675 {
2676         struct ice_switch_info *sw = hw->switch_info;
2677         u8 i;
2678
2679         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
2680                 struct LIST_HEAD_TYPE *rule_head;
2681
2682                 rule_head = &sw->recp_list[i].filt_rules;
2683                 if (!sw->recp_list[i].adv_rule)
2684                         ice_rem_sw_rule_info(hw, rule_head);
2685         }
2686 }
2687
2688 /**
2689  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
2690  * @pi: pointer to the port_info structure
2691  * @vsi_handle: VSI handle to set as default
2692  * @set: true to add the above mentioned switch rule, false to remove it
2693  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
2694  *
2695  * add filter rule to set/unset given VSI as default VSI for the switch
2696  * (represented by swid)
2697  */
2698 enum ice_status
2699 ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
2700                  u8 direction)
2701 {
2702         struct ice_aqc_sw_rules_elem *s_rule;
2703         struct ice_fltr_info f_info;
2704         struct ice_hw *hw = pi->hw;
2705         enum ice_adminq_opc opcode;
2706         enum ice_status status;
2707         u16 s_rule_size;
2708         u16 hw_vsi_id;
2709
2710         if (!ice_is_vsi_valid(hw, vsi_handle))
2711                 return ICE_ERR_PARAM;
2712         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2713
2714         s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
2715                             ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
2716         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
2717         if (!s_rule)
2718                 return ICE_ERR_NO_MEMORY;
2719
2720         ice_memset(&f_info, 0, sizeof(f_info), ICE_NONDMA_MEM);
2721
2722         f_info.lkup_type = ICE_SW_LKUP_DFLT;
2723         f_info.flag = direction;
2724         f_info.fltr_act = ICE_FWD_TO_VSI;
2725         f_info.fwd_id.hw_vsi_id = hw_vsi_id;
2726
2727         if (f_info.flag & ICE_FLTR_RX) {
2728                 f_info.src = pi->lport;
2729                 f_info.src_id = ICE_SRC_ID_LPORT;
2730                 if (!set)
2731                         f_info.fltr_rule_id =
2732                                 pi->dflt_rx_vsi_rule_id;
2733         } else if (f_info.flag & ICE_FLTR_TX) {
2734                 f_info.src_id = ICE_SRC_ID_VSI;
2735                 f_info.src = hw_vsi_id;
2736                 if (!set)
2737                         f_info.fltr_rule_id =
2738                                 pi->dflt_tx_vsi_rule_id;
2739         }
2740
2741         if (set)
2742                 opcode = ice_aqc_opc_add_sw_rules;
2743         else
2744                 opcode = ice_aqc_opc_remove_sw_rules;
2745
2746         ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
2747
2748         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
2749         if (status || !(f_info.flag & ICE_FLTR_TX_RX))
2750                 goto out;
2751         if (set) {
2752                 u16 index = LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
2753
2754                 if (f_info.flag & ICE_FLTR_TX) {
2755                         pi->dflt_tx_vsi_num = hw_vsi_id;
2756                         pi->dflt_tx_vsi_rule_id = index;
2757                 } else if (f_info.flag & ICE_FLTR_RX) {
2758                         pi->dflt_rx_vsi_num = hw_vsi_id;
2759                         pi->dflt_rx_vsi_rule_id = index;
2760                 }
2761         } else {
2762                 if (f_info.flag & ICE_FLTR_TX) {
2763                         pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
2764                         pi->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
2765                 } else if (f_info.flag & ICE_FLTR_RX) {
2766                         pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
2767                         pi->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
2768                 }
2769         }
2770
2771 out:
2772         ice_free(hw, s_rule);
2773         return status;
2774 }
2775
2776 /**
2777  * ice_remove_mac - remove a MAC address based filter rule
2778  * @hw: pointer to the hardware structure
2779  * @m_list: list of MAC addresses and forwarding information
2780  *
2781  * This function removes either a MAC filter rule or a specific VSI from a
2782  * VSI list for a multicast MAC address.
2783  *
2784  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
2785  * ice_add_mac. Caller should be aware that this call will only work if all
2786  * the entries passed into m_list were added previously. It will not attempt to
2787  * do a partial remove of entries that were found.
2788  */
2789 enum ice_status
2790 ice_remove_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2791 {
2792         struct ice_fltr_list_entry *list_itr, *tmp;
2793
2794         if (!m_list)
2795                 return ICE_ERR_PARAM;
2796
2797         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, m_list, ice_fltr_list_entry,
2798                                  list_entry) {
2799                 enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2800
2801                 if (l_type != ICE_SW_LKUP_MAC)
2802                         return ICE_ERR_PARAM;
2803                 list_itr->status = ice_remove_rule_internal(hw,
2804                                                             ICE_SW_LKUP_MAC,
2805                                                             list_itr);
2806                 if (list_itr->status)
2807                         return list_itr->status;
2808         }
2809         return ICE_SUCCESS;
2810 }
2811
2812 /**
2813  * ice_remove_vlan - Remove VLAN based filter rule
2814  * @hw: pointer to the hardware structure
2815  * @v_list: list of VLAN entries and forwarding information
2816  */
2817 enum ice_status
2818 ice_remove_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2819 {
2820         struct ice_fltr_list_entry *v_list_itr, *tmp;
2821
2822         if (!v_list || !hw)
2823                 return ICE_ERR_PARAM;
2824
2825         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2826                                  list_entry) {
2827                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2828
2829                 if (l_type != ICE_SW_LKUP_VLAN)
2830                         return ICE_ERR_PARAM;
2831                 v_list_itr->status = ice_remove_rule_internal(hw,
2832                                                               ICE_SW_LKUP_VLAN,
2833                                                               v_list_itr);
2834                 if (v_list_itr->status)
2835                         return v_list_itr->status;
2836         }
2837         return ICE_SUCCESS;
2838 }
2839
2840 #ifndef NO_MACVLAN_SUPPORT
2841 /**
2842  * ice_remove_mac_vlan - Remove MAC VLAN based filter rule
2843  * @hw: pointer to the hardware structure
2844  * @v_list: list of MAC VLAN entries and forwarding information
2845  */
2846 enum ice_status
2847 ice_remove_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2848 {
2849         struct ice_fltr_list_entry *v_list_itr, *tmp;
2850
2851         if (!v_list || !hw)
2852                 return ICE_ERR_PARAM;
2853
2854         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2855                                  list_entry) {
2856                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2857
2858                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
2859                         return ICE_ERR_PARAM;
2860                 v_list_itr->status =
2861                         ice_remove_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
2862                                                  v_list_itr);
2863                 if (v_list_itr->status)
2864                         return v_list_itr->status;
2865         }
2866         return ICE_SUCCESS;
2867 }
2868 #endif /* !NO_MACVLAN_SUPPORT */
2869
2870 /**
2871  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
2872  * @fm_entry: filter entry to inspect
2873  * @vsi_handle: VSI handle to compare with filter info
2874  */
2875 static bool
2876 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
2877 {
2878         return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
2879                  fm_entry->fltr_info.vsi_handle == vsi_handle) ||
2880                 (fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
2881                  (ice_is_bit_set(fm_entry->vsi_list_info->vsi_map,
2882                                  vsi_handle))));
2883 }
2884
2885 /**
2886  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
2887  * @hw: pointer to the hardware structure
2888  * @vsi_handle: VSI handle to remove filters from
2889  * @vsi_list_head: pointer to the list to add entry to
2890  * @fi: pointer to fltr_info of filter entry to copy & add
2891  *
2892  * Helper function, used when creating a list of filters to remove from
2893  * a specific VSI. The entry added to vsi_list_head is a COPY of the
2894  * original filter entry, with the exception of fltr_info.fltr_act and
2895  * fltr_info.fwd_id fields. These are set such that later logic can
2896  * extract which VSI to remove the fltr from, and pass on that information.
2897  */
2898 static enum ice_status
2899 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2900                                struct LIST_HEAD_TYPE *vsi_list_head,
2901                                struct ice_fltr_info *fi)
2902 {
2903         struct ice_fltr_list_entry *tmp;
2904
2905         /* this memory is freed up in the caller function
2906          * once filters for this VSI are removed
2907          */
2908         tmp = (struct ice_fltr_list_entry *)ice_malloc(hw, sizeof(*tmp));
2909         if (!tmp)
2910                 return ICE_ERR_NO_MEMORY;
2911
2912         tmp->fltr_info = *fi;
2913
2914         /* Overwrite these fields to indicate which VSI to remove filter from,
2915          * so find and remove logic can extract the information from the
2916          * list entries. Note that original entries will still have proper
2917          * values.
2918          */
2919         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2920         tmp->fltr_info.vsi_handle = vsi_handle;
2921         tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2922
2923         LIST_ADD(&tmp->list_entry, vsi_list_head);
2924
2925         return ICE_SUCCESS;
2926 }
2927
2928 /**
2929  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
2930  * @hw: pointer to the hardware structure
2931  * @vsi_handle: VSI handle to remove filters from
2932  * @lkup_list_head: pointer to the list that has certain lookup type filters
2933  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
2934  *
2935  * Locates all filters in lkup_list_head that are used by the given VSI,
2936  * and adds COPIES of those entries to vsi_list_head (intended to be used
2937  * to remove the listed filters).
2938  * Note that this means all entries in vsi_list_head must be explicitly
2939  * deallocated by the caller when done with list.
2940  */
2941 static enum ice_status
2942 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2943                          struct LIST_HEAD_TYPE *lkup_list_head,
2944                          struct LIST_HEAD_TYPE *vsi_list_head)
2945 {
2946         struct ice_fltr_mgmt_list_entry *fm_entry;
2947         enum ice_status status = ICE_SUCCESS;
2948
2949         /* check to make sure VSI ID is valid and within boundary */
2950         if (!ice_is_vsi_valid(hw, vsi_handle))
2951                 return ICE_ERR_PARAM;
2952
2953         LIST_FOR_EACH_ENTRY(fm_entry, lkup_list_head,
2954                             ice_fltr_mgmt_list_entry, list_entry) {
2955                 struct ice_fltr_info *fi;
2956
2957                 fi = &fm_entry->fltr_info;
2958                 if (!fi || !ice_vsi_uses_fltr(fm_entry, vsi_handle))
2959                         continue;
2960
2961                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2962                                                         vsi_list_head, fi);
2963                 if (status)
2964                         return status;
2965         }
2966         return status;
2967 }
2968
2969
2970 /**
2971  * ice_determine_promisc_mask
2972  * @fi: filter info to parse
2973  *
2974  * Helper function to determine which ICE_PROMISC_ mask corresponds
2975  * to given filter into.
2976  */
2977 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
2978 {
2979         u16 vid = fi->l_data.mac_vlan.vlan_id;
2980         u8 *macaddr = fi->l_data.mac.mac_addr;
2981         bool is_tx_fltr = false;
2982         u8 promisc_mask = 0;
2983
2984         if (fi->flag == ICE_FLTR_TX)
2985                 is_tx_fltr = true;
2986
2987         if (IS_BROADCAST_ETHER_ADDR(macaddr))
2988                 promisc_mask |= is_tx_fltr ?
2989                         ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
2990         else if (IS_MULTICAST_ETHER_ADDR(macaddr))
2991                 promisc_mask |= is_tx_fltr ?
2992                         ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
2993         else if (IS_UNICAST_ETHER_ADDR(macaddr))
2994                 promisc_mask |= is_tx_fltr ?
2995                         ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
2996         if (vid)
2997                 promisc_mask |= is_tx_fltr ?
2998                         ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
2999
3000         return promisc_mask;
3001 }
3002
3003 /**
3004  * ice_get_vsi_promisc - get promiscuous mode of given VSI
3005  * @hw: pointer to the hardware structure
3006  * @vsi_handle: VSI handle to retrieve info from
3007  * @promisc_mask: pointer to mask to be filled in
3008  * @vid: VLAN ID of promisc VLAN VSI
3009  */
3010 enum ice_status
3011 ice_get_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3012                     u16 *vid)
3013 {
3014         struct ice_switch_info *sw = hw->switch_info;
3015         struct ice_fltr_mgmt_list_entry *itr;
3016         struct LIST_HEAD_TYPE *rule_head;
3017         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3018
3019         if (!ice_is_vsi_valid(hw, vsi_handle))
3020                 return ICE_ERR_PARAM;
3021
3022         *vid = 0;
3023         *promisc_mask = 0;
3024         rule_head = &sw->recp_list[ICE_SW_LKUP_PROMISC].filt_rules;
3025         rule_lock = &sw->recp_list[ICE_SW_LKUP_PROMISC].filt_rule_lock;
3026
3027         ice_acquire_lock(rule_lock);
3028         LIST_FOR_EACH_ENTRY(itr, rule_head,
3029                             ice_fltr_mgmt_list_entry, list_entry) {
3030                 /* Continue if this filter doesn't apply to this VSI or the
3031                  * VSI ID is not in the VSI map for this filter
3032                  */
3033                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3034                         continue;
3035
3036                 *promisc_mask |= ice_determine_promisc_mask(&itr->fltr_info);
3037         }
3038         ice_release_lock(rule_lock);
3039
3040         return ICE_SUCCESS;
3041 }
3042
3043 /**
3044  * ice_get_vsi_vlan_promisc - get VLAN promiscuous mode of given VSI
3045  * @hw: pointer to the hardware structure
3046  * @vsi_handle: VSI handle to retrieve info from
3047  * @promisc_mask: pointer to mask to be filled in
3048  * @vid: VLAN ID of promisc VLAN VSI
3049  */
3050 enum ice_status
3051 ice_get_vsi_vlan_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3052                          u16 *vid)
3053 {
3054         struct ice_switch_info *sw = hw->switch_info;
3055         struct ice_fltr_mgmt_list_entry *itr;
3056         struct LIST_HEAD_TYPE *rule_head;
3057         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3058
3059         if (!ice_is_vsi_valid(hw, vsi_handle))
3060                 return ICE_ERR_PARAM;
3061
3062         *vid = 0;
3063         *promisc_mask = 0;
3064         rule_head = &sw->recp_list[ICE_SW_LKUP_PROMISC_VLAN].filt_rules;
3065         rule_lock = &sw->recp_list[ICE_SW_LKUP_PROMISC_VLAN].filt_rule_lock;
3066
3067         ice_acquire_lock(rule_lock);
3068         LIST_FOR_EACH_ENTRY(itr, rule_head, ice_fltr_mgmt_list_entry,
3069                             list_entry) {
3070                 /* Continue if this filter doesn't apply to this VSI or the
3071                  * VSI ID is not in the VSI map for this filter
3072                  */
3073                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3074                         continue;
3075
3076                 *promisc_mask |= ice_determine_promisc_mask(&itr->fltr_info);
3077         }
3078         ice_release_lock(rule_lock);
3079
3080         return ICE_SUCCESS;
3081 }
3082
3083 /**
3084  * ice_remove_promisc - Remove promisc based filter rules
3085  * @hw: pointer to the hardware structure
3086  * @recp_id: recipe ID for which the rule needs to removed
3087  * @v_list: list of promisc entries
3088  */
3089 static enum ice_status
3090 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
3091                    struct LIST_HEAD_TYPE *v_list)
3092 {
3093         struct ice_fltr_list_entry *v_list_itr, *tmp;
3094
3095         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
3096                                  list_entry) {
3097                 v_list_itr->status =
3098                         ice_remove_rule_internal(hw, recp_id, v_list_itr);
3099                 if (v_list_itr->status)
3100                         return v_list_itr->status;
3101         }
3102         return ICE_SUCCESS;
3103 }
3104
3105 /**
3106  * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
3107  * @hw: pointer to the hardware structure
3108  * @vsi_handle: VSI handle to clear mode
3109  * @promisc_mask: mask of promiscuous config bits to clear
3110  * @vid: VLAN ID to clear VLAN promiscuous
3111  */
3112 enum ice_status
3113 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3114                       u16 vid)
3115 {
3116         struct ice_switch_info *sw = hw->switch_info;
3117         struct ice_fltr_list_entry *fm_entry, *tmp;
3118         struct LIST_HEAD_TYPE remove_list_head;
3119         struct ice_fltr_mgmt_list_entry *itr;
3120         struct LIST_HEAD_TYPE *rule_head;
3121         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3122         enum ice_status status = ICE_SUCCESS;
3123         u8 recipe_id;
3124
3125         if (!ice_is_vsi_valid(hw, vsi_handle))
3126                 return ICE_ERR_PARAM;
3127
3128         if (vid)
3129                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3130         else
3131                 recipe_id = ICE_SW_LKUP_PROMISC;
3132
3133         rule_head = &sw->recp_list[recipe_id].filt_rules;
3134         rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
3135
3136         INIT_LIST_HEAD(&remove_list_head);
3137
3138         ice_acquire_lock(rule_lock);
3139         LIST_FOR_EACH_ENTRY(itr, rule_head,
3140                             ice_fltr_mgmt_list_entry, list_entry) {
3141                 u8 fltr_promisc_mask = 0;
3142
3143                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3144                         continue;
3145
3146                 fltr_promisc_mask |=
3147                         ice_determine_promisc_mask(&itr->fltr_info);
3148
3149                 /* Skip if filter is not completely specified by given mask */
3150                 if (fltr_promisc_mask & ~promisc_mask)
3151                         continue;
3152
3153                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3154                                                         &remove_list_head,
3155                                                         &itr->fltr_info);
3156                 if (status) {
3157                         ice_release_lock(rule_lock);
3158                         goto free_fltr_list;
3159                 }
3160         }
3161         ice_release_lock(rule_lock);
3162
3163         status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
3164
3165 free_fltr_list:
3166         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3167                                  ice_fltr_list_entry, list_entry) {
3168                 LIST_DEL(&fm_entry->list_entry);
3169                 ice_free(hw, fm_entry);
3170         }
3171
3172         return status;
3173 }
3174
3175 /**
3176  * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
3177  * @hw: pointer to the hardware structure
3178  * @vsi_handle: VSI handle to configure
3179  * @promisc_mask: mask of promiscuous config bits
3180  * @vid: VLAN ID to set VLAN promiscuous
3181  */
3182 enum ice_status
3183 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
3184 {
3185         enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
3186         struct ice_fltr_list_entry f_list_entry;
3187         struct ice_fltr_info new_fltr;
3188         enum ice_status status = ICE_SUCCESS;
3189         bool is_tx_fltr;
3190         u16 hw_vsi_id;
3191         int pkt_type;
3192         u8 recipe_id;
3193
3194         ice_debug(hw, ICE_DBG_TRACE, "ice_set_vsi_promisc\n");
3195
3196         if (!ice_is_vsi_valid(hw, vsi_handle))
3197                 return ICE_ERR_PARAM;
3198         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3199
3200         ice_memset(&new_fltr, 0, sizeof(new_fltr), ICE_NONDMA_MEM);
3201
3202         if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
3203                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
3204                 new_fltr.l_data.mac_vlan.vlan_id = vid;
3205                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3206         } else {
3207                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
3208                 recipe_id = ICE_SW_LKUP_PROMISC;
3209         }
3210
3211         /* Separate filters must be set for each direction/packet type
3212          * combination, so we will loop over the mask value, store the
3213          * individual type, and clear it out in the input mask as it
3214          * is found.
3215          */
3216         while (promisc_mask) {
3217                 u8 *mac_addr;
3218
3219                 pkt_type = 0;
3220                 is_tx_fltr = false;
3221
3222                 if (promisc_mask & ICE_PROMISC_UCAST_RX) {
3223                         promisc_mask &= ~ICE_PROMISC_UCAST_RX;
3224                         pkt_type = UCAST_FLTR;
3225                 } else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
3226                         promisc_mask &= ~ICE_PROMISC_UCAST_TX;
3227                         pkt_type = UCAST_FLTR;
3228                         is_tx_fltr = true;
3229                 } else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
3230                         promisc_mask &= ~ICE_PROMISC_MCAST_RX;
3231                         pkt_type = MCAST_FLTR;
3232                 } else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
3233                         promisc_mask &= ~ICE_PROMISC_MCAST_TX;
3234                         pkt_type = MCAST_FLTR;
3235                         is_tx_fltr = true;
3236                 } else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
3237                         promisc_mask &= ~ICE_PROMISC_BCAST_RX;
3238                         pkt_type = BCAST_FLTR;
3239                 } else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
3240                         promisc_mask &= ~ICE_PROMISC_BCAST_TX;
3241                         pkt_type = BCAST_FLTR;
3242                         is_tx_fltr = true;
3243                 }
3244
3245                 /* Check for VLAN promiscuous flag */
3246                 if (promisc_mask & ICE_PROMISC_VLAN_RX) {
3247                         promisc_mask &= ~ICE_PROMISC_VLAN_RX;
3248                 } else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
3249                         promisc_mask &= ~ICE_PROMISC_VLAN_TX;
3250                         is_tx_fltr = true;
3251                 }
3252
3253                 /* Set filter DA based on packet type */
3254                 mac_addr = new_fltr.l_data.mac.mac_addr;
3255                 if (pkt_type == BCAST_FLTR) {
3256                         ice_memset(mac_addr, 0xff, ETH_ALEN, ICE_NONDMA_MEM);
3257                 } else if (pkt_type == MCAST_FLTR ||
3258                            pkt_type == UCAST_FLTR) {
3259                         /* Use the dummy ether header DA */
3260                         ice_memcpy(mac_addr, dummy_eth_header, ETH_ALEN,
3261                                    ICE_NONDMA_TO_NONDMA);
3262                         if (pkt_type == MCAST_FLTR)
3263                                 mac_addr[0] |= 0x1;     /* Set multicast bit */
3264                 }
3265
3266                 /* Need to reset this to zero for all iterations */
3267                 new_fltr.flag = 0;
3268                 if (is_tx_fltr) {
3269                         new_fltr.flag |= ICE_FLTR_TX;
3270                         new_fltr.src = hw_vsi_id;
3271                 } else {
3272                         new_fltr.flag |= ICE_FLTR_RX;
3273                         new_fltr.src = hw->port_info->lport;
3274                 }
3275
3276                 new_fltr.fltr_act = ICE_FWD_TO_VSI;
3277                 new_fltr.vsi_handle = vsi_handle;
3278                 new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
3279                 f_list_entry.fltr_info = new_fltr;
3280
3281                 status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
3282                 if (status != ICE_SUCCESS)
3283                         goto set_promisc_exit;
3284         }
3285
3286 set_promisc_exit:
3287         return status;
3288 }
3289
3290 /**
3291  * ice_set_vlan_vsi_promisc
3292  * @hw: pointer to the hardware structure
3293  * @vsi_handle: VSI handle to configure
3294  * @promisc_mask: mask of promiscuous config bits
3295  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
3296  *
3297  * Configure VSI with all associated VLANs to given promiscuous mode(s)
3298  */
3299 enum ice_status
3300 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3301                          bool rm_vlan_promisc)
3302 {
3303         struct ice_switch_info *sw = hw->switch_info;
3304         struct ice_fltr_list_entry *list_itr, *tmp;
3305         struct LIST_HEAD_TYPE vsi_list_head;
3306         struct LIST_HEAD_TYPE *vlan_head;
3307         struct ice_lock *vlan_lock; /* Lock to protect filter rule list */
3308         enum ice_status status;
3309         u16 vlan_id;
3310
3311         INIT_LIST_HEAD(&vsi_list_head);
3312         vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3313         vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3314         ice_acquire_lock(vlan_lock);
3315         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
3316                                           &vsi_list_head);
3317         ice_release_lock(vlan_lock);
3318         if (status)
3319                 goto free_fltr_list;
3320
3321         LIST_FOR_EACH_ENTRY(list_itr, &vsi_list_head, ice_fltr_list_entry,
3322                             list_entry) {
3323                 vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
3324                 if (rm_vlan_promisc)
3325                         status = ice_clear_vsi_promisc(hw, vsi_handle,
3326                                                        promisc_mask, vlan_id);
3327                 else
3328                         status = ice_set_vsi_promisc(hw, vsi_handle,
3329                                                      promisc_mask, vlan_id);
3330                 if (status)
3331                         break;
3332         }
3333
3334 free_fltr_list:
3335         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, &vsi_list_head,
3336                                  ice_fltr_list_entry, list_entry) {
3337                 LIST_DEL(&list_itr->list_entry);
3338                 ice_free(hw, list_itr);
3339         }
3340         return status;
3341 }
3342
3343 /**
3344  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
3345  * @hw: pointer to the hardware structure
3346  * @vsi_handle: VSI handle to remove filters from
3347  * @lkup: switch rule filter lookup type
3348  */
3349 static void
3350 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
3351                          enum ice_sw_lkup_type lkup)
3352 {
3353         struct ice_switch_info *sw = hw->switch_info;
3354         struct ice_fltr_list_entry *fm_entry;
3355         struct LIST_HEAD_TYPE remove_list_head;
3356         struct LIST_HEAD_TYPE *rule_head;
3357         struct ice_fltr_list_entry *tmp;
3358         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3359         enum ice_status status;
3360
3361         INIT_LIST_HEAD(&remove_list_head);
3362         rule_lock = &sw->recp_list[lkup].filt_rule_lock;
3363         rule_head = &sw->recp_list[lkup].filt_rules;
3364         ice_acquire_lock(rule_lock);
3365         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
3366                                           &remove_list_head);
3367         ice_release_lock(rule_lock);
3368         if (status)
3369                 return;
3370
3371         switch (lkup) {
3372         case ICE_SW_LKUP_MAC:
3373                 ice_remove_mac(hw, &remove_list_head);
3374                 break;
3375         case ICE_SW_LKUP_VLAN:
3376                 ice_remove_vlan(hw, &remove_list_head);
3377                 break;
3378         case ICE_SW_LKUP_PROMISC:
3379         case ICE_SW_LKUP_PROMISC_VLAN:
3380                 ice_remove_promisc(hw, lkup, &remove_list_head);
3381                 break;
3382         case ICE_SW_LKUP_MAC_VLAN:
3383 #ifndef NO_MACVLAN_SUPPORT
3384                 ice_remove_mac_vlan(hw, &remove_list_head);
3385 #else
3386                 ice_debug(hw, ICE_DBG_SW, "MAC VLAN look up is not supported yet\n");
3387 #endif /* !NO_MACVLAN_SUPPORT */
3388                 break;
3389         case ICE_SW_LKUP_ETHERTYPE:
3390         case ICE_SW_LKUP_ETHERTYPE_MAC:
3391         case ICE_SW_LKUP_DFLT:
3392                 ice_debug(hw, ICE_DBG_SW,
3393                           "Remove filters for this lookup type hasn't been implemented yet\n");
3394                 break;
3395         case ICE_SW_LKUP_LAST:
3396                 ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type\n");
3397                 break;
3398         }
3399
3400         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3401                                  ice_fltr_list_entry, list_entry) {
3402                 LIST_DEL(&fm_entry->list_entry);
3403                 ice_free(hw, fm_entry);
3404         }
3405 }
3406
3407 /**
3408  * ice_remove_vsi_fltr - Remove all filters for a VSI
3409  * @hw: pointer to the hardware structure
3410  * @vsi_handle: VSI handle to remove filters from
3411  */
3412 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
3413 {
3414         ice_debug(hw, ICE_DBG_TRACE, "ice_remove_vsi_fltr\n");
3415
3416         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
3417         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
3418         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
3419         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
3420         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
3421         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
3422         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
3423         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
3424 }
3425
3426 /**
3427  * ice_alloc_res_cntr - allocating resource counter
3428  * @hw: pointer to the hardware structure
3429  * @type: type of resource
3430  * @alloc_shared: if set it is shared else dedicated
3431  * @num_items: number of entries requested for FD resource type
3432  * @counter_id: counter index returned by AQ call
3433  */
3434 enum ice_status
3435 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3436                    u16 *counter_id)
3437 {
3438         struct ice_aqc_alloc_free_res_elem *buf;
3439         enum ice_status status;
3440         u16 buf_len;
3441
3442         /* Allocate resource */
3443         buf_len = sizeof(*buf);
3444         buf = (struct ice_aqc_alloc_free_res_elem *)
3445                 ice_malloc(hw, buf_len);
3446         if (!buf)
3447                 return ICE_ERR_NO_MEMORY;
3448
3449         buf->num_elems = CPU_TO_LE16(num_items);
3450         buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3451                                       ICE_AQC_RES_TYPE_M) | alloc_shared);
3452
3453         status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3454                                        ice_aqc_opc_alloc_res, NULL);
3455         if (status)
3456                 goto exit;
3457
3458         *counter_id = LE16_TO_CPU(buf->elem[0].e.sw_resp);
3459
3460 exit:
3461         ice_free(hw, buf);
3462         return status;
3463 }
3464
3465 /**
3466  * ice_free_res_cntr - free resource counter
3467  * @hw: pointer to the hardware structure
3468  * @type: type of resource
3469  * @alloc_shared: if set it is shared else dedicated
3470  * @num_items: number of entries to be freed for FD resource type
3471  * @counter_id: counter ID resource which needs to be freed
3472  */
3473 enum ice_status
3474 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3475                   u16 counter_id)
3476 {
3477         struct ice_aqc_alloc_free_res_elem *buf;
3478         enum ice_status status;
3479         u16 buf_len;
3480
3481         /* Free resource */
3482         buf_len = sizeof(*buf);
3483         buf = (struct ice_aqc_alloc_free_res_elem *)
3484                 ice_malloc(hw, buf_len);
3485         if (!buf)
3486                 return ICE_ERR_NO_MEMORY;
3487
3488         buf->num_elems = CPU_TO_LE16(num_items);
3489         buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3490                                       ICE_AQC_RES_TYPE_M) | alloc_shared);
3491         buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
3492
3493         status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3494                                        ice_aqc_opc_free_res, NULL);
3495         if (status)
3496                 ice_debug(hw, ICE_DBG_SW,
3497                           "counter resource could not be freed\n");
3498
3499         ice_free(hw, buf);
3500         return status;
3501 }
3502
3503 /**
3504  * ice_alloc_vlan_res_counter - obtain counter resource for VLAN type
3505  * @hw: pointer to the hardware structure
3506  * @counter_id: returns counter index
3507  */
3508 enum ice_status ice_alloc_vlan_res_counter(struct ice_hw *hw, u16 *counter_id)
3509 {
3510         return ice_alloc_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3511                                   ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3512                                   counter_id);
3513 }
3514
3515 /**
3516  * ice_free_vlan_res_counter - Free counter resource for VLAN type
3517  * @hw: pointer to the hardware structure
3518  * @counter_id: counter index to be freed
3519  */
3520 enum ice_status ice_free_vlan_res_counter(struct ice_hw *hw, u16 counter_id)
3521 {
3522         return ice_free_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3523                                  ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3524                                  counter_id);
3525 }
3526
3527 /**
3528  * ice_alloc_res_lg_act - add large action resource
3529  * @hw: pointer to the hardware structure
3530  * @l_id: large action ID to fill it in
3531  * @num_acts: number of actions to hold with a large action entry
3532  */
3533 static enum ice_status
3534 ice_alloc_res_lg_act(struct ice_hw *hw, u16 *l_id, u16 num_acts)
3535 {
3536         struct ice_aqc_alloc_free_res_elem *sw_buf;
3537         enum ice_status status;
3538         u16 buf_len;
3539
3540         if (num_acts > ICE_MAX_LG_ACT || num_acts == 0)
3541                 return ICE_ERR_PARAM;
3542
3543         /* Allocate resource for large action */
3544         buf_len = sizeof(*sw_buf);
3545         sw_buf = (struct ice_aqc_alloc_free_res_elem *)
3546                 ice_malloc(hw, buf_len);
3547         if (!sw_buf)
3548                 return ICE_ERR_NO_MEMORY;
3549
3550         sw_buf->num_elems = CPU_TO_LE16(1);
3551
3552         /* If num_acts is 1, use ICE_AQC_RES_TYPE_WIDE_TABLE_1.
3553          * If num_acts is 2, use ICE_AQC_RES_TYPE_WIDE_TABLE_3.
3554          * If num_acts is greater than 2, then use
3555          * ICE_AQC_RES_TYPE_WIDE_TABLE_4.
3556          * The num_acts cannot exceed 4. This was ensured at the
3557          * beginning of the function.
3558          */
3559         if (num_acts == 1)
3560                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_1);
3561         else if (num_acts == 2)
3562                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_2);
3563         else
3564                 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_4);
3565
3566         status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
3567                                        ice_aqc_opc_alloc_res, NULL);
3568         if (!status)
3569                 *l_id = LE16_TO_CPU(sw_buf->elem[0].e.sw_resp);
3570
3571         ice_free(hw, sw_buf);
3572         return status;
3573 }
3574
3575 /**
3576  * ice_add_mac_with_sw_marker - add filter with sw marker
3577  * @hw: pointer to the hardware structure
3578  * @f_info: filter info structure containing the MAC filter information
3579  * @sw_marker: sw marker to tag the Rx descriptor with
3580  */
3581 enum ice_status
3582 ice_add_mac_with_sw_marker(struct ice_hw *hw, struct ice_fltr_info *f_info,
3583                            u16 sw_marker)
3584 {
3585         struct ice_switch_info *sw = hw->switch_info;
3586         struct ice_fltr_mgmt_list_entry *m_entry;
3587         struct ice_fltr_list_entry fl_info;
3588         struct LIST_HEAD_TYPE l_head;
3589         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3590         enum ice_status ret;
3591         bool entry_exists;
3592         u16 lg_act_id;
3593
3594         if (f_info->fltr_act != ICE_FWD_TO_VSI)
3595                 return ICE_ERR_PARAM;
3596
3597         if (f_info->lkup_type != ICE_SW_LKUP_MAC)
3598                 return ICE_ERR_PARAM;
3599
3600         if (sw_marker == ICE_INVAL_SW_MARKER_ID)
3601                 return ICE_ERR_PARAM;
3602
3603         if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
3604                 return ICE_ERR_PARAM;
3605         f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
3606
3607         /* Add filter if it doesn't exist so then the adding of large
3608          * action always results in update
3609          */
3610
3611         INIT_LIST_HEAD(&l_head);
3612         fl_info.fltr_info = *f_info;
3613         LIST_ADD(&fl_info.list_entry, &l_head);
3614
3615         entry_exists = false;
3616         ret = ice_add_mac(hw, &l_head);
3617         if (ret == ICE_ERR_ALREADY_EXISTS)
3618                 entry_exists = true;
3619         else if (ret)
3620                 return ret;
3621
3622         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
3623         ice_acquire_lock(rule_lock);
3624         /* Get the book keeping entry for the filter */
3625         m_entry = ice_find_rule_entry(hw, ICE_SW_LKUP_MAC, f_info);
3626         if (!m_entry)
3627                 goto exit_error;
3628
3629         /* If counter action was enabled for this rule then don't enable
3630          * sw marker large action
3631          */
3632         if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
3633                 ret = ICE_ERR_PARAM;
3634                 goto exit_error;
3635         }
3636
3637         /* if same marker was added before */
3638         if (m_entry->sw_marker_id == sw_marker) {
3639                 ret = ICE_ERR_ALREADY_EXISTS;
3640                 goto exit_error;
3641         }
3642
3643         /* Allocate a hardware table entry to hold large act. Three actions
3644          * for marker based large action
3645          */
3646         ret = ice_alloc_res_lg_act(hw, &lg_act_id, 3);
3647         if (ret)
3648                 goto exit_error;
3649
3650         if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
3651                 goto exit_error;
3652
3653         /* Update the switch rule to add the marker action */
3654         ret = ice_add_marker_act(hw, m_entry, sw_marker, lg_act_id);
3655         if (!ret) {
3656                 ice_release_lock(rule_lock);
3657                 return ret;
3658         }
3659
3660 exit_error:
3661         ice_release_lock(rule_lock);
3662         /* only remove entry if it did not exist previously */
3663         if (!entry_exists)
3664                 ret = ice_remove_mac(hw, &l_head);
3665
3666         return ret;
3667 }
3668
3669 /**
3670  * ice_add_mac_with_counter - add filter with counter enabled
3671  * @hw: pointer to the hardware structure
3672  * @f_info: pointer to filter info structure containing the MAC filter
3673  *          information
3674  */
3675 enum ice_status
3676 ice_add_mac_with_counter(struct ice_hw *hw, struct ice_fltr_info *f_info)
3677 {
3678         struct ice_switch_info *sw = hw->switch_info;
3679         struct ice_fltr_mgmt_list_entry *m_entry;
3680         struct ice_fltr_list_entry fl_info;
3681         struct LIST_HEAD_TYPE l_head;
3682         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3683         enum ice_status ret;
3684         bool entry_exist;
3685         u16 counter_id;
3686         u16 lg_act_id;
3687
3688         if (f_info->fltr_act != ICE_FWD_TO_VSI)
3689                 return ICE_ERR_PARAM;
3690
3691         if (f_info->lkup_type != ICE_SW_LKUP_MAC)
3692                 return ICE_ERR_PARAM;
3693
3694         if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
3695                 return ICE_ERR_PARAM;
3696         f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
3697
3698         entry_exist = false;
3699
3700         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
3701
3702         /* Add filter if it doesn't exist so then the adding of large
3703          * action always results in update
3704          */
3705         INIT_LIST_HEAD(&l_head);
3706
3707         fl_info.fltr_info = *f_info;
3708         LIST_ADD(&fl_info.list_entry, &l_head);
3709
3710         ret = ice_add_mac(hw, &l_head);
3711         if (ret == ICE_ERR_ALREADY_EXISTS)
3712                 entry_exist = true;
3713         else if (ret)
3714                 return ret;
3715
3716         ice_acquire_lock(rule_lock);
3717         m_entry = ice_find_rule_entry(hw, ICE_SW_LKUP_MAC, f_info);
3718         if (!m_entry) {
3719                 ret = ICE_ERR_BAD_PTR;
3720                 goto exit_error;
3721         }
3722
3723         /* Don't enable counter for a filter for which sw marker was enabled */
3724         if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID) {
3725                 ret = ICE_ERR_PARAM;
3726                 goto exit_error;
3727         }
3728
3729         /* If a counter was already enabled then don't need to add again */
3730         if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
3731                 ret = ICE_ERR_ALREADY_EXISTS;
3732                 goto exit_error;
3733         }
3734
3735         /* Allocate a hardware table entry to VLAN counter */
3736         ret = ice_alloc_vlan_res_counter(hw, &counter_id);
3737         if (ret)
3738                 goto exit_error;
3739
3740         /* Allocate a hardware table entry to hold large act. Two actions for
3741          * counter based large action
3742          */
3743         ret = ice_alloc_res_lg_act(hw, &lg_act_id, 2);
3744         if (ret)
3745                 goto exit_error;
3746
3747         if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
3748                 goto exit_error;
3749
3750         /* Update the switch rule to add the counter action */
3751         ret = ice_add_counter_act(hw, m_entry, counter_id, lg_act_id);
3752         if (!ret) {
3753                 ice_release_lock(rule_lock);
3754                 return ret;
3755         }
3756
3757 exit_error:
3758         ice_release_lock(rule_lock);
3759         /* only remove entry if it did not exist previously */
3760         if (!entry_exist)
3761                 ret = ice_remove_mac(hw, &l_head);
3762
3763         return ret;
3764 }
3765
3766 /**
3767  * ice_replay_fltr - Replay all the filters stored by a specific list head
3768  * @hw: pointer to the hardware structure
3769  * @list_head: list for which filters needs to be replayed
3770  * @recp_id: Recipe ID for which rules need to be replayed
3771  */
3772 static enum ice_status
3773 ice_replay_fltr(struct ice_hw *hw, u8 recp_id, struct LIST_HEAD_TYPE *list_head)
3774 {
3775         struct ice_fltr_mgmt_list_entry *itr;
3776         struct LIST_HEAD_TYPE l_head;
3777         enum ice_status status = ICE_SUCCESS;
3778
3779         if (LIST_EMPTY(list_head))
3780                 return status;
3781
3782         /* Move entries from the given list_head to a temporary l_head so that
3783          * they can be replayed. Otherwise when trying to re-add the same
3784          * filter, the function will return already exists
3785          */
3786         LIST_REPLACE_INIT(list_head, &l_head);
3787
3788         /* Mark the given list_head empty by reinitializing it so filters
3789          * could be added again by *handler
3790          */
3791         LIST_FOR_EACH_ENTRY(itr, &l_head, ice_fltr_mgmt_list_entry,
3792                             list_entry) {
3793                 struct ice_fltr_list_entry f_entry;
3794
3795                 f_entry.fltr_info = itr->fltr_info;
3796                 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN) {
3797                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
3798                         if (status != ICE_SUCCESS)
3799                                 goto end;
3800                         continue;
3801                 }
3802
3803                 /* Add a filter per VSI separately */
3804                 while (1) {
3805                         u16 vsi_handle;
3806
3807                         vsi_handle =
3808                                 ice_find_first_bit(itr->vsi_list_info->vsi_map,
3809                                                    ICE_MAX_VSI);
3810                         if (!ice_is_vsi_valid(hw, vsi_handle))
3811                                 break;
3812
3813                         ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
3814                         f_entry.fltr_info.vsi_handle = vsi_handle;
3815                         f_entry.fltr_info.fwd_id.hw_vsi_id =
3816                                 ice_get_hw_vsi_num(hw, vsi_handle);
3817                         f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
3818                         if (recp_id == ICE_SW_LKUP_VLAN)
3819                                 status = ice_add_vlan_internal(hw, &f_entry);
3820                         else
3821                                 status = ice_add_rule_internal(hw, recp_id,
3822                                                                &f_entry);
3823                         if (status != ICE_SUCCESS)
3824                                 goto end;
3825                 }
3826         }
3827 end:
3828         /* Clear the filter management list */
3829         ice_rem_sw_rule_info(hw, &l_head);
3830         return status;
3831 }
3832
3833 /**
3834  * ice_replay_all_fltr - replay all filters stored in bookkeeping lists
3835  * @hw: pointer to the hardware structure
3836  *
3837  * NOTE: This function does not clean up partially added filters on error.
3838  * It is up to caller of the function to issue a reset or fail early.
3839  */
3840 enum ice_status ice_replay_all_fltr(struct ice_hw *hw)
3841 {
3842         struct ice_switch_info *sw = hw->switch_info;
3843         enum ice_status status = ICE_SUCCESS;
3844         u8 i;
3845
3846         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3847                 struct LIST_HEAD_TYPE *head = &sw->recp_list[i].filt_rules;
3848
3849                 status = ice_replay_fltr(hw, i, head);
3850                 if (status != ICE_SUCCESS)
3851                         return status;
3852         }
3853         return status;
3854 }
3855
3856 /**
3857  * ice_replay_vsi_fltr - Replay filters for requested VSI
3858  * @hw: pointer to the hardware structure
3859  * @vsi_handle: driver VSI handle
3860  * @recp_id: Recipe ID for which rules need to be replayed
3861  * @list_head: list for which filters need to be replayed
3862  *
3863  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
3864  * It is required to pass valid VSI handle.
3865  */
3866 static enum ice_status
3867 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
3868                     struct LIST_HEAD_TYPE *list_head)
3869 {
3870         struct ice_fltr_mgmt_list_entry *itr;
3871         enum ice_status status = ICE_SUCCESS;
3872         u16 hw_vsi_id;
3873
3874         if (LIST_EMPTY(list_head))
3875                 return status;
3876         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3877
3878         LIST_FOR_EACH_ENTRY(itr, list_head, ice_fltr_mgmt_list_entry,
3879                             list_entry) {
3880                 struct ice_fltr_list_entry f_entry;
3881
3882                 f_entry.fltr_info = itr->fltr_info;
3883                 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
3884                     itr->fltr_info.vsi_handle == vsi_handle) {
3885                         /* update the src in case it is VSI num */
3886                         if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
3887                                 f_entry.fltr_info.src = hw_vsi_id;
3888                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
3889                         if (status != ICE_SUCCESS)
3890                                 goto end;
3891                         continue;
3892                 }
3893                 if (!itr->vsi_list_info ||
3894                     !ice_is_bit_set(itr->vsi_list_info->vsi_map, vsi_handle))
3895                         continue;
3896                 /* Clearing it so that the logic can add it back */
3897                 ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
3898                 f_entry.fltr_info.vsi_handle = vsi_handle;
3899                 f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
3900                 /* update the src in case it is VSI num */
3901                 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
3902                         f_entry.fltr_info.src = hw_vsi_id;
3903                 if (recp_id == ICE_SW_LKUP_VLAN)
3904                         status = ice_add_vlan_internal(hw, &f_entry);
3905                 else
3906                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
3907                 if (status != ICE_SUCCESS)
3908                         goto end;
3909         }
3910 end:
3911         return status;
3912 }
3913
3914
3915 /**
3916  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
3917  * @hw: pointer to the hardware structure
3918  * @vsi_handle: driver VSI handle
3919  *
3920  * Replays filters for requested VSI via vsi_handle.
3921  */
3922 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
3923 {
3924         struct ice_switch_info *sw = hw->switch_info;
3925         enum ice_status status = ICE_SUCCESS;
3926         u8 i;
3927
3928         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3929                 /* Update the default recipe lines and ones that were created */
3930                 if (i < ICE_MAX_NUM_RECIPES || sw->recp_list[i].recp_created) {
3931                         struct LIST_HEAD_TYPE *head;
3932
3933                         head = &sw->recp_list[i].filt_replay_rules;
3934                         if (!sw->recp_list[i].adv_rule)
3935                                 status = ice_replay_vsi_fltr(hw, vsi_handle, i,
3936                                                              head);
3937                         if (status != ICE_SUCCESS)
3938                                 return status;
3939                 }
3940         }
3941         return status;
3942 }
3943
3944 /**
3945  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
3946  * @hw: pointer to the HW struct
3947  *
3948  * Deletes the filter replay rules.
3949  */
3950 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
3951 {
3952         struct ice_switch_info *sw = hw->switch_info;
3953         u8 i;
3954
3955         if (!sw)
3956                 return;
3957
3958         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3959                 if (!LIST_EMPTY(&sw->recp_list[i].filt_replay_rules)) {
3960                         struct LIST_HEAD_TYPE *l_head;
3961
3962                         l_head = &sw->recp_list[i].filt_replay_rules;
3963                         if (!sw->recp_list[i].adv_rule)
3964                                 ice_rem_sw_rule_info(hw, l_head);
3965                 }
3966         }
3967 }