2 * Copyright (c) 2017 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
33 #include "opt_ratelimit.h"
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/queue.h>
39 #include <sys/taskqueue.h>
40 #include <sys/sysctl.h>
42 #include "common/common.h"
43 #include "common/t4_regs.h"
44 #include "common/t4_regs_values.h"
45 #include "common/t4_msg.h"
48 in_range(int val, int lo, int hi)
51 return (val < 0 || (val <= hi && val >= lo));
55 set_sched_class_config(struct adapter *sc, int minmax)
62 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4sscc");
65 if (hw_off_limits(sc))
68 rc = -t4_sched_config(sc, FW_SCHED_TYPE_PKTSCHED, minmax, 1);
69 end_synchronized_op(sc, 0);
75 set_sched_class_params(struct adapter *sc, struct t4_sched_class_params *p,
78 int rc, top_speed, fw_level, fw_mode, fw_rateunit, fw_ratemode;
80 struct tx_cl_rl_params *tc, old;
81 bool check_pktsize = false;
83 if (p->level == SCHED_CLASS_LEVEL_CL_RL)
84 fw_level = FW_SCHED_PARAMS_LEVEL_CL_RL;
85 else if (p->level == SCHED_CLASS_LEVEL_CL_WRR)
86 fw_level = FW_SCHED_PARAMS_LEVEL_CL_WRR;
87 else if (p->level == SCHED_CLASS_LEVEL_CH_RL)
88 fw_level = FW_SCHED_PARAMS_LEVEL_CH_RL;
92 if (p->level == SCHED_CLASS_LEVEL_CL_RL) {
93 if (p->mode == SCHED_CLASS_MODE_CLASS)
94 fw_mode = FW_SCHED_PARAMS_MODE_CLASS;
95 else if (p->mode == SCHED_CLASS_MODE_FLOW) {
97 fw_mode = FW_SCHED_PARAMS_MODE_FLOW;
103 /* Valid channel must always be provided. */
106 if (!in_range(p->channel, 0, sc->chip_params->nchan - 1))
109 pi = sc->port[sc->chan_map[p->channel]];
112 MPASS(pi->tx_chan == p->channel);
113 top_speed = port_top_speed(pi) * 1000000; /* Gbps -> Kbps */
115 if (p->level == SCHED_CLASS_LEVEL_CL_RL ||
116 p->level == SCHED_CLASS_LEVEL_CH_RL) {
118 * Valid rate (mode, unit and values) must be provided.
126 if (p->rateunit == SCHED_CLASS_RATEUNIT_BITS) {
127 fw_rateunit = FW_SCHED_PARAMS_UNIT_BITRATE;
128 /* ratemode could be relative (%) or absolute. */
129 if (p->ratemode == SCHED_CLASS_RATEMODE_REL) {
130 fw_ratemode = FW_SCHED_PARAMS_RATE_REL;
131 /* maxrate is % of port bandwidth. */
132 if (!in_range(p->minrate, 0, 100) ||
133 !in_range(p->maxrate, 0, 100)) {
136 } else if (p->ratemode == SCHED_CLASS_RATEMODE_ABS) {
137 fw_ratemode = FW_SCHED_PARAMS_RATE_ABS;
138 /* maxrate is absolute value in kbps. */
139 if (!in_range(p->minrate, 0, top_speed) ||
140 !in_range(p->maxrate, 0, top_speed)) {
145 } else if (p->rateunit == SCHED_CLASS_RATEUNIT_PKTS) {
146 /* maxrate is the absolute value in pps. */
147 check_pktsize = true;
148 fw_rateunit = FW_SCHED_PARAMS_UNIT_PKTRATE;
152 MPASS(p->level == SCHED_CLASS_LEVEL_CL_WRR);
155 * Valid weight must be provided.
159 if (!in_range(p->weight, 1, 99))
166 if (p->level == SCHED_CLASS_LEVEL_CL_RL ||
167 p->level == SCHED_CLASS_LEVEL_CL_WRR) {
169 * Valid scheduling class must be provided.
173 if (!in_range(p->cl, 0, sc->params.nsched_cls - 1))
180 if (!in_range(p->pktsize, 64, if_getmtu(pi->vi[0].ifp)))
184 if (p->level == SCHED_CLASS_LEVEL_CL_RL) {
185 tc = &pi->sched_params->cl_rl[p->cl];
186 mtx_lock(&sc->tc_lock);
187 if (tc->refcount > 0 || tc->state == CS_HW_UPDATE_IN_PROGRESS)
192 tc->flags |= CF_USER;
193 tc->state = CS_HW_UPDATE_IN_PROGRESS;
194 tc->ratemode = fw_ratemode;
195 tc->rateunit = fw_rateunit;
197 tc->maxrate = p->maxrate;
198 tc->pktsize = p->pktsize;
201 mtx_unlock(&sc->tc_lock);
206 rc = begin_synchronized_op(sc, NULL,
207 sleep_ok ? (SLEEP_OK | INTR_OK) : HOLD_LOCK, "t4sscp");
209 if (p->level == SCHED_CLASS_LEVEL_CL_RL) {
210 mtx_lock(&sc->tc_lock);
211 MPASS(tc->refcount == 0);
212 MPASS(tc->flags & CF_USER);
213 MPASS(tc->state == CS_HW_UPDATE_IN_PROGRESS);
215 mtx_unlock(&sc->tc_lock);
219 if (!hw_off_limits(sc)) {
220 rc = -t4_sched_params(sc, FW_SCHED_TYPE_PKTSCHED, fw_level,
221 fw_mode, fw_rateunit, fw_ratemode, p->channel, p->cl,
222 p->minrate, p->maxrate, p->weight, p->pktsize, 0, sleep_ok);
224 end_synchronized_op(sc, sleep_ok ? 0 : LOCK_HELD);
226 if (p->level == SCHED_CLASS_LEVEL_CL_RL) {
227 mtx_lock(&sc->tc_lock);
228 MPASS(tc->refcount == 0);
229 MPASS(tc->flags & CF_USER);
230 MPASS(tc->state == CS_HW_UPDATE_IN_PROGRESS);
233 tc->state = CS_HW_CONFIGURED;
235 /* parameters failed so we don't park at params_set */
236 tc->state = CS_UNINITIALIZED;
237 tc->flags &= ~CF_USER;
238 CH_ERR(pi, "failed to configure traffic class %d: %d. "
239 "params: mode %d, rateunit %d, ratemode %d, "
240 "channel %d, minrate %d, maxrate %d, pktsize %d, "
241 "burstsize %d\n", p->cl, rc, fw_mode, fw_rateunit,
242 fw_ratemode, p->channel, p->minrate, p->maxrate,
245 mtx_unlock(&sc->tc_lock);
252 update_tx_sched(void *context, int pending)
255 struct port_info *pi;
256 struct tx_cl_rl_params *tc;
257 struct adapter *sc = context;
258 const int n = sc->params.nsched_cls;
260 mtx_lock(&sc->tc_lock);
261 for_each_port(sc, i) {
263 tc = &pi->sched_params->cl_rl[0];
264 for (j = 0; j < n; j++, tc++) {
265 MPASS(mtx_owned(&sc->tc_lock));
266 if (tc->state != CS_HW_UPDATE_REQUESTED)
268 mtx_unlock(&sc->tc_lock);
270 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
272 mtx_lock(&sc->tc_lock);
275 rc = -t4_sched_params(sc, FW_SCHED_TYPE_PKTSCHED,
276 FW_SCHED_PARAMS_LEVEL_CL_RL, tc->mode, tc->rateunit,
277 tc->ratemode, pi->tx_chan, j, 0, tc->maxrate, 0,
278 tc->pktsize, tc->burstsize, 1);
279 end_synchronized_op(sc, 0);
281 mtx_lock(&sc->tc_lock);
282 MPASS(tc->state == CS_HW_UPDATE_REQUESTED);
284 tc->state = CS_HW_CONFIGURED;
287 /* parameters failed so we try to avoid params_set */
288 if (tc->refcount > 0)
289 tc->state = CS_PARAMS_SET;
291 tc->state = CS_UNINITIALIZED;
292 CH_ERR(pi, "failed to configure traffic class %d: %d. "
293 "params: mode %d, rateunit %d, ratemode %d, "
294 "channel %d, minrate %d, maxrate %d, pktsize %d, "
295 "burstsize %d\n", j, rc, tc->mode, tc->rateunit,
296 tc->ratemode, pi->tx_chan, 0, tc->maxrate,
297 tc->pktsize, tc->burstsize);
300 mtx_unlock(&sc->tc_lock);
304 t4_set_sched_class(struct adapter *sc, struct t4_sched_params *p)
307 if (p->type != SCHED_CLASS_TYPE_PACKET)
310 if (p->subcmd == SCHED_CLASS_SUBCMD_CONFIG)
311 return (set_sched_class_config(sc, p->u.config.minmax));
313 if (p->subcmd == SCHED_CLASS_SUBCMD_PARAMS)
314 return (set_sched_class_params(sc, &p->u.params, 1));
320 bind_txq_to_traffic_class(struct adapter *sc, struct sge_txq *txq, int idx)
322 struct tx_cl_rl_params *tc0, *tc;
324 uint32_t fw_mnem, fw_class;
326 if (!(txq->eq.flags & EQ_HW_ALLOCATED))
329 mtx_lock(&sc->tc_lock);
330 if (txq->tc_idx == -2) {
331 rc = EBUSY; /* Another bind/unbind in progress already. */
334 if (idx == txq->tc_idx) {
335 rc = 0; /* No change, nothing to do. */
339 tc0 = &sc->port[txq->eq.tx_chan]->sched_params->cl_rl[0];
342 * Bind to a different class at index idx.
345 if (tc->state != CS_HW_CONFIGURED) {
350 * Ok to proceed. Place a reference on the new class
351 * while still holding on to the reference on the
352 * previous class, if any.
357 /* Mark as busy before letting go of the lock. */
358 old_idx = txq->tc_idx;
360 mtx_unlock(&sc->tc_lock);
362 rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4btxq");
364 fw_mnem = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
365 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_EQ_SCHEDCLASS_ETH) |
366 V_FW_PARAMS_PARAM_YZ(txq->eq.cntxt_id));
367 fw_class = idx < 0 ? 0xffffffff : idx;
368 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &fw_mnem,
370 end_synchronized_op(sc, 0);
373 mtx_lock(&sc->tc_lock);
374 MPASS(txq->tc_idx == -2);
377 * Unbind, bind, or bind to a different class succeeded. Remove
378 * the reference on the old traffic class, if any.
382 MPASS(tc->refcount > 0);
388 * Unbind, bind, or bind to a different class failed. Remove
389 * the anticipatory reference on the new traffic class, if any.
393 MPASS(tc->refcount > 0);
396 txq->tc_idx = old_idx;
399 MPASS(txq->tc_idx >= -1 && txq->tc_idx < sc->params.nsched_cls);
400 mtx_unlock(&sc->tc_lock);
405 t4_set_sched_queue(struct adapter *sc, struct t4_sched_queue *p)
407 struct port_info *pi = NULL;
412 if (p->port >= sc->params.nports)
416 * XXX: cxgbetool allows the user to specify the physical port only. So
417 * we always operate on the main VI.
419 pi = sc->port[p->port];
422 /* Checking VI_INIT_DONE outside a synch-op is a harmless race here. */
423 if (!(vi->flags & VI_INIT_DONE))
427 if (!in_range(p->queue, 0, vi->ntxq - 1) ||
428 !in_range(p->cl, 0, sc->params.nsched_cls - 1))
433 * Change the scheduling on all the TX queues for the
436 for_each_txq(vi, i, txq) {
437 rc = bind_txq_to_traffic_class(sc, txq, p->cl);
443 * If op.queue is non-negative, then we're only changing the
444 * scheduling on a single specified TX queue.
446 txq = &sc->sge.txq[vi->first_txq + p->queue];
447 rc = bind_txq_to_traffic_class(sc, txq, p->cl);
454 t4_init_tx_sched(struct adapter *sc)
457 const int n = sc->params.nsched_cls;
458 struct port_info *pi;
460 mtx_init(&sc->tc_lock, "tx_sched lock", NULL, MTX_DEF);
461 TASK_INIT(&sc->tc_task, 0, update_tx_sched, sc);
462 for_each_port(sc, i) {
464 pi->sched_params = malloc(sizeof(*pi->sched_params) +
465 n * sizeof(struct tx_cl_rl_params), M_CXGBE, M_ZERO | M_WAITOK);
472 t4_free_tx_sched(struct adapter *sc)
476 taskqueue_drain(taskqueue_thread, &sc->tc_task);
478 for_each_port(sc, i) {
479 if (sc->port[i] != NULL)
480 free(sc->port[i]->sched_params, M_CXGBE);
483 if (mtx_initialized(&sc->tc_lock))
484 mtx_destroy(&sc->tc_lock);
490 t4_update_tx_sched(struct adapter *sc)
493 taskqueue_enqueue(taskqueue_thread, &sc->tc_task);
497 t4_reserve_cl_rl_kbps(struct adapter *sc, int port_id, u_int maxrate,
500 int rc = 0, fa, fa2, i, pktsize, burstsize;
502 struct tx_cl_rl_params *tc;
503 struct port_info *pi;
505 MPASS(port_id >= 0 && port_id < sc->params.nports);
507 pi = sc->port[port_id];
508 if (pi->sched_params->pktsize > 0)
509 pktsize = pi->sched_params->pktsize;
511 pktsize = if_getmtu(pi->vi[0].ifp);
512 if (pi->sched_params->burstsize > 0)
513 burstsize = pi->sched_params->burstsize;
515 burstsize = pktsize * 4;
516 tc = &pi->sched_params->cl_rl[0];
520 mtx_lock(&sc->tc_lock);
521 for (i = 0; i < sc->params.nsched_cls; i++, tc++) {
522 if (tc->state >= CS_PARAMS_SET &&
523 tc->ratemode == FW_SCHED_PARAMS_RATE_ABS &&
524 tc->rateunit == FW_SCHED_PARAMS_UNIT_BITRATE &&
525 tc->mode == FW_SCHED_PARAMS_MODE_FLOW &&
526 tc->maxrate == maxrate && tc->pktsize == pktsize &&
527 tc->burstsize == burstsize) {
530 if (tc->state == CS_PARAMS_SET) {
531 tc->state = CS_HW_UPDATE_REQUESTED;
537 if (fa < 0 && tc->state == CS_UNINITIALIZED) {
538 MPASS(tc->refcount == 0);
539 fa = i; /* first available, never used. */
541 if (fa2 < 0 && tc->refcount == 0 && !(tc->flags & CF_USER)) {
542 fa2 = i; /* first available, used previously. */
546 MPASS(i == sc->params.nsched_cls);
553 MPASS(fa >= 0 && fa < sc->params.nsched_cls);
554 tc = &pi->sched_params->cl_rl[fa];
555 MPASS(!(tc->flags & CF_USER));
556 MPASS(tc->refcount == 0);
559 tc->state = CS_HW_UPDATE_REQUESTED;
560 tc->ratemode = FW_SCHED_PARAMS_RATE_ABS;
561 tc->rateunit = FW_SCHED_PARAMS_UNIT_BITRATE;
562 tc->mode = FW_SCHED_PARAMS_MODE_FLOW;
563 tc->maxrate = maxrate;
564 tc->pktsize = pktsize;
565 tc->burstsize = burstsize;
570 mtx_unlock(&sc->tc_lock);
572 t4_update_tx_sched(sc);
577 t4_release_cl_rl(struct adapter *sc, int port_id, int tc_idx)
579 struct tx_cl_rl_params *tc;
581 MPASS(port_id >= 0 && port_id < sc->params.nports);
582 MPASS(tc_idx >= 0 && tc_idx < sc->params.nsched_cls);
584 mtx_lock(&sc->tc_lock);
585 tc = &sc->port[port_id]->sched_params->cl_rl[tc_idx];
586 MPASS(tc->refcount > 0);
588 mtx_unlock(&sc->tc_lock);
592 sysctl_tc(SYSCTL_HANDLER_ARGS)
594 struct vi_info *vi = arg1;
595 struct adapter *sc = vi->adapter;
597 int qidx = arg2, rc, tc_idx;
599 MPASS(qidx >= vi->first_txq && qidx < vi->first_txq + vi->ntxq);
601 txq = &sc->sge.txq[qidx];
602 tc_idx = txq->tc_idx;
603 rc = sysctl_handle_int(oidp, &tc_idx, 0, req);
604 if (rc != 0 || req->newptr == NULL)
607 if (sc->flags & IS_VF)
609 if (!in_range(tc_idx, 0, sc->params.nsched_cls - 1))
612 return (bind_txq_to_traffic_class(sc, txq, tc_idx));
616 sysctl_tc_params(SYSCTL_HANDLER_ARGS)
618 struct adapter *sc = arg1;
619 struct tx_cl_rl_params tc;
621 int i, rc, port_id, mbps, gbps;
623 rc = sysctl_wire_old_buffer(req, 0);
627 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
631 port_id = arg2 >> 16;
632 MPASS(port_id < sc->params.nports);
633 MPASS(sc->port[port_id] != NULL);
635 MPASS(i < sc->params.nsched_cls);
637 mtx_lock(&sc->tc_lock);
638 tc = sc->port[port_id]->sched_params->cl_rl[i];
639 mtx_unlock(&sc->tc_lock);
641 if (tc.state < CS_PARAMS_SET) {
642 sbuf_printf(sb, "uninitialized");
646 switch (tc.rateunit) {
647 case SCHED_CLASS_RATEUNIT_BITS:
648 switch (tc.ratemode) {
649 case SCHED_CLASS_RATEMODE_REL:
650 /* XXX: top speed or actual link speed? */
651 gbps = port_top_speed(sc->port[port_id]);
652 sbuf_printf(sb, "%u%% of %uGbps", tc.maxrate, gbps);
654 case SCHED_CLASS_RATEMODE_ABS:
655 mbps = tc.maxrate / 1000;
656 gbps = tc.maxrate / 1000000;
657 if (tc.maxrate == gbps * 1000000)
658 sbuf_printf(sb, "%uGbps", gbps);
659 else if (tc.maxrate == mbps * 1000)
660 sbuf_printf(sb, "%uMbps", mbps);
662 sbuf_printf(sb, "%uKbps", tc.maxrate);
669 case SCHED_CLASS_RATEUNIT_PKTS:
670 sbuf_printf(sb, "%upps", tc.maxrate);
678 case SCHED_CLASS_MODE_CLASS:
679 /* Note that pktsize and burstsize are not used in this mode. */
680 sbuf_printf(sb, " aggregate");
682 case SCHED_CLASS_MODE_FLOW:
683 sbuf_printf(sb, " per-flow");
685 sbuf_printf(sb, " pkt-size %u", tc.pktsize);
686 if (tc.burstsize > 0)
687 sbuf_printf(sb, " burst-size %u", tc.burstsize);
696 rc = sbuf_finish(sb);
704 t4_init_etid_table(struct adapter *sc)
709 if (!is_ethoffload(sc))
713 MPASS(t->netids > 0);
715 mtx_init(&t->etid_lock, "etid lock", NULL, MTX_DEF);
716 t->etid_tab = malloc(sizeof(*t->etid_tab) * t->netids, M_CXGBE,
718 t->efree = t->etid_tab;
720 for (i = 1; i < t->netids; i++)
721 t->etid_tab[i - 1].next = &t->etid_tab[i];
722 t->etid_tab[t->netids - 1].next = NULL;
726 t4_free_etid_table(struct adapter *sc)
730 if (!is_ethoffload(sc))
734 MPASS(t->netids > 0);
736 free(t->etid_tab, M_CXGBE);
739 if (mtx_initialized(&t->etid_lock))
740 mtx_destroy(&t->etid_lock);
744 static int alloc_etid(struct adapter *, struct cxgbe_rate_tag *);
745 static void free_etid(struct adapter *, int);
748 alloc_etid(struct adapter *sc, struct cxgbe_rate_tag *cst)
750 struct tid_info *t = &sc->tids;
753 mtx_lock(&t->etid_lock);
755 union etid_entry *p = t->efree;
757 etid = p - t->etid_tab + t->etid_base;
762 mtx_unlock(&t->etid_lock);
766 struct cxgbe_rate_tag *
767 lookup_etid(struct adapter *sc, int etid)
769 struct tid_info *t = &sc->tids;
771 return (t->etid_tab[etid - t->etid_base].cst);
775 free_etid(struct adapter *sc, int etid)
777 struct tid_info *t = &sc->tids;
778 union etid_entry *p = &t->etid_tab[etid - t->etid_base];
780 mtx_lock(&t->etid_lock);
784 mtx_unlock(&t->etid_lock);
787 static int cxgbe_rate_tag_modify(struct m_snd_tag *,
788 union if_snd_tag_modify_params *);
789 static int cxgbe_rate_tag_query(struct m_snd_tag *,
790 union if_snd_tag_query_params *);
791 static void cxgbe_rate_tag_free(struct m_snd_tag *);
793 static const struct if_snd_tag_sw cxgbe_rate_tag_sw = {
794 .snd_tag_modify = cxgbe_rate_tag_modify,
795 .snd_tag_query = cxgbe_rate_tag_query,
796 .snd_tag_free = cxgbe_rate_tag_free,
797 .type = IF_SND_TAG_TYPE_RATE_LIMIT
801 cxgbe_rate_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
802 struct m_snd_tag **pt)
805 struct vi_info *vi = if_getsoftc(ifp);
806 struct port_info *pi = vi->pi;
807 struct adapter *sc = pi->adapter;
808 struct cxgbe_rate_tag *cst;
810 MPASS(params->hdr.type == IF_SND_TAG_TYPE_RATE_LIMIT);
812 rc = t4_reserve_cl_rl_kbps(sc, pi->port_id,
813 (params->rate_limit.max_rate * 8ULL / 1000), &schedcl);
816 MPASS(schedcl >= 0 && schedcl < sc->params.nsched_cls);
818 cst = malloc(sizeof(*cst), M_CXGBE, M_ZERO | M_NOWAIT);
821 t4_release_cl_rl(sc, pi->port_id, schedcl);
825 cst->etid = alloc_etid(sc, cst);
831 mtx_init(&cst->lock, "cst_lock", NULL, MTX_DEF);
832 mbufq_init(&cst->pending_tx, INT_MAX);
833 mbufq_init(&cst->pending_fwack, INT_MAX);
834 m_snd_tag_init(&cst->com, ifp, &cxgbe_rate_tag_sw);
835 cst->flags |= EO_FLOWC_PENDING | EO_SND_TAG_REF;
837 cst->port_id = pi->port_id;
838 cst->schedcl = schedcl;
839 cst->max_rate = params->rate_limit.max_rate;
840 cst->tx_credits = sc->params.eo_wr_cred;
841 cst->tx_total = cst->tx_credits;
843 cst->ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
844 V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
845 V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
848 * Queues will be selected later when the connection flowid is available.
856 * Change in parameters, no change in ifp.
859 cxgbe_rate_tag_modify(struct m_snd_tag *mst,
860 union if_snd_tag_modify_params *params)
863 struct cxgbe_rate_tag *cst = mst_to_crt(mst);
864 struct adapter *sc = cst->adapter;
866 /* XXX: is schedcl -1 ok here? */
867 MPASS(cst->schedcl >= 0 && cst->schedcl < sc->params.nsched_cls);
869 mtx_lock(&cst->lock);
870 MPASS(cst->flags & EO_SND_TAG_REF);
871 rc = t4_reserve_cl_rl_kbps(sc, cst->port_id,
872 (params->rate_limit.max_rate * 8ULL / 1000), &schedcl);
875 MPASS(schedcl >= 0 && schedcl < sc->params.nsched_cls);
876 t4_release_cl_rl(sc, cst->port_id, cst->schedcl);
877 cst->schedcl = schedcl;
878 cst->max_rate = params->rate_limit.max_rate;
879 mtx_unlock(&cst->lock);
885 cxgbe_rate_tag_query(struct m_snd_tag *mst,
886 union if_snd_tag_query_params *params)
888 struct cxgbe_rate_tag *cst = mst_to_crt(mst);
890 params->rate_limit.max_rate = cst->max_rate;
892 #define CST_TO_MST_QLEVEL_SCALE (IF_SND_QUEUE_LEVEL_MAX / cst->tx_total)
893 params->rate_limit.queue_level =
894 (cst->tx_total - cst->tx_credits) * CST_TO_MST_QLEVEL_SCALE;
900 * Unlocks cst and frees it.
903 cxgbe_rate_tag_free_locked(struct cxgbe_rate_tag *cst)
905 struct adapter *sc = cst->adapter;
907 mtx_assert(&cst->lock, MA_OWNED);
908 MPASS((cst->flags & EO_SND_TAG_REF) == 0);
909 MPASS(cst->tx_credits == cst->tx_total);
910 MPASS(cst->plen == 0);
911 MPASS(mbufq_first(&cst->pending_tx) == NULL);
912 MPASS(mbufq_first(&cst->pending_fwack) == NULL);
915 free_etid(sc, cst->etid);
916 if (cst->schedcl != -1)
917 t4_release_cl_rl(sc, cst->port_id, cst->schedcl);
918 mtx_unlock(&cst->lock);
919 mtx_destroy(&cst->lock);
924 cxgbe_rate_tag_free(struct m_snd_tag *mst)
926 struct cxgbe_rate_tag *cst = mst_to_crt(mst);
928 mtx_lock(&cst->lock);
930 /* The kernel is done with the snd_tag. Remove its reference. */
931 MPASS(cst->flags & EO_SND_TAG_REF);
932 cst->flags &= ~EO_SND_TAG_REF;
934 if (cst->ncompl == 0) {
936 * No fw4_ack in flight. Free the tag right away if there are
937 * no outstanding credits. Request the firmware to return all
938 * credits for the etid otherwise.
940 if (cst->tx_credits == cst->tx_total) {
941 cxgbe_rate_tag_free_locked(cst);
942 return; /* cst is gone. */
944 send_etid_flush_wr(cst);
946 mtx_unlock(&cst->lock);
950 cxgbe_ratelimit_query(if_t ifp, struct if_ratelimit_query_results *q)
952 struct vi_info *vi = if_getsoftc(ifp);
953 struct adapter *sc = vi->adapter;
955 q->rate_table = NULL;
956 q->flags = RT_IS_SELECTABLE;
958 * Absolute max limits from the firmware configuration. Practical
959 * limits depend on the burstsize, pktsize (if_getmtu(ifp) ultimately) and
962 q->max_flows = sc->tids.netids;
963 q->number_of_rates = sc->params.nsched_cls;
964 q->min_segment_burst = 4; /* matches PKTSCHED_BURST in the firmware. */
967 if (chip_id(sc) < CHELSIO_T6) {
968 /* Based on testing by rrs@ with a T580 at burstsize = 4. */
969 MPASS(q->min_segment_burst == 4);
970 q->max_flows = min(4000, q->max_flows);
972 /* XXX: TBD, carried forward from T5 for now. */
973 q->max_flows = min(4000, q->max_flows);
977 * XXX: tcp_ratelimit.c grabs all available rates on link-up before it
978 * even knows whether hw pacing will be used or not. This prevents
979 * other consumers like SO_MAX_PACING_RATE or those using cxgbetool or
980 * the private ioctls from using any of traffic classes.
982 * Underreport the number of rates to tcp_ratelimit so that it doesn't
983 * hog all of them. This can be removed if/when tcp_ratelimit switches
984 * to making its allocations on first-use rather than link-up. There is
985 * nothing wrong with one particular consumer reserving all the classes
986 * but it should do so only if it'll actually use hw rate limiting.
988 q->number_of_rates /= 4;