1 /*
2 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include "mt76x2.h"
18
19 static int
mt76x2_start(struct ieee80211_hw * hw)20 mt76x2_start(struct ieee80211_hw *hw)
21 {
22 struct mt76x2_dev *dev = hw->priv;
23 int ret;
24
25 mutex_lock(&dev->mutex);
26
27 ret = mt76x2_mac_start(dev);
28 if (ret)
29 goto out;
30
31 ret = mt76x2_phy_start(dev);
32 if (ret)
33 goto out;
34
35 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mac_work,
36 MT_CALIBRATE_INTERVAL);
37
38 set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
39
40 out:
41 mutex_unlock(&dev->mutex);
42 return ret;
43 }
44
45 static void
mt76x2_stop(struct ieee80211_hw * hw)46 mt76x2_stop(struct ieee80211_hw *hw)
47 {
48 struct mt76x2_dev *dev = hw->priv;
49
50 mutex_lock(&dev->mutex);
51 clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
52 mt76x2_stop_hardware(dev);
53 mutex_unlock(&dev->mutex);
54 }
55
56 static int
mt76x2_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)57 mt76x2_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
58 {
59 struct mt76x2_dev *dev = hw->priv;
60 struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
61 unsigned int idx = 0;
62
63 if (vif->addr[0] & BIT(1))
64 idx = 1 + (((dev->mt76.macaddr[0] ^ vif->addr[0]) >> 2) & 7);
65
66 /*
67 * Client mode typically only has one configurable BSSID register,
68 * which is used for bssidx=0. This is linked to the MAC address.
69 * Since mac80211 allows changing interface types, and we cannot
70 * force the use of the primary MAC address for a station mode
71 * interface, we need some other way of configuring a per-interface
72 * remote BSSID.
73 * The hardware provides an AP-Client feature, where bssidx 0-7 are
74 * used for AP mode and bssidx 8-15 for client mode.
75 * We shift the station interface bss index by 8 to force the
76 * hardware to recognize the BSSID.
77 * The resulting bssidx mismatch for unicast frames is ignored by hw.
78 */
79 if (vif->type == NL80211_IFTYPE_STATION)
80 idx += 8;
81
82 mvif->idx = idx;
83 mvif->group_wcid.idx = MT_VIF_WCID(idx);
84 mvif->group_wcid.hw_key_idx = -1;
85 mt76x2_txq_init(dev, vif->txq);
86
87 return 0;
88 }
89
90 static int
mt76x2_set_channel(struct mt76x2_dev * dev,struct cfg80211_chan_def * chandef)91 mt76x2_set_channel(struct mt76x2_dev *dev, struct cfg80211_chan_def *chandef)
92 {
93 int ret;
94
95 cancel_delayed_work_sync(&dev->cal_work);
96
97 set_bit(MT76_RESET, &dev->mt76.state);
98
99 mt76_set_channel(&dev->mt76);
100
101 tasklet_disable(&dev->pre_tbtt_tasklet);
102 tasklet_disable(&dev->dfs_pd.dfs_tasklet);
103
104 mt76x2_mac_stop(dev, true);
105 ret = mt76x2_phy_set_channel(dev, chandef);
106
107 /* channel cycle counters read-and-clear */
108 mt76_rr(dev, MT_CH_IDLE);
109 mt76_rr(dev, MT_CH_BUSY);
110
111 mt76x2_dfs_init_params(dev);
112
113 mt76x2_mac_resume(dev);
114 tasklet_enable(&dev->dfs_pd.dfs_tasklet);
115 tasklet_enable(&dev->pre_tbtt_tasklet);
116
117 clear_bit(MT76_RESET, &dev->mt76.state);
118
119 mt76_txq_schedule_all(&dev->mt76);
120
121 return ret;
122 }
123
124 static int
mt76x2_config(struct ieee80211_hw * hw,u32 changed)125 mt76x2_config(struct ieee80211_hw *hw, u32 changed)
126 {
127 struct mt76x2_dev *dev = hw->priv;
128 int ret = 0;
129
130 mutex_lock(&dev->mutex);
131
132 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
133 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
134 dev->rxfilter |= MT_RX_FILTR_CFG_PROMISC;
135 else
136 dev->rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
137
138 mt76_wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
139 }
140
141 if (changed & IEEE80211_CONF_CHANGE_POWER) {
142 dev->txpower_conf = hw->conf.power_level * 2;
143
144 /* convert to per-chain power for 2x2 devices */
145 dev->txpower_conf -= 6;
146
147 if (test_bit(MT76_STATE_RUNNING, &dev->mt76.state)) {
148 mt76x2_phy_set_txpower(dev);
149 mt76x2_tx_set_txpwr_auto(dev, dev->txpower_conf);
150 }
151 }
152
153 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
154 ieee80211_stop_queues(hw);
155 ret = mt76x2_set_channel(dev, &hw->conf.chandef);
156 ieee80211_wake_queues(hw);
157 }
158
159 mutex_unlock(&dev->mutex);
160
161 return ret;
162 }
163
164 static void
mt76x2_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)165 mt76x2_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
166 struct ieee80211_bss_conf *info, u32 changed)
167 {
168 struct mt76x2_dev *dev = hw->priv;
169 struct mt76x2_vif *mvif = (struct mt76x2_vif *) vif->drv_priv;
170
171 mutex_lock(&dev->mutex);
172
173 if (changed & BSS_CHANGED_BSSID)
174 mt76x2_mac_set_bssid(dev, mvif->idx, info->bssid);
175
176 if (changed & BSS_CHANGED_BEACON_INT) {
177 mt76_rmw_field(dev, MT_BEACON_TIME_CFG,
178 MT_BEACON_TIME_CFG_INTVAL,
179 info->beacon_int << 4);
180 dev->beacon_int = info->beacon_int;
181 dev->tbtt_count = 0;
182 }
183
184 if (changed & BSS_CHANGED_BEACON_ENABLED) {
185 tasklet_disable(&dev->pre_tbtt_tasklet);
186 mt76x2_mac_set_beacon_enable(dev, mvif->idx,
187 info->enable_beacon);
188 tasklet_enable(&dev->pre_tbtt_tasklet);
189 }
190
191 if (changed & BSS_CHANGED_ERP_SLOT) {
192 int slottime = info->use_short_slot ? 9 : 20;
193
194 dev->slottime = slottime;
195 mt76x2_set_tx_ackto(dev);
196 }
197
198 mutex_unlock(&dev->mutex);
199 }
200
201 void
mt76x2_sta_ps(struct mt76_dev * mdev,struct ieee80211_sta * sta,bool ps)202 mt76x2_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps)
203 {
204 struct mt76x2_sta *msta = (struct mt76x2_sta *) sta->drv_priv;
205 struct mt76x2_dev *dev = container_of(mdev, struct mt76x2_dev, mt76);
206 int idx = msta->wcid.idx;
207
208 mt76_stop_tx_queues(&dev->mt76, sta, true);
209 mt76x2_mac_wcid_set_drop(dev, idx, ps);
210 }
211
212 static void
mt76x2_sw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const u8 * mac)213 mt76x2_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
214 const u8 *mac)
215 {
216 struct mt76x2_dev *dev = hw->priv;
217
218 tasklet_disable(&dev->pre_tbtt_tasklet);
219 set_bit(MT76_SCANNING, &dev->mt76.state);
220 }
221
222 static void
mt76x2_sw_scan_complete(struct ieee80211_hw * hw,struct ieee80211_vif * vif)223 mt76x2_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
224 {
225 struct mt76x2_dev *dev = hw->priv;
226
227 clear_bit(MT76_SCANNING, &dev->mt76.state);
228 tasklet_enable(&dev->pre_tbtt_tasklet);
229 }
230
231 static void
mt76x2_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)232 mt76x2_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
233 u32 queues, bool drop)
234 {
235 }
236
237 static int
mt76x2_get_txpower(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int * dbm)238 mt76x2_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif, int *dbm)
239 {
240 struct mt76x2_dev *dev = hw->priv;
241
242 *dbm = dev->txpower_cur / 2;
243
244 /* convert from per-chain power to combined output on 2x2 devices */
245 *dbm += 3;
246
247 return 0;
248 }
249
mt76x2_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)250 static void mt76x2_set_coverage_class(struct ieee80211_hw *hw,
251 s16 coverage_class)
252 {
253 struct mt76x2_dev *dev = hw->priv;
254
255 mutex_lock(&dev->mutex);
256 dev->coverage_class = coverage_class;
257 mt76x2_set_tx_ackto(dev);
258 mutex_unlock(&dev->mutex);
259 }
260
261 static int
mt76x2_set_tim(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool set)262 mt76x2_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
263 {
264 return 0;
265 }
266
mt76x2_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)267 static int mt76x2_set_antenna(struct ieee80211_hw *hw, u32 tx_ant,
268 u32 rx_ant)
269 {
270 struct mt76x2_dev *dev = hw->priv;
271
272 if (!tx_ant || tx_ant > 3 || tx_ant != rx_ant)
273 return -EINVAL;
274
275 mutex_lock(&dev->mutex);
276
277 dev->chainmask = (tx_ant == 3) ? 0x202 : 0x101;
278 dev->mt76.antenna_mask = tx_ant;
279
280 mt76_set_stream_caps(&dev->mt76, true);
281 mt76x2_phy_set_antenna(dev);
282
283 mutex_unlock(&dev->mutex);
284
285 return 0;
286 }
287
mt76x2_get_antenna(struct ieee80211_hw * hw,u32 * tx_ant,u32 * rx_ant)288 static int mt76x2_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
289 u32 *rx_ant)
290 {
291 struct mt76x2_dev *dev = hw->priv;
292
293 mutex_lock(&dev->mutex);
294 *tx_ant = dev->mt76.antenna_mask;
295 *rx_ant = dev->mt76.antenna_mask;
296 mutex_unlock(&dev->mutex);
297
298 return 0;
299 }
300
301 static int
mt76x2_set_rts_threshold(struct ieee80211_hw * hw,u32 val)302 mt76x2_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
303 {
304 struct mt76x2_dev *dev = hw->priv;
305
306 if (val != ~0 && val > 0xffff)
307 return -EINVAL;
308
309 mutex_lock(&dev->mutex);
310 mt76x2_mac_set_tx_protection(dev, val);
311 mutex_unlock(&dev->mutex);
312
313 return 0;
314 }
315
316 const struct ieee80211_ops mt76x2_ops = {
317 .tx = mt76x2_tx,
318 .start = mt76x2_start,
319 .stop = mt76x2_stop,
320 .add_interface = mt76x2_add_interface,
321 .remove_interface = mt76x2_remove_interface,
322 .config = mt76x2_config,
323 .configure_filter = mt76x2_configure_filter,
324 .bss_info_changed = mt76x2_bss_info_changed,
325 .sta_add = mt76x2_sta_add,
326 .sta_remove = mt76x2_sta_remove,
327 .set_key = mt76x2_set_key,
328 .conf_tx = mt76x2_conf_tx,
329 .sw_scan_start = mt76x2_sw_scan,
330 .sw_scan_complete = mt76x2_sw_scan_complete,
331 .flush = mt76x2_flush,
332 .ampdu_action = mt76x2_ampdu_action,
333 .get_txpower = mt76x2_get_txpower,
334 .wake_tx_queue = mt76_wake_tx_queue,
335 .sta_rate_tbl_update = mt76x2_sta_rate_tbl_update,
336 .release_buffered_frames = mt76_release_buffered_frames,
337 .set_coverage_class = mt76x2_set_coverage_class,
338 .get_survey = mt76_get_survey,
339 .set_tim = mt76x2_set_tim,
340 .set_antenna = mt76x2_set_antenna,
341 .get_antenna = mt76x2_get_antenna,
342 .set_rts_threshold = mt76x2_set_rts_threshold,
343 };
344
345