Merge branch 'linus-4.14-rc4-acp-prereq' of git://people.freedesktop.org/~agd5f/linux...
[sfrench/cifs-2.6.git] / drivers / net / ethernet / netronome / nfp / flower / action.c
1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/bitfield.h>
35 #include <net/pkt_cls.h>
36 #include <net/switchdev.h>
37 #include <net/tc_act/tc_gact.h>
38 #include <net/tc_act/tc_mirred.h>
39 #include <net/tc_act/tc_vlan.h>
40
41 #include "cmsg.h"
42 #include "main.h"
43 #include "../nfp_net_repr.h"
44
45 static void nfp_fl_pop_vlan(struct nfp_fl_pop_vlan *pop_vlan)
46 {
47         size_t act_size = sizeof(struct nfp_fl_pop_vlan);
48         u16 tmp_pop_vlan_op;
49
50         tmp_pop_vlan_op =
51                 FIELD_PREP(NFP_FL_ACT_LEN_LW, act_size >> NFP_FL_LW_SIZ) |
52                 FIELD_PREP(NFP_FL_ACT_JMP_ID, NFP_FL_ACTION_OPCODE_POP_VLAN);
53
54         pop_vlan->a_op = cpu_to_be16(tmp_pop_vlan_op);
55         pop_vlan->reserved = 0;
56 }
57
58 static void
59 nfp_fl_push_vlan(struct nfp_fl_push_vlan *push_vlan,
60                  const struct tc_action *action)
61 {
62         size_t act_size = sizeof(struct nfp_fl_push_vlan);
63         struct tcf_vlan *vlan = to_vlan(action);
64         u16 tmp_push_vlan_tci;
65         u16 tmp_push_vlan_op;
66
67         tmp_push_vlan_op =
68                 FIELD_PREP(NFP_FL_ACT_LEN_LW, act_size >> NFP_FL_LW_SIZ) |
69                 FIELD_PREP(NFP_FL_ACT_JMP_ID, NFP_FL_ACTION_OPCODE_PUSH_VLAN);
70
71         push_vlan->a_op = cpu_to_be16(tmp_push_vlan_op);
72         /* Set action push vlan parameters. */
73         push_vlan->reserved = 0;
74         push_vlan->vlan_tpid = tcf_vlan_push_proto(action);
75
76         tmp_push_vlan_tci =
77                 FIELD_PREP(NFP_FL_PUSH_VLAN_PRIO, vlan->tcfv_push_prio) |
78                 FIELD_PREP(NFP_FL_PUSH_VLAN_VID, vlan->tcfv_push_vid) |
79                 NFP_FL_PUSH_VLAN_CFI;
80         push_vlan->vlan_tci = cpu_to_be16(tmp_push_vlan_tci);
81 }
82
83 static int
84 nfp_fl_output(struct nfp_fl_output *output, const struct tc_action *action,
85               struct nfp_fl_payload *nfp_flow, bool last,
86               struct net_device *in_dev)
87 {
88         size_t act_size = sizeof(struct nfp_fl_output);
89         struct net_device *out_dev;
90         u16 tmp_output_op;
91         int ifindex;
92
93         /* Set action opcode to output action. */
94         tmp_output_op =
95                 FIELD_PREP(NFP_FL_ACT_LEN_LW, act_size >> NFP_FL_LW_SIZ) |
96                 FIELD_PREP(NFP_FL_ACT_JMP_ID, NFP_FL_ACTION_OPCODE_OUTPUT);
97
98         output->a_op = cpu_to_be16(tmp_output_op);
99
100         /* Set action output parameters. */
101         output->flags = cpu_to_be16(last ? NFP_FL_OUT_FLAGS_LAST : 0);
102
103         ifindex = tcf_mirred_ifindex(action);
104         out_dev = __dev_get_by_index(dev_net(in_dev), ifindex);
105         if (!out_dev)
106                 return -EOPNOTSUPP;
107
108         /* Only offload egress ports are on the same device as the ingress
109          * port.
110          */
111         if (!switchdev_port_same_parent_id(in_dev, out_dev))
112                 return -EOPNOTSUPP;
113         if (!nfp_netdev_is_nfp_repr(out_dev))
114                 return -EOPNOTSUPP;
115
116         output->port = cpu_to_be32(nfp_repr_get_port_id(out_dev));
117         if (!output->port)
118                 return -EOPNOTSUPP;
119
120         nfp_flow->meta.shortcut = output->port;
121
122         return 0;
123 }
124
125 static int
126 nfp_flower_loop_action(const struct tc_action *a,
127                        struct nfp_fl_payload *nfp_fl, int *a_len,
128                        struct net_device *netdev)
129 {
130         struct nfp_fl_push_vlan *psh_v;
131         struct nfp_fl_pop_vlan *pop_v;
132         struct nfp_fl_output *output;
133         int err;
134
135         if (is_tcf_gact_shot(a)) {
136                 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_DROP);
137         } else if (is_tcf_mirred_egress_redirect(a)) {
138                 if (*a_len + sizeof(struct nfp_fl_output) > NFP_FL_MAX_A_SIZ)
139                         return -EOPNOTSUPP;
140
141                 output = (struct nfp_fl_output *)&nfp_fl->action_data[*a_len];
142                 err = nfp_fl_output(output, a, nfp_fl, true, netdev);
143                 if (err)
144                         return err;
145
146                 *a_len += sizeof(struct nfp_fl_output);
147         } else if (is_tcf_mirred_egress_mirror(a)) {
148                 if (*a_len + sizeof(struct nfp_fl_output) > NFP_FL_MAX_A_SIZ)
149                         return -EOPNOTSUPP;
150
151                 output = (struct nfp_fl_output *)&nfp_fl->action_data[*a_len];
152                 err = nfp_fl_output(output, a, nfp_fl, false, netdev);
153                 if (err)
154                         return err;
155
156                 *a_len += sizeof(struct nfp_fl_output);
157         } else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
158                 if (*a_len + sizeof(struct nfp_fl_pop_vlan) > NFP_FL_MAX_A_SIZ)
159                         return -EOPNOTSUPP;
160
161                 pop_v = (struct nfp_fl_pop_vlan *)&nfp_fl->action_data[*a_len];
162                 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_POPV);
163
164                 nfp_fl_pop_vlan(pop_v);
165                 *a_len += sizeof(struct nfp_fl_pop_vlan);
166         } else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
167                 if (*a_len + sizeof(struct nfp_fl_push_vlan) > NFP_FL_MAX_A_SIZ)
168                         return -EOPNOTSUPP;
169
170                 psh_v = (struct nfp_fl_push_vlan *)&nfp_fl->action_data[*a_len];
171                 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
172
173                 nfp_fl_push_vlan(psh_v, a);
174                 *a_len += sizeof(struct nfp_fl_push_vlan);
175         } else {
176                 /* Currently we do not handle any other actions. */
177                 return -EOPNOTSUPP;
178         }
179
180         return 0;
181 }
182
183 int nfp_flower_compile_action(struct tc_cls_flower_offload *flow,
184                               struct net_device *netdev,
185                               struct nfp_fl_payload *nfp_flow)
186 {
187         int act_len, act_cnt, err;
188         const struct tc_action *a;
189         LIST_HEAD(actions);
190
191         memset(nfp_flow->action_data, 0, NFP_FL_MAX_A_SIZ);
192         nfp_flow->meta.act_len = 0;
193         act_len = 0;
194         act_cnt = 0;
195
196         tcf_exts_to_list(flow->exts, &actions);
197         list_for_each_entry(a, &actions, list) {
198                 err = nfp_flower_loop_action(a, nfp_flow, &act_len, netdev);
199                 if (err)
200                         return err;
201                 act_cnt++;
202         }
203
204         /* We optimise when the action list is small, this can unfortunately
205          * not happen once we have more than one action in the action list.
206          */
207         if (act_cnt > 1)
208                 nfp_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
209
210         nfp_flow->meta.act_len = act_len;
211
212         return 0;
213 }