net/ice/base: add VSI queue context framework
[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 /**
2047  * ice_add_mac - Add a MAC address based filter rule
2048  * @hw: pointer to the hardware structure
2049  * @m_list: list of MAC addresses and forwarding information
2050  *
2051  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
2052  * multiple unicast addresses, the function assumes that all the
2053  * addresses are unique in a given add_mac call. It doesn't
2054  * check for duplicates in this case, removing duplicates from a given
2055  * list should be taken care of in the caller of this function.
2056  */
2057 enum ice_status
2058 ice_add_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2059 {
2060         struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
2061         struct ice_fltr_list_entry *m_list_itr;
2062         struct LIST_HEAD_TYPE *rule_head;
2063         u16 elem_sent, total_elem_left;
2064         struct ice_switch_info *sw;
2065         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2066         enum ice_status status = ICE_SUCCESS;
2067         u16 num_unicast = 0;
2068         u16 s_rule_size;
2069
2070         if (!m_list || !hw)
2071                 return ICE_ERR_PARAM;
2072         s_rule = NULL;
2073         sw = hw->switch_info;
2074         rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2075         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2076                             list_entry) {
2077                 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
2078                 u16 vsi_handle;
2079                 u16 hw_vsi_id;
2080
2081                 m_list_itr->fltr_info.flag = ICE_FLTR_TX;
2082                 vsi_handle = m_list_itr->fltr_info.vsi_handle;
2083                 if (!ice_is_vsi_valid(hw, vsi_handle))
2084                         return ICE_ERR_PARAM;
2085                 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2086                 m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
2087                 /* update the src in case it is VSI num */
2088                 if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
2089                         return ICE_ERR_PARAM;
2090                 m_list_itr->fltr_info.src = hw_vsi_id;
2091                 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
2092                     IS_ZERO_ETHER_ADDR(add))
2093                         return ICE_ERR_PARAM;
2094                 if (IS_UNICAST_ETHER_ADDR(add) && !hw->ucast_shared) {
2095                         /* Don't overwrite the unicast address */
2096                         ice_acquire_lock(rule_lock);
2097                         if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
2098                                                 &m_list_itr->fltr_info)) {
2099                                 ice_release_lock(rule_lock);
2100                                 return ICE_ERR_ALREADY_EXISTS;
2101                         }
2102                         ice_release_lock(rule_lock);
2103                         num_unicast++;
2104                 } else if (IS_MULTICAST_ETHER_ADDR(add) ||
2105                            (IS_UNICAST_ETHER_ADDR(add) && hw->ucast_shared)) {
2106                         m_list_itr->status =
2107                                 ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
2108                                                       m_list_itr);
2109                         if (m_list_itr->status)
2110                                 return m_list_itr->status;
2111                 }
2112         }
2113
2114         ice_acquire_lock(rule_lock);
2115         /* Exit if no suitable entries were found for adding bulk switch rule */
2116         if (!num_unicast) {
2117                 status = ICE_SUCCESS;
2118                 goto ice_add_mac_exit;
2119         }
2120
2121         rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2122
2123         /* Allocate switch rule buffer for the bulk update for unicast */
2124         s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
2125         s_rule = (struct ice_aqc_sw_rules_elem *)
2126                 ice_calloc(hw, num_unicast, s_rule_size);
2127         if (!s_rule) {
2128                 status = ICE_ERR_NO_MEMORY;
2129                 goto ice_add_mac_exit;
2130         }
2131
2132         r_iter = s_rule;
2133         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2134                             list_entry) {
2135                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2136                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2137
2138                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2139                         ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
2140                                          ice_aqc_opc_add_sw_rules);
2141                         r_iter = (struct ice_aqc_sw_rules_elem *)
2142                                 ((u8 *)r_iter + s_rule_size);
2143                 }
2144         }
2145
2146         /* Call AQ bulk switch rule update for all unicast addresses */
2147         r_iter = s_rule;
2148         /* Call AQ switch rule in AQ_MAX chunk */
2149         for (total_elem_left = num_unicast; total_elem_left > 0;
2150              total_elem_left -= elem_sent) {
2151                 struct ice_aqc_sw_rules_elem *entry = r_iter;
2152
2153                 elem_sent = min(total_elem_left,
2154                                 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
2155                 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
2156                                          elem_sent, ice_aqc_opc_add_sw_rules,
2157                                          NULL);
2158                 if (status)
2159                         goto ice_add_mac_exit;
2160                 r_iter = (struct ice_aqc_sw_rules_elem *)
2161                         ((u8 *)r_iter + (elem_sent * s_rule_size));
2162         }
2163
2164         /* Fill up rule ID based on the value returned from FW */
2165         r_iter = s_rule;
2166         LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2167                             list_entry) {
2168                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2169                 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2170                 struct ice_fltr_mgmt_list_entry *fm_entry;
2171
2172                 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2173                         f_info->fltr_rule_id =
2174                                 LE16_TO_CPU(r_iter->pdata.lkup_tx_rx.index);
2175                         f_info->fltr_act = ICE_FWD_TO_VSI;
2176                         /* Create an entry to track this MAC address */
2177                         fm_entry = (struct ice_fltr_mgmt_list_entry *)
2178                                 ice_malloc(hw, sizeof(*fm_entry));
2179                         if (!fm_entry) {
2180                                 status = ICE_ERR_NO_MEMORY;
2181                                 goto ice_add_mac_exit;
2182                         }
2183                         fm_entry->fltr_info = *f_info;
2184                         fm_entry->vsi_count = 1;
2185                         /* The book keeping entries will get removed when
2186                          * base driver calls remove filter AQ command
2187                          */
2188
2189                         LIST_ADD(&fm_entry->list_entry, rule_head);
2190                         r_iter = (struct ice_aqc_sw_rules_elem *)
2191                                 ((u8 *)r_iter + s_rule_size);
2192                 }
2193         }
2194
2195 ice_add_mac_exit:
2196         ice_release_lock(rule_lock);
2197         if (s_rule)
2198                 ice_free(hw, s_rule);
2199         return status;
2200 }
2201
2202 /**
2203  * ice_add_vlan_internal - Add one VLAN based filter rule
2204  * @hw: pointer to the hardware structure
2205  * @f_entry: filter entry containing one VLAN information
2206  */
2207 static enum ice_status
2208 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
2209 {
2210         struct ice_switch_info *sw = hw->switch_info;
2211         struct ice_fltr_mgmt_list_entry *v_list_itr;
2212         struct ice_fltr_info *new_fltr, *cur_fltr;
2213         enum ice_sw_lkup_type lkup_type;
2214         u16 vsi_list_id = 0, vsi_handle;
2215         struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2216         enum ice_status status = ICE_SUCCESS;
2217
2218         if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2219                 return ICE_ERR_PARAM;
2220
2221         f_entry->fltr_info.fwd_id.hw_vsi_id =
2222                 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2223         new_fltr = &f_entry->fltr_info;
2224
2225         /* VLAN ID should only be 12 bits */
2226         if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
2227                 return ICE_ERR_PARAM;
2228
2229         if (new_fltr->src_id != ICE_SRC_ID_VSI)
2230                 return ICE_ERR_PARAM;
2231
2232         new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
2233         lkup_type = new_fltr->lkup_type;
2234         vsi_handle = new_fltr->vsi_handle;
2235         rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2236         ice_acquire_lock(rule_lock);
2237         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
2238         if (!v_list_itr) {
2239                 struct ice_vsi_list_map_info *map_info = NULL;
2240
2241                 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
2242                         /* All VLAN pruning rules use a VSI list. Check if
2243                          * there is already a VSI list containing VSI that we
2244                          * want to add. If found, use the same vsi_list_id for
2245                          * this new VLAN rule or else create a new list.
2246                          */
2247                         map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
2248                                                            vsi_handle,
2249                                                            &vsi_list_id);
2250                         if (!map_info) {
2251                                 status = ice_create_vsi_list_rule(hw,
2252                                                                   &vsi_handle,
2253                                                                   1,
2254                                                                   &vsi_list_id,
2255                                                                   lkup_type);
2256                                 if (status)
2257                                         goto exit;
2258                         }
2259                         /* Convert the action to forwarding to a VSI list. */
2260                         new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2261                         new_fltr->fwd_id.vsi_list_id = vsi_list_id;
2262                 }
2263
2264                 status = ice_create_pkt_fwd_rule(hw, f_entry);
2265                 if (!status) {
2266                         v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
2267                                                          new_fltr);
2268                         if (!v_list_itr) {
2269                                 status = ICE_ERR_DOES_NOT_EXIST;
2270                                 goto exit;
2271                         }
2272                         /* reuse VSI list for new rule and increment ref_cnt */
2273                         if (map_info) {
2274                                 v_list_itr->vsi_list_info = map_info;
2275                                 map_info->ref_cnt++;
2276                         } else {
2277                                 v_list_itr->vsi_list_info =
2278                                         ice_create_vsi_list_map(hw, &vsi_handle,
2279                                                                 1, vsi_list_id);
2280                         }
2281                 }
2282         } else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
2283                 /* Update existing VSI list to add new VSI ID only if it used
2284                  * by one VLAN rule.
2285                  */
2286                 cur_fltr = &v_list_itr->fltr_info;
2287                 status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
2288                                                  new_fltr);
2289         } else {
2290                 /* If VLAN rule exists and VSI list being used by this rule is
2291                  * referenced by more than 1 VLAN rule. Then create a new VSI
2292                  * list appending previous VSI with new VSI and update existing
2293                  * VLAN rule to point to new VSI list ID
2294                  */
2295                 struct ice_fltr_info tmp_fltr;
2296                 u16 vsi_handle_arr[2];
2297                 u16 cur_handle;
2298
2299                 /* Current implementation only supports reusing VSI list with
2300                  * one VSI count. We should never hit below condition
2301                  */
2302                 if (v_list_itr->vsi_count > 1 &&
2303                     v_list_itr->vsi_list_info->ref_cnt > 1) {
2304                         ice_debug(hw, ICE_DBG_SW,
2305                                   "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
2306                         status = ICE_ERR_CFG;
2307                         goto exit;
2308                 }
2309
2310                 cur_handle =
2311                         ice_find_first_bit(v_list_itr->vsi_list_info->vsi_map,
2312                                            ICE_MAX_VSI);
2313
2314                 /* A rule already exists with the new VSI being added */
2315                 if (cur_handle == vsi_handle) {
2316                         status = ICE_ERR_ALREADY_EXISTS;
2317                         goto exit;
2318                 }
2319
2320                 vsi_handle_arr[0] = cur_handle;
2321                 vsi_handle_arr[1] = vsi_handle;
2322                 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2323                                                   &vsi_list_id, lkup_type);
2324                 if (status)
2325                         goto exit;
2326
2327                 tmp_fltr = v_list_itr->fltr_info;
2328                 tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
2329                 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2330                 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2331                 /* Update the previous switch rule to a new VSI list which
2332                  * includes current VSI that is requested
2333                  */
2334                 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2335                 if (status)
2336                         goto exit;
2337
2338                 /* before overriding VSI list map info. decrement ref_cnt of
2339                  * previous VSI list
2340                  */
2341                 v_list_itr->vsi_list_info->ref_cnt--;
2342
2343                 /* now update to newly created list */
2344                 v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
2345                 v_list_itr->vsi_list_info =
2346                         ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2347                                                 vsi_list_id);
2348                 v_list_itr->vsi_count++;
2349         }
2350
2351 exit:
2352         ice_release_lock(rule_lock);
2353         return status;
2354 }
2355
2356 /**
2357  * ice_add_vlan - Add VLAN based filter rule
2358  * @hw: pointer to the hardware structure
2359  * @v_list: list of VLAN entries and forwarding information
2360  */
2361 enum ice_status
2362 ice_add_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2363 {
2364         struct ice_fltr_list_entry *v_list_itr;
2365
2366         if (!v_list || !hw)
2367                 return ICE_ERR_PARAM;
2368
2369         LIST_FOR_EACH_ENTRY(v_list_itr, v_list, ice_fltr_list_entry,
2370                             list_entry) {
2371                 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
2372                         return ICE_ERR_PARAM;
2373                 v_list_itr->fltr_info.flag = ICE_FLTR_TX;
2374                 v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
2375                 if (v_list_itr->status)
2376                         return v_list_itr->status;
2377         }
2378         return ICE_SUCCESS;
2379 }
2380
2381 #ifndef NO_MACVLAN_SUPPORT
2382 /**
2383  * ice_add_mac_vlan - Add MAC and VLAN pair based filter rule
2384  * @hw: pointer to the hardware structure
2385  * @mv_list: list of MAC and VLAN filters
2386  *
2387  * If the VSI on which the MAC-VLAN pair has to be added has Rx and Tx VLAN
2388  * pruning bits enabled, then it is the responsibility of the caller to make
2389  * sure to add a VLAN only filter on the same VSI. Packets belonging to that
2390  * VLAN won't be received on that VSI otherwise.
2391  */
2392 enum ice_status
2393 ice_add_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *mv_list)
2394 {
2395         struct ice_fltr_list_entry *mv_list_itr;
2396
2397         if (!mv_list || !hw)
2398                 return ICE_ERR_PARAM;
2399
2400         LIST_FOR_EACH_ENTRY(mv_list_itr, mv_list, ice_fltr_list_entry,
2401                             list_entry) {
2402                 enum ice_sw_lkup_type l_type =
2403                         mv_list_itr->fltr_info.lkup_type;
2404
2405                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
2406                         return ICE_ERR_PARAM;
2407                 mv_list_itr->fltr_info.flag = ICE_FLTR_TX;
2408                 mv_list_itr->status =
2409                         ice_add_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
2410                                               mv_list_itr);
2411                 if (mv_list_itr->status)
2412                         return mv_list_itr->status;
2413         }
2414         return ICE_SUCCESS;
2415 }
2416 #endif
2417
2418
2419
2420 /**
2421  * ice_rem_sw_rule_info
2422  * @hw: pointer to the hardware structure
2423  * @rule_head: pointer to the switch list structure that we want to delete
2424  */
2425 static void
2426 ice_rem_sw_rule_info(struct ice_hw *hw, struct LIST_HEAD_TYPE *rule_head)
2427 {
2428         if (!LIST_EMPTY(rule_head)) {
2429                 struct ice_fltr_mgmt_list_entry *entry;
2430                 struct ice_fltr_mgmt_list_entry *tmp;
2431
2432                 LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, rule_head,
2433                                          ice_fltr_mgmt_list_entry, list_entry) {
2434                         LIST_DEL(&entry->list_entry);
2435                         ice_free(hw, entry);
2436                 }
2437         }
2438 }
2439
2440
2441
2442 /**
2443  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
2444  * @pi: pointer to the port_info structure
2445  * @vsi_handle: VSI handle to set as default
2446  * @set: true to add the above mentioned switch rule, false to remove it
2447  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
2448  *
2449  * add filter rule to set/unset given VSI as default VSI for the switch
2450  * (represented by swid)
2451  */
2452 enum ice_status
2453 ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
2454                  u8 direction)
2455 {
2456         struct ice_aqc_sw_rules_elem *s_rule;
2457         struct ice_fltr_info f_info;
2458         struct ice_hw *hw = pi->hw;
2459         enum ice_adminq_opc opcode;
2460         enum ice_status status;
2461         u16 s_rule_size;
2462         u16 hw_vsi_id;
2463
2464         if (!ice_is_vsi_valid(hw, vsi_handle))
2465                 return ICE_ERR_PARAM;
2466         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2467
2468         s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
2469                             ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
2470         s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
2471         if (!s_rule)
2472                 return ICE_ERR_NO_MEMORY;
2473
2474         ice_memset(&f_info, 0, sizeof(f_info), ICE_NONDMA_MEM);
2475
2476         f_info.lkup_type = ICE_SW_LKUP_DFLT;
2477         f_info.flag = direction;
2478         f_info.fltr_act = ICE_FWD_TO_VSI;
2479         f_info.fwd_id.hw_vsi_id = hw_vsi_id;
2480
2481         if (f_info.flag & ICE_FLTR_RX) {
2482                 f_info.src = pi->lport;
2483                 f_info.src_id = ICE_SRC_ID_LPORT;
2484                 if (!set)
2485                         f_info.fltr_rule_id =
2486                                 pi->dflt_rx_vsi_rule_id;
2487         } else if (f_info.flag & ICE_FLTR_TX) {
2488                 f_info.src_id = ICE_SRC_ID_VSI;
2489                 f_info.src = hw_vsi_id;
2490                 if (!set)
2491                         f_info.fltr_rule_id =
2492                                 pi->dflt_tx_vsi_rule_id;
2493         }
2494
2495         if (set)
2496                 opcode = ice_aqc_opc_add_sw_rules;
2497         else
2498                 opcode = ice_aqc_opc_remove_sw_rules;
2499
2500         ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
2501
2502         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
2503         if (status || !(f_info.flag & ICE_FLTR_TX_RX))
2504                 goto out;
2505         if (set) {
2506                 u16 index = LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
2507
2508                 if (f_info.flag & ICE_FLTR_TX) {
2509                         pi->dflt_tx_vsi_num = hw_vsi_id;
2510                         pi->dflt_tx_vsi_rule_id = index;
2511                 } else if (f_info.flag & ICE_FLTR_RX) {
2512                         pi->dflt_rx_vsi_num = hw_vsi_id;
2513                         pi->dflt_rx_vsi_rule_id = index;
2514                 }
2515         } else {
2516                 if (f_info.flag & ICE_FLTR_TX) {
2517                         pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
2518                         pi->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
2519                 } else if (f_info.flag & ICE_FLTR_RX) {
2520                         pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
2521                         pi->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
2522                 }
2523         }
2524
2525 out:
2526         ice_free(hw, s_rule);
2527         return status;
2528 }
2529
2530 /**
2531  * ice_remove_mac - remove a MAC address based filter rule
2532  * @hw: pointer to the hardware structure
2533  * @m_list: list of MAC addresses and forwarding information
2534  *
2535  * This function removes either a MAC filter rule or a specific VSI from a
2536  * VSI list for a multicast MAC address.
2537  *
2538  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
2539  * ice_add_mac. Caller should be aware that this call will only work if all
2540  * the entries passed into m_list were added previously. It will not attempt to
2541  * do a partial remove of entries that were found.
2542  */
2543 enum ice_status
2544 ice_remove_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2545 {
2546         struct ice_fltr_list_entry *list_itr, *tmp;
2547
2548         if (!m_list)
2549                 return ICE_ERR_PARAM;
2550
2551         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, m_list, ice_fltr_list_entry,
2552                                  list_entry) {
2553                 enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2554
2555                 if (l_type != ICE_SW_LKUP_MAC)
2556                         return ICE_ERR_PARAM;
2557                 list_itr->status = ice_remove_rule_internal(hw,
2558                                                             ICE_SW_LKUP_MAC,
2559                                                             list_itr);
2560                 if (list_itr->status)
2561                         return list_itr->status;
2562         }
2563         return ICE_SUCCESS;
2564 }
2565
2566 /**
2567  * ice_remove_vlan - Remove VLAN based filter rule
2568  * @hw: pointer to the hardware structure
2569  * @v_list: list of VLAN entries and forwarding information
2570  */
2571 enum ice_status
2572 ice_remove_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2573 {
2574         struct ice_fltr_list_entry *v_list_itr, *tmp;
2575
2576         if (!v_list || !hw)
2577                 return ICE_ERR_PARAM;
2578
2579         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2580                                  list_entry) {
2581                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2582
2583                 if (l_type != ICE_SW_LKUP_VLAN)
2584                         return ICE_ERR_PARAM;
2585                 v_list_itr->status = ice_remove_rule_internal(hw,
2586                                                               ICE_SW_LKUP_VLAN,
2587                                                               v_list_itr);
2588                 if (v_list_itr->status)
2589                         return v_list_itr->status;
2590         }
2591         return ICE_SUCCESS;
2592 }
2593
2594 #ifndef NO_MACVLAN_SUPPORT
2595 /**
2596  * ice_remove_mac_vlan - Remove MAC VLAN based filter rule
2597  * @hw: pointer to the hardware structure
2598  * @v_list: list of MAC VLAN entries and forwarding information
2599  */
2600 enum ice_status
2601 ice_remove_mac_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2602 {
2603         struct ice_fltr_list_entry *v_list_itr, *tmp;
2604
2605         if (!v_list || !hw)
2606                 return ICE_ERR_PARAM;
2607
2608         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2609                                  list_entry) {
2610                 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2611
2612                 if (l_type != ICE_SW_LKUP_MAC_VLAN)
2613                         return ICE_ERR_PARAM;
2614                 v_list_itr->status =
2615                         ice_remove_rule_internal(hw, ICE_SW_LKUP_MAC_VLAN,
2616                                                  v_list_itr);
2617                 if (v_list_itr->status)
2618                         return v_list_itr->status;
2619         }
2620         return ICE_SUCCESS;
2621 }
2622 #endif /* !NO_MACVLAN_SUPPORT */
2623
2624 /**
2625  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
2626  * @fm_entry: filter entry to inspect
2627  * @vsi_handle: VSI handle to compare with filter info
2628  */
2629 static bool
2630 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
2631 {
2632         return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
2633                  fm_entry->fltr_info.vsi_handle == vsi_handle) ||
2634                 (fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
2635                  (ice_is_bit_set(fm_entry->vsi_list_info->vsi_map,
2636                                  vsi_handle))));
2637 }
2638
2639 /**
2640  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
2641  * @hw: pointer to the hardware structure
2642  * @vsi_handle: VSI handle to remove filters from
2643  * @vsi_list_head: pointer to the list to add entry to
2644  * @fi: pointer to fltr_info of filter entry to copy & add
2645  *
2646  * Helper function, used when creating a list of filters to remove from
2647  * a specific VSI. The entry added to vsi_list_head is a COPY of the
2648  * original filter entry, with the exception of fltr_info.fltr_act and
2649  * fltr_info.fwd_id fields. These are set such that later logic can
2650  * extract which VSI to remove the fltr from, and pass on that information.
2651  */
2652 static enum ice_status
2653 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2654                                struct LIST_HEAD_TYPE *vsi_list_head,
2655                                struct ice_fltr_info *fi)
2656 {
2657         struct ice_fltr_list_entry *tmp;
2658
2659         /* this memory is freed up in the caller function
2660          * once filters for this VSI are removed
2661          */
2662         tmp = (struct ice_fltr_list_entry *)ice_malloc(hw, sizeof(*tmp));
2663         if (!tmp)
2664                 return ICE_ERR_NO_MEMORY;
2665
2666         tmp->fltr_info = *fi;
2667
2668         /* Overwrite these fields to indicate which VSI to remove filter from,
2669          * so find and remove logic can extract the information from the
2670          * list entries. Note that original entries will still have proper
2671          * values.
2672          */
2673         tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2674         tmp->fltr_info.vsi_handle = vsi_handle;
2675         tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2676
2677         LIST_ADD(&tmp->list_entry, vsi_list_head);
2678
2679         return ICE_SUCCESS;
2680 }
2681
2682 /**
2683  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
2684  * @hw: pointer to the hardware structure
2685  * @vsi_handle: VSI handle to remove filters from
2686  * @lkup_list_head: pointer to the list that has certain lookup type filters
2687  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
2688  *
2689  * Locates all filters in lkup_list_head that are used by the given VSI,
2690  * and adds COPIES of those entries to vsi_list_head (intended to be used
2691  * to remove the listed filters).
2692  * Note that this means all entries in vsi_list_head must be explicitly
2693  * deallocated by the caller when done with list.
2694  */
2695 static enum ice_status
2696 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2697                          struct LIST_HEAD_TYPE *lkup_list_head,
2698                          struct LIST_HEAD_TYPE *vsi_list_head)
2699 {
2700         struct ice_fltr_mgmt_list_entry *fm_entry;
2701         enum ice_status status = ICE_SUCCESS;
2702
2703         /* check to make sure VSI ID is valid and within boundary */
2704         if (!ice_is_vsi_valid(hw, vsi_handle))
2705                 return ICE_ERR_PARAM;
2706
2707         LIST_FOR_EACH_ENTRY(fm_entry, lkup_list_head,
2708                             ice_fltr_mgmt_list_entry, list_entry) {
2709                 struct ice_fltr_info *fi;
2710
2711                 fi = &fm_entry->fltr_info;
2712                 if (!fi || !ice_vsi_uses_fltr(fm_entry, vsi_handle))
2713                         continue;
2714
2715                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2716                                                         vsi_list_head, fi);
2717                 if (status)
2718                         return status;
2719         }
2720         return status;
2721 }
2722
2723
2724 /**
2725  * ice_determine_promisc_mask
2726  * @fi: filter info to parse
2727  *
2728  * Helper function to determine which ICE_PROMISC_ mask corresponds
2729  * to given filter into.
2730  */
2731 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
2732 {
2733         u16 vid = fi->l_data.mac_vlan.vlan_id;
2734         u8 *macaddr = fi->l_data.mac.mac_addr;
2735         bool is_tx_fltr = false;
2736         u8 promisc_mask = 0;
2737
2738         if (fi->flag == ICE_FLTR_TX)
2739                 is_tx_fltr = true;
2740
2741         if (IS_BROADCAST_ETHER_ADDR(macaddr))
2742                 promisc_mask |= is_tx_fltr ?
2743                         ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
2744         else if (IS_MULTICAST_ETHER_ADDR(macaddr))
2745                 promisc_mask |= is_tx_fltr ?
2746                         ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
2747         else if (IS_UNICAST_ETHER_ADDR(macaddr))
2748                 promisc_mask |= is_tx_fltr ?
2749                         ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
2750         if (vid)
2751                 promisc_mask |= is_tx_fltr ?
2752                         ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
2753
2754         return promisc_mask;
2755 }
2756
2757
2758 /**
2759  * ice_remove_promisc - Remove promisc based filter rules
2760  * @hw: pointer to the hardware structure
2761  * @recp_id: recipe ID for which the rule needs to removed
2762  * @v_list: list of promisc entries
2763  */
2764 static enum ice_status
2765 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
2766                    struct LIST_HEAD_TYPE *v_list)
2767 {
2768         struct ice_fltr_list_entry *v_list_itr, *tmp;
2769
2770         LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2771                                  list_entry) {
2772                 v_list_itr->status =
2773                         ice_remove_rule_internal(hw, recp_id, v_list_itr);
2774                 if (v_list_itr->status)
2775                         return v_list_itr->status;
2776         }
2777         return ICE_SUCCESS;
2778 }
2779
2780 /**
2781  * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
2782  * @hw: pointer to the hardware structure
2783  * @vsi_handle: VSI handle to clear mode
2784  * @promisc_mask: mask of promiscuous config bits to clear
2785  * @vid: VLAN ID to clear VLAN promiscuous
2786  */
2787 enum ice_status
2788 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
2789                       u16 vid)
2790 {
2791         struct ice_switch_info *sw = hw->switch_info;
2792         struct ice_fltr_list_entry *fm_entry, *tmp;
2793         struct LIST_HEAD_TYPE remove_list_head;
2794         struct ice_fltr_mgmt_list_entry *itr;
2795         struct LIST_HEAD_TYPE *rule_head;
2796         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
2797         enum ice_status status = ICE_SUCCESS;
2798         u8 recipe_id;
2799
2800         if (!ice_is_vsi_valid(hw, vsi_handle))
2801                 return ICE_ERR_PARAM;
2802
2803         if (vid)
2804                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
2805         else
2806                 recipe_id = ICE_SW_LKUP_PROMISC;
2807
2808         rule_head = &sw->recp_list[recipe_id].filt_rules;
2809         rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
2810
2811         INIT_LIST_HEAD(&remove_list_head);
2812
2813         ice_acquire_lock(rule_lock);
2814         LIST_FOR_EACH_ENTRY(itr, rule_head,
2815                             ice_fltr_mgmt_list_entry, list_entry) {
2816                 u8 fltr_promisc_mask = 0;
2817
2818                 if (!ice_vsi_uses_fltr(itr, vsi_handle))
2819                         continue;
2820
2821                 fltr_promisc_mask |=
2822                         ice_determine_promisc_mask(&itr->fltr_info);
2823
2824                 /* Skip if filter is not completely specified by given mask */
2825                 if (fltr_promisc_mask & ~promisc_mask)
2826                         continue;
2827
2828                 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2829                                                         &remove_list_head,
2830                                                         &itr->fltr_info);
2831                 if (status) {
2832                         ice_release_lock(rule_lock);
2833                         goto free_fltr_list;
2834                 }
2835         }
2836         ice_release_lock(rule_lock);
2837
2838         status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
2839
2840 free_fltr_list:
2841         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
2842                                  ice_fltr_list_entry, list_entry) {
2843                 LIST_DEL(&fm_entry->list_entry);
2844                 ice_free(hw, fm_entry);
2845         }
2846
2847         return status;
2848 }
2849
2850 /**
2851  * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
2852  * @hw: pointer to the hardware structure
2853  * @vsi_handle: VSI handle to configure
2854  * @promisc_mask: mask of promiscuous config bits
2855  * @vid: VLAN ID to set VLAN promiscuous
2856  */
2857 enum ice_status
2858 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
2859 {
2860         enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
2861         struct ice_fltr_list_entry f_list_entry;
2862         struct ice_fltr_info new_fltr;
2863         enum ice_status status = ICE_SUCCESS;
2864         bool is_tx_fltr;
2865         u16 hw_vsi_id;
2866         int pkt_type;
2867         u8 recipe_id;
2868
2869         ice_debug(hw, ICE_DBG_TRACE, "ice_set_vsi_promisc\n");
2870
2871         if (!ice_is_vsi_valid(hw, vsi_handle))
2872                 return ICE_ERR_PARAM;
2873         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2874
2875         ice_memset(&new_fltr, 0, sizeof(new_fltr), ICE_NONDMA_MEM);
2876
2877         if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
2878                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
2879                 new_fltr.l_data.mac_vlan.vlan_id = vid;
2880                 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
2881         } else {
2882                 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
2883                 recipe_id = ICE_SW_LKUP_PROMISC;
2884         }
2885
2886         /* Separate filters must be set for each direction/packet type
2887          * combination, so we will loop over the mask value, store the
2888          * individual type, and clear it out in the input mask as it
2889          * is found.
2890          */
2891         while (promisc_mask) {
2892                 u8 *mac_addr;
2893
2894                 pkt_type = 0;
2895                 is_tx_fltr = false;
2896
2897                 if (promisc_mask & ICE_PROMISC_UCAST_RX) {
2898                         promisc_mask &= ~ICE_PROMISC_UCAST_RX;
2899                         pkt_type = UCAST_FLTR;
2900                 } else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
2901                         promisc_mask &= ~ICE_PROMISC_UCAST_TX;
2902                         pkt_type = UCAST_FLTR;
2903                         is_tx_fltr = true;
2904                 } else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
2905                         promisc_mask &= ~ICE_PROMISC_MCAST_RX;
2906                         pkt_type = MCAST_FLTR;
2907                 } else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
2908                         promisc_mask &= ~ICE_PROMISC_MCAST_TX;
2909                         pkt_type = MCAST_FLTR;
2910                         is_tx_fltr = true;
2911                 } else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
2912                         promisc_mask &= ~ICE_PROMISC_BCAST_RX;
2913                         pkt_type = BCAST_FLTR;
2914                 } else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
2915                         promisc_mask &= ~ICE_PROMISC_BCAST_TX;
2916                         pkt_type = BCAST_FLTR;
2917                         is_tx_fltr = true;
2918                 }
2919
2920                 /* Check for VLAN promiscuous flag */
2921                 if (promisc_mask & ICE_PROMISC_VLAN_RX) {
2922                         promisc_mask &= ~ICE_PROMISC_VLAN_RX;
2923                 } else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
2924                         promisc_mask &= ~ICE_PROMISC_VLAN_TX;
2925                         is_tx_fltr = true;
2926                 }
2927
2928                 /* Set filter DA based on packet type */
2929                 mac_addr = new_fltr.l_data.mac.mac_addr;
2930                 if (pkt_type == BCAST_FLTR) {
2931                         ice_memset(mac_addr, 0xff, ETH_ALEN, ICE_NONDMA_MEM);
2932                 } else if (pkt_type == MCAST_FLTR ||
2933                            pkt_type == UCAST_FLTR) {
2934                         /* Use the dummy ether header DA */
2935                         ice_memcpy(mac_addr, dummy_eth_header, ETH_ALEN,
2936                                    ICE_NONDMA_TO_NONDMA);
2937                         if (pkt_type == MCAST_FLTR)
2938                                 mac_addr[0] |= 0x1;     /* Set multicast bit */
2939                 }
2940
2941                 /* Need to reset this to zero for all iterations */
2942                 new_fltr.flag = 0;
2943                 if (is_tx_fltr) {
2944                         new_fltr.flag |= ICE_FLTR_TX;
2945                         new_fltr.src = hw_vsi_id;
2946                 } else {
2947                         new_fltr.flag |= ICE_FLTR_RX;
2948                         new_fltr.src = hw->port_info->lport;
2949                 }
2950
2951                 new_fltr.fltr_act = ICE_FWD_TO_VSI;
2952                 new_fltr.vsi_handle = vsi_handle;
2953                 new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
2954                 f_list_entry.fltr_info = new_fltr;
2955
2956                 status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
2957                 if (status != ICE_SUCCESS)
2958                         goto set_promisc_exit;
2959         }
2960
2961 set_promisc_exit:
2962         return status;
2963 }
2964
2965 /**
2966  * ice_set_vlan_vsi_promisc
2967  * @hw: pointer to the hardware structure
2968  * @vsi_handle: VSI handle to configure
2969  * @promisc_mask: mask of promiscuous config bits
2970  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
2971  *
2972  * Configure VSI with all associated VLANs to given promiscuous mode(s)
2973  */
2974 enum ice_status
2975 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
2976                          bool rm_vlan_promisc)
2977 {
2978         struct ice_switch_info *sw = hw->switch_info;
2979         struct ice_fltr_list_entry *list_itr, *tmp;
2980         struct LIST_HEAD_TYPE vsi_list_head;
2981         struct LIST_HEAD_TYPE *vlan_head;
2982         struct ice_lock *vlan_lock; /* Lock to protect filter rule list */
2983         enum ice_status status;
2984         u16 vlan_id;
2985
2986         INIT_LIST_HEAD(&vsi_list_head);
2987         vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2988         vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
2989         ice_acquire_lock(vlan_lock);
2990         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
2991                                           &vsi_list_head);
2992         ice_release_lock(vlan_lock);
2993         if (status)
2994                 goto free_fltr_list;
2995
2996         LIST_FOR_EACH_ENTRY(list_itr, &vsi_list_head, ice_fltr_list_entry,
2997                             list_entry) {
2998                 vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
2999                 if (rm_vlan_promisc)
3000                         status = ice_clear_vsi_promisc(hw, vsi_handle,
3001                                                        promisc_mask, vlan_id);
3002                 else
3003                         status = ice_set_vsi_promisc(hw, vsi_handle,
3004                                                      promisc_mask, vlan_id);
3005                 if (status)
3006                         break;
3007         }
3008
3009 free_fltr_list:
3010         LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, &vsi_list_head,
3011                                  ice_fltr_list_entry, list_entry) {
3012                 LIST_DEL(&list_itr->list_entry);
3013                 ice_free(hw, list_itr);
3014         }
3015         return status;
3016 }
3017
3018 /**
3019  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
3020  * @hw: pointer to the hardware structure
3021  * @vsi_handle: VSI handle to remove filters from
3022  * @lkup: switch rule filter lookup type
3023  */
3024 static void
3025 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
3026                          enum ice_sw_lkup_type lkup)
3027 {
3028         struct ice_switch_info *sw = hw->switch_info;
3029         struct ice_fltr_list_entry *fm_entry;
3030         struct LIST_HEAD_TYPE remove_list_head;
3031         struct LIST_HEAD_TYPE *rule_head;
3032         struct ice_fltr_list_entry *tmp;
3033         struct ice_lock *rule_lock;     /* Lock to protect filter rule list */
3034         enum ice_status status;
3035
3036         INIT_LIST_HEAD(&remove_list_head);
3037         rule_lock = &sw->recp_list[lkup].filt_rule_lock;
3038         rule_head = &sw->recp_list[lkup].filt_rules;
3039         ice_acquire_lock(rule_lock);
3040         status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
3041                                           &remove_list_head);
3042         ice_release_lock(rule_lock);
3043         if (status)
3044                 return;
3045
3046         switch (lkup) {
3047         case ICE_SW_LKUP_MAC:
3048                 ice_remove_mac(hw, &remove_list_head);
3049                 break;
3050         case ICE_SW_LKUP_VLAN:
3051                 ice_remove_vlan(hw, &remove_list_head);
3052                 break;
3053         case ICE_SW_LKUP_PROMISC:
3054         case ICE_SW_LKUP_PROMISC_VLAN:
3055                 ice_remove_promisc(hw, lkup, &remove_list_head);
3056                 break;
3057         case ICE_SW_LKUP_MAC_VLAN:
3058 #ifndef NO_MACVLAN_SUPPORT
3059                 ice_remove_mac_vlan(hw, &remove_list_head);
3060 #else
3061                 ice_debug(hw, ICE_DBG_SW, "MAC VLAN look up is not supported yet\n");
3062 #endif /* !NO_MACVLAN_SUPPORT */
3063                 break;
3064         case ICE_SW_LKUP_ETHERTYPE:
3065         case ICE_SW_LKUP_ETHERTYPE_MAC:
3066         case ICE_SW_LKUP_DFLT:
3067                 ice_debug(hw, ICE_DBG_SW,
3068                           "Remove filters for this lookup type hasn't been implemented yet\n");
3069                 break;
3070         case ICE_SW_LKUP_LAST:
3071                 ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type\n");
3072                 break;
3073         }
3074
3075         LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3076                                  ice_fltr_list_entry, list_entry) {
3077                 LIST_DEL(&fm_entry->list_entry);
3078                 ice_free(hw, fm_entry);
3079         }
3080 }
3081
3082 /**
3083  * ice_remove_vsi_fltr - Remove all filters for a VSI
3084  * @hw: pointer to the hardware structure
3085  * @vsi_handle: VSI handle to remove filters from
3086  */
3087 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
3088 {
3089         ice_debug(hw, ICE_DBG_TRACE, "ice_remove_vsi_fltr\n");
3090
3091         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
3092         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
3093         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
3094         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
3095         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
3096         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
3097         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
3098         ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
3099 }
3100
3101
3102
3103
3104
3105 /**
3106  * ice_replay_vsi_fltr - Replay filters for requested VSI
3107  * @hw: pointer to the hardware structure
3108  * @vsi_handle: driver VSI handle
3109  * @recp_id: Recipe ID for which rules need to be replayed
3110  * @list_head: list for which filters need to be replayed
3111  *
3112  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
3113  * It is required to pass valid VSI handle.
3114  */
3115 static enum ice_status
3116 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
3117                     struct LIST_HEAD_TYPE *list_head)
3118 {
3119         struct ice_fltr_mgmt_list_entry *itr;
3120         enum ice_status status = ICE_SUCCESS;
3121         u16 hw_vsi_id;
3122
3123         if (LIST_EMPTY(list_head))
3124                 return status;
3125         hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3126
3127         LIST_FOR_EACH_ENTRY(itr, list_head, ice_fltr_mgmt_list_entry,
3128                             list_entry) {
3129                 struct ice_fltr_list_entry f_entry;
3130
3131                 f_entry.fltr_info = itr->fltr_info;
3132                 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
3133                     itr->fltr_info.vsi_handle == vsi_handle) {
3134                         /* update the src in case it is VSI num */
3135                         if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
3136                                 f_entry.fltr_info.src = hw_vsi_id;
3137                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
3138                         if (status != ICE_SUCCESS)
3139                                 goto end;
3140                         continue;
3141                 }
3142                 if (!itr->vsi_list_info ||
3143                     !ice_is_bit_set(itr->vsi_list_info->vsi_map, vsi_handle))
3144                         continue;
3145                 /* Clearing it so that the logic can add it back */
3146                 ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
3147                 f_entry.fltr_info.vsi_handle = vsi_handle;
3148                 f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
3149                 /* update the src in case it is VSI num */
3150                 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
3151                         f_entry.fltr_info.src = hw_vsi_id;
3152                 if (recp_id == ICE_SW_LKUP_VLAN)
3153                         status = ice_add_vlan_internal(hw, &f_entry);
3154                 else
3155                         status = ice_add_rule_internal(hw, recp_id, &f_entry);
3156                 if (status != ICE_SUCCESS)
3157                         goto end;
3158         }
3159 end:
3160         return status;
3161 }
3162
3163
3164 /**
3165  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
3166  * @hw: pointer to the hardware structure
3167  * @vsi_handle: driver VSI handle
3168  *
3169  * Replays filters for requested VSI via vsi_handle.
3170  */
3171 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
3172 {
3173         struct ice_switch_info *sw = hw->switch_info;
3174         enum ice_status status = ICE_SUCCESS;
3175         u8 i;
3176
3177         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3178                 /* Update the default recipe lines and ones that were created */
3179                 if (i < ICE_MAX_NUM_RECIPES || sw->recp_list[i].recp_created) {
3180                         struct LIST_HEAD_TYPE *head;
3181
3182                         head = &sw->recp_list[i].filt_replay_rules;
3183                         if (!sw->recp_list[i].adv_rule)
3184                                 status = ice_replay_vsi_fltr(hw, vsi_handle, i,
3185                                                              head);
3186                         if (status != ICE_SUCCESS)
3187                                 return status;
3188                 }
3189         }
3190         return status;
3191 }
3192
3193 /**
3194  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
3195  * @hw: pointer to the HW struct
3196  *
3197  * Deletes the filter replay rules.
3198  */
3199 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
3200 {
3201         struct ice_switch_info *sw = hw->switch_info;
3202         u8 i;
3203
3204         if (!sw)
3205                 return;
3206
3207         for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3208                 if (!LIST_EMPTY(&sw->recp_list[i].filt_replay_rules)) {
3209                         struct LIST_HEAD_TYPE *l_head;
3210
3211                         l_head = &sw->recp_list[i].filt_replay_rules;
3212                         if (!sw->recp_list[i].adv_rule)
3213                                 ice_rem_sw_rule_info(hw, l_head);
3214                 }
3215         }
3216 }