xref: /linux/drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_matchall.c (revision 37744feebc086908fd89760650f458ab19071750)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2019 Chelsio Communications.  All rights reserved. */
3 
4 #include "cxgb4.h"
5 #include "cxgb4_tc_matchall.h"
6 #include "sched.h"
7 #include "cxgb4_uld.h"
8 #include "cxgb4_filter.h"
9 #include "cxgb4_tc_flower.h"
10 
11 static int cxgb4_matchall_egress_validate(struct net_device *dev,
12 					  struct tc_cls_matchall_offload *cls)
13 {
14 	struct netlink_ext_ack *extack = cls->common.extack;
15 	struct flow_action *actions = &cls->rule->action;
16 	struct port_info *pi = netdev2pinfo(dev);
17 	struct flow_action_entry *entry;
18 	struct ch_sched_queue qe;
19 	struct sched_class *e;
20 	u64 max_link_rate;
21 	u32 i, speed;
22 	int ret;
23 
24 	if (!flow_action_has_entries(actions)) {
25 		NL_SET_ERR_MSG_MOD(extack,
26 				   "Egress MATCHALL offload needs at least 1 policing action");
27 		return -EINVAL;
28 	} else if (!flow_offload_has_one_action(actions)) {
29 		NL_SET_ERR_MSG_MOD(extack,
30 				   "Egress MATCHALL offload only supports 1 policing action");
31 		return -EINVAL;
32 	} else if (pi->tc_block_shared) {
33 		NL_SET_ERR_MSG_MOD(extack,
34 				   "Egress MATCHALL offload not supported with shared blocks");
35 		return -EINVAL;
36 	}
37 
38 	ret = t4_get_link_params(pi, NULL, &speed, NULL);
39 	if (ret) {
40 		NL_SET_ERR_MSG_MOD(extack,
41 				   "Failed to get max speed supported by the link");
42 		return -EINVAL;
43 	}
44 
45 	/* Convert from Mbps to bps */
46 	max_link_rate = (u64)speed * 1000 * 1000;
47 
48 	flow_action_for_each(i, entry, actions) {
49 		switch (entry->id) {
50 		case FLOW_ACTION_POLICE:
51 			/* Convert bytes per second to bits per second */
52 			if (entry->police.rate_bytes_ps * 8 > max_link_rate) {
53 				NL_SET_ERR_MSG_MOD(extack,
54 						   "Specified policing max rate is larger than underlying link speed");
55 				return -ERANGE;
56 			}
57 			break;
58 		default:
59 			NL_SET_ERR_MSG_MOD(extack,
60 					   "Only policing action supported with Egress MATCHALL offload");
61 			return -EOPNOTSUPP;
62 		}
63 	}
64 
65 	for (i = 0; i < pi->nqsets; i++) {
66 		memset(&qe, 0, sizeof(qe));
67 		qe.queue = i;
68 
69 		e = cxgb4_sched_queue_lookup(dev, &qe);
70 		if (e && e->info.u.params.level != SCHED_CLASS_LEVEL_CH_RL) {
71 			NL_SET_ERR_MSG_MOD(extack,
72 					   "Some queues are already bound to different class");
73 			return -EBUSY;
74 		}
75 	}
76 
77 	return 0;
78 }
79 
80 static int cxgb4_matchall_tc_bind_queues(struct net_device *dev, u32 tc)
81 {
82 	struct port_info *pi = netdev2pinfo(dev);
83 	struct ch_sched_queue qe;
84 	int ret;
85 	u32 i;
86 
87 	for (i = 0; i < pi->nqsets; i++) {
88 		qe.queue = i;
89 		qe.class = tc;
90 		ret = cxgb4_sched_class_bind(dev, &qe, SCHED_QUEUE);
91 		if (ret)
92 			goto out_free;
93 	}
94 
95 	return 0;
96 
97 out_free:
98 	while (i--) {
99 		qe.queue = i;
100 		qe.class = SCHED_CLS_NONE;
101 		cxgb4_sched_class_unbind(dev, &qe, SCHED_QUEUE);
102 	}
103 
104 	return ret;
105 }
106 
107 static void cxgb4_matchall_tc_unbind_queues(struct net_device *dev)
108 {
109 	struct port_info *pi = netdev2pinfo(dev);
110 	struct ch_sched_queue qe;
111 	u32 i;
112 
113 	for (i = 0; i < pi->nqsets; i++) {
114 		qe.queue = i;
115 		qe.class = SCHED_CLS_NONE;
116 		cxgb4_sched_class_unbind(dev, &qe, SCHED_QUEUE);
117 	}
118 }
119 
120 static int cxgb4_matchall_alloc_tc(struct net_device *dev,
121 				   struct tc_cls_matchall_offload *cls)
122 {
123 	struct ch_sched_params p = {
124 		.type = SCHED_CLASS_TYPE_PACKET,
125 		.u.params.level = SCHED_CLASS_LEVEL_CH_RL,
126 		.u.params.mode = SCHED_CLASS_MODE_CLASS,
127 		.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS,
128 		.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS,
129 		.u.params.class = SCHED_CLS_NONE,
130 		.u.params.minrate = 0,
131 		.u.params.weight = 0,
132 		.u.params.pktsize = dev->mtu,
133 	};
134 	struct netlink_ext_ack *extack = cls->common.extack;
135 	struct cxgb4_tc_port_matchall *tc_port_matchall;
136 	struct port_info *pi = netdev2pinfo(dev);
137 	struct adapter *adap = netdev2adap(dev);
138 	struct flow_action_entry *entry;
139 	struct sched_class *e;
140 	int ret;
141 	u32 i;
142 
143 	tc_port_matchall = &adap->tc_matchall->port_matchall[pi->port_id];
144 
145 	flow_action_for_each(i, entry, &cls->rule->action)
146 		if (entry->id == FLOW_ACTION_POLICE)
147 			break;
148 
149 	/* Convert from bytes per second to Kbps */
150 	p.u.params.maxrate = div_u64(entry->police.rate_bytes_ps * 8, 1000);
151 	p.u.params.channel = pi->tx_chan;
152 	e = cxgb4_sched_class_alloc(dev, &p);
153 	if (!e) {
154 		NL_SET_ERR_MSG_MOD(extack,
155 				   "No free traffic class available for policing action");
156 		return -ENOMEM;
157 	}
158 
159 	ret = cxgb4_matchall_tc_bind_queues(dev, e->idx);
160 	if (ret) {
161 		NL_SET_ERR_MSG_MOD(extack,
162 				   "Could not bind queues to traffic class");
163 		goto out_free;
164 	}
165 
166 	tc_port_matchall->egress.hwtc = e->idx;
167 	tc_port_matchall->egress.cookie = cls->cookie;
168 	tc_port_matchall->egress.state = CXGB4_MATCHALL_STATE_ENABLED;
169 	return 0;
170 
171 out_free:
172 	cxgb4_sched_class_free(dev, e->idx);
173 	return ret;
174 }
175 
176 static void cxgb4_matchall_free_tc(struct net_device *dev)
177 {
178 	struct cxgb4_tc_port_matchall *tc_port_matchall;
179 	struct port_info *pi = netdev2pinfo(dev);
180 	struct adapter *adap = netdev2adap(dev);
181 
182 	tc_port_matchall = &adap->tc_matchall->port_matchall[pi->port_id];
183 	cxgb4_matchall_tc_unbind_queues(dev);
184 	cxgb4_sched_class_free(dev, tc_port_matchall->egress.hwtc);
185 
186 	tc_port_matchall->egress.hwtc = SCHED_CLS_NONE;
187 	tc_port_matchall->egress.cookie = 0;
188 	tc_port_matchall->egress.state = CXGB4_MATCHALL_STATE_DISABLED;
189 }
190 
191 static int cxgb4_matchall_alloc_filter(struct net_device *dev,
192 				       struct tc_cls_matchall_offload *cls)
193 {
194 	struct netlink_ext_ack *extack = cls->common.extack;
195 	struct cxgb4_tc_port_matchall *tc_port_matchall;
196 	struct port_info *pi = netdev2pinfo(dev);
197 	struct adapter *adap = netdev2adap(dev);
198 	struct ch_filter_specification *fs;
199 	int ret, fidx;
200 
201 	/* Get a free filter entry TID, where we can insert this new
202 	 * rule. Only insert rule if its prio doesn't conflict with
203 	 * existing rules.
204 	 *
205 	 * 1 slot is enough to create a wildcard matchall VIID rule.
206 	 */
207 	fidx = cxgb4_get_free_ftid(dev, PF_INET, false, cls->common.prio);
208 	if (fidx < 0) {
209 		NL_SET_ERR_MSG_MOD(extack,
210 				   "No free LETCAM index available");
211 		return -ENOMEM;
212 	}
213 
214 	tc_port_matchall = &adap->tc_matchall->port_matchall[pi->port_id];
215 	fs = &tc_port_matchall->ingress.fs;
216 	memset(fs, 0, sizeof(*fs));
217 
218 	if (fidx < adap->tids.nhpftids)
219 		fs->prio = 1;
220 	fs->tc_prio = cls->common.prio;
221 	fs->tc_cookie = cls->cookie;
222 	fs->hitcnts = 1;
223 
224 	fs->val.pfvf_vld = 1;
225 	fs->val.pf = adap->pf;
226 	fs->val.vf = pi->vin;
227 
228 	cxgb4_process_flow_actions(dev, &cls->rule->action, fs);
229 
230 	ret = cxgb4_set_filter(dev, fidx, fs);
231 	if (ret)
232 		return ret;
233 
234 	tc_port_matchall->ingress.tid = fidx;
235 	tc_port_matchall->ingress.state = CXGB4_MATCHALL_STATE_ENABLED;
236 	return 0;
237 }
238 
239 static int cxgb4_matchall_free_filter(struct net_device *dev)
240 {
241 	struct cxgb4_tc_port_matchall *tc_port_matchall;
242 	struct port_info *pi = netdev2pinfo(dev);
243 	struct adapter *adap = netdev2adap(dev);
244 	int ret;
245 
246 	tc_port_matchall = &adap->tc_matchall->port_matchall[pi->port_id];
247 
248 	ret = cxgb4_del_filter(dev, tc_port_matchall->ingress.tid,
249 			       &tc_port_matchall->ingress.fs);
250 	if (ret)
251 		return ret;
252 
253 	tc_port_matchall->ingress.packets = 0;
254 	tc_port_matchall->ingress.bytes = 0;
255 	tc_port_matchall->ingress.last_used = 0;
256 	tc_port_matchall->ingress.tid = 0;
257 	tc_port_matchall->ingress.state = CXGB4_MATCHALL_STATE_DISABLED;
258 	return 0;
259 }
260 
261 int cxgb4_tc_matchall_replace(struct net_device *dev,
262 			      struct tc_cls_matchall_offload *cls_matchall,
263 			      bool ingress)
264 {
265 	struct netlink_ext_ack *extack = cls_matchall->common.extack;
266 	struct cxgb4_tc_port_matchall *tc_port_matchall;
267 	struct port_info *pi = netdev2pinfo(dev);
268 	struct adapter *adap = netdev2adap(dev);
269 	int ret;
270 
271 	tc_port_matchall = &adap->tc_matchall->port_matchall[pi->port_id];
272 	if (ingress) {
273 		if (tc_port_matchall->ingress.state ==
274 		    CXGB4_MATCHALL_STATE_ENABLED) {
275 			NL_SET_ERR_MSG_MOD(extack,
276 					   "Only 1 Ingress MATCHALL can be offloaded");
277 			return -ENOMEM;
278 		}
279 
280 		ret = cxgb4_validate_flow_actions(dev,
281 						  &cls_matchall->rule->action,
282 						  extack);
283 		if (ret)
284 			return ret;
285 
286 		return cxgb4_matchall_alloc_filter(dev, cls_matchall);
287 	}
288 
289 	if (tc_port_matchall->egress.state == CXGB4_MATCHALL_STATE_ENABLED) {
290 		NL_SET_ERR_MSG_MOD(extack,
291 				   "Only 1 Egress MATCHALL can be offloaded");
292 		return -ENOMEM;
293 	}
294 
295 	ret = cxgb4_matchall_egress_validate(dev, cls_matchall);
296 	if (ret)
297 		return ret;
298 
299 	return cxgb4_matchall_alloc_tc(dev, cls_matchall);
300 }
301 
302 int cxgb4_tc_matchall_destroy(struct net_device *dev,
303 			      struct tc_cls_matchall_offload *cls_matchall,
304 			      bool ingress)
305 {
306 	struct cxgb4_tc_port_matchall *tc_port_matchall;
307 	struct port_info *pi = netdev2pinfo(dev);
308 	struct adapter *adap = netdev2adap(dev);
309 
310 	tc_port_matchall = &adap->tc_matchall->port_matchall[pi->port_id];
311 	if (ingress) {
312 		if (cls_matchall->cookie !=
313 		    tc_port_matchall->ingress.fs.tc_cookie)
314 			return -ENOENT;
315 
316 		return cxgb4_matchall_free_filter(dev);
317 	}
318 
319 	if (cls_matchall->cookie != tc_port_matchall->egress.cookie)
320 		return -ENOENT;
321 
322 	cxgb4_matchall_free_tc(dev);
323 	return 0;
324 }
325 
326 int cxgb4_tc_matchall_stats(struct net_device *dev,
327 			    struct tc_cls_matchall_offload *cls_matchall)
328 {
329 	struct cxgb4_tc_port_matchall *tc_port_matchall;
330 	struct port_info *pi = netdev2pinfo(dev);
331 	struct adapter *adap = netdev2adap(dev);
332 	u64 packets, bytes;
333 	int ret;
334 
335 	tc_port_matchall = &adap->tc_matchall->port_matchall[pi->port_id];
336 	if (tc_port_matchall->ingress.state == CXGB4_MATCHALL_STATE_DISABLED)
337 		return -ENOENT;
338 
339 	ret = cxgb4_get_filter_counters(dev, tc_port_matchall->ingress.tid,
340 					&packets, &bytes,
341 					tc_port_matchall->ingress.fs.hash);
342 	if (ret)
343 		return ret;
344 
345 	if (tc_port_matchall->ingress.packets != packets) {
346 		flow_stats_update(&cls_matchall->stats,
347 				  bytes - tc_port_matchall->ingress.bytes,
348 				  packets - tc_port_matchall->ingress.packets,
349 				  tc_port_matchall->ingress.last_used,
350 				  FLOW_ACTION_HW_STATS_IMMEDIATE);
351 
352 		tc_port_matchall->ingress.packets = packets;
353 		tc_port_matchall->ingress.bytes = bytes;
354 		tc_port_matchall->ingress.last_used = jiffies;
355 	}
356 
357 	return 0;
358 }
359 
360 static void cxgb4_matchall_disable_offload(struct net_device *dev)
361 {
362 	struct cxgb4_tc_port_matchall *tc_port_matchall;
363 	struct port_info *pi = netdev2pinfo(dev);
364 	struct adapter *adap = netdev2adap(dev);
365 
366 	tc_port_matchall = &adap->tc_matchall->port_matchall[pi->port_id];
367 	if (tc_port_matchall->egress.state == CXGB4_MATCHALL_STATE_ENABLED)
368 		cxgb4_matchall_free_tc(dev);
369 
370 	if (tc_port_matchall->ingress.state == CXGB4_MATCHALL_STATE_ENABLED)
371 		cxgb4_matchall_free_filter(dev);
372 }
373 
374 int cxgb4_init_tc_matchall(struct adapter *adap)
375 {
376 	struct cxgb4_tc_port_matchall *tc_port_matchall;
377 	struct cxgb4_tc_matchall *tc_matchall;
378 	int ret;
379 
380 	tc_matchall = kzalloc(sizeof(*tc_matchall), GFP_KERNEL);
381 	if (!tc_matchall)
382 		return -ENOMEM;
383 
384 	tc_port_matchall = kcalloc(adap->params.nports,
385 				   sizeof(*tc_port_matchall),
386 				   GFP_KERNEL);
387 	if (!tc_port_matchall) {
388 		ret = -ENOMEM;
389 		goto out_free_matchall;
390 	}
391 
392 	tc_matchall->port_matchall = tc_port_matchall;
393 	adap->tc_matchall = tc_matchall;
394 	return 0;
395 
396 out_free_matchall:
397 	kfree(tc_matchall);
398 	return ret;
399 }
400 
401 void cxgb4_cleanup_tc_matchall(struct adapter *adap)
402 {
403 	u8 i;
404 
405 	if (adap->tc_matchall) {
406 		if (adap->tc_matchall->port_matchall) {
407 			for (i = 0; i < adap->params.nports; i++) {
408 				struct net_device *dev = adap->port[i];
409 
410 				if (dev)
411 					cxgb4_matchall_disable_offload(dev);
412 			}
413 			kfree(adap->tc_matchall->port_matchall);
414 		}
415 		kfree(adap->tc_matchall);
416 	}
417 }
418