1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Chelsio Communications.
8 #include "cxgbe_filter.h"
11 * Validate if the requested filter specification can be set by checking
12 * if the requested features have been enabled
14 int validate_filter(struct adapter *adapter, struct ch_filter_specification *fs)
19 * Check for unconfigured fields being used.
21 fconf = adapter->params.tp.vlan_pri_map;
24 (fs->val._field || fs->mask._field)
25 #define U(_mask, _field) \
26 (!(fconf & (_mask)) && S(_field))
28 if (U(F_ETHERTYPE, ethtype) || U(F_PROTOCOL, proto))
37 * Get the queue to which the traffic must be steered to.
39 static unsigned int get_filter_steerq(struct rte_eth_dev *dev,
40 struct ch_filter_specification *fs)
42 struct port_info *pi = ethdev2pinfo(dev);
43 struct adapter *adapter = pi->adapter;
47 * If the user has requested steering matching Ingress Packets
48 * to a specific Queue Set, we need to make sure it's in range
49 * for the port and map that into the Absolute Queue ID of the
50 * Queue Set's Response Queue.
56 * If the iq id is greater than the number of qsets,
57 * then assume it is an absolute qid.
59 if (fs->iq < pi->n_rx_qsets)
60 iq = adapter->sge.ethrxq[pi->first_qset +
69 /* Return an error number if the indicated filter isn't writable ... */
70 int writable_filter(struct filter_entry *f)
81 * Check if entry already filled.
83 bool is_filter_set(struct tid_info *t, int fidx, int family)
88 /* IPv6 requires four slots and IPv4 requires only 1 slot.
89 * Ensure, there's enough slots available.
91 max = family == FILTER_TYPE_IPV6 ? fidx + 3 : fidx;
93 t4_os_lock(&t->ftid_lock);
94 for (i = fidx; i <= max; i++) {
95 if (rte_bitmap_get(t->ftid_bmap, i)) {
100 t4_os_unlock(&t->ftid_lock);
105 * Allocate a available free entry
107 int cxgbe_alloc_ftid(struct adapter *adap, unsigned int family)
109 struct tid_info *t = &adap->tids;
111 int size = t->nftids;
113 t4_os_lock(&t->ftid_lock);
114 if (family == FILTER_TYPE_IPV6)
115 pos = cxgbe_bitmap_find_free_region(t->ftid_bmap, size, 4);
117 pos = cxgbe_find_first_zero_bit(t->ftid_bmap, size);
118 t4_os_unlock(&t->ftid_lock);
120 return pos < size ? pos : -1;
124 * Clear a filter and release any of its resources that we own. This also
125 * clears the filter's "pending" status.
127 void clear_filter(struct filter_entry *f)
130 * The zeroing of the filter rule below clears the filter valid,
131 * pending, locked flags etc. so it's all we need for
134 memset(f, 0, sizeof(*f));
138 * t4_mk_filtdelwr - create a delete filter WR
139 * @ftid: the filter ID
140 * @wr: the filter work request to populate
141 * @qid: ingress queue to receive the delete notification
143 * Creates a filter work request to delete the supplied filter. If @qid is
144 * negative the delete notification is suppressed.
146 static void t4_mk_filtdelwr(unsigned int ftid, struct fw_filter_wr *wr, int qid)
148 memset(wr, 0, sizeof(*wr));
149 wr->op_pkd = cpu_to_be32(V_FW_WR_OP(FW_FILTER_WR));
150 wr->len16_pkd = cpu_to_be32(V_FW_WR_LEN16(sizeof(*wr) / 16));
151 wr->tid_to_iq = cpu_to_be32(V_FW_FILTER_WR_TID(ftid) |
152 V_FW_FILTER_WR_NOREPLY(qid < 0));
153 wr->del_filter_to_l2tix = cpu_to_be32(F_FW_FILTER_WR_DEL_FILTER);
155 wr->rx_chan_rx_rpl_iq =
156 cpu_to_be16(V_FW_FILTER_WR_RX_RPL_IQ(qid));
160 * Create FW work request to delete the filter at a specified index
162 static int del_filter_wr(struct rte_eth_dev *dev, unsigned int fidx)
164 struct adapter *adapter = ethdev2adap(dev);
165 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
166 struct rte_mbuf *mbuf;
167 struct fw_filter_wr *fwr;
168 struct sge_ctrl_txq *ctrlq;
169 unsigned int port_id = ethdev2pinfo(dev)->port_id;
171 ctrlq = &adapter->sge.ctrlq[port_id];
172 mbuf = rte_pktmbuf_alloc(ctrlq->mb_pool);
176 mbuf->data_len = sizeof(*fwr);
177 mbuf->pkt_len = mbuf->data_len;
179 fwr = rte_pktmbuf_mtod(mbuf, struct fw_filter_wr *);
180 t4_mk_filtdelwr(f->tid, fwr, adapter->sge.fw_evtq.abs_id);
183 * Mark the filter as "pending" and ship off the Filter Work Request.
184 * When we get the Work Request Reply we'll clear the pending status.
187 t4_mgmt_tx(ctrlq, mbuf);
191 int set_filter_wr(struct rte_eth_dev *dev, unsigned int fidx)
193 struct adapter *adapter = ethdev2adap(dev);
194 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
195 struct rte_mbuf *mbuf;
196 struct fw_filter_wr *fwr;
197 struct sge_ctrl_txq *ctrlq;
198 unsigned int port_id = ethdev2pinfo(dev)->port_id;
201 ctrlq = &adapter->sge.ctrlq[port_id];
202 mbuf = rte_pktmbuf_alloc(ctrlq->mb_pool);
208 mbuf->data_len = sizeof(*fwr);
209 mbuf->pkt_len = mbuf->data_len;
211 fwr = rte_pktmbuf_mtod(mbuf, struct fw_filter_wr *);
212 memset(fwr, 0, sizeof(*fwr));
215 * Construct the work request to set the filter.
217 fwr->op_pkd = cpu_to_be32(V_FW_WR_OP(FW_FILTER_WR));
218 fwr->len16_pkd = cpu_to_be32(V_FW_WR_LEN16(sizeof(*fwr) / 16));
220 cpu_to_be32(V_FW_FILTER_WR_TID(f->tid) |
221 V_FW_FILTER_WR_RQTYPE(f->fs.type) |
222 V_FW_FILTER_WR_NOREPLY(0) |
223 V_FW_FILTER_WR_IQ(f->fs.iq));
224 fwr->del_filter_to_l2tix =
225 cpu_to_be32(V_FW_FILTER_WR_DROP(f->fs.action == FILTER_DROP) |
226 V_FW_FILTER_WR_DIRSTEER(f->fs.dirsteer) |
227 V_FW_FILTER_WR_HITCNTS(f->fs.hitcnts) |
228 V_FW_FILTER_WR_PRIO(f->fs.prio));
229 fwr->ethtype = cpu_to_be16(f->fs.val.ethtype);
230 fwr->ethtypem = cpu_to_be16(f->fs.mask.ethtype);
232 fwr->rx_chan_rx_rpl_iq =
233 cpu_to_be16(V_FW_FILTER_WR_RX_CHAN(0) |
234 V_FW_FILTER_WR_RX_RPL_IQ(adapter->sge.fw_evtq.abs_id
236 fwr->ptcl = f->fs.val.proto;
237 fwr->ptclm = f->fs.mask.proto;
238 rte_memcpy(fwr->lip, f->fs.val.lip, sizeof(fwr->lip));
239 rte_memcpy(fwr->lipm, f->fs.mask.lip, sizeof(fwr->lipm));
240 rte_memcpy(fwr->fip, f->fs.val.fip, sizeof(fwr->fip));
241 rte_memcpy(fwr->fipm, f->fs.mask.fip, sizeof(fwr->fipm));
242 fwr->lp = cpu_to_be16(f->fs.val.lport);
243 fwr->lpm = cpu_to_be16(f->fs.mask.lport);
244 fwr->fp = cpu_to_be16(f->fs.val.fport);
245 fwr->fpm = cpu_to_be16(f->fs.mask.fport);
248 * Mark the filter as "pending" and ship off the Filter Work Request.
249 * When we get the Work Request Reply we'll clear the pending status.
252 t4_mgmt_tx(ctrlq, mbuf);
260 * Set the corresponding entry in the bitmap. 4 slots are
261 * marked for IPv6, whereas only 1 slot is marked for IPv4.
263 static int cxgbe_set_ftid(struct tid_info *t, int fidx, int family)
265 t4_os_lock(&t->ftid_lock);
266 if (rte_bitmap_get(t->ftid_bmap, fidx)) {
267 t4_os_unlock(&t->ftid_lock);
271 if (family == FILTER_TYPE_IPV4) {
272 rte_bitmap_set(t->ftid_bmap, fidx);
274 rte_bitmap_set(t->ftid_bmap, fidx);
275 rte_bitmap_set(t->ftid_bmap, fidx + 1);
276 rte_bitmap_set(t->ftid_bmap, fidx + 2);
277 rte_bitmap_set(t->ftid_bmap, fidx + 3);
279 t4_os_unlock(&t->ftid_lock);
284 * Clear the corresponding entry in the bitmap. 4 slots are
285 * cleared for IPv6, whereas only 1 slot is cleared for IPv4.
287 static void cxgbe_clear_ftid(struct tid_info *t, int fidx, int family)
289 t4_os_lock(&t->ftid_lock);
290 if (family == FILTER_TYPE_IPV4) {
291 rte_bitmap_clear(t->ftid_bmap, fidx);
293 rte_bitmap_clear(t->ftid_bmap, fidx);
294 rte_bitmap_clear(t->ftid_bmap, fidx + 1);
295 rte_bitmap_clear(t->ftid_bmap, fidx + 2);
296 rte_bitmap_clear(t->ftid_bmap, fidx + 3);
298 t4_os_unlock(&t->ftid_lock);
302 * Check a delete filter request for validity and send it to the hardware.
303 * Return 0 on success, an error number otherwise. We attach any provided
304 * filter operation context to the internal filter specification in order to
305 * facilitate signaling completion of the operation.
307 int cxgbe_del_filter(struct rte_eth_dev *dev, unsigned int filter_id,
308 struct ch_filter_specification *fs,
309 struct filter_ctx *ctx)
311 struct port_info *pi = (struct port_info *)(dev->data->dev_private);
312 struct adapter *adapter = pi->adapter;
313 struct filter_entry *f;
316 if (filter_id >= adapter->tids.nftids)
319 ret = is_filter_set(&adapter->tids, filter_id, fs->type);
321 dev_warn(adap, "%s: could not find filter entry: %u\n",
322 __func__, filter_id);
326 f = &adapter->tids.ftid_tab[filter_id];
327 ret = writable_filter(f);
333 cxgbe_clear_ftid(&adapter->tids,
334 f->tid - adapter->tids.ftid_base,
335 f->fs.type ? FILTER_TYPE_IPV6 :
337 return del_filter_wr(dev, filter_id);
341 * If the caller has passed in a Completion Context then we need to
342 * mark it as a successful completion so they don't stall waiting
347 t4_complete(&ctx->completion);
354 * Check a Chelsio Filter Request for validity, convert it into our internal
355 * format and send it to the hardware. Return 0 on success, an error number
356 * otherwise. We attach any provided filter operation context to the internal
357 * filter specification in order to facilitate signaling completion of the
360 int cxgbe_set_filter(struct rte_eth_dev *dev, unsigned int filter_id,
361 struct ch_filter_specification *fs,
362 struct filter_ctx *ctx)
364 struct port_info *pi = ethdev2pinfo(dev);
365 struct adapter *adapter = pi->adapter;
366 unsigned int fidx, iq, fid_bit = 0;
367 struct filter_entry *f;
370 if (filter_id >= adapter->tids.nftids)
373 ret = validate_filter(adapter, fs);
378 * Ensure filter id is aligned on the 4 slot boundary for IPv6
384 ret = is_filter_set(&adapter->tids, filter_id, fs->type);
388 iq = get_filter_steerq(dev, fs);
391 * IPv6 filters occupy four slots and must be aligned on
392 * four-slot boundaries. IPv4 filters only occupy a single
393 * slot and have no alignment requirements but writing a new
394 * IPv4 filter into the middle of an existing IPv6 filter
395 * requires clearing the old IPv6 filter.
397 if (fs->type == FILTER_TYPE_IPV4) { /* IPv4 */
399 * If our IPv4 filter isn't being written to a
400 * multiple of four filter index and there's an IPv6
401 * filter at the multiple of 4 base slot, then we need
402 * to delete that IPv6 filter ...
404 fidx = filter_id & ~0x3;
405 if (fidx != filter_id && adapter->tids.ftid_tab[fidx].fs.type) {
406 f = &adapter->tids.ftid_tab[fidx];
412 * Ensure that the IPv6 filter is aligned on a
413 * multiple of 4 boundary.
419 * Check all except the base overlapping IPv4 filter
422 for (fidx = filter_id + 1; fidx < filter_id + 4; fidx++) {
423 f = &adapter->tids.ftid_tab[fidx];
430 * Check to make sure that provided filter index is not
431 * already in use by someone else
433 f = &adapter->tids.ftid_tab[filter_id];
437 fidx = adapter->tids.ftid_base + filter_id;
439 ret = cxgbe_set_ftid(&adapter->tids, fid_bit,
440 fs->type ? FILTER_TYPE_IPV6 : FILTER_TYPE_IPV4);
445 * Check to make sure the filter requested is writable ...
447 ret = writable_filter(f);
449 /* Clear the bits we have set above */
450 cxgbe_clear_ftid(&adapter->tids, fid_bit,
451 fs->type ? FILTER_TYPE_IPV6 :
457 * Convert the filter specification into our internal format.
458 * We copy the PF/VF specification into the Outer VLAN field
459 * here so the rest of the code -- including the interface to
460 * the firmware -- doesn't have to constantly do these checks.
467 * Attempt to set the filter. If we don't succeed, we clear
468 * it and return the failure.
471 f->tid = fidx; /* Save the actual tid */
472 ret = set_filter_wr(dev, filter_id);
474 fid_bit = f->tid - adapter->tids.ftid_base;
475 cxgbe_clear_ftid(&adapter->tids, fid_bit,
476 fs->type ? FILTER_TYPE_IPV6 :
485 * Handle a LE-TCAM filter write/deletion reply.
487 void filter_rpl(struct adapter *adap, const struct cpl_set_tcb_rpl *rpl)
489 struct filter_entry *f = NULL;
490 unsigned int tid = GET_TID(rpl);
491 int idx, max_fidx = adap->tids.nftids;
493 /* Get the corresponding filter entry for this tid */
494 if (adap->tids.ftid_tab) {
495 /* Check this in normal filter region */
496 idx = tid - adap->tids.ftid_base;
500 f = &adap->tids.ftid_tab[idx];
505 /* We found the filter entry for this tid */
507 unsigned int ret = G_COOKIE(rpl->cookie);
508 struct filter_ctx *ctx;
511 * Pull off any filter operation context attached to the
517 if (ret == FW_FILTER_WR_FLT_ADDED) {
518 f->pending = 0; /* asynchronous setup completed */
524 } else if (ret == FW_FILTER_WR_FLT_DELETED) {
526 * Clear the filter when we get confirmation from the
527 * hardware that the filter has been deleted.
534 * Something went wrong. Issue a warning about the
535 * problem and clear everything out.
537 dev_warn(adap, "filter %u setup failed with error %u\n",
541 ctx->result = -EINVAL;
545 t4_complete(&ctx->completion);