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 95a781ccbSVinicius Costa Gomes #include <linux/types.h> 105a781ccbSVinicius Costa Gomes #include <linux/slab.h> 115a781ccbSVinicius Costa Gomes #include <linux/kernel.h> 125a781ccbSVinicius Costa Gomes #include <linux/string.h> 135a781ccbSVinicius Costa Gomes #include <linux/list.h> 145a781ccbSVinicius Costa Gomes #include <linux/errno.h> 155a781ccbSVinicius Costa Gomes #include <linux/skbuff.h> 1623bddf69SJakub Kicinski #include <linux/math64.h> 175a781ccbSVinicius Costa Gomes #include <linux/module.h> 185a781ccbSVinicius Costa Gomes #include <linux/spinlock.h> 19a3d43c0dSVinicius Costa Gomes #include <linux/rcupdate.h> 205a781ccbSVinicius Costa Gomes #include <net/netlink.h> 215a781ccbSVinicius Costa Gomes #include <net/pkt_sched.h> 225a781ccbSVinicius Costa Gomes #include <net/pkt_cls.h> 235a781ccbSVinicius Costa Gomes #include <net/sch_generic.h> 244cfd5779SVedang Patel #include <net/sock.h> 2554002066SVedang Patel #include <net/tcp.h> 265a781ccbSVinicius Costa Gomes 277b9eba7bSLeandro Dorileo static LIST_HEAD(taprio_list); 287b9eba7bSLeandro Dorileo static DEFINE_SPINLOCK(taprio_list_lock); 297b9eba7bSLeandro Dorileo 305a781ccbSVinicius Costa Gomes #define TAPRIO_ALL_GATES_OPEN -1 315a781ccbSVinicius Costa Gomes 324cfd5779SVedang Patel #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) 339c66d156SVinicius Costa Gomes #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD) 34a9d62274SVinicius Costa Gomes #define TAPRIO_FLAGS_INVALID U32_MAX 354cfd5779SVedang Patel 365a781ccbSVinicius Costa Gomes struct sched_entry { 375a781ccbSVinicius Costa Gomes struct list_head list; 385a781ccbSVinicius Costa Gomes 395a781ccbSVinicius Costa Gomes /* The instant that this entry "closes" and the next one 405a781ccbSVinicius Costa Gomes * should open, the qdisc will make some effort so that no 415a781ccbSVinicius Costa Gomes * packet leaves after this time. 425a781ccbSVinicius Costa Gomes */ 435a781ccbSVinicius Costa Gomes ktime_t close_time; 444cfd5779SVedang Patel ktime_t next_txtime; 455a781ccbSVinicius Costa Gomes atomic_t budget; 465a781ccbSVinicius Costa Gomes int index; 475a781ccbSVinicius Costa Gomes u32 gate_mask; 485a781ccbSVinicius Costa Gomes u32 interval; 495a781ccbSVinicius Costa Gomes u8 command; 505a781ccbSVinicius Costa Gomes }; 515a781ccbSVinicius Costa Gomes 52a3d43c0dSVinicius Costa Gomes struct sched_gate_list { 53a3d43c0dSVinicius Costa Gomes struct rcu_head rcu; 54a3d43c0dSVinicius Costa Gomes struct list_head entries; 55a3d43c0dSVinicius Costa Gomes size_t num_entries; 566ca6a665SVinicius Costa Gomes ktime_t cycle_close_time; 576ca6a665SVinicius Costa Gomes s64 cycle_time; 58c25031e9SVinicius Costa Gomes s64 cycle_time_extension; 59a3d43c0dSVinicius Costa Gomes s64 base_time; 60a3d43c0dSVinicius Costa Gomes }; 61a3d43c0dSVinicius Costa Gomes 625a781ccbSVinicius Costa Gomes struct taprio_sched { 635a781ccbSVinicius Costa Gomes struct Qdisc **qdiscs; 645a781ccbSVinicius Costa Gomes struct Qdisc *root; 654cfd5779SVedang Patel u32 flags; 667ede7b03SVedang Patel enum tk_offsets tk_offset; 675a781ccbSVinicius Costa Gomes int clockid; 687b9eba7bSLeandro Dorileo atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+ 695a781ccbSVinicius Costa Gomes * speeds it's sub-nanoseconds per byte 705a781ccbSVinicius Costa Gomes */ 715a781ccbSVinicius Costa Gomes 725a781ccbSVinicius Costa Gomes /* Protects the update side of the RCU protected current_entry */ 735a781ccbSVinicius Costa Gomes spinlock_t current_entry_lock; 745a781ccbSVinicius Costa Gomes struct sched_entry __rcu *current_entry; 75a3d43c0dSVinicius Costa Gomes struct sched_gate_list __rcu *oper_sched; 76a3d43c0dSVinicius Costa Gomes struct sched_gate_list __rcu *admin_sched; 775a781ccbSVinicius Costa Gomes struct hrtimer advance_timer; 787b9eba7bSLeandro Dorileo struct list_head taprio_list; 799c66d156SVinicius Costa Gomes struct sk_buff *(*dequeue)(struct Qdisc *sch); 809c66d156SVinicius Costa Gomes struct sk_buff *(*peek)(struct Qdisc *sch); 81a5b64700SVedang Patel u32 txtime_delay; 825a781ccbSVinicius Costa Gomes }; 835a781ccbSVinicius Costa Gomes 849c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload { 859c66d156SVinicius Costa Gomes refcount_t users; 869c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload offload; 879c66d156SVinicius Costa Gomes }; 889c66d156SVinicius Costa Gomes 89a3d43c0dSVinicius Costa Gomes static ktime_t sched_base_time(const struct sched_gate_list *sched) 90a3d43c0dSVinicius Costa Gomes { 91a3d43c0dSVinicius Costa Gomes if (!sched) 92a3d43c0dSVinicius Costa Gomes return KTIME_MAX; 93a3d43c0dSVinicius Costa Gomes 94a3d43c0dSVinicius Costa Gomes return ns_to_ktime(sched->base_time); 95a3d43c0dSVinicius Costa Gomes } 96a3d43c0dSVinicius Costa Gomes 977ede7b03SVedang Patel static ktime_t taprio_get_time(struct taprio_sched *q) 987ede7b03SVedang Patel { 997ede7b03SVedang Patel ktime_t mono = ktime_get(); 1007ede7b03SVedang Patel 1017ede7b03SVedang Patel switch (q->tk_offset) { 1027ede7b03SVedang Patel case TK_OFFS_MAX: 1037ede7b03SVedang Patel return mono; 1047ede7b03SVedang Patel default: 1057ede7b03SVedang Patel return ktime_mono_to_any(mono, q->tk_offset); 1067ede7b03SVedang Patel } 1077ede7b03SVedang Patel 1087ede7b03SVedang Patel return KTIME_MAX; 1097ede7b03SVedang Patel } 1107ede7b03SVedang Patel 111a3d43c0dSVinicius Costa Gomes static void taprio_free_sched_cb(struct rcu_head *head) 112a3d43c0dSVinicius Costa Gomes { 113a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu); 114a3d43c0dSVinicius Costa Gomes struct sched_entry *entry, *n; 115a3d43c0dSVinicius Costa Gomes 116a3d43c0dSVinicius Costa Gomes if (!sched) 117a3d43c0dSVinicius Costa Gomes return; 118a3d43c0dSVinicius Costa Gomes 119a3d43c0dSVinicius Costa Gomes list_for_each_entry_safe(entry, n, &sched->entries, list) { 120a3d43c0dSVinicius Costa Gomes list_del(&entry->list); 121a3d43c0dSVinicius Costa Gomes kfree(entry); 122a3d43c0dSVinicius Costa Gomes } 123a3d43c0dSVinicius Costa Gomes 124a3d43c0dSVinicius Costa Gomes kfree(sched); 125a3d43c0dSVinicius Costa Gomes } 126a3d43c0dSVinicius Costa Gomes 127a3d43c0dSVinicius Costa Gomes static void switch_schedules(struct taprio_sched *q, 128a3d43c0dSVinicius Costa Gomes struct sched_gate_list **admin, 129a3d43c0dSVinicius Costa Gomes struct sched_gate_list **oper) 130a3d43c0dSVinicius Costa Gomes { 131a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->oper_sched, *admin); 132a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->admin_sched, NULL); 133a3d43c0dSVinicius Costa Gomes 134a3d43c0dSVinicius Costa Gomes if (*oper) 135a3d43c0dSVinicius Costa Gomes call_rcu(&(*oper)->rcu, taprio_free_sched_cb); 136a3d43c0dSVinicius Costa Gomes 137a3d43c0dSVinicius Costa Gomes *oper = *admin; 138a3d43c0dSVinicius Costa Gomes *admin = NULL; 139a3d43c0dSVinicius Costa Gomes } 140a3d43c0dSVinicius Costa Gomes 1414cfd5779SVedang Patel /* Get how much time has been already elapsed in the current cycle. */ 1424cfd5779SVedang Patel static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time) 1434cfd5779SVedang Patel { 1444cfd5779SVedang Patel ktime_t time_since_sched_start; 1454cfd5779SVedang Patel s32 time_elapsed; 1464cfd5779SVedang Patel 1474cfd5779SVedang Patel time_since_sched_start = ktime_sub(time, sched->base_time); 1484cfd5779SVedang Patel div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed); 1494cfd5779SVedang Patel 1504cfd5779SVedang Patel return time_elapsed; 1514cfd5779SVedang Patel } 1524cfd5779SVedang Patel 1534cfd5779SVedang Patel static ktime_t get_interval_end_time(struct sched_gate_list *sched, 1544cfd5779SVedang Patel struct sched_gate_list *admin, 1554cfd5779SVedang Patel struct sched_entry *entry, 1564cfd5779SVedang Patel ktime_t intv_start) 1574cfd5779SVedang Patel { 1584cfd5779SVedang Patel s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start); 1594cfd5779SVedang Patel ktime_t intv_end, cycle_ext_end, cycle_end; 1604cfd5779SVedang Patel 1614cfd5779SVedang Patel cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed); 1624cfd5779SVedang Patel intv_end = ktime_add_ns(intv_start, entry->interval); 1634cfd5779SVedang Patel cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension); 1644cfd5779SVedang Patel 1654cfd5779SVedang Patel if (ktime_before(intv_end, cycle_end)) 1664cfd5779SVedang Patel return intv_end; 1674cfd5779SVedang Patel else if (admin && admin != sched && 1684cfd5779SVedang Patel ktime_after(admin->base_time, cycle_end) && 1694cfd5779SVedang Patel ktime_before(admin->base_time, cycle_ext_end)) 1704cfd5779SVedang Patel return admin->base_time; 1714cfd5779SVedang Patel else 1724cfd5779SVedang Patel return cycle_end; 1734cfd5779SVedang Patel } 1744cfd5779SVedang Patel 1754cfd5779SVedang Patel static int length_to_duration(struct taprio_sched *q, int len) 1764cfd5779SVedang Patel { 1774cfd5779SVedang Patel return div_u64(len * atomic64_read(&q->picos_per_byte), 1000); 1784cfd5779SVedang Patel } 1794cfd5779SVedang Patel 1804cfd5779SVedang Patel /* Returns the entry corresponding to next available interval. If 1814cfd5779SVedang Patel * validate_interval is set, it only validates whether the timestamp occurs 1824cfd5779SVedang Patel * when the gate corresponding to the skb's traffic class is open. 1834cfd5779SVedang Patel */ 1844cfd5779SVedang Patel static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb, 1854cfd5779SVedang Patel struct Qdisc *sch, 1864cfd5779SVedang Patel struct sched_gate_list *sched, 1874cfd5779SVedang Patel struct sched_gate_list *admin, 1884cfd5779SVedang Patel ktime_t time, 1894cfd5779SVedang Patel ktime_t *interval_start, 1904cfd5779SVedang Patel ktime_t *interval_end, 1914cfd5779SVedang Patel bool validate_interval) 1924cfd5779SVedang Patel { 1934cfd5779SVedang Patel ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time; 1944cfd5779SVedang Patel ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time; 1954cfd5779SVedang Patel struct sched_entry *entry = NULL, *entry_found = NULL; 1964cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 1974cfd5779SVedang Patel struct net_device *dev = qdisc_dev(sch); 1984cfd5779SVedang Patel bool entry_available = false; 1994cfd5779SVedang Patel s32 cycle_elapsed; 2004cfd5779SVedang Patel int tc, n; 2014cfd5779SVedang Patel 2024cfd5779SVedang Patel tc = netdev_get_prio_tc_map(dev, skb->priority); 2034cfd5779SVedang Patel packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb)); 2044cfd5779SVedang Patel 2054cfd5779SVedang Patel *interval_start = 0; 2064cfd5779SVedang Patel *interval_end = 0; 2074cfd5779SVedang Patel 2084cfd5779SVedang Patel if (!sched) 2094cfd5779SVedang Patel return NULL; 2104cfd5779SVedang Patel 2114cfd5779SVedang Patel cycle = sched->cycle_time; 2124cfd5779SVedang Patel cycle_elapsed = get_cycle_time_elapsed(sched, time); 2134cfd5779SVedang Patel curr_intv_end = ktime_sub_ns(time, cycle_elapsed); 2144cfd5779SVedang Patel cycle_end = ktime_add_ns(curr_intv_end, cycle); 2154cfd5779SVedang Patel 2164cfd5779SVedang Patel list_for_each_entry(entry, &sched->entries, list) { 2174cfd5779SVedang Patel curr_intv_start = curr_intv_end; 2184cfd5779SVedang Patel curr_intv_end = get_interval_end_time(sched, admin, entry, 2194cfd5779SVedang Patel curr_intv_start); 2204cfd5779SVedang Patel 2214cfd5779SVedang Patel if (ktime_after(curr_intv_start, cycle_end)) 2224cfd5779SVedang Patel break; 2234cfd5779SVedang Patel 2244cfd5779SVedang Patel if (!(entry->gate_mask & BIT(tc)) || 2254cfd5779SVedang Patel packet_transmit_time > entry->interval) 2264cfd5779SVedang Patel continue; 2274cfd5779SVedang Patel 2284cfd5779SVedang Patel txtime = entry->next_txtime; 2294cfd5779SVedang Patel 2304cfd5779SVedang Patel if (ktime_before(txtime, time) || validate_interval) { 2314cfd5779SVedang Patel transmit_end_time = ktime_add_ns(time, packet_transmit_time); 2324cfd5779SVedang Patel if ((ktime_before(curr_intv_start, time) && 2334cfd5779SVedang Patel ktime_before(transmit_end_time, curr_intv_end)) || 2344cfd5779SVedang Patel (ktime_after(curr_intv_start, time) && !validate_interval)) { 2354cfd5779SVedang Patel entry_found = entry; 2364cfd5779SVedang Patel *interval_start = curr_intv_start; 2374cfd5779SVedang Patel *interval_end = curr_intv_end; 2384cfd5779SVedang Patel break; 2394cfd5779SVedang Patel } else if (!entry_available && !validate_interval) { 2404cfd5779SVedang Patel /* Here, we are just trying to find out the 2414cfd5779SVedang Patel * first available interval in the next cycle. 2424cfd5779SVedang Patel */ 2434cfd5779SVedang Patel entry_available = 1; 2444cfd5779SVedang Patel entry_found = entry; 2454cfd5779SVedang Patel *interval_start = ktime_add_ns(curr_intv_start, cycle); 2464cfd5779SVedang Patel *interval_end = ktime_add_ns(curr_intv_end, cycle); 2474cfd5779SVedang Patel } 2484cfd5779SVedang Patel } else if (ktime_before(txtime, earliest_txtime) && 2494cfd5779SVedang Patel !entry_available) { 2504cfd5779SVedang Patel earliest_txtime = txtime; 2514cfd5779SVedang Patel entry_found = entry; 2524cfd5779SVedang Patel n = div_s64(ktime_sub(txtime, curr_intv_start), cycle); 2534cfd5779SVedang Patel *interval_start = ktime_add(curr_intv_start, n * cycle); 2544cfd5779SVedang Patel *interval_end = ktime_add(curr_intv_end, n * cycle); 2554cfd5779SVedang Patel } 2564cfd5779SVedang Patel } 2574cfd5779SVedang Patel 2584cfd5779SVedang Patel return entry_found; 2594cfd5779SVedang Patel } 2604cfd5779SVedang Patel 2614cfd5779SVedang Patel static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch) 2624cfd5779SVedang Patel { 2634cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 2644cfd5779SVedang Patel struct sched_gate_list *sched, *admin; 2654cfd5779SVedang Patel ktime_t interval_start, interval_end; 2664cfd5779SVedang Patel struct sched_entry *entry; 2674cfd5779SVedang Patel 2684cfd5779SVedang Patel rcu_read_lock(); 2694cfd5779SVedang Patel sched = rcu_dereference(q->oper_sched); 2704cfd5779SVedang Patel admin = rcu_dereference(q->admin_sched); 2714cfd5779SVedang Patel 2724cfd5779SVedang Patel entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp, 2734cfd5779SVedang Patel &interval_start, &interval_end, true); 2744cfd5779SVedang Patel rcu_read_unlock(); 2754cfd5779SVedang Patel 2764cfd5779SVedang Patel return entry; 2774cfd5779SVedang Patel } 2784cfd5779SVedang Patel 2799c66d156SVinicius Costa Gomes static bool taprio_flags_valid(u32 flags) 2809c66d156SVinicius Costa Gomes { 2819c66d156SVinicius Costa Gomes /* Make sure no other flag bits are set. */ 2829c66d156SVinicius Costa Gomes if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST | 2839c66d156SVinicius Costa Gomes TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) 2849c66d156SVinicius Costa Gomes return false; 2859c66d156SVinicius Costa Gomes /* txtime-assist and full offload are mutually exclusive */ 2869c66d156SVinicius Costa Gomes if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) && 2879c66d156SVinicius Costa Gomes (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) 2889c66d156SVinicius Costa Gomes return false; 2899c66d156SVinicius Costa Gomes return true; 2909c66d156SVinicius Costa Gomes } 2919c66d156SVinicius Costa Gomes 29254002066SVedang Patel /* This returns the tstamp value set by TCP in terms of the set clock. */ 29354002066SVedang Patel static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb) 29454002066SVedang Patel { 29554002066SVedang Patel unsigned int offset = skb_network_offset(skb); 29654002066SVedang Patel const struct ipv6hdr *ipv6h; 29754002066SVedang Patel const struct iphdr *iph; 29854002066SVedang Patel struct ipv6hdr _ipv6h; 29954002066SVedang Patel 30054002066SVedang Patel ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 30154002066SVedang Patel if (!ipv6h) 30254002066SVedang Patel return 0; 30354002066SVedang Patel 30454002066SVedang Patel if (ipv6h->version == 4) { 30554002066SVedang Patel iph = (struct iphdr *)ipv6h; 30654002066SVedang Patel offset += iph->ihl * 4; 30754002066SVedang Patel 30854002066SVedang Patel /* special-case 6in4 tunnelling, as that is a common way to get 30954002066SVedang Patel * v6 connectivity in the home 31054002066SVedang Patel */ 31154002066SVedang Patel if (iph->protocol == IPPROTO_IPV6) { 31254002066SVedang Patel ipv6h = skb_header_pointer(skb, offset, 31354002066SVedang Patel sizeof(_ipv6h), &_ipv6h); 31454002066SVedang Patel 31554002066SVedang Patel if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP) 31654002066SVedang Patel return 0; 31754002066SVedang Patel } else if (iph->protocol != IPPROTO_TCP) { 31854002066SVedang Patel return 0; 31954002066SVedang Patel } 32054002066SVedang Patel } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) { 32154002066SVedang Patel return 0; 32254002066SVedang Patel } 32354002066SVedang Patel 32454002066SVedang Patel return ktime_mono_to_any(skb->skb_mstamp_ns, q->tk_offset); 32554002066SVedang Patel } 32654002066SVedang Patel 3274cfd5779SVedang Patel /* There are a few scenarios where we will have to modify the txtime from 3284cfd5779SVedang Patel * what is read from next_txtime in sched_entry. They are: 3294cfd5779SVedang Patel * 1. If txtime is in the past, 3304cfd5779SVedang Patel * a. The gate for the traffic class is currently open and packet can be 3314cfd5779SVedang Patel * transmitted before it closes, schedule the packet right away. 3324cfd5779SVedang Patel * b. If the gate corresponding to the traffic class is going to open later 3334cfd5779SVedang Patel * in the cycle, set the txtime of packet to the interval start. 3344cfd5779SVedang Patel * 2. If txtime is in the future, there are packets corresponding to the 3354cfd5779SVedang Patel * current traffic class waiting to be transmitted. So, the following 3364cfd5779SVedang Patel * possibilities exist: 3374cfd5779SVedang Patel * a. We can transmit the packet before the window containing the txtime 3384cfd5779SVedang Patel * closes. 3394cfd5779SVedang Patel * b. The window might close before the transmission can be completed 3404cfd5779SVedang Patel * successfully. So, schedule the packet in the next open window. 3414cfd5779SVedang Patel */ 3424cfd5779SVedang Patel static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch) 3434cfd5779SVedang Patel { 34454002066SVedang Patel ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp; 3454cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 3464cfd5779SVedang Patel struct sched_gate_list *sched, *admin; 3474cfd5779SVedang Patel ktime_t minimum_time, now, txtime; 3484cfd5779SVedang Patel int len, packet_transmit_time; 3494cfd5779SVedang Patel struct sched_entry *entry; 3504cfd5779SVedang Patel bool sched_changed; 3514cfd5779SVedang Patel 3527ede7b03SVedang Patel now = taprio_get_time(q); 3534cfd5779SVedang Patel minimum_time = ktime_add_ns(now, q->txtime_delay); 3544cfd5779SVedang Patel 35554002066SVedang Patel tcp_tstamp = get_tcp_tstamp(q, skb); 35654002066SVedang Patel minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp); 35754002066SVedang Patel 3584cfd5779SVedang Patel rcu_read_lock(); 3594cfd5779SVedang Patel admin = rcu_dereference(q->admin_sched); 3604cfd5779SVedang Patel sched = rcu_dereference(q->oper_sched); 3614cfd5779SVedang Patel if (admin && ktime_after(minimum_time, admin->base_time)) 3624cfd5779SVedang Patel switch_schedules(q, &admin, &sched); 3634cfd5779SVedang Patel 3644cfd5779SVedang Patel /* Until the schedule starts, all the queues are open */ 3654cfd5779SVedang Patel if (!sched || ktime_before(minimum_time, sched->base_time)) { 3664cfd5779SVedang Patel txtime = minimum_time; 3674cfd5779SVedang Patel goto done; 3684cfd5779SVedang Patel } 3694cfd5779SVedang Patel 3704cfd5779SVedang Patel len = qdisc_pkt_len(skb); 3714cfd5779SVedang Patel packet_transmit_time = length_to_duration(q, len); 3724cfd5779SVedang Patel 3734cfd5779SVedang Patel do { 3744cfd5779SVedang Patel sched_changed = 0; 3754cfd5779SVedang Patel 3764cfd5779SVedang Patel entry = find_entry_to_transmit(skb, sch, sched, admin, 3774cfd5779SVedang Patel minimum_time, 3784cfd5779SVedang Patel &interval_start, &interval_end, 3794cfd5779SVedang Patel false); 3804cfd5779SVedang Patel if (!entry) { 3814cfd5779SVedang Patel txtime = 0; 3824cfd5779SVedang Patel goto done; 3834cfd5779SVedang Patel } 3844cfd5779SVedang Patel 3854cfd5779SVedang Patel txtime = entry->next_txtime; 3864cfd5779SVedang Patel txtime = max_t(ktime_t, txtime, minimum_time); 3874cfd5779SVedang Patel txtime = max_t(ktime_t, txtime, interval_start); 3884cfd5779SVedang Patel 3894cfd5779SVedang Patel if (admin && admin != sched && 3904cfd5779SVedang Patel ktime_after(txtime, admin->base_time)) { 3914cfd5779SVedang Patel sched = admin; 3924cfd5779SVedang Patel sched_changed = 1; 3934cfd5779SVedang Patel continue; 3944cfd5779SVedang Patel } 3954cfd5779SVedang Patel 3964cfd5779SVedang Patel transmit_end_time = ktime_add(txtime, packet_transmit_time); 3974cfd5779SVedang Patel minimum_time = transmit_end_time; 3984cfd5779SVedang Patel 3994cfd5779SVedang Patel /* Update the txtime of current entry to the next time it's 4004cfd5779SVedang Patel * interval starts. 4014cfd5779SVedang Patel */ 4024cfd5779SVedang Patel if (ktime_after(transmit_end_time, interval_end)) 4034cfd5779SVedang Patel entry->next_txtime = ktime_add(interval_start, sched->cycle_time); 4044cfd5779SVedang Patel } while (sched_changed || ktime_after(transmit_end_time, interval_end)); 4054cfd5779SVedang Patel 4064cfd5779SVedang Patel entry->next_txtime = transmit_end_time; 4074cfd5779SVedang Patel 4084cfd5779SVedang Patel done: 4094cfd5779SVedang Patel rcu_read_unlock(); 4104cfd5779SVedang Patel return txtime; 4114cfd5779SVedang Patel } 4124cfd5779SVedang Patel 413*ac5c66f2SPetr Machata static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch, 4145a781ccbSVinicius Costa Gomes struct sk_buff **to_free) 4155a781ccbSVinicius Costa Gomes { 4165a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 4175a781ccbSVinicius Costa Gomes struct Qdisc *child; 4185a781ccbSVinicius Costa Gomes int queue; 4195a781ccbSVinicius Costa Gomes 4205a781ccbSVinicius Costa Gomes queue = skb_get_queue_mapping(skb); 4215a781ccbSVinicius Costa Gomes 4225a781ccbSVinicius Costa Gomes child = q->qdiscs[queue]; 4235a781ccbSVinicius Costa Gomes if (unlikely(!child)) 4245a781ccbSVinicius Costa Gomes return qdisc_drop(skb, sch, to_free); 4255a781ccbSVinicius Costa Gomes 4264cfd5779SVedang Patel if (skb->sk && sock_flag(skb->sk, SOCK_TXTIME)) { 4274cfd5779SVedang Patel if (!is_valid_interval(skb, sch)) 4284cfd5779SVedang Patel return qdisc_drop(skb, sch, to_free); 4294cfd5779SVedang Patel } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 4304cfd5779SVedang Patel skb->tstamp = get_packet_txtime(skb, sch); 4314cfd5779SVedang Patel if (!skb->tstamp) 4324cfd5779SVedang Patel return qdisc_drop(skb, sch, to_free); 4334cfd5779SVedang Patel } 4344cfd5779SVedang Patel 4355a781ccbSVinicius Costa Gomes qdisc_qstats_backlog_inc(sch, skb); 4365a781ccbSVinicius Costa Gomes sch->q.qlen++; 4375a781ccbSVinicius Costa Gomes 438*ac5c66f2SPetr Machata return qdisc_enqueue(skb, child, to_free); 4395a781ccbSVinicius Costa Gomes } 4405a781ccbSVinicius Costa Gomes 4419c66d156SVinicius Costa Gomes static struct sk_buff *taprio_peek_soft(struct Qdisc *sch) 4425a781ccbSVinicius Costa Gomes { 4435a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 4445a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 4455a781ccbSVinicius Costa Gomes struct sched_entry *entry; 4465a781ccbSVinicius Costa Gomes struct sk_buff *skb; 4475a781ccbSVinicius Costa Gomes u32 gate_mask; 4485a781ccbSVinicius Costa Gomes int i; 4495a781ccbSVinicius Costa Gomes 4505a781ccbSVinicius Costa Gomes rcu_read_lock(); 4515a781ccbSVinicius Costa Gomes entry = rcu_dereference(q->current_entry); 4522684d1b7SAndre Guedes gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 4535a781ccbSVinicius Costa Gomes rcu_read_unlock(); 4545a781ccbSVinicius Costa Gomes 4555a781ccbSVinicius Costa Gomes if (!gate_mask) 4565a781ccbSVinicius Costa Gomes return NULL; 4575a781ccbSVinicius Costa Gomes 4585a781ccbSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 4595a781ccbSVinicius Costa Gomes struct Qdisc *child = q->qdiscs[i]; 4605a781ccbSVinicius Costa Gomes int prio; 4615a781ccbSVinicius Costa Gomes u8 tc; 4625a781ccbSVinicius Costa Gomes 4635a781ccbSVinicius Costa Gomes if (unlikely(!child)) 4645a781ccbSVinicius Costa Gomes continue; 4655a781ccbSVinicius Costa Gomes 4665a781ccbSVinicius Costa Gomes skb = child->ops->peek(child); 4675a781ccbSVinicius Costa Gomes if (!skb) 4685a781ccbSVinicius Costa Gomes continue; 4695a781ccbSVinicius Costa Gomes 4704cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(q->flags)) 4714cfd5779SVedang Patel return skb; 4724cfd5779SVedang Patel 4735a781ccbSVinicius Costa Gomes prio = skb->priority; 4745a781ccbSVinicius Costa Gomes tc = netdev_get_prio_tc_map(dev, prio); 4755a781ccbSVinicius Costa Gomes 4765a781ccbSVinicius Costa Gomes if (!(gate_mask & BIT(tc))) 4772684d1b7SAndre Guedes continue; 4785a781ccbSVinicius Costa Gomes 4795a781ccbSVinicius Costa Gomes return skb; 4805a781ccbSVinicius Costa Gomes } 4815a781ccbSVinicius Costa Gomes 4825a781ccbSVinicius Costa Gomes return NULL; 4835a781ccbSVinicius Costa Gomes } 4845a781ccbSVinicius Costa Gomes 4859c66d156SVinicius Costa Gomes static struct sk_buff *taprio_peek_offload(struct Qdisc *sch) 4869c66d156SVinicius Costa Gomes { 4879c66d156SVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 4889c66d156SVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 4899c66d156SVinicius Costa Gomes struct sk_buff *skb; 4909c66d156SVinicius Costa Gomes int i; 4919c66d156SVinicius Costa Gomes 4929c66d156SVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 4939c66d156SVinicius Costa Gomes struct Qdisc *child = q->qdiscs[i]; 4949c66d156SVinicius Costa Gomes 4959c66d156SVinicius Costa Gomes if (unlikely(!child)) 4969c66d156SVinicius Costa Gomes continue; 4979c66d156SVinicius Costa Gomes 4989c66d156SVinicius Costa Gomes skb = child->ops->peek(child); 4999c66d156SVinicius Costa Gomes if (!skb) 5009c66d156SVinicius Costa Gomes continue; 5019c66d156SVinicius Costa Gomes 5029c66d156SVinicius Costa Gomes return skb; 5039c66d156SVinicius Costa Gomes } 5049c66d156SVinicius Costa Gomes 5059c66d156SVinicius Costa Gomes return NULL; 5069c66d156SVinicius Costa Gomes } 5079c66d156SVinicius Costa Gomes 5089c66d156SVinicius Costa Gomes static struct sk_buff *taprio_peek(struct Qdisc *sch) 5099c66d156SVinicius Costa Gomes { 5109c66d156SVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 5119c66d156SVinicius Costa Gomes 5129c66d156SVinicius Costa Gomes return q->peek(sch); 5139c66d156SVinicius Costa Gomes } 5149c66d156SVinicius Costa Gomes 51523bddf69SJakub Kicinski static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry) 51623bddf69SJakub Kicinski { 51723bddf69SJakub Kicinski atomic_set(&entry->budget, 51823bddf69SJakub Kicinski div64_u64((u64)entry->interval * 1000, 51923bddf69SJakub Kicinski atomic64_read(&q->picos_per_byte))); 5205a781ccbSVinicius Costa Gomes } 5215a781ccbSVinicius Costa Gomes 5229c66d156SVinicius Costa Gomes static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch) 5235a781ccbSVinicius Costa Gomes { 5245a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 5255a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 5268c79f0eaSVinicius Costa Gomes struct sk_buff *skb = NULL; 5275a781ccbSVinicius Costa Gomes struct sched_entry *entry; 5285a781ccbSVinicius Costa Gomes u32 gate_mask; 5295a781ccbSVinicius Costa Gomes int i; 5305a781ccbSVinicius Costa Gomes 5315a781ccbSVinicius Costa Gomes rcu_read_lock(); 5325a781ccbSVinicius Costa Gomes entry = rcu_dereference(q->current_entry); 5335a781ccbSVinicius Costa Gomes /* if there's no entry, it means that the schedule didn't 5345a781ccbSVinicius Costa Gomes * start yet, so force all gates to be open, this is in 5355a781ccbSVinicius Costa Gomes * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5 5365a781ccbSVinicius Costa Gomes * "AdminGateSates" 5375a781ccbSVinicius Costa Gomes */ 5385a781ccbSVinicius Costa Gomes gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 5395a781ccbSVinicius Costa Gomes 5405a781ccbSVinicius Costa Gomes if (!gate_mask) 5418c79f0eaSVinicius Costa Gomes goto done; 5425a781ccbSVinicius Costa Gomes 5435a781ccbSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 5445a781ccbSVinicius Costa Gomes struct Qdisc *child = q->qdiscs[i]; 5455a781ccbSVinicius Costa Gomes ktime_t guard; 5465a781ccbSVinicius Costa Gomes int prio; 5475a781ccbSVinicius Costa Gomes int len; 5485a781ccbSVinicius Costa Gomes u8 tc; 5495a781ccbSVinicius Costa Gomes 5505a781ccbSVinicius Costa Gomes if (unlikely(!child)) 5515a781ccbSVinicius Costa Gomes continue; 5525a781ccbSVinicius Costa Gomes 5534cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 5544cfd5779SVedang Patel skb = child->ops->dequeue(child); 5554cfd5779SVedang Patel if (!skb) 5564cfd5779SVedang Patel continue; 5574cfd5779SVedang Patel goto skb_found; 5584cfd5779SVedang Patel } 5594cfd5779SVedang Patel 5605a781ccbSVinicius Costa Gomes skb = child->ops->peek(child); 5615a781ccbSVinicius Costa Gomes if (!skb) 5625a781ccbSVinicius Costa Gomes continue; 5635a781ccbSVinicius Costa Gomes 5645a781ccbSVinicius Costa Gomes prio = skb->priority; 5655a781ccbSVinicius Costa Gomes tc = netdev_get_prio_tc_map(dev, prio); 5665a781ccbSVinicius Costa Gomes 567b09fe70eSVinicius Costa Gomes if (!(gate_mask & BIT(tc))) { 568b09fe70eSVinicius Costa Gomes skb = NULL; 5695a781ccbSVinicius Costa Gomes continue; 570b09fe70eSVinicius Costa Gomes } 5715a781ccbSVinicius Costa Gomes 5725a781ccbSVinicius Costa Gomes len = qdisc_pkt_len(skb); 5737ede7b03SVedang Patel guard = ktime_add_ns(taprio_get_time(q), 5745a781ccbSVinicius Costa Gomes length_to_duration(q, len)); 5755a781ccbSVinicius Costa Gomes 5765a781ccbSVinicius Costa Gomes /* In the case that there's no gate entry, there's no 5775a781ccbSVinicius Costa Gomes * guard band ... 5785a781ccbSVinicius Costa Gomes */ 5795a781ccbSVinicius Costa Gomes if (gate_mask != TAPRIO_ALL_GATES_OPEN && 580b09fe70eSVinicius Costa Gomes ktime_after(guard, entry->close_time)) { 581b09fe70eSVinicius Costa Gomes skb = NULL; 5826e734c82SAndre Guedes continue; 583b09fe70eSVinicius Costa Gomes } 5845a781ccbSVinicius Costa Gomes 5855a781ccbSVinicius Costa Gomes /* ... and no budget. */ 5865a781ccbSVinicius Costa Gomes if (gate_mask != TAPRIO_ALL_GATES_OPEN && 587b09fe70eSVinicius Costa Gomes atomic_sub_return(len, &entry->budget) < 0) { 588b09fe70eSVinicius Costa Gomes skb = NULL; 5896e734c82SAndre Guedes continue; 590b09fe70eSVinicius Costa Gomes } 5915a781ccbSVinicius Costa Gomes 5925a781ccbSVinicius Costa Gomes skb = child->ops->dequeue(child); 5935a781ccbSVinicius Costa Gomes if (unlikely(!skb)) 5948c79f0eaSVinicius Costa Gomes goto done; 5955a781ccbSVinicius Costa Gomes 5964cfd5779SVedang Patel skb_found: 5975a781ccbSVinicius Costa Gomes qdisc_bstats_update(sch, skb); 5985a781ccbSVinicius Costa Gomes qdisc_qstats_backlog_dec(sch, skb); 5995a781ccbSVinicius Costa Gomes sch->q.qlen--; 6005a781ccbSVinicius Costa Gomes 6018c79f0eaSVinicius Costa Gomes goto done; 6025a781ccbSVinicius Costa Gomes } 6035a781ccbSVinicius Costa Gomes 6048c79f0eaSVinicius Costa Gomes done: 6058c79f0eaSVinicius Costa Gomes rcu_read_unlock(); 6068c79f0eaSVinicius Costa Gomes 6078c79f0eaSVinicius Costa Gomes return skb; 6085a781ccbSVinicius Costa Gomes } 6095a781ccbSVinicius Costa Gomes 6109c66d156SVinicius Costa Gomes static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch) 6119c66d156SVinicius Costa Gomes { 6129c66d156SVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 6139c66d156SVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 6149c66d156SVinicius Costa Gomes struct sk_buff *skb; 6159c66d156SVinicius Costa Gomes int i; 6169c66d156SVinicius Costa Gomes 6179c66d156SVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 6189c66d156SVinicius Costa Gomes struct Qdisc *child = q->qdiscs[i]; 6199c66d156SVinicius Costa Gomes 6209c66d156SVinicius Costa Gomes if (unlikely(!child)) 6219c66d156SVinicius Costa Gomes continue; 6229c66d156SVinicius Costa Gomes 6239c66d156SVinicius Costa Gomes skb = child->ops->dequeue(child); 6249c66d156SVinicius Costa Gomes if (unlikely(!skb)) 6259c66d156SVinicius Costa Gomes continue; 6269c66d156SVinicius Costa Gomes 6279c66d156SVinicius Costa Gomes qdisc_bstats_update(sch, skb); 6289c66d156SVinicius Costa Gomes qdisc_qstats_backlog_dec(sch, skb); 6299c66d156SVinicius Costa Gomes sch->q.qlen--; 6309c66d156SVinicius Costa Gomes 6319c66d156SVinicius Costa Gomes return skb; 6329c66d156SVinicius Costa Gomes } 6339c66d156SVinicius Costa Gomes 6349c66d156SVinicius Costa Gomes return NULL; 6359c66d156SVinicius Costa Gomes } 6369c66d156SVinicius Costa Gomes 6379c66d156SVinicius Costa Gomes static struct sk_buff *taprio_dequeue(struct Qdisc *sch) 6389c66d156SVinicius Costa Gomes { 6399c66d156SVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 6409c66d156SVinicius Costa Gomes 6419c66d156SVinicius Costa Gomes return q->dequeue(sch); 6429c66d156SVinicius Costa Gomes } 6439c66d156SVinicius Costa Gomes 6446ca6a665SVinicius Costa Gomes static bool should_restart_cycle(const struct sched_gate_list *oper, 6456ca6a665SVinicius Costa Gomes const struct sched_entry *entry) 6466ca6a665SVinicius Costa Gomes { 6476ca6a665SVinicius Costa Gomes if (list_is_last(&entry->list, &oper->entries)) 6486ca6a665SVinicius Costa Gomes return true; 6496ca6a665SVinicius Costa Gomes 6506ca6a665SVinicius Costa Gomes if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0) 6516ca6a665SVinicius Costa Gomes return true; 6526ca6a665SVinicius Costa Gomes 6536ca6a665SVinicius Costa Gomes return false; 6546ca6a665SVinicius Costa Gomes } 6556ca6a665SVinicius Costa Gomes 656a3d43c0dSVinicius Costa Gomes static bool should_change_schedules(const struct sched_gate_list *admin, 657a3d43c0dSVinicius Costa Gomes const struct sched_gate_list *oper, 658a3d43c0dSVinicius Costa Gomes ktime_t close_time) 659a3d43c0dSVinicius Costa Gomes { 660c25031e9SVinicius Costa Gomes ktime_t next_base_time, extension_time; 661a3d43c0dSVinicius Costa Gomes 662a3d43c0dSVinicius Costa Gomes if (!admin) 663a3d43c0dSVinicius Costa Gomes return false; 664a3d43c0dSVinicius Costa Gomes 665a3d43c0dSVinicius Costa Gomes next_base_time = sched_base_time(admin); 666a3d43c0dSVinicius Costa Gomes 667a3d43c0dSVinicius Costa Gomes /* This is the simple case, the close_time would fall after 668a3d43c0dSVinicius Costa Gomes * the next schedule base_time. 669a3d43c0dSVinicius Costa Gomes */ 670a3d43c0dSVinicius Costa Gomes if (ktime_compare(next_base_time, close_time) <= 0) 671a3d43c0dSVinicius Costa Gomes return true; 672a3d43c0dSVinicius Costa Gomes 673c25031e9SVinicius Costa Gomes /* This is the cycle_time_extension case, if the close_time 674c25031e9SVinicius Costa Gomes * plus the amount that can be extended would fall after the 675c25031e9SVinicius Costa Gomes * next schedule base_time, we can extend the current schedule 676c25031e9SVinicius Costa Gomes * for that amount. 677c25031e9SVinicius Costa Gomes */ 678c25031e9SVinicius Costa Gomes extension_time = ktime_add_ns(close_time, oper->cycle_time_extension); 679c25031e9SVinicius Costa Gomes 680c25031e9SVinicius Costa Gomes /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about 681c25031e9SVinicius Costa Gomes * how precisely the extension should be made. So after 682c25031e9SVinicius Costa Gomes * conformance testing, this logic may change. 683c25031e9SVinicius Costa Gomes */ 684c25031e9SVinicius Costa Gomes if (ktime_compare(next_base_time, extension_time) <= 0) 685c25031e9SVinicius Costa Gomes return true; 686c25031e9SVinicius Costa Gomes 687a3d43c0dSVinicius Costa Gomes return false; 688a3d43c0dSVinicius Costa Gomes } 689a3d43c0dSVinicius Costa Gomes 6905a781ccbSVinicius Costa Gomes static enum hrtimer_restart advance_sched(struct hrtimer *timer) 6915a781ccbSVinicius Costa Gomes { 6925a781ccbSVinicius Costa Gomes struct taprio_sched *q = container_of(timer, struct taprio_sched, 6935a781ccbSVinicius Costa Gomes advance_timer); 694a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin; 6955a781ccbSVinicius Costa Gomes struct sched_entry *entry, *next; 6965a781ccbSVinicius Costa Gomes struct Qdisc *sch = q->root; 6975a781ccbSVinicius Costa Gomes ktime_t close_time; 6985a781ccbSVinicius Costa Gomes 6995a781ccbSVinicius Costa Gomes spin_lock(&q->current_entry_lock); 7005a781ccbSVinicius Costa Gomes entry = rcu_dereference_protected(q->current_entry, 7015a781ccbSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 702a3d43c0dSVinicius Costa Gomes oper = rcu_dereference_protected(q->oper_sched, 703a3d43c0dSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 704a3d43c0dSVinicius Costa Gomes admin = rcu_dereference_protected(q->admin_sched, 705a3d43c0dSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 7065a781ccbSVinicius Costa Gomes 707a3d43c0dSVinicius Costa Gomes if (!oper) 708a3d43c0dSVinicius Costa Gomes switch_schedules(q, &admin, &oper); 709a3d43c0dSVinicius Costa Gomes 710a3d43c0dSVinicius Costa Gomes /* This can happen in two cases: 1. this is the very first run 711a3d43c0dSVinicius Costa Gomes * of this function (i.e. we weren't running any schedule 712a3d43c0dSVinicius Costa Gomes * previously); 2. The previous schedule just ended. The first 713a3d43c0dSVinicius Costa Gomes * entry of all schedules are pre-calculated during the 714a3d43c0dSVinicius Costa Gomes * schedule initialization. 7155a781ccbSVinicius Costa Gomes */ 716a3d43c0dSVinicius Costa Gomes if (unlikely(!entry || entry->close_time == oper->base_time)) { 717a3d43c0dSVinicius Costa Gomes next = list_first_entry(&oper->entries, struct sched_entry, 7185a781ccbSVinicius Costa Gomes list); 7195a781ccbSVinicius Costa Gomes close_time = next->close_time; 7205a781ccbSVinicius Costa Gomes goto first_run; 7215a781ccbSVinicius Costa Gomes } 7225a781ccbSVinicius Costa Gomes 7236ca6a665SVinicius Costa Gomes if (should_restart_cycle(oper, entry)) { 724a3d43c0dSVinicius Costa Gomes next = list_first_entry(&oper->entries, struct sched_entry, 7255a781ccbSVinicius Costa Gomes list); 7266ca6a665SVinicius Costa Gomes oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time, 7276ca6a665SVinicius Costa Gomes oper->cycle_time); 7286ca6a665SVinicius Costa Gomes } else { 7295a781ccbSVinicius Costa Gomes next = list_next_entry(entry, list); 7306ca6a665SVinicius Costa Gomes } 7315a781ccbSVinicius Costa Gomes 7325a781ccbSVinicius Costa Gomes close_time = ktime_add_ns(entry->close_time, next->interval); 7336ca6a665SVinicius Costa Gomes close_time = min_t(ktime_t, close_time, oper->cycle_close_time); 7345a781ccbSVinicius Costa Gomes 735a3d43c0dSVinicius Costa Gomes if (should_change_schedules(admin, oper, close_time)) { 736a3d43c0dSVinicius Costa Gomes /* Set things so the next time this runs, the new 737a3d43c0dSVinicius Costa Gomes * schedule runs. 738a3d43c0dSVinicius Costa Gomes */ 739a3d43c0dSVinicius Costa Gomes close_time = sched_base_time(admin); 740a3d43c0dSVinicius Costa Gomes switch_schedules(q, &admin, &oper); 741a3d43c0dSVinicius Costa Gomes } 742a3d43c0dSVinicius Costa Gomes 7435a781ccbSVinicius Costa Gomes next->close_time = close_time; 74423bddf69SJakub Kicinski taprio_set_budget(q, next); 7455a781ccbSVinicius Costa Gomes 7465a781ccbSVinicius Costa Gomes first_run: 7475a781ccbSVinicius Costa Gomes rcu_assign_pointer(q->current_entry, next); 7485a781ccbSVinicius Costa Gomes spin_unlock(&q->current_entry_lock); 7495a781ccbSVinicius Costa Gomes 7505a781ccbSVinicius Costa Gomes hrtimer_set_expires(&q->advance_timer, close_time); 7515a781ccbSVinicius Costa Gomes 7525a781ccbSVinicius Costa Gomes rcu_read_lock(); 7535a781ccbSVinicius Costa Gomes __netif_schedule(sch); 7545a781ccbSVinicius Costa Gomes rcu_read_unlock(); 7555a781ccbSVinicius Costa Gomes 7565a781ccbSVinicius Costa Gomes return HRTIMER_RESTART; 7575a781ccbSVinicius Costa Gomes } 7585a781ccbSVinicius Costa Gomes 7595a781ccbSVinicius Costa Gomes static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { 7605a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 }, 7615a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 }, 7625a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 }, 7635a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 }, 7645a781ccbSVinicius Costa Gomes }; 7655a781ccbSVinicius Costa Gomes 7665a781ccbSVinicius Costa Gomes static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = { 7675a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_PRIOMAP] = { 7685a781ccbSVinicius Costa Gomes .len = sizeof(struct tc_mqprio_qopt) 7695a781ccbSVinicius Costa Gomes }, 7705a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED }, 7715a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 }, 7725a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED }, 7735a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 }, 7746ca6a665SVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 }, 775c25031e9SVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 }, 77649c684d7SVinicius Costa Gomes [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 }, 777e13aaa06SJakub Kicinski [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 }, 7785a781ccbSVinicius Costa Gomes }; 7795a781ccbSVinicius Costa Gomes 7805a781ccbSVinicius Costa Gomes static int fill_sched_entry(struct nlattr **tb, struct sched_entry *entry, 7815a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 7825a781ccbSVinicius Costa Gomes { 7835a781ccbSVinicius Costa Gomes u32 interval = 0; 7845a781ccbSVinicius Costa Gomes 7855a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD]) 7865a781ccbSVinicius Costa Gomes entry->command = nla_get_u8( 7875a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_CMD]); 7885a781ccbSVinicius Costa Gomes 7895a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]) 7905a781ccbSVinicius Costa Gomes entry->gate_mask = nla_get_u32( 7915a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]); 7925a781ccbSVinicius Costa Gomes 7935a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]) 7945a781ccbSVinicius Costa Gomes interval = nla_get_u32( 7955a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]); 7965a781ccbSVinicius Costa Gomes 7975a781ccbSVinicius Costa Gomes if (interval == 0) { 7985a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry"); 7995a781ccbSVinicius Costa Gomes return -EINVAL; 8005a781ccbSVinicius Costa Gomes } 8015a781ccbSVinicius Costa Gomes 8025a781ccbSVinicius Costa Gomes entry->interval = interval; 8035a781ccbSVinicius Costa Gomes 8045a781ccbSVinicius Costa Gomes return 0; 8055a781ccbSVinicius Costa Gomes } 8065a781ccbSVinicius Costa Gomes 8075a781ccbSVinicius Costa Gomes static int parse_sched_entry(struct nlattr *n, struct sched_entry *entry, 8085a781ccbSVinicius Costa Gomes int index, struct netlink_ext_ack *extack) 8095a781ccbSVinicius Costa Gomes { 8105a781ccbSVinicius Costa Gomes struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { }; 8115a781ccbSVinicius Costa Gomes int err; 8125a781ccbSVinicius Costa Gomes 8138cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n, 8145a781ccbSVinicius Costa Gomes entry_policy, NULL); 8155a781ccbSVinicius Costa Gomes if (err < 0) { 8165a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Could not parse nested entry"); 8175a781ccbSVinicius Costa Gomes return -EINVAL; 8185a781ccbSVinicius Costa Gomes } 8195a781ccbSVinicius Costa Gomes 8205a781ccbSVinicius Costa Gomes entry->index = index; 8215a781ccbSVinicius Costa Gomes 8225a781ccbSVinicius Costa Gomes return fill_sched_entry(tb, entry, extack); 8235a781ccbSVinicius Costa Gomes } 8245a781ccbSVinicius Costa Gomes 8255a781ccbSVinicius Costa Gomes static int parse_sched_list(struct nlattr *list, 826a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, 8275a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 8285a781ccbSVinicius Costa Gomes { 8295a781ccbSVinicius Costa Gomes struct nlattr *n; 8305a781ccbSVinicius Costa Gomes int err, rem; 8315a781ccbSVinicius Costa Gomes int i = 0; 8325a781ccbSVinicius Costa Gomes 8335a781ccbSVinicius Costa Gomes if (!list) 8345a781ccbSVinicius Costa Gomes return -EINVAL; 8355a781ccbSVinicius Costa Gomes 8365a781ccbSVinicius Costa Gomes nla_for_each_nested(n, list, rem) { 8375a781ccbSVinicius Costa Gomes struct sched_entry *entry; 8385a781ccbSVinicius Costa Gomes 8395a781ccbSVinicius Costa Gomes if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) { 8405a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'"); 8415a781ccbSVinicius Costa Gomes continue; 8425a781ccbSVinicius Costa Gomes } 8435a781ccbSVinicius Costa Gomes 8445a781ccbSVinicius Costa Gomes entry = kzalloc(sizeof(*entry), GFP_KERNEL); 8455a781ccbSVinicius Costa Gomes if (!entry) { 8465a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Not enough memory for entry"); 8475a781ccbSVinicius Costa Gomes return -ENOMEM; 8485a781ccbSVinicius Costa Gomes } 8495a781ccbSVinicius Costa Gomes 8505a781ccbSVinicius Costa Gomes err = parse_sched_entry(n, entry, i, extack); 8515a781ccbSVinicius Costa Gomes if (err < 0) { 8525a781ccbSVinicius Costa Gomes kfree(entry); 8535a781ccbSVinicius Costa Gomes return err; 8545a781ccbSVinicius Costa Gomes } 8555a781ccbSVinicius Costa Gomes 856a3d43c0dSVinicius Costa Gomes list_add_tail(&entry->list, &sched->entries); 8575a781ccbSVinicius Costa Gomes i++; 8585a781ccbSVinicius Costa Gomes } 8595a781ccbSVinicius Costa Gomes 860a3d43c0dSVinicius Costa Gomes sched->num_entries = i; 8615a781ccbSVinicius Costa Gomes 8625a781ccbSVinicius Costa Gomes return i; 8635a781ccbSVinicius Costa Gomes } 8645a781ccbSVinicius Costa Gomes 865a3d43c0dSVinicius Costa Gomes static int parse_taprio_schedule(struct nlattr **tb, 866a3d43c0dSVinicius Costa Gomes struct sched_gate_list *new, 8675a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 8685a781ccbSVinicius Costa Gomes { 8695a781ccbSVinicius Costa Gomes int err = 0; 8705a781ccbSVinicius Costa Gomes 871a3d43c0dSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) { 872a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Adding a single entry is not supported"); 873a3d43c0dSVinicius Costa Gomes return -ENOTSUPP; 874a3d43c0dSVinicius Costa Gomes } 8755a781ccbSVinicius Costa Gomes 8765a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]) 877a3d43c0dSVinicius Costa Gomes new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]); 8785a781ccbSVinicius Costa Gomes 879c25031e9SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]) 880c25031e9SVinicius Costa Gomes new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]); 881c25031e9SVinicius Costa Gomes 8826ca6a665SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]) 8836ca6a665SVinicius Costa Gomes new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]); 8846ca6a665SVinicius Costa Gomes 8855a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]) 8865a781ccbSVinicius Costa Gomes err = parse_sched_list( 887a3d43c0dSVinicius Costa Gomes tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], new, extack); 888a3d43c0dSVinicius Costa Gomes if (err < 0) 8895a781ccbSVinicius Costa Gomes return err; 890a3d43c0dSVinicius Costa Gomes 891037be037SVedang Patel if (!new->cycle_time) { 892037be037SVedang Patel struct sched_entry *entry; 893037be037SVedang Patel ktime_t cycle = 0; 894037be037SVedang Patel 895037be037SVedang Patel list_for_each_entry(entry, &new->entries, list) 896037be037SVedang Patel cycle = ktime_add_ns(cycle, entry->interval); 897037be037SVedang Patel new->cycle_time = cycle; 898037be037SVedang Patel } 899037be037SVedang Patel 900a3d43c0dSVinicius Costa Gomes return 0; 9015a781ccbSVinicius Costa Gomes } 9025a781ccbSVinicius Costa Gomes 9035a781ccbSVinicius Costa Gomes static int taprio_parse_mqprio_opt(struct net_device *dev, 9045a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt *qopt, 9054cfd5779SVedang Patel struct netlink_ext_ack *extack, 9064cfd5779SVedang Patel u32 taprio_flags) 9075a781ccbSVinicius Costa Gomes { 9085a781ccbSVinicius Costa Gomes int i, j; 9095a781ccbSVinicius Costa Gomes 910a3d43c0dSVinicius Costa Gomes if (!qopt && !dev->num_tc) { 9115a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary"); 9125a781ccbSVinicius Costa Gomes return -EINVAL; 9135a781ccbSVinicius Costa Gomes } 9145a781ccbSVinicius Costa Gomes 915a3d43c0dSVinicius Costa Gomes /* If num_tc is already set, it means that the user already 916a3d43c0dSVinicius Costa Gomes * configured the mqprio part 917a3d43c0dSVinicius Costa Gomes */ 918a3d43c0dSVinicius Costa Gomes if (dev->num_tc) 919a3d43c0dSVinicius Costa Gomes return 0; 920a3d43c0dSVinicius Costa Gomes 9215a781ccbSVinicius Costa Gomes /* Verify num_tc is not out of max range */ 9225a781ccbSVinicius Costa Gomes if (qopt->num_tc > TC_MAX_QUEUE) { 9235a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range"); 9245a781ccbSVinicius Costa Gomes return -EINVAL; 9255a781ccbSVinicius Costa Gomes } 9265a781ccbSVinicius Costa Gomes 9275a781ccbSVinicius Costa Gomes /* taprio imposes that traffic classes map 1:n to tx queues */ 9285a781ccbSVinicius Costa Gomes if (qopt->num_tc > dev->num_tx_queues) { 9295a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues"); 9305a781ccbSVinicius Costa Gomes return -EINVAL; 9315a781ccbSVinicius Costa Gomes } 9325a781ccbSVinicius Costa Gomes 9335a781ccbSVinicius Costa Gomes /* Verify priority mapping uses valid tcs */ 934b5a0faa3SIvan Khoronzhuk for (i = 0; i <= TC_BITMASK; i++) { 9355a781ccbSVinicius Costa Gomes if (qopt->prio_tc_map[i] >= qopt->num_tc) { 9365a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping"); 9375a781ccbSVinicius Costa Gomes return -EINVAL; 9385a781ccbSVinicius Costa Gomes } 9395a781ccbSVinicius Costa Gomes } 9405a781ccbSVinicius Costa Gomes 9415a781ccbSVinicius Costa Gomes for (i = 0; i < qopt->num_tc; i++) { 9425a781ccbSVinicius Costa Gomes unsigned int last = qopt->offset[i] + qopt->count[i]; 9435a781ccbSVinicius Costa Gomes 9445a781ccbSVinicius Costa Gomes /* Verify the queue count is in tx range being equal to the 9455a781ccbSVinicius Costa Gomes * real_num_tx_queues indicates the last queue is in use. 9465a781ccbSVinicius Costa Gomes */ 9475a781ccbSVinicius Costa Gomes if (qopt->offset[i] >= dev->num_tx_queues || 9485a781ccbSVinicius Costa Gomes !qopt->count[i] || 9495a781ccbSVinicius Costa Gomes last > dev->real_num_tx_queues) { 9505a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping"); 9515a781ccbSVinicius Costa Gomes return -EINVAL; 9525a781ccbSVinicius Costa Gomes } 9535a781ccbSVinicius Costa Gomes 9544cfd5779SVedang Patel if (TXTIME_ASSIST_IS_ENABLED(taprio_flags)) 9554cfd5779SVedang Patel continue; 9564cfd5779SVedang Patel 9575a781ccbSVinicius Costa Gomes /* Verify that the offset and counts do not overlap */ 9585a781ccbSVinicius Costa Gomes for (j = i + 1; j < qopt->num_tc; j++) { 9595a781ccbSVinicius Costa Gomes if (last > qopt->offset[j]) { 9605a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping"); 9615a781ccbSVinicius Costa Gomes return -EINVAL; 9625a781ccbSVinicius Costa Gomes } 9635a781ccbSVinicius Costa Gomes } 9645a781ccbSVinicius Costa Gomes } 9655a781ccbSVinicius Costa Gomes 9665a781ccbSVinicius Costa Gomes return 0; 9675a781ccbSVinicius Costa Gomes } 9685a781ccbSVinicius Costa Gomes 969a3d43c0dSVinicius Costa Gomes static int taprio_get_start_time(struct Qdisc *sch, 970a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, 971a3d43c0dSVinicius Costa Gomes ktime_t *start) 9725a781ccbSVinicius Costa Gomes { 9735a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 9745a781ccbSVinicius Costa Gomes ktime_t now, base, cycle; 9755a781ccbSVinicius Costa Gomes s64 n; 9765a781ccbSVinicius Costa Gomes 977a3d43c0dSVinicius Costa Gomes base = sched_base_time(sched); 9787ede7b03SVedang Patel now = taprio_get_time(q); 9798599099fSAndre Guedes 9808599099fSAndre Guedes if (ktime_after(base, now)) { 9818599099fSAndre Guedes *start = base; 9828599099fSAndre Guedes return 0; 9838599099fSAndre Guedes } 9845a781ccbSVinicius Costa Gomes 985037be037SVedang Patel cycle = sched->cycle_time; 9865a781ccbSVinicius Costa Gomes 9878599099fSAndre Guedes /* The qdisc is expected to have at least one sched_entry. Moreover, 9888599099fSAndre Guedes * any entry must have 'interval' > 0. Thus if the cycle time is zero, 9898599099fSAndre Guedes * something went really wrong. In that case, we should warn about this 9908599099fSAndre Guedes * inconsistent state and return error. 9918599099fSAndre Guedes */ 9928599099fSAndre Guedes if (WARN_ON(!cycle)) 9938599099fSAndre Guedes return -EFAULT; 9945a781ccbSVinicius Costa Gomes 9955a781ccbSVinicius Costa Gomes /* Schedule the start time for the beginning of the next 9965a781ccbSVinicius Costa Gomes * cycle. 9975a781ccbSVinicius Costa Gomes */ 9985a781ccbSVinicius Costa Gomes n = div64_s64(ktime_sub_ns(now, base), cycle); 9998599099fSAndre Guedes *start = ktime_add_ns(base, (n + 1) * cycle); 10008599099fSAndre Guedes return 0; 10015a781ccbSVinicius Costa Gomes } 10025a781ccbSVinicius Costa Gomes 1003a3d43c0dSVinicius Costa Gomes static void setup_first_close_time(struct taprio_sched *q, 1004a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, ktime_t base) 10055a781ccbSVinicius Costa Gomes { 10065a781ccbSVinicius Costa Gomes struct sched_entry *first; 10076ca6a665SVinicius Costa Gomes ktime_t cycle; 10085a781ccbSVinicius Costa Gomes 1009a3d43c0dSVinicius Costa Gomes first = list_first_entry(&sched->entries, 1010a3d43c0dSVinicius Costa Gomes struct sched_entry, list); 10115a781ccbSVinicius Costa Gomes 1012037be037SVedang Patel cycle = sched->cycle_time; 10136ca6a665SVinicius Costa Gomes 10146ca6a665SVinicius Costa Gomes /* FIXME: find a better place to do this */ 10156ca6a665SVinicius Costa Gomes sched->cycle_close_time = ktime_add_ns(base, cycle); 10166ca6a665SVinicius Costa Gomes 1017a3d43c0dSVinicius Costa Gomes first->close_time = ktime_add_ns(base, first->interval); 101823bddf69SJakub Kicinski taprio_set_budget(q, first); 10195a781ccbSVinicius Costa Gomes rcu_assign_pointer(q->current_entry, NULL); 1020a3d43c0dSVinicius Costa Gomes } 10215a781ccbSVinicius Costa Gomes 1022a3d43c0dSVinicius Costa Gomes static void taprio_start_sched(struct Qdisc *sch, 1023a3d43c0dSVinicius Costa Gomes ktime_t start, struct sched_gate_list *new) 1024a3d43c0dSVinicius Costa Gomes { 1025a3d43c0dSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 1026a3d43c0dSVinicius Costa Gomes ktime_t expires; 1027a3d43c0dSVinicius Costa Gomes 10289c66d156SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 10299c66d156SVinicius Costa Gomes return; 10309c66d156SVinicius Costa Gomes 1031a3d43c0dSVinicius Costa Gomes expires = hrtimer_get_expires(&q->advance_timer); 1032a3d43c0dSVinicius Costa Gomes if (expires == 0) 1033a3d43c0dSVinicius Costa Gomes expires = KTIME_MAX; 1034a3d43c0dSVinicius Costa Gomes 1035a3d43c0dSVinicius Costa Gomes /* If the new schedule starts before the next expiration, we 1036a3d43c0dSVinicius Costa Gomes * reprogram it to the earliest one, so we change the admin 1037a3d43c0dSVinicius Costa Gomes * schedule to the operational one at the right time. 1038a3d43c0dSVinicius Costa Gomes */ 1039a3d43c0dSVinicius Costa Gomes start = min_t(ktime_t, start, expires); 10405a781ccbSVinicius Costa Gomes 10415a781ccbSVinicius Costa Gomes hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS); 10425a781ccbSVinicius Costa Gomes } 10435a781ccbSVinicius Costa Gomes 10447b9eba7bSLeandro Dorileo static void taprio_set_picos_per_byte(struct net_device *dev, 10457b9eba7bSLeandro Dorileo struct taprio_sched *q) 10467b9eba7bSLeandro Dorileo { 10477b9eba7bSLeandro Dorileo struct ethtool_link_ksettings ecmd; 1048f04b514cSVladimir Oltean int speed = SPEED_10; 1049f04b514cSVladimir Oltean int picos_per_byte; 1050f04b514cSVladimir Oltean int err; 10517b9eba7bSLeandro Dorileo 1052f04b514cSVladimir Oltean err = __ethtool_get_link_ksettings(dev, &ecmd); 1053f04b514cSVladimir Oltean if (err < 0) 1054f04b514cSVladimir Oltean goto skip; 1055f04b514cSVladimir Oltean 10569a9251a3SVladimir Oltean if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN) 1057f04b514cSVladimir Oltean speed = ecmd.base.speed; 1058f04b514cSVladimir Oltean 1059f04b514cSVladimir Oltean skip: 106068ce6688SVladimir Oltean picos_per_byte = (USEC_PER_SEC * 8) / speed; 10617b9eba7bSLeandro Dorileo 10627b9eba7bSLeandro Dorileo atomic64_set(&q->picos_per_byte, picos_per_byte); 10637b9eba7bSLeandro Dorileo netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n", 10647b9eba7bSLeandro Dorileo dev->name, (long long)atomic64_read(&q->picos_per_byte), 10657b9eba7bSLeandro Dorileo ecmd.base.speed); 10667b9eba7bSLeandro Dorileo } 10677b9eba7bSLeandro Dorileo 10687b9eba7bSLeandro Dorileo static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event, 10697b9eba7bSLeandro Dorileo void *ptr) 10707b9eba7bSLeandro Dorileo { 10717b9eba7bSLeandro Dorileo struct net_device *dev = netdev_notifier_info_to_dev(ptr); 10727b9eba7bSLeandro Dorileo struct net_device *qdev; 10737b9eba7bSLeandro Dorileo struct taprio_sched *q; 10747b9eba7bSLeandro Dorileo bool found = false; 10757b9eba7bSLeandro Dorileo 10767b9eba7bSLeandro Dorileo ASSERT_RTNL(); 10777b9eba7bSLeandro Dorileo 10787b9eba7bSLeandro Dorileo if (event != NETDEV_UP && event != NETDEV_CHANGE) 10797b9eba7bSLeandro Dorileo return NOTIFY_DONE; 10807b9eba7bSLeandro Dorileo 10817b9eba7bSLeandro Dorileo spin_lock(&taprio_list_lock); 10827b9eba7bSLeandro Dorileo list_for_each_entry(q, &taprio_list, taprio_list) { 10837b9eba7bSLeandro Dorileo qdev = qdisc_dev(q->root); 10847b9eba7bSLeandro Dorileo if (qdev == dev) { 10857b9eba7bSLeandro Dorileo found = true; 10867b9eba7bSLeandro Dorileo break; 10877b9eba7bSLeandro Dorileo } 10887b9eba7bSLeandro Dorileo } 10897b9eba7bSLeandro Dorileo spin_unlock(&taprio_list_lock); 10907b9eba7bSLeandro Dorileo 10917b9eba7bSLeandro Dorileo if (found) 10927b9eba7bSLeandro Dorileo taprio_set_picos_per_byte(dev, q); 10937b9eba7bSLeandro Dorileo 10947b9eba7bSLeandro Dorileo return NOTIFY_DONE; 10957b9eba7bSLeandro Dorileo } 10967b9eba7bSLeandro Dorileo 10974cfd5779SVedang Patel static void setup_txtime(struct taprio_sched *q, 10984cfd5779SVedang Patel struct sched_gate_list *sched, ktime_t base) 10994cfd5779SVedang Patel { 11004cfd5779SVedang Patel struct sched_entry *entry; 11014cfd5779SVedang Patel u32 interval = 0; 11024cfd5779SVedang Patel 11034cfd5779SVedang Patel list_for_each_entry(entry, &sched->entries, list) { 11044cfd5779SVedang Patel entry->next_txtime = ktime_add_ns(base, interval); 11054cfd5779SVedang Patel interval += entry->interval; 11064cfd5779SVedang Patel } 11074cfd5779SVedang Patel } 11084cfd5779SVedang Patel 11099c66d156SVinicius Costa Gomes static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries) 11109c66d156SVinicius Costa Gomes { 11119c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload *__offload; 11129c66d156SVinicius Costa Gomes 111311a33de2SGustavo A. R. Silva __offload = kzalloc(struct_size(__offload, offload.entries, num_entries), 111411a33de2SGustavo A. R. Silva GFP_KERNEL); 11159c66d156SVinicius Costa Gomes if (!__offload) 11169c66d156SVinicius Costa Gomes return NULL; 11179c66d156SVinicius Costa Gomes 11189c66d156SVinicius Costa Gomes refcount_set(&__offload->users, 1); 11199c66d156SVinicius Costa Gomes 11209c66d156SVinicius Costa Gomes return &__offload->offload; 11219c66d156SVinicius Costa Gomes } 11229c66d156SVinicius Costa Gomes 11239c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload 11249c66d156SVinicius Costa Gomes *offload) 11259c66d156SVinicius Costa Gomes { 11269c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload *__offload; 11279c66d156SVinicius Costa Gomes 11289c66d156SVinicius Costa Gomes __offload = container_of(offload, struct __tc_taprio_qopt_offload, 11299c66d156SVinicius Costa Gomes offload); 11309c66d156SVinicius Costa Gomes 11319c66d156SVinicius Costa Gomes refcount_inc(&__offload->users); 11329c66d156SVinicius Costa Gomes 11339c66d156SVinicius Costa Gomes return offload; 11349c66d156SVinicius Costa Gomes } 11359c66d156SVinicius Costa Gomes EXPORT_SYMBOL_GPL(taprio_offload_get); 11369c66d156SVinicius Costa Gomes 11379c66d156SVinicius Costa Gomes void taprio_offload_free(struct tc_taprio_qopt_offload *offload) 11389c66d156SVinicius Costa Gomes { 11399c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload *__offload; 11409c66d156SVinicius Costa Gomes 11419c66d156SVinicius Costa Gomes __offload = container_of(offload, struct __tc_taprio_qopt_offload, 11429c66d156SVinicius Costa Gomes offload); 11439c66d156SVinicius Costa Gomes 11449c66d156SVinicius Costa Gomes if (!refcount_dec_and_test(&__offload->users)) 11459c66d156SVinicius Costa Gomes return; 11469c66d156SVinicius Costa Gomes 11479c66d156SVinicius Costa Gomes kfree(__offload); 11489c66d156SVinicius Costa Gomes } 11499c66d156SVinicius Costa Gomes EXPORT_SYMBOL_GPL(taprio_offload_free); 11509c66d156SVinicius Costa Gomes 11519c66d156SVinicius Costa Gomes /* The function will only serve to keep the pointers to the "oper" and "admin" 11529c66d156SVinicius Costa Gomes * schedules valid in relation to their base times, so when calling dump() the 11539c66d156SVinicius Costa Gomes * users looks at the right schedules. 11549c66d156SVinicius Costa Gomes * When using full offload, the admin configuration is promoted to oper at the 11559c66d156SVinicius Costa Gomes * base_time in the PHC time domain. But because the system time is not 11569c66d156SVinicius Costa Gomes * necessarily in sync with that, we can't just trigger a hrtimer to call 11579c66d156SVinicius Costa Gomes * switch_schedules at the right hardware time. 11589c66d156SVinicius Costa Gomes * At the moment we call this by hand right away from taprio, but in the future 11599c66d156SVinicius Costa Gomes * it will be useful to create a mechanism for drivers to notify taprio of the 11609c66d156SVinicius Costa Gomes * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump(). 11619c66d156SVinicius Costa Gomes * This is left as TODO. 11629c66d156SVinicius Costa Gomes */ 1163d665c128SYi Wang static void taprio_offload_config_changed(struct taprio_sched *q) 11649c66d156SVinicius Costa Gomes { 11659c66d156SVinicius Costa Gomes struct sched_gate_list *oper, *admin; 11669c66d156SVinicius Costa Gomes 11679c66d156SVinicius Costa Gomes spin_lock(&q->current_entry_lock); 11689c66d156SVinicius Costa Gomes 11699c66d156SVinicius Costa Gomes oper = rcu_dereference_protected(q->oper_sched, 11709c66d156SVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 11719c66d156SVinicius Costa Gomes admin = rcu_dereference_protected(q->admin_sched, 11729c66d156SVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 11739c66d156SVinicius Costa Gomes 11749c66d156SVinicius Costa Gomes switch_schedules(q, &admin, &oper); 11759c66d156SVinicius Costa Gomes 11769c66d156SVinicius Costa Gomes spin_unlock(&q->current_entry_lock); 11779c66d156SVinicius Costa Gomes } 11789c66d156SVinicius Costa Gomes 11799c66d156SVinicius Costa Gomes static void taprio_sched_to_offload(struct taprio_sched *q, 11809c66d156SVinicius Costa Gomes struct sched_gate_list *sched, 11819c66d156SVinicius Costa Gomes const struct tc_mqprio_qopt *mqprio, 11829c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *offload) 11839c66d156SVinicius Costa Gomes { 11849c66d156SVinicius Costa Gomes struct sched_entry *entry; 11859c66d156SVinicius Costa Gomes int i = 0; 11869c66d156SVinicius Costa Gomes 11879c66d156SVinicius Costa Gomes offload->base_time = sched->base_time; 11889c66d156SVinicius Costa Gomes offload->cycle_time = sched->cycle_time; 11899c66d156SVinicius Costa Gomes offload->cycle_time_extension = sched->cycle_time_extension; 11909c66d156SVinicius Costa Gomes 11919c66d156SVinicius Costa Gomes list_for_each_entry(entry, &sched->entries, list) { 11929c66d156SVinicius Costa Gomes struct tc_taprio_sched_entry *e = &offload->entries[i]; 11939c66d156SVinicius Costa Gomes 11949c66d156SVinicius Costa Gomes e->command = entry->command; 11959c66d156SVinicius Costa Gomes e->interval = entry->interval; 11969c66d156SVinicius Costa Gomes e->gate_mask = entry->gate_mask; 11979c66d156SVinicius Costa Gomes i++; 11989c66d156SVinicius Costa Gomes } 11999c66d156SVinicius Costa Gomes 12009c66d156SVinicius Costa Gomes offload->num_entries = i; 12019c66d156SVinicius Costa Gomes } 12029c66d156SVinicius Costa Gomes 12039c66d156SVinicius Costa Gomes static int taprio_enable_offload(struct net_device *dev, 12049c66d156SVinicius Costa Gomes struct tc_mqprio_qopt *mqprio, 12059c66d156SVinicius Costa Gomes struct taprio_sched *q, 12069c66d156SVinicius Costa Gomes struct sched_gate_list *sched, 12079c66d156SVinicius Costa Gomes struct netlink_ext_ack *extack) 12089c66d156SVinicius Costa Gomes { 12099c66d156SVinicius Costa Gomes const struct net_device_ops *ops = dev->netdev_ops; 12109c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *offload; 12119c66d156SVinicius Costa Gomes int err = 0; 12129c66d156SVinicius Costa Gomes 12139c66d156SVinicius Costa Gomes if (!ops->ndo_setup_tc) { 12149c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12159c66d156SVinicius Costa Gomes "Device does not support taprio offload"); 12169c66d156SVinicius Costa Gomes return -EOPNOTSUPP; 12179c66d156SVinicius Costa Gomes } 12189c66d156SVinicius Costa Gomes 12199c66d156SVinicius Costa Gomes offload = taprio_offload_alloc(sched->num_entries); 12209c66d156SVinicius Costa Gomes if (!offload) { 12219c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12229c66d156SVinicius Costa Gomes "Not enough memory for enabling offload mode"); 12239c66d156SVinicius Costa Gomes return -ENOMEM; 12249c66d156SVinicius Costa Gomes } 12259c66d156SVinicius Costa Gomes offload->enable = 1; 12269c66d156SVinicius Costa Gomes taprio_sched_to_offload(q, sched, mqprio, offload); 12279c66d156SVinicius Costa Gomes 12289c66d156SVinicius Costa Gomes err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 12299c66d156SVinicius Costa Gomes if (err < 0) { 12309c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12319c66d156SVinicius Costa Gomes "Device failed to setup taprio offload"); 12329c66d156SVinicius Costa Gomes goto done; 12339c66d156SVinicius Costa Gomes } 12349c66d156SVinicius Costa Gomes 12359c66d156SVinicius Costa Gomes done: 12369c66d156SVinicius Costa Gomes taprio_offload_free(offload); 12379c66d156SVinicius Costa Gomes 12389c66d156SVinicius Costa Gomes return err; 12399c66d156SVinicius Costa Gomes } 12409c66d156SVinicius Costa Gomes 12419c66d156SVinicius Costa Gomes static int taprio_disable_offload(struct net_device *dev, 12429c66d156SVinicius Costa Gomes struct taprio_sched *q, 12439c66d156SVinicius Costa Gomes struct netlink_ext_ack *extack) 12449c66d156SVinicius Costa Gomes { 12459c66d156SVinicius Costa Gomes const struct net_device_ops *ops = dev->netdev_ops; 12469c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *offload; 12479c66d156SVinicius Costa Gomes int err; 12489c66d156SVinicius Costa Gomes 12499c66d156SVinicius Costa Gomes if (!FULL_OFFLOAD_IS_ENABLED(q->flags)) 12509c66d156SVinicius Costa Gomes return 0; 12519c66d156SVinicius Costa Gomes 12529c66d156SVinicius Costa Gomes if (!ops->ndo_setup_tc) 12539c66d156SVinicius Costa Gomes return -EOPNOTSUPP; 12549c66d156SVinicius Costa Gomes 12559c66d156SVinicius Costa Gomes offload = taprio_offload_alloc(0); 12569c66d156SVinicius Costa Gomes if (!offload) { 12579c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12589c66d156SVinicius Costa Gomes "Not enough memory to disable offload mode"); 12599c66d156SVinicius Costa Gomes return -ENOMEM; 12609c66d156SVinicius Costa Gomes } 12619c66d156SVinicius Costa Gomes offload->enable = 0; 12629c66d156SVinicius Costa Gomes 12639c66d156SVinicius Costa Gomes err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 12649c66d156SVinicius Costa Gomes if (err < 0) { 12659c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12669c66d156SVinicius Costa Gomes "Device failed to disable offload"); 12679c66d156SVinicius Costa Gomes goto out; 12689c66d156SVinicius Costa Gomes } 12699c66d156SVinicius Costa Gomes 12709c66d156SVinicius Costa Gomes out: 12719c66d156SVinicius Costa Gomes taprio_offload_free(offload); 12729c66d156SVinicius Costa Gomes 12739c66d156SVinicius Costa Gomes return err; 12749c66d156SVinicius Costa Gomes } 12759c66d156SVinicius Costa Gomes 12769c66d156SVinicius Costa Gomes /* If full offload is enabled, the only possible clockid is the net device's 12779c66d156SVinicius Costa Gomes * PHC. For that reason, specifying a clockid through netlink is incorrect. 12789c66d156SVinicius Costa Gomes * For txtime-assist, it is implicitly assumed that the device's PHC is kept 12799c66d156SVinicius Costa Gomes * in sync with the specified clockid via a user space daemon such as phc2sys. 12809c66d156SVinicius Costa Gomes * For both software taprio and txtime-assist, the clockid is used for the 12819c66d156SVinicius Costa Gomes * hrtimer that advances the schedule and hence mandatory. 12829c66d156SVinicius Costa Gomes */ 12839c66d156SVinicius Costa Gomes static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb, 12849c66d156SVinicius Costa Gomes struct netlink_ext_ack *extack) 12859c66d156SVinicius Costa Gomes { 12869c66d156SVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 12879c66d156SVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 12889c66d156SVinicius Costa Gomes int err = -EINVAL; 12899c66d156SVinicius Costa Gomes 12909c66d156SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 12919c66d156SVinicius Costa Gomes const struct ethtool_ops *ops = dev->ethtool_ops; 12929c66d156SVinicius Costa Gomes struct ethtool_ts_info info = { 12939c66d156SVinicius Costa Gomes .cmd = ETHTOOL_GET_TS_INFO, 12949c66d156SVinicius Costa Gomes .phc_index = -1, 12959c66d156SVinicius Costa Gomes }; 12969c66d156SVinicius Costa Gomes 12979c66d156SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 12989c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 12999c66d156SVinicius Costa Gomes "The 'clockid' cannot be specified for full offload"); 13009c66d156SVinicius Costa Gomes goto out; 13019c66d156SVinicius Costa Gomes } 13029c66d156SVinicius Costa Gomes 13039c66d156SVinicius Costa Gomes if (ops && ops->get_ts_info) 13049c66d156SVinicius Costa Gomes err = ops->get_ts_info(dev, &info); 13059c66d156SVinicius Costa Gomes 13069c66d156SVinicius Costa Gomes if (err || info.phc_index < 0) { 13079c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 13089c66d156SVinicius Costa Gomes "Device does not have a PTP clock"); 13099c66d156SVinicius Costa Gomes err = -ENOTSUPP; 13109c66d156SVinicius Costa Gomes goto out; 13119c66d156SVinicius Costa Gomes } 13129c66d156SVinicius Costa Gomes } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 13139c66d156SVinicius Costa Gomes int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]); 13149c66d156SVinicius Costa Gomes 13159c66d156SVinicius Costa Gomes /* We only support static clockids and we don't allow 13169c66d156SVinicius Costa Gomes * for it to be modified after the first init. 13179c66d156SVinicius Costa Gomes */ 13189c66d156SVinicius Costa Gomes if (clockid < 0 || 13199c66d156SVinicius Costa Gomes (q->clockid != -1 && q->clockid != clockid)) { 13209c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 13219c66d156SVinicius Costa Gomes "Changing the 'clockid' of a running schedule is not supported"); 13229c66d156SVinicius Costa Gomes err = -ENOTSUPP; 13239c66d156SVinicius Costa Gomes goto out; 13249c66d156SVinicius Costa Gomes } 13259c66d156SVinicius Costa Gomes 13269c66d156SVinicius Costa Gomes switch (clockid) { 13279c66d156SVinicius Costa Gomes case CLOCK_REALTIME: 13289c66d156SVinicius Costa Gomes q->tk_offset = TK_OFFS_REAL; 13299c66d156SVinicius Costa Gomes break; 13309c66d156SVinicius Costa Gomes case CLOCK_MONOTONIC: 13319c66d156SVinicius Costa Gomes q->tk_offset = TK_OFFS_MAX; 13329c66d156SVinicius Costa Gomes break; 13339c66d156SVinicius Costa Gomes case CLOCK_BOOTTIME: 13349c66d156SVinicius Costa Gomes q->tk_offset = TK_OFFS_BOOT; 13359c66d156SVinicius Costa Gomes break; 13369c66d156SVinicius Costa Gomes case CLOCK_TAI: 13379c66d156SVinicius Costa Gomes q->tk_offset = TK_OFFS_TAI; 13389c66d156SVinicius Costa Gomes break; 13399c66d156SVinicius Costa Gomes default: 13409c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid 'clockid'"); 13419c66d156SVinicius Costa Gomes err = -EINVAL; 13429c66d156SVinicius Costa Gomes goto out; 13439c66d156SVinicius Costa Gomes } 13449c66d156SVinicius Costa Gomes 13459c66d156SVinicius Costa Gomes q->clockid = clockid; 13469c66d156SVinicius Costa Gomes } else { 13479c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory"); 13489c66d156SVinicius Costa Gomes goto out; 13499c66d156SVinicius Costa Gomes } 1350a954380aSVinicius Costa Gomes 1351a954380aSVinicius Costa Gomes /* Everything went ok, return success. */ 1352a954380aSVinicius Costa Gomes err = 0; 1353a954380aSVinicius Costa Gomes 13549c66d156SVinicius Costa Gomes out: 13559c66d156SVinicius Costa Gomes return err; 13569c66d156SVinicius Costa Gomes } 13579c66d156SVinicius Costa Gomes 1358b5a0faa3SIvan Khoronzhuk static int taprio_mqprio_cmp(const struct net_device *dev, 1359b5a0faa3SIvan Khoronzhuk const struct tc_mqprio_qopt *mqprio) 1360b5a0faa3SIvan Khoronzhuk { 1361b5a0faa3SIvan Khoronzhuk int i; 1362b5a0faa3SIvan Khoronzhuk 1363b5a0faa3SIvan Khoronzhuk if (!mqprio || mqprio->num_tc != dev->num_tc) 1364b5a0faa3SIvan Khoronzhuk return -1; 1365b5a0faa3SIvan Khoronzhuk 1366b5a0faa3SIvan Khoronzhuk for (i = 0; i < mqprio->num_tc; i++) 1367b5a0faa3SIvan Khoronzhuk if (dev->tc_to_txq[i].count != mqprio->count[i] || 1368b5a0faa3SIvan Khoronzhuk dev->tc_to_txq[i].offset != mqprio->offset[i]) 1369b5a0faa3SIvan Khoronzhuk return -1; 1370b5a0faa3SIvan Khoronzhuk 1371b5a0faa3SIvan Khoronzhuk for (i = 0; i <= TC_BITMASK; i++) 1372b5a0faa3SIvan Khoronzhuk if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i]) 1373b5a0faa3SIvan Khoronzhuk return -1; 1374b5a0faa3SIvan Khoronzhuk 1375b5a0faa3SIvan Khoronzhuk return 0; 1376b5a0faa3SIvan Khoronzhuk } 1377b5a0faa3SIvan Khoronzhuk 1378a9d62274SVinicius Costa Gomes /* The semantics of the 'flags' argument in relation to 'change()' 1379a9d62274SVinicius Costa Gomes * requests, are interpreted following two rules (which are applied in 1380a9d62274SVinicius Costa Gomes * this order): (1) an omitted 'flags' argument is interpreted as 1381a9d62274SVinicius Costa Gomes * zero; (2) the 'flags' of a "running" taprio instance cannot be 1382a9d62274SVinicius Costa Gomes * changed. 1383a9d62274SVinicius Costa Gomes */ 1384a9d62274SVinicius Costa Gomes static int taprio_new_flags(const struct nlattr *attr, u32 old, 1385a9d62274SVinicius Costa Gomes struct netlink_ext_ack *extack) 1386a9d62274SVinicius Costa Gomes { 1387a9d62274SVinicius Costa Gomes u32 new = 0; 1388a9d62274SVinicius Costa Gomes 1389a9d62274SVinicius Costa Gomes if (attr) 1390a9d62274SVinicius Costa Gomes new = nla_get_u32(attr); 1391a9d62274SVinicius Costa Gomes 1392a9d62274SVinicius Costa Gomes if (old != TAPRIO_FLAGS_INVALID && old != new) { 1393a9d62274SVinicius Costa Gomes NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported"); 1394a9d62274SVinicius Costa Gomes return -EOPNOTSUPP; 1395a9d62274SVinicius Costa Gomes } 1396a9d62274SVinicius Costa Gomes 1397a9d62274SVinicius Costa Gomes if (!taprio_flags_valid(new)) { 1398a9d62274SVinicius Costa Gomes NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid"); 1399a9d62274SVinicius Costa Gomes return -EINVAL; 1400a9d62274SVinicius Costa Gomes } 1401a9d62274SVinicius Costa Gomes 1402a9d62274SVinicius Costa Gomes return new; 1403a9d62274SVinicius Costa Gomes } 1404a9d62274SVinicius Costa Gomes 14055a781ccbSVinicius Costa Gomes static int taprio_change(struct Qdisc *sch, struct nlattr *opt, 14065a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 14075a781ccbSVinicius Costa Gomes { 14085a781ccbSVinicius Costa Gomes struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { }; 1409a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin, *new_admin; 14105a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 14115a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 14125a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt *mqprio = NULL; 1413a3d43c0dSVinicius Costa Gomes unsigned long flags; 14145a781ccbSVinicius Costa Gomes ktime_t start; 14159c66d156SVinicius Costa Gomes int i, err; 14165a781ccbSVinicius Costa Gomes 14178cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt, 14185a781ccbSVinicius Costa Gomes taprio_policy, extack); 14195a781ccbSVinicius Costa Gomes if (err < 0) 14205a781ccbSVinicius Costa Gomes return err; 14215a781ccbSVinicius Costa Gomes 14225a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_PRIOMAP]) 14235a781ccbSVinicius Costa Gomes mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]); 14245a781ccbSVinicius Costa Gomes 1425a9d62274SVinicius Costa Gomes err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS], 1426a9d62274SVinicius Costa Gomes q->flags, extack); 1427a9d62274SVinicius Costa Gomes if (err < 0) 1428a9d62274SVinicius Costa Gomes return err; 14294cfd5779SVedang Patel 1430a9d62274SVinicius Costa Gomes q->flags = err; 14314cfd5779SVedang Patel 1432a9d62274SVinicius Costa Gomes err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags); 14335a781ccbSVinicius Costa Gomes if (err < 0) 14345a781ccbSVinicius Costa Gomes return err; 14355a781ccbSVinicius Costa Gomes 1436a3d43c0dSVinicius Costa Gomes new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL); 1437a3d43c0dSVinicius Costa Gomes if (!new_admin) { 1438a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule"); 1439a3d43c0dSVinicius Costa Gomes return -ENOMEM; 1440a3d43c0dSVinicius Costa Gomes } 1441a3d43c0dSVinicius Costa Gomes INIT_LIST_HEAD(&new_admin->entries); 14425a781ccbSVinicius Costa Gomes 1443a3d43c0dSVinicius Costa Gomes rcu_read_lock(); 1444a3d43c0dSVinicius Costa Gomes oper = rcu_dereference(q->oper_sched); 1445a3d43c0dSVinicius Costa Gomes admin = rcu_dereference(q->admin_sched); 1446a3d43c0dSVinicius Costa Gomes rcu_read_unlock(); 14475a781ccbSVinicius Costa Gomes 1448b5a0faa3SIvan Khoronzhuk /* no changes - no new mqprio settings */ 1449b5a0faa3SIvan Khoronzhuk if (!taprio_mqprio_cmp(dev, mqprio)) 1450b5a0faa3SIvan Khoronzhuk mqprio = NULL; 1451b5a0faa3SIvan Khoronzhuk 1452a3d43c0dSVinicius Costa Gomes if (mqprio && (oper || admin)) { 1453a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported"); 1454a3d43c0dSVinicius Costa Gomes err = -ENOTSUPP; 1455a3d43c0dSVinicius Costa Gomes goto free_sched; 14565a781ccbSVinicius Costa Gomes } 14575a781ccbSVinicius Costa Gomes 1458a3d43c0dSVinicius Costa Gomes err = parse_taprio_schedule(tb, new_admin, extack); 1459a3d43c0dSVinicius Costa Gomes if (err < 0) 1460a3d43c0dSVinicius Costa Gomes goto free_sched; 14615a781ccbSVinicius Costa Gomes 1462a3d43c0dSVinicius Costa Gomes if (new_admin->num_entries == 0) { 1463a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule"); 1464a3d43c0dSVinicius Costa Gomes err = -EINVAL; 1465a3d43c0dSVinicius Costa Gomes goto free_sched; 1466a3d43c0dSVinicius Costa Gomes } 14675a781ccbSVinicius Costa Gomes 14689c66d156SVinicius Costa Gomes err = taprio_parse_clockid(sch, tb, extack); 14699c66d156SVinicius Costa Gomes if (err < 0) 1470a3d43c0dSVinicius Costa Gomes goto free_sched; 1471a3d43c0dSVinicius Costa Gomes 1472a3d43c0dSVinicius Costa Gomes taprio_set_picos_per_byte(dev, q); 1473a3d43c0dSVinicius Costa Gomes 14745652e63dSVinicius Costa Gomes if (mqprio) { 14755652e63dSVinicius Costa Gomes netdev_set_num_tc(dev, mqprio->num_tc); 14765652e63dSVinicius Costa Gomes for (i = 0; i < mqprio->num_tc; i++) 14775652e63dSVinicius Costa Gomes netdev_set_tc_queue(dev, i, 14785652e63dSVinicius Costa Gomes mqprio->count[i], 14795652e63dSVinicius Costa Gomes mqprio->offset[i]); 14805652e63dSVinicius Costa Gomes 14815652e63dSVinicius Costa Gomes /* Always use supplied priority mappings */ 14825652e63dSVinicius Costa Gomes for (i = 0; i <= TC_BITMASK; i++) 14835652e63dSVinicius Costa Gomes netdev_set_prio_tc_map(dev, i, 14845652e63dSVinicius Costa Gomes mqprio->prio_tc_map[i]); 14855652e63dSVinicius Costa Gomes } 14865652e63dSVinicius Costa Gomes 1487a9d62274SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 14889c66d156SVinicius Costa Gomes err = taprio_enable_offload(dev, mqprio, q, new_admin, extack); 14899c66d156SVinicius Costa Gomes else 14909c66d156SVinicius Costa Gomes err = taprio_disable_offload(dev, q, extack); 14919c66d156SVinicius Costa Gomes if (err) 14929c66d156SVinicius Costa Gomes goto free_sched; 14939c66d156SVinicius Costa Gomes 1494a3d43c0dSVinicius Costa Gomes /* Protects against enqueue()/dequeue() */ 1495a3d43c0dSVinicius Costa Gomes spin_lock_bh(qdisc_lock(sch)); 1496a3d43c0dSVinicius Costa Gomes 14974cfd5779SVedang Patel if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) { 14984cfd5779SVedang Patel if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) { 14994cfd5779SVedang Patel NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled"); 15004cfd5779SVedang Patel err = -EINVAL; 15014cfd5779SVedang Patel goto unlock; 15024cfd5779SVedang Patel } 15034cfd5779SVedang Patel 1504a5b64700SVedang Patel q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]); 15054cfd5779SVedang Patel } 15064cfd5779SVedang Patel 1507a9d62274SVinicius Costa Gomes if (!TXTIME_ASSIST_IS_ENABLED(q->flags) && 1508a9d62274SVinicius Costa Gomes !FULL_OFFLOAD_IS_ENABLED(q->flags) && 15094cfd5779SVedang Patel !hrtimer_active(&q->advance_timer)) { 1510a3d43c0dSVinicius Costa Gomes hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS); 1511a3d43c0dSVinicius Costa Gomes q->advance_timer.function = advance_sched; 15125a781ccbSVinicius Costa Gomes } 15135a781ccbSVinicius Costa Gomes 1514a9d62274SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 15159c66d156SVinicius Costa Gomes q->dequeue = taprio_dequeue_offload; 15169c66d156SVinicius Costa Gomes q->peek = taprio_peek_offload; 15179c66d156SVinicius Costa Gomes } else { 15189c66d156SVinicius Costa Gomes /* Be sure to always keep the function pointers 15199c66d156SVinicius Costa Gomes * in a consistent state. 15209c66d156SVinicius Costa Gomes */ 15219c66d156SVinicius Costa Gomes q->dequeue = taprio_dequeue_soft; 15229c66d156SVinicius Costa Gomes q->peek = taprio_peek_soft; 15238599099fSAndre Guedes } 15245a781ccbSVinicius Costa Gomes 1525a3d43c0dSVinicius Costa Gomes err = taprio_get_start_time(sch, new_admin, &start); 1526a3d43c0dSVinicius Costa Gomes if (err < 0) { 1527a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Internal error: failed get start time"); 1528a3d43c0dSVinicius Costa Gomes goto unlock; 1529a3d43c0dSVinicius Costa Gomes } 15305a781ccbSVinicius Costa Gomes 15314cfd5779SVedang Patel setup_txtime(q, new_admin, start); 15324cfd5779SVedang Patel 1533bfabd41dSVinicius Costa Gomes if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 15344cfd5779SVedang Patel if (!oper) { 15354cfd5779SVedang Patel rcu_assign_pointer(q->oper_sched, new_admin); 15364cfd5779SVedang Patel err = 0; 15374cfd5779SVedang Patel new_admin = NULL; 15384cfd5779SVedang Patel goto unlock; 15394cfd5779SVedang Patel } 15404cfd5779SVedang Patel 15414cfd5779SVedang Patel rcu_assign_pointer(q->admin_sched, new_admin); 15424cfd5779SVedang Patel if (admin) 15434cfd5779SVedang Patel call_rcu(&admin->rcu, taprio_free_sched_cb); 15444cfd5779SVedang Patel } else { 1545a3d43c0dSVinicius Costa Gomes setup_first_close_time(q, new_admin, start); 1546a3d43c0dSVinicius Costa Gomes 1547a3d43c0dSVinicius Costa Gomes /* Protects against advance_sched() */ 1548a3d43c0dSVinicius Costa Gomes spin_lock_irqsave(&q->current_entry_lock, flags); 1549a3d43c0dSVinicius Costa Gomes 1550a3d43c0dSVinicius Costa Gomes taprio_start_sched(sch, start, new_admin); 1551a3d43c0dSVinicius Costa Gomes 1552a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->admin_sched, new_admin); 1553a3d43c0dSVinicius Costa Gomes if (admin) 1554a3d43c0dSVinicius Costa Gomes call_rcu(&admin->rcu, taprio_free_sched_cb); 1555a3d43c0dSVinicius Costa Gomes 1556a3d43c0dSVinicius Costa Gomes spin_unlock_irqrestore(&q->current_entry_lock, flags); 15570763b3e8SIvan Khoronzhuk 1558a9d62274SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 15590763b3e8SIvan Khoronzhuk taprio_offload_config_changed(q); 15604cfd5779SVedang Patel } 1561a3d43c0dSVinicius Costa Gomes 15624cfd5779SVedang Patel new_admin = NULL; 1563a3d43c0dSVinicius Costa Gomes err = 0; 1564a3d43c0dSVinicius Costa Gomes 1565a3d43c0dSVinicius Costa Gomes unlock: 1566a3d43c0dSVinicius Costa Gomes spin_unlock_bh(qdisc_lock(sch)); 1567a3d43c0dSVinicius Costa Gomes 1568a3d43c0dSVinicius Costa Gomes free_sched: 156951650d33SIvan Khoronzhuk if (new_admin) 157051650d33SIvan Khoronzhuk call_rcu(&new_admin->rcu, taprio_free_sched_cb); 1571a3d43c0dSVinicius Costa Gomes 1572a3d43c0dSVinicius Costa Gomes return err; 15735a781ccbSVinicius Costa Gomes } 15745a781ccbSVinicius Costa Gomes 15755a781ccbSVinicius Costa Gomes static void taprio_destroy(struct Qdisc *sch) 15765a781ccbSVinicius Costa Gomes { 15775a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 15785a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 15795a781ccbSVinicius Costa Gomes unsigned int i; 15805a781ccbSVinicius Costa Gomes 15817b9eba7bSLeandro Dorileo spin_lock(&taprio_list_lock); 15827b9eba7bSLeandro Dorileo list_del(&q->taprio_list); 15837b9eba7bSLeandro Dorileo spin_unlock(&taprio_list_lock); 15847b9eba7bSLeandro Dorileo 15855a781ccbSVinicius Costa Gomes hrtimer_cancel(&q->advance_timer); 15865a781ccbSVinicius Costa Gomes 15879c66d156SVinicius Costa Gomes taprio_disable_offload(dev, q, NULL); 15889c66d156SVinicius Costa Gomes 15895a781ccbSVinicius Costa Gomes if (q->qdiscs) { 15905a781ccbSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues && q->qdiscs[i]; i++) 15915a781ccbSVinicius Costa Gomes qdisc_put(q->qdiscs[i]); 15925a781ccbSVinicius Costa Gomes 15935a781ccbSVinicius Costa Gomes kfree(q->qdiscs); 15945a781ccbSVinicius Costa Gomes } 15955a781ccbSVinicius Costa Gomes q->qdiscs = NULL; 15965a781ccbSVinicius Costa Gomes 15977c16680aSVinicius Costa Gomes netdev_reset_tc(dev); 15985a781ccbSVinicius Costa Gomes 1599a3d43c0dSVinicius Costa Gomes if (q->oper_sched) 1600a3d43c0dSVinicius Costa Gomes call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb); 1601a3d43c0dSVinicius Costa Gomes 1602a3d43c0dSVinicius Costa Gomes if (q->admin_sched) 1603a3d43c0dSVinicius Costa Gomes call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb); 16045a781ccbSVinicius Costa Gomes } 16055a781ccbSVinicius Costa Gomes 16065a781ccbSVinicius Costa Gomes static int taprio_init(struct Qdisc *sch, struct nlattr *opt, 16075a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 16085a781ccbSVinicius Costa Gomes { 16095a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 16105a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 1611a3d43c0dSVinicius Costa Gomes int i; 16125a781ccbSVinicius Costa Gomes 16135a781ccbSVinicius Costa Gomes spin_lock_init(&q->current_entry_lock); 16145a781ccbSVinicius Costa Gomes 16155a781ccbSVinicius Costa Gomes hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS); 1616a3d43c0dSVinicius Costa Gomes q->advance_timer.function = advance_sched; 16175a781ccbSVinicius Costa Gomes 16189c66d156SVinicius Costa Gomes q->dequeue = taprio_dequeue_soft; 16199c66d156SVinicius Costa Gomes q->peek = taprio_peek_soft; 16209c66d156SVinicius Costa Gomes 16215a781ccbSVinicius Costa Gomes q->root = sch; 16225a781ccbSVinicius Costa Gomes 16235a781ccbSVinicius Costa Gomes /* We only support static clockids. Use an invalid value as default 16245a781ccbSVinicius Costa Gomes * and get the valid one on taprio_change(). 16255a781ccbSVinicius Costa Gomes */ 16265a781ccbSVinicius Costa Gomes q->clockid = -1; 1627a9d62274SVinicius Costa Gomes q->flags = TAPRIO_FLAGS_INVALID; 16285a781ccbSVinicius Costa Gomes 1629efb55222SVladimir Oltean spin_lock(&taprio_list_lock); 1630efb55222SVladimir Oltean list_add(&q->taprio_list, &taprio_list); 1631efb55222SVladimir Oltean spin_unlock(&taprio_list_lock); 1632efb55222SVladimir Oltean 16335a781ccbSVinicius Costa Gomes if (sch->parent != TC_H_ROOT) 16345a781ccbSVinicius Costa Gomes return -EOPNOTSUPP; 16355a781ccbSVinicius Costa Gomes 16365a781ccbSVinicius Costa Gomes if (!netif_is_multiqueue(dev)) 16375a781ccbSVinicius Costa Gomes return -EOPNOTSUPP; 16385a781ccbSVinicius Costa Gomes 16395a781ccbSVinicius Costa Gomes /* pre-allocate qdisc, attachment can't fail */ 16405a781ccbSVinicius Costa Gomes q->qdiscs = kcalloc(dev->num_tx_queues, 16415a781ccbSVinicius Costa Gomes sizeof(q->qdiscs[0]), 16425a781ccbSVinicius Costa Gomes GFP_KERNEL); 16435a781ccbSVinicius Costa Gomes 16445a781ccbSVinicius Costa Gomes if (!q->qdiscs) 16455a781ccbSVinicius Costa Gomes return -ENOMEM; 16465a781ccbSVinicius Costa Gomes 16475a781ccbSVinicius Costa Gomes if (!opt) 16485a781ccbSVinicius Costa Gomes return -EINVAL; 16495a781ccbSVinicius Costa Gomes 1650a3d43c0dSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 1651a3d43c0dSVinicius Costa Gomes struct netdev_queue *dev_queue; 1652a3d43c0dSVinicius Costa Gomes struct Qdisc *qdisc; 1653a3d43c0dSVinicius Costa Gomes 1654a3d43c0dSVinicius Costa Gomes dev_queue = netdev_get_tx_queue(dev, i); 1655a3d43c0dSVinicius Costa Gomes qdisc = qdisc_create_dflt(dev_queue, 1656a3d43c0dSVinicius Costa Gomes &pfifo_qdisc_ops, 1657a3d43c0dSVinicius Costa Gomes TC_H_MAKE(TC_H_MAJ(sch->handle), 1658a3d43c0dSVinicius Costa Gomes TC_H_MIN(i + 1)), 1659a3d43c0dSVinicius Costa Gomes extack); 1660a3d43c0dSVinicius Costa Gomes if (!qdisc) 1661a3d43c0dSVinicius Costa Gomes return -ENOMEM; 1662a3d43c0dSVinicius Costa Gomes 1663a3d43c0dSVinicius Costa Gomes if (i < dev->real_num_tx_queues) 1664a3d43c0dSVinicius Costa Gomes qdisc_hash_add(qdisc, false); 1665a3d43c0dSVinicius Costa Gomes 1666a3d43c0dSVinicius Costa Gomes q->qdiscs[i] = qdisc; 1667a3d43c0dSVinicius Costa Gomes } 1668a3d43c0dSVinicius Costa Gomes 16695a781ccbSVinicius Costa Gomes return taprio_change(sch, opt, extack); 16705a781ccbSVinicius Costa Gomes } 16715a781ccbSVinicius Costa Gomes 16725a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_queue_get(struct Qdisc *sch, 16735a781ccbSVinicius Costa Gomes unsigned long cl) 16745a781ccbSVinicius Costa Gomes { 16755a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 16765a781ccbSVinicius Costa Gomes unsigned long ntx = cl - 1; 16775a781ccbSVinicius Costa Gomes 16785a781ccbSVinicius Costa Gomes if (ntx >= dev->num_tx_queues) 16795a781ccbSVinicius Costa Gomes return NULL; 16805a781ccbSVinicius Costa Gomes 16815a781ccbSVinicius Costa Gomes return netdev_get_tx_queue(dev, ntx); 16825a781ccbSVinicius Costa Gomes } 16835a781ccbSVinicius Costa Gomes 16845a781ccbSVinicius Costa Gomes static int taprio_graft(struct Qdisc *sch, unsigned long cl, 16855a781ccbSVinicius Costa Gomes struct Qdisc *new, struct Qdisc **old, 16865a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 16875a781ccbSVinicius Costa Gomes { 16885a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 16895a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 16905a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 16915a781ccbSVinicius Costa Gomes 16925a781ccbSVinicius Costa Gomes if (!dev_queue) 16935a781ccbSVinicius Costa Gomes return -EINVAL; 16945a781ccbSVinicius Costa Gomes 16955a781ccbSVinicius Costa Gomes if (dev->flags & IFF_UP) 16965a781ccbSVinicius Costa Gomes dev_deactivate(dev); 16975a781ccbSVinicius Costa Gomes 16985a781ccbSVinicius Costa Gomes *old = q->qdiscs[cl - 1]; 16995a781ccbSVinicius Costa Gomes q->qdiscs[cl - 1] = new; 17005a781ccbSVinicius Costa Gomes 17015a781ccbSVinicius Costa Gomes if (new) 17025a781ccbSVinicius Costa Gomes new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 17035a781ccbSVinicius Costa Gomes 17045a781ccbSVinicius Costa Gomes if (dev->flags & IFF_UP) 17055a781ccbSVinicius Costa Gomes dev_activate(dev); 17065a781ccbSVinicius Costa Gomes 17075a781ccbSVinicius Costa Gomes return 0; 17085a781ccbSVinicius Costa Gomes } 17095a781ccbSVinicius Costa Gomes 17105a781ccbSVinicius Costa Gomes static int dump_entry(struct sk_buff *msg, 17115a781ccbSVinicius Costa Gomes const struct sched_entry *entry) 17125a781ccbSVinicius Costa Gomes { 17135a781ccbSVinicius Costa Gomes struct nlattr *item; 17145a781ccbSVinicius Costa Gomes 1715ae0be8deSMichal Kubecek item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY); 17165a781ccbSVinicius Costa Gomes if (!item) 17175a781ccbSVinicius Costa Gomes return -ENOSPC; 17185a781ccbSVinicius Costa Gomes 17195a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index)) 17205a781ccbSVinicius Costa Gomes goto nla_put_failure; 17215a781ccbSVinicius Costa Gomes 17225a781ccbSVinicius Costa Gomes if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command)) 17235a781ccbSVinicius Costa Gomes goto nla_put_failure; 17245a781ccbSVinicius Costa Gomes 17255a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK, 17265a781ccbSVinicius Costa Gomes entry->gate_mask)) 17275a781ccbSVinicius Costa Gomes goto nla_put_failure; 17285a781ccbSVinicius Costa Gomes 17295a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL, 17305a781ccbSVinicius Costa Gomes entry->interval)) 17315a781ccbSVinicius Costa Gomes goto nla_put_failure; 17325a781ccbSVinicius Costa Gomes 17335a781ccbSVinicius Costa Gomes return nla_nest_end(msg, item); 17345a781ccbSVinicius Costa Gomes 17355a781ccbSVinicius Costa Gomes nla_put_failure: 17365a781ccbSVinicius Costa Gomes nla_nest_cancel(msg, item); 17375a781ccbSVinicius Costa Gomes return -1; 17385a781ccbSVinicius Costa Gomes } 17395a781ccbSVinicius Costa Gomes 1740a3d43c0dSVinicius Costa Gomes static int dump_schedule(struct sk_buff *msg, 1741a3d43c0dSVinicius Costa Gomes const struct sched_gate_list *root) 1742a3d43c0dSVinicius Costa Gomes { 1743a3d43c0dSVinicius Costa Gomes struct nlattr *entry_list; 1744a3d43c0dSVinicius Costa Gomes struct sched_entry *entry; 1745a3d43c0dSVinicius Costa Gomes 1746a3d43c0dSVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME, 1747a3d43c0dSVinicius Costa Gomes root->base_time, TCA_TAPRIO_PAD)) 1748a3d43c0dSVinicius Costa Gomes return -1; 1749a3d43c0dSVinicius Costa Gomes 17506ca6a665SVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME, 17516ca6a665SVinicius Costa Gomes root->cycle_time, TCA_TAPRIO_PAD)) 17526ca6a665SVinicius Costa Gomes return -1; 17536ca6a665SVinicius Costa Gomes 1754c25031e9SVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION, 1755c25031e9SVinicius Costa Gomes root->cycle_time_extension, TCA_TAPRIO_PAD)) 1756c25031e9SVinicius Costa Gomes return -1; 1757c25031e9SVinicius Costa Gomes 1758a3d43c0dSVinicius Costa Gomes entry_list = nla_nest_start_noflag(msg, 1759a3d43c0dSVinicius Costa Gomes TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST); 1760a3d43c0dSVinicius Costa Gomes if (!entry_list) 1761a3d43c0dSVinicius Costa Gomes goto error_nest; 1762a3d43c0dSVinicius Costa Gomes 1763a3d43c0dSVinicius Costa Gomes list_for_each_entry(entry, &root->entries, list) { 1764a3d43c0dSVinicius Costa Gomes if (dump_entry(msg, entry) < 0) 1765a3d43c0dSVinicius Costa Gomes goto error_nest; 1766a3d43c0dSVinicius Costa Gomes } 1767a3d43c0dSVinicius Costa Gomes 1768a3d43c0dSVinicius Costa Gomes nla_nest_end(msg, entry_list); 1769a3d43c0dSVinicius Costa Gomes return 0; 1770a3d43c0dSVinicius Costa Gomes 1771a3d43c0dSVinicius Costa Gomes error_nest: 1772a3d43c0dSVinicius Costa Gomes nla_nest_cancel(msg, entry_list); 1773a3d43c0dSVinicius Costa Gomes return -1; 1774a3d43c0dSVinicius Costa Gomes } 1775a3d43c0dSVinicius Costa Gomes 17765a781ccbSVinicius Costa Gomes static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb) 17775a781ccbSVinicius Costa Gomes { 17785a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 17795a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 1780a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin; 17815a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt opt = { 0 }; 1782a3d43c0dSVinicius Costa Gomes struct nlattr *nest, *sched_nest; 17835a781ccbSVinicius Costa Gomes unsigned int i; 17845a781ccbSVinicius Costa Gomes 1785a3d43c0dSVinicius Costa Gomes rcu_read_lock(); 1786a3d43c0dSVinicius Costa Gomes oper = rcu_dereference(q->oper_sched); 1787a3d43c0dSVinicius Costa Gomes admin = rcu_dereference(q->admin_sched); 1788a3d43c0dSVinicius Costa Gomes 17895a781ccbSVinicius Costa Gomes opt.num_tc = netdev_get_num_tc(dev); 17905a781ccbSVinicius Costa Gomes memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map)); 17915a781ccbSVinicius Costa Gomes 17925a781ccbSVinicius Costa Gomes for (i = 0; i < netdev_get_num_tc(dev); i++) { 17935a781ccbSVinicius Costa Gomes opt.count[i] = dev->tc_to_txq[i].count; 17945a781ccbSVinicius Costa Gomes opt.offset[i] = dev->tc_to_txq[i].offset; 17955a781ccbSVinicius Costa Gomes } 17965a781ccbSVinicius Costa Gomes 1797ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 17985a781ccbSVinicius Costa Gomes if (!nest) 1799a3d43c0dSVinicius Costa Gomes goto start_error; 18005a781ccbSVinicius Costa Gomes 18015a781ccbSVinicius Costa Gomes if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt)) 18025a781ccbSVinicius Costa Gomes goto options_error; 18035a781ccbSVinicius Costa Gomes 18049c66d156SVinicius Costa Gomes if (!FULL_OFFLOAD_IS_ENABLED(q->flags) && 18059c66d156SVinicius Costa Gomes nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid)) 18065a781ccbSVinicius Costa Gomes goto options_error; 18075a781ccbSVinicius Costa Gomes 18084cfd5779SVedang Patel if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags)) 18094cfd5779SVedang Patel goto options_error; 18104cfd5779SVedang Patel 18114cfd5779SVedang Patel if (q->txtime_delay && 1812a5b64700SVedang Patel nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay)) 18134cfd5779SVedang Patel goto options_error; 18144cfd5779SVedang Patel 1815a3d43c0dSVinicius Costa Gomes if (oper && dump_schedule(skb, oper)) 18165a781ccbSVinicius Costa Gomes goto options_error; 18175a781ccbSVinicius Costa Gomes 1818a3d43c0dSVinicius Costa Gomes if (!admin) 1819a3d43c0dSVinicius Costa Gomes goto done; 18205a781ccbSVinicius Costa Gomes 1821a3d43c0dSVinicius Costa Gomes sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED); 1822e4acf427SColin Ian King if (!sched_nest) 1823e4acf427SColin Ian King goto options_error; 1824a3d43c0dSVinicius Costa Gomes 1825a3d43c0dSVinicius Costa Gomes if (dump_schedule(skb, admin)) 1826a3d43c0dSVinicius Costa Gomes goto admin_error; 1827a3d43c0dSVinicius Costa Gomes 1828a3d43c0dSVinicius Costa Gomes nla_nest_end(skb, sched_nest); 1829a3d43c0dSVinicius Costa Gomes 1830a3d43c0dSVinicius Costa Gomes done: 1831a3d43c0dSVinicius Costa Gomes rcu_read_unlock(); 18325a781ccbSVinicius Costa Gomes 18335a781ccbSVinicius Costa Gomes return nla_nest_end(skb, nest); 18345a781ccbSVinicius Costa Gomes 1835a3d43c0dSVinicius Costa Gomes admin_error: 1836a3d43c0dSVinicius Costa Gomes nla_nest_cancel(skb, sched_nest); 1837a3d43c0dSVinicius Costa Gomes 18385a781ccbSVinicius Costa Gomes options_error: 18395a781ccbSVinicius Costa Gomes nla_nest_cancel(skb, nest); 1840a3d43c0dSVinicius Costa Gomes 1841a3d43c0dSVinicius Costa Gomes start_error: 1842a3d43c0dSVinicius Costa Gomes rcu_read_unlock(); 1843a3d43c0dSVinicius Costa Gomes return -ENOSPC; 18445a781ccbSVinicius Costa Gomes } 18455a781ccbSVinicius Costa Gomes 18465a781ccbSVinicius Costa Gomes static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl) 18475a781ccbSVinicius Costa Gomes { 18485a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 18495a781ccbSVinicius Costa Gomes 18505a781ccbSVinicius Costa Gomes if (!dev_queue) 18515a781ccbSVinicius Costa Gomes return NULL; 18525a781ccbSVinicius Costa Gomes 18535a781ccbSVinicius Costa Gomes return dev_queue->qdisc_sleeping; 18545a781ccbSVinicius Costa Gomes } 18555a781ccbSVinicius Costa Gomes 18565a781ccbSVinicius Costa Gomes static unsigned long taprio_find(struct Qdisc *sch, u32 classid) 18575a781ccbSVinicius Costa Gomes { 18585a781ccbSVinicius Costa Gomes unsigned int ntx = TC_H_MIN(classid); 18595a781ccbSVinicius Costa Gomes 18605a781ccbSVinicius Costa Gomes if (!taprio_queue_get(sch, ntx)) 18615a781ccbSVinicius Costa Gomes return 0; 18625a781ccbSVinicius Costa Gomes return ntx; 18635a781ccbSVinicius Costa Gomes } 18645a781ccbSVinicius Costa Gomes 18655a781ccbSVinicius Costa Gomes static int taprio_dump_class(struct Qdisc *sch, unsigned long cl, 18665a781ccbSVinicius Costa Gomes struct sk_buff *skb, struct tcmsg *tcm) 18675a781ccbSVinicius Costa Gomes { 18685a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 18695a781ccbSVinicius Costa Gomes 18705a781ccbSVinicius Costa Gomes tcm->tcm_parent = TC_H_ROOT; 18715a781ccbSVinicius Costa Gomes tcm->tcm_handle |= TC_H_MIN(cl); 18725a781ccbSVinicius Costa Gomes tcm->tcm_info = dev_queue->qdisc_sleeping->handle; 18735a781ccbSVinicius Costa Gomes 18745a781ccbSVinicius Costa Gomes return 0; 18755a781ccbSVinicius Costa Gomes } 18765a781ccbSVinicius Costa Gomes 18775a781ccbSVinicius Costa Gomes static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 18785a781ccbSVinicius Costa Gomes struct gnet_dump *d) 18795a781ccbSVinicius Costa Gomes __releases(d->lock) 18805a781ccbSVinicius Costa Gomes __acquires(d->lock) 18815a781ccbSVinicius Costa Gomes { 18825a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 18835a781ccbSVinicius Costa Gomes 18845a781ccbSVinicius Costa Gomes sch = dev_queue->qdisc_sleeping; 18855a781ccbSVinicius Costa Gomes if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 || 18865dd431b6SPaolo Abeni qdisc_qstats_copy(d, sch) < 0) 18875a781ccbSVinicius Costa Gomes return -1; 18885a781ccbSVinicius Costa Gomes return 0; 18895a781ccbSVinicius Costa Gomes } 18905a781ccbSVinicius Costa Gomes 18915a781ccbSVinicius Costa Gomes static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 18925a781ccbSVinicius Costa Gomes { 18935a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 18945a781ccbSVinicius Costa Gomes unsigned long ntx; 18955a781ccbSVinicius Costa Gomes 18965a781ccbSVinicius Costa Gomes if (arg->stop) 18975a781ccbSVinicius Costa Gomes return; 18985a781ccbSVinicius Costa Gomes 18995a781ccbSVinicius Costa Gomes arg->count = arg->skip; 19005a781ccbSVinicius Costa Gomes for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) { 19015a781ccbSVinicius Costa Gomes if (arg->fn(sch, ntx + 1, arg) < 0) { 19025a781ccbSVinicius Costa Gomes arg->stop = 1; 19035a781ccbSVinicius Costa Gomes break; 19045a781ccbSVinicius Costa Gomes } 19055a781ccbSVinicius Costa Gomes arg->count++; 19065a781ccbSVinicius Costa Gomes } 19075a781ccbSVinicius Costa Gomes } 19085a781ccbSVinicius Costa Gomes 19095a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_select_queue(struct Qdisc *sch, 19105a781ccbSVinicius Costa Gomes struct tcmsg *tcm) 19115a781ccbSVinicius Costa Gomes { 19125a781ccbSVinicius Costa Gomes return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); 19135a781ccbSVinicius Costa Gomes } 19145a781ccbSVinicius Costa Gomes 19155a781ccbSVinicius Costa Gomes static const struct Qdisc_class_ops taprio_class_ops = { 19165a781ccbSVinicius Costa Gomes .graft = taprio_graft, 19175a781ccbSVinicius Costa Gomes .leaf = taprio_leaf, 19185a781ccbSVinicius Costa Gomes .find = taprio_find, 19195a781ccbSVinicius Costa Gomes .walk = taprio_walk, 19205a781ccbSVinicius Costa Gomes .dump = taprio_dump_class, 19215a781ccbSVinicius Costa Gomes .dump_stats = taprio_dump_class_stats, 19225a781ccbSVinicius Costa Gomes .select_queue = taprio_select_queue, 19235a781ccbSVinicius Costa Gomes }; 19245a781ccbSVinicius Costa Gomes 19255a781ccbSVinicius Costa Gomes static struct Qdisc_ops taprio_qdisc_ops __read_mostly = { 19265a781ccbSVinicius Costa Gomes .cl_ops = &taprio_class_ops, 19275a781ccbSVinicius Costa Gomes .id = "taprio", 19285a781ccbSVinicius Costa Gomes .priv_size = sizeof(struct taprio_sched), 19295a781ccbSVinicius Costa Gomes .init = taprio_init, 1930a3d43c0dSVinicius Costa Gomes .change = taprio_change, 19315a781ccbSVinicius Costa Gomes .destroy = taprio_destroy, 19325a781ccbSVinicius Costa Gomes .peek = taprio_peek, 19335a781ccbSVinicius Costa Gomes .dequeue = taprio_dequeue, 19345a781ccbSVinicius Costa Gomes .enqueue = taprio_enqueue, 19355a781ccbSVinicius Costa Gomes .dump = taprio_dump, 19365a781ccbSVinicius Costa Gomes .owner = THIS_MODULE, 19375a781ccbSVinicius Costa Gomes }; 19385a781ccbSVinicius Costa Gomes 19397b9eba7bSLeandro Dorileo static struct notifier_block taprio_device_notifier = { 19407b9eba7bSLeandro Dorileo .notifier_call = taprio_dev_notifier, 19417b9eba7bSLeandro Dorileo }; 19427b9eba7bSLeandro Dorileo 19435a781ccbSVinicius Costa Gomes static int __init taprio_module_init(void) 19445a781ccbSVinicius Costa Gomes { 19457b9eba7bSLeandro Dorileo int err = register_netdevice_notifier(&taprio_device_notifier); 19467b9eba7bSLeandro Dorileo 19477b9eba7bSLeandro Dorileo if (err) 19487b9eba7bSLeandro Dorileo return err; 19497b9eba7bSLeandro Dorileo 19505a781ccbSVinicius Costa Gomes return register_qdisc(&taprio_qdisc_ops); 19515a781ccbSVinicius Costa Gomes } 19525a781ccbSVinicius Costa Gomes 19535a781ccbSVinicius Costa Gomes static void __exit taprio_module_exit(void) 19545a781ccbSVinicius Costa Gomes { 19555a781ccbSVinicius Costa Gomes unregister_qdisc(&taprio_qdisc_ops); 19567b9eba7bSLeandro Dorileo unregister_netdevice_notifier(&taprio_device_notifier); 19575a781ccbSVinicius Costa Gomes } 19585a781ccbSVinicius Costa Gomes 19595a781ccbSVinicius Costa Gomes module_init(taprio_module_init); 19605a781ccbSVinicius Costa Gomes module_exit(taprio_module_exit); 19615a781ccbSVinicius Costa Gomes MODULE_LICENSE("GPL"); 1962