2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
4 * Copyright (c) 2016 Chelsio Communications, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/module.h>
36 #include <linux/netdevice.h>
41 static int t4_sched_class_fw_cmd(struct port_info *pi,
42 struct ch_sched_params *p,
45 struct adapter *adap = pi->adapter;
46 struct sched_table *s = pi->sched_tbl;
47 struct sched_class *e;
50 e = &s->tab[p->u.params.class];
54 err = t4_sched_params(adap, p->type,
55 p->u.params.level, p->u.params.mode,
58 p->u.params.channel, e->idx,
59 p->u.params.minrate, p->u.params.maxrate,
60 p->u.params.weight, p->u.params.pktsize);
70 static int t4_sched_bind_unbind_op(struct port_info *pi, void *arg,
71 enum sched_bind_type type, bool bind)
73 struct adapter *adap = pi->adapter;
74 u32 fw_mnem, fw_class, fw_param;
75 unsigned int pf = adap->pf;
81 struct sched_queue_entry *qe;
83 qe = (struct sched_queue_entry *)arg;
85 /* Create a template for the FW_PARAMS_CMD mnemonic and
86 * value (TX Scheduling Class in this case).
88 fw_mnem = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
90 FW_PARAMS_PARAM_DMAQ_EQ_SCHEDCLASS_ETH));
91 fw_class = bind ? qe->param.class : FW_SCHED_CLS_NONE;
92 fw_param = (fw_mnem | FW_PARAMS_PARAM_YZ_V(qe->cntxt_id));
97 err = t4_set_params(adap, adap->mbox, pf, vf, 1,
98 &fw_param, &fw_class);
102 struct sched_flowc_entry *fe;
104 fe = (struct sched_flowc_entry *)arg;
106 fw_class = bind ? fe->param.class : FW_SCHED_CLS_NONE;
107 err = cxgb4_ethofld_send_flowc(adap->port[pi->port_id],
108 fe->param.tid, fw_class);
119 static void *t4_sched_entry_lookup(struct port_info *pi,
120 enum sched_bind_type type,
123 struct sched_table *s = pi->sched_tbl;
124 struct sched_class *e, *end;
127 /* Look for an entry with matching @val */
128 end = &s->tab[s->sched_size];
129 for (e = &s->tab[0]; e != end; ++e) {
130 if (e->state == SCHED_STATE_UNUSED ||
131 e->bind_type != type)
136 struct sched_queue_entry *qe;
138 list_for_each_entry(qe, &e->entry_list, list) {
139 if (qe->cntxt_id == val) {
147 struct sched_flowc_entry *fe;
149 list_for_each_entry(fe, &e->entry_list, list) {
150 if (fe->param.tid == val) {
168 struct sched_class *cxgb4_sched_queue_lookup(struct net_device *dev,
169 struct ch_sched_queue *p)
171 struct port_info *pi = netdev2pinfo(dev);
172 struct sched_queue_entry *qe = NULL;
173 struct adapter *adap = pi->adapter;
174 struct sge_eth_txq *txq;
176 if (p->queue < 0 || p->queue >= pi->nqsets)
179 txq = &adap->sge.ethtxq[pi->first_qset + p->queue];
180 qe = t4_sched_entry_lookup(pi, SCHED_QUEUE, txq->q.cntxt_id);
181 return qe ? &pi->sched_tbl->tab[qe->param.class] : NULL;
184 static int t4_sched_queue_unbind(struct port_info *pi, struct ch_sched_queue *p)
186 struct sched_queue_entry *qe = NULL;
187 struct adapter *adap = pi->adapter;
188 struct sge_eth_txq *txq;
189 struct sched_class *e;
192 if (p->queue < 0 || p->queue >= pi->nqsets)
195 txq = &adap->sge.ethtxq[pi->first_qset + p->queue];
197 /* Find the existing entry that the queue is bound to */
198 qe = t4_sched_entry_lookup(pi, SCHED_QUEUE, txq->q.cntxt_id);
200 err = t4_sched_bind_unbind_op(pi, (void *)qe, SCHED_QUEUE,
205 e = &pi->sched_tbl->tab[qe->param.class];
208 if (atomic_dec_and_test(&e->refcnt))
209 cxgb4_sched_class_free(adap->port[pi->port_id], e->idx);
214 static int t4_sched_queue_bind(struct port_info *pi, struct ch_sched_queue *p)
216 struct sched_table *s = pi->sched_tbl;
217 struct sched_queue_entry *qe = NULL;
218 struct adapter *adap = pi->adapter;
219 struct sge_eth_txq *txq;
220 struct sched_class *e;
224 if (p->queue < 0 || p->queue >= pi->nqsets)
227 qe = kvzalloc(sizeof(struct sched_queue_entry), GFP_KERNEL);
231 txq = &adap->sge.ethtxq[pi->first_qset + p->queue];
232 qid = txq->q.cntxt_id;
234 /* Unbind queue from any existing class */
235 err = t4_sched_queue_unbind(pi, p);
239 /* Bind queue to specified class */
241 memcpy(&qe->param, p, sizeof(qe->param));
243 e = &s->tab[qe->param.class];
244 err = t4_sched_bind_unbind_op(pi, (void *)qe, SCHED_QUEUE, true);
248 list_add_tail(&qe->list, &e->entry_list);
249 e->bind_type = SCHED_QUEUE;
250 atomic_inc(&e->refcnt);
258 static int t4_sched_flowc_unbind(struct port_info *pi, struct ch_sched_flowc *p)
260 struct sched_flowc_entry *fe = NULL;
261 struct adapter *adap = pi->adapter;
262 struct sched_class *e;
265 if (p->tid < 0 || p->tid >= adap->tids.neotids)
268 /* Find the existing entry that the flowc is bound to */
269 fe = t4_sched_entry_lookup(pi, SCHED_FLOWC, p->tid);
271 err = t4_sched_bind_unbind_op(pi, (void *)fe, SCHED_FLOWC,
276 e = &pi->sched_tbl->tab[fe->param.class];
279 if (atomic_dec_and_test(&e->refcnt))
280 cxgb4_sched_class_free(adap->port[pi->port_id], e->idx);
285 static int t4_sched_flowc_bind(struct port_info *pi, struct ch_sched_flowc *p)
287 struct sched_table *s = pi->sched_tbl;
288 struct sched_flowc_entry *fe = NULL;
289 struct adapter *adap = pi->adapter;
290 struct sched_class *e;
293 if (p->tid < 0 || p->tid >= adap->tids.neotids)
296 fe = kvzalloc(sizeof(*fe), GFP_KERNEL);
300 /* Unbind flowc from any existing class */
301 err = t4_sched_flowc_unbind(pi, p);
305 /* Bind flowc to specified class */
306 memcpy(&fe->param, p, sizeof(fe->param));
308 e = &s->tab[fe->param.class];
309 err = t4_sched_bind_unbind_op(pi, (void *)fe, SCHED_FLOWC, true);
313 list_add_tail(&fe->list, &e->entry_list);
314 e->bind_type = SCHED_FLOWC;
315 atomic_inc(&e->refcnt);
323 static void t4_sched_class_unbind_all(struct port_info *pi,
324 struct sched_class *e,
325 enum sched_bind_type type)
332 struct sched_queue_entry *qe;
334 list_for_each_entry(qe, &e->entry_list, list)
335 t4_sched_queue_unbind(pi, &qe->param);
339 struct sched_flowc_entry *fe;
341 list_for_each_entry(fe, &e->entry_list, list)
342 t4_sched_flowc_unbind(pi, &fe->param);
350 static int t4_sched_class_bind_unbind_op(struct port_info *pi, void *arg,
351 enum sched_bind_type type, bool bind)
360 struct ch_sched_queue *qe = (struct ch_sched_queue *)arg;
363 err = t4_sched_queue_bind(pi, qe);
365 err = t4_sched_queue_unbind(pi, qe);
369 struct ch_sched_flowc *fe = (struct ch_sched_flowc *)arg;
372 err = t4_sched_flowc_bind(pi, fe);
374 err = t4_sched_flowc_unbind(pi, fe);
386 * cxgb4_sched_class_bind - Bind an entity to a scheduling class
387 * @dev: net_device pointer
388 * @arg: Entity opaque data
389 * @type: Entity type (Queue)
391 * Binds an entity (queue) to a scheduling class. If the entity
392 * is bound to another class, it will be unbound from the other class
393 * and bound to the class specified in @arg.
395 int cxgb4_sched_class_bind(struct net_device *dev, void *arg,
396 enum sched_bind_type type)
398 struct port_info *pi = netdev2pinfo(dev);
409 struct ch_sched_queue *qe = (struct ch_sched_queue *)arg;
411 class_id = qe->class;
415 struct ch_sched_flowc *fe = (struct ch_sched_flowc *)arg;
417 class_id = fe->class;
424 if (!valid_class_id(dev, class_id))
427 if (class_id == SCHED_CLS_NONE)
430 return t4_sched_class_bind_unbind_op(pi, arg, type, true);
435 * cxgb4_sched_class_unbind - Unbind an entity from a scheduling class
436 * @dev: net_device pointer
437 * @arg: Entity opaque data
438 * @type: Entity type (Queue)
440 * Unbinds an entity (queue) from a scheduling class.
442 int cxgb4_sched_class_unbind(struct net_device *dev, void *arg,
443 enum sched_bind_type type)
445 struct port_info *pi = netdev2pinfo(dev);
456 struct ch_sched_queue *qe = (struct ch_sched_queue *)arg;
458 class_id = qe->class;
462 struct ch_sched_flowc *fe = (struct ch_sched_flowc *)arg;
464 class_id = fe->class;
471 if (!valid_class_id(dev, class_id))
474 return t4_sched_class_bind_unbind_op(pi, arg, type, false);
477 /* If @p is NULL, fetch any available unused class */
478 static struct sched_class *t4_sched_class_lookup(struct port_info *pi,
479 const struct ch_sched_params *p)
481 struct sched_table *s = pi->sched_tbl;
482 struct sched_class *found = NULL;
483 struct sched_class *e, *end;
486 /* Get any available unused class */
487 end = &s->tab[s->sched_size];
488 for (e = &s->tab[0]; e != end; ++e) {
489 if (e->state == SCHED_STATE_UNUSED) {
495 /* Look for a class with matching scheduling parameters */
496 struct ch_sched_params info;
497 struct ch_sched_params tp;
499 memcpy(&tp, p, sizeof(tp));
500 /* Don't try to match class parameter */
501 tp.u.params.class = SCHED_CLS_NONE;
503 end = &s->tab[s->sched_size];
504 for (e = &s->tab[0]; e != end; ++e) {
505 if (e->state == SCHED_STATE_UNUSED)
508 memcpy(&info, &e->info, sizeof(info));
509 /* Don't try to match class parameter */
510 info.u.params.class = SCHED_CLS_NONE;
512 if ((info.type == tp.type) &&
513 (!memcmp(&info.u.params, &tp.u.params,
514 sizeof(info.u.params)))) {
524 static struct sched_class *t4_sched_class_alloc(struct port_info *pi,
525 struct ch_sched_params *p)
527 struct sched_class *e = NULL;
534 class_id = p->u.params.class;
536 /* Only accept search for existing class with matching params
537 * or allocation of new class with specified params
539 if (class_id != SCHED_CLS_NONE)
542 /* See if there's an exisiting class with same requested sched
543 * params. Classes can only be shared among FLOWC types. For
544 * other types, always request a new class.
546 if (p->u.params.mode == SCHED_CLASS_MODE_FLOW)
547 e = t4_sched_class_lookup(pi, p);
550 struct ch_sched_params np;
552 /* Fetch any available unused class */
553 e = t4_sched_class_lookup(pi, NULL);
557 memcpy(&np, p, sizeof(np));
558 np.u.params.class = e->idx;
560 err = t4_sched_class_fw_cmd(pi, &np, SCHED_FW_OP_ADD);
563 memcpy(&e->info, &np, sizeof(e->info));
564 atomic_set(&e->refcnt, 0);
565 e->state = SCHED_STATE_ACTIVE;
572 * cxgb4_sched_class_alloc - allocate a scheduling class
573 * @dev: net_device pointer
574 * @p: new scheduling class to create.
576 * Returns pointer to the scheduling class created. If @p is NULL, then
577 * it allocates and returns any available unused scheduling class. If a
578 * scheduling class with matching @p is found, then the matching class is
581 struct sched_class *cxgb4_sched_class_alloc(struct net_device *dev,
582 struct ch_sched_params *p)
584 struct port_info *pi = netdev2pinfo(dev);
590 class_id = p->u.params.class;
591 if (!valid_class_id(dev, class_id))
594 return t4_sched_class_alloc(pi, p);
598 * cxgb4_sched_class_free - free a scheduling class
599 * @dev: net_device pointer
600 * @e: scheduling class
602 * Frees a scheduling class if there are no users.
604 void cxgb4_sched_class_free(struct net_device *dev, u8 classid)
606 struct port_info *pi = netdev2pinfo(dev);
607 struct sched_table *s = pi->sched_tbl;
608 struct ch_sched_params p;
609 struct sched_class *e;
613 e = &s->tab[classid];
614 if (!atomic_read(&e->refcnt) && e->state != SCHED_STATE_UNUSED) {
615 /* Port based rate limiting needs explicit reset back
616 * to max rate. But, we'll do explicit reset for all
617 * types, instead of just port based type, to be on
620 memcpy(&p, &e->info, sizeof(p));
621 /* Always reset mode to 0. Otherwise, FLOWC mode will
622 * still be enabled even after resetting the traffic
626 p.u.params.minrate = 0;
627 p.u.params.pktsize = 0;
629 ret = t4_get_link_params(pi, NULL, &speed, NULL);
631 p.u.params.maxrate = speed * 1000; /* Mbps to Kbps */
633 p.u.params.maxrate = SCHED_MAX_RATE_KBPS;
635 t4_sched_class_fw_cmd(pi, &p, SCHED_FW_OP_DEL);
637 e->state = SCHED_STATE_UNUSED;
638 memset(&e->info, 0, sizeof(e->info));
642 static void t4_sched_class_free(struct net_device *dev, struct sched_class *e)
644 struct port_info *pi = netdev2pinfo(dev);
646 t4_sched_class_unbind_all(pi, e, e->bind_type);
647 cxgb4_sched_class_free(dev, e->idx);
650 struct sched_table *t4_init_sched(unsigned int sched_size)
652 struct sched_table *s;
655 s = kvzalloc(struct_size(s, tab, sched_size), GFP_KERNEL);
659 s->sched_size = sched_size;
661 for (i = 0; i < s->sched_size; i++) {
662 memset(&s->tab[i], 0, sizeof(struct sched_class));
664 s->tab[i].state = SCHED_STATE_UNUSED;
665 INIT_LIST_HEAD(&s->tab[i].entry_list);
666 atomic_set(&s->tab[i].refcnt, 0);
671 void t4_cleanup_sched(struct adapter *adap)
673 struct sched_table *s;
676 for_each_port(adap, j) {
677 struct port_info *pi = netdev2pinfo(adap->port[j]);
683 for (i = 0; i < s->sched_size; i++) {
684 struct sched_class *e;
687 if (e->state == SCHED_STATE_ACTIVE)
688 t4_sched_class_free(adap->port[j], e);