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