xref: /linux/drivers/net/wireless/mediatek/mt76/scan.c (revision 2d7eaeb05ae265bd64270376e6a2c0d80244771f)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (C) 2024 Felix Fietkau <nbd@nbd.name>
4  */
5 #include "mt76.h"
6 
7 static struct mt76_phy *
8 mt76_scan_chan_phy(struct ieee80211_hw *hw, struct ieee80211_channel *chan)
9 {
10 	struct mt76_phy *phy = hw->priv;
11 
12 	if (hw->wiphy->n_radio > 1)
13 		return phy->dev->band_phys[chan->band];
14 
15 	return phy;
16 }
17 
18 static void mt76_scan_phy_complete(struct mt76_phy *phy, bool abort)
19 {
20 	struct mt76_dev *dev = phy->dev;
21 	struct cfg80211_scan_info info = {};
22 	struct ieee80211_hw *hw = phy->hw;
23 
24 	lockdep_assert_held(&dev->mutex);
25 
26 	if (!mt76_phy_scanning(phy))
27 		return;
28 
29 	clear_bit(MT76_SCANNING, &phy->state);
30 
31 	/* Re-program the operating channel even when the scan never left it:
32 	 * any channel set during the scan ran with MT76_SCANNING held, which
33 	 * left DFS radar detection disabled
34 	 */
35 	if (phy->main_chandef.chan &&
36 	    !test_bit(MT76_MCU_RESET, &dev->phy.state)) {
37 		bool offchannel = phy->offchannel;
38 
39 		__mt76_set_channel(phy, &phy->main_chandef, false);
40 		if (offchannel)
41 			mt76_offchannel_notify(phy, false);
42 	} else {
43 		/* A phy that has no operating channel has nothing to restore,
44 		 * but the flag also gates the TX queues of every station whose
45 		 * wcid points at this band, so leaving it set stalls them until
46 		 * something else happens to set a channel.
47 		 */
48 		phy->offchannel = false;
49 	}
50 	mt76_put_vif_phy_link(phy, dev->scan.vif, phy->scan.mlink);
51 	memset(&phy->scan, 0, sizeof(phy->scan));
52 
53 	clear_bit(phy->band_idx, &dev->scan.phy_mask);
54 	if (abort)
55 		dev->scan.aborted = true;
56 
57 	if (dev->scan.phy_mask)
58 		return;
59 
60 	info.aborted = dev->scan.aborted;
61 	memset(&dev->scan, 0, sizeof(dev->scan));
62 	ieee80211_scan_completed(hw, &info);
63 }
64 
65 void mt76_abort_scan(struct mt76_dev *dev)
66 {
67 	struct mt76_phy *phy;
68 	int i;
69 
70 	for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
71 		phy = dev->phys[i];
72 		if (!phy)
73 			continue;
74 
75 		spin_lock_bh(&dev->scan_lock);
76 		phy->scan.beacon_wait = false;
77 		spin_unlock_bh(&dev->scan_lock);
78 
79 		disable_delayed_work_sync(&phy->scan_work);
80 	}
81 
82 	mutex_lock(&dev->mutex);
83 	for (i = 0; i < ARRAY_SIZE(dev->phys); i++) {
84 		phy = dev->phys[i];
85 		if (!phy)
86 			continue;
87 
88 		mt76_scan_phy_complete(phy, true);
89 		enable_delayed_work(&phy->scan_work);
90 	}
91 	mutex_unlock(&dev->mutex);
92 }
93 EXPORT_SYMBOL_GPL(mt76_abort_scan);
94 
95 static void
96 mt76_scan_send_probe(struct mt76_phy *phy, struct cfg80211_ssid *ssid)
97 {
98 	struct mt76_dev *dev = phy->dev;
99 	struct cfg80211_scan_request *req = &dev->scan.req->req;
100 	struct ieee80211_scan_ies *ies = &dev->scan.req->ies;
101 	struct ieee80211_vif *vif = dev->scan.vif;
102 	struct mt76_vif_link *mvif = phy->scan.mlink;
103 	enum nl80211_band band = phy->scan.chan->band;
104 	struct ieee80211_tx_info *info;
105 	struct sk_buff *skb;
106 	u8 link_id;
107 
108 	skb = ieee80211_probereq_get(phy->hw, vif->addr, ssid->ssid,
109 				     ssid->ssid_len,
110 				     ies->len[band] + ies->common_ie_len);
111 	if (!skb)
112 		return;
113 
114 	if (is_unicast_ether_addr(req->bssid)) {
115 		struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
116 
117 		ether_addr_copy(hdr->addr1, req->bssid);
118 		ether_addr_copy(hdr->addr3, req->bssid);
119 	}
120 
121 	if (ies->len[band])
122 		skb_put_data(skb, ies->ies[band], ies->len[band]);
123 	if (ies->common_ie_len)
124 		skb_put_data(skb, ies->common_ies, ies->common_ie_len);
125 
126 	skb->priority = 7;
127 	skb_set_queue_mapping(skb, IEEE80211_AC_VO);
128 
129 	rcu_read_lock();
130 
131 	if (!ieee80211_tx_prepare_skb(phy->hw, vif, skb, band, NULL))
132 		goto out;
133 
134 	info = IEEE80211_SKB_CB(skb);
135 	if (req->no_cck)
136 		info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
137 	info->control.flags |= IEEE80211_TX_CTRL_DONT_USE_RATE_MASK;
138 
139 	link_id = mvif->wcid ? mvif->wcid->link_id : IEEE80211_LINK_UNSPECIFIED;
140 	info->control.flags &= ~IEEE80211_TX_CTRL_MLO_LINK;
141 	info->control.flags |= u32_encode_bits(link_id, IEEE80211_TX_CTRL_MLO_LINK);
142 
143 	mt76_tx(phy, NULL, mvif->wcid, skb);
144 
145 out:
146 	rcu_read_unlock();
147 }
148 
149 void mt76_scan_rx_beacon(struct mt76_phy *phy, struct ieee80211_channel *chan)
150 {
151 	struct mt76_dev *dev = phy->dev;
152 
153 	spin_lock(&dev->scan_lock);
154 
155 	if (!phy->scan.beacon_wait || phy->scan.beacon_received ||
156 	    phy->scan.chan != chan)
157 		goto out;
158 
159 	phy->scan.beacon_received = true;
160 	ieee80211_queue_delayed_work(phy->hw, &phy->scan_work, 0);
161 
162 out:
163 	spin_unlock(&dev->scan_lock);
164 }
165 
166 static int mt76_scan_next_chan_idx(struct mt76_phy *phy)
167 {
168 	struct cfg80211_scan_request *req = &phy->dev->scan.req->req;
169 	int i;
170 
171 	for (i = phy->scan.chan_idx; i < req->n_channels; i++)
172 		if (mt76_scan_chan_phy(phy->hw, req->channels[i]) == phy)
173 			return i;
174 
175 	return -1;
176 }
177 
178 void mt76_scan_work(struct work_struct *work)
179 {
180 	struct mt76_phy *phy = container_of(work, struct mt76_phy,
181 					    scan_work.work);
182 	struct mt76_dev *dev = phy->dev;
183 	struct cfg80211_scan_request *req;
184 	struct cfg80211_chan_def chandef = {};
185 	int duration = HZ / 9; /* ~110 ms */
186 	bool beacon_rx, offchannel = true;
187 	int i, idx;
188 
189 	if (!dev->scan.req || !mt76_phy_scanning(phy))
190 		return;
191 
192 	req = &dev->scan.req->req;
193 
194 	spin_lock_bh(&dev->scan_lock);
195 	beacon_rx = phy->scan.beacon_wait && phy->scan.beacon_received;
196 	phy->scan.beacon_wait = false;
197 	spin_unlock_bh(&dev->scan_lock);
198 
199 	if (beacon_rx)
200 		goto probe;
201 
202 	idx = mt76_scan_next_chan_idx(phy);
203 	if (idx < 0) {
204 		mutex_lock(&dev->mutex);
205 		mt76_scan_phy_complete(phy, false);
206 		mutex_unlock(&dev->mutex);
207 		return;
208 	}
209 
210 	if (phy->scan.chan && phy->num_sta && phy->offchannel) {
211 		phy->scan.chan = NULL;
212 		mt76_set_channel(phy, &phy->main_chandef, false);
213 		mt76_offchannel_notify(phy, false);
214 		goto out;
215 	}
216 
217 	phy->scan.chan = req->channels[idx];
218 	phy->scan.chan_idx = idx + 1;
219 	offchannel = mt76_offchannel_chandef(phy, phy->scan.chan, &chandef);
220 
221 	if (offchannel)
222 		mt76_offchannel_notify(phy, true);
223 	mt76_set_channel(phy, &chandef, offchannel);
224 
225 	if (!req->n_ssids)
226 		goto out;
227 
228 	if (chandef.chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) {
229 		spin_lock_bh(&dev->scan_lock);
230 		phy->scan.beacon_received = false;
231 		phy->scan.beacon_wait = true;
232 		spin_unlock_bh(&dev->scan_lock);
233 		goto out;
234 	}
235 
236 probe:
237 	if (phy->offchannel)
238 		duration = HZ / 16; /* ~60 ms */
239 	local_bh_disable();
240 	for (i = 0; i < req->n_ssids; i++)
241 		mt76_scan_send_probe(phy, &req->ssids[i]);
242 	local_bh_enable();
243 
244 out:
245 	if (phy->scan.chan && phy->offchannel)
246 		duration = max_t(int, duration,
247 			         msecs_to_jiffies(req->duration +
248 						  (req->duration >> 5)));
249 
250 	ieee80211_queue_delayed_work(phy->hw, &phy->scan_work, duration);
251 }
252 
253 int mt76_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
254 		 struct ieee80211_scan_request *req)
255 {
256 	struct mt76_phy *phy = hw->priv;
257 	struct mt76_dev *dev = phy->dev;
258 	unsigned long phy_mask = 0;
259 	struct mt76_vif_link *mlink;
260 	int i, ret = 0;
261 
262 	for (i = 0; i < req->req.n_channels; i++) {
263 		phy = mt76_scan_chan_phy(hw, req->req.channels[i]);
264 		if (!phy)
265 			return -EINVAL;
266 
267 		phy_mask |= BIT(phy->band_idx);
268 	}
269 
270 	mutex_lock(&dev->mutex);
271 
272 	if (dev->scan.req || test_bit(MT76_MCU_RESET, &dev->phy.state) ||
273 	    test_bit(MT76_RESTART, &dev->phy.state)) {
274 		ret = -EBUSY;
275 		goto out;
276 	}
277 
278 	for_each_set_bit(i, &phy_mask, ARRAY_SIZE(dev->phys)) {
279 		if (dev->phys[i]->roc_vif) {
280 			ret = -EBUSY;
281 			goto out;
282 		}
283 	}
284 
285 	for_each_set_bit(i, &phy_mask, ARRAY_SIZE(dev->phys)) {
286 		phy = dev->phys[i];
287 		mlink = mt76_get_vif_phy_link(phy, vif);
288 		if (IS_ERR(mlink)) {
289 			ret = PTR_ERR(mlink);
290 			goto put_links;
291 		}
292 
293 		memset(&phy->scan, 0, sizeof(phy->scan));
294 		phy->scan.mlink = mlink;
295 	}
296 
297 	dev->scan.req = req;
298 	dev->scan.vif = vif;
299 	dev->scan.phy_mask = phy_mask;
300 	dev->scan.aborted = false;
301 
302 	for_each_set_bit(i, &phy_mask, ARRAY_SIZE(dev->phys)) {
303 		phy = dev->phys[i];
304 		set_bit(MT76_SCANNING, &phy->state);
305 		ieee80211_queue_delayed_work(phy->hw, &phy->scan_work, 0);
306 	}
307 
308 	goto out;
309 
310 put_links:
311 	for_each_set_bit(i, &phy_mask, ARRAY_SIZE(dev->phys)) {
312 		phy = dev->phys[i];
313 		if (!phy->scan.mlink)
314 			break;
315 
316 		mt76_put_vif_phy_link(phy, vif, phy->scan.mlink);
317 		phy->scan.mlink = NULL;
318 	}
319 
320 out:
321 	mutex_unlock(&dev->mutex);
322 
323 	return ret;
324 }
325 EXPORT_SYMBOL_GPL(mt76_hw_scan);
326 
327 void mt76_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
328 {
329 	struct mt76_phy *phy = hw->priv;
330 
331 	mt76_abort_scan(phy->dev);
332 }
333 EXPORT_SYMBOL_GPL(mt76_cancel_hw_scan);
334