xref: /openbmc/linux/net/sched/sch_taprio.c (revision bdf366bd867c4565b535a5825df7ddcb4773fc28)
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 
9cc69837fSJakub Kicinski #include <linux/ethtool.h>
105a781ccbSVinicius Costa Gomes #include <linux/types.h>
115a781ccbSVinicius Costa Gomes #include <linux/slab.h>
125a781ccbSVinicius Costa Gomes #include <linux/kernel.h>
135a781ccbSVinicius Costa Gomes #include <linux/string.h>
145a781ccbSVinicius Costa Gomes #include <linux/list.h>
155a781ccbSVinicius Costa Gomes #include <linux/errno.h>
165a781ccbSVinicius Costa Gomes #include <linux/skbuff.h>
1723bddf69SJakub Kicinski #include <linux/math64.h>
185a781ccbSVinicius Costa Gomes #include <linux/module.h>
195a781ccbSVinicius Costa Gomes #include <linux/spinlock.h>
20a3d43c0dSVinicius Costa Gomes #include <linux/rcupdate.h>
21837ced3aSVladimir Oltean #include <linux/time.h>
225a781ccbSVinicius Costa Gomes #include <net/netlink.h>
235a781ccbSVinicius Costa Gomes #include <net/pkt_sched.h>
245a781ccbSVinicius Costa Gomes #include <net/pkt_cls.h>
255a781ccbSVinicius Costa Gomes #include <net/sch_generic.h>
264cfd5779SVedang Patel #include <net/sock.h>
2754002066SVedang Patel #include <net/tcp.h>
285a781ccbSVinicius Costa Gomes 
291dfe086dSVladimir Oltean #include "sch_mqprio_lib.h"
301dfe086dSVladimir Oltean 
317b9eba7bSLeandro Dorileo static LIST_HEAD(taprio_list);
322f530df7SVladimir Oltean static struct static_key_false taprio_have_broken_mqprio;
332f530df7SVladimir Oltean static struct static_key_false taprio_have_working_mqprio;
347b9eba7bSLeandro Dorileo 
355a781ccbSVinicius Costa Gomes #define TAPRIO_ALL_GATES_OPEN -1
365a781ccbSVinicius Costa Gomes 
374cfd5779SVedang Patel #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
389c66d156SVinicius Costa Gomes #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
39a9d62274SVinicius Costa Gomes #define TAPRIO_FLAGS_INVALID U32_MAX
404cfd5779SVedang Patel 
415a781ccbSVinicius Costa Gomes struct sched_entry {
42a306a90cSVladimir Oltean 	/* Durations between this GCL entry and the GCL entry where the
43a306a90cSVladimir Oltean 	 * respective traffic class gate closes
44a306a90cSVladimir Oltean 	 */
45a306a90cSVladimir Oltean 	u64 gate_duration[TC_MAX_QUEUE];
46d2ad689dSVladimir Oltean 	atomic_t budget[TC_MAX_QUEUE];
47a1e6ad30SVladimir Oltean 	/* The qdisc makes some effort so that no packet leaves
48a1e6ad30SVladimir Oltean 	 * after this time
495a781ccbSVinicius Costa Gomes 	 */
50a1e6ad30SVladimir Oltean 	ktime_t gate_close_time[TC_MAX_QUEUE];
51a1e6ad30SVladimir Oltean 	struct list_head list;
52a1e6ad30SVladimir Oltean 	/* Used to calculate when to advance the schedule */
53e5517551SVladimir Oltean 	ktime_t end_time;
544cfd5779SVedang Patel 	ktime_t next_txtime;
555a781ccbSVinicius Costa Gomes 	int index;
565a781ccbSVinicius Costa Gomes 	u32 gate_mask;
575a781ccbSVinicius Costa Gomes 	u32 interval;
585a781ccbSVinicius Costa Gomes 	u8 command;
595a781ccbSVinicius Costa Gomes };
605a781ccbSVinicius Costa Gomes 
61a3d43c0dSVinicius Costa Gomes struct sched_gate_list {
62a306a90cSVladimir Oltean 	/* Longest non-zero contiguous gate durations per traffic class,
63a306a90cSVladimir Oltean 	 * or 0 if a traffic class gate never opens during the schedule.
64a306a90cSVladimir Oltean 	 */
65a306a90cSVladimir Oltean 	u64 max_open_gate_duration[TC_MAX_QUEUE];
66a878fd46SVladimir Oltean 	u32 max_frm_len[TC_MAX_QUEUE]; /* for the fast path */
67fed87cc6SVladimir Oltean 	u32 max_sdu[TC_MAX_QUEUE]; /* for dump */
68a3d43c0dSVinicius Costa Gomes 	struct rcu_head rcu;
69a3d43c0dSVinicius Costa Gomes 	struct list_head entries;
70a3d43c0dSVinicius Costa Gomes 	size_t num_entries;
71e5517551SVladimir Oltean 	ktime_t cycle_end_time;
726ca6a665SVinicius Costa Gomes 	s64 cycle_time;
73c25031e9SVinicius Costa Gomes 	s64 cycle_time_extension;
74a3d43c0dSVinicius Costa Gomes 	s64 base_time;
75a3d43c0dSVinicius Costa Gomes };
76a3d43c0dSVinicius Costa Gomes 
775a781ccbSVinicius Costa Gomes struct taprio_sched {
785a781ccbSVinicius Costa Gomes 	struct Qdisc **qdiscs;
795a781ccbSVinicius Costa Gomes 	struct Qdisc *root;
804cfd5779SVedang Patel 	u32 flags;
817ede7b03SVedang Patel 	enum tk_offsets tk_offset;
825a781ccbSVinicius Costa Gomes 	int clockid;
83db46e3a8SVladimir Oltean 	bool offloaded;
842f530df7SVladimir Oltean 	bool detected_mqprio;
852f530df7SVladimir Oltean 	bool broken_mqprio;
867b9eba7bSLeandro Dorileo 	atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
875a781ccbSVinicius Costa Gomes 				    * speeds it's sub-nanoseconds per byte
885a781ccbSVinicius Costa Gomes 				    */
895a781ccbSVinicius Costa Gomes 
905a781ccbSVinicius Costa Gomes 	/* Protects the update side of the RCU protected current_entry */
915a781ccbSVinicius Costa Gomes 	spinlock_t current_entry_lock;
925a781ccbSVinicius Costa Gomes 	struct sched_entry __rcu *current_entry;
93a3d43c0dSVinicius Costa Gomes 	struct sched_gate_list __rcu *oper_sched;
94a3d43c0dSVinicius Costa Gomes 	struct sched_gate_list __rcu *admin_sched;
955a781ccbSVinicius Costa Gomes 	struct hrtimer advance_timer;
967b9eba7bSLeandro Dorileo 	struct list_head taprio_list;
972f530df7SVladimir Oltean 	int cur_txq[TC_MAX_QUEUE];
98fed87cc6SVladimir Oltean 	u32 max_sdu[TC_MAX_QUEUE]; /* save info from the user */
99a5b64700SVedang Patel 	u32 txtime_delay;
1005a781ccbSVinicius Costa Gomes };
1015a781ccbSVinicius Costa Gomes 
1029c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload {
1039c66d156SVinicius Costa Gomes 	refcount_t users;
1049c66d156SVinicius Costa Gomes 	struct tc_taprio_qopt_offload offload;
1059c66d156SVinicius Costa Gomes };
1069c66d156SVinicius Costa Gomes 
107a306a90cSVladimir Oltean static void taprio_calculate_gate_durations(struct taprio_sched *q,
108a306a90cSVladimir Oltean 					    struct sched_gate_list *sched)
109a306a90cSVladimir Oltean {
110a306a90cSVladimir Oltean 	struct net_device *dev = qdisc_dev(q->root);
111a306a90cSVladimir Oltean 	int num_tc = netdev_get_num_tc(dev);
112a306a90cSVladimir Oltean 	struct sched_entry *entry, *cur;
113a306a90cSVladimir Oltean 	int tc;
114a306a90cSVladimir Oltean 
115a306a90cSVladimir Oltean 	list_for_each_entry(entry, &sched->entries, list) {
116a306a90cSVladimir Oltean 		u32 gates_still_open = entry->gate_mask;
117a306a90cSVladimir Oltean 
118a306a90cSVladimir Oltean 		/* For each traffic class, calculate each open gate duration,
119a306a90cSVladimir Oltean 		 * starting at this schedule entry and ending at the schedule
120a306a90cSVladimir Oltean 		 * entry containing a gate close event for that TC.
121a306a90cSVladimir Oltean 		 */
122a306a90cSVladimir Oltean 		cur = entry;
123a306a90cSVladimir Oltean 
124a306a90cSVladimir Oltean 		do {
125a306a90cSVladimir Oltean 			if (!gates_still_open)
126a306a90cSVladimir Oltean 				break;
127a306a90cSVladimir Oltean 
128a306a90cSVladimir Oltean 			for (tc = 0; tc < num_tc; tc++) {
129a306a90cSVladimir Oltean 				if (!(gates_still_open & BIT(tc)))
130a306a90cSVladimir Oltean 					continue;
131a306a90cSVladimir Oltean 
132a306a90cSVladimir Oltean 				if (cur->gate_mask & BIT(tc))
133a306a90cSVladimir Oltean 					entry->gate_duration[tc] += cur->interval;
134a306a90cSVladimir Oltean 				else
135a306a90cSVladimir Oltean 					gates_still_open &= ~BIT(tc);
136a306a90cSVladimir Oltean 			}
137a306a90cSVladimir Oltean 
138a306a90cSVladimir Oltean 			cur = list_next_entry_circular(cur, &sched->entries, list);
139a306a90cSVladimir Oltean 		} while (cur != entry);
140a306a90cSVladimir Oltean 
141a306a90cSVladimir Oltean 		/* Keep track of the maximum gate duration for each traffic
142a306a90cSVladimir Oltean 		 * class, taking care to not confuse a traffic class which is
143a306a90cSVladimir Oltean 		 * temporarily closed with one that is always closed.
144a306a90cSVladimir Oltean 		 */
145a306a90cSVladimir Oltean 		for (tc = 0; tc < num_tc; tc++)
146a306a90cSVladimir Oltean 			if (entry->gate_duration[tc] &&
147a306a90cSVladimir Oltean 			    sched->max_open_gate_duration[tc] < entry->gate_duration[tc])
148a306a90cSVladimir Oltean 				sched->max_open_gate_duration[tc] = entry->gate_duration[tc];
149a306a90cSVladimir Oltean 	}
150a306a90cSVladimir Oltean }
151a306a90cSVladimir Oltean 
152a1e6ad30SVladimir Oltean static bool taprio_entry_allows_tx(ktime_t skb_end_time,
153a1e6ad30SVladimir Oltean 				   struct sched_entry *entry, int tc)
154a1e6ad30SVladimir Oltean {
155a1e6ad30SVladimir Oltean 	return ktime_before(skb_end_time, entry->gate_close_time[tc]);
156a1e6ad30SVladimir Oltean }
157a1e6ad30SVladimir Oltean 
158a3d43c0dSVinicius Costa Gomes static ktime_t sched_base_time(const struct sched_gate_list *sched)
159a3d43c0dSVinicius Costa Gomes {
160a3d43c0dSVinicius Costa Gomes 	if (!sched)
161a3d43c0dSVinicius Costa Gomes 		return KTIME_MAX;
162a3d43c0dSVinicius Costa Gomes 
163a3d43c0dSVinicius Costa Gomes 	return ns_to_ktime(sched->base_time);
164a3d43c0dSVinicius Costa Gomes }
165a3d43c0dSVinicius Costa Gomes 
1666dc25401SEric Dumazet static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
1677ede7b03SVedang Patel {
1686dc25401SEric Dumazet 	/* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
1696dc25401SEric Dumazet 	enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
1707ede7b03SVedang Patel 
1716dc25401SEric Dumazet 	switch (tk_offset) {
1727ede7b03SVedang Patel 	case TK_OFFS_MAX:
1737ede7b03SVedang Patel 		return mono;
1747ede7b03SVedang Patel 	default:
1756dc25401SEric Dumazet 		return ktime_mono_to_any(mono, tk_offset);
1766dc25401SEric Dumazet 	}
1777ede7b03SVedang Patel }
1787ede7b03SVedang Patel 
1796dc25401SEric Dumazet static ktime_t taprio_get_time(const struct taprio_sched *q)
1806dc25401SEric Dumazet {
1816dc25401SEric Dumazet 	return taprio_mono_to_any(q, ktime_get());
1827ede7b03SVedang Patel }
1837ede7b03SVedang Patel 
184a3d43c0dSVinicius Costa Gomes static void taprio_free_sched_cb(struct rcu_head *head)
185a3d43c0dSVinicius Costa Gomes {
186a3d43c0dSVinicius Costa Gomes 	struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
187a3d43c0dSVinicius Costa Gomes 	struct sched_entry *entry, *n;
188a3d43c0dSVinicius Costa Gomes 
189a3d43c0dSVinicius Costa Gomes 	list_for_each_entry_safe(entry, n, &sched->entries, list) {
190a3d43c0dSVinicius Costa Gomes 		list_del(&entry->list);
191a3d43c0dSVinicius Costa Gomes 		kfree(entry);
192a3d43c0dSVinicius Costa Gomes 	}
193a3d43c0dSVinicius Costa Gomes 
194a3d43c0dSVinicius Costa Gomes 	kfree(sched);
195a3d43c0dSVinicius Costa Gomes }
196a3d43c0dSVinicius Costa Gomes 
197a3d43c0dSVinicius Costa Gomes static void switch_schedules(struct taprio_sched *q,
198a3d43c0dSVinicius Costa Gomes 			     struct sched_gate_list **admin,
199a3d43c0dSVinicius Costa Gomes 			     struct sched_gate_list **oper)
200a3d43c0dSVinicius Costa Gomes {
201a3d43c0dSVinicius Costa Gomes 	rcu_assign_pointer(q->oper_sched, *admin);
202a3d43c0dSVinicius Costa Gomes 	rcu_assign_pointer(q->admin_sched, NULL);
203a3d43c0dSVinicius Costa Gomes 
204a3d43c0dSVinicius Costa Gomes 	if (*oper)
205a3d43c0dSVinicius Costa Gomes 		call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
206a3d43c0dSVinicius Costa Gomes 
207a3d43c0dSVinicius Costa Gomes 	*oper = *admin;
208a3d43c0dSVinicius Costa Gomes 	*admin = NULL;
209a3d43c0dSVinicius Costa Gomes }
210a3d43c0dSVinicius Costa Gomes 
2114cfd5779SVedang Patel /* Get how much time has been already elapsed in the current cycle. */
2124cfd5779SVedang Patel static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
2134cfd5779SVedang Patel {
2144cfd5779SVedang Patel 	ktime_t time_since_sched_start;
2154cfd5779SVedang Patel 	s32 time_elapsed;
2164cfd5779SVedang Patel 
2174cfd5779SVedang Patel 	time_since_sched_start = ktime_sub(time, sched->base_time);
2184cfd5779SVedang Patel 	div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
2194cfd5779SVedang Patel 
2204cfd5779SVedang Patel 	return time_elapsed;
2214cfd5779SVedang Patel }
2224cfd5779SVedang Patel 
2234cfd5779SVedang Patel static ktime_t get_interval_end_time(struct sched_gate_list *sched,
2244cfd5779SVedang Patel 				     struct sched_gate_list *admin,
2254cfd5779SVedang Patel 				     struct sched_entry *entry,
2264cfd5779SVedang Patel 				     ktime_t intv_start)
2274cfd5779SVedang Patel {
2284cfd5779SVedang Patel 	s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
2294cfd5779SVedang Patel 	ktime_t intv_end, cycle_ext_end, cycle_end;
2304cfd5779SVedang Patel 
2314cfd5779SVedang Patel 	cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
2324cfd5779SVedang Patel 	intv_end = ktime_add_ns(intv_start, entry->interval);
2334cfd5779SVedang Patel 	cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
2344cfd5779SVedang Patel 
2354cfd5779SVedang Patel 	if (ktime_before(intv_end, cycle_end))
2364cfd5779SVedang Patel 		return intv_end;
2374cfd5779SVedang Patel 	else if (admin && admin != sched &&
2384cfd5779SVedang Patel 		 ktime_after(admin->base_time, cycle_end) &&
2394cfd5779SVedang Patel 		 ktime_before(admin->base_time, cycle_ext_end))
2404cfd5779SVedang Patel 		return admin->base_time;
2414cfd5779SVedang Patel 	else
2424cfd5779SVedang Patel 		return cycle_end;
2434cfd5779SVedang Patel }
2444cfd5779SVedang Patel 
2454cfd5779SVedang Patel static int length_to_duration(struct taprio_sched *q, int len)
2464cfd5779SVedang Patel {
247837ced3aSVladimir Oltean 	return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC);
2484cfd5779SVedang Patel }
2494cfd5779SVedang Patel 
250fed87cc6SVladimir Oltean static int duration_to_length(struct taprio_sched *q, u64 duration)
251fed87cc6SVladimir Oltean {
252fed87cc6SVladimir Oltean 	return div_u64(duration * PSEC_PER_NSEC, atomic64_read(&q->picos_per_byte));
253fed87cc6SVladimir Oltean }
254fed87cc6SVladimir Oltean 
255fed87cc6SVladimir Oltean /* Sets sched->max_sdu[] and sched->max_frm_len[] to the minimum between the
256fed87cc6SVladimir Oltean  * q->max_sdu[] requested by the user and the max_sdu dynamically determined by
257fed87cc6SVladimir Oltean  * the maximum open gate durations at the given link speed.
258fed87cc6SVladimir Oltean  */
259a878fd46SVladimir Oltean static void taprio_update_queue_max_sdu(struct taprio_sched *q,
260fed87cc6SVladimir Oltean 					struct sched_gate_list *sched,
261fed87cc6SVladimir Oltean 					struct qdisc_size_table *stab)
262a878fd46SVladimir Oltean {
263a878fd46SVladimir Oltean 	struct net_device *dev = qdisc_dev(q->root);
264a878fd46SVladimir Oltean 	int num_tc = netdev_get_num_tc(dev);
265fed87cc6SVladimir Oltean 	u32 max_sdu_from_user;
266fed87cc6SVladimir Oltean 	u32 max_sdu_dynamic;
267fed87cc6SVladimir Oltean 	u32 max_sdu;
268a878fd46SVladimir Oltean 	int tc;
269a878fd46SVladimir Oltean 
270a878fd46SVladimir Oltean 	for (tc = 0; tc < num_tc; tc++) {
271fed87cc6SVladimir Oltean 		max_sdu_from_user = q->max_sdu[tc] ?: U32_MAX;
272fed87cc6SVladimir Oltean 
273fed87cc6SVladimir Oltean 		/* TC gate never closes => keep the queueMaxSDU
274fed87cc6SVladimir Oltean 		 * selected by the user
275fed87cc6SVladimir Oltean 		 */
276fed87cc6SVladimir Oltean 		if (sched->max_open_gate_duration[tc] == sched->cycle_time) {
277fed87cc6SVladimir Oltean 			max_sdu_dynamic = U32_MAX;
278fed87cc6SVladimir Oltean 		} else {
279fed87cc6SVladimir Oltean 			u32 max_frm_len;
280fed87cc6SVladimir Oltean 
281fed87cc6SVladimir Oltean 			max_frm_len = duration_to_length(q, sched->max_open_gate_duration[tc]);
282*bdf366bdSVladimir Oltean 			/* Compensate for L1 overhead from size table,
283*bdf366bdSVladimir Oltean 			 * but don't let the frame size go negative
284*bdf366bdSVladimir Oltean 			 */
285*bdf366bdSVladimir Oltean 			if (stab) {
286fed87cc6SVladimir Oltean 				max_frm_len -= stab->szopts.overhead;
287*bdf366bdSVladimir Oltean 				max_frm_len = max_t(int, max_frm_len,
288*bdf366bdSVladimir Oltean 						    dev->hard_header_len + 1);
289*bdf366bdSVladimir Oltean 			}
290fed87cc6SVladimir Oltean 			max_sdu_dynamic = max_frm_len - dev->hard_header_len;
291fed87cc6SVladimir Oltean 		}
292fed87cc6SVladimir Oltean 
293fed87cc6SVladimir Oltean 		max_sdu = min(max_sdu_dynamic, max_sdu_from_user);
294fed87cc6SVladimir Oltean 
295fed87cc6SVladimir Oltean 		if (max_sdu != U32_MAX) {
296fed87cc6SVladimir Oltean 			sched->max_frm_len[tc] = max_sdu + dev->hard_header_len;
297fed87cc6SVladimir Oltean 			sched->max_sdu[tc] = max_sdu;
298fed87cc6SVladimir Oltean 		} else {
299a878fd46SVladimir Oltean 			sched->max_frm_len[tc] = U32_MAX; /* never oversized */
300fed87cc6SVladimir Oltean 			sched->max_sdu[tc] = 0;
301fed87cc6SVladimir Oltean 		}
302a878fd46SVladimir Oltean 	}
303a878fd46SVladimir Oltean }
304a878fd46SVladimir Oltean 
3054cfd5779SVedang Patel /* Returns the entry corresponding to next available interval. If
3064cfd5779SVedang Patel  * validate_interval is set, it only validates whether the timestamp occurs
3074cfd5779SVedang Patel  * when the gate corresponding to the skb's traffic class is open.
3084cfd5779SVedang Patel  */
3094cfd5779SVedang Patel static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
3104cfd5779SVedang Patel 						  struct Qdisc *sch,
3114cfd5779SVedang Patel 						  struct sched_gate_list *sched,
3124cfd5779SVedang Patel 						  struct sched_gate_list *admin,
3134cfd5779SVedang Patel 						  ktime_t time,
3144cfd5779SVedang Patel 						  ktime_t *interval_start,
3154cfd5779SVedang Patel 						  ktime_t *interval_end,
3164cfd5779SVedang Patel 						  bool validate_interval)
3174cfd5779SVedang Patel {
3184cfd5779SVedang Patel 	ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
3194cfd5779SVedang Patel 	ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
3204cfd5779SVedang Patel 	struct sched_entry *entry = NULL, *entry_found = NULL;
3214cfd5779SVedang Patel 	struct taprio_sched *q = qdisc_priv(sch);
3224cfd5779SVedang Patel 	struct net_device *dev = qdisc_dev(sch);
3234cfd5779SVedang Patel 	bool entry_available = false;
3244cfd5779SVedang Patel 	s32 cycle_elapsed;
3254cfd5779SVedang Patel 	int tc, n;
3264cfd5779SVedang Patel 
3274cfd5779SVedang Patel 	tc = netdev_get_prio_tc_map(dev, skb->priority);
3284cfd5779SVedang Patel 	packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
3294cfd5779SVedang Patel 
3304cfd5779SVedang Patel 	*interval_start = 0;
3314cfd5779SVedang Patel 	*interval_end = 0;
3324cfd5779SVedang Patel 
3334cfd5779SVedang Patel 	if (!sched)
3344cfd5779SVedang Patel 		return NULL;
3354cfd5779SVedang Patel 
3364cfd5779SVedang Patel 	cycle = sched->cycle_time;
3374cfd5779SVedang Patel 	cycle_elapsed = get_cycle_time_elapsed(sched, time);
3384cfd5779SVedang Patel 	curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
3394cfd5779SVedang Patel 	cycle_end = ktime_add_ns(curr_intv_end, cycle);
3404cfd5779SVedang Patel 
3414cfd5779SVedang Patel 	list_for_each_entry(entry, &sched->entries, list) {
3424cfd5779SVedang Patel 		curr_intv_start = curr_intv_end;
3434cfd5779SVedang Patel 		curr_intv_end = get_interval_end_time(sched, admin, entry,
3444cfd5779SVedang Patel 						      curr_intv_start);
3454cfd5779SVedang Patel 
3464cfd5779SVedang Patel 		if (ktime_after(curr_intv_start, cycle_end))
3474cfd5779SVedang Patel 			break;
3484cfd5779SVedang Patel 
3494cfd5779SVedang Patel 		if (!(entry->gate_mask & BIT(tc)) ||
3504cfd5779SVedang Patel 		    packet_transmit_time > entry->interval)
3514cfd5779SVedang Patel 			continue;
3524cfd5779SVedang Patel 
3534cfd5779SVedang Patel 		txtime = entry->next_txtime;
3544cfd5779SVedang Patel 
3554cfd5779SVedang Patel 		if (ktime_before(txtime, time) || validate_interval) {
3564cfd5779SVedang Patel 			transmit_end_time = ktime_add_ns(time, packet_transmit_time);
3574cfd5779SVedang Patel 			if ((ktime_before(curr_intv_start, time) &&
3584cfd5779SVedang Patel 			     ktime_before(transmit_end_time, curr_intv_end)) ||
3594cfd5779SVedang Patel 			    (ktime_after(curr_intv_start, time) && !validate_interval)) {
3604cfd5779SVedang Patel 				entry_found = entry;
3614cfd5779SVedang Patel 				*interval_start = curr_intv_start;
3624cfd5779SVedang Patel 				*interval_end = curr_intv_end;
3634cfd5779SVedang Patel 				break;
3644cfd5779SVedang Patel 			} else if (!entry_available && !validate_interval) {
3654cfd5779SVedang Patel 				/* Here, we are just trying to find out the
3664cfd5779SVedang Patel 				 * first available interval in the next cycle.
3674cfd5779SVedang Patel 				 */
3680deee7aaSJiapeng Zhong 				entry_available = true;
3694cfd5779SVedang Patel 				entry_found = entry;
3704cfd5779SVedang Patel 				*interval_start = ktime_add_ns(curr_intv_start, cycle);
3714cfd5779SVedang Patel 				*interval_end = ktime_add_ns(curr_intv_end, cycle);
3724cfd5779SVedang Patel 			}
3734cfd5779SVedang Patel 		} else if (ktime_before(txtime, earliest_txtime) &&
3744cfd5779SVedang Patel 			   !entry_available) {
3754cfd5779SVedang Patel 			earliest_txtime = txtime;
3764cfd5779SVedang Patel 			entry_found = entry;
3774cfd5779SVedang Patel 			n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
3784cfd5779SVedang Patel 			*interval_start = ktime_add(curr_intv_start, n * cycle);
3794cfd5779SVedang Patel 			*interval_end = ktime_add(curr_intv_end, n * cycle);
3804cfd5779SVedang Patel 		}
3814cfd5779SVedang Patel 	}
3824cfd5779SVedang Patel 
3834cfd5779SVedang Patel 	return entry_found;
3844cfd5779SVedang Patel }
3854cfd5779SVedang Patel 
3864cfd5779SVedang Patel static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
3874cfd5779SVedang Patel {
3884cfd5779SVedang Patel 	struct taprio_sched *q = qdisc_priv(sch);
3894cfd5779SVedang Patel 	struct sched_gate_list *sched, *admin;
3904cfd5779SVedang Patel 	ktime_t interval_start, interval_end;
3914cfd5779SVedang Patel 	struct sched_entry *entry;
3924cfd5779SVedang Patel 
3934cfd5779SVedang Patel 	rcu_read_lock();
3944cfd5779SVedang Patel 	sched = rcu_dereference(q->oper_sched);
3954cfd5779SVedang Patel 	admin = rcu_dereference(q->admin_sched);
3964cfd5779SVedang Patel 
3974cfd5779SVedang Patel 	entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
3984cfd5779SVedang Patel 				       &interval_start, &interval_end, true);
3994cfd5779SVedang Patel 	rcu_read_unlock();
4004cfd5779SVedang Patel 
4014cfd5779SVedang Patel 	return entry;
4024cfd5779SVedang Patel }
4034cfd5779SVedang Patel 
4049c66d156SVinicius Costa Gomes static bool taprio_flags_valid(u32 flags)
4059c66d156SVinicius Costa Gomes {
4069c66d156SVinicius Costa Gomes 	/* Make sure no other flag bits are set. */
4079c66d156SVinicius Costa Gomes 	if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
4089c66d156SVinicius Costa Gomes 		      TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
4099c66d156SVinicius Costa Gomes 		return false;
4109c66d156SVinicius Costa Gomes 	/* txtime-assist and full offload are mutually exclusive */
4119c66d156SVinicius Costa Gomes 	if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
4129c66d156SVinicius Costa Gomes 	    (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
4139c66d156SVinicius Costa Gomes 		return false;
4149c66d156SVinicius Costa Gomes 	return true;
4159c66d156SVinicius Costa Gomes }
4169c66d156SVinicius Costa Gomes 
41754002066SVedang Patel /* This returns the tstamp value set by TCP in terms of the set clock. */
41854002066SVedang Patel static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
41954002066SVedang Patel {
42054002066SVedang Patel 	unsigned int offset = skb_network_offset(skb);
42154002066SVedang Patel 	const struct ipv6hdr *ipv6h;
42254002066SVedang Patel 	const struct iphdr *iph;
42354002066SVedang Patel 	struct ipv6hdr _ipv6h;
42454002066SVedang Patel 
42554002066SVedang Patel 	ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
42654002066SVedang Patel 	if (!ipv6h)
42754002066SVedang Patel 		return 0;
42854002066SVedang Patel 
42954002066SVedang Patel 	if (ipv6h->version == 4) {
43054002066SVedang Patel 		iph = (struct iphdr *)ipv6h;
43154002066SVedang Patel 		offset += iph->ihl * 4;
43254002066SVedang Patel 
43354002066SVedang Patel 		/* special-case 6in4 tunnelling, as that is a common way to get
43454002066SVedang Patel 		 * v6 connectivity in the home
43554002066SVedang Patel 		 */
43654002066SVedang Patel 		if (iph->protocol == IPPROTO_IPV6) {
43754002066SVedang Patel 			ipv6h = skb_header_pointer(skb, offset,
43854002066SVedang Patel 						   sizeof(_ipv6h), &_ipv6h);
43954002066SVedang Patel 
44054002066SVedang Patel 			if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
44154002066SVedang Patel 				return 0;
44254002066SVedang Patel 		} else if (iph->protocol != IPPROTO_TCP) {
44354002066SVedang Patel 			return 0;
44454002066SVedang Patel 		}
44554002066SVedang Patel 	} else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
44654002066SVedang Patel 		return 0;
44754002066SVedang Patel 	}
44854002066SVedang Patel 
4496dc25401SEric Dumazet 	return taprio_mono_to_any(q, skb->skb_mstamp_ns);
45054002066SVedang Patel }
45154002066SVedang Patel 
4524cfd5779SVedang Patel /* There are a few scenarios where we will have to modify the txtime from
4534cfd5779SVedang Patel  * what is read from next_txtime in sched_entry. They are:
4544cfd5779SVedang Patel  * 1. If txtime is in the past,
4554cfd5779SVedang Patel  *    a. The gate for the traffic class is currently open and packet can be
4564cfd5779SVedang Patel  *       transmitted before it closes, schedule the packet right away.
4574cfd5779SVedang Patel  *    b. If the gate corresponding to the traffic class is going to open later
4584cfd5779SVedang Patel  *       in the cycle, set the txtime of packet to the interval start.
4594cfd5779SVedang Patel  * 2. If txtime is in the future, there are packets corresponding to the
4604cfd5779SVedang Patel  *    current traffic class waiting to be transmitted. So, the following
4614cfd5779SVedang Patel  *    possibilities exist:
4624cfd5779SVedang Patel  *    a. We can transmit the packet before the window containing the txtime
4634cfd5779SVedang Patel  *       closes.
4644cfd5779SVedang Patel  *    b. The window might close before the transmission can be completed
4654cfd5779SVedang Patel  *       successfully. So, schedule the packet in the next open window.
4664cfd5779SVedang Patel  */
4674cfd5779SVedang Patel static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
4684cfd5779SVedang Patel {
46954002066SVedang Patel 	ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
4704cfd5779SVedang Patel 	struct taprio_sched *q = qdisc_priv(sch);
4714cfd5779SVedang Patel 	struct sched_gate_list *sched, *admin;
4724cfd5779SVedang Patel 	ktime_t minimum_time, now, txtime;
4734cfd5779SVedang Patel 	int len, packet_transmit_time;
4744cfd5779SVedang Patel 	struct sched_entry *entry;
4754cfd5779SVedang Patel 	bool sched_changed;
4764cfd5779SVedang Patel 
4777ede7b03SVedang Patel 	now = taprio_get_time(q);
4784cfd5779SVedang Patel 	minimum_time = ktime_add_ns(now, q->txtime_delay);
4794cfd5779SVedang Patel 
48054002066SVedang Patel 	tcp_tstamp = get_tcp_tstamp(q, skb);
48154002066SVedang Patel 	minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
48254002066SVedang Patel 
4834cfd5779SVedang Patel 	rcu_read_lock();
4844cfd5779SVedang Patel 	admin = rcu_dereference(q->admin_sched);
4854cfd5779SVedang Patel 	sched = rcu_dereference(q->oper_sched);
4864cfd5779SVedang Patel 	if (admin && ktime_after(minimum_time, admin->base_time))
4874cfd5779SVedang Patel 		switch_schedules(q, &admin, &sched);
4884cfd5779SVedang Patel 
4894cfd5779SVedang Patel 	/* Until the schedule starts, all the queues are open */
4904cfd5779SVedang Patel 	if (!sched || ktime_before(minimum_time, sched->base_time)) {
4914cfd5779SVedang Patel 		txtime = minimum_time;
4924cfd5779SVedang Patel 		goto done;
4934cfd5779SVedang Patel 	}
4944cfd5779SVedang Patel 
4954cfd5779SVedang Patel 	len = qdisc_pkt_len(skb);
4964cfd5779SVedang Patel 	packet_transmit_time = length_to_duration(q, len);
4974cfd5779SVedang Patel 
4984cfd5779SVedang Patel 	do {
4990deee7aaSJiapeng Zhong 		sched_changed = false;
5004cfd5779SVedang Patel 
5014cfd5779SVedang Patel 		entry = find_entry_to_transmit(skb, sch, sched, admin,
5024cfd5779SVedang Patel 					       minimum_time,
5034cfd5779SVedang Patel 					       &interval_start, &interval_end,
5044cfd5779SVedang Patel 					       false);
5054cfd5779SVedang Patel 		if (!entry) {
5064cfd5779SVedang Patel 			txtime = 0;
5074cfd5779SVedang Patel 			goto done;
5084cfd5779SVedang Patel 		}
5094cfd5779SVedang Patel 
5104cfd5779SVedang Patel 		txtime = entry->next_txtime;
5114cfd5779SVedang Patel 		txtime = max_t(ktime_t, txtime, minimum_time);
5124cfd5779SVedang Patel 		txtime = max_t(ktime_t, txtime, interval_start);
5134cfd5779SVedang Patel 
5144cfd5779SVedang Patel 		if (admin && admin != sched &&
5154cfd5779SVedang Patel 		    ktime_after(txtime, admin->base_time)) {
5164cfd5779SVedang Patel 			sched = admin;
5170deee7aaSJiapeng Zhong 			sched_changed = true;
5184cfd5779SVedang Patel 			continue;
5194cfd5779SVedang Patel 		}
5204cfd5779SVedang Patel 
5214cfd5779SVedang Patel 		transmit_end_time = ktime_add(txtime, packet_transmit_time);
5224cfd5779SVedang Patel 		minimum_time = transmit_end_time;
5234cfd5779SVedang Patel 
5244cfd5779SVedang Patel 		/* Update the txtime of current entry to the next time it's
5254cfd5779SVedang Patel 		 * interval starts.
5264cfd5779SVedang Patel 		 */
5274cfd5779SVedang Patel 		if (ktime_after(transmit_end_time, interval_end))
5284cfd5779SVedang Patel 			entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
5294cfd5779SVedang Patel 	} while (sched_changed || ktime_after(transmit_end_time, interval_end));
5304cfd5779SVedang Patel 
5314cfd5779SVedang Patel 	entry->next_txtime = transmit_end_time;
5324cfd5779SVedang Patel 
5334cfd5779SVedang Patel done:
5344cfd5779SVedang Patel 	rcu_read_unlock();
5354cfd5779SVedang Patel 	return txtime;
5364cfd5779SVedang Patel }
5374cfd5779SVedang Patel 
538a878fd46SVladimir Oltean /* Devices with full offload are expected to honor this in hardware */
539a878fd46SVladimir Oltean static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
540a878fd46SVladimir Oltean 					     struct sk_buff *skb)
541a878fd46SVladimir Oltean {
542a878fd46SVladimir Oltean 	struct taprio_sched *q = qdisc_priv(sch);
543a878fd46SVladimir Oltean 	struct net_device *dev = qdisc_dev(sch);
544a878fd46SVladimir Oltean 	struct sched_gate_list *sched;
545a878fd46SVladimir Oltean 	int prio = skb->priority;
546a878fd46SVladimir Oltean 	bool exceeds = false;
547a878fd46SVladimir Oltean 	u8 tc;
548a878fd46SVladimir Oltean 
549a878fd46SVladimir Oltean 	tc = netdev_get_prio_tc_map(dev, prio);
550a878fd46SVladimir Oltean 
551a878fd46SVladimir Oltean 	rcu_read_lock();
552a878fd46SVladimir Oltean 	sched = rcu_dereference(q->oper_sched);
553a878fd46SVladimir Oltean 	if (sched && skb->len > sched->max_frm_len[tc])
554a878fd46SVladimir Oltean 		exceeds = true;
555a878fd46SVladimir Oltean 	rcu_read_unlock();
556a878fd46SVladimir Oltean 
557a878fd46SVladimir Oltean 	return exceeds;
558a878fd46SVladimir Oltean }
559a878fd46SVladimir Oltean 
560497cc002SKurt Kanzenbach static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
561497cc002SKurt Kanzenbach 			      struct Qdisc *child, struct sk_buff **to_free)
5625a781ccbSVinicius Costa Gomes {
5635a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
5645a781ccbSVinicius Costa Gomes 
565e8a64bbaSBenedikt Spranger 	/* sk_flags are only safe to use on full sockets. */
566e8a64bbaSBenedikt Spranger 	if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
5674cfd5779SVedang Patel 		if (!is_valid_interval(skb, sch))
5684cfd5779SVedang Patel 			return qdisc_drop(skb, sch, to_free);
5694cfd5779SVedang Patel 	} else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
5704cfd5779SVedang Patel 		skb->tstamp = get_packet_txtime(skb, sch);
5714cfd5779SVedang Patel 		if (!skb->tstamp)
5724cfd5779SVedang Patel 			return qdisc_drop(skb, sch, to_free);
5734cfd5779SVedang Patel 	}
5744cfd5779SVedang Patel 
5755a781ccbSVinicius Costa Gomes 	qdisc_qstats_backlog_inc(sch, skb);
5765a781ccbSVinicius Costa Gomes 	sch->q.qlen++;
5775a781ccbSVinicius Costa Gomes 
578ac5c66f2SPetr Machata 	return qdisc_enqueue(skb, child, to_free);
5795a781ccbSVinicius Costa Gomes }
5805a781ccbSVinicius Costa Gomes 
5812d5e8071SVladimir Oltean static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch,
5822d5e8071SVladimir Oltean 				    struct Qdisc *child,
583497cc002SKurt Kanzenbach 				    struct sk_buff **to_free)
584497cc002SKurt Kanzenbach {
585497cc002SKurt Kanzenbach 	unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
586497cc002SKurt Kanzenbach 	netdev_features_t features = netif_skb_features(skb);
587497cc002SKurt Kanzenbach 	struct sk_buff *segs, *nskb;
588497cc002SKurt Kanzenbach 	int ret;
589497cc002SKurt Kanzenbach 
590497cc002SKurt Kanzenbach 	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
591497cc002SKurt Kanzenbach 	if (IS_ERR_OR_NULL(segs))
592497cc002SKurt Kanzenbach 		return qdisc_drop(skb, sch, to_free);
593497cc002SKurt Kanzenbach 
594497cc002SKurt Kanzenbach 	skb_list_walk_safe(segs, segs, nskb) {
595497cc002SKurt Kanzenbach 		skb_mark_not_on_list(segs);
596497cc002SKurt Kanzenbach 		qdisc_skb_cb(segs)->pkt_len = segs->len;
597497cc002SKurt Kanzenbach 		slen += segs->len;
598497cc002SKurt Kanzenbach 
59939b02d6dSVladimir Oltean 		/* FIXME: we should be segmenting to a smaller size
60039b02d6dSVladimir Oltean 		 * rather than dropping these
60139b02d6dSVladimir Oltean 		 */
60239b02d6dSVladimir Oltean 		if (taprio_skb_exceeds_queue_max_sdu(sch, segs))
60339b02d6dSVladimir Oltean 			ret = qdisc_drop(segs, sch, to_free);
60439b02d6dSVladimir Oltean 		else
605497cc002SKurt Kanzenbach 			ret = taprio_enqueue_one(segs, sch, child, to_free);
60639b02d6dSVladimir Oltean 
607497cc002SKurt Kanzenbach 		if (ret != NET_XMIT_SUCCESS) {
608497cc002SKurt Kanzenbach 			if (net_xmit_drop_count(ret))
609497cc002SKurt Kanzenbach 				qdisc_qstats_drop(sch);
610497cc002SKurt Kanzenbach 		} else {
611497cc002SKurt Kanzenbach 			numsegs++;
612497cc002SKurt Kanzenbach 		}
613497cc002SKurt Kanzenbach 	}
614497cc002SKurt Kanzenbach 
615497cc002SKurt Kanzenbach 	if (numsegs > 1)
616497cc002SKurt Kanzenbach 		qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
617497cc002SKurt Kanzenbach 	consume_skb(skb);
618497cc002SKurt Kanzenbach 
619497cc002SKurt Kanzenbach 	return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
620497cc002SKurt Kanzenbach }
621497cc002SKurt Kanzenbach 
6222d5e8071SVladimir Oltean /* Will not be called in the full offload case, since the TX queues are
6232d5e8071SVladimir Oltean  * attached to the Qdisc created using qdisc_create_dflt()
6242d5e8071SVladimir Oltean  */
6252d5e8071SVladimir Oltean static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
6262d5e8071SVladimir Oltean 			  struct sk_buff **to_free)
6272d5e8071SVladimir Oltean {
6282d5e8071SVladimir Oltean 	struct taprio_sched *q = qdisc_priv(sch);
6292d5e8071SVladimir Oltean 	struct Qdisc *child;
6302d5e8071SVladimir Oltean 	int queue;
6312d5e8071SVladimir Oltean 
6322d5e8071SVladimir Oltean 	queue = skb_get_queue_mapping(skb);
6332d5e8071SVladimir Oltean 
6342d5e8071SVladimir Oltean 	child = q->qdiscs[queue];
6352d5e8071SVladimir Oltean 	if (unlikely(!child))
6362d5e8071SVladimir Oltean 		return qdisc_drop(skb, sch, to_free);
6372d5e8071SVladimir Oltean 
63839b02d6dSVladimir Oltean 	if (taprio_skb_exceeds_queue_max_sdu(sch, skb)) {
63939b02d6dSVladimir Oltean 		/* Large packets might not be transmitted when the transmission
64039b02d6dSVladimir Oltean 		 * duration exceeds any configured interval. Therefore, segment
64139b02d6dSVladimir Oltean 		 * the skb into smaller chunks. Drivers with full offload are
64239b02d6dSVladimir Oltean 		 * expected to handle this in hardware.
6432d5e8071SVladimir Oltean 		 */
6442d5e8071SVladimir Oltean 		if (skb_is_gso(skb))
64539b02d6dSVladimir Oltean 			return taprio_enqueue_segmented(skb, sch, child,
64639b02d6dSVladimir Oltean 							to_free);
64739b02d6dSVladimir Oltean 
64839b02d6dSVladimir Oltean 		return qdisc_drop(skb, sch, to_free);
64939b02d6dSVladimir Oltean 	}
6502d5e8071SVladimir Oltean 
651497cc002SKurt Kanzenbach 	return taprio_enqueue_one(skb, sch, child, to_free);
652497cc002SKurt Kanzenbach }
653497cc002SKurt Kanzenbach 
65425becba6SVladimir Oltean static struct sk_buff *taprio_peek(struct Qdisc *sch)
6555a781ccbSVinicius Costa Gomes {
656ecc0cc98SVladimir Oltean 	WARN_ONCE(1, "taprio only supports operating as root qdisc, peek() not implemented");
6575a781ccbSVinicius Costa Gomes 	return NULL;
6585a781ccbSVinicius Costa Gomes }
6595a781ccbSVinicius Costa Gomes 
660d2ad689dSVladimir Oltean static void taprio_set_budgets(struct taprio_sched *q,
661d2ad689dSVladimir Oltean 			       struct sched_gate_list *sched,
662d2ad689dSVladimir Oltean 			       struct sched_entry *entry)
66323bddf69SJakub Kicinski {
664d2ad689dSVladimir Oltean 	struct net_device *dev = qdisc_dev(q->root);
665d2ad689dSVladimir Oltean 	int num_tc = netdev_get_num_tc(dev);
666d2ad689dSVladimir Oltean 	int tc, budget;
667d2ad689dSVladimir Oltean 
668d2ad689dSVladimir Oltean 	for (tc = 0; tc < num_tc; tc++) {
669d2ad689dSVladimir Oltean 		/* Traffic classes which never close have infinite budget */
670d2ad689dSVladimir Oltean 		if (entry->gate_duration[tc] == sched->cycle_time)
671d2ad689dSVladimir Oltean 			budget = INT_MAX;
672d2ad689dSVladimir Oltean 		else
673d2ad689dSVladimir Oltean 			budget = div64_u64((u64)entry->gate_duration[tc] * PSEC_PER_NSEC,
674d2ad689dSVladimir Oltean 					   atomic64_read(&q->picos_per_byte));
675d2ad689dSVladimir Oltean 
676d2ad689dSVladimir Oltean 		atomic_set(&entry->budget[tc], budget);
677d2ad689dSVladimir Oltean 	}
678d2ad689dSVladimir Oltean }
679d2ad689dSVladimir Oltean 
680d2ad689dSVladimir Oltean /* When an skb is sent, it consumes from the budget of all traffic classes */
681d2ad689dSVladimir Oltean static int taprio_update_budgets(struct sched_entry *entry, size_t len,
682d2ad689dSVladimir Oltean 				 int tc_consumed, int num_tc)
683d2ad689dSVladimir Oltean {
684d2ad689dSVladimir Oltean 	int tc, budget, new_budget = 0;
685d2ad689dSVladimir Oltean 
686d2ad689dSVladimir Oltean 	for (tc = 0; tc < num_tc; tc++) {
687d2ad689dSVladimir Oltean 		budget = atomic_read(&entry->budget[tc]);
688d2ad689dSVladimir Oltean 		/* Don't consume from infinite budget */
689d2ad689dSVladimir Oltean 		if (budget == INT_MAX) {
690d2ad689dSVladimir Oltean 			if (tc == tc_consumed)
691d2ad689dSVladimir Oltean 				new_budget = budget;
692d2ad689dSVladimir Oltean 			continue;
693d2ad689dSVladimir Oltean 		}
694d2ad689dSVladimir Oltean 
695d2ad689dSVladimir Oltean 		if (tc == tc_consumed)
696d2ad689dSVladimir Oltean 			new_budget = atomic_sub_return(len, &entry->budget[tc]);
697d2ad689dSVladimir Oltean 		else
698d2ad689dSVladimir Oltean 			atomic_sub(len, &entry->budget[tc]);
699d2ad689dSVladimir Oltean 	}
700d2ad689dSVladimir Oltean 
701d2ad689dSVladimir Oltean 	return new_budget;
7025a781ccbSVinicius Costa Gomes }
7035a781ccbSVinicius Costa Gomes 
70492f96667SVladimir Oltean static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq,
70592f96667SVladimir Oltean 					       struct sched_entry *entry,
70692f96667SVladimir Oltean 					       u32 gate_mask)
70792f96667SVladimir Oltean {
70892f96667SVladimir Oltean 	struct taprio_sched *q = qdisc_priv(sch);
70992f96667SVladimir Oltean 	struct net_device *dev = qdisc_dev(sch);
71092f96667SVladimir Oltean 	struct Qdisc *child = q->qdiscs[txq];
711d2ad689dSVladimir Oltean 	int num_tc = netdev_get_num_tc(dev);
71292f96667SVladimir Oltean 	struct sk_buff *skb;
71392f96667SVladimir Oltean 	ktime_t guard;
71492f96667SVladimir Oltean 	int prio;
71592f96667SVladimir Oltean 	int len;
71692f96667SVladimir Oltean 	u8 tc;
71792f96667SVladimir Oltean 
71892f96667SVladimir Oltean 	if (unlikely(!child))
71992f96667SVladimir Oltean 		return NULL;
72092f96667SVladimir Oltean 
7214c229427SVladimir Oltean 	if (TXTIME_ASSIST_IS_ENABLED(q->flags))
7224c229427SVladimir Oltean 		goto skip_peek_checks;
72392f96667SVladimir Oltean 
72492f96667SVladimir Oltean 	skb = child->ops->peek(child);
72592f96667SVladimir Oltean 	if (!skb)
72692f96667SVladimir Oltean 		return NULL;
72792f96667SVladimir Oltean 
72892f96667SVladimir Oltean 	prio = skb->priority;
72992f96667SVladimir Oltean 	tc = netdev_get_prio_tc_map(dev, prio);
73092f96667SVladimir Oltean 
73192f96667SVladimir Oltean 	if (!(gate_mask & BIT(tc)))
73292f96667SVladimir Oltean 		return NULL;
73392f96667SVladimir Oltean 
73492f96667SVladimir Oltean 	len = qdisc_pkt_len(skb);
73592f96667SVladimir Oltean 	guard = ktime_add_ns(taprio_get_time(q), length_to_duration(q, len));
73692f96667SVladimir Oltean 
73792f96667SVladimir Oltean 	/* In the case that there's no gate entry, there's no
73892f96667SVladimir Oltean 	 * guard band ...
73992f96667SVladimir Oltean 	 */
74092f96667SVladimir Oltean 	if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
741a1e6ad30SVladimir Oltean 	    !taprio_entry_allows_tx(guard, entry, tc))
74292f96667SVladimir Oltean 		return NULL;
74392f96667SVladimir Oltean 
74492f96667SVladimir Oltean 	/* ... and no budget. */
74592f96667SVladimir Oltean 	if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
746d2ad689dSVladimir Oltean 	    taprio_update_budgets(entry, len, tc, num_tc) < 0)
74792f96667SVladimir Oltean 		return NULL;
74892f96667SVladimir Oltean 
7494c229427SVladimir Oltean skip_peek_checks:
75092f96667SVladimir Oltean 	skb = child->ops->dequeue(child);
75192f96667SVladimir Oltean 	if (unlikely(!skb))
75292f96667SVladimir Oltean 		return NULL;
75392f96667SVladimir Oltean 
75492f96667SVladimir Oltean 	qdisc_bstats_update(sch, skb);
75592f96667SVladimir Oltean 	qdisc_qstats_backlog_dec(sch, skb);
75692f96667SVladimir Oltean 	sch->q.qlen--;
75792f96667SVladimir Oltean 
75892f96667SVladimir Oltean 	return skb;
75992f96667SVladimir Oltean }
76092f96667SVladimir Oltean 
7612f530df7SVladimir Oltean static void taprio_next_tc_txq(struct net_device *dev, int tc, int *txq)
7622f530df7SVladimir Oltean {
7632f530df7SVladimir Oltean 	int offset = dev->tc_to_txq[tc].offset;
7642f530df7SVladimir Oltean 	int count = dev->tc_to_txq[tc].count;
7652f530df7SVladimir Oltean 
7662f530df7SVladimir Oltean 	(*txq)++;
7672f530df7SVladimir Oltean 	if (*txq == offset + count)
7682f530df7SVladimir Oltean 		*txq = offset;
7692f530df7SVladimir Oltean }
7702f530df7SVladimir Oltean 
7712f530df7SVladimir Oltean /* Prioritize higher traffic classes, and select among TXQs belonging to the
7722f530df7SVladimir Oltean  * same TC using round robin
7732f530df7SVladimir Oltean  */
7742f530df7SVladimir Oltean static struct sk_buff *taprio_dequeue_tc_priority(struct Qdisc *sch,
7752f530df7SVladimir Oltean 						  struct sched_entry *entry,
7762f530df7SVladimir Oltean 						  u32 gate_mask)
7772f530df7SVladimir Oltean {
7782f530df7SVladimir Oltean 	struct taprio_sched *q = qdisc_priv(sch);
7792f530df7SVladimir Oltean 	struct net_device *dev = qdisc_dev(sch);
7802f530df7SVladimir Oltean 	int num_tc = netdev_get_num_tc(dev);
7812f530df7SVladimir Oltean 	struct sk_buff *skb;
7822f530df7SVladimir Oltean 	int tc;
7832f530df7SVladimir Oltean 
7842f530df7SVladimir Oltean 	for (tc = num_tc - 1; tc >= 0; tc--) {
7852f530df7SVladimir Oltean 		int first_txq = q->cur_txq[tc];
7862f530df7SVladimir Oltean 
7872f530df7SVladimir Oltean 		if (!(gate_mask & BIT(tc)))
7882f530df7SVladimir Oltean 			continue;
7892f530df7SVladimir Oltean 
7902f530df7SVladimir Oltean 		do {
7912f530df7SVladimir Oltean 			skb = taprio_dequeue_from_txq(sch, q->cur_txq[tc],
7922f530df7SVladimir Oltean 						      entry, gate_mask);
7932f530df7SVladimir Oltean 
7942f530df7SVladimir Oltean 			taprio_next_tc_txq(dev, tc, &q->cur_txq[tc]);
7952f530df7SVladimir Oltean 
7962f530df7SVladimir Oltean 			if (skb)
7972f530df7SVladimir Oltean 				return skb;
7982f530df7SVladimir Oltean 		} while (q->cur_txq[tc] != first_txq);
7992f530df7SVladimir Oltean 	}
8002f530df7SVladimir Oltean 
8012f530df7SVladimir Oltean 	return NULL;
8022f530df7SVladimir Oltean }
8032f530df7SVladimir Oltean 
8042f530df7SVladimir Oltean /* Broken way of prioritizing smaller TXQ indices and ignoring the traffic
8052f530df7SVladimir Oltean  * class other than to determine whether the gate is open or not
8062f530df7SVladimir Oltean  */
8072f530df7SVladimir Oltean static struct sk_buff *taprio_dequeue_txq_priority(struct Qdisc *sch,
8082f530df7SVladimir Oltean 						   struct sched_entry *entry,
8092f530df7SVladimir Oltean 						   u32 gate_mask)
8102f530df7SVladimir Oltean {
8112f530df7SVladimir Oltean 	struct net_device *dev = qdisc_dev(sch);
8122f530df7SVladimir Oltean 	struct sk_buff *skb;
8132f530df7SVladimir Oltean 	int i;
8142f530df7SVladimir Oltean 
8152f530df7SVladimir Oltean 	for (i = 0; i < dev->num_tx_queues; i++) {
8162f530df7SVladimir Oltean 		skb = taprio_dequeue_from_txq(sch, i, entry, gate_mask);
8172f530df7SVladimir Oltean 		if (skb)
8182f530df7SVladimir Oltean 			return skb;
8192f530df7SVladimir Oltean 	}
8202f530df7SVladimir Oltean 
8212f530df7SVladimir Oltean 	return NULL;
8222f530df7SVladimir Oltean }
8232f530df7SVladimir Oltean 
8242c08a4f8SVladimir Oltean /* Will not be called in the full offload case, since the TX queues are
8252c08a4f8SVladimir Oltean  * attached to the Qdisc created using qdisc_create_dflt()
8262c08a4f8SVladimir Oltean  */
82725becba6SVladimir Oltean static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
8285a781ccbSVinicius Costa Gomes {
8295a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
8308c79f0eaSVinicius Costa Gomes 	struct sk_buff *skb = NULL;
8315a781ccbSVinicius Costa Gomes 	struct sched_entry *entry;
8325a781ccbSVinicius Costa Gomes 	u32 gate_mask;
8335a781ccbSVinicius Costa Gomes 
8345a781ccbSVinicius Costa Gomes 	rcu_read_lock();
8355a781ccbSVinicius Costa Gomes 	entry = rcu_dereference(q->current_entry);
8365a781ccbSVinicius Costa Gomes 	/* if there's no entry, it means that the schedule didn't
8375a781ccbSVinicius Costa Gomes 	 * start yet, so force all gates to be open, this is in
8385a781ccbSVinicius Costa Gomes 	 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
839633fa666SJesper Dangaard Brouer 	 * "AdminGateStates"
8405a781ccbSVinicius Costa Gomes 	 */
8415a781ccbSVinicius Costa Gomes 	gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
8425a781ccbSVinicius Costa Gomes 	if (!gate_mask)
8438c79f0eaSVinicius Costa Gomes 		goto done;
8445a781ccbSVinicius Costa Gomes 
8452f530df7SVladimir Oltean 	if (static_branch_unlikely(&taprio_have_broken_mqprio) &&
8462f530df7SVladimir Oltean 	    !static_branch_likely(&taprio_have_working_mqprio)) {
8472f530df7SVladimir Oltean 		/* Single NIC kind which is broken */
8482f530df7SVladimir Oltean 		skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
8492f530df7SVladimir Oltean 	} else if (static_branch_likely(&taprio_have_working_mqprio) &&
8502f530df7SVladimir Oltean 		   !static_branch_unlikely(&taprio_have_broken_mqprio)) {
8512f530df7SVladimir Oltean 		/* Single NIC kind which prioritizes properly */
8522f530df7SVladimir Oltean 		skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
8532f530df7SVladimir Oltean 	} else {
8542f530df7SVladimir Oltean 		/* Mixed NIC kinds present in system, need dynamic testing */
8552f530df7SVladimir Oltean 		if (q->broken_mqprio)
8562f530df7SVladimir Oltean 			skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
8572f530df7SVladimir Oltean 		else
8582f530df7SVladimir Oltean 			skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
8595a781ccbSVinicius Costa Gomes 	}
8605a781ccbSVinicius Costa Gomes 
8618c79f0eaSVinicius Costa Gomes done:
8628c79f0eaSVinicius Costa Gomes 	rcu_read_unlock();
8638c79f0eaSVinicius Costa Gomes 
8648c79f0eaSVinicius Costa Gomes 	return skb;
8655a781ccbSVinicius Costa Gomes }
8665a781ccbSVinicius Costa Gomes 
8676ca6a665SVinicius Costa Gomes static bool should_restart_cycle(const struct sched_gate_list *oper,
8686ca6a665SVinicius Costa Gomes 				 const struct sched_entry *entry)
8696ca6a665SVinicius Costa Gomes {
8706ca6a665SVinicius Costa Gomes 	if (list_is_last(&entry->list, &oper->entries))
8716ca6a665SVinicius Costa Gomes 		return true;
8726ca6a665SVinicius Costa Gomes 
873e5517551SVladimir Oltean 	if (ktime_compare(entry->end_time, oper->cycle_end_time) == 0)
8746ca6a665SVinicius Costa Gomes 		return true;
8756ca6a665SVinicius Costa Gomes 
8766ca6a665SVinicius Costa Gomes 	return false;
8776ca6a665SVinicius Costa Gomes }
8786ca6a665SVinicius Costa Gomes 
879a3d43c0dSVinicius Costa Gomes static bool should_change_schedules(const struct sched_gate_list *admin,
880a3d43c0dSVinicius Costa Gomes 				    const struct sched_gate_list *oper,
881e5517551SVladimir Oltean 				    ktime_t end_time)
882a3d43c0dSVinicius Costa Gomes {
883c25031e9SVinicius Costa Gomes 	ktime_t next_base_time, extension_time;
884a3d43c0dSVinicius Costa Gomes 
885a3d43c0dSVinicius Costa Gomes 	if (!admin)
886a3d43c0dSVinicius Costa Gomes 		return false;
887a3d43c0dSVinicius Costa Gomes 
888a3d43c0dSVinicius Costa Gomes 	next_base_time = sched_base_time(admin);
889a3d43c0dSVinicius Costa Gomes 
890e5517551SVladimir Oltean 	/* This is the simple case, the end_time would fall after
891a3d43c0dSVinicius Costa Gomes 	 * the next schedule base_time.
892a3d43c0dSVinicius Costa Gomes 	 */
893e5517551SVladimir Oltean 	if (ktime_compare(next_base_time, end_time) <= 0)
894a3d43c0dSVinicius Costa Gomes 		return true;
895a3d43c0dSVinicius Costa Gomes 
896e5517551SVladimir Oltean 	/* This is the cycle_time_extension case, if the end_time
897c25031e9SVinicius Costa Gomes 	 * plus the amount that can be extended would fall after the
898c25031e9SVinicius Costa Gomes 	 * next schedule base_time, we can extend the current schedule
899c25031e9SVinicius Costa Gomes 	 * for that amount.
900c25031e9SVinicius Costa Gomes 	 */
901e5517551SVladimir Oltean 	extension_time = ktime_add_ns(end_time, oper->cycle_time_extension);
902c25031e9SVinicius Costa Gomes 
903c25031e9SVinicius Costa Gomes 	/* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
904c25031e9SVinicius Costa Gomes 	 * how precisely the extension should be made. So after
905c25031e9SVinicius Costa Gomes 	 * conformance testing, this logic may change.
906c25031e9SVinicius Costa Gomes 	 */
907c25031e9SVinicius Costa Gomes 	if (ktime_compare(next_base_time, extension_time) <= 0)
908c25031e9SVinicius Costa Gomes 		return true;
909c25031e9SVinicius Costa Gomes 
910a3d43c0dSVinicius Costa Gomes 	return false;
911a3d43c0dSVinicius Costa Gomes }
912a3d43c0dSVinicius Costa Gomes 
9135a781ccbSVinicius Costa Gomes static enum hrtimer_restart advance_sched(struct hrtimer *timer)
9145a781ccbSVinicius Costa Gomes {
9155a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = container_of(timer, struct taprio_sched,
9165a781ccbSVinicius Costa Gomes 					      advance_timer);
917a1e6ad30SVladimir Oltean 	struct net_device *dev = qdisc_dev(q->root);
918a3d43c0dSVinicius Costa Gomes 	struct sched_gate_list *oper, *admin;
919a1e6ad30SVladimir Oltean 	int num_tc = netdev_get_num_tc(dev);
9205a781ccbSVinicius Costa Gomes 	struct sched_entry *entry, *next;
9215a781ccbSVinicius Costa Gomes 	struct Qdisc *sch = q->root;
922e5517551SVladimir Oltean 	ktime_t end_time;
923a1e6ad30SVladimir Oltean 	int tc;
9245a781ccbSVinicius Costa Gomes 
9255a781ccbSVinicius Costa Gomes 	spin_lock(&q->current_entry_lock);
9265a781ccbSVinicius Costa Gomes 	entry = rcu_dereference_protected(q->current_entry,
9275a781ccbSVinicius Costa Gomes 					  lockdep_is_held(&q->current_entry_lock));
928a3d43c0dSVinicius Costa Gomes 	oper = rcu_dereference_protected(q->oper_sched,
929a3d43c0dSVinicius Costa Gomes 					 lockdep_is_held(&q->current_entry_lock));
930a3d43c0dSVinicius Costa Gomes 	admin = rcu_dereference_protected(q->admin_sched,
931a3d43c0dSVinicius Costa Gomes 					  lockdep_is_held(&q->current_entry_lock));
9325a781ccbSVinicius Costa Gomes 
933a3d43c0dSVinicius Costa Gomes 	if (!oper)
934a3d43c0dSVinicius Costa Gomes 		switch_schedules(q, &admin, &oper);
935a3d43c0dSVinicius Costa Gomes 
936a3d43c0dSVinicius Costa Gomes 	/* This can happen in two cases: 1. this is the very first run
937a3d43c0dSVinicius Costa Gomes 	 * of this function (i.e. we weren't running any schedule
938a3d43c0dSVinicius Costa Gomes 	 * previously); 2. The previous schedule just ended. The first
939a3d43c0dSVinicius Costa Gomes 	 * entry of all schedules are pre-calculated during the
940a3d43c0dSVinicius Costa Gomes 	 * schedule initialization.
9415a781ccbSVinicius Costa Gomes 	 */
942e5517551SVladimir Oltean 	if (unlikely(!entry || entry->end_time == oper->base_time)) {
943a3d43c0dSVinicius Costa Gomes 		next = list_first_entry(&oper->entries, struct sched_entry,
9445a781ccbSVinicius Costa Gomes 					list);
945e5517551SVladimir Oltean 		end_time = next->end_time;
9465a781ccbSVinicius Costa Gomes 		goto first_run;
9475a781ccbSVinicius Costa Gomes 	}
9485a781ccbSVinicius Costa Gomes 
9496ca6a665SVinicius Costa Gomes 	if (should_restart_cycle(oper, entry)) {
950a3d43c0dSVinicius Costa Gomes 		next = list_first_entry(&oper->entries, struct sched_entry,
9515a781ccbSVinicius Costa Gomes 					list);
952e5517551SVladimir Oltean 		oper->cycle_end_time = ktime_add_ns(oper->cycle_end_time,
9536ca6a665SVinicius Costa Gomes 						    oper->cycle_time);
9546ca6a665SVinicius Costa Gomes 	} else {
9555a781ccbSVinicius Costa Gomes 		next = list_next_entry(entry, list);
9566ca6a665SVinicius Costa Gomes 	}
9575a781ccbSVinicius Costa Gomes 
958e5517551SVladimir Oltean 	end_time = ktime_add_ns(entry->end_time, next->interval);
959e5517551SVladimir Oltean 	end_time = min_t(ktime_t, end_time, oper->cycle_end_time);
9605a781ccbSVinicius Costa Gomes 
961a1e6ad30SVladimir Oltean 	for (tc = 0; tc < num_tc; tc++) {
962a1e6ad30SVladimir Oltean 		if (next->gate_duration[tc] == oper->cycle_time)
963a1e6ad30SVladimir Oltean 			next->gate_close_time[tc] = KTIME_MAX;
964a1e6ad30SVladimir Oltean 		else
965a1e6ad30SVladimir Oltean 			next->gate_close_time[tc] = ktime_add_ns(entry->end_time,
966a1e6ad30SVladimir Oltean 								 next->gate_duration[tc]);
967a1e6ad30SVladimir Oltean 	}
968a1e6ad30SVladimir Oltean 
969e5517551SVladimir Oltean 	if (should_change_schedules(admin, oper, end_time)) {
970a3d43c0dSVinicius Costa Gomes 		/* Set things so the next time this runs, the new
971a3d43c0dSVinicius Costa Gomes 		 * schedule runs.
972a3d43c0dSVinicius Costa Gomes 		 */
973e5517551SVladimir Oltean 		end_time = sched_base_time(admin);
974a3d43c0dSVinicius Costa Gomes 		switch_schedules(q, &admin, &oper);
975a3d43c0dSVinicius Costa Gomes 	}
976a3d43c0dSVinicius Costa Gomes 
977e5517551SVladimir Oltean 	next->end_time = end_time;
978d2ad689dSVladimir Oltean 	taprio_set_budgets(q, oper, next);
9795a781ccbSVinicius Costa Gomes 
9805a781ccbSVinicius Costa Gomes first_run:
9815a781ccbSVinicius Costa Gomes 	rcu_assign_pointer(q->current_entry, next);
9825a781ccbSVinicius Costa Gomes 	spin_unlock(&q->current_entry_lock);
9835a781ccbSVinicius Costa Gomes 
984e5517551SVladimir Oltean 	hrtimer_set_expires(&q->advance_timer, end_time);
9855a781ccbSVinicius Costa Gomes 
9865a781ccbSVinicius Costa Gomes 	rcu_read_lock();
9875a781ccbSVinicius Costa Gomes 	__netif_schedule(sch);
9885a781ccbSVinicius Costa Gomes 	rcu_read_unlock();
9895a781ccbSVinicius Costa Gomes 
9905a781ccbSVinicius Costa Gomes 	return HRTIMER_RESTART;
9915a781ccbSVinicius Costa Gomes }
9925a781ccbSVinicius Costa Gomes 
9935a781ccbSVinicius Costa Gomes static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
9945a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_SCHED_ENTRY_INDEX]	   = { .type = NLA_U32 },
9955a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_SCHED_ENTRY_CMD]	   = { .type = NLA_U8 },
9965a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
9975a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]  = { .type = NLA_U32 },
9985a781ccbSVinicius Costa Gomes };
9995a781ccbSVinicius Costa Gomes 
1000a54fc09eSVladimir Oltean static const struct nla_policy taprio_tc_policy[TCA_TAPRIO_TC_ENTRY_MAX + 1] = {
1001a54fc09eSVladimir Oltean 	[TCA_TAPRIO_TC_ENTRY_INDEX]	   = { .type = NLA_U32 },
1002a54fc09eSVladimir Oltean 	[TCA_TAPRIO_TC_ENTRY_MAX_SDU]	   = { .type = NLA_U32 },
1003a54fc09eSVladimir Oltean };
1004a54fc09eSVladimir Oltean 
10055a781ccbSVinicius Costa Gomes static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
10065a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_PRIOMAP]	       = {
10075a781ccbSVinicius Costa Gomes 		.len = sizeof(struct tc_mqprio_qopt)
10085a781ccbSVinicius Costa Gomes 	},
10095a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]           = { .type = NLA_NESTED },
10105a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]            = { .type = NLA_S64 },
10115a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]         = { .type = NLA_NESTED },
10125a781ccbSVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_CLOCKID]              = { .type = NLA_S32 },
10136ca6a665SVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]           = { .type = NLA_S64 },
1014c25031e9SVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
101549c684d7SVinicius Costa Gomes 	[TCA_TAPRIO_ATTR_FLAGS]                      = { .type = NLA_U32 },
1016e13aaa06SJakub Kicinski 	[TCA_TAPRIO_ATTR_TXTIME_DELAY]		     = { .type = NLA_U32 },
1017a54fc09eSVladimir Oltean 	[TCA_TAPRIO_ATTR_TC_ENTRY]		     = { .type = NLA_NESTED },
10185a781ccbSVinicius Costa Gomes };
10195a781ccbSVinicius Costa Gomes 
1020b5b73b26SVinicius Costa Gomes static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
1021b5b73b26SVinicius Costa Gomes 			    struct sched_entry *entry,
10225a781ccbSVinicius Costa Gomes 			    struct netlink_ext_ack *extack)
10235a781ccbSVinicius Costa Gomes {
1024b5b73b26SVinicius Costa Gomes 	int min_duration = length_to_duration(q, ETH_ZLEN);
10255a781ccbSVinicius Costa Gomes 	u32 interval = 0;
10265a781ccbSVinicius Costa Gomes 
10275a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
10285a781ccbSVinicius Costa Gomes 		entry->command = nla_get_u8(
10295a781ccbSVinicius Costa Gomes 			tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
10305a781ccbSVinicius Costa Gomes 
10315a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
10325a781ccbSVinicius Costa Gomes 		entry->gate_mask = nla_get_u32(
10335a781ccbSVinicius Costa Gomes 			tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
10345a781ccbSVinicius Costa Gomes 
10355a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
10365a781ccbSVinicius Costa Gomes 		interval = nla_get_u32(
10375a781ccbSVinicius Costa Gomes 			tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
10385a781ccbSVinicius Costa Gomes 
1039b5b73b26SVinicius Costa Gomes 	/* The interval should allow at least the minimum ethernet
1040b5b73b26SVinicius Costa Gomes 	 * frame to go out.
1041b5b73b26SVinicius Costa Gomes 	 */
1042b5b73b26SVinicius Costa Gomes 	if (interval < min_duration) {
10435a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
10445a781ccbSVinicius Costa Gomes 		return -EINVAL;
10455a781ccbSVinicius Costa Gomes 	}
10465a781ccbSVinicius Costa Gomes 
10475a781ccbSVinicius Costa Gomes 	entry->interval = interval;
10485a781ccbSVinicius Costa Gomes 
10495a781ccbSVinicius Costa Gomes 	return 0;
10505a781ccbSVinicius Costa Gomes }
10515a781ccbSVinicius Costa Gomes 
1052b5b73b26SVinicius Costa Gomes static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
1053b5b73b26SVinicius Costa Gomes 			     struct sched_entry *entry, int index,
1054b5b73b26SVinicius Costa Gomes 			     struct netlink_ext_ack *extack)
10555a781ccbSVinicius Costa Gomes {
10565a781ccbSVinicius Costa Gomes 	struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
10575a781ccbSVinicius Costa Gomes 	int err;
10585a781ccbSVinicius Costa Gomes 
10598cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
10605a781ccbSVinicius Costa Gomes 					  entry_policy, NULL);
10615a781ccbSVinicius Costa Gomes 	if (err < 0) {
10625a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Could not parse nested entry");
10635a781ccbSVinicius Costa Gomes 		return -EINVAL;
10645a781ccbSVinicius Costa Gomes 	}
10655a781ccbSVinicius Costa Gomes 
10665a781ccbSVinicius Costa Gomes 	entry->index = index;
10675a781ccbSVinicius Costa Gomes 
1068b5b73b26SVinicius Costa Gomes 	return fill_sched_entry(q, tb, entry, extack);
10695a781ccbSVinicius Costa Gomes }
10705a781ccbSVinicius Costa Gomes 
1071b5b73b26SVinicius Costa Gomes static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
1072a3d43c0dSVinicius Costa Gomes 			    struct sched_gate_list *sched,
10735a781ccbSVinicius Costa Gomes 			    struct netlink_ext_ack *extack)
10745a781ccbSVinicius Costa Gomes {
10755a781ccbSVinicius Costa Gomes 	struct nlattr *n;
10765a781ccbSVinicius Costa Gomes 	int err, rem;
10775a781ccbSVinicius Costa Gomes 	int i = 0;
10785a781ccbSVinicius Costa Gomes 
10795a781ccbSVinicius Costa Gomes 	if (!list)
10805a781ccbSVinicius Costa Gomes 		return -EINVAL;
10815a781ccbSVinicius Costa Gomes 
10825a781ccbSVinicius Costa Gomes 	nla_for_each_nested(n, list, rem) {
10835a781ccbSVinicius Costa Gomes 		struct sched_entry *entry;
10845a781ccbSVinicius Costa Gomes 
10855a781ccbSVinicius Costa Gomes 		if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
10865a781ccbSVinicius Costa Gomes 			NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
10875a781ccbSVinicius Costa Gomes 			continue;
10885a781ccbSVinicius Costa Gomes 		}
10895a781ccbSVinicius Costa Gomes 
10905a781ccbSVinicius Costa Gomes 		entry = kzalloc(sizeof(*entry), GFP_KERNEL);
10915a781ccbSVinicius Costa Gomes 		if (!entry) {
10925a781ccbSVinicius Costa Gomes 			NL_SET_ERR_MSG(extack, "Not enough memory for entry");
10935a781ccbSVinicius Costa Gomes 			return -ENOMEM;
10945a781ccbSVinicius Costa Gomes 		}
10955a781ccbSVinicius Costa Gomes 
1096b5b73b26SVinicius Costa Gomes 		err = parse_sched_entry(q, n, entry, i, extack);
10975a781ccbSVinicius Costa Gomes 		if (err < 0) {
10985a781ccbSVinicius Costa Gomes 			kfree(entry);
10995a781ccbSVinicius Costa Gomes 			return err;
11005a781ccbSVinicius Costa Gomes 		}
11015a781ccbSVinicius Costa Gomes 
1102a3d43c0dSVinicius Costa Gomes 		list_add_tail(&entry->list, &sched->entries);
11035a781ccbSVinicius Costa Gomes 		i++;
11045a781ccbSVinicius Costa Gomes 	}
11055a781ccbSVinicius Costa Gomes 
1106a3d43c0dSVinicius Costa Gomes 	sched->num_entries = i;
11075a781ccbSVinicius Costa Gomes 
11085a781ccbSVinicius Costa Gomes 	return i;
11095a781ccbSVinicius Costa Gomes }
11105a781ccbSVinicius Costa Gomes 
1111b5b73b26SVinicius Costa Gomes static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
1112a3d43c0dSVinicius Costa Gomes 				 struct sched_gate_list *new,
11135a781ccbSVinicius Costa Gomes 				 struct netlink_ext_ack *extack)
11145a781ccbSVinicius Costa Gomes {
11155a781ccbSVinicius Costa Gomes 	int err = 0;
11165a781ccbSVinicius Costa Gomes 
1117a3d43c0dSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
1118a3d43c0dSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
1119a3d43c0dSVinicius Costa Gomes 		return -ENOTSUPP;
1120a3d43c0dSVinicius Costa Gomes 	}
11215a781ccbSVinicius Costa Gomes 
11225a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
1123a3d43c0dSVinicius Costa Gomes 		new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
11245a781ccbSVinicius Costa Gomes 
1125c25031e9SVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
1126c25031e9SVinicius Costa Gomes 		new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
1127c25031e9SVinicius Costa Gomes 
11286ca6a665SVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
11296ca6a665SVinicius Costa Gomes 		new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
11306ca6a665SVinicius Costa Gomes 
11315a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
1132b5b73b26SVinicius Costa Gomes 		err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
1133b5b73b26SVinicius Costa Gomes 				       new, extack);
1134a3d43c0dSVinicius Costa Gomes 	if (err < 0)
11355a781ccbSVinicius Costa Gomes 		return err;
1136a3d43c0dSVinicius Costa Gomes 
1137037be037SVedang Patel 	if (!new->cycle_time) {
1138037be037SVedang Patel 		struct sched_entry *entry;
1139037be037SVedang Patel 		ktime_t cycle = 0;
1140037be037SVedang Patel 
1141037be037SVedang Patel 		list_for_each_entry(entry, &new->entries, list)
1142037be037SVedang Patel 			cycle = ktime_add_ns(cycle, entry->interval);
1143ed8157f1SDu Cheng 
1144ed8157f1SDu Cheng 		if (!cycle) {
1145ed8157f1SDu Cheng 			NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
1146ed8157f1SDu Cheng 			return -EINVAL;
1147ed8157f1SDu Cheng 		}
1148ed8157f1SDu Cheng 
1149037be037SVedang Patel 		new->cycle_time = cycle;
1150037be037SVedang Patel 	}
1151037be037SVedang Patel 
1152a306a90cSVladimir Oltean 	taprio_calculate_gate_durations(q, new);
1153a306a90cSVladimir Oltean 
1154a3d43c0dSVinicius Costa Gomes 	return 0;
11555a781ccbSVinicius Costa Gomes }
11565a781ccbSVinicius Costa Gomes 
11575a781ccbSVinicius Costa Gomes static int taprio_parse_mqprio_opt(struct net_device *dev,
11585a781ccbSVinicius Costa Gomes 				   struct tc_mqprio_qopt *qopt,
11594cfd5779SVedang Patel 				   struct netlink_ext_ack *extack,
11604cfd5779SVedang Patel 				   u32 taprio_flags)
11615a781ccbSVinicius Costa Gomes {
11621dfe086dSVladimir Oltean 	bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags);
11635a781ccbSVinicius Costa Gomes 
1164a3d43c0dSVinicius Costa Gomes 	if (!qopt && !dev->num_tc) {
11655a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
11665a781ccbSVinicius Costa Gomes 		return -EINVAL;
11675a781ccbSVinicius Costa Gomes 	}
11685a781ccbSVinicius Costa Gomes 
1169a3d43c0dSVinicius Costa Gomes 	/* If num_tc is already set, it means that the user already
1170a3d43c0dSVinicius Costa Gomes 	 * configured the mqprio part
1171a3d43c0dSVinicius Costa Gomes 	 */
1172a3d43c0dSVinicius Costa Gomes 	if (dev->num_tc)
1173a3d43c0dSVinicius Costa Gomes 		return 0;
1174a3d43c0dSVinicius Costa Gomes 
11755a781ccbSVinicius Costa Gomes 	/* taprio imposes that traffic classes map 1:n to tx queues */
11765a781ccbSVinicius Costa Gomes 	if (qopt->num_tc > dev->num_tx_queues) {
11775a781ccbSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
11785a781ccbSVinicius Costa Gomes 		return -EINVAL;
11795a781ccbSVinicius Costa Gomes 	}
11805a781ccbSVinicius Costa Gomes 
11811dfe086dSVladimir Oltean 	/* For some reason, in txtime-assist mode, we allow TXQ ranges for
11821dfe086dSVladimir Oltean 	 * different TCs to overlap, and just validate the TXQ ranges.
11835a781ccbSVinicius Costa Gomes 	 */
11841dfe086dSVladimir Oltean 	return mqprio_validate_qopt(dev, qopt, true, allow_overlapping_txqs,
11851dfe086dSVladimir Oltean 				    extack);
11865a781ccbSVinicius Costa Gomes }
11875a781ccbSVinicius Costa Gomes 
1188a3d43c0dSVinicius Costa Gomes static int taprio_get_start_time(struct Qdisc *sch,
1189a3d43c0dSVinicius Costa Gomes 				 struct sched_gate_list *sched,
1190a3d43c0dSVinicius Costa Gomes 				 ktime_t *start)
11915a781ccbSVinicius Costa Gomes {
11925a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
11935a781ccbSVinicius Costa Gomes 	ktime_t now, base, cycle;
11945a781ccbSVinicius Costa Gomes 	s64 n;
11955a781ccbSVinicius Costa Gomes 
1196a3d43c0dSVinicius Costa Gomes 	base = sched_base_time(sched);
11977ede7b03SVedang Patel 	now = taprio_get_time(q);
11988599099fSAndre Guedes 
11998599099fSAndre Guedes 	if (ktime_after(base, now)) {
12008599099fSAndre Guedes 		*start = base;
12018599099fSAndre Guedes 		return 0;
12028599099fSAndre Guedes 	}
12035a781ccbSVinicius Costa Gomes 
1204037be037SVedang Patel 	cycle = sched->cycle_time;
12055a781ccbSVinicius Costa Gomes 
12068599099fSAndre Guedes 	/* The qdisc is expected to have at least one sched_entry.  Moreover,
12078599099fSAndre Guedes 	 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
12088599099fSAndre Guedes 	 * something went really wrong. In that case, we should warn about this
12098599099fSAndre Guedes 	 * inconsistent state and return error.
12108599099fSAndre Guedes 	 */
12118599099fSAndre Guedes 	if (WARN_ON(!cycle))
12128599099fSAndre Guedes 		return -EFAULT;
12135a781ccbSVinicius Costa Gomes 
12145a781ccbSVinicius Costa Gomes 	/* Schedule the start time for the beginning of the next
12155a781ccbSVinicius Costa Gomes 	 * cycle.
12165a781ccbSVinicius Costa Gomes 	 */
12175a781ccbSVinicius Costa Gomes 	n = div64_s64(ktime_sub_ns(now, base), cycle);
12188599099fSAndre Guedes 	*start = ktime_add_ns(base, (n + 1) * cycle);
12198599099fSAndre Guedes 	return 0;
12205a781ccbSVinicius Costa Gomes }
12215a781ccbSVinicius Costa Gomes 
1222e5517551SVladimir Oltean static void setup_first_end_time(struct taprio_sched *q,
1223a3d43c0dSVinicius Costa Gomes 				 struct sched_gate_list *sched, ktime_t base)
12245a781ccbSVinicius Costa Gomes {
1225a1e6ad30SVladimir Oltean 	struct net_device *dev = qdisc_dev(q->root);
1226a1e6ad30SVladimir Oltean 	int num_tc = netdev_get_num_tc(dev);
12275a781ccbSVinicius Costa Gomes 	struct sched_entry *first;
12286ca6a665SVinicius Costa Gomes 	ktime_t cycle;
1229a1e6ad30SVladimir Oltean 	int tc;
12305a781ccbSVinicius Costa Gomes 
1231a3d43c0dSVinicius Costa Gomes 	first = list_first_entry(&sched->entries,
1232a3d43c0dSVinicius Costa Gomes 				 struct sched_entry, list);
12335a781ccbSVinicius Costa Gomes 
1234037be037SVedang Patel 	cycle = sched->cycle_time;
12356ca6a665SVinicius Costa Gomes 
12366ca6a665SVinicius Costa Gomes 	/* FIXME: find a better place to do this */
1237e5517551SVladimir Oltean 	sched->cycle_end_time = ktime_add_ns(base, cycle);
12386ca6a665SVinicius Costa Gomes 
1239e5517551SVladimir Oltean 	first->end_time = ktime_add_ns(base, first->interval);
1240d2ad689dSVladimir Oltean 	taprio_set_budgets(q, sched, first);
1241a1e6ad30SVladimir Oltean 
1242a1e6ad30SVladimir Oltean 	for (tc = 0; tc < num_tc; tc++) {
1243a1e6ad30SVladimir Oltean 		if (first->gate_duration[tc] == sched->cycle_time)
1244a1e6ad30SVladimir Oltean 			first->gate_close_time[tc] = KTIME_MAX;
1245a1e6ad30SVladimir Oltean 		else
1246a1e6ad30SVladimir Oltean 			first->gate_close_time[tc] = ktime_add_ns(base, first->gate_duration[tc]);
1247a1e6ad30SVladimir Oltean 	}
1248a1e6ad30SVladimir Oltean 
12495a781ccbSVinicius Costa Gomes 	rcu_assign_pointer(q->current_entry, NULL);
1250a3d43c0dSVinicius Costa Gomes }
12515a781ccbSVinicius Costa Gomes 
1252a3d43c0dSVinicius Costa Gomes static void taprio_start_sched(struct Qdisc *sch,
1253a3d43c0dSVinicius Costa Gomes 			       ktime_t start, struct sched_gate_list *new)
1254a3d43c0dSVinicius Costa Gomes {
1255a3d43c0dSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
1256a3d43c0dSVinicius Costa Gomes 	ktime_t expires;
1257a3d43c0dSVinicius Costa Gomes 
12589c66d156SVinicius Costa Gomes 	if (FULL_OFFLOAD_IS_ENABLED(q->flags))
12599c66d156SVinicius Costa Gomes 		return;
12609c66d156SVinicius Costa Gomes 
1261a3d43c0dSVinicius Costa Gomes 	expires = hrtimer_get_expires(&q->advance_timer);
1262a3d43c0dSVinicius Costa Gomes 	if (expires == 0)
1263a3d43c0dSVinicius Costa Gomes 		expires = KTIME_MAX;
1264a3d43c0dSVinicius Costa Gomes 
1265a3d43c0dSVinicius Costa Gomes 	/* If the new schedule starts before the next expiration, we
1266a3d43c0dSVinicius Costa Gomes 	 * reprogram it to the earliest one, so we change the admin
1267a3d43c0dSVinicius Costa Gomes 	 * schedule to the operational one at the right time.
1268a3d43c0dSVinicius Costa Gomes 	 */
1269a3d43c0dSVinicius Costa Gomes 	start = min_t(ktime_t, start, expires);
12705a781ccbSVinicius Costa Gomes 
12715a781ccbSVinicius Costa Gomes 	hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
12725a781ccbSVinicius Costa Gomes }
12735a781ccbSVinicius Costa Gomes 
12747b9eba7bSLeandro Dorileo static void taprio_set_picos_per_byte(struct net_device *dev,
12757b9eba7bSLeandro Dorileo 				      struct taprio_sched *q)
12767b9eba7bSLeandro Dorileo {
12777b9eba7bSLeandro Dorileo 	struct ethtool_link_ksettings ecmd;
1278f04b514cSVladimir Oltean 	int speed = SPEED_10;
1279f04b514cSVladimir Oltean 	int picos_per_byte;
1280f04b514cSVladimir Oltean 	int err;
12817b9eba7bSLeandro Dorileo 
1282f04b514cSVladimir Oltean 	err = __ethtool_get_link_ksettings(dev, &ecmd);
1283f04b514cSVladimir Oltean 	if (err < 0)
1284f04b514cSVladimir Oltean 		goto skip;
1285f04b514cSVladimir Oltean 
12869a9251a3SVladimir Oltean 	if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1287f04b514cSVladimir Oltean 		speed = ecmd.base.speed;
1288f04b514cSVladimir Oltean 
1289f04b514cSVladimir Oltean skip:
129068ce6688SVladimir Oltean 	picos_per_byte = (USEC_PER_SEC * 8) / speed;
12917b9eba7bSLeandro Dorileo 
12927b9eba7bSLeandro Dorileo 	atomic64_set(&q->picos_per_byte, picos_per_byte);
12937b9eba7bSLeandro Dorileo 	netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
12947b9eba7bSLeandro Dorileo 		   dev->name, (long long)atomic64_read(&q->picos_per_byte),
12957b9eba7bSLeandro Dorileo 		   ecmd.base.speed);
12967b9eba7bSLeandro Dorileo }
12977b9eba7bSLeandro Dorileo 
12987b9eba7bSLeandro Dorileo static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
12997b9eba7bSLeandro Dorileo 			       void *ptr)
13007b9eba7bSLeandro Dorileo {
13017b9eba7bSLeandro Dorileo 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1302fed87cc6SVladimir Oltean 	struct sched_gate_list *oper, *admin;
1303fed87cc6SVladimir Oltean 	struct qdisc_size_table *stab;
13047b9eba7bSLeandro Dorileo 	struct taprio_sched *q;
13057b9eba7bSLeandro Dorileo 
13067b9eba7bSLeandro Dorileo 	ASSERT_RTNL();
13077b9eba7bSLeandro Dorileo 
13087b9eba7bSLeandro Dorileo 	if (event != NETDEV_UP && event != NETDEV_CHANGE)
13097b9eba7bSLeandro Dorileo 		return NOTIFY_DONE;
13107b9eba7bSLeandro Dorileo 
13117b9eba7bSLeandro Dorileo 	list_for_each_entry(q, &taprio_list, taprio_list) {
1312fc4f2fd0SVladimir Oltean 		if (dev != qdisc_dev(q->root))
1313fc4f2fd0SVladimir Oltean 			continue;
1314fc4f2fd0SVladimir Oltean 
1315fc4f2fd0SVladimir Oltean 		taprio_set_picos_per_byte(dev, q);
1316fed87cc6SVladimir Oltean 
1317fed87cc6SVladimir Oltean 		stab = rtnl_dereference(q->root->stab);
1318fed87cc6SVladimir Oltean 
1319fed87cc6SVladimir Oltean 		oper = rtnl_dereference(q->oper_sched);
1320fed87cc6SVladimir Oltean 		if (oper)
1321fed87cc6SVladimir Oltean 			taprio_update_queue_max_sdu(q, oper, stab);
1322fed87cc6SVladimir Oltean 
1323fed87cc6SVladimir Oltean 		admin = rtnl_dereference(q->admin_sched);
1324fed87cc6SVladimir Oltean 		if (admin)
1325fed87cc6SVladimir Oltean 			taprio_update_queue_max_sdu(q, admin, stab);
1326fed87cc6SVladimir Oltean 
13277b9eba7bSLeandro Dorileo 		break;
13287b9eba7bSLeandro Dorileo 	}
13297b9eba7bSLeandro Dorileo 
13307b9eba7bSLeandro Dorileo 	return NOTIFY_DONE;
13317b9eba7bSLeandro Dorileo }
13327b9eba7bSLeandro Dorileo 
13334cfd5779SVedang Patel static void setup_txtime(struct taprio_sched *q,
13344cfd5779SVedang Patel 			 struct sched_gate_list *sched, ktime_t base)
13354cfd5779SVedang Patel {
13364cfd5779SVedang Patel 	struct sched_entry *entry;
13374cfd5779SVedang Patel 	u32 interval = 0;
13384cfd5779SVedang Patel 
13394cfd5779SVedang Patel 	list_for_each_entry(entry, &sched->entries, list) {
13404cfd5779SVedang Patel 		entry->next_txtime = ktime_add_ns(base, interval);
13414cfd5779SVedang Patel 		interval += entry->interval;
13424cfd5779SVedang Patel 	}
13434cfd5779SVedang Patel }
13444cfd5779SVedang Patel 
13459c66d156SVinicius Costa Gomes static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
13469c66d156SVinicius Costa Gomes {
13479c66d156SVinicius Costa Gomes 	struct __tc_taprio_qopt_offload *__offload;
13489c66d156SVinicius Costa Gomes 
134911a33de2SGustavo A. R. Silva 	__offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
135011a33de2SGustavo A. R. Silva 			    GFP_KERNEL);
13519c66d156SVinicius Costa Gomes 	if (!__offload)
13529c66d156SVinicius Costa Gomes 		return NULL;
13539c66d156SVinicius Costa Gomes 
13549c66d156SVinicius Costa Gomes 	refcount_set(&__offload->users, 1);
13559c66d156SVinicius Costa Gomes 
13569c66d156SVinicius Costa Gomes 	return &__offload->offload;
13579c66d156SVinicius Costa Gomes }
13589c66d156SVinicius Costa Gomes 
13599c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
13609c66d156SVinicius Costa Gomes 						  *offload)
13619c66d156SVinicius Costa Gomes {
13629c66d156SVinicius Costa Gomes 	struct __tc_taprio_qopt_offload *__offload;
13639c66d156SVinicius Costa Gomes 
13649c66d156SVinicius Costa Gomes 	__offload = container_of(offload, struct __tc_taprio_qopt_offload,
13659c66d156SVinicius Costa Gomes 				 offload);
13669c66d156SVinicius Costa Gomes 
13679c66d156SVinicius Costa Gomes 	refcount_inc(&__offload->users);
13689c66d156SVinicius Costa Gomes 
13699c66d156SVinicius Costa Gomes 	return offload;
13709c66d156SVinicius Costa Gomes }
13719c66d156SVinicius Costa Gomes EXPORT_SYMBOL_GPL(taprio_offload_get);
13729c66d156SVinicius Costa Gomes 
13739c66d156SVinicius Costa Gomes void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
13749c66d156SVinicius Costa Gomes {
13759c66d156SVinicius Costa Gomes 	struct __tc_taprio_qopt_offload *__offload;
13769c66d156SVinicius Costa Gomes 
13779c66d156SVinicius Costa Gomes 	__offload = container_of(offload, struct __tc_taprio_qopt_offload,
13789c66d156SVinicius Costa Gomes 				 offload);
13799c66d156SVinicius Costa Gomes 
13809c66d156SVinicius Costa Gomes 	if (!refcount_dec_and_test(&__offload->users))
13819c66d156SVinicius Costa Gomes 		return;
13829c66d156SVinicius Costa Gomes 
13839c66d156SVinicius Costa Gomes 	kfree(__offload);
13849c66d156SVinicius Costa Gomes }
13859c66d156SVinicius Costa Gomes EXPORT_SYMBOL_GPL(taprio_offload_free);
13869c66d156SVinicius Costa Gomes 
13879c66d156SVinicius Costa Gomes /* The function will only serve to keep the pointers to the "oper" and "admin"
13889c66d156SVinicius Costa Gomes  * schedules valid in relation to their base times, so when calling dump() the
13899c66d156SVinicius Costa Gomes  * users looks at the right schedules.
13909c66d156SVinicius Costa Gomes  * When using full offload, the admin configuration is promoted to oper at the
13919c66d156SVinicius Costa Gomes  * base_time in the PHC time domain.  But because the system time is not
13929c66d156SVinicius Costa Gomes  * necessarily in sync with that, we can't just trigger a hrtimer to call
13939c66d156SVinicius Costa Gomes  * switch_schedules at the right hardware time.
13949c66d156SVinicius Costa Gomes  * At the moment we call this by hand right away from taprio, but in the future
13959c66d156SVinicius Costa Gomes  * it will be useful to create a mechanism for drivers to notify taprio of the
13969c66d156SVinicius Costa Gomes  * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
13979c66d156SVinicius Costa Gomes  * This is left as TODO.
13989c66d156SVinicius Costa Gomes  */
1399d665c128SYi Wang static void taprio_offload_config_changed(struct taprio_sched *q)
14009c66d156SVinicius Costa Gomes {
14019c66d156SVinicius Costa Gomes 	struct sched_gate_list *oper, *admin;
14029c66d156SVinicius Costa Gomes 
1403c8cbe123SVladimir Oltean 	oper = rtnl_dereference(q->oper_sched);
1404c8cbe123SVladimir Oltean 	admin = rtnl_dereference(q->admin_sched);
14059c66d156SVinicius Costa Gomes 
14069c66d156SVinicius Costa Gomes 	switch_schedules(q, &admin, &oper);
14079c66d156SVinicius Costa Gomes }
14089c66d156SVinicius Costa Gomes 
140909e31cf0SVinicius Costa Gomes static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
141009e31cf0SVinicius Costa Gomes {
141109e31cf0SVinicius Costa Gomes 	u32 i, queue_mask = 0;
141209e31cf0SVinicius Costa Gomes 
141309e31cf0SVinicius Costa Gomes 	for (i = 0; i < dev->num_tc; i++) {
141409e31cf0SVinicius Costa Gomes 		u32 offset, count;
141509e31cf0SVinicius Costa Gomes 
141609e31cf0SVinicius Costa Gomes 		if (!(tc_mask & BIT(i)))
141709e31cf0SVinicius Costa Gomes 			continue;
141809e31cf0SVinicius Costa Gomes 
141909e31cf0SVinicius Costa Gomes 		offset = dev->tc_to_txq[i].offset;
142009e31cf0SVinicius Costa Gomes 		count = dev->tc_to_txq[i].count;
142109e31cf0SVinicius Costa Gomes 
142209e31cf0SVinicius Costa Gomes 		queue_mask |= GENMASK(offset + count - 1, offset);
142309e31cf0SVinicius Costa Gomes 	}
142409e31cf0SVinicius Costa Gomes 
142509e31cf0SVinicius Costa Gomes 	return queue_mask;
142609e31cf0SVinicius Costa Gomes }
142709e31cf0SVinicius Costa Gomes 
142809e31cf0SVinicius Costa Gomes static void taprio_sched_to_offload(struct net_device *dev,
14299c66d156SVinicius Costa Gomes 				    struct sched_gate_list *sched,
1430522d15eaSVladimir Oltean 				    struct tc_taprio_qopt_offload *offload,
1431522d15eaSVladimir Oltean 				    const struct tc_taprio_caps *caps)
14329c66d156SVinicius Costa Gomes {
14339c66d156SVinicius Costa Gomes 	struct sched_entry *entry;
14349c66d156SVinicius Costa Gomes 	int i = 0;
14359c66d156SVinicius Costa Gomes 
14369c66d156SVinicius Costa Gomes 	offload->base_time = sched->base_time;
14379c66d156SVinicius Costa Gomes 	offload->cycle_time = sched->cycle_time;
14389c66d156SVinicius Costa Gomes 	offload->cycle_time_extension = sched->cycle_time_extension;
14399c66d156SVinicius Costa Gomes 
14409c66d156SVinicius Costa Gomes 	list_for_each_entry(entry, &sched->entries, list) {
14419c66d156SVinicius Costa Gomes 		struct tc_taprio_sched_entry *e = &offload->entries[i];
14429c66d156SVinicius Costa Gomes 
14439c66d156SVinicius Costa Gomes 		e->command = entry->command;
14449c66d156SVinicius Costa Gomes 		e->interval = entry->interval;
1445522d15eaSVladimir Oltean 		if (caps->gate_mask_per_txq)
1446522d15eaSVladimir Oltean 			e->gate_mask = tc_map_to_queue_mask(dev,
1447522d15eaSVladimir Oltean 							    entry->gate_mask);
1448522d15eaSVladimir Oltean 		else
1449522d15eaSVladimir Oltean 			e->gate_mask = entry->gate_mask;
145009e31cf0SVinicius Costa Gomes 
14519c66d156SVinicius Costa Gomes 		i++;
14529c66d156SVinicius Costa Gomes 	}
14539c66d156SVinicius Costa Gomes 
14549c66d156SVinicius Costa Gomes 	offload->num_entries = i;
14559c66d156SVinicius Costa Gomes }
14569c66d156SVinicius Costa Gomes 
14572f530df7SVladimir Oltean static void taprio_detect_broken_mqprio(struct taprio_sched *q)
14582f530df7SVladimir Oltean {
14592f530df7SVladimir Oltean 	struct net_device *dev = qdisc_dev(q->root);
14602f530df7SVladimir Oltean 	struct tc_taprio_caps caps;
14612f530df7SVladimir Oltean 
14622f530df7SVladimir Oltean 	qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
14632f530df7SVladimir Oltean 				 &caps, sizeof(caps));
14642f530df7SVladimir Oltean 
14652f530df7SVladimir Oltean 	q->broken_mqprio = caps.broken_mqprio;
14662f530df7SVladimir Oltean 	if (q->broken_mqprio)
14672f530df7SVladimir Oltean 		static_branch_inc(&taprio_have_broken_mqprio);
14682f530df7SVladimir Oltean 	else
14692f530df7SVladimir Oltean 		static_branch_inc(&taprio_have_working_mqprio);
14702f530df7SVladimir Oltean 
14712f530df7SVladimir Oltean 	q->detected_mqprio = true;
14722f530df7SVladimir Oltean }
14732f530df7SVladimir Oltean 
14742f530df7SVladimir Oltean static void taprio_cleanup_broken_mqprio(struct taprio_sched *q)
14752f530df7SVladimir Oltean {
14762f530df7SVladimir Oltean 	if (!q->detected_mqprio)
14772f530df7SVladimir Oltean 		return;
14782f530df7SVladimir Oltean 
14792f530df7SVladimir Oltean 	if (q->broken_mqprio)
14802f530df7SVladimir Oltean 		static_branch_dec(&taprio_have_broken_mqprio);
14812f530df7SVladimir Oltean 	else
14822f530df7SVladimir Oltean 		static_branch_dec(&taprio_have_working_mqprio);
14832f530df7SVladimir Oltean }
14842f530df7SVladimir Oltean 
14859c66d156SVinicius Costa Gomes static int taprio_enable_offload(struct net_device *dev,
14869c66d156SVinicius Costa Gomes 				 struct taprio_sched *q,
14879c66d156SVinicius Costa Gomes 				 struct sched_gate_list *sched,
14889c66d156SVinicius Costa Gomes 				 struct netlink_ext_ack *extack)
14899c66d156SVinicius Costa Gomes {
14909c66d156SVinicius Costa Gomes 	const struct net_device_ops *ops = dev->netdev_ops;
14919c66d156SVinicius Costa Gomes 	struct tc_taprio_qopt_offload *offload;
1492a54fc09eSVladimir Oltean 	struct tc_taprio_caps caps;
1493a54fc09eSVladimir Oltean 	int tc, err = 0;
14949c66d156SVinicius Costa Gomes 
14959c66d156SVinicius Costa Gomes 	if (!ops->ndo_setup_tc) {
14969c66d156SVinicius Costa Gomes 		NL_SET_ERR_MSG(extack,
14979c66d156SVinicius Costa Gomes 			       "Device does not support taprio offload");
14989c66d156SVinicius Costa Gomes 		return -EOPNOTSUPP;
14999c66d156SVinicius Costa Gomes 	}
15009c66d156SVinicius Costa Gomes 
1501a54fc09eSVladimir Oltean 	qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1502a54fc09eSVladimir Oltean 				 &caps, sizeof(caps));
1503a54fc09eSVladimir Oltean 
1504a54fc09eSVladimir Oltean 	if (!caps.supports_queue_max_sdu) {
1505a54fc09eSVladimir Oltean 		for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
1506a54fc09eSVladimir Oltean 			if (q->max_sdu[tc]) {
1507a54fc09eSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
1508a54fc09eSVladimir Oltean 						   "Device does not handle queueMaxSDU");
1509a54fc09eSVladimir Oltean 				return -EOPNOTSUPP;
1510a54fc09eSVladimir Oltean 			}
1511a54fc09eSVladimir Oltean 		}
1512a54fc09eSVladimir Oltean 	}
1513a54fc09eSVladimir Oltean 
15149c66d156SVinicius Costa Gomes 	offload = taprio_offload_alloc(sched->num_entries);
15159c66d156SVinicius Costa Gomes 	if (!offload) {
15169c66d156SVinicius Costa Gomes 		NL_SET_ERR_MSG(extack,
15179c66d156SVinicius Costa Gomes 			       "Not enough memory for enabling offload mode");
15189c66d156SVinicius Costa Gomes 		return -ENOMEM;
15199c66d156SVinicius Costa Gomes 	}
15209c66d156SVinicius Costa Gomes 	offload->enable = 1;
152109c794c0SVladimir Oltean 	mqprio_qopt_reconstruct(dev, &offload->mqprio.qopt);
1522522d15eaSVladimir Oltean 	taprio_sched_to_offload(dev, sched, offload, &caps);
15239c66d156SVinicius Costa Gomes 
1524a54fc09eSVladimir Oltean 	for (tc = 0; tc < TC_MAX_QUEUE; tc++)
1525a54fc09eSVladimir Oltean 		offload->max_sdu[tc] = q->max_sdu[tc];
1526a54fc09eSVladimir Oltean 
15279c66d156SVinicius Costa Gomes 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
15289c66d156SVinicius Costa Gomes 	if (err < 0) {
15299c66d156SVinicius Costa Gomes 		NL_SET_ERR_MSG(extack,
15309c66d156SVinicius Costa Gomes 			       "Device failed to setup taprio offload");
15319c66d156SVinicius Costa Gomes 		goto done;
15329c66d156SVinicius Costa Gomes 	}
15339c66d156SVinicius Costa Gomes 
1534db46e3a8SVladimir Oltean 	q->offloaded = true;
1535db46e3a8SVladimir Oltean 
15369c66d156SVinicius Costa Gomes done:
15379c66d156SVinicius Costa Gomes 	taprio_offload_free(offload);
15389c66d156SVinicius Costa Gomes 
15399c66d156SVinicius Costa Gomes 	return err;
15409c66d156SVinicius Costa Gomes }
15419c66d156SVinicius Costa Gomes 
15429c66d156SVinicius Costa Gomes static int taprio_disable_offload(struct net_device *dev,
15439c66d156SVinicius Costa Gomes 				  struct taprio_sched *q,
15449c66d156SVinicius Costa Gomes 				  struct netlink_ext_ack *extack)
15459c66d156SVinicius Costa Gomes {
15469c66d156SVinicius Costa Gomes 	const struct net_device_ops *ops = dev->netdev_ops;
15479c66d156SVinicius Costa Gomes 	struct tc_taprio_qopt_offload *offload;
15489c66d156SVinicius Costa Gomes 	int err;
15499c66d156SVinicius Costa Gomes 
1550db46e3a8SVladimir Oltean 	if (!q->offloaded)
15519c66d156SVinicius Costa Gomes 		return 0;
15529c66d156SVinicius Costa Gomes 
15539c66d156SVinicius Costa Gomes 	offload = taprio_offload_alloc(0);
15549c66d156SVinicius Costa Gomes 	if (!offload) {
15559c66d156SVinicius Costa Gomes 		NL_SET_ERR_MSG(extack,
15569c66d156SVinicius Costa Gomes 			       "Not enough memory to disable offload mode");
15579c66d156SVinicius Costa Gomes 		return -ENOMEM;
15589c66d156SVinicius Costa Gomes 	}
15599c66d156SVinicius Costa Gomes 	offload->enable = 0;
15609c66d156SVinicius Costa Gomes 
15619c66d156SVinicius Costa Gomes 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
15629c66d156SVinicius Costa Gomes 	if (err < 0) {
15639c66d156SVinicius Costa Gomes 		NL_SET_ERR_MSG(extack,
15649c66d156SVinicius Costa Gomes 			       "Device failed to disable offload");
15659c66d156SVinicius Costa Gomes 		goto out;
15669c66d156SVinicius Costa Gomes 	}
15679c66d156SVinicius Costa Gomes 
1568db46e3a8SVladimir Oltean 	q->offloaded = false;
1569db46e3a8SVladimir Oltean 
15709c66d156SVinicius Costa Gomes out:
15719c66d156SVinicius Costa Gomes 	taprio_offload_free(offload);
15729c66d156SVinicius Costa Gomes 
15739c66d156SVinicius Costa Gomes 	return err;
15749c66d156SVinicius Costa Gomes }
15759c66d156SVinicius Costa Gomes 
15769c66d156SVinicius Costa Gomes /* If full offload is enabled, the only possible clockid is the net device's
15779c66d156SVinicius Costa Gomes  * PHC. For that reason, specifying a clockid through netlink is incorrect.
15789c66d156SVinicius Costa Gomes  * For txtime-assist, it is implicitly assumed that the device's PHC is kept
15799c66d156SVinicius Costa Gomes  * in sync with the specified clockid via a user space daemon such as phc2sys.
15809c66d156SVinicius Costa Gomes  * For both software taprio and txtime-assist, the clockid is used for the
15819c66d156SVinicius Costa Gomes  * hrtimer that advances the schedule and hence mandatory.
15829c66d156SVinicius Costa Gomes  */
15839c66d156SVinicius Costa Gomes static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
15849c66d156SVinicius Costa Gomes 				struct netlink_ext_ack *extack)
15859c66d156SVinicius Costa Gomes {
15869c66d156SVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
15879c66d156SVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
15889c66d156SVinicius Costa Gomes 	int err = -EINVAL;
15899c66d156SVinicius Costa Gomes 
15909c66d156SVinicius Costa Gomes 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
15919c66d156SVinicius Costa Gomes 		const struct ethtool_ops *ops = dev->ethtool_ops;
15929c66d156SVinicius Costa Gomes 		struct ethtool_ts_info info = {
15939c66d156SVinicius Costa Gomes 			.cmd = ETHTOOL_GET_TS_INFO,
15949c66d156SVinicius Costa Gomes 			.phc_index = -1,
15959c66d156SVinicius Costa Gomes 		};
15969c66d156SVinicius Costa Gomes 
15979c66d156SVinicius Costa Gomes 		if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
15989c66d156SVinicius Costa Gomes 			NL_SET_ERR_MSG(extack,
15999c66d156SVinicius Costa Gomes 				       "The 'clockid' cannot be specified for full offload");
16009c66d156SVinicius Costa Gomes 			goto out;
16019c66d156SVinicius Costa Gomes 		}
16029c66d156SVinicius Costa Gomes 
16039c66d156SVinicius Costa Gomes 		if (ops && ops->get_ts_info)
16049c66d156SVinicius Costa Gomes 			err = ops->get_ts_info(dev, &info);
16059c66d156SVinicius Costa Gomes 
16069c66d156SVinicius Costa Gomes 		if (err || info.phc_index < 0) {
16079c66d156SVinicius Costa Gomes 			NL_SET_ERR_MSG(extack,
16089c66d156SVinicius Costa Gomes 				       "Device does not have a PTP clock");
16099c66d156SVinicius Costa Gomes 			err = -ENOTSUPP;
16109c66d156SVinicius Costa Gomes 			goto out;
16119c66d156SVinicius Costa Gomes 		}
16129c66d156SVinicius Costa Gomes 	} else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
16139c66d156SVinicius Costa Gomes 		int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
16146dc25401SEric Dumazet 		enum tk_offsets tk_offset;
16159c66d156SVinicius Costa Gomes 
16169c66d156SVinicius Costa Gomes 		/* We only support static clockids and we don't allow
16179c66d156SVinicius Costa Gomes 		 * for it to be modified after the first init.
16189c66d156SVinicius Costa Gomes 		 */
16199c66d156SVinicius Costa Gomes 		if (clockid < 0 ||
16209c66d156SVinicius Costa Gomes 		    (q->clockid != -1 && q->clockid != clockid)) {
16219c66d156SVinicius Costa Gomes 			NL_SET_ERR_MSG(extack,
16229c66d156SVinicius Costa Gomes 				       "Changing the 'clockid' of a running schedule is not supported");
16239c66d156SVinicius Costa Gomes 			err = -ENOTSUPP;
16249c66d156SVinicius Costa Gomes 			goto out;
16259c66d156SVinicius Costa Gomes 		}
16269c66d156SVinicius Costa Gomes 
16279c66d156SVinicius Costa Gomes 		switch (clockid) {
16289c66d156SVinicius Costa Gomes 		case CLOCK_REALTIME:
16296dc25401SEric Dumazet 			tk_offset = TK_OFFS_REAL;
16309c66d156SVinicius Costa Gomes 			break;
16319c66d156SVinicius Costa Gomes 		case CLOCK_MONOTONIC:
16326dc25401SEric Dumazet 			tk_offset = TK_OFFS_MAX;
16339c66d156SVinicius Costa Gomes 			break;
16349c66d156SVinicius Costa Gomes 		case CLOCK_BOOTTIME:
16356dc25401SEric Dumazet 			tk_offset = TK_OFFS_BOOT;
16369c66d156SVinicius Costa Gomes 			break;
16379c66d156SVinicius Costa Gomes 		case CLOCK_TAI:
16386dc25401SEric Dumazet 			tk_offset = TK_OFFS_TAI;
16399c66d156SVinicius Costa Gomes 			break;
16409c66d156SVinicius Costa Gomes 		default:
16419c66d156SVinicius Costa Gomes 			NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
16429c66d156SVinicius Costa Gomes 			err = -EINVAL;
16439c66d156SVinicius Costa Gomes 			goto out;
16449c66d156SVinicius Costa Gomes 		}
16456dc25401SEric Dumazet 		/* This pairs with READ_ONCE() in taprio_mono_to_any */
16466dc25401SEric Dumazet 		WRITE_ONCE(q->tk_offset, tk_offset);
16479c66d156SVinicius Costa Gomes 
16489c66d156SVinicius Costa Gomes 		q->clockid = clockid;
16499c66d156SVinicius Costa Gomes 	} else {
16509c66d156SVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
16519c66d156SVinicius Costa Gomes 		goto out;
16529c66d156SVinicius Costa Gomes 	}
1653a954380aSVinicius Costa Gomes 
1654a954380aSVinicius Costa Gomes 	/* Everything went ok, return success. */
1655a954380aSVinicius Costa Gomes 	err = 0;
1656a954380aSVinicius Costa Gomes 
16579c66d156SVinicius Costa Gomes out:
16589c66d156SVinicius Costa Gomes 	return err;
16599c66d156SVinicius Costa Gomes }
16609c66d156SVinicius Costa Gomes 
1661a54fc09eSVladimir Oltean static int taprio_parse_tc_entry(struct Qdisc *sch,
1662a54fc09eSVladimir Oltean 				 struct nlattr *opt,
1663a54fc09eSVladimir Oltean 				 u32 max_sdu[TC_QOPT_MAX_QUEUE],
1664a54fc09eSVladimir Oltean 				 unsigned long *seen_tcs,
1665a54fc09eSVladimir Oltean 				 struct netlink_ext_ack *extack)
1666a54fc09eSVladimir Oltean {
1667a54fc09eSVladimir Oltean 	struct nlattr *tb[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { };
1668a54fc09eSVladimir Oltean 	struct net_device *dev = qdisc_dev(sch);
1669a54fc09eSVladimir Oltean 	u32 val = 0;
1670a54fc09eSVladimir Oltean 	int err, tc;
1671a54fc09eSVladimir Oltean 
1672a54fc09eSVladimir Oltean 	err = nla_parse_nested(tb, TCA_TAPRIO_TC_ENTRY_MAX, opt,
1673a54fc09eSVladimir Oltean 			       taprio_tc_policy, extack);
1674a54fc09eSVladimir Oltean 	if (err < 0)
1675a54fc09eSVladimir Oltean 		return err;
1676a54fc09eSVladimir Oltean 
1677a54fc09eSVladimir Oltean 	if (!tb[TCA_TAPRIO_TC_ENTRY_INDEX]) {
1678a54fc09eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "TC entry index missing");
1679a54fc09eSVladimir Oltean 		return -EINVAL;
1680a54fc09eSVladimir Oltean 	}
1681a54fc09eSVladimir Oltean 
1682a54fc09eSVladimir Oltean 	tc = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_INDEX]);
1683a54fc09eSVladimir Oltean 	if (tc >= TC_QOPT_MAX_QUEUE) {
1684a54fc09eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "TC entry index out of range");
1685a54fc09eSVladimir Oltean 		return -ERANGE;
1686a54fc09eSVladimir Oltean 	}
1687a54fc09eSVladimir Oltean 
1688a54fc09eSVladimir Oltean 	if (*seen_tcs & BIT(tc)) {
1689a54fc09eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Duplicate TC entry");
1690a54fc09eSVladimir Oltean 		return -EINVAL;
1691a54fc09eSVladimir Oltean 	}
1692a54fc09eSVladimir Oltean 
1693a54fc09eSVladimir Oltean 	*seen_tcs |= BIT(tc);
1694a54fc09eSVladimir Oltean 
1695a54fc09eSVladimir Oltean 	if (tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU])
1696a54fc09eSVladimir Oltean 		val = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]);
1697a54fc09eSVladimir Oltean 
1698a54fc09eSVladimir Oltean 	if (val > dev->max_mtu) {
1699a54fc09eSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "TC max SDU exceeds device max MTU");
1700a54fc09eSVladimir Oltean 		return -ERANGE;
1701a54fc09eSVladimir Oltean 	}
1702a54fc09eSVladimir Oltean 
1703a54fc09eSVladimir Oltean 	max_sdu[tc] = val;
1704a54fc09eSVladimir Oltean 
1705a54fc09eSVladimir Oltean 	return 0;
1706a54fc09eSVladimir Oltean }
1707a54fc09eSVladimir Oltean 
1708a54fc09eSVladimir Oltean static int taprio_parse_tc_entries(struct Qdisc *sch,
1709a54fc09eSVladimir Oltean 				   struct nlattr *opt,
1710a54fc09eSVladimir Oltean 				   struct netlink_ext_ack *extack)
1711a54fc09eSVladimir Oltean {
1712a54fc09eSVladimir Oltean 	struct taprio_sched *q = qdisc_priv(sch);
1713a54fc09eSVladimir Oltean 	u32 max_sdu[TC_QOPT_MAX_QUEUE];
1714a54fc09eSVladimir Oltean 	unsigned long seen_tcs = 0;
1715a54fc09eSVladimir Oltean 	struct nlattr *n;
1716a54fc09eSVladimir Oltean 	int tc, rem;
1717a54fc09eSVladimir Oltean 	int err = 0;
1718a54fc09eSVladimir Oltean 
1719a54fc09eSVladimir Oltean 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
1720a54fc09eSVladimir Oltean 		max_sdu[tc] = q->max_sdu[tc];
1721a54fc09eSVladimir Oltean 
1722a54fc09eSVladimir Oltean 	nla_for_each_nested(n, opt, rem) {
1723a54fc09eSVladimir Oltean 		if (nla_type(n) != TCA_TAPRIO_ATTR_TC_ENTRY)
1724a54fc09eSVladimir Oltean 			continue;
1725a54fc09eSVladimir Oltean 
1726fed87cc6SVladimir Oltean 		err = taprio_parse_tc_entry(sch, n, max_sdu, &seen_tcs,
1727fed87cc6SVladimir Oltean 					    extack);
1728a54fc09eSVladimir Oltean 		if (err)
1729a54fc09eSVladimir Oltean 			goto out;
1730a54fc09eSVladimir Oltean 	}
1731a54fc09eSVladimir Oltean 
1732a878fd46SVladimir Oltean 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
1733a54fc09eSVladimir Oltean 		q->max_sdu[tc] = max_sdu[tc];
1734a54fc09eSVladimir Oltean 
1735a54fc09eSVladimir Oltean out:
1736a54fc09eSVladimir Oltean 	return err;
1737a54fc09eSVladimir Oltean }
1738a54fc09eSVladimir Oltean 
1739b5a0faa3SIvan Khoronzhuk static int taprio_mqprio_cmp(const struct net_device *dev,
1740b5a0faa3SIvan Khoronzhuk 			     const struct tc_mqprio_qopt *mqprio)
1741b5a0faa3SIvan Khoronzhuk {
1742b5a0faa3SIvan Khoronzhuk 	int i;
1743b5a0faa3SIvan Khoronzhuk 
1744b5a0faa3SIvan Khoronzhuk 	if (!mqprio || mqprio->num_tc != dev->num_tc)
1745b5a0faa3SIvan Khoronzhuk 		return -1;
1746b5a0faa3SIvan Khoronzhuk 
1747b5a0faa3SIvan Khoronzhuk 	for (i = 0; i < mqprio->num_tc; i++)
1748b5a0faa3SIvan Khoronzhuk 		if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1749b5a0faa3SIvan Khoronzhuk 		    dev->tc_to_txq[i].offset != mqprio->offset[i])
1750b5a0faa3SIvan Khoronzhuk 			return -1;
1751b5a0faa3SIvan Khoronzhuk 
1752b5a0faa3SIvan Khoronzhuk 	for (i = 0; i <= TC_BITMASK; i++)
1753b5a0faa3SIvan Khoronzhuk 		if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1754b5a0faa3SIvan Khoronzhuk 			return -1;
1755b5a0faa3SIvan Khoronzhuk 
1756b5a0faa3SIvan Khoronzhuk 	return 0;
1757b5a0faa3SIvan Khoronzhuk }
1758b5a0faa3SIvan Khoronzhuk 
1759a9d62274SVinicius Costa Gomes /* The semantics of the 'flags' argument in relation to 'change()'
1760a9d62274SVinicius Costa Gomes  * requests, are interpreted following two rules (which are applied in
1761a9d62274SVinicius Costa Gomes  * this order): (1) an omitted 'flags' argument is interpreted as
1762a9d62274SVinicius Costa Gomes  * zero; (2) the 'flags' of a "running" taprio instance cannot be
1763a9d62274SVinicius Costa Gomes  * changed.
1764a9d62274SVinicius Costa Gomes  */
1765a9d62274SVinicius Costa Gomes static int taprio_new_flags(const struct nlattr *attr, u32 old,
1766a9d62274SVinicius Costa Gomes 			    struct netlink_ext_ack *extack)
1767a9d62274SVinicius Costa Gomes {
1768a9d62274SVinicius Costa Gomes 	u32 new = 0;
1769a9d62274SVinicius Costa Gomes 
1770a9d62274SVinicius Costa Gomes 	if (attr)
1771a9d62274SVinicius Costa Gomes 		new = nla_get_u32(attr);
1772a9d62274SVinicius Costa Gomes 
1773a9d62274SVinicius Costa Gomes 	if (old != TAPRIO_FLAGS_INVALID && old != new) {
1774a9d62274SVinicius Costa Gomes 		NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1775a9d62274SVinicius Costa Gomes 		return -EOPNOTSUPP;
1776a9d62274SVinicius Costa Gomes 	}
1777a9d62274SVinicius Costa Gomes 
1778a9d62274SVinicius Costa Gomes 	if (!taprio_flags_valid(new)) {
1779a9d62274SVinicius Costa Gomes 		NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1780a9d62274SVinicius Costa Gomes 		return -EINVAL;
1781a9d62274SVinicius Costa Gomes 	}
1782a9d62274SVinicius Costa Gomes 
1783a9d62274SVinicius Costa Gomes 	return new;
1784a9d62274SVinicius Costa Gomes }
1785a9d62274SVinicius Costa Gomes 
17865a781ccbSVinicius Costa Gomes static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
17875a781ccbSVinicius Costa Gomes 			 struct netlink_ext_ack *extack)
17885a781ccbSVinicius Costa Gomes {
1789a3d91b2cSVladimir Oltean 	struct qdisc_size_table *stab = rtnl_dereference(sch->stab);
17905a781ccbSVinicius Costa Gomes 	struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1791a3d43c0dSVinicius Costa Gomes 	struct sched_gate_list *oper, *admin, *new_admin;
17925a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
17935a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
17945a781ccbSVinicius Costa Gomes 	struct tc_mqprio_qopt *mqprio = NULL;
1795a3d43c0dSVinicius Costa Gomes 	unsigned long flags;
17965a781ccbSVinicius Costa Gomes 	ktime_t start;
17979c66d156SVinicius Costa Gomes 	int i, err;
17985a781ccbSVinicius Costa Gomes 
17998cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
18005a781ccbSVinicius Costa Gomes 					  taprio_policy, extack);
18015a781ccbSVinicius Costa Gomes 	if (err < 0)
18025a781ccbSVinicius Costa Gomes 		return err;
18035a781ccbSVinicius Costa Gomes 
18045a781ccbSVinicius Costa Gomes 	if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
18055a781ccbSVinicius Costa Gomes 		mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
18065a781ccbSVinicius Costa Gomes 
1807a9d62274SVinicius Costa Gomes 	err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1808a9d62274SVinicius Costa Gomes 			       q->flags, extack);
1809a9d62274SVinicius Costa Gomes 	if (err < 0)
1810a9d62274SVinicius Costa Gomes 		return err;
18114cfd5779SVedang Patel 
1812a9d62274SVinicius Costa Gomes 	q->flags = err;
18134cfd5779SVedang Patel 
1814a9d62274SVinicius Costa Gomes 	err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
18155a781ccbSVinicius Costa Gomes 	if (err < 0)
18165a781ccbSVinicius Costa Gomes 		return err;
18175a781ccbSVinicius Costa Gomes 
1818a54fc09eSVladimir Oltean 	err = taprio_parse_tc_entries(sch, opt, extack);
1819a54fc09eSVladimir Oltean 	if (err)
1820a54fc09eSVladimir Oltean 		return err;
1821a54fc09eSVladimir Oltean 
1822a3d43c0dSVinicius Costa Gomes 	new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1823a3d43c0dSVinicius Costa Gomes 	if (!new_admin) {
1824a3d43c0dSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1825a3d43c0dSVinicius Costa Gomes 		return -ENOMEM;
1826a3d43c0dSVinicius Costa Gomes 	}
1827a3d43c0dSVinicius Costa Gomes 	INIT_LIST_HEAD(&new_admin->entries);
18285a781ccbSVinicius Costa Gomes 
182918cdd2f0SVladimir Oltean 	oper = rtnl_dereference(q->oper_sched);
183018cdd2f0SVladimir Oltean 	admin = rtnl_dereference(q->admin_sched);
18315a781ccbSVinicius Costa Gomes 
1832b5a0faa3SIvan Khoronzhuk 	/* no changes - no new mqprio settings */
1833b5a0faa3SIvan Khoronzhuk 	if (!taprio_mqprio_cmp(dev, mqprio))
1834b5a0faa3SIvan Khoronzhuk 		mqprio = NULL;
1835b5a0faa3SIvan Khoronzhuk 
1836a3d43c0dSVinicius Costa Gomes 	if (mqprio && (oper || admin)) {
1837a3d43c0dSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1838a3d43c0dSVinicius Costa Gomes 		err = -ENOTSUPP;
1839a3d43c0dSVinicius Costa Gomes 		goto free_sched;
18405a781ccbSVinicius Costa Gomes 	}
18415a781ccbSVinicius Costa Gomes 
18425652e63dSVinicius Costa Gomes 	if (mqprio) {
1843efe487fcSHaimin Zhang 		err = netdev_set_num_tc(dev, mqprio->num_tc);
1844efe487fcSHaimin Zhang 		if (err)
1845efe487fcSHaimin Zhang 			goto free_sched;
18462f530df7SVladimir Oltean 		for (i = 0; i < mqprio->num_tc; i++) {
18475652e63dSVinicius Costa Gomes 			netdev_set_tc_queue(dev, i,
18485652e63dSVinicius Costa Gomes 					    mqprio->count[i],
18495652e63dSVinicius Costa Gomes 					    mqprio->offset[i]);
18502f530df7SVladimir Oltean 			q->cur_txq[i] = mqprio->offset[i];
18512f530df7SVladimir Oltean 		}
18525652e63dSVinicius Costa Gomes 
18535652e63dSVinicius Costa Gomes 		/* Always use supplied priority mappings */
18545652e63dSVinicius Costa Gomes 		for (i = 0; i <= TC_BITMASK; i++)
18555652e63dSVinicius Costa Gomes 			netdev_set_prio_tc_map(dev, i,
18565652e63dSVinicius Costa Gomes 					       mqprio->prio_tc_map[i]);
18575652e63dSVinicius Costa Gomes 	}
18585652e63dSVinicius Costa Gomes 
185909dbdf28SVladimir Oltean 	err = parse_taprio_schedule(q, tb, new_admin, extack);
186009dbdf28SVladimir Oltean 	if (err < 0)
186109dbdf28SVladimir Oltean 		goto free_sched;
186209dbdf28SVladimir Oltean 
186309dbdf28SVladimir Oltean 	if (new_admin->num_entries == 0) {
186409dbdf28SVladimir Oltean 		NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
186509dbdf28SVladimir Oltean 		err = -EINVAL;
186609dbdf28SVladimir Oltean 		goto free_sched;
186709dbdf28SVladimir Oltean 	}
186809dbdf28SVladimir Oltean 
186909dbdf28SVladimir Oltean 	err = taprio_parse_clockid(sch, tb, extack);
187009dbdf28SVladimir Oltean 	if (err < 0)
187109dbdf28SVladimir Oltean 		goto free_sched;
187209dbdf28SVladimir Oltean 
187309dbdf28SVladimir Oltean 	taprio_set_picos_per_byte(dev, q);
187409dbdf28SVladimir Oltean 	taprio_update_queue_max_sdu(q, new_admin, stab);
187509dbdf28SVladimir Oltean 
1876a9d62274SVinicius Costa Gomes 	if (FULL_OFFLOAD_IS_ENABLED(q->flags))
187709e31cf0SVinicius Costa Gomes 		err = taprio_enable_offload(dev, q, new_admin, extack);
18789c66d156SVinicius Costa Gomes 	else
18799c66d156SVinicius Costa Gomes 		err = taprio_disable_offload(dev, q, extack);
18809c66d156SVinicius Costa Gomes 	if (err)
18819c66d156SVinicius Costa Gomes 		goto free_sched;
18829c66d156SVinicius Costa Gomes 
1883a3d43c0dSVinicius Costa Gomes 	/* Protects against enqueue()/dequeue() */
1884a3d43c0dSVinicius Costa Gomes 	spin_lock_bh(qdisc_lock(sch));
1885a3d43c0dSVinicius Costa Gomes 
18864cfd5779SVedang Patel 	if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
18874cfd5779SVedang Patel 		if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
18884cfd5779SVedang Patel 			NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
18894cfd5779SVedang Patel 			err = -EINVAL;
18904cfd5779SVedang Patel 			goto unlock;
18914cfd5779SVedang Patel 		}
18924cfd5779SVedang Patel 
1893a5b64700SVedang Patel 		q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
18944cfd5779SVedang Patel 	}
18954cfd5779SVedang Patel 
1896a9d62274SVinicius Costa Gomes 	if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1897a9d62274SVinicius Costa Gomes 	    !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
18984cfd5779SVedang Patel 	    !hrtimer_active(&q->advance_timer)) {
1899a3d43c0dSVinicius Costa Gomes 		hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1900a3d43c0dSVinicius Costa Gomes 		q->advance_timer.function = advance_sched;
19015a781ccbSVinicius Costa Gomes 	}
19025a781ccbSVinicius Costa Gomes 
1903a3d43c0dSVinicius Costa Gomes 	err = taprio_get_start_time(sch, new_admin, &start);
1904a3d43c0dSVinicius Costa Gomes 	if (err < 0) {
1905a3d43c0dSVinicius Costa Gomes 		NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1906a3d43c0dSVinicius Costa Gomes 		goto unlock;
1907a3d43c0dSVinicius Costa Gomes 	}
19085a781ccbSVinicius Costa Gomes 
19094cfd5779SVedang Patel 	setup_txtime(q, new_admin, start);
19104cfd5779SVedang Patel 
1911bfabd41dSVinicius Costa Gomes 	if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
19124cfd5779SVedang Patel 		if (!oper) {
19134cfd5779SVedang Patel 			rcu_assign_pointer(q->oper_sched, new_admin);
19144cfd5779SVedang Patel 			err = 0;
19154cfd5779SVedang Patel 			new_admin = NULL;
19164cfd5779SVedang Patel 			goto unlock;
19174cfd5779SVedang Patel 		}
19184cfd5779SVedang Patel 
19194cfd5779SVedang Patel 		rcu_assign_pointer(q->admin_sched, new_admin);
19204cfd5779SVedang Patel 		if (admin)
19214cfd5779SVedang Patel 			call_rcu(&admin->rcu, taprio_free_sched_cb);
19224cfd5779SVedang Patel 	} else {
1923e5517551SVladimir Oltean 		setup_first_end_time(q, new_admin, start);
1924a3d43c0dSVinicius Costa Gomes 
1925a3d43c0dSVinicius Costa Gomes 		/* Protects against advance_sched() */
1926a3d43c0dSVinicius Costa Gomes 		spin_lock_irqsave(&q->current_entry_lock, flags);
1927a3d43c0dSVinicius Costa Gomes 
1928a3d43c0dSVinicius Costa Gomes 		taprio_start_sched(sch, start, new_admin);
1929a3d43c0dSVinicius Costa Gomes 
1930a3d43c0dSVinicius Costa Gomes 		rcu_assign_pointer(q->admin_sched, new_admin);
1931a3d43c0dSVinicius Costa Gomes 		if (admin)
1932a3d43c0dSVinicius Costa Gomes 			call_rcu(&admin->rcu, taprio_free_sched_cb);
1933a3d43c0dSVinicius Costa Gomes 
1934a3d43c0dSVinicius Costa Gomes 		spin_unlock_irqrestore(&q->current_entry_lock, flags);
19350763b3e8SIvan Khoronzhuk 
1936a9d62274SVinicius Costa Gomes 		if (FULL_OFFLOAD_IS_ENABLED(q->flags))
19370763b3e8SIvan Khoronzhuk 			taprio_offload_config_changed(q);
19384cfd5779SVedang Patel 	}
1939a3d43c0dSVinicius Costa Gomes 
19404cfd5779SVedang Patel 	new_admin = NULL;
1941a3d43c0dSVinicius Costa Gomes 	err = 0;
1942a3d43c0dSVinicius Costa Gomes 
1943a3d91b2cSVladimir Oltean 	if (!stab)
1944a3d91b2cSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1945a3d91b2cSVladimir Oltean 				   "Size table not specified, frame length estimations may be inaccurate");
1946a3d91b2cSVladimir Oltean 
1947a3d43c0dSVinicius Costa Gomes unlock:
1948a3d43c0dSVinicius Costa Gomes 	spin_unlock_bh(qdisc_lock(sch));
1949a3d43c0dSVinicius Costa Gomes 
1950a3d43c0dSVinicius Costa Gomes free_sched:
195151650d33SIvan Khoronzhuk 	if (new_admin)
195251650d33SIvan Khoronzhuk 		call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1953a3d43c0dSVinicius Costa Gomes 
1954a3d43c0dSVinicius Costa Gomes 	return err;
19555a781ccbSVinicius Costa Gomes }
19565a781ccbSVinicius Costa Gomes 
195744d4775cSDavide Caratti static void taprio_reset(struct Qdisc *sch)
195844d4775cSDavide Caratti {
195944d4775cSDavide Caratti 	struct taprio_sched *q = qdisc_priv(sch);
196044d4775cSDavide Caratti 	struct net_device *dev = qdisc_dev(sch);
196144d4775cSDavide Caratti 	int i;
196244d4775cSDavide Caratti 
196344d4775cSDavide Caratti 	hrtimer_cancel(&q->advance_timer);
19643a415d59SEric Dumazet 
196544d4775cSDavide Caratti 	if (q->qdiscs) {
1966698285daSDavide Caratti 		for (i = 0; i < dev->num_tx_queues; i++)
1967698285daSDavide Caratti 			if (q->qdiscs[i])
196844d4775cSDavide Caratti 				qdisc_reset(q->qdiscs[i]);
196944d4775cSDavide Caratti 	}
197044d4775cSDavide Caratti }
197144d4775cSDavide Caratti 
19725a781ccbSVinicius Costa Gomes static void taprio_destroy(struct Qdisc *sch)
19735a781ccbSVinicius Costa Gomes {
19745a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
19755a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
19769af23657SVladimir Oltean 	struct sched_gate_list *oper, *admin;
19775a781ccbSVinicius Costa Gomes 	unsigned int i;
19785a781ccbSVinicius Costa Gomes 
19797b9eba7bSLeandro Dorileo 	list_del(&q->taprio_list);
19807b9eba7bSLeandro Dorileo 
1981a56d447fSEric Dumazet 	/* Note that taprio_reset() might not be called if an error
1982a56d447fSEric Dumazet 	 * happens in qdisc_create(), after taprio_init() has been called.
1983a56d447fSEric Dumazet 	 */
1984a56d447fSEric Dumazet 	hrtimer_cancel(&q->advance_timer);
19853a415d59SEric Dumazet 	qdisc_synchronize(sch);
19865a781ccbSVinicius Costa Gomes 
19879c66d156SVinicius Costa Gomes 	taprio_disable_offload(dev, q, NULL);
19889c66d156SVinicius Costa Gomes 
19895a781ccbSVinicius Costa Gomes 	if (q->qdiscs) {
1990698285daSDavide Caratti 		for (i = 0; i < dev->num_tx_queues; i++)
19915a781ccbSVinicius Costa Gomes 			qdisc_put(q->qdiscs[i]);
19925a781ccbSVinicius Costa Gomes 
19935a781ccbSVinicius Costa Gomes 		kfree(q->qdiscs);
19945a781ccbSVinicius Costa Gomes 	}
19955a781ccbSVinicius Costa Gomes 	q->qdiscs = NULL;
19965a781ccbSVinicius Costa Gomes 
19977c16680aSVinicius Costa Gomes 	netdev_reset_tc(dev);
19985a781ccbSVinicius Costa Gomes 
19999af23657SVladimir Oltean 	oper = rtnl_dereference(q->oper_sched);
20009af23657SVladimir Oltean 	admin = rtnl_dereference(q->admin_sched);
2001a3d43c0dSVinicius Costa Gomes 
20029af23657SVladimir Oltean 	if (oper)
20039af23657SVladimir Oltean 		call_rcu(&oper->rcu, taprio_free_sched_cb);
20049af23657SVladimir Oltean 
20059af23657SVladimir Oltean 	if (admin)
20069af23657SVladimir Oltean 		call_rcu(&admin->rcu, taprio_free_sched_cb);
20072f530df7SVladimir Oltean 
20082f530df7SVladimir Oltean 	taprio_cleanup_broken_mqprio(q);
20095a781ccbSVinicius Costa Gomes }
20105a781ccbSVinicius Costa Gomes 
20115a781ccbSVinicius Costa Gomes static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
20125a781ccbSVinicius Costa Gomes 		       struct netlink_ext_ack *extack)
20135a781ccbSVinicius Costa Gomes {
20145a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
20155a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
2016a3d43c0dSVinicius Costa Gomes 	int i;
20175a781ccbSVinicius Costa Gomes 
20185a781ccbSVinicius Costa Gomes 	spin_lock_init(&q->current_entry_lock);
20195a781ccbSVinicius Costa Gomes 
20205a781ccbSVinicius Costa Gomes 	hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
2021a3d43c0dSVinicius Costa Gomes 	q->advance_timer.function = advance_sched;
20225a781ccbSVinicius Costa Gomes 
20235a781ccbSVinicius Costa Gomes 	q->root = sch;
20245a781ccbSVinicius Costa Gomes 
20255a781ccbSVinicius Costa Gomes 	/* We only support static clockids. Use an invalid value as default
20265a781ccbSVinicius Costa Gomes 	 * and get the valid one on taprio_change().
20275a781ccbSVinicius Costa Gomes 	 */
20285a781ccbSVinicius Costa Gomes 	q->clockid = -1;
2029a9d62274SVinicius Costa Gomes 	q->flags = TAPRIO_FLAGS_INVALID;
20305a781ccbSVinicius Costa Gomes 
2031efb55222SVladimir Oltean 	list_add(&q->taprio_list, &taprio_list);
2032efb55222SVladimir Oltean 
2033026de64dSVladimir Oltean 	if (sch->parent != TC_H_ROOT) {
2034026de64dSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Can only be attached as root qdisc");
20355a781ccbSVinicius Costa Gomes 		return -EOPNOTSUPP;
2036026de64dSVladimir Oltean 	}
20375a781ccbSVinicius Costa Gomes 
2038026de64dSVladimir Oltean 	if (!netif_is_multiqueue(dev)) {
2039026de64dSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Multi-queue device is required");
20405a781ccbSVinicius Costa Gomes 		return -EOPNOTSUPP;
2041026de64dSVladimir Oltean 	}
20425a781ccbSVinicius Costa Gomes 
20435a781ccbSVinicius Costa Gomes 	/* pre-allocate qdisc, attachment can't fail */
20445a781ccbSVinicius Costa Gomes 	q->qdiscs = kcalloc(dev->num_tx_queues,
20455a781ccbSVinicius Costa Gomes 			    sizeof(q->qdiscs[0]),
20465a781ccbSVinicius Costa Gomes 			    GFP_KERNEL);
20475a781ccbSVinicius Costa Gomes 
20485a781ccbSVinicius Costa Gomes 	if (!q->qdiscs)
20495a781ccbSVinicius Costa Gomes 		return -ENOMEM;
20505a781ccbSVinicius Costa Gomes 
20515a781ccbSVinicius Costa Gomes 	if (!opt)
20525a781ccbSVinicius Costa Gomes 		return -EINVAL;
20535a781ccbSVinicius Costa Gomes 
2054a3d43c0dSVinicius Costa Gomes 	for (i = 0; i < dev->num_tx_queues; i++) {
2055a3d43c0dSVinicius Costa Gomes 		struct netdev_queue *dev_queue;
2056a3d43c0dSVinicius Costa Gomes 		struct Qdisc *qdisc;
2057a3d43c0dSVinicius Costa Gomes 
2058a3d43c0dSVinicius Costa Gomes 		dev_queue = netdev_get_tx_queue(dev, i);
2059a3d43c0dSVinicius Costa Gomes 		qdisc = qdisc_create_dflt(dev_queue,
2060a3d43c0dSVinicius Costa Gomes 					  &pfifo_qdisc_ops,
2061a3d43c0dSVinicius Costa Gomes 					  TC_H_MAKE(TC_H_MAJ(sch->handle),
2062a3d43c0dSVinicius Costa Gomes 						    TC_H_MIN(i + 1)),
2063a3d43c0dSVinicius Costa Gomes 					  extack);
2064a3d43c0dSVinicius Costa Gomes 		if (!qdisc)
2065a3d43c0dSVinicius Costa Gomes 			return -ENOMEM;
2066a3d43c0dSVinicius Costa Gomes 
2067a3d43c0dSVinicius Costa Gomes 		if (i < dev->real_num_tx_queues)
2068a3d43c0dSVinicius Costa Gomes 			qdisc_hash_add(qdisc, false);
2069a3d43c0dSVinicius Costa Gomes 
2070a3d43c0dSVinicius Costa Gomes 		q->qdiscs[i] = qdisc;
2071a3d43c0dSVinicius Costa Gomes 	}
2072a3d43c0dSVinicius Costa Gomes 
20732f530df7SVladimir Oltean 	taprio_detect_broken_mqprio(q);
20742f530df7SVladimir Oltean 
20755a781ccbSVinicius Costa Gomes 	return taprio_change(sch, opt, extack);
20765a781ccbSVinicius Costa Gomes }
20775a781ccbSVinicius Costa Gomes 
207813511704SYannick Vignon static void taprio_attach(struct Qdisc *sch)
207913511704SYannick Vignon {
208013511704SYannick Vignon 	struct taprio_sched *q = qdisc_priv(sch);
208113511704SYannick Vignon 	struct net_device *dev = qdisc_dev(sch);
208213511704SYannick Vignon 	unsigned int ntx;
208313511704SYannick Vignon 
208413511704SYannick Vignon 	/* Attach underlying qdisc */
208513511704SYannick Vignon 	for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
208613511704SYannick Vignon 		struct Qdisc *qdisc = q->qdiscs[ntx];
208713511704SYannick Vignon 		struct Qdisc *old;
208813511704SYannick Vignon 
208913511704SYannick Vignon 		if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
209013511704SYannick Vignon 			qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
209113511704SYannick Vignon 			old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
209213511704SYannick Vignon 		} else {
209313511704SYannick Vignon 			old = dev_graft_qdisc(qdisc->dev_queue, sch);
209413511704SYannick Vignon 			qdisc_refcount_inc(sch);
209513511704SYannick Vignon 		}
209613511704SYannick Vignon 		if (old)
209713511704SYannick Vignon 			qdisc_put(old);
209813511704SYannick Vignon 	}
209913511704SYannick Vignon 
210013511704SYannick Vignon 	/* access to the child qdiscs is not needed in offload mode */
210113511704SYannick Vignon 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
210213511704SYannick Vignon 		kfree(q->qdiscs);
210313511704SYannick Vignon 		q->qdiscs = NULL;
210413511704SYannick Vignon 	}
210513511704SYannick Vignon }
210613511704SYannick Vignon 
21075a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
21085a781ccbSVinicius Costa Gomes 					     unsigned long cl)
21095a781ccbSVinicius Costa Gomes {
21105a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
21115a781ccbSVinicius Costa Gomes 	unsigned long ntx = cl - 1;
21125a781ccbSVinicius Costa Gomes 
21135a781ccbSVinicius Costa Gomes 	if (ntx >= dev->num_tx_queues)
21145a781ccbSVinicius Costa Gomes 		return NULL;
21155a781ccbSVinicius Costa Gomes 
21165a781ccbSVinicius Costa Gomes 	return netdev_get_tx_queue(dev, ntx);
21175a781ccbSVinicius Costa Gomes }
21185a781ccbSVinicius Costa Gomes 
21195a781ccbSVinicius Costa Gomes static int taprio_graft(struct Qdisc *sch, unsigned long cl,
21205a781ccbSVinicius Costa Gomes 			struct Qdisc *new, struct Qdisc **old,
21215a781ccbSVinicius Costa Gomes 			struct netlink_ext_ack *extack)
21225a781ccbSVinicius Costa Gomes {
21235a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
21245a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
21255a781ccbSVinicius Costa Gomes 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
21265a781ccbSVinicius Costa Gomes 
21275a781ccbSVinicius Costa Gomes 	if (!dev_queue)
21285a781ccbSVinicius Costa Gomes 		return -EINVAL;
21295a781ccbSVinicius Costa Gomes 
21305a781ccbSVinicius Costa Gomes 	if (dev->flags & IFF_UP)
21315a781ccbSVinicius Costa Gomes 		dev_deactivate(dev);
21325a781ccbSVinicius Costa Gomes 
213313511704SYannick Vignon 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
213413511704SYannick Vignon 		*old = dev_graft_qdisc(dev_queue, new);
213513511704SYannick Vignon 	} else {
21365a781ccbSVinicius Costa Gomes 		*old = q->qdiscs[cl - 1];
21375a781ccbSVinicius Costa Gomes 		q->qdiscs[cl - 1] = new;
213813511704SYannick Vignon 	}
21395a781ccbSVinicius Costa Gomes 
21405a781ccbSVinicius Costa Gomes 	if (new)
21415a781ccbSVinicius Costa Gomes 		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
21425a781ccbSVinicius Costa Gomes 
21435a781ccbSVinicius Costa Gomes 	if (dev->flags & IFF_UP)
21445a781ccbSVinicius Costa Gomes 		dev_activate(dev);
21455a781ccbSVinicius Costa Gomes 
21465a781ccbSVinicius Costa Gomes 	return 0;
21475a781ccbSVinicius Costa Gomes }
21485a781ccbSVinicius Costa Gomes 
21495a781ccbSVinicius Costa Gomes static int dump_entry(struct sk_buff *msg,
21505a781ccbSVinicius Costa Gomes 		      const struct sched_entry *entry)
21515a781ccbSVinicius Costa Gomes {
21525a781ccbSVinicius Costa Gomes 	struct nlattr *item;
21535a781ccbSVinicius Costa Gomes 
2154ae0be8deSMichal Kubecek 	item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
21555a781ccbSVinicius Costa Gomes 	if (!item)
21565a781ccbSVinicius Costa Gomes 		return -ENOSPC;
21575a781ccbSVinicius Costa Gomes 
21585a781ccbSVinicius Costa Gomes 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
21595a781ccbSVinicius Costa Gomes 		goto nla_put_failure;
21605a781ccbSVinicius Costa Gomes 
21615a781ccbSVinicius Costa Gomes 	if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
21625a781ccbSVinicius Costa Gomes 		goto nla_put_failure;
21635a781ccbSVinicius Costa Gomes 
21645a781ccbSVinicius Costa Gomes 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
21655a781ccbSVinicius Costa Gomes 			entry->gate_mask))
21665a781ccbSVinicius Costa Gomes 		goto nla_put_failure;
21675a781ccbSVinicius Costa Gomes 
21685a781ccbSVinicius Costa Gomes 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
21695a781ccbSVinicius Costa Gomes 			entry->interval))
21705a781ccbSVinicius Costa Gomes 		goto nla_put_failure;
21715a781ccbSVinicius Costa Gomes 
21725a781ccbSVinicius Costa Gomes 	return nla_nest_end(msg, item);
21735a781ccbSVinicius Costa Gomes 
21745a781ccbSVinicius Costa Gomes nla_put_failure:
21755a781ccbSVinicius Costa Gomes 	nla_nest_cancel(msg, item);
21765a781ccbSVinicius Costa Gomes 	return -1;
21775a781ccbSVinicius Costa Gomes }
21785a781ccbSVinicius Costa Gomes 
2179a3d43c0dSVinicius Costa Gomes static int dump_schedule(struct sk_buff *msg,
2180a3d43c0dSVinicius Costa Gomes 			 const struct sched_gate_list *root)
2181a3d43c0dSVinicius Costa Gomes {
2182a3d43c0dSVinicius Costa Gomes 	struct nlattr *entry_list;
2183a3d43c0dSVinicius Costa Gomes 	struct sched_entry *entry;
2184a3d43c0dSVinicius Costa Gomes 
2185a3d43c0dSVinicius Costa Gomes 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
2186a3d43c0dSVinicius Costa Gomes 			root->base_time, TCA_TAPRIO_PAD))
2187a3d43c0dSVinicius Costa Gomes 		return -1;
2188a3d43c0dSVinicius Costa Gomes 
21896ca6a665SVinicius Costa Gomes 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
21906ca6a665SVinicius Costa Gomes 			root->cycle_time, TCA_TAPRIO_PAD))
21916ca6a665SVinicius Costa Gomes 		return -1;
21926ca6a665SVinicius Costa Gomes 
2193c25031e9SVinicius Costa Gomes 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
2194c25031e9SVinicius Costa Gomes 			root->cycle_time_extension, TCA_TAPRIO_PAD))
2195c25031e9SVinicius Costa Gomes 		return -1;
2196c25031e9SVinicius Costa Gomes 
2197a3d43c0dSVinicius Costa Gomes 	entry_list = nla_nest_start_noflag(msg,
2198a3d43c0dSVinicius Costa Gomes 					   TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
2199a3d43c0dSVinicius Costa Gomes 	if (!entry_list)
2200a3d43c0dSVinicius Costa Gomes 		goto error_nest;
2201a3d43c0dSVinicius Costa Gomes 
2202a3d43c0dSVinicius Costa Gomes 	list_for_each_entry(entry, &root->entries, list) {
2203a3d43c0dSVinicius Costa Gomes 		if (dump_entry(msg, entry) < 0)
2204a3d43c0dSVinicius Costa Gomes 			goto error_nest;
2205a3d43c0dSVinicius Costa Gomes 	}
2206a3d43c0dSVinicius Costa Gomes 
2207a3d43c0dSVinicius Costa Gomes 	nla_nest_end(msg, entry_list);
2208a3d43c0dSVinicius Costa Gomes 	return 0;
2209a3d43c0dSVinicius Costa Gomes 
2210a3d43c0dSVinicius Costa Gomes error_nest:
2211a3d43c0dSVinicius Costa Gomes 	nla_nest_cancel(msg, entry_list);
2212a3d43c0dSVinicius Costa Gomes 	return -1;
2213a3d43c0dSVinicius Costa Gomes }
2214a3d43c0dSVinicius Costa Gomes 
2215fed87cc6SVladimir Oltean static int taprio_dump_tc_entries(struct sk_buff *skb,
2216fed87cc6SVladimir Oltean 				  struct sched_gate_list *sched)
2217a54fc09eSVladimir Oltean {
2218a54fc09eSVladimir Oltean 	struct nlattr *n;
2219a54fc09eSVladimir Oltean 	int tc;
2220a54fc09eSVladimir Oltean 
2221a54fc09eSVladimir Oltean 	for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
2222a54fc09eSVladimir Oltean 		n = nla_nest_start(skb, TCA_TAPRIO_ATTR_TC_ENTRY);
2223a54fc09eSVladimir Oltean 		if (!n)
2224a54fc09eSVladimir Oltean 			return -EMSGSIZE;
2225a54fc09eSVladimir Oltean 
2226a54fc09eSVladimir Oltean 		if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_INDEX, tc))
2227a54fc09eSVladimir Oltean 			goto nla_put_failure;
2228a54fc09eSVladimir Oltean 
2229a54fc09eSVladimir Oltean 		if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_MAX_SDU,
2230fed87cc6SVladimir Oltean 				sched->max_sdu[tc]))
2231a54fc09eSVladimir Oltean 			goto nla_put_failure;
2232a54fc09eSVladimir Oltean 
2233a54fc09eSVladimir Oltean 		nla_nest_end(skb, n);
2234a54fc09eSVladimir Oltean 	}
2235a54fc09eSVladimir Oltean 
2236a54fc09eSVladimir Oltean 	return 0;
2237a54fc09eSVladimir Oltean 
2238a54fc09eSVladimir Oltean nla_put_failure:
2239a54fc09eSVladimir Oltean 	nla_nest_cancel(skb, n);
2240a54fc09eSVladimir Oltean 	return -EMSGSIZE;
2241a54fc09eSVladimir Oltean }
2242a54fc09eSVladimir Oltean 
22435a781ccbSVinicius Costa Gomes static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
22445a781ccbSVinicius Costa Gomes {
22455a781ccbSVinicius Costa Gomes 	struct taprio_sched *q = qdisc_priv(sch);
22465a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
2247a3d43c0dSVinicius Costa Gomes 	struct sched_gate_list *oper, *admin;
22485a781ccbSVinicius Costa Gomes 	struct tc_mqprio_qopt opt = { 0 };
2249a3d43c0dSVinicius Costa Gomes 	struct nlattr *nest, *sched_nest;
22505a781ccbSVinicius Costa Gomes 
225118cdd2f0SVladimir Oltean 	oper = rtnl_dereference(q->oper_sched);
225218cdd2f0SVladimir Oltean 	admin = rtnl_dereference(q->admin_sched);
2253a3d43c0dSVinicius Costa Gomes 
22549dd6ad67SVladimir Oltean 	mqprio_qopt_reconstruct(dev, &opt);
22555a781ccbSVinicius Costa Gomes 
2256ae0be8deSMichal Kubecek 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
22575a781ccbSVinicius Costa Gomes 	if (!nest)
2258a3d43c0dSVinicius Costa Gomes 		goto start_error;
22595a781ccbSVinicius Costa Gomes 
22605a781ccbSVinicius Costa Gomes 	if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
22615a781ccbSVinicius Costa Gomes 		goto options_error;
22625a781ccbSVinicius Costa Gomes 
22639c66d156SVinicius Costa Gomes 	if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
22649c66d156SVinicius Costa Gomes 	    nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
22655a781ccbSVinicius Costa Gomes 		goto options_error;
22665a781ccbSVinicius Costa Gomes 
22674cfd5779SVedang Patel 	if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
22684cfd5779SVedang Patel 		goto options_error;
22694cfd5779SVedang Patel 
22704cfd5779SVedang Patel 	if (q->txtime_delay &&
2271a5b64700SVedang Patel 	    nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
22724cfd5779SVedang Patel 		goto options_error;
22734cfd5779SVedang Patel 
2274fed87cc6SVladimir Oltean 	if (oper && taprio_dump_tc_entries(skb, oper))
2275a54fc09eSVladimir Oltean 		goto options_error;
2276a54fc09eSVladimir Oltean 
2277a3d43c0dSVinicius Costa Gomes 	if (oper && dump_schedule(skb, oper))
22785a781ccbSVinicius Costa Gomes 		goto options_error;
22795a781ccbSVinicius Costa Gomes 
2280a3d43c0dSVinicius Costa Gomes 	if (!admin)
2281a3d43c0dSVinicius Costa Gomes 		goto done;
22825a781ccbSVinicius Costa Gomes 
2283a3d43c0dSVinicius Costa Gomes 	sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
2284e4acf427SColin Ian King 	if (!sched_nest)
2285e4acf427SColin Ian King 		goto options_error;
2286a3d43c0dSVinicius Costa Gomes 
2287a3d43c0dSVinicius Costa Gomes 	if (dump_schedule(skb, admin))
2288a3d43c0dSVinicius Costa Gomes 		goto admin_error;
2289a3d43c0dSVinicius Costa Gomes 
2290a3d43c0dSVinicius Costa Gomes 	nla_nest_end(skb, sched_nest);
2291a3d43c0dSVinicius Costa Gomes 
2292a3d43c0dSVinicius Costa Gomes done:
22935a781ccbSVinicius Costa Gomes 	return nla_nest_end(skb, nest);
22945a781ccbSVinicius Costa Gomes 
2295a3d43c0dSVinicius Costa Gomes admin_error:
2296a3d43c0dSVinicius Costa Gomes 	nla_nest_cancel(skb, sched_nest);
2297a3d43c0dSVinicius Costa Gomes 
22985a781ccbSVinicius Costa Gomes options_error:
22995a781ccbSVinicius Costa Gomes 	nla_nest_cancel(skb, nest);
2300a3d43c0dSVinicius Costa Gomes 
2301a3d43c0dSVinicius Costa Gomes start_error:
2302a3d43c0dSVinicius Costa Gomes 	return -ENOSPC;
23035a781ccbSVinicius Costa Gomes }
23045a781ccbSVinicius Costa Gomes 
23055a781ccbSVinicius Costa Gomes static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
23065a781ccbSVinicius Costa Gomes {
2307af7b29b1SVladimir Oltean 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
23085a781ccbSVinicius Costa Gomes 
2309af7b29b1SVladimir Oltean 	if (!dev_queue)
23105a781ccbSVinicius Costa Gomes 		return NULL;
23115a781ccbSVinicius Costa Gomes 
2312af7b29b1SVladimir Oltean 	return dev_queue->qdisc_sleeping;
23135a781ccbSVinicius Costa Gomes }
23145a781ccbSVinicius Costa Gomes 
23155a781ccbSVinicius Costa Gomes static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
23165a781ccbSVinicius Costa Gomes {
23175a781ccbSVinicius Costa Gomes 	unsigned int ntx = TC_H_MIN(classid);
23185a781ccbSVinicius Costa Gomes 
23195a781ccbSVinicius Costa Gomes 	if (!taprio_queue_get(sch, ntx))
23205a781ccbSVinicius Costa Gomes 		return 0;
23215a781ccbSVinicius Costa Gomes 	return ntx;
23225a781ccbSVinicius Costa Gomes }
23235a781ccbSVinicius Costa Gomes 
23245a781ccbSVinicius Costa Gomes static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
23255a781ccbSVinicius Costa Gomes 			     struct sk_buff *skb, struct tcmsg *tcm)
23265a781ccbSVinicius Costa Gomes {
23275a781ccbSVinicius Costa Gomes 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
23285a781ccbSVinicius Costa Gomes 
23295a781ccbSVinicius Costa Gomes 	tcm->tcm_parent = TC_H_ROOT;
23305a781ccbSVinicius Costa Gomes 	tcm->tcm_handle |= TC_H_MIN(cl);
23315a781ccbSVinicius Costa Gomes 	tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
23325a781ccbSVinicius Costa Gomes 
23335a781ccbSVinicius Costa Gomes 	return 0;
23345a781ccbSVinicius Costa Gomes }
23355a781ccbSVinicius Costa Gomes 
23365a781ccbSVinicius Costa Gomes static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
23375a781ccbSVinicius Costa Gomes 				   struct gnet_dump *d)
23385a781ccbSVinicius Costa Gomes 	__releases(d->lock)
23395a781ccbSVinicius Costa Gomes 	__acquires(d->lock)
23405a781ccbSVinicius Costa Gomes {
23415a781ccbSVinicius Costa Gomes 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
23425a781ccbSVinicius Costa Gomes 
23435a781ccbSVinicius Costa Gomes 	sch = dev_queue->qdisc_sleeping;
234429cbcd85SAhmed S. Darwish 	if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 ||
23455dd431b6SPaolo Abeni 	    qdisc_qstats_copy(d, sch) < 0)
23465a781ccbSVinicius Costa Gomes 		return -1;
23475a781ccbSVinicius Costa Gomes 	return 0;
23485a781ccbSVinicius Costa Gomes }
23495a781ccbSVinicius Costa Gomes 
23505a781ccbSVinicius Costa Gomes static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
23515a781ccbSVinicius Costa Gomes {
23525a781ccbSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
23535a781ccbSVinicius Costa Gomes 	unsigned long ntx;
23545a781ccbSVinicius Costa Gomes 
23555a781ccbSVinicius Costa Gomes 	if (arg->stop)
23565a781ccbSVinicius Costa Gomes 		return;
23575a781ccbSVinicius Costa Gomes 
23585a781ccbSVinicius Costa Gomes 	arg->count = arg->skip;
23595a781ccbSVinicius Costa Gomes 	for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2360e046fa89SZhengchao Shao 		if (!tc_qdisc_stats_dump(sch, ntx + 1, arg))
23615a781ccbSVinicius Costa Gomes 			break;
23625a781ccbSVinicius Costa Gomes 	}
23635a781ccbSVinicius Costa Gomes }
23645a781ccbSVinicius Costa Gomes 
23655a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
23665a781ccbSVinicius Costa Gomes 						struct tcmsg *tcm)
23675a781ccbSVinicius Costa Gomes {
23685a781ccbSVinicius Costa Gomes 	return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
23695a781ccbSVinicius Costa Gomes }
23705a781ccbSVinicius Costa Gomes 
23715a781ccbSVinicius Costa Gomes static const struct Qdisc_class_ops taprio_class_ops = {
23725a781ccbSVinicius Costa Gomes 	.graft		= taprio_graft,
23735a781ccbSVinicius Costa Gomes 	.leaf		= taprio_leaf,
23745a781ccbSVinicius Costa Gomes 	.find		= taprio_find,
23755a781ccbSVinicius Costa Gomes 	.walk		= taprio_walk,
23765a781ccbSVinicius Costa Gomes 	.dump		= taprio_dump_class,
23775a781ccbSVinicius Costa Gomes 	.dump_stats	= taprio_dump_class_stats,
23785a781ccbSVinicius Costa Gomes 	.select_queue	= taprio_select_queue,
23795a781ccbSVinicius Costa Gomes };
23805a781ccbSVinicius Costa Gomes 
23815a781ccbSVinicius Costa Gomes static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
23825a781ccbSVinicius Costa Gomes 	.cl_ops		= &taprio_class_ops,
23835a781ccbSVinicius Costa Gomes 	.id		= "taprio",
23845a781ccbSVinicius Costa Gomes 	.priv_size	= sizeof(struct taprio_sched),
23855a781ccbSVinicius Costa Gomes 	.init		= taprio_init,
2386a3d43c0dSVinicius Costa Gomes 	.change		= taprio_change,
23875a781ccbSVinicius Costa Gomes 	.destroy	= taprio_destroy,
238844d4775cSDavide Caratti 	.reset		= taprio_reset,
238913511704SYannick Vignon 	.attach		= taprio_attach,
23905a781ccbSVinicius Costa Gomes 	.peek		= taprio_peek,
23915a781ccbSVinicius Costa Gomes 	.dequeue	= taprio_dequeue,
23925a781ccbSVinicius Costa Gomes 	.enqueue	= taprio_enqueue,
23935a781ccbSVinicius Costa Gomes 	.dump		= taprio_dump,
23945a781ccbSVinicius Costa Gomes 	.owner		= THIS_MODULE,
23955a781ccbSVinicius Costa Gomes };
23965a781ccbSVinicius Costa Gomes 
23977b9eba7bSLeandro Dorileo static struct notifier_block taprio_device_notifier = {
23987b9eba7bSLeandro Dorileo 	.notifier_call = taprio_dev_notifier,
23997b9eba7bSLeandro Dorileo };
24007b9eba7bSLeandro Dorileo 
24015a781ccbSVinicius Costa Gomes static int __init taprio_module_init(void)
24025a781ccbSVinicius Costa Gomes {
24037b9eba7bSLeandro Dorileo 	int err = register_netdevice_notifier(&taprio_device_notifier);
24047b9eba7bSLeandro Dorileo 
24057b9eba7bSLeandro Dorileo 	if (err)
24067b9eba7bSLeandro Dorileo 		return err;
24077b9eba7bSLeandro Dorileo 
24085a781ccbSVinicius Costa Gomes 	return register_qdisc(&taprio_qdisc_ops);
24095a781ccbSVinicius Costa Gomes }
24105a781ccbSVinicius Costa Gomes 
24115a781ccbSVinicius Costa Gomes static void __exit taprio_module_exit(void)
24125a781ccbSVinicius Costa Gomes {
24135a781ccbSVinicius Costa Gomes 	unregister_qdisc(&taprio_qdisc_ops);
24147b9eba7bSLeandro Dorileo 	unregister_netdevice_notifier(&taprio_device_notifier);
24155a781ccbSVinicius Costa Gomes }
24165a781ccbSVinicius Costa Gomes 
24175a781ccbSVinicius Costa Gomes module_init(taprio_module_init);
24185a781ccbSVinicius Costa Gomes module_exit(taprio_module_exit);
24195a781ccbSVinicius Costa Gomes MODULE_LICENSE("GPL");
2420