1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2018-2020, Intel Corporation. */
4 /* flow director ethtool support for ice */
10 static struct in6_addr full_ipv6_addr_mask = {
13 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
14 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
19 static struct in6_addr zero_ipv6_addr_mask = {
22 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28 /* calls to ice_flow_add_prof require the number of segments in the array
29 * for segs_cnt. In this code that is one more than the index.
31 #define TNL_SEG_CNT(_TNL_) ((_TNL_) + 1)
34 * ice_fltr_to_ethtool_flow - convert filter type values to ethtool
36 * @flow: filter type to be converted
38 * Returns the corresponding ethtool flow type.
40 static int ice_fltr_to_ethtool_flow(enum ice_fltr_ptype flow)
43 case ICE_FLTR_PTYPE_NONF_IPV4_TCP:
45 case ICE_FLTR_PTYPE_NONF_IPV4_UDP:
47 case ICE_FLTR_PTYPE_NONF_IPV4_SCTP:
49 case ICE_FLTR_PTYPE_NONF_IPV4_OTHER:
50 return IPV4_USER_FLOW;
51 case ICE_FLTR_PTYPE_NONF_IPV6_TCP:
53 case ICE_FLTR_PTYPE_NONF_IPV6_UDP:
55 case ICE_FLTR_PTYPE_NONF_IPV6_SCTP:
57 case ICE_FLTR_PTYPE_NONF_IPV6_OTHER:
58 return IPV6_USER_FLOW;
60 /* 0 is undefined ethtool flow */
66 * ice_ethtool_flow_to_fltr - convert ethtool flow type to filter enum
67 * @eth: Ethtool flow type to be converted
71 static enum ice_fltr_ptype ice_ethtool_flow_to_fltr(int eth)
75 return ICE_FLTR_PTYPE_NONF_IPV4_TCP;
77 return ICE_FLTR_PTYPE_NONF_IPV4_UDP;
79 return ICE_FLTR_PTYPE_NONF_IPV4_SCTP;
81 return ICE_FLTR_PTYPE_NONF_IPV4_OTHER;
83 return ICE_FLTR_PTYPE_NONF_IPV6_TCP;
85 return ICE_FLTR_PTYPE_NONF_IPV6_UDP;
87 return ICE_FLTR_PTYPE_NONF_IPV6_SCTP;
89 return ICE_FLTR_PTYPE_NONF_IPV6_OTHER;
91 return ICE_FLTR_PTYPE_NONF_NONE;
96 * ice_is_mask_valid - check mask field set
97 * @mask: full mask to check
98 * @field: field for which mask should be valid
100 * If the mask is fully set return true. If it is not valid for field return
103 static bool ice_is_mask_valid(u64 mask, u64 field)
105 return (mask & field) == field;
109 * ice_get_ethtool_fdir_entry - fill ethtool structure with fdir filter data
110 * @hw: hardware structure that contains filter list
111 * @cmd: ethtool command data structure to receive the filter data
113 * Returns 0 on success and -EINVAL on failure
115 int ice_get_ethtool_fdir_entry(struct ice_hw *hw, struct ethtool_rxnfc *cmd)
117 struct ethtool_rx_flow_spec *fsp;
118 struct ice_fdir_fltr *rule;
122 fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
124 mutex_lock(&hw->fdir_fltr_lock);
126 rule = ice_fdir_find_fltr_by_idx(hw, fsp->location);
128 if (!rule || fsp->location != rule->fltr_id) {
133 fsp->flow_type = ice_fltr_to_ethtool_flow(rule->flow_type);
135 memset(&fsp->m_u, 0, sizeof(fsp->m_u));
136 memset(&fsp->m_ext, 0, sizeof(fsp->m_ext));
138 switch (fsp->flow_type) {
140 fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
141 fsp->h_u.usr_ip4_spec.proto = 0;
142 fsp->h_u.usr_ip4_spec.l4_4_bytes = rule->ip.v4.l4_header;
143 fsp->h_u.usr_ip4_spec.tos = rule->ip.v4.tos;
144 fsp->h_u.usr_ip4_spec.ip4src = rule->ip.v4.src_ip;
145 fsp->h_u.usr_ip4_spec.ip4dst = rule->ip.v4.dst_ip;
146 fsp->m_u.usr_ip4_spec.ip4src = rule->mask.v4.src_ip;
147 fsp->m_u.usr_ip4_spec.ip4dst = rule->mask.v4.dst_ip;
148 fsp->m_u.usr_ip4_spec.ip_ver = 0xFF;
149 fsp->m_u.usr_ip4_spec.proto = 0;
150 fsp->m_u.usr_ip4_spec.l4_4_bytes = rule->mask.v4.l4_header;
151 fsp->m_u.usr_ip4_spec.tos = rule->mask.v4.tos;
156 fsp->h_u.tcp_ip4_spec.psrc = rule->ip.v4.src_port;
157 fsp->h_u.tcp_ip4_spec.pdst = rule->ip.v4.dst_port;
158 fsp->h_u.tcp_ip4_spec.ip4src = rule->ip.v4.src_ip;
159 fsp->h_u.tcp_ip4_spec.ip4dst = rule->ip.v4.dst_ip;
160 fsp->m_u.tcp_ip4_spec.psrc = rule->mask.v4.src_port;
161 fsp->m_u.tcp_ip4_spec.pdst = rule->mask.v4.dst_port;
162 fsp->m_u.tcp_ip4_spec.ip4src = rule->mask.v4.src_ip;
163 fsp->m_u.tcp_ip4_spec.ip4dst = rule->mask.v4.dst_ip;
166 fsp->h_u.usr_ip6_spec.l4_4_bytes = rule->ip.v6.l4_header;
167 fsp->h_u.usr_ip6_spec.tclass = rule->ip.v6.tc;
168 fsp->h_u.usr_ip6_spec.l4_proto = rule->ip.v6.proto;
169 memcpy(fsp->h_u.tcp_ip6_spec.ip6src, rule->ip.v6.src_ip,
170 sizeof(struct in6_addr));
171 memcpy(fsp->h_u.tcp_ip6_spec.ip6dst, rule->ip.v6.dst_ip,
172 sizeof(struct in6_addr));
173 memcpy(fsp->m_u.tcp_ip6_spec.ip6src, rule->mask.v6.src_ip,
174 sizeof(struct in6_addr));
175 memcpy(fsp->m_u.tcp_ip6_spec.ip6dst, rule->mask.v6.dst_ip,
176 sizeof(struct in6_addr));
177 fsp->m_u.usr_ip6_spec.l4_4_bytes = rule->mask.v6.l4_header;
178 fsp->m_u.usr_ip6_spec.tclass = rule->mask.v6.tc;
179 fsp->m_u.usr_ip6_spec.l4_proto = rule->mask.v6.proto;
184 memcpy(fsp->h_u.tcp_ip6_spec.ip6src, rule->ip.v6.src_ip,
185 sizeof(struct in6_addr));
186 memcpy(fsp->h_u.tcp_ip6_spec.ip6dst, rule->ip.v6.dst_ip,
187 sizeof(struct in6_addr));
188 fsp->h_u.tcp_ip6_spec.psrc = rule->ip.v6.src_port;
189 fsp->h_u.tcp_ip6_spec.pdst = rule->ip.v6.dst_port;
190 memcpy(fsp->m_u.tcp_ip6_spec.ip6src,
191 rule->mask.v6.src_ip,
192 sizeof(struct in6_addr));
193 memcpy(fsp->m_u.tcp_ip6_spec.ip6dst,
194 rule->mask.v6.dst_ip,
195 sizeof(struct in6_addr));
196 fsp->m_u.tcp_ip6_spec.psrc = rule->mask.v6.src_port;
197 fsp->m_u.tcp_ip6_spec.pdst = rule->mask.v6.dst_port;
198 fsp->h_u.tcp_ip6_spec.tclass = rule->ip.v6.tc;
199 fsp->m_u.tcp_ip6_spec.tclass = rule->mask.v6.tc;
205 if (rule->dest_ctl == ICE_FLTR_PRGM_DESC_DEST_DROP_PKT)
206 fsp->ring_cookie = RX_CLS_FLOW_DISC;
208 fsp->ring_cookie = rule->q_index;
210 idx = ice_ethtool_flow_to_fltr(fsp->flow_type);
211 if (idx == ICE_FLTR_PTYPE_NONF_NONE) {
212 dev_err(ice_hw_to_dev(hw), "Missing input index for flow_type %d\n",
218 mutex_unlock(&hw->fdir_fltr_lock);
223 * ice_get_fdir_fltr_ids - fill buffer with filter IDs of active filters
224 * @hw: hardware structure containing the filter list
225 * @cmd: ethtool command data structure
226 * @rule_locs: ethtool array passed in from OS to receive filter IDs
228 * Returns 0 as expected for success by ethtool
231 ice_get_fdir_fltr_ids(struct ice_hw *hw, struct ethtool_rxnfc *cmd,
234 struct ice_fdir_fltr *f_rule;
235 unsigned int cnt = 0;
238 /* report total rule count */
239 cmd->data = ice_get_fdir_cnt_all(hw);
241 mutex_lock(&hw->fdir_fltr_lock);
243 list_for_each_entry(f_rule, &hw->fdir_list_head, fltr_node) {
244 if (cnt == cmd->rule_cnt) {
248 rule_locs[cnt] = f_rule->fltr_id;
253 mutex_unlock(&hw->fdir_fltr_lock);
260 * ice_fdir_get_hw_prof - return the ice_fd_hw_proc associated with a flow
261 * @hw: hardware structure containing the filter list
262 * @blk: hardware block
263 * @flow: FDir flow type to release
265 static struct ice_fd_hw_prof *
266 ice_fdir_get_hw_prof(struct ice_hw *hw, enum ice_block blk, int flow)
268 if (blk == ICE_BLK_FD && hw->fdir_prof)
269 return hw->fdir_prof[flow];
275 * ice_fdir_erase_flow_from_hw - remove a flow from the HW profile tables
276 * @hw: hardware structure containing the filter list
277 * @blk: hardware block
278 * @flow: FDir flow type to release
281 ice_fdir_erase_flow_from_hw(struct ice_hw *hw, enum ice_block blk, int flow)
283 struct ice_fd_hw_prof *prof = ice_fdir_get_hw_prof(hw, blk, flow);
289 for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
293 prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
294 for (j = 0; j < prof->cnt; j++) {
297 if (!prof->entry_h[j][tun] || !prof->vsi_h[j])
299 vsi_num = ice_get_hw_vsi_num(hw, prof->vsi_h[j]);
300 ice_rem_prof_id_flow(hw, blk, vsi_num, prof_id);
301 ice_flow_rem_entry(hw, blk, prof->entry_h[j][tun]);
302 prof->entry_h[j][tun] = 0;
304 ice_flow_rem_prof(hw, blk, prof_id);
309 * ice_fdir_rem_flow - release the ice_flow structures for a filter type
310 * @hw: hardware structure containing the filter list
311 * @blk: hardware block
312 * @flow_type: FDir flow type to release
315 ice_fdir_rem_flow(struct ice_hw *hw, enum ice_block blk,
316 enum ice_fltr_ptype flow_type)
318 int flow = (int)flow_type & ~FLOW_EXT;
319 struct ice_fd_hw_prof *prof;
322 prof = ice_fdir_get_hw_prof(hw, blk, flow);
326 ice_fdir_erase_flow_from_hw(hw, blk, flow);
327 for (i = 0; i < prof->cnt; i++)
329 for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
330 if (!prof->fdir_seg[tun])
332 devm_kfree(ice_hw_to_dev(hw), prof->fdir_seg[tun]);
333 prof->fdir_seg[tun] = NULL;
339 * ice_fdir_release_flows - release all flows in use for later replay
340 * @hw: pointer to HW instance
342 void ice_fdir_release_flows(struct ice_hw *hw)
346 /* release Flow Director HW table entries */
347 for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++)
348 ice_fdir_erase_flow_from_hw(hw, ICE_BLK_FD, flow);
352 * ice_fdir_replay_flows - replay HW Flow Director filter info
353 * @hw: pointer to HW instance
355 void ice_fdir_replay_flows(struct ice_hw *hw)
359 for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
362 if (!hw->fdir_prof[flow] || !hw->fdir_prof[flow]->cnt)
364 for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
365 struct ice_flow_prof *hw_prof;
366 struct ice_fd_hw_prof *prof;
370 prof = hw->fdir_prof[flow];
371 prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
372 ice_flow_add_prof(hw, ICE_BLK_FD, ICE_FLOW_RX, prof_id,
373 prof->fdir_seg[tun], TNL_SEG_CNT(tun),
375 for (j = 0; j < prof->cnt; j++) {
376 enum ice_flow_priority prio;
380 prio = ICE_FLOW_PRIO_NORMAL;
381 err = ice_flow_add_entry(hw, ICE_BLK_FD,
385 prio, prof->fdir_seg,
388 dev_err(ice_hw_to_dev(hw), "Could not replay Flow Director, flow type %d\n",
392 prof->entry_h[j][tun] = entry_h;
399 * ice_parse_rx_flow_user_data - deconstruct user-defined data
400 * @fsp: pointer to ethtool Rx flow specification
401 * @data: pointer to userdef data structure for storage
403 * Returns 0 on success, negative error value on failure
406 ice_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
407 struct ice_rx_flow_userdef *data)
411 memset(data, 0, sizeof(*data));
412 if (!(fsp->flow_type & FLOW_EXT))
415 value = be64_to_cpu(*((__force __be64 *)fsp->h_ext.data));
416 mask = be64_to_cpu(*((__force __be64 *)fsp->m_ext.data));
420 #define ICE_USERDEF_FLEX_WORD_M GENMASK_ULL(15, 0)
421 #define ICE_USERDEF_FLEX_OFFS_S 16
422 #define ICE_USERDEF_FLEX_OFFS_M GENMASK_ULL(31, ICE_USERDEF_FLEX_OFFS_S)
423 #define ICE_USERDEF_FLEX_FLTR_M GENMASK_ULL(31, 0)
425 /* 0x1fe is the maximum value for offsets stored in the internal
428 #define ICE_USERDEF_FLEX_MAX_OFFS_VAL 0x1fe
430 if (!ice_is_mask_valid(mask, ICE_USERDEF_FLEX_FLTR_M) ||
431 value > ICE_USERDEF_FLEX_FLTR_M)
434 data->flex_word = value & ICE_USERDEF_FLEX_WORD_M;
435 data->flex_offset = (value & ICE_USERDEF_FLEX_OFFS_M) >>
436 ICE_USERDEF_FLEX_OFFS_S;
437 if (data->flex_offset > ICE_USERDEF_FLEX_MAX_OFFS_VAL)
440 data->flex_fltr = true;
446 * ice_fdir_num_avail_fltr - return the number of unused flow director filters
447 * @hw: pointer to hardware structure
448 * @vsi: software VSI structure
450 * There are 2 filter pools: guaranteed and best effort(shared). Each VSI can
451 * use filters from either pool. The guaranteed pool is divided between VSIs.
452 * The best effort filter pool is common to all VSIs and is a device shared
453 * resource pool. The number of filters available to this VSI is the sum of
454 * the VSIs guaranteed filter pool and the global available best effort
457 * Returns the number of available flow director filters to this VSI
459 static int ice_fdir_num_avail_fltr(struct ice_hw *hw, struct ice_vsi *vsi)
461 u16 vsi_num = ice_get_hw_vsi_num(hw, vsi->idx);
465 /* total guaranteed filters assigned to this VSI */
466 num_guar = vsi->num_gfltr;
468 /* minus the guaranteed filters programed by this VSI */
469 num_guar -= (rd32(hw, VSIQF_FD_CNT(vsi_num)) &
470 VSIQF_FD_CNT_FD_GCNT_M) >> VSIQF_FD_CNT_FD_GCNT_S;
472 /* total global best effort filters */
473 num_be = hw->func_caps.fd_fltr_best_effort;
475 /* minus the global best effort filters programmed */
476 num_be -= (rd32(hw, GLQF_FD_CNT) & GLQF_FD_CNT_FD_BCNT_M) >>
477 GLQF_FD_CNT_FD_BCNT_S;
479 return num_guar + num_be;
483 * ice_fdir_alloc_flow_prof - allocate FDir flow profile structure(s)
484 * @hw: HW structure containing the FDir flow profile structure(s)
485 * @flow: flow type to allocate the flow profile for
487 * Allocate the fdir_prof and fdir_prof[flow] if not already created. Return 0
488 * on success and negative on error.
491 ice_fdir_alloc_flow_prof(struct ice_hw *hw, enum ice_fltr_ptype flow)
496 if (!hw->fdir_prof) {
497 hw->fdir_prof = devm_kcalloc(ice_hw_to_dev(hw),
499 sizeof(*hw->fdir_prof),
505 if (!hw->fdir_prof[flow]) {
506 hw->fdir_prof[flow] = devm_kzalloc(ice_hw_to_dev(hw),
507 sizeof(**hw->fdir_prof),
509 if (!hw->fdir_prof[flow])
517 * ice_fdir_set_hw_fltr_rule - Configure HW tables to generate a FDir rule
518 * @pf: pointer to the PF structure
519 * @seg: protocol header description pointer
521 * @tun: FDir segment to program
524 ice_fdir_set_hw_fltr_rule(struct ice_pf *pf, struct ice_flow_seg_info *seg,
525 enum ice_fltr_ptype flow, enum ice_fd_hw_seg tun)
527 struct device *dev = ice_pf_to_dev(pf);
528 struct ice_vsi *main_vsi, *ctrl_vsi;
529 struct ice_flow_seg_info *old_seg;
530 struct ice_flow_prof *prof = NULL;
531 struct ice_fd_hw_prof *hw_prof;
532 struct ice_hw *hw = &pf->hw;
533 enum ice_status status;
539 main_vsi = ice_get_main_vsi(pf);
543 ctrl_vsi = ice_get_ctrl_vsi(pf);
547 err = ice_fdir_alloc_flow_prof(hw, flow);
551 hw_prof = hw->fdir_prof[flow];
552 old_seg = hw_prof->fdir_seg[tun];
554 /* This flow_type already has a changed input set.
555 * If it matches the requested input set then we are
556 * done. Or, if it's different then it's an error.
558 if (!memcmp(old_seg, seg, sizeof(*seg)))
561 /* if there are FDir filters using this flow,
564 if (hw->fdir_fltr_cnt[flow]) {
565 dev_err(dev, "Failed to add filter. Flow director filters on each port must have the same input set.\n");
569 if (ice_is_arfs_using_perfect_flow(hw, flow)) {
570 dev_err(dev, "aRFS using perfect flow type %d, cannot change input set\n",
575 /* remove HW filter definition */
576 ice_fdir_rem_flow(hw, ICE_BLK_FD, flow);
579 /* Adding a profile, but there is only one header supported.
580 * That is the final parameters are 1 header (segment), no
581 * actions (NULL) and zero actions 0.
583 prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
584 status = ice_flow_add_prof(hw, ICE_BLK_FD, ICE_FLOW_RX, prof_id, seg,
585 TNL_SEG_CNT(tun), &prof);
587 return ice_status_to_errno(status);
588 status = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id, main_vsi->idx,
589 main_vsi->idx, ICE_FLOW_PRIO_NORMAL,
592 err = ice_status_to_errno(status);
595 status = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id, main_vsi->idx,
596 ctrl_vsi->idx, ICE_FLOW_PRIO_NORMAL,
599 err = ice_status_to_errno(status);
603 hw_prof->fdir_seg[tun] = seg;
604 hw_prof->entry_h[0][tun] = entry1_h;
605 hw_prof->entry_h[1][tun] = entry2_h;
606 hw_prof->vsi_h[0] = main_vsi->idx;
607 hw_prof->vsi_h[1] = ctrl_vsi->idx;
614 ice_rem_prof_id_flow(hw, ICE_BLK_FD,
615 ice_get_hw_vsi_num(hw, main_vsi->idx), prof_id);
616 ice_flow_rem_entry(hw, ICE_BLK_FD, entry1_h);
618 ice_flow_rem_prof(hw, ICE_BLK_FD, prof_id);
619 dev_err(dev, "Failed to add filter. Flow director filters on each port must have the same input set.\n");
625 * ice_set_init_fdir_seg
626 * @seg: flow segment for programming
627 * @l3_proto: ICE_FLOW_SEG_HDR_IPV4 or ICE_FLOW_SEG_HDR_IPV6
628 * @l4_proto: ICE_FLOW_SEG_HDR_TCP or ICE_FLOW_SEG_HDR_UDP
630 * Set the configuration for perfect filters to the provided flow segment for
631 * programming the HW filter. This is to be called only when initializing
632 * filters as this function it assumes no filters exist.
635 ice_set_init_fdir_seg(struct ice_flow_seg_info *seg,
636 enum ice_flow_seg_hdr l3_proto,
637 enum ice_flow_seg_hdr l4_proto)
639 enum ice_flow_field src_addr, dst_addr, src_port, dst_port;
644 if (l3_proto == ICE_FLOW_SEG_HDR_IPV4) {
645 src_addr = ICE_FLOW_FIELD_IDX_IPV4_SA;
646 dst_addr = ICE_FLOW_FIELD_IDX_IPV4_DA;
647 } else if (l3_proto == ICE_FLOW_SEG_HDR_IPV6) {
648 src_addr = ICE_FLOW_FIELD_IDX_IPV6_SA;
649 dst_addr = ICE_FLOW_FIELD_IDX_IPV6_DA;
654 if (l4_proto == ICE_FLOW_SEG_HDR_TCP) {
655 src_port = ICE_FLOW_FIELD_IDX_TCP_SRC_PORT;
656 dst_port = ICE_FLOW_FIELD_IDX_TCP_DST_PORT;
657 } else if (l4_proto == ICE_FLOW_SEG_HDR_UDP) {
658 src_port = ICE_FLOW_FIELD_IDX_UDP_SRC_PORT;
659 dst_port = ICE_FLOW_FIELD_IDX_UDP_DST_PORT;
664 ICE_FLOW_SET_HDRS(seg, l3_proto | l4_proto);
666 /* IP source address */
667 ice_flow_set_fld(seg, src_addr, ICE_FLOW_FLD_OFF_INVAL,
668 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL, false);
670 /* IP destination address */
671 ice_flow_set_fld(seg, dst_addr, ICE_FLOW_FLD_OFF_INVAL,
672 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL, false);
674 /* Layer 4 source port */
675 ice_flow_set_fld(seg, src_port, ICE_FLOW_FLD_OFF_INVAL,
676 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL, false);
678 /* Layer 4 destination port */
679 ice_flow_set_fld(seg, dst_port, ICE_FLOW_FLD_OFF_INVAL,
680 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL, false);
686 * ice_create_init_fdir_rule
690 * Return error value or 0 on success.
693 ice_create_init_fdir_rule(struct ice_pf *pf, enum ice_fltr_ptype flow)
695 struct ice_flow_seg_info *seg, *tun_seg;
696 struct device *dev = ice_pf_to_dev(pf);
697 struct ice_hw *hw = &pf->hw;
700 /* if there is already a filter rule for kind return -EINVAL */
701 if (hw->fdir_prof && hw->fdir_prof[flow] &&
702 hw->fdir_prof[flow]->fdir_seg[0])
705 seg = devm_kzalloc(dev, sizeof(*seg), GFP_KERNEL);
709 tun_seg = devm_kzalloc(dev, sizeof(*seg) * ICE_FD_HW_SEG_MAX,
712 devm_kfree(dev, seg);
716 if (flow == ICE_FLTR_PTYPE_NONF_IPV4_TCP)
717 ret = ice_set_init_fdir_seg(seg, ICE_FLOW_SEG_HDR_IPV4,
718 ICE_FLOW_SEG_HDR_TCP);
719 else if (flow == ICE_FLTR_PTYPE_NONF_IPV4_UDP)
720 ret = ice_set_init_fdir_seg(seg, ICE_FLOW_SEG_HDR_IPV4,
721 ICE_FLOW_SEG_HDR_UDP);
722 else if (flow == ICE_FLTR_PTYPE_NONF_IPV6_TCP)
723 ret = ice_set_init_fdir_seg(seg, ICE_FLOW_SEG_HDR_IPV6,
724 ICE_FLOW_SEG_HDR_TCP);
725 else if (flow == ICE_FLTR_PTYPE_NONF_IPV6_UDP)
726 ret = ice_set_init_fdir_seg(seg, ICE_FLOW_SEG_HDR_IPV6,
727 ICE_FLOW_SEG_HDR_UDP);
733 /* add filter for outer headers */
734 ret = ice_fdir_set_hw_fltr_rule(pf, seg, flow, ICE_FD_HW_SEG_NON_TUN);
736 /* could not write filter, free memory */
739 /* make tunneled filter HW entries if possible */
740 memcpy(&tun_seg[1], seg, sizeof(*seg));
741 ret = ice_fdir_set_hw_fltr_rule(pf, tun_seg, flow, ICE_FD_HW_SEG_TUN);
743 /* could not write tunnel filter, but outer header filter
746 devm_kfree(dev, tun_seg);
748 set_bit(flow, hw->fdir_perfect_fltr);
751 devm_kfree(dev, tun_seg);
752 devm_kfree(dev, seg);
758 * ice_set_fdir_ip4_seg
759 * @seg: flow segment for programming
760 * @tcp_ip4_spec: mask data from ethtool
761 * @l4_proto: Layer 4 protocol to program
762 * @perfect_fltr: only valid on success; returns true if perfect filter,
765 * Set the mask data into the flow segment to be used to program HW
766 * table based on provided L4 protocol for IPv4
769 ice_set_fdir_ip4_seg(struct ice_flow_seg_info *seg,
770 struct ethtool_tcpip4_spec *tcp_ip4_spec,
771 enum ice_flow_seg_hdr l4_proto, bool *perfect_fltr)
773 enum ice_flow_field src_port, dst_port;
775 /* make sure we don't have any empty rule */
776 if (!tcp_ip4_spec->psrc && !tcp_ip4_spec->ip4src &&
777 !tcp_ip4_spec->pdst && !tcp_ip4_spec->ip4dst)
780 /* filtering on TOS not supported */
781 if (tcp_ip4_spec->tos)
784 if (l4_proto == ICE_FLOW_SEG_HDR_TCP) {
785 src_port = ICE_FLOW_FIELD_IDX_TCP_SRC_PORT;
786 dst_port = ICE_FLOW_FIELD_IDX_TCP_DST_PORT;
787 } else if (l4_proto == ICE_FLOW_SEG_HDR_UDP) {
788 src_port = ICE_FLOW_FIELD_IDX_UDP_SRC_PORT;
789 dst_port = ICE_FLOW_FIELD_IDX_UDP_DST_PORT;
790 } else if (l4_proto == ICE_FLOW_SEG_HDR_SCTP) {
791 src_port = ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT;
792 dst_port = ICE_FLOW_FIELD_IDX_SCTP_DST_PORT;
797 *perfect_fltr = true;
798 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_IPV4 | l4_proto);
800 /* IP source address */
801 if (tcp_ip4_spec->ip4src == htonl(0xFFFFFFFF))
802 ice_flow_set_fld(seg, ICE_FLOW_FIELD_IDX_IPV4_SA,
803 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
804 ICE_FLOW_FLD_OFF_INVAL, false);
805 else if (!tcp_ip4_spec->ip4src)
806 *perfect_fltr = false;
810 /* IP destination address */
811 if (tcp_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
812 ice_flow_set_fld(seg, ICE_FLOW_FIELD_IDX_IPV4_DA,
813 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
814 ICE_FLOW_FLD_OFF_INVAL, false);
815 else if (!tcp_ip4_spec->ip4dst)
816 *perfect_fltr = false;
820 /* Layer 4 source port */
821 if (tcp_ip4_spec->psrc == htons(0xFFFF))
822 ice_flow_set_fld(seg, src_port, ICE_FLOW_FLD_OFF_INVAL,
823 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
825 else if (!tcp_ip4_spec->psrc)
826 *perfect_fltr = false;
830 /* Layer 4 destination port */
831 if (tcp_ip4_spec->pdst == htons(0xFFFF))
832 ice_flow_set_fld(seg, dst_port, ICE_FLOW_FLD_OFF_INVAL,
833 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
835 else if (!tcp_ip4_spec->pdst)
836 *perfect_fltr = false;
844 * ice_set_fdir_ip4_usr_seg
845 * @seg: flow segment for programming
846 * @usr_ip4_spec: ethtool userdef packet offset
847 * @perfect_fltr: only valid on success; returns true if perfect filter,
850 * Set the offset data into the flow segment to be used to program HW
854 ice_set_fdir_ip4_usr_seg(struct ice_flow_seg_info *seg,
855 struct ethtool_usrip4_spec *usr_ip4_spec,
858 /* first 4 bytes of Layer 4 header */
859 if (usr_ip4_spec->l4_4_bytes)
861 if (usr_ip4_spec->tos)
863 if (usr_ip4_spec->ip_ver)
865 /* Filtering on Layer 4 protocol not supported */
866 if (usr_ip4_spec->proto)
868 /* empty rules are not valid */
869 if (!usr_ip4_spec->ip4src && !usr_ip4_spec->ip4dst)
872 *perfect_fltr = true;
873 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_IPV4);
875 /* IP source address */
876 if (usr_ip4_spec->ip4src == htonl(0xFFFFFFFF))
877 ice_flow_set_fld(seg, ICE_FLOW_FIELD_IDX_IPV4_SA,
878 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
879 ICE_FLOW_FLD_OFF_INVAL, false);
880 else if (!usr_ip4_spec->ip4src)
881 *perfect_fltr = false;
885 /* IP destination address */
886 if (usr_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
887 ice_flow_set_fld(seg, ICE_FLOW_FIELD_IDX_IPV4_DA,
888 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
889 ICE_FLOW_FLD_OFF_INVAL, false);
890 else if (!usr_ip4_spec->ip4dst)
891 *perfect_fltr = false;
899 * ice_set_fdir_ip6_seg
900 * @seg: flow segment for programming
901 * @tcp_ip6_spec: mask data from ethtool
902 * @l4_proto: Layer 4 protocol to program
903 * @perfect_fltr: only valid on success; returns true if perfect filter,
906 * Set the mask data into the flow segment to be used to program HW
907 * table based on provided L4 protocol for IPv6
910 ice_set_fdir_ip6_seg(struct ice_flow_seg_info *seg,
911 struct ethtool_tcpip6_spec *tcp_ip6_spec,
912 enum ice_flow_seg_hdr l4_proto, bool *perfect_fltr)
914 enum ice_flow_field src_port, dst_port;
916 /* make sure we don't have any empty rule */
917 if (!memcmp(tcp_ip6_spec->ip6src, &zero_ipv6_addr_mask,
918 sizeof(struct in6_addr)) &&
919 !memcmp(tcp_ip6_spec->ip6dst, &zero_ipv6_addr_mask,
920 sizeof(struct in6_addr)) &&
921 !tcp_ip6_spec->psrc && !tcp_ip6_spec->pdst)
924 /* filtering on TC not supported */
925 if (tcp_ip6_spec->tclass)
928 if (l4_proto == ICE_FLOW_SEG_HDR_TCP) {
929 src_port = ICE_FLOW_FIELD_IDX_TCP_SRC_PORT;
930 dst_port = ICE_FLOW_FIELD_IDX_TCP_DST_PORT;
931 } else if (l4_proto == ICE_FLOW_SEG_HDR_UDP) {
932 src_port = ICE_FLOW_FIELD_IDX_UDP_SRC_PORT;
933 dst_port = ICE_FLOW_FIELD_IDX_UDP_DST_PORT;
934 } else if (l4_proto == ICE_FLOW_SEG_HDR_SCTP) {
935 src_port = ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT;
936 dst_port = ICE_FLOW_FIELD_IDX_SCTP_DST_PORT;
941 *perfect_fltr = true;
942 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_IPV6 | l4_proto);
944 if (!memcmp(tcp_ip6_spec->ip6src, &full_ipv6_addr_mask,
945 sizeof(struct in6_addr)))
946 ice_flow_set_fld(seg, ICE_FLOW_FIELD_IDX_IPV6_SA,
947 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
948 ICE_FLOW_FLD_OFF_INVAL, false);
949 else if (!memcmp(tcp_ip6_spec->ip6src, &zero_ipv6_addr_mask,
950 sizeof(struct in6_addr)))
951 *perfect_fltr = false;
955 if (!memcmp(tcp_ip6_spec->ip6dst, &full_ipv6_addr_mask,
956 sizeof(struct in6_addr)))
957 ice_flow_set_fld(seg, ICE_FLOW_FIELD_IDX_IPV6_DA,
958 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
959 ICE_FLOW_FLD_OFF_INVAL, false);
960 else if (!memcmp(tcp_ip6_spec->ip6dst, &zero_ipv6_addr_mask,
961 sizeof(struct in6_addr)))
962 *perfect_fltr = false;
966 /* Layer 4 source port */
967 if (tcp_ip6_spec->psrc == htons(0xFFFF))
968 ice_flow_set_fld(seg, src_port, ICE_FLOW_FLD_OFF_INVAL,
969 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
971 else if (!tcp_ip6_spec->psrc)
972 *perfect_fltr = false;
976 /* Layer 4 destination port */
977 if (tcp_ip6_spec->pdst == htons(0xFFFF))
978 ice_flow_set_fld(seg, dst_port, ICE_FLOW_FLD_OFF_INVAL,
979 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
981 else if (!tcp_ip6_spec->pdst)
982 *perfect_fltr = false;
990 * ice_set_fdir_ip6_usr_seg
991 * @seg: flow segment for programming
992 * @usr_ip6_spec: ethtool userdef packet offset
993 * @perfect_fltr: only valid on success; returns true if perfect filter,
996 * Set the offset data into the flow segment to be used to program HW
1000 ice_set_fdir_ip6_usr_seg(struct ice_flow_seg_info *seg,
1001 struct ethtool_usrip6_spec *usr_ip6_spec,
1004 /* filtering on Layer 4 bytes not supported */
1005 if (usr_ip6_spec->l4_4_bytes)
1007 /* filtering on TC not supported */
1008 if (usr_ip6_spec->tclass)
1010 /* filtering on Layer 4 protocol not supported */
1011 if (usr_ip6_spec->l4_proto)
1013 /* empty rules are not valid */
1014 if (!memcmp(usr_ip6_spec->ip6src, &zero_ipv6_addr_mask,
1015 sizeof(struct in6_addr)) &&
1016 !memcmp(usr_ip6_spec->ip6dst, &zero_ipv6_addr_mask,
1017 sizeof(struct in6_addr)))
1020 *perfect_fltr = true;
1021 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_IPV6);
1023 if (!memcmp(usr_ip6_spec->ip6src, &full_ipv6_addr_mask,
1024 sizeof(struct in6_addr)))
1025 ice_flow_set_fld(seg, ICE_FLOW_FIELD_IDX_IPV6_SA,
1026 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
1027 ICE_FLOW_FLD_OFF_INVAL, false);
1028 else if (!memcmp(usr_ip6_spec->ip6src, &zero_ipv6_addr_mask,
1029 sizeof(struct in6_addr)))
1030 *perfect_fltr = false;
1034 if (!memcmp(usr_ip6_spec->ip6dst, &full_ipv6_addr_mask,
1035 sizeof(struct in6_addr)))
1036 ice_flow_set_fld(seg, ICE_FLOW_FIELD_IDX_IPV6_DA,
1037 ICE_FLOW_FLD_OFF_INVAL, ICE_FLOW_FLD_OFF_INVAL,
1038 ICE_FLOW_FLD_OFF_INVAL, false);
1039 else if (!memcmp(usr_ip6_spec->ip6dst, &zero_ipv6_addr_mask,
1040 sizeof(struct in6_addr)))
1041 *perfect_fltr = false;
1049 * ice_cfg_fdir_xtrct_seq - Configure extraction sequence for the given filter
1051 * @fsp: pointer to ethtool Rx flow specification
1052 * @user: user defined data from flow specification
1054 * Returns 0 on success.
1057 ice_cfg_fdir_xtrct_seq(struct ice_pf *pf, struct ethtool_rx_flow_spec *fsp,
1058 struct ice_rx_flow_userdef *user)
1060 struct ice_flow_seg_info *seg, *tun_seg;
1061 struct device *dev = ice_pf_to_dev(pf);
1062 enum ice_fltr_ptype fltr_idx;
1063 struct ice_hw *hw = &pf->hw;
1064 bool perfect_filter;
1067 seg = devm_kzalloc(dev, sizeof(*seg), GFP_KERNEL);
1071 tun_seg = devm_kzalloc(dev, sizeof(*seg) * ICE_FD_HW_SEG_MAX,
1074 devm_kfree(dev, seg);
1078 switch (fsp->flow_type & ~FLOW_EXT) {
1080 ret = ice_set_fdir_ip4_seg(seg, &fsp->m_u.tcp_ip4_spec,
1081 ICE_FLOW_SEG_HDR_TCP,
1085 ret = ice_set_fdir_ip4_seg(seg, &fsp->m_u.tcp_ip4_spec,
1086 ICE_FLOW_SEG_HDR_UDP,
1090 ret = ice_set_fdir_ip4_seg(seg, &fsp->m_u.tcp_ip4_spec,
1091 ICE_FLOW_SEG_HDR_SCTP,
1094 case IPV4_USER_FLOW:
1095 ret = ice_set_fdir_ip4_usr_seg(seg, &fsp->m_u.usr_ip4_spec,
1099 ret = ice_set_fdir_ip6_seg(seg, &fsp->m_u.tcp_ip6_spec,
1100 ICE_FLOW_SEG_HDR_TCP,
1104 ret = ice_set_fdir_ip6_seg(seg, &fsp->m_u.tcp_ip6_spec,
1105 ICE_FLOW_SEG_HDR_UDP,
1109 ret = ice_set_fdir_ip6_seg(seg, &fsp->m_u.tcp_ip6_spec,
1110 ICE_FLOW_SEG_HDR_SCTP,
1113 case IPV6_USER_FLOW:
1114 ret = ice_set_fdir_ip6_usr_seg(seg, &fsp->m_u.usr_ip6_spec,
1123 /* tunnel segments are shifted up one. */
1124 memcpy(&tun_seg[1], seg, sizeof(*seg));
1126 if (user && user->flex_fltr) {
1127 perfect_filter = false;
1128 ice_flow_add_fld_raw(seg, user->flex_offset,
1129 ICE_FLTR_PRGM_FLEX_WORD_SIZE,
1130 ICE_FLOW_FLD_OFF_INVAL,
1131 ICE_FLOW_FLD_OFF_INVAL);
1132 ice_flow_add_fld_raw(&tun_seg[1], user->flex_offset,
1133 ICE_FLTR_PRGM_FLEX_WORD_SIZE,
1134 ICE_FLOW_FLD_OFF_INVAL,
1135 ICE_FLOW_FLD_OFF_INVAL);
1138 /* add filter for outer headers */
1139 fltr_idx = ice_ethtool_flow_to_fltr(fsp->flow_type & ~FLOW_EXT);
1140 ret = ice_fdir_set_hw_fltr_rule(pf, seg, fltr_idx,
1141 ICE_FD_HW_SEG_NON_TUN);
1143 /* Rule already exists, free memory and continue */
1144 devm_kfree(dev, seg);
1146 /* could not write filter, free memory */
1149 /* make tunneled filter HW entries if possible */
1150 memcpy(&tun_seg[1], seg, sizeof(*seg));
1151 ret = ice_fdir_set_hw_fltr_rule(pf, tun_seg, fltr_idx,
1153 if (ret == -EEXIST) {
1154 /* Rule already exists, free memory and count as success */
1155 devm_kfree(dev, tun_seg);
1158 /* could not write tunnel filter, but outer filter exists */
1159 devm_kfree(dev, tun_seg);
1163 set_bit(fltr_idx, hw->fdir_perfect_fltr);
1165 clear_bit(fltr_idx, hw->fdir_perfect_fltr);
1170 devm_kfree(dev, tun_seg);
1171 devm_kfree(dev, seg);
1177 * ice_fdir_write_fltr - send a flow director filter to the hardware
1178 * @pf: PF data structure
1179 * @input: filter structure
1180 * @add: true adds filter and false removed filter
1181 * @is_tun: true adds inner filter on tunnel and false outer headers
1183 * returns 0 on success and negative value on error
1186 ice_fdir_write_fltr(struct ice_pf *pf, struct ice_fdir_fltr *input, bool add,
1189 struct device *dev = ice_pf_to_dev(pf);
1190 struct ice_hw *hw = &pf->hw;
1191 struct ice_fltr_desc desc;
1192 struct ice_vsi *ctrl_vsi;
1193 enum ice_status status;
1198 ctrl_vsi = ice_get_ctrl_vsi(pf);
1202 pkt = devm_kzalloc(dev, ICE_FDIR_MAX_RAW_PKT_SIZE, GFP_KERNEL);
1205 frag_pkt = devm_kzalloc(dev, ICE_FDIR_MAX_RAW_PKT_SIZE, GFP_KERNEL);
1211 ice_fdir_get_prgm_desc(hw, input, &desc, add);
1212 status = ice_fdir_get_gen_prgm_pkt(hw, input, pkt, false, is_tun);
1214 err = ice_status_to_errno(status);
1217 err = ice_prgm_fdir_fltr(ctrl_vsi, &desc, pkt);
1221 /* repeat for fragment packet */
1222 has_frag = ice_fdir_has_frag(input->flow_type);
1224 /* does not return error */
1225 ice_fdir_get_prgm_desc(hw, input, &desc, add);
1226 status = ice_fdir_get_gen_prgm_pkt(hw, input, frag_pkt, true,
1229 err = ice_status_to_errno(status);
1232 err = ice_prgm_fdir_fltr(ctrl_vsi, &desc, frag_pkt);
1236 devm_kfree(dev, frag_pkt);
1242 devm_kfree(dev, frag_pkt);
1244 devm_kfree(dev, pkt);
1248 devm_kfree(dev, frag_pkt);
1253 * ice_fdir_write_all_fltr - send a flow director filter to the hardware
1254 * @pf: PF data structure
1255 * @input: filter structure
1256 * @add: true adds filter and false removed filter
1258 * returns 0 on success and negative value on error
1261 ice_fdir_write_all_fltr(struct ice_pf *pf, struct ice_fdir_fltr *input,
1267 for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
1268 bool is_tun = tun == ICE_FD_HW_SEG_TUN;
1271 if (is_tun && !ice_get_open_tunnel_port(&pf->hw, &port_num))
1273 err = ice_fdir_write_fltr(pf, input, add, is_tun);
1281 * ice_fdir_replay_fltrs - replay filters from the HW filter list
1282 * @pf: board private structure
1284 void ice_fdir_replay_fltrs(struct ice_pf *pf)
1286 struct ice_fdir_fltr *f_rule;
1287 struct ice_hw *hw = &pf->hw;
1289 list_for_each_entry(f_rule, &hw->fdir_list_head, fltr_node) {
1290 int err = ice_fdir_write_all_fltr(pf, f_rule, true);
1293 dev_dbg(ice_pf_to_dev(pf), "Flow Director error %d, could not reprogram filter %d\n",
1294 err, f_rule->fltr_id);
1299 * ice_fdir_create_dflt_rules - create default perfect filters
1300 * @pf: PF data structure
1302 * Returns 0 for success or error.
1304 int ice_fdir_create_dflt_rules(struct ice_pf *pf)
1308 /* Create perfect TCP and UDP rules in hardware. */
1309 err = ice_create_init_fdir_rule(pf, ICE_FLTR_PTYPE_NONF_IPV4_TCP);
1313 err = ice_create_init_fdir_rule(pf, ICE_FLTR_PTYPE_NONF_IPV4_UDP);
1317 err = ice_create_init_fdir_rule(pf, ICE_FLTR_PTYPE_NONF_IPV6_TCP);
1321 err = ice_create_init_fdir_rule(pf, ICE_FLTR_PTYPE_NONF_IPV6_UDP);
1327 * ice_vsi_manage_fdir - turn on/off flow director
1328 * @vsi: the VSI being changed
1329 * @ena: boolean value indicating if this is an enable or disable request
1331 void ice_vsi_manage_fdir(struct ice_vsi *vsi, bool ena)
1333 struct ice_fdir_fltr *f_rule, *tmp;
1334 struct ice_pf *pf = vsi->back;
1335 struct ice_hw *hw = &pf->hw;
1336 enum ice_fltr_ptype flow;
1339 set_bit(ICE_FLAG_FD_ENA, pf->flags);
1340 ice_fdir_create_dflt_rules(pf);
1344 mutex_lock(&hw->fdir_fltr_lock);
1345 if (!test_and_clear_bit(ICE_FLAG_FD_ENA, pf->flags))
1347 list_for_each_entry_safe(f_rule, tmp, &hw->fdir_list_head, fltr_node) {
1348 /* ignore return value */
1349 ice_fdir_write_all_fltr(pf, f_rule, false);
1350 ice_fdir_update_cntrs(hw, f_rule->flow_type, false);
1351 list_del(&f_rule->fltr_node);
1352 devm_kfree(ice_hw_to_dev(hw), f_rule);
1356 for (flow = ICE_FLTR_PTYPE_NONF_NONE; flow < ICE_FLTR_PTYPE_MAX;
1358 if (hw->fdir_prof[flow])
1359 ice_fdir_rem_flow(hw, ICE_BLK_FD, flow);
1362 mutex_unlock(&hw->fdir_fltr_lock);
1366 * ice_fdir_do_rem_flow - delete flow and possibly add perfect flow
1368 * @flow_type: FDir flow type to release
1371 ice_fdir_do_rem_flow(struct ice_pf *pf, enum ice_fltr_ptype flow_type)
1373 struct ice_hw *hw = &pf->hw;
1374 bool need_perfect = false;
1376 if (flow_type == ICE_FLTR_PTYPE_NONF_IPV4_TCP ||
1377 flow_type == ICE_FLTR_PTYPE_NONF_IPV4_UDP ||
1378 flow_type == ICE_FLTR_PTYPE_NONF_IPV6_TCP ||
1379 flow_type == ICE_FLTR_PTYPE_NONF_IPV6_UDP)
1380 need_perfect = true;
1382 if (need_perfect && test_bit(flow_type, hw->fdir_perfect_fltr))
1385 ice_fdir_rem_flow(hw, ICE_BLK_FD, flow_type);
1387 ice_create_init_fdir_rule(pf, flow_type);
1391 * ice_fdir_update_list_entry - add or delete a filter from the filter list
1393 * @input: filter structure
1394 * @fltr_idx: ethtool index of filter to modify
1396 * returns 0 on success and negative on errors
1399 ice_fdir_update_list_entry(struct ice_pf *pf, struct ice_fdir_fltr *input,
1402 struct ice_fdir_fltr *old_fltr;
1403 struct ice_hw *hw = &pf->hw;
1406 /* Do not update filters during reset */
1407 if (ice_is_reset_in_progress(pf->state))
1410 old_fltr = ice_fdir_find_fltr_by_idx(hw, fltr_idx);
1412 err = ice_fdir_write_all_fltr(pf, old_fltr, false);
1415 ice_fdir_update_cntrs(hw, old_fltr->flow_type, false);
1416 if (!input && !hw->fdir_fltr_cnt[old_fltr->flow_type])
1417 /* we just deleted the last filter of flow_type so we
1418 * should also delete the HW filter info.
1420 ice_fdir_do_rem_flow(pf, old_fltr->flow_type);
1421 list_del(&old_fltr->fltr_node);
1422 devm_kfree(ice_hw_to_dev(hw), old_fltr);
1426 ice_fdir_list_add_fltr(hw, input);
1427 ice_fdir_update_cntrs(hw, input->flow_type, true);
1432 * ice_del_fdir_ethtool - delete Flow Director filter
1433 * @vsi: pointer to target VSI
1434 * @cmd: command to add or delete Flow Director filter
1436 * Returns 0 on success and negative values for failure
1438 int ice_del_fdir_ethtool(struct ice_vsi *vsi, struct ethtool_rxnfc *cmd)
1440 struct ethtool_rx_flow_spec *fsp =
1441 (struct ethtool_rx_flow_spec *)&cmd->fs;
1442 struct ice_pf *pf = vsi->back;
1443 struct ice_hw *hw = &pf->hw;
1446 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
1449 /* Do not delete filters during reset */
1450 if (ice_is_reset_in_progress(pf->state)) {
1451 dev_err(ice_pf_to_dev(pf), "Device is resetting - deleting Flow Director filters not supported during reset\n");
1455 if (test_bit(ICE_FD_FLUSH_REQ, pf->state))
1458 mutex_lock(&hw->fdir_fltr_lock);
1459 val = ice_fdir_update_list_entry(pf, NULL, fsp->location);
1460 mutex_unlock(&hw->fdir_fltr_lock);
1466 * ice_set_fdir_input_set - Set the input set for Flow Director
1467 * @vsi: pointer to target VSI
1468 * @fsp: pointer to ethtool Rx flow specification
1469 * @input: filter structure
1472 ice_set_fdir_input_set(struct ice_vsi *vsi, struct ethtool_rx_flow_spec *fsp,
1473 struct ice_fdir_fltr *input)
1475 u16 dest_vsi, q_index = 0;
1481 if (!vsi || !fsp || !input)
1487 dest_vsi = vsi->idx;
1488 if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
1489 dest_ctl = ICE_FLTR_PRGM_DESC_DEST_DROP_PKT;
1491 u32 ring = ethtool_get_flow_spec_ring(fsp->ring_cookie);
1492 u8 vf = ethtool_get_flow_spec_ring_vf(fsp->ring_cookie);
1495 dev_err(ice_pf_to_dev(pf), "Failed to add filter. Flow director filters are not supported on VF queues.\n");
1499 if (ring >= vsi->num_rxq)
1502 dest_ctl = ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_QINDEX;
1506 input->fltr_id = fsp->location;
1507 input->q_index = q_index;
1508 flow_type = fsp->flow_type & ~FLOW_EXT;
1510 input->dest_vsi = dest_vsi;
1511 input->dest_ctl = dest_ctl;
1512 input->fltr_status = ICE_FLTR_PRGM_DESC_FD_STATUS_FD_ID;
1513 input->cnt_index = ICE_FD_SB_STAT_IDX(hw->fd_ctr_base);
1514 input->flow_type = ice_ethtool_flow_to_fltr(flow_type);
1516 if (fsp->flow_type & FLOW_EXT) {
1517 memcpy(input->ext_data.usr_def, fsp->h_ext.data,
1518 sizeof(input->ext_data.usr_def));
1519 input->ext_data.vlan_type = fsp->h_ext.vlan_etype;
1520 input->ext_data.vlan_tag = fsp->h_ext.vlan_tci;
1521 memcpy(input->ext_mask.usr_def, fsp->m_ext.data,
1522 sizeof(input->ext_mask.usr_def));
1523 input->ext_mask.vlan_type = fsp->m_ext.vlan_etype;
1524 input->ext_mask.vlan_tag = fsp->m_ext.vlan_tci;
1527 switch (flow_type) {
1531 input->ip.v4.dst_port = fsp->h_u.tcp_ip4_spec.pdst;
1532 input->ip.v4.src_port = fsp->h_u.tcp_ip4_spec.psrc;
1533 input->ip.v4.dst_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
1534 input->ip.v4.src_ip = fsp->h_u.tcp_ip4_spec.ip4src;
1535 input->mask.v4.dst_port = fsp->m_u.tcp_ip4_spec.pdst;
1536 input->mask.v4.src_port = fsp->m_u.tcp_ip4_spec.psrc;
1537 input->mask.v4.dst_ip = fsp->m_u.tcp_ip4_spec.ip4dst;
1538 input->mask.v4.src_ip = fsp->m_u.tcp_ip4_spec.ip4src;
1540 case IPV4_USER_FLOW:
1541 input->ip.v4.dst_ip = fsp->h_u.usr_ip4_spec.ip4dst;
1542 input->ip.v4.src_ip = fsp->h_u.usr_ip4_spec.ip4src;
1543 input->ip.v4.l4_header = fsp->h_u.usr_ip4_spec.l4_4_bytes;
1544 input->ip.v4.proto = fsp->h_u.usr_ip4_spec.proto;
1545 input->ip.v4.ip_ver = fsp->h_u.usr_ip4_spec.ip_ver;
1546 input->ip.v4.tos = fsp->h_u.usr_ip4_spec.tos;
1547 input->mask.v4.dst_ip = fsp->m_u.usr_ip4_spec.ip4dst;
1548 input->mask.v4.src_ip = fsp->m_u.usr_ip4_spec.ip4src;
1549 input->mask.v4.l4_header = fsp->m_u.usr_ip4_spec.l4_4_bytes;
1550 input->mask.v4.proto = fsp->m_u.usr_ip4_spec.proto;
1551 input->mask.v4.ip_ver = fsp->m_u.usr_ip4_spec.ip_ver;
1552 input->mask.v4.tos = fsp->m_u.usr_ip4_spec.tos;
1557 memcpy(input->ip.v6.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1558 sizeof(struct in6_addr));
1559 memcpy(input->ip.v6.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1560 sizeof(struct in6_addr));
1561 input->ip.v6.dst_port = fsp->h_u.tcp_ip6_spec.pdst;
1562 input->ip.v6.src_port = fsp->h_u.tcp_ip6_spec.psrc;
1563 input->ip.v6.tc = fsp->h_u.tcp_ip6_spec.tclass;
1564 memcpy(input->mask.v6.dst_ip, fsp->m_u.tcp_ip6_spec.ip6dst,
1565 sizeof(struct in6_addr));
1566 memcpy(input->mask.v6.src_ip, fsp->m_u.tcp_ip6_spec.ip6src,
1567 sizeof(struct in6_addr));
1568 input->mask.v6.dst_port = fsp->m_u.tcp_ip6_spec.pdst;
1569 input->mask.v6.src_port = fsp->m_u.tcp_ip6_spec.psrc;
1570 input->mask.v6.tc = fsp->m_u.tcp_ip6_spec.tclass;
1572 case IPV6_USER_FLOW:
1573 memcpy(input->ip.v6.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1574 sizeof(struct in6_addr));
1575 memcpy(input->ip.v6.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1576 sizeof(struct in6_addr));
1577 input->ip.v6.l4_header = fsp->h_u.usr_ip6_spec.l4_4_bytes;
1578 input->ip.v6.tc = fsp->h_u.usr_ip6_spec.tclass;
1580 /* if no protocol requested, use IPPROTO_NONE */
1581 if (!fsp->m_u.usr_ip6_spec.l4_proto)
1582 input->ip.v6.proto = IPPROTO_NONE;
1584 input->ip.v6.proto = fsp->h_u.usr_ip6_spec.l4_proto;
1586 memcpy(input->mask.v6.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1587 sizeof(struct in6_addr));
1588 memcpy(input->mask.v6.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1589 sizeof(struct in6_addr));
1590 input->mask.v6.l4_header = fsp->m_u.usr_ip6_spec.l4_4_bytes;
1591 input->mask.v6.tc = fsp->m_u.usr_ip6_spec.tclass;
1592 input->mask.v6.proto = fsp->m_u.usr_ip6_spec.l4_proto;
1595 /* not doing un-parsed flow types */
1603 * ice_add_fdir_ethtool - Add/Remove Flow Director filter
1604 * @vsi: pointer to target VSI
1605 * @cmd: command to add or delete Flow Director filter
1607 * Returns 0 on success and negative values for failure
1609 int ice_add_fdir_ethtool(struct ice_vsi *vsi, struct ethtool_rxnfc *cmd)
1611 struct ice_rx_flow_userdef userdata;
1612 struct ethtool_rx_flow_spec *fsp;
1613 struct ice_fdir_fltr *input;
1626 dev = ice_pf_to_dev(pf);
1628 if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
1631 /* Do not program filters during reset */
1632 if (ice_is_reset_in_progress(pf->state)) {
1633 dev_err(dev, "Device is resetting - adding Flow Director filters not supported during reset\n");
1637 fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1639 if (ice_parse_rx_flow_user_data(fsp, &userdata))
1642 if (fsp->flow_type & FLOW_MAC_EXT)
1645 ret = ice_cfg_fdir_xtrct_seq(pf, fsp, &userdata);
1649 if (fsp->location >= ice_get_fdir_cnt_all(hw)) {
1650 dev_err(dev, "Failed to add filter. The maximum number of flow director filters has been reached.\n");
1654 /* return error if not an update and no available filters */
1655 fltrs_needed = ice_get_open_tunnel_port(hw, &tunnel_port) ? 2 : 1;
1656 if (!ice_fdir_find_fltr_by_idx(hw, fsp->location) &&
1657 ice_fdir_num_avail_fltr(hw, pf->vsi[vsi->idx]) < fltrs_needed) {
1658 dev_err(dev, "Failed to add filter. The maximum number of flow director filters has been reached.\n");
1662 input = devm_kzalloc(dev, sizeof(*input), GFP_KERNEL);
1666 ret = ice_set_fdir_input_set(vsi, fsp, input);
1670 mutex_lock(&hw->fdir_fltr_lock);
1671 if (ice_fdir_is_dup_fltr(hw, input)) {
1676 if (userdata.flex_fltr) {
1677 input->flex_fltr = true;
1678 input->flex_word = cpu_to_be16(userdata.flex_word);
1679 input->flex_offset = userdata.flex_offset;
1682 input->cnt_ena = ICE_FXD_FLTR_QW0_STAT_ENA_PKTS;
1683 input->fdid_prio = ICE_FXD_FLTR_QW1_FDID_PRI_THREE;
1684 input->comp_report = ICE_FXD_FLTR_QW0_COMP_REPORT_SW_FAIL;
1686 /* input struct is added to the HW filter list */
1687 ice_fdir_update_list_entry(pf, input, fsp->location);
1689 ret = ice_fdir_write_all_fltr(pf, input, true);
1691 goto remove_sw_rule;
1696 ice_fdir_update_cntrs(hw, input->flow_type, false);
1697 list_del(&input->fltr_node);
1699 mutex_unlock(&hw->fdir_fltr_lock);
1702 devm_kfree(dev, input);