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 
__ieee80211_scan_completed_compat(struct ieee80211_hw * hw,bool aborted)15 static void __ieee80211_scan_completed_compat(struct ieee80211_hw *hw,
16 					      bool aborted)
17 {
18 	struct cfg80211_scan_info info = {
19 		.aborted = aborted,
20 	};
21 
22 	ieee80211_scan_completed(hw, &info);
23 }
24 
update_probe_tmpl(struct wfx_vif * wvif,struct cfg80211_scan_request * req)25 static int update_probe_tmpl(struct wfx_vif *wvif,
26 			     struct cfg80211_scan_request *req)
27 {
28 	struct sk_buff *skb;
29 
30 	skb = ieee80211_probereq_get(wvif->wdev->hw, wvif->vif->addr,
31 				     NULL, 0, req->ie_len);
32 	if (!skb)
33 		return -ENOMEM;
34 
35 	skb_put_data(skb, req->ie, req->ie_len);
36 	hif_set_template_frame(wvif, skb, HIF_TMPLT_PRBREQ, 0);
37 	dev_kfree_skb(skb);
38 	return 0;
39 }
40 
send_scan_req(struct wfx_vif * wvif,struct cfg80211_scan_request * req,int start_idx)41 static int send_scan_req(struct wfx_vif *wvif,
42 			 struct cfg80211_scan_request *req, int start_idx)
43 {
44 	int i, ret;
45 	struct ieee80211_channel *ch_start, *ch_cur;
46 
47 	for (i = start_idx; i < req->n_channels; i++) {
48 		ch_start = req->channels[start_idx];
49 		ch_cur = req->channels[i];
50 		WARN(ch_cur->band != NL80211_BAND_2GHZ, "band not supported");
51 		if (ch_cur->max_power != ch_start->max_power)
52 			break;
53 		if ((ch_cur->flags ^ ch_start->flags) & IEEE80211_CHAN_NO_IR)
54 			break;
55 	}
56 	wfx_tx_lock_flush(wvif->wdev);
57 	wvif->scan_abort = false;
58 	reinit_completion(&wvif->scan_complete);
59 	ret = hif_scan(wvif, req, start_idx, i - start_idx);
60 	if (ret) {
61 		wfx_tx_unlock(wvif->wdev);
62 		return -EIO;
63 	}
64 	ret = wait_for_completion_timeout(&wvif->scan_complete, 1 * HZ);
65 	if (!ret) {
66 		hif_stop_scan(wvif);
67 		ret = wait_for_completion_timeout(&wvif->scan_complete, 1 * HZ);
68 		dev_dbg(wvif->wdev->dev, "scan timeout (%d channels done)\n",
69 			wvif->scan_nb_chan_done);
70 	}
71 	if (!ret) {
72 		dev_err(wvif->wdev->dev, "scan didn't stop\n");
73 		ret = -ETIMEDOUT;
74 	} else if (wvif->scan_abort) {
75 		dev_notice(wvif->wdev->dev, "scan abort\n");
76 		ret = -ECONNABORTED;
77 	} else if (wvif->scan_nb_chan_done > i - start_idx) {
78 		ret = -EIO;
79 	} else {
80 		ret = wvif->scan_nb_chan_done;
81 	}
82 	if (req->channels[start_idx]->max_power != wvif->vif->bss_conf.txpower)
83 		hif_set_output_power(wvif, wvif->vif->bss_conf.txpower);
84 	wfx_tx_unlock(wvif->wdev);
85 	return ret;
86 }
87 
88 /* It is not really necessary to run scan request asynchronously. However,
89  * there is a bug in "iw scan" when ieee80211_scan_completed() is called before
90  * wfx_hw_scan() return
91  */
wfx_hw_scan_work(struct work_struct * work)92 void wfx_hw_scan_work(struct work_struct *work)
93 {
94 	struct wfx_vif *wvif = container_of(work, struct wfx_vif, scan_work);
95 	struct ieee80211_scan_request *hw_req = wvif->scan_req;
96 	int chan_cur, ret, err;
97 
98 	mutex_lock(&wvif->wdev->conf_mutex);
99 	mutex_lock(&wvif->scan_lock);
100 	if (wvif->join_in_progress) {
101 		dev_info(wvif->wdev->dev, "%s: abort in-progress REQ_JOIN",
102 			 __func__);
103 		wfx_reset(wvif);
104 	}
105 	update_probe_tmpl(wvif, &hw_req->req);
106 	chan_cur = 0;
107 	err = 0;
108 	do {
109 		ret = send_scan_req(wvif, &hw_req->req, chan_cur);
110 		if (ret > 0) {
111 			chan_cur += ret;
112 			err = 0;
113 		}
114 		if (!ret)
115 			err++;
116 		if (err > 2) {
117 			dev_err(wvif->wdev->dev, "scan has not been able to start\n");
118 			ret = -ETIMEDOUT;
119 		}
120 	} while (ret >= 0 && chan_cur < hw_req->req.n_channels);
121 	mutex_unlock(&wvif->scan_lock);
122 	mutex_unlock(&wvif->wdev->conf_mutex);
123 	__ieee80211_scan_completed_compat(wvif->wdev->hw, ret < 0);
124 }
125 
wfx_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)126 int wfx_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
127 		struct ieee80211_scan_request *hw_req)
128 {
129 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
130 
131 	WARN_ON(hw_req->req.n_channels > HIF_API_MAX_NB_CHANNELS);
132 	wvif->scan_req = hw_req;
133 	schedule_work(&wvif->scan_work);
134 	return 0;
135 }
136 
wfx_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)137 void wfx_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
138 {
139 	struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv;
140 
141 	wvif->scan_abort = true;
142 	hif_stop_scan(wvif);
143 }
144 
wfx_scan_complete(struct wfx_vif * wvif,int nb_chan_done)145 void wfx_scan_complete(struct wfx_vif *wvif, int nb_chan_done)
146 {
147 	wvif->scan_nb_chan_done = nb_chan_done;
148 	complete(&wvif->scan_complete);
149 }
150