• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mac80211 STA API for ST-Ericsson CW1200 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/vmalloc.h>
13 #include <linux/sched.h>
14 #include <linux/firmware.h>
15 #include <linux/module.h>
16 #include <linux/etherdevice.h>
17 
18 #include "cw1200.h"
19 #include "sta.h"
20 #include "fwio.h"
21 #include "bh.h"
22 #include "debug.h"
23 
24 #ifndef ERP_INFO_BYTE_OFFSET
25 #define ERP_INFO_BYTE_OFFSET 2
26 #endif
27 
28 static void cw1200_do_join(struct cw1200_common *priv);
29 static void cw1200_do_unjoin(struct cw1200_common *priv);
30 
31 static int cw1200_upload_beacon(struct cw1200_common *priv);
32 static int cw1200_upload_pspoll(struct cw1200_common *priv);
33 static int cw1200_upload_null(struct cw1200_common *priv);
34 static int cw1200_upload_qosnull(struct cw1200_common *priv);
35 static int cw1200_start_ap(struct cw1200_common *priv);
36 static int cw1200_update_beaconing(struct cw1200_common *priv);
37 static int cw1200_enable_beaconing(struct cw1200_common *priv,
38 				   bool enable);
39 static void __cw1200_sta_notify(struct ieee80211_hw *dev,
40 				struct ieee80211_vif *vif,
41 				enum sta_notify_cmd notify_cmd,
42 				int link_id);
43 static int __cw1200_flush(struct cw1200_common *priv, bool drop);
44 
__cw1200_free_event_queue(struct list_head * list)45 static inline void __cw1200_free_event_queue(struct list_head *list)
46 {
47 	struct cw1200_wsm_event *event, *tmp;
48 	list_for_each_entry_safe(event, tmp, list, link) {
49 		list_del(&event->link);
50 		kfree(event);
51 	}
52 }
53 
54 /* ******************************************************************** */
55 /* STA API								*/
56 
cw1200_start(struct ieee80211_hw * dev)57 int cw1200_start(struct ieee80211_hw *dev)
58 {
59 	struct cw1200_common *priv = dev->priv;
60 	int ret = 0;
61 
62 	cw1200_pm_stay_awake(&priv->pm_state, HZ);
63 
64 	mutex_lock(&priv->conf_mutex);
65 
66 	/* default EDCA */
67 	WSM_EDCA_SET(&priv->edca, 0, 0x0002, 0x0003, 0x0007, 47, 0xc8, false);
68 	WSM_EDCA_SET(&priv->edca, 1, 0x0002, 0x0007, 0x000f, 94, 0xc8, false);
69 	WSM_EDCA_SET(&priv->edca, 2, 0x0003, 0x000f, 0x03ff, 0, 0xc8, false);
70 	WSM_EDCA_SET(&priv->edca, 3, 0x0007, 0x000f, 0x03ff, 0, 0xc8, false);
71 	ret = wsm_set_edca_params(priv, &priv->edca);
72 	if (ret)
73 		goto out;
74 
75 	ret = cw1200_set_uapsd_param(priv, &priv->edca);
76 	if (ret)
77 		goto out;
78 
79 	priv->setbssparams_done = false;
80 
81 	memcpy(priv->mac_addr, dev->wiphy->perm_addr, ETH_ALEN);
82 	priv->mode = NL80211_IFTYPE_MONITOR;
83 	priv->wep_default_key_id = -1;
84 
85 	priv->cqm_beacon_loss_count = 10;
86 
87 	ret = cw1200_setup_mac(priv);
88 	if (ret)
89 		goto out;
90 
91 out:
92 	mutex_unlock(&priv->conf_mutex);
93 	return ret;
94 }
95 
cw1200_stop(struct ieee80211_hw * dev)96 void cw1200_stop(struct ieee80211_hw *dev)
97 {
98 	struct cw1200_common *priv = dev->priv;
99 	LIST_HEAD(list);
100 	int i;
101 
102 	wsm_lock_tx(priv);
103 
104 	while (down_trylock(&priv->scan.lock)) {
105 		/* Scan is in progress. Force it to stop. */
106 		priv->scan.req = NULL;
107 		schedule();
108 	}
109 	up(&priv->scan.lock);
110 
111 	cancel_delayed_work_sync(&priv->scan.probe_work);
112 	cancel_delayed_work_sync(&priv->scan.timeout);
113 	cancel_delayed_work_sync(&priv->clear_recent_scan_work);
114 	cancel_delayed_work_sync(&priv->join_timeout);
115 	cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
116 	cancel_work_sync(&priv->unjoin_work);
117 	cancel_delayed_work_sync(&priv->link_id_gc_work);
118 	flush_workqueue(priv->workqueue);
119 	del_timer_sync(&priv->mcast_timeout);
120 	mutex_lock(&priv->conf_mutex);
121 	priv->mode = NL80211_IFTYPE_UNSPECIFIED;
122 	priv->listening = false;
123 
124 	spin_lock(&priv->event_queue_lock);
125 	list_splice_init(&priv->event_queue, &list);
126 	spin_unlock(&priv->event_queue_lock);
127 	__cw1200_free_event_queue(&list);
128 
129 
130 	priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
131 	priv->join_pending = false;
132 
133 	for (i = 0; i < 4; i++)
134 		cw1200_queue_clear(&priv->tx_queue[i]);
135 	mutex_unlock(&priv->conf_mutex);
136 	tx_policy_clean(priv);
137 
138 	/* HACK! */
139 	if (atomic_xchg(&priv->tx_lock, 1) != 1)
140 		pr_debug("[STA] TX is force-unlocked due to stop request.\n");
141 
142 	wsm_unlock_tx(priv);
143 	atomic_xchg(&priv->tx_lock, 0); /* for recovery to work */
144 }
145 
146 static int cw1200_bssloss_mitigation = 1;
147 module_param(cw1200_bssloss_mitigation, int, 0644);
148 MODULE_PARM_DESC(cw1200_bssloss_mitigation, "BSS Loss mitigation. 0 == disabled, 1 == enabled (default)");
149 
150 
__cw1200_cqm_bssloss_sm(struct cw1200_common * priv,int init,int good,int bad)151 void __cw1200_cqm_bssloss_sm(struct cw1200_common *priv,
152 			     int init, int good, int bad)
153 {
154 	int tx = 0;
155 
156 	priv->delayed_link_loss = 0;
157 	cancel_work_sync(&priv->bss_params_work);
158 
159 	pr_debug("[STA] CQM BSSLOSS_SM: state: %d init %d good %d bad: %d txlock: %d uj: %d\n",
160 		 priv->bss_loss_state,
161 		 init, good, bad,
162 		 atomic_read(&priv->tx_lock),
163 		 priv->delayed_unjoin);
164 
165 	/* If we have a pending unjoin */
166 	if (priv->delayed_unjoin)
167 		return;
168 
169 	if (init) {
170 		queue_delayed_work(priv->workqueue,
171 				   &priv->bss_loss_work,
172 				   HZ);
173 		priv->bss_loss_state = 0;
174 
175 		/* Skip the confimration procedure in P2P case */
176 		if (!priv->vif->p2p && !atomic_read(&priv->tx_lock))
177 			tx = 1;
178 	} else if (good) {
179 		cancel_delayed_work_sync(&priv->bss_loss_work);
180 		priv->bss_loss_state = 0;
181 		queue_work(priv->workqueue, &priv->bss_params_work);
182 	} else if (bad) {
183 		/* XXX Should we just keep going until we time out? */
184 		if (priv->bss_loss_state < 3)
185 			tx = 1;
186 	} else {
187 		cancel_delayed_work_sync(&priv->bss_loss_work);
188 		priv->bss_loss_state = 0;
189 	}
190 
191 	/* Bypass mitigation if it's disabled */
192 	if (!cw1200_bssloss_mitigation)
193 		tx = 0;
194 
195 	/* Spit out a NULL packet to our AP if necessary */
196 	if (tx) {
197 		struct sk_buff *skb;
198 
199 		priv->bss_loss_state++;
200 
201 		skb = ieee80211_nullfunc_get(priv->hw, priv->vif);
202 		WARN_ON(!skb);
203 		if (skb)
204 			cw1200_tx(priv->hw, NULL, skb);
205 	}
206 }
207 
cw1200_add_interface(struct ieee80211_hw * dev,struct ieee80211_vif * vif)208 int cw1200_add_interface(struct ieee80211_hw *dev,
209 			 struct ieee80211_vif *vif)
210 {
211 	int ret;
212 	struct cw1200_common *priv = dev->priv;
213 	/* __le32 auto_calibration_mode = __cpu_to_le32(1); */
214 
215 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
216 			     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
217 
218 	mutex_lock(&priv->conf_mutex);
219 
220 	if (priv->mode != NL80211_IFTYPE_MONITOR) {
221 		mutex_unlock(&priv->conf_mutex);
222 		return -EOPNOTSUPP;
223 	}
224 
225 	switch (vif->type) {
226 	case NL80211_IFTYPE_STATION:
227 	case NL80211_IFTYPE_ADHOC:
228 	case NL80211_IFTYPE_MESH_POINT:
229 	case NL80211_IFTYPE_AP:
230 		priv->mode = vif->type;
231 		break;
232 	default:
233 		mutex_unlock(&priv->conf_mutex);
234 		return -EOPNOTSUPP;
235 	}
236 
237 	priv->vif = vif;
238 	memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
239 	ret = cw1200_setup_mac(priv);
240 	/* Enable auto-calibration */
241 	/* Exception in subsequent channel switch; disabled.
242 	 *  wsm_write_mib(priv, WSM_MIB_ID_SET_AUTO_CALIBRATION_MODE,
243 	 *      &auto_calibration_mode, sizeof(auto_calibration_mode));
244 	*/
245 
246 	mutex_unlock(&priv->conf_mutex);
247 	return ret;
248 }
249 
cw1200_remove_interface(struct ieee80211_hw * dev,struct ieee80211_vif * vif)250 void cw1200_remove_interface(struct ieee80211_hw *dev,
251 			     struct ieee80211_vif *vif)
252 {
253 	struct cw1200_common *priv = dev->priv;
254 	struct wsm_reset reset = {
255 		.reset_statistics = true,
256 	};
257 	int i;
258 
259 	mutex_lock(&priv->conf_mutex);
260 	switch (priv->join_status) {
261 	case CW1200_JOIN_STATUS_JOINING:
262 	case CW1200_JOIN_STATUS_PRE_STA:
263 	case CW1200_JOIN_STATUS_STA:
264 	case CW1200_JOIN_STATUS_IBSS:
265 		wsm_lock_tx(priv);
266 		if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
267 			wsm_unlock_tx(priv);
268 		break;
269 	case CW1200_JOIN_STATUS_AP:
270 		for (i = 0; priv->link_id_map; ++i) {
271 			if (priv->link_id_map & BIT(i)) {
272 				reset.link_id = i;
273 				wsm_reset(priv, &reset);
274 				priv->link_id_map &= ~BIT(i);
275 			}
276 		}
277 		memset(priv->link_id_db, 0, sizeof(priv->link_id_db));
278 		priv->sta_asleep_mask = 0;
279 		priv->enable_beacon = false;
280 		priv->tx_multicast = false;
281 		priv->aid0_bit_set = false;
282 		priv->buffered_multicasts = false;
283 		priv->pspoll_mask = 0;
284 		reset.link_id = 0;
285 		wsm_reset(priv, &reset);
286 		break;
287 	case CW1200_JOIN_STATUS_MONITOR:
288 		cw1200_update_listening(priv, false);
289 		break;
290 	default:
291 		break;
292 	}
293 	priv->vif = NULL;
294 	priv->mode = NL80211_IFTYPE_MONITOR;
295 	memset(priv->mac_addr, 0, ETH_ALEN);
296 	memset(&priv->p2p_ps_modeinfo, 0, sizeof(priv->p2p_ps_modeinfo));
297 	cw1200_free_keys(priv);
298 	cw1200_setup_mac(priv);
299 	priv->listening = false;
300 	priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
301 	if (!__cw1200_flush(priv, true))
302 		wsm_unlock_tx(priv);
303 
304 	mutex_unlock(&priv->conf_mutex);
305 }
306 
cw1200_change_interface(struct ieee80211_hw * dev,struct ieee80211_vif * vif,enum nl80211_iftype new_type,bool p2p)307 int cw1200_change_interface(struct ieee80211_hw *dev,
308 			    struct ieee80211_vif *vif,
309 			    enum nl80211_iftype new_type,
310 			    bool p2p)
311 {
312 	int ret = 0;
313 	pr_debug("change_interface new: %d (%d), old: %d (%d)\n", new_type,
314 		 p2p, vif->type, vif->p2p);
315 
316 	if (new_type != vif->type || vif->p2p != p2p) {
317 		cw1200_remove_interface(dev, vif);
318 		vif->type = new_type;
319 		vif->p2p = p2p;
320 		ret = cw1200_add_interface(dev, vif);
321 	}
322 
323 	return ret;
324 }
325 
cw1200_config(struct ieee80211_hw * dev,u32 changed)326 int cw1200_config(struct ieee80211_hw *dev, u32 changed)
327 {
328 	int ret = 0;
329 	struct cw1200_common *priv = dev->priv;
330 	struct ieee80211_conf *conf = &dev->conf;
331 
332 	pr_debug("CONFIG CHANGED:  %08x\n", changed);
333 
334 	down(&priv->scan.lock);
335 	mutex_lock(&priv->conf_mutex);
336 	/* TODO: IEEE80211_CONF_CHANGE_QOS */
337 	/* TODO: IEEE80211_CONF_CHANGE_LISTEN_INTERVAL */
338 
339 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
340 		priv->output_power = conf->power_level;
341 		pr_debug("[STA] TX power: %d\n", priv->output_power);
342 		wsm_set_output_power(priv, priv->output_power * 10);
343 	}
344 
345 	if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) &&
346 	    (priv->channel != conf->chandef.chan)) {
347 		struct ieee80211_channel *ch = conf->chandef.chan;
348 		struct wsm_switch_channel channel = {
349 			.channel_number = ch->hw_value,
350 		};
351 		pr_debug("[STA] Freq %d (wsm ch: %d).\n",
352 			 ch->center_freq, ch->hw_value);
353 
354 		/* __cw1200_flush() implicitly locks tx, if successful */
355 		if (!__cw1200_flush(priv, false)) {
356 			if (!wsm_switch_channel(priv, &channel)) {
357 				ret = wait_event_timeout(priv->channel_switch_done,
358 							 !priv->channel_switch_in_progress,
359 							 3 * HZ);
360 				if (ret) {
361 					/* Already unlocks if successful */
362 					priv->channel = ch;
363 					ret = 0;
364 				} else {
365 					ret = -ETIMEDOUT;
366 				}
367 			} else {
368 				/* Unlock if switch channel fails */
369 				wsm_unlock_tx(priv);
370 			}
371 		}
372 	}
373 
374 	if (changed & IEEE80211_CONF_CHANGE_PS) {
375 		if (!(conf->flags & IEEE80211_CONF_PS))
376 			priv->powersave_mode.mode = WSM_PSM_ACTIVE;
377 		else if (conf->dynamic_ps_timeout <= 0)
378 			priv->powersave_mode.mode = WSM_PSM_PS;
379 		else
380 			priv->powersave_mode.mode = WSM_PSM_FAST_PS;
381 
382 		/* Firmware requires that value for this 1-byte field must
383 		 * be specified in units of 500us. Values above the 128ms
384 		 * threshold are not supported.
385 		 */
386 		if (conf->dynamic_ps_timeout >= 0x80)
387 			priv->powersave_mode.fast_psm_idle_period = 0xFF;
388 		else
389 			priv->powersave_mode.fast_psm_idle_period =
390 					conf->dynamic_ps_timeout << 1;
391 
392 		if (priv->join_status == CW1200_JOIN_STATUS_STA &&
393 		    priv->bss_params.aid)
394 			cw1200_set_pm(priv, &priv->powersave_mode);
395 	}
396 
397 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
398 		/* TBD: It looks like it's transparent
399 		 * there's a monitor interface present -- use this
400 		 * to determine for example whether to calculate
401 		 * timestamps for packets or not, do not use instead
402 		 * of filter flags!
403 		 */
404 	}
405 
406 	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
407 		struct wsm_operational_mode mode = {
408 			.power_mode = cw1200_power_mode,
409 			.disable_more_flag_usage = true,
410 		};
411 
412 		wsm_lock_tx(priv);
413 		/* Disable p2p-dev mode forced by TX request */
414 		if ((priv->join_status == CW1200_JOIN_STATUS_MONITOR) &&
415 		    (conf->flags & IEEE80211_CONF_IDLE) &&
416 		    !priv->listening) {
417 			cw1200_disable_listening(priv);
418 			priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
419 		}
420 		wsm_set_operational_mode(priv, &mode);
421 		wsm_unlock_tx(priv);
422 	}
423 
424 	if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
425 		pr_debug("[STA] Retry limits: %d (long), %d (short).\n",
426 			 conf->long_frame_max_tx_count,
427 			 conf->short_frame_max_tx_count);
428 		spin_lock_bh(&priv->tx_policy_cache.lock);
429 		priv->long_frame_max_tx_count = conf->long_frame_max_tx_count;
430 		priv->short_frame_max_tx_count =
431 			(conf->short_frame_max_tx_count < 0x0F) ?
432 			conf->short_frame_max_tx_count : 0x0F;
433 		priv->hw->max_rate_tries = priv->short_frame_max_tx_count;
434 		spin_unlock_bh(&priv->tx_policy_cache.lock);
435 	}
436 	mutex_unlock(&priv->conf_mutex);
437 	up(&priv->scan.lock);
438 	return ret;
439 }
440 
cw1200_update_filtering(struct cw1200_common * priv)441 void cw1200_update_filtering(struct cw1200_common *priv)
442 {
443 	int ret;
444 	bool bssid_filtering = !priv->rx_filter.bssid;
445 	bool is_p2p = priv->vif && priv->vif->p2p;
446 	bool is_sta = priv->vif && NL80211_IFTYPE_STATION == priv->vif->type;
447 
448 	static struct wsm_beacon_filter_control bf_ctrl;
449 	static struct wsm_mib_beacon_filter_table bf_tbl = {
450 		.entry[0].ie_id = WLAN_EID_VENDOR_SPECIFIC,
451 		.entry[0].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
452 					WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
453 					WSM_BEACON_FILTER_IE_HAS_APPEARED,
454 		.entry[0].oui[0] = 0x50,
455 		.entry[0].oui[1] = 0x6F,
456 		.entry[0].oui[2] = 0x9A,
457 		.entry[1].ie_id = WLAN_EID_HT_OPERATION,
458 		.entry[1].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
459 					WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
460 					WSM_BEACON_FILTER_IE_HAS_APPEARED,
461 		.entry[2].ie_id = WLAN_EID_ERP_INFO,
462 		.entry[2].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
463 					WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
464 					WSM_BEACON_FILTER_IE_HAS_APPEARED,
465 	};
466 
467 	if (priv->join_status == CW1200_JOIN_STATUS_PASSIVE)
468 		return;
469 	else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
470 		bssid_filtering = false;
471 
472 	if (priv->disable_beacon_filter) {
473 		bf_ctrl.enabled = 0;
474 		bf_ctrl.bcn_count = 1;
475 		bf_tbl.num = __cpu_to_le32(0);
476 	} else if (is_p2p || !is_sta) {
477 		bf_ctrl.enabled = WSM_BEACON_FILTER_ENABLE |
478 			WSM_BEACON_FILTER_AUTO_ERP;
479 		bf_ctrl.bcn_count = 0;
480 		bf_tbl.num = __cpu_to_le32(2);
481 	} else {
482 		bf_ctrl.enabled = WSM_BEACON_FILTER_ENABLE;
483 		bf_ctrl.bcn_count = 0;
484 		bf_tbl.num = __cpu_to_le32(3);
485 	}
486 
487 	/* When acting as p2p client being connected to p2p GO, in order to
488 	 * receive frames from a different p2p device, turn off bssid filter.
489 	 *
490 	 * WARNING: FW dependency!
491 	 * This can only be used with FW WSM371 and its successors.
492 	 * In that FW version even with bssid filter turned off,
493 	 * device will block most of the unwanted frames.
494 	 */
495 	if (is_p2p)
496 		bssid_filtering = false;
497 
498 	ret = wsm_set_rx_filter(priv, &priv->rx_filter);
499 	if (!ret)
500 		ret = wsm_set_beacon_filter_table(priv, &bf_tbl);
501 	if (!ret)
502 		ret = wsm_beacon_filter_control(priv, &bf_ctrl);
503 	if (!ret)
504 		ret = wsm_set_bssid_filtering(priv, bssid_filtering);
505 	if (!ret)
506 		ret = wsm_set_multicast_filter(priv, &priv->multicast_filter);
507 	if (ret)
508 		wiphy_err(priv->hw->wiphy,
509 			  "Update filtering failed: %d.\n", ret);
510 	return;
511 }
512 
cw1200_update_filtering_work(struct work_struct * work)513 void cw1200_update_filtering_work(struct work_struct *work)
514 {
515 	struct cw1200_common *priv =
516 		container_of(work, struct cw1200_common,
517 			     update_filtering_work);
518 
519 	cw1200_update_filtering(priv);
520 }
521 
cw1200_set_beacon_wakeup_period_work(struct work_struct * work)522 void cw1200_set_beacon_wakeup_period_work(struct work_struct *work)
523 {
524 	struct cw1200_common *priv =
525 		container_of(work, struct cw1200_common,
526 			     set_beacon_wakeup_period_work);
527 
528 	wsm_set_beacon_wakeup_period(priv,
529 				     priv->beacon_int * priv->join_dtim_period >
530 				     MAX_BEACON_SKIP_TIME_MS ? 1 :
531 				     priv->join_dtim_period, 0);
532 }
533 
cw1200_prepare_multicast(struct ieee80211_hw * hw,struct netdev_hw_addr_list * mc_list)534 u64 cw1200_prepare_multicast(struct ieee80211_hw *hw,
535 			     struct netdev_hw_addr_list *mc_list)
536 {
537 	static u8 broadcast_ipv6[ETH_ALEN] = {
538 		0x33, 0x33, 0x00, 0x00, 0x00, 0x01
539 	};
540 	static u8 broadcast_ipv4[ETH_ALEN] = {
541 		0x01, 0x00, 0x5e, 0x00, 0x00, 0x01
542 	};
543 	struct cw1200_common *priv = hw->priv;
544 	struct netdev_hw_addr *ha;
545 	int count = 0;
546 
547 	/* Disable multicast filtering */
548 	priv->has_multicast_subscription = false;
549 	memset(&priv->multicast_filter, 0x00, sizeof(priv->multicast_filter));
550 
551 	if (netdev_hw_addr_list_count(mc_list) > WSM_MAX_GRP_ADDRTABLE_ENTRIES)
552 		return 0;
553 
554 	/* Enable if requested */
555 	netdev_hw_addr_list_for_each(ha, mc_list) {
556 		pr_debug("[STA] multicast: %pM\n", ha->addr);
557 		memcpy(&priv->multicast_filter.macaddrs[count],
558 		       ha->addr, ETH_ALEN);
559 		if (!ether_addr_equal(ha->addr, broadcast_ipv4) &&
560 		    !ether_addr_equal(ha->addr, broadcast_ipv6))
561 			priv->has_multicast_subscription = true;
562 		count++;
563 	}
564 
565 	if (count) {
566 		priv->multicast_filter.enable = __cpu_to_le32(1);
567 		priv->multicast_filter.num_addrs = __cpu_to_le32(count);
568 	}
569 
570 	return netdev_hw_addr_list_count(mc_list);
571 }
572 
cw1200_configure_filter(struct ieee80211_hw * dev,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)573 void cw1200_configure_filter(struct ieee80211_hw *dev,
574 			     unsigned int changed_flags,
575 			     unsigned int *total_flags,
576 			     u64 multicast)
577 {
578 	struct cw1200_common *priv = dev->priv;
579 	bool listening = !!(*total_flags &
580 			    (FIF_PROMISC_IN_BSS |
581 			     FIF_OTHER_BSS |
582 			     FIF_BCN_PRBRESP_PROMISC |
583 			     FIF_PROBE_REQ));
584 
585 	*total_flags &= FIF_PROMISC_IN_BSS |
586 			FIF_OTHER_BSS |
587 			FIF_FCSFAIL |
588 			FIF_BCN_PRBRESP_PROMISC |
589 			FIF_PROBE_REQ;
590 
591 	down(&priv->scan.lock);
592 	mutex_lock(&priv->conf_mutex);
593 
594 	priv->rx_filter.promiscuous = (*total_flags & FIF_PROMISC_IN_BSS)
595 			? 1 : 0;
596 	priv->rx_filter.bssid = (*total_flags & (FIF_OTHER_BSS |
597 			FIF_PROBE_REQ)) ? 1 : 0;
598 	priv->rx_filter.fcs = (*total_flags & FIF_FCSFAIL) ? 1 : 0;
599 	priv->disable_beacon_filter = !(*total_flags &
600 					(FIF_BCN_PRBRESP_PROMISC |
601 					 FIF_PROMISC_IN_BSS |
602 					 FIF_PROBE_REQ));
603 	if (priv->listening != listening) {
604 		priv->listening = listening;
605 		wsm_lock_tx(priv);
606 		cw1200_update_listening(priv, listening);
607 		wsm_unlock_tx(priv);
608 	}
609 	cw1200_update_filtering(priv);
610 	mutex_unlock(&priv->conf_mutex);
611 	up(&priv->scan.lock);
612 }
613 
cw1200_conf_tx(struct ieee80211_hw * dev,struct ieee80211_vif * vif,u16 queue,const struct ieee80211_tx_queue_params * params)614 int cw1200_conf_tx(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
615 		   u16 queue, const struct ieee80211_tx_queue_params *params)
616 {
617 	struct cw1200_common *priv = dev->priv;
618 	int ret = 0;
619 	/* To prevent re-applying PM request OID again and again*/
620 	bool old_uapsd_flags;
621 
622 	mutex_lock(&priv->conf_mutex);
623 
624 	if (queue < dev->queues) {
625 		old_uapsd_flags = le16_to_cpu(priv->uapsd_info.uapsd_flags);
626 
627 		WSM_TX_QUEUE_SET(&priv->tx_queue_params, queue, 0, 0, 0);
628 		ret = wsm_set_tx_queue_params(priv,
629 					      &priv->tx_queue_params.params[queue], queue);
630 		if (ret) {
631 			ret = -EINVAL;
632 			goto out;
633 		}
634 
635 		WSM_EDCA_SET(&priv->edca, queue, params->aifs,
636 			     params->cw_min, params->cw_max,
637 			     params->txop, 0xc8,
638 			     params->uapsd);
639 		ret = wsm_set_edca_params(priv, &priv->edca);
640 		if (ret) {
641 			ret = -EINVAL;
642 			goto out;
643 		}
644 
645 		if (priv->mode == NL80211_IFTYPE_STATION) {
646 			ret = cw1200_set_uapsd_param(priv, &priv->edca);
647 			if (!ret && priv->setbssparams_done &&
648 			    (priv->join_status == CW1200_JOIN_STATUS_STA) &&
649 			    (old_uapsd_flags != le16_to_cpu(priv->uapsd_info.uapsd_flags)))
650 				ret = cw1200_set_pm(priv, &priv->powersave_mode);
651 		}
652 	} else {
653 		ret = -EINVAL;
654 	}
655 
656 out:
657 	mutex_unlock(&priv->conf_mutex);
658 	return ret;
659 }
660 
cw1200_get_stats(struct ieee80211_hw * dev,struct ieee80211_low_level_stats * stats)661 int cw1200_get_stats(struct ieee80211_hw *dev,
662 		     struct ieee80211_low_level_stats *stats)
663 {
664 	struct cw1200_common *priv = dev->priv;
665 
666 	memcpy(stats, &priv->stats, sizeof(*stats));
667 	return 0;
668 }
669 
cw1200_set_pm(struct cw1200_common * priv,const struct wsm_set_pm * arg)670 int cw1200_set_pm(struct cw1200_common *priv, const struct wsm_set_pm *arg)
671 {
672 	struct wsm_set_pm pm = *arg;
673 
674 	if (priv->uapsd_info.uapsd_flags != 0)
675 		pm.mode &= ~WSM_PSM_FAST_PS_FLAG;
676 
677 	if (memcmp(&pm, &priv->firmware_ps_mode,
678 		   sizeof(struct wsm_set_pm))) {
679 		priv->firmware_ps_mode = pm;
680 		return wsm_set_pm(priv, &pm);
681 	} else {
682 		return 0;
683 	}
684 }
685 
cw1200_set_key(struct ieee80211_hw * dev,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)686 int cw1200_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
687 		   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
688 		   struct ieee80211_key_conf *key)
689 {
690 	int ret = -EOPNOTSUPP;
691 	struct cw1200_common *priv = dev->priv;
692 	struct ieee80211_key_seq seq;
693 
694 	mutex_lock(&priv->conf_mutex);
695 
696 	if (cmd == SET_KEY) {
697 		u8 *peer_addr = NULL;
698 		int pairwise = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ?
699 			1 : 0;
700 		int idx = cw1200_alloc_key(priv);
701 		struct wsm_add_key *wsm_key = &priv->keys[idx];
702 
703 		if (idx < 0) {
704 			ret = -EINVAL;
705 			goto finally;
706 		}
707 
708 		if (sta)
709 			peer_addr = sta->addr;
710 
711 		key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
712 
713 		switch (key->cipher) {
714 		case WLAN_CIPHER_SUITE_WEP40:
715 		case WLAN_CIPHER_SUITE_WEP104:
716 			if (key->keylen > 16) {
717 				cw1200_free_key(priv, idx);
718 				ret = -EINVAL;
719 				goto finally;
720 			}
721 
722 			if (pairwise) {
723 				wsm_key->type = WSM_KEY_TYPE_WEP_PAIRWISE;
724 				memcpy(wsm_key->wep_pairwise.peer,
725 				       peer_addr, ETH_ALEN);
726 				memcpy(wsm_key->wep_pairwise.keydata,
727 				       &key->key[0], key->keylen);
728 				wsm_key->wep_pairwise.keylen = key->keylen;
729 			} else {
730 				wsm_key->type = WSM_KEY_TYPE_WEP_DEFAULT;
731 				memcpy(wsm_key->wep_group.keydata,
732 				       &key->key[0], key->keylen);
733 				wsm_key->wep_group.keylen = key->keylen;
734 				wsm_key->wep_group.keyid = key->keyidx;
735 			}
736 			break;
737 		case WLAN_CIPHER_SUITE_TKIP:
738 			ieee80211_get_key_rx_seq(key, 0, &seq);
739 			if (pairwise) {
740 				wsm_key->type = WSM_KEY_TYPE_TKIP_PAIRWISE;
741 				memcpy(wsm_key->tkip_pairwise.peer,
742 				       peer_addr, ETH_ALEN);
743 				memcpy(wsm_key->tkip_pairwise.keydata,
744 				       &key->key[0], 16);
745 				memcpy(wsm_key->tkip_pairwise.tx_mic_key,
746 				       &key->key[16], 8);
747 				memcpy(wsm_key->tkip_pairwise.rx_mic_key,
748 				       &key->key[24], 8);
749 			} else {
750 				size_t mic_offset =
751 					(priv->mode == NL80211_IFTYPE_AP) ?
752 					16 : 24;
753 				wsm_key->type = WSM_KEY_TYPE_TKIP_GROUP;
754 				memcpy(wsm_key->tkip_group.keydata,
755 				       &key->key[0], 16);
756 				memcpy(wsm_key->tkip_group.rx_mic_key,
757 				       &key->key[mic_offset], 8);
758 
759 				wsm_key->tkip_group.rx_seqnum[0] = seq.tkip.iv16 & 0xff;
760 				wsm_key->tkip_group.rx_seqnum[1] = (seq.tkip.iv16 >> 8) & 0xff;
761 				wsm_key->tkip_group.rx_seqnum[2] = seq.tkip.iv32 & 0xff;
762 				wsm_key->tkip_group.rx_seqnum[3] = (seq.tkip.iv32 >> 8) & 0xff;
763 				wsm_key->tkip_group.rx_seqnum[4] = (seq.tkip.iv32 >> 16) & 0xff;
764 				wsm_key->tkip_group.rx_seqnum[5] = (seq.tkip.iv32 >> 24) & 0xff;
765 				wsm_key->tkip_group.rx_seqnum[6] = 0;
766 				wsm_key->tkip_group.rx_seqnum[7] = 0;
767 
768 				wsm_key->tkip_group.keyid = key->keyidx;
769 			}
770 			break;
771 		case WLAN_CIPHER_SUITE_CCMP:
772 			ieee80211_get_key_rx_seq(key, 0, &seq);
773 			if (pairwise) {
774 				wsm_key->type = WSM_KEY_TYPE_AES_PAIRWISE;
775 				memcpy(wsm_key->aes_pairwise.peer,
776 				       peer_addr, ETH_ALEN);
777 				memcpy(wsm_key->aes_pairwise.keydata,
778 				       &key->key[0], 16);
779 			} else {
780 				wsm_key->type = WSM_KEY_TYPE_AES_GROUP;
781 				memcpy(wsm_key->aes_group.keydata,
782 				       &key->key[0], 16);
783 
784 				wsm_key->aes_group.rx_seqnum[0] = seq.ccmp.pn[5];
785 				wsm_key->aes_group.rx_seqnum[1] = seq.ccmp.pn[4];
786 				wsm_key->aes_group.rx_seqnum[2] = seq.ccmp.pn[3];
787 				wsm_key->aes_group.rx_seqnum[3] = seq.ccmp.pn[2];
788 				wsm_key->aes_group.rx_seqnum[4] = seq.ccmp.pn[1];
789 				wsm_key->aes_group.rx_seqnum[5] = seq.ccmp.pn[0];
790 				wsm_key->aes_group.rx_seqnum[6] = 0;
791 				wsm_key->aes_group.rx_seqnum[7] = 0;
792 				wsm_key->aes_group.keyid = key->keyidx;
793 			}
794 			break;
795 		case WLAN_CIPHER_SUITE_SMS4:
796 			if (pairwise) {
797 				wsm_key->type = WSM_KEY_TYPE_WAPI_PAIRWISE;
798 				memcpy(wsm_key->wapi_pairwise.peer,
799 				       peer_addr, ETH_ALEN);
800 				memcpy(wsm_key->wapi_pairwise.keydata,
801 				       &key->key[0], 16);
802 				memcpy(wsm_key->wapi_pairwise.mic_key,
803 				       &key->key[16], 16);
804 				wsm_key->wapi_pairwise.keyid = key->keyidx;
805 			} else {
806 				wsm_key->type = WSM_KEY_TYPE_WAPI_GROUP;
807 				memcpy(wsm_key->wapi_group.keydata,
808 				       &key->key[0],  16);
809 				memcpy(wsm_key->wapi_group.mic_key,
810 				       &key->key[16], 16);
811 				wsm_key->wapi_group.keyid = key->keyidx;
812 			}
813 			break;
814 		default:
815 			pr_warn("Unhandled key type %d\n", key->cipher);
816 			cw1200_free_key(priv, idx);
817 			ret = -EOPNOTSUPP;
818 			goto finally;
819 		}
820 		ret = wsm_add_key(priv, wsm_key);
821 		if (!ret)
822 			key->hw_key_idx = idx;
823 		else
824 			cw1200_free_key(priv, idx);
825 	} else if (cmd == DISABLE_KEY) {
826 		struct wsm_remove_key wsm_key = {
827 			.index = key->hw_key_idx,
828 		};
829 
830 		if (wsm_key.index > WSM_KEY_MAX_INDEX) {
831 			ret = -EINVAL;
832 			goto finally;
833 		}
834 
835 		cw1200_free_key(priv, wsm_key.index);
836 		ret = wsm_remove_key(priv, &wsm_key);
837 	} else {
838 		pr_warn("Unhandled key command %d\n", cmd);
839 	}
840 
841 finally:
842 	mutex_unlock(&priv->conf_mutex);
843 	return ret;
844 }
845 
cw1200_wep_key_work(struct work_struct * work)846 void cw1200_wep_key_work(struct work_struct *work)
847 {
848 	struct cw1200_common *priv =
849 		container_of(work, struct cw1200_common, wep_key_work);
850 	u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
851 	struct cw1200_queue *queue = &priv->tx_queue[queue_id];
852 	__le32 wep_default_key_id = __cpu_to_le32(
853 		priv->wep_default_key_id);
854 
855 	pr_debug("[STA] Setting default WEP key: %d\n",
856 		 priv->wep_default_key_id);
857 	wsm_flush_tx(priv);
858 	wsm_write_mib(priv, WSM_MIB_ID_DOT11_WEP_DEFAULT_KEY_ID,
859 		      &wep_default_key_id, sizeof(wep_default_key_id));
860 	cw1200_queue_requeue(queue, priv->pending_frame_id);
861 	wsm_unlock_tx(priv);
862 }
863 
cw1200_set_rts_threshold(struct ieee80211_hw * hw,u32 value)864 int cw1200_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
865 {
866 	int ret = 0;
867 	__le32 val32;
868 	struct cw1200_common *priv = hw->priv;
869 
870 	if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
871 		return 0;
872 
873 	if (value != (u32) -1)
874 		val32 = __cpu_to_le32(value);
875 	else
876 		val32 = 0; /* disabled */
877 
878 	if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
879 		/* device is down, can _not_ set threshold */
880 		ret = -ENODEV;
881 		goto out;
882 	}
883 
884 	if (priv->rts_threshold == value)
885 		goto out;
886 
887 	pr_debug("[STA] Setting RTS threshold: %d\n",
888 		 priv->rts_threshold);
889 
890 	/* mutex_lock(&priv->conf_mutex); */
891 	ret = wsm_write_mib(priv, WSM_MIB_ID_DOT11_RTS_THRESHOLD,
892 			    &val32, sizeof(val32));
893 	if (!ret)
894 		priv->rts_threshold = value;
895 	/* mutex_unlock(&priv->conf_mutex); */
896 
897 out:
898 	return ret;
899 }
900 
901 /* If successful, LOCKS the TX queue! */
__cw1200_flush(struct cw1200_common * priv,bool drop)902 static int __cw1200_flush(struct cw1200_common *priv, bool drop)
903 {
904 	int i, ret;
905 
906 	for (;;) {
907 		/* TODO: correct flush handling is required when dev_stop.
908 		 * Temporary workaround: 2s
909 		 */
910 		if (drop) {
911 			for (i = 0; i < 4; ++i)
912 				cw1200_queue_clear(&priv->tx_queue[i]);
913 		} else {
914 			ret = wait_event_timeout(
915 				priv->tx_queue_stats.wait_link_id_empty,
916 				cw1200_queue_stats_is_empty(
917 					&priv->tx_queue_stats, -1),
918 				2 * HZ);
919 		}
920 
921 		if (!drop && ret <= 0) {
922 			ret = -ETIMEDOUT;
923 			break;
924 		} else {
925 			ret = 0;
926 		}
927 
928 		wsm_lock_tx(priv);
929 		if (!cw1200_queue_stats_is_empty(&priv->tx_queue_stats, -1)) {
930 			/* Highly unlikely: WSM requeued frames. */
931 			wsm_unlock_tx(priv);
932 			continue;
933 		}
934 		break;
935 	}
936 	return ret;
937 }
938 
cw1200_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)939 void cw1200_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
940 		  u32 queues, bool drop)
941 {
942 	struct cw1200_common *priv = hw->priv;
943 
944 	switch (priv->mode) {
945 	case NL80211_IFTYPE_MONITOR:
946 		drop = true;
947 		break;
948 	case NL80211_IFTYPE_AP:
949 		if (!priv->enable_beacon)
950 			drop = true;
951 		break;
952 	}
953 
954 	if (!__cw1200_flush(priv, drop))
955 		wsm_unlock_tx(priv);
956 
957 	return;
958 }
959 
960 /* ******************************************************************** */
961 /* WSM callbacks							*/
962 
cw1200_free_event_queue(struct cw1200_common * priv)963 void cw1200_free_event_queue(struct cw1200_common *priv)
964 {
965 	LIST_HEAD(list);
966 
967 	spin_lock(&priv->event_queue_lock);
968 	list_splice_init(&priv->event_queue, &list);
969 	spin_unlock(&priv->event_queue_lock);
970 
971 	__cw1200_free_event_queue(&list);
972 }
973 
cw1200_event_handler(struct work_struct * work)974 void cw1200_event_handler(struct work_struct *work)
975 {
976 	struct cw1200_common *priv =
977 		container_of(work, struct cw1200_common, event_handler);
978 	struct cw1200_wsm_event *event;
979 	LIST_HEAD(list);
980 
981 	spin_lock(&priv->event_queue_lock);
982 	list_splice_init(&priv->event_queue, &list);
983 	spin_unlock(&priv->event_queue_lock);
984 
985 	list_for_each_entry(event, &list, link) {
986 		switch (event->evt.id) {
987 		case WSM_EVENT_ERROR:
988 			pr_err("Unhandled WSM Error from LMAC\n");
989 			break;
990 		case WSM_EVENT_BSS_LOST:
991 			pr_debug("[CQM] BSS lost.\n");
992 			cancel_work_sync(&priv->unjoin_work);
993 			if (!down_trylock(&priv->scan.lock)) {
994 				cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
995 				up(&priv->scan.lock);
996 			} else {
997 				/* Scan is in progress. Delay reporting.
998 				 * Scan complete will trigger bss_loss_work
999 				 */
1000 				priv->delayed_link_loss = 1;
1001 				/* Also start a watchdog. */
1002 				queue_delayed_work(priv->workqueue,
1003 						   &priv->bss_loss_work, 5*HZ);
1004 			}
1005 			break;
1006 		case WSM_EVENT_BSS_REGAINED:
1007 			pr_debug("[CQM] BSS regained.\n");
1008 			cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
1009 			cancel_work_sync(&priv->unjoin_work);
1010 			break;
1011 		case WSM_EVENT_RADAR_DETECTED:
1012 			wiphy_info(priv->hw->wiphy, "radar pulse detected\n");
1013 			break;
1014 		case WSM_EVENT_RCPI_RSSI:
1015 		{
1016 			/* RSSI: signed Q8.0, RCPI: unsigned Q7.1
1017 			 * RSSI = RCPI / 2 - 110
1018 			 */
1019 			int rcpi_rssi = (int)(event->evt.data & 0xFF);
1020 			int cqm_evt;
1021 			if (priv->cqm_use_rssi)
1022 				rcpi_rssi = (s8)rcpi_rssi;
1023 			else
1024 				rcpi_rssi =  rcpi_rssi / 2 - 110;
1025 
1026 			cqm_evt = (rcpi_rssi <= priv->cqm_rssi_thold) ?
1027 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW :
1028 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
1029 			pr_debug("[CQM] RSSI event: %d.\n", rcpi_rssi);
1030 			ieee80211_cqm_rssi_notify(priv->vif, cqm_evt,
1031 						  GFP_KERNEL);
1032 			break;
1033 		}
1034 		case WSM_EVENT_BT_INACTIVE:
1035 			pr_warn("Unhandled BT INACTIVE from LMAC\n");
1036 			break;
1037 		case WSM_EVENT_BT_ACTIVE:
1038 			pr_warn("Unhandled BT ACTIVE from LMAC\n");
1039 			break;
1040 		}
1041 	}
1042 	__cw1200_free_event_queue(&list);
1043 }
1044 
cw1200_bss_loss_work(struct work_struct * work)1045 void cw1200_bss_loss_work(struct work_struct *work)
1046 {
1047 	struct cw1200_common *priv =
1048 		container_of(work, struct cw1200_common, bss_loss_work.work);
1049 
1050 	pr_debug("[CQM] Reporting connection loss.\n");
1051 	wsm_lock_tx(priv);
1052 	if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1053 		wsm_unlock_tx(priv);
1054 }
1055 
cw1200_bss_params_work(struct work_struct * work)1056 void cw1200_bss_params_work(struct work_struct *work)
1057 {
1058 	struct cw1200_common *priv =
1059 		container_of(work, struct cw1200_common, bss_params_work);
1060 	mutex_lock(&priv->conf_mutex);
1061 
1062 	priv->bss_params.reset_beacon_loss = 1;
1063 	wsm_set_bss_params(priv, &priv->bss_params);
1064 	priv->bss_params.reset_beacon_loss = 0;
1065 
1066 	mutex_unlock(&priv->conf_mutex);
1067 }
1068 
1069 /* ******************************************************************** */
1070 /* Internal API								*/
1071 
1072 /* This function is called to Parse the SDD file
1073  * to extract listen_interval and PTA related information
1074  * sdd is a TLV: u8 id, u8 len, u8 data[]
1075  */
cw1200_parse_sdd_file(struct cw1200_common * priv)1076 static int cw1200_parse_sdd_file(struct cw1200_common *priv)
1077 {
1078 	const u8 *p = priv->sdd->data;
1079 	int ret = 0;
1080 
1081 	while (p + 2 <= priv->sdd->data + priv->sdd->size) {
1082 		if (p + p[1] + 2 > priv->sdd->data + priv->sdd->size) {
1083 			pr_warn("Malformed sdd structure\n");
1084 			return -1;
1085 		}
1086 		switch (p[0]) {
1087 		case SDD_PTA_CFG_ELT_ID: {
1088 			u16 v;
1089 			if (p[1] < 4) {
1090 				pr_warn("SDD_PTA_CFG_ELT_ID malformed\n");
1091 				ret = -1;
1092 				break;
1093 			}
1094 			v = le16_to_cpu(*((__le16 *)(p + 2)));
1095 			if (!v)  /* non-zero means this is enabled */
1096 				break;
1097 
1098 			v = le16_to_cpu(*((__le16 *)(p + 4)));
1099 			priv->conf_listen_interval = (v >> 7) & 0x1F;
1100 			pr_debug("PTA found; Listen Interval %d\n",
1101 				 priv->conf_listen_interval);
1102 			break;
1103 		}
1104 		case SDD_REFERENCE_FREQUENCY_ELT_ID: {
1105 			u16 clk = le16_to_cpu(*((__le16 *)(p + 2)));
1106 			if (clk != priv->hw_refclk)
1107 				pr_warn("SDD file doesn't match configured refclk (%d vs %d)\n",
1108 					clk, priv->hw_refclk);
1109 			break;
1110 		}
1111 		default:
1112 			break;
1113 		}
1114 		p += p[1] + 2;
1115 	}
1116 
1117 	if (!priv->bt_present) {
1118 		pr_debug("PTA element NOT found.\n");
1119 		priv->conf_listen_interval = 0;
1120 	}
1121 	return ret;
1122 }
1123 
cw1200_setup_mac(struct cw1200_common * priv)1124 int cw1200_setup_mac(struct cw1200_common *priv)
1125 {
1126 	int ret = 0;
1127 
1128 	/* NOTE: There is a bug in FW: it reports signal
1129 	 * as RSSI if RSSI subscription is enabled.
1130 	 * It's not enough to set WSM_RCPI_RSSI_USE_RSSI.
1131 	 *
1132 	 * NOTE2: RSSI based reports have been switched to RCPI, since
1133 	 * FW has a bug and RSSI reported values are not stable,
1134 	 * what can leads to signal level oscilations in user-end applications
1135 	 */
1136 	struct wsm_rcpi_rssi_threshold threshold = {
1137 		.rssiRcpiMode = WSM_RCPI_RSSI_THRESHOLD_ENABLE |
1138 		WSM_RCPI_RSSI_DONT_USE_UPPER |
1139 		WSM_RCPI_RSSI_DONT_USE_LOWER,
1140 		.rollingAverageCount = 16,
1141 	};
1142 
1143 	struct wsm_configuration cfg = {
1144 		.dot11StationId = &priv->mac_addr[0],
1145 	};
1146 
1147 	/* Remember the decission here to make sure, we will handle
1148 	 * the RCPI/RSSI value correctly on WSM_EVENT_RCPI_RSS
1149 	 */
1150 	if (threshold.rssiRcpiMode & WSM_RCPI_RSSI_USE_RSSI)
1151 		priv->cqm_use_rssi = true;
1152 
1153 	if (!priv->sdd) {
1154 		ret = request_firmware(&priv->sdd, priv->sdd_path, priv->pdev);
1155 		if (ret) {
1156 			pr_err("Can't load sdd file %s.\n", priv->sdd_path);
1157 			return ret;
1158 		}
1159 		cw1200_parse_sdd_file(priv);
1160 	}
1161 
1162 	cfg.dpdData = priv->sdd->data;
1163 	cfg.dpdData_size = priv->sdd->size;
1164 	ret = wsm_configuration(priv, &cfg);
1165 	if (ret)
1166 		return ret;
1167 
1168 	/* Configure RSSI/SCPI reporting as RSSI. */
1169 	wsm_set_rcpi_rssi_threshold(priv, &threshold);
1170 
1171 	return 0;
1172 }
1173 
cw1200_join_complete(struct cw1200_common * priv)1174 static void cw1200_join_complete(struct cw1200_common *priv)
1175 {
1176 	pr_debug("[STA] Join complete (%d)\n", priv->join_complete_status);
1177 
1178 	priv->join_pending = false;
1179 	if (priv->join_complete_status) {
1180 		priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1181 		cw1200_update_listening(priv, priv->listening);
1182 		cw1200_do_unjoin(priv);
1183 		ieee80211_connection_loss(priv->vif);
1184 	} else {
1185 		if (priv->mode == NL80211_IFTYPE_ADHOC)
1186 			priv->join_status = CW1200_JOIN_STATUS_IBSS;
1187 		else
1188 			priv->join_status = CW1200_JOIN_STATUS_PRE_STA;
1189 	}
1190 	wsm_unlock_tx(priv); /* Clearing the lock held before do_join() */
1191 }
1192 
cw1200_join_complete_work(struct work_struct * work)1193 void cw1200_join_complete_work(struct work_struct *work)
1194 {
1195 	struct cw1200_common *priv =
1196 		container_of(work, struct cw1200_common, join_complete_work);
1197 	mutex_lock(&priv->conf_mutex);
1198 	cw1200_join_complete(priv);
1199 	mutex_unlock(&priv->conf_mutex);
1200 }
1201 
cw1200_join_complete_cb(struct cw1200_common * priv,struct wsm_join_complete * arg)1202 void cw1200_join_complete_cb(struct cw1200_common *priv,
1203 			     struct wsm_join_complete *arg)
1204 {
1205 	pr_debug("[STA] cw1200_join_complete_cb called, status=%d.\n",
1206 		 arg->status);
1207 
1208 	if (cancel_delayed_work(&priv->join_timeout)) {
1209 		priv->join_complete_status = arg->status;
1210 		queue_work(priv->workqueue, &priv->join_complete_work);
1211 	}
1212 }
1213 
1214 /* MUST be called with tx_lock held!  It will be unlocked for us. */
cw1200_do_join(struct cw1200_common * priv)1215 static void cw1200_do_join(struct cw1200_common *priv)
1216 {
1217 	const u8 *bssid;
1218 	struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
1219 	struct cfg80211_bss *bss = NULL;
1220 	struct wsm_protected_mgmt_policy mgmt_policy;
1221 	struct wsm_join join = {
1222 		.mode = conf->ibss_joined ?
1223 				WSM_JOIN_MODE_IBSS : WSM_JOIN_MODE_BSS,
1224 		.preamble_type = WSM_JOIN_PREAMBLE_LONG,
1225 		.probe_for_join = 1,
1226 		.atim_window = 0,
1227 		.basic_rate_set = cw1200_rate_mask_to_wsm(priv,
1228 							  conf->basic_rates),
1229 	};
1230 	if (delayed_work_pending(&priv->join_timeout)) {
1231 		pr_warn("[STA] - Join request already pending, skipping..\n");
1232 		wsm_unlock_tx(priv);
1233 		return;
1234 	}
1235 
1236 	if (priv->join_status)
1237 		cw1200_do_unjoin(priv);
1238 
1239 	bssid = priv->vif->bss_conf.bssid;
1240 
1241 	bss = cfg80211_get_bss(priv->hw->wiphy, priv->channel,
1242 			bssid, NULL, 0, 0, 0);
1243 
1244 	if (!bss && !conf->ibss_joined) {
1245 		wsm_unlock_tx(priv);
1246 		return;
1247 	}
1248 
1249 	mutex_lock(&priv->conf_mutex);
1250 
1251 	/* Under the conf lock: check scan status and
1252 	 * bail out if it is in progress.
1253 	 */
1254 	if (atomic_read(&priv->scan.in_progress)) {
1255 		wsm_unlock_tx(priv);
1256 		goto done_put;
1257 	}
1258 
1259 	priv->join_pending = true;
1260 
1261 	/* Sanity check basic rates */
1262 	if (!join.basic_rate_set)
1263 		join.basic_rate_set = 7;
1264 
1265 	/* Sanity check beacon interval */
1266 	if (!priv->beacon_int)
1267 		priv->beacon_int = 1;
1268 
1269 	join.beacon_interval = priv->beacon_int;
1270 
1271 	/* BT Coex related changes */
1272 	if (priv->bt_present) {
1273 		if (((priv->conf_listen_interval * 100) %
1274 		     priv->beacon_int) == 0)
1275 			priv->listen_interval =
1276 				((priv->conf_listen_interval * 100) /
1277 				 priv->beacon_int);
1278 		else
1279 			priv->listen_interval =
1280 				((priv->conf_listen_interval * 100) /
1281 				 priv->beacon_int + 1);
1282 	}
1283 
1284 	if (priv->hw->conf.ps_dtim_period)
1285 		priv->join_dtim_period = priv->hw->conf.ps_dtim_period;
1286 	join.dtim_period = priv->join_dtim_period;
1287 
1288 	join.channel_number = priv->channel->hw_value;
1289 	join.band = (priv->channel->band == IEEE80211_BAND_5GHZ) ?
1290 		WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
1291 
1292 	memcpy(join.bssid, bssid, sizeof(join.bssid));
1293 
1294 	pr_debug("[STA] Join BSSID: %pM DTIM: %d, interval: %d\n",
1295 		 join.bssid,
1296 		 join.dtim_period, priv->beacon_int);
1297 
1298 	if (!conf->ibss_joined) {
1299 		const u8 *ssidie;
1300 		rcu_read_lock();
1301 		ssidie = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1302 		if (ssidie) {
1303 			join.ssid_len = ssidie[1];
1304 			memcpy(join.ssid, &ssidie[2], join.ssid_len);
1305 		}
1306 		rcu_read_unlock();
1307 	}
1308 
1309 	if (priv->vif->p2p) {
1310 		join.flags |= WSM_JOIN_FLAGS_P2P_GO;
1311 		join.basic_rate_set =
1312 			cw1200_rate_mask_to_wsm(priv, 0xFF0);
1313 	}
1314 
1315 	/* Enable asynchronous join calls */
1316 	if (!conf->ibss_joined) {
1317 		join.flags |= WSM_JOIN_FLAGS_FORCE;
1318 		join.flags |= WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND;
1319 	}
1320 
1321 	wsm_flush_tx(priv);
1322 
1323 	/* Stay Awake for Join and Auth Timeouts and a bit more */
1324 	cw1200_pm_stay_awake(&priv->pm_state,
1325 			     CW1200_JOIN_TIMEOUT + CW1200_AUTH_TIMEOUT);
1326 
1327 	cw1200_update_listening(priv, false);
1328 
1329 	/* Turn on Block ACKs */
1330 	wsm_set_block_ack_policy(priv, priv->ba_tx_tid_mask,
1331 				 priv->ba_rx_tid_mask);
1332 
1333 	/* Set up timeout */
1334 	if (join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND) {
1335 		priv->join_status = CW1200_JOIN_STATUS_JOINING;
1336 		queue_delayed_work(priv->workqueue,
1337 				   &priv->join_timeout,
1338 				   CW1200_JOIN_TIMEOUT);
1339 	}
1340 
1341 	/* 802.11w protected mgmt frames */
1342 	mgmt_policy.protectedMgmtEnable = 0;
1343 	mgmt_policy.unprotectedMgmtFramesAllowed = 1;
1344 	mgmt_policy.encryptionForAuthFrame = 1;
1345 	wsm_set_protected_mgmt_policy(priv, &mgmt_policy);
1346 
1347 	/* Perform actual join */
1348 	if (wsm_join(priv, &join)) {
1349 		pr_err("[STA] cw1200_join_work: wsm_join failed!\n");
1350 		cancel_delayed_work_sync(&priv->join_timeout);
1351 		cw1200_update_listening(priv, priv->listening);
1352 		/* Tx lock still held, unjoin will clear it. */
1353 		if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1354 			wsm_unlock_tx(priv);
1355 	} else {
1356 		if (!(join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND))
1357 			cw1200_join_complete(priv); /* Will clear tx_lock */
1358 
1359 		/* Upload keys */
1360 		cw1200_upload_keys(priv);
1361 
1362 		/* Due to beacon filtering it is possible that the
1363 		 * AP's beacon is not known for the mac80211 stack.
1364 		 * Disable filtering temporary to make sure the stack
1365 		 * receives at least one
1366 		 */
1367 		priv->disable_beacon_filter = true;
1368 	}
1369 	cw1200_update_filtering(priv);
1370 
1371 done_put:
1372 	mutex_unlock(&priv->conf_mutex);
1373 	if (bss)
1374 		cfg80211_put_bss(priv->hw->wiphy, bss);
1375 }
1376 
cw1200_join_timeout(struct work_struct * work)1377 void cw1200_join_timeout(struct work_struct *work)
1378 {
1379 	struct cw1200_common *priv =
1380 		container_of(work, struct cw1200_common, join_timeout.work);
1381 	pr_debug("[WSM] Join timed out.\n");
1382 	wsm_lock_tx(priv);
1383 	if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1384 		wsm_unlock_tx(priv);
1385 }
1386 
cw1200_do_unjoin(struct cw1200_common * priv)1387 static void cw1200_do_unjoin(struct cw1200_common *priv)
1388 {
1389 	struct wsm_reset reset = {
1390 		.reset_statistics = true,
1391 	};
1392 
1393 	cancel_delayed_work_sync(&priv->join_timeout);
1394 
1395 	mutex_lock(&priv->conf_mutex);
1396 	priv->join_pending = false;
1397 
1398 	if (atomic_read(&priv->scan.in_progress)) {
1399 		if (priv->delayed_unjoin)
1400 			wiphy_dbg(priv->hw->wiphy, "Delayed unjoin is already scheduled.\n");
1401 		else
1402 			priv->delayed_unjoin = true;
1403 		goto done;
1404 	}
1405 
1406 	priv->delayed_link_loss = false;
1407 
1408 	if (!priv->join_status)
1409 		goto done;
1410 
1411 	if (priv->join_status == CW1200_JOIN_STATUS_AP)
1412 		goto done;
1413 
1414 	cancel_work_sync(&priv->update_filtering_work);
1415 	cancel_work_sync(&priv->set_beacon_wakeup_period_work);
1416 	priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1417 
1418 	/* Unjoin is a reset. */
1419 	wsm_flush_tx(priv);
1420 	wsm_keep_alive_period(priv, 0);
1421 	wsm_reset(priv, &reset);
1422 	wsm_set_output_power(priv, priv->output_power * 10);
1423 	priv->join_dtim_period = 0;
1424 	cw1200_setup_mac(priv);
1425 	cw1200_free_event_queue(priv);
1426 	cancel_work_sync(&priv->event_handler);
1427 	cw1200_update_listening(priv, priv->listening);
1428 	cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
1429 
1430 	/* Disable Block ACKs */
1431 	wsm_set_block_ack_policy(priv, 0, 0);
1432 
1433 	priv->disable_beacon_filter = false;
1434 	cw1200_update_filtering(priv);
1435 	memset(&priv->association_mode, 0,
1436 	       sizeof(priv->association_mode));
1437 	memset(&priv->bss_params, 0, sizeof(priv->bss_params));
1438 	priv->setbssparams_done = false;
1439 	memset(&priv->firmware_ps_mode, 0,
1440 	       sizeof(priv->firmware_ps_mode));
1441 
1442 	pr_debug("[STA] Unjoin completed.\n");
1443 
1444 done:
1445 	mutex_unlock(&priv->conf_mutex);
1446 }
1447 
cw1200_unjoin_work(struct work_struct * work)1448 void cw1200_unjoin_work(struct work_struct *work)
1449 {
1450 	struct cw1200_common *priv =
1451 		container_of(work, struct cw1200_common, unjoin_work);
1452 
1453 	cw1200_do_unjoin(priv);
1454 
1455 	/* Tell the stack we're dead */
1456 	ieee80211_connection_loss(priv->vif);
1457 
1458 	wsm_unlock_tx(priv);
1459 }
1460 
cw1200_enable_listening(struct cw1200_common * priv)1461 int cw1200_enable_listening(struct cw1200_common *priv)
1462 {
1463 	struct wsm_start start = {
1464 		.mode = WSM_START_MODE_P2P_DEV,
1465 		.band = WSM_PHY_BAND_2_4G,
1466 		.beacon_interval = 100,
1467 		.dtim_period = 1,
1468 		.probe_delay = 0,
1469 		.basic_rate_set = 0x0F,
1470 	};
1471 
1472 	if (priv->channel) {
1473 		start.band = priv->channel->band == IEEE80211_BAND_5GHZ ?
1474 			     WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
1475 		start.channel_number = priv->channel->hw_value;
1476 	} else {
1477 		start.band = WSM_PHY_BAND_2_4G;
1478 		start.channel_number = 1;
1479 	}
1480 
1481 	return wsm_start(priv, &start);
1482 }
1483 
cw1200_disable_listening(struct cw1200_common * priv)1484 int cw1200_disable_listening(struct cw1200_common *priv)
1485 {
1486 	int ret;
1487 	struct wsm_reset reset = {
1488 		.reset_statistics = true,
1489 	};
1490 	ret = wsm_reset(priv, &reset);
1491 	return ret;
1492 }
1493 
cw1200_update_listening(struct cw1200_common * priv,bool enabled)1494 void cw1200_update_listening(struct cw1200_common *priv, bool enabled)
1495 {
1496 	if (enabled) {
1497 		if (priv->join_status == CW1200_JOIN_STATUS_PASSIVE) {
1498 			if (!cw1200_enable_listening(priv))
1499 				priv->join_status = CW1200_JOIN_STATUS_MONITOR;
1500 			wsm_set_probe_responder(priv, true);
1501 		}
1502 	} else {
1503 		if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
1504 			if (!cw1200_disable_listening(priv))
1505 				priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1506 			wsm_set_probe_responder(priv, false);
1507 		}
1508 	}
1509 }
1510 
cw1200_set_uapsd_param(struct cw1200_common * priv,const struct wsm_edca_params * arg)1511 int cw1200_set_uapsd_param(struct cw1200_common *priv,
1512 			   const struct wsm_edca_params *arg)
1513 {
1514 	int ret;
1515 	u16 uapsd_flags = 0;
1516 
1517 	/* Here's the mapping AC [queue, bit]
1518 	 *  VO [0,3], VI [1, 2], BE [2, 1], BK [3, 0]
1519 	 */
1520 
1521 	if (arg->uapsd_enable[0])
1522 		uapsd_flags |= 1 << 3;
1523 
1524 	if (arg->uapsd_enable[1])
1525 		uapsd_flags |= 1 << 2;
1526 
1527 	if (arg->uapsd_enable[2])
1528 		uapsd_flags |= 1 << 1;
1529 
1530 	if (arg->uapsd_enable[3])
1531 		uapsd_flags |= 1;
1532 
1533 	/* Currently pseudo U-APSD operation is not supported, so setting
1534 	 * MinAutoTriggerInterval, MaxAutoTriggerInterval and
1535 	 * AutoTriggerStep to 0
1536 	 */
1537 
1538 	priv->uapsd_info.uapsd_flags = cpu_to_le16(uapsd_flags);
1539 	priv->uapsd_info.min_auto_trigger_interval = 0;
1540 	priv->uapsd_info.max_auto_trigger_interval = 0;
1541 	priv->uapsd_info.auto_trigger_step = 0;
1542 
1543 	ret = wsm_set_uapsd_info(priv, &priv->uapsd_info);
1544 	return ret;
1545 }
1546 
1547 /* ******************************************************************** */
1548 /* AP API								*/
1549 
cw1200_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1550 int cw1200_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1551 		   struct ieee80211_sta *sta)
1552 {
1553 	struct cw1200_common *priv = hw->priv;
1554 	struct cw1200_sta_priv *sta_priv =
1555 			(struct cw1200_sta_priv *)&sta->drv_priv;
1556 	struct cw1200_link_entry *entry;
1557 	struct sk_buff *skb;
1558 
1559 	if (priv->mode != NL80211_IFTYPE_AP)
1560 		return 0;
1561 
1562 	sta_priv->link_id = cw1200_find_link_id(priv, sta->addr);
1563 	if (WARN_ON(!sta_priv->link_id)) {
1564 		wiphy_info(priv->hw->wiphy,
1565 			   "[AP] No more link IDs available.\n");
1566 		return -ENOENT;
1567 	}
1568 
1569 	entry = &priv->link_id_db[sta_priv->link_id - 1];
1570 	spin_lock_bh(&priv->ps_state_lock);
1571 	if ((sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK) ==
1572 					IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
1573 		priv->sta_asleep_mask |= BIT(sta_priv->link_id);
1574 	entry->status = CW1200_LINK_HARD;
1575 	while ((skb = skb_dequeue(&entry->rx_queue)))
1576 		ieee80211_rx_irqsafe(priv->hw, skb);
1577 	spin_unlock_bh(&priv->ps_state_lock);
1578 	return 0;
1579 }
1580 
cw1200_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1581 int cw1200_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1582 		      struct ieee80211_sta *sta)
1583 {
1584 	struct cw1200_common *priv = hw->priv;
1585 	struct cw1200_sta_priv *sta_priv =
1586 			(struct cw1200_sta_priv *)&sta->drv_priv;
1587 	struct cw1200_link_entry *entry;
1588 
1589 	if (priv->mode != NL80211_IFTYPE_AP || !sta_priv->link_id)
1590 		return 0;
1591 
1592 	entry = &priv->link_id_db[sta_priv->link_id - 1];
1593 	spin_lock_bh(&priv->ps_state_lock);
1594 	entry->status = CW1200_LINK_RESERVE;
1595 	entry->timestamp = jiffies;
1596 	wsm_lock_tx_async(priv);
1597 	if (queue_work(priv->workqueue, &priv->link_id_work) <= 0)
1598 		wsm_unlock_tx(priv);
1599 	spin_unlock_bh(&priv->ps_state_lock);
1600 	flush_workqueue(priv->workqueue);
1601 	return 0;
1602 }
1603 
__cw1200_sta_notify(struct ieee80211_hw * dev,struct ieee80211_vif * vif,enum sta_notify_cmd notify_cmd,int link_id)1604 static void __cw1200_sta_notify(struct ieee80211_hw *dev,
1605 				struct ieee80211_vif *vif,
1606 				enum sta_notify_cmd notify_cmd,
1607 				int link_id)
1608 {
1609 	struct cw1200_common *priv = dev->priv;
1610 	u32 bit, prev;
1611 
1612 	/* Zero link id means "for all link IDs" */
1613 	if (link_id)
1614 		bit = BIT(link_id);
1615 	else if (WARN_ON_ONCE(notify_cmd != STA_NOTIFY_AWAKE))
1616 		bit = 0;
1617 	else
1618 		bit = priv->link_id_map;
1619 	prev = priv->sta_asleep_mask & bit;
1620 
1621 	switch (notify_cmd) {
1622 	case STA_NOTIFY_SLEEP:
1623 		if (!prev) {
1624 			if (priv->buffered_multicasts &&
1625 			    !priv->sta_asleep_mask)
1626 				queue_work(priv->workqueue,
1627 					   &priv->multicast_start_work);
1628 			priv->sta_asleep_mask |= bit;
1629 		}
1630 		break;
1631 	case STA_NOTIFY_AWAKE:
1632 		if (prev) {
1633 			priv->sta_asleep_mask &= ~bit;
1634 			priv->pspoll_mask &= ~bit;
1635 			if (priv->tx_multicast && link_id &&
1636 			    !priv->sta_asleep_mask)
1637 				queue_work(priv->workqueue,
1638 					   &priv->multicast_stop_work);
1639 			cw1200_bh_wakeup(priv);
1640 		}
1641 		break;
1642 	}
1643 }
1644 
cw1200_sta_notify(struct ieee80211_hw * dev,struct ieee80211_vif * vif,enum sta_notify_cmd notify_cmd,struct ieee80211_sta * sta)1645 void cw1200_sta_notify(struct ieee80211_hw *dev,
1646 		       struct ieee80211_vif *vif,
1647 		       enum sta_notify_cmd notify_cmd,
1648 		       struct ieee80211_sta *sta)
1649 {
1650 	struct cw1200_common *priv = dev->priv;
1651 	struct cw1200_sta_priv *sta_priv =
1652 		(struct cw1200_sta_priv *)&sta->drv_priv;
1653 
1654 	spin_lock_bh(&priv->ps_state_lock);
1655 	__cw1200_sta_notify(dev, vif, notify_cmd, sta_priv->link_id);
1656 	spin_unlock_bh(&priv->ps_state_lock);
1657 }
1658 
cw1200_ps_notify(struct cw1200_common * priv,int link_id,bool ps)1659 static void cw1200_ps_notify(struct cw1200_common *priv,
1660 		      int link_id, bool ps)
1661 {
1662 	if (link_id > CW1200_MAX_STA_IN_AP_MODE)
1663 		return;
1664 
1665 	pr_debug("%s for LinkId: %d. STAs asleep: %.8X\n",
1666 		 ps ? "Stop" : "Start",
1667 		 link_id, priv->sta_asleep_mask);
1668 
1669 	__cw1200_sta_notify(priv->hw, priv->vif,
1670 			    ps ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE, link_id);
1671 }
1672 
cw1200_set_tim_impl(struct cw1200_common * priv,bool aid0_bit_set)1673 static int cw1200_set_tim_impl(struct cw1200_common *priv, bool aid0_bit_set)
1674 {
1675 	struct sk_buff *skb;
1676 	struct wsm_update_ie update_ie = {
1677 		.what = WSM_UPDATE_IE_BEACON,
1678 		.count = 1,
1679 	};
1680 	u16 tim_offset, tim_length;
1681 
1682 	pr_debug("[AP] mcast: %s.\n", aid0_bit_set ? "ena" : "dis");
1683 
1684 	skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
1685 			&tim_offset, &tim_length);
1686 	if (!skb) {
1687 		if (!__cw1200_flush(priv, true))
1688 			wsm_unlock_tx(priv);
1689 		return -ENOENT;
1690 	}
1691 
1692 	if (tim_offset && tim_length >= 6) {
1693 		/* Ignore DTIM count from mac80211:
1694 		 * firmware handles DTIM internally.
1695 		 */
1696 		skb->data[tim_offset + 2] = 0;
1697 
1698 		/* Set/reset aid0 bit */
1699 		if (aid0_bit_set)
1700 			skb->data[tim_offset + 4] |= 1;
1701 		else
1702 			skb->data[tim_offset + 4] &= ~1;
1703 	}
1704 
1705 	update_ie.ies = &skb->data[tim_offset];
1706 	update_ie.length = tim_length;
1707 	wsm_update_ie(priv, &update_ie);
1708 
1709 	dev_kfree_skb(skb);
1710 
1711 	return 0;
1712 }
1713 
cw1200_set_tim_work(struct work_struct * work)1714 void cw1200_set_tim_work(struct work_struct *work)
1715 {
1716 	struct cw1200_common *priv =
1717 		container_of(work, struct cw1200_common, set_tim_work);
1718 	(void)cw1200_set_tim_impl(priv, priv->aid0_bit_set);
1719 }
1720 
cw1200_set_tim(struct ieee80211_hw * dev,struct ieee80211_sta * sta,bool set)1721 int cw1200_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
1722 		   bool set)
1723 {
1724 	struct cw1200_common *priv = dev->priv;
1725 	queue_work(priv->workqueue, &priv->set_tim_work);
1726 	return 0;
1727 }
1728 
cw1200_set_cts_work(struct work_struct * work)1729 void cw1200_set_cts_work(struct work_struct *work)
1730 {
1731 	struct cw1200_common *priv =
1732 		container_of(work, struct cw1200_common, set_cts_work);
1733 
1734 	u8 erp_ie[3] = {WLAN_EID_ERP_INFO, 0x1, 0};
1735 	struct wsm_update_ie update_ie = {
1736 		.what = WSM_UPDATE_IE_BEACON,
1737 		.count = 1,
1738 		.ies = erp_ie,
1739 		.length = 3,
1740 	};
1741 	u32 erp_info;
1742 	__le32 use_cts_prot;
1743 	mutex_lock(&priv->conf_mutex);
1744 	erp_info = priv->erp_info;
1745 	mutex_unlock(&priv->conf_mutex);
1746 	use_cts_prot =
1747 		erp_info & WLAN_ERP_USE_PROTECTION ?
1748 		__cpu_to_le32(1) : 0;
1749 
1750 	erp_ie[ERP_INFO_BYTE_OFFSET] = erp_info;
1751 
1752 	pr_debug("[STA] ERP information 0x%x\n", erp_info);
1753 
1754 	wsm_write_mib(priv, WSM_MIB_ID_NON_ERP_PROTECTION,
1755 		      &use_cts_prot, sizeof(use_cts_prot));
1756 	wsm_update_ie(priv, &update_ie);
1757 
1758 	return;
1759 }
1760 
cw1200_set_btcoexinfo(struct cw1200_common * priv)1761 static int cw1200_set_btcoexinfo(struct cw1200_common *priv)
1762 {
1763 	struct wsm_override_internal_txrate arg;
1764 	int ret = 0;
1765 
1766 	if (priv->mode == NL80211_IFTYPE_STATION) {
1767 		/* Plumb PSPOLL and NULL template */
1768 		cw1200_upload_pspoll(priv);
1769 		cw1200_upload_null(priv);
1770 		cw1200_upload_qosnull(priv);
1771 	} else {
1772 		return 0;
1773 	}
1774 
1775 	memset(&arg, 0, sizeof(struct wsm_override_internal_txrate));
1776 
1777 	if (!priv->vif->p2p) {
1778 		/* STATION mode */
1779 		if (priv->bss_params.operational_rate_set & ~0xF) {
1780 			pr_debug("[STA] STA has ERP rates\n");
1781 			/* G or BG mode */
1782 			arg.internalTxRate = (__ffs(
1783 			priv->bss_params.operational_rate_set & ~0xF));
1784 		} else {
1785 			pr_debug("[STA] STA has non ERP rates\n");
1786 			/* B only mode */
1787 			arg.internalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
1788 		}
1789 		arg.nonErpInternalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
1790 	} else {
1791 		/* P2P mode */
1792 		arg.internalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
1793 		arg.nonErpInternalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
1794 	}
1795 
1796 	pr_debug("[STA] BTCOEX_INFO MODE %d, internalTxRate : %x, nonErpInternalTxRate: %x\n",
1797 		 priv->mode,
1798 		 arg.internalTxRate,
1799 		 arg.nonErpInternalTxRate);
1800 
1801 	ret = wsm_write_mib(priv, WSM_MIB_ID_OVERRIDE_INTERNAL_TX_RATE,
1802 			    &arg, sizeof(arg));
1803 
1804 	return ret;
1805 }
1806 
cw1200_bss_info_changed(struct ieee80211_hw * dev,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)1807 void cw1200_bss_info_changed(struct ieee80211_hw *dev,
1808 			     struct ieee80211_vif *vif,
1809 			     struct ieee80211_bss_conf *info,
1810 			     u32 changed)
1811 {
1812 	struct cw1200_common *priv = dev->priv;
1813 	bool do_join = false;
1814 
1815 	mutex_lock(&priv->conf_mutex);
1816 
1817 	pr_debug("BSS CHANGED:  %08x\n", changed);
1818 
1819 	/* TODO: BSS_CHANGED_QOS */
1820 	/* TODO: BSS_CHANGED_TXPOWER */
1821 
1822 	if (changed & BSS_CHANGED_ARP_FILTER) {
1823 		struct wsm_mib_arp_ipv4_filter filter = {0};
1824 		int i;
1825 
1826 		pr_debug("[STA] BSS_CHANGED_ARP_FILTER cnt: %d\n",
1827 			 info->arp_addr_cnt);
1828 
1829 		/* Currently only one IP address is supported by firmware.
1830 		 * In case of more IPs arp filtering will be disabled.
1831 		 */
1832 		if (info->arp_addr_cnt > 0 &&
1833 		    info->arp_addr_cnt <= WSM_MAX_ARP_IP_ADDRTABLE_ENTRIES) {
1834 			for (i = 0; i < info->arp_addr_cnt; i++) {
1835 				filter.ipv4addrs[i] = info->arp_addr_list[i];
1836 				pr_debug("[STA] addr[%d]: 0x%X\n",
1837 					 i, filter.ipv4addrs[i]);
1838 			}
1839 			filter.enable = __cpu_to_le32(1);
1840 		}
1841 
1842 		pr_debug("[STA] arp ip filter enable: %d\n",
1843 			 __le32_to_cpu(filter.enable));
1844 
1845 		wsm_set_arp_ipv4_filter(priv, &filter);
1846 	}
1847 
1848 	if (changed &
1849 	    (BSS_CHANGED_BEACON |
1850 	     BSS_CHANGED_AP_PROBE_RESP |
1851 	     BSS_CHANGED_BSSID |
1852 	     BSS_CHANGED_SSID |
1853 	     BSS_CHANGED_IBSS)) {
1854 		pr_debug("BSS_CHANGED_BEACON\n");
1855 		priv->beacon_int = info->beacon_int;
1856 		cw1200_update_beaconing(priv);
1857 		cw1200_upload_beacon(priv);
1858 	}
1859 
1860 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
1861 		pr_debug("BSS_CHANGED_BEACON_ENABLED (%d)\n", info->enable_beacon);
1862 
1863 		if (priv->enable_beacon != info->enable_beacon) {
1864 			cw1200_enable_beaconing(priv, info->enable_beacon);
1865 			priv->enable_beacon = info->enable_beacon;
1866 		}
1867 	}
1868 
1869 	if (changed & BSS_CHANGED_BEACON_INT) {
1870 		pr_debug("CHANGED_BEACON_INT\n");
1871 		if (info->ibss_joined)
1872 			do_join = true;
1873 		else if (priv->join_status == CW1200_JOIN_STATUS_AP)
1874 			cw1200_update_beaconing(priv);
1875 	}
1876 
1877 	/* assoc/disassoc, or maybe AID changed */
1878 	if (changed & BSS_CHANGED_ASSOC) {
1879 		wsm_lock_tx(priv);
1880 		priv->wep_default_key_id = -1;
1881 		wsm_unlock_tx(priv);
1882 	}
1883 
1884 	if (changed & BSS_CHANGED_BSSID) {
1885 		pr_debug("BSS_CHANGED_BSSID\n");
1886 		do_join = true;
1887 	}
1888 
1889 	if (changed &
1890 	    (BSS_CHANGED_ASSOC |
1891 	     BSS_CHANGED_BSSID |
1892 	     BSS_CHANGED_IBSS |
1893 	     BSS_CHANGED_BASIC_RATES |
1894 	     BSS_CHANGED_HT)) {
1895 		pr_debug("BSS_CHANGED_ASSOC\n");
1896 		if (info->assoc) {
1897 			if (priv->join_status < CW1200_JOIN_STATUS_PRE_STA) {
1898 				ieee80211_connection_loss(vif);
1899 				mutex_unlock(&priv->conf_mutex);
1900 				return;
1901 			} else if (priv->join_status == CW1200_JOIN_STATUS_PRE_STA) {
1902 				priv->join_status = CW1200_JOIN_STATUS_STA;
1903 			}
1904 		} else {
1905 			do_join = true;
1906 		}
1907 
1908 		if (info->assoc || info->ibss_joined) {
1909 			struct ieee80211_sta *sta = NULL;
1910 			__le32 htprot = 0;
1911 
1912 			if (info->dtim_period)
1913 				priv->join_dtim_period = info->dtim_period;
1914 			priv->beacon_int = info->beacon_int;
1915 
1916 			rcu_read_lock();
1917 
1918 			if (info->bssid && !info->ibss_joined)
1919 				sta = ieee80211_find_sta(vif, info->bssid);
1920 			if (sta) {
1921 				priv->ht_info.ht_cap = sta->ht_cap;
1922 				priv->bss_params.operational_rate_set =
1923 					cw1200_rate_mask_to_wsm(priv,
1924 								sta->supp_rates[priv->channel->band]);
1925 				priv->ht_info.channel_type = cfg80211_get_chandef_type(&dev->conf.chandef);
1926 				priv->ht_info.operation_mode = info->ht_operation_mode;
1927 			} else {
1928 				memset(&priv->ht_info, 0,
1929 				       sizeof(priv->ht_info));
1930 				priv->bss_params.operational_rate_set = -1;
1931 			}
1932 			rcu_read_unlock();
1933 
1934 			/* Non Greenfield stations present */
1935 			if (priv->ht_info.operation_mode &
1936 			    IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT)
1937 				htprot |= cpu_to_le32(WSM_NON_GREENFIELD_STA_PRESENT);
1938 
1939 			/* Set HT protection method */
1940 			htprot |= cpu_to_le32((priv->ht_info.operation_mode & IEEE80211_HT_OP_MODE_PROTECTION) << 2);
1941 
1942 			/* TODO:
1943 			 * STBC_param.dual_cts
1944 			 *  STBC_param.LSIG_TXOP_FILL
1945 			 */
1946 
1947 			wsm_write_mib(priv, WSM_MIB_ID_SET_HT_PROTECTION,
1948 				      &htprot, sizeof(htprot));
1949 
1950 			priv->association_mode.greenfield =
1951 				cw1200_ht_greenfield(&priv->ht_info);
1952 			priv->association_mode.flags =
1953 				WSM_ASSOCIATION_MODE_SNOOP_ASSOC_FRAMES |
1954 				WSM_ASSOCIATION_MODE_USE_PREAMBLE_TYPE |
1955 				WSM_ASSOCIATION_MODE_USE_HT_MODE |
1956 				WSM_ASSOCIATION_MODE_USE_BASIC_RATE_SET |
1957 				WSM_ASSOCIATION_MODE_USE_MPDU_START_SPACING;
1958 			priv->association_mode.preamble =
1959 				info->use_short_preamble ?
1960 				WSM_JOIN_PREAMBLE_SHORT :
1961 				WSM_JOIN_PREAMBLE_LONG;
1962 			priv->association_mode.basic_rate_set = __cpu_to_le32(
1963 				cw1200_rate_mask_to_wsm(priv,
1964 							info->basic_rates));
1965 			priv->association_mode.mpdu_start_spacing =
1966 				cw1200_ht_ampdu_density(&priv->ht_info);
1967 
1968 			cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
1969 			cancel_work_sync(&priv->unjoin_work);
1970 
1971 			priv->bss_params.beacon_lost_count = priv->cqm_beacon_loss_count;
1972 			priv->bss_params.aid = info->aid;
1973 
1974 			if (priv->join_dtim_period < 1)
1975 				priv->join_dtim_period = 1;
1976 
1977 			pr_debug("[STA] DTIM %d, interval: %d\n",
1978 				 priv->join_dtim_period, priv->beacon_int);
1979 			pr_debug("[STA] Preamble: %d, Greenfield: %d, Aid: %d, Rates: 0x%.8X, Basic: 0x%.8X\n",
1980 				 priv->association_mode.preamble,
1981 				 priv->association_mode.greenfield,
1982 				 priv->bss_params.aid,
1983 				 priv->bss_params.operational_rate_set,
1984 				 priv->association_mode.basic_rate_set);
1985 			wsm_set_association_mode(priv, &priv->association_mode);
1986 
1987 			if (!info->ibss_joined) {
1988 				wsm_keep_alive_period(priv, 30 /* sec */);
1989 				wsm_set_bss_params(priv, &priv->bss_params);
1990 				priv->setbssparams_done = true;
1991 				cw1200_set_beacon_wakeup_period_work(&priv->set_beacon_wakeup_period_work);
1992 				cw1200_set_pm(priv, &priv->powersave_mode);
1993 			}
1994 			if (priv->vif->p2p) {
1995 				pr_debug("[STA] Setting p2p powersave configuration.\n");
1996 				wsm_set_p2p_ps_modeinfo(priv,
1997 							&priv->p2p_ps_modeinfo);
1998 			}
1999 			if (priv->bt_present)
2000 				cw1200_set_btcoexinfo(priv);
2001 		} else {
2002 			memset(&priv->association_mode, 0,
2003 			       sizeof(priv->association_mode));
2004 			memset(&priv->bss_params, 0, sizeof(priv->bss_params));
2005 		}
2006 	}
2007 
2008 	/* ERP Protection */
2009 	if (changed & (BSS_CHANGED_ASSOC |
2010 		       BSS_CHANGED_ERP_CTS_PROT |
2011 		       BSS_CHANGED_ERP_PREAMBLE)) {
2012 		u32 prev_erp_info = priv->erp_info;
2013 		if (info->use_cts_prot)
2014 			priv->erp_info |= WLAN_ERP_USE_PROTECTION;
2015 		else if (!(prev_erp_info & WLAN_ERP_NON_ERP_PRESENT))
2016 			priv->erp_info &= ~WLAN_ERP_USE_PROTECTION;
2017 
2018 		if (info->use_short_preamble)
2019 			priv->erp_info |= WLAN_ERP_BARKER_PREAMBLE;
2020 		else
2021 			priv->erp_info &= ~WLAN_ERP_BARKER_PREAMBLE;
2022 
2023 		pr_debug("[STA] ERP Protection: %x\n", priv->erp_info);
2024 
2025 		if (prev_erp_info != priv->erp_info)
2026 			queue_work(priv->workqueue, &priv->set_cts_work);
2027 	}
2028 
2029 	/* ERP Slottime */
2030 	if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_SLOT)) {
2031 		__le32 slot_time = info->use_short_slot ?
2032 			__cpu_to_le32(9) : __cpu_to_le32(20);
2033 		pr_debug("[STA] Slot time: %d us.\n",
2034 			 __le32_to_cpu(slot_time));
2035 		wsm_write_mib(priv, WSM_MIB_ID_DOT11_SLOT_TIME,
2036 			      &slot_time, sizeof(slot_time));
2037 	}
2038 
2039 	if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_CQM)) {
2040 		struct wsm_rcpi_rssi_threshold threshold = {
2041 			.rollingAverageCount = 8,
2042 		};
2043 		pr_debug("[CQM] RSSI threshold subscribe: %d +- %d\n",
2044 			 info->cqm_rssi_thold, info->cqm_rssi_hyst);
2045 		priv->cqm_rssi_thold = info->cqm_rssi_thold;
2046 		priv->cqm_rssi_hyst = info->cqm_rssi_hyst;
2047 
2048 		if (info->cqm_rssi_thold || info->cqm_rssi_hyst) {
2049 			/* RSSI subscription enabled */
2050 			/* TODO: It's not a correct way of setting threshold.
2051 			 * Upper and lower must be set equal here and adjusted
2052 			 * in callback. However current implementation is much
2053 			 * more relaible and stable.
2054 			 */
2055 
2056 			/* RSSI: signed Q8.0, RCPI: unsigned Q7.1
2057 			 * RSSI = RCPI / 2 - 110
2058 			 */
2059 			if (priv->cqm_use_rssi) {
2060 				threshold.upperThreshold =
2061 					info->cqm_rssi_thold + info->cqm_rssi_hyst;
2062 				threshold.lowerThreshold =
2063 					info->cqm_rssi_thold;
2064 				threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
2065 			} else {
2066 				threshold.upperThreshold = (info->cqm_rssi_thold + info->cqm_rssi_hyst + 110) * 2;
2067 				threshold.lowerThreshold = (info->cqm_rssi_thold + 110) * 2;
2068 			}
2069 			threshold.rssiRcpiMode |= WSM_RCPI_RSSI_THRESHOLD_ENABLE;
2070 		} else {
2071 			/* There is a bug in FW, see sta.c. We have to enable
2072 			 * dummy subscription to get correct RSSI values.
2073 			 */
2074 			threshold.rssiRcpiMode |=
2075 				WSM_RCPI_RSSI_THRESHOLD_ENABLE |
2076 				WSM_RCPI_RSSI_DONT_USE_UPPER |
2077 				WSM_RCPI_RSSI_DONT_USE_LOWER;
2078 			if (priv->cqm_use_rssi)
2079 				threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
2080 		}
2081 		wsm_set_rcpi_rssi_threshold(priv, &threshold);
2082 	}
2083 	mutex_unlock(&priv->conf_mutex);
2084 
2085 	if (do_join) {
2086 		wsm_lock_tx(priv);
2087 		cw1200_do_join(priv); /* Will unlock it for us */
2088 	}
2089 }
2090 
cw1200_multicast_start_work(struct work_struct * work)2091 void cw1200_multicast_start_work(struct work_struct *work)
2092 {
2093 	struct cw1200_common *priv =
2094 		container_of(work, struct cw1200_common, multicast_start_work);
2095 	long tmo = priv->join_dtim_period *
2096 			(priv->beacon_int + 20) * HZ / 1024;
2097 
2098 	cancel_work_sync(&priv->multicast_stop_work);
2099 
2100 	if (!priv->aid0_bit_set) {
2101 		wsm_lock_tx(priv);
2102 		cw1200_set_tim_impl(priv, true);
2103 		priv->aid0_bit_set = true;
2104 		mod_timer(&priv->mcast_timeout, jiffies + tmo);
2105 		wsm_unlock_tx(priv);
2106 	}
2107 }
2108 
cw1200_multicast_stop_work(struct work_struct * work)2109 void cw1200_multicast_stop_work(struct work_struct *work)
2110 {
2111 	struct cw1200_common *priv =
2112 		container_of(work, struct cw1200_common, multicast_stop_work);
2113 
2114 	if (priv->aid0_bit_set) {
2115 		del_timer_sync(&priv->mcast_timeout);
2116 		wsm_lock_tx(priv);
2117 		priv->aid0_bit_set = false;
2118 		cw1200_set_tim_impl(priv, false);
2119 		wsm_unlock_tx(priv);
2120 	}
2121 }
2122 
cw1200_mcast_timeout(unsigned long arg)2123 void cw1200_mcast_timeout(unsigned long arg)
2124 {
2125 	struct cw1200_common *priv =
2126 		(struct cw1200_common *)arg;
2127 
2128 	wiphy_warn(priv->hw->wiphy,
2129 		   "Multicast delivery timeout.\n");
2130 	spin_lock_bh(&priv->ps_state_lock);
2131 	priv->tx_multicast = priv->aid0_bit_set &&
2132 			priv->buffered_multicasts;
2133 	if (priv->tx_multicast)
2134 		cw1200_bh_wakeup(priv);
2135 	spin_unlock_bh(&priv->ps_state_lock);
2136 }
2137 
cw1200_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum ieee80211_ampdu_mlme_action action,struct ieee80211_sta * sta,u16 tid,u16 * ssn,u8 buf_size)2138 int cw1200_ampdu_action(struct ieee80211_hw *hw,
2139 			struct ieee80211_vif *vif,
2140 			enum ieee80211_ampdu_mlme_action action,
2141 			struct ieee80211_sta *sta, u16 tid, u16 *ssn,
2142 			u8 buf_size)
2143 {
2144 	/* Aggregation is implemented fully in firmware,
2145 	 * including block ack negotiation. Do not allow
2146 	 * mac80211 stack to do anything: it interferes with
2147 	 * the firmware.
2148 	 */
2149 
2150 	/* Note that we still need this function stubbed. */
2151 	return -ENOTSUPP;
2152 }
2153 
2154 /* ******************************************************************** */
2155 /* WSM callback								*/
cw1200_suspend_resume(struct cw1200_common * priv,struct wsm_suspend_resume * arg)2156 void cw1200_suspend_resume(struct cw1200_common *priv,
2157 			  struct wsm_suspend_resume *arg)
2158 {
2159 	pr_debug("[AP] %s: %s\n",
2160 		 arg->stop ? "stop" : "start",
2161 		 arg->multicast ? "broadcast" : "unicast");
2162 
2163 	if (arg->multicast) {
2164 		bool cancel_tmo = false;
2165 		spin_lock_bh(&priv->ps_state_lock);
2166 		if (arg->stop) {
2167 			priv->tx_multicast = false;
2168 		} else {
2169 			/* Firmware sends this indication every DTIM if there
2170 			 * is a STA in powersave connected. There is no reason
2171 			 * to suspend, following wakeup will consume much more
2172 			 * power than it could be saved.
2173 			 */
2174 			cw1200_pm_stay_awake(&priv->pm_state,
2175 					     priv->join_dtim_period *
2176 					     (priv->beacon_int + 20) * HZ / 1024);
2177 			priv->tx_multicast = (priv->aid0_bit_set &&
2178 					      priv->buffered_multicasts);
2179 			if (priv->tx_multicast) {
2180 				cancel_tmo = true;
2181 				cw1200_bh_wakeup(priv);
2182 			}
2183 		}
2184 		spin_unlock_bh(&priv->ps_state_lock);
2185 		if (cancel_tmo)
2186 			del_timer_sync(&priv->mcast_timeout);
2187 	} else {
2188 		spin_lock_bh(&priv->ps_state_lock);
2189 		cw1200_ps_notify(priv, arg->link_id, arg->stop);
2190 		spin_unlock_bh(&priv->ps_state_lock);
2191 		if (!arg->stop)
2192 			cw1200_bh_wakeup(priv);
2193 	}
2194 	return;
2195 }
2196 
2197 /* ******************************************************************** */
2198 /* AP privates								*/
2199 
cw1200_upload_beacon(struct cw1200_common * priv)2200 static int cw1200_upload_beacon(struct cw1200_common *priv)
2201 {
2202 	int ret = 0;
2203 	struct ieee80211_mgmt *mgmt;
2204 	struct wsm_template_frame frame = {
2205 		.frame_type = WSM_FRAME_TYPE_BEACON,
2206 	};
2207 
2208 	u16 tim_offset;
2209 	u16 tim_len;
2210 
2211 	if (priv->mode == NL80211_IFTYPE_STATION ||
2212 	    priv->mode == NL80211_IFTYPE_MONITOR ||
2213 	    priv->mode == NL80211_IFTYPE_UNSPECIFIED)
2214 		goto done;
2215 
2216 	if (priv->vif->p2p)
2217 		frame.rate = WSM_TRANSMIT_RATE_6;
2218 
2219 	frame.skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
2220 					     &tim_offset, &tim_len);
2221 	if (!frame.skb)
2222 		return -ENOMEM;
2223 
2224 	ret = wsm_set_template_frame(priv, &frame);
2225 
2226 	if (ret)
2227 		goto done;
2228 
2229 	/* TODO: Distill probe resp; remove TIM
2230 	 * and any other beacon-specific IEs
2231 	 */
2232 	mgmt = (void *)frame.skb->data;
2233 	mgmt->frame_control =
2234 		__cpu_to_le16(IEEE80211_FTYPE_MGMT |
2235 			      IEEE80211_STYPE_PROBE_RESP);
2236 
2237 	frame.frame_type = WSM_FRAME_TYPE_PROBE_RESPONSE;
2238 	if (priv->vif->p2p) {
2239 		ret = wsm_set_probe_responder(priv, true);
2240 	} else {
2241 		ret = wsm_set_template_frame(priv, &frame);
2242 		wsm_set_probe_responder(priv, false);
2243 	}
2244 
2245 done:
2246 	dev_kfree_skb(frame.skb);
2247 
2248 	return ret;
2249 }
2250 
cw1200_upload_pspoll(struct cw1200_common * priv)2251 static int cw1200_upload_pspoll(struct cw1200_common *priv)
2252 {
2253 	int ret = 0;
2254 	struct wsm_template_frame frame = {
2255 		.frame_type = WSM_FRAME_TYPE_PS_POLL,
2256 		.rate = 0xFF,
2257 	};
2258 
2259 
2260 	frame.skb = ieee80211_pspoll_get(priv->hw, priv->vif);
2261 	if (!frame.skb)
2262 		return -ENOMEM;
2263 
2264 	ret = wsm_set_template_frame(priv, &frame);
2265 
2266 	dev_kfree_skb(frame.skb);
2267 
2268 	return ret;
2269 }
2270 
cw1200_upload_null(struct cw1200_common * priv)2271 static int cw1200_upload_null(struct cw1200_common *priv)
2272 {
2273 	int ret = 0;
2274 	struct wsm_template_frame frame = {
2275 		.frame_type = WSM_FRAME_TYPE_NULL,
2276 		.rate = 0xFF,
2277 	};
2278 
2279 	frame.skb = ieee80211_nullfunc_get(priv->hw, priv->vif);
2280 	if (!frame.skb)
2281 		return -ENOMEM;
2282 
2283 	ret = wsm_set_template_frame(priv, &frame);
2284 
2285 	dev_kfree_skb(frame.skb);
2286 
2287 	return ret;
2288 }
2289 
cw1200_upload_qosnull(struct cw1200_common * priv)2290 static int cw1200_upload_qosnull(struct cw1200_common *priv)
2291 {
2292 	/* TODO:  This needs to be implemented
2293 
2294 	struct wsm_template_frame frame = {
2295 		.frame_type = WSM_FRAME_TYPE_QOS_NULL,
2296 		.rate = 0xFF,
2297 	};
2298 
2299 	frame.skb = ieee80211_qosnullfunc_get(priv->hw, priv->vif);
2300 	if (!frame.skb)
2301 		return -ENOMEM;
2302 
2303 	ret = wsm_set_template_frame(priv, &frame);
2304 
2305 	dev_kfree_skb(frame.skb);
2306 
2307 	*/
2308 	return 0;
2309 }
2310 
cw1200_enable_beaconing(struct cw1200_common * priv,bool enable)2311 static int cw1200_enable_beaconing(struct cw1200_common *priv,
2312 				   bool enable)
2313 {
2314 	struct wsm_beacon_transmit transmit = {
2315 		.enable_beaconing = enable,
2316 	};
2317 
2318 	return wsm_beacon_transmit(priv, &transmit);
2319 }
2320 
cw1200_start_ap(struct cw1200_common * priv)2321 static int cw1200_start_ap(struct cw1200_common *priv)
2322 {
2323 	int ret;
2324 	struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
2325 	struct wsm_start start = {
2326 		.mode = priv->vif->p2p ?
2327 				WSM_START_MODE_P2P_GO : WSM_START_MODE_AP,
2328 		.band = (priv->channel->band == IEEE80211_BAND_5GHZ) ?
2329 				WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G,
2330 		.channel_number = priv->channel->hw_value,
2331 		.beacon_interval = conf->beacon_int,
2332 		.dtim_period = conf->dtim_period,
2333 		.preamble = conf->use_short_preamble ?
2334 				WSM_JOIN_PREAMBLE_SHORT :
2335 				WSM_JOIN_PREAMBLE_LONG,
2336 		.probe_delay = 100,
2337 		.basic_rate_set = cw1200_rate_mask_to_wsm(priv,
2338 				conf->basic_rates),
2339 	};
2340 	struct wsm_operational_mode mode = {
2341 		.power_mode = cw1200_power_mode,
2342 		.disable_more_flag_usage = true,
2343 	};
2344 
2345 	memset(start.ssid, 0, sizeof(start.ssid));
2346 	if (!conf->hidden_ssid) {
2347 		start.ssid_len = conf->ssid_len;
2348 		memcpy(start.ssid, conf->ssid, start.ssid_len);
2349 	}
2350 
2351 	priv->beacon_int = conf->beacon_int;
2352 	priv->join_dtim_period = conf->dtim_period;
2353 
2354 	memset(&priv->link_id_db, 0, sizeof(priv->link_id_db));
2355 
2356 	pr_debug("[AP] ch: %d(%d), bcn: %d(%d), brt: 0x%.8X, ssid: %.*s.\n",
2357 		 start.channel_number, start.band,
2358 		 start.beacon_interval, start.dtim_period,
2359 		 start.basic_rate_set,
2360 		 start.ssid_len, start.ssid);
2361 	ret = wsm_start(priv, &start);
2362 	if (!ret)
2363 		ret = cw1200_upload_keys(priv);
2364 	if (!ret && priv->vif->p2p) {
2365 		pr_debug("[AP] Setting p2p powersave configuration.\n");
2366 		wsm_set_p2p_ps_modeinfo(priv, &priv->p2p_ps_modeinfo);
2367 	}
2368 	if (!ret) {
2369 		wsm_set_block_ack_policy(priv, 0, 0);
2370 		priv->join_status = CW1200_JOIN_STATUS_AP;
2371 		cw1200_update_filtering(priv);
2372 	}
2373 	wsm_set_operational_mode(priv, &mode);
2374 	return ret;
2375 }
2376 
cw1200_update_beaconing(struct cw1200_common * priv)2377 static int cw1200_update_beaconing(struct cw1200_common *priv)
2378 {
2379 	struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
2380 	struct wsm_reset reset = {
2381 		.link_id = 0,
2382 		.reset_statistics = true,
2383 	};
2384 
2385 	if (priv->mode == NL80211_IFTYPE_AP) {
2386 		/* TODO: check if changed channel, band */
2387 		if (priv->join_status != CW1200_JOIN_STATUS_AP ||
2388 		    priv->beacon_int != conf->beacon_int) {
2389 			pr_debug("ap restarting\n");
2390 			wsm_lock_tx(priv);
2391 			if (priv->join_status != CW1200_JOIN_STATUS_PASSIVE)
2392 				wsm_reset(priv, &reset);
2393 			priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
2394 			cw1200_start_ap(priv);
2395 			wsm_unlock_tx(priv);
2396 		} else
2397 			pr_debug("ap started join_status: %d\n",
2398 				 priv->join_status);
2399 	}
2400 	return 0;
2401 }
2402