xref: /linux/drivers/net/wireless/st/cw1200/sta.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2560424e9SKalle Valo /*
3560424e9SKalle Valo  * Mac80211 STA API for ST-Ericsson CW1200 drivers
4560424e9SKalle Valo  *
5560424e9SKalle Valo  * Copyright (c) 2010, ST-Ericsson
6560424e9SKalle Valo  * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
7560424e9SKalle Valo  */
8560424e9SKalle Valo 
9560424e9SKalle Valo #include <linux/vmalloc.h>
10560424e9SKalle Valo #include <linux/sched.h>
11560424e9SKalle Valo #include <linux/firmware.h>
12560424e9SKalle Valo #include <linux/module.h>
13560424e9SKalle Valo #include <linux/etherdevice.h>
14560424e9SKalle Valo 
15560424e9SKalle Valo #include "cw1200.h"
16560424e9SKalle Valo #include "sta.h"
17560424e9SKalle Valo #include "fwio.h"
18560424e9SKalle Valo #include "bh.h"
19560424e9SKalle Valo #include "debug.h"
20560424e9SKalle Valo 
21560424e9SKalle Valo #ifndef ERP_INFO_BYTE_OFFSET
22560424e9SKalle Valo #define ERP_INFO_BYTE_OFFSET 2
23560424e9SKalle Valo #endif
24560424e9SKalle Valo 
25560424e9SKalle Valo static void cw1200_do_join(struct cw1200_common *priv);
26560424e9SKalle Valo static void cw1200_do_unjoin(struct cw1200_common *priv);
27560424e9SKalle Valo 
28560424e9SKalle Valo static int cw1200_upload_beacon(struct cw1200_common *priv);
29560424e9SKalle Valo static int cw1200_upload_pspoll(struct cw1200_common *priv);
30560424e9SKalle Valo static int cw1200_upload_null(struct cw1200_common *priv);
31560424e9SKalle Valo static int cw1200_upload_qosnull(struct cw1200_common *priv);
32560424e9SKalle Valo static int cw1200_start_ap(struct cw1200_common *priv);
33560424e9SKalle Valo static int cw1200_update_beaconing(struct cw1200_common *priv);
34560424e9SKalle Valo static int cw1200_enable_beaconing(struct cw1200_common *priv,
35560424e9SKalle Valo 				   bool enable);
36560424e9SKalle Valo static void __cw1200_sta_notify(struct ieee80211_hw *dev,
37560424e9SKalle Valo 				struct ieee80211_vif *vif,
38560424e9SKalle Valo 				enum sta_notify_cmd notify_cmd,
39560424e9SKalle Valo 				int link_id);
40560424e9SKalle Valo static int __cw1200_flush(struct cw1200_common *priv, bool drop);
41560424e9SKalle Valo 
__cw1200_free_event_queue(struct list_head * list)42560424e9SKalle Valo static inline void __cw1200_free_event_queue(struct list_head *list)
43560424e9SKalle Valo {
44560424e9SKalle Valo 	struct cw1200_wsm_event *event, *tmp;
45560424e9SKalle Valo 	list_for_each_entry_safe(event, tmp, list, link) {
46560424e9SKalle Valo 		list_del(&event->link);
47560424e9SKalle Valo 		kfree(event);
48560424e9SKalle Valo 	}
49560424e9SKalle Valo }
50560424e9SKalle Valo 
51560424e9SKalle Valo /* ******************************************************************** */
52560424e9SKalle Valo /* STA API								*/
53560424e9SKalle Valo 
cw1200_start(struct ieee80211_hw * dev)54560424e9SKalle Valo int cw1200_start(struct ieee80211_hw *dev)
55560424e9SKalle Valo {
56560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
57560424e9SKalle Valo 	int ret = 0;
58560424e9SKalle Valo 
59560424e9SKalle Valo 	cw1200_pm_stay_awake(&priv->pm_state, HZ);
60560424e9SKalle Valo 
61560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
62560424e9SKalle Valo 
63560424e9SKalle Valo 	/* default EDCA */
64560424e9SKalle Valo 	WSM_EDCA_SET(&priv->edca, 0, 0x0002, 0x0003, 0x0007, 47, 0xc8, false);
65560424e9SKalle Valo 	WSM_EDCA_SET(&priv->edca, 1, 0x0002, 0x0007, 0x000f, 94, 0xc8, false);
66560424e9SKalle Valo 	WSM_EDCA_SET(&priv->edca, 2, 0x0003, 0x000f, 0x03ff, 0, 0xc8, false);
67560424e9SKalle Valo 	WSM_EDCA_SET(&priv->edca, 3, 0x0007, 0x000f, 0x03ff, 0, 0xc8, false);
68560424e9SKalle Valo 	ret = wsm_set_edca_params(priv, &priv->edca);
69560424e9SKalle Valo 	if (ret)
70560424e9SKalle Valo 		goto out;
71560424e9SKalle Valo 
72560424e9SKalle Valo 	ret = cw1200_set_uapsd_param(priv, &priv->edca);
73560424e9SKalle Valo 	if (ret)
74560424e9SKalle Valo 		goto out;
75560424e9SKalle Valo 
76560424e9SKalle Valo 	priv->setbssparams_done = false;
77560424e9SKalle Valo 
78560424e9SKalle Valo 	memcpy(priv->mac_addr, dev->wiphy->perm_addr, ETH_ALEN);
79560424e9SKalle Valo 	priv->mode = NL80211_IFTYPE_MONITOR;
80560424e9SKalle Valo 	priv->wep_default_key_id = -1;
81560424e9SKalle Valo 
82560424e9SKalle Valo 	priv->cqm_beacon_loss_count = 10;
83560424e9SKalle Valo 
84560424e9SKalle Valo 	ret = cw1200_setup_mac(priv);
85560424e9SKalle Valo 	if (ret)
86560424e9SKalle Valo 		goto out;
87560424e9SKalle Valo 
88560424e9SKalle Valo out:
89560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
90560424e9SKalle Valo 	return ret;
91560424e9SKalle Valo }
92560424e9SKalle Valo 
cw1200_stop(struct ieee80211_hw * dev,bool suspend)93*1decf05dSEmmanuel Grumbach void cw1200_stop(struct ieee80211_hw *dev, bool suspend)
94560424e9SKalle Valo {
95560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
96560424e9SKalle Valo 	LIST_HEAD(list);
97560424e9SKalle Valo 	int i;
98560424e9SKalle Valo 
99560424e9SKalle Valo 	wsm_lock_tx(priv);
100560424e9SKalle Valo 
101560424e9SKalle Valo 	while (down_trylock(&priv->scan.lock)) {
102560424e9SKalle Valo 		/* Scan is in progress. Force it to stop. */
103560424e9SKalle Valo 		priv->scan.req = NULL;
104560424e9SKalle Valo 		schedule();
105560424e9SKalle Valo 	}
106560424e9SKalle Valo 	up(&priv->scan.lock);
107560424e9SKalle Valo 
108560424e9SKalle Valo 	cancel_delayed_work_sync(&priv->scan.probe_work);
109560424e9SKalle Valo 	cancel_delayed_work_sync(&priv->scan.timeout);
110560424e9SKalle Valo 	cancel_delayed_work_sync(&priv->clear_recent_scan_work);
111560424e9SKalle Valo 	cancel_delayed_work_sync(&priv->join_timeout);
112560424e9SKalle Valo 	cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
113560424e9SKalle Valo 	cancel_work_sync(&priv->unjoin_work);
114560424e9SKalle Valo 	cancel_delayed_work_sync(&priv->link_id_gc_work);
115560424e9SKalle Valo 	flush_workqueue(priv->workqueue);
116560424e9SKalle Valo 	del_timer_sync(&priv->mcast_timeout);
117560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
118560424e9SKalle Valo 	priv->mode = NL80211_IFTYPE_UNSPECIFIED;
119560424e9SKalle Valo 	priv->listening = false;
120560424e9SKalle Valo 
121560424e9SKalle Valo 	spin_lock(&priv->event_queue_lock);
122560424e9SKalle Valo 	list_splice_init(&priv->event_queue, &list);
123560424e9SKalle Valo 	spin_unlock(&priv->event_queue_lock);
124560424e9SKalle Valo 	__cw1200_free_event_queue(&list);
125560424e9SKalle Valo 
126560424e9SKalle Valo 
127560424e9SKalle Valo 	priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
128560424e9SKalle Valo 	priv->join_pending = false;
129560424e9SKalle Valo 
130560424e9SKalle Valo 	for (i = 0; i < 4; i++)
131560424e9SKalle Valo 		cw1200_queue_clear(&priv->tx_queue[i]);
132560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
133560424e9SKalle Valo 	tx_policy_clean(priv);
134560424e9SKalle Valo 
135560424e9SKalle Valo 	/* HACK! */
136560424e9SKalle Valo 	if (atomic_xchg(&priv->tx_lock, 1) != 1)
137560424e9SKalle Valo 		pr_debug("[STA] TX is force-unlocked due to stop request.\n");
138560424e9SKalle Valo 
139560424e9SKalle Valo 	wsm_unlock_tx(priv);
140560424e9SKalle Valo 	atomic_xchg(&priv->tx_lock, 0); /* for recovery to work */
141560424e9SKalle Valo }
142560424e9SKalle Valo 
143560424e9SKalle Valo static int cw1200_bssloss_mitigation = 1;
144560424e9SKalle Valo module_param(cw1200_bssloss_mitigation, int, 0644);
145560424e9SKalle Valo MODULE_PARM_DESC(cw1200_bssloss_mitigation, "BSS Loss mitigation. 0 == disabled, 1 == enabled (default)");
146560424e9SKalle Valo 
147560424e9SKalle Valo 
__cw1200_cqm_bssloss_sm(struct cw1200_common * priv,int init,int good,int bad)148560424e9SKalle Valo void __cw1200_cqm_bssloss_sm(struct cw1200_common *priv,
149560424e9SKalle Valo 			     int init, int good, int bad)
150560424e9SKalle Valo {
151560424e9SKalle Valo 	int tx = 0;
152560424e9SKalle Valo 
153560424e9SKalle Valo 	priv->delayed_link_loss = 0;
154560424e9SKalle Valo 	cancel_work_sync(&priv->bss_params_work);
155560424e9SKalle Valo 
156560424e9SKalle Valo 	pr_debug("[STA] CQM BSSLOSS_SM: state: %d init %d good %d bad: %d txlock: %d uj: %d\n",
157560424e9SKalle Valo 		 priv->bss_loss_state,
158560424e9SKalle Valo 		 init, good, bad,
159560424e9SKalle Valo 		 atomic_read(&priv->tx_lock),
160560424e9SKalle Valo 		 priv->delayed_unjoin);
161560424e9SKalle Valo 
162560424e9SKalle Valo 	/* If we have a pending unjoin */
163560424e9SKalle Valo 	if (priv->delayed_unjoin)
164560424e9SKalle Valo 		return;
165560424e9SKalle Valo 
166560424e9SKalle Valo 	if (init) {
167560424e9SKalle Valo 		queue_delayed_work(priv->workqueue,
168560424e9SKalle Valo 				   &priv->bss_loss_work,
169560424e9SKalle Valo 				   HZ);
170560424e9SKalle Valo 		priv->bss_loss_state = 0;
171560424e9SKalle Valo 
172560424e9SKalle Valo 		/* Skip the confimration procedure in P2P case */
173560424e9SKalle Valo 		if (!priv->vif->p2p && !atomic_read(&priv->tx_lock))
174560424e9SKalle Valo 			tx = 1;
175560424e9SKalle Valo 	} else if (good) {
176560424e9SKalle Valo 		cancel_delayed_work_sync(&priv->bss_loss_work);
177560424e9SKalle Valo 		priv->bss_loss_state = 0;
178560424e9SKalle Valo 		queue_work(priv->workqueue, &priv->bss_params_work);
179560424e9SKalle Valo 	} else if (bad) {
180560424e9SKalle Valo 		/* XXX Should we just keep going until we time out? */
181560424e9SKalle Valo 		if (priv->bss_loss_state < 3)
182560424e9SKalle Valo 			tx = 1;
183560424e9SKalle Valo 	} else {
184560424e9SKalle Valo 		cancel_delayed_work_sync(&priv->bss_loss_work);
185560424e9SKalle Valo 		priv->bss_loss_state = 0;
186560424e9SKalle Valo 	}
187560424e9SKalle Valo 
188560424e9SKalle Valo 	/* Bypass mitigation if it's disabled */
189560424e9SKalle Valo 	if (!cw1200_bssloss_mitigation)
190560424e9SKalle Valo 		tx = 0;
191560424e9SKalle Valo 
192560424e9SKalle Valo 	/* Spit out a NULL packet to our AP if necessary */
193560424e9SKalle Valo 	if (tx) {
194560424e9SKalle Valo 		struct sk_buff *skb;
195560424e9SKalle Valo 
196560424e9SKalle Valo 		priv->bss_loss_state++;
197560424e9SKalle Valo 
1980ab26380SJohannes Berg 		skb = ieee80211_nullfunc_get(priv->hw, priv->vif, -1, false);
199560424e9SKalle Valo 		WARN_ON(!skb);
200560424e9SKalle Valo 		if (skb)
201560424e9SKalle Valo 			cw1200_tx(priv->hw, NULL, skb);
202560424e9SKalle Valo 	}
203560424e9SKalle Valo }
204560424e9SKalle Valo 
cw1200_add_interface(struct ieee80211_hw * dev,struct ieee80211_vif * vif)205560424e9SKalle Valo int cw1200_add_interface(struct ieee80211_hw *dev,
206560424e9SKalle Valo 			 struct ieee80211_vif *vif)
207560424e9SKalle Valo {
208560424e9SKalle Valo 	int ret;
209560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
210560424e9SKalle Valo 	/* __le32 auto_calibration_mode = __cpu_to_le32(1); */
211560424e9SKalle Valo 
212560424e9SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
213560424e9SKalle Valo 			     IEEE80211_VIF_SUPPORTS_UAPSD |
214560424e9SKalle Valo 			     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
215560424e9SKalle Valo 
216560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
217560424e9SKalle Valo 
218560424e9SKalle Valo 	if (priv->mode != NL80211_IFTYPE_MONITOR) {
219560424e9SKalle Valo 		mutex_unlock(&priv->conf_mutex);
220560424e9SKalle Valo 		return -EOPNOTSUPP;
221560424e9SKalle Valo 	}
222560424e9SKalle Valo 
223560424e9SKalle Valo 	switch (vif->type) {
224560424e9SKalle Valo 	case NL80211_IFTYPE_STATION:
225560424e9SKalle Valo 	case NL80211_IFTYPE_ADHOC:
226560424e9SKalle Valo 	case NL80211_IFTYPE_MESH_POINT:
227560424e9SKalle Valo 	case NL80211_IFTYPE_AP:
228560424e9SKalle Valo 		priv->mode = vif->type;
229560424e9SKalle Valo 		break;
230560424e9SKalle Valo 	default:
231560424e9SKalle Valo 		mutex_unlock(&priv->conf_mutex);
232560424e9SKalle Valo 		return -EOPNOTSUPP;
233560424e9SKalle Valo 	}
234560424e9SKalle Valo 
235560424e9SKalle Valo 	priv->vif = vif;
236560424e9SKalle Valo 	memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
237560424e9SKalle Valo 	ret = cw1200_setup_mac(priv);
238560424e9SKalle Valo 	/* Enable auto-calibration */
239560424e9SKalle Valo 	/* Exception in subsequent channel switch; disabled.
240560424e9SKalle Valo 	 *  wsm_write_mib(priv, WSM_MIB_ID_SET_AUTO_CALIBRATION_MODE,
241560424e9SKalle Valo 	 *      &auto_calibration_mode, sizeof(auto_calibration_mode));
242560424e9SKalle Valo 	*/
243560424e9SKalle Valo 
244560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
245560424e9SKalle Valo 	return ret;
246560424e9SKalle Valo }
247560424e9SKalle Valo 
cw1200_remove_interface(struct ieee80211_hw * dev,struct ieee80211_vif * vif)248560424e9SKalle Valo void cw1200_remove_interface(struct ieee80211_hw *dev,
249560424e9SKalle Valo 			     struct ieee80211_vif *vif)
250560424e9SKalle Valo {
251560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
252560424e9SKalle Valo 	struct wsm_reset reset = {
253560424e9SKalle Valo 		.reset_statistics = true,
254560424e9SKalle Valo 	};
255560424e9SKalle Valo 	int i;
256560424e9SKalle Valo 
257560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
258560424e9SKalle Valo 	switch (priv->join_status) {
259560424e9SKalle Valo 	case CW1200_JOIN_STATUS_JOINING:
260560424e9SKalle Valo 	case CW1200_JOIN_STATUS_PRE_STA:
261560424e9SKalle Valo 	case CW1200_JOIN_STATUS_STA:
262560424e9SKalle Valo 	case CW1200_JOIN_STATUS_IBSS:
263560424e9SKalle Valo 		wsm_lock_tx(priv);
264560424e9SKalle Valo 		if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
265560424e9SKalle Valo 			wsm_unlock_tx(priv);
266560424e9SKalle Valo 		break;
267560424e9SKalle Valo 	case CW1200_JOIN_STATUS_AP:
268560424e9SKalle Valo 		for (i = 0; priv->link_id_map; ++i) {
269560424e9SKalle Valo 			if (priv->link_id_map & BIT(i)) {
270560424e9SKalle Valo 				reset.link_id = i;
271560424e9SKalle Valo 				wsm_reset(priv, &reset);
272560424e9SKalle Valo 				priv->link_id_map &= ~BIT(i);
273560424e9SKalle Valo 			}
274560424e9SKalle Valo 		}
275560424e9SKalle Valo 		memset(priv->link_id_db, 0, sizeof(priv->link_id_db));
276560424e9SKalle Valo 		priv->sta_asleep_mask = 0;
277560424e9SKalle Valo 		priv->enable_beacon = false;
278560424e9SKalle Valo 		priv->tx_multicast = false;
279560424e9SKalle Valo 		priv->aid0_bit_set = false;
280560424e9SKalle Valo 		priv->buffered_multicasts = false;
281560424e9SKalle Valo 		priv->pspoll_mask = 0;
282560424e9SKalle Valo 		reset.link_id = 0;
283560424e9SKalle Valo 		wsm_reset(priv, &reset);
284560424e9SKalle Valo 		break;
285560424e9SKalle Valo 	case CW1200_JOIN_STATUS_MONITOR:
286560424e9SKalle Valo 		cw1200_update_listening(priv, false);
287560424e9SKalle Valo 		break;
288560424e9SKalle Valo 	default:
289560424e9SKalle Valo 		break;
290560424e9SKalle Valo 	}
291560424e9SKalle Valo 	priv->vif = NULL;
292560424e9SKalle Valo 	priv->mode = NL80211_IFTYPE_MONITOR;
293560424e9SKalle Valo 	eth_zero_addr(priv->mac_addr);
294560424e9SKalle Valo 	memset(&priv->p2p_ps_modeinfo, 0, sizeof(priv->p2p_ps_modeinfo));
295560424e9SKalle Valo 	cw1200_free_keys(priv);
296560424e9SKalle Valo 	cw1200_setup_mac(priv);
297560424e9SKalle Valo 	priv->listening = false;
298560424e9SKalle Valo 	priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
299560424e9SKalle Valo 	if (!__cw1200_flush(priv, true))
300560424e9SKalle Valo 		wsm_unlock_tx(priv);
301560424e9SKalle Valo 
302560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
303560424e9SKalle Valo }
304560424e9SKalle Valo 
cw1200_change_interface(struct ieee80211_hw * dev,struct ieee80211_vif * vif,enum nl80211_iftype new_type,bool p2p)305560424e9SKalle Valo int cw1200_change_interface(struct ieee80211_hw *dev,
306560424e9SKalle Valo 			    struct ieee80211_vif *vif,
307560424e9SKalle Valo 			    enum nl80211_iftype new_type,
308560424e9SKalle Valo 			    bool p2p)
309560424e9SKalle Valo {
310560424e9SKalle Valo 	int ret = 0;
311560424e9SKalle Valo 	pr_debug("change_interface new: %d (%d), old: %d (%d)\n", new_type,
312560424e9SKalle Valo 		 p2p, vif->type, vif->p2p);
313560424e9SKalle Valo 
314560424e9SKalle Valo 	if (new_type != vif->type || vif->p2p != p2p) {
315560424e9SKalle Valo 		cw1200_remove_interface(dev, vif);
316560424e9SKalle Valo 		vif->type = new_type;
317560424e9SKalle Valo 		vif->p2p = p2p;
318560424e9SKalle Valo 		ret = cw1200_add_interface(dev, vif);
319560424e9SKalle Valo 	}
320560424e9SKalle Valo 
321560424e9SKalle Valo 	return ret;
322560424e9SKalle Valo }
323560424e9SKalle Valo 
cw1200_config(struct ieee80211_hw * dev,u32 changed)324560424e9SKalle Valo int cw1200_config(struct ieee80211_hw *dev, u32 changed)
325560424e9SKalle Valo {
326560424e9SKalle Valo 	int ret = 0;
327560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
328560424e9SKalle Valo 	struct ieee80211_conf *conf = &dev->conf;
329560424e9SKalle Valo 
330560424e9SKalle Valo 	pr_debug("CONFIG CHANGED:  %08x\n", changed);
331560424e9SKalle Valo 
332560424e9SKalle Valo 	down(&priv->scan.lock);
333560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
334560424e9SKalle Valo 	/* TODO: IEEE80211_CONF_CHANGE_QOS */
335560424e9SKalle Valo 	/* TODO: IEEE80211_CONF_CHANGE_LISTEN_INTERVAL */
336560424e9SKalle Valo 
337560424e9SKalle Valo 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
338560424e9SKalle Valo 		priv->output_power = conf->power_level;
339560424e9SKalle Valo 		pr_debug("[STA] TX power: %d\n", priv->output_power);
340560424e9SKalle Valo 		wsm_set_output_power(priv, priv->output_power * 10);
341560424e9SKalle Valo 	}
342560424e9SKalle Valo 
343560424e9SKalle Valo 	if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) &&
344560424e9SKalle Valo 	    (priv->channel != conf->chandef.chan)) {
345560424e9SKalle Valo 		struct ieee80211_channel *ch = conf->chandef.chan;
346560424e9SKalle Valo 		struct wsm_switch_channel channel = {
347560424e9SKalle Valo 			.channel_number = ch->hw_value,
348560424e9SKalle Valo 		};
349560424e9SKalle Valo 		pr_debug("[STA] Freq %d (wsm ch: %d).\n",
350560424e9SKalle Valo 			 ch->center_freq, ch->hw_value);
351560424e9SKalle Valo 
352560424e9SKalle Valo 		/* __cw1200_flush() implicitly locks tx, if successful */
353560424e9SKalle Valo 		if (!__cw1200_flush(priv, false)) {
354560424e9SKalle Valo 			if (!wsm_switch_channel(priv, &channel)) {
355560424e9SKalle Valo 				ret = wait_event_timeout(priv->channel_switch_done,
356560424e9SKalle Valo 							 !priv->channel_switch_in_progress,
357560424e9SKalle Valo 							 3 * HZ);
358560424e9SKalle Valo 				if (ret) {
359560424e9SKalle Valo 					/* Already unlocks if successful */
360560424e9SKalle Valo 					priv->channel = ch;
361560424e9SKalle Valo 					ret = 0;
362560424e9SKalle Valo 				} else {
363560424e9SKalle Valo 					ret = -ETIMEDOUT;
364560424e9SKalle Valo 				}
365560424e9SKalle Valo 			} else {
366560424e9SKalle Valo 				/* Unlock if switch channel fails */
367560424e9SKalle Valo 				wsm_unlock_tx(priv);
368560424e9SKalle Valo 			}
369560424e9SKalle Valo 		}
370560424e9SKalle Valo 	}
371560424e9SKalle Valo 
372560424e9SKalle Valo 	if (changed & IEEE80211_CONF_CHANGE_PS) {
373560424e9SKalle Valo 		if (!(conf->flags & IEEE80211_CONF_PS))
374560424e9SKalle Valo 			priv->powersave_mode.mode = WSM_PSM_ACTIVE;
375560424e9SKalle Valo 		else if (conf->dynamic_ps_timeout <= 0)
376560424e9SKalle Valo 			priv->powersave_mode.mode = WSM_PSM_PS;
377560424e9SKalle Valo 		else
378560424e9SKalle Valo 			priv->powersave_mode.mode = WSM_PSM_FAST_PS;
379560424e9SKalle Valo 
380560424e9SKalle Valo 		/* Firmware requires that value for this 1-byte field must
381560424e9SKalle Valo 		 * be specified in units of 500us. Values above the 128ms
382560424e9SKalle Valo 		 * threshold are not supported.
383560424e9SKalle Valo 		 */
384560424e9SKalle Valo 		if (conf->dynamic_ps_timeout >= 0x80)
385560424e9SKalle Valo 			priv->powersave_mode.fast_psm_idle_period = 0xFF;
386560424e9SKalle Valo 		else
387560424e9SKalle Valo 			priv->powersave_mode.fast_psm_idle_period =
388560424e9SKalle Valo 					conf->dynamic_ps_timeout << 1;
389560424e9SKalle Valo 
390560424e9SKalle Valo 		if (priv->join_status == CW1200_JOIN_STATUS_STA &&
391560424e9SKalle Valo 		    priv->bss_params.aid)
392560424e9SKalle Valo 			cw1200_set_pm(priv, &priv->powersave_mode);
393560424e9SKalle Valo 	}
394560424e9SKalle Valo 
395560424e9SKalle Valo 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
396560424e9SKalle Valo 		/* TBD: It looks like it's transparent
397560424e9SKalle Valo 		 * there's a monitor interface present -- use this
398560424e9SKalle Valo 		 * to determine for example whether to calculate
399560424e9SKalle Valo 		 * timestamps for packets or not, do not use instead
400560424e9SKalle Valo 		 * of filter flags!
401560424e9SKalle Valo 		 */
402560424e9SKalle Valo 	}
403560424e9SKalle Valo 
404560424e9SKalle Valo 	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
405560424e9SKalle Valo 		struct wsm_operational_mode mode = {
406560424e9SKalle Valo 			.power_mode = cw1200_power_mode,
407560424e9SKalle Valo 			.disable_more_flag_usage = true,
408560424e9SKalle Valo 		};
409560424e9SKalle Valo 
410560424e9SKalle Valo 		wsm_lock_tx(priv);
411560424e9SKalle Valo 		/* Disable p2p-dev mode forced by TX request */
412560424e9SKalle Valo 		if ((priv->join_status == CW1200_JOIN_STATUS_MONITOR) &&
413560424e9SKalle Valo 		    (conf->flags & IEEE80211_CONF_IDLE) &&
414560424e9SKalle Valo 		    !priv->listening) {
415560424e9SKalle Valo 			cw1200_disable_listening(priv);
416560424e9SKalle Valo 			priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
417560424e9SKalle Valo 		}
418560424e9SKalle Valo 		wsm_set_operational_mode(priv, &mode);
419560424e9SKalle Valo 		wsm_unlock_tx(priv);
420560424e9SKalle Valo 	}
421560424e9SKalle Valo 
422560424e9SKalle Valo 	if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
423560424e9SKalle Valo 		pr_debug("[STA] Retry limits: %d (long), %d (short).\n",
424560424e9SKalle Valo 			 conf->long_frame_max_tx_count,
425560424e9SKalle Valo 			 conf->short_frame_max_tx_count);
426560424e9SKalle Valo 		spin_lock_bh(&priv->tx_policy_cache.lock);
427560424e9SKalle Valo 		priv->long_frame_max_tx_count = conf->long_frame_max_tx_count;
428560424e9SKalle Valo 		priv->short_frame_max_tx_count =
429560424e9SKalle Valo 			(conf->short_frame_max_tx_count < 0x0F) ?
430560424e9SKalle Valo 			conf->short_frame_max_tx_count : 0x0F;
431560424e9SKalle Valo 		priv->hw->max_rate_tries = priv->short_frame_max_tx_count;
432560424e9SKalle Valo 		spin_unlock_bh(&priv->tx_policy_cache.lock);
433560424e9SKalle Valo 	}
434560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
435560424e9SKalle Valo 	up(&priv->scan.lock);
436560424e9SKalle Valo 	return ret;
437560424e9SKalle Valo }
438560424e9SKalle Valo 
cw1200_update_filtering(struct cw1200_common * priv)439560424e9SKalle Valo void cw1200_update_filtering(struct cw1200_common *priv)
440560424e9SKalle Valo {
441560424e9SKalle Valo 	int ret;
442560424e9SKalle Valo 	bool bssid_filtering = !priv->rx_filter.bssid;
443560424e9SKalle Valo 	bool is_p2p = priv->vif && priv->vif->p2p;
444560424e9SKalle Valo 	bool is_sta = priv->vif && NL80211_IFTYPE_STATION == priv->vif->type;
445560424e9SKalle Valo 
446560424e9SKalle Valo 	static struct wsm_beacon_filter_control bf_ctrl;
447560424e9SKalle Valo 	static struct wsm_mib_beacon_filter_table bf_tbl = {
448560424e9SKalle Valo 		.entry[0].ie_id = WLAN_EID_VENDOR_SPECIFIC,
449560424e9SKalle Valo 		.entry[0].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
450560424e9SKalle Valo 					WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
451560424e9SKalle Valo 					WSM_BEACON_FILTER_IE_HAS_APPEARED,
452560424e9SKalle Valo 		.entry[0].oui[0] = 0x50,
453560424e9SKalle Valo 		.entry[0].oui[1] = 0x6F,
454560424e9SKalle Valo 		.entry[0].oui[2] = 0x9A,
455560424e9SKalle Valo 		.entry[1].ie_id = WLAN_EID_HT_OPERATION,
456560424e9SKalle Valo 		.entry[1].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
457560424e9SKalle Valo 					WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
458560424e9SKalle Valo 					WSM_BEACON_FILTER_IE_HAS_APPEARED,
459560424e9SKalle Valo 		.entry[2].ie_id = WLAN_EID_ERP_INFO,
460560424e9SKalle Valo 		.entry[2].flags = WSM_BEACON_FILTER_IE_HAS_CHANGED |
461560424e9SKalle Valo 					WSM_BEACON_FILTER_IE_NO_LONGER_PRESENT |
462560424e9SKalle Valo 					WSM_BEACON_FILTER_IE_HAS_APPEARED,
463560424e9SKalle Valo 	};
464560424e9SKalle Valo 
465560424e9SKalle Valo 	if (priv->join_status == CW1200_JOIN_STATUS_PASSIVE)
466560424e9SKalle Valo 		return;
467560424e9SKalle Valo 	else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
468560424e9SKalle Valo 		bssid_filtering = false;
469560424e9SKalle Valo 
470560424e9SKalle Valo 	if (priv->disable_beacon_filter) {
471560424e9SKalle Valo 		bf_ctrl.enabled = 0;
472560424e9SKalle Valo 		bf_ctrl.bcn_count = 1;
473560424e9SKalle Valo 		bf_tbl.num = __cpu_to_le32(0);
474560424e9SKalle Valo 	} else if (is_p2p || !is_sta) {
475560424e9SKalle Valo 		bf_ctrl.enabled = WSM_BEACON_FILTER_ENABLE |
476560424e9SKalle Valo 			WSM_BEACON_FILTER_AUTO_ERP;
477560424e9SKalle Valo 		bf_ctrl.bcn_count = 0;
478560424e9SKalle Valo 		bf_tbl.num = __cpu_to_le32(2);
479560424e9SKalle Valo 	} else {
480560424e9SKalle Valo 		bf_ctrl.enabled = WSM_BEACON_FILTER_ENABLE;
481560424e9SKalle Valo 		bf_ctrl.bcn_count = 0;
482560424e9SKalle Valo 		bf_tbl.num = __cpu_to_le32(3);
483560424e9SKalle Valo 	}
484560424e9SKalle Valo 
485560424e9SKalle Valo 	/* When acting as p2p client being connected to p2p GO, in order to
486560424e9SKalle Valo 	 * receive frames from a different p2p device, turn off bssid filter.
487560424e9SKalle Valo 	 *
488560424e9SKalle Valo 	 * WARNING: FW dependency!
489560424e9SKalle Valo 	 * This can only be used with FW WSM371 and its successors.
490560424e9SKalle Valo 	 * In that FW version even with bssid filter turned off,
491560424e9SKalle Valo 	 * device will block most of the unwanted frames.
492560424e9SKalle Valo 	 */
493560424e9SKalle Valo 	if (is_p2p)
494560424e9SKalle Valo 		bssid_filtering = false;
495560424e9SKalle Valo 
496560424e9SKalle Valo 	ret = wsm_set_rx_filter(priv, &priv->rx_filter);
497560424e9SKalle Valo 	if (!ret)
498560424e9SKalle Valo 		ret = wsm_set_beacon_filter_table(priv, &bf_tbl);
499560424e9SKalle Valo 	if (!ret)
500560424e9SKalle Valo 		ret = wsm_beacon_filter_control(priv, &bf_ctrl);
501560424e9SKalle Valo 	if (!ret)
502560424e9SKalle Valo 		ret = wsm_set_bssid_filtering(priv, bssid_filtering);
503560424e9SKalle Valo 	if (!ret)
504560424e9SKalle Valo 		ret = wsm_set_multicast_filter(priv, &priv->multicast_filter);
505560424e9SKalle Valo 	if (ret)
506560424e9SKalle Valo 		wiphy_err(priv->hw->wiphy,
507560424e9SKalle Valo 			  "Update filtering failed: %d.\n", ret);
508560424e9SKalle Valo 	return;
509560424e9SKalle Valo }
510560424e9SKalle Valo 
cw1200_update_filtering_work(struct work_struct * work)511560424e9SKalle Valo void cw1200_update_filtering_work(struct work_struct *work)
512560424e9SKalle Valo {
513560424e9SKalle Valo 	struct cw1200_common *priv =
514560424e9SKalle Valo 		container_of(work, struct cw1200_common,
515560424e9SKalle Valo 			     update_filtering_work);
516560424e9SKalle Valo 
517560424e9SKalle Valo 	cw1200_update_filtering(priv);
518560424e9SKalle Valo }
519560424e9SKalle Valo 
cw1200_set_beacon_wakeup_period_work(struct work_struct * work)520560424e9SKalle Valo void cw1200_set_beacon_wakeup_period_work(struct work_struct *work)
521560424e9SKalle Valo {
522560424e9SKalle Valo 	struct cw1200_common *priv =
523560424e9SKalle Valo 		container_of(work, struct cw1200_common,
524560424e9SKalle Valo 			     set_beacon_wakeup_period_work);
525560424e9SKalle Valo 
526560424e9SKalle Valo 	wsm_set_beacon_wakeup_period(priv,
527560424e9SKalle Valo 				     priv->beacon_int * priv->join_dtim_period >
528560424e9SKalle Valo 				     MAX_BEACON_SKIP_TIME_MS ? 1 :
529560424e9SKalle Valo 				     priv->join_dtim_period, 0);
530560424e9SKalle Valo }
531560424e9SKalle Valo 
cw1200_prepare_multicast(struct ieee80211_hw * hw,struct netdev_hw_addr_list * mc_list)532560424e9SKalle Valo u64 cw1200_prepare_multicast(struct ieee80211_hw *hw,
533560424e9SKalle Valo 			     struct netdev_hw_addr_list *mc_list)
534560424e9SKalle Valo {
535560424e9SKalle Valo 	static u8 broadcast_ipv6[ETH_ALEN] = {
536560424e9SKalle Valo 		0x33, 0x33, 0x00, 0x00, 0x00, 0x01
537560424e9SKalle Valo 	};
538560424e9SKalle Valo 	static u8 broadcast_ipv4[ETH_ALEN] = {
539560424e9SKalle Valo 		0x01, 0x00, 0x5e, 0x00, 0x00, 0x01
540560424e9SKalle Valo 	};
541560424e9SKalle Valo 	struct cw1200_common *priv = hw->priv;
542560424e9SKalle Valo 	struct netdev_hw_addr *ha;
543560424e9SKalle Valo 	int count = 0;
544560424e9SKalle Valo 
545560424e9SKalle Valo 	/* Disable multicast filtering */
546560424e9SKalle Valo 	priv->has_multicast_subscription = false;
547560424e9SKalle Valo 	memset(&priv->multicast_filter, 0x00, sizeof(priv->multicast_filter));
548560424e9SKalle Valo 
549560424e9SKalle Valo 	if (netdev_hw_addr_list_count(mc_list) > WSM_MAX_GRP_ADDRTABLE_ENTRIES)
550560424e9SKalle Valo 		return 0;
551560424e9SKalle Valo 
552560424e9SKalle Valo 	/* Enable if requested */
553560424e9SKalle Valo 	netdev_hw_addr_list_for_each(ha, mc_list) {
554560424e9SKalle Valo 		pr_debug("[STA] multicast: %pM\n", ha->addr);
555560424e9SKalle Valo 		memcpy(&priv->multicast_filter.macaddrs[count],
556560424e9SKalle Valo 		       ha->addr, ETH_ALEN);
557560424e9SKalle Valo 		if (!ether_addr_equal(ha->addr, broadcast_ipv4) &&
558560424e9SKalle Valo 		    !ether_addr_equal(ha->addr, broadcast_ipv6))
559560424e9SKalle Valo 			priv->has_multicast_subscription = true;
560560424e9SKalle Valo 		count++;
561560424e9SKalle Valo 	}
562560424e9SKalle Valo 
563560424e9SKalle Valo 	if (count) {
564560424e9SKalle Valo 		priv->multicast_filter.enable = __cpu_to_le32(1);
565560424e9SKalle Valo 		priv->multicast_filter.num_addrs = __cpu_to_le32(count);
566560424e9SKalle Valo 	}
567560424e9SKalle Valo 
568560424e9SKalle Valo 	return netdev_hw_addr_list_count(mc_list);
569560424e9SKalle Valo }
570560424e9SKalle Valo 
cw1200_configure_filter(struct ieee80211_hw * dev,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)571560424e9SKalle Valo void cw1200_configure_filter(struct ieee80211_hw *dev,
572560424e9SKalle Valo 			     unsigned int changed_flags,
573560424e9SKalle Valo 			     unsigned int *total_flags,
574560424e9SKalle Valo 			     u64 multicast)
575560424e9SKalle Valo {
576560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
577560424e9SKalle Valo 	bool listening = !!(*total_flags &
578560424e9SKalle Valo 			    (FIF_OTHER_BSS |
579560424e9SKalle Valo 			     FIF_BCN_PRBRESP_PROMISC |
580560424e9SKalle Valo 			     FIF_PROBE_REQ));
581560424e9SKalle Valo 
582560424e9SKalle Valo 	*total_flags &= FIF_OTHER_BSS |
583560424e9SKalle Valo 			FIF_FCSFAIL |
584560424e9SKalle Valo 			FIF_BCN_PRBRESP_PROMISC |
585560424e9SKalle Valo 			FIF_PROBE_REQ;
586560424e9SKalle Valo 
587560424e9SKalle Valo 	down(&priv->scan.lock);
588560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
589560424e9SKalle Valo 
590560424e9SKalle Valo 	priv->rx_filter.promiscuous = 0;
591560424e9SKalle Valo 	priv->rx_filter.bssid = (*total_flags & (FIF_OTHER_BSS |
592560424e9SKalle Valo 			FIF_PROBE_REQ)) ? 1 : 0;
593560424e9SKalle Valo 	priv->rx_filter.fcs = (*total_flags & FIF_FCSFAIL) ? 1 : 0;
594560424e9SKalle Valo 	priv->disable_beacon_filter = !(*total_flags &
595560424e9SKalle Valo 					(FIF_BCN_PRBRESP_PROMISC |
596560424e9SKalle Valo 					 FIF_PROBE_REQ));
597560424e9SKalle Valo 	if (priv->listening != listening) {
598560424e9SKalle Valo 		priv->listening = listening;
599560424e9SKalle Valo 		wsm_lock_tx(priv);
600560424e9SKalle Valo 		cw1200_update_listening(priv, listening);
601560424e9SKalle Valo 		wsm_unlock_tx(priv);
602560424e9SKalle Valo 	}
603560424e9SKalle Valo 	cw1200_update_filtering(priv);
604560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
605560424e9SKalle Valo 	up(&priv->scan.lock);
606560424e9SKalle Valo }
607560424e9SKalle Valo 
cw1200_conf_tx(struct ieee80211_hw * dev,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * params)608560424e9SKalle Valo int cw1200_conf_tx(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
609b3e2130bSJohannes Berg 		   unsigned int link_id, u16 queue,
610b3e2130bSJohannes Berg 		   const struct ieee80211_tx_queue_params *params)
611560424e9SKalle Valo {
612560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
613560424e9SKalle Valo 	int ret = 0;
614560424e9SKalle Valo 	/* To prevent re-applying PM request OID again and again*/
615560424e9SKalle Valo 	bool old_uapsd_flags;
616560424e9SKalle Valo 
617560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
618560424e9SKalle Valo 
619560424e9SKalle Valo 	if (queue < dev->queues) {
620560424e9SKalle Valo 		old_uapsd_flags = le16_to_cpu(priv->uapsd_info.uapsd_flags);
621560424e9SKalle Valo 
622560424e9SKalle Valo 		WSM_TX_QUEUE_SET(&priv->tx_queue_params, queue, 0, 0, 0);
623560424e9SKalle Valo 		ret = wsm_set_tx_queue_params(priv,
624560424e9SKalle Valo 					      &priv->tx_queue_params.params[queue], queue);
625560424e9SKalle Valo 		if (ret) {
626560424e9SKalle Valo 			ret = -EINVAL;
627560424e9SKalle Valo 			goto out;
628560424e9SKalle Valo 		}
629560424e9SKalle Valo 
630560424e9SKalle Valo 		WSM_EDCA_SET(&priv->edca, queue, params->aifs,
631560424e9SKalle Valo 			     params->cw_min, params->cw_max,
632560424e9SKalle Valo 			     params->txop, 0xc8,
633560424e9SKalle Valo 			     params->uapsd);
634560424e9SKalle Valo 		ret = wsm_set_edca_params(priv, &priv->edca);
635560424e9SKalle Valo 		if (ret) {
636560424e9SKalle Valo 			ret = -EINVAL;
637560424e9SKalle Valo 			goto out;
638560424e9SKalle Valo 		}
639560424e9SKalle Valo 
640560424e9SKalle Valo 		if (priv->mode == NL80211_IFTYPE_STATION) {
641560424e9SKalle Valo 			ret = cw1200_set_uapsd_param(priv, &priv->edca);
642560424e9SKalle Valo 			if (!ret && priv->setbssparams_done &&
643560424e9SKalle Valo 			    (priv->join_status == CW1200_JOIN_STATUS_STA) &&
644560424e9SKalle Valo 			    (old_uapsd_flags != le16_to_cpu(priv->uapsd_info.uapsd_flags)))
645560424e9SKalle Valo 				ret = cw1200_set_pm(priv, &priv->powersave_mode);
646560424e9SKalle Valo 		}
647560424e9SKalle Valo 	} else {
648560424e9SKalle Valo 		ret = -EINVAL;
649560424e9SKalle Valo 	}
650560424e9SKalle Valo 
651560424e9SKalle Valo out:
652560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
653560424e9SKalle Valo 	return ret;
654560424e9SKalle Valo }
655560424e9SKalle Valo 
cw1200_get_stats(struct ieee80211_hw * dev,struct ieee80211_low_level_stats * stats)656560424e9SKalle Valo int cw1200_get_stats(struct ieee80211_hw *dev,
657560424e9SKalle Valo 		     struct ieee80211_low_level_stats *stats)
658560424e9SKalle Valo {
659560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
660560424e9SKalle Valo 
661560424e9SKalle Valo 	memcpy(stats, &priv->stats, sizeof(*stats));
662560424e9SKalle Valo 	return 0;
663560424e9SKalle Valo }
664560424e9SKalle Valo 
cw1200_set_pm(struct cw1200_common * priv,const struct wsm_set_pm * arg)665560424e9SKalle Valo int cw1200_set_pm(struct cw1200_common *priv, const struct wsm_set_pm *arg)
666560424e9SKalle Valo {
667560424e9SKalle Valo 	struct wsm_set_pm pm = *arg;
668560424e9SKalle Valo 
669560424e9SKalle Valo 	if (priv->uapsd_info.uapsd_flags != 0)
670560424e9SKalle Valo 		pm.mode &= ~WSM_PSM_FAST_PS_FLAG;
671560424e9SKalle Valo 
672560424e9SKalle Valo 	if (memcmp(&pm, &priv->firmware_ps_mode,
673560424e9SKalle Valo 		   sizeof(struct wsm_set_pm))) {
674560424e9SKalle Valo 		priv->firmware_ps_mode = pm;
675560424e9SKalle Valo 		return wsm_set_pm(priv, &pm);
676560424e9SKalle Valo 	} else {
677560424e9SKalle Valo 		return 0;
678560424e9SKalle Valo 	}
679560424e9SKalle Valo }
680560424e9SKalle Valo 
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)681560424e9SKalle Valo int cw1200_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
682560424e9SKalle Valo 		   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
683560424e9SKalle Valo 		   struct ieee80211_key_conf *key)
684560424e9SKalle Valo {
685560424e9SKalle Valo 	int ret = -EOPNOTSUPP;
686560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
687560424e9SKalle Valo 	struct ieee80211_key_seq seq;
688560424e9SKalle Valo 
689560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
690560424e9SKalle Valo 
691560424e9SKalle Valo 	if (cmd == SET_KEY) {
692560424e9SKalle Valo 		u8 *peer_addr = NULL;
693560424e9SKalle Valo 		int pairwise = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ?
694560424e9SKalle Valo 			1 : 0;
695560424e9SKalle Valo 		int idx = cw1200_alloc_key(priv);
696560424e9SKalle Valo 		struct wsm_add_key *wsm_key = &priv->keys[idx];
697560424e9SKalle Valo 
698560424e9SKalle Valo 		if (idx < 0) {
699560424e9SKalle Valo 			ret = -EINVAL;
700560424e9SKalle Valo 			goto finally;
701560424e9SKalle Valo 		}
702560424e9SKalle Valo 
703560424e9SKalle Valo 		if (sta)
704560424e9SKalle Valo 			peer_addr = sta->addr;
705560424e9SKalle Valo 
706560424e9SKalle Valo 		key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE |
707560424e9SKalle Valo 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
708560424e9SKalle Valo 
709560424e9SKalle Valo 		switch (key->cipher) {
710560424e9SKalle Valo 		case WLAN_CIPHER_SUITE_WEP40:
711560424e9SKalle Valo 		case WLAN_CIPHER_SUITE_WEP104:
712560424e9SKalle Valo 			if (key->keylen > 16) {
713560424e9SKalle Valo 				cw1200_free_key(priv, idx);
714560424e9SKalle Valo 				ret = -EINVAL;
715560424e9SKalle Valo 				goto finally;
716560424e9SKalle Valo 			}
717560424e9SKalle Valo 
718560424e9SKalle Valo 			if (pairwise) {
719560424e9SKalle Valo 				wsm_key->type = WSM_KEY_TYPE_WEP_PAIRWISE;
720560424e9SKalle Valo 				memcpy(wsm_key->wep_pairwise.peer,
721560424e9SKalle Valo 				       peer_addr, ETH_ALEN);
722560424e9SKalle Valo 				memcpy(wsm_key->wep_pairwise.keydata,
723560424e9SKalle Valo 				       &key->key[0], key->keylen);
724560424e9SKalle Valo 				wsm_key->wep_pairwise.keylen = key->keylen;
725560424e9SKalle Valo 			} else {
726560424e9SKalle Valo 				wsm_key->type = WSM_KEY_TYPE_WEP_DEFAULT;
727560424e9SKalle Valo 				memcpy(wsm_key->wep_group.keydata,
728560424e9SKalle Valo 				       &key->key[0], key->keylen);
729560424e9SKalle Valo 				wsm_key->wep_group.keylen = key->keylen;
730560424e9SKalle Valo 				wsm_key->wep_group.keyid = key->keyidx;
731560424e9SKalle Valo 			}
732560424e9SKalle Valo 			break;
733560424e9SKalle Valo 		case WLAN_CIPHER_SUITE_TKIP:
734560424e9SKalle Valo 			ieee80211_get_key_rx_seq(key, 0, &seq);
735560424e9SKalle Valo 			if (pairwise) {
736560424e9SKalle Valo 				wsm_key->type = WSM_KEY_TYPE_TKIP_PAIRWISE;
737560424e9SKalle Valo 				memcpy(wsm_key->tkip_pairwise.peer,
738560424e9SKalle Valo 				       peer_addr, ETH_ALEN);
739560424e9SKalle Valo 				memcpy(wsm_key->tkip_pairwise.keydata,
740560424e9SKalle Valo 				       &key->key[0], 16);
741560424e9SKalle Valo 				memcpy(wsm_key->tkip_pairwise.tx_mic_key,
742560424e9SKalle Valo 				       &key->key[16], 8);
743560424e9SKalle Valo 				memcpy(wsm_key->tkip_pairwise.rx_mic_key,
744560424e9SKalle Valo 				       &key->key[24], 8);
745560424e9SKalle Valo 			} else {
746560424e9SKalle Valo 				size_t mic_offset =
747560424e9SKalle Valo 					(priv->mode == NL80211_IFTYPE_AP) ?
748560424e9SKalle Valo 					16 : 24;
749560424e9SKalle Valo 				wsm_key->type = WSM_KEY_TYPE_TKIP_GROUP;
750560424e9SKalle Valo 				memcpy(wsm_key->tkip_group.keydata,
751560424e9SKalle Valo 				       &key->key[0], 16);
752560424e9SKalle Valo 				memcpy(wsm_key->tkip_group.rx_mic_key,
753560424e9SKalle Valo 				       &key->key[mic_offset], 8);
754560424e9SKalle Valo 
755560424e9SKalle Valo 				wsm_key->tkip_group.rx_seqnum[0] = seq.tkip.iv16 & 0xff;
756560424e9SKalle Valo 				wsm_key->tkip_group.rx_seqnum[1] = (seq.tkip.iv16 >> 8) & 0xff;
757560424e9SKalle Valo 				wsm_key->tkip_group.rx_seqnum[2] = seq.tkip.iv32 & 0xff;
758560424e9SKalle Valo 				wsm_key->tkip_group.rx_seqnum[3] = (seq.tkip.iv32 >> 8) & 0xff;
759560424e9SKalle Valo 				wsm_key->tkip_group.rx_seqnum[4] = (seq.tkip.iv32 >> 16) & 0xff;
760560424e9SKalle Valo 				wsm_key->tkip_group.rx_seqnum[5] = (seq.tkip.iv32 >> 24) & 0xff;
761560424e9SKalle Valo 				wsm_key->tkip_group.rx_seqnum[6] = 0;
762560424e9SKalle Valo 				wsm_key->tkip_group.rx_seqnum[7] = 0;
763560424e9SKalle Valo 
764560424e9SKalle Valo 				wsm_key->tkip_group.keyid = key->keyidx;
765560424e9SKalle Valo 			}
766560424e9SKalle Valo 			break;
767560424e9SKalle Valo 		case WLAN_CIPHER_SUITE_CCMP:
768560424e9SKalle Valo 			ieee80211_get_key_rx_seq(key, 0, &seq);
769560424e9SKalle Valo 			if (pairwise) {
770560424e9SKalle Valo 				wsm_key->type = WSM_KEY_TYPE_AES_PAIRWISE;
771560424e9SKalle Valo 				memcpy(wsm_key->aes_pairwise.peer,
772560424e9SKalle Valo 				       peer_addr, ETH_ALEN);
773560424e9SKalle Valo 				memcpy(wsm_key->aes_pairwise.keydata,
774560424e9SKalle Valo 				       &key->key[0], 16);
775560424e9SKalle Valo 			} else {
776560424e9SKalle Valo 				wsm_key->type = WSM_KEY_TYPE_AES_GROUP;
777560424e9SKalle Valo 				memcpy(wsm_key->aes_group.keydata,
778560424e9SKalle Valo 				       &key->key[0], 16);
779560424e9SKalle Valo 
780560424e9SKalle Valo 				wsm_key->aes_group.rx_seqnum[0] = seq.ccmp.pn[5];
781560424e9SKalle Valo 				wsm_key->aes_group.rx_seqnum[1] = seq.ccmp.pn[4];
782560424e9SKalle Valo 				wsm_key->aes_group.rx_seqnum[2] = seq.ccmp.pn[3];
783560424e9SKalle Valo 				wsm_key->aes_group.rx_seqnum[3] = seq.ccmp.pn[2];
784560424e9SKalle Valo 				wsm_key->aes_group.rx_seqnum[4] = seq.ccmp.pn[1];
785560424e9SKalle Valo 				wsm_key->aes_group.rx_seqnum[5] = seq.ccmp.pn[0];
786560424e9SKalle Valo 				wsm_key->aes_group.rx_seqnum[6] = 0;
787560424e9SKalle Valo 				wsm_key->aes_group.rx_seqnum[7] = 0;
788560424e9SKalle Valo 				wsm_key->aes_group.keyid = key->keyidx;
789560424e9SKalle Valo 			}
790560424e9SKalle Valo 			break;
791560424e9SKalle Valo 		case WLAN_CIPHER_SUITE_SMS4:
792560424e9SKalle Valo 			if (pairwise) {
793560424e9SKalle Valo 				wsm_key->type = WSM_KEY_TYPE_WAPI_PAIRWISE;
794560424e9SKalle Valo 				memcpy(wsm_key->wapi_pairwise.peer,
795560424e9SKalle Valo 				       peer_addr, ETH_ALEN);
796560424e9SKalle Valo 				memcpy(wsm_key->wapi_pairwise.keydata,
797560424e9SKalle Valo 				       &key->key[0], 16);
798560424e9SKalle Valo 				memcpy(wsm_key->wapi_pairwise.mic_key,
799560424e9SKalle Valo 				       &key->key[16], 16);
800560424e9SKalle Valo 				wsm_key->wapi_pairwise.keyid = key->keyidx;
801560424e9SKalle Valo 			} else {
802560424e9SKalle Valo 				wsm_key->type = WSM_KEY_TYPE_WAPI_GROUP;
803560424e9SKalle Valo 				memcpy(wsm_key->wapi_group.keydata,
804560424e9SKalle Valo 				       &key->key[0],  16);
805560424e9SKalle Valo 				memcpy(wsm_key->wapi_group.mic_key,
806560424e9SKalle Valo 				       &key->key[16], 16);
807560424e9SKalle Valo 				wsm_key->wapi_group.keyid = key->keyidx;
808560424e9SKalle Valo 			}
809560424e9SKalle Valo 			break;
810560424e9SKalle Valo 		default:
811560424e9SKalle Valo 			pr_warn("Unhandled key type %d\n", key->cipher);
812560424e9SKalle Valo 			cw1200_free_key(priv, idx);
813560424e9SKalle Valo 			ret = -EOPNOTSUPP;
814560424e9SKalle Valo 			goto finally;
815560424e9SKalle Valo 		}
816560424e9SKalle Valo 		ret = wsm_add_key(priv, wsm_key);
817560424e9SKalle Valo 		if (!ret)
818560424e9SKalle Valo 			key->hw_key_idx = idx;
819560424e9SKalle Valo 		else
820560424e9SKalle Valo 			cw1200_free_key(priv, idx);
821560424e9SKalle Valo 	} else if (cmd == DISABLE_KEY) {
822560424e9SKalle Valo 		struct wsm_remove_key wsm_key = {
823560424e9SKalle Valo 			.index = key->hw_key_idx,
824560424e9SKalle Valo 		};
825560424e9SKalle Valo 
826560424e9SKalle Valo 		if (wsm_key.index > WSM_KEY_MAX_INDEX) {
827560424e9SKalle Valo 			ret = -EINVAL;
828560424e9SKalle Valo 			goto finally;
829560424e9SKalle Valo 		}
830560424e9SKalle Valo 
831560424e9SKalle Valo 		cw1200_free_key(priv, wsm_key.index);
832560424e9SKalle Valo 		ret = wsm_remove_key(priv, &wsm_key);
833560424e9SKalle Valo 	} else {
834560424e9SKalle Valo 		pr_warn("Unhandled key command %d\n", cmd);
835560424e9SKalle Valo 	}
836560424e9SKalle Valo 
837560424e9SKalle Valo finally:
838560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
839560424e9SKalle Valo 	return ret;
840560424e9SKalle Valo }
841560424e9SKalle Valo 
cw1200_wep_key_work(struct work_struct * work)842560424e9SKalle Valo void cw1200_wep_key_work(struct work_struct *work)
843560424e9SKalle Valo {
844560424e9SKalle Valo 	struct cw1200_common *priv =
845560424e9SKalle Valo 		container_of(work, struct cw1200_common, wep_key_work);
846560424e9SKalle Valo 	u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
847560424e9SKalle Valo 	struct cw1200_queue *queue = &priv->tx_queue[queue_id];
848560424e9SKalle Valo 	__le32 wep_default_key_id = __cpu_to_le32(
849560424e9SKalle Valo 		priv->wep_default_key_id);
850560424e9SKalle Valo 
851560424e9SKalle Valo 	pr_debug("[STA] Setting default WEP key: %d\n",
852560424e9SKalle Valo 		 priv->wep_default_key_id);
853560424e9SKalle Valo 	wsm_flush_tx(priv);
854560424e9SKalle Valo 	wsm_write_mib(priv, WSM_MIB_ID_DOT11_WEP_DEFAULT_KEY_ID,
855560424e9SKalle Valo 		      &wep_default_key_id, sizeof(wep_default_key_id));
856560424e9SKalle Valo 	cw1200_queue_requeue(queue, priv->pending_frame_id);
857560424e9SKalle Valo 	wsm_unlock_tx(priv);
858560424e9SKalle Valo }
859560424e9SKalle Valo 
cw1200_set_rts_threshold(struct ieee80211_hw * hw,u32 value)860560424e9SKalle Valo int cw1200_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
861560424e9SKalle Valo {
862560424e9SKalle Valo 	int ret = 0;
863560424e9SKalle Valo 	__le32 val32;
864560424e9SKalle Valo 	struct cw1200_common *priv = hw->priv;
865560424e9SKalle Valo 
866560424e9SKalle Valo 	if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
867560424e9SKalle Valo 		return 0;
868560424e9SKalle Valo 
869560424e9SKalle Valo 	if (value != (u32) -1)
870560424e9SKalle Valo 		val32 = __cpu_to_le32(value);
871560424e9SKalle Valo 	else
872560424e9SKalle Valo 		val32 = 0; /* disabled */
873560424e9SKalle Valo 
874560424e9SKalle Valo 	if (priv->rts_threshold == value)
875560424e9SKalle Valo 		goto out;
876560424e9SKalle Valo 
877560424e9SKalle Valo 	pr_debug("[STA] Setting RTS threshold: %d\n",
878560424e9SKalle Valo 		 priv->rts_threshold);
879560424e9SKalle Valo 
880560424e9SKalle Valo 	/* mutex_lock(&priv->conf_mutex); */
881560424e9SKalle Valo 	ret = wsm_write_mib(priv, WSM_MIB_ID_DOT11_RTS_THRESHOLD,
882560424e9SKalle Valo 			    &val32, sizeof(val32));
883560424e9SKalle Valo 	if (!ret)
884560424e9SKalle Valo 		priv->rts_threshold = value;
885560424e9SKalle Valo 	/* mutex_unlock(&priv->conf_mutex); */
886560424e9SKalle Valo 
887560424e9SKalle Valo out:
888560424e9SKalle Valo 	return ret;
889560424e9SKalle Valo }
890560424e9SKalle Valo 
891560424e9SKalle Valo /* If successful, LOCKS the TX queue! */
__cw1200_flush(struct cw1200_common * priv,bool drop)892560424e9SKalle Valo static int __cw1200_flush(struct cw1200_common *priv, bool drop)
893560424e9SKalle Valo {
894560424e9SKalle Valo 	int i, ret;
895560424e9SKalle Valo 
896560424e9SKalle Valo 	for (;;) {
897560424e9SKalle Valo 		/* TODO: correct flush handling is required when dev_stop.
898560424e9SKalle Valo 		 * Temporary workaround: 2s
899560424e9SKalle Valo 		 */
900560424e9SKalle Valo 		if (drop) {
901560424e9SKalle Valo 			for (i = 0; i < 4; ++i)
902560424e9SKalle Valo 				cw1200_queue_clear(&priv->tx_queue[i]);
903560424e9SKalle Valo 		} else {
904560424e9SKalle Valo 			ret = wait_event_timeout(
905560424e9SKalle Valo 				priv->tx_queue_stats.wait_link_id_empty,
906560424e9SKalle Valo 				cw1200_queue_stats_is_empty(
907560424e9SKalle Valo 					&priv->tx_queue_stats, -1),
908560424e9SKalle Valo 				2 * HZ);
909560424e9SKalle Valo 		}
910560424e9SKalle Valo 
911560424e9SKalle Valo 		if (!drop && ret <= 0) {
912560424e9SKalle Valo 			ret = -ETIMEDOUT;
913560424e9SKalle Valo 			break;
914560424e9SKalle Valo 		} else {
915560424e9SKalle Valo 			ret = 0;
916560424e9SKalle Valo 		}
917560424e9SKalle Valo 
918560424e9SKalle Valo 		wsm_lock_tx(priv);
919560424e9SKalle Valo 		if (!cw1200_queue_stats_is_empty(&priv->tx_queue_stats, -1)) {
920560424e9SKalle Valo 			/* Highly unlikely: WSM requeued frames. */
921560424e9SKalle Valo 			wsm_unlock_tx(priv);
922560424e9SKalle Valo 			continue;
923560424e9SKalle Valo 		}
924560424e9SKalle Valo 		break;
925560424e9SKalle Valo 	}
926560424e9SKalle Valo 	return ret;
927560424e9SKalle Valo }
928560424e9SKalle Valo 
cw1200_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)929560424e9SKalle Valo void cw1200_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
930560424e9SKalle Valo 		  u32 queues, bool drop)
931560424e9SKalle Valo {
932560424e9SKalle Valo 	struct cw1200_common *priv = hw->priv;
933560424e9SKalle Valo 
934560424e9SKalle Valo 	switch (priv->mode) {
935560424e9SKalle Valo 	case NL80211_IFTYPE_MONITOR:
936560424e9SKalle Valo 		drop = true;
937560424e9SKalle Valo 		break;
938560424e9SKalle Valo 	case NL80211_IFTYPE_AP:
939560424e9SKalle Valo 		if (!priv->enable_beacon)
940560424e9SKalle Valo 			drop = true;
941560424e9SKalle Valo 		break;
942560424e9SKalle Valo 	}
943560424e9SKalle Valo 
944560424e9SKalle Valo 	if (!__cw1200_flush(priv, drop))
945560424e9SKalle Valo 		wsm_unlock_tx(priv);
946560424e9SKalle Valo 
947560424e9SKalle Valo 	return;
948560424e9SKalle Valo }
949560424e9SKalle Valo 
950560424e9SKalle Valo /* ******************************************************************** */
951560424e9SKalle Valo /* WSM callbacks							*/
952560424e9SKalle Valo 
cw1200_free_event_queue(struct cw1200_common * priv)953560424e9SKalle Valo void cw1200_free_event_queue(struct cw1200_common *priv)
954560424e9SKalle Valo {
955560424e9SKalle Valo 	LIST_HEAD(list);
956560424e9SKalle Valo 
957560424e9SKalle Valo 	spin_lock(&priv->event_queue_lock);
958560424e9SKalle Valo 	list_splice_init(&priv->event_queue, &list);
959560424e9SKalle Valo 	spin_unlock(&priv->event_queue_lock);
960560424e9SKalle Valo 
961560424e9SKalle Valo 	__cw1200_free_event_queue(&list);
962560424e9SKalle Valo }
963560424e9SKalle Valo 
cw1200_event_handler(struct work_struct * work)964560424e9SKalle Valo void cw1200_event_handler(struct work_struct *work)
965560424e9SKalle Valo {
966560424e9SKalle Valo 	struct cw1200_common *priv =
967560424e9SKalle Valo 		container_of(work, struct cw1200_common, event_handler);
968560424e9SKalle Valo 	struct cw1200_wsm_event *event;
969560424e9SKalle Valo 	LIST_HEAD(list);
970560424e9SKalle Valo 
971560424e9SKalle Valo 	spin_lock(&priv->event_queue_lock);
972560424e9SKalle Valo 	list_splice_init(&priv->event_queue, &list);
973560424e9SKalle Valo 	spin_unlock(&priv->event_queue_lock);
974560424e9SKalle Valo 
975560424e9SKalle Valo 	list_for_each_entry(event, &list, link) {
976560424e9SKalle Valo 		switch (event->evt.id) {
977560424e9SKalle Valo 		case WSM_EVENT_ERROR:
978560424e9SKalle Valo 			pr_err("Unhandled WSM Error from LMAC\n");
979560424e9SKalle Valo 			break;
980560424e9SKalle Valo 		case WSM_EVENT_BSS_LOST:
981560424e9SKalle Valo 			pr_debug("[CQM] BSS lost.\n");
982560424e9SKalle Valo 			cancel_work_sync(&priv->unjoin_work);
983560424e9SKalle Valo 			if (!down_trylock(&priv->scan.lock)) {
984560424e9SKalle Valo 				cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
985560424e9SKalle Valo 				up(&priv->scan.lock);
986560424e9SKalle Valo 			} else {
987560424e9SKalle Valo 				/* Scan is in progress. Delay reporting.
988560424e9SKalle Valo 				 * Scan complete will trigger bss_loss_work
989560424e9SKalle Valo 				 */
990560424e9SKalle Valo 				priv->delayed_link_loss = 1;
991560424e9SKalle Valo 				/* Also start a watchdog. */
992560424e9SKalle Valo 				queue_delayed_work(priv->workqueue,
993560424e9SKalle Valo 						   &priv->bss_loss_work, 5*HZ);
994560424e9SKalle Valo 			}
995560424e9SKalle Valo 			break;
996560424e9SKalle Valo 		case WSM_EVENT_BSS_REGAINED:
997560424e9SKalle Valo 			pr_debug("[CQM] BSS regained.\n");
998560424e9SKalle Valo 			cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
999560424e9SKalle Valo 			cancel_work_sync(&priv->unjoin_work);
1000560424e9SKalle Valo 			break;
1001560424e9SKalle Valo 		case WSM_EVENT_RADAR_DETECTED:
1002560424e9SKalle Valo 			wiphy_info(priv->hw->wiphy, "radar pulse detected\n");
1003560424e9SKalle Valo 			break;
1004560424e9SKalle Valo 		case WSM_EVENT_RCPI_RSSI:
1005560424e9SKalle Valo 		{
1006560424e9SKalle Valo 			/* RSSI: signed Q8.0, RCPI: unsigned Q7.1
1007560424e9SKalle Valo 			 * RSSI = RCPI / 2 - 110
1008560424e9SKalle Valo 			 */
1009560424e9SKalle Valo 			int rcpi_rssi = (int)(event->evt.data & 0xFF);
1010560424e9SKalle Valo 			int cqm_evt;
1011560424e9SKalle Valo 			if (priv->cqm_use_rssi)
1012560424e9SKalle Valo 				rcpi_rssi = (s8)rcpi_rssi;
1013560424e9SKalle Valo 			else
1014560424e9SKalle Valo 				rcpi_rssi =  rcpi_rssi / 2 - 110;
1015560424e9SKalle Valo 
1016560424e9SKalle Valo 			cqm_evt = (rcpi_rssi <= priv->cqm_rssi_thold) ?
1017560424e9SKalle Valo 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW :
1018560424e9SKalle Valo 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
1019560424e9SKalle Valo 			pr_debug("[CQM] RSSI event: %d.\n", rcpi_rssi);
1020769f07d8SAndrzej Zaborowski 			ieee80211_cqm_rssi_notify(priv->vif, cqm_evt, rcpi_rssi,
1021560424e9SKalle Valo 						  GFP_KERNEL);
1022560424e9SKalle Valo 			break;
1023560424e9SKalle Valo 		}
1024560424e9SKalle Valo 		case WSM_EVENT_BT_INACTIVE:
1025560424e9SKalle Valo 			pr_warn("Unhandled BT INACTIVE from LMAC\n");
1026560424e9SKalle Valo 			break;
1027560424e9SKalle Valo 		case WSM_EVENT_BT_ACTIVE:
1028560424e9SKalle Valo 			pr_warn("Unhandled BT ACTIVE from LMAC\n");
1029560424e9SKalle Valo 			break;
1030560424e9SKalle Valo 		}
1031560424e9SKalle Valo 	}
1032560424e9SKalle Valo 	__cw1200_free_event_queue(&list);
1033560424e9SKalle Valo }
1034560424e9SKalle Valo 
cw1200_bss_loss_work(struct work_struct * work)1035560424e9SKalle Valo void cw1200_bss_loss_work(struct work_struct *work)
1036560424e9SKalle Valo {
1037560424e9SKalle Valo 	struct cw1200_common *priv =
1038560424e9SKalle Valo 		container_of(work, struct cw1200_common, bss_loss_work.work);
1039560424e9SKalle Valo 
1040560424e9SKalle Valo 	pr_debug("[CQM] Reporting connection loss.\n");
1041560424e9SKalle Valo 	wsm_lock_tx(priv);
1042560424e9SKalle Valo 	if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1043560424e9SKalle Valo 		wsm_unlock_tx(priv);
1044560424e9SKalle Valo }
1045560424e9SKalle Valo 
cw1200_bss_params_work(struct work_struct * work)1046560424e9SKalle Valo void cw1200_bss_params_work(struct work_struct *work)
1047560424e9SKalle Valo {
1048560424e9SKalle Valo 	struct cw1200_common *priv =
1049560424e9SKalle Valo 		container_of(work, struct cw1200_common, bss_params_work);
1050560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
1051560424e9SKalle Valo 
1052560424e9SKalle Valo 	priv->bss_params.reset_beacon_loss = 1;
1053560424e9SKalle Valo 	wsm_set_bss_params(priv, &priv->bss_params);
1054560424e9SKalle Valo 	priv->bss_params.reset_beacon_loss = 0;
1055560424e9SKalle Valo 
1056560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
1057560424e9SKalle Valo }
1058560424e9SKalle Valo 
1059560424e9SKalle Valo /* ******************************************************************** */
1060560424e9SKalle Valo /* Internal API								*/
1061560424e9SKalle Valo 
1062560424e9SKalle Valo /* This function is called to Parse the SDD file
1063560424e9SKalle Valo  * to extract listen_interval and PTA related information
1064560424e9SKalle Valo  * sdd is a TLV: u8 id, u8 len, u8 data[]
1065560424e9SKalle Valo  */
cw1200_parse_sdd_file(struct cw1200_common * priv)1066560424e9SKalle Valo static int cw1200_parse_sdd_file(struct cw1200_common *priv)
1067560424e9SKalle Valo {
1068560424e9SKalle Valo 	const u8 *p = priv->sdd->data;
1069560424e9SKalle Valo 	int ret = 0;
1070560424e9SKalle Valo 
1071560424e9SKalle Valo 	while (p + 2 <= priv->sdd->data + priv->sdd->size) {
1072560424e9SKalle Valo 		if (p + p[1] + 2 > priv->sdd->data + priv->sdd->size) {
1073560424e9SKalle Valo 			pr_warn("Malformed sdd structure\n");
1074560424e9SKalle Valo 			return -1;
1075560424e9SKalle Valo 		}
1076560424e9SKalle Valo 		switch (p[0]) {
1077560424e9SKalle Valo 		case SDD_PTA_CFG_ELT_ID: {
1078560424e9SKalle Valo 			u16 v;
1079560424e9SKalle Valo 			if (p[1] < 4) {
1080560424e9SKalle Valo 				pr_warn("SDD_PTA_CFG_ELT_ID malformed\n");
1081560424e9SKalle Valo 				ret = -1;
1082560424e9SKalle Valo 				break;
1083560424e9SKalle Valo 			}
1084560424e9SKalle Valo 			v = le16_to_cpu(*((__le16 *)(p + 2)));
1085560424e9SKalle Valo 			if (!v)  /* non-zero means this is enabled */
1086560424e9SKalle Valo 				break;
1087560424e9SKalle Valo 
1088560424e9SKalle Valo 			v = le16_to_cpu(*((__le16 *)(p + 4)));
1089560424e9SKalle Valo 			priv->conf_listen_interval = (v >> 7) & 0x1F;
1090560424e9SKalle Valo 			pr_debug("PTA found; Listen Interval %d\n",
1091560424e9SKalle Valo 				 priv->conf_listen_interval);
1092560424e9SKalle Valo 			break;
1093560424e9SKalle Valo 		}
1094560424e9SKalle Valo 		case SDD_REFERENCE_FREQUENCY_ELT_ID: {
1095560424e9SKalle Valo 			u16 clk = le16_to_cpu(*((__le16 *)(p + 2)));
1096560424e9SKalle Valo 			if (clk != priv->hw_refclk)
1097560424e9SKalle Valo 				pr_warn("SDD file doesn't match configured refclk (%d vs %d)\n",
1098560424e9SKalle Valo 					clk, priv->hw_refclk);
1099560424e9SKalle Valo 			break;
1100560424e9SKalle Valo 		}
1101560424e9SKalle Valo 		default:
1102560424e9SKalle Valo 			break;
1103560424e9SKalle Valo 		}
1104560424e9SKalle Valo 		p += p[1] + 2;
1105560424e9SKalle Valo 	}
1106560424e9SKalle Valo 
1107560424e9SKalle Valo 	if (!priv->bt_present) {
1108560424e9SKalle Valo 		pr_debug("PTA element NOT found.\n");
1109560424e9SKalle Valo 		priv->conf_listen_interval = 0;
1110560424e9SKalle Valo 	}
1111560424e9SKalle Valo 	return ret;
1112560424e9SKalle Valo }
1113560424e9SKalle Valo 
cw1200_setup_mac(struct cw1200_common * priv)1114560424e9SKalle Valo int cw1200_setup_mac(struct cw1200_common *priv)
1115560424e9SKalle Valo {
1116560424e9SKalle Valo 	int ret = 0;
1117560424e9SKalle Valo 
1118560424e9SKalle Valo 	/* NOTE: There is a bug in FW: it reports signal
1119560424e9SKalle Valo 	 * as RSSI if RSSI subscription is enabled.
1120560424e9SKalle Valo 	 * It's not enough to set WSM_RCPI_RSSI_USE_RSSI.
1121560424e9SKalle Valo 	 *
1122560424e9SKalle Valo 	 * NOTE2: RSSI based reports have been switched to RCPI, since
1123560424e9SKalle Valo 	 * FW has a bug and RSSI reported values are not stable,
1124f4bd758fSYangtao Li 	 * what can lead to signal level oscilations in user-end applications
1125560424e9SKalle Valo 	 */
1126560424e9SKalle Valo 	struct wsm_rcpi_rssi_threshold threshold = {
1127560424e9SKalle Valo 		.rssiRcpiMode = WSM_RCPI_RSSI_THRESHOLD_ENABLE |
1128560424e9SKalle Valo 		WSM_RCPI_RSSI_DONT_USE_UPPER |
1129560424e9SKalle Valo 		WSM_RCPI_RSSI_DONT_USE_LOWER,
1130560424e9SKalle Valo 		.rollingAverageCount = 16,
1131560424e9SKalle Valo 	};
1132560424e9SKalle Valo 
1133560424e9SKalle Valo 	struct wsm_configuration cfg = {
1134560424e9SKalle Valo 		.dot11StationId = &priv->mac_addr[0],
1135560424e9SKalle Valo 	};
1136560424e9SKalle Valo 
1137560424e9SKalle Valo 	/* Remember the decission here to make sure, we will handle
1138560424e9SKalle Valo 	 * the RCPI/RSSI value correctly on WSM_EVENT_RCPI_RSS
1139560424e9SKalle Valo 	 */
1140560424e9SKalle Valo 	if (threshold.rssiRcpiMode & WSM_RCPI_RSSI_USE_RSSI)
1141560424e9SKalle Valo 		priv->cqm_use_rssi = true;
1142560424e9SKalle Valo 
1143560424e9SKalle Valo 	if (!priv->sdd) {
1144560424e9SKalle Valo 		ret = request_firmware(&priv->sdd, priv->sdd_path, priv->pdev);
1145560424e9SKalle Valo 		if (ret) {
1146560424e9SKalle Valo 			pr_err("Can't load sdd file %s.\n", priv->sdd_path);
1147560424e9SKalle Valo 			return ret;
1148560424e9SKalle Valo 		}
1149560424e9SKalle Valo 		cw1200_parse_sdd_file(priv);
1150560424e9SKalle Valo 	}
1151560424e9SKalle Valo 
1152560424e9SKalle Valo 	cfg.dpdData = priv->sdd->data;
1153560424e9SKalle Valo 	cfg.dpdData_size = priv->sdd->size;
1154560424e9SKalle Valo 	ret = wsm_configuration(priv, &cfg);
1155560424e9SKalle Valo 	if (ret)
1156560424e9SKalle Valo 		return ret;
1157560424e9SKalle Valo 
1158560424e9SKalle Valo 	/* Configure RSSI/SCPI reporting as RSSI. */
1159560424e9SKalle Valo 	wsm_set_rcpi_rssi_threshold(priv, &threshold);
1160560424e9SKalle Valo 
1161560424e9SKalle Valo 	return 0;
1162560424e9SKalle Valo }
1163560424e9SKalle Valo 
cw1200_join_complete(struct cw1200_common * priv)1164560424e9SKalle Valo static void cw1200_join_complete(struct cw1200_common *priv)
1165560424e9SKalle Valo {
1166560424e9SKalle Valo 	pr_debug("[STA] Join complete (%d)\n", priv->join_complete_status);
1167560424e9SKalle Valo 
1168560424e9SKalle Valo 	priv->join_pending = false;
1169560424e9SKalle Valo 	if (priv->join_complete_status) {
1170560424e9SKalle Valo 		priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1171560424e9SKalle Valo 		cw1200_update_listening(priv, priv->listening);
1172560424e9SKalle Valo 		cw1200_do_unjoin(priv);
1173560424e9SKalle Valo 		ieee80211_connection_loss(priv->vif);
1174560424e9SKalle Valo 	} else {
1175560424e9SKalle Valo 		if (priv->mode == NL80211_IFTYPE_ADHOC)
1176560424e9SKalle Valo 			priv->join_status = CW1200_JOIN_STATUS_IBSS;
1177560424e9SKalle Valo 		else
1178560424e9SKalle Valo 			priv->join_status = CW1200_JOIN_STATUS_PRE_STA;
1179560424e9SKalle Valo 	}
1180560424e9SKalle Valo 	wsm_unlock_tx(priv); /* Clearing the lock held before do_join() */
1181560424e9SKalle Valo }
1182560424e9SKalle Valo 
cw1200_join_complete_work(struct work_struct * work)1183560424e9SKalle Valo void cw1200_join_complete_work(struct work_struct *work)
1184560424e9SKalle Valo {
1185560424e9SKalle Valo 	struct cw1200_common *priv =
1186560424e9SKalle Valo 		container_of(work, struct cw1200_common, join_complete_work);
1187560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
1188560424e9SKalle Valo 	cw1200_join_complete(priv);
1189560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
1190560424e9SKalle Valo }
1191560424e9SKalle Valo 
cw1200_join_complete_cb(struct cw1200_common * priv,struct wsm_join_complete * arg)1192560424e9SKalle Valo void cw1200_join_complete_cb(struct cw1200_common *priv,
1193560424e9SKalle Valo 			     struct wsm_join_complete *arg)
1194560424e9SKalle Valo {
1195560424e9SKalle Valo 	pr_debug("[STA] cw1200_join_complete_cb called, status=%d.\n",
1196560424e9SKalle Valo 		 arg->status);
1197560424e9SKalle Valo 
1198560424e9SKalle Valo 	if (cancel_delayed_work(&priv->join_timeout)) {
1199560424e9SKalle Valo 		priv->join_complete_status = arg->status;
1200560424e9SKalle Valo 		queue_work(priv->workqueue, &priv->join_complete_work);
1201560424e9SKalle Valo 	}
1202560424e9SKalle Valo }
1203560424e9SKalle Valo 
1204560424e9SKalle Valo /* MUST be called with tx_lock held!  It will be unlocked for us. */
cw1200_do_join(struct cw1200_common * priv)1205560424e9SKalle Valo static void cw1200_do_join(struct cw1200_common *priv)
1206560424e9SKalle Valo {
1207560424e9SKalle Valo 	const u8 *bssid;
1208560424e9SKalle Valo 	struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
1209560424e9SKalle Valo 	struct cfg80211_bss *bss = NULL;
1210560424e9SKalle Valo 	struct wsm_protected_mgmt_policy mgmt_policy;
1211560424e9SKalle Valo 	struct wsm_join join = {
1212f276e20bSJohannes Berg 		.mode = priv->vif->cfg.ibss_joined ?
1213560424e9SKalle Valo 		WSM_JOIN_MODE_IBSS : WSM_JOIN_MODE_BSS,
1214560424e9SKalle Valo 		.preamble_type = WSM_JOIN_PREAMBLE_LONG,
1215560424e9SKalle Valo 		.probe_for_join = 1,
1216560424e9SKalle Valo 		.atim_window = 0,
1217560424e9SKalle Valo 		.basic_rate_set = cw1200_rate_mask_to_wsm(priv,
1218560424e9SKalle Valo 							  conf->basic_rates),
1219560424e9SKalle Valo 	};
1220560424e9SKalle Valo 	if (delayed_work_pending(&priv->join_timeout)) {
1221560424e9SKalle Valo 		pr_warn("[STA] - Join request already pending, skipping..\n");
1222560424e9SKalle Valo 		wsm_unlock_tx(priv);
1223560424e9SKalle Valo 		return;
1224560424e9SKalle Valo 	}
1225560424e9SKalle Valo 
1226560424e9SKalle Valo 	if (priv->join_status)
1227560424e9SKalle Valo 		cw1200_do_unjoin(priv);
1228560424e9SKalle Valo 
1229560424e9SKalle Valo 	bssid = priv->vif->bss_conf.bssid;
1230560424e9SKalle Valo 
1231560424e9SKalle Valo 	bss = cfg80211_get_bss(priv->hw->wiphy, priv->channel, bssid, NULL, 0,
1232560424e9SKalle Valo 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1233560424e9SKalle Valo 
1234f276e20bSJohannes Berg 	if (!bss && !priv->vif->cfg.ibss_joined) {
1235560424e9SKalle Valo 		wsm_unlock_tx(priv);
1236560424e9SKalle Valo 		return;
1237560424e9SKalle Valo 	}
1238560424e9SKalle Valo 
1239560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
1240560424e9SKalle Valo 
1241560424e9SKalle Valo 	/* Under the conf lock: check scan status and
1242560424e9SKalle Valo 	 * bail out if it is in progress.
1243560424e9SKalle Valo 	 */
1244560424e9SKalle Valo 	if (atomic_read(&priv->scan.in_progress)) {
1245560424e9SKalle Valo 		wsm_unlock_tx(priv);
1246560424e9SKalle Valo 		goto done_put;
1247560424e9SKalle Valo 	}
1248560424e9SKalle Valo 
1249560424e9SKalle Valo 	priv->join_pending = true;
1250560424e9SKalle Valo 
1251560424e9SKalle Valo 	/* Sanity check basic rates */
1252560424e9SKalle Valo 	if (!join.basic_rate_set)
1253560424e9SKalle Valo 		join.basic_rate_set = 7;
1254560424e9SKalle Valo 
1255560424e9SKalle Valo 	/* Sanity check beacon interval */
1256560424e9SKalle Valo 	if (!priv->beacon_int)
1257560424e9SKalle Valo 		priv->beacon_int = 1;
1258560424e9SKalle Valo 
1259560424e9SKalle Valo 	join.beacon_interval = priv->beacon_int;
1260560424e9SKalle Valo 
1261560424e9SKalle Valo 	/* BT Coex related changes */
1262560424e9SKalle Valo 	if (priv->bt_present) {
1263560424e9SKalle Valo 		if (((priv->conf_listen_interval * 100) %
1264560424e9SKalle Valo 		     priv->beacon_int) == 0)
1265560424e9SKalle Valo 			priv->listen_interval =
1266560424e9SKalle Valo 				((priv->conf_listen_interval * 100) /
1267560424e9SKalle Valo 				 priv->beacon_int);
1268560424e9SKalle Valo 		else
1269560424e9SKalle Valo 			priv->listen_interval =
1270560424e9SKalle Valo 				((priv->conf_listen_interval * 100) /
1271560424e9SKalle Valo 				 priv->beacon_int + 1);
1272560424e9SKalle Valo 	}
1273560424e9SKalle Valo 
1274560424e9SKalle Valo 	if (priv->hw->conf.ps_dtim_period)
1275560424e9SKalle Valo 		priv->join_dtim_period = priv->hw->conf.ps_dtim_period;
1276560424e9SKalle Valo 	join.dtim_period = priv->join_dtim_period;
1277560424e9SKalle Valo 
1278560424e9SKalle Valo 	join.channel_number = priv->channel->hw_value;
127957fbcce3SJohannes Berg 	join.band = (priv->channel->band == NL80211_BAND_5GHZ) ?
1280560424e9SKalle Valo 		WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
1281560424e9SKalle Valo 
1282560424e9SKalle Valo 	memcpy(join.bssid, bssid, sizeof(join.bssid));
1283560424e9SKalle Valo 
1284560424e9SKalle Valo 	pr_debug("[STA] Join BSSID: %pM DTIM: %d, interval: %d\n",
1285560424e9SKalle Valo 		 join.bssid,
1286560424e9SKalle Valo 		 join.dtim_period, priv->beacon_int);
1287560424e9SKalle Valo 
1288f276e20bSJohannes Berg 	if (!priv->vif->cfg.ibss_joined) {
1289560424e9SKalle Valo 		const u8 *ssidie;
1290560424e9SKalle Valo 		rcu_read_lock();
1291560424e9SKalle Valo 		ssidie = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1292560424e9SKalle Valo 		if (ssidie) {
1293560424e9SKalle Valo 			join.ssid_len = ssidie[1];
1294560424e9SKalle Valo 			memcpy(join.ssid, &ssidie[2], join.ssid_len);
1295560424e9SKalle Valo 		}
1296560424e9SKalle Valo 		rcu_read_unlock();
1297560424e9SKalle Valo 	}
1298560424e9SKalle Valo 
1299560424e9SKalle Valo 	if (priv->vif->p2p) {
1300560424e9SKalle Valo 		join.flags |= WSM_JOIN_FLAGS_P2P_GO;
1301560424e9SKalle Valo 		join.basic_rate_set =
1302560424e9SKalle Valo 			cw1200_rate_mask_to_wsm(priv, 0xFF0);
1303560424e9SKalle Valo 	}
1304560424e9SKalle Valo 
1305560424e9SKalle Valo 	/* Enable asynchronous join calls */
1306f276e20bSJohannes Berg 	if (!priv->vif->cfg.ibss_joined) {
1307560424e9SKalle Valo 		join.flags |= WSM_JOIN_FLAGS_FORCE;
1308560424e9SKalle Valo 		join.flags |= WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND;
1309560424e9SKalle Valo 	}
1310560424e9SKalle Valo 
1311560424e9SKalle Valo 	wsm_flush_tx(priv);
1312560424e9SKalle Valo 
1313560424e9SKalle Valo 	/* Stay Awake for Join and Auth Timeouts and a bit more */
1314560424e9SKalle Valo 	cw1200_pm_stay_awake(&priv->pm_state,
1315560424e9SKalle Valo 			     CW1200_JOIN_TIMEOUT + CW1200_AUTH_TIMEOUT);
1316560424e9SKalle Valo 
1317560424e9SKalle Valo 	cw1200_update_listening(priv, false);
1318560424e9SKalle Valo 
1319560424e9SKalle Valo 	/* Turn on Block ACKs */
1320560424e9SKalle Valo 	wsm_set_block_ack_policy(priv, priv->ba_tx_tid_mask,
1321560424e9SKalle Valo 				 priv->ba_rx_tid_mask);
1322560424e9SKalle Valo 
1323560424e9SKalle Valo 	/* Set up timeout */
1324560424e9SKalle Valo 	if (join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND) {
1325560424e9SKalle Valo 		priv->join_status = CW1200_JOIN_STATUS_JOINING;
1326560424e9SKalle Valo 		queue_delayed_work(priv->workqueue,
1327560424e9SKalle Valo 				   &priv->join_timeout,
1328560424e9SKalle Valo 				   CW1200_JOIN_TIMEOUT);
1329560424e9SKalle Valo 	}
1330560424e9SKalle Valo 
1331560424e9SKalle Valo 	/* 802.11w protected mgmt frames */
1332560424e9SKalle Valo 	mgmt_policy.protectedMgmtEnable = 0;
1333560424e9SKalle Valo 	mgmt_policy.unprotectedMgmtFramesAllowed = 1;
1334560424e9SKalle Valo 	mgmt_policy.encryptionForAuthFrame = 1;
1335560424e9SKalle Valo 	wsm_set_protected_mgmt_policy(priv, &mgmt_policy);
1336560424e9SKalle Valo 
1337560424e9SKalle Valo 	/* Perform actual join */
1338560424e9SKalle Valo 	if (wsm_join(priv, &join)) {
1339560424e9SKalle Valo 		pr_err("[STA] cw1200_join_work: wsm_join failed!\n");
1340560424e9SKalle Valo 		cancel_delayed_work_sync(&priv->join_timeout);
1341560424e9SKalle Valo 		cw1200_update_listening(priv, priv->listening);
1342560424e9SKalle Valo 		/* Tx lock still held, unjoin will clear it. */
1343560424e9SKalle Valo 		if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1344560424e9SKalle Valo 			wsm_unlock_tx(priv);
1345560424e9SKalle Valo 	} else {
1346560424e9SKalle Valo 		if (!(join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND))
1347560424e9SKalle Valo 			cw1200_join_complete(priv); /* Will clear tx_lock */
1348560424e9SKalle Valo 
1349560424e9SKalle Valo 		/* Upload keys */
1350560424e9SKalle Valo 		cw1200_upload_keys(priv);
1351560424e9SKalle Valo 
1352560424e9SKalle Valo 		/* Due to beacon filtering it is possible that the
1353560424e9SKalle Valo 		 * AP's beacon is not known for the mac80211 stack.
1354560424e9SKalle Valo 		 * Disable filtering temporary to make sure the stack
1355560424e9SKalle Valo 		 * receives at least one
1356560424e9SKalle Valo 		 */
1357560424e9SKalle Valo 		priv->disable_beacon_filter = true;
1358560424e9SKalle Valo 	}
1359560424e9SKalle Valo 	cw1200_update_filtering(priv);
1360560424e9SKalle Valo 
1361560424e9SKalle Valo done_put:
1362560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
1363560424e9SKalle Valo 	if (bss)
1364560424e9SKalle Valo 		cfg80211_put_bss(priv->hw->wiphy, bss);
1365560424e9SKalle Valo }
1366560424e9SKalle Valo 
cw1200_join_timeout(struct work_struct * work)1367560424e9SKalle Valo void cw1200_join_timeout(struct work_struct *work)
1368560424e9SKalle Valo {
1369560424e9SKalle Valo 	struct cw1200_common *priv =
1370560424e9SKalle Valo 		container_of(work, struct cw1200_common, join_timeout.work);
1371560424e9SKalle Valo 	pr_debug("[WSM] Join timed out.\n");
1372560424e9SKalle Valo 	wsm_lock_tx(priv);
1373560424e9SKalle Valo 	if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1374560424e9SKalle Valo 		wsm_unlock_tx(priv);
1375560424e9SKalle Valo }
1376560424e9SKalle Valo 
cw1200_do_unjoin(struct cw1200_common * priv)1377560424e9SKalle Valo static void cw1200_do_unjoin(struct cw1200_common *priv)
1378560424e9SKalle Valo {
1379560424e9SKalle Valo 	struct wsm_reset reset = {
1380560424e9SKalle Valo 		.reset_statistics = true,
1381560424e9SKalle Valo 	};
1382560424e9SKalle Valo 
1383560424e9SKalle Valo 	cancel_delayed_work_sync(&priv->join_timeout);
1384560424e9SKalle Valo 
1385560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
1386560424e9SKalle Valo 	priv->join_pending = false;
1387560424e9SKalle Valo 
1388560424e9SKalle Valo 	if (atomic_read(&priv->scan.in_progress)) {
1389560424e9SKalle Valo 		if (priv->delayed_unjoin)
1390560424e9SKalle Valo 			wiphy_dbg(priv->hw->wiphy, "Delayed unjoin is already scheduled.\n");
1391560424e9SKalle Valo 		else
1392560424e9SKalle Valo 			priv->delayed_unjoin = true;
1393560424e9SKalle Valo 		goto done;
1394560424e9SKalle Valo 	}
1395560424e9SKalle Valo 
1396560424e9SKalle Valo 	priv->delayed_link_loss = false;
1397560424e9SKalle Valo 
1398560424e9SKalle Valo 	if (!priv->join_status)
1399560424e9SKalle Valo 		goto done;
1400560424e9SKalle Valo 
1401560424e9SKalle Valo 	if (priv->join_status == CW1200_JOIN_STATUS_AP)
1402560424e9SKalle Valo 		goto done;
1403560424e9SKalle Valo 
1404560424e9SKalle Valo 	cancel_work_sync(&priv->update_filtering_work);
1405560424e9SKalle Valo 	cancel_work_sync(&priv->set_beacon_wakeup_period_work);
1406560424e9SKalle Valo 	priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1407560424e9SKalle Valo 
1408560424e9SKalle Valo 	/* Unjoin is a reset. */
1409560424e9SKalle Valo 	wsm_flush_tx(priv);
1410560424e9SKalle Valo 	wsm_keep_alive_period(priv, 0);
1411560424e9SKalle Valo 	wsm_reset(priv, &reset);
1412560424e9SKalle Valo 	wsm_set_output_power(priv, priv->output_power * 10);
1413560424e9SKalle Valo 	priv->join_dtim_period = 0;
1414560424e9SKalle Valo 	cw1200_setup_mac(priv);
1415560424e9SKalle Valo 	cw1200_free_event_queue(priv);
1416560424e9SKalle Valo 	cancel_work_sync(&priv->event_handler);
1417560424e9SKalle Valo 	cw1200_update_listening(priv, priv->listening);
1418560424e9SKalle Valo 	cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
1419560424e9SKalle Valo 
1420560424e9SKalle Valo 	/* Disable Block ACKs */
1421560424e9SKalle Valo 	wsm_set_block_ack_policy(priv, 0, 0);
1422560424e9SKalle Valo 
1423560424e9SKalle Valo 	priv->disable_beacon_filter = false;
1424560424e9SKalle Valo 	cw1200_update_filtering(priv);
1425560424e9SKalle Valo 	memset(&priv->association_mode, 0,
1426560424e9SKalle Valo 	       sizeof(priv->association_mode));
1427560424e9SKalle Valo 	memset(&priv->bss_params, 0, sizeof(priv->bss_params));
1428560424e9SKalle Valo 	priv->setbssparams_done = false;
1429560424e9SKalle Valo 	memset(&priv->firmware_ps_mode, 0,
1430560424e9SKalle Valo 	       sizeof(priv->firmware_ps_mode));
1431560424e9SKalle Valo 
1432560424e9SKalle Valo 	pr_debug("[STA] Unjoin completed.\n");
1433560424e9SKalle Valo 
1434560424e9SKalle Valo done:
1435560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
1436560424e9SKalle Valo }
1437560424e9SKalle Valo 
cw1200_unjoin_work(struct work_struct * work)1438560424e9SKalle Valo void cw1200_unjoin_work(struct work_struct *work)
1439560424e9SKalle Valo {
1440560424e9SKalle Valo 	struct cw1200_common *priv =
1441560424e9SKalle Valo 		container_of(work, struct cw1200_common, unjoin_work);
1442560424e9SKalle Valo 
1443560424e9SKalle Valo 	cw1200_do_unjoin(priv);
1444560424e9SKalle Valo 
1445560424e9SKalle Valo 	/* Tell the stack we're dead */
1446560424e9SKalle Valo 	ieee80211_connection_loss(priv->vif);
1447560424e9SKalle Valo 
1448560424e9SKalle Valo 	wsm_unlock_tx(priv);
1449560424e9SKalle Valo }
1450560424e9SKalle Valo 
cw1200_enable_listening(struct cw1200_common * priv)1451560424e9SKalle Valo int cw1200_enable_listening(struct cw1200_common *priv)
1452560424e9SKalle Valo {
1453560424e9SKalle Valo 	struct wsm_start start = {
1454560424e9SKalle Valo 		.mode = WSM_START_MODE_P2P_DEV,
1455560424e9SKalle Valo 		.band = WSM_PHY_BAND_2_4G,
1456560424e9SKalle Valo 		.beacon_interval = 100,
1457560424e9SKalle Valo 		.dtim_period = 1,
1458560424e9SKalle Valo 		.probe_delay = 0,
1459560424e9SKalle Valo 		.basic_rate_set = 0x0F,
1460560424e9SKalle Valo 	};
1461560424e9SKalle Valo 
1462560424e9SKalle Valo 	if (priv->channel) {
146357fbcce3SJohannes Berg 		start.band = priv->channel->band == NL80211_BAND_5GHZ ?
1464560424e9SKalle Valo 			     WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
1465560424e9SKalle Valo 		start.channel_number = priv->channel->hw_value;
1466560424e9SKalle Valo 	} else {
1467560424e9SKalle Valo 		start.band = WSM_PHY_BAND_2_4G;
1468560424e9SKalle Valo 		start.channel_number = 1;
1469560424e9SKalle Valo 	}
1470560424e9SKalle Valo 
1471560424e9SKalle Valo 	return wsm_start(priv, &start);
1472560424e9SKalle Valo }
1473560424e9SKalle Valo 
cw1200_disable_listening(struct cw1200_common * priv)1474560424e9SKalle Valo int cw1200_disable_listening(struct cw1200_common *priv)
1475560424e9SKalle Valo {
1476560424e9SKalle Valo 	int ret;
1477560424e9SKalle Valo 	struct wsm_reset reset = {
1478560424e9SKalle Valo 		.reset_statistics = true,
1479560424e9SKalle Valo 	};
1480560424e9SKalle Valo 	ret = wsm_reset(priv, &reset);
1481560424e9SKalle Valo 	return ret;
1482560424e9SKalle Valo }
1483560424e9SKalle Valo 
cw1200_update_listening(struct cw1200_common * priv,bool enabled)1484560424e9SKalle Valo void cw1200_update_listening(struct cw1200_common *priv, bool enabled)
1485560424e9SKalle Valo {
1486560424e9SKalle Valo 	if (enabled) {
1487560424e9SKalle Valo 		if (priv->join_status == CW1200_JOIN_STATUS_PASSIVE) {
1488560424e9SKalle Valo 			if (!cw1200_enable_listening(priv))
1489560424e9SKalle Valo 				priv->join_status = CW1200_JOIN_STATUS_MONITOR;
1490560424e9SKalle Valo 			wsm_set_probe_responder(priv, true);
1491560424e9SKalle Valo 		}
1492560424e9SKalle Valo 	} else {
1493560424e9SKalle Valo 		if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
1494560424e9SKalle Valo 			if (!cw1200_disable_listening(priv))
1495560424e9SKalle Valo 				priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1496560424e9SKalle Valo 			wsm_set_probe_responder(priv, false);
1497560424e9SKalle Valo 		}
1498560424e9SKalle Valo 	}
1499560424e9SKalle Valo }
1500560424e9SKalle Valo 
cw1200_set_uapsd_param(struct cw1200_common * priv,const struct wsm_edca_params * arg)1501560424e9SKalle Valo int cw1200_set_uapsd_param(struct cw1200_common *priv,
1502560424e9SKalle Valo 			   const struct wsm_edca_params *arg)
1503560424e9SKalle Valo {
1504560424e9SKalle Valo 	int ret;
1505560424e9SKalle Valo 	u16 uapsd_flags = 0;
1506560424e9SKalle Valo 
1507560424e9SKalle Valo 	/* Here's the mapping AC [queue, bit]
1508560424e9SKalle Valo 	 *  VO [0,3], VI [1, 2], BE [2, 1], BK [3, 0]
1509560424e9SKalle Valo 	 */
1510560424e9SKalle Valo 
1511560424e9SKalle Valo 	if (arg->uapsd_enable[0])
1512560424e9SKalle Valo 		uapsd_flags |= 1 << 3;
1513560424e9SKalle Valo 
1514560424e9SKalle Valo 	if (arg->uapsd_enable[1])
1515560424e9SKalle Valo 		uapsd_flags |= 1 << 2;
1516560424e9SKalle Valo 
1517560424e9SKalle Valo 	if (arg->uapsd_enable[2])
1518560424e9SKalle Valo 		uapsd_flags |= 1 << 1;
1519560424e9SKalle Valo 
1520560424e9SKalle Valo 	if (arg->uapsd_enable[3])
1521560424e9SKalle Valo 		uapsd_flags |= 1;
1522560424e9SKalle Valo 
1523560424e9SKalle Valo 	/* Currently pseudo U-APSD operation is not supported, so setting
1524560424e9SKalle Valo 	 * MinAutoTriggerInterval, MaxAutoTriggerInterval and
1525560424e9SKalle Valo 	 * AutoTriggerStep to 0
1526560424e9SKalle Valo 	 */
1527560424e9SKalle Valo 
1528560424e9SKalle Valo 	priv->uapsd_info.uapsd_flags = cpu_to_le16(uapsd_flags);
1529560424e9SKalle Valo 	priv->uapsd_info.min_auto_trigger_interval = 0;
1530560424e9SKalle Valo 	priv->uapsd_info.max_auto_trigger_interval = 0;
1531560424e9SKalle Valo 	priv->uapsd_info.auto_trigger_step = 0;
1532560424e9SKalle Valo 
1533560424e9SKalle Valo 	ret = wsm_set_uapsd_info(priv, &priv->uapsd_info);
1534560424e9SKalle Valo 	return ret;
1535560424e9SKalle Valo }
1536560424e9SKalle Valo 
1537560424e9SKalle Valo /* ******************************************************************** */
1538560424e9SKalle Valo /* AP API								*/
1539560424e9SKalle Valo 
cw1200_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1540560424e9SKalle Valo int cw1200_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1541560424e9SKalle Valo 		   struct ieee80211_sta *sta)
1542560424e9SKalle Valo {
1543560424e9SKalle Valo 	struct cw1200_common *priv = hw->priv;
1544560424e9SKalle Valo 	struct cw1200_sta_priv *sta_priv =
1545560424e9SKalle Valo 			(struct cw1200_sta_priv *)&sta->drv_priv;
1546560424e9SKalle Valo 	struct cw1200_link_entry *entry;
1547560424e9SKalle Valo 	struct sk_buff *skb;
1548560424e9SKalle Valo 
1549560424e9SKalle Valo 	if (priv->mode != NL80211_IFTYPE_AP)
1550560424e9SKalle Valo 		return 0;
1551560424e9SKalle Valo 
1552560424e9SKalle Valo 	sta_priv->link_id = cw1200_find_link_id(priv, sta->addr);
1553560424e9SKalle Valo 	if (WARN_ON(!sta_priv->link_id)) {
1554560424e9SKalle Valo 		wiphy_info(priv->hw->wiphy,
1555560424e9SKalle Valo 			   "[AP] No more link IDs available.\n");
1556560424e9SKalle Valo 		return -ENOENT;
1557560424e9SKalle Valo 	}
1558560424e9SKalle Valo 
1559560424e9SKalle Valo 	entry = &priv->link_id_db[sta_priv->link_id - 1];
1560560424e9SKalle Valo 	spin_lock_bh(&priv->ps_state_lock);
1561560424e9SKalle Valo 	if ((sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK) ==
1562560424e9SKalle Valo 					IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
1563560424e9SKalle Valo 		priv->sta_asleep_mask |= BIT(sta_priv->link_id);
1564560424e9SKalle Valo 	entry->status = CW1200_LINK_HARD;
1565560424e9SKalle Valo 	while ((skb = skb_dequeue(&entry->rx_queue)))
1566560424e9SKalle Valo 		ieee80211_rx_irqsafe(priv->hw, skb);
1567560424e9SKalle Valo 	spin_unlock_bh(&priv->ps_state_lock);
1568560424e9SKalle Valo 	return 0;
1569560424e9SKalle Valo }
1570560424e9SKalle Valo 
cw1200_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1571560424e9SKalle Valo int cw1200_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1572560424e9SKalle Valo 		      struct ieee80211_sta *sta)
1573560424e9SKalle Valo {
1574560424e9SKalle Valo 	struct cw1200_common *priv = hw->priv;
1575560424e9SKalle Valo 	struct cw1200_sta_priv *sta_priv =
1576560424e9SKalle Valo 			(struct cw1200_sta_priv *)&sta->drv_priv;
1577560424e9SKalle Valo 	struct cw1200_link_entry *entry;
1578560424e9SKalle Valo 
1579560424e9SKalle Valo 	if (priv->mode != NL80211_IFTYPE_AP || !sta_priv->link_id)
1580560424e9SKalle Valo 		return 0;
1581560424e9SKalle Valo 
1582560424e9SKalle Valo 	entry = &priv->link_id_db[sta_priv->link_id - 1];
1583560424e9SKalle Valo 	spin_lock_bh(&priv->ps_state_lock);
1584560424e9SKalle Valo 	entry->status = CW1200_LINK_RESERVE;
1585560424e9SKalle Valo 	entry->timestamp = jiffies;
1586560424e9SKalle Valo 	wsm_lock_tx_async(priv);
1587560424e9SKalle Valo 	if (queue_work(priv->workqueue, &priv->link_id_work) <= 0)
1588560424e9SKalle Valo 		wsm_unlock_tx(priv);
1589560424e9SKalle Valo 	spin_unlock_bh(&priv->ps_state_lock);
1590560424e9SKalle Valo 	flush_workqueue(priv->workqueue);
1591560424e9SKalle Valo 	return 0;
1592560424e9SKalle Valo }
1593560424e9SKalle Valo 
__cw1200_sta_notify(struct ieee80211_hw * dev,struct ieee80211_vif * vif,enum sta_notify_cmd notify_cmd,int link_id)1594560424e9SKalle Valo static void __cw1200_sta_notify(struct ieee80211_hw *dev,
1595560424e9SKalle Valo 				struct ieee80211_vif *vif,
1596560424e9SKalle Valo 				enum sta_notify_cmd notify_cmd,
1597560424e9SKalle Valo 				int link_id)
1598560424e9SKalle Valo {
1599560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
1600560424e9SKalle Valo 	u32 bit, prev;
1601560424e9SKalle Valo 
1602560424e9SKalle Valo 	/* Zero link id means "for all link IDs" */
1603560424e9SKalle Valo 	if (link_id)
1604560424e9SKalle Valo 		bit = BIT(link_id);
1605560424e9SKalle Valo 	else if (WARN_ON_ONCE(notify_cmd != STA_NOTIFY_AWAKE))
1606560424e9SKalle Valo 		bit = 0;
1607560424e9SKalle Valo 	else
1608560424e9SKalle Valo 		bit = priv->link_id_map;
1609560424e9SKalle Valo 	prev = priv->sta_asleep_mask & bit;
1610560424e9SKalle Valo 
1611560424e9SKalle Valo 	switch (notify_cmd) {
1612560424e9SKalle Valo 	case STA_NOTIFY_SLEEP:
1613560424e9SKalle Valo 		if (!prev) {
1614560424e9SKalle Valo 			if (priv->buffered_multicasts &&
1615560424e9SKalle Valo 			    !priv->sta_asleep_mask)
1616560424e9SKalle Valo 				queue_work(priv->workqueue,
1617560424e9SKalle Valo 					   &priv->multicast_start_work);
1618560424e9SKalle Valo 			priv->sta_asleep_mask |= bit;
1619560424e9SKalle Valo 		}
1620560424e9SKalle Valo 		break;
1621560424e9SKalle Valo 	case STA_NOTIFY_AWAKE:
1622560424e9SKalle Valo 		if (prev) {
1623560424e9SKalle Valo 			priv->sta_asleep_mask &= ~bit;
1624560424e9SKalle Valo 			priv->pspoll_mask &= ~bit;
1625560424e9SKalle Valo 			if (priv->tx_multicast && link_id &&
1626560424e9SKalle Valo 			    !priv->sta_asleep_mask)
1627560424e9SKalle Valo 				queue_work(priv->workqueue,
1628560424e9SKalle Valo 					   &priv->multicast_stop_work);
1629560424e9SKalle Valo 			cw1200_bh_wakeup(priv);
1630560424e9SKalle Valo 		}
1631560424e9SKalle Valo 		break;
1632560424e9SKalle Valo 	}
1633560424e9SKalle Valo }
1634560424e9SKalle Valo 
cw1200_sta_notify(struct ieee80211_hw * dev,struct ieee80211_vif * vif,enum sta_notify_cmd notify_cmd,struct ieee80211_sta * sta)1635560424e9SKalle Valo void cw1200_sta_notify(struct ieee80211_hw *dev,
1636560424e9SKalle Valo 		       struct ieee80211_vif *vif,
1637560424e9SKalle Valo 		       enum sta_notify_cmd notify_cmd,
1638560424e9SKalle Valo 		       struct ieee80211_sta *sta)
1639560424e9SKalle Valo {
1640560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
1641560424e9SKalle Valo 	struct cw1200_sta_priv *sta_priv =
1642560424e9SKalle Valo 		(struct cw1200_sta_priv *)&sta->drv_priv;
1643560424e9SKalle Valo 
1644560424e9SKalle Valo 	spin_lock_bh(&priv->ps_state_lock);
1645560424e9SKalle Valo 	__cw1200_sta_notify(dev, vif, notify_cmd, sta_priv->link_id);
1646560424e9SKalle Valo 	spin_unlock_bh(&priv->ps_state_lock);
1647560424e9SKalle Valo }
1648560424e9SKalle Valo 
cw1200_ps_notify(struct cw1200_common * priv,int link_id,bool ps)1649560424e9SKalle Valo static void cw1200_ps_notify(struct cw1200_common *priv,
1650560424e9SKalle Valo 		      int link_id, bool ps)
1651560424e9SKalle Valo {
1652560424e9SKalle Valo 	if (link_id > CW1200_MAX_STA_IN_AP_MODE)
1653560424e9SKalle Valo 		return;
1654560424e9SKalle Valo 
1655560424e9SKalle Valo 	pr_debug("%s for LinkId: %d. STAs asleep: %.8X\n",
1656560424e9SKalle Valo 		 ps ? "Stop" : "Start",
1657560424e9SKalle Valo 		 link_id, priv->sta_asleep_mask);
1658560424e9SKalle Valo 
1659560424e9SKalle Valo 	__cw1200_sta_notify(priv->hw, priv->vif,
1660560424e9SKalle Valo 			    ps ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE, link_id);
1661560424e9SKalle Valo }
1662560424e9SKalle Valo 
cw1200_set_tim_impl(struct cw1200_common * priv,bool aid0_bit_set)1663560424e9SKalle Valo static int cw1200_set_tim_impl(struct cw1200_common *priv, bool aid0_bit_set)
1664560424e9SKalle Valo {
1665560424e9SKalle Valo 	struct sk_buff *skb;
1666560424e9SKalle Valo 	struct wsm_update_ie update_ie = {
1667560424e9SKalle Valo 		.what = WSM_UPDATE_IE_BEACON,
1668560424e9SKalle Valo 		.count = 1,
1669560424e9SKalle Valo 	};
1670560424e9SKalle Valo 	u16 tim_offset, tim_length;
1671560424e9SKalle Valo 
1672560424e9SKalle Valo 	pr_debug("[AP] mcast: %s.\n", aid0_bit_set ? "ena" : "dis");
1673560424e9SKalle Valo 
1674560424e9SKalle Valo 	skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
16756e8912a5SShaul Triebitz 				       &tim_offset, &tim_length, 0);
1676560424e9SKalle Valo 	if (!skb) {
1677560424e9SKalle Valo 		if (!__cw1200_flush(priv, true))
1678560424e9SKalle Valo 			wsm_unlock_tx(priv);
1679560424e9SKalle Valo 		return -ENOENT;
1680560424e9SKalle Valo 	}
1681560424e9SKalle Valo 
1682560424e9SKalle Valo 	if (tim_offset && tim_length >= 6) {
1683560424e9SKalle Valo 		/* Ignore DTIM count from mac80211:
1684560424e9SKalle Valo 		 * firmware handles DTIM internally.
1685560424e9SKalle Valo 		 */
1686560424e9SKalle Valo 		skb->data[tim_offset + 2] = 0;
1687560424e9SKalle Valo 
1688560424e9SKalle Valo 		/* Set/reset aid0 bit */
1689560424e9SKalle Valo 		if (aid0_bit_set)
1690560424e9SKalle Valo 			skb->data[tim_offset + 4] |= 1;
1691560424e9SKalle Valo 		else
1692560424e9SKalle Valo 			skb->data[tim_offset + 4] &= ~1;
1693560424e9SKalle Valo 	}
1694560424e9SKalle Valo 
1695560424e9SKalle Valo 	update_ie.ies = &skb->data[tim_offset];
1696560424e9SKalle Valo 	update_ie.length = tim_length;
1697560424e9SKalle Valo 	wsm_update_ie(priv, &update_ie);
1698560424e9SKalle Valo 
1699560424e9SKalle Valo 	dev_kfree_skb(skb);
1700560424e9SKalle Valo 
1701560424e9SKalle Valo 	return 0;
1702560424e9SKalle Valo }
1703560424e9SKalle Valo 
cw1200_set_tim_work(struct work_struct * work)1704560424e9SKalle Valo void cw1200_set_tim_work(struct work_struct *work)
1705560424e9SKalle Valo {
1706560424e9SKalle Valo 	struct cw1200_common *priv =
1707560424e9SKalle Valo 		container_of(work, struct cw1200_common, set_tim_work);
1708560424e9SKalle Valo 	(void)cw1200_set_tim_impl(priv, priv->aid0_bit_set);
1709560424e9SKalle Valo }
1710560424e9SKalle Valo 
cw1200_set_tim(struct ieee80211_hw * dev,struct ieee80211_sta * sta,bool set)1711560424e9SKalle Valo int cw1200_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
1712560424e9SKalle Valo 		   bool set)
1713560424e9SKalle Valo {
1714560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
1715560424e9SKalle Valo 	queue_work(priv->workqueue, &priv->set_tim_work);
1716560424e9SKalle Valo 	return 0;
1717560424e9SKalle Valo }
1718560424e9SKalle Valo 
cw1200_set_cts_work(struct work_struct * work)1719560424e9SKalle Valo void cw1200_set_cts_work(struct work_struct *work)
1720560424e9SKalle Valo {
1721560424e9SKalle Valo 	struct cw1200_common *priv =
1722560424e9SKalle Valo 		container_of(work, struct cw1200_common, set_cts_work);
1723560424e9SKalle Valo 
1724560424e9SKalle Valo 	u8 erp_ie[3] = {WLAN_EID_ERP_INFO, 0x1, 0};
1725560424e9SKalle Valo 	struct wsm_update_ie update_ie = {
1726560424e9SKalle Valo 		.what = WSM_UPDATE_IE_BEACON,
1727560424e9SKalle Valo 		.count = 1,
1728560424e9SKalle Valo 		.ies = erp_ie,
1729560424e9SKalle Valo 		.length = 3,
1730560424e9SKalle Valo 	};
1731560424e9SKalle Valo 	u32 erp_info;
1732560424e9SKalle Valo 	__le32 use_cts_prot;
1733560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
1734560424e9SKalle Valo 	erp_info = priv->erp_info;
1735560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
1736560424e9SKalle Valo 	use_cts_prot =
1737560424e9SKalle Valo 		erp_info & WLAN_ERP_USE_PROTECTION ?
1738560424e9SKalle Valo 		__cpu_to_le32(1) : 0;
1739560424e9SKalle Valo 
1740560424e9SKalle Valo 	erp_ie[ERP_INFO_BYTE_OFFSET] = erp_info;
1741560424e9SKalle Valo 
1742560424e9SKalle Valo 	pr_debug("[STA] ERP information 0x%x\n", erp_info);
1743560424e9SKalle Valo 
1744560424e9SKalle Valo 	wsm_write_mib(priv, WSM_MIB_ID_NON_ERP_PROTECTION,
1745560424e9SKalle Valo 		      &use_cts_prot, sizeof(use_cts_prot));
1746560424e9SKalle Valo 	wsm_update_ie(priv, &update_ie);
1747560424e9SKalle Valo 
1748560424e9SKalle Valo 	return;
1749560424e9SKalle Valo }
1750560424e9SKalle Valo 
cw1200_set_btcoexinfo(struct cw1200_common * priv)1751560424e9SKalle Valo static int cw1200_set_btcoexinfo(struct cw1200_common *priv)
1752560424e9SKalle Valo {
1753560424e9SKalle Valo 	struct wsm_override_internal_txrate arg;
1754560424e9SKalle Valo 	int ret = 0;
1755560424e9SKalle Valo 
1756560424e9SKalle Valo 	if (priv->mode == NL80211_IFTYPE_STATION) {
1757560424e9SKalle Valo 		/* Plumb PSPOLL and NULL template */
1758560424e9SKalle Valo 		cw1200_upload_pspoll(priv);
1759560424e9SKalle Valo 		cw1200_upload_null(priv);
1760560424e9SKalle Valo 		cw1200_upload_qosnull(priv);
1761560424e9SKalle Valo 	} else {
1762560424e9SKalle Valo 		return 0;
1763560424e9SKalle Valo 	}
1764560424e9SKalle Valo 
1765560424e9SKalle Valo 	memset(&arg, 0, sizeof(struct wsm_override_internal_txrate));
1766560424e9SKalle Valo 
1767560424e9SKalle Valo 	if (!priv->vif->p2p) {
1768560424e9SKalle Valo 		/* STATION mode */
1769560424e9SKalle Valo 		if (priv->bss_params.operational_rate_set & ~0xF) {
1770560424e9SKalle Valo 			pr_debug("[STA] STA has ERP rates\n");
1771560424e9SKalle Valo 			/* G or BG mode */
1772560424e9SKalle Valo 			arg.internalTxRate = (__ffs(
1773560424e9SKalle Valo 			priv->bss_params.operational_rate_set & ~0xF));
1774560424e9SKalle Valo 		} else {
1775560424e9SKalle Valo 			pr_debug("[STA] STA has non ERP rates\n");
1776560424e9SKalle Valo 			/* B only mode */
1777560424e9SKalle Valo 			arg.internalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
1778560424e9SKalle Valo 		}
1779560424e9SKalle Valo 		arg.nonErpInternalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
1780560424e9SKalle Valo 	} else {
1781560424e9SKalle Valo 		/* P2P mode */
1782560424e9SKalle Valo 		arg.internalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
1783560424e9SKalle Valo 		arg.nonErpInternalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
1784560424e9SKalle Valo 	}
1785560424e9SKalle Valo 
1786560424e9SKalle Valo 	pr_debug("[STA] BTCOEX_INFO MODE %d, internalTxRate : %x, nonErpInternalTxRate: %x\n",
1787560424e9SKalle Valo 		 priv->mode,
1788560424e9SKalle Valo 		 arg.internalTxRate,
1789560424e9SKalle Valo 		 arg.nonErpInternalTxRate);
1790560424e9SKalle Valo 
1791560424e9SKalle Valo 	ret = wsm_write_mib(priv, WSM_MIB_ID_OVERRIDE_INTERNAL_TX_RATE,
1792560424e9SKalle Valo 			    &arg, sizeof(arg));
1793560424e9SKalle Valo 
1794560424e9SKalle Valo 	return ret;
1795560424e9SKalle Valo }
1796560424e9SKalle Valo 
cw1200_bss_info_changed(struct ieee80211_hw * dev,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)1797560424e9SKalle Valo void cw1200_bss_info_changed(struct ieee80211_hw *dev,
1798560424e9SKalle Valo 			     struct ieee80211_vif *vif,
1799560424e9SKalle Valo 			     struct ieee80211_bss_conf *info,
18007b7090b4SJohannes Berg 			     u64 changed)
1801560424e9SKalle Valo {
1802560424e9SKalle Valo 	struct cw1200_common *priv = dev->priv;
1803560424e9SKalle Valo 	bool do_join = false;
1804560424e9SKalle Valo 
1805560424e9SKalle Valo 	mutex_lock(&priv->conf_mutex);
1806560424e9SKalle Valo 
18077b7090b4SJohannes Berg 	pr_debug("BSS CHANGED:  %llx\n", changed);
1808560424e9SKalle Valo 
1809560424e9SKalle Valo 	/* TODO: BSS_CHANGED_QOS */
1810560424e9SKalle Valo 	/* TODO: BSS_CHANGED_TXPOWER */
1811560424e9SKalle Valo 
1812560424e9SKalle Valo 	if (changed & BSS_CHANGED_ARP_FILTER) {
1813560424e9SKalle Valo 		struct wsm_mib_arp_ipv4_filter filter = {0};
1814560424e9SKalle Valo 		int i;
1815560424e9SKalle Valo 
1816560424e9SKalle Valo 		pr_debug("[STA] BSS_CHANGED_ARP_FILTER cnt: %d\n",
1817f276e20bSJohannes Berg 			 vif->cfg.arp_addr_cnt);
1818560424e9SKalle Valo 
1819560424e9SKalle Valo 		/* Currently only one IP address is supported by firmware.
1820560424e9SKalle Valo 		 * In case of more IPs arp filtering will be disabled.
1821560424e9SKalle Valo 		 */
1822f276e20bSJohannes Berg 		if (vif->cfg.arp_addr_cnt > 0 &&
1823f276e20bSJohannes Berg 		    vif->cfg.arp_addr_cnt <= WSM_MAX_ARP_IP_ADDRTABLE_ENTRIES) {
1824f276e20bSJohannes Berg 			for (i = 0; i < vif->cfg.arp_addr_cnt; i++) {
1825f276e20bSJohannes Berg 				filter.ipv4addrs[i] = vif->cfg.arp_addr_list[i];
1826560424e9SKalle Valo 				pr_debug("[STA] addr[%d]: 0x%X\n",
1827560424e9SKalle Valo 					 i, filter.ipv4addrs[i]);
1828560424e9SKalle Valo 			}
1829560424e9SKalle Valo 			filter.enable = __cpu_to_le32(1);
1830560424e9SKalle Valo 		}
1831560424e9SKalle Valo 
1832560424e9SKalle Valo 		pr_debug("[STA] arp ip filter enable: %d\n",
1833560424e9SKalle Valo 			 __le32_to_cpu(filter.enable));
1834560424e9SKalle Valo 
1835560424e9SKalle Valo 		wsm_set_arp_ipv4_filter(priv, &filter);
1836560424e9SKalle Valo 	}
1837560424e9SKalle Valo 
1838560424e9SKalle Valo 	if (changed &
1839560424e9SKalle Valo 	    (BSS_CHANGED_BEACON |
1840560424e9SKalle Valo 	     BSS_CHANGED_AP_PROBE_RESP |
1841560424e9SKalle Valo 	     BSS_CHANGED_BSSID |
1842560424e9SKalle Valo 	     BSS_CHANGED_SSID |
1843560424e9SKalle Valo 	     BSS_CHANGED_IBSS)) {
1844560424e9SKalle Valo 		pr_debug("BSS_CHANGED_BEACON\n");
1845560424e9SKalle Valo 		priv->beacon_int = info->beacon_int;
1846560424e9SKalle Valo 		cw1200_update_beaconing(priv);
1847560424e9SKalle Valo 		cw1200_upload_beacon(priv);
1848560424e9SKalle Valo 	}
1849560424e9SKalle Valo 
1850560424e9SKalle Valo 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
1851560424e9SKalle Valo 		pr_debug("BSS_CHANGED_BEACON_ENABLED (%d)\n", info->enable_beacon);
1852560424e9SKalle Valo 
1853560424e9SKalle Valo 		if (priv->enable_beacon != info->enable_beacon) {
1854560424e9SKalle Valo 			cw1200_enable_beaconing(priv, info->enable_beacon);
1855560424e9SKalle Valo 			priv->enable_beacon = info->enable_beacon;
1856560424e9SKalle Valo 		}
1857560424e9SKalle Valo 	}
1858560424e9SKalle Valo 
1859560424e9SKalle Valo 	if (changed & BSS_CHANGED_BEACON_INT) {
1860560424e9SKalle Valo 		pr_debug("CHANGED_BEACON_INT\n");
1861f276e20bSJohannes Berg 		if (vif->cfg.ibss_joined)
1862560424e9SKalle Valo 			do_join = true;
1863560424e9SKalle Valo 		else if (priv->join_status == CW1200_JOIN_STATUS_AP)
1864560424e9SKalle Valo 			cw1200_update_beaconing(priv);
1865560424e9SKalle Valo 	}
1866560424e9SKalle Valo 
1867560424e9SKalle Valo 	/* assoc/disassoc, or maybe AID changed */
1868560424e9SKalle Valo 	if (changed & BSS_CHANGED_ASSOC) {
1869560424e9SKalle Valo 		wsm_lock_tx(priv);
1870560424e9SKalle Valo 		priv->wep_default_key_id = -1;
1871560424e9SKalle Valo 		wsm_unlock_tx(priv);
1872560424e9SKalle Valo 	}
1873560424e9SKalle Valo 
1874560424e9SKalle Valo 	if (changed & BSS_CHANGED_BSSID) {
1875560424e9SKalle Valo 		pr_debug("BSS_CHANGED_BSSID\n");
1876560424e9SKalle Valo 		do_join = true;
1877560424e9SKalle Valo 	}
1878560424e9SKalle Valo 
1879560424e9SKalle Valo 	if (changed &
1880560424e9SKalle Valo 	    (BSS_CHANGED_ASSOC |
1881560424e9SKalle Valo 	     BSS_CHANGED_BSSID |
1882560424e9SKalle Valo 	     BSS_CHANGED_IBSS |
1883560424e9SKalle Valo 	     BSS_CHANGED_BASIC_RATES |
1884560424e9SKalle Valo 	     BSS_CHANGED_HT)) {
1885560424e9SKalle Valo 		pr_debug("BSS_CHANGED_ASSOC\n");
1886f276e20bSJohannes Berg 		if (vif->cfg.assoc) {
1887560424e9SKalle Valo 			if (priv->join_status < CW1200_JOIN_STATUS_PRE_STA) {
1888560424e9SKalle Valo 				ieee80211_connection_loss(vif);
1889560424e9SKalle Valo 				mutex_unlock(&priv->conf_mutex);
1890560424e9SKalle Valo 				return;
1891560424e9SKalle Valo 			} else if (priv->join_status == CW1200_JOIN_STATUS_PRE_STA) {
1892560424e9SKalle Valo 				priv->join_status = CW1200_JOIN_STATUS_STA;
1893560424e9SKalle Valo 			}
1894560424e9SKalle Valo 		} else {
1895560424e9SKalle Valo 			do_join = true;
1896560424e9SKalle Valo 		}
1897560424e9SKalle Valo 
1898f276e20bSJohannes Berg 		if (vif->cfg.assoc || vif->cfg.ibss_joined) {
1899560424e9SKalle Valo 			struct ieee80211_sta *sta = NULL;
1900560424e9SKalle Valo 			__le32 htprot = 0;
1901560424e9SKalle Valo 
1902560424e9SKalle Valo 			if (info->dtim_period)
1903560424e9SKalle Valo 				priv->join_dtim_period = info->dtim_period;
1904560424e9SKalle Valo 			priv->beacon_int = info->beacon_int;
1905560424e9SKalle Valo 
1906560424e9SKalle Valo 			rcu_read_lock();
1907560424e9SKalle Valo 
1908f276e20bSJohannes Berg 			if (info->bssid && !vif->cfg.ibss_joined)
1909560424e9SKalle Valo 				sta = ieee80211_find_sta(vif, info->bssid);
1910560424e9SKalle Valo 			if (sta) {
1911046d2e7cSSriram R 				priv->ht_info.ht_cap = sta->deflink.ht_cap;
1912560424e9SKalle Valo 				priv->bss_params.operational_rate_set =
1913560424e9SKalle Valo 					cw1200_rate_mask_to_wsm(priv,
1914046d2e7cSSriram R 						sta->deflink.supp_rates[priv->channel->band]);
1915560424e9SKalle Valo 				priv->ht_info.channel_type = cfg80211_get_chandef_type(&dev->conf.chandef);
1916560424e9SKalle Valo 				priv->ht_info.operation_mode = info->ht_operation_mode;
1917560424e9SKalle Valo 			} else {
1918560424e9SKalle Valo 				memset(&priv->ht_info, 0,
1919560424e9SKalle Valo 				       sizeof(priv->ht_info));
1920560424e9SKalle Valo 				priv->bss_params.operational_rate_set = -1;
1921560424e9SKalle Valo 			}
1922560424e9SKalle Valo 			rcu_read_unlock();
1923560424e9SKalle Valo 
1924560424e9SKalle Valo 			/* Non Greenfield stations present */
1925560424e9SKalle Valo 			if (priv->ht_info.operation_mode &
1926560424e9SKalle Valo 			    IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT)
1927560424e9SKalle Valo 				htprot |= cpu_to_le32(WSM_NON_GREENFIELD_STA_PRESENT);
1928560424e9SKalle Valo 
1929560424e9SKalle Valo 			/* Set HT protection method */
1930560424e9SKalle Valo 			htprot |= cpu_to_le32((priv->ht_info.operation_mode & IEEE80211_HT_OP_MODE_PROTECTION) << 2);
1931560424e9SKalle Valo 
1932560424e9SKalle Valo 			/* TODO:
1933560424e9SKalle Valo 			 * STBC_param.dual_cts
1934560424e9SKalle Valo 			 *  STBC_param.LSIG_TXOP_FILL
1935560424e9SKalle Valo 			 */
1936560424e9SKalle Valo 
1937560424e9SKalle Valo 			wsm_write_mib(priv, WSM_MIB_ID_SET_HT_PROTECTION,
1938560424e9SKalle Valo 				      &htprot, sizeof(htprot));
1939560424e9SKalle Valo 
1940560424e9SKalle Valo 			priv->association_mode.greenfield =
1941560424e9SKalle Valo 				cw1200_ht_greenfield(&priv->ht_info);
1942560424e9SKalle Valo 			priv->association_mode.flags =
1943560424e9SKalle Valo 				WSM_ASSOCIATION_MODE_SNOOP_ASSOC_FRAMES |
1944560424e9SKalle Valo 				WSM_ASSOCIATION_MODE_USE_PREAMBLE_TYPE |
1945560424e9SKalle Valo 				WSM_ASSOCIATION_MODE_USE_HT_MODE |
1946560424e9SKalle Valo 				WSM_ASSOCIATION_MODE_USE_BASIC_RATE_SET |
1947560424e9SKalle Valo 				WSM_ASSOCIATION_MODE_USE_MPDU_START_SPACING;
1948560424e9SKalle Valo 			priv->association_mode.preamble =
1949560424e9SKalle Valo 				info->use_short_preamble ?
1950560424e9SKalle Valo 				WSM_JOIN_PREAMBLE_SHORT :
1951560424e9SKalle Valo 				WSM_JOIN_PREAMBLE_LONG;
1952560424e9SKalle Valo 			priv->association_mode.basic_rate_set = __cpu_to_le32(
1953560424e9SKalle Valo 				cw1200_rate_mask_to_wsm(priv,
1954560424e9SKalle Valo 							info->basic_rates));
1955560424e9SKalle Valo 			priv->association_mode.mpdu_start_spacing =
1956560424e9SKalle Valo 				cw1200_ht_ampdu_density(&priv->ht_info);
1957560424e9SKalle Valo 
1958560424e9SKalle Valo 			cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
1959560424e9SKalle Valo 			cancel_work_sync(&priv->unjoin_work);
1960560424e9SKalle Valo 
1961560424e9SKalle Valo 			priv->bss_params.beacon_lost_count = priv->cqm_beacon_loss_count;
1962f276e20bSJohannes Berg 			priv->bss_params.aid = vif->cfg.aid;
1963560424e9SKalle Valo 
1964560424e9SKalle Valo 			if (priv->join_dtim_period < 1)
1965560424e9SKalle Valo 				priv->join_dtim_period = 1;
1966560424e9SKalle Valo 
1967560424e9SKalle Valo 			pr_debug("[STA] DTIM %d, interval: %d\n",
1968560424e9SKalle Valo 				 priv->join_dtim_period, priv->beacon_int);
1969560424e9SKalle Valo 			pr_debug("[STA] Preamble: %d, Greenfield: %d, Aid: %d, Rates: 0x%.8X, Basic: 0x%.8X\n",
1970560424e9SKalle Valo 				 priv->association_mode.preamble,
1971560424e9SKalle Valo 				 priv->association_mode.greenfield,
1972560424e9SKalle Valo 				 priv->bss_params.aid,
1973560424e9SKalle Valo 				 priv->bss_params.operational_rate_set,
1974560424e9SKalle Valo 				 priv->association_mode.basic_rate_set);
1975560424e9SKalle Valo 			wsm_set_association_mode(priv, &priv->association_mode);
1976560424e9SKalle Valo 
1977f276e20bSJohannes Berg 			if (!vif->cfg.ibss_joined) {
1978560424e9SKalle Valo 				wsm_keep_alive_period(priv, 30 /* sec */);
1979560424e9SKalle Valo 				wsm_set_bss_params(priv, &priv->bss_params);
1980560424e9SKalle Valo 				priv->setbssparams_done = true;
1981560424e9SKalle Valo 				cw1200_set_beacon_wakeup_period_work(&priv->set_beacon_wakeup_period_work);
1982560424e9SKalle Valo 				cw1200_set_pm(priv, &priv->powersave_mode);
1983560424e9SKalle Valo 			}
1984560424e9SKalle Valo 			if (priv->vif->p2p) {
1985560424e9SKalle Valo 				pr_debug("[STA] Setting p2p powersave configuration.\n");
1986560424e9SKalle Valo 				wsm_set_p2p_ps_modeinfo(priv,
1987560424e9SKalle Valo 							&priv->p2p_ps_modeinfo);
1988560424e9SKalle Valo 			}
1989560424e9SKalle Valo 			if (priv->bt_present)
1990560424e9SKalle Valo 				cw1200_set_btcoexinfo(priv);
1991560424e9SKalle Valo 		} else {
1992560424e9SKalle Valo 			memset(&priv->association_mode, 0,
1993560424e9SKalle Valo 			       sizeof(priv->association_mode));
1994560424e9SKalle Valo 			memset(&priv->bss_params, 0, sizeof(priv->bss_params));
1995560424e9SKalle Valo 		}
1996560424e9SKalle Valo 	}
1997560424e9SKalle Valo 
1998560424e9SKalle Valo 	/* ERP Protection */
1999560424e9SKalle Valo 	if (changed & (BSS_CHANGED_ASSOC |
2000560424e9SKalle Valo 		       BSS_CHANGED_ERP_CTS_PROT |
2001560424e9SKalle Valo 		       BSS_CHANGED_ERP_PREAMBLE)) {
2002560424e9SKalle Valo 		u32 prev_erp_info = priv->erp_info;
2003560424e9SKalle Valo 		if (info->use_cts_prot)
2004560424e9SKalle Valo 			priv->erp_info |= WLAN_ERP_USE_PROTECTION;
2005560424e9SKalle Valo 		else if (!(prev_erp_info & WLAN_ERP_NON_ERP_PRESENT))
2006560424e9SKalle Valo 			priv->erp_info &= ~WLAN_ERP_USE_PROTECTION;
2007560424e9SKalle Valo 
2008560424e9SKalle Valo 		if (info->use_short_preamble)
2009560424e9SKalle Valo 			priv->erp_info |= WLAN_ERP_BARKER_PREAMBLE;
2010560424e9SKalle Valo 		else
2011560424e9SKalle Valo 			priv->erp_info &= ~WLAN_ERP_BARKER_PREAMBLE;
2012560424e9SKalle Valo 
2013560424e9SKalle Valo 		pr_debug("[STA] ERP Protection: %x\n", priv->erp_info);
2014560424e9SKalle Valo 
2015560424e9SKalle Valo 		if (prev_erp_info != priv->erp_info)
2016560424e9SKalle Valo 			queue_work(priv->workqueue, &priv->set_cts_work);
2017560424e9SKalle Valo 	}
2018560424e9SKalle Valo 
2019560424e9SKalle Valo 	/* ERP Slottime */
2020560424e9SKalle Valo 	if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_SLOT)) {
2021560424e9SKalle Valo 		__le32 slot_time = info->use_short_slot ?
2022560424e9SKalle Valo 			__cpu_to_le32(9) : __cpu_to_le32(20);
2023560424e9SKalle Valo 		pr_debug("[STA] Slot time: %d us.\n",
2024560424e9SKalle Valo 			 __le32_to_cpu(slot_time));
2025560424e9SKalle Valo 		wsm_write_mib(priv, WSM_MIB_ID_DOT11_SLOT_TIME,
2026560424e9SKalle Valo 			      &slot_time, sizeof(slot_time));
2027560424e9SKalle Valo 	}
2028560424e9SKalle Valo 
2029560424e9SKalle Valo 	if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_CQM)) {
2030560424e9SKalle Valo 		struct wsm_rcpi_rssi_threshold threshold = {
2031560424e9SKalle Valo 			.rollingAverageCount = 8,
2032560424e9SKalle Valo 		};
2033560424e9SKalle Valo 		pr_debug("[CQM] RSSI threshold subscribe: %d +- %d\n",
2034560424e9SKalle Valo 			 info->cqm_rssi_thold, info->cqm_rssi_hyst);
2035560424e9SKalle Valo 		priv->cqm_rssi_thold = info->cqm_rssi_thold;
2036560424e9SKalle Valo 		priv->cqm_rssi_hyst = info->cqm_rssi_hyst;
2037560424e9SKalle Valo 
2038560424e9SKalle Valo 		if (info->cqm_rssi_thold || info->cqm_rssi_hyst) {
2039560424e9SKalle Valo 			/* RSSI subscription enabled */
2040560424e9SKalle Valo 			/* TODO: It's not a correct way of setting threshold.
2041560424e9SKalle Valo 			 * Upper and lower must be set equal here and adjusted
2042560424e9SKalle Valo 			 * in callback. However current implementation is much
2043560424e9SKalle Valo 			 * more relaible and stable.
2044560424e9SKalle Valo 			 */
2045560424e9SKalle Valo 
2046560424e9SKalle Valo 			/* RSSI: signed Q8.0, RCPI: unsigned Q7.1
2047560424e9SKalle Valo 			 * RSSI = RCPI / 2 - 110
2048560424e9SKalle Valo 			 */
2049560424e9SKalle Valo 			if (priv->cqm_use_rssi) {
2050560424e9SKalle Valo 				threshold.upperThreshold =
2051560424e9SKalle Valo 					info->cqm_rssi_thold + info->cqm_rssi_hyst;
2052560424e9SKalle Valo 				threshold.lowerThreshold =
2053560424e9SKalle Valo 					info->cqm_rssi_thold;
2054560424e9SKalle Valo 				threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
2055560424e9SKalle Valo 			} else {
2056560424e9SKalle Valo 				threshold.upperThreshold = (info->cqm_rssi_thold + info->cqm_rssi_hyst + 110) * 2;
2057560424e9SKalle Valo 				threshold.lowerThreshold = (info->cqm_rssi_thold + 110) * 2;
2058560424e9SKalle Valo 			}
2059560424e9SKalle Valo 			threshold.rssiRcpiMode |= WSM_RCPI_RSSI_THRESHOLD_ENABLE;
2060560424e9SKalle Valo 		} else {
2061560424e9SKalle Valo 			/* There is a bug in FW, see sta.c. We have to enable
2062560424e9SKalle Valo 			 * dummy subscription to get correct RSSI values.
2063560424e9SKalle Valo 			 */
2064560424e9SKalle Valo 			threshold.rssiRcpiMode |=
2065560424e9SKalle Valo 				WSM_RCPI_RSSI_THRESHOLD_ENABLE |
2066560424e9SKalle Valo 				WSM_RCPI_RSSI_DONT_USE_UPPER |
2067560424e9SKalle Valo 				WSM_RCPI_RSSI_DONT_USE_LOWER;
2068560424e9SKalle Valo 			if (priv->cqm_use_rssi)
2069560424e9SKalle Valo 				threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
2070560424e9SKalle Valo 		}
2071560424e9SKalle Valo 		wsm_set_rcpi_rssi_threshold(priv, &threshold);
2072560424e9SKalle Valo 	}
2073560424e9SKalle Valo 	mutex_unlock(&priv->conf_mutex);
2074560424e9SKalle Valo 
2075560424e9SKalle Valo 	if (do_join) {
2076560424e9SKalle Valo 		wsm_lock_tx(priv);
2077560424e9SKalle Valo 		cw1200_do_join(priv); /* Will unlock it for us */
2078560424e9SKalle Valo 	}
2079560424e9SKalle Valo }
2080560424e9SKalle Valo 
cw1200_multicast_start_work(struct work_struct * work)2081560424e9SKalle Valo void cw1200_multicast_start_work(struct work_struct *work)
2082560424e9SKalle Valo {
2083560424e9SKalle Valo 	struct cw1200_common *priv =
2084560424e9SKalle Valo 		container_of(work, struct cw1200_common, multicast_start_work);
2085560424e9SKalle Valo 	long tmo = priv->join_dtim_period *
2086560424e9SKalle Valo 			(priv->beacon_int + 20) * HZ / 1024;
2087560424e9SKalle Valo 
2088560424e9SKalle Valo 	cancel_work_sync(&priv->multicast_stop_work);
2089560424e9SKalle Valo 
2090560424e9SKalle Valo 	if (!priv->aid0_bit_set) {
2091560424e9SKalle Valo 		wsm_lock_tx(priv);
2092560424e9SKalle Valo 		cw1200_set_tim_impl(priv, true);
2093560424e9SKalle Valo 		priv->aid0_bit_set = true;
2094560424e9SKalle Valo 		mod_timer(&priv->mcast_timeout, jiffies + tmo);
2095560424e9SKalle Valo 		wsm_unlock_tx(priv);
2096560424e9SKalle Valo 	}
2097560424e9SKalle Valo }
2098560424e9SKalle Valo 
cw1200_multicast_stop_work(struct work_struct * work)2099560424e9SKalle Valo void cw1200_multicast_stop_work(struct work_struct *work)
2100560424e9SKalle Valo {
2101560424e9SKalle Valo 	struct cw1200_common *priv =
2102560424e9SKalle Valo 		container_of(work, struct cw1200_common, multicast_stop_work);
2103560424e9SKalle Valo 
2104560424e9SKalle Valo 	if (priv->aid0_bit_set) {
2105560424e9SKalle Valo 		del_timer_sync(&priv->mcast_timeout);
2106560424e9SKalle Valo 		wsm_lock_tx(priv);
2107560424e9SKalle Valo 		priv->aid0_bit_set = false;
2108560424e9SKalle Valo 		cw1200_set_tim_impl(priv, false);
2109560424e9SKalle Valo 		wsm_unlock_tx(priv);
2110560424e9SKalle Valo 	}
2111560424e9SKalle Valo }
2112560424e9SKalle Valo 
cw1200_mcast_timeout(struct timer_list * t)2113e3dcf8bbSKees Cook void cw1200_mcast_timeout(struct timer_list *t)
2114560424e9SKalle Valo {
2115e3dcf8bbSKees Cook 	struct cw1200_common *priv = from_timer(priv, t, mcast_timeout);
2116560424e9SKalle Valo 
2117560424e9SKalle Valo 	wiphy_warn(priv->hw->wiphy,
2118560424e9SKalle Valo 		   "Multicast delivery timeout.\n");
2119560424e9SKalle Valo 	spin_lock_bh(&priv->ps_state_lock);
2120560424e9SKalle Valo 	priv->tx_multicast = priv->aid0_bit_set &&
2121560424e9SKalle Valo 			priv->buffered_multicasts;
2122560424e9SKalle Valo 	if (priv->tx_multicast)
2123560424e9SKalle Valo 		cw1200_bh_wakeup(priv);
2124560424e9SKalle Valo 	spin_unlock_bh(&priv->ps_state_lock);
2125560424e9SKalle Valo }
2126560424e9SKalle Valo 
cw1200_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)2127560424e9SKalle Valo int cw1200_ampdu_action(struct ieee80211_hw *hw,
2128560424e9SKalle Valo 			struct ieee80211_vif *vif,
212950ea05efSSara Sharon 			struct ieee80211_ampdu_params *params)
2130560424e9SKalle Valo {
2131560424e9SKalle Valo 	/* Aggregation is implemented fully in firmware,
2132560424e9SKalle Valo 	 * including block ack negotiation. Do not allow
2133560424e9SKalle Valo 	 * mac80211 stack to do anything: it interferes with
2134560424e9SKalle Valo 	 * the firmware.
2135560424e9SKalle Valo 	 */
2136560424e9SKalle Valo 
2137560424e9SKalle Valo 	/* Note that we still need this function stubbed. */
2138560424e9SKalle Valo 	return -ENOTSUPP;
2139560424e9SKalle Valo }
2140560424e9SKalle Valo 
2141560424e9SKalle Valo /* ******************************************************************** */
2142560424e9SKalle Valo /* WSM callback								*/
cw1200_suspend_resume(struct cw1200_common * priv,struct wsm_suspend_resume * arg)2143560424e9SKalle Valo void cw1200_suspend_resume(struct cw1200_common *priv,
2144560424e9SKalle Valo 			  struct wsm_suspend_resume *arg)
2145560424e9SKalle Valo {
2146560424e9SKalle Valo 	pr_debug("[AP] %s: %s\n",
2147560424e9SKalle Valo 		 arg->stop ? "stop" : "start",
2148560424e9SKalle Valo 		 arg->multicast ? "broadcast" : "unicast");
2149560424e9SKalle Valo 
2150560424e9SKalle Valo 	if (arg->multicast) {
2151560424e9SKalle Valo 		bool cancel_tmo = false;
2152560424e9SKalle Valo 		spin_lock_bh(&priv->ps_state_lock);
2153560424e9SKalle Valo 		if (arg->stop) {
2154560424e9SKalle Valo 			priv->tx_multicast = false;
2155560424e9SKalle Valo 		} else {
2156560424e9SKalle Valo 			/* Firmware sends this indication every DTIM if there
2157560424e9SKalle Valo 			 * is a STA in powersave connected. There is no reason
2158560424e9SKalle Valo 			 * to suspend, following wakeup will consume much more
2159560424e9SKalle Valo 			 * power than it could be saved.
2160560424e9SKalle Valo 			 */
2161560424e9SKalle Valo 			cw1200_pm_stay_awake(&priv->pm_state,
2162560424e9SKalle Valo 					     priv->join_dtim_period *
2163560424e9SKalle Valo 					     (priv->beacon_int + 20) * HZ / 1024);
2164560424e9SKalle Valo 			priv->tx_multicast = (priv->aid0_bit_set &&
2165560424e9SKalle Valo 					      priv->buffered_multicasts);
2166560424e9SKalle Valo 			if (priv->tx_multicast) {
2167560424e9SKalle Valo 				cancel_tmo = true;
2168560424e9SKalle Valo 				cw1200_bh_wakeup(priv);
2169560424e9SKalle Valo 			}
2170560424e9SKalle Valo 		}
2171560424e9SKalle Valo 		spin_unlock_bh(&priv->ps_state_lock);
2172560424e9SKalle Valo 		if (cancel_tmo)
2173560424e9SKalle Valo 			del_timer_sync(&priv->mcast_timeout);
2174560424e9SKalle Valo 	} else {
2175560424e9SKalle Valo 		spin_lock_bh(&priv->ps_state_lock);
2176560424e9SKalle Valo 		cw1200_ps_notify(priv, arg->link_id, arg->stop);
2177560424e9SKalle Valo 		spin_unlock_bh(&priv->ps_state_lock);
2178560424e9SKalle Valo 		if (!arg->stop)
2179560424e9SKalle Valo 			cw1200_bh_wakeup(priv);
2180560424e9SKalle Valo 	}
2181560424e9SKalle Valo 	return;
2182560424e9SKalle Valo }
2183560424e9SKalle Valo 
2184560424e9SKalle Valo /* ******************************************************************** */
2185560424e9SKalle Valo /* AP privates								*/
2186560424e9SKalle Valo 
cw1200_upload_beacon(struct cw1200_common * priv)2187560424e9SKalle Valo static int cw1200_upload_beacon(struct cw1200_common *priv)
2188560424e9SKalle Valo {
2189560424e9SKalle Valo 	int ret = 0;
2190560424e9SKalle Valo 	struct ieee80211_mgmt *mgmt;
2191560424e9SKalle Valo 	struct wsm_template_frame frame = {
2192560424e9SKalle Valo 		.frame_type = WSM_FRAME_TYPE_BEACON,
2193560424e9SKalle Valo 	};
2194560424e9SKalle Valo 
2195560424e9SKalle Valo 	u16 tim_offset;
2196560424e9SKalle Valo 	u16 tim_len;
2197560424e9SKalle Valo 
2198560424e9SKalle Valo 	if (priv->mode == NL80211_IFTYPE_STATION ||
2199560424e9SKalle Valo 	    priv->mode == NL80211_IFTYPE_MONITOR ||
2200560424e9SKalle Valo 	    priv->mode == NL80211_IFTYPE_UNSPECIFIED)
2201560424e9SKalle Valo 		goto done;
2202560424e9SKalle Valo 
2203560424e9SKalle Valo 	if (priv->vif->p2p)
2204560424e9SKalle Valo 		frame.rate = WSM_TRANSMIT_RATE_6;
2205560424e9SKalle Valo 
2206560424e9SKalle Valo 	frame.skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
22076e8912a5SShaul Triebitz 					     &tim_offset, &tim_len, 0);
2208560424e9SKalle Valo 	if (!frame.skb)
2209560424e9SKalle Valo 		return -ENOMEM;
2210560424e9SKalle Valo 
2211560424e9SKalle Valo 	ret = wsm_set_template_frame(priv, &frame);
2212560424e9SKalle Valo 
2213560424e9SKalle Valo 	if (ret)
2214560424e9SKalle Valo 		goto done;
2215560424e9SKalle Valo 
2216560424e9SKalle Valo 	/* TODO: Distill probe resp; remove TIM
2217560424e9SKalle Valo 	 * and any other beacon-specific IEs
2218560424e9SKalle Valo 	 */
2219560424e9SKalle Valo 	mgmt = (void *)frame.skb->data;
2220560424e9SKalle Valo 	mgmt->frame_control =
2221560424e9SKalle Valo 		__cpu_to_le16(IEEE80211_FTYPE_MGMT |
2222560424e9SKalle Valo 			      IEEE80211_STYPE_PROBE_RESP);
2223560424e9SKalle Valo 
2224560424e9SKalle Valo 	frame.frame_type = WSM_FRAME_TYPE_PROBE_RESPONSE;
2225560424e9SKalle Valo 	if (priv->vif->p2p) {
2226560424e9SKalle Valo 		ret = wsm_set_probe_responder(priv, true);
2227560424e9SKalle Valo 	} else {
2228560424e9SKalle Valo 		ret = wsm_set_template_frame(priv, &frame);
2229560424e9SKalle Valo 		wsm_set_probe_responder(priv, false);
2230560424e9SKalle Valo 	}
2231560424e9SKalle Valo 
2232560424e9SKalle Valo done:
2233560424e9SKalle Valo 	dev_kfree_skb(frame.skb);
2234560424e9SKalle Valo 
2235560424e9SKalle Valo 	return ret;
2236560424e9SKalle Valo }
2237560424e9SKalle Valo 
cw1200_upload_pspoll(struct cw1200_common * priv)2238560424e9SKalle Valo static int cw1200_upload_pspoll(struct cw1200_common *priv)
2239560424e9SKalle Valo {
2240560424e9SKalle Valo 	int ret = 0;
2241560424e9SKalle Valo 	struct wsm_template_frame frame = {
2242560424e9SKalle Valo 		.frame_type = WSM_FRAME_TYPE_PS_POLL,
2243560424e9SKalle Valo 		.rate = 0xFF,
2244560424e9SKalle Valo 	};
2245560424e9SKalle Valo 
2246560424e9SKalle Valo 
2247560424e9SKalle Valo 	frame.skb = ieee80211_pspoll_get(priv->hw, priv->vif);
2248560424e9SKalle Valo 	if (!frame.skb)
2249560424e9SKalle Valo 		return -ENOMEM;
2250560424e9SKalle Valo 
2251560424e9SKalle Valo 	ret = wsm_set_template_frame(priv, &frame);
2252560424e9SKalle Valo 
2253560424e9SKalle Valo 	dev_kfree_skb(frame.skb);
2254560424e9SKalle Valo 
2255560424e9SKalle Valo 	return ret;
2256560424e9SKalle Valo }
2257560424e9SKalle Valo 
cw1200_upload_null(struct cw1200_common * priv)2258560424e9SKalle Valo static int cw1200_upload_null(struct cw1200_common *priv)
2259560424e9SKalle Valo {
2260560424e9SKalle Valo 	int ret = 0;
2261560424e9SKalle Valo 	struct wsm_template_frame frame = {
2262560424e9SKalle Valo 		.frame_type = WSM_FRAME_TYPE_NULL,
2263560424e9SKalle Valo 		.rate = 0xFF,
2264560424e9SKalle Valo 	};
2265560424e9SKalle Valo 
22660ab26380SJohannes Berg 	frame.skb = ieee80211_nullfunc_get(priv->hw, priv->vif,-1, false);
2267560424e9SKalle Valo 	if (!frame.skb)
2268560424e9SKalle Valo 		return -ENOMEM;
2269560424e9SKalle Valo 
2270560424e9SKalle Valo 	ret = wsm_set_template_frame(priv, &frame);
2271560424e9SKalle Valo 
2272560424e9SKalle Valo 	dev_kfree_skb(frame.skb);
2273560424e9SKalle Valo 
2274560424e9SKalle Valo 	return ret;
2275560424e9SKalle Valo }
2276560424e9SKalle Valo 
cw1200_upload_qosnull(struct cw1200_common * priv)2277560424e9SKalle Valo static int cw1200_upload_qosnull(struct cw1200_common *priv)
2278560424e9SKalle Valo {
2279560424e9SKalle Valo 	/* TODO:  This needs to be implemented
2280560424e9SKalle Valo 
2281560424e9SKalle Valo 	struct wsm_template_frame frame = {
2282560424e9SKalle Valo 		.frame_type = WSM_FRAME_TYPE_QOS_NULL,
2283560424e9SKalle Valo 		.rate = 0xFF,
2284560424e9SKalle Valo 	};
2285560424e9SKalle Valo 
2286560424e9SKalle Valo 	frame.skb = ieee80211_qosnullfunc_get(priv->hw, priv->vif);
2287560424e9SKalle Valo 	if (!frame.skb)
2288560424e9SKalle Valo 		return -ENOMEM;
2289560424e9SKalle Valo 
2290560424e9SKalle Valo 	ret = wsm_set_template_frame(priv, &frame);
2291560424e9SKalle Valo 
2292560424e9SKalle Valo 	dev_kfree_skb(frame.skb);
2293560424e9SKalle Valo 
2294560424e9SKalle Valo 	*/
2295560424e9SKalle Valo 	return 0;
2296560424e9SKalle Valo }
2297560424e9SKalle Valo 
cw1200_enable_beaconing(struct cw1200_common * priv,bool enable)2298560424e9SKalle Valo static int cw1200_enable_beaconing(struct cw1200_common *priv,
2299560424e9SKalle Valo 				   bool enable)
2300560424e9SKalle Valo {
2301560424e9SKalle Valo 	struct wsm_beacon_transmit transmit = {
2302560424e9SKalle Valo 		.enable_beaconing = enable,
2303560424e9SKalle Valo 	};
2304560424e9SKalle Valo 
2305560424e9SKalle Valo 	return wsm_beacon_transmit(priv, &transmit);
2306560424e9SKalle Valo }
2307560424e9SKalle Valo 
cw1200_start_ap(struct cw1200_common * priv)2308560424e9SKalle Valo static int cw1200_start_ap(struct cw1200_common *priv)
2309560424e9SKalle Valo {
2310560424e9SKalle Valo 	int ret;
2311560424e9SKalle Valo 	struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
2312560424e9SKalle Valo 	struct wsm_start start = {
2313560424e9SKalle Valo 		.mode = priv->vif->p2p ?
2314560424e9SKalle Valo 				WSM_START_MODE_P2P_GO : WSM_START_MODE_AP,
231557fbcce3SJohannes Berg 		.band = (priv->channel->band == NL80211_BAND_5GHZ) ?
2316560424e9SKalle Valo 				WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G,
2317560424e9SKalle Valo 		.channel_number = priv->channel->hw_value,
2318560424e9SKalle Valo 		.beacon_interval = conf->beacon_int,
2319560424e9SKalle Valo 		.dtim_period = conf->dtim_period,
2320560424e9SKalle Valo 		.preamble = conf->use_short_preamble ?
2321560424e9SKalle Valo 				WSM_JOIN_PREAMBLE_SHORT :
2322560424e9SKalle Valo 				WSM_JOIN_PREAMBLE_LONG,
2323560424e9SKalle Valo 		.probe_delay = 100,
2324560424e9SKalle Valo 		.basic_rate_set = cw1200_rate_mask_to_wsm(priv,
2325560424e9SKalle Valo 				conf->basic_rates),
2326560424e9SKalle Valo 	};
2327560424e9SKalle Valo 	struct wsm_operational_mode mode = {
2328560424e9SKalle Valo 		.power_mode = cw1200_power_mode,
2329560424e9SKalle Valo 		.disable_more_flag_usage = true,
2330560424e9SKalle Valo 	};
2331560424e9SKalle Valo 
2332560424e9SKalle Valo 	memset(start.ssid, 0, sizeof(start.ssid));
2333560424e9SKalle Valo 	if (!conf->hidden_ssid) {
2334f276e20bSJohannes Berg 		start.ssid_len = priv->vif->cfg.ssid_len;
2335f276e20bSJohannes Berg 		memcpy(start.ssid, priv->vif->cfg.ssid, start.ssid_len);
2336560424e9SKalle Valo 	}
2337560424e9SKalle Valo 
2338560424e9SKalle Valo 	priv->beacon_int = conf->beacon_int;
2339560424e9SKalle Valo 	priv->join_dtim_period = conf->dtim_period;
2340560424e9SKalle Valo 
2341560424e9SKalle Valo 	memset(&priv->link_id_db, 0, sizeof(priv->link_id_db));
2342560424e9SKalle Valo 
2343560424e9SKalle Valo 	pr_debug("[AP] ch: %d(%d), bcn: %d(%d), brt: 0x%.8X, ssid: %.*s.\n",
2344560424e9SKalle Valo 		 start.channel_number, start.band,
2345560424e9SKalle Valo 		 start.beacon_interval, start.dtim_period,
2346560424e9SKalle Valo 		 start.basic_rate_set,
2347560424e9SKalle Valo 		 start.ssid_len, start.ssid);
2348560424e9SKalle Valo 	ret = wsm_start(priv, &start);
2349560424e9SKalle Valo 	if (!ret)
2350560424e9SKalle Valo 		ret = cw1200_upload_keys(priv);
2351560424e9SKalle Valo 	if (!ret && priv->vif->p2p) {
2352560424e9SKalle Valo 		pr_debug("[AP] Setting p2p powersave configuration.\n");
2353560424e9SKalle Valo 		wsm_set_p2p_ps_modeinfo(priv, &priv->p2p_ps_modeinfo);
2354560424e9SKalle Valo 	}
2355560424e9SKalle Valo 	if (!ret) {
2356560424e9SKalle Valo 		wsm_set_block_ack_policy(priv, 0, 0);
2357560424e9SKalle Valo 		priv->join_status = CW1200_JOIN_STATUS_AP;
2358560424e9SKalle Valo 		cw1200_update_filtering(priv);
2359560424e9SKalle Valo 	}
2360560424e9SKalle Valo 	wsm_set_operational_mode(priv, &mode);
2361560424e9SKalle Valo 	return ret;
2362560424e9SKalle Valo }
2363560424e9SKalle Valo 
cw1200_update_beaconing(struct cw1200_common * priv)2364560424e9SKalle Valo static int cw1200_update_beaconing(struct cw1200_common *priv)
2365560424e9SKalle Valo {
2366560424e9SKalle Valo 	struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
2367560424e9SKalle Valo 	struct wsm_reset reset = {
2368560424e9SKalle Valo 		.link_id = 0,
2369560424e9SKalle Valo 		.reset_statistics = true,
2370560424e9SKalle Valo 	};
2371560424e9SKalle Valo 
2372560424e9SKalle Valo 	if (priv->mode == NL80211_IFTYPE_AP) {
2373560424e9SKalle Valo 		/* TODO: check if changed channel, band */
2374560424e9SKalle Valo 		if (priv->join_status != CW1200_JOIN_STATUS_AP ||
2375560424e9SKalle Valo 		    priv->beacon_int != conf->beacon_int) {
2376560424e9SKalle Valo 			pr_debug("ap restarting\n");
2377560424e9SKalle Valo 			wsm_lock_tx(priv);
2378560424e9SKalle Valo 			if (priv->join_status != CW1200_JOIN_STATUS_PASSIVE)
2379560424e9SKalle Valo 				wsm_reset(priv, &reset);
2380560424e9SKalle Valo 			priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
2381560424e9SKalle Valo 			cw1200_start_ap(priv);
2382560424e9SKalle Valo 			wsm_unlock_tx(priv);
2383560424e9SKalle Valo 		} else
2384560424e9SKalle Valo 			pr_debug("ap started join_status: %d\n",
2385560424e9SKalle Valo 				 priv->join_status);
2386560424e9SKalle Valo 	}
2387560424e9SKalle Valo 	return 0;
2388560424e9SKalle Valo }
2389