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) }
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
26 #define DUMMY_ETH_HDR_LEN 16
27 static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
31 #define ICE_SW_RULE_RX_TX_ETH_HDR_SIZE \
32 (offsetof(struct ice_aqc_sw_rules_elem, pdata.lkup_tx_rx.hdr) + \
33 (DUMMY_ETH_HDR_LEN * \
34 sizeof(((struct ice_sw_rule_lkup_rx_tx *)0)->hdr[0])))
35 #define ICE_SW_RULE_RX_TX_NO_HDR_SIZE \
36 (offsetof(struct ice_aqc_sw_rules_elem, pdata.lkup_tx_rx.hdr))
37 #define ICE_SW_RULE_LG_ACT_SIZE(n) \
38 (offsetof(struct ice_aqc_sw_rules_elem, pdata.lg_act.act) + \
39 ((n) * sizeof(((struct ice_sw_rule_lg_act *)0)->act[0])))
40 #define ICE_SW_RULE_VSI_LIST_SIZE(n) \
41 (offsetof(struct ice_aqc_sw_rules_elem, pdata.vsi_list.vsi) + \
42 ((n) * sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi[0])))
45 * ice_init_def_sw_recp - initialize the recipe book keeping tables
46 * @hw: pointer to the HW struct
48 * Allocate memory for the entire recipe table and initialize the structures/
49 * entries corresponding to basic recipes.
51 enum ice_status ice_init_def_sw_recp(struct ice_hw *hw)
53 struct ice_sw_recipe *recps;
56 recps = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_NUM_RECIPES,
57 sizeof(*recps), GFP_KERNEL);
59 return ICE_ERR_NO_MEMORY;
61 for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
62 recps[i].root_rid = i;
63 INIT_LIST_HEAD(&recps[i].filt_rules);
64 INIT_LIST_HEAD(&recps[i].filt_replay_rules);
65 mutex_init(&recps[i].filt_rule_lock);
68 hw->switch_info->recp_list = recps;
74 * ice_aq_get_sw_cfg - get switch configuration
75 * @hw: pointer to the hardware structure
76 * @buf: pointer to the result buffer
77 * @buf_size: length of the buffer available for response
78 * @req_desc: pointer to requested descriptor
79 * @num_elems: pointer to number of elements
80 * @cd: pointer to command details structure or NULL
82 * Get switch configuration (0x0200) to be placed in buf.
83 * This admin command returns information such as initial VSI/port number
84 * and switch ID it belongs to.
86 * NOTE: *req_desc is both an input/output parameter.
87 * The caller of this function first calls this function with *request_desc set
88 * to 0. If the response from f/w has *req_desc set to 0, all the switch
89 * configuration information has been returned; if non-zero (meaning not all
90 * the information was returned), the caller should call this function again
91 * with *req_desc set to the previous value returned by f/w to get the
92 * next block of switch configuration information.
94 * *num_elems is output only parameter. This reflects the number of elements
95 * in response buffer. The caller of this function to use *num_elems while
96 * parsing the response buffer.
98 static enum ice_status
99 ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp_elem *buf,
100 u16 buf_size, u16 *req_desc, u16 *num_elems,
101 struct ice_sq_cd *cd)
103 struct ice_aqc_get_sw_cfg *cmd;
104 struct ice_aq_desc desc;
105 enum ice_status status;
107 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
108 cmd = &desc.params.get_sw_conf;
109 cmd->element = cpu_to_le16(*req_desc);
111 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
113 *req_desc = le16_to_cpu(cmd->element);
114 *num_elems = le16_to_cpu(cmd->num_elems);
122 * @hw: pointer to the HW struct
123 * @vsi_ctx: pointer to a VSI context struct
124 * @cd: pointer to command details structure or NULL
126 * Add a VSI context to the hardware (0x0210)
128 static enum ice_status
129 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
130 struct ice_sq_cd *cd)
132 struct ice_aqc_add_update_free_vsi_resp *res;
133 struct ice_aqc_add_get_update_free_vsi *cmd;
134 struct ice_aq_desc desc;
135 enum ice_status status;
137 cmd = &desc.params.vsi_cmd;
138 res = &desc.params.add_update_free_vsi_res;
140 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
142 if (!vsi_ctx->alloc_from_pool)
143 cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num |
144 ICE_AQ_VSI_IS_VALID);
145 cmd->vf_id = vsi_ctx->vf_num;
147 cmd->vsi_flags = cpu_to_le16(vsi_ctx->flags);
149 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
151 status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
152 sizeof(vsi_ctx->info), cd);
155 vsi_ctx->vsi_num = le16_to_cpu(res->vsi_num) & ICE_AQ_VSI_NUM_M;
156 vsi_ctx->vsis_allocd = le16_to_cpu(res->vsi_used);
157 vsi_ctx->vsis_unallocated = le16_to_cpu(res->vsi_free);
165 * @hw: pointer to the HW struct
166 * @vsi_ctx: pointer to a VSI context struct
167 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
168 * @cd: pointer to command details structure or NULL
170 * Free VSI context info from hardware (0x0213)
172 static enum ice_status
173 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
174 bool keep_vsi_alloc, struct ice_sq_cd *cd)
176 struct ice_aqc_add_update_free_vsi_resp *resp;
177 struct ice_aqc_add_get_update_free_vsi *cmd;
178 struct ice_aq_desc desc;
179 enum ice_status status;
181 cmd = &desc.params.vsi_cmd;
182 resp = &desc.params.add_update_free_vsi_res;
184 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
186 cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
188 cmd->cmd_flags = cpu_to_le16(ICE_AQ_VSI_KEEP_ALLOC);
190 status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
192 vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
193 vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
201 * @hw: pointer to the HW struct
202 * @vsi_ctx: pointer to a VSI context struct
203 * @cd: pointer to command details structure or NULL
205 * Update VSI context in the hardware (0x0211)
207 static enum ice_status
208 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
209 struct ice_sq_cd *cd)
211 struct ice_aqc_add_update_free_vsi_resp *resp;
212 struct ice_aqc_add_get_update_free_vsi *cmd;
213 struct ice_aq_desc desc;
214 enum ice_status status;
216 cmd = &desc.params.vsi_cmd;
217 resp = &desc.params.add_update_free_vsi_res;
219 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
221 cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
223 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
225 status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
226 sizeof(vsi_ctx->info), cd);
229 vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
230 vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
237 * ice_is_vsi_valid - check whether the VSI is valid or not
238 * @hw: pointer to the HW struct
239 * @vsi_handle: VSI handle
241 * check whether the VSI is valid or not
243 bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
245 return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
249 * ice_get_hw_vsi_num - return the HW VSI number
250 * @hw: pointer to the HW struct
251 * @vsi_handle: VSI handle
253 * return the HW VSI number
254 * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
256 u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
258 return hw->vsi_ctx[vsi_handle]->vsi_num;
262 * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
263 * @hw: pointer to the HW struct
264 * @vsi_handle: VSI handle
266 * return the VSI context entry for a given VSI handle
268 struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
270 return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
274 * ice_save_vsi_ctx - save the VSI context for a given VSI handle
275 * @hw: pointer to the HW struct
276 * @vsi_handle: VSI handle
277 * @vsi: VSI context pointer
279 * save the VSI context entry for a given VSI handle
282 ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi)
284 hw->vsi_ctx[vsi_handle] = vsi;
288 * ice_clear_vsi_q_ctx - clear VSI queue contexts for all TCs
289 * @hw: pointer to the HW struct
290 * @vsi_handle: VSI handle
292 static void ice_clear_vsi_q_ctx(struct ice_hw *hw, u16 vsi_handle)
294 struct ice_vsi_ctx *vsi;
297 vsi = ice_get_vsi_ctx(hw, vsi_handle);
300 ice_for_each_traffic_class(i) {
301 if (vsi->lan_q_ctx[i]) {
302 devm_kfree(ice_hw_to_dev(hw), vsi->lan_q_ctx[i]);
303 vsi->lan_q_ctx[i] = NULL;
309 * ice_clear_vsi_ctx - clear the VSI context entry
310 * @hw: pointer to the HW struct
311 * @vsi_handle: VSI handle
313 * clear the VSI context entry
315 static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
317 struct ice_vsi_ctx *vsi;
319 vsi = ice_get_vsi_ctx(hw, vsi_handle);
321 ice_clear_vsi_q_ctx(hw, vsi_handle);
322 devm_kfree(ice_hw_to_dev(hw), vsi);
323 hw->vsi_ctx[vsi_handle] = NULL;
328 * ice_clear_all_vsi_ctx - clear all the VSI context entries
329 * @hw: pointer to the HW struct
331 void ice_clear_all_vsi_ctx(struct ice_hw *hw)
335 for (i = 0; i < ICE_MAX_VSI; i++)
336 ice_clear_vsi_ctx(hw, i);
340 * ice_add_vsi - add VSI context to the hardware and VSI handle list
341 * @hw: pointer to the HW struct
342 * @vsi_handle: unique VSI handle provided by drivers
343 * @vsi_ctx: pointer to a VSI context struct
344 * @cd: pointer to command details structure or NULL
346 * Add a VSI context to the hardware also add it into the VSI handle list.
347 * If this function gets called after reset for existing VSIs then update
348 * with the new HW VSI number in the corresponding VSI handle list entry.
351 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
352 struct ice_sq_cd *cd)
354 struct ice_vsi_ctx *tmp_vsi_ctx;
355 enum ice_status status;
357 if (vsi_handle >= ICE_MAX_VSI)
358 return ICE_ERR_PARAM;
359 status = ice_aq_add_vsi(hw, vsi_ctx, cd);
362 tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
364 /* Create a new VSI context */
365 tmp_vsi_ctx = devm_kzalloc(ice_hw_to_dev(hw),
366 sizeof(*tmp_vsi_ctx), GFP_KERNEL);
368 ice_aq_free_vsi(hw, vsi_ctx, false, cd);
369 return ICE_ERR_NO_MEMORY;
371 *tmp_vsi_ctx = *vsi_ctx;
372 ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
374 /* update with new HW VSI num */
375 tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
382 * ice_free_vsi- free VSI context from hardware and VSI handle list
383 * @hw: pointer to the HW struct
384 * @vsi_handle: unique VSI handle
385 * @vsi_ctx: pointer to a VSI context struct
386 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
387 * @cd: pointer to command details structure or NULL
389 * Free VSI context info from hardware as well as from VSI handle list
392 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
393 bool keep_vsi_alloc, struct ice_sq_cd *cd)
395 enum ice_status status;
397 if (!ice_is_vsi_valid(hw, vsi_handle))
398 return ICE_ERR_PARAM;
399 vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
400 status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
402 ice_clear_vsi_ctx(hw, vsi_handle);
408 * @hw: pointer to the HW struct
409 * @vsi_handle: unique VSI handle
410 * @vsi_ctx: pointer to a VSI context struct
411 * @cd: pointer to command details structure or NULL
413 * Update VSI context in the hardware
416 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
417 struct ice_sq_cd *cd)
419 if (!ice_is_vsi_valid(hw, vsi_handle))
420 return ICE_ERR_PARAM;
421 vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
422 return ice_aq_update_vsi(hw, vsi_ctx, cd);
426 * ice_aq_alloc_free_vsi_list
427 * @hw: pointer to the HW struct
428 * @vsi_list_id: VSI list ID returned or used for lookup
429 * @lkup_type: switch rule filter lookup type
430 * @opc: switch rules population command type - pass in the command opcode
432 * allocates or free a VSI list resource
434 static enum ice_status
435 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
436 enum ice_sw_lkup_type lkup_type,
437 enum ice_adminq_opc opc)
439 struct ice_aqc_alloc_free_res_elem *sw_buf;
440 struct ice_aqc_res_elem *vsi_ele;
441 enum ice_status status;
444 buf_len = struct_size(sw_buf, elem, 1);
445 sw_buf = devm_kzalloc(ice_hw_to_dev(hw), buf_len, GFP_KERNEL);
447 return ICE_ERR_NO_MEMORY;
448 sw_buf->num_elems = cpu_to_le16(1);
450 if (lkup_type == ICE_SW_LKUP_MAC ||
451 lkup_type == ICE_SW_LKUP_MAC_VLAN ||
452 lkup_type == ICE_SW_LKUP_ETHERTYPE ||
453 lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
454 lkup_type == ICE_SW_LKUP_PROMISC ||
455 lkup_type == ICE_SW_LKUP_PROMISC_VLAN) {
456 sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
457 } else if (lkup_type == ICE_SW_LKUP_VLAN) {
459 cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
461 status = ICE_ERR_PARAM;
462 goto ice_aq_alloc_free_vsi_list_exit;
465 if (opc == ice_aqc_opc_free_res)
466 sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
468 status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
470 goto ice_aq_alloc_free_vsi_list_exit;
472 if (opc == ice_aqc_opc_alloc_res) {
473 vsi_ele = &sw_buf->elem[0];
474 *vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp);
477 ice_aq_alloc_free_vsi_list_exit:
478 devm_kfree(ice_hw_to_dev(hw), sw_buf);
483 * ice_aq_sw_rules - add/update/remove switch rules
484 * @hw: pointer to the HW struct
485 * @rule_list: pointer to switch rule population list
486 * @rule_list_sz: total size of the rule list in bytes
487 * @num_rules: number of switch rules in the rule_list
488 * @opc: switch rules population command type - pass in the command opcode
489 * @cd: pointer to command details structure or NULL
491 * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
493 static enum ice_status
494 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
495 u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
497 struct ice_aq_desc desc;
498 enum ice_status status;
500 if (opc != ice_aqc_opc_add_sw_rules &&
501 opc != ice_aqc_opc_update_sw_rules &&
502 opc != ice_aqc_opc_remove_sw_rules)
503 return ICE_ERR_PARAM;
505 ice_fill_dflt_direct_cmd_desc(&desc, opc);
507 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
508 desc.params.sw_rules.num_rules_fltr_entry_index =
509 cpu_to_le16(num_rules);
510 status = ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
511 if (opc != ice_aqc_opc_add_sw_rules &&
512 hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT)
513 status = ICE_ERR_DOES_NOT_EXIST;
518 /* ice_init_port_info - Initialize port_info with switch configuration data
519 * @pi: pointer to port_info
520 * @vsi_port_num: VSI number or port number
521 * @type: Type of switch element (port or VSI)
522 * @swid: switch ID of the switch the element is attached to
523 * @pf_vf_num: PF or VF number
524 * @is_vf: true if the element is a VF, false otherwise
527 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
528 u16 swid, u16 pf_vf_num, bool is_vf)
531 case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
532 pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
534 pi->pf_vf_num = pf_vf_num;
536 pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
537 pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
540 ice_debug(pi->hw, ICE_DBG_SW,
541 "incorrect VSI/port type received\n");
546 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
547 * @hw: pointer to the hardware structure
549 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
551 struct ice_aqc_get_sw_cfg_resp_elem *rbuf;
552 enum ice_status status;
557 rbuf = devm_kzalloc(ice_hw_to_dev(hw), ICE_SW_CFG_MAX_BUF_LEN,
561 return ICE_ERR_NO_MEMORY;
563 /* Multiple calls to ice_aq_get_sw_cfg may be required
564 * to get all the switch configuration information. The need
565 * for additional calls is indicated by ice_aq_get_sw_cfg
566 * writing a non-zero value in req_desc
569 struct ice_aqc_get_sw_cfg_resp_elem *ele;
571 status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
572 &req_desc, &num_elems, NULL);
577 for (i = 0, ele = rbuf; i < num_elems; i++, ele++) {
578 u16 pf_vf_num, swid, vsi_port_num;
582 vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
583 ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
585 pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
586 ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
588 swid = le16_to_cpu(ele->swid);
590 if (le16_to_cpu(ele->pf_vf_num) &
591 ICE_AQC_GET_SW_CONF_RESP_IS_VF)
594 res_type = (u8)(le16_to_cpu(ele->vsi_port_num) >>
595 ICE_AQC_GET_SW_CONF_RESP_TYPE_S);
597 if (res_type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
598 /* FW VSI is not needed. Just continue. */
602 ice_init_port_info(hw->port_info, vsi_port_num,
603 res_type, swid, pf_vf_num, is_vf);
605 } while (req_desc && !status);
607 devm_kfree(ice_hw_to_dev(hw), (void *)rbuf);
612 * ice_fill_sw_info - Helper function to populate lb_en and lan_en
613 * @hw: pointer to the hardware structure
614 * @fi: filter info structure to fill/update
616 * This helper function populates the lb_en and lan_en elements of the provided
617 * ice_fltr_info struct using the switch's type and characteristics of the
618 * switch rule being configured.
620 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
624 if ((fi->flag & ICE_FLTR_TX) &&
625 (fi->fltr_act == ICE_FWD_TO_VSI ||
626 fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
627 fi->fltr_act == ICE_FWD_TO_Q ||
628 fi->fltr_act == ICE_FWD_TO_QGRP)) {
629 /* Setting LB for prune actions will result in replicated
630 * packets to the internal switch that will be dropped.
632 if (fi->lkup_type != ICE_SW_LKUP_VLAN)
635 /* Set lan_en to TRUE if
636 * 1. The switch is a VEB AND
638 * 2.1 The lookup is a directional lookup like ethertype,
639 * promiscuous, ethertype-MAC, promiscuous-VLAN
640 * and default-port OR
641 * 2.2 The lookup is VLAN, OR
642 * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
643 * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
647 * The switch is a VEPA.
649 * In all other cases, the LAN enable has to be set to false.
652 if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
653 fi->lkup_type == ICE_SW_LKUP_PROMISC ||
654 fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
655 fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
656 fi->lkup_type == ICE_SW_LKUP_DFLT ||
657 fi->lkup_type == ICE_SW_LKUP_VLAN ||
658 (fi->lkup_type == ICE_SW_LKUP_MAC &&
659 !is_unicast_ether_addr(fi->l_data.mac.mac_addr)) ||
660 (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
661 !is_unicast_ether_addr(fi->l_data.mac.mac_addr)))
670 * ice_fill_sw_rule - Helper function to fill switch rule structure
671 * @hw: pointer to the hardware structure
672 * @f_info: entry containing packet forwarding information
673 * @s_rule: switch rule structure to be filled in based on mac_entry
674 * @opc: switch rules population command type - pass in the command opcode
677 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
678 struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
680 u16 vlan_id = ICE_MAX_VLAN_ID + 1;
688 if (opc == ice_aqc_opc_remove_sw_rules) {
689 s_rule->pdata.lkup_tx_rx.act = 0;
690 s_rule->pdata.lkup_tx_rx.index =
691 cpu_to_le16(f_info->fltr_rule_id);
692 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
696 eth_hdr_sz = sizeof(dummy_eth_header);
697 eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
699 /* initialize the ether header with a dummy header */
700 memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz);
701 ice_fill_sw_info(hw, f_info);
703 switch (f_info->fltr_act) {
705 act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
706 ICE_SINGLE_ACT_VSI_ID_M;
707 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
708 act |= ICE_SINGLE_ACT_VSI_FORWARDING |
709 ICE_SINGLE_ACT_VALID_BIT;
711 case ICE_FWD_TO_VSI_LIST:
712 act |= ICE_SINGLE_ACT_VSI_LIST;
713 act |= (f_info->fwd_id.vsi_list_id <<
714 ICE_SINGLE_ACT_VSI_LIST_ID_S) &
715 ICE_SINGLE_ACT_VSI_LIST_ID_M;
716 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
717 act |= ICE_SINGLE_ACT_VSI_FORWARDING |
718 ICE_SINGLE_ACT_VALID_BIT;
721 act |= ICE_SINGLE_ACT_TO_Q;
722 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
723 ICE_SINGLE_ACT_Q_INDEX_M;
725 case ICE_DROP_PACKET:
726 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
727 ICE_SINGLE_ACT_VALID_BIT;
729 case ICE_FWD_TO_QGRP:
730 q_rgn = f_info->qgrp_size > 0 ?
731 (u8)ilog2(f_info->qgrp_size) : 0;
732 act |= ICE_SINGLE_ACT_TO_Q;
733 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
734 ICE_SINGLE_ACT_Q_INDEX_M;
735 act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
736 ICE_SINGLE_ACT_Q_REGION_M;
743 act |= ICE_SINGLE_ACT_LB_ENABLE;
745 act |= ICE_SINGLE_ACT_LAN_ENABLE;
747 switch (f_info->lkup_type) {
748 case ICE_SW_LKUP_MAC:
749 daddr = f_info->l_data.mac.mac_addr;
751 case ICE_SW_LKUP_VLAN:
752 vlan_id = f_info->l_data.vlan.vlan_id;
753 if (f_info->fltr_act == ICE_FWD_TO_VSI ||
754 f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
755 act |= ICE_SINGLE_ACT_PRUNE;
756 act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
759 case ICE_SW_LKUP_ETHERTYPE_MAC:
760 daddr = f_info->l_data.ethertype_mac.mac_addr;
762 case ICE_SW_LKUP_ETHERTYPE:
763 off = (__force __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
764 *off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
766 case ICE_SW_LKUP_MAC_VLAN:
767 daddr = f_info->l_data.mac_vlan.mac_addr;
768 vlan_id = f_info->l_data.mac_vlan.vlan_id;
770 case ICE_SW_LKUP_PROMISC_VLAN:
771 vlan_id = f_info->l_data.mac_vlan.vlan_id;
773 case ICE_SW_LKUP_PROMISC:
774 daddr = f_info->l_data.mac_vlan.mac_addr;
780 s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
781 cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
782 cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
784 /* Recipe set depending on lookup type */
785 s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(f_info->lkup_type);
786 s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(f_info->src);
787 s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);
790 ether_addr_copy(eth_hdr + ICE_ETH_DA_OFFSET, daddr);
792 if (!(vlan_id > ICE_MAX_VLAN_ID)) {
793 off = (__force __be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
794 *off = cpu_to_be16(vlan_id);
797 /* Create the switch rule with the final dummy Ethernet header */
798 if (opc != ice_aqc_opc_update_sw_rules)
799 s_rule->pdata.lkup_tx_rx.hdr_len = cpu_to_le16(eth_hdr_sz);
804 * @hw: pointer to the hardware structure
805 * @m_ent: the management entry for which sw marker needs to be added
806 * @sw_marker: sw marker to tag the Rx descriptor with
807 * @l_id: large action resource ID
809 * Create a large action to hold software marker and update the switch rule
810 * entry pointed by m_ent with newly created large action
812 static enum ice_status
813 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
814 u16 sw_marker, u16 l_id)
816 struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
817 /* For software marker we need 3 large actions
818 * 1. FWD action: FWD TO VSI or VSI LIST
819 * 2. GENERIC VALUE action to hold the profile ID
820 * 3. GENERIC VALUE action to hold the software marker ID
822 const u16 num_lg_acts = 3;
823 enum ice_status status;
829 if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
830 return ICE_ERR_PARAM;
832 /* Create two back-to-back switch rules and submit them to the HW using
837 lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
838 rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
839 lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
841 return ICE_ERR_NO_MEMORY;
843 rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
845 /* Fill in the first switch rule i.e. large action */
846 lg_act->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
847 lg_act->pdata.lg_act.index = cpu_to_le16(l_id);
848 lg_act->pdata.lg_act.size = cpu_to_le16(num_lg_acts);
850 /* First action VSI forwarding or VSI list forwarding depending on how
853 id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
854 m_ent->fltr_info.fwd_id.hw_vsi_id;
856 act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
857 act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) & ICE_LG_ACT_VSI_LIST_ID_M;
858 if (m_ent->vsi_count > 1)
859 act |= ICE_LG_ACT_VSI_LIST;
860 lg_act->pdata.lg_act.act[0] = cpu_to_le32(act);
862 /* Second action descriptor type */
863 act = ICE_LG_ACT_GENERIC;
865 act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
866 lg_act->pdata.lg_act.act[1] = cpu_to_le32(act);
868 act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
869 ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
871 /* Third action Marker value */
872 act |= ICE_LG_ACT_GENERIC;
873 act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
874 ICE_LG_ACT_GENERIC_VALUE_M;
876 lg_act->pdata.lg_act.act[2] = cpu_to_le32(act);
878 /* call the fill switch rule to fill the lookup Tx Rx structure */
879 ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
880 ice_aqc_opc_update_sw_rules);
882 /* Update the action to point to the large action ID */
883 rx_tx->pdata.lkup_tx_rx.act =
884 cpu_to_le32(ICE_SINGLE_ACT_PTR |
885 ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
886 ICE_SINGLE_ACT_PTR_VAL_M));
888 /* Use the filter rule ID of the previously created rule with single
889 * act. Once the update happens, hardware will treat this as large
892 rx_tx->pdata.lkup_tx_rx.index =
893 cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
895 status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
896 ice_aqc_opc_update_sw_rules, NULL);
898 m_ent->lg_act_idx = l_id;
899 m_ent->sw_marker_id = sw_marker;
902 devm_kfree(ice_hw_to_dev(hw), lg_act);
907 * ice_create_vsi_list_map
908 * @hw: pointer to the hardware structure
909 * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
910 * @num_vsi: number of VSI handles in the array
911 * @vsi_list_id: VSI list ID generated as part of allocate resource
913 * Helper function to create a new entry of VSI list ID to VSI mapping
914 * using the given VSI list ID
916 static struct ice_vsi_list_map_info *
917 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
920 struct ice_switch_info *sw = hw->switch_info;
921 struct ice_vsi_list_map_info *v_map;
924 v_map = devm_kcalloc(ice_hw_to_dev(hw), 1, sizeof(*v_map), GFP_KERNEL);
928 v_map->vsi_list_id = vsi_list_id;
930 for (i = 0; i < num_vsi; i++)
931 set_bit(vsi_handle_arr[i], v_map->vsi_map);
933 list_add(&v_map->list_entry, &sw->vsi_list_map_head);
938 * ice_update_vsi_list_rule
939 * @hw: pointer to the hardware structure
940 * @vsi_handle_arr: array of VSI handles to form a VSI list
941 * @num_vsi: number of VSI handles in the array
942 * @vsi_list_id: VSI list ID generated as part of allocate resource
943 * @remove: Boolean value to indicate if this is a remove action
944 * @opc: switch rules population command type - pass in the command opcode
945 * @lkup_type: lookup type of the filter
947 * Call AQ command to add a new switch rule or update existing switch rule
948 * using the given VSI list ID
950 static enum ice_status
951 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
952 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
953 enum ice_sw_lkup_type lkup_type)
955 struct ice_aqc_sw_rules_elem *s_rule;
956 enum ice_status status;
962 return ICE_ERR_PARAM;
964 if (lkup_type == ICE_SW_LKUP_MAC ||
965 lkup_type == ICE_SW_LKUP_MAC_VLAN ||
966 lkup_type == ICE_SW_LKUP_ETHERTYPE ||
967 lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
968 lkup_type == ICE_SW_LKUP_PROMISC ||
969 lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
970 rule_type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
971 ICE_AQC_SW_RULES_T_VSI_LIST_SET;
972 else if (lkup_type == ICE_SW_LKUP_VLAN)
973 rule_type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
974 ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
976 return ICE_ERR_PARAM;
978 s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
979 s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
981 return ICE_ERR_NO_MEMORY;
982 for (i = 0; i < num_vsi; i++) {
983 if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
984 status = ICE_ERR_PARAM;
987 /* AQ call requires hw_vsi_id(s) */
988 s_rule->pdata.vsi_list.vsi[i] =
989 cpu_to_le16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
992 s_rule->type = cpu_to_le16(rule_type);
993 s_rule->pdata.vsi_list.number_vsi = cpu_to_le16(num_vsi);
994 s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
996 status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
999 devm_kfree(ice_hw_to_dev(hw), s_rule);
1004 * ice_create_vsi_list_rule - Creates and populates a VSI list rule
1005 * @hw: pointer to the HW struct
1006 * @vsi_handle_arr: array of VSI handles to form a VSI list
1007 * @num_vsi: number of VSI handles in the array
1008 * @vsi_list_id: stores the ID of the VSI list to be created
1009 * @lkup_type: switch rule filter's lookup type
1011 static enum ice_status
1012 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1013 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1015 enum ice_status status;
1017 status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1018 ice_aqc_opc_alloc_res);
1022 /* Update the newly created VSI list to include the specified VSIs */
1023 return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1024 *vsi_list_id, false,
1025 ice_aqc_opc_add_sw_rules, lkup_type);
1029 * ice_create_pkt_fwd_rule
1030 * @hw: pointer to the hardware structure
1031 * @f_entry: entry containing packet forwarding information
1033 * Create switch rule with given filter information and add an entry
1034 * to the corresponding filter management list to track this switch rule
1037 static enum ice_status
1038 ice_create_pkt_fwd_rule(struct ice_hw *hw,
1039 struct ice_fltr_list_entry *f_entry)
1041 struct ice_fltr_mgmt_list_entry *fm_entry;
1042 struct ice_aqc_sw_rules_elem *s_rule;
1043 enum ice_sw_lkup_type l_type;
1044 struct ice_sw_recipe *recp;
1045 enum ice_status status;
1047 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1048 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1050 return ICE_ERR_NO_MEMORY;
1051 fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
1054 status = ICE_ERR_NO_MEMORY;
1055 goto ice_create_pkt_fwd_rule_exit;
1058 fm_entry->fltr_info = f_entry->fltr_info;
1060 /* Initialize all the fields for the management entry */
1061 fm_entry->vsi_count = 1;
1062 fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1063 fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1064 fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1066 ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1067 ice_aqc_opc_add_sw_rules);
1069 status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1070 ice_aqc_opc_add_sw_rules, NULL);
1072 devm_kfree(ice_hw_to_dev(hw), fm_entry);
1073 goto ice_create_pkt_fwd_rule_exit;
1076 f_entry->fltr_info.fltr_rule_id =
1077 le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1078 fm_entry->fltr_info.fltr_rule_id =
1079 le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1081 /* The book keeping entries will get removed when base driver
1082 * calls remove filter AQ command
1084 l_type = fm_entry->fltr_info.lkup_type;
1085 recp = &hw->switch_info->recp_list[l_type];
1086 list_add(&fm_entry->list_entry, &recp->filt_rules);
1088 ice_create_pkt_fwd_rule_exit:
1089 devm_kfree(ice_hw_to_dev(hw), s_rule);
1094 * ice_update_pkt_fwd_rule
1095 * @hw: pointer to the hardware structure
1096 * @f_info: filter information for switch rule
1098 * Call AQ command to update a previously created switch rule with a
1101 static enum ice_status
1102 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1104 struct ice_aqc_sw_rules_elem *s_rule;
1105 enum ice_status status;
1107 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1108 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1110 return ICE_ERR_NO_MEMORY;
1112 ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
1114 s_rule->pdata.lkup_tx_rx.index = cpu_to_le16(f_info->fltr_rule_id);
1116 /* Update switch rule with new rule set to forward VSI list */
1117 status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1118 ice_aqc_opc_update_sw_rules, NULL);
1120 devm_kfree(ice_hw_to_dev(hw), s_rule);
1125 * ice_update_sw_rule_bridge_mode
1126 * @hw: pointer to the HW struct
1128 * Updates unicast switch filter rules based on VEB/VEPA mode
1130 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
1132 struct ice_switch_info *sw = hw->switch_info;
1133 struct ice_fltr_mgmt_list_entry *fm_entry;
1134 enum ice_status status = 0;
1135 struct list_head *rule_head;
1136 struct mutex *rule_lock; /* Lock to protect filter rule list */
1138 rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1139 rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1141 mutex_lock(rule_lock);
1142 list_for_each_entry(fm_entry, rule_head, list_entry) {
1143 struct ice_fltr_info *fi = &fm_entry->fltr_info;
1144 u8 *addr = fi->l_data.mac.mac_addr;
1146 /* Update unicast Tx rules to reflect the selected
1149 if ((fi->flag & ICE_FLTR_TX) && is_unicast_ether_addr(addr) &&
1150 (fi->fltr_act == ICE_FWD_TO_VSI ||
1151 fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1152 fi->fltr_act == ICE_FWD_TO_Q ||
1153 fi->fltr_act == ICE_FWD_TO_QGRP)) {
1154 status = ice_update_pkt_fwd_rule(hw, fi);
1160 mutex_unlock(rule_lock);
1166 * ice_add_update_vsi_list
1167 * @hw: pointer to the hardware structure
1168 * @m_entry: pointer to current filter management list entry
1169 * @cur_fltr: filter information from the book keeping entry
1170 * @new_fltr: filter information with the new VSI to be added
1172 * Call AQ command to add or update previously created VSI list with new VSI.
1174 * Helper function to do book keeping associated with adding filter information
1175 * The algorithm to do the book keeping is described below :
1176 * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
1177 * if only one VSI has been added till now
1178 * Allocate a new VSI list and add two VSIs
1179 * to this list using switch rule command
1180 * Update the previously created switch rule with the
1181 * newly created VSI list ID
1182 * if a VSI list was previously created
1183 * Add the new VSI to the previously created VSI list set
1184 * using the update switch rule command
1186 static enum ice_status
1187 ice_add_update_vsi_list(struct ice_hw *hw,
1188 struct ice_fltr_mgmt_list_entry *m_entry,
1189 struct ice_fltr_info *cur_fltr,
1190 struct ice_fltr_info *new_fltr)
1192 enum ice_status status = 0;
1193 u16 vsi_list_id = 0;
1195 if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
1196 cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
1197 return ICE_ERR_NOT_IMPL;
1199 if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
1200 new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
1201 (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
1202 cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
1203 return ICE_ERR_NOT_IMPL;
1205 if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
1206 /* Only one entry existed in the mapping and it was not already
1207 * a part of a VSI list. So, create a VSI list with the old and
1210 struct ice_fltr_info tmp_fltr;
1211 u16 vsi_handle_arr[2];
1213 /* A rule already exists with the new VSI being added */
1214 if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
1215 return ICE_ERR_ALREADY_EXISTS;
1217 vsi_handle_arr[0] = cur_fltr->vsi_handle;
1218 vsi_handle_arr[1] = new_fltr->vsi_handle;
1219 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1221 new_fltr->lkup_type);
1225 tmp_fltr = *new_fltr;
1226 tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
1227 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1228 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1229 /* Update the previous switch rule of "MAC forward to VSI" to
1230 * "MAC fwd to VSI list"
1232 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1236 cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1237 cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1238 m_entry->vsi_list_info =
1239 ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1242 if (!m_entry->vsi_list_info)
1243 return ICE_ERR_NO_MEMORY;
1245 /* If this entry was large action then the large action needs
1246 * to be updated to point to FWD to VSI list
1248 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1250 ice_add_marker_act(hw, m_entry,
1251 m_entry->sw_marker_id,
1252 m_entry->lg_act_idx);
1254 u16 vsi_handle = new_fltr->vsi_handle;
1255 enum ice_adminq_opc opcode;
1257 if (!m_entry->vsi_list_info)
1260 /* A rule already exists with the new VSI being added */
1261 if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
1264 /* Update the previously created VSI list set with
1265 * the new VSI ID passed in
1267 vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1268 opcode = ice_aqc_opc_update_sw_rules;
1270 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
1271 vsi_list_id, false, opcode,
1272 new_fltr->lkup_type);
1273 /* update VSI list mapping info with new VSI ID */
1275 set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
1278 m_entry->vsi_count++;
1283 * ice_find_rule_entry - Search a rule entry
1284 * @hw: pointer to the hardware structure
1285 * @recp_id: lookup type for which the specified rule needs to be searched
1286 * @f_info: rule information
1288 * Helper function to search for a given rule entry
1289 * Returns pointer to entry storing the rule if found
1291 static struct ice_fltr_mgmt_list_entry *
1292 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
1294 struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
1295 struct ice_switch_info *sw = hw->switch_info;
1296 struct list_head *list_head;
1298 list_head = &sw->recp_list[recp_id].filt_rules;
1299 list_for_each_entry(list_itr, list_head, list_entry) {
1300 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
1301 sizeof(f_info->l_data)) &&
1302 f_info->flag == list_itr->fltr_info.flag) {
1311 * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
1312 * @hw: pointer to the hardware structure
1313 * @recp_id: lookup type for which VSI lists needs to be searched
1314 * @vsi_handle: VSI handle to be found in VSI list
1315 * @vsi_list_id: VSI list ID found containing vsi_handle
1317 * Helper function to search a VSI list with single entry containing given VSI
1318 * handle element. This can be extended further to search VSI list with more
1319 * than 1 vsi_count. Returns pointer to VSI list entry if found.
1321 static struct ice_vsi_list_map_info *
1322 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
1325 struct ice_vsi_list_map_info *map_info = NULL;
1326 struct ice_switch_info *sw = hw->switch_info;
1327 struct ice_fltr_mgmt_list_entry *list_itr;
1328 struct list_head *list_head;
1330 list_head = &sw->recp_list[recp_id].filt_rules;
1331 list_for_each_entry(list_itr, list_head, list_entry) {
1332 if (list_itr->vsi_count == 1 && list_itr->vsi_list_info) {
1333 map_info = list_itr->vsi_list_info;
1334 if (test_bit(vsi_handle, map_info->vsi_map)) {
1335 *vsi_list_id = map_info->vsi_list_id;
1344 * ice_add_rule_internal - add rule for a given lookup type
1345 * @hw: pointer to the hardware structure
1346 * @recp_id: lookup type (recipe ID) for which rule has to be added
1347 * @f_entry: structure containing MAC forwarding information
1349 * Adds or updates the rule lists for a given recipe
1351 static enum ice_status
1352 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
1353 struct ice_fltr_list_entry *f_entry)
1355 struct ice_switch_info *sw = hw->switch_info;
1356 struct ice_fltr_info *new_fltr, *cur_fltr;
1357 struct ice_fltr_mgmt_list_entry *m_entry;
1358 struct mutex *rule_lock; /* Lock to protect filter rule list */
1359 enum ice_status status = 0;
1361 if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1362 return ICE_ERR_PARAM;
1363 f_entry->fltr_info.fwd_id.hw_vsi_id =
1364 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1366 rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1368 mutex_lock(rule_lock);
1369 new_fltr = &f_entry->fltr_info;
1370 if (new_fltr->flag & ICE_FLTR_RX)
1371 new_fltr->src = hw->port_info->lport;
1372 else if (new_fltr->flag & ICE_FLTR_TX)
1373 new_fltr->src = f_entry->fltr_info.fwd_id.hw_vsi_id;
1375 m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
1377 mutex_unlock(rule_lock);
1378 return ice_create_pkt_fwd_rule(hw, f_entry);
1381 cur_fltr = &m_entry->fltr_info;
1382 status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
1383 mutex_unlock(rule_lock);
1389 * ice_remove_vsi_list_rule
1390 * @hw: pointer to the hardware structure
1391 * @vsi_list_id: VSI list ID generated as part of allocate resource
1392 * @lkup_type: switch rule filter lookup type
1394 * The VSI list should be emptied before this function is called to remove the
1397 static enum ice_status
1398 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1399 enum ice_sw_lkup_type lkup_type)
1401 struct ice_aqc_sw_rules_elem *s_rule;
1402 enum ice_status status;
1405 s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
1406 s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1408 return ICE_ERR_NO_MEMORY;
1410 s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
1411 s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
1413 /* Free the vsi_list resource that we allocated. It is assumed that the
1414 * list is empty at this point.
1416 status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
1417 ice_aqc_opc_free_res);
1419 devm_kfree(ice_hw_to_dev(hw), s_rule);
1424 * ice_rem_update_vsi_list
1425 * @hw: pointer to the hardware structure
1426 * @vsi_handle: VSI handle of the VSI to remove
1427 * @fm_list: filter management entry for which the VSI list management needs to
1430 static enum ice_status
1431 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
1432 struct ice_fltr_mgmt_list_entry *fm_list)
1434 enum ice_sw_lkup_type lkup_type;
1435 enum ice_status status = 0;
1438 if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
1439 fm_list->vsi_count == 0)
1440 return ICE_ERR_PARAM;
1442 /* A rule with the VSI being removed does not exist */
1443 if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
1444 return ICE_ERR_DOES_NOT_EXIST;
1446 lkup_type = fm_list->fltr_info.lkup_type;
1447 vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
1448 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
1449 ice_aqc_opc_update_sw_rules,
1454 fm_list->vsi_count--;
1455 clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
1457 if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
1458 struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
1459 struct ice_vsi_list_map_info *vsi_list_info =
1460 fm_list->vsi_list_info;
1463 rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
1465 if (!ice_is_vsi_valid(hw, rem_vsi_handle))
1466 return ICE_ERR_OUT_OF_RANGE;
1468 /* Make sure VSI list is empty before removing it below */
1469 status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
1471 ice_aqc_opc_update_sw_rules,
1476 tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
1477 tmp_fltr_info.fwd_id.hw_vsi_id =
1478 ice_get_hw_vsi_num(hw, rem_vsi_handle);
1479 tmp_fltr_info.vsi_handle = rem_vsi_handle;
1480 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
1482 ice_debug(hw, ICE_DBG_SW,
1483 "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
1484 tmp_fltr_info.fwd_id.hw_vsi_id, status);
1488 fm_list->fltr_info = tmp_fltr_info;
1491 if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
1492 (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
1493 struct ice_vsi_list_map_info *vsi_list_info =
1494 fm_list->vsi_list_info;
1496 /* Remove the VSI list since it is no longer used */
1497 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
1499 ice_debug(hw, ICE_DBG_SW,
1500 "Failed to remove VSI list %d, error %d\n",
1501 vsi_list_id, status);
1505 list_del(&vsi_list_info->list_entry);
1506 devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
1507 fm_list->vsi_list_info = NULL;
1514 * ice_remove_rule_internal - Remove a filter rule of a given type
1515 * @hw: pointer to the hardware structure
1516 * @recp_id: recipe ID for which the rule needs to removed
1517 * @f_entry: rule entry containing filter information
1519 static enum ice_status
1520 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
1521 struct ice_fltr_list_entry *f_entry)
1523 struct ice_switch_info *sw = hw->switch_info;
1524 struct ice_fltr_mgmt_list_entry *list_elem;
1525 struct mutex *rule_lock; /* Lock to protect filter rule list */
1526 enum ice_status status = 0;
1527 bool remove_rule = false;
1530 if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1531 return ICE_ERR_PARAM;
1532 f_entry->fltr_info.fwd_id.hw_vsi_id =
1533 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1535 rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1536 mutex_lock(rule_lock);
1537 list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
1539 status = ICE_ERR_DOES_NOT_EXIST;
1543 if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
1545 } else if (!list_elem->vsi_list_info) {
1546 status = ICE_ERR_DOES_NOT_EXIST;
1548 } else if (list_elem->vsi_list_info->ref_cnt > 1) {
1549 /* a ref_cnt > 1 indicates that the vsi_list is being
1550 * shared by multiple rules. Decrement the ref_cnt and
1551 * remove this rule, but do not modify the list, as it
1552 * is in-use by other rules.
1554 list_elem->vsi_list_info->ref_cnt--;
1557 /* a ref_cnt of 1 indicates the vsi_list is only used
1558 * by one rule. However, the original removal request is only
1559 * for a single VSI. Update the vsi_list first, and only
1560 * remove the rule if there are no further VSIs in this list.
1562 vsi_handle = f_entry->fltr_info.vsi_handle;
1563 status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
1566 /* if VSI count goes to zero after updating the VSI list */
1567 if (list_elem->vsi_count == 0)
1572 /* Remove the lookup rule */
1573 struct ice_aqc_sw_rules_elem *s_rule;
1575 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1576 ICE_SW_RULE_RX_TX_NO_HDR_SIZE,
1579 status = ICE_ERR_NO_MEMORY;
1583 ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
1584 ice_aqc_opc_remove_sw_rules);
1586 status = ice_aq_sw_rules(hw, s_rule,
1587 ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
1588 ice_aqc_opc_remove_sw_rules, NULL);
1590 /* Remove a book keeping from the list */
1591 devm_kfree(ice_hw_to_dev(hw), s_rule);
1596 list_del(&list_elem->list_entry);
1597 devm_kfree(ice_hw_to_dev(hw), list_elem);
1600 mutex_unlock(rule_lock);
1605 * ice_add_mac - Add a MAC address based filter rule
1606 * @hw: pointer to the hardware structure
1607 * @m_list: list of MAC addresses and forwarding information
1609 * IMPORTANT: When the ucast_shared flag is set to false and m_list has
1610 * multiple unicast addresses, the function assumes that all the
1611 * addresses are unique in a given add_mac call. It doesn't
1612 * check for duplicates in this case, removing duplicates from a given
1613 * list should be taken care of in the caller of this function.
1615 enum ice_status ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
1617 struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
1618 struct ice_fltr_list_entry *m_list_itr;
1619 struct list_head *rule_head;
1620 u16 total_elem_left, s_rule_size;
1621 struct ice_switch_info *sw;
1622 struct mutex *rule_lock; /* Lock to protect filter rule list */
1623 enum ice_status status = 0;
1624 u16 num_unicast = 0;
1628 return ICE_ERR_PARAM;
1631 sw = hw->switch_info;
1632 rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1633 list_for_each_entry(m_list_itr, m_list, list_entry) {
1634 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
1638 m_list_itr->fltr_info.flag = ICE_FLTR_TX;
1639 vsi_handle = m_list_itr->fltr_info.vsi_handle;
1640 if (!ice_is_vsi_valid(hw, vsi_handle))
1641 return ICE_ERR_PARAM;
1642 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
1643 m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
1644 /* update the src in case it is VSI num */
1645 if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
1646 return ICE_ERR_PARAM;
1647 m_list_itr->fltr_info.src = hw_vsi_id;
1648 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
1649 is_zero_ether_addr(add))
1650 return ICE_ERR_PARAM;
1651 if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
1652 /* Don't overwrite the unicast address */
1653 mutex_lock(rule_lock);
1654 if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
1655 &m_list_itr->fltr_info)) {
1656 mutex_unlock(rule_lock);
1657 return ICE_ERR_ALREADY_EXISTS;
1659 mutex_unlock(rule_lock);
1661 } else if (is_multicast_ether_addr(add) ||
1662 (is_unicast_ether_addr(add) && hw->ucast_shared)) {
1663 m_list_itr->status =
1664 ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
1666 if (m_list_itr->status)
1667 return m_list_itr->status;
1671 mutex_lock(rule_lock);
1672 /* Exit if no suitable entries were found for adding bulk switch rule */
1675 goto ice_add_mac_exit;
1678 rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1680 /* Allocate switch rule buffer for the bulk update for unicast */
1681 s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1682 s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
1685 status = ICE_ERR_NO_MEMORY;
1686 goto ice_add_mac_exit;
1690 list_for_each_entry(m_list_itr, m_list, list_entry) {
1691 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1692 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1694 if (is_unicast_ether_addr(mac_addr)) {
1695 ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
1696 ice_aqc_opc_add_sw_rules);
1697 r_iter = (struct ice_aqc_sw_rules_elem *)
1698 ((u8 *)r_iter + s_rule_size);
1702 /* Call AQ bulk switch rule update for all unicast addresses */
1704 /* Call AQ switch rule in AQ_MAX chunk */
1705 for (total_elem_left = num_unicast; total_elem_left > 0;
1706 total_elem_left -= elem_sent) {
1707 struct ice_aqc_sw_rules_elem *entry = r_iter;
1709 elem_sent = min_t(u8, total_elem_left,
1710 (ICE_AQ_MAX_BUF_LEN / s_rule_size));
1711 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
1712 elem_sent, ice_aqc_opc_add_sw_rules,
1715 goto ice_add_mac_exit;
1716 r_iter = (struct ice_aqc_sw_rules_elem *)
1717 ((u8 *)r_iter + (elem_sent * s_rule_size));
1720 /* Fill up rule ID based on the value returned from FW */
1722 list_for_each_entry(m_list_itr, m_list, list_entry) {
1723 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1724 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1725 struct ice_fltr_mgmt_list_entry *fm_entry;
1727 if (is_unicast_ether_addr(mac_addr)) {
1728 f_info->fltr_rule_id =
1729 le16_to_cpu(r_iter->pdata.lkup_tx_rx.index);
1730 f_info->fltr_act = ICE_FWD_TO_VSI;
1731 /* Create an entry to track this MAC address */
1732 fm_entry = devm_kzalloc(ice_hw_to_dev(hw),
1733 sizeof(*fm_entry), GFP_KERNEL);
1735 status = ICE_ERR_NO_MEMORY;
1736 goto ice_add_mac_exit;
1738 fm_entry->fltr_info = *f_info;
1739 fm_entry->vsi_count = 1;
1740 /* The book keeping entries will get removed when
1741 * base driver calls remove filter AQ command
1744 list_add(&fm_entry->list_entry, rule_head);
1745 r_iter = (struct ice_aqc_sw_rules_elem *)
1746 ((u8 *)r_iter + s_rule_size);
1751 mutex_unlock(rule_lock);
1753 devm_kfree(ice_hw_to_dev(hw), s_rule);
1758 * ice_add_vlan_internal - Add one VLAN based filter rule
1759 * @hw: pointer to the hardware structure
1760 * @f_entry: filter entry containing one VLAN information
1762 static enum ice_status
1763 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1765 struct ice_switch_info *sw = hw->switch_info;
1766 struct ice_fltr_mgmt_list_entry *v_list_itr;
1767 struct ice_fltr_info *new_fltr, *cur_fltr;
1768 enum ice_sw_lkup_type lkup_type;
1769 u16 vsi_list_id = 0, vsi_handle;
1770 struct mutex *rule_lock; /* Lock to protect filter rule list */
1771 enum ice_status status = 0;
1773 if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1774 return ICE_ERR_PARAM;
1776 f_entry->fltr_info.fwd_id.hw_vsi_id =
1777 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1778 new_fltr = &f_entry->fltr_info;
1780 /* VLAN ID should only be 12 bits */
1781 if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
1782 return ICE_ERR_PARAM;
1784 if (new_fltr->src_id != ICE_SRC_ID_VSI)
1785 return ICE_ERR_PARAM;
1787 new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
1788 lkup_type = new_fltr->lkup_type;
1789 vsi_handle = new_fltr->vsi_handle;
1790 rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
1791 mutex_lock(rule_lock);
1792 v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
1794 struct ice_vsi_list_map_info *map_info = NULL;
1796 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
1797 /* All VLAN pruning rules use a VSI list. Check if
1798 * there is already a VSI list containing VSI that we
1799 * want to add. If found, use the same vsi_list_id for
1800 * this new VLAN rule or else create a new list.
1802 map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
1806 status = ice_create_vsi_list_rule(hw,
1814 /* Convert the action to forwarding to a VSI list. */
1815 new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1816 new_fltr->fwd_id.vsi_list_id = vsi_list_id;
1819 status = ice_create_pkt_fwd_rule(hw, f_entry);
1821 v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
1824 status = ICE_ERR_DOES_NOT_EXIST;
1827 /* reuse VSI list for new rule and increment ref_cnt */
1829 v_list_itr->vsi_list_info = map_info;
1830 map_info->ref_cnt++;
1832 v_list_itr->vsi_list_info =
1833 ice_create_vsi_list_map(hw, &vsi_handle,
1837 } else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
1838 /* Update existing VSI list to add new VSI ID only if it used
1841 cur_fltr = &v_list_itr->fltr_info;
1842 status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
1845 /* If VLAN rule exists and VSI list being used by this rule is
1846 * referenced by more than 1 VLAN rule. Then create a new VSI
1847 * list appending previous VSI with new VSI and update existing
1848 * VLAN rule to point to new VSI list ID
1850 struct ice_fltr_info tmp_fltr;
1851 u16 vsi_handle_arr[2];
1854 /* Current implementation only supports reusing VSI list with
1855 * one VSI count. We should never hit below condition
1857 if (v_list_itr->vsi_count > 1 &&
1858 v_list_itr->vsi_list_info->ref_cnt > 1) {
1859 ice_debug(hw, ICE_DBG_SW,
1860 "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
1861 status = ICE_ERR_CFG;
1866 find_first_bit(v_list_itr->vsi_list_info->vsi_map,
1869 /* A rule already exists with the new VSI being added */
1870 if (cur_handle == vsi_handle) {
1871 status = ICE_ERR_ALREADY_EXISTS;
1875 vsi_handle_arr[0] = cur_handle;
1876 vsi_handle_arr[1] = vsi_handle;
1877 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1878 &vsi_list_id, lkup_type);
1882 tmp_fltr = v_list_itr->fltr_info;
1883 tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
1884 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1885 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1886 /* Update the previous switch rule to a new VSI list which
1887 * includes current VSI that is requested
1889 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1893 /* before overriding VSI list map info. decrement ref_cnt of
1896 v_list_itr->vsi_list_info->ref_cnt--;
1898 /* now update to newly created list */
1899 v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
1900 v_list_itr->vsi_list_info =
1901 ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1903 v_list_itr->vsi_count++;
1907 mutex_unlock(rule_lock);
1912 * ice_add_vlan - Add VLAN based filter rule
1913 * @hw: pointer to the hardware structure
1914 * @v_list: list of VLAN entries and forwarding information
1916 enum ice_status ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
1918 struct ice_fltr_list_entry *v_list_itr;
1921 return ICE_ERR_PARAM;
1923 list_for_each_entry(v_list_itr, v_list, list_entry) {
1924 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
1925 return ICE_ERR_PARAM;
1926 v_list_itr->fltr_info.flag = ICE_FLTR_TX;
1927 v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
1928 if (v_list_itr->status)
1929 return v_list_itr->status;
1935 * ice_add_eth_mac - Add ethertype and MAC based filter rule
1936 * @hw: pointer to the hardware structure
1937 * @em_list: list of ether type MAC filter, MAC is optional
1939 * This function requires the caller to populate the entries in
1940 * the filter list with the necessary fields (including flags to
1941 * indicate Tx or Rx rules).
1944 ice_add_eth_mac(struct ice_hw *hw, struct list_head *em_list)
1946 struct ice_fltr_list_entry *em_list_itr;
1948 if (!em_list || !hw)
1949 return ICE_ERR_PARAM;
1951 list_for_each_entry(em_list_itr, em_list, list_entry) {
1952 enum ice_sw_lkup_type l_type =
1953 em_list_itr->fltr_info.lkup_type;
1955 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
1956 l_type != ICE_SW_LKUP_ETHERTYPE)
1957 return ICE_ERR_PARAM;
1959 em_list_itr->status = ice_add_rule_internal(hw, l_type,
1961 if (em_list_itr->status)
1962 return em_list_itr->status;
1968 * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
1969 * @hw: pointer to the hardware structure
1970 * @em_list: list of ethertype or ethertype MAC entries
1973 ice_remove_eth_mac(struct ice_hw *hw, struct list_head *em_list)
1975 struct ice_fltr_list_entry *em_list_itr, *tmp;
1977 if (!em_list || !hw)
1978 return ICE_ERR_PARAM;
1980 list_for_each_entry_safe(em_list_itr, tmp, em_list, list_entry) {
1981 enum ice_sw_lkup_type l_type =
1982 em_list_itr->fltr_info.lkup_type;
1984 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
1985 l_type != ICE_SW_LKUP_ETHERTYPE)
1986 return ICE_ERR_PARAM;
1988 em_list_itr->status = ice_remove_rule_internal(hw, l_type,
1990 if (em_list_itr->status)
1991 return em_list_itr->status;
1997 * ice_rem_sw_rule_info
1998 * @hw: pointer to the hardware structure
1999 * @rule_head: pointer to the switch list structure that we want to delete
2002 ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
2004 if (!list_empty(rule_head)) {
2005 struct ice_fltr_mgmt_list_entry *entry;
2006 struct ice_fltr_mgmt_list_entry *tmp;
2008 list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
2009 list_del(&entry->list_entry);
2010 devm_kfree(ice_hw_to_dev(hw), entry);
2016 * ice_cfg_dflt_vsi - change state of VSI to set/clear default
2017 * @hw: pointer to the hardware structure
2018 * @vsi_handle: VSI handle to set as default
2019 * @set: true to add the above mentioned switch rule, false to remove it
2020 * @direction: ICE_FLTR_RX or ICE_FLTR_TX
2022 * add filter rule to set/unset given VSI as default VSI for the switch
2023 * (represented by swid)
2026 ice_cfg_dflt_vsi(struct ice_hw *hw, u16 vsi_handle, bool set, u8 direction)
2028 struct ice_aqc_sw_rules_elem *s_rule;
2029 struct ice_fltr_info f_info;
2030 enum ice_adminq_opc opcode;
2031 enum ice_status status;
2035 if (!ice_is_vsi_valid(hw, vsi_handle))
2036 return ICE_ERR_PARAM;
2037 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2039 s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
2040 ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
2042 s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
2044 return ICE_ERR_NO_MEMORY;
2046 memset(&f_info, 0, sizeof(f_info));
2048 f_info.lkup_type = ICE_SW_LKUP_DFLT;
2049 f_info.flag = direction;
2050 f_info.fltr_act = ICE_FWD_TO_VSI;
2051 f_info.fwd_id.hw_vsi_id = hw_vsi_id;
2053 if (f_info.flag & ICE_FLTR_RX) {
2054 f_info.src = hw->port_info->lport;
2055 f_info.src_id = ICE_SRC_ID_LPORT;
2057 f_info.fltr_rule_id =
2058 hw->port_info->dflt_rx_vsi_rule_id;
2059 } else if (f_info.flag & ICE_FLTR_TX) {
2060 f_info.src_id = ICE_SRC_ID_VSI;
2061 f_info.src = hw_vsi_id;
2063 f_info.fltr_rule_id =
2064 hw->port_info->dflt_tx_vsi_rule_id;
2068 opcode = ice_aqc_opc_add_sw_rules;
2070 opcode = ice_aqc_opc_remove_sw_rules;
2072 ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
2074 status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
2075 if (status || !(f_info.flag & ICE_FLTR_TX_RX))
2078 u16 index = le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
2080 if (f_info.flag & ICE_FLTR_TX) {
2081 hw->port_info->dflt_tx_vsi_num = hw_vsi_id;
2082 hw->port_info->dflt_tx_vsi_rule_id = index;
2083 } else if (f_info.flag & ICE_FLTR_RX) {
2084 hw->port_info->dflt_rx_vsi_num = hw_vsi_id;
2085 hw->port_info->dflt_rx_vsi_rule_id = index;
2088 if (f_info.flag & ICE_FLTR_TX) {
2089 hw->port_info->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
2090 hw->port_info->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
2091 } else if (f_info.flag & ICE_FLTR_RX) {
2092 hw->port_info->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
2093 hw->port_info->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
2098 devm_kfree(ice_hw_to_dev(hw), s_rule);
2103 * ice_find_ucast_rule_entry - Search for a unicast MAC filter rule entry
2104 * @hw: pointer to the hardware structure
2105 * @recp_id: lookup type for which the specified rule needs to be searched
2106 * @f_info: rule information
2108 * Helper function to search for a unicast rule entry - this is to be used
2109 * to remove unicast MAC filter that is not shared with other VSIs on the
2112 * Returns pointer to entry storing the rule if found
2114 static struct ice_fltr_mgmt_list_entry *
2115 ice_find_ucast_rule_entry(struct ice_hw *hw, u8 recp_id,
2116 struct ice_fltr_info *f_info)
2118 struct ice_switch_info *sw = hw->switch_info;
2119 struct ice_fltr_mgmt_list_entry *list_itr;
2120 struct list_head *list_head;
2122 list_head = &sw->recp_list[recp_id].filt_rules;
2123 list_for_each_entry(list_itr, list_head, list_entry) {
2124 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
2125 sizeof(f_info->l_data)) &&
2126 f_info->fwd_id.hw_vsi_id ==
2127 list_itr->fltr_info.fwd_id.hw_vsi_id &&
2128 f_info->flag == list_itr->fltr_info.flag)
2135 * ice_remove_mac - remove a MAC address based filter rule
2136 * @hw: pointer to the hardware structure
2137 * @m_list: list of MAC addresses and forwarding information
2139 * This function removes either a MAC filter rule or a specific VSI from a
2140 * VSI list for a multicast MAC address.
2142 * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
2143 * ice_add_mac. Caller should be aware that this call will only work if all
2144 * the entries passed into m_list were added previously. It will not attempt to
2145 * do a partial remove of entries that were found.
2147 enum ice_status ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
2149 struct ice_fltr_list_entry *list_itr, *tmp;
2150 struct mutex *rule_lock; /* Lock to protect filter rule list */
2153 return ICE_ERR_PARAM;
2155 rule_lock = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2156 list_for_each_entry_safe(list_itr, tmp, m_list, list_entry) {
2157 enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2158 u8 *add = &list_itr->fltr_info.l_data.mac.mac_addr[0];
2161 if (l_type != ICE_SW_LKUP_MAC)
2162 return ICE_ERR_PARAM;
2164 vsi_handle = list_itr->fltr_info.vsi_handle;
2165 if (!ice_is_vsi_valid(hw, vsi_handle))
2166 return ICE_ERR_PARAM;
2168 list_itr->fltr_info.fwd_id.hw_vsi_id =
2169 ice_get_hw_vsi_num(hw, vsi_handle);
2170 if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
2171 /* Don't remove the unicast address that belongs to
2172 * another VSI on the switch, since it is not being
2175 mutex_lock(rule_lock);
2176 if (!ice_find_ucast_rule_entry(hw, ICE_SW_LKUP_MAC,
2177 &list_itr->fltr_info)) {
2178 mutex_unlock(rule_lock);
2179 return ICE_ERR_DOES_NOT_EXIST;
2181 mutex_unlock(rule_lock);
2183 list_itr->status = ice_remove_rule_internal(hw,
2186 if (list_itr->status)
2187 return list_itr->status;
2193 * ice_remove_vlan - Remove VLAN based filter rule
2194 * @hw: pointer to the hardware structure
2195 * @v_list: list of VLAN entries and forwarding information
2198 ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
2200 struct ice_fltr_list_entry *v_list_itr, *tmp;
2203 return ICE_ERR_PARAM;
2205 list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
2206 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2208 if (l_type != ICE_SW_LKUP_VLAN)
2209 return ICE_ERR_PARAM;
2210 v_list_itr->status = ice_remove_rule_internal(hw,
2213 if (v_list_itr->status)
2214 return v_list_itr->status;
2220 * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
2221 * @fm_entry: filter entry to inspect
2222 * @vsi_handle: VSI handle to compare with filter info
2225 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
2227 return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
2228 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
2229 (fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
2230 fm_entry->vsi_list_info &&
2231 (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
2235 * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
2236 * @hw: pointer to the hardware structure
2237 * @vsi_handle: VSI handle to remove filters from
2238 * @vsi_list_head: pointer to the list to add entry to
2239 * @fi: pointer to fltr_info of filter entry to copy & add
2241 * Helper function, used when creating a list of filters to remove from
2242 * a specific VSI. The entry added to vsi_list_head is a COPY of the
2243 * original filter entry, with the exception of fltr_info.fltr_act and
2244 * fltr_info.fwd_id fields. These are set such that later logic can
2245 * extract which VSI to remove the fltr from, and pass on that information.
2247 static enum ice_status
2248 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2249 struct list_head *vsi_list_head,
2250 struct ice_fltr_info *fi)
2252 struct ice_fltr_list_entry *tmp;
2254 /* this memory is freed up in the caller function
2255 * once filters for this VSI are removed
2257 tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
2259 return ICE_ERR_NO_MEMORY;
2261 tmp->fltr_info = *fi;
2263 /* Overwrite these fields to indicate which VSI to remove filter from,
2264 * so find and remove logic can extract the information from the
2265 * list entries. Note that original entries will still have proper
2268 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2269 tmp->fltr_info.vsi_handle = vsi_handle;
2270 tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2272 list_add(&tmp->list_entry, vsi_list_head);
2278 * ice_add_to_vsi_fltr_list - Add VSI filters to the list
2279 * @hw: pointer to the hardware structure
2280 * @vsi_handle: VSI handle to remove filters from
2281 * @lkup_list_head: pointer to the list that has certain lookup type filters
2282 * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
2284 * Locates all filters in lkup_list_head that are used by the given VSI,
2285 * and adds COPIES of those entries to vsi_list_head (intended to be used
2286 * to remove the listed filters).
2287 * Note that this means all entries in vsi_list_head must be explicitly
2288 * deallocated by the caller when done with list.
2290 static enum ice_status
2291 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2292 struct list_head *lkup_list_head,
2293 struct list_head *vsi_list_head)
2295 struct ice_fltr_mgmt_list_entry *fm_entry;
2296 enum ice_status status = 0;
2298 /* check to make sure VSI ID is valid and within boundary */
2299 if (!ice_is_vsi_valid(hw, vsi_handle))
2300 return ICE_ERR_PARAM;
2302 list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
2303 if (!ice_vsi_uses_fltr(fm_entry, vsi_handle))
2306 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2308 &fm_entry->fltr_info);
2316 * ice_determine_promisc_mask
2317 * @fi: filter info to parse
2319 * Helper function to determine which ICE_PROMISC_ mask corresponds
2320 * to given filter into.
2322 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
2324 u16 vid = fi->l_data.mac_vlan.vlan_id;
2325 u8 *macaddr = fi->l_data.mac.mac_addr;
2326 bool is_tx_fltr = false;
2327 u8 promisc_mask = 0;
2329 if (fi->flag == ICE_FLTR_TX)
2332 if (is_broadcast_ether_addr(macaddr))
2333 promisc_mask |= is_tx_fltr ?
2334 ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
2335 else if (is_multicast_ether_addr(macaddr))
2336 promisc_mask |= is_tx_fltr ?
2337 ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
2338 else if (is_unicast_ether_addr(macaddr))
2339 promisc_mask |= is_tx_fltr ?
2340 ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
2342 promisc_mask |= is_tx_fltr ?
2343 ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
2345 return promisc_mask;
2349 * ice_remove_promisc - Remove promisc based filter rules
2350 * @hw: pointer to the hardware structure
2351 * @recp_id: recipe ID for which the rule needs to removed
2352 * @v_list: list of promisc entries
2354 static enum ice_status
2355 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
2356 struct list_head *v_list)
2358 struct ice_fltr_list_entry *v_list_itr, *tmp;
2360 list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
2361 v_list_itr->status =
2362 ice_remove_rule_internal(hw, recp_id, v_list_itr);
2363 if (v_list_itr->status)
2364 return v_list_itr->status;
2370 * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
2371 * @hw: pointer to the hardware structure
2372 * @vsi_handle: VSI handle to clear mode
2373 * @promisc_mask: mask of promiscuous config bits to clear
2374 * @vid: VLAN ID to clear VLAN promiscuous
2377 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
2380 struct ice_switch_info *sw = hw->switch_info;
2381 struct ice_fltr_list_entry *fm_entry, *tmp;
2382 struct list_head remove_list_head;
2383 struct ice_fltr_mgmt_list_entry *itr;
2384 struct list_head *rule_head;
2385 struct mutex *rule_lock; /* Lock to protect filter rule list */
2386 enum ice_status status = 0;
2389 if (!ice_is_vsi_valid(hw, vsi_handle))
2390 return ICE_ERR_PARAM;
2392 if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX))
2393 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
2395 recipe_id = ICE_SW_LKUP_PROMISC;
2397 rule_head = &sw->recp_list[recipe_id].filt_rules;
2398 rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
2400 INIT_LIST_HEAD(&remove_list_head);
2402 mutex_lock(rule_lock);
2403 list_for_each_entry(itr, rule_head, list_entry) {
2404 struct ice_fltr_info *fltr_info;
2405 u8 fltr_promisc_mask = 0;
2407 if (!ice_vsi_uses_fltr(itr, vsi_handle))
2409 fltr_info = &itr->fltr_info;
2411 if (recipe_id == ICE_SW_LKUP_PROMISC_VLAN &&
2412 vid != fltr_info->l_data.mac_vlan.vlan_id)
2415 fltr_promisc_mask |= ice_determine_promisc_mask(fltr_info);
2417 /* Skip if filter is not completely specified by given mask */
2418 if (fltr_promisc_mask & ~promisc_mask)
2421 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2425 mutex_unlock(rule_lock);
2426 goto free_fltr_list;
2429 mutex_unlock(rule_lock);
2431 status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
2434 list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
2435 list_del(&fm_entry->list_entry);
2436 devm_kfree(ice_hw_to_dev(hw), fm_entry);
2443 * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
2444 * @hw: pointer to the hardware structure
2445 * @vsi_handle: VSI handle to configure
2446 * @promisc_mask: mask of promiscuous config bits
2447 * @vid: VLAN ID to set VLAN promiscuous
2450 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
2452 enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
2453 struct ice_fltr_list_entry f_list_entry;
2454 struct ice_fltr_info new_fltr;
2455 enum ice_status status = 0;
2461 if (!ice_is_vsi_valid(hw, vsi_handle))
2462 return ICE_ERR_PARAM;
2463 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2465 memset(&new_fltr, 0, sizeof(new_fltr));
2467 if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
2468 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
2469 new_fltr.l_data.mac_vlan.vlan_id = vid;
2470 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
2472 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
2473 recipe_id = ICE_SW_LKUP_PROMISC;
2476 /* Separate filters must be set for each direction/packet type
2477 * combination, so we will loop over the mask value, store the
2478 * individual type, and clear it out in the input mask as it
2481 while (promisc_mask) {
2487 if (promisc_mask & ICE_PROMISC_UCAST_RX) {
2488 promisc_mask &= ~ICE_PROMISC_UCAST_RX;
2489 pkt_type = UCAST_FLTR;
2490 } else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
2491 promisc_mask &= ~ICE_PROMISC_UCAST_TX;
2492 pkt_type = UCAST_FLTR;
2494 } else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
2495 promisc_mask &= ~ICE_PROMISC_MCAST_RX;
2496 pkt_type = MCAST_FLTR;
2497 } else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
2498 promisc_mask &= ~ICE_PROMISC_MCAST_TX;
2499 pkt_type = MCAST_FLTR;
2501 } else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
2502 promisc_mask &= ~ICE_PROMISC_BCAST_RX;
2503 pkt_type = BCAST_FLTR;
2504 } else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
2505 promisc_mask &= ~ICE_PROMISC_BCAST_TX;
2506 pkt_type = BCAST_FLTR;
2510 /* Check for VLAN promiscuous flag */
2511 if (promisc_mask & ICE_PROMISC_VLAN_RX) {
2512 promisc_mask &= ~ICE_PROMISC_VLAN_RX;
2513 } else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
2514 promisc_mask &= ~ICE_PROMISC_VLAN_TX;
2518 /* Set filter DA based on packet type */
2519 mac_addr = new_fltr.l_data.mac.mac_addr;
2520 if (pkt_type == BCAST_FLTR) {
2521 eth_broadcast_addr(mac_addr);
2522 } else if (pkt_type == MCAST_FLTR ||
2523 pkt_type == UCAST_FLTR) {
2524 /* Use the dummy ether header DA */
2525 ether_addr_copy(mac_addr, dummy_eth_header);
2526 if (pkt_type == MCAST_FLTR)
2527 mac_addr[0] |= 0x1; /* Set multicast bit */
2530 /* Need to reset this to zero for all iterations */
2533 new_fltr.flag |= ICE_FLTR_TX;
2534 new_fltr.src = hw_vsi_id;
2536 new_fltr.flag |= ICE_FLTR_RX;
2537 new_fltr.src = hw->port_info->lport;
2540 new_fltr.fltr_act = ICE_FWD_TO_VSI;
2541 new_fltr.vsi_handle = vsi_handle;
2542 new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
2543 f_list_entry.fltr_info = new_fltr;
2545 status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
2547 goto set_promisc_exit;
2555 * ice_set_vlan_vsi_promisc
2556 * @hw: pointer to the hardware structure
2557 * @vsi_handle: VSI handle to configure
2558 * @promisc_mask: mask of promiscuous config bits
2559 * @rm_vlan_promisc: Clear VLANs VSI promisc mode
2561 * Configure VSI with all associated VLANs to given promiscuous mode(s)
2564 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
2565 bool rm_vlan_promisc)
2567 struct ice_switch_info *sw = hw->switch_info;
2568 struct ice_fltr_list_entry *list_itr, *tmp;
2569 struct list_head vsi_list_head;
2570 struct list_head *vlan_head;
2571 struct mutex *vlan_lock; /* Lock to protect filter rule list */
2572 enum ice_status status;
2575 INIT_LIST_HEAD(&vsi_list_head);
2576 vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2577 vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
2578 mutex_lock(vlan_lock);
2579 status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
2581 mutex_unlock(vlan_lock);
2583 goto free_fltr_list;
2585 list_for_each_entry(list_itr, &vsi_list_head, list_entry) {
2586 vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
2587 if (rm_vlan_promisc)
2588 status = ice_clear_vsi_promisc(hw, vsi_handle,
2589 promisc_mask, vlan_id);
2591 status = ice_set_vsi_promisc(hw, vsi_handle,
2592 promisc_mask, vlan_id);
2593 if (status && status != -EEXIST)
2598 list_for_each_entry_safe(list_itr, tmp, &vsi_list_head, list_entry) {
2599 list_del(&list_itr->list_entry);
2600 devm_kfree(ice_hw_to_dev(hw), list_itr);
2606 * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
2607 * @hw: pointer to the hardware structure
2608 * @vsi_handle: VSI handle to remove filters from
2609 * @lkup: switch rule filter lookup type
2612 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
2613 enum ice_sw_lkup_type lkup)
2615 struct ice_switch_info *sw = hw->switch_info;
2616 struct ice_fltr_list_entry *fm_entry;
2617 struct list_head remove_list_head;
2618 struct list_head *rule_head;
2619 struct ice_fltr_list_entry *tmp;
2620 struct mutex *rule_lock; /* Lock to protect filter rule list */
2621 enum ice_status status;
2623 INIT_LIST_HEAD(&remove_list_head);
2624 rule_lock = &sw->recp_list[lkup].filt_rule_lock;
2625 rule_head = &sw->recp_list[lkup].filt_rules;
2626 mutex_lock(rule_lock);
2627 status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
2629 mutex_unlock(rule_lock);
2631 goto free_fltr_list;
2634 case ICE_SW_LKUP_MAC:
2635 ice_remove_mac(hw, &remove_list_head);
2637 case ICE_SW_LKUP_VLAN:
2638 ice_remove_vlan(hw, &remove_list_head);
2640 case ICE_SW_LKUP_PROMISC:
2641 case ICE_SW_LKUP_PROMISC_VLAN:
2642 ice_remove_promisc(hw, lkup, &remove_list_head);
2644 case ICE_SW_LKUP_MAC_VLAN:
2645 case ICE_SW_LKUP_ETHERTYPE:
2646 case ICE_SW_LKUP_ETHERTYPE_MAC:
2647 case ICE_SW_LKUP_DFLT:
2648 case ICE_SW_LKUP_LAST:
2650 ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
2655 list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
2656 list_del(&fm_entry->list_entry);
2657 devm_kfree(ice_hw_to_dev(hw), fm_entry);
2662 * ice_remove_vsi_fltr - Remove all filters for a VSI
2663 * @hw: pointer to the hardware structure
2664 * @vsi_handle: VSI handle to remove filters from
2666 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
2668 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
2669 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
2670 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
2671 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
2672 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
2673 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
2674 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
2675 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
2679 * ice_alloc_res_cntr - allocating resource counter
2680 * @hw: pointer to the hardware structure
2681 * @type: type of resource
2682 * @alloc_shared: if set it is shared else dedicated
2683 * @num_items: number of entries requested for FD resource type
2684 * @counter_id: counter index returned by AQ call
2687 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
2690 struct ice_aqc_alloc_free_res_elem *buf;
2691 enum ice_status status;
2694 /* Allocate resource */
2695 buf_len = struct_size(buf, elem, 1);
2696 buf = kzalloc(buf_len, GFP_KERNEL);
2698 return ICE_ERR_NO_MEMORY;
2700 buf->num_elems = cpu_to_le16(num_items);
2701 buf->res_type = cpu_to_le16(((type << ICE_AQC_RES_TYPE_S) &
2702 ICE_AQC_RES_TYPE_M) | alloc_shared);
2704 status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
2705 ice_aqc_opc_alloc_res, NULL);
2709 *counter_id = le16_to_cpu(buf->elem[0].e.sw_resp);
2717 * ice_free_res_cntr - free resource counter
2718 * @hw: pointer to the hardware structure
2719 * @type: type of resource
2720 * @alloc_shared: if set it is shared else dedicated
2721 * @num_items: number of entries to be freed for FD resource type
2722 * @counter_id: counter ID resource which needs to be freed
2725 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
2728 struct ice_aqc_alloc_free_res_elem *buf;
2729 enum ice_status status;
2733 buf_len = struct_size(buf, elem, 1);
2734 buf = kzalloc(buf_len, GFP_KERNEL);
2736 return ICE_ERR_NO_MEMORY;
2738 buf->num_elems = cpu_to_le16(num_items);
2739 buf->res_type = cpu_to_le16(((type << ICE_AQC_RES_TYPE_S) &
2740 ICE_AQC_RES_TYPE_M) | alloc_shared);
2741 buf->elem[0].e.sw_resp = cpu_to_le16(counter_id);
2743 status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
2744 ice_aqc_opc_free_res, NULL);
2746 ice_debug(hw, ICE_DBG_SW,
2747 "counter resource could not be freed\n");
2754 * ice_replay_vsi_fltr - Replay filters for requested VSI
2755 * @hw: pointer to the hardware structure
2756 * @vsi_handle: driver VSI handle
2757 * @recp_id: Recipe ID for which rules need to be replayed
2758 * @list_head: list for which filters need to be replayed
2760 * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
2761 * It is required to pass valid VSI handle.
2763 static enum ice_status
2764 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
2765 struct list_head *list_head)
2767 struct ice_fltr_mgmt_list_entry *itr;
2768 enum ice_status status = 0;
2771 if (list_empty(list_head))
2773 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2775 list_for_each_entry(itr, list_head, list_entry) {
2776 struct ice_fltr_list_entry f_entry;
2778 f_entry.fltr_info = itr->fltr_info;
2779 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
2780 itr->fltr_info.vsi_handle == vsi_handle) {
2781 /* update the src in case it is VSI num */
2782 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2783 f_entry.fltr_info.src = hw_vsi_id;
2784 status = ice_add_rule_internal(hw, recp_id, &f_entry);
2789 if (!itr->vsi_list_info ||
2790 !test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
2792 /* Clearing it so that the logic can add it back */
2793 clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
2794 f_entry.fltr_info.vsi_handle = vsi_handle;
2795 f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
2796 /* update the src in case it is VSI num */
2797 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2798 f_entry.fltr_info.src = hw_vsi_id;
2799 if (recp_id == ICE_SW_LKUP_VLAN)
2800 status = ice_add_vlan_internal(hw, &f_entry);
2802 status = ice_add_rule_internal(hw, recp_id, &f_entry);
2811 * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
2812 * @hw: pointer to the hardware structure
2813 * @vsi_handle: driver VSI handle
2815 * Replays filters for requested VSI via vsi_handle.
2817 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
2819 struct ice_switch_info *sw = hw->switch_info;
2820 enum ice_status status = 0;
2823 for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2824 struct list_head *head;
2826 head = &sw->recp_list[i].filt_replay_rules;
2827 status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
2835 * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
2836 * @hw: pointer to the HW struct
2838 * Deletes the filter replay rules.
2840 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
2842 struct ice_switch_info *sw = hw->switch_info;
2848 for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2849 if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
2850 struct list_head *l_head;
2852 l_head = &sw->recp_list[i].filt_replay_rules;
2853 ice_rem_sw_rule_info(hw, l_head);