xref: /freebsd/sys/contrib/dev/mediatek/mt76/util.h (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020,2022-2023 Bjoern A. Zeeb <bz@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #ifndef	_MT76_UTIL_H
31 #define	_MT76_UTIL_H
32 
33 #include <linux/kthread.h>
34 
35 struct mt76_worker
36 {
37 	void(*fn)(struct mt76_worker *);
38 	struct task_struct *task;
39 	unsigned long state;
40 };
41 
42 enum mt76_worker_state {
43 	MT76_WORKER_SCHEDULED,
44 	MT76_WORKER_RUNNING,
45 };
46 
47 #if 0
48 bool __mt76_poll(struct mt76_dev *, u32, u32, u32, int);
49 bool __mt76_poll_msec(struct mt76_dev *, u32, u32, u32, int);
50 int mt76_get_min_avg_rssi(struct mt76_dev *, bool);
51 #endif
52 int mt76_wcid_alloc(u32 *, int);
53 int __mt76_worker_fn(void *);
54 
55 /* wcid_phy_mask is [32] */
56 static inline void
57 mt76_wcid_mask_set(u32 *mask, u16 bit)
58 {
59 
60 	mask[bit / 32] |= BIT(bit % 32);
61 }
62 
63 static inline void
64 mt76_wcid_mask_clear(u32 *mask, u16 bit)
65 {
66 
67 	mask[bit / 32] &= ~BIT(bit % 32);
68 }
69 
70 /* See, e.g., __mt76_worker_fn for some details. */
71 static inline int
72 mt76_worker_setup(struct ieee80211_hw *hw, struct mt76_worker *w,
73     void (*wfunc)(struct mt76_worker *), const char *name)
74 {
75 	int rc;
76 
77 	if (wfunc)
78 		w->fn = wfunc;
79 
80 	w->task = kthread_run(__mt76_worker_fn, w,
81 	    "mt76-worker");
82 
83 	if (!IS_ERR(w->task))
84 		return (0);
85 
86 	rc = PTR_ERR(w->task);
87 	w->task = NULL;
88 	return (rc);
89 }
90 
91 static inline void
92 mt76_worker_schedule(struct mt76_worker *w)
93 {
94 
95 	if (w->task == NULL)
96 		return;
97 
98 	if (!test_and_set_bit(MT76_WORKER_SCHEDULED, &w->state) ||
99 	    !test_bit(MT76_WORKER_RUNNING, &w->state))
100 		wake_up_process(w->task);
101 }
102 
103 static inline void
104 mt76_worker_enable(struct mt76_worker *w)
105 {
106 
107 	if (w->task == NULL)
108 		return;
109 
110 	kthread_unpark(w->task);
111 	mt76_worker_schedule(w);
112 }
113 
114 static inline void
115 mt76_worker_disable(struct mt76_worker *w)
116 {
117 
118 	if (w->task == NULL)
119 		return;
120 
121 	kthread_park(w->task);
122 	WRITE_ONCE(w->state, 0);
123 }
124 
125 static inline void
126 mt76_worker_teardown(struct mt76_worker *w)
127 {
128 
129 	if (w->task == NULL)
130 		return;
131 
132 	kthread_stop(w->task);
133 	w->task = NULL;
134 }
135 
136 static inline void
137 mt76_skb_set_moredata(struct sk_buff *skb, bool moredata)
138 {
139 	/*
140 	 * This would be net80211::IEEE80211_FC1_MORE_DATA
141 	 * Implement it as mostly LinuxKPI 802.11 to avoid
142 	 * further header pollution and possible conflicts.
143 	 */
144 	struct ieee80211_hdr *hdr;
145 	uint16_t val;
146 
147 	hdr = (struct ieee80211_hdr *)skb->data;
148 	val = cpu_to_le16(IEEE80211_FC1_MORE_DATA << 8);
149 	if (!moredata)
150 		hdr->frame_control &= ~val;
151 	else
152 		hdr->frame_control |= val;
153 }
154 
155 #endif	/* _MT76_UTIL_H */
156