]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/rpmsg.git/blob - drivers/net/ethernet/intel/ice/ice_switch.c
Merge tag 'xfs-fixes-for-4.19-rc7' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[rpmsg/rpmsg.git] / drivers / net / ethernet / intel / ice / ice_switch.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
4 #include "ice_switch.h"
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
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};
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)))
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;
71         cmd = &desc.params.sw_res_ctrl;
73         if (!buf)
74                 return ICE_ERR_PARAM;
76         if (buf_size < (num_entries * sizeof(buf->elem[0])))
77                 return ICE_ERR_PARAM;
79         ice_fill_dflt_direct_cmd_desc(&desc, opc);
81         desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
83         cmd->num_entries = cpu_to_le16(num_entries);
85         return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
86 }
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)
118         struct ice_aqc_get_sw_cfg *cmd;
119         enum ice_status status;
120         struct ice_aq_desc desc;
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);
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         }
132         return status;
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)
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;
152         cmd = &desc.params.vsi_cmd;
153         res = (struct ice_aqc_add_update_free_vsi_resp *)&desc.params.raw;
155         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
157         if (!vsi_ctx->alloc_from_pool)
158                 cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num |
159                                            ICE_AQ_VSI_IS_VALID);
161         cmd->vsi_flags = cpu_to_le16(vsi_ctx->flags);
163         desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
165         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
166                                  sizeof(vsi_ctx->info), cd);
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         }
174         return status;
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)
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;
194         cmd = &desc.params.vsi_cmd;
195         resp = (struct ice_aqc_add_update_free_vsi_resp *)&desc.params.raw;
197         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
199         cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
201         desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
203         status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
204                                  sizeof(vsi_ctx->info), cd);
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         }
211         return status;
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)
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;
232         cmd = &desc.params.vsi_cmd;
233         resp = (struct ice_aqc_add_update_free_vsi_resp *)&desc.params.raw;
235         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
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);
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         }
247         return status;
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)
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;
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);
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         }
290         if (opc == ice_aqc_opc_free_res)
291                 sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
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;
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         }
302 ice_aq_alloc_free_vsi_list_exit:
303         devm_kfree(ice_hw_to_dev(hw), sw_buf);
304         return status;
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)
322         struct ice_aq_desc desc;
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;
329         ice_fill_dflt_direct_cmd_desc(&desc, opc);
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);
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)
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         }
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)
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;
376         rbuf = devm_kzalloc(ice_hw_to_dev(hw), ICE_SW_CFG_MAX_BUF_LEN,
377                             GFP_KERNEL);
379         if (!rbuf)
380                 return ICE_ERR_NO_MEMORY;
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);
391                 if (status)
392                         break;
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;
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;
404                         pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
405                                 ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
407                         swid = le16_to_cpu(ele->swid);
409                         if (le16_to_cpu(ele->pf_vf_num) &
410                             ICE_AQC_GET_SW_CONF_RESP_IS_VF)
411                                 is_vf = true;
413                         type = le16_to_cpu(ele->vsi_port_num) >>
414                                 ICE_AQC_GET_SW_CONF_RESP_TYPE_S;
416                         if (type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
417                                 /* FW VSI is not needed. Just continue. */
418                                 continue;
419                         }
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);
426         devm_kfree(ice_hw_to_dev(hw), (void *)rbuf);
427         return status;
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)
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         }
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)
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;
472         if (opc == ice_aqc_opc_remove_sw_rules) {
473                 s_rule->pdata.lkup_tx_rx.act = 0;
474                 s_rule->pdata.lkup_tx_rx.index =
475                         cpu_to_le16(f_info->fltr_rule_id);
476                 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
477                 return;
478         }
480         /* initialize the ether header with a dummy header */
481         memcpy(eth_hdr, dummy_eth_header, sizeof(dummy_eth_header));
482         ice_fill_sw_info(hw, f_info);
484         switch (f_info->fltr_act) {
485         case ICE_FWD_TO_VSI:
486                 act |= (f_info->fwd_id.vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
487                         ICE_SINGLE_ACT_VSI_ID_M;
488                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
489                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
490                                 ICE_SINGLE_ACT_VALID_BIT;
491                 break;
492         case ICE_FWD_TO_VSI_LIST:
493                 act |= ICE_SINGLE_ACT_VSI_LIST;
494                 act |= (f_info->fwd_id.vsi_list_id <<
495                         ICE_SINGLE_ACT_VSI_LIST_ID_S) &
496                         ICE_SINGLE_ACT_VSI_LIST_ID_M;
497                 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
498                         act |= ICE_SINGLE_ACT_VSI_FORWARDING |
499                                 ICE_SINGLE_ACT_VALID_BIT;
500                 break;
501         case ICE_FWD_TO_Q:
502                 act |= ICE_SINGLE_ACT_TO_Q;
503                 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
504                         ICE_SINGLE_ACT_Q_INDEX_M;
505                 break;
506         case ICE_FWD_TO_QGRP:
507                 act |= ICE_SINGLE_ACT_TO_Q;
508                 act |= (f_info->qgrp_size << ICE_SINGLE_ACT_Q_REGION_S) &
509                         ICE_SINGLE_ACT_Q_REGION_M;
510                 break;
511         case ICE_DROP_PACKET:
512                 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP;
513                 break;
514         default:
515                 return;
516         }
518         if (f_info->lb_en)
519                 act |= ICE_SINGLE_ACT_LB_ENABLE;
520         if (f_info->lan_en)
521                 act |= ICE_SINGLE_ACT_LAN_ENABLE;
523         switch (f_info->lkup_type) {
524         case ICE_SW_LKUP_MAC:
525                 daddr = f_info->l_data.mac.mac_addr;
526                 break;
527         case ICE_SW_LKUP_VLAN:
528                 vlan_id = f_info->l_data.vlan.vlan_id;
529                 if (f_info->fltr_act == ICE_FWD_TO_VSI ||
530                     f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
531                         act |= ICE_SINGLE_ACT_PRUNE;
532                         act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
533                 }
534                 break;
535         case ICE_SW_LKUP_ETHERTYPE_MAC:
536                 daddr = f_info->l_data.ethertype_mac.mac_addr;
537                 /* fall-through */
538         case ICE_SW_LKUP_ETHERTYPE:
539                 off = (__be16 *)&eth_hdr[ICE_ETH_ETHTYPE_OFFSET];
540                 *off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
541                 break;
542         case ICE_SW_LKUP_MAC_VLAN:
543                 daddr = f_info->l_data.mac_vlan.mac_addr;
544                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
545                 break;
546         case ICE_SW_LKUP_PROMISC_VLAN:
547                 vlan_id = f_info->l_data.mac_vlan.vlan_id;
548                 /* fall-through */
549         case ICE_SW_LKUP_PROMISC:
550                 daddr = f_info->l_data.mac_vlan.mac_addr;
551                 break;
552         default:
553                 break;
554         }
556         s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
557                 cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
558                 cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
560         /* Recipe set depending on lookup type */
561         s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(f_info->lkup_type);
562         s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(f_info->src);
563         s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);
565         if (daddr)
566                 ether_addr_copy(&eth_hdr[ICE_ETH_DA_OFFSET], daddr);
568         if (!(vlan_id > ICE_MAX_VLAN_ID)) {
569                 off = (__be16 *)&eth_hdr[ICE_ETH_VLAN_TCI_OFFSET];
570                 *off = cpu_to_be16(vlan_id);
571         }
573         /* Create the switch rule with the final dummy Ethernet header */
574         if (opc != ice_aqc_opc_update_sw_rules)
575                 s_rule->pdata.lkup_tx_rx.hdr_len = cpu_to_le16(sizeof(eth_hdr));
577         memcpy(s_rule->pdata.lkup_tx_rx.hdr, eth_hdr, sizeof(eth_hdr));
580 /**
581  * ice_add_marker_act
582  * @hw: pointer to the hardware structure
583  * @m_ent: the management entry for which sw marker needs to be added
584  * @sw_marker: sw marker to tag the Rx descriptor with
585  * @l_id: large action resource id
586  *
587  * Create a large action to hold software marker and update the switch rule
588  * entry pointed by m_ent with newly created large action
589  */
590 static enum ice_status
591 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
592                    u16 sw_marker, u16 l_id)
594         struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
595         /* For software marker we need 3 large actions
596          * 1. FWD action: FWD TO VSI or VSI LIST
597          * 2. GENERIC VALUE action to hold the profile id
598          * 3. GENERIC VALUE action to hold the software marker id
599          */
600         const u16 num_lg_acts = 3;
601         enum ice_status status;
602         u16 lg_act_size;
603         u16 rules_size;
604         u16 vsi_info;
605         u32 act;
607         if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
608                 return ICE_ERR_PARAM;
610         /* Create two back-to-back switch rules and submit them to the HW using
611          * one memory buffer:
612          *    1. Large Action
613          *    2. Look up tx rx
614          */
615         lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
616         rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
617         lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
618         if (!lg_act)
619                 return ICE_ERR_NO_MEMORY;
621         rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
623         /* Fill in the first switch rule i.e. large action */
624         lg_act->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
625         lg_act->pdata.lg_act.index = cpu_to_le16(l_id);
626         lg_act->pdata.lg_act.size = cpu_to_le16(num_lg_acts);
628         /* First action VSI forwarding or VSI list forwarding depending on how
629          * many VSIs
630          */
631         vsi_info = (m_ent->vsi_count > 1) ?
632                 m_ent->fltr_info.fwd_id.vsi_list_id :
633                 m_ent->fltr_info.fwd_id.vsi_id;
635         act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
636         act |= (vsi_info << ICE_LG_ACT_VSI_LIST_ID_S) &
637                 ICE_LG_ACT_VSI_LIST_ID_M;
638         if (m_ent->vsi_count > 1)
639                 act |= ICE_LG_ACT_VSI_LIST;
640         lg_act->pdata.lg_act.act[0] = cpu_to_le32(act);
642         /* Second action descriptor type */
643         act = ICE_LG_ACT_GENERIC;
645         act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
646         lg_act->pdata.lg_act.act[1] = cpu_to_le32(act);
648         act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
649                ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
651         /* Third action Marker value */
652         act |= ICE_LG_ACT_GENERIC;
653         act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
654                 ICE_LG_ACT_GENERIC_VALUE_M;
656         lg_act->pdata.lg_act.act[2] = cpu_to_le32(act);
658         /* call the fill switch rule to fill the lookup tx rx structure */
659         ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
660                          ice_aqc_opc_update_sw_rules);
662         /* Update the action to point to the large action id */
663         rx_tx->pdata.lkup_tx_rx.act =
664                 cpu_to_le32(ICE_SINGLE_ACT_PTR |
665                             ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
666                              ICE_SINGLE_ACT_PTR_VAL_M));
668         /* Use the filter rule id of the previously created rule with single
669          * act. Once the update happens, hardware will treat this as large
670          * action
671          */
672         rx_tx->pdata.lkup_tx_rx.index =
673                 cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
675         status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
676                                  ice_aqc_opc_update_sw_rules, NULL);
677         if (!status) {
678                 m_ent->lg_act_idx = l_id;
679                 m_ent->sw_marker_id = sw_marker;
680         }
682         devm_kfree(ice_hw_to_dev(hw), lg_act);
683         return status;
686 /**
687  * ice_create_vsi_list_map
688  * @hw: pointer to the hardware structure
689  * @vsi_array: array of VSIs to form a VSI list
690  * @num_vsi: num VSI in the array
691  * @vsi_list_id: VSI list id generated as part of allocate resource
692  *
693  * Helper function to create a new entry of VSI list id to VSI mapping
694  * using the given VSI list id
695  */
696 static struct ice_vsi_list_map_info *
697 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_array, u16 num_vsi,
698                         u16 vsi_list_id)
700         struct ice_switch_info *sw = hw->switch_info;
701         struct ice_vsi_list_map_info *v_map;
702         int i;
704         v_map = devm_kcalloc(ice_hw_to_dev(hw), 1, sizeof(*v_map), GFP_KERNEL);
705         if (!v_map)
706                 return NULL;
708         v_map->vsi_list_id = vsi_list_id;
710         for (i = 0; i < num_vsi; i++)
711                 set_bit(vsi_array[i], v_map->vsi_map);
713         list_add(&v_map->list_entry, &sw->vsi_list_map_head);
714         return v_map;
717 /**
718  * ice_update_vsi_list_rule
719  * @hw: pointer to the hardware structure
720  * @vsi_array: array of VSIs to form a VSI list
721  * @num_vsi: num VSI in the array
722  * @vsi_list_id: VSI list id generated as part of allocate resource
723  * @remove: Boolean value to indicate if this is a remove action
724  * @opc: switch rules population command type - pass in the command opcode
725  * @lkup_type: lookup type of the filter
726  *
727  * Call AQ command to add a new switch rule or update existing switch rule
728  * using the given VSI list id
729  */
730 static enum ice_status
731 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_array, u16 num_vsi,
732                          u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
733                          enum ice_sw_lkup_type lkup_type)
735         struct ice_aqc_sw_rules_elem *s_rule;
736         enum ice_status status;
737         u16 s_rule_size;
738         u16 type;
739         int i;
741         if (!num_vsi)
742                 return ICE_ERR_PARAM;
744         if (lkup_type == ICE_SW_LKUP_MAC ||
745             lkup_type == ICE_SW_LKUP_MAC_VLAN ||
746             lkup_type == ICE_SW_LKUP_ETHERTYPE ||
747             lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
748             lkup_type == ICE_SW_LKUP_PROMISC ||
749             lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
750                 type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
751                                 ICE_AQC_SW_RULES_T_VSI_LIST_SET;
752         else if (lkup_type == ICE_SW_LKUP_VLAN)
753                 type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
754                                 ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
755         else
756                 return ICE_ERR_PARAM;
758         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
759         s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
760         if (!s_rule)
761                 return ICE_ERR_NO_MEMORY;
763         for (i = 0; i < num_vsi; i++)
764                 s_rule->pdata.vsi_list.vsi[i] = cpu_to_le16(vsi_array[i]);
766         s_rule->type = cpu_to_le16(type);
767         s_rule->pdata.vsi_list.number_vsi = cpu_to_le16(num_vsi);
768         s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
770         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
772         devm_kfree(ice_hw_to_dev(hw), s_rule);
773         return status;
776 /**
777  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
778  * @hw: pointer to the hw struct
779  * @vsi_array: array of VSIs to form a VSI list
780  * @num_vsi: number of VSIs in the array
781  * @vsi_list_id: stores the ID of the VSI list to be created
782  * @lkup_type: switch rule filter's lookup type
783  */
784 static enum ice_status
785 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_array, u16 num_vsi,
786                          u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
788         enum ice_status status;
789         int i;
791         for (i = 0; i < num_vsi; i++)
792                 if (vsi_array[i] >= ICE_MAX_VSI)
793                         return ICE_ERR_OUT_OF_RANGE;
795         status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
796                                             ice_aqc_opc_alloc_res);
797         if (status)
798                 return status;
800         /* Update the newly created VSI list to include the specified VSIs */
801         return ice_update_vsi_list_rule(hw, vsi_array, num_vsi, *vsi_list_id,
802                                         false, ice_aqc_opc_add_sw_rules,
803                                         lkup_type);
806 /**
807  * ice_create_pkt_fwd_rule
808  * @hw: pointer to the hardware structure
809  * @f_entry: entry containing packet forwarding information
810  *
811  * Create switch rule with given filter information and add an entry
812  * to the corresponding filter management list to track this switch rule
813  * and VSI mapping
814  */
815 static enum ice_status
816 ice_create_pkt_fwd_rule(struct ice_hw *hw,
817                         struct ice_fltr_list_entry *f_entry)
819         struct ice_switch_info *sw = hw->switch_info;
820         struct ice_fltr_mgmt_list_entry *fm_entry;
821         struct ice_aqc_sw_rules_elem *s_rule;
822         enum ice_sw_lkup_type l_type;
823         enum ice_status status;
825         s_rule = devm_kzalloc(ice_hw_to_dev(hw),
826                               ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
827         if (!s_rule)
828                 return ICE_ERR_NO_MEMORY;
829         fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
830                                 GFP_KERNEL);
831         if (!fm_entry) {
832                 status = ICE_ERR_NO_MEMORY;
833                 goto ice_create_pkt_fwd_rule_exit;
834         }
836         fm_entry->fltr_info = f_entry->fltr_info;
838         /* Initialize all the fields for the management entry */
839         fm_entry->vsi_count = 1;
840         fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
841         fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
842         fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
844         ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
845                          ice_aqc_opc_add_sw_rules);
847         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
848                                  ice_aqc_opc_add_sw_rules, NULL);
849         if (status) {
850                 devm_kfree(ice_hw_to_dev(hw), fm_entry);
851                 goto ice_create_pkt_fwd_rule_exit;
852         }
854         f_entry->fltr_info.fltr_rule_id =
855                 le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
856         fm_entry->fltr_info.fltr_rule_id =
857                 le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
859         /* The book keeping entries will get removed when base driver
860          * calls remove filter AQ command
861          */
862         l_type = fm_entry->fltr_info.lkup_type;
863         if (l_type == ICE_SW_LKUP_MAC) {
864                 mutex_lock(&sw->mac_list_lock);
865                 list_add(&fm_entry->list_entry, &sw->mac_list_head);
866                 mutex_unlock(&sw->mac_list_lock);
867         } else if (l_type == ICE_SW_LKUP_VLAN) {
868                 mutex_lock(&sw->vlan_list_lock);
869                 list_add(&fm_entry->list_entry, &sw->vlan_list_head);
870                 mutex_unlock(&sw->vlan_list_lock);
871         } else if (l_type == ICE_SW_LKUP_ETHERTYPE ||
872                    l_type == ICE_SW_LKUP_ETHERTYPE_MAC) {
873                 mutex_lock(&sw->eth_m_list_lock);
874                 list_add(&fm_entry->list_entry, &sw->eth_m_list_head);
875                 mutex_unlock(&sw->eth_m_list_lock);
876         } else if (l_type == ICE_SW_LKUP_PROMISC ||
877                    l_type == ICE_SW_LKUP_PROMISC_VLAN) {
878                 mutex_lock(&sw->promisc_list_lock);
879                 list_add(&fm_entry->list_entry, &sw->promisc_list_head);
880                 mutex_unlock(&sw->promisc_list_lock);
881         } else if (fm_entry->fltr_info.lkup_type == ICE_SW_LKUP_MAC_VLAN) {
882                 mutex_lock(&sw->mac_vlan_list_lock);
883                 list_add(&fm_entry->list_entry, &sw->mac_vlan_list_head);
884                 mutex_unlock(&sw->mac_vlan_list_lock);
885         } else {
886                 status = ICE_ERR_NOT_IMPL;
887         }
888 ice_create_pkt_fwd_rule_exit:
889         devm_kfree(ice_hw_to_dev(hw), s_rule);
890         return status;
893 /**
894  * ice_update_pkt_fwd_rule
895  * @hw: pointer to the hardware structure
896  * @rule_id: rule of previously created switch rule to update
897  * @vsi_list_id: VSI list id to be updated with
898  * @f_info: ice_fltr_info to pull other information for switch rule
899  *
900  * Call AQ command to update a previously created switch rule with a
901  * VSI list id
902  */
903 static enum ice_status
904 ice_update_pkt_fwd_rule(struct ice_hw *hw, u16 rule_id, u16 vsi_list_id,
905                         struct ice_fltr_info f_info)
907         struct ice_aqc_sw_rules_elem *s_rule;
908         struct ice_fltr_info tmp_fltr;
909         enum ice_status status;
911         s_rule = devm_kzalloc(ice_hw_to_dev(hw),
912                               ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
913         if (!s_rule)
914                 return ICE_ERR_NO_MEMORY;
916         tmp_fltr = f_info;
917         tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
918         tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
920         ice_fill_sw_rule(hw, &tmp_fltr, s_rule,
921                          ice_aqc_opc_update_sw_rules);
923         s_rule->pdata.lkup_tx_rx.index = cpu_to_le16(rule_id);
925         /* Update switch rule with new rule set to forward VSI list */
926         status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
927                                  ice_aqc_opc_update_sw_rules, NULL);
929         devm_kfree(ice_hw_to_dev(hw), s_rule);
930         return status;
933 /**
934  * ice_handle_vsi_list_mgmt
935  * @hw: pointer to the hardware structure
936  * @m_entry: pointer to current filter management list entry
937  * @cur_fltr: filter information from the book keeping entry
938  * @new_fltr: filter information with the new VSI to be added
939  *
940  * Call AQ command to add or update previously created VSI list with new VSI.
941  *
942  * Helper function to do book keeping associated with adding filter information
943  * The algorithm to do the booking keeping is described below :
944  * When a VSI needs to subscribe to a given filter( MAC/VLAN/Ethtype etc.)
945  *      if only one VSI has been added till now
946  *              Allocate a new VSI list and add two VSIs
947  *              to this list using switch rule command
948  *              Update the previously created switch rule with the
949  *              newly created VSI list id
950  *      if a VSI list was previously created
951  *              Add the new VSI to the previously created VSI list set
952  *              using the update switch rule command
953  */
954 static enum ice_status
955 ice_handle_vsi_list_mgmt(struct ice_hw *hw,
956                          struct ice_fltr_mgmt_list_entry *m_entry,
957                          struct ice_fltr_info *cur_fltr,
958                          struct ice_fltr_info *new_fltr)
960         enum ice_status status = 0;
961         u16 vsi_list_id = 0;
963         if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
964              cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
965                 return ICE_ERR_NOT_IMPL;
967         if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
968              new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
969             (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
970              cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
971                 return ICE_ERR_NOT_IMPL;
973         if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
974                 /* Only one entry existed in the mapping and it was not already
975                  * a part of a VSI list. So, create a VSI list with the old and
976                  * new VSIs.
977                  */
978                 u16 vsi_id_arr[2];
979                 u16 fltr_rule;
981                 /* A rule already exists with the new VSI being added */
982                 if (cur_fltr->fwd_id.vsi_id == new_fltr->fwd_id.vsi_id)
983                         return ICE_ERR_ALREADY_EXISTS;
985                 vsi_id_arr[0] = cur_fltr->fwd_id.vsi_id;
986                 vsi_id_arr[1] = new_fltr->fwd_id.vsi_id;
987                 status = ice_create_vsi_list_rule(hw, &vsi_id_arr[0], 2,
988                                                   &vsi_list_id,
989                                                   new_fltr->lkup_type);
990                 if (status)
991                         return status;
993                 fltr_rule = cur_fltr->fltr_rule_id;
994                 /* Update the previous switch rule of "MAC forward to VSI" to
995                  * "MAC fwd to VSI list"
996                  */
997                 status = ice_update_pkt_fwd_rule(hw, fltr_rule, vsi_list_id,
998                                                  *new_fltr);
999                 if (status)
1000                         return status;
1002                 cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1003                 cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1004                 m_entry->vsi_list_info =
1005                         ice_create_vsi_list_map(hw, &vsi_id_arr[0], 2,
1006                                                 vsi_list_id);
1008                 /* If this entry was large action then the large action needs
1009                  * to be updated to point to FWD to VSI list
1010                  */
1011                 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1012                         status =
1013                             ice_add_marker_act(hw, m_entry,
1014                                                m_entry->sw_marker_id,
1015                                                m_entry->lg_act_idx);
1016         } else {
1017                 u16 vsi_id = new_fltr->fwd_id.vsi_id;
1018                 enum ice_adminq_opc opcode;
1020                 /* A rule already exists with the new VSI being added */
1021                 if (test_bit(vsi_id, m_entry->vsi_list_info->vsi_map))
1022                         return 0;
1024                 /* Update the previously created VSI list set with
1025                  * the new VSI id passed in
1026                  */
1027                 vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1028                 opcode = ice_aqc_opc_update_sw_rules;
1030                 status = ice_update_vsi_list_rule(hw, &vsi_id, 1, vsi_list_id,
1031                                                   false, opcode,
1032                                                   new_fltr->lkup_type);
1033                 /* update VSI list mapping info with new VSI id */
1034                 if (!status)
1035                         set_bit(vsi_id, m_entry->vsi_list_info->vsi_map);
1036         }
1037         if (!status)
1038                 m_entry->vsi_count++;
1039         return status;
1042 /**
1043  * ice_find_mac_entry
1044  * @hw: pointer to the hardware structure
1045  * @mac_addr: MAC address to search for
1046  *
1047  * Helper function to search for a MAC entry using a given MAC address
1048  * Returns pointer to the entry if found.
1049  */
1050 static struct ice_fltr_mgmt_list_entry *
1051 ice_find_mac_entry(struct ice_hw *hw, u8 *mac_addr)
1053         struct ice_fltr_mgmt_list_entry *m_list_itr, *mac_ret = NULL;
1054         struct ice_switch_info *sw = hw->switch_info;
1056         mutex_lock(&sw->mac_list_lock);
1057         list_for_each_entry(m_list_itr, &sw->mac_list_head, list_entry) {
1058                 u8 *buf = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
1060                 if (ether_addr_equal(buf, mac_addr)) {
1061                         mac_ret = m_list_itr;
1062                         break;
1063                 }
1064         }
1065         mutex_unlock(&sw->mac_list_lock);
1066         return mac_ret;
1069 /**
1070  * ice_add_shared_mac - Add one MAC shared filter rule
1071  * @hw: pointer to the hardware structure
1072  * @f_entry: structure containing MAC forwarding information
1073  *
1074  * Adds or updates the book keeping list for the MAC addresses
1075  */
1076 static enum ice_status
1077 ice_add_shared_mac(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1079         struct ice_fltr_info *new_fltr, *cur_fltr;
1080         struct ice_fltr_mgmt_list_entry *m_entry;
1082         new_fltr = &f_entry->fltr_info;
1084         m_entry = ice_find_mac_entry(hw, &new_fltr->l_data.mac.mac_addr[0]);
1085         if (!m_entry)
1086                 return ice_create_pkt_fwd_rule(hw, f_entry);
1088         cur_fltr = &m_entry->fltr_info;
1090         return ice_handle_vsi_list_mgmt(hw, m_entry, cur_fltr, new_fltr);
1093 /**
1094  * ice_add_mac - Add a MAC address based filter rule
1095  * @hw: pointer to the hardware structure
1096  * @m_list: list of MAC addresses and forwarding information
1097  *
1098  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
1099  * multiple unicast addresses, the function assumes that all the
1100  * addresses are unique in a given add_mac call. It doesn't
1101  * check for duplicates in this case, removing duplicates from a given
1102  * list should be taken care of in the caller of this function.
1103  */
1104 enum ice_status
1105 ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
1107         struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
1108         struct ice_fltr_list_entry *m_list_itr;
1109         u16 elem_sent, total_elem_left;
1110         enum ice_status status = 0;
1111         u16 num_unicast = 0;
1112         u16 s_rule_size;
1114         if (!m_list || !hw)
1115                 return ICE_ERR_PARAM;
1117         list_for_each_entry(m_list_itr, m_list, list_entry) {
1118                 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
1120                 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1121                         return ICE_ERR_PARAM;
1122                 if (is_zero_ether_addr(add))
1123                         return ICE_ERR_PARAM;
1124                 if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
1125                         /* Don't overwrite the unicast address */
1126                         if (ice_find_mac_entry(hw, add))
1127                                 return ICE_ERR_ALREADY_EXISTS;
1128                         num_unicast++;
1129                 } else if (is_multicast_ether_addr(add) ||
1130                            (is_unicast_ether_addr(add) && hw->ucast_shared)) {
1131                         status = ice_add_shared_mac(hw, m_list_itr);
1132                         if (status) {
1133                                 m_list_itr->status = ICE_FLTR_STATUS_FW_FAIL;
1134                                 return status;
1135                         }
1136                         m_list_itr->status = ICE_FLTR_STATUS_FW_SUCCESS;
1137                 }
1138         }
1140         /* Exit if no suitable entries were found for adding bulk switch rule */
1141         if (!num_unicast)
1142                 return 0;
1144         /* Allocate switch rule buffer for the bulk update for unicast */
1145         s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1146         s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
1147                               GFP_KERNEL);
1148         if (!s_rule)
1149                 return ICE_ERR_NO_MEMORY;
1151         r_iter = s_rule;
1152         list_for_each_entry(m_list_itr, m_list, list_entry) {
1153                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1154                 u8 *addr = &f_info->l_data.mac.mac_addr[0];
1156                 if (is_unicast_ether_addr(addr)) {
1157                         ice_fill_sw_rule(hw, &m_list_itr->fltr_info,
1158                                          r_iter, ice_aqc_opc_add_sw_rules);
1159                         r_iter = (struct ice_aqc_sw_rules_elem *)
1160                                 ((u8 *)r_iter + s_rule_size);
1161                 }
1162         }
1164         /* Call AQ bulk switch rule update for all unicast addresses */
1165         r_iter = s_rule;
1166         /* Call AQ switch rule in AQ_MAX chunk */
1167         for (total_elem_left = num_unicast; total_elem_left > 0;
1168              total_elem_left -= elem_sent) {
1169                 struct ice_aqc_sw_rules_elem *entry = r_iter;
1171                 elem_sent = min(total_elem_left,
1172                                 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
1173                 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
1174                                          elem_sent, ice_aqc_opc_add_sw_rules,
1175                                          NULL);
1176                 if (status)
1177                         goto ice_add_mac_exit;
1178                 r_iter = (struct ice_aqc_sw_rules_elem *)
1179                         ((u8 *)r_iter + (elem_sent * s_rule_size));
1180         }
1182         /* Fill up rule id based on the value returned from FW */
1183         r_iter = s_rule;
1184         list_for_each_entry(m_list_itr, m_list, list_entry) {
1185                 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1186                 u8 *addr = &f_info->l_data.mac.mac_addr[0];
1187                 struct ice_switch_info *sw = hw->switch_info;
1188                 struct ice_fltr_mgmt_list_entry *fm_entry;
1190                 if (is_unicast_ether_addr(addr)) {
1191                         f_info->fltr_rule_id =
1192                                 le16_to_cpu(r_iter->pdata.lkup_tx_rx.index);
1193                         f_info->fltr_act = ICE_FWD_TO_VSI;
1194                         /* Create an entry to track this MAC address */
1195                         fm_entry = devm_kzalloc(ice_hw_to_dev(hw),
1196                                                 sizeof(*fm_entry), GFP_KERNEL);
1197                         if (!fm_entry) {
1198                                 status = ICE_ERR_NO_MEMORY;
1199                                 goto ice_add_mac_exit;
1200                         }
1201                         fm_entry->fltr_info = *f_info;
1202                         fm_entry->vsi_count = 1;
1203                         /* The book keeping entries will get removed when
1204                          * base driver calls remove filter AQ command
1205                          */
1206                         mutex_lock(&sw->mac_list_lock);
1207                         list_add(&fm_entry->list_entry, &sw->mac_list_head);
1208                         mutex_unlock(&sw->mac_list_lock);
1210                         r_iter = (struct ice_aqc_sw_rules_elem *)
1211                                 ((u8 *)r_iter + s_rule_size);
1212                 }
1213         }
1215 ice_add_mac_exit:
1216         devm_kfree(ice_hw_to_dev(hw), s_rule);
1217         return status;
1220 /**
1221  * ice_find_vlan_entry
1222  * @hw: pointer to the hardware structure
1223  * @vlan_id: VLAN id to search for
1224  *
1225  * Helper function to search for a VLAN entry using a given VLAN id
1226  * Returns pointer to the entry if found.
1227  */
1228 static struct ice_fltr_mgmt_list_entry *
1229 ice_find_vlan_entry(struct ice_hw *hw, u16 vlan_id)
1231         struct ice_fltr_mgmt_list_entry *vlan_list_itr, *vlan_ret = NULL;
1232         struct ice_switch_info *sw = hw->switch_info;
1234         mutex_lock(&sw->vlan_list_lock);
1235         list_for_each_entry(vlan_list_itr, &sw->vlan_list_head, list_entry)
1236                 if (vlan_list_itr->fltr_info.l_data.vlan.vlan_id == vlan_id) {
1237                         vlan_ret = vlan_list_itr;
1238                         break;
1239                 }
1241         mutex_unlock(&sw->vlan_list_lock);
1242         return vlan_ret;
1245 /**
1246  * ice_add_vlan_internal - Add one VLAN based filter rule
1247  * @hw: pointer to the hardware structure
1248  * @f_entry: filter entry containing one VLAN information
1249  */
1250 static enum ice_status
1251 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1253         struct ice_fltr_info *new_fltr, *cur_fltr;
1254         struct ice_fltr_mgmt_list_entry *v_list_itr;
1255         u16 vlan_id;
1257         new_fltr = &f_entry->fltr_info;
1258         /* VLAN id should only be 12 bits */
1259         if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
1260                 return ICE_ERR_PARAM;
1262         vlan_id = new_fltr->l_data.vlan.vlan_id;
1263         v_list_itr = ice_find_vlan_entry(hw, vlan_id);
1264         if (!v_list_itr) {
1265                 u16 vsi_id = ICE_VSI_INVAL_ID;
1266                 enum ice_status status;
1267                 u16 vsi_list_id = 0;
1269                 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
1270                         enum ice_sw_lkup_type lkup_type = new_fltr->lkup_type;
1272                         /* All VLAN pruning rules use a VSI list.
1273                          * Convert the action to forwarding to a VSI list.
1274                          */
1275                         vsi_id = new_fltr->fwd_id.vsi_id;
1276                         status = ice_create_vsi_list_rule(hw, &vsi_id, 1,
1277                                                           &vsi_list_id,
1278                                                           lkup_type);
1279                         if (status)
1280                                 return status;
1281                         new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1282                         new_fltr->fwd_id.vsi_list_id = vsi_list_id;
1283                 }
1285                 status = ice_create_pkt_fwd_rule(hw, f_entry);
1286                 if (!status && vsi_id != ICE_VSI_INVAL_ID) {
1287                         v_list_itr = ice_find_vlan_entry(hw, vlan_id);
1288                         if (!v_list_itr)
1289                                 return ICE_ERR_DOES_NOT_EXIST;
1290                         v_list_itr->vsi_list_info =
1291                                 ice_create_vsi_list_map(hw, &vsi_id, 1,
1292                                                         vsi_list_id);
1293                 }
1295                 return status;
1296         }
1298         cur_fltr = &v_list_itr->fltr_info;
1299         return ice_handle_vsi_list_mgmt(hw, v_list_itr, cur_fltr, new_fltr);
1302 /**
1303  * ice_add_vlan - Add VLAN based filter rule
1304  * @hw: pointer to the hardware structure
1305  * @v_list: list of VLAN entries and forwarding information
1306  */
1307 enum ice_status
1308 ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
1310         struct ice_fltr_list_entry *v_list_itr;
1312         if (!v_list || !hw)
1313                 return ICE_ERR_PARAM;
1315         list_for_each_entry(v_list_itr, v_list, list_entry) {
1316                 enum ice_status status;
1318                 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
1319                         return ICE_ERR_PARAM;
1321                 status = ice_add_vlan_internal(hw, v_list_itr);
1322                 if (status) {
1323                         v_list_itr->status = ICE_FLTR_STATUS_FW_FAIL;
1324                         return status;
1325                 }
1326                 v_list_itr->status = ICE_FLTR_STATUS_FW_SUCCESS;
1327         }
1328         return 0;
1331 /**
1332  * ice_remove_vsi_list_rule
1333  * @hw: pointer to the hardware structure
1334  * @vsi_list_id: VSI list id generated as part of allocate resource
1335  * @lkup_type: switch rule filter lookup type
1336  */
1337 static enum ice_status
1338 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1339                          enum ice_sw_lkup_type lkup_type)
1341         struct ice_aqc_sw_rules_elem *s_rule;
1342         enum ice_status status;
1343         u16 s_rule_size;
1345         s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
1346         s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1347         if (!s_rule)
1348                 return ICE_ERR_NO_MEMORY;
1350         s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
1351         s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
1352         /* FW expects number of VSIs in vsi_list resource to be 0 for clear
1353          * command. Since memory is zero'ed out during initialization, it's not
1354          * necessary to explicitly initialize the variable to 0.
1355          */
1357         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1,
1358                                  ice_aqc_opc_remove_sw_rules, NULL);
1359         if (!status)
1360                 /* Free the vsi_list resource that we allocated */
1361                 status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
1362                                                     ice_aqc_opc_free_res);
1364         devm_kfree(ice_hw_to_dev(hw), s_rule);
1365         return status;
1368 /**
1369  * ice_handle_rem_vsi_list_mgmt
1370  * @hw: pointer to the hardware structure
1371  * @vsi_id: ID of the VSI to remove
1372  * @fm_list_itr: filter management entry for which the VSI list management
1373  * needs to be done
1374  */
1375 static enum ice_status
1376 ice_handle_rem_vsi_list_mgmt(struct ice_hw *hw, u16 vsi_id,
1377                              struct ice_fltr_mgmt_list_entry *fm_list_itr)
1379         struct ice_switch_info *sw = hw->switch_info;
1380         enum ice_status status = 0;
1381         enum ice_sw_lkup_type lkup_type;
1382         bool is_last_elem = true;
1383         bool conv_list = false;
1384         bool del_list = false;
1385         u16 vsi_list_id;
1387         lkup_type = fm_list_itr->fltr_info.lkup_type;
1388         vsi_list_id = fm_list_itr->fltr_info.fwd_id.vsi_list_id;
1390         if (fm_list_itr->vsi_count > 1) {
1391                 status = ice_update_vsi_list_rule(hw, &vsi_id, 1, vsi_list_id,
1392                                                   true,
1393                                                   ice_aqc_opc_update_sw_rules,
1394                                                   lkup_type);
1395                 if (status)
1396                         return status;
1397                 fm_list_itr->vsi_count--;
1398                 is_last_elem = false;
1399                 clear_bit(vsi_id, fm_list_itr->vsi_list_info->vsi_map);
1400         }
1402         /* For non-VLAN rules that forward packets to a VSI list, convert them
1403          * to forwarding packets to a VSI if there is only one VSI left in the
1404          * list.  Unused lists are then removed.
1405          * VLAN rules need to use VSI lists even with only one VSI.
1406          */
1407         if (fm_list_itr->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST) {
1408                 if (lkup_type == ICE_SW_LKUP_VLAN) {
1409                         del_list = is_last_elem;
1410                 } else if (fm_list_itr->vsi_count == 1) {
1411                         conv_list = true;
1412                         del_list = true;
1413                 }
1414         }
1416         if (del_list) {
1417                 /* Remove the VSI list since it is no longer used */
1418                 struct ice_vsi_list_map_info *vsi_list_info =
1419                         fm_list_itr->vsi_list_info;
1421                 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
1422                 if (status)
1423                         return status;
1425                 if (conv_list) {
1426                         u16 rem_vsi_id;
1428                         rem_vsi_id = find_first_bit(vsi_list_info->vsi_map,
1429                                                     ICE_MAX_VSI);
1431                         /* Error out when the expected last element is not in
1432                          * the VSI list map
1433                          */
1434                         if (rem_vsi_id == ICE_MAX_VSI)
1435                                 return ICE_ERR_OUT_OF_RANGE;
1437                         /* Change the list entry action from VSI_LIST to VSI */
1438                         fm_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1439                         fm_list_itr->fltr_info.fwd_id.vsi_id = rem_vsi_id;
1440                 }
1442                 list_del(&vsi_list_info->list_entry);
1443                 devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
1444                 fm_list_itr->vsi_list_info = NULL;
1445         }
1447         if (conv_list) {
1448                 /* Convert the rule's forward action to forwarding packets to
1449                  * a VSI
1450                  */
1451                 struct ice_aqc_sw_rules_elem *s_rule;
1453                 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1454                                       ICE_SW_RULE_RX_TX_ETH_HDR_SIZE,
1455                                       GFP_KERNEL);
1456                 if (!s_rule)
1457                         return ICE_ERR_NO_MEMORY;
1459                 ice_fill_sw_rule(hw, &fm_list_itr->fltr_info, s_rule,
1460                                  ice_aqc_opc_update_sw_rules);
1462                 s_rule->pdata.lkup_tx_rx.index =
1463                         cpu_to_le16(fm_list_itr->fltr_info.fltr_rule_id);
1465                 status = ice_aq_sw_rules(hw, s_rule,
1466                                          ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1467                                          ice_aqc_opc_update_sw_rules, NULL);
1468                 devm_kfree(ice_hw_to_dev(hw), s_rule);
1469                 if (status)
1470                         return status;
1471         }
1473         if (is_last_elem) {
1474                 /* Remove the lookup rule */
1475                 struct ice_aqc_sw_rules_elem *s_rule;
1477                 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1478                                       ICE_SW_RULE_RX_TX_NO_HDR_SIZE,
1479                                       GFP_KERNEL);
1480                 if (!s_rule)
1481                         return ICE_ERR_NO_MEMORY;
1483                 ice_fill_sw_rule(hw, &fm_list_itr->fltr_info, s_rule,
1484                                  ice_aqc_opc_remove_sw_rules);
1486                 status = ice_aq_sw_rules(hw, s_rule,
1487                                          ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
1488                                          ice_aqc_opc_remove_sw_rules, NULL);
1489                 if (status)
1490                         return status;
1492                 /* Remove a book keeping entry from the MAC address list */
1493                 mutex_lock(&sw->mac_list_lock);
1494                 list_del(&fm_list_itr->list_entry);
1495                 mutex_unlock(&sw->mac_list_lock);
1496                 devm_kfree(ice_hw_to_dev(hw), fm_list_itr);
1497                 devm_kfree(ice_hw_to_dev(hw), s_rule);
1498         }
1499         return status;
1502 /**
1503  * ice_remove_mac_entry
1504  * @hw: pointer to the hardware structure
1505  * @f_entry: structure containing MAC forwarding information
1506  */
1507 static enum ice_status
1508 ice_remove_mac_entry(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1510         struct ice_fltr_mgmt_list_entry *m_entry;
1511         u16 vsi_id;
1512         u8 *add;
1514         add = &f_entry->fltr_info.l_data.mac.mac_addr[0];
1516         m_entry = ice_find_mac_entry(hw, add);
1517         if (!m_entry)
1518                 return ICE_ERR_PARAM;
1520         vsi_id = f_entry->fltr_info.fwd_id.vsi_id;
1521         return ice_handle_rem_vsi_list_mgmt(hw, vsi_id, m_entry);
1524 /**
1525  * ice_remove_mac - remove a MAC address based filter rule
1526  * @hw: pointer to the hardware structure
1527  * @m_list: list of MAC addresses and forwarding information
1528  *
1529  * This function removes either a MAC filter rule or a specific VSI from a
1530  * VSI list for a multicast MAC address.
1531  *
1532  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
1533  * ice_add_mac. Caller should be aware that this call will only work if all
1534  * the entries passed into m_list were added previously. It will not attempt to
1535  * do a partial remove of entries that were found.
1536  */
1537 enum ice_status
1538 ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
1540         struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
1541         u8 s_rule_size = ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
1542         struct ice_switch_info *sw = hw->switch_info;
1543         struct ice_fltr_mgmt_list_entry *m_entry;
1544         struct ice_fltr_list_entry *m_list_itr;
1545         u16 elem_sent, total_elem_left;
1546         enum ice_status status = 0;
1547         u16 num_unicast = 0;
1549         if (!m_list)
1550                 return ICE_ERR_PARAM;
1552         list_for_each_entry(m_list_itr, m_list, list_entry) {
1553                 u8 *addr = m_list_itr->fltr_info.l_data.mac.mac_addr;
1555                 if (is_unicast_ether_addr(addr) && !hw->ucast_shared)
1556                         num_unicast++;
1557                 else if (is_multicast_ether_addr(addr) ||
1558                          (is_unicast_ether_addr(addr) && hw->ucast_shared))
1559                         ice_remove_mac_entry(hw, m_list_itr);
1560         }
1562         /* Exit if no unicast addresses found. Multicast switch rules
1563          * were added individually
1564          */
1565         if (!num_unicast)
1566                 return 0;
1568         /* Allocate switch rule buffer for the bulk update for unicast */
1569         s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
1570                               GFP_KERNEL);
1571         if (!s_rule)
1572                 return ICE_ERR_NO_MEMORY;
1574         r_iter = s_rule;
1575         list_for_each_entry(m_list_itr, m_list, list_entry) {
1576                 u8 *addr = m_list_itr->fltr_info.l_data.mac.mac_addr;
1578                 if (is_unicast_ether_addr(addr)) {
1579                         m_entry = ice_find_mac_entry(hw, addr);
1580                         if (!m_entry) {
1581                                 status = ICE_ERR_DOES_NOT_EXIST;
1582                                 goto ice_remove_mac_exit;
1583                         }
1585                         ice_fill_sw_rule(hw, &m_entry->fltr_info,
1586                                          r_iter, ice_aqc_opc_remove_sw_rules);
1587                         r_iter = (struct ice_aqc_sw_rules_elem *)
1588                                 ((u8 *)r_iter + s_rule_size);
1589                 }
1590         }
1592         /* Call AQ bulk switch rule update for all unicast addresses */
1593         r_iter = s_rule;
1594         /* Call AQ switch rule in AQ_MAX chunk */
1595         for (total_elem_left = num_unicast; total_elem_left > 0;
1596              total_elem_left -= elem_sent) {
1597                 struct ice_aqc_sw_rules_elem *entry = r_iter;
1599                 elem_sent = min(total_elem_left,
1600                                 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
1601                 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
1602                                          elem_sent, ice_aqc_opc_remove_sw_rules,
1603                                          NULL);
1604                 if (status)
1605                         break;
1606                 r_iter = (struct ice_aqc_sw_rules_elem *)
1607                         ((u8 *)r_iter + s_rule_size);
1608         }
1610         list_for_each_entry(m_list_itr, m_list, list_entry) {
1611                 u8 *addr = m_list_itr->fltr_info.l_data.mac.mac_addr;
1613                 if (is_unicast_ether_addr(addr)) {
1614                         m_entry = ice_find_mac_entry(hw, addr);
1615                         if (!m_entry)
1616                                 return ICE_ERR_OUT_OF_RANGE;
1617                         mutex_lock(&sw->mac_list_lock);
1618                         list_del(&m_entry->list_entry);
1619                         mutex_unlock(&sw->mac_list_lock);
1620                         devm_kfree(ice_hw_to_dev(hw), m_entry);
1621                 }
1622         }
1624 ice_remove_mac_exit:
1625         devm_kfree(ice_hw_to_dev(hw), s_rule);
1626         return status;
1629 /**
1630  * ice_cfg_dflt_vsi - add filter rule to set/unset given VSI as default
1631  * VSI for the switch (represented by swid)
1632  * @hw: pointer to the hardware structure
1633  * @vsi_id: number of VSI to set as default
1634  * @set: true to add the above mentioned switch rule, false to remove it
1635  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
1636  */
1637 enum ice_status
1638 ice_cfg_dflt_vsi(struct ice_hw *hw, u16 vsi_id, bool set, u8 direction)
1640         struct ice_aqc_sw_rules_elem *s_rule;
1641         struct ice_fltr_info f_info;
1642         enum ice_adminq_opc opcode;
1643         enum ice_status status;
1644         u16 s_rule_size;
1646         s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
1647                             ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
1648         s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1649         if (!s_rule)
1650                 return ICE_ERR_NO_MEMORY;
1652         memset(&f_info, 0, sizeof(f_info));
1654         f_info.lkup_type = ICE_SW_LKUP_DFLT;
1655         f_info.flag = direction;
1656         f_info.fltr_act = ICE_FWD_TO_VSI;
1657         f_info.fwd_id.vsi_id = vsi_id;
1659         if (f_info.flag & ICE_FLTR_RX) {
1660                 f_info.src = hw->port_info->lport;
1661                 if (!set)
1662                         f_info.fltr_rule_id =
1663                                 hw->port_info->dflt_rx_vsi_rule_id;
1664         } else if (f_info.flag & ICE_FLTR_TX) {
1665                 f_info.src = vsi_id;
1666                 if (!set)
1667                         f_info.fltr_rule_id =
1668                                 hw->port_info->dflt_tx_vsi_rule_id;
1669         }
1671         if (set)
1672                 opcode = ice_aqc_opc_add_sw_rules;
1673         else
1674                 opcode = ice_aqc_opc_remove_sw_rules;
1676         ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
1678         status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
1679         if (status || !(f_info.flag & ICE_FLTR_TX_RX))
1680                 goto out;
1681         if (set) {
1682                 u16 index = le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1684                 if (f_info.flag & ICE_FLTR_TX) {
1685                         hw->port_info->dflt_tx_vsi_num = vsi_id;
1686                         hw->port_info->dflt_tx_vsi_rule_id = index;
1687                 } else if (f_info.flag & ICE_FLTR_RX) {
1688                         hw->port_info->dflt_rx_vsi_num = vsi_id;
1689                         hw->port_info->dflt_rx_vsi_rule_id = index;
1690                 }
1691         } else {
1692                 if (f_info.flag & ICE_FLTR_TX) {
1693                         hw->port_info->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
1694                         hw->port_info->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
1695                 } else if (f_info.flag & ICE_FLTR_RX) {
1696                         hw->port_info->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
1697                         hw->port_info->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
1698                 }
1699         }
1701 out:
1702         devm_kfree(ice_hw_to_dev(hw), s_rule);
1703         return status;
1706 /**
1707  * ice_remove_vlan_internal - Remove one VLAN based filter rule
1708  * @hw: pointer to the hardware structure
1709  * @f_entry: filter entry containing one VLAN information
1710  */
1711 static enum ice_status
1712 ice_remove_vlan_internal(struct ice_hw *hw,
1713                          struct ice_fltr_list_entry *f_entry)
1715         struct ice_fltr_info *new_fltr;
1716         struct ice_fltr_mgmt_list_entry *v_list_elem;
1717         u16 vsi_id;
1719         new_fltr = &f_entry->fltr_info;
1721         v_list_elem = ice_find_vlan_entry(hw, new_fltr->l_data.vlan.vlan_id);
1722         if (!v_list_elem)
1723                 return ICE_ERR_PARAM;
1725         vsi_id = f_entry->fltr_info.fwd_id.vsi_id;
1726         return ice_handle_rem_vsi_list_mgmt(hw, vsi_id, v_list_elem);
1729 /**
1730  * ice_remove_vlan - Remove VLAN based filter rule
1731  * @hw: pointer to the hardware structure
1732  * @v_list: list of VLAN entries and forwarding information
1733  */
1734 enum ice_status
1735 ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
1737         struct ice_fltr_list_entry *v_list_itr;
1738         enum ice_status status = 0;
1740         if (!v_list || !hw)
1741                 return ICE_ERR_PARAM;
1743         list_for_each_entry(v_list_itr, v_list, list_entry) {
1744                 status = ice_remove_vlan_internal(hw, v_list_itr);
1745                 if (status) {
1746                         v_list_itr->status = ICE_FLTR_STATUS_FW_FAIL;
1747                         return status;
1748                 }
1749                 v_list_itr->status = ICE_FLTR_STATUS_FW_SUCCESS;
1750         }
1751         return status;
1754 /**
1755  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
1756  * @hw: pointer to the hardware structure
1757  * @vsi_id: ID of VSI to remove filters from
1758  * @lkup_list_head: pointer to the list that has certain lookup type filters
1759  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_id
1760  */
1761 static enum ice_status
1762 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_id,
1763                          struct list_head *lkup_list_head,
1764                          struct list_head *vsi_list_head)
1766         struct ice_fltr_mgmt_list_entry *fm_entry;
1768         /* check to make sure VSI id is valid and within boundary */
1769         if (vsi_id >=
1770             (sizeof(fm_entry->vsi_list_info->vsi_map) * BITS_PER_BYTE - 1))
1771                 return ICE_ERR_PARAM;
1773         list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
1774                 struct ice_fltr_info *fi;
1776                 fi = &fm_entry->fltr_info;
1777                 if ((fi->fltr_act == ICE_FWD_TO_VSI &&
1778                      fi->fwd_id.vsi_id == vsi_id) ||
1779                     (fi->fltr_act == ICE_FWD_TO_VSI_LIST &&
1780                      (test_bit(vsi_id, fm_entry->vsi_list_info->vsi_map)))) {
1781                         struct ice_fltr_list_entry *tmp;
1783                         /* this memory is freed up in the caller function
1784                          * ice_remove_vsi_lkup_fltr() once filters for
1785                          * this VSI are removed
1786                          */
1787                         tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp),
1788                                            GFP_KERNEL);
1789                         if (!tmp)
1790                                 return ICE_ERR_NO_MEMORY;
1792                         memcpy(&tmp->fltr_info, fi, sizeof(*fi));
1794                         /* Expected below fields to be set to ICE_FWD_TO_VSI and
1795                          * the particular VSI id since we are only removing this
1796                          * one VSI
1797                          */
1798                         if (fi->fltr_act == ICE_FWD_TO_VSI_LIST) {
1799                                 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1800                                 tmp->fltr_info.fwd_id.vsi_id = vsi_id;
1801                         }
1803                         list_add(&tmp->list_entry, vsi_list_head);
1804                 }
1805         }
1806         return 0;
1809 /**
1810  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
1811  * @hw: pointer to the hardware structure
1812  * @vsi_id: ID of VSI to remove filters from
1813  * @lkup: switch rule filter lookup type
1814  */
1815 static void
1816 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_id,
1817                          enum ice_sw_lkup_type lkup)
1819         struct ice_switch_info *sw = hw->switch_info;
1820         struct ice_fltr_list_entry *fm_entry;
1821         struct list_head remove_list_head;
1822         struct ice_fltr_list_entry *tmp;
1823         enum ice_status status;
1825         INIT_LIST_HEAD(&remove_list_head);
1826         switch (lkup) {
1827         case ICE_SW_LKUP_MAC:
1828                 mutex_lock(&sw->mac_list_lock);
1829                 status = ice_add_to_vsi_fltr_list(hw, vsi_id,
1830                                                   &sw->mac_list_head,
1831                                                   &remove_list_head);
1832                 mutex_unlock(&sw->mac_list_lock);
1833                 if (!status) {
1834                         ice_remove_mac(hw, &remove_list_head);
1835                         goto free_fltr_list;
1836                 }
1837                 break;
1838         case ICE_SW_LKUP_VLAN:
1839                 mutex_lock(&sw->vlan_list_lock);
1840                 status = ice_add_to_vsi_fltr_list(hw, vsi_id,
1841                                                   &sw->vlan_list_head,
1842                                                   &remove_list_head);
1843                 mutex_unlock(&sw->vlan_list_lock);
1844                 if (!status) {
1845                         ice_remove_vlan(hw, &remove_list_head);
1846                         goto free_fltr_list;
1847                 }
1848                 break;
1849         case ICE_SW_LKUP_MAC_VLAN:
1850         case ICE_SW_LKUP_ETHERTYPE:
1851         case ICE_SW_LKUP_ETHERTYPE_MAC:
1852         case ICE_SW_LKUP_PROMISC:
1853         case ICE_SW_LKUP_PROMISC_VLAN:
1854         case ICE_SW_LKUP_DFLT:
1855                 ice_debug(hw, ICE_DBG_SW,
1856                           "Remove filters for this lookup type hasn't been implemented yet\n");
1857                 break;
1858         }
1860         return;
1861 free_fltr_list:
1862         list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
1863                 list_del(&fm_entry->list_entry);
1864                 devm_kfree(ice_hw_to_dev(hw), fm_entry);
1865         }
1868 /**
1869  * ice_remove_vsi_fltr - Remove all filters for a VSI
1870  * @hw: pointer to the hardware structure
1871  * @vsi_id: ID of VSI to remove filters from
1872  */
1873 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_id)
1875         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_MAC);
1876         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_MAC_VLAN);
1877         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_PROMISC);
1878         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_VLAN);
1879         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_DFLT);
1880         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_ETHERTYPE);
1881         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_ETHERTYPE_MAC);
1882         ice_remove_vsi_lkup_fltr(hw, vsi_id, ICE_SW_LKUP_PROMISC_VLAN);