1 /* SPDX-License-Identifier: ISC */
2 /* Copyright (C) 2020 MediaTek Inc. */
3
4 #ifndef __MT76_CONNAC_H
5 #define __MT76_CONNAC_H
6
7 #include "mt76.h"
8
9 #define MT76_CONNAC_SCAN_IE_LEN 600
10 #define MT76_CONNAC_MAX_NUM_SCHED_SCAN_INTERVAL 10
11 #define MT76_CONNAC_MAX_TIME_SCHED_SCAN_INTERVAL U16_MAX
12 #define MT76_CONNAC_MAX_SCHED_SCAN_SSID 10
13 #define MT76_CONNAC_MAX_SCAN_MATCH 16
14
15 #define MT76_CONNAC_MAX_WMM_SETS 4
16
17 #define MT76_CONNAC_COREDUMP_TIMEOUT (HZ / 20)
18 #define MT76_CONNAC_COREDUMP_SZ (1300 * 1024)
19
20 enum {
21 CMD_CBW_20MHZ = IEEE80211_STA_RX_BW_20,
22 CMD_CBW_40MHZ = IEEE80211_STA_RX_BW_40,
23 CMD_CBW_80MHZ = IEEE80211_STA_RX_BW_80,
24 CMD_CBW_160MHZ = IEEE80211_STA_RX_BW_160,
25 CMD_CBW_10MHZ,
26 CMD_CBW_5MHZ,
27 CMD_CBW_8080MHZ,
28
29 CMD_HE_MCS_BW80 = 0,
30 CMD_HE_MCS_BW160,
31 CMD_HE_MCS_BW8080,
32 CMD_HE_MCS_BW_NUM
33 };
34
35 enum {
36 HW_BSSID_0 = 0x0,
37 HW_BSSID_1,
38 HW_BSSID_2,
39 HW_BSSID_3,
40 HW_BSSID_MAX = HW_BSSID_3,
41 EXT_BSSID_START = 0x10,
42 EXT_BSSID_1,
43 EXT_BSSID_15 = 0x1f,
44 EXT_BSSID_MAX = EXT_BSSID_15,
45 REPEATER_BSSID_START = 0x20,
46 REPEATER_BSSID_MAX = 0x3f,
47 };
48
49 struct mt76_connac_pm {
50 bool enable:1;
51 bool enable_user:1;
52 bool ds_enable:1;
53 bool ds_enable_user:1;
54 bool suspended:1;
55
56 spinlock_t txq_lock;
57 struct {
58 struct mt76_wcid *wcid;
59 struct sk_buff *skb;
60 } tx_q[IEEE80211_NUM_ACS];
61
62 struct work_struct wake_work;
63 wait_queue_head_t wait;
64
65 struct {
66 spinlock_t lock;
67 u32 count;
68 } wake;
69 struct mutex mutex;
70
71 struct delayed_work ps_work;
72 unsigned long last_activity;
73 unsigned long idle_timeout;
74
75 struct {
76 unsigned long last_wake_event;
77 unsigned long awake_time;
78 unsigned long last_doze_event;
79 unsigned long doze_time;
80 unsigned int lp_wake;
81 } stats;
82 };
83
84 struct mt76_connac_coredump {
85 struct sk_buff_head msg_list;
86 struct delayed_work work;
87 unsigned long last_activity;
88 };
89
90 struct mt76_connac_sta_key_conf {
91 s8 keyidx;
92 u8 key[16];
93 };
94
95 extern const struct wiphy_wowlan_support mt76_connac_wowlan_support;
96
is_mt7922(struct mt76_dev * dev)97 static inline bool is_mt7922(struct mt76_dev *dev)
98 {
99 return mt76_chip(dev) == 0x7922;
100 }
101
is_mt7921(struct mt76_dev * dev)102 static inline bool is_mt7921(struct mt76_dev *dev)
103 {
104 return mt76_chip(dev) == 0x7961 || is_mt7922(dev);
105 }
106
is_mt7663(struct mt76_dev * dev)107 static inline bool is_mt7663(struct mt76_dev *dev)
108 {
109 return mt76_chip(dev) == 0x7663;
110 }
111
is_mt7915(struct mt76_dev * dev)112 static inline bool is_mt7915(struct mt76_dev *dev)
113 {
114 return mt76_chip(dev) == 0x7915;
115 }
116
is_mt7916(struct mt76_dev * dev)117 static inline bool is_mt7916(struct mt76_dev *dev)
118 {
119 return mt76_chip(dev) == 0x7906;
120 }
121
is_mt7986(struct mt76_dev * dev)122 static inline bool is_mt7986(struct mt76_dev *dev)
123 {
124 return mt76_chip(dev) == 0x7986;
125 }
126
is_mt7622(struct mt76_dev * dev)127 static inline bool is_mt7622(struct mt76_dev *dev)
128 {
129 if (!IS_ENABLED(CONFIG_MT7622_WMAC))
130 return false;
131
132 return mt76_chip(dev) == 0x7622;
133 }
134
is_mt7615(struct mt76_dev * dev)135 static inline bool is_mt7615(struct mt76_dev *dev)
136 {
137 return mt76_chip(dev) == 0x7615 || mt76_chip(dev) == 0x7611;
138 }
139
is_mt7611(struct mt76_dev * dev)140 static inline bool is_mt7611(struct mt76_dev *dev)
141 {
142 return mt76_chip(dev) == 0x7611;
143 }
144
is_connac_v1(struct mt76_dev * dev)145 static inline bool is_connac_v1(struct mt76_dev *dev)
146 {
147 return is_mt7615(dev) || is_mt7663(dev) || is_mt7622(dev);
148 }
149
mt76_connac_chan_bw(struct cfg80211_chan_def * chandef)150 static inline u8 mt76_connac_chan_bw(struct cfg80211_chan_def *chandef)
151 {
152 static const u8 width_to_bw[] = {
153 [NL80211_CHAN_WIDTH_40] = CMD_CBW_40MHZ,
154 [NL80211_CHAN_WIDTH_80] = CMD_CBW_80MHZ,
155 [NL80211_CHAN_WIDTH_80P80] = CMD_CBW_8080MHZ,
156 [NL80211_CHAN_WIDTH_160] = CMD_CBW_160MHZ,
157 [NL80211_CHAN_WIDTH_5] = CMD_CBW_5MHZ,
158 [NL80211_CHAN_WIDTH_10] = CMD_CBW_10MHZ,
159 [NL80211_CHAN_WIDTH_20] = CMD_CBW_20MHZ,
160 [NL80211_CHAN_WIDTH_20_NOHT] = CMD_CBW_20MHZ,
161 };
162
163 if (chandef->width >= ARRAY_SIZE(width_to_bw))
164 return 0;
165
166 return width_to_bw[chandef->width];
167 }
168
mt76_connac_lmac_mapping(u8 ac)169 static inline u8 mt76_connac_lmac_mapping(u8 ac)
170 {
171 /* LMAC uses the reverse order of mac80211 AC indexes */
172 return 3 - ac;
173 }
174
175 int mt76_connac_pm_wake(struct mt76_phy *phy, struct mt76_connac_pm *pm);
176 void mt76_connac_power_save_sched(struct mt76_phy *phy,
177 struct mt76_connac_pm *pm);
178 void mt76_connac_free_pending_tx_skbs(struct mt76_connac_pm *pm,
179 struct mt76_wcid *wcid);
180
181 static inline bool
mt76_connac_pm_ref(struct mt76_phy * phy,struct mt76_connac_pm * pm)182 mt76_connac_pm_ref(struct mt76_phy *phy, struct mt76_connac_pm *pm)
183 {
184 bool ret = false;
185
186 spin_lock_bh(&pm->wake.lock);
187 if (test_bit(MT76_STATE_PM, &phy->state))
188 goto out;
189
190 pm->wake.count++;
191 ret = true;
192 out:
193 spin_unlock_bh(&pm->wake.lock);
194
195 return ret;
196 }
197
198 static inline void
mt76_connac_pm_unref(struct mt76_phy * phy,struct mt76_connac_pm * pm)199 mt76_connac_pm_unref(struct mt76_phy *phy, struct mt76_connac_pm *pm)
200 {
201 spin_lock_bh(&pm->wake.lock);
202
203 pm->last_activity = jiffies;
204 if (--pm->wake.count == 0 &&
205 test_bit(MT76_STATE_MCU_RUNNING, &phy->state))
206 mt76_connac_power_save_sched(phy, pm);
207
208 spin_unlock_bh(&pm->wake.lock);
209 }
210
211 static inline bool
mt76_connac_skip_fw_pmctrl(struct mt76_phy * phy,struct mt76_connac_pm * pm)212 mt76_connac_skip_fw_pmctrl(struct mt76_phy *phy, struct mt76_connac_pm *pm)
213 {
214 struct mt76_dev *dev = phy->dev;
215 bool ret;
216
217 if (dev->token_count)
218 return true;
219
220 spin_lock_bh(&pm->wake.lock);
221 ret = pm->wake.count || test_and_set_bit(MT76_STATE_PM, &phy->state);
222 spin_unlock_bh(&pm->wake.lock);
223
224 return ret;
225 }
226
227 static inline void
mt76_connac_mutex_acquire(struct mt76_dev * dev,struct mt76_connac_pm * pm)228 mt76_connac_mutex_acquire(struct mt76_dev *dev, struct mt76_connac_pm *pm)
229 __acquires(&dev->mutex)
230 {
231 mutex_lock(&dev->mutex);
232 mt76_connac_pm_wake(&dev->phy, pm);
233 }
234
235 static inline void
mt76_connac_mutex_release(struct mt76_dev * dev,struct mt76_connac_pm * pm)236 mt76_connac_mutex_release(struct mt76_dev *dev, struct mt76_connac_pm *pm)
237 __releases(&dev->mutex)
238 {
239 mt76_connac_power_save_sched(&dev->phy, pm);
240 mutex_unlock(&dev->mutex);
241 }
242
243 void mt76_connac_pm_queue_skb(struct ieee80211_hw *hw,
244 struct mt76_connac_pm *pm,
245 struct mt76_wcid *wcid,
246 struct sk_buff *skb);
247 void mt76_connac_pm_dequeue_skbs(struct mt76_phy *phy,
248 struct mt76_connac_pm *pm);
249 void mt76_connac2_mac_write_txwi(struct mt76_dev *dev, __le32 *txwi,
250 struct sk_buff *skb, struct mt76_wcid *wcid,
251 struct ieee80211_key_conf *key, int pid,
252 u32 changed);
253
254 #endif /* __MT76_CONNAC_H */
255