1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Scan related functions.
4  *
5  * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6  * Copyright (c) 2010, ST-Ericsson
7  */
8 #include <net/mac80211.h>
9 
10 #include "scan.h"
11 #include "wfx.h"
12 #include "sta.h"
13 #include "hif_tx_mib.h"
14 
wfx_ieee80211_scan_completed_compat(struct ieee80211_hw * hw,bool aborted)15 static void wfx_ieee80211_scan_completed_compat(struct ieee80211_hw *hw, bool aborted)
16 {
17 	struct cfg80211_scan_info info = {
18 		.aborted = aborted,
19 	};
20 
21 	ieee80211_scan_completed(hw, &info);
22 }
23 
update_probe_tmpl(struct wfx_vif * wvif,struct cfg80211_scan_request * req)24 static int update_probe_tmpl(struct wfx_vif *wvif, struct cfg80211_scan_request *req)
25 {
26 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
27 	struct sk_buff *skb;
28 
29 	skb = ieee80211_probereq_get(wvif->wdev->hw, vif->addr, NULL, 0,
30 				     req->ie_len);
31 	if (!skb)
32 		return -ENOMEM;
33 
34 	skb_put_data(skb, req->ie, req->ie_len);
35 	wfx_hif_set_template_frame(wvif, skb, HIF_TMPLT_PRBREQ, 0);
36 	dev_kfree_skb(skb);
37 	return 0;
38 }
39 
send_scan_req(struct wfx_vif * wvif,struct cfg80211_scan_request * req,int start_idx)40 static int send_scan_req(struct wfx_vif *wvif, struct cfg80211_scan_request *req, int start_idx)
41 {
42 	struct ieee80211_vif *vif = wvif_to_vif(wvif);
43 	struct ieee80211_channel *ch_start, *ch_cur;
44 	int i, ret;
45 
46 	for (i = start_idx; i < req->n_channels; i++) {
47 		ch_start = req->channels[start_idx];
48 		ch_cur = req->channels[i];
49 		WARN(ch_cur->band != NL80211_BAND_2GHZ, "band not supported");
50 		if (ch_cur->max_power != ch_start->max_power)
51 			break;
52 		if ((ch_cur->flags ^ ch_start->flags) & IEEE80211_CHAN_NO_IR)
53 			break;
54 	}
55 	wfx_tx_lock_flush(wvif->wdev);
56 	wvif->scan_abort = false;
57 	reinit_completion(&wvif->scan_complete);
58 	ret = wfx_hif_scan(wvif, req, start_idx, i - start_idx);
59 	if (ret) {
60 		wfx_tx_unlock(wvif->wdev);
61 		return -EIO;
62 	}
63 	ret = wait_for_completion_timeout(&wvif->scan_complete, 1 * HZ);
64 	if (!ret) {
65 		wfx_hif_stop_scan(wvif);
66 		ret = wait_for_completion_timeout(&wvif->scan_complete, 1 * HZ);
67 		dev_dbg(wvif->wdev->dev, "scan timeout (%d channels done)\n",
68 			wvif->scan_nb_chan_done);
69 	}
70 	if (!ret) {
71 		dev_err(wvif->wdev->dev, "scan didn't stop\n");
72 		ret = -ETIMEDOUT;
73 	} else if (wvif->scan_abort) {
74 		dev_notice(wvif->wdev->dev, "scan abort\n");
75 		ret = -ECONNABORTED;
76 	} else if (wvif->scan_nb_chan_done > i - start_idx) {
77 		ret = -EIO;
78 	} else {
79 		ret = wvif->scan_nb_chan_done;
80 	}
81 	if (req->channels[start_idx]->max_power != vif->bss_conf.txpower)
82 		wfx_hif_set_output_power(wvif, vif->bss_conf.txpower);
83 	wfx_tx_unlock(wvif->wdev);
84 	return ret;
85 }
86 
87 /* It is not really necessary to run scan request asynchronously. However,
88  * there is a bug in "iw scan" when ieee80211_scan_completed() is called before
89  * wfx_hw_scan() return
90  */
wfx_hw_scan_work(struct work_struct * work)91 void wfx_hw_scan_work(struct work_struct *work)
92 {
93 	struct wfx_vif *wvif = container_of(work, struct wfx_vif, scan_work);
94 	struct ieee80211_scan_request *hw_req = wvif->scan_req;
95 	int chan_cur, ret, err;
96 
97 	mutex_lock(&wvif->wdev->conf_mutex);
98 	mutex_lock(&wvif->wdev->scan_lock);
99 	if (wvif->join_in_progress) {
100 		dev_info(wvif->wdev->dev, "abort in-progress REQ_JOIN");
101 		wfx_reset(wvif);
102 	}
103 	update_probe_tmpl(wvif, &hw_req->req);
104 	chan_cur = 0;
105 	err = 0;
106 	do {
107 		ret = send_scan_req(wvif, &hw_req->req, chan_cur);
108 		if (ret > 0) {
109 			chan_cur += ret;
110 			err = 0;
111 		}
112 		if (!ret)
113 			err++;
114 		if (err > 2) {
115 			dev_err(wvif->wdev->dev, "scan has not been able to start\n");
116 			ret = -ETIMEDOUT;
117 		}
118 	} while (ret >= 0 && chan_cur < hw_req->req.n_channels);
119 	mutex_unlock(&wvif->wdev->scan_lock);
120 	mutex_unlock(&wvif->wdev->conf_mutex);
121 	wfx_ieee80211_scan_completed_compat(wvif->wdev->hw, ret < 0);
122 }
123 
wfx_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)124 int wfx_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
125 		struct ieee80211_scan_request *hw_req)
126 {
127 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
128 
129 	WARN_ON(hw_req->req.n_channels > HIF_API_MAX_NB_CHANNELS);
130 	wvif->scan_req = hw_req;
131 	schedule_work(&wvif->scan_work);
132 	return 0;
133 }
134 
wfx_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)135 void wfx_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
136 {
137 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
138 
139 	wvif->scan_abort = true;
140 	wfx_hif_stop_scan(wvif);
141 }
142 
wfx_scan_complete(struct wfx_vif * wvif,int nb_chan_done)143 void wfx_scan_complete(struct wfx_vif *wvif, int nb_chan_done)
144 {
145 	wvif->scan_nb_chan_done = nb_chan_done;
146 	complete(&wvif->scan_complete);
147 }
148 
wfx_remain_on_channel_work(struct work_struct * work)149 void wfx_remain_on_channel_work(struct work_struct *work)
150 {
151 	struct wfx_vif *wvif = container_of(work, struct wfx_vif, remain_on_channel_work);
152 	struct ieee80211_channel *chan = wvif->remain_on_channel_chan;
153 	int duration = wvif->remain_on_channel_duration;
154 	int ret;
155 
156 	/* Hijack scan request to implement Remain-On-Channel */
157 	mutex_lock(&wvif->wdev->conf_mutex);
158 	mutex_lock(&wvif->wdev->scan_lock);
159 	if (wvif->join_in_progress) {
160 		dev_info(wvif->wdev->dev, "abort in-progress REQ_JOIN");
161 		wfx_reset(wvif);
162 	}
163 	wfx_tx_flush(wvif->wdev);
164 
165 	reinit_completion(&wvif->scan_complete);
166 	ret = wfx_hif_scan_uniq(wvif, chan, duration);
167 	if (ret)
168 		goto end;
169 	ieee80211_ready_on_channel(wvif->wdev->hw);
170 	ret = wait_for_completion_timeout(&wvif->scan_complete,
171 					  msecs_to_jiffies(duration * 120 / 100));
172 	if (!ret) {
173 		wfx_hif_stop_scan(wvif);
174 		ret = wait_for_completion_timeout(&wvif->scan_complete, 1 * HZ);
175 		dev_dbg(wvif->wdev->dev, "roc timeout\n");
176 	}
177 	if (!ret)
178 		dev_err(wvif->wdev->dev, "roc didn't stop\n");
179 	ieee80211_remain_on_channel_expired(wvif->wdev->hw);
180 end:
181 	mutex_unlock(&wvif->wdev->scan_lock);
182 	mutex_unlock(&wvif->wdev->conf_mutex);
183 	wfx_bh_request_tx(wvif->wdev);
184 }
185 
wfx_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * chan,int duration,enum ieee80211_roc_type type)186 int wfx_remain_on_channel(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
187 			  struct ieee80211_channel *chan, int duration,
188 			  enum ieee80211_roc_type type)
189 {
190 	struct wfx_dev *wdev = hw->priv;
191 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
192 
193 	if (wfx_api_older_than(wdev, 3, 10))
194 		return -EOPNOTSUPP;
195 
196 	wvif->remain_on_channel_duration = duration;
197 	wvif->remain_on_channel_chan = chan;
198 	schedule_work(&wvif->remain_on_channel_work);
199 	return 0;
200 }
201 
wfx_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)202 int wfx_cancel_remain_on_channel(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
203 {
204 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
205 
206 	wfx_hif_stop_scan(wvif);
207 	flush_work(&wvif->remain_on_channel_work);
208 	return 0;
209 }
210