net/ice/base: add functions for resource counter
[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 /**
1343  * ice_create_vsi_list_map
1344  * @hw: pointer to the hardware structure
1345  * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
1346  * @num_vsi: number of VSI handles in the array
1347  * @vsi_list_id: VSI list ID generated as part of allocate resource
1348  *
1349  * Helper function to create a new entry of VSI list ID to VSI mapping
1350  * using the given VSI list ID
1351  */
1352 static struct ice_vsi_list_map_info *
1353 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1354                         u16 vsi_list_id)
1355 {
1356         struct ice_switch_info *sw = hw->switch_info;
1357         struct ice_vsi_list_map_info *v_map;
1358         int i;
1359
1360         v_map = (struct ice_vsi_list_map_info *)ice_calloc(hw, 1,
1361                 sizeof(*v_map));
1362         if (!v_map)
1363                 return NULL;
1364
1365         v_map->vsi_list_id = vsi_list_id;
1366         v_map->ref_cnt = 1;
1367         for (i = 0; i < num_vsi; i++)
1368                 ice_set_bit(vsi_handle_arr[i], v_map->vsi_map);
1369
1370         LIST_ADD(&v_map->list_entry, &sw->vsi_list_map_head);
1371         return v_map;
1372 }
1373
1374 /**
1375  * ice_update_vsi_list_rule
1376  * @hw: pointer to the hardware structure
1377  * @vsi_handle_arr: array of VSI handles to form a VSI list
1378  * @num_vsi: number of VSI handles in the array
1379  * @vsi_list_id: VSI list ID generated as part of allocate resource
1380  * @remove: Boolean value to indicate if this is a remove action
1381  * @opc: switch rules population command type - pass in the command opcode
1382  * @lkup_type: lookup type of the filter
1383  *
1384  * Call AQ command to add a new switch rule or update existing switch rule
1385  * using the given VSI list ID
1386  */
1387 static enum ice_status
1388 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1389                          u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
1390                          enum ice_sw_lkup_type lkup_type)
1391 {
1392         struct ice_aqc_sw_rules_elem *s_rule;
1393         enum ice_status status;
1394         u16 s_rule_size;
1395         u16 type;
1396         int i;
1397
1398         if (!num_vsi)
1399                 return ICE_ERR_PARAM;
1400
1401         if (lkup_type == ICE_SW_LKUP_MAC ||
1402             lkup_type == ICE_SW_LKUP_MAC_VLAN ||
1403             lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1404             lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1405             lkup_type == ICE_SW_LKUP_PROMISC ||
1406             lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
1407                 type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
1408                                 ICE_AQC_SW_RULES_T_VSI_LIST_SET;
1409         else if (lkup_type == ICE_SW_LKUP_VLAN)
1410                 type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
1411                                 ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
1412         else
1413                 return ICE_ERR_PARAM;
1414
1415         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
1416         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
1417         if (!s_rule)
1418                 return ICE_ERR_NO_MEMORY;
1419         for (i = 0; i < num_vsi; i++) {
1420                 if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
1421                         status = ICE_ERR_PARAM;
1422                         goto exit;
1423                 }
1424                 /* AQ call requires hw_vsi_id(s) */
1425                 s_rule->pdata.vsi_list.vsi[i] =
1426                         CPU_TO_LE16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
1427         }
1428
1429         s_rule->type = CPU_TO_LE16(type);
1430         s_rule->pdata.vsi_list.number_vsi = CPU_TO_LE16(num_vsi);
1431         s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
1432
1433         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
1434
1435 exit:
1436         ice_free(hw, s_rule);
1437         return status;
1438 }
1439
1440 /**
1441  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
1442  * @hw: pointer to the HW struct
1443  * @vsi_handle_arr: array of VSI handles to form a VSI list
1444  * @num_vsi: number of VSI handles in the array
1445  * @vsi_list_id: stores the ID of the VSI list to be created
1446  * @lkup_type: switch rule filter's lookup type
1447  */
1448 static enum ice_status
1449 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1450                          u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1451 {
1452         enum ice_status status;
1453
1454         status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1455                                             ice_aqc_opc_alloc_res);
1456         if (status)
1457                 return status;
1458
1459         /* Update the newly created VSI list to include the specified VSIs */
1460         return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1461                                         *vsi_list_id, false,
1462                                         ice_aqc_opc_add_sw_rules, lkup_type);
1463 }
1464
1465 /**
1466  * ice_create_pkt_fwd_rule
1467  * @hw: pointer to the hardware structure
1468  * @f_entry: entry containing packet forwarding information
1469  *
1470  * Create switch rule with given filter information and add an entry
1471  * to the corresponding filter management list to track this switch rule
1472  * and VSI mapping
1473  */
1474 static enum ice_status
1475 ice_create_pkt_fwd_rule(struct ice_hw *hw,
1476                         struct ice_fltr_list_entry *f_entry)
1477 {
1478         struct ice_fltr_mgmt_list_entry *fm_entry;
1479         struct ice_aqc_sw_rules_elem *s_rule;
1480         enum ice_sw_lkup_type l_type;
1481         struct ice_sw_recipe *recp;
1482         enum ice_status status;
1483
1484         s_rule = (struct ice_aqc_sw_rules_elem *)
1485                 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1486         if (!s_rule)
1487                 return ICE_ERR_NO_MEMORY;
1488         fm_entry = (struct ice_fltr_mgmt_list_entry *)
1489                    ice_malloc(hw, sizeof(*fm_entry));
1490         if (!fm_entry) {
1491                 status = ICE_ERR_NO_MEMORY;
1492                 goto ice_create_pkt_fwd_rule_exit;
1493         }
1494
1495         fm_entry->fltr_info = f_entry->fltr_info;
1496
1497         /* Initialize all the fields for the management entry */
1498         fm_entry->vsi_count = 1;
1499         fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1500         fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1501         fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1502
1503         ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1504                          ice_aqc_opc_add_sw_rules);
1505
1506         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1507                                  ice_aqc_opc_add_sw_rules, NULL);
1508         if (status) {
1509                 ice_free(hw, fm_entry);
1510                 goto ice_create_pkt_fwd_rule_exit;
1511         }
1512
1513         f_entry->fltr_info.fltr_rule_id =
1514                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1515         fm_entry->fltr_info.fltr_rule_id =
1516                 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1517
1518         /* The book keeping entries will get removed when base driver
1519          * calls remove filter AQ command
1520          */
1521         l_type = fm_entry->fltr_info.lkup_type;
1522         recp = &hw->switch_info->recp_list[l_type];
1523         LIST_ADD(&fm_entry->list_entry, &recp->filt_rules);
1524
1525 ice_create_pkt_fwd_rule_exit:
1526         ice_free(hw, s_rule);
1527         return status;
1528 }
1529
1530 /**
1531  * ice_update_pkt_fwd_rule
1532  * @hw: pointer to the hardware structure
1533  * @f_info: filter information for switch rule
1534  *
1535  * Call AQ command to update a previously created switch rule with a
1536  * VSI list ID
1537  */
1538 static enum ice_status
1539 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1540 {
1541         struct ice_aqc_sw_rules_elem *s_rule;
1542         enum ice_status status;
1543
1544         s_rule = (struct ice_aqc_sw_rules_elem *)
1545                 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1546         if (!s_rule)
1547                 return ICE_ERR_NO_MEMORY;
1548
1549         ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
1550
1551         s_rule->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_info->fltr_rule_id);
1552
1553         /* Update switch rule with new rule set to forward VSI list */
1554         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1555                                  ice_aqc_opc_update_sw_rules, NULL);
1556
1557         ice_free(hw, s_rule);
1558         return status;
1559 }
1560
1561 /**
1562  * ice_update_sw_rule_bridge_mode
1563  * @hw: pointer to the HW struct
1564  *
1565  * Updates unicast switch filter rules based on VEB/VEPA mode
1566  */
1567 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
1568 {
1569         struct ice_switch_info *sw = hw->switch_info;
1570         struct ice_fltr_mgmt_list_entry *fm_entry;
1571         enum ice_status status = ICE_SUCCESS;
1572         struct LIST_HEAD_TYPE *rule_head;
1573         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
1574
1575         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1576         rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1577
1578         ice_acquire_lock(rule_lock);
1579         LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry,
1580                             list_entry) {
1581                 struct ice_fltr_info *fi = &fm_entry->fltr_info;
1582                 u8 *addr = fi->l_data.mac.mac_addr;
1583
1584                 /* Update unicast Tx rules to reflect the selected
1585                  * VEB/VEPA mode
1586                  */
1587                 if ((fi->flag & ICE_FLTR_TX) && IS_UNICAST_ETHER_ADDR(addr) &&
1588                     (fi->fltr_act == ICE_FWD_TO_VSI ||
1589                      fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1590                      fi->fltr_act == ICE_FWD_TO_Q ||
1591                      fi->fltr_act == ICE_FWD_TO_QGRP)) {
1592                         status = ice_update_pkt_fwd_rule(hw, fi);
1593                         if (status)
1594                                 break;
1595                 }
1596         }
1597
1598         ice_release_lock(rule_lock);
1599
1600         return status;
1601 }
1602
1603 /**
1604  * ice_add_update_vsi_list
1605  * @hw: pointer to the hardware structure
1606  * @m_entry: pointer to current filter management list entry
1607  * @cur_fltr: filter information from the book keeping entry
1608  * @new_fltr: filter information with the new VSI to be added
1609  *
1610  * Call AQ command to add or update previously created VSI list with new VSI.
1611  *
1612  * Helper function to do book keeping associated with adding filter information
1613  * The algorithm to do the book keeping is described below :
1614  * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
1615  *      if only one VSI has been added till now
1616  *              Allocate a new VSI list and add two VSIs
1617  *              to this list using switch rule command
1618  *              Update the previously created switch rule with the
1619  *              newly created VSI list ID
1620  *      if a VSI list was previously created
1621  *              Add the new VSI to the previously created VSI list set
1622  *              using the update switch rule command
1623  */
1624 static enum ice_status
1625 ice_add_update_vsi_list(struct ice_hw *hw,
1626                         struct ice_fltr_mgmt_list_entry *m_entry,
1627                         struct ice_fltr_info *cur_fltr,
1628                         struct ice_fltr_info *new_fltr)
1629 {
1630         enum ice_status status = ICE_SUCCESS;
1631         u16 vsi_list_id = 0;
1632
1633         if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
1634              cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
1635                 return ICE_ERR_NOT_IMPL;
1636
1637         if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
1638              new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
1639             (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
1640              cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
1641                 return ICE_ERR_NOT_IMPL;
1642
1643         if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
1644                 /* Only one entry existed in the mapping and it was not already
1645                  * a part of a VSI list. So, create a VSI list with the old and
1646                  * new VSIs.
1647                  */
1648                 struct ice_fltr_info tmp_fltr;
1649                 u16 vsi_handle_arr[2];
1650
1651                 /* A rule already exists with the new VSI being added */
1652                 if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
1653                         return ICE_ERR_ALREADY_EXISTS;
1654
1655                 vsi_handle_arr[0] = cur_fltr->vsi_handle;
1656                 vsi_handle_arr[1] = new_fltr->vsi_handle;
1657                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1658                                                   &vsi_list_id,
1659                                                   new_fltr->lkup_type);
1660                 if (status)
1661                         return status;
1662
1663                 tmp_fltr = *new_fltr;
1664                 tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
1665                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1666                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1667                 /* Update the previous switch rule of "MAC forward to VSI" to
1668                  * "MAC fwd to VSI list"
1669                  */
1670                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1671                 if (status)
1672                         return status;
1673
1674                 cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1675                 cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1676                 m_entry->vsi_list_info =
1677                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1678                                                 vsi_list_id);
1679
1680                 /* If this entry was large action then the large action needs
1681                  * to be updated to point to FWD to VSI list
1682                  */
1683                 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1684                         status =
1685                             ice_add_marker_act(hw, m_entry,
1686                                                m_entry->sw_marker_id,
1687                                                m_entry->lg_act_idx);
1688         } else {
1689                 u16 vsi_handle = new_fltr->vsi_handle;
1690                 enum ice_adminq_opc opcode;
1691
1692                 if (!m_entry->vsi_list_info)
1693                         return ICE_ERR_CFG;
1694
1695                 /* A rule already exists with the new VSI being added */
1696                 if (ice_is_bit_set(m_entry->vsi_list_info->vsi_map, vsi_handle))
1697                         return ICE_SUCCESS;
1698
1699                 /* Update the previously created VSI list set with
1700                  * the new VSI ID passed in
1701                  */
1702                 vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1703                 opcode = ice_aqc_opc_update_sw_rules;
1704
1705                 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
1706                                                   vsi_list_id, false, opcode,
1707                                                   new_fltr->lkup_type);
1708                 /* update VSI list mapping info with new VSI ID */
1709                 if (!status)
1710                         ice_set_bit(vsi_handle,
1711                                     m_entry->vsi_list_info->vsi_map);
1712         }
1713         if (!status)
1714                 m_entry->vsi_count++;
1715         return status;
1716 }
1717
1718 /**
1719  * ice_find_rule_entry - Search a rule entry
1720  * @hw: pointer to the hardware structure
1721  * @recp_id: lookup type for which the specified rule needs to be searched
1722  * @f_info: rule information
1723  *
1724  * Helper function to search for a given rule entry
1725  * Returns pointer to entry storing the rule if found
1726  */
1727 static struct ice_fltr_mgmt_list_entry *
1728 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
1729 {
1730         struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
1731         struct ice_switch_info *sw = hw->switch_info;
1732         struct LIST_HEAD_TYPE *list_head;
1733
1734         list_head = &sw->recp_list[recp_id].filt_rules;
1735         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
1736                             list_entry) {
1737                 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
1738                             sizeof(f_info->l_data)) &&
1739                     f_info->flag == list_itr->fltr_info.flag) {
1740                         ret = list_itr;
1741                         break;
1742                 }
1743         }
1744         return ret;
1745 }
1746
1747 /**
1748  * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
1749  * @hw: pointer to the hardware structure
1750  * @recp_id: lookup type for which VSI lists needs to be searched
1751  * @vsi_handle: VSI handle to be found in VSI list
1752  * @vsi_list_id: VSI list ID found containing vsi_handle
1753  *
1754  * Helper function to search a VSI list with single entry containing given VSI
1755  * handle element. This can be extended further to search VSI list with more
1756  * than 1 vsi_count. Returns pointer to VSI list entry if found.
1757  */
1758 static struct ice_vsi_list_map_info *
1759 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
1760                         u16 *vsi_list_id)
1761 {
1762         struct ice_vsi_list_map_info *map_info = NULL;
1763         struct ice_switch_info *sw = hw->switch_info;
1764         struct ice_fltr_mgmt_list_entry *list_itr;
1765         struct LIST_HEAD_TYPE *list_head;
1766
1767         list_head = &sw->recp_list[recp_id].filt_rules;
1768         LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
1769                             list_entry) {
1770                 if (list_itr->vsi_count == 1 && list_itr->vsi_list_info) {
1771                         map_info = list_itr->vsi_list_info;
1772                         if (ice_is_bit_set(map_info->vsi_map, vsi_handle)) {
1773                                 *vsi_list_id = map_info->vsi_list_id;
1774                                 return map_info;
1775                         }
1776                 }
1777         }
1778         return NULL;
1779 }
1780
1781 /**
1782  * ice_add_rule_internal - add rule for a given lookup type
1783  * @hw: pointer to the hardware structure
1784  * @recp_id: lookup type (recipe ID) for which rule has to be added
1785  * @f_entry: structure containing MAC forwarding information
1786  *
1787  * Adds or updates the rule lists for a given recipe
1788  */
1789 static enum ice_status
1790 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
1791                       struct ice_fltr_list_entry *f_entry)
1792 {
1793         struct ice_switch_info *sw = hw->switch_info;
1794         struct ice_fltr_info *new_fltr, *cur_fltr;
1795         struct ice_fltr_mgmt_list_entry *m_entry;
1796         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
1797         enum ice_status status = ICE_SUCCESS;
1798
1799         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1800                 return ICE_ERR_PARAM;
1801
1802         /* Load the hw_vsi_id only if the fwd action is fwd to VSI */
1803         if (f_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI)
1804                 f_entry->fltr_info.fwd_id.hw_vsi_id =
1805                         ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1806
1807         rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1808
1809         ice_acquire_lock(rule_lock);
1810         new_fltr = &f_entry->fltr_info;
1811         if (new_fltr->flag & ICE_FLTR_RX)
1812                 new_fltr->src = hw->port_info->lport;
1813         else if (new_fltr->flag & ICE_FLTR_TX)
1814                 new_fltr->src =
1815                         ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1816
1817         m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
1818         if (!m_entry) {
1819                 ice_release_lock(rule_lock);
1820                 return ice_create_pkt_fwd_rule(hw, f_entry);
1821         }
1822
1823         cur_fltr = &m_entry->fltr_info;
1824         status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
1825         ice_release_lock(rule_lock);
1826
1827         return status;
1828 }
1829
1830 /**
1831  * ice_remove_vsi_list_rule
1832  * @hw: pointer to the hardware structure
1833  * @vsi_list_id: VSI list ID generated as part of allocate resource
1834  * @lkup_type: switch rule filter lookup type
1835  *
1836  * The VSI list should be emptied before this function is called to remove the
1837  * VSI list.
1838  */
1839 static enum ice_status
1840 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1841                          enum ice_sw_lkup_type lkup_type)
1842 {
1843         struct ice_aqc_sw_rules_elem *s_rule;
1844         enum ice_status status;
1845         u16 s_rule_size;
1846
1847         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
1848         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
1849         if (!s_rule)
1850                 return ICE_ERR_NO_MEMORY;
1851
1852         s_rule->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
1853         s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
1854
1855         /* Free the vsi_list resource that we allocated. It is assumed that the
1856          * list is empty at this point.
1857          */
1858         status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
1859                                             ice_aqc_opc_free_res);
1860
1861         ice_free(hw, s_rule);
1862         return status;
1863 }
1864
1865 /**
1866  * ice_rem_update_vsi_list
1867  * @hw: pointer to the hardware structure
1868  * @vsi_handle: VSI handle of the VSI to remove
1869  * @fm_list: filter management entry for which the VSI list management needs to
1870  *           be done
1871  */
1872 static enum ice_status
1873 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
1874                         struct ice_fltr_mgmt_list_entry *fm_list)
1875 {
1876         enum ice_sw_lkup_type lkup_type;
1877         enum ice_status status = ICE_SUCCESS;
1878         u16 vsi_list_id;
1879
1880         if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
1881             fm_list->vsi_count == 0)
1882                 return ICE_ERR_PARAM;
1883
1884         /* A rule with the VSI being removed does not exist */
1885         if (!ice_is_bit_set(fm_list->vsi_list_info->vsi_map, vsi_handle))
1886                 return ICE_ERR_DOES_NOT_EXIST;
1887
1888         lkup_type = fm_list->fltr_info.lkup_type;
1889         vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
1890         status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
1891                                           ice_aqc_opc_update_sw_rules,
1892                                           lkup_type);
1893         if (status)
1894                 return status;
1895
1896         fm_list->vsi_count--;
1897         ice_clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
1898
1899         if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
1900                 struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
1901                 struct ice_vsi_list_map_info *vsi_list_info =
1902                         fm_list->vsi_list_info;
1903                 u16 rem_vsi_handle;
1904
1905                 rem_vsi_handle = ice_find_first_bit(vsi_list_info->vsi_map,
1906                                                     ICE_MAX_VSI);
1907                 if (!ice_is_vsi_valid(hw, rem_vsi_handle))
1908                         return ICE_ERR_OUT_OF_RANGE;
1909
1910                 /* Make sure VSI list is empty before removing it below */
1911                 status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
1912                                                   vsi_list_id, true,
1913                                                   ice_aqc_opc_update_sw_rules,
1914                                                   lkup_type);
1915                 if (status)
1916                         return status;
1917
1918                 tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
1919                 tmp_fltr_info.fwd_id.hw_vsi_id =
1920                         ice_get_hw_vsi_num(hw, rem_vsi_handle);
1921                 tmp_fltr_info.vsi_handle = rem_vsi_handle;
1922                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
1923                 if (status) {
1924                         ice_debug(hw, ICE_DBG_SW,
1925                                   "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
1926                                   tmp_fltr_info.fwd_id.hw_vsi_id, status);
1927                         return status;
1928                 }
1929
1930                 fm_list->fltr_info = tmp_fltr_info;
1931         }
1932
1933         if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
1934             (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
1935                 struct ice_vsi_list_map_info *vsi_list_info =
1936                         fm_list->vsi_list_info;
1937
1938                 /* Remove the VSI list since it is no longer used */
1939                 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
1940                 if (status) {
1941                         ice_debug(hw, ICE_DBG_SW,
1942                                   "Failed to remove VSI list %d, error %d\n",
1943                                   vsi_list_id, status);
1944                         return status;
1945                 }
1946
1947                 LIST_DEL(&vsi_list_info->list_entry);
1948                 ice_free(hw, vsi_list_info);
1949                 fm_list->vsi_list_info = NULL;
1950         }
1951
1952         return status;
1953 }
1954
1955 /**
1956  * ice_remove_rule_internal - Remove a filter rule of a given type
1957  *
1958  * @hw: pointer to the hardware structure
1959  * @recp_id: recipe ID for which the rule needs to removed
1960  * @f_entry: rule entry containing filter information
1961  */
1962 static enum ice_status
1963 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
1964                          struct ice_fltr_list_entry *f_entry)
1965 {
1966         struct ice_switch_info *sw = hw->switch_info;
1967         struct ice_fltr_mgmt_list_entry *list_elem;
1968         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
1969         enum ice_status status = ICE_SUCCESS;
1970         bool remove_rule = false;
1971         u16 vsi_handle;
1972
1973         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1974                 return ICE_ERR_PARAM;
1975         f_entry->fltr_info.fwd_id.hw_vsi_id =
1976                 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1977
1978         rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1979         ice_acquire_lock(rule_lock);
1980         list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
1981         if (!list_elem) {
1982                 status = ICE_ERR_DOES_NOT_EXIST;
1983                 goto exit;
1984         }
1985
1986         if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
1987                 remove_rule = true;
1988         } else if (!list_elem->vsi_list_info) {
1989                 status = ICE_ERR_DOES_NOT_EXIST;
1990                 goto exit;
1991         } else if (list_elem->vsi_list_info->ref_cnt > 1) {
1992                 /* a ref_cnt > 1 indicates that the vsi_list is being
1993                  * shared by multiple rules. Decrement the ref_cnt and
1994                  * remove this rule, but do not modify the list, as it
1995                  * is in-use by other rules.
1996                  */
1997                 list_elem->vsi_list_info->ref_cnt--;
1998                 remove_rule = true;
1999         } else {
2000                 /* a ref_cnt of 1 indicates the vsi_list is only used
2001                  * by one rule. However, the original removal request is only
2002                  * for a single VSI. Update the vsi_list first, and only
2003                  * remove the rule if there are no further VSIs in this list.
2004                  */
2005                 vsi_handle = f_entry->fltr_info.vsi_handle;
2006                 status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
2007                 if (status)
2008                         goto exit;
2009                 /* if VSI count goes to zero after updating the VSI list */
2010                 if (list_elem->vsi_count == 0)
2011                         remove_rule = true;
2012         }
2013
2014         if (remove_rule) {
2015                 /* Remove the lookup rule */
2016                 struct ice_aqc_sw_rules_elem *s_rule;
2017
2018                 s_rule = (struct ice_aqc_sw_rules_elem *)
2019                         ice_malloc(hw, ICE_SW_RULE_RX_TX_NO_HDR_SIZE);
2020                 if (!s_rule) {
2021                         status = ICE_ERR_NO_MEMORY;
2022                         goto exit;
2023                 }
2024
2025                 ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
2026                                  ice_aqc_opc_remove_sw_rules);
2027
2028                 status = ice_aq_sw_rules(hw, s_rule,
2029                                          ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
2030                                          ice_aqc_opc_remove_sw_rules, NULL);
2031                 if (status)
2032                         goto exit;
2033
2034                 /* Remove a book keeping from the list */
2035                 ice_free(hw, s_rule);
2036
2037                 LIST_DEL(&list_elem->list_entry);
2038                 ice_free(hw, list_elem);
2039         }
2040 exit:
2041         ice_release_lock(rule_lock);
2042         return status;
2043 }
2044
2045 /**
2046  * ice_aq_get_res_alloc - get allocated resources
2047  * @hw: pointer to the HW struct
2048  * @num_entries: pointer to u16 to store the number of resource entries returned
2049  * @buf: pointer to user-supplied buffer
2050  * @buf_size: size of buff
2051  * @cd: pointer to command details structure or NULL
2052  *
2053  * The user-supplied buffer must be large enough to store the resource
2054  * information for all resource types. Each resource type is an
2055  * ice_aqc_get_res_resp_data_elem structure.
2056  */
2057 enum ice_status
2058 ice_aq_get_res_alloc(struct ice_hw *hw, u16 *num_entries, void *buf,
2059                      u16 buf_size, struct ice_sq_cd *cd)
2060 {
2061         struct ice_aqc_get_res_alloc *resp;
2062         enum ice_status status;
2063         struct ice_aq_desc desc;
2064
2065         if (!buf)
2066                 return ICE_ERR_BAD_PTR;
2067
2068         if (buf_size < ICE_AQ_GET_RES_ALLOC_BUF_LEN)
2069                 return ICE_ERR_INVAL_SIZE;
2070
2071         resp = &desc.params.get_res;
2072
2073         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_res_alloc);
2074         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2075
2076         if (!status && num_entries)
2077                 *num_entries = LE16_TO_CPU(resp->resp_elem_num);
2078
2079         return status;
2080 }
2081
2082 /**
2083  * ice_aq_get_res_descs - get allocated resource descriptors
2084  * @hw: pointer to the hardware structure
2085  * @num_entries: number of resource entries in buffer
2086  * @buf: Indirect buffer to hold data parameters and response
2087  * @buf_size: size of buffer for indirect commands
2088  * @res_type: resource type
2089  * @res_shared: is resource shared
2090  * @desc_id: input - first desc ID to start; output - next desc ID
2091  * @cd: pointer to command details structure or NULL
2092  */
2093 enum ice_status
2094 ice_aq_get_res_descs(struct ice_hw *hw, u16 num_entries,
2095                      struct ice_aqc_get_allocd_res_desc_resp *buf,
2096                      u16 buf_size, u16 res_type, bool res_shared, u16 *desc_id,
2097                      struct ice_sq_cd *cd)
2098 {
2099         struct ice_aqc_get_allocd_res_desc *cmd;
2100         struct ice_aq_desc desc;
2101         enum ice_status status;
2102
2103         ice_debug(hw, ICE_DBG_TRACE, "ice_aq_get_res_descs");
2104
2105         cmd = &desc.params.get_res_desc;
2106
2107         if (!buf)
2108                 return ICE_ERR_PARAM;
2109
2110         if (buf_size != (num_entries * sizeof(*buf)))
2111                 return ICE_ERR_PARAM;
2112
2113         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_allocd_res_desc);
2114
2115         cmd->ops.cmd.res = CPU_TO_LE16(((res_type << ICE_AQC_RES_TYPE_S) &
2116                                          ICE_AQC_RES_TYPE_M) | (res_shared ?
2117                                         ICE_AQC_RES_TYPE_FLAG_SHARED : 0));
2118         cmd->ops.cmd.first_desc = CPU_TO_LE16(*desc_id);
2119
2120         desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
2121
2122         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2123         if (!status)
2124                 *desc_id = LE16_TO_CPU(cmd->ops.resp.next_desc);
2125
2126         return status;
2127 }
2128
2129 /**
2130  * ice_add_mac - Add a MAC address based filter rule
2131  * @hw: pointer to the hardware structure
2132  * @m_list: list of MAC addresses and forwarding information
2133  *
2134  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
2135  * multiple unicast addresses, the function assumes that all the
2136  * addresses are unique in a given add_mac call. It doesn't
2137  * check for duplicates in this case, removing duplicates from a given
2138  * list should be taken care of in the caller of this function.
2139  */
2140 enum ice_status
2141 ice_add_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2142 {
2143         struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
2144         struct ice_fltr_list_entry *m_list_itr;
2145         struct LIST_HEAD_TYPE *rule_head;
2146         u16 elem_sent, total_elem_left;
2147         struct ice_switch_info *sw;
2148         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2149         enum ice_status status = ICE_SUCCESS;
2150         u16 num_unicast = 0;
2151         u16 s_rule_size;
2152
2153         if (!m_list || !hw)
2154                 return ICE_ERR_PARAM;
2155         s_rule = NULL;
2156         sw = hw->switch_info;
2157         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2158         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2159                             list_entry) {
2160                 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
2161                 u16 vsi_handle;
2162                 u16 hw_vsi_id;
2163
2164                 m_list_itr->fltr_info.flag = ICE_FLTR_TX;
2165                 vsi_handle = m_list_itr->fltr_info.vsi_handle;
2166                 if (!ice_is_vsi_valid(hw, vsi_handle))
2167                         return ICE_ERR_PARAM;
2168                 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2169                 m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
2170                 /* update the src in case it is VSI num */
2171                 if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
2172                         return ICE_ERR_PARAM;
2173                 m_list_itr->fltr_info.src = hw_vsi_id;
2174                 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
2175                     IS_ZERO_ETHER_ADDR(add))
2176                         return ICE_ERR_PARAM;
2177                 if (IS_UNICAST_ETHER_ADDR(add) && !hw->ucast_shared) {
2178                         /* Don't overwrite the unicast address */
2179                         ice_acquire_lock(rule_lock);
2180                         if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
2181                                                 &m_list_itr->fltr_info)) {
2182                                 ice_release_lock(rule_lock);
2183                                 return ICE_ERR_ALREADY_EXISTS;
2184                         }
2185                         ice_release_lock(rule_lock);
2186                         num_unicast++;
2187                 } else if (IS_MULTICAST_ETHER_ADDR(add) ||
2188                            (IS_UNICAST_ETHER_ADDR(add) && hw->ucast_shared)) {
2189                         m_list_itr->status =
2190                                 ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
2191                                                       m_list_itr);
2192                         if (m_list_itr->status)
2193                                 return m_list_itr->status;
2194                 }
2195         }
2196
2197         ice_acquire_lock(rule_lock);
2198         /* Exit if no suitable entries were found for adding bulk switch rule */
2199         if (!num_unicast) {
2200                 status = ICE_SUCCESS;
2201                 goto ice_add_mac_exit;
2202         }
2203
2204         rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2205
2206         /* Allocate switch rule buffer for the bulk update for unicast */
2207         s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
2208         s_rule = (struct ice_aqc_sw_rules_elem *)
2209                 ice_calloc(hw, num_unicast, s_rule_size);
2210         if (!s_rule) {
2211                 status = ICE_ERR_NO_MEMORY;
2212                 goto ice_add_mac_exit;
2213         }
2214
2215         r_iter = s_rule;
2216         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2217                             list_entry) {
2218                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2219                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2220
2221                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2222                         ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
2223                                          ice_aqc_opc_add_sw_rules);
2224                         r_iter = (struct ice_aqc_sw_rules_elem *)
2225                                 ((u8 *)r_iter + s_rule_size);
2226                 }
2227         }
2228
2229         /* Call AQ bulk switch rule update for all unicast addresses */
2230         r_iter = s_rule;
2231         /* Call AQ switch rule in AQ_MAX chunk */
2232         for (total_elem_left = num_unicast; total_elem_left > 0;
2233              total_elem_left -= elem_sent) {
2234                 struct ice_aqc_sw_rules_elem *entry = r_iter;
2235
2236                 elem_sent = min(total_elem_left,
2237                                 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
2238                 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
2239                                          elem_sent, ice_aqc_opc_add_sw_rules,
2240                                          NULL);
2241                 if (status)
2242                         goto ice_add_mac_exit;
2243                 r_iter = (struct ice_aqc_sw_rules_elem *)
2244                         ((u8 *)r_iter + (elem_sent * s_rule_size));
2245         }
2246
2247         /* Fill up rule ID based on the value returned from FW */
2248         r_iter = s_rule;
2249         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2250                             list_entry) {
2251                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2252                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2253                 struct ice_fltr_mgmt_list_entry *fm_entry;
2254
2255                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2256                         f_info->fltr_rule_id =
2257                                 LE16_TO_CPU(r_iter->pdata.lkup_tx_rx.index);
2258                         f_info->fltr_act = ICE_FWD_TO_VSI;
2259                         /* Create an entry to track this MAC address */
2260                         fm_entry = (struct ice_fltr_mgmt_list_entry *)
2261                                 ice_malloc(hw, sizeof(*fm_entry));
2262                         if (!fm_entry) {
2263                                 status = ICE_ERR_NO_MEMORY;
2264                                 goto ice_add_mac_exit;
2265                         }
2266                         fm_entry->fltr_info = *f_info;
2267                         fm_entry->vsi_count = 1;
2268                         /* The book keeping entries will get removed when
2269                          * base driver calls remove filter AQ command
2270                          */
2271
2272                         LIST_ADD(&fm_entry->list_entry, rule_head);
2273                         r_iter = (struct ice_aqc_sw_rules_elem *)
2274                                 ((u8 *)r_iter + s_rule_size);
2275                 }
2276         }
2277
2278 ice_add_mac_exit:
2279         ice_release_lock(rule_lock);
2280         if (s_rule)
2281                 ice_free(hw, s_rule);
2282         return status;
2283 }
2284
2285 /**
2286  * ice_add_vlan_internal - Add one VLAN based filter rule
2287  * @hw: pointer to the hardware structure
2288  * @f_entry: filter entry containing one VLAN information
2289  */
2290 static enum ice_status
2291 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
2292 {
2293         struct ice_switch_info *sw = hw->switch_info;
2294         struct ice_fltr_mgmt_list_entry *v_list_itr;
2295         struct ice_fltr_info *new_fltr, *cur_fltr;
2296         enum ice_sw_lkup_type lkup_type;
2297         u16 vsi_list_id = 0, vsi_handle;
2298         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2299         enum ice_status status = ICE_SUCCESS;
2300
2301         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2302                 return ICE_ERR_PARAM;
2303
2304         f_entry->fltr_info.fwd_id.hw_vsi_id =
2305                 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2306         new_fltr = &f_entry->fltr_info;
2307
2308         /* VLAN ID should only be 12 bits */
2309         if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
2310                 return ICE_ERR_PARAM;
2311
2312         if (new_fltr->src_id != ICE_SRC_ID_VSI)
2313                 return ICE_ERR_PARAM;
2314
2315         new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
2316         lkup_type = new_fltr->lkup_type;
2317         vsi_handle = new_fltr->vsi_handle;
2318         rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2319         ice_acquire_lock(rule_lock);
2320         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
2321         if (!v_list_itr) {
2322                 struct ice_vsi_list_map_info *map_info = NULL;
2323
2324                 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
2325                         /* All VLAN pruning rules use a VSI list. Check if
2326                          * there is already a VSI list containing VSI that we
2327                          * want to add. If found, use the same vsi_list_id for
2328                          * this new VLAN rule or else create a new list.
2329                          */
2330                         map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
2331                                                            vsi_handle,
2332                                                            &vsi_list_id);
2333                         if (!map_info) {
2334                                 status = ice_create_vsi_list_rule(hw,
2335                                                                   &vsi_handle,
2336                                                                   1,
2337                                                                   &vsi_list_id,
2338                                                                   lkup_type);
2339                                 if (status)
2340                                         goto exit;
2341                         }
2342                         /* Convert the action to forwarding to a VSI list. */
2343                         new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2344                         new_fltr->fwd_id.vsi_list_id = vsi_list_id;
2345                 }
2346
2347                 status = ice_create_pkt_fwd_rule(hw, f_entry);
2348                 if (!status) {
2349                         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
2350                                                          new_fltr);
2351                         if (!v_list_itr) {
2352                                 status = ICE_ERR_DOES_NOT_EXIST;
2353                                 goto exit;
2354                         }
2355                         /* reuse VSI list for new rule and increment ref_cnt */
2356                         if (map_info) {
2357                                 v_list_itr->vsi_list_info = map_info;
2358                                 map_info->ref_cnt++;
2359                         } else {
2360                                 v_list_itr->vsi_list_info =
2361                                         ice_create_vsi_list_map(hw, &vsi_handle,
2362                                                                 1, vsi_list_id);
2363                         }
2364                 }
2365         } else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
2366                 /* Update existing VSI list to add new VSI ID only if it used
2367                  * by one VLAN rule.
2368                  */
2369                 cur_fltr = &v_list_itr->fltr_info;
2370                 status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
2371                                                  new_fltr);
2372         } else {
2373                 /* If VLAN rule exists and VSI list being used by this rule is
2374                  * referenced by more than 1 VLAN rule. Then create a new VSI
2375                  * list appending previous VSI with new VSI and update existing
2376                  * VLAN rule to point to new VSI list ID
2377                  */
2378                 struct ice_fltr_info tmp_fltr;
2379                 u16 vsi_handle_arr[2];
2380                 u16 cur_handle;
2381
2382                 /* Current implementation only supports reusing VSI list with
2383                  * one VSI count. We should never hit below condition
2384                  */
2385                 if (v_list_itr->vsi_count > 1 &&
2386                     v_list_itr->vsi_list_info->ref_cnt > 1) {
2387                         ice_debug(hw, ICE_DBG_SW,
2388                                   "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
2389                         status = ICE_ERR_CFG;
2390                         goto exit;
2391                 }
2392
2393                 cur_handle =
2394                         ice_find_first_bit(v_list_itr->vsi_list_info->vsi_map,
2395                                            ICE_MAX_VSI);
2396
2397                 /* A rule already exists with the new VSI being added */
2398                 if (cur_handle == vsi_handle) {
2399                         status = ICE_ERR_ALREADY_EXISTS;
2400                         goto exit;
2401                 }
2402
2403                 vsi_handle_arr[0] = cur_handle;
2404                 vsi_handle_arr[1] = vsi_handle;
2405                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2406                                                   &vsi_list_id, lkup_type);
2407                 if (status)
2408                         goto exit;
2409
2410                 tmp_fltr = v_list_itr->fltr_info;
2411                 tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
2412                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2413                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2414                 /* Update the previous switch rule to a new VSI list which
2415                  * includes current VSI that is requested
2416                  */
2417                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2418                 if (status)
2419                         goto exit;
2420
2421                 /* before overriding VSI list map info. decrement ref_cnt of
2422                  * previous VSI list
2423                  */
2424                 v_list_itr->vsi_list_info->ref_cnt--;
2425
2426                 /* now update to newly created list */
2427                 v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
2428                 v_list_itr->vsi_list_info =
2429                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2430                                                 vsi_list_id);
2431                 v_list_itr->vsi_count++;
2432         }
2433
2434 exit:
2435         ice_release_lock(rule_lock);
2436         return status;
2437 }
2438
2439 /**
2440  * ice_add_vlan - Add VLAN based filter rule
2441  * @hw: pointer to the hardware structure
2442  * @v_list: list of VLAN entries and forwarding information
2443  */
2444 enum ice_status
2445 ice_add_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2446 {
2447         struct ice_fltr_list_entry *v_list_itr;
2448
2449         if (!v_list || !hw)
2450                 return ICE_ERR_PARAM;
2451
2452         LIST_FOR_EACH_ENTRY(v_list_itr, v_list, ice_fltr_list_entry,
2453                             list_entry) {
2454                 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
2455                         return ICE_ERR_PARAM;
2456                 v_list_itr->fltr_info.flag = ICE_FLTR_TX;
2457                 v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
2458                 if (v_list_itr->status)
2459                         return v_list_itr->status;
2460         }
2461         return ICE_SUCCESS;
2462 }
2463
2464 #ifndef NO_MACVLAN_SUPPORT
2465 /**
2466  * ice_add_mac_vlan - Add MAC and VLAN pair based filter rule
2467  * @hw: pointer to the hardware structure
2468  * @mv_list: list of MAC and VLAN filters
2469  *
2470  * If the VSI on which the MAC-VLAN pair has to be added has Rx and Tx VLAN
2471  * pruning bits enabled, then it is the responsibility of the caller to make
2472  * sure to add a VLAN only filter on the same VSI. Packets belonging to that
2473  * VLAN won't be received on that VSI otherwise.
2474  */
2475 enum ice_status
2476 ice_add_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *mv_list)
2477 {
2478         struct ice_fltr_list_entry *mv_list_itr;
2479
2480         if (!mv_list || !hw)
2481                 return ICE_ERR_PARAM;
2482
2483         LIST_FOR_EACH_ENTRY(mv_list_itr, mv_list, ice_fltr_list_entry,
2484                             list_entry) {
2485                 enum ice_sw_lkup_type l_type =
2486                         mv_list_itr->fltr_info.lkup_type;
2487
2488                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
2489                         return ICE_ERR_PARAM;
2490                 mv_list_itr->fltr_info.flag = ICE_FLTR_TX;
2491                 mv_list_itr->status =
2492                         ice_add_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
2493                                               mv_list_itr);
2494                 if (mv_list_itr->status)
2495                         return mv_list_itr->status;
2496         }
2497         return ICE_SUCCESS;
2498 }
2499 #endif
2500
2501 /**
2502  * ice_add_eth_mac - Add ethertype and MAC based filter rule
2503  * @hw: pointer to the hardware structure
2504  * @em_list: list of ether type MAC filter, MAC is optional
2505  */
2506 enum ice_status
2507 ice_add_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2508 {
2509         struct ice_fltr_list_entry *em_list_itr;
2510
2511         LIST_FOR_EACH_ENTRY(em_list_itr, em_list, ice_fltr_list_entry,
2512                             list_entry) {
2513                 enum ice_sw_lkup_type l_type =
2514                         em_list_itr->fltr_info.lkup_type;
2515
2516                 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2517                     l_type != ICE_SW_LKUP_ETHERTYPE)
2518                         return ICE_ERR_PARAM;
2519
2520                 em_list_itr->fltr_info.flag = ICE_FLTR_TX;
2521                 em_list_itr->status = ice_add_rule_internal(hw, l_type,
2522                                                             em_list_itr);
2523                 if (em_list_itr->status)
2524                         return em_list_itr->status;
2525         }
2526         return ICE_SUCCESS;
2527 }
2528
2529 /**
2530  * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
2531  * @hw: pointer to the hardware structure
2532  * @em_list: list of ethertype or ethertype MAC entries
2533  */
2534 enum ice_status
2535 ice_remove_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2536 {
2537         struct ice_fltr_list_entry *em_list_itr, *tmp;
2538
2539         if (!em_list || !hw)
2540                 return ICE_ERR_PARAM;
2541
2542         LIST_FOR_EACH_ENTRY_SAFE(em_list_itr, tmp, em_list, ice_fltr_list_entry,
2543                                  list_entry) {
2544                 enum ice_sw_lkup_type l_type =
2545                         em_list_itr->fltr_info.lkup_type;
2546
2547                 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2548                     l_type != ICE_SW_LKUP_ETHERTYPE)
2549                         return ICE_ERR_PARAM;
2550
2551                 em_list_itr->status = ice_remove_rule_internal(hw, l_type,
2552                                                                em_list_itr);
2553                 if (em_list_itr->status)
2554                         return em_list_itr->status;
2555         }
2556         return ICE_SUCCESS;
2557 }
2558
2559
2560 /**
2561  * ice_rem_sw_rule_info
2562  * @hw: pointer to the hardware structure
2563  * @rule_head: pointer to the switch list structure that we want to delete
2564  */
2565 static void
2566 ice_rem_sw_rule_info(struct ice_hw *hw, struct LIST_HEAD_TYPE *rule_head)
2567 {
2568         if (!LIST_EMPTY(rule_head)) {
2569                 struct ice_fltr_mgmt_list_entry *entry;
2570                 struct ice_fltr_mgmt_list_entry *tmp;
2571
2572                 LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, rule_head,
2573                                          ice_fltr_mgmt_list_entry, list_entry) {
2574                         LIST_DEL(&entry->list_entry);
2575                         ice_free(hw, entry);
2576                 }
2577         }
2578 }
2579
2580
2581
2582 /**
2583  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
2584  * @pi: pointer to the port_info structure
2585  * @vsi_handle: VSI handle to set as default
2586  * @set: true to add the above mentioned switch rule, false to remove it
2587  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
2588  *
2589  * add filter rule to set/unset given VSI as default VSI for the switch
2590  * (represented by swid)
2591  */
2592 enum ice_status
2593 ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
2594                  u8 direction)
2595 {
2596         struct ice_aqc_sw_rules_elem *s_rule;
2597         struct ice_fltr_info f_info;
2598         struct ice_hw *hw = pi->hw;
2599         enum ice_adminq_opc opcode;
2600         enum ice_status status;
2601         u16 s_rule_size;
2602         u16 hw_vsi_id;
2603
2604         if (!ice_is_vsi_valid(hw, vsi_handle))
2605                 return ICE_ERR_PARAM;
2606         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2607
2608         s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
2609                             ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
2610         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
2611         if (!s_rule)
2612                 return ICE_ERR_NO_MEMORY;
2613
2614         ice_memset(&f_info, 0, sizeof(f_info), ICE_NONDMA_MEM);
2615
2616         f_info.lkup_type = ICE_SW_LKUP_DFLT;
2617         f_info.flag = direction;
2618         f_info.fltr_act = ICE_FWD_TO_VSI;
2619         f_info.fwd_id.hw_vsi_id = hw_vsi_id;
2620
2621         if (f_info.flag & ICE_FLTR_RX) {
2622                 f_info.src = pi->lport;
2623                 f_info.src_id = ICE_SRC_ID_LPORT;
2624                 if (!set)
2625                         f_info.fltr_rule_id =
2626                                 pi->dflt_rx_vsi_rule_id;
2627         } else if (f_info.flag & ICE_FLTR_TX) {
2628                 f_info.src_id = ICE_SRC_ID_VSI;
2629                 f_info.src = hw_vsi_id;
2630                 if (!set)
2631                         f_info.fltr_rule_id =
2632                                 pi->dflt_tx_vsi_rule_id;
2633         }
2634
2635         if (set)
2636                 opcode = ice_aqc_opc_add_sw_rules;
2637         else
2638                 opcode = ice_aqc_opc_remove_sw_rules;
2639
2640         ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
2641
2642         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
2643         if (status || !(f_info.flag & ICE_FLTR_TX_RX))
2644                 goto out;
2645         if (set) {
2646                 u16 index = LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
2647
2648                 if (f_info.flag & ICE_FLTR_TX) {
2649                         pi->dflt_tx_vsi_num = hw_vsi_id;
2650                         pi->dflt_tx_vsi_rule_id = index;
2651                 } else if (f_info.flag & ICE_FLTR_RX) {
2652                         pi->dflt_rx_vsi_num = hw_vsi_id;
2653                         pi->dflt_rx_vsi_rule_id = index;
2654                 }
2655         } else {
2656                 if (f_info.flag & ICE_FLTR_TX) {
2657                         pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
2658                         pi->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
2659                 } else if (f_info.flag & ICE_FLTR_RX) {
2660                         pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
2661                         pi->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
2662                 }
2663         }
2664
2665 out:
2666         ice_free(hw, s_rule);
2667         return status;
2668 }
2669
2670 /**
2671  * ice_remove_mac - remove a MAC address based filter rule
2672  * @hw: pointer to the hardware structure
2673  * @m_list: list of MAC addresses and forwarding information
2674  *
2675  * This function removes either a MAC filter rule or a specific VSI from a
2676  * VSI list for a multicast MAC address.
2677  *
2678  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
2679  * ice_add_mac. Caller should be aware that this call will only work if all
2680  * the entries passed into m_list were added previously. It will not attempt to
2681  * do a partial remove of entries that were found.
2682  */
2683 enum ice_status
2684 ice_remove_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2685 {
2686         struct ice_fltr_list_entry *list_itr, *tmp;
2687
2688         if (!m_list)
2689                 return ICE_ERR_PARAM;
2690
2691         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, m_list, ice_fltr_list_entry,
2692                                  list_entry) {
2693                 enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2694
2695                 if (l_type != ICE_SW_LKUP_MAC)
2696                         return ICE_ERR_PARAM;
2697                 list_itr->status = ice_remove_rule_internal(hw,
2698                                                             ICE_SW_LKUP_MAC,
2699                                                             list_itr);
2700                 if (list_itr->status)
2701                         return list_itr->status;
2702         }
2703         return ICE_SUCCESS;
2704 }
2705
2706 /**
2707  * ice_remove_vlan - Remove VLAN based filter rule
2708  * @hw: pointer to the hardware structure
2709  * @v_list: list of VLAN entries and forwarding information
2710  */
2711 enum ice_status
2712 ice_remove_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2713 {
2714         struct ice_fltr_list_entry *v_list_itr, *tmp;
2715
2716         if (!v_list || !hw)
2717                 return ICE_ERR_PARAM;
2718
2719         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2720                                  list_entry) {
2721                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2722
2723                 if (l_type != ICE_SW_LKUP_VLAN)
2724                         return ICE_ERR_PARAM;
2725                 v_list_itr->status = ice_remove_rule_internal(hw,
2726                                                               ICE_SW_LKUP_VLAN,
2727                                                               v_list_itr);
2728                 if (v_list_itr->status)
2729                         return v_list_itr->status;
2730         }
2731         return ICE_SUCCESS;
2732 }
2733
2734 #ifndef NO_MACVLAN_SUPPORT
2735 /**
2736  * ice_remove_mac_vlan - Remove MAC VLAN based filter rule
2737  * @hw: pointer to the hardware structure
2738  * @v_list: list of MAC VLAN entries and forwarding information
2739  */
2740 enum ice_status
2741 ice_remove_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2742 {
2743         struct ice_fltr_list_entry *v_list_itr, *tmp;
2744
2745         if (!v_list || !hw)
2746                 return ICE_ERR_PARAM;
2747
2748         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2749                                  list_entry) {
2750                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2751
2752                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
2753                         return ICE_ERR_PARAM;
2754                 v_list_itr->status =
2755                         ice_remove_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
2756                                                  v_list_itr);
2757                 if (v_list_itr->status)
2758                         return v_list_itr->status;
2759         }
2760         return ICE_SUCCESS;
2761 }
2762 #endif /* !NO_MACVLAN_SUPPORT */
2763
2764 /**
2765  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
2766  * @fm_entry: filter entry to inspect
2767  * @vsi_handle: VSI handle to compare with filter info
2768  */
2769 static bool
2770 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
2771 {
2772         return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
2773                  fm_entry->fltr_info.vsi_handle == vsi_handle) ||
2774                 (fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
2775                  (ice_is_bit_set(fm_entry->vsi_list_info->vsi_map,
2776                                  vsi_handle))));
2777 }
2778
2779 /**
2780  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
2781  * @hw: pointer to the hardware structure
2782  * @vsi_handle: VSI handle to remove filters from
2783  * @vsi_list_head: pointer to the list to add entry to
2784  * @fi: pointer to fltr_info of filter entry to copy & add
2785  *
2786  * Helper function, used when creating a list of filters to remove from
2787  * a specific VSI. The entry added to vsi_list_head is a COPY of the
2788  * original filter entry, with the exception of fltr_info.fltr_act and
2789  * fltr_info.fwd_id fields. These are set such that later logic can
2790  * extract which VSI to remove the fltr from, and pass on that information.
2791  */
2792 static enum ice_status
2793 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2794                                struct LIST_HEAD_TYPE *vsi_list_head,
2795                                struct ice_fltr_info *fi)
2796 {
2797         struct ice_fltr_list_entry *tmp;
2798
2799         /* this memory is freed up in the caller function
2800          * once filters for this VSI are removed
2801          */
2802         tmp = (struct ice_fltr_list_entry *)ice_malloc(hw, sizeof(*tmp));
2803         if (!tmp)
2804                 return ICE_ERR_NO_MEMORY;
2805
2806         tmp->fltr_info = *fi;
2807
2808         /* Overwrite these fields to indicate which VSI to remove filter from,
2809          * so find and remove logic can extract the information from the
2810          * list entries. Note that original entries will still have proper
2811          * values.
2812          */
2813         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2814         tmp->fltr_info.vsi_handle = vsi_handle;
2815         tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2816
2817         LIST_ADD(&tmp->list_entry, vsi_list_head);
2818
2819         return ICE_SUCCESS;
2820 }
2821
2822 /**
2823  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
2824  * @hw: pointer to the hardware structure
2825  * @vsi_handle: VSI handle to remove filters from
2826  * @lkup_list_head: pointer to the list that has certain lookup type filters
2827  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
2828  *
2829  * Locates all filters in lkup_list_head that are used by the given VSI,
2830  * and adds COPIES of those entries to vsi_list_head (intended to be used
2831  * to remove the listed filters).
2832  * Note that this means all entries in vsi_list_head must be explicitly
2833  * deallocated by the caller when done with list.
2834  */
2835 static enum ice_status
2836 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2837                          struct LIST_HEAD_TYPE *lkup_list_head,
2838                          struct LIST_HEAD_TYPE *vsi_list_head)
2839 {
2840         struct ice_fltr_mgmt_list_entry *fm_entry;
2841         enum ice_status status = ICE_SUCCESS;
2842
2843         /* check to make sure VSI ID is valid and within boundary */
2844         if (!ice_is_vsi_valid(hw, vsi_handle))
2845                 return ICE_ERR_PARAM;
2846
2847         LIST_FOR_EACH_ENTRY(fm_entry, lkup_list_head,
2848                             ice_fltr_mgmt_list_entry, list_entry) {
2849                 struct ice_fltr_info *fi;
2850
2851                 fi = &fm_entry->fltr_info;
2852                 if (!fi || !ice_vsi_uses_fltr(fm_entry, vsi_handle))
2853                         continue;
2854
2855                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2856                                                         vsi_list_head, fi);
2857                 if (status)
2858                         return status;
2859         }
2860         return status;
2861 }
2862
2863
2864 /**
2865  * ice_determine_promisc_mask
2866  * @fi: filter info to parse
2867  *
2868  * Helper function to determine which ICE_PROMISC_ mask corresponds
2869  * to given filter into.
2870  */
2871 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
2872 {
2873         u16 vid = fi->l_data.mac_vlan.vlan_id;
2874         u8 *macaddr = fi->l_data.mac.mac_addr;
2875         bool is_tx_fltr = false;
2876         u8 promisc_mask = 0;
2877
2878         if (fi->flag == ICE_FLTR_TX)
2879                 is_tx_fltr = true;
2880
2881         if (IS_BROADCAST_ETHER_ADDR(macaddr))
2882                 promisc_mask |= is_tx_fltr ?
2883                         ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
2884         else if (IS_MULTICAST_ETHER_ADDR(macaddr))
2885                 promisc_mask |= is_tx_fltr ?
2886                         ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
2887         else if (IS_UNICAST_ETHER_ADDR(macaddr))
2888                 promisc_mask |= is_tx_fltr ?
2889                         ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
2890         if (vid)
2891                 promisc_mask |= is_tx_fltr ?
2892                         ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
2893
2894         return promisc_mask;
2895 }
2896
2897
2898 /**
2899  * ice_remove_promisc - Remove promisc based filter rules
2900  * @hw: pointer to the hardware structure
2901  * @recp_id: recipe ID for which the rule needs to removed
2902  * @v_list: list of promisc entries
2903  */
2904 static enum ice_status
2905 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
2906                    struct LIST_HEAD_TYPE *v_list)
2907 {
2908         struct ice_fltr_list_entry *v_list_itr, *tmp;
2909
2910         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2911                                  list_entry) {
2912                 v_list_itr->status =
2913                         ice_remove_rule_internal(hw, recp_id, v_list_itr);
2914                 if (v_list_itr->status)
2915                         return v_list_itr->status;
2916         }
2917         return ICE_SUCCESS;
2918 }
2919
2920 /**
2921  * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
2922  * @hw: pointer to the hardware structure
2923  * @vsi_handle: VSI handle to clear mode
2924  * @promisc_mask: mask of promiscuous config bits to clear
2925  * @vid: VLAN ID to clear VLAN promiscuous
2926  */
2927 enum ice_status
2928 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
2929                       u16 vid)
2930 {
2931         struct ice_switch_info *sw = hw->switch_info;
2932         struct ice_fltr_list_entry *fm_entry, *tmp;
2933         struct LIST_HEAD_TYPE remove_list_head;
2934         struct ice_fltr_mgmt_list_entry *itr;
2935         struct LIST_HEAD_TYPE *rule_head;
2936         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
2937         enum ice_status status = ICE_SUCCESS;
2938         u8 recipe_id;
2939
2940         if (!ice_is_vsi_valid(hw, vsi_handle))
2941                 return ICE_ERR_PARAM;
2942
2943         if (vid)
2944                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
2945         else
2946                 recipe_id = ICE_SW_LKUP_PROMISC;
2947
2948         rule_head = &sw->recp_list[recipe_id].filt_rules;
2949         rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
2950
2951         INIT_LIST_HEAD(&remove_list_head);
2952
2953         ice_acquire_lock(rule_lock);
2954         LIST_FOR_EACH_ENTRY(itr, rule_head,
2955                             ice_fltr_mgmt_list_entry, list_entry) {
2956                 u8 fltr_promisc_mask = 0;
2957
2958                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
2959                         continue;
2960
2961                 fltr_promisc_mask |=
2962                         ice_determine_promisc_mask(&itr->fltr_info);
2963
2964                 /* Skip if filter is not completely specified by given mask */
2965                 if (fltr_promisc_mask & ~promisc_mask)
2966                         continue;
2967
2968                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2969                                                         &remove_list_head,
2970                                                         &itr->fltr_info);
2971                 if (status) {
2972                         ice_release_lock(rule_lock);
2973                         goto free_fltr_list;
2974                 }
2975         }
2976         ice_release_lock(rule_lock);
2977
2978         status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
2979
2980 free_fltr_list:
2981         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
2982                                  ice_fltr_list_entry, list_entry) {
2983                 LIST_DEL(&fm_entry->list_entry);
2984                 ice_free(hw, fm_entry);
2985         }
2986
2987         return status;
2988 }
2989
2990 /**
2991  * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
2992  * @hw: pointer to the hardware structure
2993  * @vsi_handle: VSI handle to configure
2994  * @promisc_mask: mask of promiscuous config bits
2995  * @vid: VLAN ID to set VLAN promiscuous
2996  */
2997 enum ice_status
2998 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
2999 {
3000         enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
3001         struct ice_fltr_list_entry f_list_entry;
3002         struct ice_fltr_info new_fltr;
3003         enum ice_status status = ICE_SUCCESS;
3004         bool is_tx_fltr;
3005         u16 hw_vsi_id;
3006         int pkt_type;
3007         u8 recipe_id;
3008
3009         ice_debug(hw, ICE_DBG_TRACE, "ice_set_vsi_promisc\n");
3010
3011         if (!ice_is_vsi_valid(hw, vsi_handle))
3012                 return ICE_ERR_PARAM;
3013         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3014
3015         ice_memset(&new_fltr, 0, sizeof(new_fltr), ICE_NONDMA_MEM);
3016
3017         if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
3018                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
3019                 new_fltr.l_data.mac_vlan.vlan_id = vid;
3020                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3021         } else {
3022                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
3023                 recipe_id = ICE_SW_LKUP_PROMISC;
3024         }
3025
3026         /* Separate filters must be set for each direction/packet type
3027          * combination, so we will loop over the mask value, store the
3028          * individual type, and clear it out in the input mask as it
3029          * is found.
3030          */
3031         while (promisc_mask) {
3032                 u8 *mac_addr;
3033
3034                 pkt_type = 0;
3035                 is_tx_fltr = false;
3036
3037                 if (promisc_mask & ICE_PROMISC_UCAST_RX) {
3038                         promisc_mask &= ~ICE_PROMISC_UCAST_RX;
3039                         pkt_type = UCAST_FLTR;
3040                 } else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
3041                         promisc_mask &= ~ICE_PROMISC_UCAST_TX;
3042                         pkt_type = UCAST_FLTR;
3043                         is_tx_fltr = true;
3044                 } else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
3045                         promisc_mask &= ~ICE_PROMISC_MCAST_RX;
3046                         pkt_type = MCAST_FLTR;
3047                 } else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
3048                         promisc_mask &= ~ICE_PROMISC_MCAST_TX;
3049                         pkt_type = MCAST_FLTR;
3050                         is_tx_fltr = true;
3051                 } else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
3052                         promisc_mask &= ~ICE_PROMISC_BCAST_RX;
3053                         pkt_type = BCAST_FLTR;
3054                 } else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
3055                         promisc_mask &= ~ICE_PROMISC_BCAST_TX;
3056                         pkt_type = BCAST_FLTR;
3057                         is_tx_fltr = true;
3058                 }
3059
3060                 /* Check for VLAN promiscuous flag */
3061                 if (promisc_mask & ICE_PROMISC_VLAN_RX) {
3062                         promisc_mask &= ~ICE_PROMISC_VLAN_RX;
3063                 } else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
3064                         promisc_mask &= ~ICE_PROMISC_VLAN_TX;
3065                         is_tx_fltr = true;
3066                 }
3067
3068                 /* Set filter DA based on packet type */
3069                 mac_addr = new_fltr.l_data.mac.mac_addr;
3070                 if (pkt_type == BCAST_FLTR) {
3071                         ice_memset(mac_addr, 0xff, ETH_ALEN, ICE_NONDMA_MEM);
3072                 } else if (pkt_type == MCAST_FLTR ||
3073                            pkt_type == UCAST_FLTR) {
3074                         /* Use the dummy ether header DA */
3075                         ice_memcpy(mac_addr, dummy_eth_header, ETH_ALEN,
3076                                    ICE_NONDMA_TO_NONDMA);
3077                         if (pkt_type == MCAST_FLTR)
3078                                 mac_addr[0] |= 0x1;     /* Set multicast bit */
3079                 }
3080
3081                 /* Need to reset this to zero for all iterations */
3082                 new_fltr.flag = 0;
3083                 if (is_tx_fltr) {
3084                         new_fltr.flag |= ICE_FLTR_TX;
3085                         new_fltr.src = hw_vsi_id;
3086                 } else {
3087                         new_fltr.flag |= ICE_FLTR_RX;
3088                         new_fltr.src = hw->port_info->lport;
3089                 }
3090
3091                 new_fltr.fltr_act = ICE_FWD_TO_VSI;
3092                 new_fltr.vsi_handle = vsi_handle;
3093                 new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
3094                 f_list_entry.fltr_info = new_fltr;
3095
3096                 status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
3097                 if (status != ICE_SUCCESS)
3098                         goto set_promisc_exit;
3099         }
3100
3101 set_promisc_exit:
3102         return status;
3103 }
3104
3105 /**
3106  * ice_set_vlan_vsi_promisc
3107  * @hw: pointer to the hardware structure
3108  * @vsi_handle: VSI handle to configure
3109  * @promisc_mask: mask of promiscuous config bits
3110  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
3111  *
3112  * Configure VSI with all associated VLANs to given promiscuous mode(s)
3113  */
3114 enum ice_status
3115 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3116                          bool rm_vlan_promisc)
3117 {
3118         struct ice_switch_info *sw = hw->switch_info;
3119         struct ice_fltr_list_entry *list_itr, *tmp;
3120         struct LIST_HEAD_TYPE vsi_list_head;
3121         struct LIST_HEAD_TYPE *vlan_head;
3122         struct ice_lock *vlan_lock; /* Lock to protect filter rule list */
3123         enum ice_status status;
3124         u16 vlan_id;
3125
3126         INIT_LIST_HEAD(&vsi_list_head);
3127         vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3128         vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3129         ice_acquire_lock(vlan_lock);
3130         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
3131                                           &vsi_list_head);
3132         ice_release_lock(vlan_lock);
3133         if (status)
3134                 goto free_fltr_list;
3135
3136         LIST_FOR_EACH_ENTRY(list_itr, &vsi_list_head, ice_fltr_list_entry,
3137                             list_entry) {
3138                 vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
3139                 if (rm_vlan_promisc)
3140                         status = ice_clear_vsi_promisc(hw, vsi_handle,
3141                                                        promisc_mask, vlan_id);
3142                 else
3143                         status = ice_set_vsi_promisc(hw, vsi_handle,
3144                                                      promisc_mask, vlan_id);
3145                 if (status)
3146                         break;
3147         }
3148
3149 free_fltr_list:
3150         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, &vsi_list_head,
3151                                  ice_fltr_list_entry, list_entry) {
3152                 LIST_DEL(&list_itr->list_entry);
3153                 ice_free(hw, list_itr);
3154         }
3155         return status;
3156 }
3157
3158 /**
3159  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
3160  * @hw: pointer to the hardware structure
3161  * @vsi_handle: VSI handle to remove filters from
3162  * @lkup: switch rule filter lookup type
3163  */
3164 static void
3165 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
3166                          enum ice_sw_lkup_type lkup)
3167 {
3168         struct ice_switch_info *sw = hw->switch_info;
3169         struct ice_fltr_list_entry *fm_entry;
3170         struct LIST_HEAD_TYPE remove_list_head;
3171         struct LIST_HEAD_TYPE *rule_head;
3172         struct ice_fltr_list_entry *tmp;
3173         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3174         enum ice_status status;
3175
3176         INIT_LIST_HEAD(&remove_list_head);
3177         rule_lock = &sw->recp_list[lkup].filt_rule_lock;
3178         rule_head = &sw->recp_list[lkup].filt_rules;
3179         ice_acquire_lock(rule_lock);
3180         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
3181                                           &remove_list_head);
3182         ice_release_lock(rule_lock);
3183         if (status)
3184                 return;
3185
3186         switch (lkup) {
3187         case ICE_SW_LKUP_MAC:
3188                 ice_remove_mac(hw, &remove_list_head);
3189                 break;
3190         case ICE_SW_LKUP_VLAN:
3191                 ice_remove_vlan(hw, &remove_list_head);
3192                 break;
3193         case ICE_SW_LKUP_PROMISC:
3194         case ICE_SW_LKUP_PROMISC_VLAN:
3195                 ice_remove_promisc(hw, lkup, &remove_list_head);
3196                 break;
3197         case ICE_SW_LKUP_MAC_VLAN:
3198 #ifndef NO_MACVLAN_SUPPORT
3199                 ice_remove_mac_vlan(hw, &remove_list_head);
3200 #else
3201                 ice_debug(hw, ICE_DBG_SW, "MAC VLAN look up is not supported yet\n");
3202 #endif /* !NO_MACVLAN_SUPPORT */
3203                 break;
3204         case ICE_SW_LKUP_ETHERTYPE:
3205         case ICE_SW_LKUP_ETHERTYPE_MAC:
3206         case ICE_SW_LKUP_DFLT:
3207                 ice_debug(hw, ICE_DBG_SW,
3208                           "Remove filters for this lookup type hasn't been implemented yet\n");
3209                 break;
3210         case ICE_SW_LKUP_LAST:
3211                 ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type\n");
3212                 break;
3213         }
3214
3215         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3216                                  ice_fltr_list_entry, list_entry) {
3217                 LIST_DEL(&fm_entry->list_entry);
3218                 ice_free(hw, fm_entry);
3219         }
3220 }
3221
3222 /**
3223  * ice_remove_vsi_fltr - Remove all filters for a VSI
3224  * @hw: pointer to the hardware structure
3225  * @vsi_handle: VSI handle to remove filters from
3226  */
3227 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
3228 {
3229         ice_debug(hw, ICE_DBG_TRACE, "ice_remove_vsi_fltr\n");
3230
3231         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
3232         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
3233         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
3234         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
3235         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
3236         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
3237         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
3238         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
3239 }
3240
3241 /**
3242  * ice_alloc_res_cntr - allocating resource counter
3243  * @hw: pointer to the hardware structure
3244  * @type: type of resource
3245  * @alloc_shared: if set it is shared else dedicated
3246  * @num_items: number of entries requested for FD resource type
3247  * @counter_id: counter index returned by AQ call
3248  */
3249 enum ice_status
3250 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3251                    u16 *counter_id)
3252 {
3253         struct ice_aqc_alloc_free_res_elem *buf;
3254         enum ice_status status;
3255         u16 buf_len;
3256
3257         /* Allocate resource */
3258         buf_len = sizeof(*buf);
3259         buf = (struct ice_aqc_alloc_free_res_elem *)
3260                 ice_malloc(hw, buf_len);
3261         if (!buf)
3262                 return ICE_ERR_NO_MEMORY;
3263
3264         buf->num_elems = CPU_TO_LE16(num_items);
3265         buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3266                                       ICE_AQC_RES_TYPE_M) | alloc_shared);
3267
3268         status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3269                                        ice_aqc_opc_alloc_res, NULL);
3270         if (status)
3271                 goto exit;
3272
3273         *counter_id = LE16_TO_CPU(buf->elem[0].e.sw_resp);
3274
3275 exit:
3276         ice_free(hw, buf);
3277         return status;
3278 }
3279
3280 /**
3281  * ice_free_res_cntr - free resource counter
3282  * @hw: pointer to the hardware structure
3283  * @type: type of resource
3284  * @alloc_shared: if set it is shared else dedicated
3285  * @num_items: number of entries to be freed for FD resource type
3286  * @counter_id: counter ID resource which needs to be freed
3287  */
3288 enum ice_status
3289 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3290                   u16 counter_id)
3291 {
3292         struct ice_aqc_alloc_free_res_elem *buf;
3293         enum ice_status status;
3294         u16 buf_len;
3295
3296         /* Free resource */
3297         buf_len = sizeof(*buf);
3298         buf = (struct ice_aqc_alloc_free_res_elem *)
3299                 ice_malloc(hw, buf_len);
3300         if (!buf)
3301                 return ICE_ERR_NO_MEMORY;
3302
3303         buf->num_elems = CPU_TO_LE16(num_items);
3304         buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3305                                       ICE_AQC_RES_TYPE_M) | alloc_shared);
3306         buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
3307
3308         status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3309                                        ice_aqc_opc_free_res, NULL);
3310         if (status)
3311                 ice_debug(hw, ICE_DBG_SW,
3312                           "counter resource could not be freed\n");
3313
3314         ice_free(hw, buf);
3315         return status;
3316 }
3317
3318 /**
3319  * ice_alloc_vlan_res_counter - obtain counter resource for VLAN type
3320  * @hw: pointer to the hardware structure
3321  * @counter_id: returns counter index
3322  */
3323 enum ice_status ice_alloc_vlan_res_counter(struct ice_hw *hw, u16 *counter_id)
3324 {
3325         return ice_alloc_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3326                                   ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3327                                   counter_id);
3328 }
3329
3330 /**
3331  * ice_free_vlan_res_counter - Free counter resource for VLAN type
3332  * @hw: pointer to the hardware structure
3333  * @counter_id: counter index to be freed
3334  */
3335 enum ice_status ice_free_vlan_res_counter(struct ice_hw *hw, u16 counter_id)
3336 {
3337         return ice_free_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3338                                  ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3339                                  counter_id);
3340 }
3341
3342 /**
3343  * ice_replay_vsi_fltr - Replay filters for requested VSI
3344  * @hw: pointer to the hardware structure
3345  * @vsi_handle: driver VSI handle
3346  * @recp_id: Recipe ID for which rules need to be replayed
3347  * @list_head: list for which filters need to be replayed
3348  *
3349  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
3350  * It is required to pass valid VSI handle.
3351  */
3352 static enum ice_status
3353 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
3354                     struct LIST_HEAD_TYPE *list_head)
3355 {
3356         struct ice_fltr_mgmt_list_entry *itr;
3357         enum ice_status status = ICE_SUCCESS;
3358         u16 hw_vsi_id;
3359
3360         if (LIST_EMPTY(list_head))
3361                 return status;
3362         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3363
3364         LIST_FOR_EACH_ENTRY(itr, list_head, ice_fltr_mgmt_list_entry,
3365                             list_entry) {
3366                 struct ice_fltr_list_entry f_entry;
3367
3368                 f_entry.fltr_info = itr->fltr_info;
3369                 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
3370                     itr->fltr_info.vsi_handle == vsi_handle) {
3371                         /* update the src in case it is VSI num */
3372                         if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
3373                                 f_entry.fltr_info.src = hw_vsi_id;
3374                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
3375                         if (status != ICE_SUCCESS)
3376                                 goto end;
3377                         continue;
3378                 }
3379                 if (!itr->vsi_list_info ||
3380                     !ice_is_bit_set(itr->vsi_list_info->vsi_map, vsi_handle))
3381                         continue;
3382                 /* Clearing it so that the logic can add it back */
3383                 ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
3384                 f_entry.fltr_info.vsi_handle = vsi_handle;
3385                 f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
3386                 /* update the src in case it is VSI num */
3387                 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
3388                         f_entry.fltr_info.src = hw_vsi_id;
3389                 if (recp_id == ICE_SW_LKUP_VLAN)
3390                         status = ice_add_vlan_internal(hw, &f_entry);
3391                 else
3392                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
3393                 if (status != ICE_SUCCESS)
3394                         goto end;
3395         }
3396 end:
3397         return status;
3398 }
3399
3400
3401 /**
3402  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
3403  * @hw: pointer to the hardware structure
3404  * @vsi_handle: driver VSI handle
3405  *
3406  * Replays filters for requested VSI via vsi_handle.
3407  */
3408 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
3409 {
3410         struct ice_switch_info *sw = hw->switch_info;
3411         enum ice_status status = ICE_SUCCESS;
3412         u8 i;
3413
3414         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3415                 /* Update the default recipe lines and ones that were created */
3416                 if (i < ICE_MAX_NUM_RECIPES || sw->recp_list[i].recp_created) {
3417                         struct LIST_HEAD_TYPE *head;
3418
3419                         head = &sw->recp_list[i].filt_replay_rules;
3420                         if (!sw->recp_list[i].adv_rule)
3421                                 status = ice_replay_vsi_fltr(hw, vsi_handle, i,
3422                                                              head);
3423                         if (status != ICE_SUCCESS)
3424                                 return status;
3425                 }
3426         }
3427         return status;
3428 }
3429
3430 /**
3431  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
3432  * @hw: pointer to the HW struct
3433  *
3434  * Deletes the filter replay rules.
3435  */
3436 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
3437 {
3438         struct ice_switch_info *sw = hw->switch_info;
3439         u8 i;
3440
3441         if (!sw)
3442                 return;
3443
3444         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3445                 if (!LIST_EMPTY(&sw->recp_list[i].filt_replay_rules)) {
3446                         struct LIST_HEAD_TYPE *l_head;
3447
3448                         l_head = &sw->recp_list[i].filt_replay_rules;
3449                         if (!sw->recp_list[i].adv_rule)
3450                                 ice_rem_sw_rule_info(hw, l_head);
3451                 }
3452         }
3453 }