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