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