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 /* Spinlock must be held by caller */
42 static int t4_sched_class_fw_cmd(struct port_info *pi,
43 struct ch_sched_params *p,
46 struct adapter *adap = pi->adapter;
47 struct sched_table *s = pi->sched_tbl;
48 struct sched_class *e;
51 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 /* Spinlock must be held by caller */
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));
104 err = t4_set_params(adap, adap->mbox, pf, vf, 1, &fw_param, &fw_class);
110 static struct sched_class *t4_sched_queue_lookup(struct port_info *pi,
111 const unsigned int qid,
114 struct sched_table *s = pi->sched_tbl;
115 struct sched_class *e, *end;
116 struct sched_class *found = NULL;
119 /* Look for a class with matching bound queue parameters */
120 end = &s->tab[s->sched_size];
121 for (e = &s->tab[0]; e != end; ++e) {
122 struct sched_queue_entry *qe;
125 if (e->state == SCHED_STATE_UNUSED)
128 list_for_each_entry(qe, &e->queue_list, list) {
129 if (qe->cntxt_id == qid) {
145 static int t4_sched_queue_unbind(struct port_info *pi, struct ch_sched_queue *p)
147 struct adapter *adap = pi->adapter;
148 struct sched_class *e;
149 struct sched_queue_entry *qe = NULL;
150 struct sge_eth_txq *txq;
155 if (p->queue < 0 || p->queue >= pi->nqsets)
158 txq = &adap->sge.ethtxq[pi->first_qset + p->queue];
159 qid = txq->q.cntxt_id;
161 /* Find the existing class that the queue is bound to */
162 e = t4_sched_queue_lookup(pi, qid, &index);
163 if (e && index >= 0) {
167 list_for_each_entry(qe, &e->queue_list, list) {
172 err = t4_sched_bind_unbind_op(pi, (void *)qe, SCHED_QUEUE,
175 spin_unlock(&e->lock);
181 if (atomic_dec_and_test(&e->refcnt)) {
182 e->state = SCHED_STATE_UNUSED;
183 memset(&e->info, 0, sizeof(e->info));
185 spin_unlock(&e->lock);
191 static int t4_sched_queue_bind(struct port_info *pi, struct ch_sched_queue *p)
193 struct adapter *adap = pi->adapter;
194 struct sched_table *s = pi->sched_tbl;
195 struct sched_class *e;
196 struct sched_queue_entry *qe = NULL;
197 struct sge_eth_txq *txq;
201 if (p->queue < 0 || p->queue >= pi->nqsets)
204 qe = t4_alloc_mem(sizeof(struct sched_queue_entry));
208 txq = &adap->sge.ethtxq[pi->first_qset + p->queue];
209 qid = txq->q.cntxt_id;
211 /* Unbind queue from any existing class */
212 err = t4_sched_queue_unbind(pi, p);
216 /* Bind queue to specified class */
217 memset(qe, 0, sizeof(*qe));
219 memcpy(&qe->param, p, sizeof(qe->param));
221 e = &s->tab[qe->param.class];
223 err = t4_sched_bind_unbind_op(pi, (void *)qe, SCHED_QUEUE, true);
226 spin_unlock(&e->lock);
230 list_add_tail(&qe->list, &e->queue_list);
231 atomic_inc(&e->refcnt);
232 spin_unlock(&e->lock);
237 static void t4_sched_class_unbind_all(struct port_info *pi,
238 struct sched_class *e,
239 enum sched_bind_type type)
246 struct sched_queue_entry *qe;
248 list_for_each_entry(qe, &e->queue_list, list)
249 t4_sched_queue_unbind(pi, &qe->param);
257 static int t4_sched_class_bind_unbind_op(struct port_info *pi, void *arg,
258 enum sched_bind_type type, bool bind)
267 struct ch_sched_queue *qe = (struct ch_sched_queue *)arg;
270 err = t4_sched_queue_bind(pi, qe);
272 err = t4_sched_queue_unbind(pi, qe);
284 * cxgb4_sched_class_bind - Bind an entity to a scheduling class
285 * @dev: net_device pointer
286 * @arg: Entity opaque data
287 * @type: Entity type (Queue)
289 * Binds an entity (queue) to a scheduling class. If the entity
290 * is bound to another class, it will be unbound from the other class
291 * and bound to the class specified in @arg.
293 int cxgb4_sched_class_bind(struct net_device *dev, void *arg,
294 enum sched_bind_type type)
296 struct port_info *pi = netdev2pinfo(dev);
297 struct sched_table *s;
309 struct ch_sched_queue *qe = (struct ch_sched_queue *)arg;
311 class_id = qe->class;
318 if (!valid_class_id(dev, class_id))
321 if (class_id == SCHED_CLS_NONE)
325 write_lock(&s->rw_lock);
326 err = t4_sched_class_bind_unbind_op(pi, arg, type, true);
327 write_unlock(&s->rw_lock);
333 * cxgb4_sched_class_unbind - Unbind an entity from a scheduling class
334 * @dev: net_device pointer
335 * @arg: Entity opaque data
336 * @type: Entity type (Queue)
338 * Unbinds an entity (queue) from a scheduling class.
340 int cxgb4_sched_class_unbind(struct net_device *dev, void *arg,
341 enum sched_bind_type type)
343 struct port_info *pi = netdev2pinfo(dev);
344 struct sched_table *s;
356 struct ch_sched_queue *qe = (struct ch_sched_queue *)arg;
358 class_id = qe->class;
365 if (!valid_class_id(dev, class_id))
369 write_lock(&s->rw_lock);
370 err = t4_sched_class_bind_unbind_op(pi, arg, type, false);
371 write_unlock(&s->rw_lock);
376 /* If @p is NULL, fetch any available unused class */
377 static struct sched_class *t4_sched_class_lookup(struct port_info *pi,
378 const struct ch_sched_params *p)
380 struct sched_table *s = pi->sched_tbl;
381 struct sched_class *e, *end;
382 struct sched_class *found = NULL;
385 /* Get any available unused class */
386 end = &s->tab[s->sched_size];
387 for (e = &s->tab[0]; e != end; ++e) {
388 if (e->state == SCHED_STATE_UNUSED) {
394 /* Look for a class with matching scheduling parameters */
395 struct ch_sched_params info;
396 struct ch_sched_params tp;
398 memset(&info, 0, sizeof(info));
399 memset(&tp, 0, sizeof(tp));
401 memcpy(&tp, p, sizeof(tp));
402 /* Don't try to match class parameter */
403 tp.u.params.class = SCHED_CLS_NONE;
405 end = &s->tab[s->sched_size];
406 for (e = &s->tab[0]; e != end; ++e) {
407 if (e->state == SCHED_STATE_UNUSED)
410 memset(&info, 0, sizeof(info));
411 memcpy(&info, &e->info, sizeof(info));
412 /* Don't try to match class parameter */
413 info.u.params.class = SCHED_CLS_NONE;
415 if ((info.type == tp.type) &&
416 (!memcmp(&info.u.params, &tp.u.params,
417 sizeof(info.u.params)))) {
427 static struct sched_class *t4_sched_class_alloc(struct port_info *pi,
428 struct ch_sched_params *p)
430 struct sched_table *s = pi->sched_tbl;
431 struct sched_class *e;
438 class_id = p->u.params.class;
440 /* Only accept search for existing class with matching params
441 * or allocation of new class with specified params
443 if (class_id != SCHED_CLS_NONE)
446 write_lock(&s->rw_lock);
447 /* See if there's an exisiting class with same
448 * requested sched params
450 e = t4_sched_class_lookup(pi, p);
452 struct ch_sched_params np;
454 /* Fetch any available unused class */
455 e = t4_sched_class_lookup(pi, NULL);
459 memset(&np, 0, sizeof(np));
460 memcpy(&np, p, sizeof(np));
461 np.u.params.class = e->idx;
465 err = t4_sched_class_fw_cmd(pi, &np, SCHED_FW_OP_ADD);
467 spin_unlock(&e->lock);
471 memcpy(&e->info, &np, sizeof(e->info));
472 atomic_set(&e->refcnt, 0);
473 e->state = SCHED_STATE_ACTIVE;
474 spin_unlock(&e->lock);
478 write_unlock(&s->rw_lock);
483 * cxgb4_sched_class_alloc - allocate a scheduling class
484 * @dev: net_device pointer
485 * @p: new scheduling class to create.
487 * Returns pointer to the scheduling class created. If @p is NULL, then
488 * it allocates and returns any available unused scheduling class. If a
489 * scheduling class with matching @p is found, then the matching class is
492 struct sched_class *cxgb4_sched_class_alloc(struct net_device *dev,
493 struct ch_sched_params *p)
495 struct port_info *pi = netdev2pinfo(dev);
501 class_id = p->u.params.class;
502 if (!valid_class_id(dev, class_id))
505 return t4_sched_class_alloc(pi, p);
508 static void t4_sched_class_free(struct port_info *pi, struct sched_class *e)
510 t4_sched_class_unbind_all(pi, e, SCHED_QUEUE);
513 struct sched_table *t4_init_sched(unsigned int sched_size)
515 struct sched_table *s;
518 s = t4_alloc_mem(sizeof(*s) + sched_size * sizeof(struct sched_class));
522 s->sched_size = sched_size;
523 rwlock_init(&s->rw_lock);
525 for (i = 0; i < s->sched_size; i++) {
526 memset(&s->tab[i], 0, sizeof(struct sched_class));
528 s->tab[i].state = SCHED_STATE_UNUSED;
529 INIT_LIST_HEAD(&s->tab[i].queue_list);
530 spin_lock_init(&s->tab[i].lock);
531 atomic_set(&s->tab[i].refcnt, 0);
536 void t4_cleanup_sched(struct adapter *adap)
538 struct sched_table *s;
541 for_each_port(adap, i) {
542 struct port_info *pi = netdev2pinfo(adap->port[i]);
545 for (i = 0; i < s->sched_size; i++) {
546 struct sched_class *e;
548 write_lock(&s->rw_lock);
550 if (e->state == SCHED_STATE_ACTIVE)
551 t4_sched_class_free(pi, e);
552 write_unlock(&s->rw_lock);