15a781ccbSVinicius Costa Gomes // SPDX-License-Identifier: GPL-2.0 25a781ccbSVinicius Costa Gomes 35a781ccbSVinicius Costa Gomes /* net/sched/sch_taprio.c Time Aware Priority Scheduler 45a781ccbSVinicius Costa Gomes * 55a781ccbSVinicius Costa Gomes * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com> 65a781ccbSVinicius Costa Gomes * 75a781ccbSVinicius Costa Gomes */ 85a781ccbSVinicius Costa Gomes 9cc69837fSJakub Kicinski #include <linux/ethtool.h> 10a721c3e5SVladimir Oltean #include <linux/ethtool_netlink.h> 115a781ccbSVinicius Costa Gomes #include <linux/types.h> 125a781ccbSVinicius Costa Gomes #include <linux/slab.h> 135a781ccbSVinicius Costa Gomes #include <linux/kernel.h> 145a781ccbSVinicius Costa Gomes #include <linux/string.h> 155a781ccbSVinicius Costa Gomes #include <linux/list.h> 165a781ccbSVinicius Costa Gomes #include <linux/errno.h> 175a781ccbSVinicius Costa Gomes #include <linux/skbuff.h> 1823bddf69SJakub Kicinski #include <linux/math64.h> 195a781ccbSVinicius Costa Gomes #include <linux/module.h> 205a781ccbSVinicius Costa Gomes #include <linux/spinlock.h> 21a3d43c0dSVinicius Costa Gomes #include <linux/rcupdate.h> 22837ced3aSVladimir Oltean #include <linux/time.h> 235a781ccbSVinicius Costa Gomes #include <net/netlink.h> 245a781ccbSVinicius Costa Gomes #include <net/pkt_sched.h> 255a781ccbSVinicius Costa Gomes #include <net/pkt_cls.h> 265a781ccbSVinicius Costa Gomes #include <net/sch_generic.h> 274cfd5779SVedang Patel #include <net/sock.h> 2854002066SVedang Patel #include <net/tcp.h> 295a781ccbSVinicius Costa Gomes 301dfe086dSVladimir Oltean #include "sch_mqprio_lib.h" 311dfe086dSVladimir Oltean 327b9eba7bSLeandro Dorileo static LIST_HEAD(taprio_list); 332f530df7SVladimir Oltean static struct static_key_false taprio_have_broken_mqprio; 342f530df7SVladimir Oltean static struct static_key_false taprio_have_working_mqprio; 357b9eba7bSLeandro Dorileo 365a781ccbSVinicius Costa Gomes #define TAPRIO_ALL_GATES_OPEN -1 375a781ccbSVinicius Costa Gomes 384cfd5779SVedang Patel #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) 399c66d156SVinicius Costa Gomes #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD) 40a9d62274SVinicius Costa Gomes #define TAPRIO_FLAGS_INVALID U32_MAX 414cfd5779SVedang Patel 425a781ccbSVinicius Costa Gomes struct sched_entry { 43a306a90cSVladimir Oltean /* Durations between this GCL entry and the GCL entry where the 44a306a90cSVladimir Oltean * respective traffic class gate closes 45a306a90cSVladimir Oltean */ 46a306a90cSVladimir Oltean u64 gate_duration[TC_MAX_QUEUE]; 47d2ad689dSVladimir Oltean atomic_t budget[TC_MAX_QUEUE]; 48a1e6ad30SVladimir Oltean /* The qdisc makes some effort so that no packet leaves 49a1e6ad30SVladimir Oltean * after this time 505a781ccbSVinicius Costa Gomes */ 51a1e6ad30SVladimir Oltean ktime_t gate_close_time[TC_MAX_QUEUE]; 52a1e6ad30SVladimir Oltean struct list_head list; 53a1e6ad30SVladimir Oltean /* Used to calculate when to advance the schedule */ 54e5517551SVladimir Oltean ktime_t end_time; 554cfd5779SVedang Patel ktime_t next_txtime; 565a781ccbSVinicius Costa Gomes int index; 575a781ccbSVinicius Costa Gomes u32 gate_mask; 585a781ccbSVinicius Costa Gomes u32 interval; 595a781ccbSVinicius Costa Gomes u8 command; 605a781ccbSVinicius Costa Gomes }; 615a781ccbSVinicius Costa Gomes 62a3d43c0dSVinicius Costa Gomes struct sched_gate_list { 63a306a90cSVladimir Oltean /* Longest non-zero contiguous gate durations per traffic class, 64a306a90cSVladimir Oltean * or 0 if a traffic class gate never opens during the schedule. 65a306a90cSVladimir Oltean */ 66a306a90cSVladimir Oltean u64 max_open_gate_duration[TC_MAX_QUEUE]; 67a878fd46SVladimir Oltean u32 max_frm_len[TC_MAX_QUEUE]; /* for the fast path */ 68fed87cc6SVladimir Oltean u32 max_sdu[TC_MAX_QUEUE]; /* for dump */ 69a3d43c0dSVinicius Costa Gomes struct rcu_head rcu; 70a3d43c0dSVinicius Costa Gomes struct list_head entries; 71a3d43c0dSVinicius Costa Gomes size_t num_entries; 72e5517551SVladimir Oltean ktime_t cycle_end_time; 736ca6a665SVinicius Costa Gomes s64 cycle_time; 74c25031e9SVinicius Costa Gomes s64 cycle_time_extension; 75a3d43c0dSVinicius Costa Gomes s64 base_time; 76a3d43c0dSVinicius Costa Gomes }; 77a3d43c0dSVinicius Costa Gomes 785a781ccbSVinicius Costa Gomes struct taprio_sched { 795a781ccbSVinicius Costa Gomes struct Qdisc **qdiscs; 805a781ccbSVinicius Costa Gomes struct Qdisc *root; 814cfd5779SVedang Patel u32 flags; 827ede7b03SVedang Patel enum tk_offsets tk_offset; 835a781ccbSVinicius Costa Gomes int clockid; 84db46e3a8SVladimir Oltean bool offloaded; 852f530df7SVladimir Oltean bool detected_mqprio; 862f530df7SVladimir Oltean bool broken_mqprio; 877b9eba7bSLeandro Dorileo atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+ 885a781ccbSVinicius Costa Gomes * speeds it's sub-nanoseconds per byte 895a781ccbSVinicius Costa Gomes */ 905a781ccbSVinicius Costa Gomes 915a781ccbSVinicius Costa Gomes /* Protects the update side of the RCU protected current_entry */ 925a781ccbSVinicius Costa Gomes spinlock_t current_entry_lock; 935a781ccbSVinicius Costa Gomes struct sched_entry __rcu *current_entry; 94a3d43c0dSVinicius Costa Gomes struct sched_gate_list __rcu *oper_sched; 95a3d43c0dSVinicius Costa Gomes struct sched_gate_list __rcu *admin_sched; 965a781ccbSVinicius Costa Gomes struct hrtimer advance_timer; 977b9eba7bSLeandro Dorileo struct list_head taprio_list; 982f530df7SVladimir Oltean int cur_txq[TC_MAX_QUEUE]; 99fed87cc6SVladimir Oltean u32 max_sdu[TC_MAX_QUEUE]; /* save info from the user */ 100a721c3e5SVladimir Oltean u32 fp[TC_QOPT_MAX_QUEUE]; /* only for dump and offloading */ 101a5b64700SVedang Patel u32 txtime_delay; 1025a781ccbSVinicius Costa Gomes }; 1035a781ccbSVinicius Costa Gomes 1049c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload { 1059c66d156SVinicius Costa Gomes refcount_t users; 1069c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload offload; 1079c66d156SVinicius Costa Gomes }; 1089c66d156SVinicius Costa Gomes 109a306a90cSVladimir Oltean static void taprio_calculate_gate_durations(struct taprio_sched *q, 110a306a90cSVladimir Oltean struct sched_gate_list *sched) 111a306a90cSVladimir Oltean { 112a306a90cSVladimir Oltean struct net_device *dev = qdisc_dev(q->root); 113a306a90cSVladimir Oltean int num_tc = netdev_get_num_tc(dev); 114a306a90cSVladimir Oltean struct sched_entry *entry, *cur; 115a306a90cSVladimir Oltean int tc; 116a306a90cSVladimir Oltean 117a306a90cSVladimir Oltean list_for_each_entry(entry, &sched->entries, list) { 118a306a90cSVladimir Oltean u32 gates_still_open = entry->gate_mask; 119a306a90cSVladimir Oltean 120a306a90cSVladimir Oltean /* For each traffic class, calculate each open gate duration, 121a306a90cSVladimir Oltean * starting at this schedule entry and ending at the schedule 122a306a90cSVladimir Oltean * entry containing a gate close event for that TC. 123a306a90cSVladimir Oltean */ 124a306a90cSVladimir Oltean cur = entry; 125a306a90cSVladimir Oltean 126a306a90cSVladimir Oltean do { 127a306a90cSVladimir Oltean if (!gates_still_open) 128a306a90cSVladimir Oltean break; 129a306a90cSVladimir Oltean 130a306a90cSVladimir Oltean for (tc = 0; tc < num_tc; tc++) { 131a306a90cSVladimir Oltean if (!(gates_still_open & BIT(tc))) 132a306a90cSVladimir Oltean continue; 133a306a90cSVladimir Oltean 134a306a90cSVladimir Oltean if (cur->gate_mask & BIT(tc)) 135a306a90cSVladimir Oltean entry->gate_duration[tc] += cur->interval; 136a306a90cSVladimir Oltean else 137a306a90cSVladimir Oltean gates_still_open &= ~BIT(tc); 138a306a90cSVladimir Oltean } 139a306a90cSVladimir Oltean 140a306a90cSVladimir Oltean cur = list_next_entry_circular(cur, &sched->entries, list); 141a306a90cSVladimir Oltean } while (cur != entry); 142a306a90cSVladimir Oltean 143a306a90cSVladimir Oltean /* Keep track of the maximum gate duration for each traffic 144a306a90cSVladimir Oltean * class, taking care to not confuse a traffic class which is 145a306a90cSVladimir Oltean * temporarily closed with one that is always closed. 146a306a90cSVladimir Oltean */ 147a306a90cSVladimir Oltean for (tc = 0; tc < num_tc; tc++) 148a306a90cSVladimir Oltean if (entry->gate_duration[tc] && 149a306a90cSVladimir Oltean sched->max_open_gate_duration[tc] < entry->gate_duration[tc]) 150a306a90cSVladimir Oltean sched->max_open_gate_duration[tc] = entry->gate_duration[tc]; 151a306a90cSVladimir Oltean } 152a306a90cSVladimir Oltean } 153a306a90cSVladimir Oltean 154a1e6ad30SVladimir Oltean static bool taprio_entry_allows_tx(ktime_t skb_end_time, 155a1e6ad30SVladimir Oltean struct sched_entry *entry, int tc) 156a1e6ad30SVladimir Oltean { 157a1e6ad30SVladimir Oltean return ktime_before(skb_end_time, entry->gate_close_time[tc]); 158a1e6ad30SVladimir Oltean } 159a1e6ad30SVladimir Oltean 160a3d43c0dSVinicius Costa Gomes static ktime_t sched_base_time(const struct sched_gate_list *sched) 161a3d43c0dSVinicius Costa Gomes { 162a3d43c0dSVinicius Costa Gomes if (!sched) 163a3d43c0dSVinicius Costa Gomes return KTIME_MAX; 164a3d43c0dSVinicius Costa Gomes 165a3d43c0dSVinicius Costa Gomes return ns_to_ktime(sched->base_time); 166a3d43c0dSVinicius Costa Gomes } 167a3d43c0dSVinicius Costa Gomes 1686dc25401SEric Dumazet static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono) 1697ede7b03SVedang Patel { 1706dc25401SEric Dumazet /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */ 1716dc25401SEric Dumazet enum tk_offsets tk_offset = READ_ONCE(q->tk_offset); 1727ede7b03SVedang Patel 1736dc25401SEric Dumazet switch (tk_offset) { 1747ede7b03SVedang Patel case TK_OFFS_MAX: 1757ede7b03SVedang Patel return mono; 1767ede7b03SVedang Patel default: 1776dc25401SEric Dumazet return ktime_mono_to_any(mono, tk_offset); 1786dc25401SEric Dumazet } 1797ede7b03SVedang Patel } 1807ede7b03SVedang Patel 1816dc25401SEric Dumazet static ktime_t taprio_get_time(const struct taprio_sched *q) 1826dc25401SEric Dumazet { 1836dc25401SEric Dumazet return taprio_mono_to_any(q, ktime_get()); 1847ede7b03SVedang Patel } 1857ede7b03SVedang Patel 186a3d43c0dSVinicius Costa Gomes static void taprio_free_sched_cb(struct rcu_head *head) 187a3d43c0dSVinicius Costa Gomes { 188a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu); 189a3d43c0dSVinicius Costa Gomes struct sched_entry *entry, *n; 190a3d43c0dSVinicius Costa Gomes 191a3d43c0dSVinicius Costa Gomes list_for_each_entry_safe(entry, n, &sched->entries, list) { 192a3d43c0dSVinicius Costa Gomes list_del(&entry->list); 193a3d43c0dSVinicius Costa Gomes kfree(entry); 194a3d43c0dSVinicius Costa Gomes } 195a3d43c0dSVinicius Costa Gomes 196a3d43c0dSVinicius Costa Gomes kfree(sched); 197a3d43c0dSVinicius Costa Gomes } 198a3d43c0dSVinicius Costa Gomes 199a3d43c0dSVinicius Costa Gomes static void switch_schedules(struct taprio_sched *q, 200a3d43c0dSVinicius Costa Gomes struct sched_gate_list **admin, 201a3d43c0dSVinicius Costa Gomes struct sched_gate_list **oper) 202a3d43c0dSVinicius Costa Gomes { 203a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->oper_sched, *admin); 204a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->admin_sched, NULL); 205a3d43c0dSVinicius Costa Gomes 206a3d43c0dSVinicius Costa Gomes if (*oper) 207a3d43c0dSVinicius Costa Gomes call_rcu(&(*oper)->rcu, taprio_free_sched_cb); 208a3d43c0dSVinicius Costa Gomes 209a3d43c0dSVinicius Costa Gomes *oper = *admin; 210a3d43c0dSVinicius Costa Gomes *admin = NULL; 211a3d43c0dSVinicius Costa Gomes } 212a3d43c0dSVinicius Costa Gomes 2134cfd5779SVedang Patel /* Get how much time has been already elapsed in the current cycle. */ 2144cfd5779SVedang Patel static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time) 2154cfd5779SVedang Patel { 2164cfd5779SVedang Patel ktime_t time_since_sched_start; 2174cfd5779SVedang Patel s32 time_elapsed; 2184cfd5779SVedang Patel 2194cfd5779SVedang Patel time_since_sched_start = ktime_sub(time, sched->base_time); 2204cfd5779SVedang Patel div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed); 2214cfd5779SVedang Patel 2224cfd5779SVedang Patel return time_elapsed; 2234cfd5779SVedang Patel } 2244cfd5779SVedang Patel 2254cfd5779SVedang Patel static ktime_t get_interval_end_time(struct sched_gate_list *sched, 2264cfd5779SVedang Patel struct sched_gate_list *admin, 2274cfd5779SVedang Patel struct sched_entry *entry, 2284cfd5779SVedang Patel ktime_t intv_start) 2294cfd5779SVedang Patel { 2304cfd5779SVedang Patel s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start); 2314cfd5779SVedang Patel ktime_t intv_end, cycle_ext_end, cycle_end; 2324cfd5779SVedang Patel 2334cfd5779SVedang Patel cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed); 2344cfd5779SVedang Patel intv_end = ktime_add_ns(intv_start, entry->interval); 2354cfd5779SVedang Patel cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension); 2364cfd5779SVedang Patel 2374cfd5779SVedang Patel if (ktime_before(intv_end, cycle_end)) 2384cfd5779SVedang Patel return intv_end; 2394cfd5779SVedang Patel else if (admin && admin != sched && 2404cfd5779SVedang Patel ktime_after(admin->base_time, cycle_end) && 2414cfd5779SVedang Patel ktime_before(admin->base_time, cycle_ext_end)) 2424cfd5779SVedang Patel return admin->base_time; 2434cfd5779SVedang Patel else 2444cfd5779SVedang Patel return cycle_end; 2454cfd5779SVedang Patel } 2464cfd5779SVedang Patel 2474cfd5779SVedang Patel static int length_to_duration(struct taprio_sched *q, int len) 2484cfd5779SVedang Patel { 249837ced3aSVladimir Oltean return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC); 2504cfd5779SVedang Patel } 2514cfd5779SVedang Patel 252fed87cc6SVladimir Oltean static int duration_to_length(struct taprio_sched *q, u64 duration) 253fed87cc6SVladimir Oltean { 254fed87cc6SVladimir Oltean return div_u64(duration * PSEC_PER_NSEC, atomic64_read(&q->picos_per_byte)); 255fed87cc6SVladimir Oltean } 256fed87cc6SVladimir Oltean 257fed87cc6SVladimir Oltean /* Sets sched->max_sdu[] and sched->max_frm_len[] to the minimum between the 258fed87cc6SVladimir Oltean * q->max_sdu[] requested by the user and the max_sdu dynamically determined by 259fed87cc6SVladimir Oltean * the maximum open gate durations at the given link speed. 260fed87cc6SVladimir Oltean */ 261a878fd46SVladimir Oltean static void taprio_update_queue_max_sdu(struct taprio_sched *q, 262fed87cc6SVladimir Oltean struct sched_gate_list *sched, 263fed87cc6SVladimir Oltean struct qdisc_size_table *stab) 264a878fd46SVladimir Oltean { 265a878fd46SVladimir Oltean struct net_device *dev = qdisc_dev(q->root); 266a878fd46SVladimir Oltean int num_tc = netdev_get_num_tc(dev); 267fed87cc6SVladimir Oltean u32 max_sdu_from_user; 268fed87cc6SVladimir Oltean u32 max_sdu_dynamic; 269fed87cc6SVladimir Oltean u32 max_sdu; 270a878fd46SVladimir Oltean int tc; 271a878fd46SVladimir Oltean 272a878fd46SVladimir Oltean for (tc = 0; tc < num_tc; tc++) { 273fed87cc6SVladimir Oltean max_sdu_from_user = q->max_sdu[tc] ?: U32_MAX; 274fed87cc6SVladimir Oltean 275fed87cc6SVladimir Oltean /* TC gate never closes => keep the queueMaxSDU 276fed87cc6SVladimir Oltean * selected by the user 277fed87cc6SVladimir Oltean */ 278fed87cc6SVladimir Oltean if (sched->max_open_gate_duration[tc] == sched->cycle_time) { 279fed87cc6SVladimir Oltean max_sdu_dynamic = U32_MAX; 280fed87cc6SVladimir Oltean } else { 281fed87cc6SVladimir Oltean u32 max_frm_len; 282fed87cc6SVladimir Oltean 283fed87cc6SVladimir Oltean max_frm_len = duration_to_length(q, sched->max_open_gate_duration[tc]); 284bdf366bdSVladimir Oltean /* Compensate for L1 overhead from size table, 285bdf366bdSVladimir Oltean * but don't let the frame size go negative 286bdf366bdSVladimir Oltean */ 287bdf366bdSVladimir Oltean if (stab) { 288fed87cc6SVladimir Oltean max_frm_len -= stab->szopts.overhead; 289bdf366bdSVladimir Oltean max_frm_len = max_t(int, max_frm_len, 290bdf366bdSVladimir Oltean dev->hard_header_len + 1); 291bdf366bdSVladimir Oltean } 292fed87cc6SVladimir Oltean max_sdu_dynamic = max_frm_len - dev->hard_header_len; 29364cb6aadSVladimir Oltean if (max_sdu_dynamic > dev->max_mtu) 29464cb6aadSVladimir Oltean max_sdu_dynamic = U32_MAX; 295fed87cc6SVladimir Oltean } 296fed87cc6SVladimir Oltean 297fed87cc6SVladimir Oltean max_sdu = min(max_sdu_dynamic, max_sdu_from_user); 298fed87cc6SVladimir Oltean 299fed87cc6SVladimir Oltean if (max_sdu != U32_MAX) { 300fed87cc6SVladimir Oltean sched->max_frm_len[tc] = max_sdu + dev->hard_header_len; 301fed87cc6SVladimir Oltean sched->max_sdu[tc] = max_sdu; 302fed87cc6SVladimir Oltean } else { 303a878fd46SVladimir Oltean sched->max_frm_len[tc] = U32_MAX; /* never oversized */ 304fed87cc6SVladimir Oltean sched->max_sdu[tc] = 0; 305fed87cc6SVladimir Oltean } 306a878fd46SVladimir Oltean } 307a878fd46SVladimir Oltean } 308a878fd46SVladimir Oltean 3094cfd5779SVedang Patel /* Returns the entry corresponding to next available interval. If 3104cfd5779SVedang Patel * validate_interval is set, it only validates whether the timestamp occurs 3114cfd5779SVedang Patel * when the gate corresponding to the skb's traffic class is open. 3124cfd5779SVedang Patel */ 3134cfd5779SVedang Patel static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb, 3144cfd5779SVedang Patel struct Qdisc *sch, 3154cfd5779SVedang Patel struct sched_gate_list *sched, 3164cfd5779SVedang Patel struct sched_gate_list *admin, 3174cfd5779SVedang Patel ktime_t time, 3184cfd5779SVedang Patel ktime_t *interval_start, 3194cfd5779SVedang Patel ktime_t *interval_end, 3204cfd5779SVedang Patel bool validate_interval) 3214cfd5779SVedang Patel { 3224cfd5779SVedang Patel ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time; 3234cfd5779SVedang Patel ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time; 3244cfd5779SVedang Patel struct sched_entry *entry = NULL, *entry_found = NULL; 3254cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 3264cfd5779SVedang Patel struct net_device *dev = qdisc_dev(sch); 3274cfd5779SVedang Patel bool entry_available = false; 3284cfd5779SVedang Patel s32 cycle_elapsed; 3294cfd5779SVedang Patel int tc, n; 3304cfd5779SVedang Patel 3314cfd5779SVedang Patel tc = netdev_get_prio_tc_map(dev, skb->priority); 3324cfd5779SVedang Patel packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb)); 3334cfd5779SVedang Patel 3344cfd5779SVedang Patel *interval_start = 0; 3354cfd5779SVedang Patel *interval_end = 0; 3364cfd5779SVedang Patel 3374cfd5779SVedang Patel if (!sched) 3384cfd5779SVedang Patel return NULL; 3394cfd5779SVedang Patel 3404cfd5779SVedang Patel cycle = sched->cycle_time; 3414cfd5779SVedang Patel cycle_elapsed = get_cycle_time_elapsed(sched, time); 3424cfd5779SVedang Patel curr_intv_end = ktime_sub_ns(time, cycle_elapsed); 3434cfd5779SVedang Patel cycle_end = ktime_add_ns(curr_intv_end, cycle); 3444cfd5779SVedang Patel 3454cfd5779SVedang Patel list_for_each_entry(entry, &sched->entries, list) { 3464cfd5779SVedang Patel curr_intv_start = curr_intv_end; 3474cfd5779SVedang Patel curr_intv_end = get_interval_end_time(sched, admin, entry, 3484cfd5779SVedang Patel curr_intv_start); 3494cfd5779SVedang Patel 3504cfd5779SVedang Patel if (ktime_after(curr_intv_start, cycle_end)) 3514cfd5779SVedang Patel break; 3524cfd5779SVedang Patel 3534cfd5779SVedang Patel if (!(entry->gate_mask & BIT(tc)) || 3544cfd5779SVedang Patel packet_transmit_time > entry->interval) 3554cfd5779SVedang Patel continue; 3564cfd5779SVedang Patel 3574cfd5779SVedang Patel txtime = entry->next_txtime; 3584cfd5779SVedang Patel 3594cfd5779SVedang Patel if (ktime_before(txtime, time) || validate_interval) { 3604cfd5779SVedang Patel transmit_end_time = ktime_add_ns(time, packet_transmit_time); 3614cfd5779SVedang Patel if ((ktime_before(curr_intv_start, time) && 3624cfd5779SVedang Patel ktime_before(transmit_end_time, curr_intv_end)) || 3634cfd5779SVedang Patel (ktime_after(curr_intv_start, time) && !validate_interval)) { 3644cfd5779SVedang Patel entry_found = entry; 3654cfd5779SVedang Patel *interval_start = curr_intv_start; 3664cfd5779SVedang Patel *interval_end = curr_intv_end; 3674cfd5779SVedang Patel break; 3684cfd5779SVedang Patel } else if (!entry_available && !validate_interval) { 3694cfd5779SVedang Patel /* Here, we are just trying to find out the 3704cfd5779SVedang Patel * first available interval in the next cycle. 3714cfd5779SVedang Patel */ 3720deee7aaSJiapeng Zhong entry_available = true; 3734cfd5779SVedang Patel entry_found = entry; 3744cfd5779SVedang Patel *interval_start = ktime_add_ns(curr_intv_start, cycle); 3754cfd5779SVedang Patel *interval_end = ktime_add_ns(curr_intv_end, cycle); 3764cfd5779SVedang Patel } 3774cfd5779SVedang Patel } else if (ktime_before(txtime, earliest_txtime) && 3784cfd5779SVedang Patel !entry_available) { 3794cfd5779SVedang Patel earliest_txtime = txtime; 3804cfd5779SVedang Patel entry_found = entry; 3814cfd5779SVedang Patel n = div_s64(ktime_sub(txtime, curr_intv_start), cycle); 3824cfd5779SVedang Patel *interval_start = ktime_add(curr_intv_start, n * cycle); 3834cfd5779SVedang Patel *interval_end = ktime_add(curr_intv_end, n * cycle); 3844cfd5779SVedang Patel } 3854cfd5779SVedang Patel } 3864cfd5779SVedang Patel 3874cfd5779SVedang Patel return entry_found; 3884cfd5779SVedang Patel } 3894cfd5779SVedang Patel 3904cfd5779SVedang Patel static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch) 3914cfd5779SVedang Patel { 3924cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 3934cfd5779SVedang Patel struct sched_gate_list *sched, *admin; 3944cfd5779SVedang Patel ktime_t interval_start, interval_end; 3954cfd5779SVedang Patel struct sched_entry *entry; 3964cfd5779SVedang Patel 3974cfd5779SVedang Patel rcu_read_lock(); 3984cfd5779SVedang Patel sched = rcu_dereference(q->oper_sched); 3994cfd5779SVedang Patel admin = rcu_dereference(q->admin_sched); 4004cfd5779SVedang Patel 4014cfd5779SVedang Patel entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp, 4024cfd5779SVedang Patel &interval_start, &interval_end, true); 4034cfd5779SVedang Patel rcu_read_unlock(); 4044cfd5779SVedang Patel 4054cfd5779SVedang Patel return entry; 4064cfd5779SVedang Patel } 4074cfd5779SVedang Patel 4089c66d156SVinicius Costa Gomes static bool taprio_flags_valid(u32 flags) 4099c66d156SVinicius Costa Gomes { 4109c66d156SVinicius Costa Gomes /* Make sure no other flag bits are set. */ 4119c66d156SVinicius Costa Gomes if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST | 4129c66d156SVinicius Costa Gomes TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) 4139c66d156SVinicius Costa Gomes return false; 4149c66d156SVinicius Costa Gomes /* txtime-assist and full offload are mutually exclusive */ 4159c66d156SVinicius Costa Gomes if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) && 4169c66d156SVinicius Costa Gomes (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) 4179c66d156SVinicius Costa Gomes return false; 4189c66d156SVinicius Costa Gomes return true; 4199c66d156SVinicius Costa Gomes } 4209c66d156SVinicius Costa Gomes 42154002066SVedang Patel /* This returns the tstamp value set by TCP in terms of the set clock. */ 42254002066SVedang Patel static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb) 42354002066SVedang Patel { 42454002066SVedang Patel unsigned int offset = skb_network_offset(skb); 42554002066SVedang Patel const struct ipv6hdr *ipv6h; 42654002066SVedang Patel const struct iphdr *iph; 42754002066SVedang Patel struct ipv6hdr _ipv6h; 42854002066SVedang Patel 42954002066SVedang Patel ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 43054002066SVedang Patel if (!ipv6h) 43154002066SVedang Patel return 0; 43254002066SVedang Patel 43354002066SVedang Patel if (ipv6h->version == 4) { 43454002066SVedang Patel iph = (struct iphdr *)ipv6h; 43554002066SVedang Patel offset += iph->ihl * 4; 43654002066SVedang Patel 43754002066SVedang Patel /* special-case 6in4 tunnelling, as that is a common way to get 43854002066SVedang Patel * v6 connectivity in the home 43954002066SVedang Patel */ 44054002066SVedang Patel if (iph->protocol == IPPROTO_IPV6) { 44154002066SVedang Patel ipv6h = skb_header_pointer(skb, offset, 44254002066SVedang Patel sizeof(_ipv6h), &_ipv6h); 44354002066SVedang Patel 44454002066SVedang Patel if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP) 44554002066SVedang Patel return 0; 44654002066SVedang Patel } else if (iph->protocol != IPPROTO_TCP) { 44754002066SVedang Patel return 0; 44854002066SVedang Patel } 44954002066SVedang Patel } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) { 45054002066SVedang Patel return 0; 45154002066SVedang Patel } 45254002066SVedang Patel 4536dc25401SEric Dumazet return taprio_mono_to_any(q, skb->skb_mstamp_ns); 45454002066SVedang Patel } 45554002066SVedang Patel 4564cfd5779SVedang Patel /* There are a few scenarios where we will have to modify the txtime from 4574cfd5779SVedang Patel * what is read from next_txtime in sched_entry. They are: 4584cfd5779SVedang Patel * 1. If txtime is in the past, 4594cfd5779SVedang Patel * a. The gate for the traffic class is currently open and packet can be 4604cfd5779SVedang Patel * transmitted before it closes, schedule the packet right away. 4614cfd5779SVedang Patel * b. If the gate corresponding to the traffic class is going to open later 4624cfd5779SVedang Patel * in the cycle, set the txtime of packet to the interval start. 4634cfd5779SVedang Patel * 2. If txtime is in the future, there are packets corresponding to the 4644cfd5779SVedang Patel * current traffic class waiting to be transmitted. So, the following 4654cfd5779SVedang Patel * possibilities exist: 4664cfd5779SVedang Patel * a. We can transmit the packet before the window containing the txtime 4674cfd5779SVedang Patel * closes. 4684cfd5779SVedang Patel * b. The window might close before the transmission can be completed 4694cfd5779SVedang Patel * successfully. So, schedule the packet in the next open window. 4704cfd5779SVedang Patel */ 4714cfd5779SVedang Patel static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch) 4724cfd5779SVedang Patel { 47354002066SVedang Patel ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp; 4744cfd5779SVedang Patel struct taprio_sched *q = qdisc_priv(sch); 4754cfd5779SVedang Patel struct sched_gate_list *sched, *admin; 4764cfd5779SVedang Patel ktime_t minimum_time, now, txtime; 4774cfd5779SVedang Patel int len, packet_transmit_time; 4784cfd5779SVedang Patel struct sched_entry *entry; 4794cfd5779SVedang Patel bool sched_changed; 4804cfd5779SVedang Patel 4817ede7b03SVedang Patel now = taprio_get_time(q); 4824cfd5779SVedang Patel minimum_time = ktime_add_ns(now, q->txtime_delay); 4834cfd5779SVedang Patel 48454002066SVedang Patel tcp_tstamp = get_tcp_tstamp(q, skb); 48554002066SVedang Patel minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp); 48654002066SVedang Patel 4874cfd5779SVedang Patel rcu_read_lock(); 4884cfd5779SVedang Patel admin = rcu_dereference(q->admin_sched); 4894cfd5779SVedang Patel sched = rcu_dereference(q->oper_sched); 4904cfd5779SVedang Patel if (admin && ktime_after(minimum_time, admin->base_time)) 4914cfd5779SVedang Patel switch_schedules(q, &admin, &sched); 4924cfd5779SVedang Patel 4934cfd5779SVedang Patel /* Until the schedule starts, all the queues are open */ 4944cfd5779SVedang Patel if (!sched || ktime_before(minimum_time, sched->base_time)) { 4954cfd5779SVedang Patel txtime = minimum_time; 4964cfd5779SVedang Patel goto done; 4974cfd5779SVedang Patel } 4984cfd5779SVedang Patel 4994cfd5779SVedang Patel len = qdisc_pkt_len(skb); 5004cfd5779SVedang Patel packet_transmit_time = length_to_duration(q, len); 5014cfd5779SVedang Patel 5024cfd5779SVedang Patel do { 5030deee7aaSJiapeng Zhong sched_changed = false; 5044cfd5779SVedang Patel 5054cfd5779SVedang Patel entry = find_entry_to_transmit(skb, sch, sched, admin, 5064cfd5779SVedang Patel minimum_time, 5074cfd5779SVedang Patel &interval_start, &interval_end, 5084cfd5779SVedang Patel false); 5094cfd5779SVedang Patel if (!entry) { 5104cfd5779SVedang Patel txtime = 0; 5114cfd5779SVedang Patel goto done; 5124cfd5779SVedang Patel } 5134cfd5779SVedang Patel 5144cfd5779SVedang Patel txtime = entry->next_txtime; 5154cfd5779SVedang Patel txtime = max_t(ktime_t, txtime, minimum_time); 5164cfd5779SVedang Patel txtime = max_t(ktime_t, txtime, interval_start); 5174cfd5779SVedang Patel 5184cfd5779SVedang Patel if (admin && admin != sched && 5194cfd5779SVedang Patel ktime_after(txtime, admin->base_time)) { 5204cfd5779SVedang Patel sched = admin; 5210deee7aaSJiapeng Zhong sched_changed = true; 5224cfd5779SVedang Patel continue; 5234cfd5779SVedang Patel } 5244cfd5779SVedang Patel 5254cfd5779SVedang Patel transmit_end_time = ktime_add(txtime, packet_transmit_time); 5264cfd5779SVedang Patel minimum_time = transmit_end_time; 5274cfd5779SVedang Patel 5284cfd5779SVedang Patel /* Update the txtime of current entry to the next time it's 5294cfd5779SVedang Patel * interval starts. 5304cfd5779SVedang Patel */ 5314cfd5779SVedang Patel if (ktime_after(transmit_end_time, interval_end)) 5324cfd5779SVedang Patel entry->next_txtime = ktime_add(interval_start, sched->cycle_time); 5334cfd5779SVedang Patel } while (sched_changed || ktime_after(transmit_end_time, interval_end)); 5344cfd5779SVedang Patel 5354cfd5779SVedang Patel entry->next_txtime = transmit_end_time; 5364cfd5779SVedang Patel 5374cfd5779SVedang Patel done: 5384cfd5779SVedang Patel rcu_read_unlock(); 5394cfd5779SVedang Patel return txtime; 5404cfd5779SVedang Patel } 5414cfd5779SVedang Patel 542a878fd46SVladimir Oltean /* Devices with full offload are expected to honor this in hardware */ 543a878fd46SVladimir Oltean static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch, 544a878fd46SVladimir Oltean struct sk_buff *skb) 545a878fd46SVladimir Oltean { 546a878fd46SVladimir Oltean struct taprio_sched *q = qdisc_priv(sch); 547a878fd46SVladimir Oltean struct net_device *dev = qdisc_dev(sch); 548a878fd46SVladimir Oltean struct sched_gate_list *sched; 549a878fd46SVladimir Oltean int prio = skb->priority; 550a878fd46SVladimir Oltean bool exceeds = false; 551a878fd46SVladimir Oltean u8 tc; 552a878fd46SVladimir Oltean 553a878fd46SVladimir Oltean tc = netdev_get_prio_tc_map(dev, prio); 554a878fd46SVladimir Oltean 555a878fd46SVladimir Oltean rcu_read_lock(); 556a878fd46SVladimir Oltean sched = rcu_dereference(q->oper_sched); 557a878fd46SVladimir Oltean if (sched && skb->len > sched->max_frm_len[tc]) 558a878fd46SVladimir Oltean exceeds = true; 559a878fd46SVladimir Oltean rcu_read_unlock(); 560a878fd46SVladimir Oltean 561a878fd46SVladimir Oltean return exceeds; 562a878fd46SVladimir Oltean } 563a878fd46SVladimir Oltean 564497cc002SKurt Kanzenbach static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch, 565497cc002SKurt Kanzenbach struct Qdisc *child, struct sk_buff **to_free) 5665a781ccbSVinicius Costa Gomes { 5675a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 5685a781ccbSVinicius Costa Gomes 569e8a64bbaSBenedikt Spranger /* sk_flags are only safe to use on full sockets. */ 570e8a64bbaSBenedikt Spranger if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) { 5714cfd5779SVedang Patel if (!is_valid_interval(skb, sch)) 5724cfd5779SVedang Patel return qdisc_drop(skb, sch, to_free); 5734cfd5779SVedang Patel } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 5744cfd5779SVedang Patel skb->tstamp = get_packet_txtime(skb, sch); 5754cfd5779SVedang Patel if (!skb->tstamp) 5764cfd5779SVedang Patel return qdisc_drop(skb, sch, to_free); 5774cfd5779SVedang Patel } 5784cfd5779SVedang Patel 5795a781ccbSVinicius Costa Gomes qdisc_qstats_backlog_inc(sch, skb); 5805a781ccbSVinicius Costa Gomes sch->q.qlen++; 5815a781ccbSVinicius Costa Gomes 582ac5c66f2SPetr Machata return qdisc_enqueue(skb, child, to_free); 5835a781ccbSVinicius Costa Gomes } 5845a781ccbSVinicius Costa Gomes 5852d5e8071SVladimir Oltean static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch, 5862d5e8071SVladimir Oltean struct Qdisc *child, 587497cc002SKurt Kanzenbach struct sk_buff **to_free) 588497cc002SKurt Kanzenbach { 589497cc002SKurt Kanzenbach unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb); 590497cc002SKurt Kanzenbach netdev_features_t features = netif_skb_features(skb); 591497cc002SKurt Kanzenbach struct sk_buff *segs, *nskb; 592497cc002SKurt Kanzenbach int ret; 593497cc002SKurt Kanzenbach 594497cc002SKurt Kanzenbach segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); 595497cc002SKurt Kanzenbach if (IS_ERR_OR_NULL(segs)) 596497cc002SKurt Kanzenbach return qdisc_drop(skb, sch, to_free); 597497cc002SKurt Kanzenbach 598497cc002SKurt Kanzenbach skb_list_walk_safe(segs, segs, nskb) { 599497cc002SKurt Kanzenbach skb_mark_not_on_list(segs); 600497cc002SKurt Kanzenbach qdisc_skb_cb(segs)->pkt_len = segs->len; 601497cc002SKurt Kanzenbach slen += segs->len; 602497cc002SKurt Kanzenbach 60339b02d6dSVladimir Oltean /* FIXME: we should be segmenting to a smaller size 60439b02d6dSVladimir Oltean * rather than dropping these 60539b02d6dSVladimir Oltean */ 60639b02d6dSVladimir Oltean if (taprio_skb_exceeds_queue_max_sdu(sch, segs)) 60739b02d6dSVladimir Oltean ret = qdisc_drop(segs, sch, to_free); 60839b02d6dSVladimir Oltean else 609497cc002SKurt Kanzenbach ret = taprio_enqueue_one(segs, sch, child, to_free); 61039b02d6dSVladimir Oltean 611497cc002SKurt Kanzenbach if (ret != NET_XMIT_SUCCESS) { 612497cc002SKurt Kanzenbach if (net_xmit_drop_count(ret)) 613497cc002SKurt Kanzenbach qdisc_qstats_drop(sch); 614497cc002SKurt Kanzenbach } else { 615497cc002SKurt Kanzenbach numsegs++; 616497cc002SKurt Kanzenbach } 617497cc002SKurt Kanzenbach } 618497cc002SKurt Kanzenbach 619497cc002SKurt Kanzenbach if (numsegs > 1) 620497cc002SKurt Kanzenbach qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen); 621497cc002SKurt Kanzenbach consume_skb(skb); 622497cc002SKurt Kanzenbach 623497cc002SKurt Kanzenbach return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP; 624497cc002SKurt Kanzenbach } 625497cc002SKurt Kanzenbach 6262d5e8071SVladimir Oltean /* Will not be called in the full offload case, since the TX queues are 6272d5e8071SVladimir Oltean * attached to the Qdisc created using qdisc_create_dflt() 6282d5e8071SVladimir Oltean */ 6292d5e8071SVladimir Oltean static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch, 6302d5e8071SVladimir Oltean struct sk_buff **to_free) 6312d5e8071SVladimir Oltean { 6322d5e8071SVladimir Oltean struct taprio_sched *q = qdisc_priv(sch); 6332d5e8071SVladimir Oltean struct Qdisc *child; 6342d5e8071SVladimir Oltean int queue; 6352d5e8071SVladimir Oltean 6362d5e8071SVladimir Oltean queue = skb_get_queue_mapping(skb); 6372d5e8071SVladimir Oltean 6382d5e8071SVladimir Oltean child = q->qdiscs[queue]; 6392d5e8071SVladimir Oltean if (unlikely(!child)) 6402d5e8071SVladimir Oltean return qdisc_drop(skb, sch, to_free); 6412d5e8071SVladimir Oltean 64239b02d6dSVladimir Oltean if (taprio_skb_exceeds_queue_max_sdu(sch, skb)) { 64339b02d6dSVladimir Oltean /* Large packets might not be transmitted when the transmission 64439b02d6dSVladimir Oltean * duration exceeds any configured interval. Therefore, segment 64539b02d6dSVladimir Oltean * the skb into smaller chunks. Drivers with full offload are 64639b02d6dSVladimir Oltean * expected to handle this in hardware. 6472d5e8071SVladimir Oltean */ 6482d5e8071SVladimir Oltean if (skb_is_gso(skb)) 64939b02d6dSVladimir Oltean return taprio_enqueue_segmented(skb, sch, child, 65039b02d6dSVladimir Oltean to_free); 65139b02d6dSVladimir Oltean 65239b02d6dSVladimir Oltean return qdisc_drop(skb, sch, to_free); 65339b02d6dSVladimir Oltean } 6542d5e8071SVladimir Oltean 655497cc002SKurt Kanzenbach return taprio_enqueue_one(skb, sch, child, to_free); 656497cc002SKurt Kanzenbach } 657497cc002SKurt Kanzenbach 65825becba6SVladimir Oltean static struct sk_buff *taprio_peek(struct Qdisc *sch) 6595a781ccbSVinicius Costa Gomes { 660ecc0cc98SVladimir Oltean WARN_ONCE(1, "taprio only supports operating as root qdisc, peek() not implemented"); 6615a781ccbSVinicius Costa Gomes return NULL; 6625a781ccbSVinicius Costa Gomes } 6635a781ccbSVinicius Costa Gomes 664d2ad689dSVladimir Oltean static void taprio_set_budgets(struct taprio_sched *q, 665d2ad689dSVladimir Oltean struct sched_gate_list *sched, 666d2ad689dSVladimir Oltean struct sched_entry *entry) 66723bddf69SJakub Kicinski { 668d2ad689dSVladimir Oltean struct net_device *dev = qdisc_dev(q->root); 669d2ad689dSVladimir Oltean int num_tc = netdev_get_num_tc(dev); 670d2ad689dSVladimir Oltean int tc, budget; 671d2ad689dSVladimir Oltean 672d2ad689dSVladimir Oltean for (tc = 0; tc < num_tc; tc++) { 673d2ad689dSVladimir Oltean /* Traffic classes which never close have infinite budget */ 674d2ad689dSVladimir Oltean if (entry->gate_duration[tc] == sched->cycle_time) 675d2ad689dSVladimir Oltean budget = INT_MAX; 676d2ad689dSVladimir Oltean else 677d2ad689dSVladimir Oltean budget = div64_u64((u64)entry->gate_duration[tc] * PSEC_PER_NSEC, 678d2ad689dSVladimir Oltean atomic64_read(&q->picos_per_byte)); 679d2ad689dSVladimir Oltean 680d2ad689dSVladimir Oltean atomic_set(&entry->budget[tc], budget); 681d2ad689dSVladimir Oltean } 682d2ad689dSVladimir Oltean } 683d2ad689dSVladimir Oltean 684d2ad689dSVladimir Oltean /* When an skb is sent, it consumes from the budget of all traffic classes */ 685d2ad689dSVladimir Oltean static int taprio_update_budgets(struct sched_entry *entry, size_t len, 686d2ad689dSVladimir Oltean int tc_consumed, int num_tc) 687d2ad689dSVladimir Oltean { 688d2ad689dSVladimir Oltean int tc, budget, new_budget = 0; 689d2ad689dSVladimir Oltean 690d2ad689dSVladimir Oltean for (tc = 0; tc < num_tc; tc++) { 691d2ad689dSVladimir Oltean budget = atomic_read(&entry->budget[tc]); 692d2ad689dSVladimir Oltean /* Don't consume from infinite budget */ 693d2ad689dSVladimir Oltean if (budget == INT_MAX) { 694d2ad689dSVladimir Oltean if (tc == tc_consumed) 695d2ad689dSVladimir Oltean new_budget = budget; 696d2ad689dSVladimir Oltean continue; 697d2ad689dSVladimir Oltean } 698d2ad689dSVladimir Oltean 699d2ad689dSVladimir Oltean if (tc == tc_consumed) 700d2ad689dSVladimir Oltean new_budget = atomic_sub_return(len, &entry->budget[tc]); 701d2ad689dSVladimir Oltean else 702d2ad689dSVladimir Oltean atomic_sub(len, &entry->budget[tc]); 703d2ad689dSVladimir Oltean } 704d2ad689dSVladimir Oltean 705d2ad689dSVladimir Oltean return new_budget; 7065a781ccbSVinicius Costa Gomes } 7075a781ccbSVinicius Costa Gomes 70892f96667SVladimir Oltean static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq, 70992f96667SVladimir Oltean struct sched_entry *entry, 71092f96667SVladimir Oltean u32 gate_mask) 71192f96667SVladimir Oltean { 71292f96667SVladimir Oltean struct taprio_sched *q = qdisc_priv(sch); 71392f96667SVladimir Oltean struct net_device *dev = qdisc_dev(sch); 71492f96667SVladimir Oltean struct Qdisc *child = q->qdiscs[txq]; 715d2ad689dSVladimir Oltean int num_tc = netdev_get_num_tc(dev); 71692f96667SVladimir Oltean struct sk_buff *skb; 71792f96667SVladimir Oltean ktime_t guard; 71892f96667SVladimir Oltean int prio; 71992f96667SVladimir Oltean int len; 72092f96667SVladimir Oltean u8 tc; 72192f96667SVladimir Oltean 72292f96667SVladimir Oltean if (unlikely(!child)) 72392f96667SVladimir Oltean return NULL; 72492f96667SVladimir Oltean 7254c229427SVladimir Oltean if (TXTIME_ASSIST_IS_ENABLED(q->flags)) 7264c229427SVladimir Oltean goto skip_peek_checks; 72792f96667SVladimir Oltean 72892f96667SVladimir Oltean skb = child->ops->peek(child); 72992f96667SVladimir Oltean if (!skb) 73092f96667SVladimir Oltean return NULL; 73192f96667SVladimir Oltean 73292f96667SVladimir Oltean prio = skb->priority; 73392f96667SVladimir Oltean tc = netdev_get_prio_tc_map(dev, prio); 73492f96667SVladimir Oltean 73592f96667SVladimir Oltean if (!(gate_mask & BIT(tc))) 73692f96667SVladimir Oltean return NULL; 73792f96667SVladimir Oltean 73892f96667SVladimir Oltean len = qdisc_pkt_len(skb); 73992f96667SVladimir Oltean guard = ktime_add_ns(taprio_get_time(q), length_to_duration(q, len)); 74092f96667SVladimir Oltean 74192f96667SVladimir Oltean /* In the case that there's no gate entry, there's no 74292f96667SVladimir Oltean * guard band ... 74392f96667SVladimir Oltean */ 74492f96667SVladimir Oltean if (gate_mask != TAPRIO_ALL_GATES_OPEN && 745a1e6ad30SVladimir Oltean !taprio_entry_allows_tx(guard, entry, tc)) 74692f96667SVladimir Oltean return NULL; 74792f96667SVladimir Oltean 74892f96667SVladimir Oltean /* ... and no budget. */ 74992f96667SVladimir Oltean if (gate_mask != TAPRIO_ALL_GATES_OPEN && 750d2ad689dSVladimir Oltean taprio_update_budgets(entry, len, tc, num_tc) < 0) 75192f96667SVladimir Oltean return NULL; 75292f96667SVladimir Oltean 7534c229427SVladimir Oltean skip_peek_checks: 75492f96667SVladimir Oltean skb = child->ops->dequeue(child); 75592f96667SVladimir Oltean if (unlikely(!skb)) 75692f96667SVladimir Oltean return NULL; 75792f96667SVladimir Oltean 75892f96667SVladimir Oltean qdisc_bstats_update(sch, skb); 75992f96667SVladimir Oltean qdisc_qstats_backlog_dec(sch, skb); 76092f96667SVladimir Oltean sch->q.qlen--; 76192f96667SVladimir Oltean 76292f96667SVladimir Oltean return skb; 76392f96667SVladimir Oltean } 76492f96667SVladimir Oltean 7652f530df7SVladimir Oltean static void taprio_next_tc_txq(struct net_device *dev, int tc, int *txq) 7662f530df7SVladimir Oltean { 7672f530df7SVladimir Oltean int offset = dev->tc_to_txq[tc].offset; 7682f530df7SVladimir Oltean int count = dev->tc_to_txq[tc].count; 7692f530df7SVladimir Oltean 7702f530df7SVladimir Oltean (*txq)++; 7712f530df7SVladimir Oltean if (*txq == offset + count) 7722f530df7SVladimir Oltean *txq = offset; 7732f530df7SVladimir Oltean } 7742f530df7SVladimir Oltean 7752f530df7SVladimir Oltean /* Prioritize higher traffic classes, and select among TXQs belonging to the 7762f530df7SVladimir Oltean * same TC using round robin 7772f530df7SVladimir Oltean */ 7782f530df7SVladimir Oltean static struct sk_buff *taprio_dequeue_tc_priority(struct Qdisc *sch, 7792f530df7SVladimir Oltean struct sched_entry *entry, 7802f530df7SVladimir Oltean u32 gate_mask) 7812f530df7SVladimir Oltean { 7822f530df7SVladimir Oltean struct taprio_sched *q = qdisc_priv(sch); 7832f530df7SVladimir Oltean struct net_device *dev = qdisc_dev(sch); 7842f530df7SVladimir Oltean int num_tc = netdev_get_num_tc(dev); 7852f530df7SVladimir Oltean struct sk_buff *skb; 7862f530df7SVladimir Oltean int tc; 7872f530df7SVladimir Oltean 7882f530df7SVladimir Oltean for (tc = num_tc - 1; tc >= 0; tc--) { 7892f530df7SVladimir Oltean int first_txq = q->cur_txq[tc]; 7902f530df7SVladimir Oltean 7912f530df7SVladimir Oltean if (!(gate_mask & BIT(tc))) 7922f530df7SVladimir Oltean continue; 7932f530df7SVladimir Oltean 7942f530df7SVladimir Oltean do { 7952f530df7SVladimir Oltean skb = taprio_dequeue_from_txq(sch, q->cur_txq[tc], 7962f530df7SVladimir Oltean entry, gate_mask); 7972f530df7SVladimir Oltean 7982f530df7SVladimir Oltean taprio_next_tc_txq(dev, tc, &q->cur_txq[tc]); 7992f530df7SVladimir Oltean 8002f530df7SVladimir Oltean if (skb) 8012f530df7SVladimir Oltean return skb; 8022f530df7SVladimir Oltean } while (q->cur_txq[tc] != first_txq); 8032f530df7SVladimir Oltean } 8042f530df7SVladimir Oltean 8052f530df7SVladimir Oltean return NULL; 8062f530df7SVladimir Oltean } 8072f530df7SVladimir Oltean 8082f530df7SVladimir Oltean /* Broken way of prioritizing smaller TXQ indices and ignoring the traffic 8092f530df7SVladimir Oltean * class other than to determine whether the gate is open or not 8102f530df7SVladimir Oltean */ 8112f530df7SVladimir Oltean static struct sk_buff *taprio_dequeue_txq_priority(struct Qdisc *sch, 8122f530df7SVladimir Oltean struct sched_entry *entry, 8132f530df7SVladimir Oltean u32 gate_mask) 8142f530df7SVladimir Oltean { 8152f530df7SVladimir Oltean struct net_device *dev = qdisc_dev(sch); 8162f530df7SVladimir Oltean struct sk_buff *skb; 8172f530df7SVladimir Oltean int i; 8182f530df7SVladimir Oltean 8192f530df7SVladimir Oltean for (i = 0; i < dev->num_tx_queues; i++) { 8202f530df7SVladimir Oltean skb = taprio_dequeue_from_txq(sch, i, entry, gate_mask); 8212f530df7SVladimir Oltean if (skb) 8222f530df7SVladimir Oltean return skb; 8232f530df7SVladimir Oltean } 8242f530df7SVladimir Oltean 8252f530df7SVladimir Oltean return NULL; 8262f530df7SVladimir Oltean } 8272f530df7SVladimir Oltean 8282c08a4f8SVladimir Oltean /* Will not be called in the full offload case, since the TX queues are 8292c08a4f8SVladimir Oltean * attached to the Qdisc created using qdisc_create_dflt() 8302c08a4f8SVladimir Oltean */ 83125becba6SVladimir Oltean static struct sk_buff *taprio_dequeue(struct Qdisc *sch) 8325a781ccbSVinicius Costa Gomes { 8335a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 8348c79f0eaSVinicius Costa Gomes struct sk_buff *skb = NULL; 8355a781ccbSVinicius Costa Gomes struct sched_entry *entry; 8365a781ccbSVinicius Costa Gomes u32 gate_mask; 8375a781ccbSVinicius Costa Gomes 8385a781ccbSVinicius Costa Gomes rcu_read_lock(); 8395a781ccbSVinicius Costa Gomes entry = rcu_dereference(q->current_entry); 8405a781ccbSVinicius Costa Gomes /* if there's no entry, it means that the schedule didn't 8415a781ccbSVinicius Costa Gomes * start yet, so force all gates to be open, this is in 8425a781ccbSVinicius Costa Gomes * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5 843633fa666SJesper Dangaard Brouer * "AdminGateStates" 8445a781ccbSVinicius Costa Gomes */ 8455a781ccbSVinicius Costa Gomes gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 8465a781ccbSVinicius Costa Gomes if (!gate_mask) 8478c79f0eaSVinicius Costa Gomes goto done; 8485a781ccbSVinicius Costa Gomes 8492f530df7SVladimir Oltean if (static_branch_unlikely(&taprio_have_broken_mqprio) && 8502f530df7SVladimir Oltean !static_branch_likely(&taprio_have_working_mqprio)) { 8512f530df7SVladimir Oltean /* Single NIC kind which is broken */ 8522f530df7SVladimir Oltean skb = taprio_dequeue_txq_priority(sch, entry, gate_mask); 8532f530df7SVladimir Oltean } else if (static_branch_likely(&taprio_have_working_mqprio) && 8542f530df7SVladimir Oltean !static_branch_unlikely(&taprio_have_broken_mqprio)) { 8552f530df7SVladimir Oltean /* Single NIC kind which prioritizes properly */ 8562f530df7SVladimir Oltean skb = taprio_dequeue_tc_priority(sch, entry, gate_mask); 8572f530df7SVladimir Oltean } else { 8582f530df7SVladimir Oltean /* Mixed NIC kinds present in system, need dynamic testing */ 8592f530df7SVladimir Oltean if (q->broken_mqprio) 8602f530df7SVladimir Oltean skb = taprio_dequeue_txq_priority(sch, entry, gate_mask); 8612f530df7SVladimir Oltean else 8622f530df7SVladimir Oltean skb = taprio_dequeue_tc_priority(sch, entry, gate_mask); 8635a781ccbSVinicius Costa Gomes } 8645a781ccbSVinicius Costa Gomes 8658c79f0eaSVinicius Costa Gomes done: 8668c79f0eaSVinicius Costa Gomes rcu_read_unlock(); 8678c79f0eaSVinicius Costa Gomes 8688c79f0eaSVinicius Costa Gomes return skb; 8695a781ccbSVinicius Costa Gomes } 8705a781ccbSVinicius Costa Gomes 8716ca6a665SVinicius Costa Gomes static bool should_restart_cycle(const struct sched_gate_list *oper, 8726ca6a665SVinicius Costa Gomes const struct sched_entry *entry) 8736ca6a665SVinicius Costa Gomes { 8746ca6a665SVinicius Costa Gomes if (list_is_last(&entry->list, &oper->entries)) 8756ca6a665SVinicius Costa Gomes return true; 8766ca6a665SVinicius Costa Gomes 877e5517551SVladimir Oltean if (ktime_compare(entry->end_time, oper->cycle_end_time) == 0) 8786ca6a665SVinicius Costa Gomes return true; 8796ca6a665SVinicius Costa Gomes 8806ca6a665SVinicius Costa Gomes return false; 8816ca6a665SVinicius Costa Gomes } 8826ca6a665SVinicius Costa Gomes 883a3d43c0dSVinicius Costa Gomes static bool should_change_schedules(const struct sched_gate_list *admin, 884a3d43c0dSVinicius Costa Gomes const struct sched_gate_list *oper, 885e5517551SVladimir Oltean ktime_t end_time) 886a3d43c0dSVinicius Costa Gomes { 887c25031e9SVinicius Costa Gomes ktime_t next_base_time, extension_time; 888a3d43c0dSVinicius Costa Gomes 889a3d43c0dSVinicius Costa Gomes if (!admin) 890a3d43c0dSVinicius Costa Gomes return false; 891a3d43c0dSVinicius Costa Gomes 892a3d43c0dSVinicius Costa Gomes next_base_time = sched_base_time(admin); 893a3d43c0dSVinicius Costa Gomes 894e5517551SVladimir Oltean /* This is the simple case, the end_time would fall after 895a3d43c0dSVinicius Costa Gomes * the next schedule base_time. 896a3d43c0dSVinicius Costa Gomes */ 897e5517551SVladimir Oltean if (ktime_compare(next_base_time, end_time) <= 0) 898a3d43c0dSVinicius Costa Gomes return true; 899a3d43c0dSVinicius Costa Gomes 900e5517551SVladimir Oltean /* This is the cycle_time_extension case, if the end_time 901c25031e9SVinicius Costa Gomes * plus the amount that can be extended would fall after the 902c25031e9SVinicius Costa Gomes * next schedule base_time, we can extend the current schedule 903c25031e9SVinicius Costa Gomes * for that amount. 904c25031e9SVinicius Costa Gomes */ 905e5517551SVladimir Oltean extension_time = ktime_add_ns(end_time, oper->cycle_time_extension); 906c25031e9SVinicius Costa Gomes 907c25031e9SVinicius Costa Gomes /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about 908c25031e9SVinicius Costa Gomes * how precisely the extension should be made. So after 909c25031e9SVinicius Costa Gomes * conformance testing, this logic may change. 910c25031e9SVinicius Costa Gomes */ 911c25031e9SVinicius Costa Gomes if (ktime_compare(next_base_time, extension_time) <= 0) 912c25031e9SVinicius Costa Gomes return true; 913c25031e9SVinicius Costa Gomes 914a3d43c0dSVinicius Costa Gomes return false; 915a3d43c0dSVinicius Costa Gomes } 916a3d43c0dSVinicius Costa Gomes 9175a781ccbSVinicius Costa Gomes static enum hrtimer_restart advance_sched(struct hrtimer *timer) 9185a781ccbSVinicius Costa Gomes { 9195a781ccbSVinicius Costa Gomes struct taprio_sched *q = container_of(timer, struct taprio_sched, 9205a781ccbSVinicius Costa Gomes advance_timer); 921a1e6ad30SVladimir Oltean struct net_device *dev = qdisc_dev(q->root); 922a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin; 923a1e6ad30SVladimir Oltean int num_tc = netdev_get_num_tc(dev); 9245a781ccbSVinicius Costa Gomes struct sched_entry *entry, *next; 9255a781ccbSVinicius Costa Gomes struct Qdisc *sch = q->root; 926e5517551SVladimir Oltean ktime_t end_time; 927a1e6ad30SVladimir Oltean int tc; 9285a781ccbSVinicius Costa Gomes 9295a781ccbSVinicius Costa Gomes spin_lock(&q->current_entry_lock); 9305a781ccbSVinicius Costa Gomes entry = rcu_dereference_protected(q->current_entry, 9315a781ccbSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 932a3d43c0dSVinicius Costa Gomes oper = rcu_dereference_protected(q->oper_sched, 933a3d43c0dSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 934a3d43c0dSVinicius Costa Gomes admin = rcu_dereference_protected(q->admin_sched, 935a3d43c0dSVinicius Costa Gomes lockdep_is_held(&q->current_entry_lock)); 9365a781ccbSVinicius Costa Gomes 937a3d43c0dSVinicius Costa Gomes if (!oper) 938a3d43c0dSVinicius Costa Gomes switch_schedules(q, &admin, &oper); 939a3d43c0dSVinicius Costa Gomes 940a3d43c0dSVinicius Costa Gomes /* This can happen in two cases: 1. this is the very first run 941a3d43c0dSVinicius Costa Gomes * of this function (i.e. we weren't running any schedule 942a3d43c0dSVinicius Costa Gomes * previously); 2. The previous schedule just ended. The first 943a3d43c0dSVinicius Costa Gomes * entry of all schedules are pre-calculated during the 944a3d43c0dSVinicius Costa Gomes * schedule initialization. 9455a781ccbSVinicius Costa Gomes */ 946e5517551SVladimir Oltean if (unlikely(!entry || entry->end_time == oper->base_time)) { 947a3d43c0dSVinicius Costa Gomes next = list_first_entry(&oper->entries, struct sched_entry, 9485a781ccbSVinicius Costa Gomes list); 949e5517551SVladimir Oltean end_time = next->end_time; 9505a781ccbSVinicius Costa Gomes goto first_run; 9515a781ccbSVinicius Costa Gomes } 9525a781ccbSVinicius Costa Gomes 9536ca6a665SVinicius Costa Gomes if (should_restart_cycle(oper, entry)) { 954a3d43c0dSVinicius Costa Gomes next = list_first_entry(&oper->entries, struct sched_entry, 9555a781ccbSVinicius Costa Gomes list); 956e5517551SVladimir Oltean oper->cycle_end_time = ktime_add_ns(oper->cycle_end_time, 9576ca6a665SVinicius Costa Gomes oper->cycle_time); 9586ca6a665SVinicius Costa Gomes } else { 9595a781ccbSVinicius Costa Gomes next = list_next_entry(entry, list); 9606ca6a665SVinicius Costa Gomes } 9615a781ccbSVinicius Costa Gomes 962e5517551SVladimir Oltean end_time = ktime_add_ns(entry->end_time, next->interval); 963e5517551SVladimir Oltean end_time = min_t(ktime_t, end_time, oper->cycle_end_time); 9645a781ccbSVinicius Costa Gomes 965a1e6ad30SVladimir Oltean for (tc = 0; tc < num_tc; tc++) { 966a1e6ad30SVladimir Oltean if (next->gate_duration[tc] == oper->cycle_time) 967a1e6ad30SVladimir Oltean next->gate_close_time[tc] = KTIME_MAX; 968a1e6ad30SVladimir Oltean else 969a1e6ad30SVladimir Oltean next->gate_close_time[tc] = ktime_add_ns(entry->end_time, 970a1e6ad30SVladimir Oltean next->gate_duration[tc]); 971a1e6ad30SVladimir Oltean } 972a1e6ad30SVladimir Oltean 973e5517551SVladimir Oltean if (should_change_schedules(admin, oper, end_time)) { 974a3d43c0dSVinicius Costa Gomes /* Set things so the next time this runs, the new 975a3d43c0dSVinicius Costa Gomes * schedule runs. 976a3d43c0dSVinicius Costa Gomes */ 977e5517551SVladimir Oltean end_time = sched_base_time(admin); 978a3d43c0dSVinicius Costa Gomes switch_schedules(q, &admin, &oper); 979a3d43c0dSVinicius Costa Gomes } 980a3d43c0dSVinicius Costa Gomes 981e5517551SVladimir Oltean next->end_time = end_time; 982d2ad689dSVladimir Oltean taprio_set_budgets(q, oper, next); 9835a781ccbSVinicius Costa Gomes 9845a781ccbSVinicius Costa Gomes first_run: 9855a781ccbSVinicius Costa Gomes rcu_assign_pointer(q->current_entry, next); 9865a781ccbSVinicius Costa Gomes spin_unlock(&q->current_entry_lock); 9875a781ccbSVinicius Costa Gomes 988e5517551SVladimir Oltean hrtimer_set_expires(&q->advance_timer, end_time); 9895a781ccbSVinicius Costa Gomes 9905a781ccbSVinicius Costa Gomes rcu_read_lock(); 9915a781ccbSVinicius Costa Gomes __netif_schedule(sch); 9925a781ccbSVinicius Costa Gomes rcu_read_unlock(); 9935a781ccbSVinicius Costa Gomes 9945a781ccbSVinicius Costa Gomes return HRTIMER_RESTART; 9955a781ccbSVinicius Costa Gomes } 9965a781ccbSVinicius Costa Gomes 9975a781ccbSVinicius Costa Gomes static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { 9985a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 }, 9995a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 }, 10005a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 }, 10015a781ccbSVinicius Costa Gomes [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 }, 10025a781ccbSVinicius Costa Gomes }; 10035a781ccbSVinicius Costa Gomes 1004a54fc09eSVladimir Oltean static const struct nla_policy taprio_tc_policy[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { 1005a54fc09eSVladimir Oltean [TCA_TAPRIO_TC_ENTRY_INDEX] = { .type = NLA_U32 }, 1006a54fc09eSVladimir Oltean [TCA_TAPRIO_TC_ENTRY_MAX_SDU] = { .type = NLA_U32 }, 1007a721c3e5SVladimir Oltean [TCA_TAPRIO_TC_ENTRY_FP] = NLA_POLICY_RANGE(NLA_U32, 1008a721c3e5SVladimir Oltean TC_FP_EXPRESS, 1009a721c3e5SVladimir Oltean TC_FP_PREEMPTIBLE), 1010a54fc09eSVladimir Oltean }; 1011a54fc09eSVladimir Oltean 10125a781ccbSVinicius Costa Gomes static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = { 10135a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_PRIOMAP] = { 10145a781ccbSVinicius Costa Gomes .len = sizeof(struct tc_mqprio_qopt) 10155a781ccbSVinicius Costa Gomes }, 10165a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED }, 10175a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 }, 10185a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED }, 10195a781ccbSVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 }, 10206ca6a665SVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 }, 1021c25031e9SVinicius Costa Gomes [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 }, 102249c684d7SVinicius Costa Gomes [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 }, 1023e13aaa06SJakub Kicinski [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 }, 1024a54fc09eSVladimir Oltean [TCA_TAPRIO_ATTR_TC_ENTRY] = { .type = NLA_NESTED }, 10255a781ccbSVinicius Costa Gomes }; 10265a781ccbSVinicius Costa Gomes 1027b5b73b26SVinicius Costa Gomes static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb, 1028b5b73b26SVinicius Costa Gomes struct sched_entry *entry, 10295a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 10305a781ccbSVinicius Costa Gomes { 1031b5b73b26SVinicius Costa Gomes int min_duration = length_to_duration(q, ETH_ZLEN); 10325a781ccbSVinicius Costa Gomes u32 interval = 0; 10335a781ccbSVinicius Costa Gomes 10345a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD]) 10355a781ccbSVinicius Costa Gomes entry->command = nla_get_u8( 10365a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_CMD]); 10375a781ccbSVinicius Costa Gomes 10385a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]) 10395a781ccbSVinicius Costa Gomes entry->gate_mask = nla_get_u32( 10405a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]); 10415a781ccbSVinicius Costa Gomes 10425a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]) 10435a781ccbSVinicius Costa Gomes interval = nla_get_u32( 10445a781ccbSVinicius Costa Gomes tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]); 10455a781ccbSVinicius Costa Gomes 1046b5b73b26SVinicius Costa Gomes /* The interval should allow at least the minimum ethernet 1047b5b73b26SVinicius Costa Gomes * frame to go out. 1048b5b73b26SVinicius Costa Gomes */ 1049b5b73b26SVinicius Costa Gomes if (interval < min_duration) { 10505a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry"); 10515a781ccbSVinicius Costa Gomes return -EINVAL; 10525a781ccbSVinicius Costa Gomes } 10535a781ccbSVinicius Costa Gomes 10545a781ccbSVinicius Costa Gomes entry->interval = interval; 10555a781ccbSVinicius Costa Gomes 10565a781ccbSVinicius Costa Gomes return 0; 10575a781ccbSVinicius Costa Gomes } 10585a781ccbSVinicius Costa Gomes 1059b5b73b26SVinicius Costa Gomes static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n, 1060b5b73b26SVinicius Costa Gomes struct sched_entry *entry, int index, 1061b5b73b26SVinicius Costa Gomes struct netlink_ext_ack *extack) 10625a781ccbSVinicius Costa Gomes { 10635a781ccbSVinicius Costa Gomes struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { }; 10645a781ccbSVinicius Costa Gomes int err; 10655a781ccbSVinicius Costa Gomes 10668cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n, 10675a781ccbSVinicius Costa Gomes entry_policy, NULL); 10685a781ccbSVinicius Costa Gomes if (err < 0) { 10695a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Could not parse nested entry"); 10705a781ccbSVinicius Costa Gomes return -EINVAL; 10715a781ccbSVinicius Costa Gomes } 10725a781ccbSVinicius Costa Gomes 10735a781ccbSVinicius Costa Gomes entry->index = index; 10745a781ccbSVinicius Costa Gomes 1075b5b73b26SVinicius Costa Gomes return fill_sched_entry(q, tb, entry, extack); 10765a781ccbSVinicius Costa Gomes } 10775a781ccbSVinicius Costa Gomes 1078b5b73b26SVinicius Costa Gomes static int parse_sched_list(struct taprio_sched *q, struct nlattr *list, 1079a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, 10805a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 10815a781ccbSVinicius Costa Gomes { 10825a781ccbSVinicius Costa Gomes struct nlattr *n; 10835a781ccbSVinicius Costa Gomes int err, rem; 10845a781ccbSVinicius Costa Gomes int i = 0; 10855a781ccbSVinicius Costa Gomes 10865a781ccbSVinicius Costa Gomes if (!list) 10875a781ccbSVinicius Costa Gomes return -EINVAL; 10885a781ccbSVinicius Costa Gomes 10895a781ccbSVinicius Costa Gomes nla_for_each_nested(n, list, rem) { 10905a781ccbSVinicius Costa Gomes struct sched_entry *entry; 10915a781ccbSVinicius Costa Gomes 10925a781ccbSVinicius Costa Gomes if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) { 10935a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'"); 10945a781ccbSVinicius Costa Gomes continue; 10955a781ccbSVinicius Costa Gomes } 10965a781ccbSVinicius Costa Gomes 10975a781ccbSVinicius Costa Gomes entry = kzalloc(sizeof(*entry), GFP_KERNEL); 10985a781ccbSVinicius Costa Gomes if (!entry) { 10995a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Not enough memory for entry"); 11005a781ccbSVinicius Costa Gomes return -ENOMEM; 11015a781ccbSVinicius Costa Gomes } 11025a781ccbSVinicius Costa Gomes 1103b5b73b26SVinicius Costa Gomes err = parse_sched_entry(q, n, entry, i, extack); 11045a781ccbSVinicius Costa Gomes if (err < 0) { 11055a781ccbSVinicius Costa Gomes kfree(entry); 11065a781ccbSVinicius Costa Gomes return err; 11075a781ccbSVinicius Costa Gomes } 11085a781ccbSVinicius Costa Gomes 1109a3d43c0dSVinicius Costa Gomes list_add_tail(&entry->list, &sched->entries); 11105a781ccbSVinicius Costa Gomes i++; 11115a781ccbSVinicius Costa Gomes } 11125a781ccbSVinicius Costa Gomes 1113a3d43c0dSVinicius Costa Gomes sched->num_entries = i; 11145a781ccbSVinicius Costa Gomes 11155a781ccbSVinicius Costa Gomes return i; 11165a781ccbSVinicius Costa Gomes } 11175a781ccbSVinicius Costa Gomes 1118b5b73b26SVinicius Costa Gomes static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb, 1119a3d43c0dSVinicius Costa Gomes struct sched_gate_list *new, 11205a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 11215a781ccbSVinicius Costa Gomes { 11225a781ccbSVinicius Costa Gomes int err = 0; 11235a781ccbSVinicius Costa Gomes 1124a3d43c0dSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) { 1125a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Adding a single entry is not supported"); 1126a3d43c0dSVinicius Costa Gomes return -ENOTSUPP; 1127a3d43c0dSVinicius Costa Gomes } 11285a781ccbSVinicius Costa Gomes 11295a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]) 1130a3d43c0dSVinicius Costa Gomes new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]); 11315a781ccbSVinicius Costa Gomes 1132c25031e9SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]) 1133c25031e9SVinicius Costa Gomes new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]); 1134c25031e9SVinicius Costa Gomes 11356ca6a665SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]) 11366ca6a665SVinicius Costa Gomes new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]); 11376ca6a665SVinicius Costa Gomes 11385a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]) 1139b5b73b26SVinicius Costa Gomes err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], 1140b5b73b26SVinicius Costa Gomes new, extack); 1141a3d43c0dSVinicius Costa Gomes if (err < 0) 11425a781ccbSVinicius Costa Gomes return err; 1143a3d43c0dSVinicius Costa Gomes 1144037be037SVedang Patel if (!new->cycle_time) { 1145037be037SVedang Patel struct sched_entry *entry; 1146037be037SVedang Patel ktime_t cycle = 0; 1147037be037SVedang Patel 1148037be037SVedang Patel list_for_each_entry(entry, &new->entries, list) 1149037be037SVedang Patel cycle = ktime_add_ns(cycle, entry->interval); 1150ed8157f1SDu Cheng 1151ed8157f1SDu Cheng if (!cycle) { 1152ed8157f1SDu Cheng NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0"); 1153ed8157f1SDu Cheng return -EINVAL; 1154ed8157f1SDu Cheng } 1155ed8157f1SDu Cheng 1156037be037SVedang Patel new->cycle_time = cycle; 1157037be037SVedang Patel } 1158037be037SVedang Patel 1159a306a90cSVladimir Oltean taprio_calculate_gate_durations(q, new); 1160a306a90cSVladimir Oltean 1161a3d43c0dSVinicius Costa Gomes return 0; 11625a781ccbSVinicius Costa Gomes } 11635a781ccbSVinicius Costa Gomes 11645a781ccbSVinicius Costa Gomes static int taprio_parse_mqprio_opt(struct net_device *dev, 11655a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt *qopt, 11664cfd5779SVedang Patel struct netlink_ext_ack *extack, 11674cfd5779SVedang Patel u32 taprio_flags) 11685a781ccbSVinicius Costa Gomes { 11691dfe086dSVladimir Oltean bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags); 11705a781ccbSVinicius Costa Gomes 1171a3d43c0dSVinicius Costa Gomes if (!qopt && !dev->num_tc) { 11725a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary"); 11735a781ccbSVinicius Costa Gomes return -EINVAL; 11745a781ccbSVinicius Costa Gomes } 11755a781ccbSVinicius Costa Gomes 1176a3d43c0dSVinicius Costa Gomes /* If num_tc is already set, it means that the user already 1177a3d43c0dSVinicius Costa Gomes * configured the mqprio part 1178a3d43c0dSVinicius Costa Gomes */ 1179a3d43c0dSVinicius Costa Gomes if (dev->num_tc) 1180a3d43c0dSVinicius Costa Gomes return 0; 1181a3d43c0dSVinicius Costa Gomes 11825a781ccbSVinicius Costa Gomes /* taprio imposes that traffic classes map 1:n to tx queues */ 11835a781ccbSVinicius Costa Gomes if (qopt->num_tc > dev->num_tx_queues) { 11845a781ccbSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues"); 11855a781ccbSVinicius Costa Gomes return -EINVAL; 11865a781ccbSVinicius Costa Gomes } 11875a781ccbSVinicius Costa Gomes 11881dfe086dSVladimir Oltean /* For some reason, in txtime-assist mode, we allow TXQ ranges for 11891dfe086dSVladimir Oltean * different TCs to overlap, and just validate the TXQ ranges. 11905a781ccbSVinicius Costa Gomes */ 11911dfe086dSVladimir Oltean return mqprio_validate_qopt(dev, qopt, true, allow_overlapping_txqs, 11921dfe086dSVladimir Oltean extack); 11935a781ccbSVinicius Costa Gomes } 11945a781ccbSVinicius Costa Gomes 1195a3d43c0dSVinicius Costa Gomes static int taprio_get_start_time(struct Qdisc *sch, 1196a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, 1197a3d43c0dSVinicius Costa Gomes ktime_t *start) 11985a781ccbSVinicius Costa Gomes { 11995a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 12005a781ccbSVinicius Costa Gomes ktime_t now, base, cycle; 12015a781ccbSVinicius Costa Gomes s64 n; 12025a781ccbSVinicius Costa Gomes 1203a3d43c0dSVinicius Costa Gomes base = sched_base_time(sched); 12047ede7b03SVedang Patel now = taprio_get_time(q); 12058599099fSAndre Guedes 12068599099fSAndre Guedes if (ktime_after(base, now)) { 12078599099fSAndre Guedes *start = base; 12088599099fSAndre Guedes return 0; 12098599099fSAndre Guedes } 12105a781ccbSVinicius Costa Gomes 1211037be037SVedang Patel cycle = sched->cycle_time; 12125a781ccbSVinicius Costa Gomes 12138599099fSAndre Guedes /* The qdisc is expected to have at least one sched_entry. Moreover, 12148599099fSAndre Guedes * any entry must have 'interval' > 0. Thus if the cycle time is zero, 12158599099fSAndre Guedes * something went really wrong. In that case, we should warn about this 12168599099fSAndre Guedes * inconsistent state and return error. 12178599099fSAndre Guedes */ 12188599099fSAndre Guedes if (WARN_ON(!cycle)) 12198599099fSAndre Guedes return -EFAULT; 12205a781ccbSVinicius Costa Gomes 12215a781ccbSVinicius Costa Gomes /* Schedule the start time for the beginning of the next 12225a781ccbSVinicius Costa Gomes * cycle. 12235a781ccbSVinicius Costa Gomes */ 12245a781ccbSVinicius Costa Gomes n = div64_s64(ktime_sub_ns(now, base), cycle); 12258599099fSAndre Guedes *start = ktime_add_ns(base, (n + 1) * cycle); 12268599099fSAndre Guedes return 0; 12275a781ccbSVinicius Costa Gomes } 12285a781ccbSVinicius Costa Gomes 1229e5517551SVladimir Oltean static void setup_first_end_time(struct taprio_sched *q, 1230a3d43c0dSVinicius Costa Gomes struct sched_gate_list *sched, ktime_t base) 12315a781ccbSVinicius Costa Gomes { 1232a1e6ad30SVladimir Oltean struct net_device *dev = qdisc_dev(q->root); 1233a1e6ad30SVladimir Oltean int num_tc = netdev_get_num_tc(dev); 12345a781ccbSVinicius Costa Gomes struct sched_entry *first; 12356ca6a665SVinicius Costa Gomes ktime_t cycle; 1236a1e6ad30SVladimir Oltean int tc; 12375a781ccbSVinicius Costa Gomes 1238a3d43c0dSVinicius Costa Gomes first = list_first_entry(&sched->entries, 1239a3d43c0dSVinicius Costa Gomes struct sched_entry, list); 12405a781ccbSVinicius Costa Gomes 1241037be037SVedang Patel cycle = sched->cycle_time; 12426ca6a665SVinicius Costa Gomes 12436ca6a665SVinicius Costa Gomes /* FIXME: find a better place to do this */ 1244e5517551SVladimir Oltean sched->cycle_end_time = ktime_add_ns(base, cycle); 12456ca6a665SVinicius Costa Gomes 1246e5517551SVladimir Oltean first->end_time = ktime_add_ns(base, first->interval); 1247d2ad689dSVladimir Oltean taprio_set_budgets(q, sched, first); 1248a1e6ad30SVladimir Oltean 1249a1e6ad30SVladimir Oltean for (tc = 0; tc < num_tc; tc++) { 1250a1e6ad30SVladimir Oltean if (first->gate_duration[tc] == sched->cycle_time) 1251a1e6ad30SVladimir Oltean first->gate_close_time[tc] = KTIME_MAX; 1252a1e6ad30SVladimir Oltean else 1253a1e6ad30SVladimir Oltean first->gate_close_time[tc] = ktime_add_ns(base, first->gate_duration[tc]); 1254a1e6ad30SVladimir Oltean } 1255a1e6ad30SVladimir Oltean 12565a781ccbSVinicius Costa Gomes rcu_assign_pointer(q->current_entry, NULL); 1257a3d43c0dSVinicius Costa Gomes } 12585a781ccbSVinicius Costa Gomes 1259a3d43c0dSVinicius Costa Gomes static void taprio_start_sched(struct Qdisc *sch, 1260a3d43c0dSVinicius Costa Gomes ktime_t start, struct sched_gate_list *new) 1261a3d43c0dSVinicius Costa Gomes { 1262a3d43c0dSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 1263a3d43c0dSVinicius Costa Gomes ktime_t expires; 1264a3d43c0dSVinicius Costa Gomes 12659c66d156SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 12669c66d156SVinicius Costa Gomes return; 12679c66d156SVinicius Costa Gomes 1268a3d43c0dSVinicius Costa Gomes expires = hrtimer_get_expires(&q->advance_timer); 1269a3d43c0dSVinicius Costa Gomes if (expires == 0) 1270a3d43c0dSVinicius Costa Gomes expires = KTIME_MAX; 1271a3d43c0dSVinicius Costa Gomes 1272a3d43c0dSVinicius Costa Gomes /* If the new schedule starts before the next expiration, we 1273a3d43c0dSVinicius Costa Gomes * reprogram it to the earliest one, so we change the admin 1274a3d43c0dSVinicius Costa Gomes * schedule to the operational one at the right time. 1275a3d43c0dSVinicius Costa Gomes */ 1276a3d43c0dSVinicius Costa Gomes start = min_t(ktime_t, start, expires); 12775a781ccbSVinicius Costa Gomes 12785a781ccbSVinicius Costa Gomes hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS); 12795a781ccbSVinicius Costa Gomes } 12805a781ccbSVinicius Costa Gomes 12817b9eba7bSLeandro Dorileo static void taprio_set_picos_per_byte(struct net_device *dev, 12827b9eba7bSLeandro Dorileo struct taprio_sched *q) 12837b9eba7bSLeandro Dorileo { 12847b9eba7bSLeandro Dorileo struct ethtool_link_ksettings ecmd; 1285f04b514cSVladimir Oltean int speed = SPEED_10; 1286f04b514cSVladimir Oltean int picos_per_byte; 1287f04b514cSVladimir Oltean int err; 12887b9eba7bSLeandro Dorileo 1289f04b514cSVladimir Oltean err = __ethtool_get_link_ksettings(dev, &ecmd); 1290f04b514cSVladimir Oltean if (err < 0) 1291f04b514cSVladimir Oltean goto skip; 1292f04b514cSVladimir Oltean 12939a9251a3SVladimir Oltean if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN) 1294f04b514cSVladimir Oltean speed = ecmd.base.speed; 1295f04b514cSVladimir Oltean 1296f04b514cSVladimir Oltean skip: 129768ce6688SVladimir Oltean picos_per_byte = (USEC_PER_SEC * 8) / speed; 12987b9eba7bSLeandro Dorileo 12997b9eba7bSLeandro Dorileo atomic64_set(&q->picos_per_byte, picos_per_byte); 13007b9eba7bSLeandro Dorileo netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n", 13017b9eba7bSLeandro Dorileo dev->name, (long long)atomic64_read(&q->picos_per_byte), 13027b9eba7bSLeandro Dorileo ecmd.base.speed); 13037b9eba7bSLeandro Dorileo } 13047b9eba7bSLeandro Dorileo 13057b9eba7bSLeandro Dorileo static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event, 13067b9eba7bSLeandro Dorileo void *ptr) 13077b9eba7bSLeandro Dorileo { 13087b9eba7bSLeandro Dorileo struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1309fed87cc6SVladimir Oltean struct sched_gate_list *oper, *admin; 1310fed87cc6SVladimir Oltean struct qdisc_size_table *stab; 13117b9eba7bSLeandro Dorileo struct taprio_sched *q; 13127b9eba7bSLeandro Dorileo 13137b9eba7bSLeandro Dorileo ASSERT_RTNL(); 13147b9eba7bSLeandro Dorileo 13157b9eba7bSLeandro Dorileo if (event != NETDEV_UP && event != NETDEV_CHANGE) 13167b9eba7bSLeandro Dorileo return NOTIFY_DONE; 13177b9eba7bSLeandro Dorileo 13187b9eba7bSLeandro Dorileo list_for_each_entry(q, &taprio_list, taprio_list) { 1319fc4f2fd0SVladimir Oltean if (dev != qdisc_dev(q->root)) 1320fc4f2fd0SVladimir Oltean continue; 1321fc4f2fd0SVladimir Oltean 1322fc4f2fd0SVladimir Oltean taprio_set_picos_per_byte(dev, q); 1323fed87cc6SVladimir Oltean 1324fed87cc6SVladimir Oltean stab = rtnl_dereference(q->root->stab); 1325fed87cc6SVladimir Oltean 1326fed87cc6SVladimir Oltean oper = rtnl_dereference(q->oper_sched); 1327fed87cc6SVladimir Oltean if (oper) 1328fed87cc6SVladimir Oltean taprio_update_queue_max_sdu(q, oper, stab); 1329fed87cc6SVladimir Oltean 1330fed87cc6SVladimir Oltean admin = rtnl_dereference(q->admin_sched); 1331fed87cc6SVladimir Oltean if (admin) 1332fed87cc6SVladimir Oltean taprio_update_queue_max_sdu(q, admin, stab); 1333fed87cc6SVladimir Oltean 13347b9eba7bSLeandro Dorileo break; 13357b9eba7bSLeandro Dorileo } 13367b9eba7bSLeandro Dorileo 13377b9eba7bSLeandro Dorileo return NOTIFY_DONE; 13387b9eba7bSLeandro Dorileo } 13397b9eba7bSLeandro Dorileo 13404cfd5779SVedang Patel static void setup_txtime(struct taprio_sched *q, 13414cfd5779SVedang Patel struct sched_gate_list *sched, ktime_t base) 13424cfd5779SVedang Patel { 13434cfd5779SVedang Patel struct sched_entry *entry; 13444cfd5779SVedang Patel u32 interval = 0; 13454cfd5779SVedang Patel 13464cfd5779SVedang Patel list_for_each_entry(entry, &sched->entries, list) { 13474cfd5779SVedang Patel entry->next_txtime = ktime_add_ns(base, interval); 13484cfd5779SVedang Patel interval += entry->interval; 13494cfd5779SVedang Patel } 13504cfd5779SVedang Patel } 13514cfd5779SVedang Patel 13529c66d156SVinicius Costa Gomes static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries) 13539c66d156SVinicius Costa Gomes { 13549c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload *__offload; 13559c66d156SVinicius Costa Gomes 135611a33de2SGustavo A. R. Silva __offload = kzalloc(struct_size(__offload, offload.entries, num_entries), 135711a33de2SGustavo A. R. Silva GFP_KERNEL); 13589c66d156SVinicius Costa Gomes if (!__offload) 13599c66d156SVinicius Costa Gomes return NULL; 13609c66d156SVinicius Costa Gomes 13619c66d156SVinicius Costa Gomes refcount_set(&__offload->users, 1); 13629c66d156SVinicius Costa Gomes 13639c66d156SVinicius Costa Gomes return &__offload->offload; 13649c66d156SVinicius Costa Gomes } 13659c66d156SVinicius Costa Gomes 13669c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload 13679c66d156SVinicius Costa Gomes *offload) 13689c66d156SVinicius Costa Gomes { 13699c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload *__offload; 13709c66d156SVinicius Costa Gomes 13719c66d156SVinicius Costa Gomes __offload = container_of(offload, struct __tc_taprio_qopt_offload, 13729c66d156SVinicius Costa Gomes offload); 13739c66d156SVinicius Costa Gomes 13749c66d156SVinicius Costa Gomes refcount_inc(&__offload->users); 13759c66d156SVinicius Costa Gomes 13769c66d156SVinicius Costa Gomes return offload; 13779c66d156SVinicius Costa Gomes } 13789c66d156SVinicius Costa Gomes EXPORT_SYMBOL_GPL(taprio_offload_get); 13799c66d156SVinicius Costa Gomes 13809c66d156SVinicius Costa Gomes void taprio_offload_free(struct tc_taprio_qopt_offload *offload) 13819c66d156SVinicius Costa Gomes { 13829c66d156SVinicius Costa Gomes struct __tc_taprio_qopt_offload *__offload; 13839c66d156SVinicius Costa Gomes 13849c66d156SVinicius Costa Gomes __offload = container_of(offload, struct __tc_taprio_qopt_offload, 13859c66d156SVinicius Costa Gomes offload); 13869c66d156SVinicius Costa Gomes 13879c66d156SVinicius Costa Gomes if (!refcount_dec_and_test(&__offload->users)) 13889c66d156SVinicius Costa Gomes return; 13899c66d156SVinicius Costa Gomes 13909c66d156SVinicius Costa Gomes kfree(__offload); 13919c66d156SVinicius Costa Gomes } 13929c66d156SVinicius Costa Gomes EXPORT_SYMBOL_GPL(taprio_offload_free); 13939c66d156SVinicius Costa Gomes 13949c66d156SVinicius Costa Gomes /* The function will only serve to keep the pointers to the "oper" and "admin" 13959c66d156SVinicius Costa Gomes * schedules valid in relation to their base times, so when calling dump() the 13969c66d156SVinicius Costa Gomes * users looks at the right schedules. 13979c66d156SVinicius Costa Gomes * When using full offload, the admin configuration is promoted to oper at the 13989c66d156SVinicius Costa Gomes * base_time in the PHC time domain. But because the system time is not 13999c66d156SVinicius Costa Gomes * necessarily in sync with that, we can't just trigger a hrtimer to call 14009c66d156SVinicius Costa Gomes * switch_schedules at the right hardware time. 14019c66d156SVinicius Costa Gomes * At the moment we call this by hand right away from taprio, but in the future 14029c66d156SVinicius Costa Gomes * it will be useful to create a mechanism for drivers to notify taprio of the 14039c66d156SVinicius Costa Gomes * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump(). 14049c66d156SVinicius Costa Gomes * This is left as TODO. 14059c66d156SVinicius Costa Gomes */ 1406d665c128SYi Wang static void taprio_offload_config_changed(struct taprio_sched *q) 14079c66d156SVinicius Costa Gomes { 14089c66d156SVinicius Costa Gomes struct sched_gate_list *oper, *admin; 14099c66d156SVinicius Costa Gomes 1410c8cbe123SVladimir Oltean oper = rtnl_dereference(q->oper_sched); 1411c8cbe123SVladimir Oltean admin = rtnl_dereference(q->admin_sched); 14129c66d156SVinicius Costa Gomes 14139c66d156SVinicius Costa Gomes switch_schedules(q, &admin, &oper); 14149c66d156SVinicius Costa Gomes } 14159c66d156SVinicius Costa Gomes 141609e31cf0SVinicius Costa Gomes static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask) 141709e31cf0SVinicius Costa Gomes { 141809e31cf0SVinicius Costa Gomes u32 i, queue_mask = 0; 141909e31cf0SVinicius Costa Gomes 142009e31cf0SVinicius Costa Gomes for (i = 0; i < dev->num_tc; i++) { 142109e31cf0SVinicius Costa Gomes u32 offset, count; 142209e31cf0SVinicius Costa Gomes 142309e31cf0SVinicius Costa Gomes if (!(tc_mask & BIT(i))) 142409e31cf0SVinicius Costa Gomes continue; 142509e31cf0SVinicius Costa Gomes 142609e31cf0SVinicius Costa Gomes offset = dev->tc_to_txq[i].offset; 142709e31cf0SVinicius Costa Gomes count = dev->tc_to_txq[i].count; 142809e31cf0SVinicius Costa Gomes 142909e31cf0SVinicius Costa Gomes queue_mask |= GENMASK(offset + count - 1, offset); 143009e31cf0SVinicius Costa Gomes } 143109e31cf0SVinicius Costa Gomes 143209e31cf0SVinicius Costa Gomes return queue_mask; 143309e31cf0SVinicius Costa Gomes } 143409e31cf0SVinicius Costa Gomes 143509e31cf0SVinicius Costa Gomes static void taprio_sched_to_offload(struct net_device *dev, 14369c66d156SVinicius Costa Gomes struct sched_gate_list *sched, 1437522d15eaSVladimir Oltean struct tc_taprio_qopt_offload *offload, 1438522d15eaSVladimir Oltean const struct tc_taprio_caps *caps) 14399c66d156SVinicius Costa Gomes { 14409c66d156SVinicius Costa Gomes struct sched_entry *entry; 14419c66d156SVinicius Costa Gomes int i = 0; 14429c66d156SVinicius Costa Gomes 14439c66d156SVinicius Costa Gomes offload->base_time = sched->base_time; 14449c66d156SVinicius Costa Gomes offload->cycle_time = sched->cycle_time; 14459c66d156SVinicius Costa Gomes offload->cycle_time_extension = sched->cycle_time_extension; 14469c66d156SVinicius Costa Gomes 14479c66d156SVinicius Costa Gomes list_for_each_entry(entry, &sched->entries, list) { 14489c66d156SVinicius Costa Gomes struct tc_taprio_sched_entry *e = &offload->entries[i]; 14499c66d156SVinicius Costa Gomes 14509c66d156SVinicius Costa Gomes e->command = entry->command; 14519c66d156SVinicius Costa Gomes e->interval = entry->interval; 1452522d15eaSVladimir Oltean if (caps->gate_mask_per_txq) 1453522d15eaSVladimir Oltean e->gate_mask = tc_map_to_queue_mask(dev, 1454522d15eaSVladimir Oltean entry->gate_mask); 1455522d15eaSVladimir Oltean else 1456522d15eaSVladimir Oltean e->gate_mask = entry->gate_mask; 145709e31cf0SVinicius Costa Gomes 14589c66d156SVinicius Costa Gomes i++; 14599c66d156SVinicius Costa Gomes } 14609c66d156SVinicius Costa Gomes 14619c66d156SVinicius Costa Gomes offload->num_entries = i; 14629c66d156SVinicius Costa Gomes } 14639c66d156SVinicius Costa Gomes 14642f530df7SVladimir Oltean static void taprio_detect_broken_mqprio(struct taprio_sched *q) 14652f530df7SVladimir Oltean { 14662f530df7SVladimir Oltean struct net_device *dev = qdisc_dev(q->root); 14672f530df7SVladimir Oltean struct tc_taprio_caps caps; 14682f530df7SVladimir Oltean 14692f530df7SVladimir Oltean qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO, 14702f530df7SVladimir Oltean &caps, sizeof(caps)); 14712f530df7SVladimir Oltean 14722f530df7SVladimir Oltean q->broken_mqprio = caps.broken_mqprio; 14732f530df7SVladimir Oltean if (q->broken_mqprio) 14742f530df7SVladimir Oltean static_branch_inc(&taprio_have_broken_mqprio); 14752f530df7SVladimir Oltean else 14762f530df7SVladimir Oltean static_branch_inc(&taprio_have_working_mqprio); 14772f530df7SVladimir Oltean 14782f530df7SVladimir Oltean q->detected_mqprio = true; 14792f530df7SVladimir Oltean } 14802f530df7SVladimir Oltean 14812f530df7SVladimir Oltean static void taprio_cleanup_broken_mqprio(struct taprio_sched *q) 14822f530df7SVladimir Oltean { 14832f530df7SVladimir Oltean if (!q->detected_mqprio) 14842f530df7SVladimir Oltean return; 14852f530df7SVladimir Oltean 14862f530df7SVladimir Oltean if (q->broken_mqprio) 14872f530df7SVladimir Oltean static_branch_dec(&taprio_have_broken_mqprio); 14882f530df7SVladimir Oltean else 14892f530df7SVladimir Oltean static_branch_dec(&taprio_have_working_mqprio); 14902f530df7SVladimir Oltean } 14912f530df7SVladimir Oltean 14929c66d156SVinicius Costa Gomes static int taprio_enable_offload(struct net_device *dev, 14939c66d156SVinicius Costa Gomes struct taprio_sched *q, 14949c66d156SVinicius Costa Gomes struct sched_gate_list *sched, 14959c66d156SVinicius Costa Gomes struct netlink_ext_ack *extack) 14969c66d156SVinicius Costa Gomes { 14979c66d156SVinicius Costa Gomes const struct net_device_ops *ops = dev->netdev_ops; 14989c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *offload; 1499a54fc09eSVladimir Oltean struct tc_taprio_caps caps; 1500a54fc09eSVladimir Oltean int tc, err = 0; 15019c66d156SVinicius Costa Gomes 15029c66d156SVinicius Costa Gomes if (!ops->ndo_setup_tc) { 15039c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 15049c66d156SVinicius Costa Gomes "Device does not support taprio offload"); 15059c66d156SVinicius Costa Gomes return -EOPNOTSUPP; 15069c66d156SVinicius Costa Gomes } 15079c66d156SVinicius Costa Gomes 1508a54fc09eSVladimir Oltean qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO, 1509a54fc09eSVladimir Oltean &caps, sizeof(caps)); 1510a54fc09eSVladimir Oltean 1511a54fc09eSVladimir Oltean if (!caps.supports_queue_max_sdu) { 1512a54fc09eSVladimir Oltean for (tc = 0; tc < TC_MAX_QUEUE; tc++) { 1513a54fc09eSVladimir Oltean if (q->max_sdu[tc]) { 1514a54fc09eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1515a54fc09eSVladimir Oltean "Device does not handle queueMaxSDU"); 1516a54fc09eSVladimir Oltean return -EOPNOTSUPP; 1517a54fc09eSVladimir Oltean } 1518a54fc09eSVladimir Oltean } 1519a54fc09eSVladimir Oltean } 1520a54fc09eSVladimir Oltean 15219c66d156SVinicius Costa Gomes offload = taprio_offload_alloc(sched->num_entries); 15229c66d156SVinicius Costa Gomes if (!offload) { 15239c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 15249c66d156SVinicius Costa Gomes "Not enough memory for enabling offload mode"); 15259c66d156SVinicius Costa Gomes return -ENOMEM; 15269c66d156SVinicius Costa Gomes } 1527*2d800bc5SVladimir Oltean offload->cmd = TAPRIO_CMD_REPLACE; 1528c54876cdSVladimir Oltean offload->extack = extack; 152909c794c0SVladimir Oltean mqprio_qopt_reconstruct(dev, &offload->mqprio.qopt); 1530c54876cdSVladimir Oltean offload->mqprio.extack = extack; 1531522d15eaSVladimir Oltean taprio_sched_to_offload(dev, sched, offload, &caps); 1532a721c3e5SVladimir Oltean mqprio_fp_to_offload(q->fp, &offload->mqprio); 15339c66d156SVinicius Costa Gomes 1534a54fc09eSVladimir Oltean for (tc = 0; tc < TC_MAX_QUEUE; tc++) 1535a54fc09eSVladimir Oltean offload->max_sdu[tc] = q->max_sdu[tc]; 1536a54fc09eSVladimir Oltean 15379c66d156SVinicius Costa Gomes err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 15389c66d156SVinicius Costa Gomes if (err < 0) { 1539c54876cdSVladimir Oltean NL_SET_ERR_MSG_WEAK(extack, 15409c66d156SVinicius Costa Gomes "Device failed to setup taprio offload"); 15419c66d156SVinicius Costa Gomes goto done; 15429c66d156SVinicius Costa Gomes } 15439c66d156SVinicius Costa Gomes 1544db46e3a8SVladimir Oltean q->offloaded = true; 1545db46e3a8SVladimir Oltean 15469c66d156SVinicius Costa Gomes done: 1547c54876cdSVladimir Oltean /* The offload structure may linger around via a reference taken by the 1548c54876cdSVladimir Oltean * device driver, so clear up the netlink extack pointer so that the 1549c54876cdSVladimir Oltean * driver isn't tempted to dereference data which stopped being valid 1550c54876cdSVladimir Oltean */ 1551c54876cdSVladimir Oltean offload->extack = NULL; 1552c54876cdSVladimir Oltean offload->mqprio.extack = NULL; 15539c66d156SVinicius Costa Gomes taprio_offload_free(offload); 15549c66d156SVinicius Costa Gomes 15559c66d156SVinicius Costa Gomes return err; 15569c66d156SVinicius Costa Gomes } 15579c66d156SVinicius Costa Gomes 15589c66d156SVinicius Costa Gomes static int taprio_disable_offload(struct net_device *dev, 15599c66d156SVinicius Costa Gomes struct taprio_sched *q, 15609c66d156SVinicius Costa Gomes struct netlink_ext_ack *extack) 15619c66d156SVinicius Costa Gomes { 15629c66d156SVinicius Costa Gomes const struct net_device_ops *ops = dev->netdev_ops; 15639c66d156SVinicius Costa Gomes struct tc_taprio_qopt_offload *offload; 15649c66d156SVinicius Costa Gomes int err; 15659c66d156SVinicius Costa Gomes 1566db46e3a8SVladimir Oltean if (!q->offloaded) 15679c66d156SVinicius Costa Gomes return 0; 15689c66d156SVinicius Costa Gomes 15699c66d156SVinicius Costa Gomes offload = taprio_offload_alloc(0); 15709c66d156SVinicius Costa Gomes if (!offload) { 15719c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 15729c66d156SVinicius Costa Gomes "Not enough memory to disable offload mode"); 15739c66d156SVinicius Costa Gomes return -ENOMEM; 15749c66d156SVinicius Costa Gomes } 1575*2d800bc5SVladimir Oltean offload->cmd = TAPRIO_CMD_DESTROY; 15769c66d156SVinicius Costa Gomes 15779c66d156SVinicius Costa Gomes err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 15789c66d156SVinicius Costa Gomes if (err < 0) { 15799c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 15809c66d156SVinicius Costa Gomes "Device failed to disable offload"); 15819c66d156SVinicius Costa Gomes goto out; 15829c66d156SVinicius Costa Gomes } 15839c66d156SVinicius Costa Gomes 1584db46e3a8SVladimir Oltean q->offloaded = false; 1585db46e3a8SVladimir Oltean 15869c66d156SVinicius Costa Gomes out: 15879c66d156SVinicius Costa Gomes taprio_offload_free(offload); 15889c66d156SVinicius Costa Gomes 15899c66d156SVinicius Costa Gomes return err; 15909c66d156SVinicius Costa Gomes } 15919c66d156SVinicius Costa Gomes 15929c66d156SVinicius Costa Gomes /* If full offload is enabled, the only possible clockid is the net device's 15939c66d156SVinicius Costa Gomes * PHC. For that reason, specifying a clockid through netlink is incorrect. 15949c66d156SVinicius Costa Gomes * For txtime-assist, it is implicitly assumed that the device's PHC is kept 15959c66d156SVinicius Costa Gomes * in sync with the specified clockid via a user space daemon such as phc2sys. 15969c66d156SVinicius Costa Gomes * For both software taprio and txtime-assist, the clockid is used for the 15979c66d156SVinicius Costa Gomes * hrtimer that advances the schedule and hence mandatory. 15989c66d156SVinicius Costa Gomes */ 15999c66d156SVinicius Costa Gomes static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb, 16009c66d156SVinicius Costa Gomes struct netlink_ext_ack *extack) 16019c66d156SVinicius Costa Gomes { 16029c66d156SVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 16039c66d156SVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 16049c66d156SVinicius Costa Gomes int err = -EINVAL; 16059c66d156SVinicius Costa Gomes 16069c66d156SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 16079c66d156SVinicius Costa Gomes const struct ethtool_ops *ops = dev->ethtool_ops; 16089c66d156SVinicius Costa Gomes struct ethtool_ts_info info = { 16099c66d156SVinicius Costa Gomes .cmd = ETHTOOL_GET_TS_INFO, 16109c66d156SVinicius Costa Gomes .phc_index = -1, 16119c66d156SVinicius Costa Gomes }; 16129c66d156SVinicius Costa Gomes 16139c66d156SVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 16149c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 16159c66d156SVinicius Costa Gomes "The 'clockid' cannot be specified for full offload"); 16169c66d156SVinicius Costa Gomes goto out; 16179c66d156SVinicius Costa Gomes } 16189c66d156SVinicius Costa Gomes 16199c66d156SVinicius Costa Gomes if (ops && ops->get_ts_info) 16209c66d156SVinicius Costa Gomes err = ops->get_ts_info(dev, &info); 16219c66d156SVinicius Costa Gomes 16229c66d156SVinicius Costa Gomes if (err || info.phc_index < 0) { 16239c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 16249c66d156SVinicius Costa Gomes "Device does not have a PTP clock"); 16259c66d156SVinicius Costa Gomes err = -ENOTSUPP; 16269c66d156SVinicius Costa Gomes goto out; 16279c66d156SVinicius Costa Gomes } 16289c66d156SVinicius Costa Gomes } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 16299c66d156SVinicius Costa Gomes int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]); 16306dc25401SEric Dumazet enum tk_offsets tk_offset; 16319c66d156SVinicius Costa Gomes 16329c66d156SVinicius Costa Gomes /* We only support static clockids and we don't allow 16339c66d156SVinicius Costa Gomes * for it to be modified after the first init. 16349c66d156SVinicius Costa Gomes */ 16359c66d156SVinicius Costa Gomes if (clockid < 0 || 16369c66d156SVinicius Costa Gomes (q->clockid != -1 && q->clockid != clockid)) { 16379c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, 16389c66d156SVinicius Costa Gomes "Changing the 'clockid' of a running schedule is not supported"); 16399c66d156SVinicius Costa Gomes err = -ENOTSUPP; 16409c66d156SVinicius Costa Gomes goto out; 16419c66d156SVinicius Costa Gomes } 16429c66d156SVinicius Costa Gomes 16439c66d156SVinicius Costa Gomes switch (clockid) { 16449c66d156SVinicius Costa Gomes case CLOCK_REALTIME: 16456dc25401SEric Dumazet tk_offset = TK_OFFS_REAL; 16469c66d156SVinicius Costa Gomes break; 16479c66d156SVinicius Costa Gomes case CLOCK_MONOTONIC: 16486dc25401SEric Dumazet tk_offset = TK_OFFS_MAX; 16499c66d156SVinicius Costa Gomes break; 16509c66d156SVinicius Costa Gomes case CLOCK_BOOTTIME: 16516dc25401SEric Dumazet tk_offset = TK_OFFS_BOOT; 16529c66d156SVinicius Costa Gomes break; 16539c66d156SVinicius Costa Gomes case CLOCK_TAI: 16546dc25401SEric Dumazet tk_offset = TK_OFFS_TAI; 16559c66d156SVinicius Costa Gomes break; 16569c66d156SVinicius Costa Gomes default: 16579c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Invalid 'clockid'"); 16589c66d156SVinicius Costa Gomes err = -EINVAL; 16599c66d156SVinicius Costa Gomes goto out; 16609c66d156SVinicius Costa Gomes } 16616dc25401SEric Dumazet /* This pairs with READ_ONCE() in taprio_mono_to_any */ 16626dc25401SEric Dumazet WRITE_ONCE(q->tk_offset, tk_offset); 16639c66d156SVinicius Costa Gomes 16649c66d156SVinicius Costa Gomes q->clockid = clockid; 16659c66d156SVinicius Costa Gomes } else { 16669c66d156SVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory"); 16679c66d156SVinicius Costa Gomes goto out; 16689c66d156SVinicius Costa Gomes } 1669a954380aSVinicius Costa Gomes 1670a954380aSVinicius Costa Gomes /* Everything went ok, return success. */ 1671a954380aSVinicius Costa Gomes err = 0; 1672a954380aSVinicius Costa Gomes 16739c66d156SVinicius Costa Gomes out: 16749c66d156SVinicius Costa Gomes return err; 16759c66d156SVinicius Costa Gomes } 16769c66d156SVinicius Costa Gomes 1677a54fc09eSVladimir Oltean static int taprio_parse_tc_entry(struct Qdisc *sch, 1678a54fc09eSVladimir Oltean struct nlattr *opt, 1679a54fc09eSVladimir Oltean u32 max_sdu[TC_QOPT_MAX_QUEUE], 1680a721c3e5SVladimir Oltean u32 fp[TC_QOPT_MAX_QUEUE], 1681a54fc09eSVladimir Oltean unsigned long *seen_tcs, 1682a54fc09eSVladimir Oltean struct netlink_ext_ack *extack) 1683a54fc09eSVladimir Oltean { 1684a54fc09eSVladimir Oltean struct nlattr *tb[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { }; 1685a54fc09eSVladimir Oltean struct net_device *dev = qdisc_dev(sch); 1686a54fc09eSVladimir Oltean int err, tc; 1687a721c3e5SVladimir Oltean u32 val; 1688a54fc09eSVladimir Oltean 1689a54fc09eSVladimir Oltean err = nla_parse_nested(tb, TCA_TAPRIO_TC_ENTRY_MAX, opt, 1690a54fc09eSVladimir Oltean taprio_tc_policy, extack); 1691a54fc09eSVladimir Oltean if (err < 0) 1692a54fc09eSVladimir Oltean return err; 1693a54fc09eSVladimir Oltean 1694a54fc09eSVladimir Oltean if (!tb[TCA_TAPRIO_TC_ENTRY_INDEX]) { 1695a54fc09eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "TC entry index missing"); 1696a54fc09eSVladimir Oltean return -EINVAL; 1697a54fc09eSVladimir Oltean } 1698a54fc09eSVladimir Oltean 1699a54fc09eSVladimir Oltean tc = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_INDEX]); 1700a54fc09eSVladimir Oltean if (tc >= TC_QOPT_MAX_QUEUE) { 1701a54fc09eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "TC entry index out of range"); 1702a54fc09eSVladimir Oltean return -ERANGE; 1703a54fc09eSVladimir Oltean } 1704a54fc09eSVladimir Oltean 1705a54fc09eSVladimir Oltean if (*seen_tcs & BIT(tc)) { 1706a54fc09eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Duplicate TC entry"); 1707a54fc09eSVladimir Oltean return -EINVAL; 1708a54fc09eSVladimir Oltean } 1709a54fc09eSVladimir Oltean 1710a54fc09eSVladimir Oltean *seen_tcs |= BIT(tc); 1711a54fc09eSVladimir Oltean 1712a721c3e5SVladimir Oltean if (tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]) { 1713a54fc09eSVladimir Oltean val = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]); 1714a54fc09eSVladimir Oltean if (val > dev->max_mtu) { 1715a54fc09eSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "TC max SDU exceeds device max MTU"); 1716a54fc09eSVladimir Oltean return -ERANGE; 1717a54fc09eSVladimir Oltean } 1718a54fc09eSVladimir Oltean 1719a54fc09eSVladimir Oltean max_sdu[tc] = val; 1720a721c3e5SVladimir Oltean } 1721a721c3e5SVladimir Oltean 1722a721c3e5SVladimir Oltean if (tb[TCA_TAPRIO_TC_ENTRY_FP]) 1723a721c3e5SVladimir Oltean fp[tc] = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_FP]); 1724a54fc09eSVladimir Oltean 1725a54fc09eSVladimir Oltean return 0; 1726a54fc09eSVladimir Oltean } 1727a54fc09eSVladimir Oltean 1728a54fc09eSVladimir Oltean static int taprio_parse_tc_entries(struct Qdisc *sch, 1729a54fc09eSVladimir Oltean struct nlattr *opt, 1730a54fc09eSVladimir Oltean struct netlink_ext_ack *extack) 1731a54fc09eSVladimir Oltean { 1732a54fc09eSVladimir Oltean struct taprio_sched *q = qdisc_priv(sch); 1733a721c3e5SVladimir Oltean struct net_device *dev = qdisc_dev(sch); 1734a54fc09eSVladimir Oltean u32 max_sdu[TC_QOPT_MAX_QUEUE]; 1735a721c3e5SVladimir Oltean bool have_preemption = false; 1736a54fc09eSVladimir Oltean unsigned long seen_tcs = 0; 1737a721c3e5SVladimir Oltean u32 fp[TC_QOPT_MAX_QUEUE]; 1738a54fc09eSVladimir Oltean struct nlattr *n; 1739a54fc09eSVladimir Oltean int tc, rem; 1740a54fc09eSVladimir Oltean int err = 0; 1741a54fc09eSVladimir Oltean 1742a721c3e5SVladimir Oltean for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) { 1743a54fc09eSVladimir Oltean max_sdu[tc] = q->max_sdu[tc]; 1744a721c3e5SVladimir Oltean fp[tc] = q->fp[tc]; 1745a721c3e5SVladimir Oltean } 1746a54fc09eSVladimir Oltean 1747a54fc09eSVladimir Oltean nla_for_each_nested(n, opt, rem) { 1748a54fc09eSVladimir Oltean if (nla_type(n) != TCA_TAPRIO_ATTR_TC_ENTRY) 1749a54fc09eSVladimir Oltean continue; 1750a54fc09eSVladimir Oltean 1751a721c3e5SVladimir Oltean err = taprio_parse_tc_entry(sch, n, max_sdu, fp, &seen_tcs, 1752fed87cc6SVladimir Oltean extack); 1753a54fc09eSVladimir Oltean if (err) 1754a721c3e5SVladimir Oltean return err; 1755a54fc09eSVladimir Oltean } 1756a54fc09eSVladimir Oltean 1757a721c3e5SVladimir Oltean for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) { 1758a54fc09eSVladimir Oltean q->max_sdu[tc] = max_sdu[tc]; 1759a721c3e5SVladimir Oltean q->fp[tc] = fp[tc]; 1760a721c3e5SVladimir Oltean if (fp[tc] != TC_FP_EXPRESS) 1761a721c3e5SVladimir Oltean have_preemption = true; 1762a721c3e5SVladimir Oltean } 1763a54fc09eSVladimir Oltean 1764a721c3e5SVladimir Oltean if (have_preemption) { 1765a721c3e5SVladimir Oltean if (!FULL_OFFLOAD_IS_ENABLED(q->flags)) { 1766a721c3e5SVladimir Oltean NL_SET_ERR_MSG(extack, 1767a721c3e5SVladimir Oltean "Preemption only supported with full offload"); 1768a721c3e5SVladimir Oltean return -EOPNOTSUPP; 1769a721c3e5SVladimir Oltean } 1770a721c3e5SVladimir Oltean 1771a721c3e5SVladimir Oltean if (!ethtool_dev_mm_supported(dev)) { 1772a721c3e5SVladimir Oltean NL_SET_ERR_MSG(extack, 1773a721c3e5SVladimir Oltean "Device does not support preemption"); 1774a721c3e5SVladimir Oltean return -EOPNOTSUPP; 1775a721c3e5SVladimir Oltean } 1776a721c3e5SVladimir Oltean } 1777a721c3e5SVladimir Oltean 1778a54fc09eSVladimir Oltean return err; 1779a54fc09eSVladimir Oltean } 1780a54fc09eSVladimir Oltean 1781b5a0faa3SIvan Khoronzhuk static int taprio_mqprio_cmp(const struct net_device *dev, 1782b5a0faa3SIvan Khoronzhuk const struct tc_mqprio_qopt *mqprio) 1783b5a0faa3SIvan Khoronzhuk { 1784b5a0faa3SIvan Khoronzhuk int i; 1785b5a0faa3SIvan Khoronzhuk 1786b5a0faa3SIvan Khoronzhuk if (!mqprio || mqprio->num_tc != dev->num_tc) 1787b5a0faa3SIvan Khoronzhuk return -1; 1788b5a0faa3SIvan Khoronzhuk 1789b5a0faa3SIvan Khoronzhuk for (i = 0; i < mqprio->num_tc; i++) 1790b5a0faa3SIvan Khoronzhuk if (dev->tc_to_txq[i].count != mqprio->count[i] || 1791b5a0faa3SIvan Khoronzhuk dev->tc_to_txq[i].offset != mqprio->offset[i]) 1792b5a0faa3SIvan Khoronzhuk return -1; 1793b5a0faa3SIvan Khoronzhuk 1794b5a0faa3SIvan Khoronzhuk for (i = 0; i <= TC_BITMASK; i++) 1795b5a0faa3SIvan Khoronzhuk if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i]) 1796b5a0faa3SIvan Khoronzhuk return -1; 1797b5a0faa3SIvan Khoronzhuk 1798b5a0faa3SIvan Khoronzhuk return 0; 1799b5a0faa3SIvan Khoronzhuk } 1800b5a0faa3SIvan Khoronzhuk 1801a9d62274SVinicius Costa Gomes /* The semantics of the 'flags' argument in relation to 'change()' 1802a9d62274SVinicius Costa Gomes * requests, are interpreted following two rules (which are applied in 1803a9d62274SVinicius Costa Gomes * this order): (1) an omitted 'flags' argument is interpreted as 1804a9d62274SVinicius Costa Gomes * zero; (2) the 'flags' of a "running" taprio instance cannot be 1805a9d62274SVinicius Costa Gomes * changed. 1806a9d62274SVinicius Costa Gomes */ 1807a9d62274SVinicius Costa Gomes static int taprio_new_flags(const struct nlattr *attr, u32 old, 1808a9d62274SVinicius Costa Gomes struct netlink_ext_ack *extack) 1809a9d62274SVinicius Costa Gomes { 1810a9d62274SVinicius Costa Gomes u32 new = 0; 1811a9d62274SVinicius Costa Gomes 1812a9d62274SVinicius Costa Gomes if (attr) 1813a9d62274SVinicius Costa Gomes new = nla_get_u32(attr); 1814a9d62274SVinicius Costa Gomes 1815a9d62274SVinicius Costa Gomes if (old != TAPRIO_FLAGS_INVALID && old != new) { 1816a9d62274SVinicius Costa Gomes NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported"); 1817a9d62274SVinicius Costa Gomes return -EOPNOTSUPP; 1818a9d62274SVinicius Costa Gomes } 1819a9d62274SVinicius Costa Gomes 1820a9d62274SVinicius Costa Gomes if (!taprio_flags_valid(new)) { 1821a9d62274SVinicius Costa Gomes NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid"); 1822a9d62274SVinicius Costa Gomes return -EINVAL; 1823a9d62274SVinicius Costa Gomes } 1824a9d62274SVinicius Costa Gomes 1825a9d62274SVinicius Costa Gomes return new; 1826a9d62274SVinicius Costa Gomes } 1827a9d62274SVinicius Costa Gomes 18285a781ccbSVinicius Costa Gomes static int taprio_change(struct Qdisc *sch, struct nlattr *opt, 18295a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 18305a781ccbSVinicius Costa Gomes { 1831a3d91b2cSVladimir Oltean struct qdisc_size_table *stab = rtnl_dereference(sch->stab); 18325a781ccbSVinicius Costa Gomes struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { }; 1833a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin, *new_admin; 18345a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 18355a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 18365a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt *mqprio = NULL; 1837a3d43c0dSVinicius Costa Gomes unsigned long flags; 18385a781ccbSVinicius Costa Gomes ktime_t start; 18399c66d156SVinicius Costa Gomes int i, err; 18405a781ccbSVinicius Costa Gomes 18418cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt, 18425a781ccbSVinicius Costa Gomes taprio_policy, extack); 18435a781ccbSVinicius Costa Gomes if (err < 0) 18445a781ccbSVinicius Costa Gomes return err; 18455a781ccbSVinicius Costa Gomes 18465a781ccbSVinicius Costa Gomes if (tb[TCA_TAPRIO_ATTR_PRIOMAP]) 18475a781ccbSVinicius Costa Gomes mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]); 18485a781ccbSVinicius Costa Gomes 1849a9d62274SVinicius Costa Gomes err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS], 1850a9d62274SVinicius Costa Gomes q->flags, extack); 1851a9d62274SVinicius Costa Gomes if (err < 0) 1852a9d62274SVinicius Costa Gomes return err; 18534cfd5779SVedang Patel 1854a9d62274SVinicius Costa Gomes q->flags = err; 18554cfd5779SVedang Patel 1856a9d62274SVinicius Costa Gomes err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags); 18575a781ccbSVinicius Costa Gomes if (err < 0) 18585a781ccbSVinicius Costa Gomes return err; 18595a781ccbSVinicius Costa Gomes 1860a54fc09eSVladimir Oltean err = taprio_parse_tc_entries(sch, opt, extack); 1861a54fc09eSVladimir Oltean if (err) 1862a54fc09eSVladimir Oltean return err; 1863a54fc09eSVladimir Oltean 1864a3d43c0dSVinicius Costa Gomes new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL); 1865a3d43c0dSVinicius Costa Gomes if (!new_admin) { 1866a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule"); 1867a3d43c0dSVinicius Costa Gomes return -ENOMEM; 1868a3d43c0dSVinicius Costa Gomes } 1869a3d43c0dSVinicius Costa Gomes INIT_LIST_HEAD(&new_admin->entries); 18705a781ccbSVinicius Costa Gomes 187118cdd2f0SVladimir Oltean oper = rtnl_dereference(q->oper_sched); 187218cdd2f0SVladimir Oltean admin = rtnl_dereference(q->admin_sched); 18735a781ccbSVinicius Costa Gomes 1874b5a0faa3SIvan Khoronzhuk /* no changes - no new mqprio settings */ 1875b5a0faa3SIvan Khoronzhuk if (!taprio_mqprio_cmp(dev, mqprio)) 1876b5a0faa3SIvan Khoronzhuk mqprio = NULL; 1877b5a0faa3SIvan Khoronzhuk 1878a3d43c0dSVinicius Costa Gomes if (mqprio && (oper || admin)) { 1879a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported"); 1880a3d43c0dSVinicius Costa Gomes err = -ENOTSUPP; 1881a3d43c0dSVinicius Costa Gomes goto free_sched; 18825a781ccbSVinicius Costa Gomes } 18835a781ccbSVinicius Costa Gomes 18845652e63dSVinicius Costa Gomes if (mqprio) { 1885efe487fcSHaimin Zhang err = netdev_set_num_tc(dev, mqprio->num_tc); 1886efe487fcSHaimin Zhang if (err) 1887efe487fcSHaimin Zhang goto free_sched; 18882f530df7SVladimir Oltean for (i = 0; i < mqprio->num_tc; i++) { 18895652e63dSVinicius Costa Gomes netdev_set_tc_queue(dev, i, 18905652e63dSVinicius Costa Gomes mqprio->count[i], 18915652e63dSVinicius Costa Gomes mqprio->offset[i]); 18922f530df7SVladimir Oltean q->cur_txq[i] = mqprio->offset[i]; 18932f530df7SVladimir Oltean } 18945652e63dSVinicius Costa Gomes 18955652e63dSVinicius Costa Gomes /* Always use supplied priority mappings */ 18965652e63dSVinicius Costa Gomes for (i = 0; i <= TC_BITMASK; i++) 18975652e63dSVinicius Costa Gomes netdev_set_prio_tc_map(dev, i, 18985652e63dSVinicius Costa Gomes mqprio->prio_tc_map[i]); 18995652e63dSVinicius Costa Gomes } 19005652e63dSVinicius Costa Gomes 190109dbdf28SVladimir Oltean err = parse_taprio_schedule(q, tb, new_admin, extack); 190209dbdf28SVladimir Oltean if (err < 0) 190309dbdf28SVladimir Oltean goto free_sched; 190409dbdf28SVladimir Oltean 190509dbdf28SVladimir Oltean if (new_admin->num_entries == 0) { 190609dbdf28SVladimir Oltean NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule"); 190709dbdf28SVladimir Oltean err = -EINVAL; 190809dbdf28SVladimir Oltean goto free_sched; 190909dbdf28SVladimir Oltean } 191009dbdf28SVladimir Oltean 191109dbdf28SVladimir Oltean err = taprio_parse_clockid(sch, tb, extack); 191209dbdf28SVladimir Oltean if (err < 0) 191309dbdf28SVladimir Oltean goto free_sched; 191409dbdf28SVladimir Oltean 191509dbdf28SVladimir Oltean taprio_set_picos_per_byte(dev, q); 191609dbdf28SVladimir Oltean taprio_update_queue_max_sdu(q, new_admin, stab); 191709dbdf28SVladimir Oltean 1918a9d62274SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 191909e31cf0SVinicius Costa Gomes err = taprio_enable_offload(dev, q, new_admin, extack); 19209c66d156SVinicius Costa Gomes else 19219c66d156SVinicius Costa Gomes err = taprio_disable_offload(dev, q, extack); 19229c66d156SVinicius Costa Gomes if (err) 19239c66d156SVinicius Costa Gomes goto free_sched; 19249c66d156SVinicius Costa Gomes 1925a3d43c0dSVinicius Costa Gomes /* Protects against enqueue()/dequeue() */ 1926a3d43c0dSVinicius Costa Gomes spin_lock_bh(qdisc_lock(sch)); 1927a3d43c0dSVinicius Costa Gomes 19284cfd5779SVedang Patel if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) { 19294cfd5779SVedang Patel if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) { 19304cfd5779SVedang Patel NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled"); 19314cfd5779SVedang Patel err = -EINVAL; 19324cfd5779SVedang Patel goto unlock; 19334cfd5779SVedang Patel } 19344cfd5779SVedang Patel 1935a5b64700SVedang Patel q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]); 19364cfd5779SVedang Patel } 19374cfd5779SVedang Patel 1938a9d62274SVinicius Costa Gomes if (!TXTIME_ASSIST_IS_ENABLED(q->flags) && 1939a9d62274SVinicius Costa Gomes !FULL_OFFLOAD_IS_ENABLED(q->flags) && 19404cfd5779SVedang Patel !hrtimer_active(&q->advance_timer)) { 1941a3d43c0dSVinicius Costa Gomes hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS); 1942a3d43c0dSVinicius Costa Gomes q->advance_timer.function = advance_sched; 19435a781ccbSVinicius Costa Gomes } 19445a781ccbSVinicius Costa Gomes 1945a3d43c0dSVinicius Costa Gomes err = taprio_get_start_time(sch, new_admin, &start); 1946a3d43c0dSVinicius Costa Gomes if (err < 0) { 1947a3d43c0dSVinicius Costa Gomes NL_SET_ERR_MSG(extack, "Internal error: failed get start time"); 1948a3d43c0dSVinicius Costa Gomes goto unlock; 1949a3d43c0dSVinicius Costa Gomes } 19505a781ccbSVinicius Costa Gomes 19514cfd5779SVedang Patel setup_txtime(q, new_admin, start); 19524cfd5779SVedang Patel 1953bfabd41dSVinicius Costa Gomes if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 19544cfd5779SVedang Patel if (!oper) { 19554cfd5779SVedang Patel rcu_assign_pointer(q->oper_sched, new_admin); 19564cfd5779SVedang Patel err = 0; 19574cfd5779SVedang Patel new_admin = NULL; 19584cfd5779SVedang Patel goto unlock; 19594cfd5779SVedang Patel } 19604cfd5779SVedang Patel 19614cfd5779SVedang Patel rcu_assign_pointer(q->admin_sched, new_admin); 19624cfd5779SVedang Patel if (admin) 19634cfd5779SVedang Patel call_rcu(&admin->rcu, taprio_free_sched_cb); 19644cfd5779SVedang Patel } else { 1965e5517551SVladimir Oltean setup_first_end_time(q, new_admin, start); 1966a3d43c0dSVinicius Costa Gomes 1967a3d43c0dSVinicius Costa Gomes /* Protects against advance_sched() */ 1968a3d43c0dSVinicius Costa Gomes spin_lock_irqsave(&q->current_entry_lock, flags); 1969a3d43c0dSVinicius Costa Gomes 1970a3d43c0dSVinicius Costa Gomes taprio_start_sched(sch, start, new_admin); 1971a3d43c0dSVinicius Costa Gomes 1972a3d43c0dSVinicius Costa Gomes rcu_assign_pointer(q->admin_sched, new_admin); 1973a3d43c0dSVinicius Costa Gomes if (admin) 1974a3d43c0dSVinicius Costa Gomes call_rcu(&admin->rcu, taprio_free_sched_cb); 1975a3d43c0dSVinicius Costa Gomes 1976a3d43c0dSVinicius Costa Gomes spin_unlock_irqrestore(&q->current_entry_lock, flags); 19770763b3e8SIvan Khoronzhuk 1978a9d62274SVinicius Costa Gomes if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 19790763b3e8SIvan Khoronzhuk taprio_offload_config_changed(q); 19804cfd5779SVedang Patel } 1981a3d43c0dSVinicius Costa Gomes 19824cfd5779SVedang Patel new_admin = NULL; 1983a3d43c0dSVinicius Costa Gomes err = 0; 1984a3d43c0dSVinicius Costa Gomes 1985a3d91b2cSVladimir Oltean if (!stab) 1986a3d91b2cSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1987a3d91b2cSVladimir Oltean "Size table not specified, frame length estimations may be inaccurate"); 1988a3d91b2cSVladimir Oltean 1989a3d43c0dSVinicius Costa Gomes unlock: 1990a3d43c0dSVinicius Costa Gomes spin_unlock_bh(qdisc_lock(sch)); 1991a3d43c0dSVinicius Costa Gomes 1992a3d43c0dSVinicius Costa Gomes free_sched: 199351650d33SIvan Khoronzhuk if (new_admin) 199451650d33SIvan Khoronzhuk call_rcu(&new_admin->rcu, taprio_free_sched_cb); 1995a3d43c0dSVinicius Costa Gomes 1996a3d43c0dSVinicius Costa Gomes return err; 19975a781ccbSVinicius Costa Gomes } 19985a781ccbSVinicius Costa Gomes 199944d4775cSDavide Caratti static void taprio_reset(struct Qdisc *sch) 200044d4775cSDavide Caratti { 200144d4775cSDavide Caratti struct taprio_sched *q = qdisc_priv(sch); 200244d4775cSDavide Caratti struct net_device *dev = qdisc_dev(sch); 200344d4775cSDavide Caratti int i; 200444d4775cSDavide Caratti 200544d4775cSDavide Caratti hrtimer_cancel(&q->advance_timer); 20063a415d59SEric Dumazet 200744d4775cSDavide Caratti if (q->qdiscs) { 2008698285daSDavide Caratti for (i = 0; i < dev->num_tx_queues; i++) 2009698285daSDavide Caratti if (q->qdiscs[i]) 201044d4775cSDavide Caratti qdisc_reset(q->qdiscs[i]); 201144d4775cSDavide Caratti } 201244d4775cSDavide Caratti } 201344d4775cSDavide Caratti 20145a781ccbSVinicius Costa Gomes static void taprio_destroy(struct Qdisc *sch) 20155a781ccbSVinicius Costa Gomes { 20165a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 20175a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 20189af23657SVladimir Oltean struct sched_gate_list *oper, *admin; 20195a781ccbSVinicius Costa Gomes unsigned int i; 20205a781ccbSVinicius Costa Gomes 20217b9eba7bSLeandro Dorileo list_del(&q->taprio_list); 20227b9eba7bSLeandro Dorileo 2023a56d447fSEric Dumazet /* Note that taprio_reset() might not be called if an error 2024a56d447fSEric Dumazet * happens in qdisc_create(), after taprio_init() has been called. 2025a56d447fSEric Dumazet */ 2026a56d447fSEric Dumazet hrtimer_cancel(&q->advance_timer); 20273a415d59SEric Dumazet qdisc_synchronize(sch); 20285a781ccbSVinicius Costa Gomes 20299c66d156SVinicius Costa Gomes taprio_disable_offload(dev, q, NULL); 20309c66d156SVinicius Costa Gomes 20315a781ccbSVinicius Costa Gomes if (q->qdiscs) { 2032698285daSDavide Caratti for (i = 0; i < dev->num_tx_queues; i++) 20335a781ccbSVinicius Costa Gomes qdisc_put(q->qdiscs[i]); 20345a781ccbSVinicius Costa Gomes 20355a781ccbSVinicius Costa Gomes kfree(q->qdiscs); 20365a781ccbSVinicius Costa Gomes } 20375a781ccbSVinicius Costa Gomes q->qdiscs = NULL; 20385a781ccbSVinicius Costa Gomes 20397c16680aSVinicius Costa Gomes netdev_reset_tc(dev); 20405a781ccbSVinicius Costa Gomes 20419af23657SVladimir Oltean oper = rtnl_dereference(q->oper_sched); 20429af23657SVladimir Oltean admin = rtnl_dereference(q->admin_sched); 2043a3d43c0dSVinicius Costa Gomes 20449af23657SVladimir Oltean if (oper) 20459af23657SVladimir Oltean call_rcu(&oper->rcu, taprio_free_sched_cb); 20469af23657SVladimir Oltean 20479af23657SVladimir Oltean if (admin) 20489af23657SVladimir Oltean call_rcu(&admin->rcu, taprio_free_sched_cb); 20492f530df7SVladimir Oltean 20502f530df7SVladimir Oltean taprio_cleanup_broken_mqprio(q); 20515a781ccbSVinicius Costa Gomes } 20525a781ccbSVinicius Costa Gomes 20535a781ccbSVinicius Costa Gomes static int taprio_init(struct Qdisc *sch, struct nlattr *opt, 20545a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 20555a781ccbSVinicius Costa Gomes { 20565a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 20575a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 2058a721c3e5SVladimir Oltean int i, tc; 20595a781ccbSVinicius Costa Gomes 20605a781ccbSVinicius Costa Gomes spin_lock_init(&q->current_entry_lock); 20615a781ccbSVinicius Costa Gomes 20625a781ccbSVinicius Costa Gomes hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS); 2063a3d43c0dSVinicius Costa Gomes q->advance_timer.function = advance_sched; 20645a781ccbSVinicius Costa Gomes 20655a781ccbSVinicius Costa Gomes q->root = sch; 20665a781ccbSVinicius Costa Gomes 20675a781ccbSVinicius Costa Gomes /* We only support static clockids. Use an invalid value as default 20685a781ccbSVinicius Costa Gomes * and get the valid one on taprio_change(). 20695a781ccbSVinicius Costa Gomes */ 20705a781ccbSVinicius Costa Gomes q->clockid = -1; 2071a9d62274SVinicius Costa Gomes q->flags = TAPRIO_FLAGS_INVALID; 20725a781ccbSVinicius Costa Gomes 2073efb55222SVladimir Oltean list_add(&q->taprio_list, &taprio_list); 2074efb55222SVladimir Oltean 2075026de64dSVladimir Oltean if (sch->parent != TC_H_ROOT) { 2076026de64dSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Can only be attached as root qdisc"); 20775a781ccbSVinicius Costa Gomes return -EOPNOTSUPP; 2078026de64dSVladimir Oltean } 20795a781ccbSVinicius Costa Gomes 2080026de64dSVladimir Oltean if (!netif_is_multiqueue(dev)) { 2081026de64dSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Multi-queue device is required"); 20825a781ccbSVinicius Costa Gomes return -EOPNOTSUPP; 2083026de64dSVladimir Oltean } 20845a781ccbSVinicius Costa Gomes 20855a781ccbSVinicius Costa Gomes /* pre-allocate qdisc, attachment can't fail */ 20865a781ccbSVinicius Costa Gomes q->qdiscs = kcalloc(dev->num_tx_queues, 20875a781ccbSVinicius Costa Gomes sizeof(q->qdiscs[0]), 20885a781ccbSVinicius Costa Gomes GFP_KERNEL); 20895a781ccbSVinicius Costa Gomes 20905a781ccbSVinicius Costa Gomes if (!q->qdiscs) 20915a781ccbSVinicius Costa Gomes return -ENOMEM; 20925a781ccbSVinicius Costa Gomes 20935a781ccbSVinicius Costa Gomes if (!opt) 20945a781ccbSVinicius Costa Gomes return -EINVAL; 20955a781ccbSVinicius Costa Gomes 2096a3d43c0dSVinicius Costa Gomes for (i = 0; i < dev->num_tx_queues; i++) { 2097a3d43c0dSVinicius Costa Gomes struct netdev_queue *dev_queue; 2098a3d43c0dSVinicius Costa Gomes struct Qdisc *qdisc; 2099a3d43c0dSVinicius Costa Gomes 2100a3d43c0dSVinicius Costa Gomes dev_queue = netdev_get_tx_queue(dev, i); 2101a3d43c0dSVinicius Costa Gomes qdisc = qdisc_create_dflt(dev_queue, 2102a3d43c0dSVinicius Costa Gomes &pfifo_qdisc_ops, 2103a3d43c0dSVinicius Costa Gomes TC_H_MAKE(TC_H_MAJ(sch->handle), 2104a3d43c0dSVinicius Costa Gomes TC_H_MIN(i + 1)), 2105a3d43c0dSVinicius Costa Gomes extack); 2106a3d43c0dSVinicius Costa Gomes if (!qdisc) 2107a3d43c0dSVinicius Costa Gomes return -ENOMEM; 2108a3d43c0dSVinicius Costa Gomes 2109a3d43c0dSVinicius Costa Gomes if (i < dev->real_num_tx_queues) 2110a3d43c0dSVinicius Costa Gomes qdisc_hash_add(qdisc, false); 2111a3d43c0dSVinicius Costa Gomes 2112a3d43c0dSVinicius Costa Gomes q->qdiscs[i] = qdisc; 2113a3d43c0dSVinicius Costa Gomes } 2114a3d43c0dSVinicius Costa Gomes 2115a721c3e5SVladimir Oltean for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) 2116a721c3e5SVladimir Oltean q->fp[tc] = TC_FP_EXPRESS; 2117a721c3e5SVladimir Oltean 21182f530df7SVladimir Oltean taprio_detect_broken_mqprio(q); 21192f530df7SVladimir Oltean 21205a781ccbSVinicius Costa Gomes return taprio_change(sch, opt, extack); 21215a781ccbSVinicius Costa Gomes } 21225a781ccbSVinicius Costa Gomes 212313511704SYannick Vignon static void taprio_attach(struct Qdisc *sch) 212413511704SYannick Vignon { 212513511704SYannick Vignon struct taprio_sched *q = qdisc_priv(sch); 212613511704SYannick Vignon struct net_device *dev = qdisc_dev(sch); 212713511704SYannick Vignon unsigned int ntx; 212813511704SYannick Vignon 212913511704SYannick Vignon /* Attach underlying qdisc */ 213013511704SYannick Vignon for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { 213113511704SYannick Vignon struct Qdisc *qdisc = q->qdiscs[ntx]; 213213511704SYannick Vignon struct Qdisc *old; 213313511704SYannick Vignon 213413511704SYannick Vignon if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 213513511704SYannick Vignon qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 213613511704SYannick Vignon old = dev_graft_qdisc(qdisc->dev_queue, qdisc); 213713511704SYannick Vignon } else { 213813511704SYannick Vignon old = dev_graft_qdisc(qdisc->dev_queue, sch); 213913511704SYannick Vignon qdisc_refcount_inc(sch); 214013511704SYannick Vignon } 214113511704SYannick Vignon if (old) 214213511704SYannick Vignon qdisc_put(old); 214313511704SYannick Vignon } 214413511704SYannick Vignon 214513511704SYannick Vignon /* access to the child qdiscs is not needed in offload mode */ 214613511704SYannick Vignon if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 214713511704SYannick Vignon kfree(q->qdiscs); 214813511704SYannick Vignon q->qdiscs = NULL; 214913511704SYannick Vignon } 215013511704SYannick Vignon } 215113511704SYannick Vignon 21525a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_queue_get(struct Qdisc *sch, 21535a781ccbSVinicius Costa Gomes unsigned long cl) 21545a781ccbSVinicius Costa Gomes { 21555a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 21565a781ccbSVinicius Costa Gomes unsigned long ntx = cl - 1; 21575a781ccbSVinicius Costa Gomes 21585a781ccbSVinicius Costa Gomes if (ntx >= dev->num_tx_queues) 21595a781ccbSVinicius Costa Gomes return NULL; 21605a781ccbSVinicius Costa Gomes 21615a781ccbSVinicius Costa Gomes return netdev_get_tx_queue(dev, ntx); 21625a781ccbSVinicius Costa Gomes } 21635a781ccbSVinicius Costa Gomes 21645a781ccbSVinicius Costa Gomes static int taprio_graft(struct Qdisc *sch, unsigned long cl, 21655a781ccbSVinicius Costa Gomes struct Qdisc *new, struct Qdisc **old, 21665a781ccbSVinicius Costa Gomes struct netlink_ext_ack *extack) 21675a781ccbSVinicius Costa Gomes { 21685a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 21695a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 21705a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 21715a781ccbSVinicius Costa Gomes 21725a781ccbSVinicius Costa Gomes if (!dev_queue) 21735a781ccbSVinicius Costa Gomes return -EINVAL; 21745a781ccbSVinicius Costa Gomes 21755a781ccbSVinicius Costa Gomes if (dev->flags & IFF_UP) 21765a781ccbSVinicius Costa Gomes dev_deactivate(dev); 21775a781ccbSVinicius Costa Gomes 217813511704SYannick Vignon if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 217913511704SYannick Vignon *old = dev_graft_qdisc(dev_queue, new); 218013511704SYannick Vignon } else { 21815a781ccbSVinicius Costa Gomes *old = q->qdiscs[cl - 1]; 21825a781ccbSVinicius Costa Gomes q->qdiscs[cl - 1] = new; 218313511704SYannick Vignon } 21845a781ccbSVinicius Costa Gomes 21855a781ccbSVinicius Costa Gomes if (new) 21865a781ccbSVinicius Costa Gomes new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 21875a781ccbSVinicius Costa Gomes 21885a781ccbSVinicius Costa Gomes if (dev->flags & IFF_UP) 21895a781ccbSVinicius Costa Gomes dev_activate(dev); 21905a781ccbSVinicius Costa Gomes 21915a781ccbSVinicius Costa Gomes return 0; 21925a781ccbSVinicius Costa Gomes } 21935a781ccbSVinicius Costa Gomes 21945a781ccbSVinicius Costa Gomes static int dump_entry(struct sk_buff *msg, 21955a781ccbSVinicius Costa Gomes const struct sched_entry *entry) 21965a781ccbSVinicius Costa Gomes { 21975a781ccbSVinicius Costa Gomes struct nlattr *item; 21985a781ccbSVinicius Costa Gomes 2199ae0be8deSMichal Kubecek item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY); 22005a781ccbSVinicius Costa Gomes if (!item) 22015a781ccbSVinicius Costa Gomes return -ENOSPC; 22025a781ccbSVinicius Costa Gomes 22035a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index)) 22045a781ccbSVinicius Costa Gomes goto nla_put_failure; 22055a781ccbSVinicius Costa Gomes 22065a781ccbSVinicius Costa Gomes if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command)) 22075a781ccbSVinicius Costa Gomes goto nla_put_failure; 22085a781ccbSVinicius Costa Gomes 22095a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK, 22105a781ccbSVinicius Costa Gomes entry->gate_mask)) 22115a781ccbSVinicius Costa Gomes goto nla_put_failure; 22125a781ccbSVinicius Costa Gomes 22135a781ccbSVinicius Costa Gomes if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL, 22145a781ccbSVinicius Costa Gomes entry->interval)) 22155a781ccbSVinicius Costa Gomes goto nla_put_failure; 22165a781ccbSVinicius Costa Gomes 22175a781ccbSVinicius Costa Gomes return nla_nest_end(msg, item); 22185a781ccbSVinicius Costa Gomes 22195a781ccbSVinicius Costa Gomes nla_put_failure: 22205a781ccbSVinicius Costa Gomes nla_nest_cancel(msg, item); 22215a781ccbSVinicius Costa Gomes return -1; 22225a781ccbSVinicius Costa Gomes } 22235a781ccbSVinicius Costa Gomes 2224a3d43c0dSVinicius Costa Gomes static int dump_schedule(struct sk_buff *msg, 2225a3d43c0dSVinicius Costa Gomes const struct sched_gate_list *root) 2226a3d43c0dSVinicius Costa Gomes { 2227a3d43c0dSVinicius Costa Gomes struct nlattr *entry_list; 2228a3d43c0dSVinicius Costa Gomes struct sched_entry *entry; 2229a3d43c0dSVinicius Costa Gomes 2230a3d43c0dSVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME, 2231a3d43c0dSVinicius Costa Gomes root->base_time, TCA_TAPRIO_PAD)) 2232a3d43c0dSVinicius Costa Gomes return -1; 2233a3d43c0dSVinicius Costa Gomes 22346ca6a665SVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME, 22356ca6a665SVinicius Costa Gomes root->cycle_time, TCA_TAPRIO_PAD)) 22366ca6a665SVinicius Costa Gomes return -1; 22376ca6a665SVinicius Costa Gomes 2238c25031e9SVinicius Costa Gomes if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION, 2239c25031e9SVinicius Costa Gomes root->cycle_time_extension, TCA_TAPRIO_PAD)) 2240c25031e9SVinicius Costa Gomes return -1; 2241c25031e9SVinicius Costa Gomes 2242a3d43c0dSVinicius Costa Gomes entry_list = nla_nest_start_noflag(msg, 2243a3d43c0dSVinicius Costa Gomes TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST); 2244a3d43c0dSVinicius Costa Gomes if (!entry_list) 2245a3d43c0dSVinicius Costa Gomes goto error_nest; 2246a3d43c0dSVinicius Costa Gomes 2247a3d43c0dSVinicius Costa Gomes list_for_each_entry(entry, &root->entries, list) { 2248a3d43c0dSVinicius Costa Gomes if (dump_entry(msg, entry) < 0) 2249a3d43c0dSVinicius Costa Gomes goto error_nest; 2250a3d43c0dSVinicius Costa Gomes } 2251a3d43c0dSVinicius Costa Gomes 2252a3d43c0dSVinicius Costa Gomes nla_nest_end(msg, entry_list); 2253a3d43c0dSVinicius Costa Gomes return 0; 2254a3d43c0dSVinicius Costa Gomes 2255a3d43c0dSVinicius Costa Gomes error_nest: 2256a3d43c0dSVinicius Costa Gomes nla_nest_cancel(msg, entry_list); 2257a3d43c0dSVinicius Costa Gomes return -1; 2258a3d43c0dSVinicius Costa Gomes } 2259a3d43c0dSVinicius Costa Gomes 2260fed87cc6SVladimir Oltean static int taprio_dump_tc_entries(struct sk_buff *skb, 2261a721c3e5SVladimir Oltean struct taprio_sched *q, 2262fed87cc6SVladimir Oltean struct sched_gate_list *sched) 2263a54fc09eSVladimir Oltean { 2264a54fc09eSVladimir Oltean struct nlattr *n; 2265a54fc09eSVladimir Oltean int tc; 2266a54fc09eSVladimir Oltean 2267a54fc09eSVladimir Oltean for (tc = 0; tc < TC_MAX_QUEUE; tc++) { 2268a54fc09eSVladimir Oltean n = nla_nest_start(skb, TCA_TAPRIO_ATTR_TC_ENTRY); 2269a54fc09eSVladimir Oltean if (!n) 2270a54fc09eSVladimir Oltean return -EMSGSIZE; 2271a54fc09eSVladimir Oltean 2272a54fc09eSVladimir Oltean if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_INDEX, tc)) 2273a54fc09eSVladimir Oltean goto nla_put_failure; 2274a54fc09eSVladimir Oltean 2275a54fc09eSVladimir Oltean if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_MAX_SDU, 2276fed87cc6SVladimir Oltean sched->max_sdu[tc])) 2277a54fc09eSVladimir Oltean goto nla_put_failure; 2278a54fc09eSVladimir Oltean 2279a721c3e5SVladimir Oltean if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_FP, q->fp[tc])) 2280a721c3e5SVladimir Oltean goto nla_put_failure; 2281a721c3e5SVladimir Oltean 2282a54fc09eSVladimir Oltean nla_nest_end(skb, n); 2283a54fc09eSVladimir Oltean } 2284a54fc09eSVladimir Oltean 2285a54fc09eSVladimir Oltean return 0; 2286a54fc09eSVladimir Oltean 2287a54fc09eSVladimir Oltean nla_put_failure: 2288a54fc09eSVladimir Oltean nla_nest_cancel(skb, n); 2289a54fc09eSVladimir Oltean return -EMSGSIZE; 2290a54fc09eSVladimir Oltean } 2291a54fc09eSVladimir Oltean 22925a781ccbSVinicius Costa Gomes static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb) 22935a781ccbSVinicius Costa Gomes { 22945a781ccbSVinicius Costa Gomes struct taprio_sched *q = qdisc_priv(sch); 22955a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 2296a3d43c0dSVinicius Costa Gomes struct sched_gate_list *oper, *admin; 22975a781ccbSVinicius Costa Gomes struct tc_mqprio_qopt opt = { 0 }; 2298a3d43c0dSVinicius Costa Gomes struct nlattr *nest, *sched_nest; 22995a781ccbSVinicius Costa Gomes 230018cdd2f0SVladimir Oltean oper = rtnl_dereference(q->oper_sched); 230118cdd2f0SVladimir Oltean admin = rtnl_dereference(q->admin_sched); 2302a3d43c0dSVinicius Costa Gomes 23039dd6ad67SVladimir Oltean mqprio_qopt_reconstruct(dev, &opt); 23045a781ccbSVinicius Costa Gomes 2305ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 23065a781ccbSVinicius Costa Gomes if (!nest) 2307a3d43c0dSVinicius Costa Gomes goto start_error; 23085a781ccbSVinicius Costa Gomes 23095a781ccbSVinicius Costa Gomes if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt)) 23105a781ccbSVinicius Costa Gomes goto options_error; 23115a781ccbSVinicius Costa Gomes 23129c66d156SVinicius Costa Gomes if (!FULL_OFFLOAD_IS_ENABLED(q->flags) && 23139c66d156SVinicius Costa Gomes nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid)) 23145a781ccbSVinicius Costa Gomes goto options_error; 23155a781ccbSVinicius Costa Gomes 23164cfd5779SVedang Patel if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags)) 23174cfd5779SVedang Patel goto options_error; 23184cfd5779SVedang Patel 23194cfd5779SVedang Patel if (q->txtime_delay && 2320a5b64700SVedang Patel nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay)) 23214cfd5779SVedang Patel goto options_error; 23224cfd5779SVedang Patel 2323a721c3e5SVladimir Oltean if (oper && taprio_dump_tc_entries(skb, q, oper)) 2324a54fc09eSVladimir Oltean goto options_error; 2325a54fc09eSVladimir Oltean 2326a3d43c0dSVinicius Costa Gomes if (oper && dump_schedule(skb, oper)) 23275a781ccbSVinicius Costa Gomes goto options_error; 23285a781ccbSVinicius Costa Gomes 2329a3d43c0dSVinicius Costa Gomes if (!admin) 2330a3d43c0dSVinicius Costa Gomes goto done; 23315a781ccbSVinicius Costa Gomes 2332a3d43c0dSVinicius Costa Gomes sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED); 2333e4acf427SColin Ian King if (!sched_nest) 2334e4acf427SColin Ian King goto options_error; 2335a3d43c0dSVinicius Costa Gomes 2336a3d43c0dSVinicius Costa Gomes if (dump_schedule(skb, admin)) 2337a3d43c0dSVinicius Costa Gomes goto admin_error; 2338a3d43c0dSVinicius Costa Gomes 2339a3d43c0dSVinicius Costa Gomes nla_nest_end(skb, sched_nest); 2340a3d43c0dSVinicius Costa Gomes 2341a3d43c0dSVinicius Costa Gomes done: 23425a781ccbSVinicius Costa Gomes return nla_nest_end(skb, nest); 23435a781ccbSVinicius Costa Gomes 2344a3d43c0dSVinicius Costa Gomes admin_error: 2345a3d43c0dSVinicius Costa Gomes nla_nest_cancel(skb, sched_nest); 2346a3d43c0dSVinicius Costa Gomes 23475a781ccbSVinicius Costa Gomes options_error: 23485a781ccbSVinicius Costa Gomes nla_nest_cancel(skb, nest); 2349a3d43c0dSVinicius Costa Gomes 2350a3d43c0dSVinicius Costa Gomes start_error: 2351a3d43c0dSVinicius Costa Gomes return -ENOSPC; 23525a781ccbSVinicius Costa Gomes } 23535a781ccbSVinicius Costa Gomes 23545a781ccbSVinicius Costa Gomes static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl) 23555a781ccbSVinicius Costa Gomes { 2356af7b29b1SVladimir Oltean struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 23575a781ccbSVinicius Costa Gomes 2358af7b29b1SVladimir Oltean if (!dev_queue) 23595a781ccbSVinicius Costa Gomes return NULL; 23605a781ccbSVinicius Costa Gomes 2361af7b29b1SVladimir Oltean return dev_queue->qdisc_sleeping; 23625a781ccbSVinicius Costa Gomes } 23635a781ccbSVinicius Costa Gomes 23645a781ccbSVinicius Costa Gomes static unsigned long taprio_find(struct Qdisc *sch, u32 classid) 23655a781ccbSVinicius Costa Gomes { 23665a781ccbSVinicius Costa Gomes unsigned int ntx = TC_H_MIN(classid); 23675a781ccbSVinicius Costa Gomes 23685a781ccbSVinicius Costa Gomes if (!taprio_queue_get(sch, ntx)) 23695a781ccbSVinicius Costa Gomes return 0; 23705a781ccbSVinicius Costa Gomes return ntx; 23715a781ccbSVinicius Costa Gomes } 23725a781ccbSVinicius Costa Gomes 23735a781ccbSVinicius Costa Gomes static int taprio_dump_class(struct Qdisc *sch, unsigned long cl, 23745a781ccbSVinicius Costa Gomes struct sk_buff *skb, struct tcmsg *tcm) 23755a781ccbSVinicius Costa Gomes { 23765a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 23775a781ccbSVinicius Costa Gomes 23785a781ccbSVinicius Costa Gomes tcm->tcm_parent = TC_H_ROOT; 23795a781ccbSVinicius Costa Gomes tcm->tcm_handle |= TC_H_MIN(cl); 23805a781ccbSVinicius Costa Gomes tcm->tcm_info = dev_queue->qdisc_sleeping->handle; 23815a781ccbSVinicius Costa Gomes 23825a781ccbSVinicius Costa Gomes return 0; 23835a781ccbSVinicius Costa Gomes } 23845a781ccbSVinicius Costa Gomes 23855a781ccbSVinicius Costa Gomes static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 23865a781ccbSVinicius Costa Gomes struct gnet_dump *d) 23875a781ccbSVinicius Costa Gomes __releases(d->lock) 23885a781ccbSVinicius Costa Gomes __acquires(d->lock) 23895a781ccbSVinicius Costa Gomes { 23905a781ccbSVinicius Costa Gomes struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 2391dced11efSVladimir Oltean struct Qdisc *child = dev_queue->qdisc_sleeping; 23925a781ccbSVinicius Costa Gomes 2393dced11efSVladimir Oltean if (gnet_stats_copy_basic(d, NULL, &child->bstats, true) < 0 || 2394dced11efSVladimir Oltean qdisc_qstats_copy(d, child) < 0) 23955a781ccbSVinicius Costa Gomes return -1; 23965a781ccbSVinicius Costa Gomes return 0; 23975a781ccbSVinicius Costa Gomes } 23985a781ccbSVinicius Costa Gomes 23995a781ccbSVinicius Costa Gomes static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 24005a781ccbSVinicius Costa Gomes { 24015a781ccbSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 24025a781ccbSVinicius Costa Gomes unsigned long ntx; 24035a781ccbSVinicius Costa Gomes 24045a781ccbSVinicius Costa Gomes if (arg->stop) 24055a781ccbSVinicius Costa Gomes return; 24065a781ccbSVinicius Costa Gomes 24075a781ccbSVinicius Costa Gomes arg->count = arg->skip; 24085a781ccbSVinicius Costa Gomes for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) { 2409e046fa89SZhengchao Shao if (!tc_qdisc_stats_dump(sch, ntx + 1, arg)) 24105a781ccbSVinicius Costa Gomes break; 24115a781ccbSVinicius Costa Gomes } 24125a781ccbSVinicius Costa Gomes } 24135a781ccbSVinicius Costa Gomes 24145a781ccbSVinicius Costa Gomes static struct netdev_queue *taprio_select_queue(struct Qdisc *sch, 24155a781ccbSVinicius Costa Gomes struct tcmsg *tcm) 24165a781ccbSVinicius Costa Gomes { 24175a781ccbSVinicius Costa Gomes return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); 24185a781ccbSVinicius Costa Gomes } 24195a781ccbSVinicius Costa Gomes 24205a781ccbSVinicius Costa Gomes static const struct Qdisc_class_ops taprio_class_ops = { 24215a781ccbSVinicius Costa Gomes .graft = taprio_graft, 24225a781ccbSVinicius Costa Gomes .leaf = taprio_leaf, 24235a781ccbSVinicius Costa Gomes .find = taprio_find, 24245a781ccbSVinicius Costa Gomes .walk = taprio_walk, 24255a781ccbSVinicius Costa Gomes .dump = taprio_dump_class, 24265a781ccbSVinicius Costa Gomes .dump_stats = taprio_dump_class_stats, 24275a781ccbSVinicius Costa Gomes .select_queue = taprio_select_queue, 24285a781ccbSVinicius Costa Gomes }; 24295a781ccbSVinicius Costa Gomes 24305a781ccbSVinicius Costa Gomes static struct Qdisc_ops taprio_qdisc_ops __read_mostly = { 24315a781ccbSVinicius Costa Gomes .cl_ops = &taprio_class_ops, 24325a781ccbSVinicius Costa Gomes .id = "taprio", 24335a781ccbSVinicius Costa Gomes .priv_size = sizeof(struct taprio_sched), 24345a781ccbSVinicius Costa Gomes .init = taprio_init, 2435a3d43c0dSVinicius Costa Gomes .change = taprio_change, 24365a781ccbSVinicius Costa Gomes .destroy = taprio_destroy, 243744d4775cSDavide Caratti .reset = taprio_reset, 243813511704SYannick Vignon .attach = taprio_attach, 24395a781ccbSVinicius Costa Gomes .peek = taprio_peek, 24405a781ccbSVinicius Costa Gomes .dequeue = taprio_dequeue, 24415a781ccbSVinicius Costa Gomes .enqueue = taprio_enqueue, 24425a781ccbSVinicius Costa Gomes .dump = taprio_dump, 24435a781ccbSVinicius Costa Gomes .owner = THIS_MODULE, 24445a781ccbSVinicius Costa Gomes }; 24455a781ccbSVinicius Costa Gomes 24467b9eba7bSLeandro Dorileo static struct notifier_block taprio_device_notifier = { 24477b9eba7bSLeandro Dorileo .notifier_call = taprio_dev_notifier, 24487b9eba7bSLeandro Dorileo }; 24497b9eba7bSLeandro Dorileo 24505a781ccbSVinicius Costa Gomes static int __init taprio_module_init(void) 24515a781ccbSVinicius Costa Gomes { 24527b9eba7bSLeandro Dorileo int err = register_netdevice_notifier(&taprio_device_notifier); 24537b9eba7bSLeandro Dorileo 24547b9eba7bSLeandro Dorileo if (err) 24557b9eba7bSLeandro Dorileo return err; 24567b9eba7bSLeandro Dorileo 24575a781ccbSVinicius Costa Gomes return register_qdisc(&taprio_qdisc_ops); 24585a781ccbSVinicius Costa Gomes } 24595a781ccbSVinicius Costa Gomes 24605a781ccbSVinicius Costa Gomes static void __exit taprio_module_exit(void) 24615a781ccbSVinicius Costa Gomes { 24625a781ccbSVinicius Costa Gomes unregister_qdisc(&taprio_qdisc_ops); 24637b9eba7bSLeandro Dorileo unregister_netdevice_notifier(&taprio_device_notifier); 24645a781ccbSVinicius Costa Gomes } 24655a781ccbSVinicius Costa Gomes 24665a781ccbSVinicius Costa Gomes module_init(taprio_module_init); 24675a781ccbSVinicius Costa Gomes module_exit(taprio_module_exit); 24685a781ccbSVinicius Costa Gomes MODULE_LICENSE("GPL"); 2469