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