xref: /openbmc/linux/net/sched/sch_taprio.c (revision 7b9eba7ba0c1b24df42b70b62d154b284befbccf)
15a781ccbSVinicius Costa Gomes // SPDX-License-Identifier: GPL-2.0
25a781ccbSVinicius Costa Gomes 
35a781ccbSVinicius Costa Gomes /* net/sched/sch_taprio.c	 Time Aware Priority Scheduler
45a781ccbSVinicius Costa Gomes  *
55a781ccbSVinicius Costa Gomes  * Authors:	Vinicius Costa Gomes <vinicius.gomes@intel.com>
65a781ccbSVinicius Costa Gomes  *
75a781ccbSVinicius Costa Gomes  */
85a781ccbSVinicius Costa Gomes 
95a781ccbSVinicius Costa Gomes #include <linux/types.h>
105a781ccbSVinicius Costa Gomes #include <linux/slab.h>
115a781ccbSVinicius Costa Gomes #include <linux/kernel.h>
125a781ccbSVinicius Costa Gomes #include <linux/string.h>
135a781ccbSVinicius Costa Gomes #include <linux/list.h>
145a781ccbSVinicius Costa Gomes #include <linux/errno.h>
155a781ccbSVinicius Costa Gomes #include <linux/skbuff.h>
165a781ccbSVinicius Costa Gomes #include <linux/module.h>
175a781ccbSVinicius Costa Gomes #include <linux/spinlock.h>
185a781ccbSVinicius Costa Gomes #include <net/netlink.h>
195a781ccbSVinicius Costa Gomes #include <net/pkt_sched.h>
205a781ccbSVinicius Costa Gomes #include <net/pkt_cls.h>
215a781ccbSVinicius Costa Gomes #include <net/sch_generic.h>
225a781ccbSVinicius Costa Gomes 
23*7b9eba7bSLeandro Dorileo static LIST_HEAD(taprio_list);
24*7b9eba7bSLeandro Dorileo static DEFINE_SPINLOCK(taprio_list_lock);
25*7b9eba7bSLeandro Dorileo 
265a781ccbSVinicius Costa Gomes #define TAPRIO_ALL_GATES_OPEN -1
275a781ccbSVinicius Costa Gomes 
285a781ccbSVinicius Costa Gomes struct sched_entry {
295a781ccbSVinicius Costa Gomes 	struct list_head list;
305a781ccbSVinicius Costa Gomes 
315a781ccbSVinicius Costa Gomes 	/* The instant that this entry "closes" and the next one
325a781ccbSVinicius Costa Gomes 	 * should open, the qdisc will make some effort so that no
335a781ccbSVinicius Costa Gomes 	 * packet leaves after this time.
345a781ccbSVinicius Costa Gomes 	 */
355a781ccbSVinicius Costa Gomes 	ktime_t close_time;
365a781ccbSVinicius Costa Gomes 	atomic_t budget;
375a781ccbSVinicius Costa Gomes 	int index;
385a781ccbSVinicius Costa Gomes 	u32 gate_mask;
395a781ccbSVinicius Costa Gomes 	u32 interval;
405a781ccbSVinicius Costa Gomes 	u8 command;
415a781ccbSVinicius Costa Gomes };
425a781ccbSVinicius Costa Gomes 
435a781ccbSVinicius Costa Gomes struct taprio_sched {
445a781ccbSVinicius Costa Gomes 	struct Qdisc **qdiscs;
455a781ccbSVinicius Costa Gomes 	struct Qdisc *root;
465a781ccbSVinicius Costa Gomes 	s64 base_time;
475a781ccbSVinicius Costa Gomes 	int clockid;
48*7b9eba7bSLeandro Dorileo 	atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
495a781ccbSVinicius Costa Gomes 				    * speeds it's sub-nanoseconds per byte
505a781ccbSVinicius Costa Gomes 				    */
515a781ccbSVinicius Costa Gomes 	size_t num_entries;
525a781ccbSVinicius Costa Gomes 
535a781ccbSVinicius Costa Gomes 	/* Protects the update side of the RCU protected current_entry */
545a781ccbSVinicius Costa Gomes 	spinlock_t current_entry_lock;
555a781ccbSVinicius Costa Gomes 	struct sched_entry __rcu *current_entry;
565a781ccbSVinicius Costa Gomes 	struct list_head entries;
575a781ccbSVinicius Costa Gomes 	ktime_t (*get_time)(void);
585a781ccbSVinicius Costa Gomes 	struct hrtimer advance_timer;
59*7b9eba7bSLeandro Dorileo 	struct list_head taprio_list;
605a781ccbSVinicius Costa Gomes };
615a781ccbSVinicius Costa Gomes 
625a781ccbSVinicius Costa Gomes static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
635a781ccbSVinicius Costa Gomes 			  struct sk_buff **to_free)
645a781ccbSVinicius Costa Gomes {
655a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
665a781ccbSVinicius Costa Gomes 	struct Qdisc *child;
675a781ccbSVinicius Costa Gomes 	int queue;
685a781ccbSVinicius Costa Gomes 
695a781ccbSVinicius Costa Gomes 	queue = skb_get_queue_mapping(skb);
705a781ccbSVinicius Costa Gomes 
715a781ccbSVinicius Costa Gomes 	child = q->qdiscs[queue];
725a781ccbSVinicius Costa Gomes 	if (unlikely(!child))
735a781ccbSVinicius Costa Gomes 		return qdisc_drop(skb, sch, to_free);
745a781ccbSVinicius Costa Gomes 
755a781ccbSVinicius Costa Gomes 	qdisc_qstats_backlog_inc(sch, skb);
765a781ccbSVinicius Costa Gomes 	sch->q.qlen++;
775a781ccbSVinicius Costa Gomes 
785a781ccbSVinicius Costa Gomes 	return qdisc_enqueue(skb, child, to_free);
795a781ccbSVinicius Costa Gomes }
805a781ccbSVinicius Costa Gomes 
815a781ccbSVinicius Costa Gomes static struct sk_buff *taprio_peek(struct Qdisc *sch)
825a781ccbSVinicius Costa Gomes {
835a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
845a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
855a781ccbSVinicius Costa Gomes 	struct sched_entry *entry;
865a781ccbSVinicius Costa Gomes 	struct sk_buff *skb;
875a781ccbSVinicius Costa Gomes 	u32 gate_mask;
885a781ccbSVinicius Costa Gomes 	int i;
895a781ccbSVinicius Costa Gomes 
905a781ccbSVinicius Costa Gomes 	rcu_read_lock();
915a781ccbSVinicius Costa Gomes 	entry = rcu_dereference(q->current_entry);
925a781ccbSVinicius Costa Gomes 	gate_mask = entry ? entry->gate_mask : -1;
935a781ccbSVinicius Costa Gomes 	rcu_read_unlock();
945a781ccbSVinicius Costa Gomes 
955a781ccbSVinicius Costa Gomes 	if (!gate_mask)
965a781ccbSVinicius Costa Gomes 		return NULL;
975a781ccbSVinicius Costa Gomes 
985a781ccbSVinicius Costa Gomes 	for (i = 0; i < dev->num_tx_queues; i++) {
995a781ccbSVinicius Costa Gomes 		struct Qdisc *child = q->qdiscs[i];
1005a781ccbSVinicius Costa Gomes 		int prio;
1015a781ccbSVinicius Costa Gomes 		u8 tc;
1025a781ccbSVinicius Costa Gomes 
1035a781ccbSVinicius Costa Gomes 		if (unlikely(!child))
1045a781ccbSVinicius Costa Gomes 			continue;
1055a781ccbSVinicius Costa Gomes 
1065a781ccbSVinicius Costa Gomes 		skb = child->ops->peek(child);
1075a781ccbSVinicius Costa Gomes 		if (!skb)
1085a781ccbSVinicius Costa Gomes 			continue;
1095a781ccbSVinicius Costa Gomes 
1105a781ccbSVinicius Costa Gomes 		prio = skb->priority;
1115a781ccbSVinicius Costa Gomes 		tc = netdev_get_prio_tc_map(dev, prio);
1125a781ccbSVinicius Costa Gomes 
1135a781ccbSVinicius Costa Gomes 		if (!(gate_mask & BIT(tc)))
1145a781ccbSVinicius Costa Gomes 			return NULL;
1155a781ccbSVinicius Costa Gomes 
1165a781ccbSVinicius Costa Gomes 		return skb;
1175a781ccbSVinicius Costa Gomes 	}
1185a781ccbSVinicius Costa Gomes 
1195a781ccbSVinicius Costa Gomes 	return NULL;
1205a781ccbSVinicius Costa Gomes }
1215a781ccbSVinicius Costa Gomes 
1225a781ccbSVinicius Costa Gomes static inline int length_to_duration(struct taprio_sched *q, int len)
1235a781ccbSVinicius Costa Gomes {
124*7b9eba7bSLeandro Dorileo 	return (len * atomic64_read(&q->picos_per_byte)) / 1000;
1255a781ccbSVinicius Costa Gomes }
1265a781ccbSVinicius Costa Gomes 
1275a781ccbSVinicius Costa Gomes static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
1285a781ccbSVinicius Costa Gomes {
1295a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
1305a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
1315a781ccbSVinicius Costa Gomes 	struct sched_entry *entry;
1325a781ccbSVinicius Costa Gomes 	struct sk_buff *skb;
1335a781ccbSVinicius Costa Gomes 	u32 gate_mask;
1345a781ccbSVinicius Costa Gomes 	int i;
1355a781ccbSVinicius Costa Gomes 
136*7b9eba7bSLeandro Dorileo 	if (atomic64_read(&q->picos_per_byte) == -1) {
137*7b9eba7bSLeandro Dorileo 		WARN_ONCE(1, "taprio: dequeue() called with unknown picos per byte.");
138*7b9eba7bSLeandro Dorileo 		return NULL;
139*7b9eba7bSLeandro Dorileo 	}
140*7b9eba7bSLeandro Dorileo 
1415a781ccbSVinicius Costa Gomes 	rcu_read_lock();
1425a781ccbSVinicius Costa Gomes 	entry = rcu_dereference(q->current_entry);
1435a781ccbSVinicius Costa Gomes 	/* if there's no entry, it means that the schedule didn't
1445a781ccbSVinicius Costa Gomes 	 * start yet, so force all gates to be open, this is in
1455a781ccbSVinicius Costa Gomes 	 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
1465a781ccbSVinicius Costa Gomes 	 * "AdminGateSates"
1475a781ccbSVinicius Costa Gomes 	 */
1485a781ccbSVinicius Costa Gomes 	gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
1495a781ccbSVinicius Costa Gomes 	rcu_read_unlock();
1505a781ccbSVinicius Costa Gomes 
1515a781ccbSVinicius Costa Gomes 	if (!gate_mask)
1525a781ccbSVinicius Costa Gomes 		return NULL;
1535a781ccbSVinicius Costa Gomes 
1545a781ccbSVinicius Costa Gomes 	for (i = 0; i < dev->num_tx_queues; i++) {
1555a781ccbSVinicius Costa Gomes 		struct Qdisc *child = q->qdiscs[i];
1565a781ccbSVinicius Costa Gomes 		ktime_t guard;
1575a781ccbSVinicius Costa Gomes 		int prio;
1585a781ccbSVinicius Costa Gomes 		int len;
1595a781ccbSVinicius Costa Gomes 		u8 tc;
1605a781ccbSVinicius Costa Gomes 
1615a781ccbSVinicius Costa Gomes 		if (unlikely(!child))
1625a781ccbSVinicius Costa Gomes 			continue;
1635a781ccbSVinicius Costa Gomes 
1645a781ccbSVinicius Costa Gomes 		skb = child->ops->peek(child);
1655a781ccbSVinicius Costa Gomes 		if (!skb)
1665a781ccbSVinicius Costa Gomes 			continue;
1675a781ccbSVinicius Costa Gomes 
1685a781ccbSVinicius Costa Gomes 		prio = skb->priority;
1695a781ccbSVinicius Costa Gomes 		tc = netdev_get_prio_tc_map(dev, prio);
1705a781ccbSVinicius Costa Gomes 
1715a781ccbSVinicius Costa Gomes 		if (!(gate_mask & BIT(tc)))
1725a781ccbSVinicius Costa Gomes 			continue;
1735a781ccbSVinicius Costa Gomes 
1745a781ccbSVinicius Costa Gomes 		len = qdisc_pkt_len(skb);
1755a781ccbSVinicius Costa Gomes 		guard = ktime_add_ns(q->get_time(),
1765a781ccbSVinicius Costa Gomes 				     length_to_duration(q, len));
1775a781ccbSVinicius Costa Gomes 
1785a781ccbSVinicius Costa Gomes 		/* In the case that there's no gate entry, there's no
1795a781ccbSVinicius Costa Gomes 		 * guard band ...
1805a781ccbSVinicius Costa Gomes 		 */
1815a781ccbSVinicius Costa Gomes 		if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
1825a781ccbSVinicius Costa Gomes 		    ktime_after(guard, entry->close_time))
1835a781ccbSVinicius Costa Gomes 			return NULL;
1845a781ccbSVinicius Costa Gomes 
1855a781ccbSVinicius Costa Gomes 		/* ... and no budget. */
1865a781ccbSVinicius Costa Gomes 		if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
1875a781ccbSVinicius Costa Gomes 		    atomic_sub_return(len, &entry->budget) < 0)
1885a781ccbSVinicius Costa Gomes 			return NULL;
1895a781ccbSVinicius Costa Gomes 
1905a781ccbSVinicius Costa Gomes 		skb = child->ops->dequeue(child);
1915a781ccbSVinicius Costa Gomes 		if (unlikely(!skb))
1925a781ccbSVinicius Costa Gomes 			return NULL;
1935a781ccbSVinicius Costa Gomes 
1945a781ccbSVinicius Costa Gomes 		qdisc_bstats_update(sch, skb);
1955a781ccbSVinicius Costa Gomes 		qdisc_qstats_backlog_dec(sch, skb);
1965a781ccbSVinicius Costa Gomes 		sch->q.qlen--;
1975a781ccbSVinicius Costa Gomes 
1985a781ccbSVinicius Costa Gomes 		return skb;
1995a781ccbSVinicius Costa Gomes 	}
2005a781ccbSVinicius Costa Gomes 
2015a781ccbSVinicius Costa Gomes 	return NULL;
2025a781ccbSVinicius Costa Gomes }
2035a781ccbSVinicius Costa Gomes 
2045a781ccbSVinicius Costa Gomes static bool should_restart_cycle(const struct taprio_sched *q,
2055a781ccbSVinicius Costa Gomes 				 const struct sched_entry *entry)
2065a781ccbSVinicius Costa Gomes {
2075a781ccbSVinicius Costa Gomes 	WARN_ON(!entry);
2085a781ccbSVinicius Costa Gomes 
2095a781ccbSVinicius Costa Gomes 	return list_is_last(&entry->list, &q->entries);
2105a781ccbSVinicius Costa Gomes }
2115a781ccbSVinicius Costa Gomes 
2125a781ccbSVinicius Costa Gomes static enum hrtimer_restart advance_sched(struct hrtimer *timer)
2135a781ccbSVinicius Costa Gomes {
2145a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = container_of(timer, struct taprio_sched,
2155a781ccbSVinicius Costa Gomes 					      advance_timer);
2165a781ccbSVinicius Costa Gomes 	struct sched_entry *entry, *next;
2175a781ccbSVinicius Costa Gomes 	struct Qdisc *sch = q->root;
2185a781ccbSVinicius Costa Gomes 	ktime_t close_time;
2195a781ccbSVinicius Costa Gomes 
2205a781ccbSVinicius Costa Gomes 	spin_lock(&q->current_entry_lock);
2215a781ccbSVinicius Costa Gomes 	entry = rcu_dereference_protected(q->current_entry,
2225a781ccbSVinicius Costa Gomes 					  lockdep_is_held(&q->current_entry_lock));
2235a781ccbSVinicius Costa Gomes 
2245a781ccbSVinicius Costa Gomes 	/* This is the case that it's the first time that the schedule
2255a781ccbSVinicius Costa Gomes 	 * runs, so it only happens once per schedule. The first entry
2265a781ccbSVinicius Costa Gomes 	 * is pre-calculated during the schedule initialization.
2275a781ccbSVinicius Costa Gomes 	 */
2285a781ccbSVinicius Costa Gomes 	if (unlikely(!entry)) {
2295a781ccbSVinicius Costa Gomes 		next = list_first_entry(&q->entries, struct sched_entry,
2305a781ccbSVinicius Costa Gomes 					list);
2315a781ccbSVinicius Costa Gomes 		close_time = next->close_time;
2325a781ccbSVinicius Costa Gomes 		goto first_run;
2335a781ccbSVinicius Costa Gomes 	}
2345a781ccbSVinicius Costa Gomes 
2355a781ccbSVinicius Costa Gomes 	if (should_restart_cycle(q, entry))
2365a781ccbSVinicius Costa Gomes 		next = list_first_entry(&q->entries, struct sched_entry,
2375a781ccbSVinicius Costa Gomes 					list);
2385a781ccbSVinicius Costa Gomes 	else
2395a781ccbSVinicius Costa Gomes 		next = list_next_entry(entry, list);
2405a781ccbSVinicius Costa Gomes 
2415a781ccbSVinicius Costa Gomes 	close_time = ktime_add_ns(entry->close_time, next->interval);
2425a781ccbSVinicius Costa Gomes 
2435a781ccbSVinicius Costa Gomes 	next->close_time = close_time;
2445a781ccbSVinicius Costa Gomes 	atomic_set(&next->budget,
245*7b9eba7bSLeandro Dorileo 		   (next->interval * 1000) / atomic64_read(&q->picos_per_byte));
2465a781ccbSVinicius Costa Gomes 
2475a781ccbSVinicius Costa Gomes first_run:
2485a781ccbSVinicius Costa Gomes 	rcu_assign_pointer(q->current_entry, next);
2495a781ccbSVinicius Costa Gomes 	spin_unlock(&q->current_entry_lock);
2505a781ccbSVinicius Costa Gomes 
2515a781ccbSVinicius Costa Gomes 	hrtimer_set_expires(&q->advance_timer, close_time);
2525a781ccbSVinicius Costa Gomes 
2535a781ccbSVinicius Costa Gomes 	rcu_read_lock();
2545a781ccbSVinicius Costa Gomes 	__netif_schedule(sch);
2555a781ccbSVinicius Costa Gomes 	rcu_read_unlock();
2565a781ccbSVinicius Costa Gomes 
2575a781ccbSVinicius Costa Gomes 	return HRTIMER_RESTART;
2585a781ccbSVinicius Costa Gomes }
2595a781ccbSVinicius Costa Gomes 
2605a781ccbSVinicius Costa Gomes static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
2615a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_SCHED_ENTRY_INDEX]	   = { .type = NLA_U32 },
2625a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_SCHED_ENTRY_CMD]	   = { .type = NLA_U8 },
2635a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
2645a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]  = { .type = NLA_U32 },
2655a781ccbSVinicius Costa Gomes };
2665a781ccbSVinicius Costa Gomes 
2675a781ccbSVinicius Costa Gomes static const struct nla_policy entry_list_policy[TCA_TAPRIO_SCHED_MAX + 1] = {
2685a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_SCHED_ENTRY] = { .type = NLA_NESTED },
2695a781ccbSVinicius Costa Gomes };
2705a781ccbSVinicius Costa Gomes 
2715a781ccbSVinicius Costa Gomes static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
2725a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_PRIOMAP]	       = {
2735a781ccbSVinicius Costa Gomes 		.len = sizeof(struct tc_mqprio_qopt)
2745a781ccbSVinicius Costa Gomes 	},
2755a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]     = { .type = NLA_NESTED },
2765a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]      = { .type = NLA_S64 },
2775a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]   = { .type = NLA_NESTED },
2785a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_CLOCKID]        = { .type = NLA_S32 },
2795a781ccbSVinicius Costa Gomes };
2805a781ccbSVinicius Costa Gomes 
2815a781ccbSVinicius Costa Gomes static int fill_sched_entry(struct nlattr **tb, struct sched_entry *entry,
2825a781ccbSVinicius Costa Gomes 			    struct netlink_ext_ack *extack)
2835a781ccbSVinicius Costa Gomes {
2845a781ccbSVinicius Costa Gomes 	u32 interval = 0;
2855a781ccbSVinicius Costa Gomes 
2865a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
2875a781ccbSVinicius Costa Gomes 		entry->command = nla_get_u8(
2885a781ccbSVinicius Costa Gomes 			tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
2895a781ccbSVinicius Costa Gomes 
2905a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
2915a781ccbSVinicius Costa Gomes 		entry->gate_mask = nla_get_u32(
2925a781ccbSVinicius Costa Gomes 			tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
2935a781ccbSVinicius Costa Gomes 
2945a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
2955a781ccbSVinicius Costa Gomes 		interval = nla_get_u32(
2965a781ccbSVinicius Costa Gomes 			tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
2975a781ccbSVinicius Costa Gomes 
2985a781ccbSVinicius Costa Gomes 	if (interval == 0) {
2995a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
3005a781ccbSVinicius Costa Gomes 		return -EINVAL;
3015a781ccbSVinicius Costa Gomes 	}
3025a781ccbSVinicius Costa Gomes 
3035a781ccbSVinicius Costa Gomes 	entry->interval = interval;
3045a781ccbSVinicius Costa Gomes 
3055a781ccbSVinicius Costa Gomes 	return 0;
3065a781ccbSVinicius Costa Gomes }
3075a781ccbSVinicius Costa Gomes 
3085a781ccbSVinicius Costa Gomes static int parse_sched_entry(struct nlattr *n, struct sched_entry *entry,
3095a781ccbSVinicius Costa Gomes 			     int index, struct netlink_ext_ack *extack)
3105a781ccbSVinicius Costa Gomes {
3115a781ccbSVinicius Costa Gomes 	struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
3125a781ccbSVinicius Costa Gomes 	int err;
3135a781ccbSVinicius Costa Gomes 
3145a781ccbSVinicius Costa Gomes 	err = nla_parse_nested(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
3155a781ccbSVinicius Costa Gomes 			       entry_policy, NULL);
3165a781ccbSVinicius Costa Gomes 	if (err < 0) {
3175a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Could not parse nested entry");
3185a781ccbSVinicius Costa Gomes 		return -EINVAL;
3195a781ccbSVinicius Costa Gomes 	}
3205a781ccbSVinicius Costa Gomes 
3215a781ccbSVinicius Costa Gomes 	entry->index = index;
3225a781ccbSVinicius Costa Gomes 
3235a781ccbSVinicius Costa Gomes 	return fill_sched_entry(tb, entry, extack);
3245a781ccbSVinicius Costa Gomes }
3255a781ccbSVinicius Costa Gomes 
3265a781ccbSVinicius Costa Gomes /* Returns the number of entries in case of success */
3275a781ccbSVinicius Costa Gomes static int parse_sched_single_entry(struct nlattr *n,
3285a781ccbSVinicius Costa Gomes 				    struct taprio_sched *q,
3295a781ccbSVinicius Costa Gomes 				    struct netlink_ext_ack *extack)
3305a781ccbSVinicius Costa Gomes {
3315a781ccbSVinicius Costa Gomes 	struct nlattr *tb_entry[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
3325a781ccbSVinicius Costa Gomes 	struct nlattr *tb_list[TCA_TAPRIO_SCHED_MAX + 1] = { };
3335a781ccbSVinicius Costa Gomes 	struct sched_entry *entry;
3345a781ccbSVinicius Costa Gomes 	bool found = false;
3355a781ccbSVinicius Costa Gomes 	u32 index;
3365a781ccbSVinicius Costa Gomes 	int err;
3375a781ccbSVinicius Costa Gomes 
3385a781ccbSVinicius Costa Gomes 	err = nla_parse_nested(tb_list, TCA_TAPRIO_SCHED_MAX,
3395a781ccbSVinicius Costa Gomes 			       n, entry_list_policy, NULL);
3405a781ccbSVinicius Costa Gomes 	if (err < 0) {
3415a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Could not parse nested entry");
3425a781ccbSVinicius Costa Gomes 		return -EINVAL;
3435a781ccbSVinicius Costa Gomes 	}
3445a781ccbSVinicius Costa Gomes 
3455a781ccbSVinicius Costa Gomes 	if (!tb_list[TCA_TAPRIO_SCHED_ENTRY]) {
3465a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Single-entry must include an entry");
3475a781ccbSVinicius Costa Gomes 		return -EINVAL;
3485a781ccbSVinicius Costa Gomes 	}
3495a781ccbSVinicius Costa Gomes 
3505a781ccbSVinicius Costa Gomes 	err = nla_parse_nested(tb_entry, TCA_TAPRIO_SCHED_ENTRY_MAX,
3515a781ccbSVinicius Costa Gomes 			       tb_list[TCA_TAPRIO_SCHED_ENTRY],
3525a781ccbSVinicius Costa Gomes 			       entry_policy, NULL);
3535a781ccbSVinicius Costa Gomes 	if (err < 0) {
3545a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Could not parse nested entry");
3555a781ccbSVinicius Costa Gomes 		return -EINVAL;
3565a781ccbSVinicius Costa Gomes 	}
3575a781ccbSVinicius Costa Gomes 
3585a781ccbSVinicius Costa Gomes 	if (!tb_entry[TCA_TAPRIO_SCHED_ENTRY_INDEX]) {
3595a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Entry must specify an index\n");
3605a781ccbSVinicius Costa Gomes 		return -EINVAL;
3615a781ccbSVinicius Costa Gomes 	}
3625a781ccbSVinicius Costa Gomes 
3635a781ccbSVinicius Costa Gomes 	index = nla_get_u32(tb_entry[TCA_TAPRIO_SCHED_ENTRY_INDEX]);
3645a781ccbSVinicius Costa Gomes 	if (index >= q->num_entries) {
3655a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Index for single entry exceeds number of entries in schedule");
3665a781ccbSVinicius Costa Gomes 		return -EINVAL;
3675a781ccbSVinicius Costa Gomes 	}
3685a781ccbSVinicius Costa Gomes 
3695a781ccbSVinicius Costa Gomes 	list_for_each_entry(entry, &q->entries, list) {
3705a781ccbSVinicius Costa Gomes 		if (entry->index == index) {
3715a781ccbSVinicius Costa Gomes 			found = true;
3725a781ccbSVinicius Costa Gomes 			break;
3735a781ccbSVinicius Costa Gomes 		}
3745a781ccbSVinicius Costa Gomes 	}
3755a781ccbSVinicius Costa Gomes 
3765a781ccbSVinicius Costa Gomes 	if (!found) {
3775a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Could not find entry");
3785a781ccbSVinicius Costa Gomes 		return -ENOENT;
3795a781ccbSVinicius Costa Gomes 	}
3805a781ccbSVinicius Costa Gomes 
3815a781ccbSVinicius Costa Gomes 	err = fill_sched_entry(tb_entry, entry, extack);
3825a781ccbSVinicius Costa Gomes 	if (err < 0)
3835a781ccbSVinicius Costa Gomes 		return err;
3845a781ccbSVinicius Costa Gomes 
3855a781ccbSVinicius Costa Gomes 	return q->num_entries;
3865a781ccbSVinicius Costa Gomes }
3875a781ccbSVinicius Costa Gomes 
3885a781ccbSVinicius Costa Gomes static int parse_sched_list(struct nlattr *list,
3895a781ccbSVinicius Costa Gomes 			    struct taprio_sched *q,
3905a781ccbSVinicius Costa Gomes 			    struct netlink_ext_ack *extack)
3915a781ccbSVinicius Costa Gomes {
3925a781ccbSVinicius Costa Gomes 	struct nlattr *n;
3935a781ccbSVinicius Costa Gomes 	int err, rem;
3945a781ccbSVinicius Costa Gomes 	int i = 0;
3955a781ccbSVinicius Costa Gomes 
3965a781ccbSVinicius Costa Gomes 	if (!list)
3975a781ccbSVinicius Costa Gomes 		return -EINVAL;
3985a781ccbSVinicius Costa Gomes 
3995a781ccbSVinicius Costa Gomes 	nla_for_each_nested(n, list, rem) {
4005a781ccbSVinicius Costa Gomes 		struct sched_entry *entry;
4015a781ccbSVinicius Costa Gomes 
4025a781ccbSVinicius Costa Gomes 		if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
4035a781ccbSVinicius Costa Gomes 			NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
4045a781ccbSVinicius Costa Gomes 			continue;
4055a781ccbSVinicius Costa Gomes 		}
4065a781ccbSVinicius Costa Gomes 
4075a781ccbSVinicius Costa Gomes 		entry = kzalloc(sizeof(*entry), GFP_KERNEL);
4085a781ccbSVinicius Costa Gomes 		if (!entry) {
4095a781ccbSVinicius Costa Gomes 			NL_SET_ERR_MSG(extack, "Not enough memory for entry");
4105a781ccbSVinicius Costa Gomes 			return -ENOMEM;
4115a781ccbSVinicius Costa Gomes 		}
4125a781ccbSVinicius Costa Gomes 
4135a781ccbSVinicius Costa Gomes 		err = parse_sched_entry(n, entry, i, extack);
4145a781ccbSVinicius Costa Gomes 		if (err < 0) {
4155a781ccbSVinicius Costa Gomes 			kfree(entry);
4165a781ccbSVinicius Costa Gomes 			return err;
4175a781ccbSVinicius Costa Gomes 		}
4185a781ccbSVinicius Costa Gomes 
4195a781ccbSVinicius Costa Gomes 		list_add_tail(&entry->list, &q->entries);
4205a781ccbSVinicius Costa Gomes 		i++;
4215a781ccbSVinicius Costa Gomes 	}
4225a781ccbSVinicius Costa Gomes 
4235a781ccbSVinicius Costa Gomes 	q->num_entries = i;
4245a781ccbSVinicius Costa Gomes 
4255a781ccbSVinicius Costa Gomes 	return i;
4265a781ccbSVinicius Costa Gomes }
4275a781ccbSVinicius Costa Gomes 
4285a781ccbSVinicius Costa Gomes /* Returns the number of entries in case of success */
4295a781ccbSVinicius Costa Gomes static int parse_taprio_opt(struct nlattr **tb, struct taprio_sched *q,
4305a781ccbSVinicius Costa Gomes 			    struct netlink_ext_ack *extack)
4315a781ccbSVinicius Costa Gomes {
4325a781ccbSVinicius Costa Gomes 	int err = 0;
4335a781ccbSVinicius Costa Gomes 	int clockid;
4345a781ccbSVinicius Costa Gomes 
4355a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] &&
4365a781ccbSVinicius Costa Gomes 	    tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY])
4375a781ccbSVinicius Costa Gomes 		return -EINVAL;
4385a781ccbSVinicius Costa Gomes 
4395a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] && q->num_entries == 0)
4405a781ccbSVinicius Costa Gomes 		return -EINVAL;
4415a781ccbSVinicius Costa Gomes 
4425a781ccbSVinicius Costa Gomes 	if (q->clockid == -1 && !tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID])
4435a781ccbSVinicius Costa Gomes 		return -EINVAL;
4445a781ccbSVinicius Costa Gomes 
4455a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
4465a781ccbSVinicius Costa Gomes 		q->base_time = nla_get_s64(
4475a781ccbSVinicius Costa Gomes 			tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
4485a781ccbSVinicius Costa Gomes 
4495a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
4505a781ccbSVinicius Costa Gomes 		clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
4515a781ccbSVinicius Costa Gomes 
4525a781ccbSVinicius Costa Gomes 		/* We only support static clockids and we don't allow
4535a781ccbSVinicius Costa Gomes 		 * for it to be modified after the first init.
4545a781ccbSVinicius Costa Gomes 		 */
4555a781ccbSVinicius Costa Gomes 		if (clockid < 0 || (q->clockid != -1 && q->clockid != clockid))
4565a781ccbSVinicius Costa Gomes 			return -EINVAL;
4575a781ccbSVinicius Costa Gomes 
4585a781ccbSVinicius Costa Gomes 		q->clockid = clockid;
4595a781ccbSVinicius Costa Gomes 	}
4605a781ccbSVinicius Costa Gomes 
4615a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
4625a781ccbSVinicius Costa Gomes 		err = parse_sched_list(
4635a781ccbSVinicius Costa Gomes 			tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], q, extack);
4645a781ccbSVinicius Costa Gomes 	else if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY])
4655a781ccbSVinicius Costa Gomes 		err = parse_sched_single_entry(
4665a781ccbSVinicius Costa Gomes 			tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY], q, extack);
4675a781ccbSVinicius Costa Gomes 
4685a781ccbSVinicius Costa Gomes 	/* parse_sched_* return the number of entries in the schedule,
4695a781ccbSVinicius Costa Gomes 	 * a schedule with zero entries is an error.
4705a781ccbSVinicius Costa Gomes 	 */
4715a781ccbSVinicius Costa Gomes 	if (err == 0) {
4725a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "The schedule should contain at least one entry");
4735a781ccbSVinicius Costa Gomes 		return -EINVAL;
4745a781ccbSVinicius Costa Gomes 	}
4755a781ccbSVinicius Costa Gomes 
4765a781ccbSVinicius Costa Gomes 	return err;
4775a781ccbSVinicius Costa Gomes }
4785a781ccbSVinicius Costa Gomes 
4795a781ccbSVinicius Costa Gomes static int taprio_parse_mqprio_opt(struct net_device *dev,
4805a781ccbSVinicius Costa Gomes 				   struct tc_mqprio_qopt *qopt,
4815a781ccbSVinicius Costa Gomes 				   struct netlink_ext_ack *extack)
4825a781ccbSVinicius Costa Gomes {
4835a781ccbSVinicius Costa Gomes 	int i, j;
4845a781ccbSVinicius Costa Gomes 
4855a781ccbSVinicius Costa Gomes 	if (!qopt) {
4865a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
4875a781ccbSVinicius Costa Gomes 		return -EINVAL;
4885a781ccbSVinicius Costa Gomes 	}
4895a781ccbSVinicius Costa Gomes 
4905a781ccbSVinicius Costa Gomes 	/* Verify num_tc is not out of max range */
4915a781ccbSVinicius Costa Gomes 	if (qopt->num_tc > TC_MAX_QUEUE) {
4925a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
4935a781ccbSVinicius Costa Gomes 		return -EINVAL;
4945a781ccbSVinicius Costa Gomes 	}
4955a781ccbSVinicius Costa Gomes 
4965a781ccbSVinicius Costa Gomes 	/* taprio imposes that traffic classes map 1:n to tx queues */
4975a781ccbSVinicius Costa Gomes 	if (qopt->num_tc > dev->num_tx_queues) {
4985a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
4995a781ccbSVinicius Costa Gomes 		return -EINVAL;
5005a781ccbSVinicius Costa Gomes 	}
5015a781ccbSVinicius Costa Gomes 
5025a781ccbSVinicius Costa Gomes 	/* Verify priority mapping uses valid tcs */
5035a781ccbSVinicius Costa Gomes 	for (i = 0; i < TC_BITMASK + 1; i++) {
5045a781ccbSVinicius Costa Gomes 		if (qopt->prio_tc_map[i] >= qopt->num_tc) {
5055a781ccbSVinicius Costa Gomes 			NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
5065a781ccbSVinicius Costa Gomes 			return -EINVAL;
5075a781ccbSVinicius Costa Gomes 		}
5085a781ccbSVinicius Costa Gomes 	}
5095a781ccbSVinicius Costa Gomes 
5105a781ccbSVinicius Costa Gomes 	for (i = 0; i < qopt->num_tc; i++) {
5115a781ccbSVinicius Costa Gomes 		unsigned int last = qopt->offset[i] + qopt->count[i];
5125a781ccbSVinicius Costa Gomes 
5135a781ccbSVinicius Costa Gomes 		/* Verify the queue count is in tx range being equal to the
5145a781ccbSVinicius Costa Gomes 		 * real_num_tx_queues indicates the last queue is in use.
5155a781ccbSVinicius Costa Gomes 		 */
5165a781ccbSVinicius Costa Gomes 		if (qopt->offset[i] >= dev->num_tx_queues ||
5175a781ccbSVinicius Costa Gomes 		    !qopt->count[i] ||
5185a781ccbSVinicius Costa Gomes 		    last > dev->real_num_tx_queues) {
5195a781ccbSVinicius Costa Gomes 			NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
5205a781ccbSVinicius Costa Gomes 			return -EINVAL;
5215a781ccbSVinicius Costa Gomes 		}
5225a781ccbSVinicius Costa Gomes 
5235a781ccbSVinicius Costa Gomes 		/* Verify that the offset and counts do not overlap */
5245a781ccbSVinicius Costa Gomes 		for (j = i + 1; j < qopt->num_tc; j++) {
5255a781ccbSVinicius Costa Gomes 			if (last > qopt->offset[j]) {
5265a781ccbSVinicius Costa Gomes 				NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
5275a781ccbSVinicius Costa Gomes 				return -EINVAL;
5285a781ccbSVinicius Costa Gomes 			}
5295a781ccbSVinicius Costa Gomes 		}
5305a781ccbSVinicius Costa Gomes 	}
5315a781ccbSVinicius Costa Gomes 
5325a781ccbSVinicius Costa Gomes 	return 0;
5335a781ccbSVinicius Costa Gomes }
5345a781ccbSVinicius Costa Gomes 
5355a781ccbSVinicius Costa Gomes static ktime_t taprio_get_start_time(struct Qdisc *sch)
5365a781ccbSVinicius Costa Gomes {
5375a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
5385a781ccbSVinicius Costa Gomes 	struct sched_entry *entry;
5395a781ccbSVinicius Costa Gomes 	ktime_t now, base, cycle;
5405a781ccbSVinicius Costa Gomes 	s64 n;
5415a781ccbSVinicius Costa Gomes 
5425a781ccbSVinicius Costa Gomes 	base = ns_to_ktime(q->base_time);
5435a781ccbSVinicius Costa Gomes 	cycle = 0;
5445a781ccbSVinicius Costa Gomes 
5455a781ccbSVinicius Costa Gomes 	/* Calculate the cycle_time, by summing all the intervals.
5465a781ccbSVinicius Costa Gomes 	 */
5475a781ccbSVinicius Costa Gomes 	list_for_each_entry(entry, &q->entries, list)
5485a781ccbSVinicius Costa Gomes 		cycle = ktime_add_ns(cycle, entry->interval);
5495a781ccbSVinicius Costa Gomes 
5505a781ccbSVinicius Costa Gomes 	if (!cycle)
5515a781ccbSVinicius Costa Gomes 		return base;
5525a781ccbSVinicius Costa Gomes 
5535a781ccbSVinicius Costa Gomes 	now = q->get_time();
5545a781ccbSVinicius Costa Gomes 
5555a781ccbSVinicius Costa Gomes 	if (ktime_after(base, now))
5565a781ccbSVinicius Costa Gomes 		return base;
5575a781ccbSVinicius Costa Gomes 
5585a781ccbSVinicius Costa Gomes 	/* Schedule the start time for the beginning of the next
5595a781ccbSVinicius Costa Gomes 	 * cycle.
5605a781ccbSVinicius Costa Gomes 	 */
5615a781ccbSVinicius Costa Gomes 	n = div64_s64(ktime_sub_ns(now, base), cycle);
5625a781ccbSVinicius Costa Gomes 
5635a781ccbSVinicius Costa Gomes 	return ktime_add_ns(base, (n + 1) * cycle);
5645a781ccbSVinicius Costa Gomes }
5655a781ccbSVinicius Costa Gomes 
5665a781ccbSVinicius Costa Gomes static void taprio_start_sched(struct Qdisc *sch, ktime_t start)
5675a781ccbSVinicius Costa Gomes {
5685a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
5695a781ccbSVinicius Costa Gomes 	struct sched_entry *first;
5705a781ccbSVinicius Costa Gomes 	unsigned long flags;
5715a781ccbSVinicius Costa Gomes 
5725a781ccbSVinicius Costa Gomes 	spin_lock_irqsave(&q->current_entry_lock, flags);
5735a781ccbSVinicius Costa Gomes 
5745a781ccbSVinicius Costa Gomes 	first = list_first_entry(&q->entries, struct sched_entry,
5755a781ccbSVinicius Costa Gomes 				 list);
5765a781ccbSVinicius Costa Gomes 
5775a781ccbSVinicius Costa Gomes 	first->close_time = ktime_add_ns(start, first->interval);
5785a781ccbSVinicius Costa Gomes 	atomic_set(&first->budget,
579*7b9eba7bSLeandro Dorileo 		   (first->interval * 1000) /
580*7b9eba7bSLeandro Dorileo 		   atomic64_read(&q->picos_per_byte));
5815a781ccbSVinicius Costa Gomes 	rcu_assign_pointer(q->current_entry, NULL);
5825a781ccbSVinicius Costa Gomes 
5835a781ccbSVinicius Costa Gomes 	spin_unlock_irqrestore(&q->current_entry_lock, flags);
5845a781ccbSVinicius Costa Gomes 
5855a781ccbSVinicius Costa Gomes 	hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
5865a781ccbSVinicius Costa Gomes }
5875a781ccbSVinicius Costa Gomes 
588*7b9eba7bSLeandro Dorileo static void taprio_set_picos_per_byte(struct net_device *dev,
589*7b9eba7bSLeandro Dorileo 				      struct taprio_sched *q)
590*7b9eba7bSLeandro Dorileo {
591*7b9eba7bSLeandro Dorileo 	struct ethtool_link_ksettings ecmd;
592*7b9eba7bSLeandro Dorileo 	int picos_per_byte = -1;
593*7b9eba7bSLeandro Dorileo 
594*7b9eba7bSLeandro Dorileo 	if (!__ethtool_get_link_ksettings(dev, &ecmd) &&
595*7b9eba7bSLeandro Dorileo 	    ecmd.base.speed != SPEED_UNKNOWN)
596*7b9eba7bSLeandro Dorileo 		picos_per_byte = div64_s64(NSEC_PER_SEC * 1000LL * 8,
597*7b9eba7bSLeandro Dorileo 					   ecmd.base.speed * 1000 * 1000);
598*7b9eba7bSLeandro Dorileo 
599*7b9eba7bSLeandro Dorileo 	atomic64_set(&q->picos_per_byte, picos_per_byte);
600*7b9eba7bSLeandro Dorileo 	netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
601*7b9eba7bSLeandro Dorileo 		   dev->name, (long long)atomic64_read(&q->picos_per_byte),
602*7b9eba7bSLeandro Dorileo 		   ecmd.base.speed);
603*7b9eba7bSLeandro Dorileo }
604*7b9eba7bSLeandro Dorileo 
605*7b9eba7bSLeandro Dorileo static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
606*7b9eba7bSLeandro Dorileo 			       void *ptr)
607*7b9eba7bSLeandro Dorileo {
608*7b9eba7bSLeandro Dorileo 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
609*7b9eba7bSLeandro Dorileo 	struct net_device *qdev;
610*7b9eba7bSLeandro Dorileo 	struct taprio_sched *q;
611*7b9eba7bSLeandro Dorileo 	bool found = false;
612*7b9eba7bSLeandro Dorileo 
613*7b9eba7bSLeandro Dorileo 	ASSERT_RTNL();
614*7b9eba7bSLeandro Dorileo 
615*7b9eba7bSLeandro Dorileo 	if (event != NETDEV_UP && event != NETDEV_CHANGE)
616*7b9eba7bSLeandro Dorileo 		return NOTIFY_DONE;
617*7b9eba7bSLeandro Dorileo 
618*7b9eba7bSLeandro Dorileo 	spin_lock(&taprio_list_lock);
619*7b9eba7bSLeandro Dorileo 	list_for_each_entry(q, &taprio_list, taprio_list) {
620*7b9eba7bSLeandro Dorileo 		qdev = qdisc_dev(q->root);
621*7b9eba7bSLeandro Dorileo 		if (qdev == dev) {
622*7b9eba7bSLeandro Dorileo 			found = true;
623*7b9eba7bSLeandro Dorileo 			break;
624*7b9eba7bSLeandro Dorileo 		}
625*7b9eba7bSLeandro Dorileo 	}
626*7b9eba7bSLeandro Dorileo 	spin_unlock(&taprio_list_lock);
627*7b9eba7bSLeandro Dorileo 
628*7b9eba7bSLeandro Dorileo 	if (found)
629*7b9eba7bSLeandro Dorileo 		taprio_set_picos_per_byte(dev, q);
630*7b9eba7bSLeandro Dorileo 
631*7b9eba7bSLeandro Dorileo 	return NOTIFY_DONE;
632*7b9eba7bSLeandro Dorileo }
633*7b9eba7bSLeandro Dorileo 
6345a781ccbSVinicius Costa Gomes static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
6355a781ccbSVinicius Costa Gomes 			 struct netlink_ext_ack *extack)
6365a781ccbSVinicius Costa Gomes {
6375a781ccbSVinicius Costa Gomes 	struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
6385a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
6395a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
6405a781ccbSVinicius Costa Gomes 	struct tc_mqprio_qopt *mqprio = NULL;
6415a781ccbSVinicius Costa Gomes 	int i, err, size;
6425a781ccbSVinicius Costa Gomes 	ktime_t start;
6435a781ccbSVinicius Costa Gomes 
6445a781ccbSVinicius Costa Gomes 	err = nla_parse_nested(tb, TCA_TAPRIO_ATTR_MAX, opt,
6455a781ccbSVinicius Costa Gomes 			       taprio_policy, extack);
6465a781ccbSVinicius Costa Gomes 	if (err < 0)
6475a781ccbSVinicius Costa Gomes 		return err;
6485a781ccbSVinicius Costa Gomes 
6495a781ccbSVinicius Costa Gomes 	err = -EINVAL;
6505a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
6515a781ccbSVinicius Costa Gomes 		mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
6525a781ccbSVinicius Costa Gomes 
6535a781ccbSVinicius Costa Gomes 	err = taprio_parse_mqprio_opt(dev, mqprio, extack);
6545a781ccbSVinicius Costa Gomes 	if (err < 0)
6555a781ccbSVinicius Costa Gomes 		return err;
6565a781ccbSVinicius Costa Gomes 
6575a781ccbSVinicius Costa Gomes 	/* A schedule with less than one entry is an error */
6585a781ccbSVinicius Costa Gomes 	size = parse_taprio_opt(tb, q, extack);
6595a781ccbSVinicius Costa Gomes 	if (size < 0)
6605a781ccbSVinicius Costa Gomes 		return size;
6615a781ccbSVinicius Costa Gomes 
6625a781ccbSVinicius Costa Gomes 	hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
6635a781ccbSVinicius Costa Gomes 	q->advance_timer.function = advance_sched;
6645a781ccbSVinicius Costa Gomes 
6655a781ccbSVinicius Costa Gomes 	switch (q->clockid) {
6665a781ccbSVinicius Costa Gomes 	case CLOCK_REALTIME:
6675a781ccbSVinicius Costa Gomes 		q->get_time = ktime_get_real;
6685a781ccbSVinicius Costa Gomes 		break;
6695a781ccbSVinicius Costa Gomes 	case CLOCK_MONOTONIC:
6705a781ccbSVinicius Costa Gomes 		q->get_time = ktime_get;
6715a781ccbSVinicius Costa Gomes 		break;
6725a781ccbSVinicius Costa Gomes 	case CLOCK_BOOTTIME:
6735a781ccbSVinicius Costa Gomes 		q->get_time = ktime_get_boottime;
6745a781ccbSVinicius Costa Gomes 		break;
6755a781ccbSVinicius Costa Gomes 	case CLOCK_TAI:
6765a781ccbSVinicius Costa Gomes 		q->get_time = ktime_get_clocktai;
6775a781ccbSVinicius Costa Gomes 		break;
6785a781ccbSVinicius Costa Gomes 	default:
6795a781ccbSVinicius Costa Gomes 		return -ENOTSUPP;
6805a781ccbSVinicius Costa Gomes 	}
6815a781ccbSVinicius Costa Gomes 
6825a781ccbSVinicius Costa Gomes 	for (i = 0; i < dev->num_tx_queues; i++) {
6835a781ccbSVinicius Costa Gomes 		struct netdev_queue *dev_queue;
6845a781ccbSVinicius Costa Gomes 		struct Qdisc *qdisc;
6855a781ccbSVinicius Costa Gomes 
6865a781ccbSVinicius Costa Gomes 		dev_queue = netdev_get_tx_queue(dev, i);
6875a781ccbSVinicius Costa Gomes 		qdisc = qdisc_create_dflt(dev_queue,
6885a781ccbSVinicius Costa Gomes 					  &pfifo_qdisc_ops,
6895a781ccbSVinicius Costa Gomes 					  TC_H_MAKE(TC_H_MAJ(sch->handle),
6905a781ccbSVinicius Costa Gomes 						    TC_H_MIN(i + 1)),
6915a781ccbSVinicius Costa Gomes 					  extack);
6925a781ccbSVinicius Costa Gomes 		if (!qdisc)
6935a781ccbSVinicius Costa Gomes 			return -ENOMEM;
6945a781ccbSVinicius Costa Gomes 
6955a781ccbSVinicius Costa Gomes 		if (i < dev->real_num_tx_queues)
6965a781ccbSVinicius Costa Gomes 			qdisc_hash_add(qdisc, false);
6975a781ccbSVinicius Costa Gomes 
6985a781ccbSVinicius Costa Gomes 		q->qdiscs[i] = qdisc;
6995a781ccbSVinicius Costa Gomes 	}
7005a781ccbSVinicius Costa Gomes 
7015a781ccbSVinicius Costa Gomes 	if (mqprio) {
7025a781ccbSVinicius Costa Gomes 		netdev_set_num_tc(dev, mqprio->num_tc);
7035a781ccbSVinicius Costa Gomes 		for (i = 0; i < mqprio->num_tc; i++)
7045a781ccbSVinicius Costa Gomes 			netdev_set_tc_queue(dev, i,
7055a781ccbSVinicius Costa Gomes 					    mqprio->count[i],
7065a781ccbSVinicius Costa Gomes 					    mqprio->offset[i]);
7075a781ccbSVinicius Costa Gomes 
7085a781ccbSVinicius Costa Gomes 		/* Always use supplied priority mappings */
7095a781ccbSVinicius Costa Gomes 		for (i = 0; i < TC_BITMASK + 1; i++)
7105a781ccbSVinicius Costa Gomes 			netdev_set_prio_tc_map(dev, i,
7115a781ccbSVinicius Costa Gomes 					       mqprio->prio_tc_map[i]);
7125a781ccbSVinicius Costa Gomes 	}
7135a781ccbSVinicius Costa Gomes 
714*7b9eba7bSLeandro Dorileo 	taprio_set_picos_per_byte(dev, q);
7155a781ccbSVinicius Costa Gomes 	start = taprio_get_start_time(sch);
7165a781ccbSVinicius Costa Gomes 	if (!start)
7175a781ccbSVinicius Costa Gomes 		return 0;
7185a781ccbSVinicius Costa Gomes 
7195a781ccbSVinicius Costa Gomes 	taprio_start_sched(sch, start);
7205a781ccbSVinicius Costa Gomes 
7215a781ccbSVinicius Costa Gomes 	return 0;
7225a781ccbSVinicius Costa Gomes }
7235a781ccbSVinicius Costa Gomes 
7245a781ccbSVinicius Costa Gomes static void taprio_destroy(struct Qdisc *sch)
7255a781ccbSVinicius Costa Gomes {
7265a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
7275a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
7285a781ccbSVinicius Costa Gomes 	struct sched_entry *entry, *n;
7295a781ccbSVinicius Costa Gomes 	unsigned int i;
7305a781ccbSVinicius Costa Gomes 
731*7b9eba7bSLeandro Dorileo 	spin_lock(&taprio_list_lock);
732*7b9eba7bSLeandro Dorileo 	list_del(&q->taprio_list);
733*7b9eba7bSLeandro Dorileo 	spin_unlock(&taprio_list_lock);
734*7b9eba7bSLeandro Dorileo 
7355a781ccbSVinicius Costa Gomes 	hrtimer_cancel(&q->advance_timer);
7365a781ccbSVinicius Costa Gomes 
7375a781ccbSVinicius Costa Gomes 	if (q->qdiscs) {
7385a781ccbSVinicius Costa Gomes 		for (i = 0; i < dev->num_tx_queues && q->qdiscs[i]; i++)
7395a781ccbSVinicius Costa Gomes 			qdisc_put(q->qdiscs[i]);
7405a781ccbSVinicius Costa Gomes 
7415a781ccbSVinicius Costa Gomes 		kfree(q->qdiscs);
7425a781ccbSVinicius Costa Gomes 	}
7435a781ccbSVinicius Costa Gomes 	q->qdiscs = NULL;
7445a781ccbSVinicius Costa Gomes 
7455a781ccbSVinicius Costa Gomes 	netdev_set_num_tc(dev, 0);
7465a781ccbSVinicius Costa Gomes 
7475a781ccbSVinicius Costa Gomes 	list_for_each_entry_safe(entry, n, &q->entries, list) {
7485a781ccbSVinicius Costa Gomes 		list_del(&entry->list);
7495a781ccbSVinicius Costa Gomes 		kfree(entry);
7505a781ccbSVinicius Costa Gomes 	}
7515a781ccbSVinicius Costa Gomes }
7525a781ccbSVinicius Costa Gomes 
7535a781ccbSVinicius Costa Gomes static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
7545a781ccbSVinicius Costa Gomes 		       struct netlink_ext_ack *extack)
7555a781ccbSVinicius Costa Gomes {
7565a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
7575a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
7585a781ccbSVinicius Costa Gomes 
7595a781ccbSVinicius Costa Gomes 	INIT_LIST_HEAD(&q->entries);
7605a781ccbSVinicius Costa Gomes 	spin_lock_init(&q->current_entry_lock);
7615a781ccbSVinicius Costa Gomes 
7625a781ccbSVinicius Costa Gomes 	/* We may overwrite the configuration later */
7635a781ccbSVinicius Costa Gomes 	hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
7645a781ccbSVinicius Costa Gomes 
7655a781ccbSVinicius Costa Gomes 	q->root = sch;
7665a781ccbSVinicius Costa Gomes 
7675a781ccbSVinicius Costa Gomes 	/* We only support static clockids. Use an invalid value as default
7685a781ccbSVinicius Costa Gomes 	 * and get the valid one on taprio_change().
7695a781ccbSVinicius Costa Gomes 	 */
7705a781ccbSVinicius Costa Gomes 	q->clockid = -1;
7715a781ccbSVinicius Costa Gomes 
7725a781ccbSVinicius Costa Gomes 	if (sch->parent != TC_H_ROOT)
7735a781ccbSVinicius Costa Gomes 		return -EOPNOTSUPP;
7745a781ccbSVinicius Costa Gomes 
7755a781ccbSVinicius Costa Gomes 	if (!netif_is_multiqueue(dev))
7765a781ccbSVinicius Costa Gomes 		return -EOPNOTSUPP;
7775a781ccbSVinicius Costa Gomes 
7785a781ccbSVinicius Costa Gomes 	/* pre-allocate qdisc, attachment can't fail */
7795a781ccbSVinicius Costa Gomes 	q->qdiscs = kcalloc(dev->num_tx_queues,
7805a781ccbSVinicius Costa Gomes 			    sizeof(q->qdiscs[0]),
7815a781ccbSVinicius Costa Gomes 			    GFP_KERNEL);
7825a781ccbSVinicius Costa Gomes 
7835a781ccbSVinicius Costa Gomes 	if (!q->qdiscs)
7845a781ccbSVinicius Costa Gomes 		return -ENOMEM;
7855a781ccbSVinicius Costa Gomes 
7865a781ccbSVinicius Costa Gomes 	if (!opt)
7875a781ccbSVinicius Costa Gomes 		return -EINVAL;
7885a781ccbSVinicius Costa Gomes 
789*7b9eba7bSLeandro Dorileo 	spin_lock(&taprio_list_lock);
790*7b9eba7bSLeandro Dorileo 	list_add(&q->taprio_list, &taprio_list);
791*7b9eba7bSLeandro Dorileo 	spin_unlock(&taprio_list_lock);
792*7b9eba7bSLeandro Dorileo 
7935a781ccbSVinicius Costa Gomes 	return taprio_change(sch, opt, extack);
7945a781ccbSVinicius Costa Gomes }
7955a781ccbSVinicius Costa Gomes 
7965a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
7975a781ccbSVinicius Costa Gomes 					     unsigned long cl)
7985a781ccbSVinicius Costa Gomes {
7995a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
8005a781ccbSVinicius Costa Gomes 	unsigned long ntx = cl - 1;
8015a781ccbSVinicius Costa Gomes 
8025a781ccbSVinicius Costa Gomes 	if (ntx >= dev->num_tx_queues)
8035a781ccbSVinicius Costa Gomes 		return NULL;
8045a781ccbSVinicius Costa Gomes 
8055a781ccbSVinicius Costa Gomes 	return netdev_get_tx_queue(dev, ntx);
8065a781ccbSVinicius Costa Gomes }
8075a781ccbSVinicius Costa Gomes 
8085a781ccbSVinicius Costa Gomes static int taprio_graft(struct Qdisc *sch, unsigned long cl,
8095a781ccbSVinicius Costa Gomes 			struct Qdisc *new, struct Qdisc **old,
8105a781ccbSVinicius Costa Gomes 			struct netlink_ext_ack *extack)
8115a781ccbSVinicius Costa Gomes {
8125a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
8135a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
8145a781ccbSVinicius Costa Gomes 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
8155a781ccbSVinicius Costa Gomes 
8165a781ccbSVinicius Costa Gomes 	if (!dev_queue)
8175a781ccbSVinicius Costa Gomes 		return -EINVAL;
8185a781ccbSVinicius Costa Gomes 
8195a781ccbSVinicius Costa Gomes 	if (dev->flags & IFF_UP)
8205a781ccbSVinicius Costa Gomes 		dev_deactivate(dev);
8215a781ccbSVinicius Costa Gomes 
8225a781ccbSVinicius Costa Gomes 	*old = q->qdiscs[cl - 1];
8235a781ccbSVinicius Costa Gomes 	q->qdiscs[cl - 1] = new;
8245a781ccbSVinicius Costa Gomes 
8255a781ccbSVinicius Costa Gomes 	if (new)
8265a781ccbSVinicius Costa Gomes 		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
8275a781ccbSVinicius Costa Gomes 
8285a781ccbSVinicius Costa Gomes 	if (dev->flags & IFF_UP)
8295a781ccbSVinicius Costa Gomes 		dev_activate(dev);
8305a781ccbSVinicius Costa Gomes 
8315a781ccbSVinicius Costa Gomes 	return 0;
8325a781ccbSVinicius Costa Gomes }
8335a781ccbSVinicius Costa Gomes 
8345a781ccbSVinicius Costa Gomes static int dump_entry(struct sk_buff *msg,
8355a781ccbSVinicius Costa Gomes 		      const struct sched_entry *entry)
8365a781ccbSVinicius Costa Gomes {
8375a781ccbSVinicius Costa Gomes 	struct nlattr *item;
8385a781ccbSVinicius Costa Gomes 
8395a781ccbSVinicius Costa Gomes 	item = nla_nest_start(msg, TCA_TAPRIO_SCHED_ENTRY);
8405a781ccbSVinicius Costa Gomes 	if (!item)
8415a781ccbSVinicius Costa Gomes 		return -ENOSPC;
8425a781ccbSVinicius Costa Gomes 
8435a781ccbSVinicius Costa Gomes 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
8445a781ccbSVinicius Costa Gomes 		goto nla_put_failure;
8455a781ccbSVinicius Costa Gomes 
8465a781ccbSVinicius Costa Gomes 	if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
8475a781ccbSVinicius Costa Gomes 		goto nla_put_failure;
8485a781ccbSVinicius Costa Gomes 
8495a781ccbSVinicius Costa Gomes 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
8505a781ccbSVinicius Costa Gomes 			entry->gate_mask))
8515a781ccbSVinicius Costa Gomes 		goto nla_put_failure;
8525a781ccbSVinicius Costa Gomes 
8535a781ccbSVinicius Costa Gomes 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
8545a781ccbSVinicius Costa Gomes 			entry->interval))
8555a781ccbSVinicius Costa Gomes 		goto nla_put_failure;
8565a781ccbSVinicius Costa Gomes 
8575a781ccbSVinicius Costa Gomes 	return nla_nest_end(msg, item);
8585a781ccbSVinicius Costa Gomes 
8595a781ccbSVinicius Costa Gomes nla_put_failure:
8605a781ccbSVinicius Costa Gomes 	nla_nest_cancel(msg, item);
8615a781ccbSVinicius Costa Gomes 	return -1;
8625a781ccbSVinicius Costa Gomes }
8635a781ccbSVinicius Costa Gomes 
8645a781ccbSVinicius Costa Gomes static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
8655a781ccbSVinicius Costa Gomes {
8665a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
8675a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
8685a781ccbSVinicius Costa Gomes 	struct tc_mqprio_qopt opt = { 0 };
8695a781ccbSVinicius Costa Gomes 	struct nlattr *nest, *entry_list;
8705a781ccbSVinicius Costa Gomes 	struct sched_entry *entry;
8715a781ccbSVinicius Costa Gomes 	unsigned int i;
8725a781ccbSVinicius Costa Gomes 
8735a781ccbSVinicius Costa Gomes 	opt.num_tc = netdev_get_num_tc(dev);
8745a781ccbSVinicius Costa Gomes 	memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
8755a781ccbSVinicius Costa Gomes 
8765a781ccbSVinicius Costa Gomes 	for (i = 0; i < netdev_get_num_tc(dev); i++) {
8775a781ccbSVinicius Costa Gomes 		opt.count[i] = dev->tc_to_txq[i].count;
8785a781ccbSVinicius Costa Gomes 		opt.offset[i] = dev->tc_to_txq[i].offset;
8795a781ccbSVinicius Costa Gomes 	}
8805a781ccbSVinicius Costa Gomes 
8815a781ccbSVinicius Costa Gomes 	nest = nla_nest_start(skb, TCA_OPTIONS);
8825a781ccbSVinicius Costa Gomes 	if (!nest)
8835a781ccbSVinicius Costa Gomes 		return -ENOSPC;
8845a781ccbSVinicius Costa Gomes 
8855a781ccbSVinicius Costa Gomes 	if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
8865a781ccbSVinicius Costa Gomes 		goto options_error;
8875a781ccbSVinicius Costa Gomes 
8885a781ccbSVinicius Costa Gomes 	if (nla_put_s64(skb, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
8895a781ccbSVinicius Costa Gomes 			q->base_time, TCA_TAPRIO_PAD))
8905a781ccbSVinicius Costa Gomes 		goto options_error;
8915a781ccbSVinicius Costa Gomes 
8925a781ccbSVinicius Costa Gomes 	if (nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
8935a781ccbSVinicius Costa Gomes 		goto options_error;
8945a781ccbSVinicius Costa Gomes 
8955a781ccbSVinicius Costa Gomes 	entry_list = nla_nest_start(skb, TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
8965a781ccbSVinicius Costa Gomes 	if (!entry_list)
8975a781ccbSVinicius Costa Gomes 		goto options_error;
8985a781ccbSVinicius Costa Gomes 
8995a781ccbSVinicius Costa Gomes 	list_for_each_entry(entry, &q->entries, list) {
9005a781ccbSVinicius Costa Gomes 		if (dump_entry(skb, entry) < 0)
9015a781ccbSVinicius Costa Gomes 			goto options_error;
9025a781ccbSVinicius Costa Gomes 	}
9035a781ccbSVinicius Costa Gomes 
9045a781ccbSVinicius Costa Gomes 	nla_nest_end(skb, entry_list);
9055a781ccbSVinicius Costa Gomes 
9065a781ccbSVinicius Costa Gomes 	return nla_nest_end(skb, nest);
9075a781ccbSVinicius Costa Gomes 
9085a781ccbSVinicius Costa Gomes options_error:
9095a781ccbSVinicius Costa Gomes 	nla_nest_cancel(skb, nest);
9105a781ccbSVinicius Costa Gomes 	return -1;
9115a781ccbSVinicius Costa Gomes }
9125a781ccbSVinicius Costa Gomes 
9135a781ccbSVinicius Costa Gomes static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
9145a781ccbSVinicius Costa Gomes {
9155a781ccbSVinicius Costa Gomes 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
9165a781ccbSVinicius Costa Gomes 
9175a781ccbSVinicius Costa Gomes 	if (!dev_queue)
9185a781ccbSVinicius Costa Gomes 		return NULL;
9195a781ccbSVinicius Costa Gomes 
9205a781ccbSVinicius Costa Gomes 	return dev_queue->qdisc_sleeping;
9215a781ccbSVinicius Costa Gomes }
9225a781ccbSVinicius Costa Gomes 
9235a781ccbSVinicius Costa Gomes static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
9245a781ccbSVinicius Costa Gomes {
9255a781ccbSVinicius Costa Gomes 	unsigned int ntx = TC_H_MIN(classid);
9265a781ccbSVinicius Costa Gomes 
9275a781ccbSVinicius Costa Gomes 	if (!taprio_queue_get(sch, ntx))
9285a781ccbSVinicius Costa Gomes 		return 0;
9295a781ccbSVinicius Costa Gomes 	return ntx;
9305a781ccbSVinicius Costa Gomes }
9315a781ccbSVinicius Costa Gomes 
9325a781ccbSVinicius Costa Gomes static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
9335a781ccbSVinicius Costa Gomes 			     struct sk_buff *skb, struct tcmsg *tcm)
9345a781ccbSVinicius Costa Gomes {
9355a781ccbSVinicius Costa Gomes 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
9365a781ccbSVinicius Costa Gomes 
9375a781ccbSVinicius Costa Gomes 	tcm->tcm_parent = TC_H_ROOT;
9385a781ccbSVinicius Costa Gomes 	tcm->tcm_handle |= TC_H_MIN(cl);
9395a781ccbSVinicius Costa Gomes 	tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
9405a781ccbSVinicius Costa Gomes 
9415a781ccbSVinicius Costa Gomes 	return 0;
9425a781ccbSVinicius Costa Gomes }
9435a781ccbSVinicius Costa Gomes 
9445a781ccbSVinicius Costa Gomes static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
9455a781ccbSVinicius Costa Gomes 				   struct gnet_dump *d)
9465a781ccbSVinicius Costa Gomes 	__releases(d->lock)
9475a781ccbSVinicius Costa Gomes 	__acquires(d->lock)
9485a781ccbSVinicius Costa Gomes {
9495a781ccbSVinicius Costa Gomes 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
9505a781ccbSVinicius Costa Gomes 
9515a781ccbSVinicius Costa Gomes 	sch = dev_queue->qdisc_sleeping;
9525a781ccbSVinicius Costa Gomes 	if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 ||
9535dd431b6SPaolo Abeni 	    qdisc_qstats_copy(d, sch) < 0)
9545a781ccbSVinicius Costa Gomes 		return -1;
9555a781ccbSVinicius Costa Gomes 	return 0;
9565a781ccbSVinicius Costa Gomes }
9575a781ccbSVinicius Costa Gomes 
9585a781ccbSVinicius Costa Gomes static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
9595a781ccbSVinicius Costa Gomes {
9605a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
9615a781ccbSVinicius Costa Gomes 	unsigned long ntx;
9625a781ccbSVinicius Costa Gomes 
9635a781ccbSVinicius Costa Gomes 	if (arg->stop)
9645a781ccbSVinicius Costa Gomes 		return;
9655a781ccbSVinicius Costa Gomes 
9665a781ccbSVinicius Costa Gomes 	arg->count = arg->skip;
9675a781ccbSVinicius Costa Gomes 	for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
9685a781ccbSVinicius Costa Gomes 		if (arg->fn(sch, ntx + 1, arg) < 0) {
9695a781ccbSVinicius Costa Gomes 			arg->stop = 1;
9705a781ccbSVinicius Costa Gomes 			break;
9715a781ccbSVinicius Costa Gomes 		}
9725a781ccbSVinicius Costa Gomes 		arg->count++;
9735a781ccbSVinicius Costa Gomes 	}
9745a781ccbSVinicius Costa Gomes }
9755a781ccbSVinicius Costa Gomes 
9765a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
9775a781ccbSVinicius Costa Gomes 						struct tcmsg *tcm)
9785a781ccbSVinicius Costa Gomes {
9795a781ccbSVinicius Costa Gomes 	return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
9805a781ccbSVinicius Costa Gomes }
9815a781ccbSVinicius Costa Gomes 
9825a781ccbSVinicius Costa Gomes static const struct Qdisc_class_ops taprio_class_ops = {
9835a781ccbSVinicius Costa Gomes 	.graft		= taprio_graft,
9845a781ccbSVinicius Costa Gomes 	.leaf		= taprio_leaf,
9855a781ccbSVinicius Costa Gomes 	.find		= taprio_find,
9865a781ccbSVinicius Costa Gomes 	.walk		= taprio_walk,
9875a781ccbSVinicius Costa Gomes 	.dump		= taprio_dump_class,
9885a781ccbSVinicius Costa Gomes 	.dump_stats	= taprio_dump_class_stats,
9895a781ccbSVinicius Costa Gomes 	.select_queue	= taprio_select_queue,
9905a781ccbSVinicius Costa Gomes };
9915a781ccbSVinicius Costa Gomes 
9925a781ccbSVinicius Costa Gomes static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
9935a781ccbSVinicius Costa Gomes 	.cl_ops		= &taprio_class_ops,
9945a781ccbSVinicius Costa Gomes 	.id		= "taprio",
9955a781ccbSVinicius Costa Gomes 	.priv_size	= sizeof(struct taprio_sched),
9965a781ccbSVinicius Costa Gomes 	.init		= taprio_init,
9975a781ccbSVinicius Costa Gomes 	.destroy	= taprio_destroy,
9985a781ccbSVinicius Costa Gomes 	.peek		= taprio_peek,
9995a781ccbSVinicius Costa Gomes 	.dequeue	= taprio_dequeue,
10005a781ccbSVinicius Costa Gomes 	.enqueue	= taprio_enqueue,
10015a781ccbSVinicius Costa Gomes 	.dump		= taprio_dump,
10025a781ccbSVinicius Costa Gomes 	.owner		= THIS_MODULE,
10035a781ccbSVinicius Costa Gomes };
10045a781ccbSVinicius Costa Gomes 
1005*7b9eba7bSLeandro Dorileo static struct notifier_block taprio_device_notifier = {
1006*7b9eba7bSLeandro Dorileo 	.notifier_call = taprio_dev_notifier,
1007*7b9eba7bSLeandro Dorileo };
1008*7b9eba7bSLeandro Dorileo 
10095a781ccbSVinicius Costa Gomes static int __init taprio_module_init(void)
10105a781ccbSVinicius Costa Gomes {
1011*7b9eba7bSLeandro Dorileo 	int err = register_netdevice_notifier(&taprio_device_notifier);
1012*7b9eba7bSLeandro Dorileo 
1013*7b9eba7bSLeandro Dorileo 	if (err)
1014*7b9eba7bSLeandro Dorileo 		return err;
1015*7b9eba7bSLeandro Dorileo 
10165a781ccbSVinicius Costa Gomes 	return register_qdisc(&taprio_qdisc_ops);
10175a781ccbSVinicius Costa Gomes }
10185a781ccbSVinicius Costa Gomes 
10195a781ccbSVinicius Costa Gomes static void __exit taprio_module_exit(void)
10205a781ccbSVinicius Costa Gomes {
10215a781ccbSVinicius Costa Gomes 	unregister_qdisc(&taprio_qdisc_ops);
1022*7b9eba7bSLeandro Dorileo 	unregister_netdevice_notifier(&taprio_device_notifier);
10235a781ccbSVinicius Costa Gomes }
10245a781ccbSVinicius Costa Gomes 
10255a781ccbSVinicius Costa Gomes module_init(taprio_module_init);
10265a781ccbSVinicius Costa Gomes module_exit(taprio_module_exit);
10275a781ccbSVinicius Costa Gomes MODULE_LICENSE("GPL");
1028