Merge tag 'for-4.18-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-2.6-microblaze.git] / drivers / net / ethernet / chelsio / cxgb4 / cxgb4_filter.c
1 /*
2  * This file is part of the Chelsio T4 Ethernet driver for Linux.
3  *
4  * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
5  *
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:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
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.
24  *
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
32  * SOFTWARE.
33  */
34 #include <net/ipv6.h>
35
36 #include "cxgb4.h"
37 #include "t4_regs.h"
38 #include "t4_tcb.h"
39 #include "t4_values.h"
40 #include "clip_tbl.h"
41 #include "l2t.h"
42 #include "smt.h"
43 #include "t4fw_api.h"
44 #include "cxgb4_filter.h"
45
46 static inline bool is_field_set(u32 val, u32 mask)
47 {
48         return val || mask;
49 }
50
51 static inline bool unsupported(u32 conf, u32 conf_mask, u32 val, u32 mask)
52 {
53         return !(conf & conf_mask) && is_field_set(val, mask);
54 }
55
56 static int set_tcb_field(struct adapter *adap, struct filter_entry *f,
57                          unsigned int ftid,  u16 word, u64 mask, u64 val,
58                          int no_reply)
59 {
60         struct cpl_set_tcb_field *req;
61         struct sk_buff *skb;
62
63         skb = alloc_skb(sizeof(struct cpl_set_tcb_field), GFP_ATOMIC);
64         if (!skb)
65                 return -ENOMEM;
66
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);
77         return 0;
78 }
79
80 /* Set one of the t_flags bits in the TCB.
81  */
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)
85 {
86         return set_tcb_field(adap, f, ftid,  TCB_T_FLAGS_W, 1ULL << bit_pos,
87                              (unsigned long long)val << bit_pos, no_reply);
88 }
89
90 static void mk_abort_req_ulp(struct cpl_abort_req *abort_req, unsigned int tid)
91 {
92         struct ulp_txpkt *txpkt = (struct ulp_txpkt *)abort_req;
93         struct ulptx_idata *sc = (struct ulptx_idata *)(txpkt + 1);
94
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;
103 }
104
105 static void mk_abort_rpl_ulp(struct cpl_abort_rpl *abort_rpl, unsigned int tid)
106 {
107         struct ulp_txpkt *txpkt = (struct ulp_txpkt *)abort_rpl;
108         struct ulptx_idata *sc = (struct ulptx_idata *)(txpkt + 1);
109
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;
118 }
119
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)
124 {
125         struct ulp_txpkt *txpkt = (struct ulp_txpkt *)req;
126         struct ulptx_idata *sc = (struct ulptx_idata *)(txpkt + 1);
127
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) |
134                                 QUEUENO_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));
140         sc->len = htonl(0);
141 }
142
143 static int configure_filter_smac(struct adapter *adap, struct filter_entry *f)
144 {
145         int err;
146
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);
149         if (err)
150                 goto smac_err;
151
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);
155         if (!err)
156                 return 0;
157
158 smac_err:
159         dev_err(adap->pdev_dev, "filter %u smac config failed with error %u\n",
160                 f->tid, err);
161         return err;
162 }
163
164 static void set_nat_params(struct adapter *adap, struct filter_entry *f,
165                            unsigned int tid, bool dip, bool sip, bool dp,
166                            bool sp)
167 {
168         if (dip) {
169                 if (f->fs.type) {
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);
175
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);
181
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);
187
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);
193                 } else {
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);
199                 }
200         }
201
202         if (sip) {
203                 if (f->fs.type) {
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);
209
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);
215
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);
221
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);
227
228                 } else {
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);
235                 }
236         }
237
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);
241 }
242
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)
246 {
247         struct adapter *adapter = netdev2adap(dev);
248         u32 fconf, iconf;
249
250         /* Check for unconfigured fields being used. */
251         fconf = adapter->params.tp.vlan_pri_map;
252         iconf = adapter->params.tp.ingress_config;
253
254         if (unsupported(fconf, FCOE_F, fs->val.fcoe, fs->mask.fcoe) ||
255             unsupported(fconf, PORT_F, fs->val.iport, fs->mask.iport) ||
256             unsupported(fconf, TOS_F, fs->val.tos, fs->mask.tos) ||
257             unsupported(fconf, ETHERTYPE_F, fs->val.ethtype,
258                         fs->mask.ethtype) ||
259             unsupported(fconf, MACMATCH_F, fs->val.macidx, fs->mask.macidx) ||
260             unsupported(fconf, MPSHITTYPE_F, fs->val.matchtype,
261                         fs->mask.matchtype) ||
262             unsupported(fconf, FRAGMENTATION_F, fs->val.frag, fs->mask.frag) ||
263             unsupported(fconf, PROTOCOL_F, fs->val.proto, fs->mask.proto) ||
264             unsupported(fconf, VNIC_ID_F, fs->val.pfvf_vld,
265                         fs->mask.pfvf_vld) ||
266             unsupported(fconf, VNIC_ID_F, fs->val.ovlan_vld,
267                         fs->mask.ovlan_vld) ||
268             unsupported(fconf, VNIC_ID_F, fs->val.encap_vld,
269                         fs->mask.encap_vld) ||
270             unsupported(fconf, VLAN_F, fs->val.ivlan_vld, fs->mask.ivlan_vld))
271                 return -EOPNOTSUPP;
272
273         /* T4 inconveniently uses the same FT_VNIC_ID_W bits for both the Outer
274          * VLAN Tag and PF/VF/VFvld fields based on VNIC_F being set
275          * in TP_INGRESS_CONFIG.  Hense the somewhat crazy checks
276          * below.  Additionally, since the T4 firmware interface also
277          * carries that overlap, we need to translate any PF/VF
278          * specification into that internal format below.
279          */
280         if ((is_field_set(fs->val.pfvf_vld, fs->mask.pfvf_vld) &&
281              is_field_set(fs->val.ovlan_vld, fs->mask.ovlan_vld)) ||
282             (is_field_set(fs->val.pfvf_vld, fs->mask.pfvf_vld) &&
283              is_field_set(fs->val.encap_vld, fs->mask.encap_vld)) ||
284             (is_field_set(fs->val.ovlan_vld, fs->mask.ovlan_vld) &&
285              is_field_set(fs->val.encap_vld, fs->mask.encap_vld)))
286                 return -EOPNOTSUPP;
287         if (unsupported(iconf, VNIC_F, fs->val.pfvf_vld, fs->mask.pfvf_vld) ||
288             (is_field_set(fs->val.ovlan_vld, fs->mask.ovlan_vld) &&
289              (iconf & VNIC_F)))
290                 return -EOPNOTSUPP;
291         if (fs->val.pf > 0x7 || fs->val.vf > 0x7f)
292                 return -ERANGE;
293         fs->mask.pf &= 0x7;
294         fs->mask.vf &= 0x7f;
295
296         /* If the user is requesting that the filter action loop
297          * matching packets back out one of our ports, make sure that
298          * the egress port is in range.
299          */
300         if (fs->action == FILTER_SWITCH &&
301             fs->eport >= adapter->params.nports)
302                 return -ERANGE;
303
304         /* Don't allow various trivially obvious bogus out-of-range values... */
305         if (fs->val.iport >= adapter->params.nports)
306                 return -ERANGE;
307
308         /* T4 doesn't support removing VLAN Tags for loop back filters. */
309         if (is_t4(adapter->params.chip) &&
310             fs->action == FILTER_SWITCH &&
311             (fs->newvlan == VLAN_REMOVE ||
312              fs->newvlan == VLAN_REWRITE))
313                 return -EOPNOTSUPP;
314
315         if (fs->val.encap_vld &&
316             CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
317                 return -EOPNOTSUPP;
318         return 0;
319 }
320
321 static int get_filter_steerq(struct net_device *dev,
322                              struct ch_filter_specification *fs)
323 {
324         struct adapter *adapter = netdev2adap(dev);
325         int iq;
326
327         /* If the user has requested steering matching Ingress Packets
328          * to a specific Queue Set, we need to make sure it's in range
329          * for the port and map that into the Absolute Queue ID of the
330          * Queue Set's Response Queue.
331          */
332         if (!fs->dirsteer) {
333                 if (fs->iq)
334                         return -EINVAL;
335                 iq = 0;
336         } else {
337                 struct port_info *pi = netdev_priv(dev);
338
339                 /* If the iq id is greater than the number of qsets,
340                  * then assume it is an absolute qid.
341                  */
342                 if (fs->iq < pi->nqsets)
343                         iq = adapter->sge.ethrxq[pi->first_qset +
344                                                  fs->iq].rspq.abs_id;
345                 else
346                         iq = fs->iq;
347         }
348
349         return iq;
350 }
351
352 static int get_filter_count(struct adapter *adapter, unsigned int fidx,
353                             u64 *pkts, u64 *bytes, bool hash)
354 {
355         unsigned int tcb_base, tcbaddr;
356         unsigned int word_offset;
357         struct filter_entry *f;
358         __be64 be64_byte_count;
359         int ret;
360
361         tcb_base = t4_read_reg(adapter, TP_CMM_TCB_BASE_A);
362         if (is_hashfilter(adapter) && hash) {
363                 if (fidx < adapter->tids.ntids) {
364                         f = adapter->tids.tid_tab[fidx];
365                         if (!f)
366                                 return -EINVAL;
367                 } else {
368                         return -E2BIG;
369                 }
370         } else {
371                 if ((fidx != (adapter->tids.nftids +
372                               adapter->tids.nsftids - 1)) &&
373                     fidx >= adapter->tids.nftids)
374                         return -E2BIG;
375
376                 f = &adapter->tids.ftid_tab[fidx];
377                 if (!f->valid)
378                         return -EINVAL;
379         }
380         tcbaddr = tcb_base + f->tid * TCB_SIZE;
381
382         spin_lock(&adapter->win0_lock);
383         if (is_t4(adapter->params.chip)) {
384                 __be64 be64_count;
385
386                 /* T4 doesn't maintain byte counts in hw */
387                 *bytes = 0;
388
389                 /* Get pkts */
390                 word_offset = 4;
391                 ret = t4_memory_rw(adapter, MEMWIN_NIC, MEM_EDC0,
392                                    tcbaddr + (word_offset * sizeof(__be32)),
393                                    sizeof(be64_count),
394                                    (__be32 *)&be64_count,
395                                    T4_MEMORY_READ);
396                 if (ret < 0)
397                         goto out;
398                 *pkts = be64_to_cpu(be64_count);
399         } else {
400                 __be32 be32_count;
401
402                 /* Get bytes */
403                 word_offset = 4;
404                 ret = t4_memory_rw(adapter, MEMWIN_NIC, MEM_EDC0,
405                                    tcbaddr + (word_offset * sizeof(__be32)),
406                                    sizeof(be64_byte_count),
407                                    &be64_byte_count,
408                                    T4_MEMORY_READ);
409                 if (ret < 0)
410                         goto out;
411                 *bytes = be64_to_cpu(be64_byte_count);
412
413                 /* Get pkts */
414                 word_offset = 6;
415                 ret = t4_memory_rw(adapter, MEMWIN_NIC, MEM_EDC0,
416                                    tcbaddr + (word_offset * sizeof(__be32)),
417                                    sizeof(be32_count),
418                                    &be32_count,
419                                    T4_MEMORY_READ);
420                 if (ret < 0)
421                         goto out;
422                 *pkts = (u64)be32_to_cpu(be32_count);
423         }
424
425 out:
426         spin_unlock(&adapter->win0_lock);
427         return ret;
428 }
429
430 int cxgb4_get_filter_counters(struct net_device *dev, unsigned int fidx,
431                               u64 *hitcnt, u64 *bytecnt, bool hash)
432 {
433         struct adapter *adapter = netdev2adap(dev);
434
435         return get_filter_count(adapter, fidx, hitcnt, bytecnt, hash);
436 }
437
438 int cxgb4_get_free_ftid(struct net_device *dev, int family)
439 {
440         struct adapter *adap = netdev2adap(dev);
441         struct tid_info *t = &adap->tids;
442         int ftid;
443
444         spin_lock_bh(&t->ftid_lock);
445         if (family == PF_INET) {
446                 ftid = find_first_zero_bit(t->ftid_bmap, t->nftids);
447                 if (ftid >= t->nftids)
448                         ftid = -1;
449         } else {
450                 if (is_t6(adap->params.chip)) {
451                         ftid = bitmap_find_free_region(t->ftid_bmap,
452                                                        t->nftids, 1);
453                         if (ftid < 0)
454                                 goto out_unlock;
455
456                         /* this is only a lookup, keep the found region
457                          * unallocated
458                          */
459                         bitmap_release_region(t->ftid_bmap, ftid, 1);
460                 } else {
461                         ftid = bitmap_find_free_region(t->ftid_bmap,
462                                                        t->nftids, 2);
463                         if (ftid < 0)
464                                 goto out_unlock;
465
466                         bitmap_release_region(t->ftid_bmap, ftid, 2);
467                 }
468         }
469 out_unlock:
470         spin_unlock_bh(&t->ftid_lock);
471         return ftid;
472 }
473
474 static int cxgb4_set_ftid(struct tid_info *t, int fidx, int family,
475                           unsigned int chip_ver)
476 {
477         spin_lock_bh(&t->ftid_lock);
478
479         if (test_bit(fidx, t->ftid_bmap)) {
480                 spin_unlock_bh(&t->ftid_lock);
481                 return -EBUSY;
482         }
483
484         if (family == PF_INET) {
485                 __set_bit(fidx, t->ftid_bmap);
486         } else {
487                 if (chip_ver < CHELSIO_T6)
488                         bitmap_allocate_region(t->ftid_bmap, fidx, 2);
489                 else
490                         bitmap_allocate_region(t->ftid_bmap, fidx, 1);
491         }
492
493         spin_unlock_bh(&t->ftid_lock);
494         return 0;
495 }
496
497 static void cxgb4_clear_ftid(struct tid_info *t, int fidx, int family,
498                              unsigned int chip_ver)
499 {
500         spin_lock_bh(&t->ftid_lock);
501         if (family == PF_INET) {
502                 __clear_bit(fidx, t->ftid_bmap);
503         } else {
504                 if (chip_ver < CHELSIO_T6)
505                         bitmap_release_region(t->ftid_bmap, fidx, 2);
506                 else
507                         bitmap_release_region(t->ftid_bmap, fidx, 1);
508         }
509         spin_unlock_bh(&t->ftid_lock);
510 }
511
512 /* Delete the filter at a specified index. */
513 static int del_filter_wr(struct adapter *adapter, int fidx)
514 {
515         struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
516         struct fw_filter_wr *fwr;
517         struct sk_buff *skb;
518         unsigned int len;
519
520         len = sizeof(*fwr);
521
522         skb = alloc_skb(len, GFP_KERNEL);
523         if (!skb)
524                 return -ENOMEM;
525
526         fwr = __skb_put(skb, len);
527         t4_mk_filtdelwr(f->tid, fwr, (adapter->flags & SHUTTING_DOWN) ? -1
528                         : adapter->sge.fw_evtq.abs_id);
529
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.
532          */
533         f->pending = 1;
534         t4_mgmt_tx(adapter, skb);
535         return 0;
536 }
537
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
542  * request.
543  */
544 int set_filter_wr(struct adapter *adapter, int fidx)
545 {
546         struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
547         struct fw_filter2_wr *fwr;
548         struct sk_buff *skb;
549
550         skb = alloc_skb(sizeof(*fwr), GFP_KERNEL);
551         if (!skb)
552                 return -ENOMEM;
553
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
556          * the filter.
557          */
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);
562                 if (!f->l2t) {
563                         kfree_skb(skb);
564                         return -ENOMEM;
565                 }
566         }
567
568         /* If the new filter requires loopback Source MAC rewriting then
569          * we need to allocate a SMT entry for the filter.
570          */
571         if (f->fs.newsmac) {
572                 f->smt = cxgb4_smt_alloc_switching(f->dev, f->fs.smac);
573                 if (!f->smt) {
574                         if (f->l2t) {
575                                 cxgb4_l2t_release(f->l2t);
576                                 f->l2t = NULL;
577                         }
578                         kfree_skb(skb);
579                         return -ENOMEM;
580                 }
581         }
582
583         fwr = __skb_put_zero(skb, sizeof(*fwr));
584
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 ...
592          */
593         if (adapter->params.filter2_wr_support)
594                 fwr->op_pkd = htonl(FW_WR_OP_V(FW_FILTER2_WR));
595         else
596                 fwr->op_pkd = htonl(FW_WR_OP_V(FW_FILTER_WR));
597         fwr->len16_pkd = htonl(FW_WR_LEN16_V(sizeof(*fwr) / 16));
598         fwr->tid_to_iq =
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));
628         fwr->smac_sel = 0;
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);
657
658         if (adapter->params.filter2_wr_support) {
659                 fwr->natmode_to_ulp_type =
660                         FW_FILTER2_WR_ULP_TYPE_V(f->fs.nat_mode ?
661                                                  ULP_MODE_TCPDDP :
662                                                  ULP_MODE_NONE) |
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);
668         }
669
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.
672          */
673         f->pending = 1;
674         set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
675         t4_ofld_send(adapter, skb);
676         return 0;
677 }
678
679 /* Return an error number if the indicated filter isn't writable ... */
680 int writable_filter(struct filter_entry *f)
681 {
682         if (f->locked)
683                 return -EPERM;
684         if (f->pending)
685                 return -EBUSY;
686
687         return 0;
688 }
689
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.
693  */
694 int delete_filter(struct adapter *adapter, unsigned int fidx)
695 {
696         struct filter_entry *f;
697         int ret;
698
699         if (fidx >= adapter->tids.nftids + adapter->tids.nsftids)
700                 return -EINVAL;
701
702         f = &adapter->tids.ftid_tab[fidx];
703         ret = writable_filter(f);
704         if (ret)
705                 return ret;
706         if (f->valid)
707                 return del_filter_wr(adapter, fidx);
708
709         return 0;
710 }
711
712 /* Clear a filter and release any of its resources that we own.  This also
713  * clears the filter's "pending" status.
714  */
715 void clear_filter(struct adapter *adap, struct filter_entry *f)
716 {
717         struct port_info *pi = netdev_priv(f->dev);
718
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
721          * rule.
722          */
723         if (f->l2t)
724                 cxgb4_l2t_release(f->l2t);
725
726         if (f->smt)
727                 cxgb4_smt_release(f->smt);
728
729         if (f->fs.val.encap_vld && f->fs.val.ovlan_vld)
730                 if (atomic_dec_and_test(&adap->mps_encap[f->fs.val.ovlan &
731                                                          0x1ff].refcnt))
732                         t4_free_encap_mac_filt(adap, pi->viid,
733                                                f->fs.val.ovlan & 0x1ff, 0);
734
735         if ((f->fs.hash || is_t6(adap->params.chip)) && f->fs.type)
736                 cxgb4_clip_release(f->dev, (const u32 *)&f->fs.val.lip, 1);
737
738         /* The zeroing of the filter rule below clears the filter valid,
739          * pending, locked flags, l2t pointer, etc. so it's all we need for
740          * this operation.
741          */
742         memset(f, 0, sizeof(*f));
743 }
744
745 void clear_all_filters(struct adapter *adapter)
746 {
747         unsigned int i;
748
749         if (adapter->tids.ftid_tab) {
750                 struct filter_entry *f = &adapter->tids.ftid_tab[0];
751                 unsigned int max_ftid = adapter->tids.nftids +
752                                         adapter->tids.nsftids;
753
754                 for (i = 0; i < max_ftid; i++, f++)
755                         if (f->valid || f->pending)
756                                 clear_filter(adapter, f);
757         }
758 }
759
760 /* Fill up default masks for set match fields. */
761 static void fill_default_mask(struct ch_filter_specification *fs)
762 {
763         unsigned int lip = 0, lip_mask = 0;
764         unsigned int fip = 0, fip_mask = 0;
765         unsigned int i;
766
767         if (fs->val.iport && !fs->mask.iport)
768                 fs->mask.iport |= ~0;
769         if (fs->val.fcoe && !fs->mask.fcoe)
770                 fs->mask.fcoe |= ~0;
771         if (fs->val.matchtype && !fs->mask.matchtype)
772                 fs->mask.matchtype |= ~0;
773         if (fs->val.macidx && !fs->mask.macidx)
774                 fs->mask.macidx |= ~0;
775         if (fs->val.ethtype && !fs->mask.ethtype)
776                 fs->mask.ethtype |= ~0;
777         if (fs->val.ivlan && !fs->mask.ivlan)
778                 fs->mask.ivlan |= ~0;
779         if (fs->val.ovlan && !fs->mask.ovlan)
780                 fs->mask.ovlan |= ~0;
781         if (fs->val.frag && !fs->mask.frag)
782                 fs->mask.frag |= ~0;
783         if (fs->val.tos && !fs->mask.tos)
784                 fs->mask.tos |= ~0;
785         if (fs->val.proto && !fs->mask.proto)
786                 fs->mask.proto |= ~0;
787
788         for (i = 0; i < ARRAY_SIZE(fs->val.lip); i++) {
789                 lip |= fs->val.lip[i];
790                 lip_mask |= fs->mask.lip[i];
791                 fip |= fs->val.fip[i];
792                 fip_mask |= fs->mask.fip[i];
793         }
794
795         if (lip && !lip_mask)
796                 memset(fs->mask.lip, ~0, sizeof(fs->mask.lip));
797
798         if (fip && !fip_mask)
799                 memset(fs->mask.fip, ~0, sizeof(fs->mask.lip));
800
801         if (fs->val.lport && !fs->mask.lport)
802                 fs->mask.lport = ~0;
803         if (fs->val.fport && !fs->mask.fport)
804                 fs->mask.fport = ~0;
805 }
806
807 static bool is_addr_all_mask(u8 *ipmask, int family)
808 {
809         if (family == AF_INET) {
810                 struct in_addr *addr;
811
812                 addr = (struct in_addr *)ipmask;
813                 if (addr->s_addr == 0xffffffff)
814                         return true;
815         } else if (family == AF_INET6) {
816                 struct in6_addr *addr6;
817
818                 addr6 = (struct in6_addr *)ipmask;
819                 if (addr6->s6_addr32[0] == 0xffffffff &&
820                     addr6->s6_addr32[1] == 0xffffffff &&
821                     addr6->s6_addr32[2] == 0xffffffff &&
822                     addr6->s6_addr32[3] == 0xffffffff)
823                         return true;
824         }
825         return false;
826 }
827
828 static bool is_inaddr_any(u8 *ip, int family)
829 {
830         int addr_type;
831
832         if (family == AF_INET) {
833                 struct in_addr *addr;
834
835                 addr = (struct in_addr *)ip;
836                 if (addr->s_addr == htonl(INADDR_ANY))
837                         return true;
838         } else if (family == AF_INET6) {
839                 struct in6_addr *addr6;
840
841                 addr6 = (struct in6_addr *)ip;
842                 addr_type = ipv6_addr_type((const struct in6_addr *)
843                                            &addr6);
844                 if (addr_type == IPV6_ADDR_ANY)
845                         return true;
846         }
847         return false;
848 }
849
850 bool is_filter_exact_match(struct adapter *adap,
851                            struct ch_filter_specification *fs)
852 {
853         struct tp_params *tp = &adap->params.tp;
854         u64 hash_filter_mask = tp->hash_filter_mask;
855         u64 ntuple_mask = 0;
856
857         if (!is_hashfilter(adap))
858                 return false;
859
860          /* Keep tunnel VNI match disabled for hash-filters for now */
861         if (fs->mask.encap_vld)
862                 return false;
863
864         if (fs->type) {
865                 if (is_inaddr_any(fs->val.fip, AF_INET6) ||
866                     !is_addr_all_mask(fs->mask.fip, AF_INET6))
867                         return false;
868
869                 if (is_inaddr_any(fs->val.lip, AF_INET6) ||
870                     !is_addr_all_mask(fs->mask.lip, AF_INET6))
871                         return false;
872         } else {
873                 if (is_inaddr_any(fs->val.fip, AF_INET) ||
874                     !is_addr_all_mask(fs->mask.fip, AF_INET))
875                         return false;
876
877                 if (is_inaddr_any(fs->val.lip, AF_INET) ||
878                     !is_addr_all_mask(fs->mask.lip, AF_INET))
879                         return false;
880         }
881
882         if (!fs->val.lport || fs->mask.lport != 0xffff)
883                 return false;
884
885         if (!fs->val.fport || fs->mask.fport != 0xffff)
886                 return false;
887
888         /* calculate tuple mask and compare with mask configured in hw */
889         if (tp->fcoe_shift >= 0)
890                 ntuple_mask |= (u64)fs->mask.fcoe << tp->fcoe_shift;
891
892         if (tp->port_shift >= 0)
893                 ntuple_mask |= (u64)fs->mask.iport << tp->port_shift;
894
895         if (tp->vnic_shift >= 0) {
896                 if ((adap->params.tp.ingress_config & VNIC_F))
897                         ntuple_mask |= (u64)fs->mask.pfvf_vld << tp->vnic_shift;
898                 else
899                         ntuple_mask |= (u64)fs->mask.ovlan_vld <<
900                                 tp->vnic_shift;
901         }
902
903         if (tp->vlan_shift >= 0)
904                 ntuple_mask |= (u64)fs->mask.ivlan << tp->vlan_shift;
905
906         if (tp->tos_shift >= 0)
907                 ntuple_mask |= (u64)fs->mask.tos << tp->tos_shift;
908
909         if (tp->protocol_shift >= 0)
910                 ntuple_mask |= (u64)fs->mask.proto << tp->protocol_shift;
911
912         if (tp->ethertype_shift >= 0)
913                 ntuple_mask |= (u64)fs->mask.ethtype << tp->ethertype_shift;
914
915         if (tp->macmatch_shift >= 0)
916                 ntuple_mask |= (u64)fs->mask.macidx << tp->macmatch_shift;
917
918         if (tp->matchtype_shift >= 0)
919                 ntuple_mask |= (u64)fs->mask.matchtype << tp->matchtype_shift;
920
921         if (tp->frag_shift >= 0)
922                 ntuple_mask |= (u64)fs->mask.frag << tp->frag_shift;
923
924         if (ntuple_mask != hash_filter_mask)
925                 return false;
926
927         return true;
928 }
929
930 static u64 hash_filter_ntuple(struct ch_filter_specification *fs,
931                               struct net_device *dev)
932 {
933         struct adapter *adap = netdev2adap(dev);
934         struct tp_params *tp = &adap->params.tp;
935         u64 ntuple = 0;
936
937         /* Initialize each of the fields which we care about which are present
938          * in the Compressed Filter Tuple.
939          */
940         if (tp->vlan_shift >= 0 && fs->mask.ivlan)
941                 ntuple |= (FT_VLAN_VLD_F | fs->val.ivlan) << tp->vlan_shift;
942
943         if (tp->port_shift >= 0 && fs->mask.iport)
944                 ntuple |= (u64)fs->val.iport << tp->port_shift;
945
946         if (tp->protocol_shift >= 0) {
947                 if (!fs->val.proto)
948                         ntuple |= (u64)IPPROTO_TCP << tp->protocol_shift;
949                 else
950                         ntuple |= (u64)fs->val.proto << tp->protocol_shift;
951         }
952
953         if (tp->tos_shift >= 0 && fs->mask.tos)
954                 ntuple |= (u64)(fs->val.tos) << tp->tos_shift;
955
956         if (tp->vnic_shift >= 0) {
957                 if ((adap->params.tp.ingress_config & USE_ENC_IDX_F) &&
958                     fs->mask.encap_vld)
959                         ntuple |= (u64)((fs->val.encap_vld << 16) |
960                                         (fs->val.ovlan)) << tp->vnic_shift;
961                 else if ((adap->params.tp.ingress_config & VNIC_F) &&
962                          fs->mask.pfvf_vld)
963                         ntuple |= (u64)((fs->val.pfvf_vld << 16) |
964                                         (fs->val.pf << 13) |
965                                         (fs->val.vf)) << tp->vnic_shift;
966                 else
967                         ntuple |= (u64)((fs->val.ovlan_vld << 16) |
968                                         (fs->val.ovlan)) << tp->vnic_shift;
969         }
970
971         if (tp->macmatch_shift >= 0 && fs->mask.macidx)
972                 ntuple |= (u64)(fs->val.macidx) << tp->macmatch_shift;
973
974         if (tp->ethertype_shift >= 0 && fs->mask.ethtype)
975                 ntuple |= (u64)(fs->val.ethtype) << tp->ethertype_shift;
976
977         if (tp->matchtype_shift >= 0 && fs->mask.matchtype)
978                 ntuple |= (u64)(fs->val.matchtype) << tp->matchtype_shift;
979
980         if (tp->frag_shift >= 0 && fs->mask.frag)
981                 ntuple |= (u64)(fs->val.frag) << tp->frag_shift;
982
983         if (tp->fcoe_shift >= 0 && fs->mask.fcoe)
984                 ntuple |= (u64)(fs->val.fcoe) << tp->fcoe_shift;
985         return ntuple;
986 }
987
988 static void mk_act_open_req6(struct filter_entry *f, struct sk_buff *skb,
989                              unsigned int qid_filterid, struct adapter *adap)
990 {
991         struct cpl_t6_act_open_req6 *t6req = NULL;
992         struct cpl_act_open_req6 *req = NULL;
993
994         t6req = (struct cpl_t6_act_open_req6 *)__skb_put(skb, sizeof(*t6req));
995         INIT_TP_WR(t6req, 0);
996         req = (struct cpl_act_open_req6 *)t6req;
997         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6, qid_filterid));
998         req->local_port = cpu_to_be16(f->fs.val.lport);
999         req->peer_port = cpu_to_be16(f->fs.val.fport);
1000         req->local_ip_hi = *(__be64 *)(&f->fs.val.lip);
1001         req->local_ip_lo = *(((__be64 *)&f->fs.val.lip) + 1);
1002         req->peer_ip_hi = *(__be64 *)(&f->fs.val.fip);
1003         req->peer_ip_lo = *(((__be64 *)&f->fs.val.fip) + 1);
1004         req->opt0 = cpu_to_be64(NAGLE_V(f->fs.newvlan == VLAN_REMOVE ||
1005                                         f->fs.newvlan == VLAN_REWRITE) |
1006                                 DELACK_V(f->fs.hitcnts) |
1007                                 L2T_IDX_V(f->l2t ? f->l2t->idx : 0) |
1008                                 SMAC_SEL_V((cxgb4_port_viid(f->dev) &
1009                                             0x7F) << 1) |
1010                                 TX_CHAN_V(f->fs.eport) |
1011                                 NO_CONG_V(f->fs.rpttid) |
1012                                 ULP_MODE_V(f->fs.nat_mode ?
1013                                            ULP_MODE_TCPDDP : ULP_MODE_NONE) |
1014                                 TCAM_BYPASS_F | NON_OFFLOAD_F);
1015         t6req->params = cpu_to_be64(FILTER_TUPLE_V(hash_filter_ntuple(&f->fs,
1016                                                                       f->dev)));
1017         t6req->opt2 = htonl(RSS_QUEUE_VALID_F |
1018                             RSS_QUEUE_V(f->fs.iq) |
1019                             TX_QUEUE_V(f->fs.nat_mode) |
1020                             T5_OPT_2_VALID_F |
1021                             RX_CHANNEL_F |
1022                             CONG_CNTRL_V((f->fs.action == FILTER_DROP) |
1023                                          (f->fs.dirsteer << 1)) |
1024                             PACE_V((f->fs.maskhash) |
1025                                    ((f->fs.dirsteerhash) << 1)) |
1026                             CCTRL_ECN_V(f->fs.action == FILTER_SWITCH));
1027 }
1028
1029 static void mk_act_open_req(struct filter_entry *f, struct sk_buff *skb,
1030                             unsigned int qid_filterid, struct adapter *adap)
1031 {
1032         struct cpl_t6_act_open_req *t6req = NULL;
1033         struct cpl_act_open_req *req = NULL;
1034
1035         t6req = (struct cpl_t6_act_open_req *)__skb_put(skb, sizeof(*t6req));
1036         INIT_TP_WR(t6req, 0);
1037         req = (struct cpl_act_open_req *)t6req;
1038         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, qid_filterid));
1039         req->local_port = cpu_to_be16(f->fs.val.lport);
1040         req->peer_port = cpu_to_be16(f->fs.val.fport);
1041         req->local_ip = f->fs.val.lip[0] | f->fs.val.lip[1] << 8 |
1042                 f->fs.val.lip[2] << 16 | f->fs.val.lip[3] << 24;
1043         req->peer_ip = f->fs.val.fip[0] | f->fs.val.fip[1] << 8 |
1044                 f->fs.val.fip[2] << 16 | f->fs.val.fip[3] << 24;
1045         req->opt0 = cpu_to_be64(NAGLE_V(f->fs.newvlan == VLAN_REMOVE ||
1046                                         f->fs.newvlan == VLAN_REWRITE) |
1047                                 DELACK_V(f->fs.hitcnts) |
1048                                 L2T_IDX_V(f->l2t ? f->l2t->idx : 0) |
1049                                 SMAC_SEL_V((cxgb4_port_viid(f->dev) &
1050                                             0x7F) << 1) |
1051                                 TX_CHAN_V(f->fs.eport) |
1052                                 NO_CONG_V(f->fs.rpttid) |
1053                                 ULP_MODE_V(f->fs.nat_mode ?
1054                                            ULP_MODE_TCPDDP : ULP_MODE_NONE) |
1055                                 TCAM_BYPASS_F | NON_OFFLOAD_F);
1056
1057         t6req->params = cpu_to_be64(FILTER_TUPLE_V(hash_filter_ntuple(&f->fs,
1058                                                                       f->dev)));
1059         t6req->opt2 = htonl(RSS_QUEUE_VALID_F |
1060                             RSS_QUEUE_V(f->fs.iq) |
1061                             TX_QUEUE_V(f->fs.nat_mode) |
1062                             T5_OPT_2_VALID_F |
1063                             RX_CHANNEL_F |
1064                             CONG_CNTRL_V((f->fs.action == FILTER_DROP) |
1065                                          (f->fs.dirsteer << 1)) |
1066                             PACE_V((f->fs.maskhash) |
1067                                    ((f->fs.dirsteerhash) << 1)) |
1068                             CCTRL_ECN_V(f->fs.action == FILTER_SWITCH));
1069 }
1070
1071 static int cxgb4_set_hash_filter(struct net_device *dev,
1072                                  struct ch_filter_specification *fs,
1073                                  struct filter_ctx *ctx)
1074 {
1075         struct adapter *adapter = netdev2adap(dev);
1076         struct port_info *pi = netdev_priv(dev);
1077         struct tid_info *t = &adapter->tids;
1078         struct filter_entry *f;
1079         struct sk_buff *skb;
1080         int iq, atid, size;
1081         int ret = 0;
1082         u32 iconf;
1083
1084         fill_default_mask(fs);
1085         ret = validate_filter(dev, fs);
1086         if (ret)
1087                 return ret;
1088
1089         iq = get_filter_steerq(dev, fs);
1090         if (iq < 0)
1091                 return iq;
1092
1093         f = kzalloc(sizeof(*f), GFP_KERNEL);
1094         if (!f)
1095                 return -ENOMEM;
1096
1097         f->fs = *fs;
1098         f->ctx = ctx;
1099         f->dev = dev;
1100         f->fs.iq = iq;
1101
1102         /* If the new filter requires loopback Destination MAC and/or VLAN
1103          * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
1104          * the filter.
1105          */
1106         if (f->fs.newdmac || f->fs.newvlan) {
1107                 /* allocate L2T entry for new filter */
1108                 f->l2t = t4_l2t_alloc_switching(adapter, f->fs.vlan,
1109                                                 f->fs.eport, f->fs.dmac);
1110                 if (!f->l2t) {
1111                         ret = -ENOMEM;
1112                         goto out_err;
1113                 }
1114         }
1115
1116         /* If the new filter requires loopback Source MAC rewriting then
1117          * we need to allocate a SMT entry for the filter.
1118          */
1119         if (f->fs.newsmac) {
1120                 f->smt = cxgb4_smt_alloc_switching(f->dev, f->fs.smac);
1121                 if (!f->smt) {
1122                         if (f->l2t) {
1123                                 cxgb4_l2t_release(f->l2t);
1124                                 f->l2t = NULL;
1125                         }
1126                         ret = -ENOMEM;
1127                         goto free_l2t;
1128                 }
1129         }
1130
1131         atid = cxgb4_alloc_atid(t, f);
1132         if (atid < 0) {
1133                 ret = atid;
1134                 goto free_smt;
1135         }
1136
1137         iconf = adapter->params.tp.ingress_config;
1138         if (iconf & VNIC_F) {
1139                 f->fs.val.ovlan = (fs->val.pf << 13) | fs->val.vf;
1140                 f->fs.mask.ovlan = (fs->mask.pf << 13) | fs->mask.vf;
1141                 f->fs.val.ovlan_vld = fs->val.pfvf_vld;
1142                 f->fs.mask.ovlan_vld = fs->mask.pfvf_vld;
1143         } else if (iconf & USE_ENC_IDX_F) {
1144                 if (f->fs.val.encap_vld) {
1145                         struct port_info *pi = netdev_priv(f->dev);
1146                         u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
1147
1148                         /* allocate MPS TCAM entry */
1149                         ret = t4_alloc_encap_mac_filt(adapter, pi->viid,
1150                                                       match_all_mac,
1151                                                       match_all_mac,
1152                                                       f->fs.val.vni,
1153                                                       f->fs.mask.vni,
1154                                                       0, 1, 1);
1155                         if (ret < 0)
1156                                 goto free_atid;
1157
1158                         atomic_inc(&adapter->mps_encap[ret].refcnt);
1159                         f->fs.val.ovlan = ret;
1160                         f->fs.mask.ovlan = 0xffff;
1161                         f->fs.val.ovlan_vld = 1;
1162                         f->fs.mask.ovlan_vld = 1;
1163                 }
1164         }
1165
1166         size = sizeof(struct cpl_t6_act_open_req);
1167         if (f->fs.type) {
1168                 ret = cxgb4_clip_get(f->dev, (const u32 *)&f->fs.val.lip, 1);
1169                 if (ret)
1170                         goto free_mps;
1171
1172                 skb = alloc_skb(size, GFP_KERNEL);
1173                 if (!skb) {
1174                         ret = -ENOMEM;
1175                         goto free_clip;
1176                 }
1177
1178                 mk_act_open_req6(f, skb,
1179                                  ((adapter->sge.fw_evtq.abs_id << 14) | atid),
1180                                  adapter);
1181         } else {
1182                 skb = alloc_skb(size, GFP_KERNEL);
1183                 if (!skb) {
1184                         ret = -ENOMEM;
1185                         goto free_mps;
1186                 }
1187
1188                 mk_act_open_req(f, skb,
1189                                 ((adapter->sge.fw_evtq.abs_id << 14) | atid),
1190                                 adapter);
1191         }
1192
1193         f->pending = 1;
1194         set_wr_txq(skb, CPL_PRIORITY_SETUP, f->fs.val.iport & 0x3);
1195         t4_ofld_send(adapter, skb);
1196         return 0;
1197
1198 free_clip:
1199         cxgb4_clip_release(f->dev, (const u32 *)&f->fs.val.lip, 1);
1200
1201 free_mps:
1202         if (f->fs.val.encap_vld && f->fs.val.ovlan_vld)
1203                 t4_free_encap_mac_filt(adapter, pi->viid, f->fs.val.ovlan, 1);
1204
1205 free_atid:
1206         cxgb4_free_atid(t, atid);
1207
1208 free_smt:
1209         if (f->smt) {
1210                 cxgb4_smt_release(f->smt);
1211                 f->smt = NULL;
1212         }
1213
1214 free_l2t:
1215         if (f->l2t) {
1216                 cxgb4_l2t_release(f->l2t);
1217                 f->l2t = NULL;
1218         }
1219
1220 out_err:
1221         kfree(f);
1222         return ret;
1223 }
1224
1225 /* Check a Chelsio Filter Request for validity, convert it into our internal
1226  * format and send it to the hardware.  Return 0 on success, an error number
1227  * otherwise.  We attach any provided filter operation context to the internal
1228  * filter specification in order to facilitate signaling completion of the
1229  * operation.
1230  */
1231 int __cxgb4_set_filter(struct net_device *dev, int filter_id,
1232                        struct ch_filter_specification *fs,
1233                        struct filter_ctx *ctx)
1234 {
1235         struct adapter *adapter = netdev2adap(dev);
1236         unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
1237         unsigned int max_fidx, fidx;
1238         struct filter_entry *f;
1239         u32 iconf;
1240         int iq, ret;
1241
1242         if (fs->hash) {
1243                 if (is_hashfilter(adapter))
1244                         return cxgb4_set_hash_filter(dev, fs, ctx);
1245                 netdev_err(dev, "%s: Exact-match filters only supported with Hash Filter configuration\n",
1246                            __func__);
1247                 return -EINVAL;
1248         }
1249
1250         max_fidx = adapter->tids.nftids;
1251         if (filter_id != (max_fidx + adapter->tids.nsftids - 1) &&
1252             filter_id >= max_fidx)
1253                 return -E2BIG;
1254
1255         fill_default_mask(fs);
1256
1257         ret = validate_filter(dev, fs);
1258         if (ret)
1259                 return ret;
1260
1261         iq = get_filter_steerq(dev, fs);
1262         if (iq < 0)
1263                 return iq;
1264
1265         /* IPv6 filters occupy four slots and must be aligned on
1266          * four-slot boundaries.  IPv4 filters only occupy a single
1267          * slot and have no alignment requirements but writing a new
1268          * IPv4 filter into the middle of an existing IPv6 filter
1269          * requires clearing the old IPv6 filter and hence we prevent
1270          * insertion.
1271          */
1272         if (fs->type == 0) { /* IPv4 */
1273                 /* For T6, If our IPv4 filter isn't being written to a
1274                  * multiple of two filter index and there's an IPv6
1275                  * filter at the multiple of 2 base slot, then we need
1276                  * to delete that IPv6 filter ...
1277                  * For adapters below T6, IPv6 filter occupies 4 entries.
1278                  * Hence we need to delete the filter in multiple of 4 slot.
1279                  */
1280                 if (chip_ver < CHELSIO_T6)
1281                         fidx = filter_id & ~0x3;
1282                 else
1283                         fidx = filter_id & ~0x1;
1284
1285                 if (fidx != filter_id &&
1286                     adapter->tids.ftid_tab[fidx].fs.type) {
1287                         f = &adapter->tids.ftid_tab[fidx];
1288                         if (f->valid) {
1289                                 dev_err(adapter->pdev_dev,
1290                                         "Invalid location. IPv6 requires 4 slots and is occupying slots %u to %u\n",
1291                                         fidx, fidx + 3);
1292                                 return -EINVAL;
1293                         }
1294                 }
1295         } else { /* IPv6 */
1296                 if (chip_ver < CHELSIO_T6) {
1297                         /* Ensure that the IPv6 filter is aligned on a
1298                          * multiple of 4 boundary.
1299                          */
1300                         if (filter_id & 0x3) {
1301                                 dev_err(adapter->pdev_dev,
1302                                         "Invalid location. IPv6 must be aligned on a 4-slot boundary\n");
1303                                 return -EINVAL;
1304                         }
1305
1306                         /* Check all except the base overlapping IPv4 filter
1307                          * slots.
1308                          */
1309                         for (fidx = filter_id + 1; fidx < filter_id + 4;
1310                              fidx++) {
1311                                 f = &adapter->tids.ftid_tab[fidx];
1312                                 if (f->valid) {
1313                                         dev_err(adapter->pdev_dev,
1314                                                 "Invalid location.  IPv6 requires 4 slots and an IPv4 filter exists at %u\n",
1315                                                 fidx);
1316                                         return -EBUSY;
1317                                 }
1318                         }
1319                 } else {
1320                         /* For T6, CLIP being enabled, IPv6 filter would occupy
1321                          * 2 entries.
1322                          */
1323                         if (filter_id & 0x1)
1324                                 return -EINVAL;
1325                         /* Check overlapping IPv4 filter slot */
1326                         fidx = filter_id + 1;
1327                         f = &adapter->tids.ftid_tab[fidx];
1328                         if (f->valid) {
1329                                 pr_err("%s: IPv6 filter requires 2 indices. IPv4 filter already present at %d. Please remove IPv4 filter first.\n",
1330                                        __func__, fidx);
1331                                 return -EBUSY;
1332                         }
1333                 }
1334         }
1335
1336         /* Check to make sure that provided filter index is not
1337          * already in use by someone else
1338          */
1339         f = &adapter->tids.ftid_tab[filter_id];
1340         if (f->valid)
1341                 return -EBUSY;
1342
1343         fidx = filter_id + adapter->tids.ftid_base;
1344         ret = cxgb4_set_ftid(&adapter->tids, filter_id,
1345                              fs->type ? PF_INET6 : PF_INET,
1346                              chip_ver);
1347         if (ret)
1348                 return ret;
1349
1350         /* Check t  make sure the filter requested is writable ... */
1351         ret = writable_filter(f);
1352         if (ret) {
1353                 /* Clear the bits we have set above */
1354                 cxgb4_clear_ftid(&adapter->tids, filter_id,
1355                                  fs->type ? PF_INET6 : PF_INET,
1356                                  chip_ver);
1357                 return ret;
1358         }
1359
1360         if (is_t6(adapter->params.chip) && fs->type &&
1361             ipv6_addr_type((const struct in6_addr *)fs->val.lip) !=
1362             IPV6_ADDR_ANY) {
1363                 ret = cxgb4_clip_get(dev, (const u32 *)&fs->val.lip, 1);
1364                 if (ret) {
1365                         cxgb4_clear_ftid(&adapter->tids, filter_id, PF_INET6,
1366                                          chip_ver);
1367                         return ret;
1368                 }
1369         }
1370
1371         /* Convert the filter specification into our internal format.
1372          * We copy the PF/VF specification into the Outer VLAN field
1373          * here so the rest of the code -- including the interface to
1374          * the firmware -- doesn't have to constantly do these checks.
1375          */
1376         f->fs = *fs;
1377         f->fs.iq = iq;
1378         f->dev = dev;
1379
1380         iconf = adapter->params.tp.ingress_config;
1381         if (iconf & VNIC_F) {
1382                 f->fs.val.ovlan = (fs->val.pf << 13) | fs->val.vf;
1383                 f->fs.mask.ovlan = (fs->mask.pf << 13) | fs->mask.vf;
1384                 f->fs.val.ovlan_vld = fs->val.pfvf_vld;
1385                 f->fs.mask.ovlan_vld = fs->mask.pfvf_vld;
1386         } else if (iconf & USE_ENC_IDX_F) {
1387                 if (f->fs.val.encap_vld) {
1388                         struct port_info *pi = netdev_priv(f->dev);
1389                         u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
1390
1391                         /* allocate MPS TCAM entry */
1392                         ret = t4_alloc_encap_mac_filt(adapter, pi->viid,
1393                                                       match_all_mac,
1394                                                       match_all_mac,
1395                                                       f->fs.val.vni,
1396                                                       f->fs.mask.vni,
1397                                                       0, 1, 1);
1398                         if (ret < 0)
1399                                 goto free_clip;
1400
1401                         atomic_inc(&adapter->mps_encap[ret].refcnt);
1402                         f->fs.val.ovlan = ret;
1403                         f->fs.mask.ovlan = 0x1ff;
1404                         f->fs.val.ovlan_vld = 1;
1405                         f->fs.mask.ovlan_vld = 1;
1406                 }
1407         }
1408
1409         /* Attempt to set the filter.  If we don't succeed, we clear
1410          * it and return the failure.
1411          */
1412         f->ctx = ctx;
1413         f->tid = fidx; /* Save the actual tid */
1414         ret = set_filter_wr(adapter, filter_id);
1415         if (ret) {
1416                 cxgb4_clear_ftid(&adapter->tids, filter_id,
1417                                  fs->type ? PF_INET6 : PF_INET,
1418                                  chip_ver);
1419                 clear_filter(adapter, f);
1420         }
1421
1422         return ret;
1423
1424 free_clip:
1425         if (is_t6(adapter->params.chip) && f->fs.type)
1426                 cxgb4_clip_release(f->dev, (const u32 *)&f->fs.val.lip, 1);
1427         cxgb4_clear_ftid(&adapter->tids, filter_id,
1428                          fs->type ? PF_INET6 : PF_INET, chip_ver);
1429         return ret;
1430 }
1431
1432 static int cxgb4_del_hash_filter(struct net_device *dev, int filter_id,
1433                                  struct filter_ctx *ctx)
1434 {
1435         struct adapter *adapter = netdev2adap(dev);
1436         struct tid_info *t = &adapter->tids;
1437         struct cpl_abort_req *abort_req;
1438         struct cpl_abort_rpl *abort_rpl;
1439         struct cpl_set_tcb_field *req;
1440         struct ulptx_idata *aligner;
1441         struct work_request_hdr *wr;
1442         struct filter_entry *f;
1443         struct sk_buff *skb;
1444         unsigned int wrlen;
1445         int ret;
1446
1447         netdev_dbg(dev, "%s: filter_id = %d ; nftids = %d\n",
1448                    __func__, filter_id, adapter->tids.nftids);
1449
1450         if (filter_id > adapter->tids.ntids)
1451                 return -E2BIG;
1452
1453         f = lookup_tid(t, filter_id);
1454         if (!f) {
1455                 netdev_err(dev, "%s: no filter entry for filter_id = %d",
1456                            __func__, filter_id);
1457                 return -EINVAL;
1458         }
1459
1460         ret = writable_filter(f);
1461         if (ret)
1462                 return ret;
1463
1464         if (!f->valid)
1465                 return -EINVAL;
1466
1467         f->ctx = ctx;
1468         f->pending = 1;
1469         wrlen = roundup(sizeof(*wr) + (sizeof(*req) + sizeof(*aligner))
1470                         + sizeof(*abort_req) + sizeof(*abort_rpl), 16);
1471         skb = alloc_skb(wrlen, GFP_KERNEL);
1472         if (!skb) {
1473                 netdev_err(dev, "%s: could not allocate skb ..\n", __func__);
1474                 return -ENOMEM;
1475         }
1476         set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
1477         req = (struct cpl_set_tcb_field *)__skb_put(skb, wrlen);
1478         INIT_ULPTX_WR(req, wrlen, 0, 0);
1479         wr = (struct work_request_hdr *)req;
1480         wr++;
1481         req = (struct cpl_set_tcb_field *)wr;
1482         mk_set_tcb_ulp(f, req, TCB_RSS_INFO_W, TCB_RSS_INFO_V(TCB_RSS_INFO_M),
1483                        TCB_RSS_INFO_V(adapter->sge.fw_evtq.abs_id), 0, 1);
1484         aligner = (struct ulptx_idata *)(req + 1);
1485         abort_req = (struct cpl_abort_req *)(aligner + 1);
1486         mk_abort_req_ulp(abort_req, f->tid);
1487         abort_rpl = (struct cpl_abort_rpl *)(abort_req + 1);
1488         mk_abort_rpl_ulp(abort_rpl, f->tid);
1489         t4_ofld_send(adapter, skb);
1490         return 0;
1491 }
1492
1493 /* Check a delete filter request for validity and send it to the hardware.
1494  * Return 0 on success, an error number otherwise.  We attach any provided
1495  * filter operation context to the internal filter specification in order to
1496  * facilitate signaling completion of the operation.
1497  */
1498 int __cxgb4_del_filter(struct net_device *dev, int filter_id,
1499                        struct ch_filter_specification *fs,
1500                        struct filter_ctx *ctx)
1501 {
1502         struct adapter *adapter = netdev2adap(dev);
1503         unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
1504         struct filter_entry *f;
1505         unsigned int max_fidx;
1506         int ret;
1507
1508         if (fs && fs->hash) {
1509                 if (is_hashfilter(adapter))
1510                         return cxgb4_del_hash_filter(dev, filter_id, ctx);
1511                 netdev_err(dev, "%s: Exact-match filters only supported with Hash Filter configuration\n",
1512                            __func__);
1513                 return -EINVAL;
1514         }
1515
1516         max_fidx = adapter->tids.nftids;
1517         if (filter_id != (max_fidx + adapter->tids.nsftids - 1) &&
1518             filter_id >= max_fidx)
1519                 return -E2BIG;
1520
1521         f = &adapter->tids.ftid_tab[filter_id];
1522         ret = writable_filter(f);
1523         if (ret)
1524                 return ret;
1525
1526         if (f->valid) {
1527                 f->ctx = ctx;
1528                 cxgb4_clear_ftid(&adapter->tids, filter_id,
1529                                  f->fs.type ? PF_INET6 : PF_INET,
1530                                  chip_ver);
1531                 return del_filter_wr(adapter, filter_id);
1532         }
1533
1534         /* If the caller has passed in a Completion Context then we need to
1535          * mark it as a successful completion so they don't stall waiting
1536          * for it.
1537          */
1538         if (ctx) {
1539                 ctx->result = 0;
1540                 complete(&ctx->completion);
1541         }
1542         return ret;
1543 }
1544
1545 int cxgb4_set_filter(struct net_device *dev, int filter_id,
1546                      struct ch_filter_specification *fs)
1547 {
1548         struct filter_ctx ctx;
1549         int ret;
1550
1551         init_completion(&ctx.completion);
1552
1553         ret = __cxgb4_set_filter(dev, filter_id, fs, &ctx);
1554         if (ret)
1555                 goto out;
1556
1557         /* Wait for reply */
1558         ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
1559         if (!ret)
1560                 return -ETIMEDOUT;
1561
1562         ret = ctx.result;
1563 out:
1564         return ret;
1565 }
1566
1567 int cxgb4_del_filter(struct net_device *dev, int filter_id,
1568                      struct ch_filter_specification *fs)
1569 {
1570         struct filter_ctx ctx;
1571         int ret;
1572
1573         /* If we are shutting down the adapter do not wait for completion */
1574         if (netdev2adap(dev)->flags & SHUTTING_DOWN)
1575                 return __cxgb4_del_filter(dev, filter_id, fs, NULL);
1576
1577         init_completion(&ctx.completion);
1578
1579         ret = __cxgb4_del_filter(dev, filter_id, fs, &ctx);
1580         if (ret)
1581                 goto out;
1582
1583         /* Wait for reply */
1584         ret = wait_for_completion_timeout(&ctx.completion, 10 * HZ);
1585         if (!ret)
1586                 return -ETIMEDOUT;
1587
1588         ret = ctx.result;
1589 out:
1590         return ret;
1591 }
1592
1593 static int configure_filter_tcb(struct adapter *adap, unsigned int tid,
1594                                 struct filter_entry *f)
1595 {
1596         if (f->fs.hitcnts)
1597                 set_tcb_field(adap, f, tid, TCB_TIMESTAMP_W,
1598                               TCB_TIMESTAMP_V(TCB_TIMESTAMP_M) |
1599                               TCB_RTT_TS_RECENT_AGE_V(TCB_RTT_TS_RECENT_AGE_M),
1600                               TCB_TIMESTAMP_V(0ULL) |
1601                               TCB_RTT_TS_RECENT_AGE_V(0ULL),
1602                               1);
1603
1604         if (f->fs.newdmac)
1605                 set_tcb_tflag(adap, f, tid, TF_CCTRL_ECE_S, 1,
1606                               1);
1607
1608         if (f->fs.newvlan == VLAN_INSERT ||
1609             f->fs.newvlan == VLAN_REWRITE)
1610                 set_tcb_tflag(adap, f, tid, TF_CCTRL_RFR_S, 1,
1611                               1);
1612         if (f->fs.newsmac)
1613                 configure_filter_smac(adap, f);
1614
1615         if (f->fs.nat_mode) {
1616                 switch (f->fs.nat_mode) {
1617                 case NAT_MODE_DIP:
1618                         set_nat_params(adap, f, tid, true, false, false, false);
1619                         break;
1620
1621                 case NAT_MODE_DIP_DP:
1622                         set_nat_params(adap, f, tid, true, false, true, false);
1623                         break;
1624
1625                 case NAT_MODE_DIP_DP_SIP:
1626                         set_nat_params(adap, f, tid, true, true, true, false);
1627                         break;
1628                 case NAT_MODE_DIP_DP_SP:
1629                         set_nat_params(adap, f, tid, true, false, true, true);
1630                         break;
1631
1632                 case NAT_MODE_SIP_SP:
1633                         set_nat_params(adap, f, tid, false, true, false, true);
1634                         break;
1635
1636                 case NAT_MODE_DIP_SIP_SP:
1637                         set_nat_params(adap, f, tid, true, true, false, true);
1638                         break;
1639
1640                 case NAT_MODE_ALL:
1641                         set_nat_params(adap, f, tid, true, true, true, true);
1642                         break;
1643
1644                 default:
1645                         pr_err("%s: Invalid NAT mode: %d\n",
1646                                __func__, f->fs.nat_mode);
1647                         return -EINVAL;
1648                 }
1649         }
1650         return 0;
1651 }
1652
1653 void hash_del_filter_rpl(struct adapter *adap,
1654                          const struct cpl_abort_rpl_rss *rpl)
1655 {
1656         unsigned int status = rpl->status;
1657         struct tid_info *t = &adap->tids;
1658         unsigned int tid = GET_TID(rpl);
1659         struct filter_ctx *ctx = NULL;
1660         struct filter_entry *f;
1661
1662         dev_dbg(adap->pdev_dev, "%s: status = %u; tid = %u\n",
1663                 __func__, status, tid);
1664
1665         f = lookup_tid(t, tid);
1666         if (!f) {
1667                 dev_err(adap->pdev_dev, "%s:could not find filter entry",
1668                         __func__);
1669                 return;
1670         }
1671         ctx = f->ctx;
1672         f->ctx = NULL;
1673         clear_filter(adap, f);
1674         cxgb4_remove_tid(t, 0, tid, 0);
1675         kfree(f);
1676         if (ctx) {
1677                 ctx->result = 0;
1678                 complete(&ctx->completion);
1679         }
1680 }
1681
1682 void hash_filter_rpl(struct adapter *adap, const struct cpl_act_open_rpl *rpl)
1683 {
1684         unsigned int ftid = TID_TID_G(AOPEN_ATID_G(ntohl(rpl->atid_status)));
1685         unsigned int status  = AOPEN_STATUS_G(ntohl(rpl->atid_status));
1686         struct tid_info *t = &adap->tids;
1687         unsigned int tid = GET_TID(rpl);
1688         struct filter_ctx *ctx = NULL;
1689         struct filter_entry *f;
1690
1691         dev_dbg(adap->pdev_dev, "%s: tid = %u; atid = %u; status = %u\n",
1692                 __func__, tid, ftid, status);
1693
1694         f = lookup_atid(t, ftid);
1695         if (!f) {
1696                 dev_err(adap->pdev_dev, "%s:could not find filter entry",
1697                         __func__);
1698                 return;
1699         }
1700         ctx = f->ctx;
1701         f->ctx = NULL;
1702
1703         switch (status) {
1704         case CPL_ERR_NONE:
1705                 f->tid = tid;
1706                 f->pending = 0;
1707                 f->valid = 1;
1708                 cxgb4_insert_tid(t, f, f->tid, 0);
1709                 cxgb4_free_atid(t, ftid);
1710                 if (ctx) {
1711                         ctx->tid = f->tid;
1712                         ctx->result = 0;
1713                 }
1714                 if (configure_filter_tcb(adap, tid, f)) {
1715                         clear_filter(adap, f);
1716                         cxgb4_remove_tid(t, 0, tid, 0);
1717                         kfree(f);
1718                         if (ctx) {
1719                                 ctx->result = -EINVAL;
1720                                 complete(&ctx->completion);
1721                         }
1722                         return;
1723                 }
1724                 break;
1725
1726         default:
1727                 dev_err(adap->pdev_dev, "%s: filter creation PROBLEM; status = %u\n",
1728                         __func__, status);
1729
1730                 if (ctx) {
1731                         if (status == CPL_ERR_TCAM_FULL)
1732                                 ctx->result = -EAGAIN;
1733                         else
1734                                 ctx->result = -EINVAL;
1735                 }
1736                 clear_filter(adap, f);
1737                 cxgb4_free_atid(t, ftid);
1738                 kfree(f);
1739         }
1740         if (ctx)
1741                 complete(&ctx->completion);
1742 }
1743
1744 /* Handle a filter write/deletion reply. */
1745 void filter_rpl(struct adapter *adap, const struct cpl_set_tcb_rpl *rpl)
1746 {
1747         unsigned int tid = GET_TID(rpl);
1748         struct filter_entry *f = NULL;
1749         unsigned int max_fidx;
1750         int idx;
1751
1752         max_fidx = adap->tids.nftids + adap->tids.nsftids;
1753         /* Get the corresponding filter entry for this tid */
1754         if (adap->tids.ftid_tab) {
1755                 /* Check this in normal filter region */
1756                 idx = tid - adap->tids.ftid_base;
1757                 if (idx >= max_fidx)
1758                         return;
1759                 f = &adap->tids.ftid_tab[idx];
1760                 if (f->tid != tid)
1761                         return;
1762         }
1763
1764         /* We found the filter entry for this tid */
1765         if (f) {
1766                 unsigned int ret = TCB_COOKIE_G(rpl->cookie);
1767                 struct filter_ctx *ctx;
1768
1769                 /* Pull off any filter operation context attached to the
1770                  * filter.
1771                  */
1772                 ctx = f->ctx;
1773                 f->ctx = NULL;
1774
1775                 if (ret == FW_FILTER_WR_FLT_DELETED) {
1776                         /* Clear the filter when we get confirmation from the
1777                          * hardware that the filter has been deleted.
1778                          */
1779                         clear_filter(adap, f);
1780                         if (ctx)
1781                                 ctx->result = 0;
1782                 } else if (ret == FW_FILTER_WR_FLT_ADDED) {
1783                         int err = 0;
1784
1785                         if (f->fs.newsmac)
1786                                 err = configure_filter_smac(adap, f);
1787
1788                         if (!err) {
1789                                 f->pending = 0;  /* async setup completed */
1790                                 f->valid = 1;
1791                                 if (ctx) {
1792                                         ctx->result = 0;
1793                                         ctx->tid = idx;
1794                                 }
1795                         } else {
1796                                 clear_filter(adap, f);
1797                                 if (ctx)
1798                                         ctx->result = err;
1799                         }
1800                 } else {
1801                         /* Something went wrong.  Issue a warning about the
1802                          * problem and clear everything out.
1803                          */
1804                         dev_err(adap->pdev_dev, "filter %u setup failed with error %u\n",
1805                                 idx, ret);
1806                         clear_filter(adap, f);
1807                         if (ctx)
1808                                 ctx->result = -EINVAL;
1809                 }
1810                 if (ctx)
1811                         complete(&ctx->completion);
1812         }
1813 }
1814
1815 int init_hash_filter(struct adapter *adap)
1816 {
1817         /* On T6, verify the necessary register configs and warn the user in
1818          * case of improper config
1819          */
1820         if (is_t6(adap->params.chip)) {
1821                 if (TCAM_ACTV_HIT_G(t4_read_reg(adap, LE_DB_RSP_CODE_0_A)) != 4)
1822                         goto err;
1823
1824                 if (HASH_ACTV_HIT_G(t4_read_reg(adap, LE_DB_RSP_CODE_1_A)) != 4)
1825                         goto err;
1826         } else {
1827                 dev_err(adap->pdev_dev, "Hash filter supported only on T6\n");
1828                 return -EINVAL;
1829         }
1830         adap->params.hash_filter = 1;
1831         return 0;
1832 err:
1833         dev_warn(adap->pdev_dev, "Invalid hash filter config!\n");
1834         return -EINVAL;
1835 }