GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / net / ethernet / intel / ice / ice_switch.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 #include "ice_switch.h"
5
6 #define ICE_ETH_DA_OFFSET               0
7 #define ICE_ETH_ETHTYPE_OFFSET          12
8 #define ICE_ETH_VLAN_TCI_OFFSET         14
9 #define ICE_MAX_VLAN_ID                 0xFFF
10
11 /* Dummy ethernet header needed in the ice_aqc_sw_rules_elem
12  * struct to configure any switch filter rules.
13  * {DA (6 bytes), SA(6 bytes),
14  * Ether type (2 bytes for header without VLAN tag) OR
15  * VLAN tag (4 bytes for header with VLAN tag) }
16  *
17  * Word on Hardcoded values
18  * byte 0 = 0x2: to identify it as locally administered DA MAC
19  * byte 6 = 0x2: to identify it as locally administered SA MAC
20  * byte 12 = 0x81 & byte 13 = 0x00:
21  *      In case of VLAN filter first two bytes defines ether type (0x8100)
22  *      and remaining two bytes are placeholder for programming a given VLAN id
23  *      In case of Ether type filter it is treated as header without VLAN tag
24  *      and byte 12 and 13 is used to program a given Ether type instead
25  */
26 #define DUMMY_ETH_HDR_LEN               16
27 static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
28                                                         0x2, 0, 0, 0, 0, 0,
29                                                         0x81, 0, 0, 0};
30
31 #define ICE_SW_RULE_RX_TX_ETH_HDR_SIZE \
32         (sizeof(struct ice_aqc_sw_rules_elem) - \
33          sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
34          sizeof(struct ice_sw_rule_lkup_rx_tx) + DUMMY_ETH_HDR_LEN - 1)
35 #define ICE_SW_RULE_RX_TX_NO_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) - 1)
39 #define ICE_SW_RULE_LG_ACT_SIZE(n) \
40         (sizeof(struct ice_aqc_sw_rules_elem) - \
41          sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
42          sizeof(struct ice_sw_rule_lg_act) - \
43          sizeof(((struct ice_sw_rule_lg_act *)0)->act) + \
44          ((n) * sizeof(((struct ice_sw_rule_lg_act *)0)->act)))
45 #define ICE_SW_RULE_VSI_LIST_SIZE(n) \
46         (sizeof(struct ice_aqc_sw_rules_elem) - \
47          sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
48          sizeof(struct ice_sw_rule_vsi_list) - \
49          sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi) + \
50          ((n) * sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi)))
51
52 /**
53  * ice_aq_alloc_free_res - command to allocate/free resources
54  * @hw: pointer to the hw struct
55  * @num_entries: number of resource entries in buffer
56  * @buf: Indirect buffer to hold data parameters and response
57  * @buf_size: size of buffer for indirect commands
58  * @opc: pass in the command opcode
59  * @cd: pointer to command details structure or NULL
60  *
61  * Helper function to allocate/free resources using the admin queue commands
62  */
63 static enum ice_status
64 ice_aq_alloc_free_res(struct ice_hw *hw, u16 num_entries,
65                       struct ice_aqc_alloc_free_res_elem *buf, u16 buf_size,
66                       enum ice_adminq_opc opc, struct ice_sq_cd *cd)
67 {
68         struct ice_aqc_alloc_free_res_cmd *cmd;
69         struct ice_aq_desc desc;
70
71         cmd = &desc.params.sw_res_ctrl;
72
73         if (!buf)
74                 return ICE_ERR_PARAM;
75
76         if (buf_size < (num_entries * sizeof(buf->elem[0])))
77                 return ICE_ERR_PARAM;
78
79         ice_fill_dflt_direct_cmd_desc(&desc, opc);
80
81         desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
82
83         cmd->num_entries = cpu_to_le16(num_entries);
84
85         return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
86 }
87
88 /**
89  * ice_aq_get_sw_cfg - get switch configuration
90  * @hw: pointer to the hardware structure
91  * @buf: pointer to the result buffer
92  * @buf_size: length of the buffer available for response
93  * @req_desc: pointer to requested descriptor
94  * @num_elems: pointer to number of elements
95  * @cd: pointer to command details structure or NULL
96  *
97  * Get switch configuration (0x0200) to be placed in 'buff'.
98  * This admin command returns information such as initial VSI/port number
99  * and switch ID it belongs to.
100  *
101  * NOTE: *req_desc is both an input/output parameter.
102  * The caller of this function first calls this function with *request_desc set
103  * to 0.  If the response from f/w has *req_desc set to 0, all the switch
104  * configuration information has been returned; if non-zero (meaning not all
105  * the information was returned), the caller should call this function again
106  * with *req_desc set to the previous value returned by f/w to get the
107  * next block of switch configuration information.
108  *
109  * *num_elems is output only parameter. This reflects the number of elements
110  * in response buffer. The caller of this function to use *num_elems while
111  * parsing the response buffer.
112  */
113 static enum ice_status
114 ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp *buf,
115                   u16 buf_size, u16 *req_desc, u16 *num_elems,
116                   struct ice_sq_cd *cd)
117 {
118         struct ice_aqc_get_sw_cfg *cmd;
119         enum ice_status status;
120         struct ice_aq_desc desc;
121
122         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
123         cmd = &desc.params.get_sw_conf;
124         cmd->element = cpu_to_le16(*req_desc);
125
126         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
127         if (!status) {
128                 *req_desc = le16_to_cpu(cmd->element);
129                 *num_elems = le16_to_cpu(cmd->num_elems);
130         }
131
132         return status;
133 }
134
135 /**
136  * ice_aq_add_vsi
137  * @hw: pointer to the hw struct
138  * @vsi_ctx: pointer to a VSI context struct
139  * @cd: pointer to command details structure or NULL
140  *
141  * Add a VSI context to the hardware (0x0210)
142  */
143 enum ice_status
144 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
145                struct ice_sq_cd *cd)
146 {
147         struct ice_aqc_add_update_free_vsi_resp *res;
148         struct ice_aqc_add_get_update_free_vsi *cmd;
149         enum ice_status status;
150         struct ice_aq_desc desc;
151
152         cmd = &desc.params.vsi_cmd;
153         res = (struct ice_aqc_add_update_free_vsi_resp *)&desc.params.raw;
154
155         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
156
157         if (!vsi_ctx->alloc_from_pool)
158                 cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num |
159                                            ICE_AQ_VSI_IS_VALID);
160
161         cmd->vsi_flags = cpu_to_le16(vsi_ctx->flags);
162
163         desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
164
165         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
166                                  sizeof(vsi_ctx->info), cd);
167
168         if (!status) {
169                 vsi_ctx->vsi_num = le16_to_cpu(res->vsi_num) & ICE_AQ_VSI_NUM_M;
170                 vsi_ctx->vsis_allocd = le16_to_cpu(res->vsi_used);
171                 vsi_ctx->vsis_unallocated = le16_to_cpu(res->vsi_free);
172         }
173
174         return status;
175 }
176
177 /**
178  * ice_aq_update_vsi
179  * @hw: pointer to the hw struct
180  * @vsi_ctx: pointer to a VSI context struct
181  * @cd: pointer to command details structure or NULL
182  *
183  * Update VSI context in the hardware (0x0211)
184  */
185 enum ice_status
186 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
187                   struct ice_sq_cd *cd)
188 {
189         struct ice_aqc_add_update_free_vsi_resp *resp;
190         struct ice_aqc_add_get_update_free_vsi *cmd;
191         struct ice_aq_desc desc;
192         enum ice_status status;
193
194         cmd = &desc.params.vsi_cmd;
195         resp = (struct ice_aqc_add_update_free_vsi_resp *)&desc.params.raw;
196
197         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
198
199         cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
200
201         desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
202
203         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
204                                  sizeof(vsi_ctx->info), cd);
205
206         if (!status) {
207                 vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
208                 vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
209         }
210
211         return status;
212 }
213
214 /**
215  * ice_aq_free_vsi
216  * @hw: pointer to the hw struct
217  * @vsi_ctx: pointer to a VSI context struct
218  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
219  * @cd: pointer to command details structure or NULL
220  *
221  * Get VSI context info from hardware (0x0213)
222  */
223 enum ice_status
224 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
225                 bool keep_vsi_alloc, struct ice_sq_cd *cd)
226 {
227         struct ice_aqc_add_update_free_vsi_resp *resp;
228         struct ice_aqc_add_get_update_free_vsi *cmd;
229         struct ice_aq_desc desc;
230         enum ice_status status;
231
232         cmd = &desc.params.vsi_cmd;
233         resp = (struct ice_aqc_add_update_free_vsi_resp *)&desc.params.raw;
234
235         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
236
237         cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
238         if (keep_vsi_alloc)
239                 cmd->cmd_flags = cpu_to_le16(ICE_AQ_VSI_KEEP_ALLOC);
240
241         status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
242         if (!status) {
243                 vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
244                 vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
245         }
246
247         return status;
248 }
249
250 /**
251  * ice_aq_alloc_free_vsi_list
252  * @hw: pointer to the hw struct
253  * @vsi_list_id: VSI list id returned or used for lookup
254  * @lkup_type: switch rule filter lookup type
255  * @opc: switch rules population command type - pass in the command opcode
256  *
257  * allocates or free a VSI list resource
258  */
259 static enum ice_status
260 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
261                            enum ice_sw_lkup_type lkup_type,
262                            enum ice_adminq_opc opc)
263 {
264         struct ice_aqc_alloc_free_res_elem *sw_buf;
265         struct ice_aqc_res_elem *vsi_ele;
266         enum ice_status status;
267         u16 buf_len;
268
269         buf_len = sizeof(*sw_buf);
270         sw_buf = devm_kzalloc(ice_hw_to_dev(hw), buf_len, GFP_KERNEL);
271         if (!sw_buf)
272                 return ICE_ERR_NO_MEMORY;
273         sw_buf->num_elems = cpu_to_le16(1);
274
275         if (lkup_type == ICE_SW_LKUP_MAC ||
276             lkup_type == ICE_SW_LKUP_MAC_VLAN ||
277             lkup_type == ICE_SW_LKUP_ETHERTYPE ||
278             lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
279             lkup_type == ICE_SW_LKUP_PROMISC ||
280             lkup_type == ICE_SW_LKUP_PROMISC_VLAN) {
281                 sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
282         } else if (lkup_type == ICE_SW_LKUP_VLAN) {
283                 sw_buf->res_type =
284                         cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
285         } else {
286                 status = ICE_ERR_PARAM;
287                 goto ice_aq_alloc_free_vsi_list_exit;
288         }
289
290         if (opc == ice_aqc_opc_free_res)
291                 sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
292
293         status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
294         if (status)
295                 goto ice_aq_alloc_free_vsi_list_exit;
296
297         if (opc == ice_aqc_opc_alloc_res) {
298                 vsi_ele = &sw_buf->elem[0];
299                 *vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp);
300         }
301
302 ice_aq_alloc_free_vsi_list_exit:
303         devm_kfree(ice_hw_to_dev(hw), sw_buf);
304         return status;
305 }
306
307 /**
308  * ice_aq_sw_rules - add/update/remove switch rules
309  * @hw: pointer to the hw struct
310  * @rule_list: pointer to switch rule population list
311  * @rule_list_sz: total size of the rule list in bytes
312  * @num_rules: number of switch rules in the rule_list
313  * @opc: switch rules population command type - pass in the command opcode
314  * @cd: pointer to command details structure or NULL
315  *
316  * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
317  */
318 static enum ice_status
319 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
320                 u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
321 {
322         struct ice_aq_desc desc;
323
324         if (opc != ice_aqc_opc_add_sw_rules &&
325             opc != ice_aqc_opc_update_sw_rules &&
326             opc != ice_aqc_opc_remove_sw_rules)
327                 return ICE_ERR_PARAM;
328
329         ice_fill_dflt_direct_cmd_desc(&desc, opc);
330
331         desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
332         desc.params.sw_rules.num_rules_fltr_entry_index =
333                 cpu_to_le16(num_rules);
334         return ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
335 }
336
337 /* ice_init_port_info - Initialize port_info with switch configuration data
338  * @pi: pointer to port_info
339  * @vsi_port_num: VSI number or port number
340  * @type: Type of switch element (port or VSI)
341  * @swid: switch ID of the switch the element is attached to
342  * @pf_vf_num: PF or VF number
343  * @is_vf: true if the element is a VF, false otherwise
344  */
345 static void
346 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
347                    u16 swid, u16 pf_vf_num, bool is_vf)
348 {
349         switch (type) {
350         case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
351                 pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
352                 pi->sw_id = swid;
353                 pi->pf_vf_num = pf_vf_num;
354                 pi->is_vf = is_vf;
355                 pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
356                 pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
357                 break;
358         default:
359                 ice_debug(pi->hw, ICE_DBG_SW,
360                           "incorrect VSI/port type received\n");
361                 break;
362         }
363 }
364
365 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
366  * @hw: pointer to the hardware structure
367  */
368 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
369 {
370         struct ice_aqc_get_sw_cfg_resp *rbuf;
371         enum ice_status status;
372         u16 req_desc = 0;
373         u16 num_elems;
374         u16 i;
375
376         rbuf = devm_kzalloc(ice_hw_to_dev(hw), ICE_SW_CFG_MAX_BUF_LEN,
377                             GFP_KERNEL);
378
379         if (!rbuf)
380                 return ICE_ERR_NO_MEMORY;
381
382         /* Multiple calls to ice_aq_get_sw_cfg may be required
383          * to get all the switch configuration information. The need
384          * for additional calls is indicated by ice_aq_get_sw_cfg
385          * writing a non-zero value in req_desc
386          */
387         do {
388                 status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
389                                            &req_desc, &num_elems, NULL);
390
391                 if (status)
392                         break;
393
394                 for (i = 0; i < num_elems; i++) {
395                         struct ice_aqc_get_sw_cfg_resp_elem *ele;
396                         u16 pf_vf_num, swid, vsi_port_num;
397                         bool is_vf = false;
398                         u8 type;
399
400                         ele = rbuf[i].elements;
401                         vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
402                                 ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
403
404                         pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
405                                 ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
406
407                         swid = le16_to_cpu(ele->swid);
408
409                         if (le16_to_cpu(ele->pf_vf_num) &
410                             ICE_AQC_GET_SW_CONF_RESP_IS_VF)
411                                 is_vf = true;
412
413                         type = le16_to_cpu(ele->vsi_port_num) >>
414                                 ICE_AQC_GET_SW_CONF_RESP_TYPE_S;
415
416                         if (type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
417                                 /* FW VSI is not needed. Just continue. */
418                                 continue;
419                         }
420
421                         ice_init_port_info(hw->port_info, vsi_port_num,
422                                            type, swid, pf_vf_num, is_vf);
423                 }
424         } while (req_desc && !status);
425
426         devm_kfree(ice_hw_to_dev(hw), (void *)rbuf);
427         return status;
428 }
429
430 /**
431  * ice_fill_sw_info - Helper function to populate lb_en and lan_en
432  * @hw: pointer to the hardware structure
433  * @f_info: filter info structure to fill/update
434  *
435  * This helper function populates the lb_en and lan_en elements of the provided
436  * ice_fltr_info struct using the switch's type and characteristics of the
437  * switch rule being configured.
438  */
439 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *f_info)
440 {
441         f_info->lb_en = false;
442         f_info->lan_en = false;
443         if ((f_info->flag & ICE_FLTR_TX) &&
444             (f_info->fltr_act == ICE_FWD_TO_VSI ||
445              f_info->fltr_act == ICE_FWD_TO_VSI_LIST ||
446              f_info->fltr_act == ICE_FWD_TO_Q ||
447              f_info->fltr_act == ICE_FWD_TO_QGRP)) {
448                 f_info->lb_en = true;
449                 if (!(hw->evb_veb && f_info->lkup_type == ICE_SW_LKUP_MAC &&
450                       is_unicast_ether_addr(f_info->l_data.mac.mac_addr)))
451                         f_info->lan_en = true;
452         }
453 }
454
455 /**
456  * ice_fill_sw_rule - Helper function to fill switch rule structure
457  * @hw: pointer to the hardware structure
458  * @f_info: entry containing packet forwarding information
459  * @s_rule: switch rule structure to be filled in based on mac_entry
460  * @opc: switch rules population command type - pass in the command opcode
461  */
462 static void
463 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
464                  struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
465 {
466         u16 vlan_id = ICE_MAX_VLAN_ID + 1;
467         u8 eth_hdr[DUMMY_ETH_HDR_LEN];
468         void *daddr = NULL;
469         u32 act = 0;
470         __be16 *off;
471         u8 q_rgn;
472
473         if (opc == ice_aqc_opc_remove_sw_rules) {
474                 s_rule->pdata.lkup_tx_rx.act = 0;
475                 s_rule->pdata.lkup_tx_rx.index =
476                         cpu_to_le16(f_info->fltr_rule_id);
477                 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
478                 return;
479         }
480
481         /* initialize the ether header with a dummy header */
482         memcpy(eth_hdr, dummy_eth_header, sizeof(dummy_eth_header));
483         ice_fill_sw_info(hw, f_info);
484
485         switch (f_info->fltr_act) {
486         case ICE_FWD_TO_VSI:
487                 act |= (f_info->fwd_id.vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
488                         ICE_SINGLE_ACT_VSI_ID_M;
489                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
490                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
491                                 ICE_SINGLE_ACT_VALID_BIT;
492                 break;
493         case ICE_FWD_TO_VSI_LIST:
494                 act |= ICE_SINGLE_ACT_VSI_LIST;
495                 act |= (f_info->fwd_id.vsi_list_id <<
496                         ICE_SINGLE_ACT_VSI_LIST_ID_S) &
497                         ICE_SINGLE_ACT_VSI_LIST_ID_M;
498                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
499                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
500                                 ICE_SINGLE_ACT_VALID_BIT;
501                 break;
502         case ICE_FWD_TO_Q:
503                 act |= ICE_SINGLE_ACT_TO_Q;
504                 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
505                         ICE_SINGLE_ACT_Q_INDEX_M;
506                 break;
507         case ICE_DROP_PACKET:
508                 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
509                         ICE_SINGLE_ACT_VALID_BIT;
510                 break;
511         case ICE_FWD_TO_QGRP:
512                 q_rgn = f_info->qgrp_size > 0 ?
513                         (u8)ilog2(f_info->qgrp_size) : 0;
514                 act |= ICE_SINGLE_ACT_TO_Q;
515                 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
516                         ICE_SINGLE_ACT_Q_INDEX_M;
517                 act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
518                         ICE_SINGLE_ACT_Q_REGION_M;
519                 break;
520         default:
521                 return;
522         }
523
524         if (f_info->lb_en)
525                 act |= ICE_SINGLE_ACT_LB_ENABLE;
526         if (f_info->lan_en)
527                 act |= ICE_SINGLE_ACT_LAN_ENABLE;
528
529         switch (f_info->lkup_type) {
530         case ICE_SW_LKUP_MAC:
531                 daddr = f_info->l_data.mac.mac_addr;
532                 break;
533         case ICE_SW_LKUP_VLAN:
534                 vlan_id = f_info->l_data.vlan.vlan_id;
535                 if (f_info->fltr_act == ICE_FWD_TO_VSI ||
536                     f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
537                         act |= ICE_SINGLE_ACT_PRUNE;
538                         act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
539                 }
540                 break;
541         case ICE_SW_LKUP_ETHERTYPE_MAC:
542                 daddr = f_info->l_data.ethertype_mac.mac_addr;
543                 /* fall-through */
544         case ICE_SW_LKUP_ETHERTYPE:
545                 off = (__be16 *)&eth_hdr[ICE_ETH_ETHTYPE_OFFSET];
546                 *off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
547                 break;
548         case ICE_SW_LKUP_MAC_VLAN:
549                 daddr = f_info->l_data.mac_vlan.mac_addr;
550                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
551                 break;
552         case ICE_SW_LKUP_PROMISC_VLAN:
553                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
554                 /* fall-through */
555         case ICE_SW_LKUP_PROMISC:
556                 daddr = f_info->l_data.mac_vlan.mac_addr;
557                 break;
558         default:
559                 break;
560         }
561
562         s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
563                 cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
564                 cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
565
566         /* Recipe set depending on lookup type */
567         s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(f_info->lkup_type);
568         s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(f_info->src);
569         s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);
570
571         if (daddr)
572                 ether_addr_copy(&eth_hdr[ICE_ETH_DA_OFFSET], daddr);
573
574         if (!(vlan_id > ICE_MAX_VLAN_ID)) {
575                 off = (__be16 *)&eth_hdr[ICE_ETH_VLAN_TCI_OFFSET];
576                 *off = cpu_to_be16(vlan_id);
577         }
578
579         /* Create the switch rule with the final dummy Ethernet header */
580         if (opc != ice_aqc_opc_update_sw_rules)
581                 s_rule->pdata.lkup_tx_rx.hdr_len = cpu_to_le16(sizeof(eth_hdr));
582
583         memcpy(s_rule->pdata.lkup_tx_rx.hdr, eth_hdr, sizeof(eth_hdr));
584 }
585
586 /**
587  * ice_add_marker_act
588  * @hw: pointer to the hardware structure
589  * @m_ent: the management entry for which sw marker needs to be added
590  * @sw_marker: sw marker to tag the Rx descriptor with
591  * @l_id: large action resource id
592  *
593  * Create a large action to hold software marker and update the switch rule
594  * entry pointed by m_ent with newly created large action
595  */
596 static enum ice_status
597 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
598                    u16 sw_marker, u16 l_id)
599 {
600         struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
601         /* For software marker we need 3 large actions
602          * 1. FWD action: FWD TO VSI or VSI LIST
603          * 2. GENERIC VALUE action to hold the profile id
604          * 3. GENERIC VALUE action to hold the software marker id
605          */
606         const u16 num_lg_acts = 3;
607         enum ice_status status;
608         u16 lg_act_size;
609         u16 rules_size;
610         u16 vsi_info;
611         u32 act;
612
613         if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
614                 return ICE_ERR_PARAM;
615
616         /* Create two back-to-back switch rules and submit them to the HW using
617          * one memory buffer:
618          *    1. Large Action
619          *    2. Look up tx rx
620          */
621         lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
622         rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
623         lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
624         if (!lg_act)
625                 return ICE_ERR_NO_MEMORY;
626
627         rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
628
629         /* Fill in the first switch rule i.e. large action */
630         lg_act->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
631         lg_act->pdata.lg_act.index = cpu_to_le16(l_id);
632         lg_act->pdata.lg_act.size = cpu_to_le16(num_lg_acts);
633
634         /* First action VSI forwarding or VSI list forwarding depending on how
635          * many VSIs
636          */
637         vsi_info = (m_ent->vsi_count > 1) ?
638                 m_ent->fltr_info.fwd_id.vsi_list_id :
639                 m_ent->fltr_info.fwd_id.vsi_id;
640
641         act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
642         act |= (vsi_info << ICE_LG_ACT_VSI_LIST_ID_S) &
643                 ICE_LG_ACT_VSI_LIST_ID_M;
644         if (m_ent->vsi_count > 1)
645                 act |= ICE_LG_ACT_VSI_LIST;
646         lg_act->pdata.lg_act.act[0] = cpu_to_le32(act);
647
648         /* Second action descriptor type */
649         act = ICE_LG_ACT_GENERIC;
650
651         act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
652         lg_act->pdata.lg_act.act[1] = cpu_to_le32(act);
653
654         act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
655                ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
656
657         /* Third action Marker value */
658         act |= ICE_LG_ACT_GENERIC;
659         act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
660                 ICE_LG_ACT_GENERIC_VALUE_M;
661
662         lg_act->pdata.lg_act.act[2] = cpu_to_le32(act);
663
664         /* call the fill switch rule to fill the lookup tx rx structure */
665         ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
666                          ice_aqc_opc_update_sw_rules);
667
668         /* Update the action to point to the large action id */
669         rx_tx->pdata.lkup_tx_rx.act =
670                 cpu_to_le32(ICE_SINGLE_ACT_PTR |
671                             ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
672                              ICE_SINGLE_ACT_PTR_VAL_M));
673
674         /* Use the filter rule id of the previously created rule with single
675          * act. Once the update happens, hardware will treat this as large
676          * action
677          */
678         rx_tx->pdata.lkup_tx_rx.index =
679                 cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
680
681         status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
682                                  ice_aqc_opc_update_sw_rules, NULL);
683         if (!status) {
684                 m_ent->lg_act_idx = l_id;
685                 m_ent->sw_marker_id = sw_marker;
686         }
687
688         devm_kfree(ice_hw_to_dev(hw), lg_act);
689         return status;
690 }
691
692 /**
693  * ice_create_vsi_list_map
694  * @hw: pointer to the hardware structure
695  * @vsi_array: array of VSIs to form a VSI list
696  * @num_vsi: num VSI in the array
697  * @vsi_list_id: VSI list id generated as part of allocate resource
698  *
699  * Helper function to create a new entry of VSI list id to VSI mapping
700  * using the given VSI list id
701  */
702 static struct ice_vsi_list_map_info *
703 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_array, u16 num_vsi,
704                         u16 vsi_list_id)
705 {
706         struct ice_switch_info *sw = hw->switch_info;
707         struct ice_vsi_list_map_info *v_map;
708         int i;
709
710         v_map = devm_kcalloc(ice_hw_to_dev(hw), 1, sizeof(*v_map), GFP_KERNEL);
711         if (!v_map)
712                 return NULL;
713
714         v_map->vsi_list_id = vsi_list_id;
715
716         for (i = 0; i < num_vsi; i++)
717                 set_bit(vsi_array[i], v_map->vsi_map);
718
719         list_add(&v_map->list_entry, &sw->vsi_list_map_head);
720         return v_map;
721 }
722
723 /**
724  * ice_update_vsi_list_rule
725  * @hw: pointer to the hardware structure
726  * @vsi_array: array of VSIs to form a VSI list
727  * @num_vsi: num VSI in the array
728  * @vsi_list_id: VSI list id generated as part of allocate resource
729  * @remove: Boolean value to indicate if this is a remove action
730  * @opc: switch rules population command type - pass in the command opcode
731  * @lkup_type: lookup type of the filter
732  *
733  * Call AQ command to add a new switch rule or update existing switch rule
734  * using the given VSI list id
735  */
736 static enum ice_status
737 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_array, u16 num_vsi,
738                          u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
739                          enum ice_sw_lkup_type lkup_type)
740 {
741         struct ice_aqc_sw_rules_elem *s_rule;
742         enum ice_status status;
743         u16 s_rule_size;
744         u16 type;
745         int i;
746
747         if (!num_vsi)
748                 return ICE_ERR_PARAM;
749
750         if (lkup_type == ICE_SW_LKUP_MAC ||
751             lkup_type == ICE_SW_LKUP_MAC_VLAN ||
752             lkup_type == ICE_SW_LKUP_ETHERTYPE ||
753             lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
754             lkup_type == ICE_SW_LKUP_PROMISC ||
755             lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
756                 type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
757                                 ICE_AQC_SW_RULES_T_VSI_LIST_SET;
758         else if (lkup_type == ICE_SW_LKUP_VLAN)
759                 type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
760                                 ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
761         else
762                 return ICE_ERR_PARAM;
763
764         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
765         s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
766         if (!s_rule)
767                 return ICE_ERR_NO_MEMORY;
768
769         for (i = 0; i < num_vsi; i++)
770                 s_rule->pdata.vsi_list.vsi[i] = cpu_to_le16(vsi_array[i]);
771
772         s_rule->type = cpu_to_le16(type);
773         s_rule->pdata.vsi_list.number_vsi = cpu_to_le16(num_vsi);
774         s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
775
776         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
777
778         devm_kfree(ice_hw_to_dev(hw), s_rule);
779         return status;
780 }
781
782 /**
783  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
784  * @hw: pointer to the hw struct
785  * @vsi_array: array of VSIs to form a VSI list
786  * @num_vsi: number of VSIs in the array
787  * @vsi_list_id: stores the ID of the VSI list to be created
788  * @lkup_type: switch rule filter's lookup type
789  */
790 static enum ice_status
791 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_array, u16 num_vsi,
792                          u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
793 {
794         enum ice_status status;
795         int i;
796
797         for (i = 0; i < num_vsi; i++)
798                 if (vsi_array[i] >= ICE_MAX_VSI)
799                         return ICE_ERR_OUT_OF_RANGE;
800
801         status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
802                                             ice_aqc_opc_alloc_res);
803         if (status)
804                 return status;
805
806         /* Update the newly created VSI list to include the specified VSIs */
807         return ice_update_vsi_list_rule(hw, vsi_array, num_vsi, *vsi_list_id,
808                                         false, ice_aqc_opc_add_sw_rules,
809                                         lkup_type);
810 }
811
812 /**
813  * ice_create_pkt_fwd_rule
814  * @hw: pointer to the hardware structure
815  * @f_entry: entry containing packet forwarding information
816  *
817  * Create switch rule with given filter information and add an entry
818  * to the corresponding filter management list to track this switch rule
819  * and VSI mapping
820  */
821 static enum ice_status
822 ice_create_pkt_fwd_rule(struct ice_hw *hw,
823                         struct ice_fltr_list_entry *f_entry)
824 {
825         struct ice_switch_info *sw = hw->switch_info;
826         struct ice_fltr_mgmt_list_entry *fm_entry;
827         struct ice_aqc_sw_rules_elem *s_rule;
828         enum ice_sw_lkup_type l_type;
829         enum ice_status status;
830
831         s_rule = devm_kzalloc(ice_hw_to_dev(hw),
832                               ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
833         if (!s_rule)
834                 return ICE_ERR_NO_MEMORY;
835         fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
836                                 GFP_KERNEL);
837         if (!fm_entry) {
838                 status = ICE_ERR_NO_MEMORY;
839                 goto ice_create_pkt_fwd_rule_exit;
840         }
841
842         fm_entry->fltr_info = f_entry->fltr_info;
843
844         /* Initialize all the fields for the management entry */
845         fm_entry->vsi_count = 1;
846         fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
847         fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
848         fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
849
850         ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
851                          ice_aqc_opc_add_sw_rules);
852
853         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
854                                  ice_aqc_opc_add_sw_rules, NULL);
855         if (status) {
856                 devm_kfree(ice_hw_to_dev(hw), fm_entry);
857                 goto ice_create_pkt_fwd_rule_exit;
858         }
859
860         f_entry->fltr_info.fltr_rule_id =
861                 le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
862         fm_entry->fltr_info.fltr_rule_id =
863                 le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
864
865         /* The book keeping entries will get removed when base driver
866          * calls remove filter AQ command
867          */
868         l_type = fm_entry->fltr_info.lkup_type;
869         if (l_type == ICE_SW_LKUP_MAC) {
870                 mutex_lock(&sw->mac_list_lock);
871                 list_add(&fm_entry->list_entry, &sw->mac_list_head);
872                 mutex_unlock(&sw->mac_list_lock);
873         } else if (l_type == ICE_SW_LKUP_VLAN) {
874                 mutex_lock(&sw->vlan_list_lock);
875                 list_add(&fm_entry->list_entry, &sw->vlan_list_head);
876                 mutex_unlock(&sw->vlan_list_lock);
877         } else if (l_type == ICE_SW_LKUP_ETHERTYPE ||
878                    l_type == ICE_SW_LKUP_ETHERTYPE_MAC) {
879                 mutex_lock(&sw->eth_m_list_lock);
880                 list_add(&fm_entry->list_entry, &sw->eth_m_list_head);
881                 mutex_unlock(&sw->eth_m_list_lock);
882         } else if (l_type == ICE_SW_LKUP_PROMISC ||
883                    l_type == ICE_SW_LKUP_PROMISC_VLAN) {
884                 mutex_lock(&sw->promisc_list_lock);
885                 list_add(&fm_entry->list_entry, &sw->promisc_list_head);
886                 mutex_unlock(&sw->promisc_list_lock);
887         } else if (fm_entry->fltr_info.lkup_type == ICE_SW_LKUP_MAC_VLAN) {
888                 mutex_lock(&sw->mac_vlan_list_lock);
889                 list_add(&fm_entry->list_entry, &sw->mac_vlan_list_head);
890                 mutex_unlock(&sw->mac_vlan_list_lock);
891         } else {
892                 status = ICE_ERR_NOT_IMPL;
893         }
894 ice_create_pkt_fwd_rule_exit:
895         devm_kfree(ice_hw_to_dev(hw), s_rule);
896         return status;
897 }
898
899 /**
900  * ice_update_pkt_fwd_rule
901  * @hw: pointer to the hardware structure
902  * @rule_id: rule of previously created switch rule to update
903  * @vsi_list_id: VSI list id to be updated with
904  * @f_info: ice_fltr_info to pull other information for switch rule
905  *
906  * Call AQ command to update a previously created switch rule with a
907  * VSI list id
908  */
909 static enum ice_status
910 ice_update_pkt_fwd_rule(struct ice_hw *hw, u16 rule_id, u16 vsi_list_id,
911                         struct ice_fltr_info f_info)
912 {
913         struct ice_aqc_sw_rules_elem *s_rule;
914         struct ice_fltr_info tmp_fltr;
915         enum ice_status status;
916
917         s_rule = devm_kzalloc(ice_hw_to_dev(hw),
918                               ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
919         if (!s_rule)
920                 return ICE_ERR_NO_MEMORY;
921
922         tmp_fltr = f_info;
923         tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
924         tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
925
926         ice_fill_sw_rule(hw, &tmp_fltr, s_rule,
927                          ice_aqc_opc_update_sw_rules);
928
929         s_rule->pdata.lkup_tx_rx.index = cpu_to_le16(rule_id);
930
931         /* Update switch rule with new rule set to forward VSI list */
932         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
933                                  ice_aqc_opc_update_sw_rules, NULL);
934
935         devm_kfree(ice_hw_to_dev(hw), s_rule);
936         return status;
937 }
938
939 /**
940  * ice_handle_vsi_list_mgmt
941  * @hw: pointer to the hardware structure
942  * @m_entry: pointer to current filter management list entry
943  * @cur_fltr: filter information from the book keeping entry
944  * @new_fltr: filter information with the new VSI to be added
945  *
946  * Call AQ command to add or update previously created VSI list with new VSI.
947  *
948  * Helper function to do book keeping associated with adding filter information
949  * The algorithm to do the booking keeping is described below :
950  * When a VSI needs to subscribe to a given filter( MAC/VLAN/Ethtype etc.)
951  *      if only one VSI has been added till now
952  *              Allocate a new VSI list and add two VSIs
953  *              to this list using switch rule command
954  *              Update the previously created switch rule with the
955  *              newly created VSI list id
956  *      if a VSI list was previously created
957  *              Add the new VSI to the previously created VSI list set
958  *              using the update switch rule command
959  */
960 static enum ice_status
961 ice_handle_vsi_list_mgmt(struct ice_hw *hw,
962                          struct ice_fltr_mgmt_list_entry *m_entry,
963                          struct ice_fltr_info *cur_fltr,
964                          struct ice_fltr_info *new_fltr)
965 {
966         enum ice_status status = 0;
967         u16 vsi_list_id = 0;
968
969         if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
970              cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
971                 return ICE_ERR_NOT_IMPL;
972
973         if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
974              new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
975             (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
976              cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
977                 return ICE_ERR_NOT_IMPL;
978
979         if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
980                 /* Only one entry existed in the mapping and it was not already
981                  * a part of a VSI list. So, create a VSI list with the old and
982                  * new VSIs.
983                  */
984                 u16 vsi_id_arr[2];
985                 u16 fltr_rule;
986
987                 /* A rule already exists with the new VSI being added */
988                 if (cur_fltr->fwd_id.vsi_id == new_fltr->fwd_id.vsi_id)
989                         return ICE_ERR_ALREADY_EXISTS;
990
991                 vsi_id_arr[0] = cur_fltr->fwd_id.vsi_id;
992                 vsi_id_arr[1] = new_fltr->fwd_id.vsi_id;
993                 status = ice_create_vsi_list_rule(hw, &vsi_id_arr[0], 2,
994                                                   &vsi_list_id,
995                                                   new_fltr->lkup_type);
996                 if (status)
997                         return status;
998
999                 fltr_rule = cur_fltr->fltr_rule_id;
1000                 /* Update the previous switch rule of "MAC forward to VSI" to
1001                  * "MAC fwd to VSI list"
1002                  */
1003                 status = ice_update_pkt_fwd_rule(hw, fltr_rule, vsi_list_id,
1004                                                  *new_fltr);
1005                 if (status)
1006                         return status;
1007
1008                 cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1009                 cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1010                 m_entry->vsi_list_info =
1011                         ice_create_vsi_list_map(hw, &vsi_id_arr[0], 2,
1012                                                 vsi_list_id);
1013
1014                 /* If this entry was large action then the large action needs
1015                  * to be updated to point to FWD to VSI list
1016                  */
1017                 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1018                         status =
1019                             ice_add_marker_act(hw, m_entry,
1020                                                m_entry->sw_marker_id,
1021                                                m_entry->lg_act_idx);
1022         } else {
1023                 u16 vsi_id = new_fltr->fwd_id.vsi_id;
1024                 enum ice_adminq_opc opcode;
1025
1026                 if (!m_entry->vsi_list_info)
1027                         return ICE_ERR_CFG;
1028
1029                 /* A rule already exists with the new VSI being added */
1030                 if (test_bit(vsi_id, m_entry->vsi_list_info->vsi_map))
1031                         return 0;
1032
1033                 /* Update the previously created VSI list set with
1034                  * the new VSI id passed in
1035                  */
1036                 vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1037                 opcode = ice_aqc_opc_update_sw_rules;
1038
1039                 status = ice_update_vsi_list_rule(hw, &vsi_id, 1, vsi_list_id,
1040                                                   false, opcode,
1041                                                   new_fltr->lkup_type);
1042                 /* update VSI list mapping info with new VSI id */
1043                 if (!status)
1044                         set_bit(vsi_id, m_entry->vsi_list_info->vsi_map);
1045         }
1046         if (!status)
1047                 m_entry->vsi_count++;
1048         return status;
1049 }
1050
1051 /**
1052  * ice_find_mac_entry
1053  * @hw: pointer to the hardware structure
1054  * @mac_addr: MAC address to search for
1055  *
1056  * Helper function to search for a MAC entry using a given MAC address
1057  * Returns pointer to the entry if found.
1058  */
1059 static struct ice_fltr_mgmt_list_entry *
1060 ice_find_mac_entry(struct ice_hw *hw, u8 *mac_addr)
1061 {
1062         struct ice_fltr_mgmt_list_entry *m_list_itr, *mac_ret = NULL;
1063         struct ice_switch_info *sw = hw->switch_info;
1064
1065         mutex_lock(&sw->mac_list_lock);
1066         list_for_each_entry(m_list_itr, &sw->mac_list_head, list_entry) {
1067                 u8 *buf = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
1068
1069                 if (ether_addr_equal(buf, mac_addr)) {
1070                         mac_ret = m_list_itr;
1071                         break;
1072                 }
1073         }
1074         mutex_unlock(&sw->mac_list_lock);
1075         return mac_ret;
1076 }
1077
1078 /**
1079  * ice_add_shared_mac - Add one MAC shared filter rule
1080  * @hw: pointer to the hardware structure
1081  * @f_entry: structure containing MAC forwarding information
1082  *
1083  * Adds or updates the book keeping list for the MAC addresses
1084  */
1085 static enum ice_status
1086 ice_add_shared_mac(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1087 {
1088         struct ice_fltr_info *new_fltr, *cur_fltr;
1089         struct ice_fltr_mgmt_list_entry *m_entry;
1090
1091         new_fltr = &f_entry->fltr_info;
1092
1093         m_entry = ice_find_mac_entry(hw, &new_fltr->l_data.mac.mac_addr[0]);
1094         if (!m_entry)
1095                 return ice_create_pkt_fwd_rule(hw, f_entry);
1096
1097         cur_fltr = &m_entry->fltr_info;
1098
1099         return ice_handle_vsi_list_mgmt(hw, m_entry, cur_fltr, new_fltr);
1100 }
1101
1102 /**
1103  * ice_add_mac - Add a MAC address based filter rule
1104  * @hw: pointer to the hardware structure
1105  * @m_list: list of MAC addresses and forwarding information
1106  *
1107  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
1108  * multiple unicast addresses, the function assumes that all the
1109  * addresses are unique in a given add_mac call. It doesn't
1110  * check for duplicates in this case, removing duplicates from a given
1111  * list should be taken care of in the caller of this function.
1112  */
1113 enum ice_status
1114 ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
1115 {
1116         struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
1117         struct ice_fltr_list_entry *m_list_itr;
1118         u16 elem_sent, total_elem_left;
1119         enum ice_status status = 0;
1120         u16 num_unicast = 0;
1121         u16 s_rule_size;
1122
1123         if (!m_list || !hw)
1124                 return ICE_ERR_PARAM;
1125
1126         list_for_each_entry(m_list_itr, m_list, list_entry) {
1127                 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
1128
1129                 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1130                         return ICE_ERR_PARAM;
1131                 if (is_zero_ether_addr(add))
1132                         return ICE_ERR_PARAM;
1133                 if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
1134                         /* Don't overwrite the unicast address */
1135                         if (ice_find_mac_entry(hw, add))
1136                                 return ICE_ERR_ALREADY_EXISTS;
1137                         num_unicast++;
1138                 } else if (is_multicast_ether_addr(add) ||
1139                            (is_unicast_ether_addr(add) && hw->ucast_shared)) {
1140                         status = ice_add_shared_mac(hw, m_list_itr);
1141                         if (status) {
1142                                 m_list_itr->status = ICE_FLTR_STATUS_FW_FAIL;
1143                                 return status;
1144                         }
1145                         m_list_itr->status = ICE_FLTR_STATUS_FW_SUCCESS;
1146                 }
1147         }
1148
1149         /* Exit if no suitable entries were found for adding bulk switch rule */
1150         if (!num_unicast)
1151                 return 0;
1152
1153         /* Allocate switch rule buffer for the bulk update for unicast */
1154         s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1155         s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
1156                               GFP_KERNEL);
1157         if (!s_rule)
1158                 return ICE_ERR_NO_MEMORY;
1159
1160         r_iter = s_rule;
1161         list_for_each_entry(m_list_itr, m_list, list_entry) {
1162                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1163                 u8 *addr = &f_info->l_data.mac.mac_addr[0];
1164
1165                 if (is_unicast_ether_addr(addr)) {
1166                         ice_fill_sw_rule(hw, &m_list_itr->fltr_info,
1167                                          r_iter, ice_aqc_opc_add_sw_rules);
1168                         r_iter = (struct ice_aqc_sw_rules_elem *)
1169                                 ((u8 *)r_iter + s_rule_size);
1170                 }
1171         }
1172
1173         /* Call AQ bulk switch rule update for all unicast addresses */
1174         r_iter = s_rule;
1175         /* Call AQ switch rule in AQ_MAX chunk */
1176         for (total_elem_left = num_unicast; total_elem_left > 0;
1177              total_elem_left -= elem_sent) {
1178                 struct ice_aqc_sw_rules_elem *entry = r_iter;
1179
1180                 elem_sent = min(total_elem_left,
1181                                 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
1182                 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
1183                                          elem_sent, ice_aqc_opc_add_sw_rules,
1184                                          NULL);
1185                 if (status)
1186                         goto ice_add_mac_exit;
1187                 r_iter = (struct ice_aqc_sw_rules_elem *)
1188                         ((u8 *)r_iter + (elem_sent * s_rule_size));
1189         }
1190
1191         /* Fill up rule id based on the value returned from FW */
1192         r_iter = s_rule;
1193         list_for_each_entry(m_list_itr, m_list, list_entry) {
1194                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1195                 u8 *addr = &f_info->l_data.mac.mac_addr[0];
1196                 struct ice_switch_info *sw = hw->switch_info;
1197                 struct ice_fltr_mgmt_list_entry *fm_entry;
1198
1199                 if (is_unicast_ether_addr(addr)) {
1200                         f_info->fltr_rule_id =
1201                                 le16_to_cpu(r_iter->pdata.lkup_tx_rx.index);
1202                         f_info->fltr_act = ICE_FWD_TO_VSI;
1203                         /* Create an entry to track this MAC address */
1204                         fm_entry = devm_kzalloc(ice_hw_to_dev(hw),
1205                                                 sizeof(*fm_entry), GFP_KERNEL);
1206                         if (!fm_entry) {
1207                                 status = ICE_ERR_NO_MEMORY;
1208                                 goto ice_add_mac_exit;
1209                         }
1210                         fm_entry->fltr_info = *f_info;
1211                         fm_entry->vsi_count = 1;
1212                         /* The book keeping entries will get removed when
1213                          * base driver calls remove filter AQ command
1214                          */
1215                         mutex_lock(&sw->mac_list_lock);
1216                         list_add(&fm_entry->list_entry, &sw->mac_list_head);
1217                         mutex_unlock(&sw->mac_list_lock);
1218
1219                         r_iter = (struct ice_aqc_sw_rules_elem *)
1220                                 ((u8 *)r_iter + s_rule_size);
1221                 }
1222         }
1223
1224 ice_add_mac_exit:
1225         devm_kfree(ice_hw_to_dev(hw), s_rule);
1226         return status;
1227 }
1228
1229 /**
1230  * ice_find_vlan_entry
1231  * @hw: pointer to the hardware structure
1232  * @vlan_id: VLAN id to search for
1233  *
1234  * Helper function to search for a VLAN entry using a given VLAN id
1235  * Returns pointer to the entry if found.
1236  */
1237 static struct ice_fltr_mgmt_list_entry *
1238 ice_find_vlan_entry(struct ice_hw *hw, u16 vlan_id)
1239 {
1240         struct ice_fltr_mgmt_list_entry *vlan_list_itr, *vlan_ret = NULL;
1241         struct ice_switch_info *sw = hw->switch_info;
1242
1243         mutex_lock(&sw->vlan_list_lock);
1244         list_for_each_entry(vlan_list_itr, &sw->vlan_list_head, list_entry)
1245                 if (vlan_list_itr->fltr_info.l_data.vlan.vlan_id == vlan_id) {
1246                         vlan_ret = vlan_list_itr;
1247                         break;
1248                 }
1249
1250         mutex_unlock(&sw->vlan_list_lock);
1251         return vlan_ret;
1252 }
1253
1254 /**
1255  * ice_add_vlan_internal - Add one VLAN based filter rule
1256  * @hw: pointer to the hardware structure
1257  * @f_entry: filter entry containing one VLAN information
1258  */
1259 static enum ice_status
1260 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1261 {
1262         struct ice_fltr_info *new_fltr, *cur_fltr;
1263         struct ice_fltr_mgmt_list_entry *v_list_itr;
1264         u16 vlan_id;
1265
1266         new_fltr = &f_entry->fltr_info;
1267         /* VLAN id should only be 12 bits */
1268         if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
1269                 return ICE_ERR_PARAM;
1270
1271         vlan_id = new_fltr->l_data.vlan.vlan_id;
1272         v_list_itr = ice_find_vlan_entry(hw, vlan_id);
1273         if (!v_list_itr) {
1274                 u16 vsi_id = ICE_VSI_INVAL_ID;
1275                 enum ice_status status;
1276                 u16 vsi_list_id = 0;
1277
1278                 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
1279                         enum ice_sw_lkup_type lkup_type = new_fltr->lkup_type;
1280
1281                         /* All VLAN pruning rules use a VSI list.
1282                          * Convert the action to forwarding to a VSI list.
1283                          */
1284                         vsi_id = new_fltr->fwd_id.vsi_id;
1285                         status = ice_create_vsi_list_rule(hw, &vsi_id, 1,
1286                                                           &vsi_list_id,
1287                                                           lkup_type);
1288                         if (status)
1289                                 return status;
1290                         new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1291                         new_fltr->fwd_id.vsi_list_id = vsi_list_id;
1292                 }
1293
1294                 status = ice_create_pkt_fwd_rule(hw, f_entry);
1295                 if (!status && vsi_id != ICE_VSI_INVAL_ID) {
1296                         v_list_itr = ice_find_vlan_entry(hw, vlan_id);
1297                         if (!v_list_itr)
1298                                 return ICE_ERR_DOES_NOT_EXIST;
1299                         v_list_itr->vsi_list_info =
1300                                 ice_create_vsi_list_map(hw, &vsi_id, 1,
1301                                                         vsi_list_id);
1302                 }
1303
1304                 return status;
1305         }
1306
1307         cur_fltr = &v_list_itr->fltr_info;
1308         return ice_handle_vsi_list_mgmt(hw, v_list_itr, cur_fltr, new_fltr);
1309 }
1310
1311 /**
1312  * ice_add_vlan - Add VLAN based filter rule
1313  * @hw: pointer to the hardware structure
1314  * @v_list: list of VLAN entries and forwarding information
1315  */
1316 enum ice_status
1317 ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
1318 {
1319         struct ice_fltr_list_entry *v_list_itr;
1320
1321         if (!v_list || !hw)
1322                 return ICE_ERR_PARAM;
1323
1324         list_for_each_entry(v_list_itr, v_list, list_entry) {
1325                 enum ice_status status;
1326
1327                 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
1328                         return ICE_ERR_PARAM;
1329
1330                 status = ice_add_vlan_internal(hw, v_list_itr);
1331                 if (status) {
1332                         v_list_itr->status = ICE_FLTR_STATUS_FW_FAIL;
1333                         return status;
1334                 }
1335                 v_list_itr->status = ICE_FLTR_STATUS_FW_SUCCESS;
1336         }
1337         return 0;
1338 }
1339
1340 /**
1341  * ice_remove_vsi_list_rule
1342  * @hw: pointer to the hardware structure
1343  * @vsi_list_id: VSI list id generated as part of allocate resource
1344  * @lkup_type: switch rule filter lookup type
1345  */
1346 static enum ice_status
1347 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1348                          enum ice_sw_lkup_type lkup_type)
1349 {
1350         struct ice_aqc_sw_rules_elem *s_rule;
1351         enum ice_status status;
1352         u16 s_rule_size;
1353
1354         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
1355         s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1356         if (!s_rule)
1357                 return ICE_ERR_NO_MEMORY;
1358
1359         s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
1360         s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
1361         /* FW expects number of VSIs in vsi_list resource to be 0 for clear
1362          * command. Since memory is zero'ed out during initialization, it's not
1363          * necessary to explicitly initialize the variable to 0.
1364          */
1365
1366         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1,
1367                                  ice_aqc_opc_remove_sw_rules, NULL);
1368         if (!status)
1369                 /* Free the vsi_list resource that we allocated */
1370                 status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
1371                                                     ice_aqc_opc_free_res);
1372
1373         devm_kfree(ice_hw_to_dev(hw), s_rule);
1374         return status;
1375 }
1376
1377 /**
1378  * ice_handle_rem_vsi_list_mgmt
1379  * @hw: pointer to the hardware structure
1380  * @vsi_id: ID of the VSI to remove
1381  * @fm_list_itr: filter management entry for which the VSI list management
1382  * needs to be done
1383  */
1384 static enum ice_status
1385 ice_handle_rem_vsi_list_mgmt(struct ice_hw *hw, u16 vsi_id,
1386                              struct ice_fltr_mgmt_list_entry *fm_list_itr)
1387 {
1388         struct ice_switch_info *sw = hw->switch_info;
1389         enum ice_status status = 0;
1390         enum ice_sw_lkup_type lkup_type;
1391         bool is_last_elem = true;
1392         bool conv_list = false;
1393         bool del_list = false;
1394         u16 vsi_list_id;
1395
1396         lkup_type = fm_list_itr->fltr_info.lkup_type;
1397         vsi_list_id = fm_list_itr->fltr_info.fwd_id.vsi_list_id;
1398
1399         if (fm_list_itr->vsi_count > 1) {
1400                 status = ice_update_vsi_list_rule(hw, &vsi_id, 1, vsi_list_id,
1401                                                   true,
1402                                                   ice_aqc_opc_update_sw_rules,
1403                                                   lkup_type);
1404                 if (status)
1405                         return status;
1406                 fm_list_itr->vsi_count--;
1407                 is_last_elem = false;
1408                 clear_bit(vsi_id, fm_list_itr->vsi_list_info->vsi_map);
1409         }
1410
1411         /* For non-VLAN rules that forward packets to a VSI list, convert them
1412          * to forwarding packets to a VSI if there is only one VSI left in the
1413          * list.  Unused lists are then removed.
1414          * VLAN rules need to use VSI lists even with only one VSI.
1415          */
1416         if (fm_list_itr->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST) {
1417                 if (lkup_type == ICE_SW_LKUP_VLAN) {
1418                         del_list = is_last_elem;
1419                 } else if (fm_list_itr->vsi_count == 1) {
1420                         conv_list = true;
1421                         del_list = true;
1422                 }
1423         }
1424
1425         if (del_list) {
1426                 /* Remove the VSI list since it is no longer used */
1427                 struct ice_vsi_list_map_info *vsi_list_info =
1428                         fm_list_itr->vsi_list_info;
1429
1430                 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
1431                 if (status)
1432                         return status;
1433
1434                 if (conv_list) {
1435                         u16 rem_vsi_id;
1436
1437                         rem_vsi_id = find_first_bit(vsi_list_info->vsi_map,
1438                                                     ICE_MAX_VSI);
1439
1440                         /* Error out when the expected last element is not in
1441                          * the VSI list map
1442                          */
1443                         if (rem_vsi_id == ICE_MAX_VSI)
1444                                 return ICE_ERR_OUT_OF_RANGE;
1445
1446                         /* Change the list entry action from VSI_LIST to VSI */
1447                         fm_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1448                         fm_list_itr->fltr_info.fwd_id.vsi_id = rem_vsi_id;
1449                 }
1450
1451                 list_del(&vsi_list_info->list_entry);
1452                 devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
1453                 fm_list_itr->vsi_list_info = NULL;
1454         }
1455
1456         if (conv_list) {
1457                 /* Convert the rule's forward action to forwarding packets to
1458                  * a VSI
1459                  */
1460                 struct ice_aqc_sw_rules_elem *s_rule;
1461
1462                 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1463                                       ICE_SW_RULE_RX_TX_ETH_HDR_SIZE,
1464                                       GFP_KERNEL);
1465                 if (!s_rule)
1466                         return ICE_ERR_NO_MEMORY;
1467
1468                 ice_fill_sw_rule(hw, &fm_list_itr->fltr_info, s_rule,
1469                                  ice_aqc_opc_update_sw_rules);
1470
1471                 s_rule->pdata.lkup_tx_rx.index =
1472                         cpu_to_le16(fm_list_itr->fltr_info.fltr_rule_id);
1473
1474                 status = ice_aq_sw_rules(hw, s_rule,
1475                                          ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1476                                          ice_aqc_opc_update_sw_rules, NULL);
1477                 devm_kfree(ice_hw_to_dev(hw), s_rule);
1478                 if (status)
1479                         return status;
1480         }
1481
1482         if (is_last_elem) {
1483                 /* Remove the lookup rule */
1484                 struct ice_aqc_sw_rules_elem *s_rule;
1485
1486                 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1487                                       ICE_SW_RULE_RX_TX_NO_HDR_SIZE,
1488                                       GFP_KERNEL);
1489                 if (!s_rule)
1490                         return ICE_ERR_NO_MEMORY;
1491
1492                 ice_fill_sw_rule(hw, &fm_list_itr->fltr_info, s_rule,
1493                                  ice_aqc_opc_remove_sw_rules);
1494
1495                 status = ice_aq_sw_rules(hw, s_rule,
1496                                          ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
1497                                          ice_aqc_opc_remove_sw_rules, NULL);
1498                 if (status)
1499                         return status;
1500
1501                 /* Remove a book keeping entry from the MAC address list */
1502                 mutex_lock(&sw->mac_list_lock);
1503                 list_del(&fm_list_itr->list_entry);
1504                 mutex_unlock(&sw->mac_list_lock);
1505                 devm_kfree(ice_hw_to_dev(hw), fm_list_itr);
1506                 devm_kfree(ice_hw_to_dev(hw), s_rule);
1507         }
1508         return status;
1509 }
1510
1511 /**
1512  * ice_remove_mac_entry
1513  * @hw: pointer to the hardware structure
1514  * @f_entry: structure containing MAC forwarding information
1515  */
1516 static enum ice_status
1517 ice_remove_mac_entry(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1518 {
1519         struct ice_fltr_mgmt_list_entry *m_entry;
1520         u16 vsi_id;
1521         u8 *add;
1522
1523         add = &f_entry->fltr_info.l_data.mac.mac_addr[0];
1524
1525         m_entry = ice_find_mac_entry(hw, add);
1526         if (!m_entry)
1527                 return ICE_ERR_PARAM;
1528
1529         vsi_id = f_entry->fltr_info.fwd_id.vsi_id;
1530         return ice_handle_rem_vsi_list_mgmt(hw, vsi_id, m_entry);
1531 }
1532
1533 /**
1534  * ice_remove_mac - remove a MAC address based filter rule
1535  * @hw: pointer to the hardware structure
1536  * @m_list: list of MAC addresses and forwarding information
1537  *
1538  * This function removes either a MAC filter rule or a specific VSI from a
1539  * VSI list for a multicast MAC address.
1540  *
1541  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
1542  * ice_add_mac. Caller should be aware that this call will only work if all
1543  * the entries passed into m_list were added previously. It will not attempt to
1544  * do a partial remove of entries that were found.
1545  */
1546 enum ice_status
1547 ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
1548 {
1549         struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
1550         u8 s_rule_size = ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
1551         struct ice_switch_info *sw = hw->switch_info;
1552         struct ice_fltr_mgmt_list_entry *m_entry;
1553         struct ice_fltr_list_entry *m_list_itr;
1554         u16 elem_sent, total_elem_left;
1555         enum ice_status status = 0;
1556         u16 num_unicast = 0;
1557
1558         if (!m_list)
1559                 return ICE_ERR_PARAM;
1560
1561         list_for_each_entry(m_list_itr, m_list, list_entry) {
1562                 u8 *addr = m_list_itr->fltr_info.l_data.mac.mac_addr;
1563
1564                 if (is_unicast_ether_addr(addr) && !hw->ucast_shared)
1565                         num_unicast++;
1566                 else if (is_multicast_ether_addr(addr) ||
1567                          (is_unicast_ether_addr(addr) && hw->ucast_shared))
1568                         ice_remove_mac_entry(hw, m_list_itr);
1569         }
1570
1571         /* Exit if no unicast addresses found. Multicast switch rules
1572          * were added individually
1573          */
1574         if (!num_unicast)
1575                 return 0;
1576
1577         /* Allocate switch rule buffer for the bulk update for unicast */
1578         s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
1579                               GFP_KERNEL);
1580         if (!s_rule)
1581                 return ICE_ERR_NO_MEMORY;
1582
1583         r_iter = s_rule;
1584         list_for_each_entry(m_list_itr, m_list, list_entry) {
1585                 u8 *addr = m_list_itr->fltr_info.l_data.mac.mac_addr;
1586
1587                 if (is_unicast_ether_addr(addr)) {
1588                         m_entry = ice_find_mac_entry(hw, addr);
1589                         if (!m_entry) {
1590                                 status = ICE_ERR_DOES_NOT_EXIST;
1591                                 goto ice_remove_mac_exit;
1592                         }
1593
1594                         ice_fill_sw_rule(hw, &m_entry->fltr_info,
1595                                          r_iter, ice_aqc_opc_remove_sw_rules);
1596                         r_iter = (struct ice_aqc_sw_rules_elem *)
1597                                 ((u8 *)r_iter + s_rule_size);
1598                 }
1599         }
1600
1601         /* Call AQ bulk switch rule update for all unicast addresses */
1602         r_iter = s_rule;
1603         /* Call AQ switch rule in AQ_MAX chunk */
1604         for (total_elem_left = num_unicast; total_elem_left > 0;
1605              total_elem_left -= elem_sent) {
1606                 struct ice_aqc_sw_rules_elem *entry = r_iter;
1607
1608                 elem_sent = min(total_elem_left,
1609                                 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
1610                 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
1611                                          elem_sent, ice_aqc_opc_remove_sw_rules,
1612                                          NULL);
1613                 if (status)
1614                         break;
1615                 r_iter = (struct ice_aqc_sw_rules_elem *)
1616                         ((u8 *)r_iter + s_rule_size);
1617         }
1618
1619         list_for_each_entry(m_list_itr, m_list, list_entry) {
1620                 u8 *addr = m_list_itr->fltr_info.l_data.mac.mac_addr;
1621
1622                 if (is_unicast_ether_addr(addr)) {
1623                         m_entry = ice_find_mac_entry(hw, addr);
1624                         if (!m_entry)
1625                                 return ICE_ERR_OUT_OF_RANGE;
1626                         mutex_lock(&sw->mac_list_lock);
1627                         list_del(&m_entry->list_entry);
1628                         mutex_unlock(&sw->mac_list_lock);
1629                         devm_kfree(ice_hw_to_dev(hw), m_entry);
1630                 }
1631         }
1632
1633 ice_remove_mac_exit:
1634         devm_kfree(ice_hw_to_dev(hw), s_rule);
1635         return status;
1636 }
1637
1638 /**
1639  * ice_cfg_dflt_vsi - add filter rule to set/unset given VSI as default
1640  * VSI for the switch (represented by swid)
1641  * @hw: pointer to the hardware structure
1642  * @vsi_id: number of VSI to set as default
1643  * @set: true to add the above mentioned switch rule, false to remove it
1644  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
1645  */
1646 enum ice_status
1647 ice_cfg_dflt_vsi(struct ice_hw *hw, u16 vsi_id, bool set, u8 direction)
1648 {
1649         struct ice_aqc_sw_rules_elem *s_rule;
1650         struct ice_fltr_info f_info;
1651         enum ice_adminq_opc opcode;
1652         enum ice_status status;
1653         u16 s_rule_size;
1654
1655         s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
1656                             ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
1657         s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1658         if (!s_rule)
1659                 return ICE_ERR_NO_MEMORY;
1660
1661         memset(&f_info, 0, sizeof(f_info));
1662
1663         f_info.lkup_type = ICE_SW_LKUP_DFLT;
1664         f_info.flag = direction;
1665         f_info.fltr_act = ICE_FWD_TO_VSI;
1666         f_info.fwd_id.vsi_id = vsi_id;
1667
1668         if (f_info.flag & ICE_FLTR_RX) {
1669                 f_info.src = hw->port_info->lport;
1670                 if (!set)
1671                         f_info.fltr_rule_id =
1672                                 hw->port_info->dflt_rx_vsi_rule_id;
1673         } else if (f_info.flag & ICE_FLTR_TX) {
1674                 f_info.src = vsi_id;
1675                 if (!set)
1676                         f_info.fltr_rule_id =
1677                                 hw->port_info->dflt_tx_vsi_rule_id;
1678         }
1679
1680         if (set)
1681                 opcode = ice_aqc_opc_add_sw_rules;
1682         else
1683                 opcode = ice_aqc_opc_remove_sw_rules;
1684
1685         ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
1686
1687         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
1688         if (status || !(f_info.flag & ICE_FLTR_TX_RX))
1689                 goto out;
1690         if (set) {
1691                 u16 index = le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1692
1693                 if (f_info.flag & ICE_FLTR_TX) {
1694                         hw->port_info->dflt_tx_vsi_num = vsi_id;
1695                         hw->port_info->dflt_tx_vsi_rule_id = index;
1696                 } else if (f_info.flag & ICE_FLTR_RX) {
1697                         hw->port_info->dflt_rx_vsi_num = vsi_id;
1698                         hw->port_info->dflt_rx_vsi_rule_id = index;
1699                 }
1700         } else {
1701                 if (f_info.flag & ICE_FLTR_TX) {
1702                         hw->port_info->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
1703                         hw->port_info->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
1704                 } else if (f_info.flag & ICE_FLTR_RX) {
1705                         hw->port_info->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
1706                         hw->port_info->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
1707                 }
1708         }
1709
1710 out:
1711         devm_kfree(ice_hw_to_dev(hw), s_rule);
1712         return status;
1713 }
1714
1715 /**
1716  * ice_remove_vlan_internal - Remove one VLAN based filter rule
1717  * @hw: pointer to the hardware structure
1718  * @f_entry: filter entry containing one VLAN information
1719  */
1720 static enum ice_status
1721 ice_remove_vlan_internal(struct ice_hw *hw,
1722                          struct ice_fltr_list_entry *f_entry)
1723 {
1724         struct ice_fltr_info *new_fltr;
1725         struct ice_fltr_mgmt_list_entry *v_list_elem;
1726         u16 vsi_id;
1727
1728         new_fltr = &f_entry->fltr_info;
1729
1730         v_list_elem = ice_find_vlan_entry(hw, new_fltr->l_data.vlan.vlan_id);
1731         if (!v_list_elem)
1732                 return ICE_ERR_PARAM;
1733
1734         vsi_id = f_entry->fltr_info.fwd_id.vsi_id;
1735         return ice_handle_rem_vsi_list_mgmt(hw, vsi_id, v_list_elem);
1736 }
1737
1738 /**
1739  * ice_remove_vlan - Remove VLAN based filter rule
1740  * @hw: pointer to the hardware structure
1741  * @v_list: list of VLAN entries and forwarding information
1742  */
1743 enum ice_status
1744 ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
1745 {
1746         struct ice_fltr_list_entry *v_list_itr;
1747         enum ice_status status = 0;
1748
1749         if (!v_list || !hw)
1750                 return ICE_ERR_PARAM;
1751
1752         list_for_each_entry(v_list_itr, v_list, list_entry) {
1753                 status = ice_remove_vlan_internal(hw, v_list_itr);
1754                 if (status) {
1755                         v_list_itr->status = ICE_FLTR_STATUS_FW_FAIL;
1756                         return status;
1757                 }
1758                 v_list_itr->status = ICE_FLTR_STATUS_FW_SUCCESS;
1759         }
1760         return status;
1761 }
1762
1763 /**
1764  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
1765  * @hw: pointer to the hardware structure
1766  * @vsi_id: ID of VSI to remove filters from
1767  * @lkup_list_head: pointer to the list that has certain lookup type filters
1768  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_id
1769  */
1770 static enum ice_status
1771 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_id,
1772                          struct list_head *lkup_list_head,
1773                          struct list_head *vsi_list_head)
1774 {
1775         struct ice_fltr_mgmt_list_entry *fm_entry;
1776
1777         /* check to make sure VSI id is valid and within boundary */
1778         if (vsi_id >=
1779             (sizeof(fm_entry->vsi_list_info->vsi_map) * BITS_PER_BYTE - 1))
1780                 return ICE_ERR_PARAM;
1781
1782         list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
1783                 struct ice_fltr_info *fi;
1784
1785                 fi = &fm_entry->fltr_info;
1786                 if ((fi->fltr_act == ICE_FWD_TO_VSI &&
1787                      fi->fwd_id.vsi_id == vsi_id) ||
1788                     (fi->fltr_act == ICE_FWD_TO_VSI_LIST &&
1789                      (test_bit(vsi_id, fm_entry->vsi_list_info->vsi_map)))) {
1790                         struct ice_fltr_list_entry *tmp;
1791
1792                         /* this memory is freed up in the caller function
1793                          * ice_remove_vsi_lkup_fltr() once filters for
1794                          * this VSI are removed
1795                          */
1796                         tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp),
1797                                            GFP_KERNEL);
1798                         if (!tmp)
1799                                 return ICE_ERR_NO_MEMORY;
1800
1801                         memcpy(&tmp->fltr_info, fi, sizeof(*fi));
1802
1803                         /* Expected below fields to be set to ICE_FWD_TO_VSI and
1804                          * the particular VSI id since we are only removing this
1805                          * one VSI
1806                          */
1807                         if (fi->fltr_act == ICE_FWD_TO_VSI_LIST) {
1808                                 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1809                                 tmp->fltr_info.fwd_id.vsi_id = vsi_id;
1810                         }
1811
1812                         list_add(&tmp->list_entry, vsi_list_head);
1813                 }
1814         }
1815         return 0;
1816 }
1817
1818 /**
1819  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
1820  * @hw: pointer to the hardware structure
1821  * @vsi_id: ID of VSI to remove filters from
1822  * @lkup: switch rule filter lookup type
1823  */
1824 static void
1825 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_id,
1826                          enum ice_sw_lkup_type lkup)
1827 {
1828         struct ice_switch_info *sw = hw->switch_info;
1829         struct ice_fltr_list_entry *fm_entry;
1830         struct list_head remove_list_head;
1831         struct ice_fltr_list_entry *tmp;
1832         enum ice_status status;
1833
1834         INIT_LIST_HEAD(&remove_list_head);
1835         switch (lkup) {
1836         case ICE_SW_LKUP_MAC:
1837                 mutex_lock(&sw->mac_list_lock);
1838                 status = ice_add_to_vsi_fltr_list(hw, vsi_id,
1839                                                   &sw->mac_list_head,
1840                                                   &remove_list_head);
1841                 mutex_unlock(&sw->mac_list_lock);
1842                 if (!status) {
1843                         ice_remove_mac(hw, &remove_list_head);
1844                         goto free_fltr_list;
1845                 }
1846                 break;
1847         case ICE_SW_LKUP_VLAN:
1848                 mutex_lock(&sw->vlan_list_lock);
1849                 status = ice_add_to_vsi_fltr_list(hw, vsi_id,
1850                                                   &sw->vlan_list_head,
1851                                                   &remove_list_head);
1852                 mutex_unlock(&sw->vlan_list_lock);
1853                 if (!status) {
1854                         ice_remove_vlan(hw, &remove_list_head);
1855                         goto free_fltr_list;
1856                 }
1857                 break;
1858         case ICE_SW_LKUP_MAC_VLAN:
1859         case ICE_SW_LKUP_ETHERTYPE:
1860         case ICE_SW_LKUP_ETHERTYPE_MAC:
1861         case ICE_SW_LKUP_PROMISC:
1862         case ICE_SW_LKUP_PROMISC_VLAN:
1863         case ICE_SW_LKUP_DFLT:
1864                 ice_debug(hw, ICE_DBG_SW,
1865                           "Remove filters for this lookup type hasn't been implemented yet\n");
1866                 break;
1867         }
1868
1869         return;
1870 free_fltr_list:
1871         list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
1872                 list_del(&fm_entry->list_entry);
1873                 devm_kfree(ice_hw_to_dev(hw), fm_entry);
1874         }
1875 }
1876
1877 /**
1878  * ice_remove_vsi_fltr - Remove all filters for a VSI
1879  * @hw: pointer to the hardware structure
1880  * @vsi_id: ID of VSI to remove filters from
1881  */
1882 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_id)
1883 {
1884         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_MAC);
1885         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_MAC_VLAN);
1886         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_PROMISC);
1887         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_VLAN);
1888         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_DFLT);
1889         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_ETHERTYPE);
1890         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_ETHERTYPE_MAC);
1891         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_PROMISC_VLAN);
1892 }