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