xref: /linux/net/sched/sch_taprio.c (revision 2bc124a5f8df56aeabd444cded132407d91d9876)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* net/sched/sch_taprio.c	 Time Aware Priority Scheduler
4  *
5  * Authors:	Vinicius Costa Gomes <vinicius.gomes@intel.com>
6  *
7  */
8 
9 #include <linux/ethtool.h>
10 #include <linux/ethtool_netlink.h>
11 #include <linux/types.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/skbuff.h>
18 #include <linux/math64.h>
19 #include <linux/module.h>
20 #include <linux/spinlock.h>
21 #include <linux/rcupdate.h>
22 #include <linux/time.h>
23 #include <net/gso.h>
24 #include <net/netlink.h>
25 #include <net/pkt_sched.h>
26 #include <net/pkt_cls.h>
27 #include <net/sch_generic.h>
28 #include <net/sock.h>
29 #include <net/tcp.h>
30 
31 #define TAPRIO_STAT_NOT_SET	(~0ULL)
32 
33 #include "sch_mqprio_lib.h"
34 
35 static LIST_HEAD(taprio_list);
36 static struct static_key_false taprio_have_broken_mqprio;
37 static struct static_key_false taprio_have_working_mqprio;
38 
39 #define TAPRIO_ALL_GATES_OPEN -1
40 
41 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
42 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
43 #define TAPRIO_SUPPORTED_FLAGS \
44 	(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST | TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
45 #define TAPRIO_FLAGS_INVALID U32_MAX
46 /* Minimum value for picos_per_byte to ensure non-zero duration
47  * for minimum-sized Ethernet frames (ETH_ZLEN = 60).
48  * 60 * 17 > PSEC_PER_NSEC (1000)
49  */
50 #define TAPRIO_PICOS_PER_BYTE_MIN 17
51 
52 /* Each entry is a separate 368-byte sched_entry and, in full offload, is
53  * also composed into a per-entry offload array. The u16 nla_len admits
54  * roughly 5460 minimal 12-byte entries, pinning ~2 MiB; 1024 matches the
55  * SJA1105 schedule-table capacity (SJA1110 provides 4096) and the act_gate cap.
56  */
57 #define TAPRIO_ENTRIES_MAX	1024U
58 
59 struct sched_entry {
60 	/* Durations between this GCL entry and the GCL entry where the
61 	 * respective traffic class gate closes
62 	 */
63 	u64 gate_duration[TC_MAX_QUEUE];
64 	atomic_t budget[TC_MAX_QUEUE];
65 	/* The qdisc makes some effort so that no packet leaves
66 	 * after this time
67 	 */
68 	ktime_t gate_close_time[TC_MAX_QUEUE];
69 	struct list_head list;
70 	/* Used to calculate when to advance the schedule */
71 	ktime_t end_time;
72 	ktime_t next_txtime;
73 	int index;
74 	u32 gate_mask;
75 	u32 interval;
76 	u8 command;
77 };
78 
79 struct sched_gate_list {
80 	/* Longest non-zero contiguous gate durations per traffic class,
81 	 * or 0 if a traffic class gate never opens during the schedule.
82 	 */
83 	u64 max_open_gate_duration[TC_MAX_QUEUE];
84 	u32 max_frm_len[TC_MAX_QUEUE]; /* for the fast path */
85 	u32 max_sdu[TC_MAX_QUEUE]; /* for dump */
86 	struct rcu_head rcu;
87 	struct list_head entries;
88 	size_t num_entries;
89 	ktime_t cycle_end_time;
90 	s64 cycle_time;
91 	s64 cycle_time_extension;
92 	s64 base_time;
93 };
94 
95 struct taprio_sched {
96 	struct Qdisc **qdiscs;
97 	struct Qdisc *root;
98 	u32 flags;
99 	enum tk_offsets tk_offset;
100 	int clockid;
101 	bool offloaded;
102 	bool detected_mqprio;
103 	bool broken_mqprio;
104 	atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
105 				    * speeds it's sub-nanoseconds per byte
106 				    */
107 
108 	/* Protects the update side of the RCU protected current_entry */
109 	spinlock_t current_entry_lock;
110 	struct sched_entry __rcu *current_entry;
111 	struct sched_gate_list __rcu *oper_sched;
112 	struct sched_gate_list __rcu *admin_sched;
113 	struct hrtimer advance_timer;
114 	struct list_head taprio_list;
115 	int cur_txq[TC_MAX_QUEUE];
116 	u32 max_sdu[TC_MAX_QUEUE]; /* save info from the user */
117 	u32 fp[TC_QOPT_MAX_QUEUE]; /* only for dump and offloading */
118 	u32 txtime_delay;
119 };
120 
121 struct __tc_taprio_qopt_offload {
122 	refcount_t users;
123 	struct tc_taprio_qopt_offload offload;
124 };
125 
126 static void taprio_calculate_gate_durations(struct taprio_sched *q,
127 					    struct sched_gate_list *sched)
128 {
129 	struct net_device *dev = qdisc_dev(q->root);
130 	int num_tc = netdev_get_num_tc(dev);
131 	struct sched_entry *entry, *cur;
132 	int tc;
133 
134 	list_for_each_entry(entry, &sched->entries, list) {
135 		u32 gates_still_open = entry->gate_mask;
136 
137 		/* For each traffic class, calculate each open gate duration,
138 		 * starting at this schedule entry and ending at the schedule
139 		 * entry containing a gate close event for that TC.
140 		 */
141 		cur = entry;
142 
143 		do {
144 			if (!gates_still_open)
145 				break;
146 
147 			for (tc = 0; tc < num_tc; tc++) {
148 				if (!(gates_still_open & BIT(tc)))
149 					continue;
150 
151 				if (cur->gate_mask & BIT(tc))
152 					entry->gate_duration[tc] += cur->interval;
153 				else
154 					gates_still_open &= ~BIT(tc);
155 			}
156 
157 			cur = list_next_entry_circular(cur, &sched->entries, list);
158 		} while (cur != entry);
159 
160 		/* Keep track of the maximum gate duration for each traffic
161 		 * class, taking care to not confuse a traffic class which is
162 		 * temporarily closed with one that is always closed.
163 		 */
164 		for (tc = 0; tc < num_tc; tc++)
165 			if (entry->gate_duration[tc] &&
166 			    sched->max_open_gate_duration[tc] < entry->gate_duration[tc])
167 				sched->max_open_gate_duration[tc] = entry->gate_duration[tc];
168 	}
169 }
170 
171 static bool taprio_entry_allows_tx(ktime_t skb_end_time,
172 				   struct sched_entry *entry, int tc)
173 {
174 	return ktime_before(skb_end_time, entry->gate_close_time[tc]);
175 }
176 
177 static ktime_t sched_base_time(const struct sched_gate_list *sched)
178 {
179 	if (!sched)
180 		return KTIME_MAX;
181 
182 	return ns_to_ktime(sched->base_time);
183 }
184 
185 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
186 {
187 	/* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
188 	enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
189 
190 	switch (tk_offset) {
191 	case TK_OFFS_MAX:
192 		return mono;
193 	default:
194 		return ktime_mono_to_any(mono, tk_offset);
195 	}
196 }
197 
198 static ktime_t taprio_get_time(const struct taprio_sched *q)
199 {
200 	return taprio_mono_to_any(q, ktime_get());
201 }
202 
203 static void taprio_free_sched_cb(struct rcu_head *head)
204 {
205 	struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
206 	struct sched_entry *entry, *n;
207 
208 	list_for_each_entry_safe(entry, n, &sched->entries, list) {
209 		list_del(&entry->list);
210 		kfree(entry);
211 	}
212 
213 	kfree(sched);
214 }
215 
216 static void switch_schedules(struct taprio_sched *q,
217 			     struct sched_gate_list **admin,
218 			     struct sched_gate_list **oper)
219 {
220 	rcu_assign_pointer(q->oper_sched, *admin);
221 	rcu_assign_pointer(q->admin_sched, NULL);
222 
223 	if (*oper)
224 		call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
225 
226 	*oper = *admin;
227 	*admin = NULL;
228 }
229 
230 /* Get how much time has been already elapsed in the current cycle. */
231 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
232 {
233 	ktime_t time_since_sched_start;
234 	s32 time_elapsed;
235 
236 	time_since_sched_start = ktime_sub(time, sched->base_time);
237 	div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
238 
239 	return time_elapsed;
240 }
241 
242 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
243 				     struct sched_gate_list *admin,
244 				     struct sched_entry *entry,
245 				     ktime_t intv_start)
246 {
247 	s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
248 	ktime_t intv_end, cycle_ext_end, cycle_end;
249 
250 	cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
251 	intv_end = ktime_add_ns(intv_start, entry->interval);
252 	cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
253 
254 	if (ktime_before(intv_end, cycle_end))
255 		return intv_end;
256 	else if (admin && admin != sched &&
257 		 ktime_after(admin->base_time, cycle_end) &&
258 		 ktime_before(admin->base_time, cycle_ext_end))
259 		return admin->base_time;
260 	else
261 		return cycle_end;
262 }
263 
264 static int length_to_duration(struct taprio_sched *q, int len)
265 {
266 	return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC);
267 }
268 
269 static int duration_to_length(struct taprio_sched *q, u64 duration)
270 {
271 	return div_u64(duration * PSEC_PER_NSEC, atomic64_read(&q->picos_per_byte));
272 }
273 
274 /* Sets sched->max_sdu[] and sched->max_frm_len[] to the minimum between the
275  * q->max_sdu[] requested by the user and the max_sdu dynamically determined by
276  * the maximum open gate durations at the given link speed.
277  */
278 static void taprio_update_queue_max_sdu(struct taprio_sched *q,
279 					struct sched_gate_list *sched,
280 					struct qdisc_size_table *stab)
281 {
282 	struct net_device *dev = qdisc_dev(q->root);
283 	int num_tc = netdev_get_num_tc(dev);
284 	u32 max_sdu_from_user;
285 	u32 max_sdu_dynamic;
286 	u32 max_sdu;
287 	int tc;
288 
289 	for (tc = 0; tc < num_tc; tc++) {
290 		max_sdu_from_user = q->max_sdu[tc] ?: U32_MAX;
291 
292 		/* TC gate never closes => keep the queueMaxSDU
293 		 * selected by the user
294 		 */
295 		if (sched->max_open_gate_duration[tc] == sched->cycle_time) {
296 			max_sdu_dynamic = U32_MAX;
297 		} else {
298 			u32 max_frm_len;
299 
300 			max_frm_len = duration_to_length(q, sched->max_open_gate_duration[tc]);
301 			/* Compensate for L1 overhead from size table,
302 			 * but don't let the frame size go negative
303 			 */
304 			if (stab) {
305 				max_frm_len -= stab->szopts.overhead;
306 				max_frm_len = max_t(int, max_frm_len,
307 						    dev->hard_header_len + 1);
308 			}
309 			max_sdu_dynamic = max_frm_len - dev->hard_header_len;
310 			if (max_sdu_dynamic > dev->max_mtu)
311 				max_sdu_dynamic = U32_MAX;
312 		}
313 
314 		max_sdu = min(max_sdu_dynamic, max_sdu_from_user);
315 
316 		if (max_sdu != U32_MAX) {
317 			sched->max_frm_len[tc] = max_sdu + dev->hard_header_len;
318 			WRITE_ONCE(sched->max_sdu[tc], max_sdu);
319 		} else {
320 			sched->max_frm_len[tc] = U32_MAX; /* never oversized */
321 			WRITE_ONCE(sched->max_sdu[tc], 0);
322 		}
323 	}
324 }
325 
326 /* Returns the entry corresponding to next available interval. If
327  * validate_interval is set, it only validates whether the timestamp occurs
328  * when the gate corresponding to the skb's traffic class is open.
329  */
330 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
331 						  struct Qdisc *sch,
332 						  struct sched_gate_list *sched,
333 						  struct sched_gate_list *admin,
334 						  ktime_t time,
335 						  ktime_t *interval_start,
336 						  ktime_t *interval_end,
337 						  bool validate_interval)
338 {
339 	ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
340 	ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
341 	struct sched_entry *entry = NULL, *entry_found = NULL;
342 	struct taprio_sched *q = qdisc_priv(sch);
343 	struct net_device *dev = qdisc_dev(sch);
344 	bool entry_available = false;
345 	s32 cycle_elapsed;
346 	int tc, n;
347 
348 	tc = netdev_get_prio_tc_map(dev, skb->priority);
349 	packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
350 
351 	*interval_start = 0;
352 	*interval_end = 0;
353 
354 	if (!sched)
355 		return NULL;
356 
357 	cycle = sched->cycle_time;
358 	cycle_elapsed = get_cycle_time_elapsed(sched, time);
359 	curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
360 	cycle_end = ktime_add_ns(curr_intv_end, cycle);
361 
362 	list_for_each_entry(entry, &sched->entries, list) {
363 		curr_intv_start = curr_intv_end;
364 		curr_intv_end = get_interval_end_time(sched, admin, entry,
365 						      curr_intv_start);
366 
367 		if (ktime_after(curr_intv_start, cycle_end))
368 			break;
369 
370 		if (!(entry->gate_mask & BIT(tc)) ||
371 		    packet_transmit_time > entry->interval)
372 			continue;
373 
374 		txtime = entry->next_txtime;
375 
376 		if (ktime_before(txtime, time) || validate_interval) {
377 			transmit_end_time = ktime_add_ns(time, packet_transmit_time);
378 			if ((ktime_before(curr_intv_start, time) &&
379 			     ktime_before(transmit_end_time, curr_intv_end)) ||
380 			    (ktime_after(curr_intv_start, time) && !validate_interval)) {
381 				entry_found = entry;
382 				*interval_start = curr_intv_start;
383 				*interval_end = curr_intv_end;
384 				break;
385 			} else if (!entry_available && !validate_interval) {
386 				/* Here, we are just trying to find out the
387 				 * first available interval in the next cycle.
388 				 */
389 				entry_available = true;
390 				entry_found = entry;
391 				*interval_start = ktime_add_ns(curr_intv_start, cycle);
392 				*interval_end = ktime_add_ns(curr_intv_end, cycle);
393 			}
394 		} else if (ktime_before(txtime, earliest_txtime) &&
395 			   !entry_available) {
396 			earliest_txtime = txtime;
397 			entry_found = entry;
398 			n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
399 			*interval_start = ktime_add(curr_intv_start, n * cycle);
400 			*interval_end = ktime_add(curr_intv_end, n * cycle);
401 		}
402 	}
403 
404 	return entry_found;
405 }
406 
407 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
408 {
409 	struct taprio_sched *q = qdisc_priv(sch);
410 	struct sched_gate_list *sched, *admin;
411 	ktime_t interval_start, interval_end;
412 	struct sched_entry *entry;
413 
414 	rcu_read_lock();
415 	sched = rcu_dereference(q->oper_sched);
416 	admin = rcu_dereference(q->admin_sched);
417 
418 	entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
419 				       &interval_start, &interval_end, true);
420 	rcu_read_unlock();
421 
422 	return entry;
423 }
424 
425 /* This returns the tstamp value set by TCP in terms of the set clock. */
426 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
427 {
428 	unsigned int offset = skb_network_offset(skb);
429 	const struct ipv6hdr *ipv6h;
430 	const struct iphdr *iph;
431 	struct ipv6hdr _ipv6h;
432 
433 	ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
434 	if (!ipv6h)
435 		return 0;
436 
437 	if (ipv6h->version == 4) {
438 		iph = (struct iphdr *)ipv6h;
439 		offset += iph->ihl * 4;
440 
441 		/* special-case 6in4 tunnelling, as that is a common way to get
442 		 * v6 connectivity in the home
443 		 */
444 		if (iph->protocol == IPPROTO_IPV6) {
445 			ipv6h = skb_header_pointer(skb, offset,
446 						   sizeof(_ipv6h), &_ipv6h);
447 
448 			if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
449 				return 0;
450 		} else if (iph->protocol != IPPROTO_TCP) {
451 			return 0;
452 		}
453 	} else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
454 		return 0;
455 	}
456 
457 	return taprio_mono_to_any(q, skb->skb_mstamp_ns);
458 }
459 
460 /* There are a few scenarios where we will have to modify the txtime from
461  * what is read from next_txtime in sched_entry. They are:
462  * 1. If txtime is in the past,
463  *    a. The gate for the traffic class is currently open and packet can be
464  *       transmitted before it closes, schedule the packet right away.
465  *    b. If the gate corresponding to the traffic class is going to open later
466  *       in the cycle, set the txtime of packet to the interval start.
467  * 2. If txtime is in the future, there are packets corresponding to the
468  *    current traffic class waiting to be transmitted. So, the following
469  *    possibilities exist:
470  *    a. We can transmit the packet before the window containing the txtime
471  *       closes.
472  *    b. The window might close before the transmission can be completed
473  *       successfully. So, schedule the packet in the next open window.
474  */
475 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
476 {
477 	ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
478 	struct taprio_sched *q = qdisc_priv(sch);
479 	struct sched_gate_list *sched, *admin;
480 	ktime_t minimum_time, now, txtime;
481 	int len, packet_transmit_time;
482 	struct sched_entry *entry;
483 	bool sched_changed;
484 
485 	now = taprio_get_time(q);
486 	minimum_time = ktime_add_ns(now, q->txtime_delay);
487 
488 	tcp_tstamp = get_tcp_tstamp(q, skb);
489 	minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
490 
491 	rcu_read_lock();
492 	admin = rcu_dereference(q->admin_sched);
493 	sched = rcu_dereference(q->oper_sched);
494 	if (admin && ktime_after(minimum_time, admin->base_time))
495 		switch_schedules(q, &admin, &sched);
496 
497 	/* Until the schedule starts, all the queues are open */
498 	if (!sched || ktime_before(minimum_time, sched->base_time)) {
499 		txtime = minimum_time;
500 		goto done;
501 	}
502 
503 	len = qdisc_pkt_len(skb);
504 	packet_transmit_time = length_to_duration(q, len);
505 
506 	do {
507 		sched_changed = false;
508 
509 		entry = find_entry_to_transmit(skb, sch, sched, admin,
510 					       minimum_time,
511 					       &interval_start, &interval_end,
512 					       false);
513 		if (!entry) {
514 			txtime = 0;
515 			goto done;
516 		}
517 
518 		txtime = entry->next_txtime;
519 		txtime = max_t(ktime_t, txtime, minimum_time);
520 		txtime = max_t(ktime_t, txtime, interval_start);
521 
522 		if (admin && admin != sched &&
523 		    ktime_after(txtime, admin->base_time)) {
524 			sched = admin;
525 			sched_changed = true;
526 			continue;
527 		}
528 
529 		transmit_end_time = ktime_add(txtime, packet_transmit_time);
530 		minimum_time = transmit_end_time;
531 
532 		/* Update the txtime of current entry to the next time it's
533 		 * interval starts.
534 		 */
535 		if (ktime_after(transmit_end_time, interval_end))
536 			entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
537 	} while (sched_changed || ktime_after(transmit_end_time, interval_end));
538 
539 	entry->next_txtime = transmit_end_time;
540 
541 done:
542 	rcu_read_unlock();
543 	return txtime;
544 }
545 
546 /* Devices with full offload are expected to honor this in hardware */
547 static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch,
548 					     struct sk_buff *skb)
549 {
550 	struct taprio_sched *q = qdisc_priv(sch);
551 	struct net_device *dev = qdisc_dev(sch);
552 	struct sched_gate_list *sched;
553 	int prio = skb->priority;
554 	bool exceeds = false;
555 	u8 tc;
556 
557 	tc = netdev_get_prio_tc_map(dev, prio);
558 
559 	rcu_read_lock();
560 	sched = rcu_dereference(q->oper_sched);
561 	if (sched && skb->len > sched->max_frm_len[tc])
562 		exceeds = true;
563 	rcu_read_unlock();
564 
565 	return exceeds;
566 }
567 
568 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
569 			      struct Qdisc *child, struct sk_buff **to_free)
570 {
571 	struct taprio_sched *q = qdisc_priv(sch);
572 
573 	/* sk_flags are only safe to use on full sockets. */
574 	if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
575 		if (!is_valid_interval(skb, sch))
576 			return qdisc_drop(skb, sch, to_free);
577 	} else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
578 		skb->tstamp = get_packet_txtime(skb, sch);
579 		if (!skb->tstamp)
580 			return qdisc_drop(skb, sch, to_free);
581 	}
582 
583 	qdisc_qstats_backlog_inc(sch, skb);
584 	qdisc_qlen_inc(sch);
585 
586 	return qdisc_enqueue(skb, child, to_free);
587 }
588 
589 static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch,
590 				    struct Qdisc *child,
591 				    struct sk_buff **to_free)
592 {
593 	unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
594 	netdev_features_t features = netif_skb_features(skb);
595 	struct sk_buff *segs, *nskb;
596 	int ret;
597 
598 	segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
599 	if (IS_ERR_OR_NULL(segs))
600 		return qdisc_drop(skb, sch, to_free);
601 
602 	skb_list_walk_safe(segs, segs, nskb) {
603 		skb_mark_not_on_list(segs);
604 		qdisc_skb_cb(segs)->pkt_len = segs->len;
605 		qdisc_skb_cb(segs)->pkt_segs = 1;
606 		slen += segs->len;
607 
608 		/* FIXME: we should be segmenting to a smaller size
609 		 * rather than dropping these
610 		 */
611 		if (taprio_skb_exceeds_queue_max_sdu(sch, segs))
612 			ret = qdisc_drop(segs, sch, to_free);
613 		else
614 			ret = taprio_enqueue_one(segs, sch, child, to_free);
615 
616 		if (ret != NET_XMIT_SUCCESS) {
617 			if (net_xmit_drop_count(ret))
618 				qdisc_qstats_drop(sch);
619 		} else {
620 			numsegs++;
621 		}
622 	}
623 
624 	if (numsegs > 1)
625 		qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
626 	consume_skb(skb);
627 
628 	return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
629 }
630 
631 /* Will not be called in the full offload case, since the TX queues are
632  * attached to the Qdisc created using qdisc_create_dflt()
633  */
634 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
635 			  struct sk_buff **to_free)
636 {
637 	struct taprio_sched *q = qdisc_priv(sch);
638 	struct Qdisc *child;
639 	int queue;
640 
641 	queue = skb_get_queue_mapping(skb);
642 
643 	child = q->qdiscs[queue];
644 	if (unlikely(child == &noop_qdisc))
645 		return qdisc_drop(skb, sch, to_free);
646 
647 	if (taprio_skb_exceeds_queue_max_sdu(sch, skb)) {
648 		/* Large packets might not be transmitted when the transmission
649 		 * duration exceeds any configured interval. Therefore, segment
650 		 * the skb into smaller chunks. Drivers with full offload are
651 		 * expected to handle this in hardware.
652 		 */
653 		if (skb_is_gso(skb))
654 			return taprio_enqueue_segmented(skb, sch, child,
655 							to_free);
656 
657 		return qdisc_drop(skb, sch, to_free);
658 	}
659 
660 	return taprio_enqueue_one(skb, sch, child, to_free);
661 }
662 
663 static struct sk_buff *taprio_peek(struct Qdisc *sch)
664 {
665 	WARN_ONCE(1, "taprio only supports operating as root qdisc, peek() not implemented");
666 	return NULL;
667 }
668 
669 static void taprio_set_budgets(struct taprio_sched *q,
670 			       struct sched_gate_list *sched,
671 			       struct sched_entry *entry)
672 {
673 	struct net_device *dev = qdisc_dev(q->root);
674 	int num_tc = netdev_get_num_tc(dev);
675 	int tc, budget;
676 
677 	for (tc = 0; tc < num_tc; tc++) {
678 		/* Traffic classes which never close have infinite budget */
679 		if (entry->gate_duration[tc] == sched->cycle_time)
680 			budget = INT_MAX;
681 		else
682 			budget = div64_u64((u64)entry->gate_duration[tc] * PSEC_PER_NSEC,
683 					   atomic64_read(&q->picos_per_byte));
684 
685 		atomic_set(&entry->budget[tc], budget);
686 	}
687 }
688 
689 /* When an skb is sent, it consumes from the budget of all traffic classes */
690 static int taprio_update_budgets(struct sched_entry *entry, size_t len,
691 				 int tc_consumed, int num_tc)
692 {
693 	int tc, budget, new_budget = 0;
694 
695 	for (tc = 0; tc < num_tc; tc++) {
696 		budget = atomic_read(&entry->budget[tc]);
697 		/* Don't consume from infinite budget */
698 		if (budget == INT_MAX) {
699 			if (tc == tc_consumed)
700 				new_budget = budget;
701 			continue;
702 		}
703 
704 		if (tc == tc_consumed)
705 			new_budget = atomic_sub_return(len, &entry->budget[tc]);
706 		else
707 			atomic_sub(len, &entry->budget[tc]);
708 	}
709 
710 	return new_budget;
711 }
712 
713 static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq,
714 					       struct sched_entry *entry,
715 					       u32 gate_mask)
716 {
717 	struct taprio_sched *q = qdisc_priv(sch);
718 	struct net_device *dev = qdisc_dev(sch);
719 	struct Qdisc *child = q->qdiscs[txq];
720 	int num_tc = netdev_get_num_tc(dev);
721 	struct sk_buff *skb;
722 	ktime_t guard;
723 	int prio;
724 	int len;
725 	u8 tc;
726 
727 	if (unlikely(child == &noop_qdisc))
728 		return NULL;
729 
730 	if (TXTIME_ASSIST_IS_ENABLED(q->flags))
731 		goto skip_peek_checks;
732 
733 	skb = child->ops->peek(child);
734 	if (!skb)
735 		return NULL;
736 
737 	prio = skb->priority;
738 	tc = netdev_get_prio_tc_map(dev, prio);
739 
740 	if (!(gate_mask & BIT(tc)))
741 		return NULL;
742 
743 	len = qdisc_pkt_len(skb);
744 	guard = ktime_add_ns(taprio_get_time(q), length_to_duration(q, len));
745 
746 	/* In the case that there's no gate entry, there's no
747 	 * guard band ...
748 	 */
749 	if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
750 	    !taprio_entry_allows_tx(guard, entry, tc))
751 		return NULL;
752 
753 	/* ... and no budget. */
754 	if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
755 	    taprio_update_budgets(entry, len, tc, num_tc) < 0)
756 		return NULL;
757 
758 skip_peek_checks:
759 	skb = qdisc_dequeue_peeked(child);
760 	if (unlikely(!skb))
761 		return NULL;
762 
763 	qdisc_bstats_update(sch, skb);
764 	qdisc_qstats_backlog_dec(sch, skb);
765 	qdisc_qlen_dec(sch);
766 
767 	return skb;
768 }
769 
770 static void taprio_next_tc_txq(struct net_device *dev, int tc, int *txq)
771 {
772 	struct netdev_tc_txq res;
773 
774 	res.combined = READ_ONCE(dev->tc_to_txq[tc].combined);
775 
776 	(*txq)++;
777 	if (*txq == res.offset + res.count)
778 		*txq = res.offset;
779 }
780 
781 /* Prioritize higher traffic classes, and select among TXQs belonging to the
782  * same TC using round robin
783  */
784 static struct sk_buff *taprio_dequeue_tc_priority(struct Qdisc *sch,
785 						  struct sched_entry *entry,
786 						  u32 gate_mask)
787 {
788 	struct taprio_sched *q = qdisc_priv(sch);
789 	struct net_device *dev = qdisc_dev(sch);
790 	int num_tc = netdev_get_num_tc(dev);
791 	struct sk_buff *skb;
792 	int tc;
793 
794 	for (tc = num_tc - 1; tc >= 0; tc--) {
795 		int first_txq = q->cur_txq[tc];
796 
797 		if (!(gate_mask & BIT(tc)))
798 			continue;
799 
800 		do {
801 			skb = taprio_dequeue_from_txq(sch, q->cur_txq[tc],
802 						      entry, gate_mask);
803 
804 			taprio_next_tc_txq(dev, tc, &q->cur_txq[tc]);
805 
806 			if (q->cur_txq[tc] >= dev->num_tx_queues)
807 				q->cur_txq[tc] = first_txq;
808 
809 			if (skb)
810 				return skb;
811 		} while (q->cur_txq[tc] != first_txq);
812 	}
813 
814 	return NULL;
815 }
816 
817 /* Broken way of prioritizing smaller TXQ indices and ignoring the traffic
818  * class other than to determine whether the gate is open or not
819  */
820 static struct sk_buff *taprio_dequeue_txq_priority(struct Qdisc *sch,
821 						   struct sched_entry *entry,
822 						   u32 gate_mask)
823 {
824 	struct net_device *dev = qdisc_dev(sch);
825 	struct sk_buff *skb;
826 	int i;
827 
828 	for (i = 0; i < dev->num_tx_queues; i++) {
829 		skb = taprio_dequeue_from_txq(sch, i, entry, gate_mask);
830 		if (skb)
831 			return skb;
832 	}
833 
834 	return NULL;
835 }
836 
837 /* Will not be called in the full offload case, since the TX queues are
838  * attached to the Qdisc created using qdisc_create_dflt()
839  */
840 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
841 {
842 	struct taprio_sched *q = qdisc_priv(sch);
843 	struct sk_buff *skb = NULL;
844 	struct sched_entry *entry;
845 	u32 gate_mask;
846 
847 	rcu_read_lock();
848 	entry = rcu_dereference(q->current_entry);
849 	/* if there's no entry, it means that the schedule didn't
850 	 * start yet, so force all gates to be open, this is in
851 	 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
852 	 * "AdminGateStates"
853 	 */
854 	gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
855 	if (!gate_mask)
856 		goto done;
857 
858 	if (static_branch_unlikely(&taprio_have_broken_mqprio) &&
859 	    !static_branch_likely(&taprio_have_working_mqprio)) {
860 		/* Single NIC kind which is broken */
861 		skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
862 	} else if (static_branch_likely(&taprio_have_working_mqprio) &&
863 		   !static_branch_unlikely(&taprio_have_broken_mqprio)) {
864 		/* Single NIC kind which prioritizes properly */
865 		skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
866 	} else {
867 		/* Mixed NIC kinds present in system, need dynamic testing */
868 		if (q->broken_mqprio)
869 			skb = taprio_dequeue_txq_priority(sch, entry, gate_mask);
870 		else
871 			skb = taprio_dequeue_tc_priority(sch, entry, gate_mask);
872 	}
873 
874 done:
875 	rcu_read_unlock();
876 
877 	return skb;
878 }
879 
880 static bool should_restart_cycle(const struct sched_gate_list *oper,
881 				 const struct sched_entry *entry)
882 {
883 	if (list_is_last(&entry->list, &oper->entries))
884 		return true;
885 
886 	if (ktime_compare(entry->end_time, oper->cycle_end_time) == 0)
887 		return true;
888 
889 	return false;
890 }
891 
892 static bool should_change_schedules(const struct sched_gate_list *admin,
893 				    const struct sched_gate_list *oper,
894 				    ktime_t end_time)
895 {
896 	ktime_t next_base_time, extension_time;
897 
898 	if (!admin)
899 		return false;
900 
901 	next_base_time = sched_base_time(admin);
902 
903 	/* This is the simple case, the end_time would fall after
904 	 * the next schedule base_time.
905 	 */
906 	if (ktime_compare(next_base_time, end_time) <= 0)
907 		return true;
908 
909 	/* This is the cycle_time_extension case, if the end_time
910 	 * plus the amount that can be extended would fall after the
911 	 * next schedule base_time, we can extend the current schedule
912 	 * for that amount.
913 	 */
914 	extension_time = ktime_add_ns(end_time, oper->cycle_time_extension);
915 
916 	/* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
917 	 * how precisely the extension should be made. So after
918 	 * conformance testing, this logic may change.
919 	 */
920 	if (ktime_compare(next_base_time, extension_time) <= 0)
921 		return true;
922 
923 	return false;
924 }
925 
926 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
927 {
928 	struct taprio_sched *q = container_of(timer, struct taprio_sched,
929 					      advance_timer);
930 	struct net_device *dev = qdisc_dev(q->root);
931 	struct sched_gate_list *oper, *admin;
932 	int num_tc = netdev_get_num_tc(dev);
933 	struct sched_entry *entry, *next;
934 	struct Qdisc *sch = q->root;
935 	ktime_t end_time;
936 	int tc;
937 
938 	spin_lock(&q->current_entry_lock);
939 	entry = rcu_dereference_protected(q->current_entry,
940 					  lockdep_is_held(&q->current_entry_lock));
941 	oper = rcu_dereference_protected(q->oper_sched,
942 					 lockdep_is_held(&q->current_entry_lock));
943 	admin = rcu_dereference_protected(q->admin_sched,
944 					  lockdep_is_held(&q->current_entry_lock));
945 
946 	if (!oper)
947 		switch_schedules(q, &admin, &oper);
948 
949 	/* This can happen in two cases: 1. this is the very first run
950 	 * of this function (i.e. we weren't running any schedule
951 	 * previously); 2. The previous schedule just ended. The first
952 	 * entry of all schedules are pre-calculated during the
953 	 * schedule initialization.
954 	 */
955 	if (unlikely(!entry || entry->end_time == oper->base_time)) {
956 		next = list_first_entry(&oper->entries, struct sched_entry,
957 					list);
958 		end_time = next->end_time;
959 		goto first_run;
960 	}
961 
962 	if (should_restart_cycle(oper, entry)) {
963 		next = list_first_entry(&oper->entries, struct sched_entry,
964 					list);
965 		oper->cycle_end_time = ktime_add_ns(oper->cycle_end_time,
966 						    oper->cycle_time);
967 	} else {
968 		next = list_next_entry(entry, list);
969 	}
970 
971 	end_time = ktime_add_ns(entry->end_time, next->interval);
972 	end_time = min_t(ktime_t, end_time, oper->cycle_end_time);
973 
974 	for (tc = 0; tc < num_tc; tc++) {
975 		if (next->gate_duration[tc] == oper->cycle_time)
976 			next->gate_close_time[tc] = KTIME_MAX;
977 		else
978 			next->gate_close_time[tc] = ktime_add_ns(entry->end_time,
979 								 next->gate_duration[tc]);
980 	}
981 
982 	if (should_change_schedules(admin, oper, end_time)) {
983 		switch_schedules(q, &admin, &oper);
984 		/* After changing schedules, the next entry is the first one
985 		 * in the new schedule, with a pre-calculated end_time.
986 		 */
987 		next = list_first_entry(&oper->entries, struct sched_entry, list);
988 		end_time = next->end_time;
989 	}
990 
991 	next->end_time = end_time;
992 	taprio_set_budgets(q, oper, next);
993 
994 first_run:
995 	rcu_assign_pointer(q->current_entry, next);
996 	spin_unlock(&q->current_entry_lock);
997 
998 	hrtimer_set_expires(&q->advance_timer, end_time);
999 
1000 	rcu_read_lock();
1001 	__netif_schedule(sch);
1002 	rcu_read_unlock();
1003 
1004 	return HRTIMER_RESTART;
1005 }
1006 
1007 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
1008 	[TCA_TAPRIO_SCHED_ENTRY_INDEX]	   = { .type = NLA_U32 },
1009 	[TCA_TAPRIO_SCHED_ENTRY_CMD]	   = { .type = NLA_U8 },
1010 	[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
1011 	[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]  = { .type = NLA_U32 },
1012 };
1013 
1014 static const struct nla_policy taprio_tc_policy[TCA_TAPRIO_TC_ENTRY_MAX + 1] = {
1015 	[TCA_TAPRIO_TC_ENTRY_INDEX]	   = NLA_POLICY_MAX(NLA_U32,
1016 							    TC_QOPT_MAX_QUEUE - 1),
1017 	[TCA_TAPRIO_TC_ENTRY_MAX_SDU]	   = { .type = NLA_U32 },
1018 	[TCA_TAPRIO_TC_ENTRY_FP]	   = NLA_POLICY_RANGE(NLA_U32,
1019 							      TC_FP_EXPRESS,
1020 							      TC_FP_PREEMPTIBLE),
1021 };
1022 
1023 static const struct netlink_range_validation_signed taprio_cycle_time_range = {
1024 	.min = 0,
1025 	.max = INT_MAX,
1026 };
1027 
1028 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
1029 	[TCA_TAPRIO_ATTR_PRIOMAP]	       = {
1030 		.len = sizeof(struct tc_mqprio_qopt)
1031 	},
1032 	[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]           = { .type = NLA_NESTED },
1033 	[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]            = { .type = NLA_S64 },
1034 	[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]         = { .type = NLA_NESTED },
1035 	[TCA_TAPRIO_ATTR_SCHED_CLOCKID]              = { .type = NLA_S32 },
1036 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]           =
1037 		NLA_POLICY_FULL_RANGE_SIGNED(NLA_S64, &taprio_cycle_time_range),
1038 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
1039 	[TCA_TAPRIO_ATTR_FLAGS]                      =
1040 		NLA_POLICY_MASK(NLA_U32, TAPRIO_SUPPORTED_FLAGS),
1041 	[TCA_TAPRIO_ATTR_TXTIME_DELAY]		     = { .type = NLA_U32 },
1042 	[TCA_TAPRIO_ATTR_TC_ENTRY]		     = { .type = NLA_NESTED },
1043 };
1044 
1045 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
1046 			    struct sched_entry *entry,
1047 			    struct netlink_ext_ack *extack)
1048 {
1049 	int min_duration = length_to_duration(q, ETH_ZLEN);
1050 	u32 interval = 0;
1051 
1052 	if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
1053 		entry->command = nla_get_u8(
1054 			tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
1055 
1056 	if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
1057 		entry->gate_mask = nla_get_u32(
1058 			tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
1059 
1060 	if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
1061 		interval = nla_get_u32(
1062 			tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
1063 
1064 	/* The interval should allow at least the minimum ethernet
1065 	 * frame to go out.
1066 	 */
1067 	if (interval < min_duration) {
1068 		NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
1069 		return -EINVAL;
1070 	}
1071 
1072 	entry->interval = interval;
1073 
1074 	return 0;
1075 }
1076 
1077 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
1078 			     struct sched_entry *entry, int index,
1079 			     struct netlink_ext_ack *extack)
1080 {
1081 	struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
1082 	int err;
1083 
1084 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
1085 					  entry_policy, NULL);
1086 	if (err < 0) {
1087 		NL_SET_ERR_MSG(extack, "Could not parse nested entry");
1088 		return -EINVAL;
1089 	}
1090 
1091 	entry->index = index;
1092 
1093 	return fill_sched_entry(q, tb, entry, extack);
1094 }
1095 
1096 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
1097 			    struct sched_gate_list *sched,
1098 			    struct netlink_ext_ack *extack)
1099 {
1100 	struct nlattr *n;
1101 	int err, rem;
1102 	int i = 0;
1103 
1104 	if (!list)
1105 		return -EINVAL;
1106 
1107 	nla_for_each_nested(n, list, rem) {
1108 		struct sched_entry *entry;
1109 
1110 		if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
1111 			NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
1112 			continue;
1113 		}
1114 
1115 		if (i >= TAPRIO_ENTRIES_MAX) {
1116 			NL_SET_ERR_MSG_FMT(extack,
1117 					   "Too many schedule entries, at most %u are supported",
1118 					   TAPRIO_ENTRIES_MAX);
1119 			return -E2BIG;
1120 		}
1121 
1122 		entry = kzalloc_obj(*entry);
1123 		if (!entry) {
1124 			NL_SET_ERR_MSG(extack, "Not enough memory for entry");
1125 			return -ENOMEM;
1126 		}
1127 
1128 		err = parse_sched_entry(q, n, entry, i, extack);
1129 		if (err < 0) {
1130 			kfree(entry);
1131 			return err;
1132 		}
1133 
1134 		list_add_tail(&entry->list, &sched->entries);
1135 		i++;
1136 	}
1137 
1138 	sched->num_entries = i;
1139 
1140 	return i;
1141 }
1142 
1143 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
1144 				 struct sched_gate_list *new,
1145 				 struct netlink_ext_ack *extack)
1146 {
1147 	int err = 0;
1148 
1149 	if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
1150 		NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
1151 		return -ENOTSUPP;
1152 	}
1153 
1154 	if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
1155 		new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
1156 
1157 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
1158 		new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
1159 
1160 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
1161 		new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
1162 
1163 	if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
1164 		err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
1165 				       new, extack);
1166 	if (err < 0)
1167 		return err;
1168 
1169 	if (!new->cycle_time) {
1170 		struct sched_entry *entry;
1171 		ktime_t cycle = 0;
1172 
1173 		list_for_each_entry(entry, &new->entries, list)
1174 			cycle = ktime_add_ns(cycle, entry->interval);
1175 
1176 		if (cycle < 0 || cycle > INT_MAX) {
1177 			NL_SET_ERR_MSG(extack, "'cycle_time' is too big");
1178 			return -EINVAL;
1179 		}
1180 
1181 		new->cycle_time = cycle;
1182 	}
1183 
1184 	if (new->cycle_time < new->num_entries * length_to_duration(q, ETH_ZLEN)) {
1185 		NL_SET_ERR_MSG(extack, "'cycle_time' is too small");
1186 		return -EINVAL;
1187 	}
1188 
1189 	taprio_calculate_gate_durations(q, new);
1190 
1191 	return 0;
1192 }
1193 
1194 static int taprio_parse_mqprio_opt(struct net_device *dev,
1195 				   struct tc_mqprio_qopt *qopt,
1196 				   struct netlink_ext_ack *extack,
1197 				   u32 taprio_flags)
1198 {
1199 	bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags);
1200 
1201 	if (!qopt) {
1202 		if (!netdev_get_num_tc(dev)) {
1203 			NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
1204 			return -EINVAL;
1205 		}
1206 		return 0;
1207 	}
1208 
1209 	/* taprio imposes that traffic classes map 1:n to tx queues */
1210 	if (qopt->num_tc > dev->num_tx_queues) {
1211 		NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
1212 		return -EINVAL;
1213 	}
1214 
1215 	/* For some reason, in txtime-assist mode, we allow TXQ ranges for
1216 	 * different TCs to overlap, and just validate the TXQ ranges.
1217 	 */
1218 	return mqprio_validate_qopt(dev, qopt, true, allow_overlapping_txqs,
1219 				    extack);
1220 }
1221 
1222 static int taprio_get_start_time(struct Qdisc *sch,
1223 				 struct sched_gate_list *sched,
1224 				 ktime_t *start)
1225 {
1226 	struct taprio_sched *q = qdisc_priv(sch);
1227 	ktime_t now, base, cycle;
1228 	s64 n;
1229 
1230 	base = sched_base_time(sched);
1231 	now = taprio_get_time(q);
1232 
1233 	if (ktime_after(base, now)) {
1234 		*start = base;
1235 		return 0;
1236 	}
1237 
1238 	cycle = sched->cycle_time;
1239 
1240 	/* The qdisc is expected to have at least one sched_entry.  Moreover,
1241 	 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1242 	 * something went really wrong. In that case, we should warn about this
1243 	 * inconsistent state and return error.
1244 	 */
1245 	if (WARN_ON(!cycle))
1246 		return -EFAULT;
1247 
1248 	/* Schedule the start time for the beginning of the next
1249 	 * cycle.
1250 	 */
1251 	n = div64_s64(ktime_sub_ns(now, base), cycle);
1252 	*start = ktime_add_ns(base, (n + 1) * cycle);
1253 	return 0;
1254 }
1255 
1256 static void setup_first_end_time(struct taprio_sched *q,
1257 				 struct sched_gate_list *sched, ktime_t base)
1258 {
1259 	struct net_device *dev = qdisc_dev(q->root);
1260 	int num_tc = netdev_get_num_tc(dev);
1261 	struct sched_entry *first;
1262 	ktime_t cycle;
1263 	int tc;
1264 
1265 	first = list_first_entry(&sched->entries,
1266 				 struct sched_entry, list);
1267 
1268 	cycle = sched->cycle_time;
1269 
1270 	/* FIXME: find a better place to do this */
1271 	sched->cycle_end_time = ktime_add_ns(base, cycle);
1272 
1273 	first->end_time = ktime_add_ns(base, first->interval);
1274 	taprio_set_budgets(q, sched, first);
1275 
1276 	for (tc = 0; tc < num_tc; tc++) {
1277 		if (first->gate_duration[tc] == sched->cycle_time)
1278 			first->gate_close_time[tc] = KTIME_MAX;
1279 		else
1280 			first->gate_close_time[tc] = ktime_add_ns(base, first->gate_duration[tc]);
1281 	}
1282 
1283 	rcu_assign_pointer(q->current_entry, NULL);
1284 }
1285 
1286 static void taprio_start_sched(struct Qdisc *sch,
1287 			       ktime_t start, struct sched_gate_list *new)
1288 {
1289 	struct taprio_sched *q = qdisc_priv(sch);
1290 	ktime_t expires;
1291 
1292 	if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1293 		return;
1294 
1295 	expires = hrtimer_get_expires(&q->advance_timer);
1296 	if (expires == 0)
1297 		expires = KTIME_MAX;
1298 
1299 	/* If the new schedule starts before the next expiration, we
1300 	 * reprogram it to the earliest one, so we change the admin
1301 	 * schedule to the operational one at the right time.
1302 	 */
1303 	start = min_t(ktime_t, start, expires);
1304 
1305 	hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1306 }
1307 
1308 static void taprio_set_picos_per_byte(struct net_device *dev,
1309 				      struct taprio_sched *q,
1310 				      struct netlink_ext_ack *extack)
1311 {
1312 	struct ethtool_link_ksettings ecmd;
1313 	int speed = SPEED_10;
1314 	int picos_per_byte;
1315 	int err;
1316 
1317 	err = netif_get_link_ksettings(dev, &ecmd);
1318 	if (err < 0)
1319 		goto skip;
1320 
1321 	if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1322 		speed = ecmd.base.speed;
1323 
1324 skip:
1325 	picos_per_byte = (USEC_PER_SEC * 8) / speed;
1326 	if (picos_per_byte < TAPRIO_PICOS_PER_BYTE_MIN) {
1327 		if (!extack)
1328 			pr_warn("Link speed %d is too high. Schedule may be inaccurate.\n",
1329 				speed);
1330 		NL_SET_ERR_MSG_FMT_MOD(extack,
1331 				       "Link speed %d is too high. Schedule may be inaccurate.",
1332 				       speed);
1333 		picos_per_byte = TAPRIO_PICOS_PER_BYTE_MIN;
1334 	}
1335 
1336 	atomic64_set(&q->picos_per_byte, picos_per_byte);
1337 	netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1338 		   dev->name, (long long)atomic64_read(&q->picos_per_byte),
1339 		   speed);
1340 }
1341 
1342 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1343 			       void *ptr)
1344 {
1345 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1346 	struct sched_gate_list *oper, *admin;
1347 	struct qdisc_size_table *stab;
1348 	struct taprio_sched *q;
1349 
1350 	ASSERT_RTNL();
1351 
1352 	if (event != NETDEV_UP && event != NETDEV_CHANGE)
1353 		return NOTIFY_DONE;
1354 
1355 	list_for_each_entry(q, &taprio_list, taprio_list) {
1356 		if (dev != qdisc_dev(q->root))
1357 			continue;
1358 
1359 		taprio_set_picos_per_byte(dev, q, NULL);
1360 
1361 		stab = rtnl_dereference(q->root->stab);
1362 
1363 		rcu_read_lock();
1364 		oper = rcu_dereference(q->oper_sched);
1365 		if (oper)
1366 			taprio_update_queue_max_sdu(q, oper, stab);
1367 
1368 		admin = rcu_dereference(q->admin_sched);
1369 		if (admin)
1370 			taprio_update_queue_max_sdu(q, admin, stab);
1371 		rcu_read_unlock();
1372 
1373 		break;
1374 	}
1375 
1376 	return NOTIFY_DONE;
1377 }
1378 
1379 static void setup_txtime(struct taprio_sched *q,
1380 			 struct sched_gate_list *sched, ktime_t base)
1381 {
1382 	struct sched_entry *entry;
1383 	u64 interval = 0;
1384 
1385 	list_for_each_entry(entry, &sched->entries, list) {
1386 		entry->next_txtime = ktime_add_ns(base, interval);
1387 		interval += entry->interval;
1388 	}
1389 }
1390 
1391 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1392 {
1393 	struct __tc_taprio_qopt_offload *__offload;
1394 
1395 	__offload = kzalloc_flex(*__offload, offload.entries, num_entries);
1396 	if (!__offload)
1397 		return NULL;
1398 
1399 	refcount_set(&__offload->users, 1);
1400 
1401 	return &__offload->offload;
1402 }
1403 
1404 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1405 						  *offload)
1406 {
1407 	struct __tc_taprio_qopt_offload *__offload;
1408 
1409 	__offload = container_of(offload, struct __tc_taprio_qopt_offload,
1410 				 offload);
1411 
1412 	refcount_inc(&__offload->users);
1413 
1414 	return offload;
1415 }
1416 EXPORT_SYMBOL_GPL(taprio_offload_get);
1417 
1418 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1419 {
1420 	struct __tc_taprio_qopt_offload *__offload;
1421 
1422 	__offload = container_of(offload, struct __tc_taprio_qopt_offload,
1423 				 offload);
1424 
1425 	if (!refcount_dec_and_test(&__offload->users))
1426 		return;
1427 
1428 	kfree(__offload);
1429 }
1430 EXPORT_SYMBOL_GPL(taprio_offload_free);
1431 
1432 /* The function will only serve to keep the pointers to the "oper" and "admin"
1433  * schedules valid in relation to their base times, so when calling dump() the
1434  * users looks at the right schedules.
1435  * When using full offload, the admin configuration is promoted to oper at the
1436  * base_time in the PHC time domain.  But because the system time is not
1437  * necessarily in sync with that, we can't just trigger a hrtimer to call
1438  * switch_schedules at the right hardware time.
1439  * At the moment we call this by hand right away from taprio, but in the future
1440  * it will be useful to create a mechanism for drivers to notify taprio of the
1441  * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1442  * This is left as TODO.
1443  */
1444 static void taprio_offload_config_changed(struct taprio_sched *q)
1445 {
1446 	struct sched_gate_list *oper, *admin;
1447 
1448 	oper = rtnl_dereference(q->oper_sched);
1449 	admin = rtnl_dereference(q->admin_sched);
1450 
1451 	switch_schedules(q, &admin, &oper);
1452 }
1453 
1454 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1455 {
1456 	int num_tc = netdev_get_num_tc(dev);
1457 	u32 i, queue_mask = 0;
1458 
1459 	for (i = 0; i < num_tc; i++) {
1460 		struct netdev_tc_txq res;
1461 
1462 		if (!(tc_mask & BIT(i)))
1463 			continue;
1464 
1465 		res.combined = READ_ONCE(dev->tc_to_txq[i].combined);
1466 
1467 		queue_mask |= GENMASK(res.offset + res.count - 1, res.offset);
1468 	}
1469 
1470 	return queue_mask;
1471 }
1472 
1473 static void taprio_sched_to_offload(struct net_device *dev,
1474 				    struct sched_gate_list *sched,
1475 				    struct tc_taprio_qopt_offload *offload,
1476 				    const struct tc_taprio_caps *caps)
1477 {
1478 	struct sched_entry *entry;
1479 	int i = 0;
1480 
1481 	offload->base_time = sched->base_time;
1482 	offload->cycle_time = sched->cycle_time;
1483 	offload->cycle_time_extension = sched->cycle_time_extension;
1484 
1485 	list_for_each_entry(entry, &sched->entries, list) {
1486 		struct tc_taprio_sched_entry *e = &offload->entries[i];
1487 
1488 		e->command = entry->command;
1489 		e->interval = entry->interval;
1490 		if (caps->gate_mask_per_txq)
1491 			e->gate_mask = tc_map_to_queue_mask(dev,
1492 							    entry->gate_mask);
1493 		else
1494 			e->gate_mask = entry->gate_mask;
1495 
1496 		i++;
1497 	}
1498 
1499 	offload->num_entries = i;
1500 }
1501 
1502 static void taprio_detect_broken_mqprio(struct taprio_sched *q)
1503 {
1504 	struct net_device *dev = qdisc_dev(q->root);
1505 	struct tc_taprio_caps caps;
1506 
1507 	qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1508 				 &caps, sizeof(caps));
1509 
1510 	q->broken_mqprio = caps.broken_mqprio;
1511 	if (q->broken_mqprio)
1512 		static_branch_inc(&taprio_have_broken_mqprio);
1513 	else
1514 		static_branch_inc(&taprio_have_working_mqprio);
1515 
1516 	q->detected_mqprio = true;
1517 }
1518 
1519 static void taprio_cleanup_broken_mqprio(struct taprio_sched *q)
1520 {
1521 	if (!q->detected_mqprio)
1522 		return;
1523 
1524 	if (q->broken_mqprio)
1525 		static_branch_dec(&taprio_have_broken_mqprio);
1526 	else
1527 		static_branch_dec(&taprio_have_working_mqprio);
1528 }
1529 
1530 static int taprio_enable_offload(struct net_device *dev,
1531 				 struct taprio_sched *q,
1532 				 struct sched_gate_list *sched,
1533 				 struct netlink_ext_ack *extack)
1534 {
1535 	const struct net_device_ops *ops = dev->netdev_ops;
1536 	struct tc_taprio_qopt_offload *offload;
1537 	struct tc_taprio_caps caps;
1538 	int tc, err = 0;
1539 
1540 	if (!ops->ndo_setup_tc) {
1541 		NL_SET_ERR_MSG(extack,
1542 			       "Device does not support taprio offload");
1543 		return -EOPNOTSUPP;
1544 	}
1545 
1546 	qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO,
1547 				 &caps, sizeof(caps));
1548 
1549 	if (!caps.supports_queue_max_sdu) {
1550 		for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
1551 			if (q->max_sdu[tc]) {
1552 				NL_SET_ERR_MSG_MOD(extack,
1553 						   "Device does not handle queueMaxSDU");
1554 				return -EOPNOTSUPP;
1555 			}
1556 		}
1557 	}
1558 
1559 	offload = taprio_offload_alloc(sched->num_entries);
1560 	if (!offload) {
1561 		NL_SET_ERR_MSG(extack,
1562 			       "Not enough memory for enabling offload mode");
1563 		return -ENOMEM;
1564 	}
1565 	offload->cmd = TAPRIO_CMD_REPLACE;
1566 	offload->extack = extack;
1567 	mqprio_qopt_reconstruct(dev, &offload->mqprio.qopt);
1568 	offload->mqprio.extack = extack;
1569 	taprio_sched_to_offload(dev, sched, offload, &caps);
1570 	mqprio_fp_to_offload(q->fp, &offload->mqprio);
1571 
1572 	for (tc = 0; tc < TC_MAX_QUEUE; tc++)
1573 		offload->max_sdu[tc] = q->max_sdu[tc];
1574 
1575 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1576 	if (err < 0) {
1577 		NL_SET_ERR_MSG_WEAK(extack,
1578 				    "Device failed to setup taprio offload");
1579 		goto done;
1580 	}
1581 
1582 	q->offloaded = true;
1583 
1584 done:
1585 	/* The offload structure may linger around via a reference taken by the
1586 	 * device driver, so clear up the netlink extack pointer so that the
1587 	 * driver isn't tempted to dereference data which stopped being valid
1588 	 */
1589 	offload->extack = NULL;
1590 	offload->mqprio.extack = NULL;
1591 	taprio_offload_free(offload);
1592 
1593 	return err;
1594 }
1595 
1596 static int taprio_disable_offload(struct net_device *dev,
1597 				  struct taprio_sched *q,
1598 				  struct netlink_ext_ack *extack)
1599 {
1600 	const struct net_device_ops *ops = dev->netdev_ops;
1601 	struct tc_taprio_qopt_offload *offload;
1602 	int err;
1603 
1604 	if (!q->offloaded)
1605 		return 0;
1606 
1607 	offload = taprio_offload_alloc(0);
1608 	if (!offload) {
1609 		NL_SET_ERR_MSG(extack,
1610 			       "Not enough memory to disable offload mode");
1611 		return -ENOMEM;
1612 	}
1613 	offload->cmd = TAPRIO_CMD_DESTROY;
1614 
1615 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1616 	if (err < 0) {
1617 		NL_SET_ERR_MSG(extack,
1618 			       "Device failed to disable offload");
1619 		goto out;
1620 	}
1621 
1622 	q->offloaded = false;
1623 
1624 out:
1625 	taprio_offload_free(offload);
1626 
1627 	return err;
1628 }
1629 
1630 /* If full offload is enabled, the only possible clockid is the net device's
1631  * PHC. For that reason, specifying a clockid through netlink is incorrect.
1632  * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1633  * in sync with the specified clockid via a user space daemon such as phc2sys.
1634  * For both software taprio and txtime-assist, the clockid is used for the
1635  * hrtimer that advances the schedule and hence mandatory.
1636  */
1637 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1638 				struct netlink_ext_ack *extack)
1639 {
1640 	struct taprio_sched *q = qdisc_priv(sch);
1641 	struct net_device *dev = qdisc_dev(sch);
1642 	int err = -EINVAL;
1643 
1644 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1645 		const struct ethtool_ops *ops = dev->ethtool_ops;
1646 		struct kernel_ethtool_ts_info info = {
1647 			.cmd = ETHTOOL_GET_TS_INFO,
1648 			.phc_index = -1,
1649 		};
1650 
1651 		if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1652 			NL_SET_ERR_MSG(extack,
1653 				       "The 'clockid' cannot be specified for full offload");
1654 			goto out;
1655 		}
1656 
1657 		if (ops && ops->get_ts_info)
1658 			err = ops->get_ts_info(dev, &info);
1659 
1660 		if (err || info.phc_index < 0) {
1661 			NL_SET_ERR_MSG(extack,
1662 				       "Device does not have a PTP clock");
1663 			err = -ENOTSUPP;
1664 			goto out;
1665 		}
1666 	} else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1667 		int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1668 		enum tk_offsets tk_offset;
1669 
1670 		/* We only support static clockids and we don't allow
1671 		 * for it to be modified after the first init.
1672 		 */
1673 		if (clockid < 0 ||
1674 		    (q->clockid != -1 && q->clockid != clockid)) {
1675 			NL_SET_ERR_MSG(extack,
1676 				       "Changing the 'clockid' of a running schedule is not supported");
1677 			err = -ENOTSUPP;
1678 			goto out;
1679 		}
1680 
1681 		switch (clockid) {
1682 		case CLOCK_REALTIME:
1683 			tk_offset = TK_OFFS_REAL;
1684 			break;
1685 		case CLOCK_MONOTONIC:
1686 			tk_offset = TK_OFFS_MAX;
1687 			break;
1688 		case CLOCK_BOOTTIME:
1689 			tk_offset = TK_OFFS_BOOT;
1690 			break;
1691 		case CLOCK_TAI:
1692 			tk_offset = TK_OFFS_TAI;
1693 			break;
1694 		default:
1695 			NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1696 			err = -EINVAL;
1697 			goto out;
1698 		}
1699 		/* This pairs with READ_ONCE() in taprio_mono_to_any */
1700 		WRITE_ONCE(q->tk_offset, tk_offset);
1701 
1702 		q->clockid = clockid;
1703 	} else {
1704 		NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1705 		goto out;
1706 	}
1707 
1708 	/* Everything went ok, return success. */
1709 	err = 0;
1710 
1711 out:
1712 	return err;
1713 }
1714 
1715 static int taprio_parse_tc_entry(struct Qdisc *sch,
1716 				 struct nlattr *opt,
1717 				 u32 max_sdu[TC_QOPT_MAX_QUEUE],
1718 				 u32 fp[TC_QOPT_MAX_QUEUE],
1719 				 unsigned long *seen_tcs,
1720 				 struct netlink_ext_ack *extack)
1721 {
1722 	struct nlattr *tb[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { };
1723 	struct net_device *dev = qdisc_dev(sch);
1724 	int err, tc;
1725 	u32 val;
1726 
1727 	err = nla_parse_nested(tb, TCA_TAPRIO_TC_ENTRY_MAX, opt,
1728 			       taprio_tc_policy, extack);
1729 	if (err < 0)
1730 		return err;
1731 
1732 	if (NL_REQ_ATTR_CHECK(extack, opt, tb, TCA_TAPRIO_TC_ENTRY_INDEX)) {
1733 		NL_SET_ERR_MSG_MOD(extack, "TC entry index missing");
1734 		return -EINVAL;
1735 	}
1736 
1737 	tc = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_INDEX]);
1738 	if (*seen_tcs & BIT(tc)) {
1739 		NL_SET_ERR_MSG_ATTR(extack, tb[TCA_TAPRIO_TC_ENTRY_INDEX],
1740 				    "Duplicate tc entry");
1741 		return -EINVAL;
1742 	}
1743 
1744 	*seen_tcs |= BIT(tc);
1745 
1746 	if (tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]) {
1747 		val = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]);
1748 		if (val > dev->max_mtu) {
1749 			NL_SET_ERR_MSG_MOD(extack, "TC max SDU exceeds device max MTU");
1750 			return -ERANGE;
1751 		}
1752 
1753 		max_sdu[tc] = val;
1754 	}
1755 
1756 	if (tb[TCA_TAPRIO_TC_ENTRY_FP])
1757 		fp[tc] = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_FP]);
1758 
1759 	return 0;
1760 }
1761 
1762 static int taprio_parse_tc_entries(struct Qdisc *sch,
1763 				   struct nlattr *opt,
1764 				   struct netlink_ext_ack *extack)
1765 {
1766 	struct taprio_sched *q = qdisc_priv(sch);
1767 	struct net_device *dev = qdisc_dev(sch);
1768 	u32 max_sdu[TC_QOPT_MAX_QUEUE];
1769 	bool have_preemption = false;
1770 	unsigned long seen_tcs = 0;
1771 	u32 fp[TC_QOPT_MAX_QUEUE];
1772 	struct nlattr *n;
1773 	int tc, rem;
1774 	int err = 0;
1775 
1776 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) {
1777 		max_sdu[tc] = q->max_sdu[tc];
1778 		fp[tc] = q->fp[tc];
1779 	}
1780 
1781 	nla_for_each_nested_type(n, TCA_TAPRIO_ATTR_TC_ENTRY, opt, rem) {
1782 		err = taprio_parse_tc_entry(sch, n, max_sdu, fp, &seen_tcs,
1783 					    extack);
1784 		if (err)
1785 			return err;
1786 	}
1787 
1788 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) {
1789 		WRITE_ONCE(q->max_sdu[tc], max_sdu[tc]);
1790 		WRITE_ONCE(q->fp[tc], fp[tc]);
1791 		if (fp[tc] != TC_FP_EXPRESS)
1792 			have_preemption = true;
1793 	}
1794 
1795 	if (have_preemption) {
1796 		if (!FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1797 			NL_SET_ERR_MSG(extack,
1798 				       "Preemption only supported with full offload");
1799 			return -EOPNOTSUPP;
1800 		}
1801 
1802 		if (!ethtool_dev_mm_supported(dev)) {
1803 			NL_SET_ERR_MSG(extack,
1804 				       "Device does not support preemption");
1805 			return -EOPNOTSUPP;
1806 		}
1807 	}
1808 
1809 	return err;
1810 }
1811 
1812 static int taprio_mqprio_cmp(const struct net_device *dev,
1813 			     const struct tc_mqprio_qopt *mqprio)
1814 {
1815 	int i;
1816 
1817 	if (!mqprio || mqprio->num_tc != netdev_get_num_tc(dev))
1818 		return -1;
1819 
1820 	for (i = 0; i < mqprio->num_tc; i++) {
1821 		struct netdev_tc_txq res;
1822 
1823 		res.combined = READ_ONCE(dev->tc_to_txq[i].combined);
1824 		if (res.count != mqprio->count[i] ||
1825 		    res.offset != mqprio->offset[i])
1826 			return -1;
1827 	}
1828 
1829 	for (i = 0; i <= TC_BITMASK; i++)
1830 		if (netdev_get_prio_tc_map(dev, i) != mqprio->prio_tc_map[i])
1831 			return -1;
1832 
1833 	return 0;
1834 }
1835 
1836 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1837 			 struct netlink_ext_ack *extack)
1838 {
1839 	struct qdisc_size_table *stab = rtnl_dereference(sch->stab);
1840 	struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1841 	struct sched_gate_list *oper, *admin, *new_admin;
1842 	struct taprio_sched *q = qdisc_priv(sch);
1843 	struct net_device *dev = qdisc_dev(sch);
1844 	struct tc_mqprio_qopt *mqprio = NULL;
1845 	unsigned long flags;
1846 	u32 taprio_flags;
1847 	ktime_t start;
1848 	int i, err;
1849 
1850 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1851 					  taprio_policy, extack);
1852 	if (err < 0)
1853 		return err;
1854 
1855 	if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1856 		mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1857 
1858 	/* The semantics of the 'flags' argument in relation to 'change()'
1859 	 * requests, are interpreted following two rules (which are applied in
1860 	 * this order): (1) an omitted 'flags' argument is interpreted as
1861 	 * zero; (2) the 'flags' of a "running" taprio instance cannot be
1862 	 * changed.
1863 	 */
1864 	taprio_flags = nla_get_u32_default(tb[TCA_TAPRIO_ATTR_FLAGS], 0);
1865 
1866 	/* txtime-assist and full offload are mutually exclusive */
1867 	if ((taprio_flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
1868 	    (taprio_flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) {
1869 		NL_SET_ERR_MSG_ATTR(extack, tb[TCA_TAPRIO_ATTR_FLAGS],
1870 				    "TXTIME_ASSIST and FULL_OFFLOAD are mutually exclusive");
1871 		return -EINVAL;
1872 	}
1873 
1874 	if (q->flags != taprio_flags) {
1875 		if (q->flags != TAPRIO_FLAGS_INVALID) {
1876 			NL_SET_ERR_MSG_MOD(extack,
1877 					   "Changing 'flags' of a running schedule is not supported");
1878 			return -EOPNOTSUPP;
1879 		}
1880 		WRITE_ONCE(q->flags, taprio_flags);
1881 	}
1882 
1883 	/* Needed for length_to_duration() during netlink attribute parsing */
1884 	taprio_set_picos_per_byte(dev, q, extack);
1885 
1886 	err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1887 	if (err < 0)
1888 		return err;
1889 
1890 	err = taprio_parse_tc_entries(sch, opt, extack);
1891 	if (err)
1892 		return err;
1893 
1894 	new_admin = kzalloc_obj(*new_admin);
1895 	if (!new_admin) {
1896 		NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1897 		return -ENOMEM;
1898 	}
1899 	INIT_LIST_HEAD(&new_admin->entries);
1900 
1901 	oper = rtnl_dereference(q->oper_sched);
1902 	admin = rtnl_dereference(q->admin_sched);
1903 
1904 	/* no changes - no new mqprio settings */
1905 	if (!taprio_mqprio_cmp(dev, mqprio))
1906 		mqprio = NULL;
1907 
1908 	if (mqprio && (oper || admin)) {
1909 		NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1910 		err = -ENOTSUPP;
1911 		goto free_sched;
1912 	}
1913 
1914 	if (mqprio) {
1915 		err = netdev_set_num_tc(dev, mqprio->num_tc);
1916 		if (err)
1917 			goto free_sched;
1918 		for (i = 0; i < mqprio->num_tc; i++) {
1919 			netdev_set_tc_queue(dev, i,
1920 					    mqprio->count[i],
1921 					    mqprio->offset[i]);
1922 			q->cur_txq[i] = mqprio->offset[i];
1923 		}
1924 
1925 		/* Always use supplied priority mappings */
1926 		for (i = 0; i <= TC_BITMASK; i++)
1927 			netdev_set_prio_tc_map(dev, i,
1928 					       mqprio->prio_tc_map[i]);
1929 	}
1930 
1931 	err = parse_taprio_schedule(q, tb, new_admin, extack);
1932 	if (err < 0)
1933 		goto free_sched;
1934 
1935 	if (new_admin->num_entries == 0) {
1936 		NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1937 		err = -EINVAL;
1938 		goto free_sched;
1939 	}
1940 
1941 	err = taprio_parse_clockid(sch, tb, extack);
1942 	if (err < 0)
1943 		goto free_sched;
1944 
1945 	taprio_update_queue_max_sdu(q, new_admin, stab);
1946 
1947 	if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1948 		err = taprio_enable_offload(dev, q, new_admin, extack);
1949 	else
1950 		err = taprio_disable_offload(dev, q, extack);
1951 	if (err)
1952 		goto free_sched;
1953 
1954 	/* Protects against enqueue()/dequeue() */
1955 	spin_lock_bh(qdisc_lock(sch));
1956 
1957 	if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1958 		if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1959 			NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1960 			err = -EINVAL;
1961 			goto unlock;
1962 		}
1963 
1964 		WRITE_ONCE(q->txtime_delay,
1965 			   nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]));
1966 	}
1967 
1968 	if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1969 	    !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1970 	    !hrtimer_active(&q->advance_timer)) {
1971 		hrtimer_setup(&q->advance_timer, advance_sched, q->clockid, HRTIMER_MODE_ABS);
1972 	}
1973 
1974 	err = taprio_get_start_time(sch, new_admin, &start);
1975 	if (err < 0) {
1976 		NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1977 		goto unlock;
1978 	}
1979 
1980 	setup_txtime(q, new_admin, start);
1981 
1982 	if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1983 		if (!oper) {
1984 			rcu_assign_pointer(q->oper_sched, new_admin);
1985 			err = 0;
1986 			new_admin = NULL;
1987 			goto unlock;
1988 		}
1989 
1990 		/* Not going to race against advance_sched(), but still */
1991 		admin = rcu_replace_pointer(q->admin_sched, new_admin,
1992 					    lockdep_rtnl_is_held());
1993 		if (admin)
1994 			call_rcu(&admin->rcu, taprio_free_sched_cb);
1995 	} else {
1996 		setup_first_end_time(q, new_admin, start);
1997 
1998 		/* Protects against advance_sched() */
1999 		spin_lock_irqsave(&q->current_entry_lock, flags);
2000 
2001 		taprio_start_sched(sch, start, new_admin);
2002 
2003 		admin = rcu_replace_pointer(q->admin_sched, new_admin,
2004 					    lockdep_rtnl_is_held());
2005 		if (admin)
2006 			call_rcu(&admin->rcu, taprio_free_sched_cb);
2007 
2008 		spin_unlock_irqrestore(&q->current_entry_lock, flags);
2009 
2010 		if (FULL_OFFLOAD_IS_ENABLED(q->flags))
2011 			taprio_offload_config_changed(q);
2012 	}
2013 
2014 	new_admin = NULL;
2015 	err = 0;
2016 
2017 	if (!stab)
2018 		NL_SET_ERR_MSG_MOD(extack,
2019 				   "Size table not specified, frame length estimations may be inaccurate");
2020 
2021 unlock:
2022 	spin_unlock_bh(qdisc_lock(sch));
2023 
2024 free_sched:
2025 	if (new_admin)
2026 		call_rcu(&new_admin->rcu, taprio_free_sched_cb);
2027 
2028 	return err;
2029 }
2030 
2031 static void taprio_reset(struct Qdisc *sch)
2032 {
2033 	struct taprio_sched *q = qdisc_priv(sch);
2034 	struct net_device *dev = qdisc_dev(sch);
2035 	int i;
2036 
2037 	hrtimer_cancel(&q->advance_timer);
2038 
2039 	if (q->qdiscs) {
2040 		for (i = 0; i < dev->num_tx_queues; i++)
2041 			if (q->qdiscs[i])
2042 				qdisc_reset(q->qdiscs[i]);
2043 	}
2044 }
2045 
2046 static void taprio_destroy(struct Qdisc *sch)
2047 {
2048 	struct taprio_sched *q = qdisc_priv(sch);
2049 	struct net_device *dev = qdisc_dev(sch);
2050 	struct sched_gate_list *oper, *admin;
2051 	unsigned int i;
2052 
2053 	list_del(&q->taprio_list);
2054 
2055 	/* Note that taprio_reset() might not be called if an error
2056 	 * happens in qdisc_create(), after taprio_init() has been called.
2057 	 */
2058 	hrtimer_cancel(&q->advance_timer);
2059 	qdisc_synchronize(sch);
2060 
2061 	taprio_disable_offload(dev, q, NULL);
2062 
2063 	if (q->qdiscs) {
2064 		for (i = 0; i < dev->num_tx_queues; i++)
2065 			qdisc_put(q->qdiscs[i]);
2066 
2067 		kfree(q->qdiscs);
2068 	}
2069 	q->qdiscs = NULL;
2070 
2071 	netdev_reset_tc(dev);
2072 
2073 	oper = rtnl_dereference(q->oper_sched);
2074 	admin = rtnl_dereference(q->admin_sched);
2075 
2076 	if (oper)
2077 		call_rcu(&oper->rcu, taprio_free_sched_cb);
2078 
2079 	if (admin)
2080 		call_rcu(&admin->rcu, taprio_free_sched_cb);
2081 
2082 	taprio_cleanup_broken_mqprio(q);
2083 }
2084 
2085 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
2086 		       struct netlink_ext_ack *extack)
2087 {
2088 	struct taprio_sched *q = qdisc_priv(sch);
2089 	struct net_device *dev = qdisc_dev(sch);
2090 	int i, tc;
2091 
2092 	spin_lock_init(&q->current_entry_lock);
2093 
2094 	hrtimer_setup(&q->advance_timer, advance_sched, CLOCK_TAI, HRTIMER_MODE_ABS);
2095 
2096 	q->root = sch;
2097 
2098 	/* We only support static clockids. Use an invalid value as default
2099 	 * and get the valid one on taprio_change().
2100 	 */
2101 	q->clockid = -1;
2102 	q->flags = TAPRIO_FLAGS_INVALID;
2103 
2104 	list_add(&q->taprio_list, &taprio_list);
2105 
2106 	if (sch->parent != TC_H_ROOT) {
2107 		NL_SET_ERR_MSG_MOD(extack, "Can only be attached as root qdisc");
2108 		return -EOPNOTSUPP;
2109 	}
2110 
2111 	if (!netif_is_multiqueue(dev)) {
2112 		NL_SET_ERR_MSG_MOD(extack, "Multi-queue device is required");
2113 		return -EOPNOTSUPP;
2114 	}
2115 
2116 	q->qdiscs = kzalloc_objs(q->qdiscs[0], dev->num_tx_queues);
2117 	if (!q->qdiscs)
2118 		return -ENOMEM;
2119 
2120 	if (!opt)
2121 		return -EINVAL;
2122 
2123 	for (i = 0; i < dev->num_tx_queues; i++) {
2124 		struct netdev_queue *dev_queue;
2125 		struct Qdisc *qdisc;
2126 
2127 		dev_queue = netdev_get_tx_queue(dev, i);
2128 		qdisc = qdisc_create_dflt(dev_queue,
2129 					  &pfifo_qdisc_ops,
2130 					  TC_H_MAKE(TC_H_MAJ(sch->handle),
2131 						    TC_H_MIN(i + 1)),
2132 					  extack);
2133 		if (!qdisc)
2134 			return -ENOMEM;
2135 
2136 		if (i < dev->real_num_tx_queues)
2137 			qdisc_hash_add(qdisc, false);
2138 
2139 		q->qdiscs[i] = qdisc;
2140 	}
2141 
2142 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
2143 		q->fp[tc] = TC_FP_EXPRESS;
2144 
2145 	taprio_detect_broken_mqprio(q);
2146 
2147 	return taprio_change(sch, opt, extack);
2148 }
2149 
2150 static void taprio_attach(struct Qdisc *sch)
2151 {
2152 	struct taprio_sched *q = qdisc_priv(sch);
2153 	struct net_device *dev = qdisc_dev(sch);
2154 	unsigned int ntx;
2155 
2156 	/* Attach underlying qdisc */
2157 	for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
2158 		struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, ntx);
2159 		struct Qdisc *old, *dev_queue_qdisc;
2160 
2161 		if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2162 			struct Qdisc *qdisc = q->qdiscs[ntx];
2163 
2164 			/* In offload mode, the root taprio qdisc is bypassed
2165 			 * and the netdev TX queues see the children directly
2166 			 */
2167 			qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
2168 			dev_queue_qdisc = qdisc;
2169 		} else {
2170 			/* In software mode, attach the root taprio qdisc
2171 			 * to all netdev TX queues, so that dev_qdisc_enqueue()
2172 			 * goes through taprio_enqueue().
2173 			 */
2174 			dev_queue_qdisc = sch;
2175 		}
2176 		old = dev_graft_qdisc(dev_queue, dev_queue_qdisc);
2177 		/* The qdisc's refcount requires to be elevated once
2178 		 * for each netdev TX queue it is grafted onto
2179 		 */
2180 		qdisc_refcount_inc(dev_queue_qdisc);
2181 		if (old)
2182 			qdisc_put(old);
2183 	}
2184 }
2185 
2186 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
2187 					     unsigned long cl)
2188 {
2189 	struct net_device *dev = qdisc_dev(sch);
2190 	unsigned long ntx = cl - 1;
2191 
2192 	if (ntx >= dev->num_tx_queues)
2193 		return NULL;
2194 
2195 	return netdev_get_tx_queue(dev, ntx);
2196 }
2197 
2198 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
2199 			struct Qdisc *new, struct Qdisc **old,
2200 			struct netlink_ext_ack *extack)
2201 {
2202 	struct taprio_sched *q = qdisc_priv(sch);
2203 	struct net_device *dev = qdisc_dev(sch);
2204 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
2205 
2206 	if (!dev_queue)
2207 		return -EINVAL;
2208 
2209 	if (!new)
2210 		new = &noop_qdisc;
2211 
2212 	if (dev->flags & IFF_UP)
2213 		dev_deactivate(dev, false);
2214 
2215 	/* In offload mode, the child Qdisc is directly attached to the netdev
2216 	 * TX queue, and thus, we need to keep its refcount elevated in order
2217 	 * to counteract qdisc_graft()'s call to qdisc_put() once per TX queue.
2218 	 * However, save the reference to the new qdisc in the private array in
2219 	 * both software and offload cases, to have an up-to-date reference to
2220 	 * our children.
2221 	 */
2222 	*old = q->qdiscs[cl - 1];
2223 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
2224 		WARN_ON_ONCE(dev_graft_qdisc(dev_queue, new) != *old);
2225 		if (new != &noop_qdisc)
2226 			qdisc_refcount_inc(new);
2227 		if (*old && *old != &noop_qdisc)
2228 			qdisc_put(*old);
2229 	}
2230 
2231 	q->qdiscs[cl - 1] = new;
2232 	if (new != &noop_qdisc)
2233 		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
2234 
2235 	if (dev->flags & IFF_UP)
2236 		dev_activate(dev);
2237 
2238 	return 0;
2239 }
2240 
2241 static int dump_entry(struct sk_buff *msg,
2242 		      const struct sched_entry *entry)
2243 {
2244 	struct nlattr *item;
2245 
2246 	item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
2247 	if (!item)
2248 		return -ENOSPC;
2249 
2250 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
2251 		goto nla_put_failure;
2252 
2253 	if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
2254 		goto nla_put_failure;
2255 
2256 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
2257 			entry->gate_mask))
2258 		goto nla_put_failure;
2259 
2260 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
2261 			entry->interval))
2262 		goto nla_put_failure;
2263 
2264 	return nla_nest_end(msg, item);
2265 
2266 nla_put_failure:
2267 	nla_nest_cancel(msg, item);
2268 	return -1;
2269 }
2270 
2271 static int dump_schedule(struct sk_buff *msg,
2272 			 const struct sched_gate_list *root)
2273 {
2274 	struct nlattr *entry_list;
2275 	struct sched_entry *entry;
2276 
2277 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
2278 			root->base_time, TCA_TAPRIO_PAD))
2279 		return -1;
2280 
2281 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
2282 			root->cycle_time, TCA_TAPRIO_PAD))
2283 		return -1;
2284 
2285 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
2286 			root->cycle_time_extension, TCA_TAPRIO_PAD))
2287 		return -1;
2288 
2289 	entry_list = nla_nest_start_noflag(msg,
2290 					   TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
2291 	if (!entry_list)
2292 		goto error_nest;
2293 
2294 	list_for_each_entry(entry, &root->entries, list) {
2295 		if (dump_entry(msg, entry) < 0)
2296 			goto error_nest;
2297 	}
2298 
2299 	nla_nest_end(msg, entry_list);
2300 	return 0;
2301 
2302 error_nest:
2303 	nla_nest_cancel(msg, entry_list);
2304 	return -1;
2305 }
2306 
2307 static int taprio_dump_tc_entries(struct sk_buff *skb,
2308 				  const struct taprio_sched *q,
2309 				  const struct sched_gate_list *sched)
2310 {
2311 	struct nlattr *n;
2312 	int tc;
2313 
2314 	for (tc = 0; tc < TC_MAX_QUEUE; tc++) {
2315 		n = nla_nest_start(skb, TCA_TAPRIO_ATTR_TC_ENTRY);
2316 		if (!n)
2317 			return -EMSGSIZE;
2318 
2319 		if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_INDEX, tc))
2320 			goto nla_put_failure;
2321 
2322 		if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_MAX_SDU,
2323 				READ_ONCE(sched->max_sdu[tc])))
2324 			goto nla_put_failure;
2325 
2326 		if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_FP,
2327 				READ_ONCE(q->fp[tc])))
2328 			goto nla_put_failure;
2329 
2330 		nla_nest_end(skb, n);
2331 	}
2332 
2333 	return 0;
2334 
2335 nla_put_failure:
2336 	nla_nest_cancel(skb, n);
2337 	return -EMSGSIZE;
2338 }
2339 
2340 static int taprio_put_stat(struct sk_buff *skb, u64 val, u16 attrtype)
2341 {
2342 	if (val == TAPRIO_STAT_NOT_SET)
2343 		return 0;
2344 	if (nla_put_u64_64bit(skb, attrtype, val, TCA_TAPRIO_OFFLOAD_STATS_PAD))
2345 		return -EMSGSIZE;
2346 	return 0;
2347 }
2348 
2349 static int taprio_dump_xstats(struct Qdisc *sch, struct gnet_dump *d,
2350 			      struct tc_taprio_qopt_offload *offload,
2351 			      struct tc_taprio_qopt_stats *stats)
2352 {
2353 	struct net_device *dev = qdisc_dev(sch);
2354 	const struct net_device_ops *ops;
2355 	struct sk_buff *skb = d->skb;
2356 	struct nlattr *xstats;
2357 	int err;
2358 
2359 	ops = qdisc_dev(sch)->netdev_ops;
2360 
2361 	/* FIXME I could use qdisc_offload_dump_helper(), but that messes
2362 	 * with sch->flags depending on whether the device reports taprio
2363 	 * stats, and I'm not sure whether that's a good idea, considering
2364 	 * that stats are optional to the offload itself
2365 	 */
2366 	if (!ops->ndo_setup_tc)
2367 		return 0;
2368 
2369 	memset(stats, 0xff, sizeof(*stats));
2370 
2371 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
2372 	if (err == -EOPNOTSUPP)
2373 		return 0;
2374 	if (err)
2375 		return err;
2376 
2377 	xstats = nla_nest_start(skb, TCA_STATS_APP);
2378 	if (!xstats)
2379 		goto err;
2380 
2381 	if (taprio_put_stat(skb, stats->window_drops,
2382 			    TCA_TAPRIO_OFFLOAD_STATS_WINDOW_DROPS) ||
2383 	    taprio_put_stat(skb, stats->tx_overruns,
2384 			    TCA_TAPRIO_OFFLOAD_STATS_TX_OVERRUNS))
2385 		goto err_cancel;
2386 
2387 	nla_nest_end(skb, xstats);
2388 
2389 	return 0;
2390 
2391 err_cancel:
2392 	nla_nest_cancel(skb, xstats);
2393 err:
2394 	return -EMSGSIZE;
2395 }
2396 
2397 static int taprio_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
2398 {
2399 	struct tc_taprio_qopt_offload offload = {
2400 		.cmd = TAPRIO_CMD_STATS,
2401 	};
2402 
2403 	return taprio_dump_xstats(sch, d, &offload, &offload.stats);
2404 }
2405 
2406 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
2407 {
2408 	struct taprio_sched *q = qdisc_priv(sch);
2409 	struct net_device *dev = qdisc_dev(sch);
2410 	struct sched_gate_list *oper, *admin;
2411 	struct tc_mqprio_qopt opt = { 0 };
2412 	struct nlattr *nest, *sched_nest;
2413 	u32 txtime_delay;
2414 
2415 	mqprio_qopt_reconstruct(dev, &opt);
2416 
2417 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
2418 	if (!nest)
2419 		goto start_error;
2420 
2421 	if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
2422 		goto options_error;
2423 
2424 	if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
2425 	    nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
2426 		goto options_error;
2427 
2428 	if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
2429 		goto options_error;
2430 
2431 	txtime_delay = READ_ONCE(q->txtime_delay);
2432 	if (txtime_delay &&
2433 	    nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, txtime_delay))
2434 		goto options_error;
2435 
2436 	rcu_read_lock();
2437 
2438 	oper = rcu_dereference(q->oper_sched);
2439 	admin = rcu_dereference(q->admin_sched);
2440 
2441 	if (oper && taprio_dump_tc_entries(skb, q, oper))
2442 		goto options_error_rcu;
2443 
2444 	if (oper && dump_schedule(skb, oper))
2445 		goto options_error_rcu;
2446 
2447 	if (!admin)
2448 		goto done;
2449 
2450 	sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
2451 	if (!sched_nest)
2452 		goto options_error_rcu;
2453 
2454 	if (dump_schedule(skb, admin))
2455 		goto admin_error;
2456 
2457 	nla_nest_end(skb, sched_nest);
2458 
2459 done:
2460 	rcu_read_unlock();
2461 	return nla_nest_end(skb, nest);
2462 
2463 admin_error:
2464 	nla_nest_cancel(skb, sched_nest);
2465 
2466 options_error_rcu:
2467 	rcu_read_unlock();
2468 
2469 options_error:
2470 	nla_nest_cancel(skb, nest);
2471 
2472 start_error:
2473 	return -ENOSPC;
2474 }
2475 
2476 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
2477 {
2478 	struct taprio_sched *q = qdisc_priv(sch);
2479 	struct net_device *dev = qdisc_dev(sch);
2480 	unsigned int ntx = cl - 1;
2481 
2482 	if (ntx >= dev->num_tx_queues)
2483 		return NULL;
2484 
2485 	return q->qdiscs[ntx];
2486 }
2487 
2488 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
2489 {
2490 	unsigned int ntx = TC_H_MIN(classid);
2491 
2492 	if (!taprio_queue_get(sch, ntx))
2493 		return 0;
2494 	return ntx;
2495 }
2496 
2497 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
2498 			     struct sk_buff *skb, struct tcmsg *tcm)
2499 {
2500 	struct Qdisc *child = taprio_leaf(sch, cl);
2501 
2502 	tcm->tcm_parent = TC_H_ROOT;
2503 	tcm->tcm_handle |= TC_H_MIN(cl);
2504 	tcm->tcm_info = child->handle;
2505 
2506 	return 0;
2507 }
2508 
2509 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
2510 				   struct gnet_dump *d)
2511 	__releases(d->lock)
2512 	__acquires(d->lock)
2513 {
2514 	struct Qdisc *child = taprio_leaf(sch, cl);
2515 	struct tc_taprio_qopt_offload offload = {
2516 		.cmd = TAPRIO_CMD_QUEUE_STATS,
2517 		.queue_stats = {
2518 			.queue = cl - 1,
2519 		},
2520 	};
2521 
2522 	if (gnet_stats_copy_basic(d, NULL, &child->bstats, true) < 0 ||
2523 	    qdisc_qstats_copy(d, child) < 0)
2524 		return -1;
2525 
2526 	return taprio_dump_xstats(sch, d, &offload, &offload.queue_stats.stats);
2527 }
2528 
2529 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
2530 {
2531 	struct net_device *dev = qdisc_dev(sch);
2532 	unsigned long ntx;
2533 
2534 	if (arg->stop)
2535 		return;
2536 
2537 	arg->count = arg->skip;
2538 	for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2539 		if (!tc_qdisc_stats_dump(sch, ntx + 1, arg))
2540 			break;
2541 	}
2542 }
2543 
2544 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2545 						struct tcmsg *tcm)
2546 {
2547 	return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2548 }
2549 
2550 static const struct Qdisc_class_ops taprio_class_ops = {
2551 	.graft		= taprio_graft,
2552 	.leaf		= taprio_leaf,
2553 	.find		= taprio_find,
2554 	.walk		= taprio_walk,
2555 	.dump		= taprio_dump_class,
2556 	.dump_stats	= taprio_dump_class_stats,
2557 	.select_queue	= taprio_select_queue,
2558 };
2559 
2560 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2561 	.cl_ops		= &taprio_class_ops,
2562 	.id		= "taprio",
2563 	.priv_size	= sizeof(struct taprio_sched),
2564 	.init		= taprio_init,
2565 	.change		= taprio_change,
2566 	.destroy	= taprio_destroy,
2567 	.reset		= taprio_reset,
2568 	.attach		= taprio_attach,
2569 	.peek		= taprio_peek,
2570 	.dequeue	= taprio_dequeue,
2571 	.enqueue	= taprio_enqueue,
2572 	.dump		= taprio_dump,
2573 	.dump_stats	= taprio_dump_stats,
2574 	.owner		= THIS_MODULE,
2575 };
2576 MODULE_ALIAS_NET_SCH("taprio");
2577 
2578 static struct notifier_block taprio_device_notifier = {
2579 	.notifier_call = taprio_dev_notifier,
2580 };
2581 
2582 static int __init taprio_module_init(void)
2583 {
2584 	int err = register_netdevice_notifier(&taprio_device_notifier);
2585 
2586 	if (err)
2587 		return err;
2588 
2589 	return register_qdisc(&taprio_qdisc_ops);
2590 }
2591 
2592 static void __exit taprio_module_exit(void)
2593 {
2594 	unregister_qdisc(&taprio_qdisc_ops);
2595 	unregister_netdevice_notifier(&taprio_device_notifier);
2596 }
2597 
2598 module_init(taprio_module_init);
2599 module_exit(taprio_module_exit);
2600 MODULE_LICENSE("GPL");
2601 MODULE_DESCRIPTION("Time Aware Priority qdisc");
2602