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