Debian Codegraph @

drivers/net/wireless/mediatek/mt76/scan.c

raw
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3 * Copyright (C) 2024 Felix Fietkau <[email protected]>
4 */
5 #include "mt76.h"
6
7 static void mt76_scan_complete(struct mt76_dev *dev, bool abort)
8 {
9 struct mt76_phy *phy = dev->scan.phy;
10 struct cfg80211_scan_info info = {
11 .aborted = abort,
12 };
13
14 if (!phy)
15 return;
16
17 clear_bit(MT76_SCANNING, &phy->state);
18
19 if (dev->scan.chan && phy->main_chandef.chan && phy->offchannel &&
20 !test_bit(MT76_MCU_RESET, &dev->phy.state))
21 mt76_set_channel(phy, &phy->main_chandef, false);
22 mt76_put_vif_phy_link(phy, dev->scan.vif, dev->scan.mlink);
23 memset(&dev->scan, 0, sizeof(dev->scan));
24 if (!test_bit(MT76_MCU_RESET, &dev->phy.state))
25 ieee80211_scan_completed(phy->hw, &info);
26 }
27
28 void mt76_abort_scan(struct mt76_dev *dev)
29 {
30 spin_lock_bh(&dev->scan_lock);
31 dev->scan.beacon_wait = false;
32 spin_unlock_bh(&dev->scan_lock);
33
34 cancel_delayed_work_sync(&dev->scan_work);
35 mt76_scan_complete(dev, true);
36 }
37 EXPORT_SYMBOL_GPL(mt76_abort_scan);
38
39 static void
40 mt76_scan_send_probe(struct mt76_dev *dev, struct cfg80211_ssid *ssid)
41 {
42 struct cfg80211_scan_request *req = dev->scan.req;
43 struct ieee80211_vif *vif = dev->scan.vif;
44 struct mt76_vif_link *mvif = dev->scan.mlink;
45 enum nl80211_band band = dev->scan.chan->band;
46 struct mt76_phy *phy = dev->scan.phy;
47 struct ieee80211_tx_info *info;
48 struct sk_buff *skb;
49
50 skb = ieee80211_probereq_get(phy->hw, vif->addr, ssid->ssid,
51 ssid->ssid_len, req->ie_len);
52 if (!skb)
53 return;
54
55 if (is_unicast_ether_addr(req->bssid)) {
56 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
57
58 ether_addr_copy(hdr->addr1, req->bssid);
59 ether_addr_copy(hdr->addr3, req->bssid);
60 }
61
62 if (req->ie_len)
63 skb_put_data(skb, req->ie, req->ie_len);
64
65 skb->priority = 7;
66 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
67
68 rcu_read_lock();
69
70 if (!ieee80211_tx_prepare_skb(phy->hw, vif, skb, band, NULL))
71 goto out;
72
73 info = IEEE80211_SKB_CB(skb);
74 if (req->no_cck)
75 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
76 info->control.flags |= IEEE80211_TX_CTRL_DONT_USE_RATE_MASK;
77
78 mt76_tx(phy, NULL, mvif->wcid, skb);
79
80 out:
81 rcu_read_unlock();
82 }
83
84 void mt76_scan_rx_beacon(struct mt76_dev *dev, struct ieee80211_channel *chan)
85 {
86 struct mt76_phy *phy;
87
88 spin_lock(&dev->scan_lock);
89
90 if (!dev->scan.beacon_wait || dev->scan.beacon_received ||
91 dev->scan.chan != chan)
92 goto out;
93
94 phy = dev->scan.phy;
95 if (!phy)
96 goto out;
97
98 dev->scan.beacon_received = true;
99 ieee80211_queue_delayed_work(phy->hw, &dev->scan_work, 0);
100
101 out:
102 spin_unlock(&dev->scan_lock);
103 }
104 EXPORT_SYMBOL_GPL(mt76_scan_rx_beacon);
105
106 void mt76_scan_work(struct work_struct *work)
107 {
108 struct mt76_dev *dev = container_of(work, struct mt76_dev,
109 scan_work.work);
110 struct cfg80211_scan_request *req = dev->scan.req;
111 struct cfg80211_chan_def chandef = {};
112 struct mt76_phy *phy = dev->scan.phy;
113 int duration = HZ / 9; /* ~110 ms */
114 bool beacon_rx, offchannel = true;
115 int i;
116
117 if (!phy || !req)
118 return;
119
120 spin_lock_bh(&dev->scan_lock);
121 beacon_rx = dev->scan.beacon_wait && dev->scan.beacon_received;
122 dev->scan.beacon_wait = false;
123 spin_unlock_bh(&dev->scan_lock);
124
125 if (beacon_rx)
126 goto probe;
127
128 if (dev->scan.chan_idx >= req->n_channels) {
129 mt76_scan_complete(dev, false);
130 return;
131 }
132
133 if (dev->scan.chan && phy->num_sta && phy->offchannel) {
134 dev->scan.chan = NULL;
135 mt76_set_channel(phy, &phy->main_chandef, false);
136 goto out;
137 }
138
139 dev->scan.chan = req->channels[dev->scan.chan_idx++];
140 cfg80211_chandef_create(&chandef, dev->scan.chan, NL80211_CHAN_HT20);
141 if (phy->main_chandef.chan == dev->scan.chan) {
142 chandef = phy->main_chandef;
143 offchannel = false;
144 }
145
146 mt76_set_channel(phy, &chandef, offchannel);
147
148 if (!req->n_ssids)
149 goto out;
150
151 if (chandef.chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) {
152 spin_lock_bh(&dev->scan_lock);
153 dev->scan.beacon_received = false;
154 dev->scan.beacon_wait = true;
155 spin_unlock_bh(&dev->scan_lock);
156 goto out;
157 }
158
159 probe:
160 if (phy->offchannel)
161 duration = HZ / 16; /* ~60 ms */
162 local_bh_disable();
163 for (i = 0; i < req->n_ssids; i++)
164 mt76_scan_send_probe(dev, &req->ssids[i]);
165 local_bh_enable();
166
167 out:
168 if (dev->scan.chan && phy->offchannel)
169 duration = max_t(int, duration,
170 msecs_to_jiffies(req->duration +
171 (req->duration >> 5)));
172
173 ieee80211_queue_delayed_work(dev->phy.hw, &dev->scan_work, duration);
174 }
175
176 int mt76_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
177 struct ieee80211_scan_request *req)
178 {
179 struct mt76_phy *phy = hw->priv;
180 struct mt76_dev *dev = phy->dev;
181 struct mt76_vif_link *mlink;
182 int ret = 0;
183
184 if (hw->wiphy->n_radio > 1) {
185 phy = dev->band_phys[req->req.channels[0]->band];
186 if (!phy)
187 return -EINVAL;
188 }
189
190 mutex_lock(&dev->mutex);
191
192 if (dev->scan.req || phy->roc_vif ||
193 test_bit(MT76_MCU_RESET, &dev->phy.state)) {
194 ret = -EBUSY;
195 goto out;
196 }
197
198 mlink = mt76_get_vif_phy_link(phy, vif);
199 if (IS_ERR(mlink)) {
200 ret = PTR_ERR(mlink);
201 goto out;
202 }
203
204 memset(&dev->scan, 0, sizeof(dev->scan));
205 dev->scan.req = &req->req;
206 dev->scan.vif = vif;
207 dev->scan.phy = phy;
208 dev->scan.mlink = mlink;
209 ieee80211_queue_delayed_work(dev->phy.hw, &dev->scan_work, 0);
210
211 out:
212 mutex_unlock(&dev->mutex);
213
214 return ret;
215 }
216 EXPORT_SYMBOL_GPL(mt76_hw_scan);
217
218 void mt76_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
219 {
220 struct mt76_phy *phy = hw->priv;
221
222 mt76_abort_scan(phy->dev);
223 }
224 EXPORT_SYMBOL_GPL(mt76_cancel_hw_scan);