net/cxgbe: support flow API for matching all packets on PF
[dpdk.git] / drivers / net / cxgbe / cxgbe_flow.c
index 9ee8353..138d075 100644 (file)
@@ -87,6 +87,15 @@ cxgbe_tweak_filter_spec(struct adapter *adap,
                                fs->mask.ethtype = 0;
                        }
                        break;
+               case RTE_ETHER_TYPE_QINQ:
+                       if (adap->params.tp.ethertype_shift < 0 &&
+                           adap->params.tp.vnic_shift >= 0) {
+                               fs->val.ovlan_vld = 1;
+                               fs->mask.ovlan_vld = 1;
+                               fs->val.ethtype = 0;
+                               fs->mask.ethtype = 0;
+                       }
+                       break;
                default:
                        break;
                }
@@ -145,6 +154,17 @@ cxgbe_fill_filter_region(struct adapter *adap,
        if (tp->vlan_shift >= 0 && fs->mask.ivlan_vld)
                ntuple_mask |= (u64)(F_FT_VLAN_VLD | fs->mask.ivlan) <<
                               tp->vlan_shift;
+       if (tp->vnic_shift >= 0) {
+               if (fs->mask.ovlan_vld)
+                       ntuple_mask |= (u64)(fs->val.ovlan_vld << 16 |
+                                            fs->mask.ovlan) << tp->vnic_shift;
+               else if (fs->mask.pfvf_vld)
+                       ntuple_mask |= (u64)((fs->mask.pfvf_vld << 16) |
+                                            (fs->mask.pf << 13)) <<
+                                            tp->vnic_shift;
+       }
+       if (tp->tos_shift >= 0)
+               ntuple_mask |= (u64)fs->mask.tos << tp->tos_shift;
 
        if (ntuple_mask != hash_filter_mask)
                return;
@@ -167,22 +187,6 @@ ch_rte_parsetype_eth(const void *dmask, const struct rte_flow_item *item,
        if (!spec)
                return 0;
 
-       /* Chelsio hardware supports matching on only one ethertype
-        * (i.e. either the outer or inner ethertype, but not both). If
-        * we already encountered VLAN item, then ensure that the outer
-        * ethertype is VLAN (0x8100) and don't overwrite the inner
-        * ethertype stored during VLAN item parsing. Note that if
-        * 'ivlan_vld' bit is set in Chelsio filter spec, then the
-        * hardware automatically only matches packets with outer
-        * ethertype having VLAN (0x8100).
-        */
-       if (fs->mask.ivlan_vld &&
-           be16_to_cpu(spec->type) != RTE_ETHER_TYPE_VLAN)
-               return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
-                                         item,
-                                         "Already encountered VLAN item,"
-                                         " but outer ethertype is not 0x8100");
-
        /* we don't support SRC_MAC filtering*/
        if (!rte_is_zero_ether_addr(&mask->src))
                return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
@@ -206,13 +210,9 @@ ch_rte_parsetype_eth(const void *dmask, const struct rte_flow_item *item,
                CXGBE_FILL_FS(idx, 0x1ff, macidx);
        }
 
-       /* Only set outer ethertype, if we didn't encounter VLAN item yet.
-        * Otherwise, the inner ethertype set by VLAN item will get
-        * overwritten.
-        */
-       if (!fs->mask.ivlan_vld)
-               CXGBE_FILL_FS(be16_to_cpu(spec->type),
-                             be16_to_cpu(mask->type), ethtype);
+       CXGBE_FILL_FS(be16_to_cpu(spec->type),
+                     be16_to_cpu(mask->type), ethtype);
+
        return 0;
 }
 
@@ -227,6 +227,9 @@ ch_rte_parsetype_port(const void *dmask, const struct rte_flow_item *item,
 
        mask = umask ? umask : (const struct rte_flow_item_phy_port *)dmask;
 
+       if (!val)
+               return 0; /* Wildcard, match all physical ports */
+
        if (val->index > 0x7)
                return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
                                          item,
@@ -249,38 +252,70 @@ ch_rte_parsetype_vlan(const void *dmask, const struct rte_flow_item *item,
        /* If user has not given any mask, then use chelsio supported mask. */
        mask = umask ? umask : (const struct rte_flow_item_vlan *)dmask;
 
-       CXGBE_FILL_FS(1, 1, ivlan_vld);
-       if (!spec)
-               return 0; /* Wildcard, match all VLAN */
-
-       /* Chelsio hardware supports matching on only one ethertype
-        * (i.e. either the outer or inner ethertype, but not both).
-        * If outer ethertype is already set and is not VLAN (0x8100),
-        * then don't proceed further. Otherwise, reset the outer
-        * ethertype, so that it can be replaced by inner ethertype.
-        * Note that the hardware will automatically match on outer
-        * ethertype 0x8100, if 'ivlan_vld' bit is set in Chelsio
-        * filter spec.
+       if (!fs->mask.ethtype)
+               return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
+                                         item,
+                                         "Can't parse VLAN item without knowing ethertype");
+
+       /* If ethertype is already set and is not VLAN (0x8100) or
+        * QINQ(0x88A8), then don't proceed further. Otherwise,
+        * reset the outer ethertype, so that it can be replaced by
+        * innermost ethertype. Note that hardware will automatically
+        * match against VLAN or QINQ packets, based on 'ivlan_vld' or
+        * 'ovlan_vld' bit set in Chelsio filter spec, respectively.
         */
        if (fs->mask.ethtype) {
-               if (fs->val.ethtype != RTE_ETHER_TYPE_VLAN)
+               if (fs->val.ethtype != RTE_ETHER_TYPE_VLAN &&
+                   fs->val.ethtype != RTE_ETHER_TYPE_QINQ)
                        return rte_flow_error_set(e, EINVAL,
                                                  RTE_FLOW_ERROR_TYPE_ITEM,
                                                  item,
-                                                 "Outer ethertype not 0x8100");
+                                                 "Ethertype must be 0x8100 or 0x88a8");
+       }
 
-               fs->val.ethtype = 0;
-               fs->mask.ethtype = 0;
+       if (fs->val.ethtype == RTE_ETHER_TYPE_QINQ) {
+               CXGBE_FILL_FS(1, 1, ovlan_vld);
+               if (spec) {
+                       CXGBE_FILL_FS(be16_to_cpu(spec->tci),
+                                     be16_to_cpu(mask->tci), ovlan);
+
+                       fs->mask.ethtype = 0;
+                       fs->val.ethtype = 0;
+               }
+       } else if (fs->val.ethtype == RTE_ETHER_TYPE_VLAN) {
+               CXGBE_FILL_FS(1, 1, ivlan_vld);
+               if (spec) {
+                       CXGBE_FILL_FS(be16_to_cpu(spec->tci),
+                                     be16_to_cpu(mask->tci), ivlan);
+
+                       fs->mask.ethtype = 0;
+                       fs->val.ethtype = 0;
+               }
        }
 
-       CXGBE_FILL_FS(be16_to_cpu(spec->tci), be16_to_cpu(mask->tci), ivlan);
-       if (spec->inner_type)
+       if (spec)
                CXGBE_FILL_FS(be16_to_cpu(spec->inner_type),
                              be16_to_cpu(mask->inner_type), ethtype);
 
        return 0;
 }
 
+static int
+ch_rte_parsetype_pf(const void *dmask __rte_unused,
+                   const struct rte_flow_item *item __rte_unused,
+                   struct ch_filter_specification *fs,
+                   struct rte_flow_error *e __rte_unused)
+{
+       struct rte_flow *flow = (struct rte_flow *)fs->private;
+       struct rte_eth_dev *dev = flow->dev;
+       struct adapter *adap = ethdev2adap(dev);
+
+       CXGBE_FILL_FS(1, 1, pfvf_vld);
+
+       CXGBE_FILL_FS(adap->pf, 0x7, pf);
+       return 0;
+}
+
 static int
 ch_rte_parsetype_udp(const void *dmask, const struct rte_flow_item *item,
                     struct ch_filter_specification *fs,
@@ -346,13 +381,12 @@ ch_rte_parsetype_ipv4(const void *dmask, const struct rte_flow_item *item,
 
        mask = umask ? umask : (const struct rte_flow_item_ipv4 *)dmask;
 
-       if (mask->hdr.time_to_live || mask->hdr.type_of_service)
+       if (mask->hdr.time_to_live)
                return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
-                                         item, "ttl/tos are not supported");
+                                         item, "ttl is not supported");
 
        if (fs->mask.ethtype &&
-           (fs->val.ethtype != RTE_ETHER_TYPE_VLAN &&
-            fs->val.ethtype != RTE_ETHER_TYPE_IPV4))
+           (fs->val.ethtype != RTE_ETHER_TYPE_IPV4))
                return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
                                          item,
                                          "Couldn't find IPv4 ethertype");
@@ -363,6 +397,7 @@ ch_rte_parsetype_ipv4(const void *dmask, const struct rte_flow_item *item,
        CXGBE_FILL_FS(val->hdr.next_proto_id, mask->hdr.next_proto_id, proto);
        CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
        CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
+       CXGBE_FILL_FS(val->hdr.type_of_service, mask->hdr.type_of_service, tos);
 
        return 0;
 }
@@ -375,18 +410,20 @@ ch_rte_parsetype_ipv6(const void *dmask, const struct rte_flow_item *item,
        const struct rte_flow_item_ipv6 *val = item->spec;
        const struct rte_flow_item_ipv6 *umask = item->mask;
        const struct rte_flow_item_ipv6 *mask;
+       u32 vtc_flow, vtc_flow_mask;
 
        mask = umask ? umask : (const struct rte_flow_item_ipv6 *)dmask;
 
-       if (mask->hdr.vtc_flow ||
+       vtc_flow_mask = be32_to_cpu(mask->hdr.vtc_flow);
+
+       if (vtc_flow_mask & RTE_IPV6_HDR_FL_MASK ||
            mask->hdr.payload_len || mask->hdr.hop_limits)
                return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
                                          item,
-                                         "tc/flow/hop are not supported");
+                                         "flow/hop are not supported");
 
        if (fs->mask.ethtype &&
-           (fs->val.ethtype != RTE_ETHER_TYPE_VLAN &&
-            fs->val.ethtype != RTE_ETHER_TYPE_IPV6))
+           (fs->val.ethtype != RTE_ETHER_TYPE_IPV6))
                return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
                                          item,
                                          "Couldn't find IPv6 ethertype");
@@ -395,6 +432,14 @@ ch_rte_parsetype_ipv6(const void *dmask, const struct rte_flow_item *item,
                return 0; /* ipv6 wild card */
 
        CXGBE_FILL_FS(val->hdr.proto, mask->hdr.proto, proto);
+
+       vtc_flow = be32_to_cpu(val->hdr.vtc_flow);
+       CXGBE_FILL_FS((vtc_flow & RTE_IPV6_HDR_TC_MASK) >>
+                     RTE_IPV6_HDR_TC_SHIFT,
+                     (vtc_flow_mask & RTE_IPV6_HDR_TC_MASK) >>
+                     RTE_IPV6_HDR_TC_SHIFT,
+                     tos);
+
        CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
        CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
 
@@ -865,12 +910,28 @@ static struct chrte_fparse parseitem[] = {
 
        [RTE_FLOW_ITEM_TYPE_IPV4] = {
                .fptr  = ch_rte_parsetype_ipv4,
-               .dmask = &rte_flow_item_ipv4_mask,
+               .dmask = &(const struct rte_flow_item_ipv4) {
+                       .hdr = {
+                               .src_addr = RTE_BE32(0xffffffff),
+                               .dst_addr = RTE_BE32(0xffffffff),
+                               .type_of_service = 0xff,
+                       },
+               },
        },
 
        [RTE_FLOW_ITEM_TYPE_IPV6] = {
                .fptr  = ch_rte_parsetype_ipv6,
-               .dmask = &rte_flow_item_ipv6_mask,
+               .dmask = &(const struct rte_flow_item_ipv6) {
+                       .hdr = {
+                               .src_addr =
+                                       "\xff\xff\xff\xff\xff\xff\xff\xff"
+                                       "\xff\xff\xff\xff\xff\xff\xff\xff",
+                               .dst_addr =
+                                       "\xff\xff\xff\xff\xff\xff\xff\xff"
+                                       "\xff\xff\xff\xff\xff\xff\xff\xff",
+                               .vtc_flow = RTE_BE32(0xff000000),
+                       },
+               },
        },
 
        [RTE_FLOW_ITEM_TYPE_UDP] = {
@@ -882,6 +943,11 @@ static struct chrte_fparse parseitem[] = {
                .fptr  = ch_rte_parsetype_tcp,
                .dmask = &rte_flow_item_tcp_mask,
        },
+
+       [RTE_FLOW_ITEM_TYPE_PF] = {
+               .fptr = ch_rte_parsetype_pf,
+               .dmask = NULL,
+       },
 };
 
 static int
@@ -907,15 +973,13 @@ cxgbe_rtef_parse_items(struct rte_flow *flow,
                        continue;
                default:
                        /* check if item is repeated */
-                       if (repeat[i->type])
+                       if (repeat[i->type] &&
+                           i->type != RTE_FLOW_ITEM_TYPE_VLAN)
                                return rte_flow_error_set(e, ENOTSUP,
                                                RTE_FLOW_ERROR_TYPE_ITEM, i,
-                                               "parse items cannot be repeated (except void)");
-                       repeat[i->type] = 1;
+                                               "parse items cannot be repeated(except void/vlan)");
 
-                       /* No spec found for this pattern item. Skip it */
-                       if (!i->spec)
-                               break;
+                       repeat[i->type] = 1;
 
                        /* validate the item */
                        ret = cxgbe_validate_item(i, e);
@@ -1014,6 +1078,7 @@ cxgbe_flow_create(struct rte_eth_dev *dev,
                  const struct rte_flow_action action[],
                  struct rte_flow_error *e)
 {
+       struct adapter *adap = ethdev2adap(dev);
        struct rte_flow *flow;
        int ret;
 
@@ -1034,8 +1099,10 @@ cxgbe_flow_create(struct rte_eth_dev *dev,
                return NULL;
        }
 
+       t4_os_lock(&adap->flow_lock);
        /* go, interact with cxgbe_filter */
        ret = __cxgbe_flow_create(dev, flow);
+       t4_os_unlock(&adap->flow_lock);
        if (ret) {
                rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
                                   NULL, "Unable to create flow rule");
@@ -1100,9 +1167,12 @@ static int
 cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
                   struct rte_flow_error *e)
 {
+       struct adapter *adap = ethdev2adap(dev);
        int ret;
 
+       t4_os_lock(&adap->flow_lock);
        ret = __cxgbe_flow_destroy(dev, flow);
+       t4_os_unlock(&adap->flow_lock);
        if (ret)
                return rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
                                          flow, "error destroying filter.");
@@ -1159,11 +1229,14 @@ cxgbe_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
                                          " enabled during filter creation");
 
        c = (struct rte_flow_query_count *)data;
+
+       t4_os_lock(&adap->flow_lock);
        ret = __cxgbe_flow_query(flow, &c->hits, &c->bytes);
-       if (ret)
-               return rte_flow_error_set(e, -ret, RTE_FLOW_ERROR_TYPE_ACTION,
-                                         f, "cxgbe pmd failed to"
-                                         " perform query");
+       if (ret) {
+               rte_flow_error_set(e, -ret, RTE_FLOW_ERROR_TYPE_ACTION,
+                                  f, "cxgbe pmd failed to perform query");
+               goto out;
+       }
 
        /* Query was successful */
        c->bytes_set = 1;
@@ -1171,7 +1244,9 @@ cxgbe_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
        if (c->reset)
                cxgbe_clear_filter_count(adap, flow->fidx, f->fs.cap, true);
 
-       return 0; /* success / partial_success */
+out:
+       t4_os_unlock(&adap->flow_lock);
+       return ret;
 }
 
 static int
@@ -1184,7 +1259,7 @@ cxgbe_flow_validate(struct rte_eth_dev *dev,
        struct adapter *adap = ethdev2adap(dev);
        struct rte_flow *flow;
        unsigned int fidx;
-       int ret;
+       int ret = 0;
 
        flow = t4_os_alloc(sizeof(struct rte_flow));
        if (!flow)
@@ -1208,20 +1283,23 @@ cxgbe_flow_validate(struct rte_eth_dev *dev,
                                "validation failed. Check f/w config file.");
        }
 
+       t4_os_lock(&adap->flow_lock);
        if (cxgbe_get_fidx(flow, &fidx)) {
-               t4_os_free(flow);
-               return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
-                                         NULL, "no memory in tcam.");
+               ret = rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
+                                        NULL, "no memory in tcam.");
+               goto out;
        }
 
        if (cxgbe_verify_fidx(flow, fidx, 0)) {
-               t4_os_free(flow);
-               return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
-                                         NULL, "validation failed");
+               ret = rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
+                                        NULL, "validation failed");
+               goto out;
        }
 
+out:
+       t4_os_unlock(&adap->flow_lock);
        t4_os_free(flow);
-       return 0;
+       return ret;
 }
 
 /*
@@ -1230,14 +1308,12 @@ cxgbe_flow_validate(struct rte_eth_dev *dev,
  *        == 1 filter not active / not found
  */
 static int
-cxgbe_check_n_destroy(struct filter_entry *f, struct rte_eth_dev *dev,
-                     struct rte_flow_error *e)
+cxgbe_check_n_destroy(struct filter_entry *f, struct rte_eth_dev *dev)
 {
        if (f && (f->valid || f->pending) &&
            f->dev == dev && /* Only if user has asked for this port */
             f->private) /* We (rte_flow) created this filter */
-               return cxgbe_flow_destroy(dev, (struct rte_flow *)f->private,
-                                         e);
+               return __cxgbe_flow_destroy(dev, (struct rte_flow *)f->private);
        return 1;
 }
 
@@ -1247,13 +1323,20 @@ static int cxgbe_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *e)
        unsigned int i;
        int ret = 0;
 
+       t4_os_lock(&adap->flow_lock);
        if (adap->tids.ftid_tab) {
                struct filter_entry *f = &adap->tids.ftid_tab[0];
 
                for (i = 0; i < adap->tids.nftids; i++, f++) {
-                       ret = cxgbe_check_n_destroy(f, dev, e);
-                       if (ret < 0)
+                       ret = cxgbe_check_n_destroy(f, dev);
+                       if (ret < 0) {
+                               rte_flow_error_set(e, ret,
+                                                  RTE_FLOW_ERROR_TYPE_HANDLE,
+                                                  f->private,
+                                                  "error destroying TCAM "
+                                                  "filter.");
                                goto out;
+                       }
                }
        }
 
@@ -1263,13 +1346,20 @@ static int cxgbe_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *e)
                for (i = adap->tids.hash_base; i <= adap->tids.ntids; i++) {
                        f = (struct filter_entry *)adap->tids.tid_tab[i];
 
-                       ret = cxgbe_check_n_destroy(f, dev, e);
-                       if (ret < 0)
+                       ret = cxgbe_check_n_destroy(f, dev);
+                       if (ret < 0) {
+                               rte_flow_error_set(e, ret,
+                                                  RTE_FLOW_ERROR_TYPE_HANDLE,
+                                                  f->private,
+                                                  "error destroying HASH "
+                                                  "filter.");
                                goto out;
+                       }
                }
        }
 
 out:
+       t4_os_unlock(&adap->flow_lock);
        return ret >= 0 ? 0 : ret;
 }