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 (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)))
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
61 * Helper function to allocate/free resources using the admin queue commands
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)
68 struct ice_aqc_alloc_free_res_cmd *cmd;
69 struct ice_aq_desc desc;
71 cmd = &desc.params.sw_res_ctrl;
76 if (buf_size < (num_entries * sizeof(buf->elem[0])))
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);
89 * ice_init_def_sw_recp - initialize the recipe book keeping tables
90 * @hw: pointer to the hw struct
92 * Allocate memory for the entire recipe table and initialize the structures/
93 * entries corresponding to basic recipes.
96 ice_init_def_sw_recp(struct ice_hw *hw)
98 struct ice_sw_recipe *recps;
101 recps = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_NUM_RECIPES,
102 sizeof(struct ice_sw_recipe), GFP_KERNEL);
104 return ICE_ERR_NO_MEMORY;
106 for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
107 recps[i].root_rid = i;
108 INIT_LIST_HEAD(&recps[i].filt_rules);
109 INIT_LIST_HEAD(&recps[i].filt_replay_rules);
110 mutex_init(&recps[i].filt_rule_lock);
113 hw->switch_info->recp_list = recps;
119 * ice_aq_get_sw_cfg - get switch configuration
120 * @hw: pointer to the hardware structure
121 * @buf: pointer to the result buffer
122 * @buf_size: length of the buffer available for response
123 * @req_desc: pointer to requested descriptor
124 * @num_elems: pointer to number of elements
125 * @cd: pointer to command details structure or NULL
127 * Get switch configuration (0x0200) to be placed in 'buff'.
128 * This admin command returns information such as initial VSI/port number
129 * and switch ID it belongs to.
131 * NOTE: *req_desc is both an input/output parameter.
132 * The caller of this function first calls this function with *request_desc set
133 * to 0. If the response from f/w has *req_desc set to 0, all the switch
134 * configuration information has been returned; if non-zero (meaning not all
135 * the information was returned), the caller should call this function again
136 * with *req_desc set to the previous value returned by f/w to get the
137 * next block of switch configuration information.
139 * *num_elems is output only parameter. This reflects the number of elements
140 * in response buffer. The caller of this function to use *num_elems while
141 * parsing the response buffer.
143 static enum ice_status
144 ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp *buf,
145 u16 buf_size, u16 *req_desc, u16 *num_elems,
146 struct ice_sq_cd *cd)
148 struct ice_aqc_get_sw_cfg *cmd;
149 enum ice_status status;
150 struct ice_aq_desc desc;
152 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
153 cmd = &desc.params.get_sw_conf;
154 cmd->element = cpu_to_le16(*req_desc);
156 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
158 *req_desc = le16_to_cpu(cmd->element);
159 *num_elems = le16_to_cpu(cmd->num_elems);
167 * @hw: pointer to the hw struct
168 * @vsi_ctx: pointer to a VSI context struct
169 * @cd: pointer to command details structure or NULL
171 * Add a VSI context to the hardware (0x0210)
173 static enum ice_status
174 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
175 struct ice_sq_cd *cd)
177 struct ice_aqc_add_update_free_vsi_resp *res;
178 struct ice_aqc_add_get_update_free_vsi *cmd;
179 struct ice_aq_desc desc;
180 enum ice_status status;
182 cmd = &desc.params.vsi_cmd;
183 res = &desc.params.add_update_free_vsi_res;
185 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
187 if (!vsi_ctx->alloc_from_pool)
188 cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num |
189 ICE_AQ_VSI_IS_VALID);
190 cmd->vf_id = vsi_ctx->vf_num;
192 cmd->vsi_flags = cpu_to_le16(vsi_ctx->flags);
194 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
196 status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
197 sizeof(vsi_ctx->info), cd);
200 vsi_ctx->vsi_num = le16_to_cpu(res->vsi_num) & ICE_AQ_VSI_NUM_M;
201 vsi_ctx->vsis_allocd = le16_to_cpu(res->vsi_used);
202 vsi_ctx->vsis_unallocated = le16_to_cpu(res->vsi_free);
210 * @hw: pointer to the hw struct
211 * @vsi_ctx: pointer to a VSI context struct
212 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
213 * @cd: pointer to command details structure or NULL
215 * Free VSI context info from hardware (0x0213)
217 static enum ice_status
218 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
219 bool keep_vsi_alloc, struct ice_sq_cd *cd)
221 struct ice_aqc_add_update_free_vsi_resp *resp;
222 struct ice_aqc_add_get_update_free_vsi *cmd;
223 struct ice_aq_desc desc;
224 enum ice_status status;
226 cmd = &desc.params.vsi_cmd;
227 resp = &desc.params.add_update_free_vsi_res;
229 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
231 cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
233 cmd->cmd_flags = cpu_to_le16(ICE_AQ_VSI_KEEP_ALLOC);
235 status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
237 vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
238 vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
246 * @hw: pointer to the hw struct
247 * @vsi_ctx: pointer to a VSI context struct
248 * @cd: pointer to command details structure or NULL
250 * Update VSI context in the hardware (0x0211)
252 static enum ice_status
253 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
254 struct ice_sq_cd *cd)
256 struct ice_aqc_add_update_free_vsi_resp *resp;
257 struct ice_aqc_add_get_update_free_vsi *cmd;
258 struct ice_aq_desc desc;
259 enum ice_status status;
261 cmd = &desc.params.vsi_cmd;
262 resp = &desc.params.add_update_free_vsi_res;
264 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
266 cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
268 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
270 status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
271 sizeof(vsi_ctx->info), cd);
274 vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
275 vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
282 * ice_is_vsi_valid - check whether the VSI is valid or not
283 * @hw: pointer to the hw struct
284 * @vsi_handle: VSI handle
286 * check whether the VSI is valid or not
288 bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
290 return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
294 * ice_get_hw_vsi_num - return the hw VSI number
295 * @hw: pointer to the hw struct
296 * @vsi_handle: VSI handle
298 * return the hw VSI number
299 * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
301 u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
303 return hw->vsi_ctx[vsi_handle]->vsi_num;
307 * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
308 * @hw: pointer to the hw struct
309 * @vsi_handle: VSI handle
311 * return the VSI context entry for a given VSI handle
313 struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
315 return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
319 * ice_save_vsi_ctx - save the VSI context for a given VSI handle
320 * @hw: pointer to the hw struct
321 * @vsi_handle: VSI handle
322 * @vsi: VSI context pointer
324 * save the VSI context entry for a given VSI handle
326 static void ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle,
327 struct ice_vsi_ctx *vsi)
329 hw->vsi_ctx[vsi_handle] = vsi;
333 * ice_clear_vsi_ctx - clear the VSI context entry
334 * @hw: pointer to the hw struct
335 * @vsi_handle: VSI handle
337 * clear the VSI context entry
339 static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
341 struct ice_vsi_ctx *vsi;
343 vsi = ice_get_vsi_ctx(hw, vsi_handle);
345 devm_kfree(ice_hw_to_dev(hw), vsi);
346 hw->vsi_ctx[vsi_handle] = NULL;
351 * ice_add_vsi - add VSI context to the hardware and VSI handle list
352 * @hw: pointer to the hw struct
353 * @vsi_handle: unique VSI handle provided by drivers
354 * @vsi_ctx: pointer to a VSI context struct
355 * @cd: pointer to command details structure or NULL
357 * Add a VSI context to the hardware also add it into the VSI handle list.
358 * If this function gets called after reset for existing VSIs then update
359 * with the new HW VSI number in the corresponding VSI handle list entry.
362 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
363 struct ice_sq_cd *cd)
365 struct ice_vsi_ctx *tmp_vsi_ctx;
366 enum ice_status status;
368 if (vsi_handle >= ICE_MAX_VSI)
369 return ICE_ERR_PARAM;
370 status = ice_aq_add_vsi(hw, vsi_ctx, cd);
373 tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
375 /* Create a new vsi context */
376 tmp_vsi_ctx = devm_kzalloc(ice_hw_to_dev(hw),
377 sizeof(*tmp_vsi_ctx), GFP_KERNEL);
379 ice_aq_free_vsi(hw, vsi_ctx, false, cd);
380 return ICE_ERR_NO_MEMORY;
382 *tmp_vsi_ctx = *vsi_ctx;
383 ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
385 /* update with new HW VSI num */
386 if (tmp_vsi_ctx->vsi_num != vsi_ctx->vsi_num)
387 tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
394 * ice_free_vsi- free VSI context from hardware and VSI handle list
395 * @hw: pointer to the hw struct
396 * @vsi_handle: unique VSI handle
397 * @vsi_ctx: pointer to a VSI context struct
398 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
399 * @cd: pointer to command details structure or NULL
401 * Free VSI context info from hardware as well as from VSI handle list
404 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
405 bool keep_vsi_alloc, struct ice_sq_cd *cd)
407 enum ice_status status;
409 if (!ice_is_vsi_valid(hw, vsi_handle))
410 return ICE_ERR_PARAM;
411 vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
412 status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
414 ice_clear_vsi_ctx(hw, vsi_handle);
420 * @hw: pointer to the hw struct
421 * @vsi_handle: unique VSI handle
422 * @vsi_ctx: pointer to a VSI context struct
423 * @cd: pointer to command details structure or NULL
425 * Update VSI context in the hardware
428 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
429 struct ice_sq_cd *cd)
431 if (!ice_is_vsi_valid(hw, vsi_handle))
432 return ICE_ERR_PARAM;
433 vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
434 return ice_aq_update_vsi(hw, vsi_ctx, cd);
438 * ice_aq_alloc_free_vsi_list
439 * @hw: pointer to the hw struct
440 * @vsi_list_id: VSI list id returned or used for lookup
441 * @lkup_type: switch rule filter lookup type
442 * @opc: switch rules population command type - pass in the command opcode
444 * allocates or free a VSI list resource
446 static enum ice_status
447 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
448 enum ice_sw_lkup_type lkup_type,
449 enum ice_adminq_opc opc)
451 struct ice_aqc_alloc_free_res_elem *sw_buf;
452 struct ice_aqc_res_elem *vsi_ele;
453 enum ice_status status;
456 buf_len = sizeof(*sw_buf);
457 sw_buf = devm_kzalloc(ice_hw_to_dev(hw), buf_len, GFP_KERNEL);
459 return ICE_ERR_NO_MEMORY;
460 sw_buf->num_elems = cpu_to_le16(1);
462 if (lkup_type == ICE_SW_LKUP_MAC ||
463 lkup_type == ICE_SW_LKUP_MAC_VLAN ||
464 lkup_type == ICE_SW_LKUP_ETHERTYPE ||
465 lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
466 lkup_type == ICE_SW_LKUP_PROMISC ||
467 lkup_type == ICE_SW_LKUP_PROMISC_VLAN) {
468 sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
469 } else if (lkup_type == ICE_SW_LKUP_VLAN) {
471 cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
473 status = ICE_ERR_PARAM;
474 goto ice_aq_alloc_free_vsi_list_exit;
477 if (opc == ice_aqc_opc_free_res)
478 sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
480 status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
482 goto ice_aq_alloc_free_vsi_list_exit;
484 if (opc == ice_aqc_opc_alloc_res) {
485 vsi_ele = &sw_buf->elem[0];
486 *vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp);
489 ice_aq_alloc_free_vsi_list_exit:
490 devm_kfree(ice_hw_to_dev(hw), sw_buf);
495 * ice_aq_sw_rules - add/update/remove switch rules
496 * @hw: pointer to the hw struct
497 * @rule_list: pointer to switch rule population list
498 * @rule_list_sz: total size of the rule list in bytes
499 * @num_rules: number of switch rules in the rule_list
500 * @opc: switch rules population command type - pass in the command opcode
501 * @cd: pointer to command details structure or NULL
503 * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
505 static enum ice_status
506 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
507 u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
509 struct ice_aq_desc desc;
511 if (opc != ice_aqc_opc_add_sw_rules &&
512 opc != ice_aqc_opc_update_sw_rules &&
513 opc != ice_aqc_opc_remove_sw_rules)
514 return ICE_ERR_PARAM;
516 ice_fill_dflt_direct_cmd_desc(&desc, opc);
518 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
519 desc.params.sw_rules.num_rules_fltr_entry_index =
520 cpu_to_le16(num_rules);
521 return ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
524 /* ice_init_port_info - Initialize port_info with switch configuration data
525 * @pi: pointer to port_info
526 * @vsi_port_num: VSI number or port number
527 * @type: Type of switch element (port or VSI)
528 * @swid: switch ID of the switch the element is attached to
529 * @pf_vf_num: PF or VF number
530 * @is_vf: true if the element is a VF, false otherwise
533 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
534 u16 swid, u16 pf_vf_num, bool is_vf)
537 case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
538 pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
540 pi->pf_vf_num = pf_vf_num;
542 pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
543 pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
546 ice_debug(pi->hw, ICE_DBG_SW,
547 "incorrect VSI/port type received\n");
552 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
553 * @hw: pointer to the hardware structure
555 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
557 struct ice_aqc_get_sw_cfg_resp *rbuf;
558 enum ice_status status;
563 rbuf = devm_kzalloc(ice_hw_to_dev(hw), ICE_SW_CFG_MAX_BUF_LEN,
567 return ICE_ERR_NO_MEMORY;
569 /* Multiple calls to ice_aq_get_sw_cfg may be required
570 * to get all the switch configuration information. The need
571 * for additional calls is indicated by ice_aq_get_sw_cfg
572 * writing a non-zero value in req_desc
575 status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
576 &req_desc, &num_elems, NULL);
581 for (i = 0; i < num_elems; i++) {
582 struct ice_aqc_get_sw_cfg_resp_elem *ele;
583 u16 pf_vf_num, swid, vsi_port_num;
587 ele = rbuf[i].elements;
588 vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
589 ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
591 pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
592 ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
594 swid = le16_to_cpu(ele->swid);
596 if (le16_to_cpu(ele->pf_vf_num) &
597 ICE_AQC_GET_SW_CONF_RESP_IS_VF)
600 type = le16_to_cpu(ele->vsi_port_num) >>
601 ICE_AQC_GET_SW_CONF_RESP_TYPE_S;
603 if (type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
604 /* FW VSI is not needed. Just continue. */
608 ice_init_port_info(hw->port_info, vsi_port_num,
609 type, swid, pf_vf_num, is_vf);
611 } while (req_desc && !status);
613 devm_kfree(ice_hw_to_dev(hw), (void *)rbuf);
618 * ice_fill_sw_info - Helper function to populate lb_en and lan_en
619 * @hw: pointer to the hardware structure
620 * @f_info: filter info structure to fill/update
622 * This helper function populates the lb_en and lan_en elements of the provided
623 * ice_fltr_info struct using the switch's type and characteristics of the
624 * switch rule being configured.
626 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *f_info)
628 f_info->lb_en = false;
629 f_info->lan_en = false;
630 if ((f_info->flag & ICE_FLTR_TX) &&
631 (f_info->fltr_act == ICE_FWD_TO_VSI ||
632 f_info->fltr_act == ICE_FWD_TO_VSI_LIST ||
633 f_info->fltr_act == ICE_FWD_TO_Q ||
634 f_info->fltr_act == ICE_FWD_TO_QGRP)) {
635 f_info->lb_en = true;
636 if (!(hw->evb_veb && f_info->lkup_type == ICE_SW_LKUP_MAC &&
637 is_unicast_ether_addr(f_info->l_data.mac.mac_addr)))
638 f_info->lan_en = true;
643 * ice_fill_sw_rule - Helper function to fill switch rule structure
644 * @hw: pointer to the hardware structure
645 * @f_info: entry containing packet forwarding information
646 * @s_rule: switch rule structure to be filled in based on mac_entry
647 * @opc: switch rules population command type - pass in the command opcode
650 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
651 struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
653 u16 vlan_id = ICE_MAX_VLAN_ID + 1;
661 if (opc == ice_aqc_opc_remove_sw_rules) {
662 s_rule->pdata.lkup_tx_rx.act = 0;
663 s_rule->pdata.lkup_tx_rx.index =
664 cpu_to_le16(f_info->fltr_rule_id);
665 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
669 eth_hdr_sz = sizeof(dummy_eth_header);
670 eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
672 /* initialize the ether header with a dummy header */
673 memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz);
674 ice_fill_sw_info(hw, f_info);
676 switch (f_info->fltr_act) {
678 act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
679 ICE_SINGLE_ACT_VSI_ID_M;
680 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
681 act |= ICE_SINGLE_ACT_VSI_FORWARDING |
682 ICE_SINGLE_ACT_VALID_BIT;
684 case ICE_FWD_TO_VSI_LIST:
685 act |= ICE_SINGLE_ACT_VSI_LIST;
686 act |= (f_info->fwd_id.vsi_list_id <<
687 ICE_SINGLE_ACT_VSI_LIST_ID_S) &
688 ICE_SINGLE_ACT_VSI_LIST_ID_M;
689 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
690 act |= ICE_SINGLE_ACT_VSI_FORWARDING |
691 ICE_SINGLE_ACT_VALID_BIT;
694 act |= ICE_SINGLE_ACT_TO_Q;
695 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
696 ICE_SINGLE_ACT_Q_INDEX_M;
698 case ICE_DROP_PACKET:
699 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
700 ICE_SINGLE_ACT_VALID_BIT;
702 case ICE_FWD_TO_QGRP:
703 q_rgn = f_info->qgrp_size > 0 ?
704 (u8)ilog2(f_info->qgrp_size) : 0;
705 act |= ICE_SINGLE_ACT_TO_Q;
706 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
707 ICE_SINGLE_ACT_Q_INDEX_M;
708 act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
709 ICE_SINGLE_ACT_Q_REGION_M;
716 act |= ICE_SINGLE_ACT_LB_ENABLE;
718 act |= ICE_SINGLE_ACT_LAN_ENABLE;
720 switch (f_info->lkup_type) {
721 case ICE_SW_LKUP_MAC:
722 daddr = f_info->l_data.mac.mac_addr;
724 case ICE_SW_LKUP_VLAN:
725 vlan_id = f_info->l_data.vlan.vlan_id;
726 if (f_info->fltr_act == ICE_FWD_TO_VSI ||
727 f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
728 act |= ICE_SINGLE_ACT_PRUNE;
729 act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
732 case ICE_SW_LKUP_ETHERTYPE_MAC:
733 daddr = f_info->l_data.ethertype_mac.mac_addr;
735 case ICE_SW_LKUP_ETHERTYPE:
736 off = (__be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
737 *off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
739 case ICE_SW_LKUP_MAC_VLAN:
740 daddr = f_info->l_data.mac_vlan.mac_addr;
741 vlan_id = f_info->l_data.mac_vlan.vlan_id;
743 case ICE_SW_LKUP_PROMISC_VLAN:
744 vlan_id = f_info->l_data.mac_vlan.vlan_id;
746 case ICE_SW_LKUP_PROMISC:
747 daddr = f_info->l_data.mac_vlan.mac_addr;
753 s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
754 cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
755 cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
757 /* Recipe set depending on lookup type */
758 s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(f_info->lkup_type);
759 s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(f_info->src);
760 s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);
763 ether_addr_copy(eth_hdr + ICE_ETH_DA_OFFSET, daddr);
765 if (!(vlan_id > ICE_MAX_VLAN_ID)) {
766 off = (__be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
767 *off = cpu_to_be16(vlan_id);
770 /* Create the switch rule with the final dummy Ethernet header */
771 if (opc != ice_aqc_opc_update_sw_rules)
772 s_rule->pdata.lkup_tx_rx.hdr_len = cpu_to_le16(eth_hdr_sz);
777 * @hw: pointer to the hardware structure
778 * @m_ent: the management entry for which sw marker needs to be added
779 * @sw_marker: sw marker to tag the Rx descriptor with
780 * @l_id: large action resource id
782 * Create a large action to hold software marker and update the switch rule
783 * entry pointed by m_ent with newly created large action
785 static enum ice_status
786 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
787 u16 sw_marker, u16 l_id)
789 struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
790 /* For software marker we need 3 large actions
791 * 1. FWD action: FWD TO VSI or VSI LIST
792 * 2. GENERIC VALUE action to hold the profile id
793 * 3. GENERIC VALUE action to hold the software marker id
795 const u16 num_lg_acts = 3;
796 enum ice_status status;
802 if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
803 return ICE_ERR_PARAM;
805 /* Create two back-to-back switch rules and submit them to the HW using
810 lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
811 rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
812 lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
814 return ICE_ERR_NO_MEMORY;
816 rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
818 /* Fill in the first switch rule i.e. large action */
819 lg_act->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
820 lg_act->pdata.lg_act.index = cpu_to_le16(l_id);
821 lg_act->pdata.lg_act.size = cpu_to_le16(num_lg_acts);
823 /* First action VSI forwarding or VSI list forwarding depending on how
826 id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
827 m_ent->fltr_info.fwd_id.hw_vsi_id;
829 act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
830 act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
831 ICE_LG_ACT_VSI_LIST_ID_M;
832 if (m_ent->vsi_count > 1)
833 act |= ICE_LG_ACT_VSI_LIST;
834 lg_act->pdata.lg_act.act[0] = cpu_to_le32(act);
836 /* Second action descriptor type */
837 act = ICE_LG_ACT_GENERIC;
839 act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
840 lg_act->pdata.lg_act.act[1] = cpu_to_le32(act);
842 act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
843 ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
845 /* Third action Marker value */
846 act |= ICE_LG_ACT_GENERIC;
847 act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
848 ICE_LG_ACT_GENERIC_VALUE_M;
850 lg_act->pdata.lg_act.act[2] = cpu_to_le32(act);
852 /* call the fill switch rule to fill the lookup tx rx structure */
853 ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
854 ice_aqc_opc_update_sw_rules);
856 /* Update the action to point to the large action id */
857 rx_tx->pdata.lkup_tx_rx.act =
858 cpu_to_le32(ICE_SINGLE_ACT_PTR |
859 ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
860 ICE_SINGLE_ACT_PTR_VAL_M));
862 /* Use the filter rule id of the previously created rule with single
863 * act. Once the update happens, hardware will treat this as large
866 rx_tx->pdata.lkup_tx_rx.index =
867 cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
869 status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
870 ice_aqc_opc_update_sw_rules, NULL);
872 m_ent->lg_act_idx = l_id;
873 m_ent->sw_marker_id = sw_marker;
876 devm_kfree(ice_hw_to_dev(hw), lg_act);
881 * ice_create_vsi_list_map
882 * @hw: pointer to the hardware structure
883 * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
884 * @num_vsi: number of VSI handles in the array
885 * @vsi_list_id: VSI list id generated as part of allocate resource
887 * Helper function to create a new entry of VSI list id to VSI mapping
888 * using the given VSI list id
890 static struct ice_vsi_list_map_info *
891 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
894 struct ice_switch_info *sw = hw->switch_info;
895 struct ice_vsi_list_map_info *v_map;
898 v_map = devm_kcalloc(ice_hw_to_dev(hw), 1, sizeof(*v_map), GFP_KERNEL);
902 v_map->vsi_list_id = vsi_list_id;
904 for (i = 0; i < num_vsi; i++)
905 set_bit(vsi_handle_arr[i], v_map->vsi_map);
907 list_add(&v_map->list_entry, &sw->vsi_list_map_head);
912 * ice_update_vsi_list_rule
913 * @hw: pointer to the hardware structure
914 * @vsi_handle_arr: array of VSI handles to form a VSI list
915 * @num_vsi: number of VSI handles in the array
916 * @vsi_list_id: VSI list id generated as part of allocate resource
917 * @remove: Boolean value to indicate if this is a remove action
918 * @opc: switch rules population command type - pass in the command opcode
919 * @lkup_type: lookup type of the filter
921 * Call AQ command to add a new switch rule or update existing switch rule
922 * using the given VSI list id
924 static enum ice_status
925 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
926 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
927 enum ice_sw_lkup_type lkup_type)
929 struct ice_aqc_sw_rules_elem *s_rule;
930 enum ice_status status;
936 return ICE_ERR_PARAM;
938 if (lkup_type == ICE_SW_LKUP_MAC ||
939 lkup_type == ICE_SW_LKUP_MAC_VLAN ||
940 lkup_type == ICE_SW_LKUP_ETHERTYPE ||
941 lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
942 lkup_type == ICE_SW_LKUP_PROMISC ||
943 lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
944 type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
945 ICE_AQC_SW_RULES_T_VSI_LIST_SET;
946 else if (lkup_type == ICE_SW_LKUP_VLAN)
947 type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
948 ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
950 return ICE_ERR_PARAM;
952 s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
953 s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
955 return ICE_ERR_NO_MEMORY;
956 for (i = 0; i < num_vsi; i++) {
957 if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
958 status = ICE_ERR_PARAM;
961 /* AQ call requires hw_vsi_id(s) */
962 s_rule->pdata.vsi_list.vsi[i] =
963 cpu_to_le16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
966 s_rule->type = cpu_to_le16(type);
967 s_rule->pdata.vsi_list.number_vsi = cpu_to_le16(num_vsi);
968 s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
970 status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
973 devm_kfree(ice_hw_to_dev(hw), s_rule);
978 * ice_create_vsi_list_rule - Creates and populates a VSI list rule
979 * @hw: pointer to the hw struct
980 * @vsi_handle_arr: array of VSI handles to form a VSI list
981 * @num_vsi: number of VSI handles in the array
982 * @vsi_list_id: stores the ID of the VSI list to be created
983 * @lkup_type: switch rule filter's lookup type
985 static enum ice_status
986 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
987 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
989 enum ice_status status;
991 status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
992 ice_aqc_opc_alloc_res);
996 /* Update the newly created VSI list to include the specified VSIs */
997 return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
999 ice_aqc_opc_add_sw_rules, lkup_type);
1003 * ice_create_pkt_fwd_rule
1004 * @hw: pointer to the hardware structure
1005 * @f_entry: entry containing packet forwarding information
1007 * Create switch rule with given filter information and add an entry
1008 * to the corresponding filter management list to track this switch rule
1011 static enum ice_status
1012 ice_create_pkt_fwd_rule(struct ice_hw *hw,
1013 struct ice_fltr_list_entry *f_entry)
1015 struct ice_fltr_mgmt_list_entry *fm_entry;
1016 struct ice_aqc_sw_rules_elem *s_rule;
1017 enum ice_sw_lkup_type l_type;
1018 struct ice_sw_recipe *recp;
1019 enum ice_status status;
1021 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1022 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1024 return ICE_ERR_NO_MEMORY;
1025 fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
1028 status = ICE_ERR_NO_MEMORY;
1029 goto ice_create_pkt_fwd_rule_exit;
1032 fm_entry->fltr_info = f_entry->fltr_info;
1034 /* Initialize all the fields for the management entry */
1035 fm_entry->vsi_count = 1;
1036 fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1037 fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1038 fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1040 ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1041 ice_aqc_opc_add_sw_rules);
1043 status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1044 ice_aqc_opc_add_sw_rules, NULL);
1046 devm_kfree(ice_hw_to_dev(hw), fm_entry);
1047 goto ice_create_pkt_fwd_rule_exit;
1050 f_entry->fltr_info.fltr_rule_id =
1051 le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1052 fm_entry->fltr_info.fltr_rule_id =
1053 le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1055 /* The book keeping entries will get removed when base driver
1056 * calls remove filter AQ command
1058 l_type = fm_entry->fltr_info.lkup_type;
1059 recp = &hw->switch_info->recp_list[l_type];
1060 list_add(&fm_entry->list_entry, &recp->filt_rules);
1062 ice_create_pkt_fwd_rule_exit:
1063 devm_kfree(ice_hw_to_dev(hw), s_rule);
1068 * ice_update_pkt_fwd_rule
1069 * @hw: pointer to the hardware structure
1070 * @f_info: filter information for switch rule
1072 * Call AQ command to update a previously created switch rule with a
1075 static enum ice_status
1076 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1078 struct ice_aqc_sw_rules_elem *s_rule;
1079 enum ice_status status;
1081 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1082 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1084 return ICE_ERR_NO_MEMORY;
1086 ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
1088 s_rule->pdata.lkup_tx_rx.index = cpu_to_le16(f_info->fltr_rule_id);
1090 /* Update switch rule with new rule set to forward VSI list */
1091 status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1092 ice_aqc_opc_update_sw_rules, NULL);
1094 devm_kfree(ice_hw_to_dev(hw), s_rule);
1099 * ice_update_sw_rule_bridge_mode
1100 * @hw: pointer to the hw struct
1102 * Updates unicast switch filter rules based on VEB/VEPA mode
1104 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
1106 struct ice_switch_info *sw = hw->switch_info;
1107 struct ice_fltr_mgmt_list_entry *fm_entry;
1108 enum ice_status status = 0;
1109 struct list_head *rule_head;
1110 struct mutex *rule_lock; /* Lock to protect filter rule list */
1112 rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1113 rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1115 mutex_lock(rule_lock);
1116 list_for_each_entry(fm_entry, rule_head, list_entry) {
1117 struct ice_fltr_info *fi = &fm_entry->fltr_info;
1118 u8 *addr = fi->l_data.mac.mac_addr;
1120 /* Update unicast Tx rules to reflect the selected
1123 if ((fi->flag & ICE_FLTR_TX) && is_unicast_ether_addr(addr) &&
1124 (fi->fltr_act == ICE_FWD_TO_VSI ||
1125 fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1126 fi->fltr_act == ICE_FWD_TO_Q ||
1127 fi->fltr_act == ICE_FWD_TO_QGRP)) {
1128 status = ice_update_pkt_fwd_rule(hw, fi);
1134 mutex_unlock(rule_lock);
1140 * ice_add_update_vsi_list
1141 * @hw: pointer to the hardware structure
1142 * @m_entry: pointer to current filter management list entry
1143 * @cur_fltr: filter information from the book keeping entry
1144 * @new_fltr: filter information with the new VSI to be added
1146 * Call AQ command to add or update previously created VSI list with new VSI.
1148 * Helper function to do book keeping associated with adding filter information
1149 * The algorithm to do the booking keeping is described below :
1150 * When a VSI needs to subscribe to a given filter( MAC/VLAN/Ethtype etc.)
1151 * if only one VSI has been added till now
1152 * Allocate a new VSI list and add two VSIs
1153 * to this list using switch rule command
1154 * Update the previously created switch rule with the
1155 * newly created VSI list id
1156 * if a VSI list was previously created
1157 * Add the new VSI to the previously created VSI list set
1158 * using the update switch rule command
1160 static enum ice_status
1161 ice_add_update_vsi_list(struct ice_hw *hw,
1162 struct ice_fltr_mgmt_list_entry *m_entry,
1163 struct ice_fltr_info *cur_fltr,
1164 struct ice_fltr_info *new_fltr)
1166 enum ice_status status = 0;
1167 u16 vsi_list_id = 0;
1169 if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
1170 cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
1171 return ICE_ERR_NOT_IMPL;
1173 if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
1174 new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
1175 (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
1176 cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
1177 return ICE_ERR_NOT_IMPL;
1179 if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
1180 /* Only one entry existed in the mapping and it was not already
1181 * a part of a VSI list. So, create a VSI list with the old and
1184 struct ice_fltr_info tmp_fltr;
1185 u16 vsi_handle_arr[2];
1187 /* A rule already exists with the new VSI being added */
1188 if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
1189 return ICE_ERR_ALREADY_EXISTS;
1191 vsi_handle_arr[0] = cur_fltr->vsi_handle;
1192 vsi_handle_arr[1] = new_fltr->vsi_handle;
1193 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1195 new_fltr->lkup_type);
1199 tmp_fltr = *new_fltr;
1200 tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
1201 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1202 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1203 /* Update the previous switch rule of "MAC forward to VSI" to
1204 * "MAC fwd to VSI list"
1206 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1210 cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1211 cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1212 m_entry->vsi_list_info =
1213 ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1216 /* If this entry was large action then the large action needs
1217 * to be updated to point to FWD to VSI list
1219 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1221 ice_add_marker_act(hw, m_entry,
1222 m_entry->sw_marker_id,
1223 m_entry->lg_act_idx);
1225 u16 vsi_handle = new_fltr->vsi_handle;
1226 enum ice_adminq_opc opcode;
1228 /* A rule already exists with the new VSI being added */
1229 if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
1232 /* Update the previously created VSI list set with
1233 * the new VSI id passed in
1235 vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1236 opcode = ice_aqc_opc_update_sw_rules;
1238 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
1239 vsi_list_id, false, opcode,
1240 new_fltr->lkup_type);
1241 /* update VSI list mapping info with new VSI id */
1243 set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
1246 m_entry->vsi_count++;
1251 * ice_find_rule_entry - Search a rule entry
1252 * @hw: pointer to the hardware structure
1253 * @recp_id: lookup type for which the specified rule needs to be searched
1254 * @f_info: rule information
1256 * Helper function to search for a given rule entry
1257 * Returns pointer to entry storing the rule if found
1259 static struct ice_fltr_mgmt_list_entry *
1260 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
1262 struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
1263 struct ice_switch_info *sw = hw->switch_info;
1264 struct list_head *list_head;
1266 list_head = &sw->recp_list[recp_id].filt_rules;
1267 list_for_each_entry(list_itr, list_head, list_entry) {
1268 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
1269 sizeof(f_info->l_data)) &&
1270 f_info->flag == list_itr->fltr_info.flag) {
1279 * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
1280 * @hw: pointer to the hardware structure
1281 * @recp_id: lookup type for which VSI lists needs to be searched
1282 * @vsi_handle: VSI handle to be found in VSI list
1283 * @vsi_list_id: VSI list id found containing vsi_handle
1285 * Helper function to search a VSI list with single entry containing given VSI
1286 * handle element. This can be extended further to search VSI list with more
1287 * than 1 vsi_count. Returns pointer to VSI list entry if found.
1289 static struct ice_vsi_list_map_info *
1290 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
1293 struct ice_vsi_list_map_info *map_info = NULL;
1294 struct ice_switch_info *sw = hw->switch_info;
1295 struct ice_fltr_mgmt_list_entry *list_itr;
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 (list_itr->vsi_count == 1 && list_itr->vsi_list_info) {
1301 map_info = list_itr->vsi_list_info;
1302 if (test_bit(vsi_handle, map_info->vsi_map)) {
1303 *vsi_list_id = map_info->vsi_list_id;
1312 * ice_add_rule_internal - add rule for a given lookup type
1313 * @hw: pointer to the hardware structure
1314 * @recp_id: lookup type (recipe id) for which rule has to be added
1315 * @f_entry: structure containing MAC forwarding information
1317 * Adds or updates the rule lists for a given recipe
1319 static enum ice_status
1320 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
1321 struct ice_fltr_list_entry *f_entry)
1323 struct ice_switch_info *sw = hw->switch_info;
1324 struct ice_fltr_info *new_fltr, *cur_fltr;
1325 struct ice_fltr_mgmt_list_entry *m_entry;
1326 struct mutex *rule_lock; /* Lock to protect filter rule list */
1327 enum ice_status status = 0;
1329 if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1330 return ICE_ERR_PARAM;
1331 f_entry->fltr_info.fwd_id.hw_vsi_id =
1332 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1334 rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1336 mutex_lock(rule_lock);
1337 new_fltr = &f_entry->fltr_info;
1338 if (new_fltr->flag & ICE_FLTR_RX)
1339 new_fltr->src = hw->port_info->lport;
1340 else if (new_fltr->flag & ICE_FLTR_TX)
1341 new_fltr->src = f_entry->fltr_info.fwd_id.hw_vsi_id;
1343 m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
1345 mutex_unlock(rule_lock);
1346 return ice_create_pkt_fwd_rule(hw, f_entry);
1349 cur_fltr = &m_entry->fltr_info;
1350 status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
1351 mutex_unlock(rule_lock);
1357 * ice_remove_vsi_list_rule
1358 * @hw: pointer to the hardware structure
1359 * @vsi_list_id: VSI list id generated as part of allocate resource
1360 * @lkup_type: switch rule filter lookup type
1362 * The VSI list should be emptied before this function is called to remove the
1365 static enum ice_status
1366 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1367 enum ice_sw_lkup_type lkup_type)
1369 struct ice_aqc_sw_rules_elem *s_rule;
1370 enum ice_status status;
1373 s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
1374 s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1376 return ICE_ERR_NO_MEMORY;
1378 s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
1379 s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
1381 /* Free the vsi_list resource that we allocated. It is assumed that the
1382 * list is empty at this point.
1384 status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
1385 ice_aqc_opc_free_res);
1387 devm_kfree(ice_hw_to_dev(hw), s_rule);
1392 * ice_rem_update_vsi_list
1393 * @hw: pointer to the hardware structure
1394 * @vsi_handle: VSI handle of the VSI to remove
1395 * @fm_list: filter management entry for which the VSI list management needs to
1398 static enum ice_status
1399 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
1400 struct ice_fltr_mgmt_list_entry *fm_list)
1402 enum ice_sw_lkup_type lkup_type;
1403 enum ice_status status = 0;
1406 if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
1407 fm_list->vsi_count == 0)
1408 return ICE_ERR_PARAM;
1410 /* A rule with the VSI being removed does not exist */
1411 if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
1412 return ICE_ERR_DOES_NOT_EXIST;
1414 lkup_type = fm_list->fltr_info.lkup_type;
1415 vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
1416 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
1417 ice_aqc_opc_update_sw_rules,
1422 fm_list->vsi_count--;
1423 clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
1425 if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
1426 (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
1427 struct ice_vsi_list_map_info *vsi_list_info =
1428 fm_list->vsi_list_info;
1431 rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
1433 if (!ice_is_vsi_valid(hw, rem_vsi_handle))
1434 return ICE_ERR_OUT_OF_RANGE;
1435 status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
1437 ice_aqc_opc_update_sw_rules,
1442 /* Remove the VSI list since it is no longer used */
1443 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
1447 /* Change the list entry action from VSI_LIST to VSI */
1448 fm_list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1449 fm_list->fltr_info.fwd_id.hw_vsi_id =
1450 ice_get_hw_vsi_num(hw, rem_vsi_handle);
1451 fm_list->fltr_info.vsi_handle = rem_vsi_handle;
1453 list_del(&vsi_list_info->list_entry);
1454 devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
1455 fm_list->vsi_list_info = NULL;
1462 * ice_remove_rule_internal - Remove a filter rule of a given type
1463 * @hw: pointer to the hardware structure
1464 * @recp_id: recipe id for which the rule needs to removed
1465 * @f_entry: rule entry containing filter information
1467 static enum ice_status
1468 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
1469 struct ice_fltr_list_entry *f_entry)
1471 struct ice_switch_info *sw = hw->switch_info;
1472 struct ice_fltr_mgmt_list_entry *list_elem;
1473 struct mutex *rule_lock; /* Lock to protect filter rule list */
1474 enum ice_status status = 0;
1475 bool remove_rule = false;
1478 if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1479 return ICE_ERR_PARAM;
1480 f_entry->fltr_info.fwd_id.hw_vsi_id =
1481 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1483 rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
1484 mutex_lock(rule_lock);
1485 list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
1487 status = ICE_ERR_DOES_NOT_EXIST;
1491 if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
1493 } else if (!list_elem->vsi_list_info) {
1494 status = ICE_ERR_DOES_NOT_EXIST;
1497 if (list_elem->vsi_list_info->ref_cnt > 1)
1498 list_elem->vsi_list_info->ref_cnt--;
1499 vsi_handle = f_entry->fltr_info.vsi_handle;
1500 status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
1503 /* if vsi count goes to zero after updating the vsi list */
1504 if (list_elem->vsi_count == 0)
1509 /* Remove the lookup rule */
1510 struct ice_aqc_sw_rules_elem *s_rule;
1512 s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1513 ICE_SW_RULE_RX_TX_NO_HDR_SIZE,
1516 status = ICE_ERR_NO_MEMORY;
1520 ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
1521 ice_aqc_opc_remove_sw_rules);
1523 status = ice_aq_sw_rules(hw, s_rule,
1524 ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
1525 ice_aqc_opc_remove_sw_rules, NULL);
1529 /* Remove a book keeping from the list */
1530 devm_kfree(ice_hw_to_dev(hw), s_rule);
1532 list_del(&list_elem->list_entry);
1533 devm_kfree(ice_hw_to_dev(hw), list_elem);
1536 mutex_unlock(rule_lock);
1541 * ice_add_mac - Add a MAC address based filter rule
1542 * @hw: pointer to the hardware structure
1543 * @m_list: list of MAC addresses and forwarding information
1545 * IMPORTANT: When the ucast_shared flag is set to false and m_list has
1546 * multiple unicast addresses, the function assumes that all the
1547 * addresses are unique in a given add_mac call. It doesn't
1548 * check for duplicates in this case, removing duplicates from a given
1549 * list should be taken care of in the caller of this function.
1552 ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
1554 struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
1555 struct ice_fltr_list_entry *m_list_itr;
1556 struct list_head *rule_head;
1557 u16 elem_sent, total_elem_left;
1558 struct ice_switch_info *sw;
1559 struct mutex *rule_lock; /* Lock to protect filter rule list */
1560 enum ice_status status = 0;
1561 u16 num_unicast = 0;
1565 return ICE_ERR_PARAM;
1568 sw = hw->switch_info;
1569 rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1570 list_for_each_entry(m_list_itr, m_list, list_entry) {
1571 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
1575 m_list_itr->fltr_info.flag = ICE_FLTR_TX;
1576 vsi_handle = m_list_itr->fltr_info.vsi_handle;
1577 if (!ice_is_vsi_valid(hw, vsi_handle))
1578 return ICE_ERR_PARAM;
1579 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
1580 m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
1581 /* update the src in case it is vsi num */
1582 if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
1583 return ICE_ERR_PARAM;
1584 m_list_itr->fltr_info.src = hw_vsi_id;
1585 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
1586 is_zero_ether_addr(add))
1587 return ICE_ERR_PARAM;
1588 if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
1589 /* Don't overwrite the unicast address */
1590 mutex_lock(rule_lock);
1591 if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
1592 &m_list_itr->fltr_info)) {
1593 mutex_unlock(rule_lock);
1594 return ICE_ERR_ALREADY_EXISTS;
1596 mutex_unlock(rule_lock);
1598 } else if (is_multicast_ether_addr(add) ||
1599 (is_unicast_ether_addr(add) && hw->ucast_shared)) {
1600 m_list_itr->status =
1601 ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
1603 if (m_list_itr->status)
1604 return m_list_itr->status;
1608 mutex_lock(rule_lock);
1609 /* Exit if no suitable entries were found for adding bulk switch rule */
1612 goto ice_add_mac_exit;
1615 rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1617 /* Allocate switch rule buffer for the bulk update for unicast */
1618 s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1619 s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
1622 status = ICE_ERR_NO_MEMORY;
1623 goto ice_add_mac_exit;
1627 list_for_each_entry(m_list_itr, m_list, list_entry) {
1628 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1629 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1631 if (is_unicast_ether_addr(mac_addr)) {
1632 ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
1633 ice_aqc_opc_add_sw_rules);
1634 r_iter = (struct ice_aqc_sw_rules_elem *)
1635 ((u8 *)r_iter + s_rule_size);
1639 /* Call AQ bulk switch rule update for all unicast addresses */
1641 /* Call AQ switch rule in AQ_MAX chunk */
1642 for (total_elem_left = num_unicast; total_elem_left > 0;
1643 total_elem_left -= elem_sent) {
1644 struct ice_aqc_sw_rules_elem *entry = r_iter;
1646 elem_sent = min(total_elem_left,
1647 (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
1648 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
1649 elem_sent, ice_aqc_opc_add_sw_rules,
1652 goto ice_add_mac_exit;
1653 r_iter = (struct ice_aqc_sw_rules_elem *)
1654 ((u8 *)r_iter + (elem_sent * s_rule_size));
1657 /* Fill up rule id based on the value returned from FW */
1659 list_for_each_entry(m_list_itr, m_list, list_entry) {
1660 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
1661 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
1662 struct ice_fltr_mgmt_list_entry *fm_entry;
1664 if (is_unicast_ether_addr(mac_addr)) {
1665 f_info->fltr_rule_id =
1666 le16_to_cpu(r_iter->pdata.lkup_tx_rx.index);
1667 f_info->fltr_act = ICE_FWD_TO_VSI;
1668 /* Create an entry to track this MAC address */
1669 fm_entry = devm_kzalloc(ice_hw_to_dev(hw),
1670 sizeof(*fm_entry), GFP_KERNEL);
1672 status = ICE_ERR_NO_MEMORY;
1673 goto ice_add_mac_exit;
1675 fm_entry->fltr_info = *f_info;
1676 fm_entry->vsi_count = 1;
1677 /* The book keeping entries will get removed when
1678 * base driver calls remove filter AQ command
1681 list_add(&fm_entry->list_entry, rule_head);
1682 r_iter = (struct ice_aqc_sw_rules_elem *)
1683 ((u8 *)r_iter + s_rule_size);
1688 mutex_unlock(rule_lock);
1690 devm_kfree(ice_hw_to_dev(hw), s_rule);
1695 * ice_add_vlan_internal - Add one VLAN based filter rule
1696 * @hw: pointer to the hardware structure
1697 * @f_entry: filter entry containing one VLAN information
1699 static enum ice_status
1700 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
1702 struct ice_switch_info *sw = hw->switch_info;
1703 struct ice_fltr_mgmt_list_entry *v_list_itr;
1704 struct ice_fltr_info *new_fltr, *cur_fltr;
1705 enum ice_sw_lkup_type lkup_type;
1706 u16 vsi_list_id = 0, vsi_handle;
1707 struct mutex *rule_lock; /* Lock to protect filter rule list */
1708 enum ice_status status = 0;
1710 if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1711 return ICE_ERR_PARAM;
1713 f_entry->fltr_info.fwd_id.hw_vsi_id =
1714 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1715 new_fltr = &f_entry->fltr_info;
1717 /* VLAN id should only be 12 bits */
1718 if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
1719 return ICE_ERR_PARAM;
1721 if (new_fltr->src_id != ICE_SRC_ID_VSI)
1722 return ICE_ERR_PARAM;
1724 new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
1725 lkup_type = new_fltr->lkup_type;
1726 vsi_handle = new_fltr->vsi_handle;
1727 rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
1728 mutex_lock(rule_lock);
1729 v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
1731 struct ice_vsi_list_map_info *map_info = NULL;
1733 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
1734 /* All VLAN pruning rules use a VSI list. Check if
1735 * there is already a VSI list containing VSI that we
1736 * want to add. If found, use the same vsi_list_id for
1737 * this new VLAN rule or else create a new list.
1739 map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
1743 status = ice_create_vsi_list_rule(hw,
1751 /* Convert the action to forwarding to a VSI list. */
1752 new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1753 new_fltr->fwd_id.vsi_list_id = vsi_list_id;
1756 status = ice_create_pkt_fwd_rule(hw, f_entry);
1758 v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
1761 status = ICE_ERR_DOES_NOT_EXIST;
1764 /* reuse VSI list for new rule and increment ref_cnt */
1766 v_list_itr->vsi_list_info = map_info;
1767 map_info->ref_cnt++;
1769 v_list_itr->vsi_list_info =
1770 ice_create_vsi_list_map(hw, &vsi_handle,
1774 } else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
1775 /* Update existing VSI list to add new VSI id only if it used
1778 cur_fltr = &v_list_itr->fltr_info;
1779 status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
1782 /* If VLAN rule exists and VSI list being used by this rule is
1783 * referenced by more than 1 VLAN rule. Then create a new VSI
1784 * list appending previous VSI with new VSI and update existing
1785 * VLAN rule to point to new VSI list id
1787 struct ice_fltr_info tmp_fltr;
1788 u16 vsi_handle_arr[2];
1791 /* Current implementation only supports reusing VSI list with
1792 * one VSI count. We should never hit below condition
1794 if (v_list_itr->vsi_count > 1 &&
1795 v_list_itr->vsi_list_info->ref_cnt > 1) {
1796 ice_debug(hw, ICE_DBG_SW,
1797 "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
1798 status = ICE_ERR_CFG;
1803 find_first_bit(v_list_itr->vsi_list_info->vsi_map,
1806 /* A rule already exists with the new VSI being added */
1807 if (cur_handle == vsi_handle) {
1808 status = ICE_ERR_ALREADY_EXISTS;
1812 vsi_handle_arr[0] = cur_handle;
1813 vsi_handle_arr[1] = vsi_handle;
1814 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1815 &vsi_list_id, lkup_type);
1819 tmp_fltr = v_list_itr->fltr_info;
1820 tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
1821 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1822 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1823 /* Update the previous switch rule to a new VSI list which
1824 * includes current VSI thats requested
1826 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1830 /* before overriding VSI list map info. decrement ref_cnt of
1833 v_list_itr->vsi_list_info->ref_cnt--;
1835 /* now update to newly created list */
1836 v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
1837 v_list_itr->vsi_list_info =
1838 ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1840 v_list_itr->vsi_count++;
1844 mutex_unlock(rule_lock);
1849 * ice_add_vlan - Add VLAN based filter rule
1850 * @hw: pointer to the hardware structure
1851 * @v_list: list of VLAN entries and forwarding information
1854 ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
1856 struct ice_fltr_list_entry *v_list_itr;
1859 return ICE_ERR_PARAM;
1861 list_for_each_entry(v_list_itr, v_list, list_entry) {
1862 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
1863 return ICE_ERR_PARAM;
1864 v_list_itr->fltr_info.flag = ICE_FLTR_TX;
1865 v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
1866 if (v_list_itr->status)
1867 return v_list_itr->status;
1873 * ice_rem_sw_rule_info
1874 * @hw: pointer to the hardware structure
1875 * @rule_head: pointer to the switch list structure that we want to delete
1878 ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
1880 if (!list_empty(rule_head)) {
1881 struct ice_fltr_mgmt_list_entry *entry;
1882 struct ice_fltr_mgmt_list_entry *tmp;
1884 list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
1885 list_del(&entry->list_entry);
1886 devm_kfree(ice_hw_to_dev(hw), entry);
1892 * ice_cfg_dflt_vsi - change state of VSI to set/clear default
1893 * @hw: pointer to the hardware structure
1894 * @vsi_handle: VSI handle to set as default
1895 * @set: true to add the above mentioned switch rule, false to remove it
1896 * @direction: ICE_FLTR_RX or ICE_FLTR_TX
1898 * add filter rule to set/unset given VSI as default VSI for the switch
1899 * (represented by swid)
1902 ice_cfg_dflt_vsi(struct ice_hw *hw, u16 vsi_handle, bool set, u8 direction)
1904 struct ice_aqc_sw_rules_elem *s_rule;
1905 struct ice_fltr_info f_info;
1906 enum ice_adminq_opc opcode;
1907 enum ice_status status;
1911 if (!ice_is_vsi_valid(hw, vsi_handle))
1912 return ICE_ERR_PARAM;
1913 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
1915 s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
1916 ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
1917 s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1919 return ICE_ERR_NO_MEMORY;
1921 memset(&f_info, 0, sizeof(f_info));
1923 f_info.lkup_type = ICE_SW_LKUP_DFLT;
1924 f_info.flag = direction;
1925 f_info.fltr_act = ICE_FWD_TO_VSI;
1926 f_info.fwd_id.hw_vsi_id = hw_vsi_id;
1928 if (f_info.flag & ICE_FLTR_RX) {
1929 f_info.src = hw->port_info->lport;
1930 f_info.src_id = ICE_SRC_ID_LPORT;
1932 f_info.fltr_rule_id =
1933 hw->port_info->dflt_rx_vsi_rule_id;
1934 } else if (f_info.flag & ICE_FLTR_TX) {
1935 f_info.src_id = ICE_SRC_ID_VSI;
1936 f_info.src = hw_vsi_id;
1938 f_info.fltr_rule_id =
1939 hw->port_info->dflt_tx_vsi_rule_id;
1943 opcode = ice_aqc_opc_add_sw_rules;
1945 opcode = ice_aqc_opc_remove_sw_rules;
1947 ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
1949 status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
1950 if (status || !(f_info.flag & ICE_FLTR_TX_RX))
1953 u16 index = le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1955 if (f_info.flag & ICE_FLTR_TX) {
1956 hw->port_info->dflt_tx_vsi_num = hw_vsi_id;
1957 hw->port_info->dflt_tx_vsi_rule_id = index;
1958 } else if (f_info.flag & ICE_FLTR_RX) {
1959 hw->port_info->dflt_rx_vsi_num = hw_vsi_id;
1960 hw->port_info->dflt_rx_vsi_rule_id = index;
1963 if (f_info.flag & ICE_FLTR_TX) {
1964 hw->port_info->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
1965 hw->port_info->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
1966 } else if (f_info.flag & ICE_FLTR_RX) {
1967 hw->port_info->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
1968 hw->port_info->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
1973 devm_kfree(ice_hw_to_dev(hw), s_rule);
1978 * ice_remove_mac - remove a MAC address based filter rule
1979 * @hw: pointer to the hardware structure
1980 * @m_list: list of MAC addresses and forwarding information
1982 * This function removes either a MAC filter rule or a specific VSI from a
1983 * VSI list for a multicast MAC address.
1985 * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
1986 * ice_add_mac. Caller should be aware that this call will only work if all
1987 * the entries passed into m_list were added previously. It will not attempt to
1988 * do a partial remove of entries that were found.
1991 ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
1993 struct ice_fltr_list_entry *list_itr;
1996 return ICE_ERR_PARAM;
1998 list_for_each_entry(list_itr, m_list, list_entry) {
1999 enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2001 if (l_type != ICE_SW_LKUP_MAC)
2002 return ICE_ERR_PARAM;
2003 list_itr->status = ice_remove_rule_internal(hw,
2006 if (list_itr->status)
2007 return list_itr->status;
2013 * ice_remove_vlan - Remove VLAN based filter rule
2014 * @hw: pointer to the hardware structure
2015 * @v_list: list of VLAN entries and forwarding information
2018 ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
2020 struct ice_fltr_list_entry *v_list_itr;
2023 return ICE_ERR_PARAM;
2025 list_for_each_entry(v_list_itr, v_list, list_entry) {
2026 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2028 if (l_type != ICE_SW_LKUP_VLAN)
2029 return ICE_ERR_PARAM;
2030 v_list_itr->status = ice_remove_rule_internal(hw,
2033 if (v_list_itr->status)
2034 return v_list_itr->status;
2040 * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
2041 * @fm_entry: filter entry to inspect
2042 * @vsi_handle: VSI handle to compare with filter info
2045 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
2047 return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
2048 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
2049 (fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
2050 (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
2054 * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
2055 * @hw: pointer to the hardware structure
2056 * @vsi_handle: VSI handle to remove filters from
2057 * @vsi_list_head: pointer to the list to add entry to
2058 * @fi: pointer to fltr_info of filter entry to copy & add
2060 * Helper function, used when creating a list of filters to remove from
2061 * a specific VSI. The entry added to vsi_list_head is a COPY of the
2062 * original filter entry, with the exception of fltr_info.fltr_act and
2063 * fltr_info.fwd_id fields. These are set such that later logic can
2064 * extract which VSI to remove the fltr from, and pass on that information.
2066 static enum ice_status
2067 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2068 struct list_head *vsi_list_head,
2069 struct ice_fltr_info *fi)
2071 struct ice_fltr_list_entry *tmp;
2073 /* this memory is freed up in the caller function
2074 * once filters for this VSI are removed
2076 tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
2078 return ICE_ERR_NO_MEMORY;
2080 tmp->fltr_info = *fi;
2082 /* Overwrite these fields to indicate which VSI to remove filter from,
2083 * so find and remove logic can extract the information from the
2084 * list entries. Note that original entries will still have proper
2087 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2088 tmp->fltr_info.vsi_handle = vsi_handle;
2089 tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2091 list_add(&tmp->list_entry, vsi_list_head);
2097 * ice_add_to_vsi_fltr_list - Add VSI filters to the list
2098 * @hw: pointer to the hardware structure
2099 * @vsi_handle: VSI handle to remove filters from
2100 * @lkup_list_head: pointer to the list that has certain lookup type filters
2101 * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
2103 * Locates all filters in lkup_list_head that are used by the given VSI,
2104 * and adds COPIES of those entries to vsi_list_head (intended to be used
2105 * to remove the listed filters).
2106 * Note that this means all entries in vsi_list_head must be explicitly
2107 * deallocated by the caller when done with list.
2109 static enum ice_status
2110 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2111 struct list_head *lkup_list_head,
2112 struct list_head *vsi_list_head)
2114 struct ice_fltr_mgmt_list_entry *fm_entry;
2115 enum ice_status status = 0;
2117 /* check to make sure VSI id is valid and within boundary */
2118 if (!ice_is_vsi_valid(hw, vsi_handle))
2119 return ICE_ERR_PARAM;
2121 list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
2122 struct ice_fltr_info *fi;
2124 fi = &fm_entry->fltr_info;
2125 if (!ice_vsi_uses_fltr(fm_entry, vsi_handle))
2128 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
2137 * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
2138 * @hw: pointer to the hardware structure
2139 * @vsi_handle: VSI handle to remove filters from
2140 * @lkup: switch rule filter lookup type
2143 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
2144 enum ice_sw_lkup_type lkup)
2146 struct ice_switch_info *sw = hw->switch_info;
2147 struct ice_fltr_list_entry *fm_entry;
2148 struct list_head remove_list_head;
2149 struct list_head *rule_head;
2150 struct ice_fltr_list_entry *tmp;
2151 struct mutex *rule_lock; /* Lock to protect filter rule list */
2152 enum ice_status status;
2154 INIT_LIST_HEAD(&remove_list_head);
2155 rule_lock = &sw->recp_list[lkup].filt_rule_lock;
2156 rule_head = &sw->recp_list[lkup].filt_rules;
2157 mutex_lock(rule_lock);
2158 status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
2160 mutex_unlock(rule_lock);
2165 case ICE_SW_LKUP_MAC:
2166 ice_remove_mac(hw, &remove_list_head);
2168 case ICE_SW_LKUP_VLAN:
2169 ice_remove_vlan(hw, &remove_list_head);
2171 case ICE_SW_LKUP_MAC_VLAN:
2172 case ICE_SW_LKUP_ETHERTYPE:
2173 case ICE_SW_LKUP_ETHERTYPE_MAC:
2174 case ICE_SW_LKUP_PROMISC:
2175 case ICE_SW_LKUP_DFLT:
2176 case ICE_SW_LKUP_PROMISC_VLAN:
2177 case ICE_SW_LKUP_LAST:
2179 ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
2183 list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
2184 list_del(&fm_entry->list_entry);
2185 devm_kfree(ice_hw_to_dev(hw), fm_entry);
2190 * ice_remove_vsi_fltr - Remove all filters for a VSI
2191 * @hw: pointer to the hardware structure
2192 * @vsi_handle: VSI handle to remove filters from
2194 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
2196 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
2197 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
2198 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
2199 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
2200 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
2201 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
2202 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
2203 ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
2207 * ice_replay_vsi_fltr - Replay filters for requested VSI
2208 * @hw: pointer to the hardware structure
2209 * @vsi_handle: driver VSI handle
2210 * @recp_id: Recipe id for which rules need to be replayed
2211 * @list_head: list for which filters need to be replayed
2213 * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
2214 * It is required to pass valid VSI handle.
2216 static enum ice_status
2217 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
2218 struct list_head *list_head)
2220 struct ice_fltr_mgmt_list_entry *itr;
2221 enum ice_status status = 0;
2224 if (list_empty(list_head))
2226 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2228 list_for_each_entry(itr, list_head, list_entry) {
2229 struct ice_fltr_list_entry f_entry;
2231 f_entry.fltr_info = itr->fltr_info;
2232 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
2233 itr->fltr_info.vsi_handle == vsi_handle) {
2234 /* update the src in case it is vsi num */
2235 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2236 f_entry.fltr_info.src = hw_vsi_id;
2237 status = ice_add_rule_internal(hw, recp_id, &f_entry);
2242 if (!test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
2244 /* Clearing it so that the logic can add it back */
2245 clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
2246 f_entry.fltr_info.vsi_handle = vsi_handle;
2247 f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
2248 /* update the src in case it is vsi num */
2249 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
2250 f_entry.fltr_info.src = hw_vsi_id;
2251 if (recp_id == ICE_SW_LKUP_VLAN)
2252 status = ice_add_vlan_internal(hw, &f_entry);
2254 status = ice_add_rule_internal(hw, recp_id, &f_entry);
2263 * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
2264 * @hw: pointer to the hardware structure
2265 * @vsi_handle: driver VSI handle
2267 * Replays filters for requested VSI via vsi_handle.
2269 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
2271 struct ice_switch_info *sw = hw->switch_info;
2272 enum ice_status status = 0;
2275 for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2276 struct list_head *head;
2278 head = &sw->recp_list[i].filt_replay_rules;
2279 status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
2287 * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
2288 * @hw: pointer to the hw struct
2290 * Deletes the filter replay rules.
2292 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
2294 struct ice_switch_info *sw = hw->switch_info;
2300 for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
2301 if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
2302 struct list_head *l_head;
2304 l_head = &sw->recp_list[i].filt_replay_rules;
2305 ice_rem_sw_rule_info(hw, l_head);