xref: /src/sys/contrib/dev/mediatek/mt76/util.h (revision b1bebaaba9b9c0ddfe503c43ca8e9e3917ee2c57)
1 /* SPDX-License-Identifier: BSD-3-Clause-Clear */
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  */
5 
6 #ifndef __MT76_UTIL_H
7 #define __MT76_UTIL_H
8 
9 #include <linux/skbuff.h>
10 #include <linux/bitops.h>
11 #include <linux/bitfield.h>
12 #include <net/mac80211.h>
13 #if defined(__FreeBSD__)
14 #include <linux/kthread.h>
15 #endif
16 
17 struct mt76_worker
18 {
19 	struct task_struct *task;
20 	void (*fn)(struct mt76_worker *);
21 	unsigned long state;
22 };
23 
24 enum {
25 	MT76_WORKER_SCHEDULED,
26 	MT76_WORKER_RUNNING,
27 };
28 
29 #define MT76_INCR(_var, _size) \
30 	(_var = (((_var) + 1) % (_size)))
31 
32 int mt76_wcid_alloc(u32 *mask, int size);
33 
34 static inline void
mt76_wcid_mask_set(u32 * mask,int idx)35 mt76_wcid_mask_set(u32 *mask, int idx)
36 {
37 	mask[idx / 32] |= BIT(idx % 32);
38 }
39 
40 static inline void
mt76_wcid_mask_clear(u32 * mask,int idx)41 mt76_wcid_mask_clear(u32 *mask, int idx)
42 {
43 	mask[idx / 32] &= ~BIT(idx % 32);
44 }
45 
46 static inline void
mt76_skb_set_moredata(struct sk_buff * skb,bool enable)47 mt76_skb_set_moredata(struct sk_buff *skb, bool enable)
48 {
49 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
50 
51 	if (enable)
52 		hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
53 	else
54 		hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_MOREDATA);
55 }
56 
57 int __mt76_worker_fn(void *ptr);
58 
59 static inline int
mt76_worker_setup(struct ieee80211_hw * hw,struct mt76_worker * w,void (* fn)(struct mt76_worker *),const char * name)60 mt76_worker_setup(struct ieee80211_hw *hw, struct mt76_worker *w,
61 		  void (*fn)(struct mt76_worker *),
62 		  const char *name)
63 {
64 	const char *dev_name = wiphy_name(hw->wiphy);
65 	int ret;
66 
67 	if (fn)
68 		w->fn = fn;
69 	w->task = kthread_run(__mt76_worker_fn, w,
70 			      "mt76-%s %s", name, dev_name);
71 
72 	if (IS_ERR(w->task)) {
73 		ret = PTR_ERR(w->task);
74 		w->task = NULL;
75 		return ret;
76 	}
77 
78 	return 0;
79 }
80 
mt76_worker_schedule(struct mt76_worker * w)81 static inline void mt76_worker_schedule(struct mt76_worker *w)
82 {
83 	if (!w->task)
84 		return;
85 
86 	if (!test_and_set_bit(MT76_WORKER_SCHEDULED, &w->state) &&
87 	    !test_bit(MT76_WORKER_RUNNING, &w->state))
88 		wake_up_process(w->task);
89 }
90 
mt76_worker_disable(struct mt76_worker * w)91 static inline void mt76_worker_disable(struct mt76_worker *w)
92 {
93 	if (!w->task)
94 		return;
95 
96 	kthread_park(w->task);
97 	WRITE_ONCE(w->state, 0);
98 }
99 
mt76_worker_enable(struct mt76_worker * w)100 static inline void mt76_worker_enable(struct mt76_worker *w)
101 {
102 	if (!w->task)
103 		return;
104 
105 	kthread_unpark(w->task);
106 	mt76_worker_schedule(w);
107 }
108 
mt76_worker_teardown(struct mt76_worker * w)109 static inline void mt76_worker_teardown(struct mt76_worker *w)
110 {
111 	if (!w->task)
112 		return;
113 
114 	kthread_stop(w->task);
115 	w->task = NULL;
116 }
117 
118 #endif
119