1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Chelsio Communications.
7 #include "base/common.h"
8 #include "base/t4_tcb.h"
9 #include "base/t4_regs.h"
10 #include "cxgbe_filter.h"
16 * Initialize Hash Filters
18 int cxgbe_init_hash_filter(struct adapter *adap)
20 unsigned int n_user_filters;
21 unsigned int user_filter_perc;
23 u32 params[7], val[7];
25 #define FW_PARAM_DEV(param) \
26 (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
27 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
29 #define FW_PARAM_PFVF(param) \
30 (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
31 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param) | \
32 V_FW_PARAMS_PARAM_Y(0) | \
33 V_FW_PARAMS_PARAM_Z(0))
35 params[0] = FW_PARAM_DEV(NTID);
36 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
40 adap->tids.ntids = val[0];
41 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
43 user_filter_perc = 100;
44 n_user_filters = mult_frac(adap->tids.nftids,
48 adap->tids.nftids = n_user_filters;
49 adap->params.hash_filter = 1;
54 * Validate if the requested filter specification can be set by checking
55 * if the requested features have been enabled
57 int cxgbe_validate_filter(struct adapter *adapter,
58 struct ch_filter_specification *fs)
63 * Check for unconfigured fields being used.
65 fconf = fs->cap ? adapter->params.tp.filter_mask :
66 adapter->params.tp.vlan_pri_map;
68 iconf = adapter->params.tp.ingress_config;
71 (fs->val._field || fs->mask._field)
72 #define U(_mask, _field) \
73 (!(fconf & (_mask)) && S(_field))
75 if (U(F_PORT, iport) || U(F_ETHERTYPE, ethtype) ||
76 U(F_PROTOCOL, proto) || U(F_MACMATCH, macidx) ||
77 U(F_VLAN, ivlan_vld) || U(F_VNIC_ID, ovlan_vld) ||
78 U(F_TOS, tos) || U(F_VNIC_ID, pfvf_vld))
81 /* Either OVLAN or PFVF match is enabled in hardware, but not both */
82 if ((S(pfvf_vld) && !(iconf & F_VNIC)) ||
83 (S(ovlan_vld) && (iconf & F_VNIC)))
86 /* To use OVLAN or PFVF, L4 encapsulation match must not be enabled */
87 if ((S(ovlan_vld) && (iconf & F_USE_ENC_IDX)) ||
88 (S(pfvf_vld) && (iconf & F_USE_ENC_IDX)))
95 * If the user is requesting that the filter action loop
96 * matching packets back out one of our ports, make sure that
97 * the egress port is in range.
99 if (fs->action == FILTER_SWITCH &&
100 fs->eport >= adapter->params.nports)
104 * Don't allow various trivially obvious bogus out-of-range
107 if (fs->val.iport >= adapter->params.nports)
110 if (!fs->cap && fs->nat_mode && !adapter->params.filter2_wr_support)
113 if (!fs->cap && fs->swapmac && !adapter->params.filter2_wr_support)
120 * Get the queue to which the traffic must be steered to.
122 static unsigned int get_filter_steerq(struct rte_eth_dev *dev,
123 struct ch_filter_specification *fs)
125 struct port_info *pi = ethdev2pinfo(dev);
126 struct adapter *adapter = pi->adapter;
130 * If the user has requested steering matching Ingress Packets
131 * to a specific Queue Set, we need to make sure it's in range
132 * for the port and map that into the Absolute Queue ID of the
133 * Queue Set's Response Queue.
139 * If the iq id is greater than the number of qsets,
140 * then assume it is an absolute qid.
142 if (fs->iq < pi->n_rx_qsets)
143 iq = adapter->sge.ethrxq[pi->first_qset +
152 /* Return an error number if the indicated filter isn't writable ... */
153 static int writable_filter(struct filter_entry *f)
164 * Send CPL_SET_TCB_FIELD message
166 static void set_tcb_field(struct adapter *adapter, unsigned int ftid,
167 u16 word, u64 mask, u64 val, int no_reply)
169 struct rte_mbuf *mbuf;
170 struct cpl_set_tcb_field *req;
171 struct sge_ctrl_txq *ctrlq;
173 ctrlq = &adapter->sge.ctrlq[0];
174 mbuf = rte_pktmbuf_alloc(ctrlq->mb_pool);
177 mbuf->data_len = sizeof(*req);
178 mbuf->pkt_len = mbuf->data_len;
180 req = rte_pktmbuf_mtod(mbuf, struct cpl_set_tcb_field *);
181 memset(req, 0, sizeof(*req));
182 INIT_TP_WR_MIT_CPL(req, CPL_SET_TCB_FIELD, ftid);
183 req->reply_ctrl = cpu_to_be16(V_REPLY_CHAN(0) |
184 V_QUEUENO(adapter->sge.fw_evtq.abs_id) |
185 V_NO_REPLY(no_reply));
186 req->word_cookie = cpu_to_be16(V_WORD(word) | V_COOKIE(ftid));
187 req->mask = cpu_to_be64(mask);
188 req->val = cpu_to_be64(val);
190 t4_mgmt_tx(ctrlq, mbuf);
194 * Set one of the t_flags bits in the TCB.
196 static void set_tcb_tflag(struct adapter *adap, unsigned int ftid,
197 unsigned int bit_pos, unsigned int val, int no_reply)
199 set_tcb_field(adap, ftid, W_TCB_T_FLAGS, 1ULL << bit_pos,
200 (unsigned long long)val << bit_pos, no_reply);
204 * Build a CPL_SET_TCB_FIELD message as payload of a ULP_TX_PKT command.
206 static inline void mk_set_tcb_field_ulp(struct filter_entry *f,
207 struct cpl_set_tcb_field *req,
209 u64 mask, u64 val, u8 cookie,
212 struct ulp_txpkt *txpkt = (struct ulp_txpkt *)req;
213 struct ulptx_idata *sc = (struct ulptx_idata *)(txpkt + 1);
215 txpkt->cmd_dest = cpu_to_be32(V_ULPTX_CMD(ULP_TX_PKT) |
216 V_ULP_TXPKT_DEST(0));
217 txpkt->len = cpu_to_be32(DIV_ROUND_UP(sizeof(*req), 16));
218 sc->cmd_more = cpu_to_be32(V_ULPTX_CMD(ULP_TX_SC_IMM));
219 sc->len = cpu_to_be32(sizeof(*req) - sizeof(struct work_request_hdr));
220 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_SET_TCB_FIELD, f->tid));
221 req->reply_ctrl = cpu_to_be16(V_NO_REPLY(no_reply) | V_REPLY_CHAN(0) |
223 req->word_cookie = cpu_to_be16(V_WORD(word) | V_COOKIE(cookie));
224 req->mask = cpu_to_be64(mask);
225 req->val = cpu_to_be64(val);
226 sc = (struct ulptx_idata *)(req + 1);
227 sc->cmd_more = cpu_to_be32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
228 sc->len = cpu_to_be32(0);
232 * IPv6 requires 2 slots on T6 and 4 slots for cards below T6.
233 * IPv4 requires only 1 slot on all cards.
235 u8 cxgbe_filter_slots(struct adapter *adap, u8 family)
237 if (family == FILTER_TYPE_IPV6) {
238 if (CHELSIO_CHIP_VERSION(adap->params.chip) < CHELSIO_T6)
248 * Check if entries are already filled.
250 bool cxgbe_is_filter_set(struct tid_info *t, u32 fidx, u8 nentries)
255 /* Ensure there's enough slots available. */
256 t4_os_lock(&t->ftid_lock);
257 for (i = fidx; i < fidx + nentries; i++) {
258 if (rte_bitmap_get(t->ftid_bmap, i)) {
263 t4_os_unlock(&t->ftid_lock);
268 * Allocate available free entries.
270 int cxgbe_alloc_ftid(struct adapter *adap, u8 nentries)
272 struct tid_info *t = &adap->tids;
274 int size = t->nftids;
276 t4_os_lock(&t->ftid_lock);
278 pos = cxgbe_bitmap_find_free_region(t->ftid_bmap, size,
281 pos = cxgbe_find_first_zero_bit(t->ftid_bmap, size);
282 t4_os_unlock(&t->ftid_lock);
284 return pos < size ? pos : -1;
288 * Clear a filter and release any of its resources that we own. This also
289 * clears the filter's "pending" status.
291 static void clear_filter(struct filter_entry *f)
294 cxgbe_clip_release(f->dev, f->clipt);
297 cxgbe_l2t_release(f->l2t);
299 /* The zeroing of the filter rule below clears the filter valid,
300 * pending, locked flags etc. so it's all we need for
303 memset(f, 0, sizeof(*f));
307 * Construct hash filter ntuple.
309 static u64 hash_filter_ntuple(const struct filter_entry *f)
311 struct adapter *adap = ethdev2adap(f->dev);
312 struct tp_params *tp = &adap->params.tp;
314 u16 tcp_proto = IPPROTO_TCP; /* TCP Protocol Number */
316 if (tp->port_shift >= 0 && f->fs.mask.iport)
317 ntuple |= (u64)f->fs.val.iport << tp->port_shift;
319 if (tp->protocol_shift >= 0) {
320 if (!f->fs.val.proto)
321 ntuple |= (u64)tcp_proto << tp->protocol_shift;
323 ntuple |= (u64)f->fs.val.proto << tp->protocol_shift;
326 if (tp->ethertype_shift >= 0 && f->fs.mask.ethtype)
327 ntuple |= (u64)(f->fs.val.ethtype) << tp->ethertype_shift;
328 if (tp->macmatch_shift >= 0 && f->fs.mask.macidx)
329 ntuple |= (u64)(f->fs.val.macidx) << tp->macmatch_shift;
330 if (tp->vlan_shift >= 0 && f->fs.mask.ivlan)
331 ntuple |= (u64)(F_FT_VLAN_VLD | f->fs.val.ivlan) <<
333 if (tp->vnic_shift >= 0) {
334 if ((adap->params.tp.ingress_config & F_VNIC) &&
336 ntuple |= (u64)(f->fs.val.pfvf_vld << 16 |
337 f->fs.val.pf << 13 | f->fs.val.vf) <<
339 else if (!(adap->params.tp.ingress_config & F_VNIC) &&
340 f->fs.mask.ovlan_vld)
341 ntuple |= (u64)(f->fs.val.ovlan_vld << 16 |
342 f->fs.val.ovlan) << tp->vnic_shift;
344 if (tp->tos_shift >= 0 && f->fs.mask.tos)
345 ntuple |= (u64)f->fs.val.tos << tp->tos_shift;
351 * Build a CPL_ABORT_REQ message as payload of a ULP_TX_PKT command.
353 static void mk_abort_req_ulp(struct cpl_abort_req *abort_req,
356 struct ulp_txpkt *txpkt = (struct ulp_txpkt *)abort_req;
357 struct ulptx_idata *sc = (struct ulptx_idata *)(txpkt + 1);
359 txpkt->cmd_dest = cpu_to_be32(V_ULPTX_CMD(ULP_TX_PKT) |
360 V_ULP_TXPKT_DEST(0));
361 txpkt->len = cpu_to_be32(DIV_ROUND_UP(sizeof(*abort_req), 16));
362 sc->cmd_more = cpu_to_be32(V_ULPTX_CMD(ULP_TX_SC_IMM));
363 sc->len = cpu_to_be32(sizeof(*abort_req) -
364 sizeof(struct work_request_hdr));
365 OPCODE_TID(abort_req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, tid));
366 abort_req->rsvd0 = cpu_to_be32(0);
367 abort_req->rsvd1 = 0;
368 abort_req->cmd = CPL_ABORT_NO_RST;
369 sc = (struct ulptx_idata *)(abort_req + 1);
370 sc->cmd_more = cpu_to_be32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
371 sc->len = cpu_to_be32(0);
375 * Build a CPL_ABORT_RPL message as payload of a ULP_TX_PKT command.
377 static void mk_abort_rpl_ulp(struct cpl_abort_rpl *abort_rpl,
380 struct ulp_txpkt *txpkt = (struct ulp_txpkt *)abort_rpl;
381 struct ulptx_idata *sc = (struct ulptx_idata *)(txpkt + 1);
383 txpkt->cmd_dest = cpu_to_be32(V_ULPTX_CMD(ULP_TX_PKT) |
384 V_ULP_TXPKT_DEST(0));
385 txpkt->len = cpu_to_be32(DIV_ROUND_UP(sizeof(*abort_rpl), 16));
386 sc->cmd_more = cpu_to_be32(V_ULPTX_CMD(ULP_TX_SC_IMM));
387 sc->len = cpu_to_be32(sizeof(*abort_rpl) -
388 sizeof(struct work_request_hdr));
389 OPCODE_TID(abort_rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, tid));
390 abort_rpl->rsvd0 = cpu_to_be32(0);
391 abort_rpl->rsvd1 = 0;
392 abort_rpl->cmd = CPL_ABORT_NO_RST;
393 sc = (struct ulptx_idata *)(abort_rpl + 1);
394 sc->cmd_more = cpu_to_be32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
395 sc->len = cpu_to_be32(0);
399 * Delete the specified hash filter.
401 static int cxgbe_del_hash_filter(struct rte_eth_dev *dev,
402 unsigned int filter_id,
403 struct filter_ctx *ctx)
405 struct adapter *adapter = ethdev2adap(dev);
406 struct tid_info *t = &adapter->tids;
407 struct filter_entry *f;
408 struct sge_ctrl_txq *ctrlq;
409 unsigned int port_id = ethdev2pinfo(dev)->port_id;
412 if (filter_id > adapter->tids.ntids)
415 f = lookup_tid(t, filter_id);
417 dev_err(adapter, "%s: no filter entry for filter_id = %d\n",
418 __func__, filter_id);
422 ret = writable_filter(f);
428 struct rte_mbuf *mbuf;
429 struct work_request_hdr *wr;
430 struct ulptx_idata *aligner;
431 struct cpl_set_tcb_field *req;
432 struct cpl_abort_req *abort_req;
433 struct cpl_abort_rpl *abort_rpl;
438 wrlen = cxgbe_roundup(sizeof(*wr) +
439 (sizeof(*req) + sizeof(*aligner)) +
440 sizeof(*abort_req) + sizeof(*abort_rpl),
443 ctrlq = &adapter->sge.ctrlq[port_id];
444 mbuf = rte_pktmbuf_alloc(ctrlq->mb_pool);
446 dev_err(adapter, "%s: could not allocate skb ..\n",
451 mbuf->data_len = wrlen;
452 mbuf->pkt_len = mbuf->data_len;
454 req = rte_pktmbuf_mtod(mbuf, struct cpl_set_tcb_field *);
455 INIT_ULPTX_WR(req, wrlen, 0, 0);
456 wr = (struct work_request_hdr *)req;
458 req = (struct cpl_set_tcb_field *)wr;
459 mk_set_tcb_field_ulp(f, req, W_TCB_RSS_INFO,
460 V_TCB_RSS_INFO(M_TCB_RSS_INFO),
461 V_TCB_RSS_INFO(adapter->sge.fw_evtq.abs_id),
463 aligner = (struct ulptx_idata *)(req + 1);
464 abort_req = (struct cpl_abort_req *)(aligner + 1);
465 mk_abort_req_ulp(abort_req, f->tid);
466 abort_rpl = (struct cpl_abort_rpl *)(abort_req + 1);
467 mk_abort_rpl_ulp(abort_rpl, f->tid);
468 t4_mgmt_tx(ctrlq, mbuf);
477 * Build a ACT_OPEN_REQ6 message for setting IPv6 hash filter.
479 static void mk_act_open_req6(struct filter_entry *f, struct rte_mbuf *mbuf,
480 unsigned int qid_filterid, struct adapter *adap)
482 struct cpl_t6_act_open_req6 *req = NULL;
483 u64 local_lo, local_hi, peer_lo, peer_hi;
484 u32 *lip = (u32 *)f->fs.val.lip;
485 u32 *fip = (u32 *)f->fs.val.fip;
487 switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
489 req = rte_pktmbuf_mtod(mbuf, struct cpl_t6_act_open_req6 *);
494 dev_err(adap, "%s: unsupported chip type!\n", __func__);
498 local_hi = ((u64)lip[1]) << 32 | lip[0];
499 local_lo = ((u64)lip[3]) << 32 | lip[2];
500 peer_hi = ((u64)fip[1]) << 32 | fip[0];
501 peer_lo = ((u64)fip[3]) << 32 | fip[2];
503 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
505 req->local_port = cpu_to_be16(f->fs.val.lport);
506 req->peer_port = cpu_to_be16(f->fs.val.fport);
507 req->local_ip_hi = local_hi;
508 req->local_ip_lo = local_lo;
509 req->peer_ip_hi = peer_hi;
510 req->peer_ip_lo = peer_lo;
511 req->opt0 = cpu_to_be64(V_NAGLE(f->fs.newvlan == VLAN_REMOVE ||
512 f->fs.newvlan == VLAN_REWRITE) |
513 V_DELACK(f->fs.hitcnts) |
514 V_L2T_IDX(f->l2t ? f->l2t->idx : 0) |
515 V_SMAC_SEL((cxgbe_port_viid(f->dev) & 0x7F)
517 V_TX_CHAN(f->fs.eport) |
518 V_ULP_MODE(ULP_MODE_NONE) |
519 F_TCAM_BYPASS | F_NON_OFFLOAD);
520 req->params = cpu_to_be64(V_FILTER_TUPLE(hash_filter_ntuple(f)));
521 req->opt2 = cpu_to_be32(F_RSS_QUEUE_VALID |
522 V_RSS_QUEUE(f->fs.iq) |
525 V_SACK_EN(f->fs.swapmac) |
526 V_CONG_CNTRL((f->fs.action == FILTER_DROP) |
527 (f->fs.dirsteer << 1)) |
528 V_CCTRL_ECN(f->fs.action == FILTER_SWITCH));
532 * Build a ACT_OPEN_REQ message for setting IPv4 hash filter.
534 static void mk_act_open_req(struct filter_entry *f, struct rte_mbuf *mbuf,
535 unsigned int qid_filterid, struct adapter *adap)
537 struct cpl_t6_act_open_req *req = NULL;
539 switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
541 req = rte_pktmbuf_mtod(mbuf, struct cpl_t6_act_open_req *);
546 dev_err(adap, "%s: unsupported chip type!\n", __func__);
550 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
552 req->local_port = cpu_to_be16(f->fs.val.lport);
553 req->peer_port = cpu_to_be16(f->fs.val.fport);
554 req->local_ip = f->fs.val.lip[0] | f->fs.val.lip[1] << 8 |
555 f->fs.val.lip[2] << 16 | f->fs.val.lip[3] << 24;
556 req->peer_ip = f->fs.val.fip[0] | f->fs.val.fip[1] << 8 |
557 f->fs.val.fip[2] << 16 | f->fs.val.fip[3] << 24;
558 req->opt0 = cpu_to_be64(V_NAGLE(f->fs.newvlan == VLAN_REMOVE ||
559 f->fs.newvlan == VLAN_REWRITE) |
560 V_DELACK(f->fs.hitcnts) |
561 V_L2T_IDX(f->l2t ? f->l2t->idx : 0) |
562 V_SMAC_SEL((cxgbe_port_viid(f->dev) & 0x7F)
564 V_TX_CHAN(f->fs.eport) |
565 V_ULP_MODE(ULP_MODE_NONE) |
566 F_TCAM_BYPASS | F_NON_OFFLOAD);
567 req->params = cpu_to_be64(V_FILTER_TUPLE(hash_filter_ntuple(f)));
568 req->opt2 = cpu_to_be32(F_RSS_QUEUE_VALID |
569 V_RSS_QUEUE(f->fs.iq) |
572 V_SACK_EN(f->fs.swapmac) |
573 V_CONG_CNTRL((f->fs.action == FILTER_DROP) |
574 (f->fs.dirsteer << 1)) |
575 V_CCTRL_ECN(f->fs.action == FILTER_SWITCH));
579 * Set the specified hash filter.
581 static int cxgbe_set_hash_filter(struct rte_eth_dev *dev,
582 struct ch_filter_specification *fs,
583 struct filter_ctx *ctx)
585 struct port_info *pi = ethdev2pinfo(dev);
586 struct adapter *adapter = pi->adapter;
587 struct tid_info *t = &adapter->tids;
588 struct filter_entry *f;
589 struct rte_mbuf *mbuf;
590 struct sge_ctrl_txq *ctrlq;
595 ret = cxgbe_validate_filter(adapter, fs);
599 iq = get_filter_steerq(dev, fs);
601 ctrlq = &adapter->sge.ctrlq[pi->port_id];
603 f = t4_os_alloc(sizeof(*f));
613 * If the new filter requires loopback Destination MAC and/or VLAN
614 * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
617 if (f->fs.newdmac || f->fs.newvlan == VLAN_INSERT ||
618 f->fs.newvlan == VLAN_REWRITE) {
619 /* allocate L2T entry for new filter */
620 f->l2t = cxgbe_l2t_alloc_switching(dev, f->fs.vlan,
621 f->fs.eport, f->fs.dmac);
628 /* If the new filter requires Source MAC rewriting then we need to
629 * allocate a SMT entry for the filter
632 f->smt = cxgbe_smt_alloc_switching(f->dev, f->fs.smac);
639 atid = cxgbe_alloc_atid(t, f);
643 if (f->fs.type == FILTER_TYPE_IPV6) {
644 /* IPv6 hash filter */
645 f->clipt = cxgbe_clip_alloc(f->dev, (u32 *)&f->fs.val.lip);
649 size = sizeof(struct cpl_t6_act_open_req6);
650 mbuf = rte_pktmbuf_alloc(ctrlq->mb_pool);
656 mbuf->data_len = size;
657 mbuf->pkt_len = mbuf->data_len;
659 mk_act_open_req6(f, mbuf,
660 ((adapter->sge.fw_evtq.abs_id << 14) | atid),
663 /* IPv4 hash filter */
664 size = sizeof(struct cpl_t6_act_open_req);
665 mbuf = rte_pktmbuf_alloc(ctrlq->mb_pool);
671 mbuf->data_len = size;
672 mbuf->pkt_len = mbuf->data_len;
674 mk_act_open_req(f, mbuf,
675 ((adapter->sge.fw_evtq.abs_id << 14) | atid),
680 t4_mgmt_tx(ctrlq, mbuf);
684 cxgbe_free_atid(t, atid);
693 * t4_mk_filtdelwr - create a delete filter WR
694 * @adap: adapter context
695 * @ftid: the filter ID
696 * @wr: the filter work request to populate
697 * @qid: ingress queue to receive the delete notification
699 * Creates a filter work request to delete the supplied filter. If @qid is
700 * negative the delete notification is suppressed.
702 static void t4_mk_filtdelwr(struct adapter *adap, unsigned int ftid,
703 struct fw_filter2_wr *wr, int qid)
705 memset(wr, 0, sizeof(*wr));
706 if (adap->params.filter2_wr_support)
707 wr->op_pkd = cpu_to_be32(V_FW_WR_OP(FW_FILTER2_WR));
709 wr->op_pkd = cpu_to_be32(V_FW_WR_OP(FW_FILTER_WR));
710 wr->len16_pkd = cpu_to_be32(V_FW_WR_LEN16(sizeof(*wr) / 16));
711 wr->tid_to_iq = cpu_to_be32(V_FW_FILTER_WR_TID(ftid) |
712 V_FW_FILTER_WR_NOREPLY(qid < 0));
713 wr->del_filter_to_l2tix = cpu_to_be32(F_FW_FILTER_WR_DEL_FILTER);
715 wr->rx_chan_rx_rpl_iq =
716 cpu_to_be16(V_FW_FILTER_WR_RX_RPL_IQ(qid));
720 * Create FW work request to delete the filter at a specified index
722 static int del_filter_wr(struct rte_eth_dev *dev, unsigned int fidx)
724 struct adapter *adapter = ethdev2adap(dev);
725 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
726 struct rte_mbuf *mbuf;
727 struct fw_filter2_wr *fwr;
728 struct sge_ctrl_txq *ctrlq;
729 unsigned int port_id = ethdev2pinfo(dev)->port_id;
731 ctrlq = &adapter->sge.ctrlq[port_id];
732 mbuf = rte_pktmbuf_alloc(ctrlq->mb_pool);
736 mbuf->data_len = sizeof(*fwr);
737 mbuf->pkt_len = mbuf->data_len;
739 fwr = rte_pktmbuf_mtod(mbuf, struct fw_filter2_wr *);
740 t4_mk_filtdelwr(adapter, f->tid, fwr, adapter->sge.fw_evtq.abs_id);
743 * Mark the filter as "pending" and ship off the Filter Work Request.
744 * When we get the Work Request Reply we'll clear the pending status.
747 t4_mgmt_tx(ctrlq, mbuf);
751 static int set_filter_wr(struct rte_eth_dev *dev, unsigned int fidx)
753 struct adapter *adapter = ethdev2adap(dev);
754 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
755 struct rte_mbuf *mbuf;
756 struct fw_filter2_wr *fwr;
757 struct sge_ctrl_txq *ctrlq;
758 unsigned int port_id = ethdev2pinfo(dev)->port_id;
761 /* If the new filter requires Source MAC rewriting then we need to
762 * allocate a SMT entry for the filter
765 f->smt = cxgbe_smt_alloc_switching(f->dev, f->fs.smac);
768 cxgbe_l2t_release(f->l2t);
775 ctrlq = &adapter->sge.ctrlq[port_id];
776 mbuf = rte_pktmbuf_alloc(ctrlq->mb_pool);
782 mbuf->data_len = sizeof(*fwr);
783 mbuf->pkt_len = mbuf->data_len;
785 fwr = rte_pktmbuf_mtod(mbuf, struct fw_filter2_wr *);
786 memset(fwr, 0, sizeof(*fwr));
789 * Construct the work request to set the filter.
791 if (adapter->params.filter2_wr_support)
792 fwr->op_pkd = cpu_to_be32(V_FW_WR_OP(FW_FILTER2_WR));
794 fwr->op_pkd = cpu_to_be32(V_FW_WR_OP(FW_FILTER_WR));
795 fwr->len16_pkd = cpu_to_be32(V_FW_WR_LEN16(sizeof(*fwr) / 16));
797 cpu_to_be32(V_FW_FILTER_WR_TID(f->tid) |
798 V_FW_FILTER_WR_RQTYPE(f->fs.type) |
799 V_FW_FILTER_WR_NOREPLY(0) |
800 V_FW_FILTER_WR_IQ(f->fs.iq));
801 fwr->del_filter_to_l2tix =
802 cpu_to_be32(V_FW_FILTER_WR_DROP(f->fs.action == FILTER_DROP) |
803 V_FW_FILTER_WR_DIRSTEER(f->fs.dirsteer) |
804 V_FW_FILTER_WR_LPBK(f->fs.action == FILTER_SWITCH) |
805 V_FW_FILTER_WR_SMAC(f->fs.newsmac) |
806 V_FW_FILTER_WR_DMAC(f->fs.newdmac) |
807 V_FW_FILTER_WR_INSVLAN
808 (f->fs.newvlan == VLAN_INSERT ||
809 f->fs.newvlan == VLAN_REWRITE) |
810 V_FW_FILTER_WR_RMVLAN
811 (f->fs.newvlan == VLAN_REMOVE ||
812 f->fs.newvlan == VLAN_REWRITE) |
813 V_FW_FILTER_WR_HITCNTS(f->fs.hitcnts) |
814 V_FW_FILTER_WR_TXCHAN(f->fs.eport) |
815 V_FW_FILTER_WR_PRIO(f->fs.prio) |
816 V_FW_FILTER_WR_L2TIX(f->l2t ? f->l2t->idx : 0));
817 fwr->ethtype = cpu_to_be16(f->fs.val.ethtype);
818 fwr->ethtypem = cpu_to_be16(f->fs.mask.ethtype);
819 fwr->frag_to_ovlan_vldm =
820 (V_FW_FILTER_WR_IVLAN_VLD(f->fs.val.ivlan_vld) |
821 V_FW_FILTER_WR_IVLAN_VLDM(f->fs.mask.ivlan_vld) |
822 V_FW_FILTER_WR_OVLAN_VLD(f->fs.val.ovlan_vld) |
823 V_FW_FILTER_WR_OVLAN_VLDM(f->fs.mask.ovlan_vld));
824 fwr->smac_sel = f->smt ? f->smt->hw_idx : 0;
825 fwr->rx_chan_rx_rpl_iq =
826 cpu_to_be16(V_FW_FILTER_WR_RX_CHAN(0) |
827 V_FW_FILTER_WR_RX_RPL_IQ(adapter->sge.fw_evtq.abs_id
829 fwr->maci_to_matchtypem =
830 cpu_to_be32(V_FW_FILTER_WR_MACI(f->fs.val.macidx) |
831 V_FW_FILTER_WR_MACIM(f->fs.mask.macidx) |
832 V_FW_FILTER_WR_PORT(f->fs.val.iport) |
833 V_FW_FILTER_WR_PORTM(f->fs.mask.iport));
834 fwr->ptcl = f->fs.val.proto;
835 fwr->ptclm = f->fs.mask.proto;
836 fwr->ttyp = f->fs.val.tos;
837 fwr->ttypm = f->fs.mask.tos;
838 fwr->ivlan = cpu_to_be16(f->fs.val.ivlan);
839 fwr->ivlanm = cpu_to_be16(f->fs.mask.ivlan);
840 fwr->ovlan = cpu_to_be16(f->fs.val.ovlan);
841 fwr->ovlanm = cpu_to_be16(f->fs.mask.ovlan);
842 rte_memcpy(fwr->lip, f->fs.val.lip, sizeof(fwr->lip));
843 rte_memcpy(fwr->lipm, f->fs.mask.lip, sizeof(fwr->lipm));
844 rte_memcpy(fwr->fip, f->fs.val.fip, sizeof(fwr->fip));
845 rte_memcpy(fwr->fipm, f->fs.mask.fip, sizeof(fwr->fipm));
846 fwr->lp = cpu_to_be16(f->fs.val.lport);
847 fwr->lpm = cpu_to_be16(f->fs.mask.lport);
848 fwr->fp = cpu_to_be16(f->fs.val.fport);
849 fwr->fpm = cpu_to_be16(f->fs.mask.fport);
851 if (adapter->params.filter2_wr_support) {
852 fwr->filter_type_swapmac =
853 V_FW_FILTER2_WR_SWAPMAC(f->fs.swapmac);
854 fwr->natmode_to_ulp_type =
855 V_FW_FILTER2_WR_ULP_TYPE(f->fs.nat_mode ?
858 V_FW_FILTER2_WR_NATMODE(f->fs.nat_mode);
859 memcpy(fwr->newlip, f->fs.nat_lip, sizeof(fwr->newlip));
860 memcpy(fwr->newfip, f->fs.nat_fip, sizeof(fwr->newfip));
861 fwr->newlport = cpu_to_be16(f->fs.nat_lport);
862 fwr->newfport = cpu_to_be16(f->fs.nat_fport);
866 * Mark the filter as "pending" and ship off the Filter Work Request.
867 * When we get the Work Request Reply we'll clear the pending status.
870 t4_mgmt_tx(ctrlq, mbuf);
878 * Set the corresponding entries in the bitmap.
880 static int cxgbe_set_ftid(struct tid_info *t, u32 fidx, u8 nentries)
884 t4_os_lock(&t->ftid_lock);
885 if (rte_bitmap_get(t->ftid_bmap, fidx)) {
886 t4_os_unlock(&t->ftid_lock);
890 for (i = fidx; i < fidx + nentries; i++)
891 rte_bitmap_set(t->ftid_bmap, i);
892 t4_os_unlock(&t->ftid_lock);
897 * Clear the corresponding entries in the bitmap.
899 static void cxgbe_clear_ftid(struct tid_info *t, u32 fidx, u8 nentries)
903 t4_os_lock(&t->ftid_lock);
904 for (i = fidx; i < fidx + nentries; i++)
905 rte_bitmap_clear(t->ftid_bmap, i);
906 t4_os_unlock(&t->ftid_lock);
910 * Check a delete filter request for validity and send it to the hardware.
911 * Return 0 on success, an error number otherwise. We attach any provided
912 * filter operation context to the internal filter specification in order to
913 * facilitate signaling completion of the operation.
915 int cxgbe_del_filter(struct rte_eth_dev *dev, unsigned int filter_id,
916 struct ch_filter_specification *fs,
917 struct filter_ctx *ctx)
919 struct port_info *pi = dev->data->dev_private;
920 struct adapter *adapter = pi->adapter;
921 struct filter_entry *f;
922 unsigned int chip_ver;
926 if (is_hashfilter(adapter) && fs->cap)
927 return cxgbe_del_hash_filter(dev, filter_id, ctx);
929 if (filter_id >= adapter->tids.nftids)
932 chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
935 * Ensure IPv6 filter id is aligned on the 2 slot boundary for T6,
936 * and 4 slot boundary for cards below T6.
938 if (fs->type == FILTER_TYPE_IPV6) {
939 if (chip_ver < CHELSIO_T6)
945 nentries = cxgbe_filter_slots(adapter, fs->type);
946 ret = cxgbe_is_filter_set(&adapter->tids, filter_id, nentries);
948 dev_warn(adap, "%s: could not find filter entry: %u\n",
949 __func__, filter_id);
953 f = &adapter->tids.ftid_tab[filter_id];
954 ret = writable_filter(f);
960 cxgbe_clear_ftid(&adapter->tids,
961 f->tid - adapter->tids.ftid_base,
963 return del_filter_wr(dev, filter_id);
967 * If the caller has passed in a Completion Context then we need to
968 * mark it as a successful completion so they don't stall waiting
973 t4_complete(&ctx->completion);
980 * Check a Chelsio Filter Request for validity, convert it into our internal
981 * format and send it to the hardware. Return 0 on success, an error number
982 * otherwise. We attach any provided filter operation context to the internal
983 * filter specification in order to facilitate signaling completion of the
986 int cxgbe_set_filter(struct rte_eth_dev *dev, unsigned int filter_id,
987 struct ch_filter_specification *fs,
988 struct filter_ctx *ctx)
990 struct port_info *pi = ethdev2pinfo(dev);
991 struct adapter *adapter = pi->adapter;
992 u8 nentries, bitoff[16] = {0};
993 struct filter_entry *f;
994 unsigned int chip_ver;
995 unsigned int fidx, iq;
999 if (is_hashfilter(adapter) && fs->cap)
1000 return cxgbe_set_hash_filter(dev, fs, ctx);
1002 if (filter_id >= adapter->tids.nftids)
1005 chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
1007 ret = cxgbe_validate_filter(adapter, fs);
1012 * IPv6 filters occupy four slots and must be aligned on four-slot
1013 * boundaries for T5. On T6, IPv6 filters occupy two-slots and
1014 * must be aligned on two-slot boundaries.
1016 * IPv4 filters only occupy a single slot and have no alignment
1020 if (fs->type == FILTER_TYPE_IPV6) {
1021 if (chip_ver < CHELSIO_T6)
1027 if (fidx != filter_id)
1030 nentries = cxgbe_filter_slots(adapter, fs->type);
1031 ret = cxgbe_is_filter_set(&adapter->tids, filter_id, nentries);
1035 iq = get_filter_steerq(dev, fs);
1038 * Check to make sure that provided filter index is not
1039 * already in use by someone else
1041 f = &adapter->tids.ftid_tab[filter_id];
1045 fidx = adapter->tids.ftid_base + filter_id;
1046 ret = cxgbe_set_ftid(&adapter->tids, filter_id, nentries);
1051 * Check to make sure the filter requested is writable ...
1053 ret = writable_filter(f);
1055 /* Clear the bits we have set above */
1056 cxgbe_clear_ftid(&adapter->tids, filter_id, nentries);
1061 * Convert the filter specification into our internal format.
1062 * We copy the PF/VF specification into the Outer VLAN field
1063 * here so the rest of the code -- including the interface to
1064 * the firmware -- doesn't have to constantly do these checks.
1070 /* Allocate a clip table entry only if we have non-zero IPv6 address. */
1071 if (chip_ver > CHELSIO_T5 && f->fs.type &&
1072 memcmp(f->fs.val.lip, bitoff, sizeof(bitoff))) {
1073 f->clipt = cxgbe_clip_alloc(dev, (u32 *)&f->fs.val.lip);
1080 /* If the new filter requires loopback Destination MAC and/or VLAN
1081 * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
1084 if (f->fs.newvlan || f->fs.newdmac) {
1085 f->l2t = cxgbe_l2t_alloc_switching(f->dev, f->fs.vlan,
1086 f->fs.eport, f->fs.dmac);
1093 iconf = adapter->params.tp.ingress_config;
1095 /* Either PFVF or OVLAN can be active, but not both
1096 * So, if PFVF is enabled, then overwrite the OVLAN
1097 * fields with PFVF fields before writing the spec
1100 if (iconf & F_VNIC) {
1101 f->fs.val.ovlan = fs->val.pf << 13 | fs->val.vf;
1102 f->fs.mask.ovlan = fs->mask.pf << 13 | fs->mask.vf;
1103 f->fs.val.ovlan_vld = fs->val.pfvf_vld;
1104 f->fs.mask.ovlan_vld = fs->mask.pfvf_vld;
1108 * Attempt to set the filter. If we don't succeed, we clear
1109 * it and return the failure.
1112 f->tid = fidx; /* Save the actual tid */
1113 ret = set_filter_wr(dev, filter_id);
1120 cxgbe_clear_ftid(&adapter->tids, filter_id, nentries);
1126 * Handle a Hash filter write reply.
1128 void cxgbe_hash_filter_rpl(struct adapter *adap,
1129 const struct cpl_act_open_rpl *rpl)
1131 struct tid_info *t = &adap->tids;
1132 struct filter_entry *f;
1133 struct filter_ctx *ctx = NULL;
1134 unsigned int tid = GET_TID(rpl);
1135 unsigned int ftid = G_TID_TID(G_AOPEN_ATID
1136 (be32_to_cpu(rpl->atid_status)));
1137 unsigned int status = G_AOPEN_STATUS(be32_to_cpu(rpl->atid_status));
1139 f = lookup_atid(t, ftid);
1141 dev_warn(adap, "%s: could not find filter entry: %d\n",
1150 case CPL_ERR_NONE: {
1152 f->pending = 0; /* asynchronous setup completed */
1155 cxgbe_insert_tid(t, f, f->tid, 0);
1156 cxgbe_free_atid(t, ftid);
1162 set_tcb_field(adap, tid,
1164 V_TCB_TIMESTAMP(M_TCB_TIMESTAMP) |
1165 V_TCB_T_RTT_TS_RECENT_AGE
1166 (M_TCB_T_RTT_TS_RECENT_AGE),
1167 V_TCB_TIMESTAMP(0ULL) |
1168 V_TCB_T_RTT_TS_RECENT_AGE(0ULL),
1171 set_tcb_tflag(adap, tid, S_TF_CCTRL_ECE, 1, 1);
1172 if (f->fs.newvlan == VLAN_INSERT ||
1173 f->fs.newvlan == VLAN_REWRITE)
1174 set_tcb_tflag(adap, tid, S_TF_CCTRL_RFR, 1, 1);
1175 if (f->fs.newsmac) {
1176 set_tcb_tflag(adap, tid, S_TF_CCTRL_CWR, 1, 1);
1177 set_tcb_field(adap, tid, W_TCB_SMAC_SEL,
1178 V_TCB_SMAC_SEL(M_TCB_SMAC_SEL),
1179 V_TCB_SMAC_SEL(f->smt->hw_idx), 1);
1184 dev_warn(adap, "%s: filter creation failed with status = %u\n",
1188 if (status == CPL_ERR_TCAM_FULL)
1189 ctx->result = -EAGAIN;
1191 ctx->result = -EINVAL;
1194 cxgbe_free_atid(t, ftid);
1200 t4_complete(&ctx->completion);
1204 * Handle a LE-TCAM filter write/deletion reply.
1206 void cxgbe_filter_rpl(struct adapter *adap, const struct cpl_set_tcb_rpl *rpl)
1208 struct filter_entry *f = NULL;
1209 unsigned int tid = GET_TID(rpl);
1210 int idx, max_fidx = adap->tids.nftids;
1212 /* Get the corresponding filter entry for this tid */
1213 if (adap->tids.ftid_tab) {
1214 /* Check this in normal filter region */
1215 idx = tid - adap->tids.ftid_base;
1216 if (idx >= max_fidx)
1219 f = &adap->tids.ftid_tab[idx];
1224 /* We found the filter entry for this tid */
1226 unsigned int ret = G_COOKIE(rpl->cookie);
1227 struct filter_ctx *ctx;
1230 * Pull off any filter operation context attached to the
1236 if (ret == FW_FILTER_WR_FLT_ADDED) {
1237 f->pending = 0; /* asynchronous setup completed */
1243 } else if (ret == FW_FILTER_WR_FLT_DELETED) {
1245 * Clear the filter when we get confirmation from the
1246 * hardware that the filter has been deleted.
1253 * Something went wrong. Issue a warning about the
1254 * problem and clear everything out.
1256 dev_warn(adap, "filter %u setup failed with error %u\n",
1260 ctx->result = -EINVAL;
1264 t4_complete(&ctx->completion);
1269 * Retrieve the packet count for the specified filter.
1271 int cxgbe_get_filter_count(struct adapter *adapter, unsigned int fidx,
1272 u64 *c, int hash, bool get_byte)
1274 struct filter_entry *f;
1275 unsigned int tcb_base, tcbaddr;
1278 tcb_base = t4_read_reg(adapter, A_TP_CMM_TCB_BASE);
1279 if (is_hashfilter(adapter) && hash) {
1280 if (fidx < adapter->tids.ntids) {
1281 f = adapter->tids.tid_tab[fidx];
1285 if (is_t5(adapter->params.chip)) {
1289 tcbaddr = tcb_base + (fidx * TCB_SIZE);
1295 if (fidx >= adapter->tids.nftids)
1298 f = &adapter->tids.ftid_tab[fidx];
1302 tcbaddr = tcb_base + f->tid * TCB_SIZE;
1305 f = &adapter->tids.ftid_tab[fidx];
1310 if (is_t5(adapter->params.chip) || is_t6(adapter->params.chip)) {
1312 * For T5, the Filter Packet Hit Count is maintained as a
1313 * 32-bit Big Endian value in the TCB field {timestamp}.
1314 * Similar to the craziness above, instead of the filter hit
1315 * count showing up at offset 20 ((W_TCB_TIMESTAMP == 5) *
1316 * sizeof(u32)), it actually shows up at offset 24. Whacky.
1319 unsigned int word_offset = 4;
1320 __be64 be64_byte_count;
1322 t4_os_lock(&adapter->win0_lock);
1323 ret = t4_memory_rw(adapter, MEMWIN_NIC, MEM_EDC0,
1325 (word_offset * sizeof(__be32)),
1326 sizeof(be64_byte_count),
1329 t4_os_unlock(&adapter->win0_lock);
1332 *c = be64_to_cpu(be64_byte_count);
1334 unsigned int word_offset = 6;
1337 t4_os_lock(&adapter->win0_lock);
1338 ret = t4_memory_rw(adapter, MEMWIN_NIC, MEM_EDC0,
1340 (word_offset * sizeof(__be32)),
1341 sizeof(be32_count), &be32_count,
1343 t4_os_unlock(&adapter->win0_lock);
1346 *c = (u64)be32_to_cpu(be32_count);
1353 * Clear the packet count for the specified filter.
1355 int cxgbe_clear_filter_count(struct adapter *adapter, unsigned int fidx,
1356 int hash, bool clear_byte)
1358 u64 tcb_mask = 0, tcb_val = 0;
1359 struct filter_entry *f = NULL;
1362 if (is_hashfilter(adapter) && hash) {
1363 if (fidx >= adapter->tids.ntids)
1366 /* No hitcounts supported for T5 hashfilters */
1367 if (is_t5(adapter->params.chip))
1370 f = adapter->tids.tid_tab[fidx];
1372 if (fidx >= adapter->tids.nftids)
1375 f = &adapter->tids.ftid_tab[fidx];
1378 if (!f || !f->valid)
1381 tcb_word = W_TCB_TIMESTAMP;
1382 tcb_mask = V_TCB_TIMESTAMP(M_TCB_TIMESTAMP);
1383 tcb_val = V_TCB_TIMESTAMP(0ULL);
1385 set_tcb_field(adapter, f->tid, tcb_word, tcb_mask, tcb_val, 1);
1388 tcb_word = W_TCB_T_RTT_TS_RECENT_AGE;
1390 V_TCB_T_RTT_TS_RECENT_AGE(M_TCB_T_RTT_TS_RECENT_AGE) |
1391 V_TCB_T_RTSEQ_RECENT(M_TCB_T_RTSEQ_RECENT);
1392 tcb_val = V_TCB_T_RTT_TS_RECENT_AGE(0ULL) |
1393 V_TCB_T_RTSEQ_RECENT(0ULL);
1395 set_tcb_field(adapter, f->tid, tcb_word, tcb_mask, tcb_val, 1);
1402 * Handle a Hash filter delete reply.
1404 void cxgbe_hash_del_filter_rpl(struct adapter *adap,
1405 const struct cpl_abort_rpl_rss *rpl)
1407 struct tid_info *t = &adap->tids;
1408 struct filter_entry *f;
1409 struct filter_ctx *ctx = NULL;
1410 unsigned int tid = GET_TID(rpl);
1412 f = lookup_tid(t, tid);
1414 dev_warn(adap, "%s: could not find filter entry: %u\n",
1422 cxgbe_remove_tid(t, 0, tid, 0);
1427 t4_complete(&ctx->completion);