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,
61 p->u.params.burstsize);
71 static int t4_sched_bind_unbind_op(struct port_info *pi, void *arg,
72 enum sched_bind_type type, bool bind)
74 struct adapter *adap = pi->adapter;
75 u32 fw_mnem, fw_class, fw_param;
76 unsigned int pf = adap->pf;
82 struct sched_queue_entry *qe;
84 qe = (struct sched_queue_entry *)arg;
86 /* Create a template for the FW_PARAMS_CMD mnemonic and
87 * value (TX Scheduling Class in this case).
89 fw_mnem = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
91 FW_PARAMS_PARAM_DMAQ_EQ_SCHEDCLASS_ETH));
92 fw_class = bind ? qe->param.class : FW_SCHED_CLS_NONE;
93 fw_param = (fw_mnem | FW_PARAMS_PARAM_YZ_V(qe->cntxt_id));
98 err = t4_set_params(adap, adap->mbox, pf, vf, 1,
99 &fw_param, &fw_class);
103 struct sched_flowc_entry *fe;
105 fe = (struct sched_flowc_entry *)arg;
107 fw_class = bind ? fe->param.class : FW_SCHED_CLS_NONE;
108 err = cxgb4_ethofld_send_flowc(adap->port[pi->port_id],
109 fe->param.tid, fw_class);
120 static void *t4_sched_entry_lookup(struct port_info *pi,
121 enum sched_bind_type type,
124 struct sched_table *s = pi->sched_tbl;
125 struct sched_class *e, *end;
128 /* Look for an entry with matching @val */
129 end = &s->tab[s->sched_size];
130 for (e = &s->tab[0]; e != end; ++e) {
131 if (e->state == SCHED_STATE_UNUSED ||
132 e->bind_type != type)
137 struct sched_queue_entry *qe;
139 list_for_each_entry(qe, &e->entry_list, list) {
140 if (qe->cntxt_id == val) {
148 struct sched_flowc_entry *fe;
150 list_for_each_entry(fe, &e->entry_list, list) {
151 if (fe->param.tid == val) {
169 struct sched_class *cxgb4_sched_queue_lookup(struct net_device *dev,
170 struct ch_sched_queue *p)
172 struct port_info *pi = netdev2pinfo(dev);
173 struct sched_queue_entry *qe = NULL;
174 struct adapter *adap = pi->adapter;
175 struct sge_eth_txq *txq;
177 if (p->queue < 0 || p->queue >= pi->nqsets)
180 txq = &adap->sge.ethtxq[pi->first_qset + p->queue];
181 qe = t4_sched_entry_lookup(pi, SCHED_QUEUE, txq->q.cntxt_id);
182 return qe ? &pi->sched_tbl->tab[qe->param.class] : NULL;
185 static int t4_sched_queue_unbind(struct port_info *pi, struct ch_sched_queue *p)
187 struct sched_queue_entry *qe = NULL;
188 struct adapter *adap = pi->adapter;
189 struct sge_eth_txq *txq;
190 struct sched_class *e;
193 if (p->queue < 0 || p->queue >= pi->nqsets)
196 txq = &adap->sge.ethtxq[pi->first_qset + p->queue];
198 /* Find the existing entry that the queue is bound to */
199 qe = t4_sched_entry_lookup(pi, SCHED_QUEUE, txq->q.cntxt_id);
201 err = t4_sched_bind_unbind_op(pi, (void *)qe, SCHED_QUEUE,
206 e = &pi->sched_tbl->tab[qe->param.class];
209 if (atomic_dec_and_test(&e->refcnt))
210 cxgb4_sched_class_free(adap->port[pi->port_id], e->idx);
215 static int t4_sched_queue_bind(struct port_info *pi, struct ch_sched_queue *p)
217 struct sched_table *s = pi->sched_tbl;
218 struct sched_queue_entry *qe = NULL;
219 struct adapter *adap = pi->adapter;
220 struct sge_eth_txq *txq;
221 struct sched_class *e;
225 if (p->queue < 0 || p->queue >= pi->nqsets)
228 qe = kvzalloc(sizeof(struct sched_queue_entry), GFP_KERNEL);
232 txq = &adap->sge.ethtxq[pi->first_qset + p->queue];
233 qid = txq->q.cntxt_id;
235 /* Unbind queue from any existing class */
236 err = t4_sched_queue_unbind(pi, p);
240 /* Bind queue to specified class */
242 memcpy(&qe->param, p, sizeof(qe->param));
244 e = &s->tab[qe->param.class];
245 err = t4_sched_bind_unbind_op(pi, (void *)qe, SCHED_QUEUE, true);
249 list_add_tail(&qe->list, &e->entry_list);
250 e->bind_type = SCHED_QUEUE;
251 atomic_inc(&e->refcnt);
259 static int t4_sched_flowc_unbind(struct port_info *pi, struct ch_sched_flowc *p)
261 struct sched_flowc_entry *fe = NULL;
262 struct adapter *adap = pi->adapter;
263 struct sched_class *e;
266 if (p->tid < 0 || p->tid >= adap->tids.neotids)
269 /* Find the existing entry that the flowc is bound to */
270 fe = t4_sched_entry_lookup(pi, SCHED_FLOWC, p->tid);
272 err = t4_sched_bind_unbind_op(pi, (void *)fe, SCHED_FLOWC,
277 e = &pi->sched_tbl->tab[fe->param.class];
280 if (atomic_dec_and_test(&e->refcnt))
281 cxgb4_sched_class_free(adap->port[pi->port_id], e->idx);
286 static int t4_sched_flowc_bind(struct port_info *pi, struct ch_sched_flowc *p)
288 struct sched_table *s = pi->sched_tbl;
289 struct sched_flowc_entry *fe = NULL;
290 struct adapter *adap = pi->adapter;
291 struct sched_class *e;
294 if (p->tid < 0 || p->tid >= adap->tids.neotids)
297 fe = kvzalloc(sizeof(*fe), GFP_KERNEL);
301 /* Unbind flowc from any existing class */
302 err = t4_sched_flowc_unbind(pi, p);
306 /* Bind flowc to specified class */
307 memcpy(&fe->param, p, sizeof(fe->param));
309 e = &s->tab[fe->param.class];
310 err = t4_sched_bind_unbind_op(pi, (void *)fe, SCHED_FLOWC, true);
314 list_add_tail(&fe->list, &e->entry_list);
315 e->bind_type = SCHED_FLOWC;
316 atomic_inc(&e->refcnt);
324 static void t4_sched_class_unbind_all(struct port_info *pi,
325 struct sched_class *e,
326 enum sched_bind_type type)
333 struct sched_queue_entry *qe;
335 list_for_each_entry(qe, &e->entry_list, list)
336 t4_sched_queue_unbind(pi, &qe->param);
340 struct sched_flowc_entry *fe;
342 list_for_each_entry(fe, &e->entry_list, list)
343 t4_sched_flowc_unbind(pi, &fe->param);
351 static int t4_sched_class_bind_unbind_op(struct port_info *pi, void *arg,
352 enum sched_bind_type type, bool bind)
361 struct ch_sched_queue *qe = (struct ch_sched_queue *)arg;
364 err = t4_sched_queue_bind(pi, qe);
366 err = t4_sched_queue_unbind(pi, qe);
370 struct ch_sched_flowc *fe = (struct ch_sched_flowc *)arg;
373 err = t4_sched_flowc_bind(pi, fe);
375 err = t4_sched_flowc_unbind(pi, fe);
387 * cxgb4_sched_class_bind - Bind an entity to a scheduling class
388 * @dev: net_device pointer
389 * @arg: Entity opaque data
390 * @type: Entity type (Queue)
392 * Binds an entity (queue) to a scheduling class. If the entity
393 * is bound to another class, it will be unbound from the other class
394 * and bound to the class specified in @arg.
396 int cxgb4_sched_class_bind(struct net_device *dev, void *arg,
397 enum sched_bind_type type)
399 struct port_info *pi = netdev2pinfo(dev);
410 struct ch_sched_queue *qe = (struct ch_sched_queue *)arg;
412 class_id = qe->class;
416 struct ch_sched_flowc *fe = (struct ch_sched_flowc *)arg;
418 class_id = fe->class;
425 if (!valid_class_id(dev, class_id))
428 if (class_id == SCHED_CLS_NONE)
431 return t4_sched_class_bind_unbind_op(pi, arg, type, true);
436 * cxgb4_sched_class_unbind - Unbind an entity from a scheduling class
437 * @dev: net_device pointer
438 * @arg: Entity opaque data
439 * @type: Entity type (Queue)
441 * Unbinds an entity (queue) from a scheduling class.
443 int cxgb4_sched_class_unbind(struct net_device *dev, void *arg,
444 enum sched_bind_type type)
446 struct port_info *pi = netdev2pinfo(dev);
457 struct ch_sched_queue *qe = (struct ch_sched_queue *)arg;
459 class_id = qe->class;
463 struct ch_sched_flowc *fe = (struct ch_sched_flowc *)arg;
465 class_id = fe->class;
472 if (!valid_class_id(dev, class_id))
475 return t4_sched_class_bind_unbind_op(pi, arg, type, false);
478 /* If @p is NULL, fetch any available unused class */
479 static struct sched_class *t4_sched_class_lookup(struct port_info *pi,
480 const struct ch_sched_params *p)
482 struct sched_table *s = pi->sched_tbl;
483 struct sched_class *found = NULL;
484 struct sched_class *e, *end;
487 /* Get any available unused class */
488 end = &s->tab[s->sched_size];
489 for (e = &s->tab[0]; e != end; ++e) {
490 if (e->state == SCHED_STATE_UNUSED) {
496 /* Look for a class with matching scheduling parameters */
497 struct ch_sched_params info;
498 struct ch_sched_params tp;
500 memcpy(&tp, p, sizeof(tp));
501 /* Don't try to match class parameter */
502 tp.u.params.class = SCHED_CLS_NONE;
504 end = &s->tab[s->sched_size];
505 for (e = &s->tab[0]; e != end; ++e) {
506 if (e->state == SCHED_STATE_UNUSED)
509 memcpy(&info, &e->info, sizeof(info));
510 /* Don't try to match class parameter */
511 info.u.params.class = SCHED_CLS_NONE;
513 if ((info.type == tp.type) &&
514 (!memcmp(&info.u.params, &tp.u.params,
515 sizeof(info.u.params)))) {
525 static struct sched_class *t4_sched_class_alloc(struct port_info *pi,
526 struct ch_sched_params *p)
528 struct sched_class *e = NULL;
535 class_id = p->u.params.class;
537 /* Only accept search for existing class with matching params
538 * or allocation of new class with specified params
540 if (class_id != SCHED_CLS_NONE)
543 /* See if there's an exisiting class with same requested sched
544 * params. Classes can only be shared among FLOWC types. For
545 * other types, always request a new class.
547 if (p->u.params.mode == SCHED_CLASS_MODE_FLOW)
548 e = t4_sched_class_lookup(pi, p);
551 struct ch_sched_params np;
553 /* Fetch any available unused class */
554 e = t4_sched_class_lookup(pi, NULL);
558 memcpy(&np, p, sizeof(np));
559 np.u.params.class = e->idx;
561 err = t4_sched_class_fw_cmd(pi, &np, SCHED_FW_OP_ADD);
564 memcpy(&e->info, &np, sizeof(e->info));
565 atomic_set(&e->refcnt, 0);
566 e->state = SCHED_STATE_ACTIVE;
573 * cxgb4_sched_class_alloc - allocate a scheduling class
574 * @dev: net_device pointer
575 * @p: new scheduling class to create.
577 * Returns pointer to the scheduling class created. If @p is NULL, then
578 * it allocates and returns any available unused scheduling class. If a
579 * scheduling class with matching @p is found, then the matching class is
582 struct sched_class *cxgb4_sched_class_alloc(struct net_device *dev,
583 struct ch_sched_params *p)
585 struct port_info *pi = netdev2pinfo(dev);
591 class_id = p->u.params.class;
592 if (!valid_class_id(dev, class_id))
595 return t4_sched_class_alloc(pi, p);
599 * cxgb4_sched_class_free - free a scheduling class
600 * @dev: net_device pointer
601 * @classid: scheduling class id to free
603 * Frees a scheduling class if there are no users.
605 void cxgb4_sched_class_free(struct net_device *dev, u8 classid)
607 struct port_info *pi = netdev2pinfo(dev);
608 struct sched_table *s = pi->sched_tbl;
609 struct ch_sched_params p;
610 struct sched_class *e;
614 e = &s->tab[classid];
615 if (!atomic_read(&e->refcnt) && e->state != SCHED_STATE_UNUSED) {
616 /* Port based rate limiting needs explicit reset back
617 * to max rate. But, we'll do explicit reset for all
618 * types, instead of just port based type, to be on
621 memcpy(&p, &e->info, sizeof(p));
622 /* Always reset mode to 0. Otherwise, FLOWC mode will
623 * still be enabled even after resetting the traffic
627 p.u.params.minrate = 0;
628 p.u.params.pktsize = 0;
630 ret = t4_get_link_params(pi, NULL, &speed, NULL);
632 p.u.params.maxrate = speed * 1000; /* Mbps to Kbps */
634 p.u.params.maxrate = SCHED_MAX_RATE_KBPS;
636 t4_sched_class_fw_cmd(pi, &p, SCHED_FW_OP_DEL);
638 e->state = SCHED_STATE_UNUSED;
639 memset(&e->info, 0, sizeof(e->info));
643 static void t4_sched_class_free(struct net_device *dev, struct sched_class *e)
645 struct port_info *pi = netdev2pinfo(dev);
647 t4_sched_class_unbind_all(pi, e, e->bind_type);
648 cxgb4_sched_class_free(dev, e->idx);
651 struct sched_table *t4_init_sched(unsigned int sched_size)
653 struct sched_table *s;
656 s = kvzalloc(struct_size(s, tab, sched_size), GFP_KERNEL);
660 s->sched_size = sched_size;
662 for (i = 0; i < s->sched_size; i++) {
663 memset(&s->tab[i], 0, sizeof(struct sched_class));
665 s->tab[i].state = SCHED_STATE_UNUSED;
666 INIT_LIST_HEAD(&s->tab[i].entry_list);
667 atomic_set(&s->tab[i].refcnt, 0);
672 void t4_cleanup_sched(struct adapter *adap)
674 struct sched_table *s;
677 for_each_port(adap, j) {
678 struct port_info *pi = netdev2pinfo(adap->port[j]);
684 for (i = 0; i < s->sched_size; i++) {
685 struct sched_class *e;
688 if (e->state == SCHED_STATE_ACTIVE)
689 t4_sched_class_free(adap->port[j], e);