xref: /openbmc/linux/drivers/net/wireless/st/cw1200/scan.c (revision 0b26ca68)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Scan implementation for ST-Ericsson CW1200 mac80211 drivers
4  *
5  * Copyright (c) 2010, ST-Ericsson
6  * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
7  */
8 
9 #include <linux/sched.h>
10 #include "cw1200.h"
11 #include "scan.h"
12 #include "sta.h"
13 #include "pm.h"
14 
15 static void cw1200_scan_restart_delayed(struct cw1200_common *priv);
16 
17 static int cw1200_scan_start(struct cw1200_common *priv, struct wsm_scan *scan)
18 {
19 	int ret, i;
20 	int tmo = 2000;
21 
22 	switch (priv->join_status) {
23 	case CW1200_JOIN_STATUS_PRE_STA:
24 	case CW1200_JOIN_STATUS_JOINING:
25 		return -EBUSY;
26 	default:
27 		break;
28 	}
29 
30 	wiphy_dbg(priv->hw->wiphy, "[SCAN] hw req, type %d, %d channels, flags: 0x%x.\n",
31 		  scan->type, scan->num_channels, scan->flags);
32 
33 	for (i = 0; i < scan->num_channels; ++i)
34 		tmo += scan->ch[i].max_chan_time + 10;
35 
36 	cancel_delayed_work_sync(&priv->clear_recent_scan_work);
37 	atomic_set(&priv->scan.in_progress, 1);
38 	atomic_set(&priv->recent_scan, 1);
39 	cw1200_pm_stay_awake(&priv->pm_state, msecs_to_jiffies(tmo));
40 	queue_delayed_work(priv->workqueue, &priv->scan.timeout,
41 			   msecs_to_jiffies(tmo));
42 	ret = wsm_scan(priv, scan);
43 	if (ret) {
44 		atomic_set(&priv->scan.in_progress, 0);
45 		cancel_delayed_work_sync(&priv->scan.timeout);
46 		cw1200_scan_restart_delayed(priv);
47 	}
48 	return ret;
49 }
50 
51 int cw1200_hw_scan(struct ieee80211_hw *hw,
52 		   struct ieee80211_vif *vif,
53 		   struct ieee80211_scan_request *hw_req)
54 {
55 	struct cw1200_common *priv = hw->priv;
56 	struct cfg80211_scan_request *req = &hw_req->req;
57 	struct wsm_template_frame frame = {
58 		.frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
59 	};
60 	int i, ret;
61 
62 	if (!priv->vif)
63 		return -EINVAL;
64 
65 	/* Scan when P2P_GO corrupt firmware MiniAP mode */
66 	if (priv->join_status == CW1200_JOIN_STATUS_AP)
67 		return -EOPNOTSUPP;
68 
69 	if (req->n_ssids == 1 && !req->ssids[0].ssid_len)
70 		req->n_ssids = 0;
71 
72 	wiphy_dbg(hw->wiphy, "[SCAN] Scan request for %d SSIDs.\n",
73 		  req->n_ssids);
74 
75 	if (req->n_ssids > WSM_SCAN_MAX_NUM_OF_SSIDS)
76 		return -EINVAL;
77 
78 	/* will be unlocked in cw1200_scan_work() */
79 	down(&priv->scan.lock);
80 	mutex_lock(&priv->conf_mutex);
81 
82 	frame.skb = ieee80211_probereq_get(hw, priv->vif->addr, NULL, 0,
83 		req->ie_len);
84 	if (!frame.skb) {
85 		mutex_unlock(&priv->conf_mutex);
86 		up(&priv->scan.lock);
87 		return -ENOMEM;
88 	}
89 
90 	if (req->ie_len)
91 		skb_put_data(frame.skb, req->ie, req->ie_len);
92 
93 	ret = wsm_set_template_frame(priv, &frame);
94 	if (!ret) {
95 		/* Host want to be the probe responder. */
96 		ret = wsm_set_probe_responder(priv, true);
97 	}
98 	if (ret) {
99 		dev_kfree_skb(frame.skb);
100 		mutex_unlock(&priv->conf_mutex);
101 		up(&priv->scan.lock);
102 		return ret;
103 	}
104 
105 	wsm_lock_tx(priv);
106 
107 	BUG_ON(priv->scan.req);
108 	priv->scan.req = req;
109 	priv->scan.n_ssids = 0;
110 	priv->scan.status = 0;
111 	priv->scan.begin = &req->channels[0];
112 	priv->scan.curr = priv->scan.begin;
113 	priv->scan.end = &req->channels[req->n_channels];
114 	priv->scan.output_power = priv->output_power;
115 
116 	for (i = 0; i < req->n_ssids; ++i) {
117 		struct wsm_ssid *dst = &priv->scan.ssids[priv->scan.n_ssids];
118 		memcpy(&dst->ssid[0], req->ssids[i].ssid, sizeof(dst->ssid));
119 		dst->length = req->ssids[i].ssid_len;
120 		++priv->scan.n_ssids;
121 	}
122 
123 	dev_kfree_skb(frame.skb);
124 	mutex_unlock(&priv->conf_mutex);
125 	queue_work(priv->workqueue, &priv->scan.work);
126 	return 0;
127 }
128 
129 void cw1200_scan_work(struct work_struct *work)
130 {
131 	struct cw1200_common *priv = container_of(work, struct cw1200_common,
132 							scan.work);
133 	struct ieee80211_channel **it;
134 	struct wsm_scan scan = {
135 		.type = WSM_SCAN_TYPE_FOREGROUND,
136 		.flags = WSM_SCAN_FLAG_SPLIT_METHOD,
137 	};
138 	bool first_run = (priv->scan.begin == priv->scan.curr &&
139 			  priv->scan.begin != priv->scan.end);
140 	int i;
141 
142 	if (first_run) {
143 		/* Firmware gets crazy if scan request is sent
144 		 * when STA is joined but not yet associated.
145 		 * Force unjoin in this case.
146 		 */
147 		if (cancel_delayed_work_sync(&priv->join_timeout) > 0)
148 			cw1200_join_timeout(&priv->join_timeout.work);
149 	}
150 
151 	mutex_lock(&priv->conf_mutex);
152 
153 	if (first_run) {
154 		if (priv->join_status == CW1200_JOIN_STATUS_STA &&
155 		    !(priv->powersave_mode.mode & WSM_PSM_PS)) {
156 			struct wsm_set_pm pm = priv->powersave_mode;
157 			pm.mode = WSM_PSM_PS;
158 			cw1200_set_pm(priv, &pm);
159 		} else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
160 			/* FW bug: driver has to restart p2p-dev mode
161 			 * after scan
162 			 */
163 			cw1200_disable_listening(priv);
164 		}
165 	}
166 
167 	if (!priv->scan.req || (priv->scan.curr == priv->scan.end)) {
168 		struct cfg80211_scan_info info = {
169 			.aborted = priv->scan.status ? 1 : 0,
170 		};
171 
172 		if (priv->scan.output_power != priv->output_power)
173 			wsm_set_output_power(priv, priv->output_power * 10);
174 		if (priv->join_status == CW1200_JOIN_STATUS_STA &&
175 		    !(priv->powersave_mode.mode & WSM_PSM_PS))
176 			cw1200_set_pm(priv, &priv->powersave_mode);
177 
178 		if (priv->scan.status < 0)
179 			wiphy_warn(priv->hw->wiphy,
180 				   "[SCAN] Scan failed (%d).\n",
181 				   priv->scan.status);
182 		else if (priv->scan.req)
183 			wiphy_dbg(priv->hw->wiphy,
184 				  "[SCAN] Scan completed.\n");
185 		else
186 			wiphy_dbg(priv->hw->wiphy,
187 				  "[SCAN] Scan canceled.\n");
188 
189 		priv->scan.req = NULL;
190 		cw1200_scan_restart_delayed(priv);
191 		wsm_unlock_tx(priv);
192 		mutex_unlock(&priv->conf_mutex);
193 		ieee80211_scan_completed(priv->hw, &info);
194 		up(&priv->scan.lock);
195 		return;
196 	} else {
197 		struct ieee80211_channel *first = *priv->scan.curr;
198 		for (it = priv->scan.curr + 1, i = 1;
199 		     it != priv->scan.end && i < WSM_SCAN_MAX_NUM_OF_CHANNELS;
200 		     ++it, ++i) {
201 			if ((*it)->band != first->band)
202 				break;
203 			if (((*it)->flags ^ first->flags) &
204 					IEEE80211_CHAN_NO_IR)
205 				break;
206 			if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
207 			    (*it)->max_power != first->max_power)
208 				break;
209 		}
210 		scan.band = first->band;
211 
212 		if (priv->scan.req->no_cck)
213 			scan.max_tx_rate = WSM_TRANSMIT_RATE_6;
214 		else
215 			scan.max_tx_rate = WSM_TRANSMIT_RATE_1;
216 		scan.num_probes =
217 			(first->flags & IEEE80211_CHAN_NO_IR) ? 0 : 2;
218 		scan.num_ssids = priv->scan.n_ssids;
219 		scan.ssids = &priv->scan.ssids[0];
220 		scan.num_channels = it - priv->scan.curr;
221 		/* TODO: Is it optimal? */
222 		scan.probe_delay = 100;
223 		/* It is not stated in WSM specification, however
224 		 * FW team says that driver may not use FG scan
225 		 * when joined.
226 		 */
227 		if (priv->join_status == CW1200_JOIN_STATUS_STA) {
228 			scan.type = WSM_SCAN_TYPE_BACKGROUND;
229 			scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
230 		}
231 		scan.ch = kcalloc(it - priv->scan.curr,
232 				  sizeof(struct wsm_scan_ch),
233 				  GFP_KERNEL);
234 		if (!scan.ch) {
235 			priv->scan.status = -ENOMEM;
236 			goto fail;
237 		}
238 		for (i = 0; i < scan.num_channels; ++i) {
239 			scan.ch[i].number = priv->scan.curr[i]->hw_value;
240 			if (priv->scan.curr[i]->flags & IEEE80211_CHAN_NO_IR) {
241 				scan.ch[i].min_chan_time = 50;
242 				scan.ch[i].max_chan_time = 100;
243 			} else {
244 				scan.ch[i].min_chan_time = 10;
245 				scan.ch[i].max_chan_time = 25;
246 			}
247 		}
248 		if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
249 		    priv->scan.output_power != first->max_power) {
250 			priv->scan.output_power = first->max_power;
251 			wsm_set_output_power(priv,
252 					     priv->scan.output_power * 10);
253 		}
254 		priv->scan.status = cw1200_scan_start(priv, &scan);
255 		kfree(scan.ch);
256 		if (priv->scan.status)
257 			goto fail;
258 		priv->scan.curr = it;
259 	}
260 	mutex_unlock(&priv->conf_mutex);
261 	return;
262 
263 fail:
264 	priv->scan.curr = priv->scan.end;
265 	mutex_unlock(&priv->conf_mutex);
266 	queue_work(priv->workqueue, &priv->scan.work);
267 	return;
268 }
269 
270 static void cw1200_scan_restart_delayed(struct cw1200_common *priv)
271 {
272 	/* FW bug: driver has to restart p2p-dev mode after scan. */
273 	if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
274 		cw1200_enable_listening(priv);
275 		cw1200_update_filtering(priv);
276 	}
277 
278 	if (priv->delayed_unjoin) {
279 		priv->delayed_unjoin = false;
280 		if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
281 			wsm_unlock_tx(priv);
282 	} else if (priv->delayed_link_loss) {
283 			wiphy_dbg(priv->hw->wiphy, "[CQM] Requeue BSS loss.\n");
284 			priv->delayed_link_loss = 0;
285 			cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
286 	}
287 }
288 
289 static void cw1200_scan_complete(struct cw1200_common *priv)
290 {
291 	queue_delayed_work(priv->workqueue, &priv->clear_recent_scan_work, HZ);
292 	if (priv->scan.direct_probe) {
293 		wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe complete.\n");
294 		cw1200_scan_restart_delayed(priv);
295 		priv->scan.direct_probe = 0;
296 		up(&priv->scan.lock);
297 		wsm_unlock_tx(priv);
298 	} else {
299 		cw1200_scan_work(&priv->scan.work);
300 	}
301 }
302 
303 void cw1200_scan_failed_cb(struct cw1200_common *priv)
304 {
305 	if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
306 		/* STA is stopped. */
307 		return;
308 
309 	if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
310 		priv->scan.status = -EIO;
311 		queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
312 	}
313 }
314 
315 
316 void cw1200_scan_complete_cb(struct cw1200_common *priv,
317 				struct wsm_scan_complete *arg)
318 {
319 	if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
320 		/* STA is stopped. */
321 		return;
322 
323 	if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
324 		priv->scan.status = 1;
325 		queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
326 	}
327 }
328 
329 void cw1200_clear_recent_scan_work(struct work_struct *work)
330 {
331 	struct cw1200_common *priv =
332 		container_of(work, struct cw1200_common,
333 			     clear_recent_scan_work.work);
334 	atomic_xchg(&priv->recent_scan, 0);
335 }
336 
337 void cw1200_scan_timeout(struct work_struct *work)
338 {
339 	struct cw1200_common *priv =
340 		container_of(work, struct cw1200_common, scan.timeout.work);
341 	if (atomic_xchg(&priv->scan.in_progress, 0)) {
342 		if (priv->scan.status > 0) {
343 			priv->scan.status = 0;
344 		} else if (!priv->scan.status) {
345 			wiphy_warn(priv->hw->wiphy,
346 				   "Timeout waiting for scan complete notification.\n");
347 			priv->scan.status = -ETIMEDOUT;
348 			priv->scan.curr = priv->scan.end;
349 			wsm_stop_scan(priv);
350 		}
351 		cw1200_scan_complete(priv);
352 	}
353 }
354 
355 void cw1200_probe_work(struct work_struct *work)
356 {
357 	struct cw1200_common *priv =
358 		container_of(work, struct cw1200_common, scan.probe_work.work);
359 	u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
360 	struct cw1200_queue *queue = &priv->tx_queue[queue_id];
361 	const struct cw1200_txpriv *txpriv;
362 	struct wsm_tx *wsm;
363 	struct wsm_template_frame frame = {
364 		.frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
365 	};
366 	struct wsm_ssid ssids[1] = {{
367 		.length = 0,
368 	} };
369 	struct wsm_scan_ch ch[1] = {{
370 		.min_chan_time = 0,
371 		.max_chan_time = 10,
372 	} };
373 	struct wsm_scan scan = {
374 		.type = WSM_SCAN_TYPE_FOREGROUND,
375 		.num_probes = 1,
376 		.probe_delay = 0,
377 		.num_channels = 1,
378 		.ssids = ssids,
379 		.ch = ch,
380 	};
381 	u8 *ies;
382 	size_t ies_len;
383 	int ret;
384 
385 	wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe work.\n");
386 
387 	mutex_lock(&priv->conf_mutex);
388 	if (down_trylock(&priv->scan.lock)) {
389 		/* Scan is already in progress. Requeue self. */
390 		schedule();
391 		queue_delayed_work(priv->workqueue, &priv->scan.probe_work,
392 				   msecs_to_jiffies(100));
393 		mutex_unlock(&priv->conf_mutex);
394 		return;
395 	}
396 
397 	/* Make sure we still have a pending probe req */
398 	if (cw1200_queue_get_skb(queue,	priv->pending_frame_id,
399 				 &frame.skb, &txpriv)) {
400 		up(&priv->scan.lock);
401 		mutex_unlock(&priv->conf_mutex);
402 		wsm_unlock_tx(priv);
403 		return;
404 	}
405 	wsm = (struct wsm_tx *)frame.skb->data;
406 	scan.max_tx_rate = wsm->max_tx_rate;
407 	scan.band = (priv->channel->band == NL80211_BAND_5GHZ) ?
408 		WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
409 	if (priv->join_status == CW1200_JOIN_STATUS_STA ||
410 	    priv->join_status == CW1200_JOIN_STATUS_IBSS) {
411 		scan.type = WSM_SCAN_TYPE_BACKGROUND;
412 		scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
413 	}
414 	ch[0].number = priv->channel->hw_value;
415 
416 	skb_pull(frame.skb, txpriv->offset);
417 
418 	ies = &frame.skb->data[sizeof(struct ieee80211_hdr_3addr)];
419 	ies_len = frame.skb->len - sizeof(struct ieee80211_hdr_3addr);
420 
421 	if (ies_len) {
422 		u8 *ssidie =
423 			(u8 *)cfg80211_find_ie(WLAN_EID_SSID, ies, ies_len);
424 		if (ssidie && ssidie[1] && ssidie[1] <= sizeof(ssids[0].ssid)) {
425 			u8 *nextie = &ssidie[2 + ssidie[1]];
426 			/* Remove SSID from the IE list. It has to be provided
427 			 * as a separate argument in cw1200_scan_start call
428 			 */
429 
430 			/* Store SSID localy */
431 			ssids[0].length = ssidie[1];
432 			memcpy(ssids[0].ssid, &ssidie[2], ssids[0].length);
433 			scan.num_ssids = 1;
434 
435 			/* Remove SSID from IE list */
436 			ssidie[1] = 0;
437 			memmove(&ssidie[2], nextie, &ies[ies_len] - nextie);
438 			skb_trim(frame.skb, frame.skb->len - ssids[0].length);
439 		}
440 	}
441 
442 	/* FW bug: driver has to restart p2p-dev mode after scan */
443 	if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
444 		cw1200_disable_listening(priv);
445 	ret = wsm_set_template_frame(priv, &frame);
446 	priv->scan.direct_probe = 1;
447 	if (!ret) {
448 		wsm_flush_tx(priv);
449 		ret = cw1200_scan_start(priv, &scan);
450 	}
451 	mutex_unlock(&priv->conf_mutex);
452 
453 	skb_push(frame.skb, txpriv->offset);
454 	if (!ret)
455 		IEEE80211_SKB_CB(frame.skb)->flags |= IEEE80211_TX_STAT_ACK;
456 	BUG_ON(cw1200_queue_remove(queue, priv->pending_frame_id));
457 
458 	if (ret) {
459 		priv->scan.direct_probe = 0;
460 		up(&priv->scan.lock);
461 		wsm_unlock_tx(priv);
462 	}
463 
464 	return;
465 }
466