xref: /openbmc/linux/net/sched/sch_taprio.c (revision fe7498ef)
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/types.h>
11 #include <linux/slab.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/list.h>
15 #include <linux/errno.h>
16 #include <linux/skbuff.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
19 #include <linux/spinlock.h>
20 #include <linux/rcupdate.h>
21 #include <net/netlink.h>
22 #include <net/pkt_sched.h>
23 #include <net/pkt_cls.h>
24 #include <net/sch_generic.h>
25 #include <net/sock.h>
26 #include <net/tcp.h>
27 
28 static LIST_HEAD(taprio_list);
29 static DEFINE_SPINLOCK(taprio_list_lock);
30 
31 #define TAPRIO_ALL_GATES_OPEN -1
32 
33 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
34 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
35 #define TAPRIO_FLAGS_INVALID U32_MAX
36 
37 struct sched_entry {
38 	struct list_head list;
39 
40 	/* The instant that this entry "closes" and the next one
41 	 * should open, the qdisc will make some effort so that no
42 	 * packet leaves after this time.
43 	 */
44 	ktime_t close_time;
45 	ktime_t next_txtime;
46 	atomic_t budget;
47 	int index;
48 	u32 gate_mask;
49 	u32 interval;
50 	u8 command;
51 };
52 
53 struct sched_gate_list {
54 	struct rcu_head rcu;
55 	struct list_head entries;
56 	size_t num_entries;
57 	ktime_t cycle_close_time;
58 	s64 cycle_time;
59 	s64 cycle_time_extension;
60 	s64 base_time;
61 };
62 
63 struct taprio_sched {
64 	struct Qdisc **qdiscs;
65 	struct Qdisc *root;
66 	u32 flags;
67 	enum tk_offsets tk_offset;
68 	int clockid;
69 	atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+
70 				    * speeds it's sub-nanoseconds per byte
71 				    */
72 
73 	/* Protects the update side of the RCU protected current_entry */
74 	spinlock_t current_entry_lock;
75 	struct sched_entry __rcu *current_entry;
76 	struct sched_gate_list __rcu *oper_sched;
77 	struct sched_gate_list __rcu *admin_sched;
78 	struct hrtimer advance_timer;
79 	struct list_head taprio_list;
80 	struct sk_buff *(*dequeue)(struct Qdisc *sch);
81 	struct sk_buff *(*peek)(struct Qdisc *sch);
82 	u32 txtime_delay;
83 };
84 
85 struct __tc_taprio_qopt_offload {
86 	refcount_t users;
87 	struct tc_taprio_qopt_offload offload;
88 };
89 
90 static ktime_t sched_base_time(const struct sched_gate_list *sched)
91 {
92 	if (!sched)
93 		return KTIME_MAX;
94 
95 	return ns_to_ktime(sched->base_time);
96 }
97 
98 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
99 {
100 	/* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
101 	enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
102 
103 	switch (tk_offset) {
104 	case TK_OFFS_MAX:
105 		return mono;
106 	default:
107 		return ktime_mono_to_any(mono, tk_offset);
108 	}
109 }
110 
111 static ktime_t taprio_get_time(const struct taprio_sched *q)
112 {
113 	return taprio_mono_to_any(q, ktime_get());
114 }
115 
116 static void taprio_free_sched_cb(struct rcu_head *head)
117 {
118 	struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
119 	struct sched_entry *entry, *n;
120 
121 	list_for_each_entry_safe(entry, n, &sched->entries, list) {
122 		list_del(&entry->list);
123 		kfree(entry);
124 	}
125 
126 	kfree(sched);
127 }
128 
129 static void switch_schedules(struct taprio_sched *q,
130 			     struct sched_gate_list **admin,
131 			     struct sched_gate_list **oper)
132 {
133 	rcu_assign_pointer(q->oper_sched, *admin);
134 	rcu_assign_pointer(q->admin_sched, NULL);
135 
136 	if (*oper)
137 		call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
138 
139 	*oper = *admin;
140 	*admin = NULL;
141 }
142 
143 /* Get how much time has been already elapsed in the current cycle. */
144 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
145 {
146 	ktime_t time_since_sched_start;
147 	s32 time_elapsed;
148 
149 	time_since_sched_start = ktime_sub(time, sched->base_time);
150 	div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
151 
152 	return time_elapsed;
153 }
154 
155 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
156 				     struct sched_gate_list *admin,
157 				     struct sched_entry *entry,
158 				     ktime_t intv_start)
159 {
160 	s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
161 	ktime_t intv_end, cycle_ext_end, cycle_end;
162 
163 	cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
164 	intv_end = ktime_add_ns(intv_start, entry->interval);
165 	cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
166 
167 	if (ktime_before(intv_end, cycle_end))
168 		return intv_end;
169 	else if (admin && admin != sched &&
170 		 ktime_after(admin->base_time, cycle_end) &&
171 		 ktime_before(admin->base_time, cycle_ext_end))
172 		return admin->base_time;
173 	else
174 		return cycle_end;
175 }
176 
177 static int length_to_duration(struct taprio_sched *q, int len)
178 {
179 	return div_u64(len * atomic64_read(&q->picos_per_byte), 1000);
180 }
181 
182 /* Returns the entry corresponding to next available interval. If
183  * validate_interval is set, it only validates whether the timestamp occurs
184  * when the gate corresponding to the skb's traffic class is open.
185  */
186 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
187 						  struct Qdisc *sch,
188 						  struct sched_gate_list *sched,
189 						  struct sched_gate_list *admin,
190 						  ktime_t time,
191 						  ktime_t *interval_start,
192 						  ktime_t *interval_end,
193 						  bool validate_interval)
194 {
195 	ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
196 	ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
197 	struct sched_entry *entry = NULL, *entry_found = NULL;
198 	struct taprio_sched *q = qdisc_priv(sch);
199 	struct net_device *dev = qdisc_dev(sch);
200 	bool entry_available = false;
201 	s32 cycle_elapsed;
202 	int tc, n;
203 
204 	tc = netdev_get_prio_tc_map(dev, skb->priority);
205 	packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
206 
207 	*interval_start = 0;
208 	*interval_end = 0;
209 
210 	if (!sched)
211 		return NULL;
212 
213 	cycle = sched->cycle_time;
214 	cycle_elapsed = get_cycle_time_elapsed(sched, time);
215 	curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
216 	cycle_end = ktime_add_ns(curr_intv_end, cycle);
217 
218 	list_for_each_entry(entry, &sched->entries, list) {
219 		curr_intv_start = curr_intv_end;
220 		curr_intv_end = get_interval_end_time(sched, admin, entry,
221 						      curr_intv_start);
222 
223 		if (ktime_after(curr_intv_start, cycle_end))
224 			break;
225 
226 		if (!(entry->gate_mask & BIT(tc)) ||
227 		    packet_transmit_time > entry->interval)
228 			continue;
229 
230 		txtime = entry->next_txtime;
231 
232 		if (ktime_before(txtime, time) || validate_interval) {
233 			transmit_end_time = ktime_add_ns(time, packet_transmit_time);
234 			if ((ktime_before(curr_intv_start, time) &&
235 			     ktime_before(transmit_end_time, curr_intv_end)) ||
236 			    (ktime_after(curr_intv_start, time) && !validate_interval)) {
237 				entry_found = entry;
238 				*interval_start = curr_intv_start;
239 				*interval_end = curr_intv_end;
240 				break;
241 			} else if (!entry_available && !validate_interval) {
242 				/* Here, we are just trying to find out the
243 				 * first available interval in the next cycle.
244 				 */
245 				entry_available = true;
246 				entry_found = entry;
247 				*interval_start = ktime_add_ns(curr_intv_start, cycle);
248 				*interval_end = ktime_add_ns(curr_intv_end, cycle);
249 			}
250 		} else if (ktime_before(txtime, earliest_txtime) &&
251 			   !entry_available) {
252 			earliest_txtime = txtime;
253 			entry_found = entry;
254 			n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
255 			*interval_start = ktime_add(curr_intv_start, n * cycle);
256 			*interval_end = ktime_add(curr_intv_end, n * cycle);
257 		}
258 	}
259 
260 	return entry_found;
261 }
262 
263 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
264 {
265 	struct taprio_sched *q = qdisc_priv(sch);
266 	struct sched_gate_list *sched, *admin;
267 	ktime_t interval_start, interval_end;
268 	struct sched_entry *entry;
269 
270 	rcu_read_lock();
271 	sched = rcu_dereference(q->oper_sched);
272 	admin = rcu_dereference(q->admin_sched);
273 
274 	entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
275 				       &interval_start, &interval_end, true);
276 	rcu_read_unlock();
277 
278 	return entry;
279 }
280 
281 static bool taprio_flags_valid(u32 flags)
282 {
283 	/* Make sure no other flag bits are set. */
284 	if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
285 		      TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
286 		return false;
287 	/* txtime-assist and full offload are mutually exclusive */
288 	if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
289 	    (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
290 		return false;
291 	return true;
292 }
293 
294 /* This returns the tstamp value set by TCP in terms of the set clock. */
295 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
296 {
297 	unsigned int offset = skb_network_offset(skb);
298 	const struct ipv6hdr *ipv6h;
299 	const struct iphdr *iph;
300 	struct ipv6hdr _ipv6h;
301 
302 	ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
303 	if (!ipv6h)
304 		return 0;
305 
306 	if (ipv6h->version == 4) {
307 		iph = (struct iphdr *)ipv6h;
308 		offset += iph->ihl * 4;
309 
310 		/* special-case 6in4 tunnelling, as that is a common way to get
311 		 * v6 connectivity in the home
312 		 */
313 		if (iph->protocol == IPPROTO_IPV6) {
314 			ipv6h = skb_header_pointer(skb, offset,
315 						   sizeof(_ipv6h), &_ipv6h);
316 
317 			if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
318 				return 0;
319 		} else if (iph->protocol != IPPROTO_TCP) {
320 			return 0;
321 		}
322 	} else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
323 		return 0;
324 	}
325 
326 	return taprio_mono_to_any(q, skb->skb_mstamp_ns);
327 }
328 
329 /* There are a few scenarios where we will have to modify the txtime from
330  * what is read from next_txtime in sched_entry. They are:
331  * 1. If txtime is in the past,
332  *    a. The gate for the traffic class is currently open and packet can be
333  *       transmitted before it closes, schedule the packet right away.
334  *    b. If the gate corresponding to the traffic class is going to open later
335  *       in the cycle, set the txtime of packet to the interval start.
336  * 2. If txtime is in the future, there are packets corresponding to the
337  *    current traffic class waiting to be transmitted. So, the following
338  *    possibilities exist:
339  *    a. We can transmit the packet before the window containing the txtime
340  *       closes.
341  *    b. The window might close before the transmission can be completed
342  *       successfully. So, schedule the packet in the next open window.
343  */
344 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
345 {
346 	ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
347 	struct taprio_sched *q = qdisc_priv(sch);
348 	struct sched_gate_list *sched, *admin;
349 	ktime_t minimum_time, now, txtime;
350 	int len, packet_transmit_time;
351 	struct sched_entry *entry;
352 	bool sched_changed;
353 
354 	now = taprio_get_time(q);
355 	minimum_time = ktime_add_ns(now, q->txtime_delay);
356 
357 	tcp_tstamp = get_tcp_tstamp(q, skb);
358 	minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
359 
360 	rcu_read_lock();
361 	admin = rcu_dereference(q->admin_sched);
362 	sched = rcu_dereference(q->oper_sched);
363 	if (admin && ktime_after(minimum_time, admin->base_time))
364 		switch_schedules(q, &admin, &sched);
365 
366 	/* Until the schedule starts, all the queues are open */
367 	if (!sched || ktime_before(minimum_time, sched->base_time)) {
368 		txtime = minimum_time;
369 		goto done;
370 	}
371 
372 	len = qdisc_pkt_len(skb);
373 	packet_transmit_time = length_to_duration(q, len);
374 
375 	do {
376 		sched_changed = false;
377 
378 		entry = find_entry_to_transmit(skb, sch, sched, admin,
379 					       minimum_time,
380 					       &interval_start, &interval_end,
381 					       false);
382 		if (!entry) {
383 			txtime = 0;
384 			goto done;
385 		}
386 
387 		txtime = entry->next_txtime;
388 		txtime = max_t(ktime_t, txtime, minimum_time);
389 		txtime = max_t(ktime_t, txtime, interval_start);
390 
391 		if (admin && admin != sched &&
392 		    ktime_after(txtime, admin->base_time)) {
393 			sched = admin;
394 			sched_changed = true;
395 			continue;
396 		}
397 
398 		transmit_end_time = ktime_add(txtime, packet_transmit_time);
399 		minimum_time = transmit_end_time;
400 
401 		/* Update the txtime of current entry to the next time it's
402 		 * interval starts.
403 		 */
404 		if (ktime_after(transmit_end_time, interval_end))
405 			entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
406 	} while (sched_changed || ktime_after(transmit_end_time, interval_end));
407 
408 	entry->next_txtime = transmit_end_time;
409 
410 done:
411 	rcu_read_unlock();
412 	return txtime;
413 }
414 
415 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
416 			      struct Qdisc *child, struct sk_buff **to_free)
417 {
418 	struct taprio_sched *q = qdisc_priv(sch);
419 
420 	if (skb->sk && sock_flag(skb->sk, SOCK_TXTIME)) {
421 		if (!is_valid_interval(skb, sch))
422 			return qdisc_drop(skb, sch, to_free);
423 	} else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
424 		skb->tstamp = get_packet_txtime(skb, sch);
425 		if (!skb->tstamp)
426 			return qdisc_drop(skb, sch, to_free);
427 	}
428 
429 	qdisc_qstats_backlog_inc(sch, skb);
430 	sch->q.qlen++;
431 
432 	return qdisc_enqueue(skb, child, to_free);
433 }
434 
435 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
436 			  struct sk_buff **to_free)
437 {
438 	struct taprio_sched *q = qdisc_priv(sch);
439 	struct Qdisc *child;
440 	int queue;
441 
442 	if (unlikely(FULL_OFFLOAD_IS_ENABLED(q->flags))) {
443 		WARN_ONCE(1, "Trying to enqueue skb into the root of a taprio qdisc configured with full offload\n");
444 		return qdisc_drop(skb, sch, to_free);
445 	}
446 
447 	queue = skb_get_queue_mapping(skb);
448 
449 	child = q->qdiscs[queue];
450 	if (unlikely(!child))
451 		return qdisc_drop(skb, sch, to_free);
452 
453 	/* Large packets might not be transmitted when the transmission duration
454 	 * exceeds any configured interval. Therefore, segment the skb into
455 	 * smaller chunks. Skip it for the full offload case, as the driver
456 	 * and/or the hardware is expected to handle this.
457 	 */
458 	if (skb_is_gso(skb) && !FULL_OFFLOAD_IS_ENABLED(q->flags)) {
459 		unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
460 		netdev_features_t features = netif_skb_features(skb);
461 		struct sk_buff *segs, *nskb;
462 		int ret;
463 
464 		segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
465 		if (IS_ERR_OR_NULL(segs))
466 			return qdisc_drop(skb, sch, to_free);
467 
468 		skb_list_walk_safe(segs, segs, nskb) {
469 			skb_mark_not_on_list(segs);
470 			qdisc_skb_cb(segs)->pkt_len = segs->len;
471 			slen += segs->len;
472 
473 			ret = taprio_enqueue_one(segs, sch, child, to_free);
474 			if (ret != NET_XMIT_SUCCESS) {
475 				if (net_xmit_drop_count(ret))
476 					qdisc_qstats_drop(sch);
477 			} else {
478 				numsegs++;
479 			}
480 		}
481 
482 		if (numsegs > 1)
483 			qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
484 		consume_skb(skb);
485 
486 		return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
487 	}
488 
489 	return taprio_enqueue_one(skb, sch, child, to_free);
490 }
491 
492 static struct sk_buff *taprio_peek_soft(struct Qdisc *sch)
493 {
494 	struct taprio_sched *q = qdisc_priv(sch);
495 	struct net_device *dev = qdisc_dev(sch);
496 	struct sched_entry *entry;
497 	struct sk_buff *skb;
498 	u32 gate_mask;
499 	int i;
500 
501 	rcu_read_lock();
502 	entry = rcu_dereference(q->current_entry);
503 	gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
504 	rcu_read_unlock();
505 
506 	if (!gate_mask)
507 		return NULL;
508 
509 	for (i = 0; i < dev->num_tx_queues; i++) {
510 		struct Qdisc *child = q->qdiscs[i];
511 		int prio;
512 		u8 tc;
513 
514 		if (unlikely(!child))
515 			continue;
516 
517 		skb = child->ops->peek(child);
518 		if (!skb)
519 			continue;
520 
521 		if (TXTIME_ASSIST_IS_ENABLED(q->flags))
522 			return skb;
523 
524 		prio = skb->priority;
525 		tc = netdev_get_prio_tc_map(dev, prio);
526 
527 		if (!(gate_mask & BIT(tc)))
528 			continue;
529 
530 		return skb;
531 	}
532 
533 	return NULL;
534 }
535 
536 static struct sk_buff *taprio_peek_offload(struct Qdisc *sch)
537 {
538 	WARN_ONCE(1, "Trying to peek into the root of a taprio qdisc configured with full offload\n");
539 
540 	return NULL;
541 }
542 
543 static struct sk_buff *taprio_peek(struct Qdisc *sch)
544 {
545 	struct taprio_sched *q = qdisc_priv(sch);
546 
547 	return q->peek(sch);
548 }
549 
550 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
551 {
552 	atomic_set(&entry->budget,
553 		   div64_u64((u64)entry->interval * 1000,
554 			     atomic64_read(&q->picos_per_byte)));
555 }
556 
557 static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch)
558 {
559 	struct taprio_sched *q = qdisc_priv(sch);
560 	struct net_device *dev = qdisc_dev(sch);
561 	struct sk_buff *skb = NULL;
562 	struct sched_entry *entry;
563 	u32 gate_mask;
564 	int i;
565 
566 	rcu_read_lock();
567 	entry = rcu_dereference(q->current_entry);
568 	/* if there's no entry, it means that the schedule didn't
569 	 * start yet, so force all gates to be open, this is in
570 	 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5
571 	 * "AdminGateStates"
572 	 */
573 	gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
574 
575 	if (!gate_mask)
576 		goto done;
577 
578 	for (i = 0; i < dev->num_tx_queues; i++) {
579 		struct Qdisc *child = q->qdiscs[i];
580 		ktime_t guard;
581 		int prio;
582 		int len;
583 		u8 tc;
584 
585 		if (unlikely(!child))
586 			continue;
587 
588 		if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
589 			skb = child->ops->dequeue(child);
590 			if (!skb)
591 				continue;
592 			goto skb_found;
593 		}
594 
595 		skb = child->ops->peek(child);
596 		if (!skb)
597 			continue;
598 
599 		prio = skb->priority;
600 		tc = netdev_get_prio_tc_map(dev, prio);
601 
602 		if (!(gate_mask & BIT(tc))) {
603 			skb = NULL;
604 			continue;
605 		}
606 
607 		len = qdisc_pkt_len(skb);
608 		guard = ktime_add_ns(taprio_get_time(q),
609 				     length_to_duration(q, len));
610 
611 		/* In the case that there's no gate entry, there's no
612 		 * guard band ...
613 		 */
614 		if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
615 		    ktime_after(guard, entry->close_time)) {
616 			skb = NULL;
617 			continue;
618 		}
619 
620 		/* ... and no budget. */
621 		if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
622 		    atomic_sub_return(len, &entry->budget) < 0) {
623 			skb = NULL;
624 			continue;
625 		}
626 
627 		skb = child->ops->dequeue(child);
628 		if (unlikely(!skb))
629 			goto done;
630 
631 skb_found:
632 		qdisc_bstats_update(sch, skb);
633 		qdisc_qstats_backlog_dec(sch, skb);
634 		sch->q.qlen--;
635 
636 		goto done;
637 	}
638 
639 done:
640 	rcu_read_unlock();
641 
642 	return skb;
643 }
644 
645 static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch)
646 {
647 	WARN_ONCE(1, "Trying to dequeue from the root of a taprio qdisc configured with full offload\n");
648 
649 	return NULL;
650 }
651 
652 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
653 {
654 	struct taprio_sched *q = qdisc_priv(sch);
655 
656 	return q->dequeue(sch);
657 }
658 
659 static bool should_restart_cycle(const struct sched_gate_list *oper,
660 				 const struct sched_entry *entry)
661 {
662 	if (list_is_last(&entry->list, &oper->entries))
663 		return true;
664 
665 	if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
666 		return true;
667 
668 	return false;
669 }
670 
671 static bool should_change_schedules(const struct sched_gate_list *admin,
672 				    const struct sched_gate_list *oper,
673 				    ktime_t close_time)
674 {
675 	ktime_t next_base_time, extension_time;
676 
677 	if (!admin)
678 		return false;
679 
680 	next_base_time = sched_base_time(admin);
681 
682 	/* This is the simple case, the close_time would fall after
683 	 * the next schedule base_time.
684 	 */
685 	if (ktime_compare(next_base_time, close_time) <= 0)
686 		return true;
687 
688 	/* This is the cycle_time_extension case, if the close_time
689 	 * plus the amount that can be extended would fall after the
690 	 * next schedule base_time, we can extend the current schedule
691 	 * for that amount.
692 	 */
693 	extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
694 
695 	/* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about
696 	 * how precisely the extension should be made. So after
697 	 * conformance testing, this logic may change.
698 	 */
699 	if (ktime_compare(next_base_time, extension_time) <= 0)
700 		return true;
701 
702 	return false;
703 }
704 
705 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
706 {
707 	struct taprio_sched *q = container_of(timer, struct taprio_sched,
708 					      advance_timer);
709 	struct sched_gate_list *oper, *admin;
710 	struct sched_entry *entry, *next;
711 	struct Qdisc *sch = q->root;
712 	ktime_t close_time;
713 
714 	spin_lock(&q->current_entry_lock);
715 	entry = rcu_dereference_protected(q->current_entry,
716 					  lockdep_is_held(&q->current_entry_lock));
717 	oper = rcu_dereference_protected(q->oper_sched,
718 					 lockdep_is_held(&q->current_entry_lock));
719 	admin = rcu_dereference_protected(q->admin_sched,
720 					  lockdep_is_held(&q->current_entry_lock));
721 
722 	if (!oper)
723 		switch_schedules(q, &admin, &oper);
724 
725 	/* This can happen in two cases: 1. this is the very first run
726 	 * of this function (i.e. we weren't running any schedule
727 	 * previously); 2. The previous schedule just ended. The first
728 	 * entry of all schedules are pre-calculated during the
729 	 * schedule initialization.
730 	 */
731 	if (unlikely(!entry || entry->close_time == oper->base_time)) {
732 		next = list_first_entry(&oper->entries, struct sched_entry,
733 					list);
734 		close_time = next->close_time;
735 		goto first_run;
736 	}
737 
738 	if (should_restart_cycle(oper, entry)) {
739 		next = list_first_entry(&oper->entries, struct sched_entry,
740 					list);
741 		oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
742 						      oper->cycle_time);
743 	} else {
744 		next = list_next_entry(entry, list);
745 	}
746 
747 	close_time = ktime_add_ns(entry->close_time, next->interval);
748 	close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
749 
750 	if (should_change_schedules(admin, oper, close_time)) {
751 		/* Set things so the next time this runs, the new
752 		 * schedule runs.
753 		 */
754 		close_time = sched_base_time(admin);
755 		switch_schedules(q, &admin, &oper);
756 	}
757 
758 	next->close_time = close_time;
759 	taprio_set_budget(q, next);
760 
761 first_run:
762 	rcu_assign_pointer(q->current_entry, next);
763 	spin_unlock(&q->current_entry_lock);
764 
765 	hrtimer_set_expires(&q->advance_timer, close_time);
766 
767 	rcu_read_lock();
768 	__netif_schedule(sch);
769 	rcu_read_unlock();
770 
771 	return HRTIMER_RESTART;
772 }
773 
774 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
775 	[TCA_TAPRIO_SCHED_ENTRY_INDEX]	   = { .type = NLA_U32 },
776 	[TCA_TAPRIO_SCHED_ENTRY_CMD]	   = { .type = NLA_U8 },
777 	[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
778 	[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]  = { .type = NLA_U32 },
779 };
780 
781 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
782 	[TCA_TAPRIO_ATTR_PRIOMAP]	       = {
783 		.len = sizeof(struct tc_mqprio_qopt)
784 	},
785 	[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]           = { .type = NLA_NESTED },
786 	[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]            = { .type = NLA_S64 },
787 	[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]         = { .type = NLA_NESTED },
788 	[TCA_TAPRIO_ATTR_SCHED_CLOCKID]              = { .type = NLA_S32 },
789 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]           = { .type = NLA_S64 },
790 	[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
791 	[TCA_TAPRIO_ATTR_FLAGS]                      = { .type = NLA_U32 },
792 	[TCA_TAPRIO_ATTR_TXTIME_DELAY]		     = { .type = NLA_U32 },
793 };
794 
795 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
796 			    struct sched_entry *entry,
797 			    struct netlink_ext_ack *extack)
798 {
799 	int min_duration = length_to_duration(q, ETH_ZLEN);
800 	u32 interval = 0;
801 
802 	if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
803 		entry->command = nla_get_u8(
804 			tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
805 
806 	if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
807 		entry->gate_mask = nla_get_u32(
808 			tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
809 
810 	if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
811 		interval = nla_get_u32(
812 			tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
813 
814 	/* The interval should allow at least the minimum ethernet
815 	 * frame to go out.
816 	 */
817 	if (interval < min_duration) {
818 		NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
819 		return -EINVAL;
820 	}
821 
822 	entry->interval = interval;
823 
824 	return 0;
825 }
826 
827 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
828 			     struct sched_entry *entry, int index,
829 			     struct netlink_ext_ack *extack)
830 {
831 	struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
832 	int err;
833 
834 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
835 					  entry_policy, NULL);
836 	if (err < 0) {
837 		NL_SET_ERR_MSG(extack, "Could not parse nested entry");
838 		return -EINVAL;
839 	}
840 
841 	entry->index = index;
842 
843 	return fill_sched_entry(q, tb, entry, extack);
844 }
845 
846 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
847 			    struct sched_gate_list *sched,
848 			    struct netlink_ext_ack *extack)
849 {
850 	struct nlattr *n;
851 	int err, rem;
852 	int i = 0;
853 
854 	if (!list)
855 		return -EINVAL;
856 
857 	nla_for_each_nested(n, list, rem) {
858 		struct sched_entry *entry;
859 
860 		if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
861 			NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
862 			continue;
863 		}
864 
865 		entry = kzalloc(sizeof(*entry), GFP_KERNEL);
866 		if (!entry) {
867 			NL_SET_ERR_MSG(extack, "Not enough memory for entry");
868 			return -ENOMEM;
869 		}
870 
871 		err = parse_sched_entry(q, n, entry, i, extack);
872 		if (err < 0) {
873 			kfree(entry);
874 			return err;
875 		}
876 
877 		list_add_tail(&entry->list, &sched->entries);
878 		i++;
879 	}
880 
881 	sched->num_entries = i;
882 
883 	return i;
884 }
885 
886 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
887 				 struct sched_gate_list *new,
888 				 struct netlink_ext_ack *extack)
889 {
890 	int err = 0;
891 
892 	if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
893 		NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
894 		return -ENOTSUPP;
895 	}
896 
897 	if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
898 		new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
899 
900 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
901 		new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
902 
903 	if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
904 		new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
905 
906 	if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
907 		err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
908 				       new, extack);
909 	if (err < 0)
910 		return err;
911 
912 	if (!new->cycle_time) {
913 		struct sched_entry *entry;
914 		ktime_t cycle = 0;
915 
916 		list_for_each_entry(entry, &new->entries, list)
917 			cycle = ktime_add_ns(cycle, entry->interval);
918 
919 		if (!cycle) {
920 			NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
921 			return -EINVAL;
922 		}
923 
924 		new->cycle_time = cycle;
925 	}
926 
927 	return 0;
928 }
929 
930 static int taprio_parse_mqprio_opt(struct net_device *dev,
931 				   struct tc_mqprio_qopt *qopt,
932 				   struct netlink_ext_ack *extack,
933 				   u32 taprio_flags)
934 {
935 	int i, j;
936 
937 	if (!qopt && !dev->num_tc) {
938 		NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
939 		return -EINVAL;
940 	}
941 
942 	/* If num_tc is already set, it means that the user already
943 	 * configured the mqprio part
944 	 */
945 	if (dev->num_tc)
946 		return 0;
947 
948 	/* Verify num_tc is not out of max range */
949 	if (qopt->num_tc > TC_MAX_QUEUE) {
950 		NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
951 		return -EINVAL;
952 	}
953 
954 	/* taprio imposes that traffic classes map 1:n to tx queues */
955 	if (qopt->num_tc > dev->num_tx_queues) {
956 		NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
957 		return -EINVAL;
958 	}
959 
960 	/* Verify priority mapping uses valid tcs */
961 	for (i = 0; i <= TC_BITMASK; i++) {
962 		if (qopt->prio_tc_map[i] >= qopt->num_tc) {
963 			NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
964 			return -EINVAL;
965 		}
966 	}
967 
968 	for (i = 0; i < qopt->num_tc; i++) {
969 		unsigned int last = qopt->offset[i] + qopt->count[i];
970 
971 		/* Verify the queue count is in tx range being equal to the
972 		 * real_num_tx_queues indicates the last queue is in use.
973 		 */
974 		if (qopt->offset[i] >= dev->num_tx_queues ||
975 		    !qopt->count[i] ||
976 		    last > dev->real_num_tx_queues) {
977 			NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
978 			return -EINVAL;
979 		}
980 
981 		if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
982 			continue;
983 
984 		/* Verify that the offset and counts do not overlap */
985 		for (j = i + 1; j < qopt->num_tc; j++) {
986 			if (last > qopt->offset[j]) {
987 				NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
988 				return -EINVAL;
989 			}
990 		}
991 	}
992 
993 	return 0;
994 }
995 
996 static int taprio_get_start_time(struct Qdisc *sch,
997 				 struct sched_gate_list *sched,
998 				 ktime_t *start)
999 {
1000 	struct taprio_sched *q = qdisc_priv(sch);
1001 	ktime_t now, base, cycle;
1002 	s64 n;
1003 
1004 	base = sched_base_time(sched);
1005 	now = taprio_get_time(q);
1006 
1007 	if (ktime_after(base, now)) {
1008 		*start = base;
1009 		return 0;
1010 	}
1011 
1012 	cycle = sched->cycle_time;
1013 
1014 	/* The qdisc is expected to have at least one sched_entry.  Moreover,
1015 	 * any entry must have 'interval' > 0. Thus if the cycle time is zero,
1016 	 * something went really wrong. In that case, we should warn about this
1017 	 * inconsistent state and return error.
1018 	 */
1019 	if (WARN_ON(!cycle))
1020 		return -EFAULT;
1021 
1022 	/* Schedule the start time for the beginning of the next
1023 	 * cycle.
1024 	 */
1025 	n = div64_s64(ktime_sub_ns(now, base), cycle);
1026 	*start = ktime_add_ns(base, (n + 1) * cycle);
1027 	return 0;
1028 }
1029 
1030 static void setup_first_close_time(struct taprio_sched *q,
1031 				   struct sched_gate_list *sched, ktime_t base)
1032 {
1033 	struct sched_entry *first;
1034 	ktime_t cycle;
1035 
1036 	first = list_first_entry(&sched->entries,
1037 				 struct sched_entry, list);
1038 
1039 	cycle = sched->cycle_time;
1040 
1041 	/* FIXME: find a better place to do this */
1042 	sched->cycle_close_time = ktime_add_ns(base, cycle);
1043 
1044 	first->close_time = ktime_add_ns(base, first->interval);
1045 	taprio_set_budget(q, first);
1046 	rcu_assign_pointer(q->current_entry, NULL);
1047 }
1048 
1049 static void taprio_start_sched(struct Qdisc *sch,
1050 			       ktime_t start, struct sched_gate_list *new)
1051 {
1052 	struct taprio_sched *q = qdisc_priv(sch);
1053 	ktime_t expires;
1054 
1055 	if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1056 		return;
1057 
1058 	expires = hrtimer_get_expires(&q->advance_timer);
1059 	if (expires == 0)
1060 		expires = KTIME_MAX;
1061 
1062 	/* If the new schedule starts before the next expiration, we
1063 	 * reprogram it to the earliest one, so we change the admin
1064 	 * schedule to the operational one at the right time.
1065 	 */
1066 	start = min_t(ktime_t, start, expires);
1067 
1068 	hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1069 }
1070 
1071 static void taprio_set_picos_per_byte(struct net_device *dev,
1072 				      struct taprio_sched *q)
1073 {
1074 	struct ethtool_link_ksettings ecmd;
1075 	int speed = SPEED_10;
1076 	int picos_per_byte;
1077 	int err;
1078 
1079 	err = __ethtool_get_link_ksettings(dev, &ecmd);
1080 	if (err < 0)
1081 		goto skip;
1082 
1083 	if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1084 		speed = ecmd.base.speed;
1085 
1086 skip:
1087 	picos_per_byte = (USEC_PER_SEC * 8) / speed;
1088 
1089 	atomic64_set(&q->picos_per_byte, picos_per_byte);
1090 	netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1091 		   dev->name, (long long)atomic64_read(&q->picos_per_byte),
1092 		   ecmd.base.speed);
1093 }
1094 
1095 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1096 			       void *ptr)
1097 {
1098 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1099 	struct net_device *qdev;
1100 	struct taprio_sched *q;
1101 	bool found = false;
1102 
1103 	ASSERT_RTNL();
1104 
1105 	if (event != NETDEV_UP && event != NETDEV_CHANGE)
1106 		return NOTIFY_DONE;
1107 
1108 	spin_lock(&taprio_list_lock);
1109 	list_for_each_entry(q, &taprio_list, taprio_list) {
1110 		qdev = qdisc_dev(q->root);
1111 		if (qdev == dev) {
1112 			found = true;
1113 			break;
1114 		}
1115 	}
1116 	spin_unlock(&taprio_list_lock);
1117 
1118 	if (found)
1119 		taprio_set_picos_per_byte(dev, q);
1120 
1121 	return NOTIFY_DONE;
1122 }
1123 
1124 static void setup_txtime(struct taprio_sched *q,
1125 			 struct sched_gate_list *sched, ktime_t base)
1126 {
1127 	struct sched_entry *entry;
1128 	u32 interval = 0;
1129 
1130 	list_for_each_entry(entry, &sched->entries, list) {
1131 		entry->next_txtime = ktime_add_ns(base, interval);
1132 		interval += entry->interval;
1133 	}
1134 }
1135 
1136 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1137 {
1138 	struct __tc_taprio_qopt_offload *__offload;
1139 
1140 	__offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1141 			    GFP_KERNEL);
1142 	if (!__offload)
1143 		return NULL;
1144 
1145 	refcount_set(&__offload->users, 1);
1146 
1147 	return &__offload->offload;
1148 }
1149 
1150 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1151 						  *offload)
1152 {
1153 	struct __tc_taprio_qopt_offload *__offload;
1154 
1155 	__offload = container_of(offload, struct __tc_taprio_qopt_offload,
1156 				 offload);
1157 
1158 	refcount_inc(&__offload->users);
1159 
1160 	return offload;
1161 }
1162 EXPORT_SYMBOL_GPL(taprio_offload_get);
1163 
1164 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1165 {
1166 	struct __tc_taprio_qopt_offload *__offload;
1167 
1168 	__offload = container_of(offload, struct __tc_taprio_qopt_offload,
1169 				 offload);
1170 
1171 	if (!refcount_dec_and_test(&__offload->users))
1172 		return;
1173 
1174 	kfree(__offload);
1175 }
1176 EXPORT_SYMBOL_GPL(taprio_offload_free);
1177 
1178 /* The function will only serve to keep the pointers to the "oper" and "admin"
1179  * schedules valid in relation to their base times, so when calling dump() the
1180  * users looks at the right schedules.
1181  * When using full offload, the admin configuration is promoted to oper at the
1182  * base_time in the PHC time domain.  But because the system time is not
1183  * necessarily in sync with that, we can't just trigger a hrtimer to call
1184  * switch_schedules at the right hardware time.
1185  * At the moment we call this by hand right away from taprio, but in the future
1186  * it will be useful to create a mechanism for drivers to notify taprio of the
1187  * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump().
1188  * This is left as TODO.
1189  */
1190 static void taprio_offload_config_changed(struct taprio_sched *q)
1191 {
1192 	struct sched_gate_list *oper, *admin;
1193 
1194 	spin_lock(&q->current_entry_lock);
1195 
1196 	oper = rcu_dereference_protected(q->oper_sched,
1197 					 lockdep_is_held(&q->current_entry_lock));
1198 	admin = rcu_dereference_protected(q->admin_sched,
1199 					  lockdep_is_held(&q->current_entry_lock));
1200 
1201 	switch_schedules(q, &admin, &oper);
1202 
1203 	spin_unlock(&q->current_entry_lock);
1204 }
1205 
1206 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1207 {
1208 	u32 i, queue_mask = 0;
1209 
1210 	for (i = 0; i < dev->num_tc; i++) {
1211 		u32 offset, count;
1212 
1213 		if (!(tc_mask & BIT(i)))
1214 			continue;
1215 
1216 		offset = dev->tc_to_txq[i].offset;
1217 		count = dev->tc_to_txq[i].count;
1218 
1219 		queue_mask |= GENMASK(offset + count - 1, offset);
1220 	}
1221 
1222 	return queue_mask;
1223 }
1224 
1225 static void taprio_sched_to_offload(struct net_device *dev,
1226 				    struct sched_gate_list *sched,
1227 				    struct tc_taprio_qopt_offload *offload)
1228 {
1229 	struct sched_entry *entry;
1230 	int i = 0;
1231 
1232 	offload->base_time = sched->base_time;
1233 	offload->cycle_time = sched->cycle_time;
1234 	offload->cycle_time_extension = sched->cycle_time_extension;
1235 
1236 	list_for_each_entry(entry, &sched->entries, list) {
1237 		struct tc_taprio_sched_entry *e = &offload->entries[i];
1238 
1239 		e->command = entry->command;
1240 		e->interval = entry->interval;
1241 		e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1242 
1243 		i++;
1244 	}
1245 
1246 	offload->num_entries = i;
1247 }
1248 
1249 static int taprio_enable_offload(struct net_device *dev,
1250 				 struct taprio_sched *q,
1251 				 struct sched_gate_list *sched,
1252 				 struct netlink_ext_ack *extack)
1253 {
1254 	const struct net_device_ops *ops = dev->netdev_ops;
1255 	struct tc_taprio_qopt_offload *offload;
1256 	int err = 0;
1257 
1258 	if (!ops->ndo_setup_tc) {
1259 		NL_SET_ERR_MSG(extack,
1260 			       "Device does not support taprio offload");
1261 		return -EOPNOTSUPP;
1262 	}
1263 
1264 	offload = taprio_offload_alloc(sched->num_entries);
1265 	if (!offload) {
1266 		NL_SET_ERR_MSG(extack,
1267 			       "Not enough memory for enabling offload mode");
1268 		return -ENOMEM;
1269 	}
1270 	offload->enable = 1;
1271 	taprio_sched_to_offload(dev, sched, offload);
1272 
1273 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1274 	if (err < 0) {
1275 		NL_SET_ERR_MSG(extack,
1276 			       "Device failed to setup taprio offload");
1277 		goto done;
1278 	}
1279 
1280 done:
1281 	taprio_offload_free(offload);
1282 
1283 	return err;
1284 }
1285 
1286 static int taprio_disable_offload(struct net_device *dev,
1287 				  struct taprio_sched *q,
1288 				  struct netlink_ext_ack *extack)
1289 {
1290 	const struct net_device_ops *ops = dev->netdev_ops;
1291 	struct tc_taprio_qopt_offload *offload;
1292 	int err;
1293 
1294 	if (!FULL_OFFLOAD_IS_ENABLED(q->flags))
1295 		return 0;
1296 
1297 	if (!ops->ndo_setup_tc)
1298 		return -EOPNOTSUPP;
1299 
1300 	offload = taprio_offload_alloc(0);
1301 	if (!offload) {
1302 		NL_SET_ERR_MSG(extack,
1303 			       "Not enough memory to disable offload mode");
1304 		return -ENOMEM;
1305 	}
1306 	offload->enable = 0;
1307 
1308 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1309 	if (err < 0) {
1310 		NL_SET_ERR_MSG(extack,
1311 			       "Device failed to disable offload");
1312 		goto out;
1313 	}
1314 
1315 out:
1316 	taprio_offload_free(offload);
1317 
1318 	return err;
1319 }
1320 
1321 /* If full offload is enabled, the only possible clockid is the net device's
1322  * PHC. For that reason, specifying a clockid through netlink is incorrect.
1323  * For txtime-assist, it is implicitly assumed that the device's PHC is kept
1324  * in sync with the specified clockid via a user space daemon such as phc2sys.
1325  * For both software taprio and txtime-assist, the clockid is used for the
1326  * hrtimer that advances the schedule and hence mandatory.
1327  */
1328 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1329 				struct netlink_ext_ack *extack)
1330 {
1331 	struct taprio_sched *q = qdisc_priv(sch);
1332 	struct net_device *dev = qdisc_dev(sch);
1333 	int err = -EINVAL;
1334 
1335 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1336 		const struct ethtool_ops *ops = dev->ethtool_ops;
1337 		struct ethtool_ts_info info = {
1338 			.cmd = ETHTOOL_GET_TS_INFO,
1339 			.phc_index = -1,
1340 		};
1341 
1342 		if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1343 			NL_SET_ERR_MSG(extack,
1344 				       "The 'clockid' cannot be specified for full offload");
1345 			goto out;
1346 		}
1347 
1348 		if (ops && ops->get_ts_info)
1349 			err = ops->get_ts_info(dev, &info);
1350 
1351 		if (err || info.phc_index < 0) {
1352 			NL_SET_ERR_MSG(extack,
1353 				       "Device does not have a PTP clock");
1354 			err = -ENOTSUPP;
1355 			goto out;
1356 		}
1357 	} else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1358 		int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1359 		enum tk_offsets tk_offset;
1360 
1361 		/* We only support static clockids and we don't allow
1362 		 * for it to be modified after the first init.
1363 		 */
1364 		if (clockid < 0 ||
1365 		    (q->clockid != -1 && q->clockid != clockid)) {
1366 			NL_SET_ERR_MSG(extack,
1367 				       "Changing the 'clockid' of a running schedule is not supported");
1368 			err = -ENOTSUPP;
1369 			goto out;
1370 		}
1371 
1372 		switch (clockid) {
1373 		case CLOCK_REALTIME:
1374 			tk_offset = TK_OFFS_REAL;
1375 			break;
1376 		case CLOCK_MONOTONIC:
1377 			tk_offset = TK_OFFS_MAX;
1378 			break;
1379 		case CLOCK_BOOTTIME:
1380 			tk_offset = TK_OFFS_BOOT;
1381 			break;
1382 		case CLOCK_TAI:
1383 			tk_offset = TK_OFFS_TAI;
1384 			break;
1385 		default:
1386 			NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1387 			err = -EINVAL;
1388 			goto out;
1389 		}
1390 		/* This pairs with READ_ONCE() in taprio_mono_to_any */
1391 		WRITE_ONCE(q->tk_offset, tk_offset);
1392 
1393 		q->clockid = clockid;
1394 	} else {
1395 		NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1396 		goto out;
1397 	}
1398 
1399 	/* Everything went ok, return success. */
1400 	err = 0;
1401 
1402 out:
1403 	return err;
1404 }
1405 
1406 static int taprio_mqprio_cmp(const struct net_device *dev,
1407 			     const struct tc_mqprio_qopt *mqprio)
1408 {
1409 	int i;
1410 
1411 	if (!mqprio || mqprio->num_tc != dev->num_tc)
1412 		return -1;
1413 
1414 	for (i = 0; i < mqprio->num_tc; i++)
1415 		if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1416 		    dev->tc_to_txq[i].offset != mqprio->offset[i])
1417 			return -1;
1418 
1419 	for (i = 0; i <= TC_BITMASK; i++)
1420 		if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1421 			return -1;
1422 
1423 	return 0;
1424 }
1425 
1426 /* The semantics of the 'flags' argument in relation to 'change()'
1427  * requests, are interpreted following two rules (which are applied in
1428  * this order): (1) an omitted 'flags' argument is interpreted as
1429  * zero; (2) the 'flags' of a "running" taprio instance cannot be
1430  * changed.
1431  */
1432 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1433 			    struct netlink_ext_ack *extack)
1434 {
1435 	u32 new = 0;
1436 
1437 	if (attr)
1438 		new = nla_get_u32(attr);
1439 
1440 	if (old != TAPRIO_FLAGS_INVALID && old != new) {
1441 		NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1442 		return -EOPNOTSUPP;
1443 	}
1444 
1445 	if (!taprio_flags_valid(new)) {
1446 		NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1447 		return -EINVAL;
1448 	}
1449 
1450 	return new;
1451 }
1452 
1453 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1454 			 struct netlink_ext_ack *extack)
1455 {
1456 	struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1457 	struct sched_gate_list *oper, *admin, *new_admin;
1458 	struct taprio_sched *q = qdisc_priv(sch);
1459 	struct net_device *dev = qdisc_dev(sch);
1460 	struct tc_mqprio_qopt *mqprio = NULL;
1461 	unsigned long flags;
1462 	ktime_t start;
1463 	int i, err;
1464 
1465 	err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1466 					  taprio_policy, extack);
1467 	if (err < 0)
1468 		return err;
1469 
1470 	if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1471 		mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1472 
1473 	err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1474 			       q->flags, extack);
1475 	if (err < 0)
1476 		return err;
1477 
1478 	q->flags = err;
1479 
1480 	err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1481 	if (err < 0)
1482 		return err;
1483 
1484 	new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1485 	if (!new_admin) {
1486 		NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1487 		return -ENOMEM;
1488 	}
1489 	INIT_LIST_HEAD(&new_admin->entries);
1490 
1491 	rcu_read_lock();
1492 	oper = rcu_dereference(q->oper_sched);
1493 	admin = rcu_dereference(q->admin_sched);
1494 	rcu_read_unlock();
1495 
1496 	/* no changes - no new mqprio settings */
1497 	if (!taprio_mqprio_cmp(dev, mqprio))
1498 		mqprio = NULL;
1499 
1500 	if (mqprio && (oper || admin)) {
1501 		NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1502 		err = -ENOTSUPP;
1503 		goto free_sched;
1504 	}
1505 
1506 	err = parse_taprio_schedule(q, tb, new_admin, extack);
1507 	if (err < 0)
1508 		goto free_sched;
1509 
1510 	if (new_admin->num_entries == 0) {
1511 		NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1512 		err = -EINVAL;
1513 		goto free_sched;
1514 	}
1515 
1516 	err = taprio_parse_clockid(sch, tb, extack);
1517 	if (err < 0)
1518 		goto free_sched;
1519 
1520 	taprio_set_picos_per_byte(dev, q);
1521 
1522 	if (mqprio) {
1523 		err = netdev_set_num_tc(dev, mqprio->num_tc);
1524 		if (err)
1525 			goto free_sched;
1526 		for (i = 0; i < mqprio->num_tc; i++)
1527 			netdev_set_tc_queue(dev, i,
1528 					    mqprio->count[i],
1529 					    mqprio->offset[i]);
1530 
1531 		/* Always use supplied priority mappings */
1532 		for (i = 0; i <= TC_BITMASK; i++)
1533 			netdev_set_prio_tc_map(dev, i,
1534 					       mqprio->prio_tc_map[i]);
1535 	}
1536 
1537 	if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1538 		err = taprio_enable_offload(dev, q, new_admin, extack);
1539 	else
1540 		err = taprio_disable_offload(dev, q, extack);
1541 	if (err)
1542 		goto free_sched;
1543 
1544 	/* Protects against enqueue()/dequeue() */
1545 	spin_lock_bh(qdisc_lock(sch));
1546 
1547 	if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1548 		if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1549 			NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1550 			err = -EINVAL;
1551 			goto unlock;
1552 		}
1553 
1554 		q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1555 	}
1556 
1557 	if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1558 	    !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1559 	    !hrtimer_active(&q->advance_timer)) {
1560 		hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1561 		q->advance_timer.function = advance_sched;
1562 	}
1563 
1564 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1565 		q->dequeue = taprio_dequeue_offload;
1566 		q->peek = taprio_peek_offload;
1567 	} else {
1568 		/* Be sure to always keep the function pointers
1569 		 * in a consistent state.
1570 		 */
1571 		q->dequeue = taprio_dequeue_soft;
1572 		q->peek = taprio_peek_soft;
1573 	}
1574 
1575 	err = taprio_get_start_time(sch, new_admin, &start);
1576 	if (err < 0) {
1577 		NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1578 		goto unlock;
1579 	}
1580 
1581 	setup_txtime(q, new_admin, start);
1582 
1583 	if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1584 		if (!oper) {
1585 			rcu_assign_pointer(q->oper_sched, new_admin);
1586 			err = 0;
1587 			new_admin = NULL;
1588 			goto unlock;
1589 		}
1590 
1591 		rcu_assign_pointer(q->admin_sched, new_admin);
1592 		if (admin)
1593 			call_rcu(&admin->rcu, taprio_free_sched_cb);
1594 	} else {
1595 		setup_first_close_time(q, new_admin, start);
1596 
1597 		/* Protects against advance_sched() */
1598 		spin_lock_irqsave(&q->current_entry_lock, flags);
1599 
1600 		taprio_start_sched(sch, start, new_admin);
1601 
1602 		rcu_assign_pointer(q->admin_sched, new_admin);
1603 		if (admin)
1604 			call_rcu(&admin->rcu, taprio_free_sched_cb);
1605 
1606 		spin_unlock_irqrestore(&q->current_entry_lock, flags);
1607 
1608 		if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1609 			taprio_offload_config_changed(q);
1610 	}
1611 
1612 	new_admin = NULL;
1613 	err = 0;
1614 
1615 unlock:
1616 	spin_unlock_bh(qdisc_lock(sch));
1617 
1618 free_sched:
1619 	if (new_admin)
1620 		call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1621 
1622 	return err;
1623 }
1624 
1625 static void taprio_reset(struct Qdisc *sch)
1626 {
1627 	struct taprio_sched *q = qdisc_priv(sch);
1628 	struct net_device *dev = qdisc_dev(sch);
1629 	int i;
1630 
1631 	hrtimer_cancel(&q->advance_timer);
1632 	if (q->qdiscs) {
1633 		for (i = 0; i < dev->num_tx_queues; i++)
1634 			if (q->qdiscs[i])
1635 				qdisc_reset(q->qdiscs[i]);
1636 	}
1637 	sch->qstats.backlog = 0;
1638 	sch->q.qlen = 0;
1639 }
1640 
1641 static void taprio_destroy(struct Qdisc *sch)
1642 {
1643 	struct taprio_sched *q = qdisc_priv(sch);
1644 	struct net_device *dev = qdisc_dev(sch);
1645 	unsigned int i;
1646 
1647 	spin_lock(&taprio_list_lock);
1648 	list_del(&q->taprio_list);
1649 	spin_unlock(&taprio_list_lock);
1650 
1651 	/* Note that taprio_reset() might not be called if an error
1652 	 * happens in qdisc_create(), after taprio_init() has been called.
1653 	 */
1654 	hrtimer_cancel(&q->advance_timer);
1655 
1656 	taprio_disable_offload(dev, q, NULL);
1657 
1658 	if (q->qdiscs) {
1659 		for (i = 0; i < dev->num_tx_queues; i++)
1660 			qdisc_put(q->qdiscs[i]);
1661 
1662 		kfree(q->qdiscs);
1663 	}
1664 	q->qdiscs = NULL;
1665 
1666 	netdev_reset_tc(dev);
1667 
1668 	if (q->oper_sched)
1669 		call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1670 
1671 	if (q->admin_sched)
1672 		call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1673 }
1674 
1675 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1676 		       struct netlink_ext_ack *extack)
1677 {
1678 	struct taprio_sched *q = qdisc_priv(sch);
1679 	struct net_device *dev = qdisc_dev(sch);
1680 	int i;
1681 
1682 	spin_lock_init(&q->current_entry_lock);
1683 
1684 	hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1685 	q->advance_timer.function = advance_sched;
1686 
1687 	q->dequeue = taprio_dequeue_soft;
1688 	q->peek = taprio_peek_soft;
1689 
1690 	q->root = sch;
1691 
1692 	/* We only support static clockids. Use an invalid value as default
1693 	 * and get the valid one on taprio_change().
1694 	 */
1695 	q->clockid = -1;
1696 	q->flags = TAPRIO_FLAGS_INVALID;
1697 
1698 	spin_lock(&taprio_list_lock);
1699 	list_add(&q->taprio_list, &taprio_list);
1700 	spin_unlock(&taprio_list_lock);
1701 
1702 	if (sch->parent != TC_H_ROOT)
1703 		return -EOPNOTSUPP;
1704 
1705 	if (!netif_is_multiqueue(dev))
1706 		return -EOPNOTSUPP;
1707 
1708 	/* pre-allocate qdisc, attachment can't fail */
1709 	q->qdiscs = kcalloc(dev->num_tx_queues,
1710 			    sizeof(q->qdiscs[0]),
1711 			    GFP_KERNEL);
1712 
1713 	if (!q->qdiscs)
1714 		return -ENOMEM;
1715 
1716 	if (!opt)
1717 		return -EINVAL;
1718 
1719 	for (i = 0; i < dev->num_tx_queues; i++) {
1720 		struct netdev_queue *dev_queue;
1721 		struct Qdisc *qdisc;
1722 
1723 		dev_queue = netdev_get_tx_queue(dev, i);
1724 		qdisc = qdisc_create_dflt(dev_queue,
1725 					  &pfifo_qdisc_ops,
1726 					  TC_H_MAKE(TC_H_MAJ(sch->handle),
1727 						    TC_H_MIN(i + 1)),
1728 					  extack);
1729 		if (!qdisc)
1730 			return -ENOMEM;
1731 
1732 		if (i < dev->real_num_tx_queues)
1733 			qdisc_hash_add(qdisc, false);
1734 
1735 		q->qdiscs[i] = qdisc;
1736 	}
1737 
1738 	return taprio_change(sch, opt, extack);
1739 }
1740 
1741 static void taprio_attach(struct Qdisc *sch)
1742 {
1743 	struct taprio_sched *q = qdisc_priv(sch);
1744 	struct net_device *dev = qdisc_dev(sch);
1745 	unsigned int ntx;
1746 
1747 	/* Attach underlying qdisc */
1748 	for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
1749 		struct Qdisc *qdisc = q->qdiscs[ntx];
1750 		struct Qdisc *old;
1751 
1752 		if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1753 			qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1754 			old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
1755 		} else {
1756 			old = dev_graft_qdisc(qdisc->dev_queue, sch);
1757 			qdisc_refcount_inc(sch);
1758 		}
1759 		if (old)
1760 			qdisc_put(old);
1761 	}
1762 
1763 	/* access to the child qdiscs is not needed in offload mode */
1764 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1765 		kfree(q->qdiscs);
1766 		q->qdiscs = NULL;
1767 	}
1768 }
1769 
1770 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1771 					     unsigned long cl)
1772 {
1773 	struct net_device *dev = qdisc_dev(sch);
1774 	unsigned long ntx = cl - 1;
1775 
1776 	if (ntx >= dev->num_tx_queues)
1777 		return NULL;
1778 
1779 	return netdev_get_tx_queue(dev, ntx);
1780 }
1781 
1782 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1783 			struct Qdisc *new, struct Qdisc **old,
1784 			struct netlink_ext_ack *extack)
1785 {
1786 	struct taprio_sched *q = qdisc_priv(sch);
1787 	struct net_device *dev = qdisc_dev(sch);
1788 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1789 
1790 	if (!dev_queue)
1791 		return -EINVAL;
1792 
1793 	if (dev->flags & IFF_UP)
1794 		dev_deactivate(dev);
1795 
1796 	if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1797 		*old = dev_graft_qdisc(dev_queue, new);
1798 	} else {
1799 		*old = q->qdiscs[cl - 1];
1800 		q->qdiscs[cl - 1] = new;
1801 	}
1802 
1803 	if (new)
1804 		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1805 
1806 	if (dev->flags & IFF_UP)
1807 		dev_activate(dev);
1808 
1809 	return 0;
1810 }
1811 
1812 static int dump_entry(struct sk_buff *msg,
1813 		      const struct sched_entry *entry)
1814 {
1815 	struct nlattr *item;
1816 
1817 	item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1818 	if (!item)
1819 		return -ENOSPC;
1820 
1821 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1822 		goto nla_put_failure;
1823 
1824 	if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1825 		goto nla_put_failure;
1826 
1827 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1828 			entry->gate_mask))
1829 		goto nla_put_failure;
1830 
1831 	if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1832 			entry->interval))
1833 		goto nla_put_failure;
1834 
1835 	return nla_nest_end(msg, item);
1836 
1837 nla_put_failure:
1838 	nla_nest_cancel(msg, item);
1839 	return -1;
1840 }
1841 
1842 static int dump_schedule(struct sk_buff *msg,
1843 			 const struct sched_gate_list *root)
1844 {
1845 	struct nlattr *entry_list;
1846 	struct sched_entry *entry;
1847 
1848 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1849 			root->base_time, TCA_TAPRIO_PAD))
1850 		return -1;
1851 
1852 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1853 			root->cycle_time, TCA_TAPRIO_PAD))
1854 		return -1;
1855 
1856 	if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1857 			root->cycle_time_extension, TCA_TAPRIO_PAD))
1858 		return -1;
1859 
1860 	entry_list = nla_nest_start_noflag(msg,
1861 					   TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1862 	if (!entry_list)
1863 		goto error_nest;
1864 
1865 	list_for_each_entry(entry, &root->entries, list) {
1866 		if (dump_entry(msg, entry) < 0)
1867 			goto error_nest;
1868 	}
1869 
1870 	nla_nest_end(msg, entry_list);
1871 	return 0;
1872 
1873 error_nest:
1874 	nla_nest_cancel(msg, entry_list);
1875 	return -1;
1876 }
1877 
1878 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1879 {
1880 	struct taprio_sched *q = qdisc_priv(sch);
1881 	struct net_device *dev = qdisc_dev(sch);
1882 	struct sched_gate_list *oper, *admin;
1883 	struct tc_mqprio_qopt opt = { 0 };
1884 	struct nlattr *nest, *sched_nest;
1885 	unsigned int i;
1886 
1887 	rcu_read_lock();
1888 	oper = rcu_dereference(q->oper_sched);
1889 	admin = rcu_dereference(q->admin_sched);
1890 
1891 	opt.num_tc = netdev_get_num_tc(dev);
1892 	memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1893 
1894 	for (i = 0; i < netdev_get_num_tc(dev); i++) {
1895 		opt.count[i] = dev->tc_to_txq[i].count;
1896 		opt.offset[i] = dev->tc_to_txq[i].offset;
1897 	}
1898 
1899 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1900 	if (!nest)
1901 		goto start_error;
1902 
1903 	if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1904 		goto options_error;
1905 
1906 	if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1907 	    nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1908 		goto options_error;
1909 
1910 	if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1911 		goto options_error;
1912 
1913 	if (q->txtime_delay &&
1914 	    nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1915 		goto options_error;
1916 
1917 	if (oper && dump_schedule(skb, oper))
1918 		goto options_error;
1919 
1920 	if (!admin)
1921 		goto done;
1922 
1923 	sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1924 	if (!sched_nest)
1925 		goto options_error;
1926 
1927 	if (dump_schedule(skb, admin))
1928 		goto admin_error;
1929 
1930 	nla_nest_end(skb, sched_nest);
1931 
1932 done:
1933 	rcu_read_unlock();
1934 
1935 	return nla_nest_end(skb, nest);
1936 
1937 admin_error:
1938 	nla_nest_cancel(skb, sched_nest);
1939 
1940 options_error:
1941 	nla_nest_cancel(skb, nest);
1942 
1943 start_error:
1944 	rcu_read_unlock();
1945 	return -ENOSPC;
1946 }
1947 
1948 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1949 {
1950 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1951 
1952 	if (!dev_queue)
1953 		return NULL;
1954 
1955 	return dev_queue->qdisc_sleeping;
1956 }
1957 
1958 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1959 {
1960 	unsigned int ntx = TC_H_MIN(classid);
1961 
1962 	if (!taprio_queue_get(sch, ntx))
1963 		return 0;
1964 	return ntx;
1965 }
1966 
1967 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1968 			     struct sk_buff *skb, struct tcmsg *tcm)
1969 {
1970 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1971 
1972 	tcm->tcm_parent = TC_H_ROOT;
1973 	tcm->tcm_handle |= TC_H_MIN(cl);
1974 	tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1975 
1976 	return 0;
1977 }
1978 
1979 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1980 				   struct gnet_dump *d)
1981 	__releases(d->lock)
1982 	__acquires(d->lock)
1983 {
1984 	struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1985 
1986 	sch = dev_queue->qdisc_sleeping;
1987 	if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 ||
1988 	    qdisc_qstats_copy(d, sch) < 0)
1989 		return -1;
1990 	return 0;
1991 }
1992 
1993 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1994 {
1995 	struct net_device *dev = qdisc_dev(sch);
1996 	unsigned long ntx;
1997 
1998 	if (arg->stop)
1999 		return;
2000 
2001 	arg->count = arg->skip;
2002 	for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2003 		if (arg->fn(sch, ntx + 1, arg) < 0) {
2004 			arg->stop = 1;
2005 			break;
2006 		}
2007 		arg->count++;
2008 	}
2009 }
2010 
2011 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2012 						struct tcmsg *tcm)
2013 {
2014 	return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2015 }
2016 
2017 static const struct Qdisc_class_ops taprio_class_ops = {
2018 	.graft		= taprio_graft,
2019 	.leaf		= taprio_leaf,
2020 	.find		= taprio_find,
2021 	.walk		= taprio_walk,
2022 	.dump		= taprio_dump_class,
2023 	.dump_stats	= taprio_dump_class_stats,
2024 	.select_queue	= taprio_select_queue,
2025 };
2026 
2027 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2028 	.cl_ops		= &taprio_class_ops,
2029 	.id		= "taprio",
2030 	.priv_size	= sizeof(struct taprio_sched),
2031 	.init		= taprio_init,
2032 	.change		= taprio_change,
2033 	.destroy	= taprio_destroy,
2034 	.reset		= taprio_reset,
2035 	.attach		= taprio_attach,
2036 	.peek		= taprio_peek,
2037 	.dequeue	= taprio_dequeue,
2038 	.enqueue	= taprio_enqueue,
2039 	.dump		= taprio_dump,
2040 	.owner		= THIS_MODULE,
2041 };
2042 
2043 static struct notifier_block taprio_device_notifier = {
2044 	.notifier_call = taprio_dev_notifier,
2045 };
2046 
2047 static int __init taprio_module_init(void)
2048 {
2049 	int err = register_netdevice_notifier(&taprio_device_notifier);
2050 
2051 	if (err)
2052 		return err;
2053 
2054 	return register_qdisc(&taprio_qdisc_ops);
2055 }
2056 
2057 static void __exit taprio_module_exit(void)
2058 {
2059 	unregister_qdisc(&taprio_qdisc_ops);
2060 	unregister_netdevice_notifier(&taprio_device_notifier);
2061 }
2062 
2063 module_init(taprio_module_init);
2064 module_exit(taprio_module_exit);
2065 MODULE_LICENSE("GPL");
2066