xref: /freebsd/sys/contrib/dev/mediatek/mt76/util.h (revision 1d386b48)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020,2022 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 static inline bool
71 mt76_wcid_mask_test(u32 *mask, u16 bit)
72 {
73 
74 	return (mask[bit / 32] & BIT(bit % 32));
75 }
76 
77 /* See, e.g., __mt76_worker_fn for some details. */
78 static inline int
79 mt76_worker_setup(struct ieee80211_hw *hw, struct mt76_worker *w,
80     void (*wfunc)(struct mt76_worker *), const char *name)
81 {
82 	int rc;
83 
84 	if (wfunc)
85 		w->fn = wfunc;
86 
87 	w->task = kthread_run(__mt76_worker_fn, w,
88 	    "mt76-worker");
89 
90 	if (!IS_ERR(w->task))
91 		return (0);
92 
93 	rc = PTR_ERR(w->task);
94 	w->task = NULL;
95 	return (rc);
96 }
97 
98 static inline void
99 mt76_worker_schedule(struct mt76_worker *w)
100 {
101 
102 	if (w->task == NULL)
103 		return;
104 
105 	if (!test_and_set_bit(MT76_WORKER_SCHEDULED, &w->state) ||
106 	    !test_bit(MT76_WORKER_RUNNING, &w->state))
107 		wake_up_process(w->task);
108 }
109 
110 static inline void
111 mt76_worker_enable(struct mt76_worker *w)
112 {
113 
114 	if (w->task == NULL)
115 		return;
116 
117 	kthread_unpark(w->task);
118 	mt76_worker_schedule(w);
119 }
120 
121 static inline void
122 mt76_worker_disable(struct mt76_worker *w)
123 {
124 
125 	if (w->task == NULL)
126 		return;
127 
128 	kthread_park(w->task);
129 	WRITE_ONCE(w->state, 0);
130 }
131 
132 static inline void
133 mt76_worker_teardown(struct mt76_worker *w)
134 {
135 
136 	if (w->task == NULL)
137 		return;
138 
139 	kthread_stop(w->task);
140 	w->task = NULL;
141 }
142 
143 static inline void
144 mt76_skb_set_moredata(struct sk_buff *skb, bool moredata)
145 {
146 	/*
147 	 * This would be net80211::IEEE80211_FC1_MORE_DATA
148 	 * Implement it as mostly LinuxKPI 802.11 to avoid
149 	 * further header pollution and possible conflicts.
150 	 */
151 	struct ieee80211_hdr *hdr;
152 	uint16_t val;
153 
154 	hdr = (struct ieee80211_hdr *)skb->data;
155 	val = cpu_to_le16(IEEE80211_FC1_MORE_DATA << 8);
156 	if (!moredata)
157 		hdr->frame_control &= ~val;
158 	else
159 		hdr->frame_control |= val;
160 }
161 
162 #endif	/* _MT76_UTIL_H */
163