xref: /freebsd/sys/dev/cxgbe/t4_sched.c (revision fee1489eb14e022d52bea01ff4399bc6d7db3015)
1 /*-
2  * Copyright (c) 2017 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_ratelimit.h"
34 
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/queue.h>
38 #include <sys/sbuf.h>
39 #include <sys/taskqueue.h>
40 #include <sys/sysctl.h>
41 
42 #include "common/common.h"
43 #include "common/t4_regs.h"
44 #include "common/t4_regs_values.h"
45 #include "common/t4_msg.h"
46 
47 
48 static int
49 in_range(int val, int lo, int hi)
50 {
51 
52 	return (val < 0 || (val <= hi && val >= lo));
53 }
54 
55 static int
56 set_sched_class_config(struct adapter *sc, int minmax)
57 {
58 	int rc;
59 
60 	if (minmax < 0)
61 		return (EINVAL);
62 
63 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4sscc");
64 	if (rc)
65 		return (rc);
66 	rc = -t4_sched_config(sc, FW_SCHED_TYPE_PKTSCHED, minmax, 1);
67 	end_synchronized_op(sc, 0);
68 
69 	return (rc);
70 }
71 
72 static int
73 set_sched_class_params(struct adapter *sc, struct t4_sched_class_params *p,
74     int sleep_ok)
75 {
76 	int rc, top_speed, fw_level, fw_mode, fw_rateunit, fw_ratemode;
77 	struct port_info *pi;
78 	struct tx_cl_rl_params *tc, old;
79 	bool check_pktsize = false;
80 
81 	if (p->level == SCHED_CLASS_LEVEL_CL_RL)
82 		fw_level = FW_SCHED_PARAMS_LEVEL_CL_RL;
83 	else if (p->level == SCHED_CLASS_LEVEL_CL_WRR)
84 		fw_level = FW_SCHED_PARAMS_LEVEL_CL_WRR;
85 	else if (p->level == SCHED_CLASS_LEVEL_CH_RL)
86 		fw_level = FW_SCHED_PARAMS_LEVEL_CH_RL;
87 	else
88 		return (EINVAL);
89 
90 	if (p->level == SCHED_CLASS_LEVEL_CL_RL) {
91 		if (p->mode == SCHED_CLASS_MODE_CLASS)
92 			fw_mode = FW_SCHED_PARAMS_MODE_CLASS;
93 		else if (p->mode == SCHED_CLASS_MODE_FLOW) {
94 			check_pktsize = true;
95 			fw_mode = FW_SCHED_PARAMS_MODE_FLOW;
96 		} else
97 			return (EINVAL);
98 	} else
99 		fw_mode = 0;
100 
101 	/* Valid channel must always be provided. */
102 	if (p->channel < 0)
103 		return (EINVAL);
104 	if (!in_range(p->channel, 0, sc->chip_params->nchan - 1))
105 		return (ERANGE);
106 
107 	pi = sc->port[sc->chan_map[p->channel]];
108 	if (pi == NULL)
109 		return (ENXIO);
110 	MPASS(pi->tx_chan == p->channel);
111 	top_speed = port_top_speed(pi) * 1000000; /* Gbps -> Kbps */
112 
113 	if (p->level == SCHED_CLASS_LEVEL_CL_RL ||
114 	    p->level == SCHED_CLASS_LEVEL_CH_RL) {
115 		/*
116 		 * Valid rate (mode, unit and values) must be provided.
117 		 */
118 
119 		if (p->minrate < 0)
120 			p->minrate = 0;
121 		if (p->maxrate < 0)
122 			return (EINVAL);
123 
124 		if (p->rateunit == SCHED_CLASS_RATEUNIT_BITS) {
125 			fw_rateunit = FW_SCHED_PARAMS_UNIT_BITRATE;
126 			/* ratemode could be relative (%) or absolute. */
127 			if (p->ratemode == SCHED_CLASS_RATEMODE_REL) {
128 				fw_ratemode = FW_SCHED_PARAMS_RATE_REL;
129 				/* maxrate is % of port bandwidth. */
130 				if (!in_range(p->minrate, 0, 100) ||
131 				    !in_range(p->maxrate, 0, 100)) {
132 					return (ERANGE);
133 				}
134 			} else if (p->ratemode == SCHED_CLASS_RATEMODE_ABS) {
135 				fw_ratemode = FW_SCHED_PARAMS_RATE_ABS;
136 				/* maxrate is absolute value in kbps. */
137 				if (!in_range(p->minrate, 0, top_speed) ||
138 				    !in_range(p->maxrate, 0, top_speed)) {
139 					return (ERANGE);
140 				}
141 			} else
142 				return (EINVAL);
143 		} else if (p->rateunit == SCHED_CLASS_RATEUNIT_PKTS) {
144 			/* maxrate is the absolute value in pps. */
145 			check_pktsize = true;
146 			fw_rateunit = FW_SCHED_PARAMS_UNIT_PKTRATE;
147 		} else
148 			return (EINVAL);
149 	} else {
150 		MPASS(p->level == SCHED_CLASS_LEVEL_CL_WRR);
151 
152 		/*
153 		 * Valid weight must be provided.
154 		 */
155 		if (p->weight < 0)
156 		       return (EINVAL);
157 		if (!in_range(p->weight, 1, 99))
158 			return (ERANGE);
159 
160 		fw_rateunit = 0;
161 		fw_ratemode = 0;
162 	}
163 
164 	if (p->level == SCHED_CLASS_LEVEL_CL_RL ||
165 	    p->level == SCHED_CLASS_LEVEL_CL_WRR) {
166 		/*
167 		 * Valid scheduling class must be provided.
168 		 */
169 		if (p->cl < 0)
170 			return (EINVAL);
171 		if (!in_range(p->cl, 0, sc->chip_params->nsched_cls - 1))
172 			return (ERANGE);
173 	}
174 
175 	if (check_pktsize) {
176 		if (p->pktsize < 0)
177 			return (EINVAL);
178 		if (!in_range(p->pktsize, 64, pi->vi[0].ifp->if_mtu))
179 			return (ERANGE);
180 	}
181 
182 	if (p->level == SCHED_CLASS_LEVEL_CL_RL) {
183 		tc = &pi->sched_params->cl_rl[p->cl];
184 		mtx_lock(&sc->tc_lock);
185 		if (tc->refcount > 0 || tc->flags & (CLRL_SYNC | CLRL_ASYNC))
186 			rc = EBUSY;
187 		else {
188 			tc->flags |= CLRL_SYNC | CLRL_USER;
189 			tc->ratemode = fw_ratemode;
190 			tc->rateunit = fw_rateunit;
191 			tc->mode = fw_mode;
192 			tc->maxrate = p->maxrate;
193 			tc->pktsize = p->pktsize;
194 			rc = 0;
195 			old= *tc;
196 		}
197 		mtx_unlock(&sc->tc_lock);
198 		if (rc != 0)
199 			return (rc);
200 	}
201 
202 	rc = begin_synchronized_op(sc, NULL,
203 	    sleep_ok ? (SLEEP_OK | INTR_OK) : HOLD_LOCK, "t4sscp");
204 	if (rc != 0) {
205 		if (p->level == SCHED_CLASS_LEVEL_CL_RL) {
206 			mtx_lock(&sc->tc_lock);
207 			*tc = old;
208 			mtx_unlock(&sc->tc_lock);
209 		}
210 		return (rc);
211 	}
212 	rc = -t4_sched_params(sc, FW_SCHED_TYPE_PKTSCHED, fw_level, fw_mode,
213 	    fw_rateunit, fw_ratemode, p->channel, p->cl, p->minrate, p->maxrate,
214 	    p->weight, p->pktsize, sleep_ok);
215 	end_synchronized_op(sc, sleep_ok ? 0 : LOCK_HELD);
216 
217 	if (p->level == SCHED_CLASS_LEVEL_CL_RL) {
218 		mtx_lock(&sc->tc_lock);
219 		MPASS(tc->flags & CLRL_SYNC);
220 		MPASS(tc->flags & CLRL_USER);
221 		MPASS(tc->refcount == 0);
222 
223 		tc->flags &= ~CLRL_SYNC;
224 		if (rc == 0)
225 			tc->flags &= ~CLRL_ERR;
226 		else
227 			tc->flags |= CLRL_ERR;
228 		mtx_unlock(&sc->tc_lock);
229 	}
230 
231 	return (rc);
232 }
233 
234 static void
235 update_tx_sched(void *context, int pending)
236 {
237 	int i, j, rc;
238 	struct port_info *pi;
239 	struct tx_cl_rl_params *tc;
240 	struct adapter *sc = context;
241 	const int n = sc->chip_params->nsched_cls;
242 
243 	mtx_lock(&sc->tc_lock);
244 	for_each_port(sc, i) {
245 		pi = sc->port[i];
246 		tc = &pi->sched_params->cl_rl[0];
247 		for (j = 0; j < n; j++, tc++) {
248 			MPASS(mtx_owned(&sc->tc_lock));
249 			if ((tc->flags & CLRL_ASYNC) == 0)
250 				continue;
251 			mtx_unlock(&sc->tc_lock);
252 
253 			if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
254 			    "t4utxs") != 0) {
255 				mtx_lock(&sc->tc_lock);
256 				continue;
257 			}
258 			rc = -t4_sched_params(sc, FW_SCHED_TYPE_PKTSCHED,
259 			    FW_SCHED_PARAMS_LEVEL_CL_RL, tc->mode, tc->rateunit,
260 			    tc->ratemode, pi->tx_chan, j, 0, tc->maxrate, 0,
261 			    tc->pktsize, 1);
262 			end_synchronized_op(sc, 0);
263 
264 			mtx_lock(&sc->tc_lock);
265 			MPASS(tc->flags & CLRL_ASYNC);
266 			tc->flags &= ~CLRL_ASYNC;
267 			if (rc == 0)
268 				tc->flags &= ~CLRL_ERR;
269 			else
270 				tc->flags |= CLRL_ERR;
271 		}
272 	}
273 	mtx_unlock(&sc->tc_lock);
274 }
275 
276 int
277 t4_set_sched_class(struct adapter *sc, struct t4_sched_params *p)
278 {
279 
280 	if (p->type != SCHED_CLASS_TYPE_PACKET)
281 		return (EINVAL);
282 
283 	if (p->subcmd == SCHED_CLASS_SUBCMD_CONFIG)
284 		return (set_sched_class_config(sc, p->u.config.minmax));
285 
286 	if (p->subcmd == SCHED_CLASS_SUBCMD_PARAMS)
287 		return (set_sched_class_params(sc, &p->u.params, 1));
288 
289 	return (EINVAL);
290 }
291 
292 static int
293 bind_txq_to_traffic_class(struct adapter *sc, struct sge_txq *txq, int idx)
294 {
295 	struct tx_cl_rl_params *tc0, *tc;
296 	int rc, old_idx;
297 	uint32_t fw_mnem, fw_class;
298 
299 	if (!(txq->eq.flags & EQ_ALLOCATED))
300 		return (EAGAIN);
301 
302 	mtx_lock(&sc->tc_lock);
303 	if (txq->tc_idx == -2) {
304 		rc = EBUSY;	/* Another bind/unbind in progress already. */
305 		goto done;
306 	}
307 	if (idx == txq->tc_idx) {
308 		rc = 0;		/* No change, nothing to do. */
309 		goto done;
310 	}
311 
312 	tc0 = &sc->port[txq->eq.tx_chan]->sched_params->cl_rl[0];
313 	if (idx != -1) {
314 		/*
315 		 * Bind to a different class at index idx.
316 		 */
317 		tc = &tc0[idx];
318 		if (tc->flags & CLRL_ERR) {
319 			rc = ENXIO;
320 			goto done;
321 		} else {
322 			/*
323 			 * Ok to proceed.  Place a reference on the new class
324 			 * while still holding on to the reference on the
325 			 * previous class, if any.
326 			 */
327 			tc->refcount++;
328 		}
329 	}
330 	/* Mark as busy before letting go of the lock. */
331 	old_idx = txq->tc_idx;
332 	txq->tc_idx = -2;
333 	mtx_unlock(&sc->tc_lock);
334 
335 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4btxq");
336 	if (rc != 0)
337 		return (rc);
338 	fw_mnem = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
339 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_EQ_SCHEDCLASS_ETH) |
340 	    V_FW_PARAMS_PARAM_YZ(txq->eq.cntxt_id));
341 	fw_class = idx < 0 ? 0xffffffff : idx;
342 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &fw_mnem, &fw_class);
343 	end_synchronized_op(sc, 0);
344 
345 	mtx_lock(&sc->tc_lock);
346 	MPASS(txq->tc_idx == -2);
347 	if (rc == 0) {
348 		/*
349 		 * Unbind, bind, or bind to a different class succeeded.  Remove
350 		 * the reference on the old traffic class, if any.
351 		 */
352 		if (old_idx != -1) {
353 			tc = &tc0[old_idx];
354 			MPASS(tc->refcount > 0);
355 			tc->refcount--;
356 		}
357 		txq->tc_idx = idx;
358 	} else {
359 		/*
360 		 * Unbind, bind, or bind to a different class failed.  Remove
361 		 * the anticipatory reference on the new traffic class, if any.
362 		 */
363 		if (idx != -1) {
364 			tc = &tc0[idx];
365 			MPASS(tc->refcount > 0);
366 			tc->refcount--;
367 		}
368 		txq->tc_idx = old_idx;
369 	}
370 done:
371 	MPASS(txq->tc_idx >= -1 && txq->tc_idx < sc->chip_params->nsched_cls);
372 	mtx_unlock(&sc->tc_lock);
373 	return (rc);
374 }
375 
376 int
377 t4_set_sched_queue(struct adapter *sc, struct t4_sched_queue *p)
378 {
379 	struct port_info *pi = NULL;
380 	struct vi_info *vi;
381 	struct sge_txq *txq;
382 	int i, rc;
383 
384 	if (p->port >= sc->params.nports)
385 		return (EINVAL);
386 
387 	/*
388 	 * XXX: cxgbetool allows the user to specify the physical port only.  So
389 	 * we always operate on the main VI.
390 	 */
391 	pi = sc->port[p->port];
392 	vi = &pi->vi[0];
393 
394 	/* Checking VI_INIT_DONE outside a synch-op is a harmless race here. */
395 	if (!(vi->flags & VI_INIT_DONE))
396 		return (EAGAIN);
397 
398 	if (!in_range(p->queue, 0, vi->ntxq - 1) ||
399 	    !in_range(p->cl, 0, sc->chip_params->nsched_cls - 1))
400 		return (EINVAL);
401 
402 	if (p->queue < 0) {
403 		/*
404 		 * Change the scheduling on all the TX queues for the
405 		 * interface.
406 		 */
407 		for_each_txq(vi, i, txq) {
408 			rc = bind_txq_to_traffic_class(sc, txq, p->cl);
409 			if (rc != 0)
410 				break;
411 		}
412 	} else {
413 		/*
414 		 * If op.queue is non-negative, then we're only changing the
415 		 * scheduling on a single specified TX queue.
416 		 */
417 		txq = &sc->sge.txq[vi->first_txq + p->queue];
418 		rc = bind_txq_to_traffic_class(sc, txq, p->cl);
419 	}
420 
421 	return (rc);
422 }
423 
424 int
425 t4_init_tx_sched(struct adapter *sc)
426 {
427 	int i, j;
428 	const int n = sc->chip_params->nsched_cls;
429 	struct port_info *pi;
430 	struct tx_cl_rl_params *tc;
431 
432 	mtx_init(&sc->tc_lock, "tx_sched lock", NULL, MTX_DEF);
433 	TASK_INIT(&sc->tc_task, 0, update_tx_sched, sc);
434 	for_each_port(sc, i) {
435 		pi = sc->port[i];
436 		pi->sched_params = malloc(sizeof(*pi->sched_params) +
437 		    n * sizeof(*tc), M_CXGBE, M_ZERO | M_WAITOK);
438 		tc = &pi->sched_params->cl_rl[0];
439 		for (j = 0; j < n; j++, tc++) {
440 			tc->refcount = 0;
441 			tc->ratemode = FW_SCHED_PARAMS_RATE_ABS;
442 			tc->rateunit = FW_SCHED_PARAMS_UNIT_BITRATE;
443 			tc->mode = FW_SCHED_PARAMS_MODE_CLASS;
444 			tc->maxrate = 1000 * 1000;	/* 1 Gbps.  Arbitrary */
445 
446 			if (t4_sched_params_cl_rl_kbps(sc, pi->tx_chan, j,
447 			    tc->mode, tc->maxrate, tc->pktsize, 1) != 0)
448 				tc->flags = CLRL_ERR;
449 		}
450 	}
451 
452 	return (0);
453 }
454 
455 int
456 t4_free_tx_sched(struct adapter *sc)
457 {
458 	int i;
459 
460 	taskqueue_drain(taskqueue_thread, &sc->tc_task);
461 
462 	for_each_port(sc, i) {
463 		if (sc->port[i] != NULL)
464 			free(sc->port[i]->sched_params, M_CXGBE);
465 	}
466 
467 	if (mtx_initialized(&sc->tc_lock))
468 		mtx_destroy(&sc->tc_lock);
469 
470 	return (0);
471 }
472 
473 void
474 t4_update_tx_sched(struct adapter *sc)
475 {
476 
477 	taskqueue_enqueue(taskqueue_thread, &sc->tc_task);
478 }
479 
480 int
481 t4_reserve_cl_rl_kbps(struct adapter *sc, int port_id, u_int maxrate,
482     int *tc_idx)
483 {
484 	int rc = 0, fa = -1, i;
485 	bool update;
486 	struct tx_cl_rl_params *tc;
487 	struct port_info *pi;
488 
489 	MPASS(port_id >= 0 && port_id < sc->params.nports);
490 
491 	pi = sc->port[port_id];
492 	tc = &pi->sched_params->cl_rl[0];
493 	update = false;
494 	mtx_lock(&sc->tc_lock);
495 	for (i = 0; i < sc->chip_params->nsched_cls; i++, tc++) {
496 		if (fa < 0 && tc->refcount == 0 && !(tc->flags & CLRL_USER))
497 			fa = i;		/* first available */
498 
499 		if (tc->ratemode == FW_SCHED_PARAMS_RATE_ABS &&
500 		    tc->rateunit == FW_SCHED_PARAMS_UNIT_BITRATE &&
501 		    tc->mode == FW_SCHED_PARAMS_MODE_FLOW &&
502 		    tc->maxrate == maxrate &&
503 		    tc->pktsize == pi->vi[0].ifp->if_mtu) {
504 			tc->refcount++;
505 			*tc_idx = i;
506 			if ((tc->flags & (CLRL_ERR | CLRL_ASYNC | CLRL_SYNC)) ==
507 			    CLRL_ERR) {
508 				update = true;
509 			}
510 			goto done;
511 		}
512 	}
513 	/* Not found */
514 	MPASS(i == sc->chip_params->nsched_cls);
515 	if (fa != -1) {
516 		tc = &pi->sched_params->cl_rl[fa];
517 		tc->refcount = 1;
518 		tc->ratemode = FW_SCHED_PARAMS_RATE_ABS;
519 		tc->rateunit = FW_SCHED_PARAMS_UNIT_BITRATE;
520 		tc->mode = FW_SCHED_PARAMS_MODE_FLOW;
521 		tc->maxrate = maxrate;
522 		tc->pktsize = pi->vi[0].ifp->if_mtu;
523 		*tc_idx = fa;
524 		update = true;
525 	} else {
526 		*tc_idx = -1;
527 		rc = ENOSPC;
528 	}
529 done:
530 	mtx_unlock(&sc->tc_lock);
531 	if (update) {
532 		tc->flags |= CLRL_ASYNC;
533 		t4_update_tx_sched(sc);
534 	}
535 	return (rc);
536 }
537 
538 void
539 t4_release_cl_rl(struct adapter *sc, int port_id, int tc_idx)
540 {
541 	struct tx_cl_rl_params *tc;
542 
543 	MPASS(port_id >= 0 && port_id < sc->params.nports);
544 	MPASS(tc_idx >= 0 && tc_idx < sc->chip_params->nsched_cls);
545 
546 	mtx_lock(&sc->tc_lock);
547 	tc = &sc->port[port_id]->sched_params->cl_rl[tc_idx];
548 	MPASS(tc->refcount > 0);
549 	tc->refcount--;
550 	mtx_unlock(&sc->tc_lock);
551 }
552 
553 int
554 sysctl_tc(SYSCTL_HANDLER_ARGS)
555 {
556 	struct vi_info *vi = arg1;
557 	struct port_info *pi;
558 	struct adapter *sc;
559 	struct sge_txq *txq;
560 	int qidx = arg2, rc, tc_idx;
561 
562 	MPASS(qidx >= 0 && qidx < vi->ntxq);
563 	pi = vi->pi;
564 	sc = pi->adapter;
565 	txq = &sc->sge.txq[vi->first_txq + qidx];
566 
567 	tc_idx = txq->tc_idx;
568 	rc = sysctl_handle_int(oidp, &tc_idx, 0, req);
569 	if (rc != 0 || req->newptr == NULL)
570 		return (rc);
571 
572 	if (sc->flags & IS_VF)
573 		return (EPERM);
574 	if (!in_range(tc_idx, 0, sc->chip_params->nsched_cls - 1))
575 		return (EINVAL);
576 
577 	return (bind_txq_to_traffic_class(sc, txq, tc_idx));
578 }
579 
580 int
581 sysctl_tc_params(SYSCTL_HANDLER_ARGS)
582 {
583 	struct adapter *sc = arg1;
584 	struct tx_cl_rl_params tc;
585 	struct sbuf *sb;
586 	int i, rc, port_id, mbps, gbps;
587 
588 	rc = sysctl_wire_old_buffer(req, 0);
589 	if (rc != 0)
590 		return (rc);
591 
592 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
593 	if (sb == NULL)
594 		return (ENOMEM);
595 
596 	port_id = arg2 >> 16;
597 	MPASS(port_id < sc->params.nports);
598 	MPASS(sc->port[port_id] != NULL);
599 	i = arg2 & 0xffff;
600 	MPASS(i < sc->chip_params->nsched_cls);
601 
602 	mtx_lock(&sc->tc_lock);
603 	tc = sc->port[port_id]->sched_params->cl_rl[i];
604 	mtx_unlock(&sc->tc_lock);
605 
606 	switch (tc.rateunit) {
607 	case SCHED_CLASS_RATEUNIT_BITS:
608 		switch (tc.ratemode) {
609 		case SCHED_CLASS_RATEMODE_REL:
610 			/* XXX: top speed or actual link speed? */
611 			gbps = port_top_speed(sc->port[port_id]);
612 			sbuf_printf(sb, "%u%% of %uGbps", tc.maxrate, gbps);
613 			break;
614 		case SCHED_CLASS_RATEMODE_ABS:
615 			mbps = tc.maxrate / 1000;
616 			gbps = tc.maxrate / 1000000;
617 			if (tc.maxrate == gbps * 1000000)
618 				sbuf_printf(sb, "%uGbps", gbps);
619 			else if (tc.maxrate == mbps * 1000)
620 				sbuf_printf(sb, "%uMbps", mbps);
621 			else
622 				sbuf_printf(sb, "%uKbps", tc.maxrate);
623 			break;
624 		default:
625 			rc = ENXIO;
626 			goto done;
627 		}
628 		break;
629 	case SCHED_CLASS_RATEUNIT_PKTS:
630 		sbuf_printf(sb, "%upps", tc.maxrate);
631 		break;
632 	default:
633 		rc = ENXIO;
634 		goto done;
635 	}
636 
637 	switch (tc.mode) {
638 	case SCHED_CLASS_MODE_CLASS:
639 		sbuf_printf(sb, " aggregate");
640 		break;
641 	case SCHED_CLASS_MODE_FLOW:
642 		sbuf_printf(sb, " per-flow");
643 		break;
644 	default:
645 		rc = ENXIO;
646 		goto done;
647 	}
648 
649 done:
650 	if (rc == 0)
651 		rc = sbuf_finish(sb);
652 	sbuf_delete(sb);
653 
654 	return (rc);
655 }
656 
657 #ifdef RATELIMIT
658 void
659 t4_init_etid_table(struct adapter *sc)
660 {
661 	int i;
662 	struct tid_info *t;
663 
664 	if (!is_ethoffload(sc))
665 		return;
666 
667 	t = &sc->tids;
668 	MPASS(t->netids > 0);
669 
670 	mtx_init(&t->etid_lock, "etid lock", NULL, MTX_DEF);
671 	t->etid_tab = malloc(sizeof(*t->etid_tab) * t->netids, M_CXGBE,
672 			M_ZERO | M_WAITOK);
673 	t->efree = t->etid_tab;
674 	t->etids_in_use = 0;
675 	for (i = 1; i < t->netids; i++)
676 		t->etid_tab[i - 1].next = &t->etid_tab[i];
677 	t->etid_tab[t->netids - 1].next = NULL;
678 }
679 
680 void
681 t4_free_etid_table(struct adapter *sc)
682 {
683 	struct tid_info *t;
684 
685 	if (!is_ethoffload(sc))
686 		return;
687 
688 	t = &sc->tids;
689 	MPASS(t->netids > 0);
690 
691 	free(t->etid_tab, M_CXGBE);
692 	t->etid_tab = NULL;
693 
694 	if (mtx_initialized(&t->etid_lock))
695 		mtx_destroy(&t->etid_lock);
696 }
697 
698 /* etid services */
699 static int alloc_etid(struct adapter *, struct cxgbe_snd_tag *);
700 static void free_etid(struct adapter *, int);
701 
702 static int
703 alloc_etid(struct adapter *sc, struct cxgbe_snd_tag *cst)
704 {
705 	struct tid_info *t = &sc->tids;
706 	int etid = -1;
707 
708 	mtx_lock(&t->etid_lock);
709 	if (t->efree) {
710 		union etid_entry *p = t->efree;
711 
712 		etid = p - t->etid_tab + t->etid_base;
713 		t->efree = p->next;
714 		p->cst = cst;
715 		t->etids_in_use++;
716 	}
717 	mtx_unlock(&t->etid_lock);
718 	return (etid);
719 }
720 
721 struct cxgbe_snd_tag *
722 lookup_etid(struct adapter *sc, int etid)
723 {
724 	struct tid_info *t = &sc->tids;
725 
726 	return (t->etid_tab[etid - t->etid_base].cst);
727 }
728 
729 static void
730 free_etid(struct adapter *sc, int etid)
731 {
732 	struct tid_info *t = &sc->tids;
733 	union etid_entry *p = &t->etid_tab[etid - t->etid_base];
734 
735 	mtx_lock(&t->etid_lock);
736 	p->next = t->efree;
737 	t->efree = p;
738 	t->etids_in_use--;
739 	mtx_unlock(&t->etid_lock);
740 }
741 
742 int
743 cxgbe_snd_tag_alloc(struct ifnet *ifp, union if_snd_tag_alloc_params *params,
744     struct m_snd_tag **pt)
745 {
746 	int rc, schedcl;
747 	struct vi_info *vi = ifp->if_softc;
748 	struct port_info *pi = vi->pi;
749 	struct adapter *sc = pi->adapter;
750 	struct cxgbe_snd_tag *cst;
751 
752 	if (params->hdr.type != IF_SND_TAG_TYPE_RATE_LIMIT)
753 		return (ENOTSUP);
754 
755 	rc = t4_reserve_cl_rl_kbps(sc, pi->port_id,
756 	    (params->rate_limit.max_rate * 8ULL / 1000), &schedcl);
757 	if (rc != 0)
758 		return (rc);
759 	MPASS(schedcl >= 0 && schedcl < sc->chip_params->nsched_cls);
760 
761 	cst = malloc(sizeof(*cst), M_CXGBE, M_ZERO | M_NOWAIT);
762 	if (cst == NULL) {
763 failed:
764 		t4_release_cl_rl(sc, pi->port_id, schedcl);
765 		return (ENOMEM);
766 	}
767 
768 	cst->etid = alloc_etid(sc, cst);
769 	if (cst->etid < 0) {
770 		free(cst, M_CXGBE);
771 		goto failed;
772 	}
773 
774 	mtx_init(&cst->lock, "cst_lock", NULL, MTX_DEF);
775 	mbufq_init(&cst->pending_tx, INT_MAX);
776 	mbufq_init(&cst->pending_fwack, INT_MAX);
777 	cst->com.ifp = ifp;
778 	cst->flags |= EO_FLOWC_PENDING | EO_SND_TAG_REF;
779 	cst->adapter = sc;
780 	cst->port_id = pi->port_id;
781 	cst->schedcl = schedcl;
782 	cst->max_rate = params->rate_limit.max_rate;
783 	cst->tx_credits = sc->params.eo_wr_cred;
784 	cst->tx_total = cst->tx_credits;
785 	cst->plen = 0;
786 	cst->ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) |
787 	    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(G_FW_VIID_PFN(vi->viid)) |
788 	    V_TXPKT_VF(G_FW_VIID_VIN(vi->viid)) |
789 	    V_TXPKT_VF_VLD(G_FW_VIID_VIVLD(vi->viid)));
790 
791 	/*
792 	 * Queues will be selected later when the connection flowid is available.
793 	 */
794 
795 	*pt = &cst->com;
796 	return (0);
797 }
798 
799 /*
800  * Change in parameters, no change in ifp.
801  */
802 int
803 cxgbe_snd_tag_modify(struct m_snd_tag *mst,
804     union if_snd_tag_modify_params *params)
805 {
806 	int rc, schedcl;
807 	struct cxgbe_snd_tag *cst = mst_to_cst(mst);
808 	struct adapter *sc = cst->adapter;
809 
810 	/* XXX: is schedcl -1 ok here? */
811 	MPASS(cst->schedcl >= 0 && cst->schedcl < sc->chip_params->nsched_cls);
812 
813 	mtx_lock(&cst->lock);
814 	MPASS(cst->flags & EO_SND_TAG_REF);
815 	rc = t4_reserve_cl_rl_kbps(sc, cst->port_id,
816 	    (params->rate_limit.max_rate * 8ULL / 1000), &schedcl);
817 	if (rc != 0)
818 		return (rc);
819 	MPASS(schedcl >= 0 && schedcl < sc->chip_params->nsched_cls);
820 	t4_release_cl_rl(sc, cst->port_id, cst->schedcl);
821 	cst->schedcl = schedcl;
822 	cst->max_rate = params->rate_limit.max_rate;
823 	mtx_unlock(&cst->lock);
824 
825 	return (0);
826 }
827 
828 int
829 cxgbe_snd_tag_query(struct m_snd_tag *mst,
830     union if_snd_tag_query_params *params)
831 {
832 	struct cxgbe_snd_tag *cst = mst_to_cst(mst);
833 
834 	params->rate_limit.max_rate = cst->max_rate;
835 
836 #define CST_TO_MST_QLEVEL_SCALE (IF_SND_QUEUE_LEVEL_MAX / cst->tx_total)
837 	params->rate_limit.queue_level =
838 		(cst->tx_total - cst->tx_credits) * CST_TO_MST_QLEVEL_SCALE;
839 
840 	return (0);
841 }
842 
843 /*
844  * Unlocks cst and frees it.
845  */
846 void
847 cxgbe_snd_tag_free_locked(struct cxgbe_snd_tag *cst)
848 {
849 	struct adapter *sc = cst->adapter;
850 
851 	mtx_assert(&cst->lock, MA_OWNED);
852 	MPASS((cst->flags & EO_SND_TAG_REF) == 0);
853 	MPASS(cst->tx_credits == cst->tx_total);
854 	MPASS(cst->plen == 0);
855 	MPASS(mbufq_first(&cst->pending_tx) == NULL);
856 	MPASS(mbufq_first(&cst->pending_fwack) == NULL);
857 
858 	if (cst->etid >= 0)
859 		free_etid(sc, cst->etid);
860 	if (cst->schedcl != -1)
861 		t4_release_cl_rl(sc, cst->port_id, cst->schedcl);
862 	mtx_unlock(&cst->lock);
863 	mtx_destroy(&cst->lock);
864 	free(cst, M_CXGBE);
865 }
866 
867 void
868 cxgbe_snd_tag_free(struct m_snd_tag *mst)
869 {
870 	struct cxgbe_snd_tag *cst = mst_to_cst(mst);
871 
872 	mtx_lock(&cst->lock);
873 
874 	/* The kernel is done with the snd_tag.  Remove its reference. */
875 	MPASS(cst->flags & EO_SND_TAG_REF);
876 	cst->flags &= ~EO_SND_TAG_REF;
877 
878 	if (cst->ncompl == 0) {
879 		/*
880 		 * No fw4_ack in flight.  Free the tag right away if there are
881 		 * no outstanding credits.  Request the firmware to return all
882 		 * credits for the etid otherwise.
883 		 */
884 		if (cst->tx_credits == cst->tx_total) {
885 			cxgbe_snd_tag_free_locked(cst);
886 			return;	/* cst is gone. */
887 		}
888 		send_etid_flush_wr(cst);
889 	}
890 	mtx_unlock(&cst->lock);
891 }
892 #endif
893