15a781ccbSVinicius Costa Gomes // SPDX-License-Identifier: GPL-2.0 25a781ccbSVinicius Costa Gomes 35a781ccbSVinicius Costa Gomes /* net/sched/sch_taprio.c Time Aware Priority Scheduler 45a781ccbSVinicius Costa Gomes * 55a781ccbSVinicius Costa Gomes * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com> 65a781ccbSVinicius Costa Gomes * 75a781ccbSVinicius Costa Gomes */ 85a781ccbSVinicius Costa Gomes 9cc69837fSJakub Kicinski #include <linux/ethtool.h> 105a781ccbSVinicius Costa Gomes #include <linux/types.h> 115a781ccbSVinicius Costa Gomes #include <linux/slab.h> 125a781ccbSVinicius Costa Gomes #include <linux/kernel.h> 135a781ccbSVinicius Costa Gomes #include <linux/string.h> 145a781ccbSVinicius Costa Gomes #include <linux/list.h> 155a781ccbSVinicius Costa Gomes #include <linux/errno.h> 165a781ccbSVinicius Costa Gomes #include <linux/skbuff.h> 1723bddf69SJakub Kicinski #include <linux/math64.h> 185a781ccbSVinicius Costa Gomes #include <linux/module.h> 195a781ccbSVinicius Costa Gomes #include <linux/spinlock.h> 20a3d43c0dSVinicius Costa Gomes #include <linux/rcupdate.h> 21837ced3aSVladimir Oltean #include <linux/time.h> 225a781ccbSVinicius Costa Gomes #include <net/netlink.h> 235a781ccbSVinicius Costa Gomes #include <net/pkt_sched.h> 245a781ccbSVinicius Costa Gomes #include <net/pkt_cls.h> 255a781ccbSVinicius Costa Gomes #include <net/sch_generic.h> 264cfd5779SVedang Patel #include <net/sock.h> 2754002066SVedang Patel #include <net/tcp.h> 285a781ccbSVinicius Costa Gomes 297b9eba7bSLeandro Dorileo static LIST_HEAD(taprio_list); 307b9eba7bSLeandro Dorileo static DEFINE_SPINLOCK(taprio_list_lock); 317b9eba7bSLeandro Dorileo 325a781ccbSVinicius Costa Gomes #define TAPRIO_ALL_GATES_OPEN -1 335a781ccbSVinicius Costa Gomes 344cfd5779SVedang Patel #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) 359c66d156SVinicius Costa Gomes #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD) 36a9d62274SVinicius Costa Gomes #define TAPRIO_FLAGS_INVALID U32_MAX 374cfd5779SVedang Patel 385a781ccbSVinicius Costa Gomes struct sched_entry { 395a781ccbSVinicius Costa Gomes struct list_head list; 405a781ccbSVinicius Costa Gomes 415a781ccbSVinicius Costa Gomes /* The instant that this entry "closes" and the next one 425a781ccbSVinicius Costa Gomes * should open, the qdisc will make some effort so that no 435a781ccbSVinicius Costa Gomes * packet leaves after this time. 445a781ccbSVinicius Costa Gomes */ 455a781ccbSVinicius Costa Gomes ktime_t close_time; 464cfd5779SVedang Patel ktime_t next_txtime; 475a781ccbSVinicius Costa Gomes atomic_t budget; 485a781ccbSVinicius Costa Gomes int index; 495a781ccbSVinicius Costa Gomes u32 gate_mask; 505a781ccbSVinicius Costa Gomes u32 interval; 515a781ccbSVinicius Costa Gomes u8 command; 525a781ccbSVinicius Costa Gomes }; 535a781ccbSVinicius Costa Gomes 54a3d43c0dSVinicius Costa Gomes struct sched_gate_list { 55a3d43c0dSVinicius Costa Gomes struct rcu_head rcu; 56a3d43c0dSVinicius Costa Gomes struct list_head entries; 57a3d43c0dSVinicius Costa Gomes size_t num_entries; 586ca6a665SVinicius Costa Gomes ktime_t cycle_close_time; 596ca6a665SVinicius Costa Gomes s64 cycle_time; 60c25031e9SVinicius Costa Gomes s64 cycle_time_extension; 61a3d43c0dSVinicius Costa Gomes s64 base_time; 62a3d43c0dSVinicius Costa Gomes }; 63a3d43c0dSVinicius Costa Gomes 645a781ccbSVinicius Costa Gomes struct taprio_sched { 655a781ccbSVinicius Costa Gomes struct Qdisc **qdiscs; 665a781ccbSVinicius Costa Gomes struct Qdisc *root; 674cfd5779SVedang Patel u32 flags; 687ede7b03SVedang Patel enum tk_offsets tk_offset; 695a781ccbSVinicius Costa Gomes int clockid; 707b9eba7bSLeandro Dorileo atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+ 715a781ccbSVinicius Costa Gomes * speeds it's sub-nanoseconds per byte 725a781ccbSVinicius Costa Gomes */ 735a781ccbSVinicius Costa Gomes 745a781ccbSVinicius Costa Gomes /* Protects the update side of the RCU protected current_entry */ 755a781ccbSVinicius Costa Gomes spinlock_t current_entry_lock; 765a781ccbSVinicius Costa Gomes struct sched_entry __rcu *current_entry; 77a3d43c0dSVinicius Costa Gomes struct sched_gate_list __rcu *oper_sched; 78a3d43c0dSVinicius Costa Gomes struct sched_gate_list __rcu *admin_sched; 795a781ccbSVinicius Costa Gomes struct hrtimer advance_timer; 807b9eba7bSLeandro Dorileo struct list_head taprio_list; 819c66d156SVinicius Costa Gomes struct sk_buff *(*dequeue)(struct Qdisc *sch); 829c66d156SVinicius Costa Gomes struct sk_buff *(*peek)(struct Qdisc *sch); 83a5b64700SVedang Patel u32 txtime_delay; 845a781ccbSVinicius Costa Gomes }; 855a781ccbSVinicius Costa Gomes 869c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload { 879c66d156SVinicius Costa Gomes refcount_t users; 889c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload offload; 899c66d156SVinicius Costa Gomes }; 909c66d156SVinicius Costa Gomes 91a3d43c0dSVinicius Costa Gomes static ktime_t sched_base_time(const struct sched_gate_list *sched) 92a3d43c0dSVinicius Costa Gomes { 93a3d43c0dSVinicius Costa Gomes if (!sched) 94a3d43c0dSVinicius Costa Gomes return KTIME_MAX; 95a3d43c0dSVinicius Costa Gomes 96a3d43c0dSVinicius Costa Gomes return ns_to_ktime(sched->base_time); 97a3d43c0dSVinicius Costa Gomes } 98a3d43c0dSVinicius Costa Gomes 996dc25401SEric Dumazet static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono) 1007ede7b03SVedang Patel { 1016dc25401SEric Dumazet /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */ 1026dc25401SEric Dumazet enum tk_offsets tk_offset = READ_ONCE(q->tk_offset); 1037ede7b03SVedang Patel 1046dc25401SEric Dumazet switch (tk_offset) { 1057ede7b03SVedang Patel case TK_OFFS_MAX: 1067ede7b03SVedang Patel return mono; 1077ede7b03SVedang Patel default: 1086dc25401SEric Dumazet return ktime_mono_to_any(mono, tk_offset); 1096dc25401SEric Dumazet } 1107ede7b03SVedang Patel } 1117ede7b03SVedang Patel 1126dc25401SEric Dumazet static ktime_t taprio_get_time(const struct taprio_sched *q) 1136dc25401SEric Dumazet { 1146dc25401SEric Dumazet return taprio_mono_to_any(q, ktime_get()); 1157ede7b03SVedang Patel } 1167ede7b03SVedang Patel 117a3d43c0dSVinicius Costa Gomes static void taprio_free_sched_cb(struct rcu_head *head) 118a3d43c0dSVinicius Costa Gomes { 119a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu); 120a3d43c0dSVinicius Costa Gomes struct sched_entry *entry, *n; 121a3d43c0dSVinicius Costa Gomes 122a3d43c0dSVinicius Costa Gomes list_for_each_entry_safe(entry, n, &sched->entries, list) { 123a3d43c0dSVinicius Costa Gomes list_del(&entry->list); 124a3d43c0dSVinicius Costa Gomes kfree(entry); 125a3d43c0dSVinicius Costa Gomes } 126a3d43c0dSVinicius Costa Gomes 127a3d43c0dSVinicius Costa Gomes kfree(sched); 128a3d43c0dSVinicius Costa Gomes } 129a3d43c0dSVinicius Costa Gomes 130a3d43c0dSVinicius Costa Gomes static void switch_schedules(struct taprio_sched *q, 131a3d43c0dSVinicius Costa Gomes struct sched_gate_list **admin, 132a3d43c0dSVinicius Costa Gomes struct sched_gate_list **oper) 133a3d43c0dSVinicius Costa Gomes { 134a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->oper_sched, *admin); 135a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->admin_sched, NULL); 136a3d43c0dSVinicius Costa Gomes 137a3d43c0dSVinicius Costa Gomes if (*oper) 138a3d43c0dSVinicius Costa Gomes call_rcu(&(*oper)->rcu, taprio_free_sched_cb); 139a3d43c0dSVinicius Costa Gomes 140a3d43c0dSVinicius Costa Gomes *oper = *admin; 141a3d43c0dSVinicius Costa Gomes *admin = NULL; 142a3d43c0dSVinicius Costa Gomes } 143a3d43c0dSVinicius Costa Gomes 1444cfd5779SVedang Patel /* Get how much time has been already elapsed in the current cycle. */ 1454cfd5779SVedang Patel static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time) 1464cfd5779SVedang Patel { 1474cfd5779SVedang Patel ktime_t time_since_sched_start; 1484cfd5779SVedang Patel s32 time_elapsed; 1494cfd5779SVedang Patel 1504cfd5779SVedang Patel time_since_sched_start = ktime_sub(time, sched->base_time); 1514cfd5779SVedang Patel div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed); 1524cfd5779SVedang Patel 1534cfd5779SVedang Patel return time_elapsed; 1544cfd5779SVedang Patel } 1554cfd5779SVedang Patel 1564cfd5779SVedang Patel static ktime_t get_interval_end_time(struct sched_gate_list *sched, 1574cfd5779SVedang Patel struct sched_gate_list *admin, 1584cfd5779SVedang Patel struct sched_entry *entry, 1594cfd5779SVedang Patel ktime_t intv_start) 1604cfd5779SVedang Patel { 1614cfd5779SVedang Patel s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start); 1624cfd5779SVedang Patel ktime_t intv_end, cycle_ext_end, cycle_end; 1634cfd5779SVedang Patel 1644cfd5779SVedang Patel cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed); 1654cfd5779SVedang Patel intv_end = ktime_add_ns(intv_start, entry->interval); 1664cfd5779SVedang Patel cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension); 1674cfd5779SVedang Patel 1684cfd5779SVedang Patel if (ktime_before(intv_end, cycle_end)) 1694cfd5779SVedang Patel return intv_end; 1704cfd5779SVedang Patel else if (admin && admin != sched && 1714cfd5779SVedang Patel ktime_after(admin->base_time, cycle_end) && 1724cfd5779SVedang Patel ktime_before(admin->base_time, cycle_ext_end)) 1734cfd5779SVedang Patel return admin->base_time; 1744cfd5779SVedang Patel else 1754cfd5779SVedang Patel return cycle_end; 1764cfd5779SVedang Patel } 1774cfd5779SVedang Patel 1784cfd5779SVedang Patel static int length_to_duration(struct taprio_sched *q, int len) 1794cfd5779SVedang Patel { 180837ced3aSVladimir Oltean return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC); 1814cfd5779SVedang Patel } 1824cfd5779SVedang Patel 1834cfd5779SVedang Patel /* Returns the entry corresponding to next available interval. If 1844cfd5779SVedang Patel * validate_interval is set, it only validates whether the timestamp occurs 1854cfd5779SVedang Patel * when the gate corresponding to the skb's traffic class is open. 1864cfd5779SVedang Patel */ 1874cfd5779SVedang Patel static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb, 1884cfd5779SVedang Patel struct Qdisc *sch, 1894cfd5779SVedang Patel struct sched_gate_list *sched, 1904cfd5779SVedang Patel struct sched_gate_list *admin, 1914cfd5779SVedang Patel ktime_t time, 1924cfd5779SVedang Patel ktime_t *interval_start, 1934cfd5779SVedang Patel ktime_t *interval_end, 1944cfd5779SVedang Patel bool validate_interval) 1954cfd5779SVedang Patel { 1964cfd5779SVedang Patel ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time; 1974cfd5779SVedang Patel ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time; 1984cfd5779SVedang Patel struct sched_entry *entry = NULL, *entry_found = NULL; 1994cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 2004cfd5779SVedang Patel struct net_device *dev = qdisc_dev(sch); 2014cfd5779SVedang Patel bool entry_available = false; 2024cfd5779SVedang Patel s32 cycle_elapsed; 2034cfd5779SVedang Patel int tc, n; 2044cfd5779SVedang Patel 2054cfd5779SVedang Patel tc = netdev_get_prio_tc_map(dev, skb->priority); 2064cfd5779SVedang Patel packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb)); 2074cfd5779SVedang Patel 2084cfd5779SVedang Patel *interval_start = 0; 2094cfd5779SVedang Patel *interval_end = 0; 2104cfd5779SVedang Patel 2114cfd5779SVedang Patel if (!sched) 2124cfd5779SVedang Patel return NULL; 2134cfd5779SVedang Patel 2144cfd5779SVedang Patel cycle = sched->cycle_time; 2154cfd5779SVedang Patel cycle_elapsed = get_cycle_time_elapsed(sched, time); 2164cfd5779SVedang Patel curr_intv_end = ktime_sub_ns(time, cycle_elapsed); 2174cfd5779SVedang Patel cycle_end = ktime_add_ns(curr_intv_end, cycle); 2184cfd5779SVedang Patel 2194cfd5779SVedang Patel list_for_each_entry(entry, &sched->entries, list) { 2204cfd5779SVedang Patel curr_intv_start = curr_intv_end; 2214cfd5779SVedang Patel curr_intv_end = get_interval_end_time(sched, admin, entry, 2224cfd5779SVedang Patel curr_intv_start); 2234cfd5779SVedang Patel 2244cfd5779SVedang Patel if (ktime_after(curr_intv_start, cycle_end)) 2254cfd5779SVedang Patel break; 2264cfd5779SVedang Patel 2274cfd5779SVedang Patel if (!(entry->gate_mask & BIT(tc)) || 2284cfd5779SVedang Patel packet_transmit_time > entry->interval) 2294cfd5779SVedang Patel continue; 2304cfd5779SVedang Patel 2314cfd5779SVedang Patel txtime = entry->next_txtime; 2324cfd5779SVedang Patel 2334cfd5779SVedang Patel if (ktime_before(txtime, time) || validate_interval) { 2344cfd5779SVedang Patel transmit_end_time = ktime_add_ns(time, packet_transmit_time); 2354cfd5779SVedang Patel if ((ktime_before(curr_intv_start, time) && 2364cfd5779SVedang Patel ktime_before(transmit_end_time, curr_intv_end)) || 2374cfd5779SVedang Patel (ktime_after(curr_intv_start, time) && !validate_interval)) { 2384cfd5779SVedang Patel entry_found = entry; 2394cfd5779SVedang Patel *interval_start = curr_intv_start; 2404cfd5779SVedang Patel *interval_end = curr_intv_end; 2414cfd5779SVedang Patel break; 2424cfd5779SVedang Patel } else if (!entry_available && !validate_interval) { 2434cfd5779SVedang Patel /* Here, we are just trying to find out the 2444cfd5779SVedang Patel * first available interval in the next cycle. 2454cfd5779SVedang Patel */ 2460deee7aaSJiapeng Zhong entry_available = true; 2474cfd5779SVedang Patel entry_found = entry; 2484cfd5779SVedang Patel *interval_start = ktime_add_ns(curr_intv_start, cycle); 2494cfd5779SVedang Patel *interval_end = ktime_add_ns(curr_intv_end, cycle); 2504cfd5779SVedang Patel } 2514cfd5779SVedang Patel } else if (ktime_before(txtime, earliest_txtime) && 2524cfd5779SVedang Patel !entry_available) { 2534cfd5779SVedang Patel earliest_txtime = txtime; 2544cfd5779SVedang Patel entry_found = entry; 2554cfd5779SVedang Patel n = div_s64(ktime_sub(txtime, curr_intv_start), cycle); 2564cfd5779SVedang Patel *interval_start = ktime_add(curr_intv_start, n * cycle); 2574cfd5779SVedang Patel *interval_end = ktime_add(curr_intv_end, n * cycle); 2584cfd5779SVedang Patel } 2594cfd5779SVedang Patel } 2604cfd5779SVedang Patel 2614cfd5779SVedang Patel return entry_found; 2624cfd5779SVedang Patel } 2634cfd5779SVedang Patel 2644cfd5779SVedang Patel static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch) 2654cfd5779SVedang Patel { 2664cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 2674cfd5779SVedang Patel struct sched_gate_list *sched, *admin; 2684cfd5779SVedang Patel ktime_t interval_start, interval_end; 2694cfd5779SVedang Patel struct sched_entry *entry; 2704cfd5779SVedang Patel 2714cfd5779SVedang Patel rcu_read_lock(); 2724cfd5779SVedang Patel sched = rcu_dereference(q->oper_sched); 2734cfd5779SVedang Patel admin = rcu_dereference(q->admin_sched); 2744cfd5779SVedang Patel 2754cfd5779SVedang Patel entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp, 2764cfd5779SVedang Patel &interval_start, &interval_end, true); 2774cfd5779SVedang Patel rcu_read_unlock(); 2784cfd5779SVedang Patel 2794cfd5779SVedang Patel return entry; 2804cfd5779SVedang Patel } 2814cfd5779SVedang Patel 2829c66d156SVinicius Costa Gomes static bool taprio_flags_valid(u32 flags) 2839c66d156SVinicius Costa Gomes { 2849c66d156SVinicius Costa Gomes /* Make sure no other flag bits are set. */ 2859c66d156SVinicius Costa Gomes if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST | 2869c66d156SVinicius Costa Gomes TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) 2879c66d156SVinicius Costa Gomes return false; 2889c66d156SVinicius Costa Gomes /* txtime-assist and full offload are mutually exclusive */ 2899c66d156SVinicius Costa Gomes if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) && 2909c66d156SVinicius Costa Gomes (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) 2919c66d156SVinicius Costa Gomes return false; 2929c66d156SVinicius Costa Gomes return true; 2939c66d156SVinicius Costa Gomes } 2949c66d156SVinicius Costa Gomes 29554002066SVedang Patel /* This returns the tstamp value set by TCP in terms of the set clock. */ 29654002066SVedang Patel static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb) 29754002066SVedang Patel { 29854002066SVedang Patel unsigned int offset = skb_network_offset(skb); 29954002066SVedang Patel const struct ipv6hdr *ipv6h; 30054002066SVedang Patel const struct iphdr *iph; 30154002066SVedang Patel struct ipv6hdr _ipv6h; 30254002066SVedang Patel 30354002066SVedang Patel ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 30454002066SVedang Patel if (!ipv6h) 30554002066SVedang Patel return 0; 30654002066SVedang Patel 30754002066SVedang Patel if (ipv6h->version == 4) { 30854002066SVedang Patel iph = (struct iphdr *)ipv6h; 30954002066SVedang Patel offset += iph->ihl * 4; 31054002066SVedang Patel 31154002066SVedang Patel /* special-case 6in4 tunnelling, as that is a common way to get 31254002066SVedang Patel * v6 connectivity in the home 31354002066SVedang Patel */ 31454002066SVedang Patel if (iph->protocol == IPPROTO_IPV6) { 31554002066SVedang Patel ipv6h = skb_header_pointer(skb, offset, 31654002066SVedang Patel sizeof(_ipv6h), &_ipv6h); 31754002066SVedang Patel 31854002066SVedang Patel if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP) 31954002066SVedang Patel return 0; 32054002066SVedang Patel } else if (iph->protocol != IPPROTO_TCP) { 32154002066SVedang Patel return 0; 32254002066SVedang Patel } 32354002066SVedang Patel } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) { 32454002066SVedang Patel return 0; 32554002066SVedang Patel } 32654002066SVedang Patel 3276dc25401SEric Dumazet return taprio_mono_to_any(q, skb->skb_mstamp_ns); 32854002066SVedang Patel } 32954002066SVedang Patel 3304cfd5779SVedang Patel /* There are a few scenarios where we will have to modify the txtime from 3314cfd5779SVedang Patel * what is read from next_txtime in sched_entry. They are: 3324cfd5779SVedang Patel * 1. If txtime is in the past, 3334cfd5779SVedang Patel * a. The gate for the traffic class is currently open and packet can be 3344cfd5779SVedang Patel * transmitted before it closes, schedule the packet right away. 3354cfd5779SVedang Patel * b. If the gate corresponding to the traffic class is going to open later 3364cfd5779SVedang Patel * in the cycle, set the txtime of packet to the interval start. 3374cfd5779SVedang Patel * 2. If txtime is in the future, there are packets corresponding to the 3384cfd5779SVedang Patel * current traffic class waiting to be transmitted. So, the following 3394cfd5779SVedang Patel * possibilities exist: 3404cfd5779SVedang Patel * a. We can transmit the packet before the window containing the txtime 3414cfd5779SVedang Patel * closes. 3424cfd5779SVedang Patel * b. The window might close before the transmission can be completed 3434cfd5779SVedang Patel * successfully. So, schedule the packet in the next open window. 3444cfd5779SVedang Patel */ 3454cfd5779SVedang Patel static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch) 3464cfd5779SVedang Patel { 34754002066SVedang Patel ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp; 3484cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 3494cfd5779SVedang Patel struct sched_gate_list *sched, *admin; 3504cfd5779SVedang Patel ktime_t minimum_time, now, txtime; 3514cfd5779SVedang Patel int len, packet_transmit_time; 3524cfd5779SVedang Patel struct sched_entry *entry; 3534cfd5779SVedang Patel bool sched_changed; 3544cfd5779SVedang Patel 3557ede7b03SVedang Patel now = taprio_get_time(q); 3564cfd5779SVedang Patel minimum_time = ktime_add_ns(now, q->txtime_delay); 3574cfd5779SVedang Patel 35854002066SVedang Patel tcp_tstamp = get_tcp_tstamp(q, skb); 35954002066SVedang Patel minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp); 36054002066SVedang Patel 3614cfd5779SVedang Patel rcu_read_lock(); 3624cfd5779SVedang Patel admin = rcu_dereference(q->admin_sched); 3634cfd5779SVedang Patel sched = rcu_dereference(q->oper_sched); 3644cfd5779SVedang Patel if (admin && ktime_after(minimum_time, admin->base_time)) 3654cfd5779SVedang Patel switch_schedules(q, &admin, &sched); 3664cfd5779SVedang Patel 3674cfd5779SVedang Patel /* Until the schedule starts, all the queues are open */ 3684cfd5779SVedang Patel if (!sched || ktime_before(minimum_time, sched->base_time)) { 3694cfd5779SVedang Patel txtime = minimum_time; 3704cfd5779SVedang Patel goto done; 3714cfd5779SVedang Patel } 3724cfd5779SVedang Patel 3734cfd5779SVedang Patel len = qdisc_pkt_len(skb); 3744cfd5779SVedang Patel packet_transmit_time = length_to_duration(q, len); 3754cfd5779SVedang Patel 3764cfd5779SVedang Patel do { 3770deee7aaSJiapeng Zhong sched_changed = false; 3784cfd5779SVedang Patel 3794cfd5779SVedang Patel entry = find_entry_to_transmit(skb, sch, sched, admin, 3804cfd5779SVedang Patel minimum_time, 3814cfd5779SVedang Patel &interval_start, &interval_end, 3824cfd5779SVedang Patel false); 3834cfd5779SVedang Patel if (!entry) { 3844cfd5779SVedang Patel txtime = 0; 3854cfd5779SVedang Patel goto done; 3864cfd5779SVedang Patel } 3874cfd5779SVedang Patel 3884cfd5779SVedang Patel txtime = entry->next_txtime; 3894cfd5779SVedang Patel txtime = max_t(ktime_t, txtime, minimum_time); 3904cfd5779SVedang Patel txtime = max_t(ktime_t, txtime, interval_start); 3914cfd5779SVedang Patel 3924cfd5779SVedang Patel if (admin && admin != sched && 3934cfd5779SVedang Patel ktime_after(txtime, admin->base_time)) { 3944cfd5779SVedang Patel sched = admin; 3950deee7aaSJiapeng Zhong sched_changed = true; 3964cfd5779SVedang Patel continue; 3974cfd5779SVedang Patel } 3984cfd5779SVedang Patel 3994cfd5779SVedang Patel transmit_end_time = ktime_add(txtime, packet_transmit_time); 4004cfd5779SVedang Patel minimum_time = transmit_end_time; 4014cfd5779SVedang Patel 4024cfd5779SVedang Patel /* Update the txtime of current entry to the next time it's 4034cfd5779SVedang Patel * interval starts. 4044cfd5779SVedang Patel */ 4054cfd5779SVedang Patel if (ktime_after(transmit_end_time, interval_end)) 4064cfd5779SVedang Patel entry->next_txtime = ktime_add(interval_start, sched->cycle_time); 4074cfd5779SVedang Patel } while (sched_changed || ktime_after(transmit_end_time, interval_end)); 4084cfd5779SVedang Patel 4094cfd5779SVedang Patel entry->next_txtime = transmit_end_time; 4104cfd5779SVedang Patel 4114cfd5779SVedang Patel done: 4124cfd5779SVedang Patel rcu_read_unlock(); 4134cfd5779SVedang Patel return txtime; 4144cfd5779SVedang Patel } 4154cfd5779SVedang Patel 416497cc002SKurt Kanzenbach static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch, 417497cc002SKurt Kanzenbach struct Qdisc *child, struct sk_buff **to_free) 4185a781ccbSVinicius Costa Gomes { 4195a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 4205a781ccbSVinicius Costa Gomes 421e8a64bbaSBenedikt Spranger /* sk_flags are only safe to use on full sockets. */ 422e8a64bbaSBenedikt Spranger if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) { 4234cfd5779SVedang Patel if (!is_valid_interval(skb, sch)) 4244cfd5779SVedang Patel return qdisc_drop(skb, sch, to_free); 4254cfd5779SVedang Patel } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 4264cfd5779SVedang Patel skb->tstamp = get_packet_txtime(skb, sch); 4274cfd5779SVedang Patel if (!skb->tstamp) 4284cfd5779SVedang Patel return qdisc_drop(skb, sch, to_free); 4294cfd5779SVedang Patel } 4304cfd5779SVedang Patel 4315a781ccbSVinicius Costa Gomes qdisc_qstats_backlog_inc(sch, skb); 4325a781ccbSVinicius Costa Gomes sch->q.qlen++; 4335a781ccbSVinicius Costa Gomes 434ac5c66f2SPetr Machata return qdisc_enqueue(skb, child, to_free); 4355a781ccbSVinicius Costa Gomes } 4365a781ccbSVinicius Costa Gomes 437497cc002SKurt Kanzenbach static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch, 438497cc002SKurt Kanzenbach struct sk_buff **to_free) 439497cc002SKurt Kanzenbach { 440497cc002SKurt Kanzenbach struct taprio_sched *q = qdisc_priv(sch); 441497cc002SKurt Kanzenbach struct Qdisc *child; 442497cc002SKurt Kanzenbach int queue; 443497cc002SKurt Kanzenbach 44413511704SYannick Vignon if (unlikely(FULL_OFFLOAD_IS_ENABLED(q->flags))) { 44513511704SYannick Vignon WARN_ONCE(1, "Trying to enqueue skb into the root of a taprio qdisc configured with full offload\n"); 44613511704SYannick Vignon return qdisc_drop(skb, sch, to_free); 44713511704SYannick Vignon } 44813511704SYannick Vignon 449497cc002SKurt Kanzenbach queue = skb_get_queue_mapping(skb); 450497cc002SKurt Kanzenbach 451497cc002SKurt Kanzenbach child = q->qdiscs[queue]; 452497cc002SKurt Kanzenbach if (unlikely(!child)) 453497cc002SKurt Kanzenbach return qdisc_drop(skb, sch, to_free); 454497cc002SKurt Kanzenbach 455497cc002SKurt Kanzenbach /* Large packets might not be transmitted when the transmission duration 456497cc002SKurt Kanzenbach * exceeds any configured interval. Therefore, segment the skb into 457497cc002SKurt Kanzenbach * smaller chunks. Skip it for the full offload case, as the driver 458497cc002SKurt Kanzenbach * and/or the hardware is expected to handle this. 459497cc002SKurt Kanzenbach */ 460497cc002SKurt Kanzenbach if (skb_is_gso(skb) && !FULL_OFFLOAD_IS_ENABLED(q->flags)) { 461497cc002SKurt Kanzenbach unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb); 462497cc002SKurt Kanzenbach netdev_features_t features = netif_skb_features(skb); 463497cc002SKurt Kanzenbach struct sk_buff *segs, *nskb; 464497cc002SKurt Kanzenbach int ret; 465497cc002SKurt Kanzenbach 466497cc002SKurt Kanzenbach segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); 467497cc002SKurt Kanzenbach if (IS_ERR_OR_NULL(segs)) 468497cc002SKurt Kanzenbach return qdisc_drop(skb, sch, to_free); 469497cc002SKurt Kanzenbach 470497cc002SKurt Kanzenbach skb_list_walk_safe(segs, segs, nskb) { 471497cc002SKurt Kanzenbach skb_mark_not_on_list(segs); 472497cc002SKurt Kanzenbach qdisc_skb_cb(segs)->pkt_len = segs->len; 473497cc002SKurt Kanzenbach slen += segs->len; 474497cc002SKurt Kanzenbach 475497cc002SKurt Kanzenbach ret = taprio_enqueue_one(segs, sch, child, to_free); 476497cc002SKurt Kanzenbach if (ret != NET_XMIT_SUCCESS) { 477497cc002SKurt Kanzenbach if (net_xmit_drop_count(ret)) 478497cc002SKurt Kanzenbach qdisc_qstats_drop(sch); 479497cc002SKurt Kanzenbach } else { 480497cc002SKurt Kanzenbach numsegs++; 481497cc002SKurt Kanzenbach } 482497cc002SKurt Kanzenbach } 483497cc002SKurt Kanzenbach 484497cc002SKurt Kanzenbach if (numsegs > 1) 485497cc002SKurt Kanzenbach qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen); 486497cc002SKurt Kanzenbach consume_skb(skb); 487497cc002SKurt Kanzenbach 488497cc002SKurt Kanzenbach return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP; 489497cc002SKurt Kanzenbach } 490497cc002SKurt Kanzenbach 491497cc002SKurt Kanzenbach return taprio_enqueue_one(skb, sch, child, to_free); 492497cc002SKurt Kanzenbach } 493497cc002SKurt Kanzenbach 4949c66d156SVinicius Costa Gomes static struct sk_buff *taprio_peek_soft(struct Qdisc *sch) 4955a781ccbSVinicius Costa Gomes { 4965a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 4975a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 4985a781ccbSVinicius Costa Gomes struct sched_entry *entry; 4995a781ccbSVinicius Costa Gomes struct sk_buff *skb; 5005a781ccbSVinicius Costa Gomes u32 gate_mask; 5015a781ccbSVinicius Costa Gomes int i; 5025a781ccbSVinicius Costa Gomes 5035a781ccbSVinicius Costa Gomes rcu_read_lock(); 5045a781ccbSVinicius Costa Gomes entry = rcu_dereference(q->current_entry); 5052684d1b7SAndre Guedes gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 5065a781ccbSVinicius Costa Gomes rcu_read_unlock(); 5075a781ccbSVinicius Costa Gomes 5085a781ccbSVinicius Costa Gomes if (!gate_mask) 5095a781ccbSVinicius Costa Gomes return NULL; 5105a781ccbSVinicius Costa Gomes 5115a781ccbSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 5125a781ccbSVinicius Costa Gomes struct Qdisc *child = q->qdiscs[i]; 5135a781ccbSVinicius Costa Gomes int prio; 5145a781ccbSVinicius Costa Gomes u8 tc; 5155a781ccbSVinicius Costa Gomes 5165a781ccbSVinicius Costa Gomes if (unlikely(!child)) 5175a781ccbSVinicius Costa Gomes continue; 5185a781ccbSVinicius Costa Gomes 5195a781ccbSVinicius Costa Gomes skb = child->ops->peek(child); 5205a781ccbSVinicius Costa Gomes if (!skb) 5215a781ccbSVinicius Costa Gomes continue; 5225a781ccbSVinicius Costa Gomes 5234cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(q->flags)) 5244cfd5779SVedang Patel return skb; 5254cfd5779SVedang Patel 5265a781ccbSVinicius Costa Gomes prio = skb->priority; 5275a781ccbSVinicius Costa Gomes tc = netdev_get_prio_tc_map(dev, prio); 5285a781ccbSVinicius Costa Gomes 5295a781ccbSVinicius Costa Gomes if (!(gate_mask & BIT(tc))) 5302684d1b7SAndre Guedes continue; 5315a781ccbSVinicius Costa Gomes 5325a781ccbSVinicius Costa Gomes return skb; 5335a781ccbSVinicius Costa Gomes } 5345a781ccbSVinicius Costa Gomes 5355a781ccbSVinicius Costa Gomes return NULL; 5365a781ccbSVinicius Costa Gomes } 5375a781ccbSVinicius Costa Gomes 5389c66d156SVinicius Costa Gomes static struct sk_buff *taprio_peek_offload(struct Qdisc *sch) 5399c66d156SVinicius Costa Gomes { 54013511704SYannick Vignon WARN_ONCE(1, "Trying to peek into the root of a taprio qdisc configured with full offload\n"); 5419c66d156SVinicius Costa Gomes 5429c66d156SVinicius Costa Gomes return NULL; 5439c66d156SVinicius Costa Gomes } 5449c66d156SVinicius Costa Gomes 5459c66d156SVinicius Costa Gomes static struct sk_buff *taprio_peek(struct Qdisc *sch) 5469c66d156SVinicius Costa Gomes { 5479c66d156SVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 5489c66d156SVinicius Costa Gomes 5499c66d156SVinicius Costa Gomes return q->peek(sch); 5509c66d156SVinicius Costa Gomes } 5519c66d156SVinicius Costa Gomes 55223bddf69SJakub Kicinski static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry) 55323bddf69SJakub Kicinski { 55423bddf69SJakub Kicinski atomic_set(&entry->budget, 555837ced3aSVladimir Oltean div64_u64((u64)entry->interval * PSEC_PER_NSEC, 55623bddf69SJakub Kicinski atomic64_read(&q->picos_per_byte))); 5575a781ccbSVinicius Costa Gomes } 5585a781ccbSVinicius Costa Gomes 5599c66d156SVinicius Costa Gomes static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch) 5605a781ccbSVinicius Costa Gomes { 5615a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 5625a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 5638c79f0eaSVinicius Costa Gomes struct sk_buff *skb = NULL; 5645a781ccbSVinicius Costa Gomes struct sched_entry *entry; 5655a781ccbSVinicius Costa Gomes u32 gate_mask; 5665a781ccbSVinicius Costa Gomes int i; 5675a781ccbSVinicius Costa Gomes 5685a781ccbSVinicius Costa Gomes rcu_read_lock(); 5695a781ccbSVinicius Costa Gomes entry = rcu_dereference(q->current_entry); 5705a781ccbSVinicius Costa Gomes /* if there's no entry, it means that the schedule didn't 5715a781ccbSVinicius Costa Gomes * start yet, so force all gates to be open, this is in 5725a781ccbSVinicius Costa Gomes * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5 573633fa666SJesper Dangaard Brouer * "AdminGateStates" 5745a781ccbSVinicius Costa Gomes */ 5755a781ccbSVinicius Costa Gomes gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 5765a781ccbSVinicius Costa Gomes 5775a781ccbSVinicius Costa Gomes if (!gate_mask) 5788c79f0eaSVinicius Costa Gomes goto done; 5795a781ccbSVinicius Costa Gomes 5805a781ccbSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 5815a781ccbSVinicius Costa Gomes struct Qdisc *child = q->qdiscs[i]; 5825a781ccbSVinicius Costa Gomes ktime_t guard; 5835a781ccbSVinicius Costa Gomes int prio; 5845a781ccbSVinicius Costa Gomes int len; 5855a781ccbSVinicius Costa Gomes u8 tc; 5865a781ccbSVinicius Costa Gomes 5875a781ccbSVinicius Costa Gomes if (unlikely(!child)) 5885a781ccbSVinicius Costa Gomes continue; 5895a781ccbSVinicius Costa Gomes 5904cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 5914cfd5779SVedang Patel skb = child->ops->dequeue(child); 5924cfd5779SVedang Patel if (!skb) 5934cfd5779SVedang Patel continue; 5944cfd5779SVedang Patel goto skb_found; 5954cfd5779SVedang Patel } 5964cfd5779SVedang Patel 5975a781ccbSVinicius Costa Gomes skb = child->ops->peek(child); 5985a781ccbSVinicius Costa Gomes if (!skb) 5995a781ccbSVinicius Costa Gomes continue; 6005a781ccbSVinicius Costa Gomes 6015a781ccbSVinicius Costa Gomes prio = skb->priority; 6025a781ccbSVinicius Costa Gomes tc = netdev_get_prio_tc_map(dev, prio); 6035a781ccbSVinicius Costa Gomes 604b09fe70eSVinicius Costa Gomes if (!(gate_mask & BIT(tc))) { 605b09fe70eSVinicius Costa Gomes skb = NULL; 6065a781ccbSVinicius Costa Gomes continue; 607b09fe70eSVinicius Costa Gomes } 6085a781ccbSVinicius Costa Gomes 6095a781ccbSVinicius Costa Gomes len = qdisc_pkt_len(skb); 6107ede7b03SVedang Patel guard = ktime_add_ns(taprio_get_time(q), 6115a781ccbSVinicius Costa Gomes length_to_duration(q, len)); 6125a781ccbSVinicius Costa Gomes 6135a781ccbSVinicius Costa Gomes /* In the case that there's no gate entry, there's no 6145a781ccbSVinicius Costa Gomes * guard band ... 6155a781ccbSVinicius Costa Gomes */ 6165a781ccbSVinicius Costa Gomes if (gate_mask != TAPRIO_ALL_GATES_OPEN && 617b09fe70eSVinicius Costa Gomes ktime_after(guard, entry->close_time)) { 618b09fe70eSVinicius Costa Gomes skb = NULL; 6196e734c82SAndre Guedes continue; 620b09fe70eSVinicius Costa Gomes } 6215a781ccbSVinicius Costa Gomes 6225a781ccbSVinicius Costa Gomes /* ... and no budget. */ 6235a781ccbSVinicius Costa Gomes if (gate_mask != TAPRIO_ALL_GATES_OPEN && 624b09fe70eSVinicius Costa Gomes atomic_sub_return(len, &entry->budget) < 0) { 625b09fe70eSVinicius Costa Gomes skb = NULL; 6266e734c82SAndre Guedes continue; 627b09fe70eSVinicius Costa Gomes } 6285a781ccbSVinicius Costa Gomes 6295a781ccbSVinicius Costa Gomes skb = child->ops->dequeue(child); 6305a781ccbSVinicius Costa Gomes if (unlikely(!skb)) 6318c79f0eaSVinicius Costa Gomes goto done; 6325a781ccbSVinicius Costa Gomes 6334cfd5779SVedang Patel skb_found: 6345a781ccbSVinicius Costa Gomes qdisc_bstats_update(sch, skb); 6355a781ccbSVinicius Costa Gomes qdisc_qstats_backlog_dec(sch, skb); 6365a781ccbSVinicius Costa Gomes sch->q.qlen--; 6375a781ccbSVinicius Costa Gomes 6388c79f0eaSVinicius Costa Gomes goto done; 6395a781ccbSVinicius Costa Gomes } 6405a781ccbSVinicius Costa Gomes 6418c79f0eaSVinicius Costa Gomes done: 6428c79f0eaSVinicius Costa Gomes rcu_read_unlock(); 6438c79f0eaSVinicius Costa Gomes 6448c79f0eaSVinicius Costa Gomes return skb; 6455a781ccbSVinicius Costa Gomes } 6465a781ccbSVinicius Costa Gomes 6479c66d156SVinicius Costa Gomes static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch) 6489c66d156SVinicius Costa Gomes { 64913511704SYannick Vignon WARN_ONCE(1, "Trying to dequeue from the root of a taprio qdisc configured with full offload\n"); 6509c66d156SVinicius Costa Gomes 6519c66d156SVinicius Costa Gomes return NULL; 6529c66d156SVinicius Costa Gomes } 6539c66d156SVinicius Costa Gomes 6549c66d156SVinicius Costa Gomes static struct sk_buff *taprio_dequeue(struct Qdisc *sch) 6559c66d156SVinicius Costa Gomes { 6569c66d156SVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 6579c66d156SVinicius Costa Gomes 6589c66d156SVinicius Costa Gomes return q->dequeue(sch); 6599c66d156SVinicius Costa Gomes } 6609c66d156SVinicius Costa Gomes 6616ca6a665SVinicius Costa Gomes static bool should_restart_cycle(const struct sched_gate_list *oper, 6626ca6a665SVinicius Costa Gomes const struct sched_entry *entry) 6636ca6a665SVinicius Costa Gomes { 6646ca6a665SVinicius Costa Gomes if (list_is_last(&entry->list, &oper->entries)) 6656ca6a665SVinicius Costa Gomes return true; 6666ca6a665SVinicius Costa Gomes 6676ca6a665SVinicius Costa Gomes if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0) 6686ca6a665SVinicius Costa Gomes return true; 6696ca6a665SVinicius Costa Gomes 6706ca6a665SVinicius Costa Gomes return false; 6716ca6a665SVinicius Costa Gomes } 6726ca6a665SVinicius Costa Gomes 673a3d43c0dSVinicius Costa Gomes static bool should_change_schedules(const struct sched_gate_list *admin, 674a3d43c0dSVinicius Costa Gomes const struct sched_gate_list *oper, 675a3d43c0dSVinicius Costa Gomes ktime_t close_time) 676a3d43c0dSVinicius Costa Gomes { 677c25031e9SVinicius Costa Gomes ktime_t next_base_time, extension_time; 678a3d43c0dSVinicius Costa Gomes 679a3d43c0dSVinicius Costa Gomes if (!admin) 680a3d43c0dSVinicius Costa Gomes return false; 681a3d43c0dSVinicius Costa Gomes 682a3d43c0dSVinicius Costa Gomes next_base_time = sched_base_time(admin); 683a3d43c0dSVinicius Costa Gomes 684a3d43c0dSVinicius Costa Gomes /* This is the simple case, the close_time would fall after 685a3d43c0dSVinicius Costa Gomes * the next schedule base_time. 686a3d43c0dSVinicius Costa Gomes */ 687a3d43c0dSVinicius Costa Gomes if (ktime_compare(next_base_time, close_time) <= 0) 688a3d43c0dSVinicius Costa Gomes return true; 689a3d43c0dSVinicius Costa Gomes 690c25031e9SVinicius Costa Gomes /* This is the cycle_time_extension case, if the close_time 691c25031e9SVinicius Costa Gomes * plus the amount that can be extended would fall after the 692c25031e9SVinicius Costa Gomes * next schedule base_time, we can extend the current schedule 693c25031e9SVinicius Costa Gomes * for that amount. 694c25031e9SVinicius Costa Gomes */ 695c25031e9SVinicius Costa Gomes extension_time = ktime_add_ns(close_time, oper->cycle_time_extension); 696c25031e9SVinicius Costa Gomes 697c25031e9SVinicius Costa Gomes /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about 698c25031e9SVinicius Costa Gomes * how precisely the extension should be made. So after 699c25031e9SVinicius Costa Gomes * conformance testing, this logic may change. 700c25031e9SVinicius Costa Gomes */ 701c25031e9SVinicius Costa Gomes if (ktime_compare(next_base_time, extension_time) <= 0) 702c25031e9SVinicius Costa Gomes return true; 703c25031e9SVinicius Costa Gomes 704a3d43c0dSVinicius Costa Gomes return false; 705a3d43c0dSVinicius Costa Gomes } 706a3d43c0dSVinicius Costa Gomes 7075a781ccbSVinicius Costa Gomes static enum hrtimer_restart advance_sched(struct hrtimer *timer) 7085a781ccbSVinicius Costa Gomes { 7095a781ccbSVinicius Costa Gomes struct taprio_sched *q = container_of(timer, struct taprio_sched, 7105a781ccbSVinicius Costa Gomes advance_timer); 711a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin; 7125a781ccbSVinicius Costa Gomes struct sched_entry *entry, *next; 7135a781ccbSVinicius Costa Gomes struct Qdisc *sch = q->root; 7145a781ccbSVinicius Costa Gomes ktime_t close_time; 7155a781ccbSVinicius Costa Gomes 7165a781ccbSVinicius Costa Gomes spin_lock(&q->current_entry_lock); 7175a781ccbSVinicius Costa Gomes entry = rcu_dereference_protected(q->current_entry, 7185a781ccbSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 719a3d43c0dSVinicius Costa Gomes oper = rcu_dereference_protected(q->oper_sched, 720a3d43c0dSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 721a3d43c0dSVinicius Costa Gomes admin = rcu_dereference_protected(q->admin_sched, 722a3d43c0dSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 7235a781ccbSVinicius Costa Gomes 724a3d43c0dSVinicius Costa Gomes if (!oper) 725a3d43c0dSVinicius Costa Gomes switch_schedules(q, &admin, &oper); 726a3d43c0dSVinicius Costa Gomes 727a3d43c0dSVinicius Costa Gomes /* This can happen in two cases: 1. this is the very first run 728a3d43c0dSVinicius Costa Gomes * of this function (i.e. we weren't running any schedule 729a3d43c0dSVinicius Costa Gomes * previously); 2. The previous schedule just ended. The first 730a3d43c0dSVinicius Costa Gomes * entry of all schedules are pre-calculated during the 731a3d43c0dSVinicius Costa Gomes * schedule initialization. 7325a781ccbSVinicius Costa Gomes */ 733a3d43c0dSVinicius Costa Gomes if (unlikely(!entry || entry->close_time == oper->base_time)) { 734a3d43c0dSVinicius Costa Gomes next = list_first_entry(&oper->entries, struct sched_entry, 7355a781ccbSVinicius Costa Gomes list); 7365a781ccbSVinicius Costa Gomes close_time = next->close_time; 7375a781ccbSVinicius Costa Gomes goto first_run; 7385a781ccbSVinicius Costa Gomes } 7395a781ccbSVinicius Costa Gomes 7406ca6a665SVinicius Costa Gomes if (should_restart_cycle(oper, entry)) { 741a3d43c0dSVinicius Costa Gomes next = list_first_entry(&oper->entries, struct sched_entry, 7425a781ccbSVinicius Costa Gomes list); 7436ca6a665SVinicius Costa Gomes oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time, 7446ca6a665SVinicius Costa Gomes oper->cycle_time); 7456ca6a665SVinicius Costa Gomes } else { 7465a781ccbSVinicius Costa Gomes next = list_next_entry(entry, list); 7476ca6a665SVinicius Costa Gomes } 7485a781ccbSVinicius Costa Gomes 7495a781ccbSVinicius Costa Gomes close_time = ktime_add_ns(entry->close_time, next->interval); 7506ca6a665SVinicius Costa Gomes close_time = min_t(ktime_t, close_time, oper->cycle_close_time); 7515a781ccbSVinicius Costa Gomes 752a3d43c0dSVinicius Costa Gomes if (should_change_schedules(admin, oper, close_time)) { 753a3d43c0dSVinicius Costa Gomes /* Set things so the next time this runs, the new 754a3d43c0dSVinicius Costa Gomes * schedule runs. 755a3d43c0dSVinicius Costa Gomes */ 756a3d43c0dSVinicius Costa Gomes close_time = sched_base_time(admin); 757a3d43c0dSVinicius Costa Gomes switch_schedules(q, &admin, &oper); 758a3d43c0dSVinicius Costa Gomes } 759a3d43c0dSVinicius Costa Gomes 7605a781ccbSVinicius Costa Gomes next->close_time = close_time; 76123bddf69SJakub Kicinski taprio_set_budget(q, next); 7625a781ccbSVinicius Costa Gomes 7635a781ccbSVinicius Costa Gomes first_run: 7645a781ccbSVinicius Costa Gomes rcu_assign_pointer(q->current_entry, next); 7655a781ccbSVinicius Costa Gomes spin_unlock(&q->current_entry_lock); 7665a781ccbSVinicius Costa Gomes 7675a781ccbSVinicius Costa Gomes hrtimer_set_expires(&q->advance_timer, close_time); 7685a781ccbSVinicius Costa Gomes 7695a781ccbSVinicius Costa Gomes rcu_read_lock(); 7705a781ccbSVinicius Costa Gomes __netif_schedule(sch); 7715a781ccbSVinicius Costa Gomes rcu_read_unlock(); 7725a781ccbSVinicius Costa Gomes 7735a781ccbSVinicius Costa Gomes return HRTIMER_RESTART; 7745a781ccbSVinicius Costa Gomes } 7755a781ccbSVinicius Costa Gomes 7765a781ccbSVinicius Costa Gomes static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { 7775a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 }, 7785a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 }, 7795a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 }, 7805a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 }, 7815a781ccbSVinicius Costa Gomes }; 7825a781ccbSVinicius Costa Gomes 7835a781ccbSVinicius Costa Gomes static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = { 7845a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_PRIOMAP] = { 7855a781ccbSVinicius Costa Gomes .len = sizeof(struct tc_mqprio_qopt) 7865a781ccbSVinicius Costa Gomes }, 7875a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED }, 7885a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 }, 7895a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED }, 7905a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 }, 7916ca6a665SVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 }, 792c25031e9SVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 }, 79349c684d7SVinicius Costa Gomes [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 }, 794e13aaa06SJakub Kicinski [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 }, 7955a781ccbSVinicius Costa Gomes }; 7965a781ccbSVinicius Costa Gomes 797b5b73b26SVinicius Costa Gomes static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb, 798b5b73b26SVinicius Costa Gomes struct sched_entry *entry, 7995a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 8005a781ccbSVinicius Costa Gomes { 801b5b73b26SVinicius Costa Gomes int min_duration = length_to_duration(q, ETH_ZLEN); 8025a781ccbSVinicius Costa Gomes u32 interval = 0; 8035a781ccbSVinicius Costa Gomes 8045a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD]) 8055a781ccbSVinicius Costa Gomes entry->command = nla_get_u8( 8065a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_CMD]); 8075a781ccbSVinicius Costa Gomes 8085a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]) 8095a781ccbSVinicius Costa Gomes entry->gate_mask = nla_get_u32( 8105a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]); 8115a781ccbSVinicius Costa Gomes 8125a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]) 8135a781ccbSVinicius Costa Gomes interval = nla_get_u32( 8145a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]); 8155a781ccbSVinicius Costa Gomes 816b5b73b26SVinicius Costa Gomes /* The interval should allow at least the minimum ethernet 817b5b73b26SVinicius Costa Gomes * frame to go out. 818b5b73b26SVinicius Costa Gomes */ 819b5b73b26SVinicius Costa Gomes if (interval < min_duration) { 8205a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry"); 8215a781ccbSVinicius Costa Gomes return -EINVAL; 8225a781ccbSVinicius Costa Gomes } 8235a781ccbSVinicius Costa Gomes 8245a781ccbSVinicius Costa Gomes entry->interval = interval; 8255a781ccbSVinicius Costa Gomes 8265a781ccbSVinicius Costa Gomes return 0; 8275a781ccbSVinicius Costa Gomes } 8285a781ccbSVinicius Costa Gomes 829b5b73b26SVinicius Costa Gomes static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n, 830b5b73b26SVinicius Costa Gomes struct sched_entry *entry, int index, 831b5b73b26SVinicius Costa Gomes struct netlink_ext_ack *extack) 8325a781ccbSVinicius Costa Gomes { 8335a781ccbSVinicius Costa Gomes struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { }; 8345a781ccbSVinicius Costa Gomes int err; 8355a781ccbSVinicius Costa Gomes 8368cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n, 8375a781ccbSVinicius Costa Gomes entry_policy, NULL); 8385a781ccbSVinicius Costa Gomes if (err < 0) { 8395a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Could not parse nested entry"); 8405a781ccbSVinicius Costa Gomes return -EINVAL; 8415a781ccbSVinicius Costa Gomes } 8425a781ccbSVinicius Costa Gomes 8435a781ccbSVinicius Costa Gomes entry->index = index; 8445a781ccbSVinicius Costa Gomes 845b5b73b26SVinicius Costa Gomes return fill_sched_entry(q, tb, entry, extack); 8465a781ccbSVinicius Costa Gomes } 8475a781ccbSVinicius Costa Gomes 848b5b73b26SVinicius Costa Gomes static int parse_sched_list(struct taprio_sched *q, struct nlattr *list, 849a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, 8505a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 8515a781ccbSVinicius Costa Gomes { 8525a781ccbSVinicius Costa Gomes struct nlattr *n; 8535a781ccbSVinicius Costa Gomes int err, rem; 8545a781ccbSVinicius Costa Gomes int i = 0; 8555a781ccbSVinicius Costa Gomes 8565a781ccbSVinicius Costa Gomes if (!list) 8575a781ccbSVinicius Costa Gomes return -EINVAL; 8585a781ccbSVinicius Costa Gomes 8595a781ccbSVinicius Costa Gomes nla_for_each_nested(n, list, rem) { 8605a781ccbSVinicius Costa Gomes struct sched_entry *entry; 8615a781ccbSVinicius Costa Gomes 8625a781ccbSVinicius Costa Gomes if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) { 8635a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'"); 8645a781ccbSVinicius Costa Gomes continue; 8655a781ccbSVinicius Costa Gomes } 8665a781ccbSVinicius Costa Gomes 8675a781ccbSVinicius Costa Gomes entry = kzalloc(sizeof(*entry), GFP_KERNEL); 8685a781ccbSVinicius Costa Gomes if (!entry) { 8695a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Not enough memory for entry"); 8705a781ccbSVinicius Costa Gomes return -ENOMEM; 8715a781ccbSVinicius Costa Gomes } 8725a781ccbSVinicius Costa Gomes 873b5b73b26SVinicius Costa Gomes err = parse_sched_entry(q, n, entry, i, extack); 8745a781ccbSVinicius Costa Gomes if (err < 0) { 8755a781ccbSVinicius Costa Gomes kfree(entry); 8765a781ccbSVinicius Costa Gomes return err; 8775a781ccbSVinicius Costa Gomes } 8785a781ccbSVinicius Costa Gomes 879a3d43c0dSVinicius Costa Gomes list_add_tail(&entry->list, &sched->entries); 8805a781ccbSVinicius Costa Gomes i++; 8815a781ccbSVinicius Costa Gomes } 8825a781ccbSVinicius Costa Gomes 883a3d43c0dSVinicius Costa Gomes sched->num_entries = i; 8845a781ccbSVinicius Costa Gomes 8855a781ccbSVinicius Costa Gomes return i; 8865a781ccbSVinicius Costa Gomes } 8875a781ccbSVinicius Costa Gomes 888b5b73b26SVinicius Costa Gomes static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb, 889a3d43c0dSVinicius Costa Gomes struct sched_gate_list *new, 8905a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 8915a781ccbSVinicius Costa Gomes { 8925a781ccbSVinicius Costa Gomes int err = 0; 8935a781ccbSVinicius Costa Gomes 894a3d43c0dSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) { 895a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Adding a single entry is not supported"); 896a3d43c0dSVinicius Costa Gomes return -ENOTSUPP; 897a3d43c0dSVinicius Costa Gomes } 8985a781ccbSVinicius Costa Gomes 8995a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]) 900a3d43c0dSVinicius Costa Gomes new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]); 9015a781ccbSVinicius Costa Gomes 902c25031e9SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]) 903c25031e9SVinicius Costa Gomes new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]); 904c25031e9SVinicius Costa Gomes 9056ca6a665SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]) 9066ca6a665SVinicius Costa Gomes new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]); 9076ca6a665SVinicius Costa Gomes 9085a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]) 909b5b73b26SVinicius Costa Gomes err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], 910b5b73b26SVinicius Costa Gomes new, extack); 911a3d43c0dSVinicius Costa Gomes if (err < 0) 9125a781ccbSVinicius Costa Gomes return err; 913a3d43c0dSVinicius Costa Gomes 914037be037SVedang Patel if (!new->cycle_time) { 915037be037SVedang Patel struct sched_entry *entry; 916037be037SVedang Patel ktime_t cycle = 0; 917037be037SVedang Patel 918037be037SVedang Patel list_for_each_entry(entry, &new->entries, list) 919037be037SVedang Patel cycle = ktime_add_ns(cycle, entry->interval); 920ed8157f1SDu Cheng 921ed8157f1SDu Cheng if (!cycle) { 922ed8157f1SDu Cheng NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0"); 923ed8157f1SDu Cheng return -EINVAL; 924ed8157f1SDu Cheng } 925ed8157f1SDu Cheng 926037be037SVedang Patel new->cycle_time = cycle; 927037be037SVedang Patel } 928037be037SVedang Patel 929a3d43c0dSVinicius Costa Gomes return 0; 9305a781ccbSVinicius Costa Gomes } 9315a781ccbSVinicius Costa Gomes 9325a781ccbSVinicius Costa Gomes static int taprio_parse_mqprio_opt(struct net_device *dev, 9335a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt *qopt, 9344cfd5779SVedang Patel struct netlink_ext_ack *extack, 9354cfd5779SVedang Patel u32 taprio_flags) 9365a781ccbSVinicius Costa Gomes { 9375a781ccbSVinicius Costa Gomes int i, j; 9385a781ccbSVinicius Costa Gomes 939a3d43c0dSVinicius Costa Gomes if (!qopt && !dev->num_tc) { 9405a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary"); 9415a781ccbSVinicius Costa Gomes return -EINVAL; 9425a781ccbSVinicius Costa Gomes } 9435a781ccbSVinicius Costa Gomes 944a3d43c0dSVinicius Costa Gomes /* If num_tc is already set, it means that the user already 945a3d43c0dSVinicius Costa Gomes * configured the mqprio part 946a3d43c0dSVinicius Costa Gomes */ 947a3d43c0dSVinicius Costa Gomes if (dev->num_tc) 948a3d43c0dSVinicius Costa Gomes return 0; 949a3d43c0dSVinicius Costa Gomes 9505a781ccbSVinicius Costa Gomes /* Verify num_tc is not out of max range */ 9515a781ccbSVinicius Costa Gomes if (qopt->num_tc > TC_MAX_QUEUE) { 9525a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range"); 9535a781ccbSVinicius Costa Gomes return -EINVAL; 9545a781ccbSVinicius Costa Gomes } 9555a781ccbSVinicius Costa Gomes 9565a781ccbSVinicius Costa Gomes /* taprio imposes that traffic classes map 1:n to tx queues */ 9575a781ccbSVinicius Costa Gomes if (qopt->num_tc > dev->num_tx_queues) { 9585a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues"); 9595a781ccbSVinicius Costa Gomes return -EINVAL; 9605a781ccbSVinicius Costa Gomes } 9615a781ccbSVinicius Costa Gomes 9625a781ccbSVinicius Costa Gomes /* Verify priority mapping uses valid tcs */ 963b5a0faa3SIvan Khoronzhuk for (i = 0; i <= TC_BITMASK; i++) { 9645a781ccbSVinicius Costa Gomes if (qopt->prio_tc_map[i] >= qopt->num_tc) { 9655a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping"); 9665a781ccbSVinicius Costa Gomes return -EINVAL; 9675a781ccbSVinicius Costa Gomes } 9685a781ccbSVinicius Costa Gomes } 9695a781ccbSVinicius Costa Gomes 9705a781ccbSVinicius Costa Gomes for (i = 0; i < qopt->num_tc; i++) { 9715a781ccbSVinicius Costa Gomes unsigned int last = qopt->offset[i] + qopt->count[i]; 9725a781ccbSVinicius Costa Gomes 9735a781ccbSVinicius Costa Gomes /* Verify the queue count is in tx range being equal to the 9745a781ccbSVinicius Costa Gomes * real_num_tx_queues indicates the last queue is in use. 9755a781ccbSVinicius Costa Gomes */ 9765a781ccbSVinicius Costa Gomes if (qopt->offset[i] >= dev->num_tx_queues || 9775a781ccbSVinicius Costa Gomes !qopt->count[i] || 9785a781ccbSVinicius Costa Gomes last > dev->real_num_tx_queues) { 9795a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping"); 9805a781ccbSVinicius Costa Gomes return -EINVAL; 9815a781ccbSVinicius Costa Gomes } 9825a781ccbSVinicius Costa Gomes 9834cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(taprio_flags)) 9844cfd5779SVedang Patel continue; 9854cfd5779SVedang Patel 9865a781ccbSVinicius Costa Gomes /* Verify that the offset and counts do not overlap */ 9875a781ccbSVinicius Costa Gomes for (j = i + 1; j < qopt->num_tc; j++) { 9885a781ccbSVinicius Costa Gomes if (last > qopt->offset[j]) { 9895a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping"); 9905a781ccbSVinicius Costa Gomes return -EINVAL; 9915a781ccbSVinicius Costa Gomes } 9925a781ccbSVinicius Costa Gomes } 9935a781ccbSVinicius Costa Gomes } 9945a781ccbSVinicius Costa Gomes 9955a781ccbSVinicius Costa Gomes return 0; 9965a781ccbSVinicius Costa Gomes } 9975a781ccbSVinicius Costa Gomes 998a3d43c0dSVinicius Costa Gomes static int taprio_get_start_time(struct Qdisc *sch, 999a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, 1000a3d43c0dSVinicius Costa Gomes ktime_t *start) 10015a781ccbSVinicius Costa Gomes { 10025a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 10035a781ccbSVinicius Costa Gomes ktime_t now, base, cycle; 10045a781ccbSVinicius Costa Gomes s64 n; 10055a781ccbSVinicius Costa Gomes 1006a3d43c0dSVinicius Costa Gomes base = sched_base_time(sched); 10077ede7b03SVedang Patel now = taprio_get_time(q); 10088599099fSAndre Guedes 10098599099fSAndre Guedes if (ktime_after(base, now)) { 10108599099fSAndre Guedes *start = base; 10118599099fSAndre Guedes return 0; 10128599099fSAndre Guedes } 10135a781ccbSVinicius Costa Gomes 1014037be037SVedang Patel cycle = sched->cycle_time; 10155a781ccbSVinicius Costa Gomes 10168599099fSAndre Guedes /* The qdisc is expected to have at least one sched_entry. Moreover, 10178599099fSAndre Guedes * any entry must have 'interval' > 0. Thus if the cycle time is zero, 10188599099fSAndre Guedes * something went really wrong. In that case, we should warn about this 10198599099fSAndre Guedes * inconsistent state and return error. 10208599099fSAndre Guedes */ 10218599099fSAndre Guedes if (WARN_ON(!cycle)) 10228599099fSAndre Guedes return -EFAULT; 10235a781ccbSVinicius Costa Gomes 10245a781ccbSVinicius Costa Gomes /* Schedule the start time for the beginning of the next 10255a781ccbSVinicius Costa Gomes * cycle. 10265a781ccbSVinicius Costa Gomes */ 10275a781ccbSVinicius Costa Gomes n = div64_s64(ktime_sub_ns(now, base), cycle); 10288599099fSAndre Guedes *start = ktime_add_ns(base, (n + 1) * cycle); 10298599099fSAndre Guedes return 0; 10305a781ccbSVinicius Costa Gomes } 10315a781ccbSVinicius Costa Gomes 1032a3d43c0dSVinicius Costa Gomes static void setup_first_close_time(struct taprio_sched *q, 1033a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, ktime_t base) 10345a781ccbSVinicius Costa Gomes { 10355a781ccbSVinicius Costa Gomes struct sched_entry *first; 10366ca6a665SVinicius Costa Gomes ktime_t cycle; 10375a781ccbSVinicius Costa Gomes 1038a3d43c0dSVinicius Costa Gomes first = list_first_entry(&sched->entries, 1039a3d43c0dSVinicius Costa Gomes struct sched_entry, list); 10405a781ccbSVinicius Costa Gomes 1041037be037SVedang Patel cycle = sched->cycle_time; 10426ca6a665SVinicius Costa Gomes 10436ca6a665SVinicius Costa Gomes /* FIXME: find a better place to do this */ 10446ca6a665SVinicius Costa Gomes sched->cycle_close_time = ktime_add_ns(base, cycle); 10456ca6a665SVinicius Costa Gomes 1046a3d43c0dSVinicius Costa Gomes first->close_time = ktime_add_ns(base, first->interval); 104723bddf69SJakub Kicinski taprio_set_budget(q, first); 10485a781ccbSVinicius Costa Gomes rcu_assign_pointer(q->current_entry, NULL); 1049a3d43c0dSVinicius Costa Gomes } 10505a781ccbSVinicius Costa Gomes 1051a3d43c0dSVinicius Costa Gomes static void taprio_start_sched(struct Qdisc *sch, 1052a3d43c0dSVinicius Costa Gomes ktime_t start, struct sched_gate_list *new) 1053a3d43c0dSVinicius Costa Gomes { 1054a3d43c0dSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 1055a3d43c0dSVinicius Costa Gomes ktime_t expires; 1056a3d43c0dSVinicius Costa Gomes 10579c66d156SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 10589c66d156SVinicius Costa Gomes return; 10599c66d156SVinicius Costa Gomes 1060a3d43c0dSVinicius Costa Gomes expires = hrtimer_get_expires(&q->advance_timer); 1061a3d43c0dSVinicius Costa Gomes if (expires == 0) 1062a3d43c0dSVinicius Costa Gomes expires = KTIME_MAX; 1063a3d43c0dSVinicius Costa Gomes 1064a3d43c0dSVinicius Costa Gomes /* If the new schedule starts before the next expiration, we 1065a3d43c0dSVinicius Costa Gomes * reprogram it to the earliest one, so we change the admin 1066a3d43c0dSVinicius Costa Gomes * schedule to the operational one at the right time. 1067a3d43c0dSVinicius Costa Gomes */ 1068a3d43c0dSVinicius Costa Gomes start = min_t(ktime_t, start, expires); 10695a781ccbSVinicius Costa Gomes 10705a781ccbSVinicius Costa Gomes hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS); 10715a781ccbSVinicius Costa Gomes } 10725a781ccbSVinicius Costa Gomes 10737b9eba7bSLeandro Dorileo static void taprio_set_picos_per_byte(struct net_device *dev, 10747b9eba7bSLeandro Dorileo struct taprio_sched *q) 10757b9eba7bSLeandro Dorileo { 10767b9eba7bSLeandro Dorileo struct ethtool_link_ksettings ecmd; 1077f04b514cSVladimir Oltean int speed = SPEED_10; 1078f04b514cSVladimir Oltean int picos_per_byte; 1079f04b514cSVladimir Oltean int err; 10807b9eba7bSLeandro Dorileo 1081f04b514cSVladimir Oltean err = __ethtool_get_link_ksettings(dev, &ecmd); 1082f04b514cSVladimir Oltean if (err < 0) 1083f04b514cSVladimir Oltean goto skip; 1084f04b514cSVladimir Oltean 10859a9251a3SVladimir Oltean if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN) 1086f04b514cSVladimir Oltean speed = ecmd.base.speed; 1087f04b514cSVladimir Oltean 1088f04b514cSVladimir Oltean skip: 108968ce6688SVladimir Oltean picos_per_byte = (USEC_PER_SEC * 8) / speed; 10907b9eba7bSLeandro Dorileo 10917b9eba7bSLeandro Dorileo atomic64_set(&q->picos_per_byte, picos_per_byte); 10927b9eba7bSLeandro Dorileo netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n", 10937b9eba7bSLeandro Dorileo dev->name, (long long)atomic64_read(&q->picos_per_byte), 10947b9eba7bSLeandro Dorileo ecmd.base.speed); 10957b9eba7bSLeandro Dorileo } 10967b9eba7bSLeandro Dorileo 10977b9eba7bSLeandro Dorileo static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event, 10987b9eba7bSLeandro Dorileo void *ptr) 10997b9eba7bSLeandro Dorileo { 11007b9eba7bSLeandro Dorileo struct net_device *dev = netdev_notifier_info_to_dev(ptr); 11017b9eba7bSLeandro Dorileo struct net_device *qdev; 11027b9eba7bSLeandro Dorileo struct taprio_sched *q; 11037b9eba7bSLeandro Dorileo bool found = false; 11047b9eba7bSLeandro Dorileo 11057b9eba7bSLeandro Dorileo ASSERT_RTNL(); 11067b9eba7bSLeandro Dorileo 11077b9eba7bSLeandro Dorileo if (event != NETDEV_UP && event != NETDEV_CHANGE) 11087b9eba7bSLeandro Dorileo return NOTIFY_DONE; 11097b9eba7bSLeandro Dorileo 11107b9eba7bSLeandro Dorileo spin_lock(&taprio_list_lock); 11117b9eba7bSLeandro Dorileo list_for_each_entry(q, &taprio_list, taprio_list) { 11127b9eba7bSLeandro Dorileo qdev = qdisc_dev(q->root); 11137b9eba7bSLeandro Dorileo if (qdev == dev) { 11147b9eba7bSLeandro Dorileo found = true; 11157b9eba7bSLeandro Dorileo break; 11167b9eba7bSLeandro Dorileo } 11177b9eba7bSLeandro Dorileo } 11187b9eba7bSLeandro Dorileo spin_unlock(&taprio_list_lock); 11197b9eba7bSLeandro Dorileo 11207b9eba7bSLeandro Dorileo if (found) 11217b9eba7bSLeandro Dorileo taprio_set_picos_per_byte(dev, q); 11227b9eba7bSLeandro Dorileo 11237b9eba7bSLeandro Dorileo return NOTIFY_DONE; 11247b9eba7bSLeandro Dorileo } 11257b9eba7bSLeandro Dorileo 11264cfd5779SVedang Patel static void setup_txtime(struct taprio_sched *q, 11274cfd5779SVedang Patel struct sched_gate_list *sched, ktime_t base) 11284cfd5779SVedang Patel { 11294cfd5779SVedang Patel struct sched_entry *entry; 11304cfd5779SVedang Patel u32 interval = 0; 11314cfd5779SVedang Patel 11324cfd5779SVedang Patel list_for_each_entry(entry, &sched->entries, list) { 11334cfd5779SVedang Patel entry->next_txtime = ktime_add_ns(base, interval); 11344cfd5779SVedang Patel interval += entry->interval; 11354cfd5779SVedang Patel } 11364cfd5779SVedang Patel } 11374cfd5779SVedang Patel 11389c66d156SVinicius Costa Gomes static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries) 11399c66d156SVinicius Costa Gomes { 11409c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload *__offload; 11419c66d156SVinicius Costa Gomes 114211a33de2SGustavo A. R. Silva __offload = kzalloc(struct_size(__offload, offload.entries, num_entries), 114311a33de2SGustavo A. R. Silva GFP_KERNEL); 11449c66d156SVinicius Costa Gomes if (!__offload) 11459c66d156SVinicius Costa Gomes return NULL; 11469c66d156SVinicius Costa Gomes 11479c66d156SVinicius Costa Gomes refcount_set(&__offload->users, 1); 11489c66d156SVinicius Costa Gomes 11499c66d156SVinicius Costa Gomes return &__offload->offload; 11509c66d156SVinicius Costa Gomes } 11519c66d156SVinicius Costa Gomes 11529c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload 11539c66d156SVinicius Costa Gomes *offload) 11549c66d156SVinicius Costa Gomes { 11559c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload *__offload; 11569c66d156SVinicius Costa Gomes 11579c66d156SVinicius Costa Gomes __offload = container_of(offload, struct __tc_taprio_qopt_offload, 11589c66d156SVinicius Costa Gomes offload); 11599c66d156SVinicius Costa Gomes 11609c66d156SVinicius Costa Gomes refcount_inc(&__offload->users); 11619c66d156SVinicius Costa Gomes 11629c66d156SVinicius Costa Gomes return offload; 11639c66d156SVinicius Costa Gomes } 11649c66d156SVinicius Costa Gomes EXPORT_SYMBOL_GPL(taprio_offload_get); 11659c66d156SVinicius Costa Gomes 11669c66d156SVinicius Costa Gomes void taprio_offload_free(struct tc_taprio_qopt_offload *offload) 11679c66d156SVinicius Costa Gomes { 11689c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload *__offload; 11699c66d156SVinicius Costa Gomes 11709c66d156SVinicius Costa Gomes __offload = container_of(offload, struct __tc_taprio_qopt_offload, 11719c66d156SVinicius Costa Gomes offload); 11729c66d156SVinicius Costa Gomes 11739c66d156SVinicius Costa Gomes if (!refcount_dec_and_test(&__offload->users)) 11749c66d156SVinicius Costa Gomes return; 11759c66d156SVinicius Costa Gomes 11769c66d156SVinicius Costa Gomes kfree(__offload); 11779c66d156SVinicius Costa Gomes } 11789c66d156SVinicius Costa Gomes EXPORT_SYMBOL_GPL(taprio_offload_free); 11799c66d156SVinicius Costa Gomes 11809c66d156SVinicius Costa Gomes /* The function will only serve to keep the pointers to the "oper" and "admin" 11819c66d156SVinicius Costa Gomes * schedules valid in relation to their base times, so when calling dump() the 11829c66d156SVinicius Costa Gomes * users looks at the right schedules. 11839c66d156SVinicius Costa Gomes * When using full offload, the admin configuration is promoted to oper at the 11849c66d156SVinicius Costa Gomes * base_time in the PHC time domain. But because the system time is not 11859c66d156SVinicius Costa Gomes * necessarily in sync with that, we can't just trigger a hrtimer to call 11869c66d156SVinicius Costa Gomes * switch_schedules at the right hardware time. 11879c66d156SVinicius Costa Gomes * At the moment we call this by hand right away from taprio, but in the future 11889c66d156SVinicius Costa Gomes * it will be useful to create a mechanism for drivers to notify taprio of the 11899c66d156SVinicius Costa Gomes * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump(). 11909c66d156SVinicius Costa Gomes * This is left as TODO. 11919c66d156SVinicius Costa Gomes */ 1192d665c128SYi Wang static void taprio_offload_config_changed(struct taprio_sched *q) 11939c66d156SVinicius Costa Gomes { 11949c66d156SVinicius Costa Gomes struct sched_gate_list *oper, *admin; 11959c66d156SVinicius Costa Gomes 1196c8cbe123SVladimir Oltean oper = rtnl_dereference(q->oper_sched); 1197c8cbe123SVladimir Oltean admin = rtnl_dereference(q->admin_sched); 11989c66d156SVinicius Costa Gomes 11999c66d156SVinicius Costa Gomes switch_schedules(q, &admin, &oper); 12009c66d156SVinicius Costa Gomes } 12019c66d156SVinicius Costa Gomes 120209e31cf0SVinicius Costa Gomes static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask) 120309e31cf0SVinicius Costa Gomes { 120409e31cf0SVinicius Costa Gomes u32 i, queue_mask = 0; 120509e31cf0SVinicius Costa Gomes 120609e31cf0SVinicius Costa Gomes for (i = 0; i < dev->num_tc; i++) { 120709e31cf0SVinicius Costa Gomes u32 offset, count; 120809e31cf0SVinicius Costa Gomes 120909e31cf0SVinicius Costa Gomes if (!(tc_mask & BIT(i))) 121009e31cf0SVinicius Costa Gomes continue; 121109e31cf0SVinicius Costa Gomes 121209e31cf0SVinicius Costa Gomes offset = dev->tc_to_txq[i].offset; 121309e31cf0SVinicius Costa Gomes count = dev->tc_to_txq[i].count; 121409e31cf0SVinicius Costa Gomes 121509e31cf0SVinicius Costa Gomes queue_mask |= GENMASK(offset + count - 1, offset); 121609e31cf0SVinicius Costa Gomes } 121709e31cf0SVinicius Costa Gomes 121809e31cf0SVinicius Costa Gomes return queue_mask; 121909e31cf0SVinicius Costa Gomes } 122009e31cf0SVinicius Costa Gomes 122109e31cf0SVinicius Costa Gomes static void taprio_sched_to_offload(struct net_device *dev, 12229c66d156SVinicius Costa Gomes struct sched_gate_list *sched, 12239c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *offload) 12249c66d156SVinicius Costa Gomes { 12259c66d156SVinicius Costa Gomes struct sched_entry *entry; 12269c66d156SVinicius Costa Gomes int i = 0; 12279c66d156SVinicius Costa Gomes 12289c66d156SVinicius Costa Gomes offload->base_time = sched->base_time; 12299c66d156SVinicius Costa Gomes offload->cycle_time = sched->cycle_time; 12309c66d156SVinicius Costa Gomes offload->cycle_time_extension = sched->cycle_time_extension; 12319c66d156SVinicius Costa Gomes 12329c66d156SVinicius Costa Gomes list_for_each_entry(entry, &sched->entries, list) { 12339c66d156SVinicius Costa Gomes struct tc_taprio_sched_entry *e = &offload->entries[i]; 12349c66d156SVinicius Costa Gomes 12359c66d156SVinicius Costa Gomes e->command = entry->command; 12369c66d156SVinicius Costa Gomes e->interval = entry->interval; 123709e31cf0SVinicius Costa Gomes e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask); 123809e31cf0SVinicius Costa Gomes 12399c66d156SVinicius Costa Gomes i++; 12409c66d156SVinicius Costa Gomes } 12419c66d156SVinicius Costa Gomes 12429c66d156SVinicius Costa Gomes offload->num_entries = i; 12439c66d156SVinicius Costa Gomes } 12449c66d156SVinicius Costa Gomes 12459c66d156SVinicius Costa Gomes static int taprio_enable_offload(struct net_device *dev, 12469c66d156SVinicius Costa Gomes struct taprio_sched *q, 12479c66d156SVinicius Costa Gomes struct sched_gate_list *sched, 12489c66d156SVinicius Costa Gomes struct netlink_ext_ack *extack) 12499c66d156SVinicius Costa Gomes { 12509c66d156SVinicius Costa Gomes const struct net_device_ops *ops = dev->netdev_ops; 12519c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *offload; 12529c66d156SVinicius Costa Gomes int err = 0; 12539c66d156SVinicius Costa Gomes 12549c66d156SVinicius Costa Gomes if (!ops->ndo_setup_tc) { 12559c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12569c66d156SVinicius Costa Gomes "Device does not support taprio offload"); 12579c66d156SVinicius Costa Gomes return -EOPNOTSUPP; 12589c66d156SVinicius Costa Gomes } 12599c66d156SVinicius Costa Gomes 12609c66d156SVinicius Costa Gomes offload = taprio_offload_alloc(sched->num_entries); 12619c66d156SVinicius Costa Gomes if (!offload) { 12629c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12639c66d156SVinicius Costa Gomes "Not enough memory for enabling offload mode"); 12649c66d156SVinicius Costa Gomes return -ENOMEM; 12659c66d156SVinicius Costa Gomes } 12669c66d156SVinicius Costa Gomes offload->enable = 1; 126709e31cf0SVinicius Costa Gomes taprio_sched_to_offload(dev, sched, offload); 12689c66d156SVinicius Costa Gomes 12699c66d156SVinicius Costa Gomes err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 12709c66d156SVinicius Costa Gomes if (err < 0) { 12719c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12729c66d156SVinicius Costa Gomes "Device failed to setup taprio offload"); 12739c66d156SVinicius Costa Gomes goto done; 12749c66d156SVinicius Costa Gomes } 12759c66d156SVinicius Costa Gomes 12769c66d156SVinicius Costa Gomes done: 12779c66d156SVinicius Costa Gomes taprio_offload_free(offload); 12789c66d156SVinicius Costa Gomes 12799c66d156SVinicius Costa Gomes return err; 12809c66d156SVinicius Costa Gomes } 12819c66d156SVinicius Costa Gomes 12829c66d156SVinicius Costa Gomes static int taprio_disable_offload(struct net_device *dev, 12839c66d156SVinicius Costa Gomes struct taprio_sched *q, 12849c66d156SVinicius Costa Gomes struct netlink_ext_ack *extack) 12859c66d156SVinicius Costa Gomes { 12869c66d156SVinicius Costa Gomes const struct net_device_ops *ops = dev->netdev_ops; 12879c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *offload; 12889c66d156SVinicius Costa Gomes int err; 12899c66d156SVinicius Costa Gomes 12909c66d156SVinicius Costa Gomes if (!FULL_OFFLOAD_IS_ENABLED(q->flags)) 12919c66d156SVinicius Costa Gomes return 0; 12929c66d156SVinicius Costa Gomes 12939c66d156SVinicius Costa Gomes if (!ops->ndo_setup_tc) 12949c66d156SVinicius Costa Gomes return -EOPNOTSUPP; 12959c66d156SVinicius Costa Gomes 12969c66d156SVinicius Costa Gomes offload = taprio_offload_alloc(0); 12979c66d156SVinicius Costa Gomes if (!offload) { 12989c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12999c66d156SVinicius Costa Gomes "Not enough memory to disable offload mode"); 13009c66d156SVinicius Costa Gomes return -ENOMEM; 13019c66d156SVinicius Costa Gomes } 13029c66d156SVinicius Costa Gomes offload->enable = 0; 13039c66d156SVinicius Costa Gomes 13049c66d156SVinicius Costa Gomes err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 13059c66d156SVinicius Costa Gomes if (err < 0) { 13069c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 13079c66d156SVinicius Costa Gomes "Device failed to disable offload"); 13089c66d156SVinicius Costa Gomes goto out; 13099c66d156SVinicius Costa Gomes } 13109c66d156SVinicius Costa Gomes 13119c66d156SVinicius Costa Gomes out: 13129c66d156SVinicius Costa Gomes taprio_offload_free(offload); 13139c66d156SVinicius Costa Gomes 13149c66d156SVinicius Costa Gomes return err; 13159c66d156SVinicius Costa Gomes } 13169c66d156SVinicius Costa Gomes 13179c66d156SVinicius Costa Gomes /* If full offload is enabled, the only possible clockid is the net device's 13189c66d156SVinicius Costa Gomes * PHC. For that reason, specifying a clockid through netlink is incorrect. 13199c66d156SVinicius Costa Gomes * For txtime-assist, it is implicitly assumed that the device's PHC is kept 13209c66d156SVinicius Costa Gomes * in sync with the specified clockid via a user space daemon such as phc2sys. 13219c66d156SVinicius Costa Gomes * For both software taprio and txtime-assist, the clockid is used for the 13229c66d156SVinicius Costa Gomes * hrtimer that advances the schedule and hence mandatory. 13239c66d156SVinicius Costa Gomes */ 13249c66d156SVinicius Costa Gomes static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb, 13259c66d156SVinicius Costa Gomes struct netlink_ext_ack *extack) 13269c66d156SVinicius Costa Gomes { 13279c66d156SVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 13289c66d156SVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 13299c66d156SVinicius Costa Gomes int err = -EINVAL; 13309c66d156SVinicius Costa Gomes 13319c66d156SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 13329c66d156SVinicius Costa Gomes const struct ethtool_ops *ops = dev->ethtool_ops; 13339c66d156SVinicius Costa Gomes struct ethtool_ts_info info = { 13349c66d156SVinicius Costa Gomes .cmd = ETHTOOL_GET_TS_INFO, 13359c66d156SVinicius Costa Gomes .phc_index = -1, 13369c66d156SVinicius Costa Gomes }; 13379c66d156SVinicius Costa Gomes 13389c66d156SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 13399c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 13409c66d156SVinicius Costa Gomes "The 'clockid' cannot be specified for full offload"); 13419c66d156SVinicius Costa Gomes goto out; 13429c66d156SVinicius Costa Gomes } 13439c66d156SVinicius Costa Gomes 13449c66d156SVinicius Costa Gomes if (ops && ops->get_ts_info) 13459c66d156SVinicius Costa Gomes err = ops->get_ts_info(dev, &info); 13469c66d156SVinicius Costa Gomes 13479c66d156SVinicius Costa Gomes if (err || info.phc_index < 0) { 13489c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 13499c66d156SVinicius Costa Gomes "Device does not have a PTP clock"); 13509c66d156SVinicius Costa Gomes err = -ENOTSUPP; 13519c66d156SVinicius Costa Gomes goto out; 13529c66d156SVinicius Costa Gomes } 13539c66d156SVinicius Costa Gomes } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 13549c66d156SVinicius Costa Gomes int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]); 13556dc25401SEric Dumazet enum tk_offsets tk_offset; 13569c66d156SVinicius Costa Gomes 13579c66d156SVinicius Costa Gomes /* We only support static clockids and we don't allow 13589c66d156SVinicius Costa Gomes * for it to be modified after the first init. 13599c66d156SVinicius Costa Gomes */ 13609c66d156SVinicius Costa Gomes if (clockid < 0 || 13619c66d156SVinicius Costa Gomes (q->clockid != -1 && q->clockid != clockid)) { 13629c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 13639c66d156SVinicius Costa Gomes "Changing the 'clockid' of a running schedule is not supported"); 13649c66d156SVinicius Costa Gomes err = -ENOTSUPP; 13659c66d156SVinicius Costa Gomes goto out; 13669c66d156SVinicius Costa Gomes } 13679c66d156SVinicius Costa Gomes 13689c66d156SVinicius Costa Gomes switch (clockid) { 13699c66d156SVinicius Costa Gomes case CLOCK_REALTIME: 13706dc25401SEric Dumazet tk_offset = TK_OFFS_REAL; 13719c66d156SVinicius Costa Gomes break; 13729c66d156SVinicius Costa Gomes case CLOCK_MONOTONIC: 13736dc25401SEric Dumazet tk_offset = TK_OFFS_MAX; 13749c66d156SVinicius Costa Gomes break; 13759c66d156SVinicius Costa Gomes case CLOCK_BOOTTIME: 13766dc25401SEric Dumazet tk_offset = TK_OFFS_BOOT; 13779c66d156SVinicius Costa Gomes break; 13789c66d156SVinicius Costa Gomes case CLOCK_TAI: 13796dc25401SEric Dumazet tk_offset = TK_OFFS_TAI; 13809c66d156SVinicius Costa Gomes break; 13819c66d156SVinicius Costa Gomes default: 13829c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid 'clockid'"); 13839c66d156SVinicius Costa Gomes err = -EINVAL; 13849c66d156SVinicius Costa Gomes goto out; 13859c66d156SVinicius Costa Gomes } 13866dc25401SEric Dumazet /* This pairs with READ_ONCE() in taprio_mono_to_any */ 13876dc25401SEric Dumazet WRITE_ONCE(q->tk_offset, tk_offset); 13889c66d156SVinicius Costa Gomes 13899c66d156SVinicius Costa Gomes q->clockid = clockid; 13909c66d156SVinicius Costa Gomes } else { 13919c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory"); 13929c66d156SVinicius Costa Gomes goto out; 13939c66d156SVinicius Costa Gomes } 1394a954380aSVinicius Costa Gomes 1395a954380aSVinicius Costa Gomes /* Everything went ok, return success. */ 1396a954380aSVinicius Costa Gomes err = 0; 1397a954380aSVinicius Costa Gomes 13989c66d156SVinicius Costa Gomes out: 13999c66d156SVinicius Costa Gomes return err; 14009c66d156SVinicius Costa Gomes } 14019c66d156SVinicius Costa Gomes 1402b5a0faa3SIvan Khoronzhuk static int taprio_mqprio_cmp(const struct net_device *dev, 1403b5a0faa3SIvan Khoronzhuk const struct tc_mqprio_qopt *mqprio) 1404b5a0faa3SIvan Khoronzhuk { 1405b5a0faa3SIvan Khoronzhuk int i; 1406b5a0faa3SIvan Khoronzhuk 1407b5a0faa3SIvan Khoronzhuk if (!mqprio || mqprio->num_tc != dev->num_tc) 1408b5a0faa3SIvan Khoronzhuk return -1; 1409b5a0faa3SIvan Khoronzhuk 1410b5a0faa3SIvan Khoronzhuk for (i = 0; i < mqprio->num_tc; i++) 1411b5a0faa3SIvan Khoronzhuk if (dev->tc_to_txq[i].count != mqprio->count[i] || 1412b5a0faa3SIvan Khoronzhuk dev->tc_to_txq[i].offset != mqprio->offset[i]) 1413b5a0faa3SIvan Khoronzhuk return -1; 1414b5a0faa3SIvan Khoronzhuk 1415b5a0faa3SIvan Khoronzhuk for (i = 0; i <= TC_BITMASK; i++) 1416b5a0faa3SIvan Khoronzhuk if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i]) 1417b5a0faa3SIvan Khoronzhuk return -1; 1418b5a0faa3SIvan Khoronzhuk 1419b5a0faa3SIvan Khoronzhuk return 0; 1420b5a0faa3SIvan Khoronzhuk } 1421b5a0faa3SIvan Khoronzhuk 1422a9d62274SVinicius Costa Gomes /* The semantics of the 'flags' argument in relation to 'change()' 1423a9d62274SVinicius Costa Gomes * requests, are interpreted following two rules (which are applied in 1424a9d62274SVinicius Costa Gomes * this order): (1) an omitted 'flags' argument is interpreted as 1425a9d62274SVinicius Costa Gomes * zero; (2) the 'flags' of a "running" taprio instance cannot be 1426a9d62274SVinicius Costa Gomes * changed. 1427a9d62274SVinicius Costa Gomes */ 1428a9d62274SVinicius Costa Gomes static int taprio_new_flags(const struct nlattr *attr, u32 old, 1429a9d62274SVinicius Costa Gomes struct netlink_ext_ack *extack) 1430a9d62274SVinicius Costa Gomes { 1431a9d62274SVinicius Costa Gomes u32 new = 0; 1432a9d62274SVinicius Costa Gomes 1433a9d62274SVinicius Costa Gomes if (attr) 1434a9d62274SVinicius Costa Gomes new = nla_get_u32(attr); 1435a9d62274SVinicius Costa Gomes 1436a9d62274SVinicius Costa Gomes if (old != TAPRIO_FLAGS_INVALID && old != new) { 1437a9d62274SVinicius Costa Gomes NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported"); 1438a9d62274SVinicius Costa Gomes return -EOPNOTSUPP; 1439a9d62274SVinicius Costa Gomes } 1440a9d62274SVinicius Costa Gomes 1441a9d62274SVinicius Costa Gomes if (!taprio_flags_valid(new)) { 1442a9d62274SVinicius Costa Gomes NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid"); 1443a9d62274SVinicius Costa Gomes return -EINVAL; 1444a9d62274SVinicius Costa Gomes } 1445a9d62274SVinicius Costa Gomes 1446a9d62274SVinicius Costa Gomes return new; 1447a9d62274SVinicius Costa Gomes } 1448a9d62274SVinicius Costa Gomes 14495a781ccbSVinicius Costa Gomes static int taprio_change(struct Qdisc *sch, struct nlattr *opt, 14505a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 14515a781ccbSVinicius Costa Gomes { 14525a781ccbSVinicius Costa Gomes struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { }; 1453a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin, *new_admin; 14545a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 14555a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 14565a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt *mqprio = NULL; 1457a3d43c0dSVinicius Costa Gomes unsigned long flags; 14585a781ccbSVinicius Costa Gomes ktime_t start; 14599c66d156SVinicius Costa Gomes int i, err; 14605a781ccbSVinicius Costa Gomes 14618cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt, 14625a781ccbSVinicius Costa Gomes taprio_policy, extack); 14635a781ccbSVinicius Costa Gomes if (err < 0) 14645a781ccbSVinicius Costa Gomes return err; 14655a781ccbSVinicius Costa Gomes 14665a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_PRIOMAP]) 14675a781ccbSVinicius Costa Gomes mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]); 14685a781ccbSVinicius Costa Gomes 1469a9d62274SVinicius Costa Gomes err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS], 1470a9d62274SVinicius Costa Gomes q->flags, extack); 1471a9d62274SVinicius Costa Gomes if (err < 0) 1472a9d62274SVinicius Costa Gomes return err; 14734cfd5779SVedang Patel 1474a9d62274SVinicius Costa Gomes q->flags = err; 14754cfd5779SVedang Patel 1476a9d62274SVinicius Costa Gomes err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags); 14775a781ccbSVinicius Costa Gomes if (err < 0) 14785a781ccbSVinicius Costa Gomes return err; 14795a781ccbSVinicius Costa Gomes 1480a3d43c0dSVinicius Costa Gomes new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL); 1481a3d43c0dSVinicius Costa Gomes if (!new_admin) { 1482a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule"); 1483a3d43c0dSVinicius Costa Gomes return -ENOMEM; 1484a3d43c0dSVinicius Costa Gomes } 1485a3d43c0dSVinicius Costa Gomes INIT_LIST_HEAD(&new_admin->entries); 14865a781ccbSVinicius Costa Gomes 148718cdd2f0SVladimir Oltean oper = rtnl_dereference(q->oper_sched); 148818cdd2f0SVladimir Oltean admin = rtnl_dereference(q->admin_sched); 14895a781ccbSVinicius Costa Gomes 1490b5a0faa3SIvan Khoronzhuk /* no changes - no new mqprio settings */ 1491b5a0faa3SIvan Khoronzhuk if (!taprio_mqprio_cmp(dev, mqprio)) 1492b5a0faa3SIvan Khoronzhuk mqprio = NULL; 1493b5a0faa3SIvan Khoronzhuk 1494a3d43c0dSVinicius Costa Gomes if (mqprio && (oper || admin)) { 1495a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported"); 1496a3d43c0dSVinicius Costa Gomes err = -ENOTSUPP; 1497a3d43c0dSVinicius Costa Gomes goto free_sched; 14985a781ccbSVinicius Costa Gomes } 14995a781ccbSVinicius Costa Gomes 1500b5b73b26SVinicius Costa Gomes err = parse_taprio_schedule(q, tb, new_admin, extack); 1501a3d43c0dSVinicius Costa Gomes if (err < 0) 1502a3d43c0dSVinicius Costa Gomes goto free_sched; 15035a781ccbSVinicius Costa Gomes 1504a3d43c0dSVinicius Costa Gomes if (new_admin->num_entries == 0) { 1505a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule"); 1506a3d43c0dSVinicius Costa Gomes err = -EINVAL; 1507a3d43c0dSVinicius Costa Gomes goto free_sched; 1508a3d43c0dSVinicius Costa Gomes } 15095a781ccbSVinicius Costa Gomes 15109c66d156SVinicius Costa Gomes err = taprio_parse_clockid(sch, tb, extack); 15119c66d156SVinicius Costa Gomes if (err < 0) 1512a3d43c0dSVinicius Costa Gomes goto free_sched; 1513a3d43c0dSVinicius Costa Gomes 1514a3d43c0dSVinicius Costa Gomes taprio_set_picos_per_byte(dev, q); 1515a3d43c0dSVinicius Costa Gomes 15165652e63dSVinicius Costa Gomes if (mqprio) { 1517efe487fcSHaimin Zhang err = netdev_set_num_tc(dev, mqprio->num_tc); 1518efe487fcSHaimin Zhang if (err) 1519efe487fcSHaimin Zhang goto free_sched; 15205652e63dSVinicius Costa Gomes for (i = 0; i < mqprio->num_tc; i++) 15215652e63dSVinicius Costa Gomes netdev_set_tc_queue(dev, i, 15225652e63dSVinicius Costa Gomes mqprio->count[i], 15235652e63dSVinicius Costa Gomes mqprio->offset[i]); 15245652e63dSVinicius Costa Gomes 15255652e63dSVinicius Costa Gomes /* Always use supplied priority mappings */ 15265652e63dSVinicius Costa Gomes for (i = 0; i <= TC_BITMASK; i++) 15275652e63dSVinicius Costa Gomes netdev_set_prio_tc_map(dev, i, 15285652e63dSVinicius Costa Gomes mqprio->prio_tc_map[i]); 15295652e63dSVinicius Costa Gomes } 15305652e63dSVinicius Costa Gomes 1531a9d62274SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 153209e31cf0SVinicius Costa Gomes err = taprio_enable_offload(dev, q, new_admin, extack); 15339c66d156SVinicius Costa Gomes else 15349c66d156SVinicius Costa Gomes err = taprio_disable_offload(dev, q, extack); 15359c66d156SVinicius Costa Gomes if (err) 15369c66d156SVinicius Costa Gomes goto free_sched; 15379c66d156SVinicius Costa Gomes 1538a3d43c0dSVinicius Costa Gomes /* Protects against enqueue()/dequeue() */ 1539a3d43c0dSVinicius Costa Gomes spin_lock_bh(qdisc_lock(sch)); 1540a3d43c0dSVinicius Costa Gomes 15414cfd5779SVedang Patel if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) { 15424cfd5779SVedang Patel if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) { 15434cfd5779SVedang Patel NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled"); 15444cfd5779SVedang Patel err = -EINVAL; 15454cfd5779SVedang Patel goto unlock; 15464cfd5779SVedang Patel } 15474cfd5779SVedang Patel 1548a5b64700SVedang Patel q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]); 15494cfd5779SVedang Patel } 15504cfd5779SVedang Patel 1551a9d62274SVinicius Costa Gomes if (!TXTIME_ASSIST_IS_ENABLED(q->flags) && 1552a9d62274SVinicius Costa Gomes !FULL_OFFLOAD_IS_ENABLED(q->flags) && 15534cfd5779SVedang Patel !hrtimer_active(&q->advance_timer)) { 1554a3d43c0dSVinicius Costa Gomes hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS); 1555a3d43c0dSVinicius Costa Gomes q->advance_timer.function = advance_sched; 15565a781ccbSVinicius Costa Gomes } 15575a781ccbSVinicius Costa Gomes 1558a9d62274SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 15599c66d156SVinicius Costa Gomes q->dequeue = taprio_dequeue_offload; 15609c66d156SVinicius Costa Gomes q->peek = taprio_peek_offload; 15619c66d156SVinicius Costa Gomes } else { 15629c66d156SVinicius Costa Gomes /* Be sure to always keep the function pointers 15639c66d156SVinicius Costa Gomes * in a consistent state. 15649c66d156SVinicius Costa Gomes */ 15659c66d156SVinicius Costa Gomes q->dequeue = taprio_dequeue_soft; 15669c66d156SVinicius Costa Gomes q->peek = taprio_peek_soft; 15678599099fSAndre Guedes } 15685a781ccbSVinicius Costa Gomes 1569a3d43c0dSVinicius Costa Gomes err = taprio_get_start_time(sch, new_admin, &start); 1570a3d43c0dSVinicius Costa Gomes if (err < 0) { 1571a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Internal error: failed get start time"); 1572a3d43c0dSVinicius Costa Gomes goto unlock; 1573a3d43c0dSVinicius Costa Gomes } 15745a781ccbSVinicius Costa Gomes 15754cfd5779SVedang Patel setup_txtime(q, new_admin, start); 15764cfd5779SVedang Patel 1577bfabd41dSVinicius Costa Gomes if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 15784cfd5779SVedang Patel if (!oper) { 15794cfd5779SVedang Patel rcu_assign_pointer(q->oper_sched, new_admin); 15804cfd5779SVedang Patel err = 0; 15814cfd5779SVedang Patel new_admin = NULL; 15824cfd5779SVedang Patel goto unlock; 15834cfd5779SVedang Patel } 15844cfd5779SVedang Patel 15854cfd5779SVedang Patel rcu_assign_pointer(q->admin_sched, new_admin); 15864cfd5779SVedang Patel if (admin) 15874cfd5779SVedang Patel call_rcu(&admin->rcu, taprio_free_sched_cb); 15884cfd5779SVedang Patel } else { 1589a3d43c0dSVinicius Costa Gomes setup_first_close_time(q, new_admin, start); 1590a3d43c0dSVinicius Costa Gomes 1591a3d43c0dSVinicius Costa Gomes /* Protects against advance_sched() */ 1592a3d43c0dSVinicius Costa Gomes spin_lock_irqsave(&q->current_entry_lock, flags); 1593a3d43c0dSVinicius Costa Gomes 1594a3d43c0dSVinicius Costa Gomes taprio_start_sched(sch, start, new_admin); 1595a3d43c0dSVinicius Costa Gomes 1596a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->admin_sched, new_admin); 1597a3d43c0dSVinicius Costa Gomes if (admin) 1598a3d43c0dSVinicius Costa Gomes call_rcu(&admin->rcu, taprio_free_sched_cb); 1599a3d43c0dSVinicius Costa Gomes 1600a3d43c0dSVinicius Costa Gomes spin_unlock_irqrestore(&q->current_entry_lock, flags); 16010763b3e8SIvan Khoronzhuk 1602a9d62274SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 16030763b3e8SIvan Khoronzhuk taprio_offload_config_changed(q); 16044cfd5779SVedang Patel } 1605a3d43c0dSVinicius Costa Gomes 16064cfd5779SVedang Patel new_admin = NULL; 1607a3d43c0dSVinicius Costa Gomes err = 0; 1608a3d43c0dSVinicius Costa Gomes 1609a3d43c0dSVinicius Costa Gomes unlock: 1610a3d43c0dSVinicius Costa Gomes spin_unlock_bh(qdisc_lock(sch)); 1611a3d43c0dSVinicius Costa Gomes 1612a3d43c0dSVinicius Costa Gomes free_sched: 161351650d33SIvan Khoronzhuk if (new_admin) 161451650d33SIvan Khoronzhuk call_rcu(&new_admin->rcu, taprio_free_sched_cb); 1615a3d43c0dSVinicius Costa Gomes 1616a3d43c0dSVinicius Costa Gomes return err; 16175a781ccbSVinicius Costa Gomes } 16185a781ccbSVinicius Costa Gomes 161944d4775cSDavide Caratti static void taprio_reset(struct Qdisc *sch) 162044d4775cSDavide Caratti { 162144d4775cSDavide Caratti struct taprio_sched *q = qdisc_priv(sch); 162244d4775cSDavide Caratti struct net_device *dev = qdisc_dev(sch); 162344d4775cSDavide Caratti int i; 162444d4775cSDavide Caratti 162544d4775cSDavide Caratti hrtimer_cancel(&q->advance_timer); 162644d4775cSDavide Caratti if (q->qdiscs) { 1627698285daSDavide Caratti for (i = 0; i < dev->num_tx_queues; i++) 1628698285daSDavide Caratti if (q->qdiscs[i]) 162944d4775cSDavide Caratti qdisc_reset(q->qdiscs[i]); 163044d4775cSDavide Caratti } 163144d4775cSDavide Caratti } 163244d4775cSDavide Caratti 16335a781ccbSVinicius Costa Gomes static void taprio_destroy(struct Qdisc *sch) 16345a781ccbSVinicius Costa Gomes { 16355a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 16365a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 1637*9af23657SVladimir Oltean struct sched_gate_list *oper, *admin; 16385a781ccbSVinicius Costa Gomes unsigned int i; 16395a781ccbSVinicius Costa Gomes 16407b9eba7bSLeandro Dorileo spin_lock(&taprio_list_lock); 16417b9eba7bSLeandro Dorileo list_del(&q->taprio_list); 16427b9eba7bSLeandro Dorileo spin_unlock(&taprio_list_lock); 16437b9eba7bSLeandro Dorileo 1644a56d447fSEric Dumazet /* Note that taprio_reset() might not be called if an error 1645a56d447fSEric Dumazet * happens in qdisc_create(), after taprio_init() has been called. 1646a56d447fSEric Dumazet */ 1647a56d447fSEric Dumazet hrtimer_cancel(&q->advance_timer); 16485a781ccbSVinicius Costa Gomes 16499c66d156SVinicius Costa Gomes taprio_disable_offload(dev, q, NULL); 16509c66d156SVinicius Costa Gomes 16515a781ccbSVinicius Costa Gomes if (q->qdiscs) { 1652698285daSDavide Caratti for (i = 0; i < dev->num_tx_queues; i++) 16535a781ccbSVinicius Costa Gomes qdisc_put(q->qdiscs[i]); 16545a781ccbSVinicius Costa Gomes 16555a781ccbSVinicius Costa Gomes kfree(q->qdiscs); 16565a781ccbSVinicius Costa Gomes } 16575a781ccbSVinicius Costa Gomes q->qdiscs = NULL; 16585a781ccbSVinicius Costa Gomes 16597c16680aSVinicius Costa Gomes netdev_reset_tc(dev); 16605a781ccbSVinicius Costa Gomes 1661*9af23657SVladimir Oltean oper = rtnl_dereference(q->oper_sched); 1662*9af23657SVladimir Oltean admin = rtnl_dereference(q->admin_sched); 1663a3d43c0dSVinicius Costa Gomes 1664*9af23657SVladimir Oltean if (oper) 1665*9af23657SVladimir Oltean call_rcu(&oper->rcu, taprio_free_sched_cb); 1666*9af23657SVladimir Oltean 1667*9af23657SVladimir Oltean if (admin) 1668*9af23657SVladimir Oltean call_rcu(&admin->rcu, taprio_free_sched_cb); 16695a781ccbSVinicius Costa Gomes } 16705a781ccbSVinicius Costa Gomes 16715a781ccbSVinicius Costa Gomes static int taprio_init(struct Qdisc *sch, struct nlattr *opt, 16725a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 16735a781ccbSVinicius Costa Gomes { 16745a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 16755a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 1676a3d43c0dSVinicius Costa Gomes int i; 16775a781ccbSVinicius Costa Gomes 16785a781ccbSVinicius Costa Gomes spin_lock_init(&q->current_entry_lock); 16795a781ccbSVinicius Costa Gomes 16805a781ccbSVinicius Costa Gomes hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS); 1681a3d43c0dSVinicius Costa Gomes q->advance_timer.function = advance_sched; 16825a781ccbSVinicius Costa Gomes 16839c66d156SVinicius Costa Gomes q->dequeue = taprio_dequeue_soft; 16849c66d156SVinicius Costa Gomes q->peek = taprio_peek_soft; 16859c66d156SVinicius Costa Gomes 16865a781ccbSVinicius Costa Gomes q->root = sch; 16875a781ccbSVinicius Costa Gomes 16885a781ccbSVinicius Costa Gomes /* We only support static clockids. Use an invalid value as default 16895a781ccbSVinicius Costa Gomes * and get the valid one on taprio_change(). 16905a781ccbSVinicius Costa Gomes */ 16915a781ccbSVinicius Costa Gomes q->clockid = -1; 1692a9d62274SVinicius Costa Gomes q->flags = TAPRIO_FLAGS_INVALID; 16935a781ccbSVinicius Costa Gomes 1694efb55222SVladimir Oltean spin_lock(&taprio_list_lock); 1695efb55222SVladimir Oltean list_add(&q->taprio_list, &taprio_list); 1696efb55222SVladimir Oltean spin_unlock(&taprio_list_lock); 1697efb55222SVladimir Oltean 16985a781ccbSVinicius Costa Gomes if (sch->parent != TC_H_ROOT) 16995a781ccbSVinicius Costa Gomes return -EOPNOTSUPP; 17005a781ccbSVinicius Costa Gomes 17015a781ccbSVinicius Costa Gomes if (!netif_is_multiqueue(dev)) 17025a781ccbSVinicius Costa Gomes return -EOPNOTSUPP; 17035a781ccbSVinicius Costa Gomes 17045a781ccbSVinicius Costa Gomes /* pre-allocate qdisc, attachment can't fail */ 17055a781ccbSVinicius Costa Gomes q->qdiscs = kcalloc(dev->num_tx_queues, 17065a781ccbSVinicius Costa Gomes sizeof(q->qdiscs[0]), 17075a781ccbSVinicius Costa Gomes GFP_KERNEL); 17085a781ccbSVinicius Costa Gomes 17095a781ccbSVinicius Costa Gomes if (!q->qdiscs) 17105a781ccbSVinicius Costa Gomes return -ENOMEM; 17115a781ccbSVinicius Costa Gomes 17125a781ccbSVinicius Costa Gomes if (!opt) 17135a781ccbSVinicius Costa Gomes return -EINVAL; 17145a781ccbSVinicius Costa Gomes 1715a3d43c0dSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 1716a3d43c0dSVinicius Costa Gomes struct netdev_queue *dev_queue; 1717a3d43c0dSVinicius Costa Gomes struct Qdisc *qdisc; 1718a3d43c0dSVinicius Costa Gomes 1719a3d43c0dSVinicius Costa Gomes dev_queue = netdev_get_tx_queue(dev, i); 1720a3d43c0dSVinicius Costa Gomes qdisc = qdisc_create_dflt(dev_queue, 1721a3d43c0dSVinicius Costa Gomes &pfifo_qdisc_ops, 1722a3d43c0dSVinicius Costa Gomes TC_H_MAKE(TC_H_MAJ(sch->handle), 1723a3d43c0dSVinicius Costa Gomes TC_H_MIN(i + 1)), 1724a3d43c0dSVinicius Costa Gomes extack); 1725a3d43c0dSVinicius Costa Gomes if (!qdisc) 1726a3d43c0dSVinicius Costa Gomes return -ENOMEM; 1727a3d43c0dSVinicius Costa Gomes 1728a3d43c0dSVinicius Costa Gomes if (i < dev->real_num_tx_queues) 1729a3d43c0dSVinicius Costa Gomes qdisc_hash_add(qdisc, false); 1730a3d43c0dSVinicius Costa Gomes 1731a3d43c0dSVinicius Costa Gomes q->qdiscs[i] = qdisc; 1732a3d43c0dSVinicius Costa Gomes } 1733a3d43c0dSVinicius Costa Gomes 17345a781ccbSVinicius Costa Gomes return taprio_change(sch, opt, extack); 17355a781ccbSVinicius Costa Gomes } 17365a781ccbSVinicius Costa Gomes 173713511704SYannick Vignon static void taprio_attach(struct Qdisc *sch) 173813511704SYannick Vignon { 173913511704SYannick Vignon struct taprio_sched *q = qdisc_priv(sch); 174013511704SYannick Vignon struct net_device *dev = qdisc_dev(sch); 174113511704SYannick Vignon unsigned int ntx; 174213511704SYannick Vignon 174313511704SYannick Vignon /* Attach underlying qdisc */ 174413511704SYannick Vignon for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { 174513511704SYannick Vignon struct Qdisc *qdisc = q->qdiscs[ntx]; 174613511704SYannick Vignon struct Qdisc *old; 174713511704SYannick Vignon 174813511704SYannick Vignon if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 174913511704SYannick Vignon qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 175013511704SYannick Vignon old = dev_graft_qdisc(qdisc->dev_queue, qdisc); 175113511704SYannick Vignon } else { 175213511704SYannick Vignon old = dev_graft_qdisc(qdisc->dev_queue, sch); 175313511704SYannick Vignon qdisc_refcount_inc(sch); 175413511704SYannick Vignon } 175513511704SYannick Vignon if (old) 175613511704SYannick Vignon qdisc_put(old); 175713511704SYannick Vignon } 175813511704SYannick Vignon 175913511704SYannick Vignon /* access to the child qdiscs is not needed in offload mode */ 176013511704SYannick Vignon if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 176113511704SYannick Vignon kfree(q->qdiscs); 176213511704SYannick Vignon q->qdiscs = NULL; 176313511704SYannick Vignon } 176413511704SYannick Vignon } 176513511704SYannick Vignon 17665a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_queue_get(struct Qdisc *sch, 17675a781ccbSVinicius Costa Gomes unsigned long cl) 17685a781ccbSVinicius Costa Gomes { 17695a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 17705a781ccbSVinicius Costa Gomes unsigned long ntx = cl - 1; 17715a781ccbSVinicius Costa Gomes 17725a781ccbSVinicius Costa Gomes if (ntx >= dev->num_tx_queues) 17735a781ccbSVinicius Costa Gomes return NULL; 17745a781ccbSVinicius Costa Gomes 17755a781ccbSVinicius Costa Gomes return netdev_get_tx_queue(dev, ntx); 17765a781ccbSVinicius Costa Gomes } 17775a781ccbSVinicius Costa Gomes 17785a781ccbSVinicius Costa Gomes static int taprio_graft(struct Qdisc *sch, unsigned long cl, 17795a781ccbSVinicius Costa Gomes struct Qdisc *new, struct Qdisc **old, 17805a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 17815a781ccbSVinicius Costa Gomes { 17825a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 17835a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 17845a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 17855a781ccbSVinicius Costa Gomes 17865a781ccbSVinicius Costa Gomes if (!dev_queue) 17875a781ccbSVinicius Costa Gomes return -EINVAL; 17885a781ccbSVinicius Costa Gomes 17895a781ccbSVinicius Costa Gomes if (dev->flags & IFF_UP) 17905a781ccbSVinicius Costa Gomes dev_deactivate(dev); 17915a781ccbSVinicius Costa Gomes 179213511704SYannick Vignon if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 179313511704SYannick Vignon *old = dev_graft_qdisc(dev_queue, new); 179413511704SYannick Vignon } else { 17955a781ccbSVinicius Costa Gomes *old = q->qdiscs[cl - 1]; 17965a781ccbSVinicius Costa Gomes q->qdiscs[cl - 1] = new; 179713511704SYannick Vignon } 17985a781ccbSVinicius Costa Gomes 17995a781ccbSVinicius Costa Gomes if (new) 18005a781ccbSVinicius Costa Gomes new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 18015a781ccbSVinicius Costa Gomes 18025a781ccbSVinicius Costa Gomes if (dev->flags & IFF_UP) 18035a781ccbSVinicius Costa Gomes dev_activate(dev); 18045a781ccbSVinicius Costa Gomes 18055a781ccbSVinicius Costa Gomes return 0; 18065a781ccbSVinicius Costa Gomes } 18075a781ccbSVinicius Costa Gomes 18085a781ccbSVinicius Costa Gomes static int dump_entry(struct sk_buff *msg, 18095a781ccbSVinicius Costa Gomes const struct sched_entry *entry) 18105a781ccbSVinicius Costa Gomes { 18115a781ccbSVinicius Costa Gomes struct nlattr *item; 18125a781ccbSVinicius Costa Gomes 1813ae0be8deSMichal Kubecek item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY); 18145a781ccbSVinicius Costa Gomes if (!item) 18155a781ccbSVinicius Costa Gomes return -ENOSPC; 18165a781ccbSVinicius Costa Gomes 18175a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index)) 18185a781ccbSVinicius Costa Gomes goto nla_put_failure; 18195a781ccbSVinicius Costa Gomes 18205a781ccbSVinicius Costa Gomes if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command)) 18215a781ccbSVinicius Costa Gomes goto nla_put_failure; 18225a781ccbSVinicius Costa Gomes 18235a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK, 18245a781ccbSVinicius Costa Gomes entry->gate_mask)) 18255a781ccbSVinicius Costa Gomes goto nla_put_failure; 18265a781ccbSVinicius Costa Gomes 18275a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL, 18285a781ccbSVinicius Costa Gomes entry->interval)) 18295a781ccbSVinicius Costa Gomes goto nla_put_failure; 18305a781ccbSVinicius Costa Gomes 18315a781ccbSVinicius Costa Gomes return nla_nest_end(msg, item); 18325a781ccbSVinicius Costa Gomes 18335a781ccbSVinicius Costa Gomes nla_put_failure: 18345a781ccbSVinicius Costa Gomes nla_nest_cancel(msg, item); 18355a781ccbSVinicius Costa Gomes return -1; 18365a781ccbSVinicius Costa Gomes } 18375a781ccbSVinicius Costa Gomes 1838a3d43c0dSVinicius Costa Gomes static int dump_schedule(struct sk_buff *msg, 1839a3d43c0dSVinicius Costa Gomes const struct sched_gate_list *root) 1840a3d43c0dSVinicius Costa Gomes { 1841a3d43c0dSVinicius Costa Gomes struct nlattr *entry_list; 1842a3d43c0dSVinicius Costa Gomes struct sched_entry *entry; 1843a3d43c0dSVinicius Costa Gomes 1844a3d43c0dSVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME, 1845a3d43c0dSVinicius Costa Gomes root->base_time, TCA_TAPRIO_PAD)) 1846a3d43c0dSVinicius Costa Gomes return -1; 1847a3d43c0dSVinicius Costa Gomes 18486ca6a665SVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME, 18496ca6a665SVinicius Costa Gomes root->cycle_time, TCA_TAPRIO_PAD)) 18506ca6a665SVinicius Costa Gomes return -1; 18516ca6a665SVinicius Costa Gomes 1852c25031e9SVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION, 1853c25031e9SVinicius Costa Gomes root->cycle_time_extension, TCA_TAPRIO_PAD)) 1854c25031e9SVinicius Costa Gomes return -1; 1855c25031e9SVinicius Costa Gomes 1856a3d43c0dSVinicius Costa Gomes entry_list = nla_nest_start_noflag(msg, 1857a3d43c0dSVinicius Costa Gomes TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST); 1858a3d43c0dSVinicius Costa Gomes if (!entry_list) 1859a3d43c0dSVinicius Costa Gomes goto error_nest; 1860a3d43c0dSVinicius Costa Gomes 1861a3d43c0dSVinicius Costa Gomes list_for_each_entry(entry, &root->entries, list) { 1862a3d43c0dSVinicius Costa Gomes if (dump_entry(msg, entry) < 0) 1863a3d43c0dSVinicius Costa Gomes goto error_nest; 1864a3d43c0dSVinicius Costa Gomes } 1865a3d43c0dSVinicius Costa Gomes 1866a3d43c0dSVinicius Costa Gomes nla_nest_end(msg, entry_list); 1867a3d43c0dSVinicius Costa Gomes return 0; 1868a3d43c0dSVinicius Costa Gomes 1869a3d43c0dSVinicius Costa Gomes error_nest: 1870a3d43c0dSVinicius Costa Gomes nla_nest_cancel(msg, entry_list); 1871a3d43c0dSVinicius Costa Gomes return -1; 1872a3d43c0dSVinicius Costa Gomes } 1873a3d43c0dSVinicius Costa Gomes 18745a781ccbSVinicius Costa Gomes static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb) 18755a781ccbSVinicius Costa Gomes { 18765a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 18775a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 1878a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin; 18795a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt opt = { 0 }; 1880a3d43c0dSVinicius Costa Gomes struct nlattr *nest, *sched_nest; 18815a781ccbSVinicius Costa Gomes unsigned int i; 18825a781ccbSVinicius Costa Gomes 188318cdd2f0SVladimir Oltean oper = rtnl_dereference(q->oper_sched); 188418cdd2f0SVladimir Oltean admin = rtnl_dereference(q->admin_sched); 1885a3d43c0dSVinicius Costa Gomes 18865a781ccbSVinicius Costa Gomes opt.num_tc = netdev_get_num_tc(dev); 18875a781ccbSVinicius Costa Gomes memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map)); 18885a781ccbSVinicius Costa Gomes 18895a781ccbSVinicius Costa Gomes for (i = 0; i < netdev_get_num_tc(dev); i++) { 18905a781ccbSVinicius Costa Gomes opt.count[i] = dev->tc_to_txq[i].count; 18915a781ccbSVinicius Costa Gomes opt.offset[i] = dev->tc_to_txq[i].offset; 18925a781ccbSVinicius Costa Gomes } 18935a781ccbSVinicius Costa Gomes 1894ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 18955a781ccbSVinicius Costa Gomes if (!nest) 1896a3d43c0dSVinicius Costa Gomes goto start_error; 18975a781ccbSVinicius Costa Gomes 18985a781ccbSVinicius Costa Gomes if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt)) 18995a781ccbSVinicius Costa Gomes goto options_error; 19005a781ccbSVinicius Costa Gomes 19019c66d156SVinicius Costa Gomes if (!FULL_OFFLOAD_IS_ENABLED(q->flags) && 19029c66d156SVinicius Costa Gomes nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid)) 19035a781ccbSVinicius Costa Gomes goto options_error; 19045a781ccbSVinicius Costa Gomes 19054cfd5779SVedang Patel if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags)) 19064cfd5779SVedang Patel goto options_error; 19074cfd5779SVedang Patel 19084cfd5779SVedang Patel if (q->txtime_delay && 1909a5b64700SVedang Patel nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay)) 19104cfd5779SVedang Patel goto options_error; 19114cfd5779SVedang Patel 1912a3d43c0dSVinicius Costa Gomes if (oper && dump_schedule(skb, oper)) 19135a781ccbSVinicius Costa Gomes goto options_error; 19145a781ccbSVinicius Costa Gomes 1915a3d43c0dSVinicius Costa Gomes if (!admin) 1916a3d43c0dSVinicius Costa Gomes goto done; 19175a781ccbSVinicius Costa Gomes 1918a3d43c0dSVinicius Costa Gomes sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED); 1919e4acf427SColin Ian King if (!sched_nest) 1920e4acf427SColin Ian King goto options_error; 1921a3d43c0dSVinicius Costa Gomes 1922a3d43c0dSVinicius Costa Gomes if (dump_schedule(skb, admin)) 1923a3d43c0dSVinicius Costa Gomes goto admin_error; 1924a3d43c0dSVinicius Costa Gomes 1925a3d43c0dSVinicius Costa Gomes nla_nest_end(skb, sched_nest); 1926a3d43c0dSVinicius Costa Gomes 1927a3d43c0dSVinicius Costa Gomes done: 19285a781ccbSVinicius Costa Gomes return nla_nest_end(skb, nest); 19295a781ccbSVinicius Costa Gomes 1930a3d43c0dSVinicius Costa Gomes admin_error: 1931a3d43c0dSVinicius Costa Gomes nla_nest_cancel(skb, sched_nest); 1932a3d43c0dSVinicius Costa Gomes 19335a781ccbSVinicius Costa Gomes options_error: 19345a781ccbSVinicius Costa Gomes nla_nest_cancel(skb, nest); 1935a3d43c0dSVinicius Costa Gomes 1936a3d43c0dSVinicius Costa Gomes start_error: 1937a3d43c0dSVinicius Costa Gomes return -ENOSPC; 19385a781ccbSVinicius Costa Gomes } 19395a781ccbSVinicius Costa Gomes 19405a781ccbSVinicius Costa Gomes static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl) 19415a781ccbSVinicius Costa Gomes { 19425a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 19435a781ccbSVinicius Costa Gomes 19445a781ccbSVinicius Costa Gomes if (!dev_queue) 19455a781ccbSVinicius Costa Gomes return NULL; 19465a781ccbSVinicius Costa Gomes 19475a781ccbSVinicius Costa Gomes return dev_queue->qdisc_sleeping; 19485a781ccbSVinicius Costa Gomes } 19495a781ccbSVinicius Costa Gomes 19505a781ccbSVinicius Costa Gomes static unsigned long taprio_find(struct Qdisc *sch, u32 classid) 19515a781ccbSVinicius Costa Gomes { 19525a781ccbSVinicius Costa Gomes unsigned int ntx = TC_H_MIN(classid); 19535a781ccbSVinicius Costa Gomes 19545a781ccbSVinicius Costa Gomes if (!taprio_queue_get(sch, ntx)) 19555a781ccbSVinicius Costa Gomes return 0; 19565a781ccbSVinicius Costa Gomes return ntx; 19575a781ccbSVinicius Costa Gomes } 19585a781ccbSVinicius Costa Gomes 19595a781ccbSVinicius Costa Gomes static int taprio_dump_class(struct Qdisc *sch, unsigned long cl, 19605a781ccbSVinicius Costa Gomes struct sk_buff *skb, struct tcmsg *tcm) 19615a781ccbSVinicius Costa Gomes { 19625a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 19635a781ccbSVinicius Costa Gomes 19645a781ccbSVinicius Costa Gomes tcm->tcm_parent = TC_H_ROOT; 19655a781ccbSVinicius Costa Gomes tcm->tcm_handle |= TC_H_MIN(cl); 19665a781ccbSVinicius Costa Gomes tcm->tcm_info = dev_queue->qdisc_sleeping->handle; 19675a781ccbSVinicius Costa Gomes 19685a781ccbSVinicius Costa Gomes return 0; 19695a781ccbSVinicius Costa Gomes } 19705a781ccbSVinicius Costa Gomes 19715a781ccbSVinicius Costa Gomes static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 19725a781ccbSVinicius Costa Gomes struct gnet_dump *d) 19735a781ccbSVinicius Costa Gomes __releases(d->lock) 19745a781ccbSVinicius Costa Gomes __acquires(d->lock) 19755a781ccbSVinicius Costa Gomes { 19765a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 19775a781ccbSVinicius Costa Gomes 19785a781ccbSVinicius Costa Gomes sch = dev_queue->qdisc_sleeping; 197929cbcd85SAhmed S. Darwish if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 || 19805dd431b6SPaolo Abeni qdisc_qstats_copy(d, sch) < 0) 19815a781ccbSVinicius Costa Gomes return -1; 19825a781ccbSVinicius Costa Gomes return 0; 19835a781ccbSVinicius Costa Gomes } 19845a781ccbSVinicius Costa Gomes 19855a781ccbSVinicius Costa Gomes static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 19865a781ccbSVinicius Costa Gomes { 19875a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 19885a781ccbSVinicius Costa Gomes unsigned long ntx; 19895a781ccbSVinicius Costa Gomes 19905a781ccbSVinicius Costa Gomes if (arg->stop) 19915a781ccbSVinicius Costa Gomes return; 19925a781ccbSVinicius Costa Gomes 19935a781ccbSVinicius Costa Gomes arg->count = arg->skip; 19945a781ccbSVinicius Costa Gomes for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) { 19955a781ccbSVinicius Costa Gomes if (arg->fn(sch, ntx + 1, arg) < 0) { 19965a781ccbSVinicius Costa Gomes arg->stop = 1; 19975a781ccbSVinicius Costa Gomes break; 19985a781ccbSVinicius Costa Gomes } 19995a781ccbSVinicius Costa Gomes arg->count++; 20005a781ccbSVinicius Costa Gomes } 20015a781ccbSVinicius Costa Gomes } 20025a781ccbSVinicius Costa Gomes 20035a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_select_queue(struct Qdisc *sch, 20045a781ccbSVinicius Costa Gomes struct tcmsg *tcm) 20055a781ccbSVinicius Costa Gomes { 20065a781ccbSVinicius Costa Gomes return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); 20075a781ccbSVinicius Costa Gomes } 20085a781ccbSVinicius Costa Gomes 20095a781ccbSVinicius Costa Gomes static const struct Qdisc_class_ops taprio_class_ops = { 20105a781ccbSVinicius Costa Gomes .graft = taprio_graft, 20115a781ccbSVinicius Costa Gomes .leaf = taprio_leaf, 20125a781ccbSVinicius Costa Gomes .find = taprio_find, 20135a781ccbSVinicius Costa Gomes .walk = taprio_walk, 20145a781ccbSVinicius Costa Gomes .dump = taprio_dump_class, 20155a781ccbSVinicius Costa Gomes .dump_stats = taprio_dump_class_stats, 20165a781ccbSVinicius Costa Gomes .select_queue = taprio_select_queue, 20175a781ccbSVinicius Costa Gomes }; 20185a781ccbSVinicius Costa Gomes 20195a781ccbSVinicius Costa Gomes static struct Qdisc_ops taprio_qdisc_ops __read_mostly = { 20205a781ccbSVinicius Costa Gomes .cl_ops = &taprio_class_ops, 20215a781ccbSVinicius Costa Gomes .id = "taprio", 20225a781ccbSVinicius Costa Gomes .priv_size = sizeof(struct taprio_sched), 20235a781ccbSVinicius Costa Gomes .init = taprio_init, 2024a3d43c0dSVinicius Costa Gomes .change = taprio_change, 20255a781ccbSVinicius Costa Gomes .destroy = taprio_destroy, 202644d4775cSDavide Caratti .reset = taprio_reset, 202713511704SYannick Vignon .attach = taprio_attach, 20285a781ccbSVinicius Costa Gomes .peek = taprio_peek, 20295a781ccbSVinicius Costa Gomes .dequeue = taprio_dequeue, 20305a781ccbSVinicius Costa Gomes .enqueue = taprio_enqueue, 20315a781ccbSVinicius Costa Gomes .dump = taprio_dump, 20325a781ccbSVinicius Costa Gomes .owner = THIS_MODULE, 20335a781ccbSVinicius Costa Gomes }; 20345a781ccbSVinicius Costa Gomes 20357b9eba7bSLeandro Dorileo static struct notifier_block taprio_device_notifier = { 20367b9eba7bSLeandro Dorileo .notifier_call = taprio_dev_notifier, 20377b9eba7bSLeandro Dorileo }; 20387b9eba7bSLeandro Dorileo 20395a781ccbSVinicius Costa Gomes static int __init taprio_module_init(void) 20405a781ccbSVinicius Costa Gomes { 20417b9eba7bSLeandro Dorileo int err = register_netdevice_notifier(&taprio_device_notifier); 20427b9eba7bSLeandro Dorileo 20437b9eba7bSLeandro Dorileo if (err) 20447b9eba7bSLeandro Dorileo return err; 20457b9eba7bSLeandro Dorileo 20465a781ccbSVinicius Costa Gomes return register_qdisc(&taprio_qdisc_ops); 20475a781ccbSVinicius Costa Gomes } 20485a781ccbSVinicius Costa Gomes 20495a781ccbSVinicius Costa Gomes static void __exit taprio_module_exit(void) 20505a781ccbSVinicius Costa Gomes { 20515a781ccbSVinicius Costa Gomes unregister_qdisc(&taprio_qdisc_ops); 20527b9eba7bSLeandro Dorileo unregister_netdevice_notifier(&taprio_device_notifier); 20535a781ccbSVinicius Costa Gomes } 20545a781ccbSVinicius Costa Gomes 20555a781ccbSVinicius Costa Gomes module_init(taprio_module_init); 20565a781ccbSVinicius Costa Gomes module_exit(taprio_module_exit); 20575a781ccbSVinicius Costa Gomes MODULE_LICENSE("GPL"); 2058