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