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