2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
4 * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 #include "t4_values.h"
44 #include "cxgb4_filter.h"
46 static inline bool is_field_set(u32 val, u32 mask)
51 static inline bool unsupported(u32 conf, u32 conf_mask, u32 val, u32 mask)
53 return !(conf & conf_mask) && is_field_set(val, mask);
56 static int set_tcb_field(struct adapter *adap, struct filter_entry *f,
57 unsigned int ftid, u16 word, u64 mask, u64 val,
60 struct cpl_set_tcb_field *req;
63 skb = alloc_skb(sizeof(struct cpl_set_tcb_field), GFP_ATOMIC);
67 req = (struct cpl_set_tcb_field *)__skb_put_zero(skb, sizeof(*req));
68 INIT_TP_WR_CPL(req, CPL_SET_TCB_FIELD, ftid);
69 req->reply_ctrl = htons(REPLY_CHAN_V(0) |
70 QUEUENO_V(adap->sge.fw_evtq.abs_id) |
71 NO_REPLY_V(no_reply));
72 req->word_cookie = htons(TCB_WORD_V(word) | TCB_COOKIE_V(ftid));
73 req->mask = cpu_to_be64(mask);
74 req->val = cpu_to_be64(val);
75 set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
76 t4_ofld_send(adap, skb);
80 /* Set one of the t_flags bits in the TCB.
82 static int set_tcb_tflag(struct adapter *adap, struct filter_entry *f,
83 unsigned int ftid, unsigned int bit_pos,
84 unsigned int val, int no_reply)
86 return set_tcb_field(adap, f, ftid, TCB_T_FLAGS_W, 1ULL << bit_pos,
87 (unsigned long long)val << bit_pos, no_reply);
90 static void mk_abort_req_ulp(struct cpl_abort_req *abort_req, unsigned int tid)
92 struct ulp_txpkt *txpkt = (struct ulp_txpkt *)abort_req;
93 struct ulptx_idata *sc = (struct ulptx_idata *)(txpkt + 1);
95 txpkt->cmd_dest = htonl(ULPTX_CMD_V(ULP_TX_PKT) | ULP_TXPKT_DEST_V(0));
96 txpkt->len = htonl(DIV_ROUND_UP(sizeof(*abort_req), 16));
97 sc->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM));
98 sc->len = htonl(sizeof(*abort_req) - sizeof(struct work_request_hdr));
99 OPCODE_TID(abort_req) = htonl(MK_OPCODE_TID(CPL_ABORT_REQ, tid));
100 abort_req->rsvd0 = htonl(0);
101 abort_req->rsvd1 = 0;
102 abort_req->cmd = CPL_ABORT_NO_RST;
105 static void mk_abort_rpl_ulp(struct cpl_abort_rpl *abort_rpl, unsigned int tid)
107 struct ulp_txpkt *txpkt = (struct ulp_txpkt *)abort_rpl;
108 struct ulptx_idata *sc = (struct ulptx_idata *)(txpkt + 1);
110 txpkt->cmd_dest = htonl(ULPTX_CMD_V(ULP_TX_PKT) | ULP_TXPKT_DEST_V(0));
111 txpkt->len = htonl(DIV_ROUND_UP(sizeof(*abort_rpl), 16));
112 sc->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM));
113 sc->len = htonl(sizeof(*abort_rpl) - sizeof(struct work_request_hdr));
114 OPCODE_TID(abort_rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, tid));
115 abort_rpl->rsvd0 = htonl(0);
116 abort_rpl->rsvd1 = 0;
117 abort_rpl->cmd = CPL_ABORT_NO_RST;
120 static void mk_set_tcb_ulp(struct filter_entry *f,
121 struct cpl_set_tcb_field *req,
122 unsigned int word, u64 mask, u64 val,
123 u8 cookie, int no_reply)
125 struct ulp_txpkt *txpkt = (struct ulp_txpkt *)req;
126 struct ulptx_idata *sc = (struct ulptx_idata *)(txpkt + 1);
128 txpkt->cmd_dest = htonl(ULPTX_CMD_V(ULP_TX_PKT) | ULP_TXPKT_DEST_V(0));
129 txpkt->len = htonl(DIV_ROUND_UP(sizeof(*req), 16));
130 sc->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_IMM));
131 sc->len = htonl(sizeof(*req) - sizeof(struct work_request_hdr));
132 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, f->tid));
133 req->reply_ctrl = htons(NO_REPLY_V(no_reply) | REPLY_CHAN_V(0) |
135 req->word_cookie = htons(TCB_WORD_V(word) | TCB_COOKIE_V(cookie));
136 req->mask = cpu_to_be64(mask);
137 req->val = cpu_to_be64(val);
138 sc = (struct ulptx_idata *)(req + 1);
139 sc->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_NOOP));
143 static int configure_filter_smac(struct adapter *adap, struct filter_entry *f)
147 /* do a set-tcb for smac-sel and CWR bit.. */
148 err = set_tcb_tflag(adap, f, f->tid, TF_CCTRL_CWR_S, 1, 1);
152 err = set_tcb_field(adap, f, f->tid, TCB_SMAC_SEL_W,
153 TCB_SMAC_SEL_V(TCB_SMAC_SEL_M),
154 TCB_SMAC_SEL_V(f->smt->idx), 1);
159 dev_err(adap->pdev_dev, "filter %u smac config failed with error %u\n",
164 static void set_nat_params(struct adapter *adap, struct filter_entry *f,
165 unsigned int tid, bool dip, bool sip, bool dp,
170 set_tcb_field(adap, f, tid, TCB_SND_UNA_RAW_W,
171 WORD_MASK, f->fs.nat_lip[15] |
172 f->fs.nat_lip[14] << 8 |
173 f->fs.nat_lip[13] << 16 |
174 f->fs.nat_lip[12] << 24, 1);
176 set_tcb_field(adap, f, tid, TCB_SND_UNA_RAW_W + 1,
177 WORD_MASK, f->fs.nat_lip[11] |
178 f->fs.nat_lip[10] << 8 |
179 f->fs.nat_lip[9] << 16 |
180 f->fs.nat_lip[8] << 24, 1);
182 set_tcb_field(adap, f, tid, TCB_SND_UNA_RAW_W + 2,
183 WORD_MASK, f->fs.nat_lip[7] |
184 f->fs.nat_lip[6] << 8 |
185 f->fs.nat_lip[5] << 16 |
186 f->fs.nat_lip[4] << 24, 1);
188 set_tcb_field(adap, f, tid, TCB_SND_UNA_RAW_W + 3,
189 WORD_MASK, f->fs.nat_lip[3] |
190 f->fs.nat_lip[2] << 8 |
191 f->fs.nat_lip[1] << 16 |
192 f->fs.nat_lip[0] << 24, 1);
194 set_tcb_field(adap, f, tid, TCB_RX_FRAG3_LEN_RAW_W,
195 WORD_MASK, f->fs.nat_lip[3] |
196 f->fs.nat_lip[2] << 8 |
197 f->fs.nat_lip[1] << 16 |
198 f->fs.nat_lip[0] << 24, 1);
204 set_tcb_field(adap, f, tid, TCB_RX_FRAG2_PTR_RAW_W,
205 WORD_MASK, f->fs.nat_fip[15] |
206 f->fs.nat_fip[14] << 8 |
207 f->fs.nat_fip[13] << 16 |
208 f->fs.nat_fip[12] << 24, 1);
210 set_tcb_field(adap, f, tid, TCB_RX_FRAG2_PTR_RAW_W + 1,
211 WORD_MASK, f->fs.nat_fip[11] |
212 f->fs.nat_fip[10] << 8 |
213 f->fs.nat_fip[9] << 16 |
214 f->fs.nat_fip[8] << 24, 1);
216 set_tcb_field(adap, f, tid, TCB_RX_FRAG2_PTR_RAW_W + 2,
217 WORD_MASK, f->fs.nat_fip[7] |
218 f->fs.nat_fip[6] << 8 |
219 f->fs.nat_fip[5] << 16 |
220 f->fs.nat_fip[4] << 24, 1);
222 set_tcb_field(adap, f, tid, TCB_RX_FRAG2_PTR_RAW_W + 3,
223 WORD_MASK, f->fs.nat_fip[3] |
224 f->fs.nat_fip[2] << 8 |
225 f->fs.nat_fip[1] << 16 |
226 f->fs.nat_fip[0] << 24, 1);
229 set_tcb_field(adap, f, tid,
230 TCB_RX_FRAG3_START_IDX_OFFSET_RAW_W,
231 WORD_MASK, f->fs.nat_fip[3] |
232 f->fs.nat_fip[2] << 8 |
233 f->fs.nat_fip[1] << 16 |
234 f->fs.nat_fip[0] << 24, 1);
238 set_tcb_field(adap, f, tid, TCB_PDU_HDR_LEN_W, WORD_MASK,
239 (dp ? f->fs.nat_lport : 0) |
240 (sp ? f->fs.nat_fport << 16 : 0), 1);
243 /* Validate filter spec against configuration done on the card. */
244 static int validate_filter(struct net_device *dev,
245 struct ch_filter_specification *fs)
247 struct adapter *adapter = netdev2adap(dev);
250 /* Check for unconfigured fields being used. */
251 iconf = adapter->params.tp.ingress_config;
252 fconf = fs->hash ? adapter->params.tp.filter_mask :
253 adapter->params.tp.vlan_pri_map;
255 if (unsupported(fconf, FCOE_F, fs->val.fcoe, fs->mask.fcoe) ||
256 unsupported(fconf, PORT_F, fs->val.iport, fs->mask.iport) ||
257 unsupported(fconf, TOS_F, fs->val.tos, fs->mask.tos) ||
258 unsupported(fconf, ETHERTYPE_F, fs->val.ethtype,
260 unsupported(fconf, MACMATCH_F, fs->val.macidx, fs->mask.macidx) ||
261 unsupported(fconf, MPSHITTYPE_F, fs->val.matchtype,
262 fs->mask.matchtype) ||
263 unsupported(fconf, FRAGMENTATION_F, fs->val.frag, fs->mask.frag) ||
264 unsupported(fconf, PROTOCOL_F, fs->val.proto, fs->mask.proto) ||
265 unsupported(fconf, VNIC_ID_F, fs->val.pfvf_vld,
266 fs->mask.pfvf_vld) ||
267 unsupported(fconf, VNIC_ID_F, fs->val.ovlan_vld,
268 fs->mask.ovlan_vld) ||
269 unsupported(fconf, VNIC_ID_F, fs->val.encap_vld,
270 fs->mask.encap_vld) ||
271 unsupported(fconf, VLAN_F, fs->val.ivlan_vld, fs->mask.ivlan_vld))
274 /* T4 inconveniently uses the same FT_VNIC_ID_W bits for both the Outer
275 * VLAN Tag and PF/VF/VFvld fields based on VNIC_F being set
276 * in TP_INGRESS_CONFIG. Hense the somewhat crazy checks
277 * below. Additionally, since the T4 firmware interface also
278 * carries that overlap, we need to translate any PF/VF
279 * specification into that internal format below.
281 if ((is_field_set(fs->val.pfvf_vld, fs->mask.pfvf_vld) &&
282 is_field_set(fs->val.ovlan_vld, fs->mask.ovlan_vld)) ||
283 (is_field_set(fs->val.pfvf_vld, fs->mask.pfvf_vld) &&
284 is_field_set(fs->val.encap_vld, fs->mask.encap_vld)) ||
285 (is_field_set(fs->val.ovlan_vld, fs->mask.ovlan_vld) &&
286 is_field_set(fs->val.encap_vld, fs->mask.encap_vld)))
288 if (unsupported(iconf, VNIC_F, fs->val.pfvf_vld, fs->mask.pfvf_vld) ||
289 (is_field_set(fs->val.ovlan_vld, fs->mask.ovlan_vld) &&
292 if (fs->val.pf > 0x7 || fs->val.vf > 0x7f)
297 /* If the user is requesting that the filter action loop
298 * matching packets back out one of our ports, make sure that
299 * the egress port is in range.
301 if (fs->action == FILTER_SWITCH &&
302 fs->eport >= adapter->params.nports)
305 /* Don't allow various trivially obvious bogus out-of-range values... */
306 if (fs->val.iport >= adapter->params.nports)
309 /* T4 doesn't support removing VLAN Tags for loop back filters. */
310 if (is_t4(adapter->params.chip) &&
311 fs->action == FILTER_SWITCH &&
312 (fs->newvlan == VLAN_REMOVE ||
313 fs->newvlan == VLAN_REWRITE))
316 if (fs->val.encap_vld &&
317 CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
322 static int get_filter_steerq(struct net_device *dev,
323 struct ch_filter_specification *fs)
325 struct adapter *adapter = netdev2adap(dev);
328 /* If the user has requested steering matching Ingress Packets
329 * to a specific Queue Set, we need to make sure it's in range
330 * for the port and map that into the Absolute Queue ID of the
331 * Queue Set's Response Queue.
338 struct port_info *pi = netdev_priv(dev);
340 /* If the iq id is greater than the number of qsets,
341 * then assume it is an absolute qid.
343 if (fs->iq < pi->nqsets)
344 iq = adapter->sge.ethrxq[pi->first_qset +
353 static int get_filter_count(struct adapter *adapter, unsigned int fidx,
354 u64 *pkts, u64 *bytes, bool hash)
356 unsigned int tcb_base, tcbaddr;
357 unsigned int word_offset;
358 struct filter_entry *f;
359 __be64 be64_byte_count;
362 tcb_base = t4_read_reg(adapter, TP_CMM_TCB_BASE_A);
363 if (is_hashfilter(adapter) && hash) {
364 if (fidx < adapter->tids.ntids) {
365 f = adapter->tids.tid_tab[fidx];
372 if ((fidx != (adapter->tids.nftids +
373 adapter->tids.nsftids - 1)) &&
374 fidx >= adapter->tids.nftids)
377 f = &adapter->tids.ftid_tab[fidx];
381 tcbaddr = tcb_base + f->tid * TCB_SIZE;
383 spin_lock(&adapter->win0_lock);
384 if (is_t4(adapter->params.chip)) {
387 /* T4 doesn't maintain byte counts in hw */
392 ret = t4_memory_rw(adapter, MEMWIN_NIC, MEM_EDC0,
393 tcbaddr + (word_offset * sizeof(__be32)),
395 (__be32 *)&be64_count,
399 *pkts = be64_to_cpu(be64_count);
405 ret = t4_memory_rw(adapter, MEMWIN_NIC, MEM_EDC0,
406 tcbaddr + (word_offset * sizeof(__be32)),
407 sizeof(be64_byte_count),
412 *bytes = be64_to_cpu(be64_byte_count);
416 ret = t4_memory_rw(adapter, MEMWIN_NIC, MEM_EDC0,
417 tcbaddr + (word_offset * sizeof(__be32)),
423 *pkts = (u64)be32_to_cpu(be32_count);
427 spin_unlock(&adapter->win0_lock);
431 int cxgb4_get_filter_counters(struct net_device *dev, unsigned int fidx,
432 u64 *hitcnt, u64 *bytecnt, bool hash)
434 struct adapter *adapter = netdev2adap(dev);
436 return get_filter_count(adapter, fidx, hitcnt, bytecnt, hash);
439 int cxgb4_get_free_ftid(struct net_device *dev, int family)
441 struct adapter *adap = netdev2adap(dev);
442 struct tid_info *t = &adap->tids;
445 spin_lock_bh(&t->ftid_lock);
446 if (family == PF_INET) {
447 ftid = find_first_zero_bit(t->ftid_bmap, t->nftids);
448 if (ftid >= t->nftids)
451 if (is_t6(adap->params.chip)) {
452 ftid = bitmap_find_free_region(t->ftid_bmap,
457 /* this is only a lookup, keep the found region
460 bitmap_release_region(t->ftid_bmap, ftid, 1);
462 ftid = bitmap_find_free_region(t->ftid_bmap,
467 bitmap_release_region(t->ftid_bmap, ftid, 2);
471 spin_unlock_bh(&t->ftid_lock);
475 static int cxgb4_set_ftid(struct tid_info *t, int fidx, int family,
476 unsigned int chip_ver)
478 spin_lock_bh(&t->ftid_lock);
480 if (test_bit(fidx, t->ftid_bmap)) {
481 spin_unlock_bh(&t->ftid_lock);
485 if (family == PF_INET) {
486 __set_bit(fidx, t->ftid_bmap);
488 if (chip_ver < CHELSIO_T6)
489 bitmap_allocate_region(t->ftid_bmap, fidx, 2);
491 bitmap_allocate_region(t->ftid_bmap, fidx, 1);
494 spin_unlock_bh(&t->ftid_lock);
498 static void cxgb4_clear_ftid(struct tid_info *t, int fidx, int family,
499 unsigned int chip_ver)
501 spin_lock_bh(&t->ftid_lock);
502 if (family == PF_INET) {
503 __clear_bit(fidx, t->ftid_bmap);
505 if (chip_ver < CHELSIO_T6)
506 bitmap_release_region(t->ftid_bmap, fidx, 2);
508 bitmap_release_region(t->ftid_bmap, fidx, 1);
510 spin_unlock_bh(&t->ftid_lock);
513 /* Delete the filter at a specified index. */
514 static int del_filter_wr(struct adapter *adapter, int fidx)
516 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
517 struct fw_filter_wr *fwr;
523 skb = alloc_skb(len, GFP_KERNEL);
527 fwr = __skb_put(skb, len);
528 t4_mk_filtdelwr(f->tid, fwr, adapter->sge.fw_evtq.abs_id);
530 /* Mark the filter as "pending" and ship off the Filter Work Request.
531 * When we get the Work Request Reply we'll clear the pending status.
534 t4_mgmt_tx(adapter, skb);
538 /* Send a Work Request to write the filter at a specified index. We construct
539 * a Firmware Filter Work Request to have the work done and put the indicated
540 * filter into "pending" mode which will prevent any further actions against
541 * it till we get a reply from the firmware on the completion status of the
544 int set_filter_wr(struct adapter *adapter, int fidx)
546 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
547 struct fw_filter2_wr *fwr;
550 skb = alloc_skb(sizeof(*fwr), GFP_KERNEL);
554 /* If the new filter requires loopback Destination MAC and/or VLAN
555 * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
558 if (f->fs.newdmac || f->fs.newvlan) {
559 /* allocate L2T entry for new filter */
560 f->l2t = t4_l2t_alloc_switching(adapter, f->fs.vlan,
561 f->fs.eport, f->fs.dmac);
568 /* If the new filter requires loopback Source MAC rewriting then
569 * we need to allocate a SMT entry for the filter.
572 f->smt = cxgb4_smt_alloc_switching(f->dev, f->fs.smac);
575 cxgb4_l2t_release(f->l2t);
583 fwr = __skb_put_zero(skb, sizeof(*fwr));
585 /* It would be nice to put most of the following in t4_hw.c but most
586 * of the work is translating the cxgbtool ch_filter_specification
587 * into the Work Request and the definition of that structure is
588 * currently in cxgbtool.h which isn't appropriate to pull into the
589 * common code. We may eventually try to come up with a more neutral
590 * filter specification structure but for now it's easiest to simply
591 * put this fairly direct code in line ...
593 if (adapter->params.filter2_wr_support)
594 fwr->op_pkd = htonl(FW_WR_OP_V(FW_FILTER2_WR));
596 fwr->op_pkd = htonl(FW_WR_OP_V(FW_FILTER_WR));
597 fwr->len16_pkd = htonl(FW_WR_LEN16_V(sizeof(*fwr) / 16));
599 htonl(FW_FILTER_WR_TID_V(f->tid) |
600 FW_FILTER_WR_RQTYPE_V(f->fs.type) |
601 FW_FILTER_WR_NOREPLY_V(0) |
602 FW_FILTER_WR_IQ_V(f->fs.iq));
603 fwr->del_filter_to_l2tix =
604 htonl(FW_FILTER_WR_RPTTID_V(f->fs.rpttid) |
605 FW_FILTER_WR_DROP_V(f->fs.action == FILTER_DROP) |
606 FW_FILTER_WR_DIRSTEER_V(f->fs.dirsteer) |
607 FW_FILTER_WR_MASKHASH_V(f->fs.maskhash) |
608 FW_FILTER_WR_DIRSTEERHASH_V(f->fs.dirsteerhash) |
609 FW_FILTER_WR_LPBK_V(f->fs.action == FILTER_SWITCH) |
610 FW_FILTER_WR_DMAC_V(f->fs.newdmac) |
611 FW_FILTER_WR_INSVLAN_V(f->fs.newvlan == VLAN_INSERT ||
612 f->fs.newvlan == VLAN_REWRITE) |
613 FW_FILTER_WR_RMVLAN_V(f->fs.newvlan == VLAN_REMOVE ||
614 f->fs.newvlan == VLAN_REWRITE) |
615 FW_FILTER_WR_HITCNTS_V(f->fs.hitcnts) |
616 FW_FILTER_WR_TXCHAN_V(f->fs.eport) |
617 FW_FILTER_WR_PRIO_V(f->fs.prio) |
618 FW_FILTER_WR_L2TIX_V(f->l2t ? f->l2t->idx : 0));
619 fwr->ethtype = htons(f->fs.val.ethtype);
620 fwr->ethtypem = htons(f->fs.mask.ethtype);
621 fwr->frag_to_ovlan_vldm =
622 (FW_FILTER_WR_FRAG_V(f->fs.val.frag) |
623 FW_FILTER_WR_FRAGM_V(f->fs.mask.frag) |
624 FW_FILTER_WR_IVLAN_VLD_V(f->fs.val.ivlan_vld) |
625 FW_FILTER_WR_OVLAN_VLD_V(f->fs.val.ovlan_vld) |
626 FW_FILTER_WR_IVLAN_VLDM_V(f->fs.mask.ivlan_vld) |
627 FW_FILTER_WR_OVLAN_VLDM_V(f->fs.mask.ovlan_vld));
629 fwr->rx_chan_rx_rpl_iq =
630 htons(FW_FILTER_WR_RX_CHAN_V(0) |
631 FW_FILTER_WR_RX_RPL_IQ_V(adapter->sge.fw_evtq.abs_id));
632 fwr->maci_to_matchtypem =
633 htonl(FW_FILTER_WR_MACI_V(f->fs.val.macidx) |
634 FW_FILTER_WR_MACIM_V(f->fs.mask.macidx) |
635 FW_FILTER_WR_FCOE_V(f->fs.val.fcoe) |
636 FW_FILTER_WR_FCOEM_V(f->fs.mask.fcoe) |
637 FW_FILTER_WR_PORT_V(f->fs.val.iport) |
638 FW_FILTER_WR_PORTM_V(f->fs.mask.iport) |
639 FW_FILTER_WR_MATCHTYPE_V(f->fs.val.matchtype) |
640 FW_FILTER_WR_MATCHTYPEM_V(f->fs.mask.matchtype));
641 fwr->ptcl = f->fs.val.proto;
642 fwr->ptclm = f->fs.mask.proto;
643 fwr->ttyp = f->fs.val.tos;
644 fwr->ttypm = f->fs.mask.tos;
645 fwr->ivlan = htons(f->fs.val.ivlan);
646 fwr->ivlanm = htons(f->fs.mask.ivlan);
647 fwr->ovlan = htons(f->fs.val.ovlan);
648 fwr->ovlanm = htons(f->fs.mask.ovlan);
649 memcpy(fwr->lip, f->fs.val.lip, sizeof(fwr->lip));
650 memcpy(fwr->lipm, f->fs.mask.lip, sizeof(fwr->lipm));
651 memcpy(fwr->fip, f->fs.val.fip, sizeof(fwr->fip));
652 memcpy(fwr->fipm, f->fs.mask.fip, sizeof(fwr->fipm));
653 fwr->lp = htons(f->fs.val.lport);
654 fwr->lpm = htons(f->fs.mask.lport);
655 fwr->fp = htons(f->fs.val.fport);
656 fwr->fpm = htons(f->fs.mask.fport);
658 if (adapter->params.filter2_wr_support) {
659 fwr->natmode_to_ulp_type =
660 FW_FILTER2_WR_ULP_TYPE_V(f->fs.nat_mode ?
663 FW_FILTER2_WR_NATMODE_V(f->fs.nat_mode);
664 memcpy(fwr->newlip, f->fs.nat_lip, sizeof(fwr->newlip));
665 memcpy(fwr->newfip, f->fs.nat_fip, sizeof(fwr->newfip));
666 fwr->newlport = htons(f->fs.nat_lport);
667 fwr->newfport = htons(f->fs.nat_fport);
670 /* Mark the filter as "pending" and ship off the Filter Work Request.
671 * When we get the Work Request Reply we'll clear the pending status.
674 set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
675 t4_ofld_send(adapter, skb);
679 /* Return an error number if the indicated filter isn't writable ... */
680 int writable_filter(struct filter_entry *f)
690 /* Delete the filter at the specified index (if valid). The checks for all
691 * the common problems with doing this like the filter being locked, currently
692 * pending in another operation, etc.
694 int delete_filter(struct adapter *adapter, unsigned int fidx)
696 struct filter_entry *f;
699 if (fidx >= adapter->tids.nftids + adapter->tids.nsftids)
702 f = &adapter->tids.ftid_tab[fidx];
703 ret = writable_filter(f);
707 return del_filter_wr(adapter, fidx);
712 /* Clear a filter and release any of its resources that we own. This also
713 * clears the filter's "pending" status.
715 void clear_filter(struct adapter *adap, struct filter_entry *f)
717 struct port_info *pi = netdev_priv(f->dev);
719 /* If the new or old filter have loopback rewriteing rules then we'll
720 * need to free any existing L2T, SMT, CLIP entries of filter
724 cxgb4_l2t_release(f->l2t);
727 cxgb4_smt_release(f->smt);
729 if (f->fs.val.encap_vld && f->fs.val.ovlan_vld)
730 t4_free_encap_mac_filt(adap, pi->viid,
731 f->fs.val.ovlan & 0x1ff, 0);
733 if ((f->fs.hash || is_t6(adap->params.chip)) && f->fs.type)
734 cxgb4_clip_release(f->dev, (const u32 *)&f->fs.val.lip, 1);
736 /* The zeroing of the filter rule below clears the filter valid,
737 * pending, locked flags, l2t pointer, etc. so it's all we need for
740 memset(f, 0, sizeof(*f));
743 void clear_all_filters(struct adapter *adapter)
745 struct net_device *dev = adapter->port[0];
748 if (adapter->tids.ftid_tab) {
749 struct filter_entry *f = &adapter->tids.ftid_tab[0];
750 unsigned int max_ftid = adapter->tids.nftids +
751 adapter->tids.nsftids;
752 /* Clear all TCAM filters */
753 for (i = 0; i < max_ftid; i++, f++)
754 if (f->valid || f->pending)
755 cxgb4_del_filter(dev, i, &f->fs);
758 /* Clear all hash filters */
759 if (is_hashfilter(adapter) && adapter->tids.tid_tab) {
760 struct filter_entry *f;
763 for (i = adapter->tids.hash_base;
764 i <= adapter->tids.ntids; i++) {
765 f = (struct filter_entry *)
766 adapter->tids.tid_tab[i];
768 if (f && (f->valid || f->pending))
769 cxgb4_del_filter(dev, i, &f->fs);
772 sb = t4_read_reg(adapter, LE_DB_SRVR_START_INDEX_A);
773 for (i = 0; i < sb; i++) {
774 f = (struct filter_entry *)adapter->tids.tid_tab[i];
776 if (f && (f->valid || f->pending))
777 cxgb4_del_filter(dev, i, &f->fs);
782 /* Fill up default masks for set match fields. */
783 static void fill_default_mask(struct ch_filter_specification *fs)
785 unsigned int lip = 0, lip_mask = 0;
786 unsigned int fip = 0, fip_mask = 0;
789 if (fs->val.iport && !fs->mask.iport)
790 fs->mask.iport |= ~0;
791 if (fs->val.fcoe && !fs->mask.fcoe)
793 if (fs->val.matchtype && !fs->mask.matchtype)
794 fs->mask.matchtype |= ~0;
795 if (fs->val.macidx && !fs->mask.macidx)
796 fs->mask.macidx |= ~0;
797 if (fs->val.ethtype && !fs->mask.ethtype)
798 fs->mask.ethtype |= ~0;
799 if (fs->val.ivlan && !fs->mask.ivlan)
800 fs->mask.ivlan |= ~0;
801 if (fs->val.ovlan && !fs->mask.ovlan)
802 fs->mask.ovlan |= ~0;
803 if (fs->val.frag && !fs->mask.frag)
805 if (fs->val.tos && !fs->mask.tos)
807 if (fs->val.proto && !fs->mask.proto)
808 fs->mask.proto |= ~0;
810 for (i = 0; i < ARRAY_SIZE(fs->val.lip); i++) {
811 lip |= fs->val.lip[i];
812 lip_mask |= fs->mask.lip[i];
813 fip |= fs->val.fip[i];
814 fip_mask |= fs->mask.fip[i];
817 if (lip && !lip_mask)
818 memset(fs->mask.lip, ~0, sizeof(fs->mask.lip));
820 if (fip && !fip_mask)
821 memset(fs->mask.fip, ~0, sizeof(fs->mask.lip));
823 if (fs->val.lport && !fs->mask.lport)
825 if (fs->val.fport && !fs->mask.fport)
829 static bool is_addr_all_mask(u8 *ipmask, int family)
831 if (family == AF_INET) {
832 struct in_addr *addr;
834 addr = (struct in_addr *)ipmask;
835 if (addr->s_addr == 0xffffffff)
837 } else if (family == AF_INET6) {
838 struct in6_addr *addr6;
840 addr6 = (struct in6_addr *)ipmask;
841 if (addr6->s6_addr32[0] == 0xffffffff &&
842 addr6->s6_addr32[1] == 0xffffffff &&
843 addr6->s6_addr32[2] == 0xffffffff &&
844 addr6->s6_addr32[3] == 0xffffffff)
850 static bool is_inaddr_any(u8 *ip, int family)
854 if (family == AF_INET) {
855 struct in_addr *addr;
857 addr = (struct in_addr *)ip;
858 if (addr->s_addr == htonl(INADDR_ANY))
860 } else if (family == AF_INET6) {
861 struct in6_addr *addr6;
863 addr6 = (struct in6_addr *)ip;
864 addr_type = ipv6_addr_type((const struct in6_addr *)
866 if (addr_type == IPV6_ADDR_ANY)
872 bool is_filter_exact_match(struct adapter *adap,
873 struct ch_filter_specification *fs)
875 struct tp_params *tp = &adap->params.tp;
876 u64 hash_filter_mask = tp->hash_filter_mask;
879 if (!is_hashfilter(adap))
882 /* Keep tunnel VNI match disabled for hash-filters for now */
883 if (fs->mask.encap_vld)
887 if (is_inaddr_any(fs->val.fip, AF_INET6) ||
888 !is_addr_all_mask(fs->mask.fip, AF_INET6))
891 if (is_inaddr_any(fs->val.lip, AF_INET6) ||
892 !is_addr_all_mask(fs->mask.lip, AF_INET6))
895 if (is_inaddr_any(fs->val.fip, AF_INET) ||
896 !is_addr_all_mask(fs->mask.fip, AF_INET))
899 if (is_inaddr_any(fs->val.lip, AF_INET) ||
900 !is_addr_all_mask(fs->mask.lip, AF_INET))
904 if (!fs->val.lport || fs->mask.lport != 0xffff)
907 if (!fs->val.fport || fs->mask.fport != 0xffff)
910 /* calculate tuple mask and compare with mask configured in hw */
911 if (tp->fcoe_shift >= 0)
912 ntuple_mask |= (u64)fs->mask.fcoe << tp->fcoe_shift;
914 if (tp->port_shift >= 0)
915 ntuple_mask |= (u64)fs->mask.iport << tp->port_shift;
917 if (tp->vnic_shift >= 0) {
918 if ((adap->params.tp.ingress_config & VNIC_F))
919 ntuple_mask |= (u64)fs->mask.pfvf_vld << tp->vnic_shift;
921 ntuple_mask |= (u64)fs->mask.ovlan_vld <<
925 if (tp->vlan_shift >= 0)
926 ntuple_mask |= (u64)fs->mask.ivlan << tp->vlan_shift;
928 if (tp->tos_shift >= 0)
929 ntuple_mask |= (u64)fs->mask.tos << tp->tos_shift;
931 if (tp->protocol_shift >= 0)
932 ntuple_mask |= (u64)fs->mask.proto << tp->protocol_shift;
934 if (tp->ethertype_shift >= 0)
935 ntuple_mask |= (u64)fs->mask.ethtype << tp->ethertype_shift;
937 if (tp->macmatch_shift >= 0)
938 ntuple_mask |= (u64)fs->mask.macidx << tp->macmatch_shift;
940 if (tp->matchtype_shift >= 0)
941 ntuple_mask |= (u64)fs->mask.matchtype << tp->matchtype_shift;
943 if (tp->frag_shift >= 0)
944 ntuple_mask |= (u64)fs->mask.frag << tp->frag_shift;
946 if (ntuple_mask != hash_filter_mask)
952 static u64 hash_filter_ntuple(struct ch_filter_specification *fs,
953 struct net_device *dev)
955 struct adapter *adap = netdev2adap(dev);
956 struct tp_params *tp = &adap->params.tp;
959 /* Initialize each of the fields which we care about which are present
960 * in the Compressed Filter Tuple.
962 if (tp->vlan_shift >= 0 && fs->mask.ivlan)
963 ntuple |= (FT_VLAN_VLD_F | fs->val.ivlan) << tp->vlan_shift;
965 if (tp->port_shift >= 0 && fs->mask.iport)
966 ntuple |= (u64)fs->val.iport << tp->port_shift;
968 if (tp->protocol_shift >= 0) {
970 ntuple |= (u64)IPPROTO_TCP << tp->protocol_shift;
972 ntuple |= (u64)fs->val.proto << tp->protocol_shift;
975 if (tp->tos_shift >= 0 && fs->mask.tos)
976 ntuple |= (u64)(fs->val.tos) << tp->tos_shift;
978 if (tp->vnic_shift >= 0) {
979 if ((adap->params.tp.ingress_config & USE_ENC_IDX_F) &&
981 ntuple |= (u64)((fs->val.encap_vld << 16) |
982 (fs->val.ovlan)) << tp->vnic_shift;
983 else if ((adap->params.tp.ingress_config & VNIC_F) &&
985 ntuple |= (u64)((fs->val.pfvf_vld << 16) |
987 (fs->val.vf)) << tp->vnic_shift;
989 ntuple |= (u64)((fs->val.ovlan_vld << 16) |
990 (fs->val.ovlan)) << tp->vnic_shift;
993 if (tp->macmatch_shift >= 0 && fs->mask.macidx)
994 ntuple |= (u64)(fs->val.macidx) << tp->macmatch_shift;
996 if (tp->ethertype_shift >= 0 && fs->mask.ethtype)
997 ntuple |= (u64)(fs->val.ethtype) << tp->ethertype_shift;
999 if (tp->matchtype_shift >= 0 && fs->mask.matchtype)
1000 ntuple |= (u64)(fs->val.matchtype) << tp->matchtype_shift;
1002 if (tp->frag_shift >= 0 && fs->mask.frag)
1003 ntuple |= (u64)(fs->val.frag) << tp->frag_shift;
1005 if (tp->fcoe_shift >= 0 && fs->mask.fcoe)
1006 ntuple |= (u64)(fs->val.fcoe) << tp->fcoe_shift;
1010 static void mk_act_open_req6(struct filter_entry *f, struct sk_buff *skb,
1011 unsigned int qid_filterid, struct adapter *adap)
1013 struct cpl_t6_act_open_req6 *t6req = NULL;
1014 struct cpl_act_open_req6 *req = NULL;
1016 t6req = (struct cpl_t6_act_open_req6 *)__skb_put(skb, sizeof(*t6req));
1017 INIT_TP_WR(t6req, 0);
1018 req = (struct cpl_act_open_req6 *)t6req;
1019 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6, qid_filterid));
1020 req->local_port = cpu_to_be16(f->fs.val.lport);
1021 req->peer_port = cpu_to_be16(f->fs.val.fport);
1022 req->local_ip_hi = *(__be64 *)(&f->fs.val.lip);
1023 req->local_ip_lo = *(((__be64 *)&f->fs.val.lip) + 1);
1024 req->peer_ip_hi = *(__be64 *)(&f->fs.val.fip);
1025 req->peer_ip_lo = *(((__be64 *)&f->fs.val.fip) + 1);
1026 req->opt0 = cpu_to_be64(NAGLE_V(f->fs.newvlan == VLAN_REMOVE ||
1027 f->fs.newvlan == VLAN_REWRITE) |
1028 DELACK_V(f->fs.hitcnts) |
1029 L2T_IDX_V(f->l2t ? f->l2t->idx : 0) |
1030 SMAC_SEL_V((cxgb4_port_viid(f->dev) &
1032 TX_CHAN_V(f->fs.eport) |
1033 NO_CONG_V(f->fs.rpttid) |
1034 ULP_MODE_V(f->fs.nat_mode ?
1035 ULP_MODE_TCPDDP : ULP_MODE_NONE) |
1036 TCAM_BYPASS_F | NON_OFFLOAD_F);
1037 t6req->params = cpu_to_be64(FILTER_TUPLE_V(hash_filter_ntuple(&f->fs,
1039 t6req->opt2 = htonl(RSS_QUEUE_VALID_F |
1040 RSS_QUEUE_V(f->fs.iq) |
1041 TX_QUEUE_V(f->fs.nat_mode) |
1043 RX_CHANNEL_V(cxgb4_port_e2cchan(f->dev)) |
1044 CONG_CNTRL_V((f->fs.action == FILTER_DROP) |
1045 (f->fs.dirsteer << 1)) |
1046 PACE_V((f->fs.maskhash) |
1047 ((f->fs.dirsteerhash) << 1)) |
1048 CCTRL_ECN_V(f->fs.action == FILTER_SWITCH));
1051 static void mk_act_open_req(struct filter_entry *f, struct sk_buff *skb,
1052 unsigned int qid_filterid, struct adapter *adap)
1054 struct cpl_t6_act_open_req *t6req = NULL;
1055 struct cpl_act_open_req *req = NULL;
1057 t6req = (struct cpl_t6_act_open_req *)__skb_put(skb, sizeof(*t6req));
1058 INIT_TP_WR(t6req, 0);
1059 req = (struct cpl_act_open_req *)t6req;
1060 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, qid_filterid));
1061 req->local_port = cpu_to_be16(f->fs.val.lport);
1062 req->peer_port = cpu_to_be16(f->fs.val.fport);
1063 memcpy(&req->local_ip, f->fs.val.lip, 4);
1064 memcpy(&req->peer_ip, f->fs.val.fip, 4);
1065 req->opt0 = cpu_to_be64(NAGLE_V(f->fs.newvlan == VLAN_REMOVE ||
1066 f->fs.newvlan == VLAN_REWRITE) |
1067 DELACK_V(f->fs.hitcnts) |
1068 L2T_IDX_V(f->l2t ? f->l2t->idx : 0) |
1069 SMAC_SEL_V((cxgb4_port_viid(f->dev) &
1071 TX_CHAN_V(f->fs.eport) |
1072 NO_CONG_V(f->fs.rpttid) |
1073 ULP_MODE_V(f->fs.nat_mode ?
1074 ULP_MODE_TCPDDP : ULP_MODE_NONE) |
1075 TCAM_BYPASS_F | NON_OFFLOAD_F);
1077 t6req->params = cpu_to_be64(FILTER_TUPLE_V(hash_filter_ntuple(&f->fs,
1079 t6req->opt2 = htonl(RSS_QUEUE_VALID_F |
1080 RSS_QUEUE_V(f->fs.iq) |
1081 TX_QUEUE_V(f->fs.nat_mode) |
1083 RX_CHANNEL_V(cxgb4_port_e2cchan(f->dev)) |
1084 CONG_CNTRL_V((f->fs.action == FILTER_DROP) |
1085 (f->fs.dirsteer << 1)) |
1086 PACE_V((f->fs.maskhash) |
1087 ((f->fs.dirsteerhash) << 1)) |
1088 CCTRL_ECN_V(f->fs.action == FILTER_SWITCH));
1091 static int cxgb4_set_hash_filter(struct net_device *dev,
1092 struct ch_filter_specification *fs,
1093 struct filter_ctx *ctx)
1095 struct adapter *adapter = netdev2adap(dev);
1096 struct port_info *pi = netdev_priv(dev);
1097 struct tid_info *t = &adapter->tids;
1098 struct filter_entry *f;
1099 struct sk_buff *skb;
1104 fill_default_mask(fs);
1105 ret = validate_filter(dev, fs);
1109 iq = get_filter_steerq(dev, fs);
1113 f = kzalloc(sizeof(*f), GFP_KERNEL);
1122 /* If the new filter requires loopback Destination MAC and/or VLAN
1123 * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
1126 if (f->fs.newdmac || f->fs.newvlan) {
1127 /* allocate L2T entry for new filter */
1128 f->l2t = t4_l2t_alloc_switching(adapter, f->fs.vlan,
1129 f->fs.eport, f->fs.dmac);
1136 /* If the new filter requires loopback Source MAC rewriting then
1137 * we need to allocate a SMT entry for the filter.
1139 if (f->fs.newsmac) {
1140 f->smt = cxgb4_smt_alloc_switching(f->dev, f->fs.smac);
1143 cxgb4_l2t_release(f->l2t);
1151 atid = cxgb4_alloc_atid(t, f);
1157 iconf = adapter->params.tp.ingress_config;
1158 if (iconf & VNIC_F) {
1159 f->fs.val.ovlan = (fs->val.pf << 13) | fs->val.vf;
1160 f->fs.mask.ovlan = (fs->mask.pf << 13) | fs->mask.vf;
1161 f->fs.val.ovlan_vld = fs->val.pfvf_vld;
1162 f->fs.mask.ovlan_vld = fs->mask.pfvf_vld;
1163 } else if (iconf & USE_ENC_IDX_F) {
1164 if (f->fs.val.encap_vld) {
1165 struct port_info *pi = netdev_priv(f->dev);
1166 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
1168 /* allocate MPS TCAM entry */
1169 ret = t4_alloc_encap_mac_filt(adapter, pi->viid,
1178 f->fs.val.ovlan = ret;
1179 f->fs.mask.ovlan = 0xffff;
1180 f->fs.val.ovlan_vld = 1;
1181 f->fs.mask.ovlan_vld = 1;
1185 size = sizeof(struct cpl_t6_act_open_req);
1187 ret = cxgb4_clip_get(f->dev, (const u32 *)&f->fs.val.lip, 1);
1191 skb = alloc_skb(size, GFP_KERNEL);
1197 mk_act_open_req6(f, skb,
1198 ((adapter->sge.fw_evtq.abs_id << 14) | atid),
1201 skb = alloc_skb(size, GFP_KERNEL);
1207 mk_act_open_req(f, skb,
1208 ((adapter->sge.fw_evtq.abs_id << 14) | atid),
1213 set_wr_txq(skb, CPL_PRIORITY_SETUP, f->fs.val.iport & 0x3);
1214 t4_ofld_send(adapter, skb);
1218 cxgb4_clip_release(f->dev, (const u32 *)&f->fs.val.lip, 1);
1221 if (f->fs.val.encap_vld && f->fs.val.ovlan_vld)
1222 t4_free_encap_mac_filt(adapter, pi->viid, f->fs.val.ovlan, 1);
1225 cxgb4_free_atid(t, atid);
1229 cxgb4_smt_release(f->smt);
1235 cxgb4_l2t_release(f->l2t);
1244 /* Check a Chelsio Filter Request for validity, convert it into our internal
1245 * format and send it to the hardware. Return 0 on success, an error number
1246 * otherwise. We attach any provided filter operation context to the internal
1247 * filter specification in order to facilitate signaling completion of the
1250 int __cxgb4_set_filter(struct net_device *dev, int filter_id,
1251 struct ch_filter_specification *fs,
1252 struct filter_ctx *ctx)
1254 struct adapter *adapter = netdev2adap(dev);
1255 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
1256 unsigned int max_fidx, fidx;
1257 struct filter_entry *f;
1262 if (is_hashfilter(adapter))
1263 return cxgb4_set_hash_filter(dev, fs, ctx);
1264 netdev_err(dev, "%s: Exact-match filters only supported with Hash Filter configuration\n",
1269 max_fidx = adapter->tids.nftids;
1270 if (filter_id != (max_fidx + adapter->tids.nsftids - 1) &&
1271 filter_id >= max_fidx)
1274 fill_default_mask(fs);
1276 ret = validate_filter(dev, fs);
1280 iq = get_filter_steerq(dev, fs);
1284 /* IPv6 filters occupy four slots and must be aligned on
1285 * four-slot boundaries. IPv4 filters only occupy a single
1286 * slot and have no alignment requirements but writing a new
1287 * IPv4 filter into the middle of an existing IPv6 filter
1288 * requires clearing the old IPv6 filter and hence we prevent
1291 if (fs->type == 0) { /* IPv4 */
1292 /* For T6, If our IPv4 filter isn't being written to a
1293 * multiple of two filter index and there's an IPv6
1294 * filter at the multiple of 2 base slot, then we need
1295 * to delete that IPv6 filter ...
1296 * For adapters below T6, IPv6 filter occupies 4 entries.
1297 * Hence we need to delete the filter in multiple of 4 slot.
1299 if (chip_ver < CHELSIO_T6)
1300 fidx = filter_id & ~0x3;
1302 fidx = filter_id & ~0x1;
1304 if (fidx != filter_id &&
1305 adapter->tids.ftid_tab[fidx].fs.type) {
1306 f = &adapter->tids.ftid_tab[fidx];
1308 dev_err(adapter->pdev_dev,
1309 "Invalid location. IPv6 requires 4 slots and is occupying slots %u to %u\n",
1315 if (chip_ver < CHELSIO_T6) {
1316 /* Ensure that the IPv6 filter is aligned on a
1317 * multiple of 4 boundary.
1319 if (filter_id & 0x3) {
1320 dev_err(adapter->pdev_dev,
1321 "Invalid location. IPv6 must be aligned on a 4-slot boundary\n");
1325 /* Check all except the base overlapping IPv4 filter
1328 for (fidx = filter_id + 1; fidx < filter_id + 4;
1330 f = &adapter->tids.ftid_tab[fidx];
1332 dev_err(adapter->pdev_dev,
1333 "Invalid location. IPv6 requires 4 slots and an IPv4 filter exists at %u\n",
1339 /* For T6, CLIP being enabled, IPv6 filter would occupy
1342 if (filter_id & 0x1)
1344 /* Check overlapping IPv4 filter slot */
1345 fidx = filter_id + 1;
1346 f = &adapter->tids.ftid_tab[fidx];
1348 pr_err("%s: IPv6 filter requires 2 indices. IPv4 filter already present at %d. Please remove IPv4 filter first.\n",
1355 /* Check to make sure that provided filter index is not
1356 * already in use by someone else
1358 f = &adapter->tids.ftid_tab[filter_id];
1362 fidx = filter_id + adapter->tids.ftid_base;
1363 ret = cxgb4_set_ftid(&adapter->tids, filter_id,
1364 fs->type ? PF_INET6 : PF_INET,
1369 /* Check t make sure the filter requested is writable ... */
1370 ret = writable_filter(f);
1372 /* Clear the bits we have set above */
1373 cxgb4_clear_ftid(&adapter->tids, filter_id,
1374 fs->type ? PF_INET6 : PF_INET,
1379 if (is_t6(adapter->params.chip) && fs->type &&
1380 ipv6_addr_type((const struct in6_addr *)fs->val.lip) !=
1382 ret = cxgb4_clip_get(dev, (const u32 *)&fs->val.lip, 1);
1384 cxgb4_clear_ftid(&adapter->tids, filter_id, PF_INET6,
1390 /* Convert the filter specification into our internal format.
1391 * We copy the PF/VF specification into the Outer VLAN field
1392 * here so the rest of the code -- including the interface to
1393 * the firmware -- doesn't have to constantly do these checks.
1399 iconf = adapter->params.tp.ingress_config;
1400 if (iconf & VNIC_F) {
1401 f->fs.val.ovlan = (fs->val.pf << 13) | fs->val.vf;
1402 f->fs.mask.ovlan = (fs->mask.pf << 13) | fs->mask.vf;
1403 f->fs.val.ovlan_vld = fs->val.pfvf_vld;
1404 f->fs.mask.ovlan_vld = fs->mask.pfvf_vld;
1405 } else if (iconf & USE_ENC_IDX_F) {
1406 if (f->fs.val.encap_vld) {
1407 struct port_info *pi = netdev_priv(f->dev);
1408 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
1410 /* allocate MPS TCAM entry */
1411 ret = t4_alloc_encap_mac_filt(adapter, pi->viid,
1420 f->fs.val.ovlan = ret;
1421 f->fs.mask.ovlan = 0x1ff;
1422 f->fs.val.ovlan_vld = 1;
1423 f->fs.mask.ovlan_vld = 1;
1427 /* Attempt to set the filter. If we don't succeed, we clear
1428 * it and return the failure.
1431 f->tid = fidx; /* Save the actual tid */
1432 ret = set_filter_wr(adapter, filter_id);
1434 cxgb4_clear_ftid(&adapter->tids, filter_id,
1435 fs->type ? PF_INET6 : PF_INET,
1437 clear_filter(adapter, f);
1443 if (is_t6(adapter->params.chip) && f->fs.type)
1444 cxgb4_clip_release(f->dev, (const u32 *)&f->fs.val.lip, 1);
1445 cxgb4_clear_ftid(&adapter->tids, filter_id,
1446 fs->type ? PF_INET6 : PF_INET, chip_ver);
1450 static int cxgb4_del_hash_filter(struct net_device *dev, int filter_id,
1451 struct filter_ctx *ctx)
1453 struct adapter *adapter = netdev2adap(dev);
1454 struct tid_info *t = &adapter->tids;
1455 struct cpl_abort_req *abort_req;
1456 struct cpl_abort_rpl *abort_rpl;
1457 struct cpl_set_tcb_field *req;
1458 struct ulptx_idata *aligner;
1459 struct work_request_hdr *wr;
1460 struct filter_entry *f;
1461 struct sk_buff *skb;
1465 netdev_dbg(dev, "%s: filter_id = %d ; nftids = %d\n",
1466 __func__, filter_id, adapter->tids.nftids);
1468 if (filter_id > adapter->tids.ntids)
1471 f = lookup_tid(t, filter_id);
1473 netdev_err(dev, "%s: no filter entry for filter_id = %d",
1474 __func__, filter_id);
1478 ret = writable_filter(f);
1487 wrlen = roundup(sizeof(*wr) + (sizeof(*req) + sizeof(*aligner))
1488 + sizeof(*abort_req) + sizeof(*abort_rpl), 16);
1489 skb = alloc_skb(wrlen, GFP_KERNEL);
1491 netdev_err(dev, "%s: could not allocate skb ..\n", __func__);
1494 set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
1495 req = (struct cpl_set_tcb_field *)__skb_put(skb, wrlen);
1496 INIT_ULPTX_WR(req, wrlen, 0, 0);
1497 wr = (struct work_request_hdr *)req;
1499 req = (struct cpl_set_tcb_field *)wr;
1500 mk_set_tcb_ulp(f, req, TCB_RSS_INFO_W, TCB_RSS_INFO_V(TCB_RSS_INFO_M),
1501 TCB_RSS_INFO_V(adapter->sge.fw_evtq.abs_id), 0, 1);
1502 aligner = (struct ulptx_idata *)(req + 1);
1503 abort_req = (struct cpl_abort_req *)(aligner + 1);
1504 mk_abort_req_ulp(abort_req, f->tid);
1505 abort_rpl = (struct cpl_abort_rpl *)(abort_req + 1);
1506 mk_abort_rpl_ulp(abort_rpl, f->tid);
1507 t4_ofld_send(adapter, skb);
1511 /* Check a delete filter request for validity and send it to the hardware.
1512 * Return 0 on success, an error number otherwise. We attach any provided
1513 * filter operation context to the internal filter specification in order to
1514 * facilitate signaling completion of the operation.
1516 int __cxgb4_del_filter(struct net_device *dev, int filter_id,
1517 struct ch_filter_specification *fs,
1518 struct filter_ctx *ctx)
1520 struct adapter *adapter = netdev2adap(dev);
1521 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
1522 struct filter_entry *f;
1523 unsigned int max_fidx;
1526 if (fs && fs->hash) {
1527 if (is_hashfilter(adapter))
1528 return cxgb4_del_hash_filter(dev, filter_id, ctx);
1529 netdev_err(dev, "%s: Exact-match filters only supported with Hash Filter configuration\n",
1534 max_fidx = adapter->tids.nftids;
1535 if (filter_id != (max_fidx + adapter->tids.nsftids - 1) &&
1536 filter_id >= max_fidx)
1539 f = &adapter->tids.ftid_tab[filter_id];
1540 ret = writable_filter(f);
1546 cxgb4_clear_ftid(&adapter->tids, filter_id,
1547 f->fs.type ? PF_INET6 : PF_INET,
1549 return del_filter_wr(adapter, filter_id);
1552 /* If the caller has passed in a Completion Context then we need to
1553 * mark it as a successful completion so they don't stall waiting
1558 complete(&ctx->completion);
1563 int cxgb4_set_filter(struct net_device *dev, int filter_id,
1564 struct ch_filter_specification *fs)
1566 struct filter_ctx ctx;
1569 init_completion(&ctx.completion);
1571 ret = __cxgb4_set_filter(dev, filter_id, fs, &ctx);
1575 /* Wait for reply */
1576 ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
1585 int cxgb4_del_filter(struct net_device *dev, int filter_id,
1586 struct ch_filter_specification *fs)
1588 struct filter_ctx ctx;
1591 if (netdev2adap(dev)->flags & CXGB4_SHUTTING_DOWN)
1594 init_completion(&ctx.completion);
1596 ret = __cxgb4_del_filter(dev, filter_id, fs, &ctx);
1600 /* Wait for reply */
1601 ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
1610 static int configure_filter_tcb(struct adapter *adap, unsigned int tid,
1611 struct filter_entry *f)
1614 set_tcb_field(adap, f, tid, TCB_TIMESTAMP_W,
1615 TCB_TIMESTAMP_V(TCB_TIMESTAMP_M) |
1616 TCB_RTT_TS_RECENT_AGE_V(TCB_RTT_TS_RECENT_AGE_M),
1617 TCB_TIMESTAMP_V(0ULL) |
1618 TCB_RTT_TS_RECENT_AGE_V(0ULL),
1622 set_tcb_tflag(adap, f, tid, TF_CCTRL_ECE_S, 1,
1625 if (f->fs.newvlan == VLAN_INSERT ||
1626 f->fs.newvlan == VLAN_REWRITE)
1627 set_tcb_tflag(adap, f, tid, TF_CCTRL_RFR_S, 1,
1630 configure_filter_smac(adap, f);
1632 if (f->fs.nat_mode) {
1633 switch (f->fs.nat_mode) {
1635 set_nat_params(adap, f, tid, true, false, false, false);
1638 case NAT_MODE_DIP_DP:
1639 set_nat_params(adap, f, tid, true, false, true, false);
1642 case NAT_MODE_DIP_DP_SIP:
1643 set_nat_params(adap, f, tid, true, true, true, false);
1645 case NAT_MODE_DIP_DP_SP:
1646 set_nat_params(adap, f, tid, true, false, true, true);
1649 case NAT_MODE_SIP_SP:
1650 set_nat_params(adap, f, tid, false, true, false, true);
1653 case NAT_MODE_DIP_SIP_SP:
1654 set_nat_params(adap, f, tid, true, true, false, true);
1658 set_nat_params(adap, f, tid, true, true, true, true);
1662 pr_err("%s: Invalid NAT mode: %d\n",
1663 __func__, f->fs.nat_mode);
1670 void hash_del_filter_rpl(struct adapter *adap,
1671 const struct cpl_abort_rpl_rss *rpl)
1673 unsigned int status = rpl->status;
1674 struct tid_info *t = &adap->tids;
1675 unsigned int tid = GET_TID(rpl);
1676 struct filter_ctx *ctx = NULL;
1677 struct filter_entry *f;
1679 dev_dbg(adap->pdev_dev, "%s: status = %u; tid = %u\n",
1680 __func__, status, tid);
1682 f = lookup_tid(t, tid);
1684 dev_err(adap->pdev_dev, "%s:could not find filter entry",
1690 clear_filter(adap, f);
1691 cxgb4_remove_tid(t, 0, tid, 0);
1695 complete(&ctx->completion);
1699 void hash_filter_rpl(struct adapter *adap, const struct cpl_act_open_rpl *rpl)
1701 unsigned int ftid = TID_TID_G(AOPEN_ATID_G(ntohl(rpl->atid_status)));
1702 unsigned int status = AOPEN_STATUS_G(ntohl(rpl->atid_status));
1703 struct tid_info *t = &adap->tids;
1704 unsigned int tid = GET_TID(rpl);
1705 struct filter_ctx *ctx = NULL;
1706 struct filter_entry *f;
1708 dev_dbg(adap->pdev_dev, "%s: tid = %u; atid = %u; status = %u\n",
1709 __func__, tid, ftid, status);
1711 f = lookup_atid(t, ftid);
1713 dev_err(adap->pdev_dev, "%s:could not find filter entry",
1725 cxgb4_insert_tid(t, f, f->tid, 0);
1726 cxgb4_free_atid(t, ftid);
1731 if (configure_filter_tcb(adap, tid, f)) {
1732 clear_filter(adap, f);
1733 cxgb4_remove_tid(t, 0, tid, 0);
1736 ctx->result = -EINVAL;
1737 complete(&ctx->completion);
1744 if (status != CPL_ERR_TCAM_FULL)
1745 dev_err(adap->pdev_dev, "%s: filter creation PROBLEM; status = %u\n",
1749 if (status == CPL_ERR_TCAM_FULL)
1750 ctx->result = -ENOSPC;
1752 ctx->result = -EINVAL;
1754 clear_filter(adap, f);
1755 cxgb4_free_atid(t, ftid);
1759 complete(&ctx->completion);
1762 /* Handle a filter write/deletion reply. */
1763 void filter_rpl(struct adapter *adap, const struct cpl_set_tcb_rpl *rpl)
1765 unsigned int tid = GET_TID(rpl);
1766 struct filter_entry *f = NULL;
1767 unsigned int max_fidx;
1770 max_fidx = adap->tids.nftids + adap->tids.nsftids;
1771 /* Get the corresponding filter entry for this tid */
1772 if (adap->tids.ftid_tab) {
1773 /* Check this in normal filter region */
1774 idx = tid - adap->tids.ftid_base;
1775 if (idx >= max_fidx)
1777 f = &adap->tids.ftid_tab[idx];
1782 /* We found the filter entry for this tid */
1784 unsigned int ret = TCB_COOKIE_G(rpl->cookie);
1785 struct filter_ctx *ctx;
1787 /* Pull off any filter operation context attached to the
1793 if (ret == FW_FILTER_WR_FLT_DELETED) {
1794 /* Clear the filter when we get confirmation from the
1795 * hardware that the filter has been deleted.
1797 clear_filter(adap, f);
1800 } else if (ret == FW_FILTER_WR_FLT_ADDED) {
1804 err = configure_filter_smac(adap, f);
1807 f->pending = 0; /* async setup completed */
1814 clear_filter(adap, f);
1819 /* Something went wrong. Issue a warning about the
1820 * problem and clear everything out.
1822 dev_err(adap->pdev_dev, "filter %u setup failed with error %u\n",
1824 clear_filter(adap, f);
1826 ctx->result = -EINVAL;
1829 complete(&ctx->completion);
1833 void init_hash_filter(struct adapter *adap)
1837 /* On T6, verify the necessary register configs and warn the user in
1838 * case of improper config
1840 if (is_t6(adap->params.chip)) {
1841 if (is_offload(adap)) {
1842 if (!(t4_read_reg(adap, TP_GLOBAL_CONFIG_A)
1843 & ACTIVEFILTERCOUNTS_F)) {
1844 dev_err(adap->pdev_dev, "Invalid hash filter + ofld config\n");
1848 reg = t4_read_reg(adap, LE_DB_RSP_CODE_0_A);
1849 if (TCAM_ACTV_HIT_G(reg) != 4) {
1850 dev_err(adap->pdev_dev, "Invalid hash filter config\n");
1854 reg = t4_read_reg(adap, LE_DB_RSP_CODE_1_A);
1855 if (HASH_ACTV_HIT_G(reg) != 4) {
1856 dev_err(adap->pdev_dev, "Invalid hash filter config\n");
1862 dev_err(adap->pdev_dev, "Hash filter supported only on T6\n");
1866 adap->params.hash_filter = 1;