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)93 void cw1200_stop(struct ieee80211_hw *dev)
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 	del_timer_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,u32 changed)324 int cw1200_config(struct ieee80211_hw *dev, 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,u32 value)860 int cw1200_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
861 {
862 	int ret = 0;
863 	__le32 val32;
864 	struct cw1200_common *priv = hw->priv;
865 
866 	if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
867 		return 0;
868 
869 	if (value != (u32) -1)
870 		val32 = __cpu_to_le32(value);
871 	else
872 		val32 = 0; /* disabled */
873 
874 	if (priv->rts_threshold == value)
875 		goto out;
876 
877 	pr_debug("[STA] Setting RTS threshold: %d\n",
878 		 priv->rts_threshold);
879 
880 	/* mutex_lock(&priv->conf_mutex); */
881 	ret = wsm_write_mib(priv, WSM_MIB_ID_DOT11_RTS_THRESHOLD,
882 			    &val32, sizeof(val32));
883 	if (!ret)
884 		priv->rts_threshold = value;
885 	/* mutex_unlock(&priv->conf_mutex); */
886 
887 out:
888 	return ret;
889 }
890 
891 /* If successful, LOCKS the TX queue! */
__cw1200_flush(struct cw1200_common * priv,bool drop)892 static int __cw1200_flush(struct cw1200_common *priv, bool drop)
893 {
894 	int i, ret;
895 
896 	for (;;) {
897 		/* TODO: correct flush handling is required when dev_stop.
898 		 * Temporary workaround: 2s
899 		 */
900 		if (drop) {
901 			for (i = 0; i < 4; ++i)
902 				cw1200_queue_clear(&priv->tx_queue[i]);
903 		} else {
904 			ret = wait_event_timeout(
905 				priv->tx_queue_stats.wait_link_id_empty,
906 				cw1200_queue_stats_is_empty(
907 					&priv->tx_queue_stats, -1),
908 				2 * HZ);
909 		}
910 
911 		if (!drop && ret <= 0) {
912 			ret = -ETIMEDOUT;
913 			break;
914 		} else {
915 			ret = 0;
916 		}
917 
918 		wsm_lock_tx(priv);
919 		if (!cw1200_queue_stats_is_empty(&priv->tx_queue_stats, -1)) {
920 			/* Highly unlikely: WSM requeued frames. */
921 			wsm_unlock_tx(priv);
922 			continue;
923 		}
924 		break;
925 	}
926 	return ret;
927 }
928 
cw1200_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)929 void cw1200_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
930 		  u32 queues, bool drop)
931 {
932 	struct cw1200_common *priv = hw->priv;
933 
934 	switch (priv->mode) {
935 	case NL80211_IFTYPE_MONITOR:
936 		drop = true;
937 		break;
938 	case NL80211_IFTYPE_AP:
939 		if (!priv->enable_beacon)
940 			drop = true;
941 		break;
942 	}
943 
944 	if (!__cw1200_flush(priv, drop))
945 		wsm_unlock_tx(priv);
946 
947 	return;
948 }
949 
950 /* ******************************************************************** */
951 /* WSM callbacks							*/
952 
cw1200_free_event_queue(struct cw1200_common * priv)953 void cw1200_free_event_queue(struct cw1200_common *priv)
954 {
955 	LIST_HEAD(list);
956 
957 	spin_lock(&priv->event_queue_lock);
958 	list_splice_init(&priv->event_queue, &list);
959 	spin_unlock(&priv->event_queue_lock);
960 
961 	__cw1200_free_event_queue(&list);
962 }
963 
cw1200_event_handler(struct work_struct * work)964 void cw1200_event_handler(struct work_struct *work)
965 {
966 	struct cw1200_common *priv =
967 		container_of(work, struct cw1200_common, event_handler);
968 	struct cw1200_wsm_event *event;
969 	LIST_HEAD(list);
970 
971 	spin_lock(&priv->event_queue_lock);
972 	list_splice_init(&priv->event_queue, &list);
973 	spin_unlock(&priv->event_queue_lock);
974 
975 	list_for_each_entry(event, &list, link) {
976 		switch (event->evt.id) {
977 		case WSM_EVENT_ERROR:
978 			pr_err("Unhandled WSM Error from LMAC\n");
979 			break;
980 		case WSM_EVENT_BSS_LOST:
981 			pr_debug("[CQM] BSS lost.\n");
982 			cancel_work_sync(&priv->unjoin_work);
983 			if (!down_trylock(&priv->scan.lock)) {
984 				cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
985 				up(&priv->scan.lock);
986 			} else {
987 				/* Scan is in progress. Delay reporting.
988 				 * Scan complete will trigger bss_loss_work
989 				 */
990 				priv->delayed_link_loss = 1;
991 				/* Also start a watchdog. */
992 				queue_delayed_work(priv->workqueue,
993 						   &priv->bss_loss_work, 5*HZ);
994 			}
995 			break;
996 		case WSM_EVENT_BSS_REGAINED:
997 			pr_debug("[CQM] BSS regained.\n");
998 			cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
999 			cancel_work_sync(&priv->unjoin_work);
1000 			break;
1001 		case WSM_EVENT_RADAR_DETECTED:
1002 			wiphy_info(priv->hw->wiphy, "radar pulse detected\n");
1003 			break;
1004 		case WSM_EVENT_RCPI_RSSI:
1005 		{
1006 			/* RSSI: signed Q8.0, RCPI: unsigned Q7.1
1007 			 * RSSI = RCPI / 2 - 110
1008 			 */
1009 			int rcpi_rssi = (int)(event->evt.data & 0xFF);
1010 			int cqm_evt;
1011 			if (priv->cqm_use_rssi)
1012 				rcpi_rssi = (s8)rcpi_rssi;
1013 			else
1014 				rcpi_rssi =  rcpi_rssi / 2 - 110;
1015 
1016 			cqm_evt = (rcpi_rssi <= priv->cqm_rssi_thold) ?
1017 				NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW :
1018 				NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
1019 			pr_debug("[CQM] RSSI event: %d.\n", rcpi_rssi);
1020 			ieee80211_cqm_rssi_notify(priv->vif, cqm_evt, rcpi_rssi,
1021 						  GFP_KERNEL);
1022 			break;
1023 		}
1024 		case WSM_EVENT_BT_INACTIVE:
1025 			pr_warn("Unhandled BT INACTIVE from LMAC\n");
1026 			break;
1027 		case WSM_EVENT_BT_ACTIVE:
1028 			pr_warn("Unhandled BT ACTIVE from LMAC\n");
1029 			break;
1030 		}
1031 	}
1032 	__cw1200_free_event_queue(&list);
1033 }
1034 
cw1200_bss_loss_work(struct work_struct * work)1035 void cw1200_bss_loss_work(struct work_struct *work)
1036 {
1037 	struct cw1200_common *priv =
1038 		container_of(work, struct cw1200_common, bss_loss_work.work);
1039 
1040 	pr_debug("[CQM] Reporting connection loss.\n");
1041 	wsm_lock_tx(priv);
1042 	if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1043 		wsm_unlock_tx(priv);
1044 }
1045 
cw1200_bss_params_work(struct work_struct * work)1046 void cw1200_bss_params_work(struct work_struct *work)
1047 {
1048 	struct cw1200_common *priv =
1049 		container_of(work, struct cw1200_common, bss_params_work);
1050 	mutex_lock(&priv->conf_mutex);
1051 
1052 	priv->bss_params.reset_beacon_loss = 1;
1053 	wsm_set_bss_params(priv, &priv->bss_params);
1054 	priv->bss_params.reset_beacon_loss = 0;
1055 
1056 	mutex_unlock(&priv->conf_mutex);
1057 }
1058 
1059 /* ******************************************************************** */
1060 /* Internal API								*/
1061 
1062 /* This function is called to Parse the SDD file
1063  * to extract listen_interval and PTA related information
1064  * sdd is a TLV: u8 id, u8 len, u8 data[]
1065  */
cw1200_parse_sdd_file(struct cw1200_common * priv)1066 static int cw1200_parse_sdd_file(struct cw1200_common *priv)
1067 {
1068 	const u8 *p = priv->sdd->data;
1069 	int ret = 0;
1070 
1071 	while (p + 2 <= priv->sdd->data + priv->sdd->size) {
1072 		if (p + p[1] + 2 > priv->sdd->data + priv->sdd->size) {
1073 			pr_warn("Malformed sdd structure\n");
1074 			return -1;
1075 		}
1076 		switch (p[0]) {
1077 		case SDD_PTA_CFG_ELT_ID: {
1078 			u16 v;
1079 			if (p[1] < 4) {
1080 				pr_warn("SDD_PTA_CFG_ELT_ID malformed\n");
1081 				ret = -1;
1082 				break;
1083 			}
1084 			v = le16_to_cpu(*((__le16 *)(p + 2)));
1085 			if (!v)  /* non-zero means this is enabled */
1086 				break;
1087 
1088 			v = le16_to_cpu(*((__le16 *)(p + 4)));
1089 			priv->conf_listen_interval = (v >> 7) & 0x1F;
1090 			pr_debug("PTA found; Listen Interval %d\n",
1091 				 priv->conf_listen_interval);
1092 			break;
1093 		}
1094 		case SDD_REFERENCE_FREQUENCY_ELT_ID: {
1095 			u16 clk = le16_to_cpu(*((__le16 *)(p + 2)));
1096 			if (clk != priv->hw_refclk)
1097 				pr_warn("SDD file doesn't match configured refclk (%d vs %d)\n",
1098 					clk, priv->hw_refclk);
1099 			break;
1100 		}
1101 		default:
1102 			break;
1103 		}
1104 		p += p[1] + 2;
1105 	}
1106 
1107 	if (!priv->bt_present) {
1108 		pr_debug("PTA element NOT found.\n");
1109 		priv->conf_listen_interval = 0;
1110 	}
1111 	return ret;
1112 }
1113 
cw1200_setup_mac(struct cw1200_common * priv)1114 int cw1200_setup_mac(struct cw1200_common *priv)
1115 {
1116 	int ret = 0;
1117 
1118 	/* NOTE: There is a bug in FW: it reports signal
1119 	 * as RSSI if RSSI subscription is enabled.
1120 	 * It's not enough to set WSM_RCPI_RSSI_USE_RSSI.
1121 	 *
1122 	 * NOTE2: RSSI based reports have been switched to RCPI, since
1123 	 * FW has a bug and RSSI reported values are not stable,
1124 	 * what can lead to signal level oscilations in user-end applications
1125 	 */
1126 	struct wsm_rcpi_rssi_threshold threshold = {
1127 		.rssiRcpiMode = WSM_RCPI_RSSI_THRESHOLD_ENABLE |
1128 		WSM_RCPI_RSSI_DONT_USE_UPPER |
1129 		WSM_RCPI_RSSI_DONT_USE_LOWER,
1130 		.rollingAverageCount = 16,
1131 	};
1132 
1133 	struct wsm_configuration cfg = {
1134 		.dot11StationId = &priv->mac_addr[0],
1135 	};
1136 
1137 	/* Remember the decission here to make sure, we will handle
1138 	 * the RCPI/RSSI value correctly on WSM_EVENT_RCPI_RSS
1139 	 */
1140 	if (threshold.rssiRcpiMode & WSM_RCPI_RSSI_USE_RSSI)
1141 		priv->cqm_use_rssi = true;
1142 
1143 	if (!priv->sdd) {
1144 		ret = request_firmware(&priv->sdd, priv->sdd_path, priv->pdev);
1145 		if (ret) {
1146 			pr_err("Can't load sdd file %s.\n", priv->sdd_path);
1147 			return ret;
1148 		}
1149 		cw1200_parse_sdd_file(priv);
1150 	}
1151 
1152 	cfg.dpdData = priv->sdd->data;
1153 	cfg.dpdData_size = priv->sdd->size;
1154 	ret = wsm_configuration(priv, &cfg);
1155 	if (ret)
1156 		return ret;
1157 
1158 	/* Configure RSSI/SCPI reporting as RSSI. */
1159 	wsm_set_rcpi_rssi_threshold(priv, &threshold);
1160 
1161 	return 0;
1162 }
1163 
cw1200_join_complete(struct cw1200_common * priv)1164 static void cw1200_join_complete(struct cw1200_common *priv)
1165 {
1166 	pr_debug("[STA] Join complete (%d)\n", priv->join_complete_status);
1167 
1168 	priv->join_pending = false;
1169 	if (priv->join_complete_status) {
1170 		priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1171 		cw1200_update_listening(priv, priv->listening);
1172 		cw1200_do_unjoin(priv);
1173 		ieee80211_connection_loss(priv->vif);
1174 	} else {
1175 		if (priv->mode == NL80211_IFTYPE_ADHOC)
1176 			priv->join_status = CW1200_JOIN_STATUS_IBSS;
1177 		else
1178 			priv->join_status = CW1200_JOIN_STATUS_PRE_STA;
1179 	}
1180 	wsm_unlock_tx(priv); /* Clearing the lock held before do_join() */
1181 }
1182 
cw1200_join_complete_work(struct work_struct * work)1183 void cw1200_join_complete_work(struct work_struct *work)
1184 {
1185 	struct cw1200_common *priv =
1186 		container_of(work, struct cw1200_common, join_complete_work);
1187 	mutex_lock(&priv->conf_mutex);
1188 	cw1200_join_complete(priv);
1189 	mutex_unlock(&priv->conf_mutex);
1190 }
1191 
cw1200_join_complete_cb(struct cw1200_common * priv,struct wsm_join_complete * arg)1192 void cw1200_join_complete_cb(struct cw1200_common *priv,
1193 			     struct wsm_join_complete *arg)
1194 {
1195 	pr_debug("[STA] cw1200_join_complete_cb called, status=%d.\n",
1196 		 arg->status);
1197 
1198 	if (cancel_delayed_work(&priv->join_timeout)) {
1199 		priv->join_complete_status = arg->status;
1200 		queue_work(priv->workqueue, &priv->join_complete_work);
1201 	}
1202 }
1203 
1204 /* MUST be called with tx_lock held!  It will be unlocked for us. */
cw1200_do_join(struct cw1200_common * priv)1205 static void cw1200_do_join(struct cw1200_common *priv)
1206 {
1207 	const u8 *bssid;
1208 	struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
1209 	struct cfg80211_bss *bss = NULL;
1210 	struct wsm_protected_mgmt_policy mgmt_policy;
1211 	struct wsm_join join = {
1212 		.mode = priv->vif->cfg.ibss_joined ?
1213 		WSM_JOIN_MODE_IBSS : WSM_JOIN_MODE_BSS,
1214 		.preamble_type = WSM_JOIN_PREAMBLE_LONG,
1215 		.probe_for_join = 1,
1216 		.atim_window = 0,
1217 		.basic_rate_set = cw1200_rate_mask_to_wsm(priv,
1218 							  conf->basic_rates),
1219 	};
1220 	if (delayed_work_pending(&priv->join_timeout)) {
1221 		pr_warn("[STA] - Join request already pending, skipping..\n");
1222 		wsm_unlock_tx(priv);
1223 		return;
1224 	}
1225 
1226 	if (priv->join_status)
1227 		cw1200_do_unjoin(priv);
1228 
1229 	bssid = priv->vif->bss_conf.bssid;
1230 
1231 	bss = cfg80211_get_bss(priv->hw->wiphy, priv->channel, bssid, NULL, 0,
1232 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1233 
1234 	if (!bss && !priv->vif->cfg.ibss_joined) {
1235 		wsm_unlock_tx(priv);
1236 		return;
1237 	}
1238 
1239 	mutex_lock(&priv->conf_mutex);
1240 
1241 	/* Under the conf lock: check scan status and
1242 	 * bail out if it is in progress.
1243 	 */
1244 	if (atomic_read(&priv->scan.in_progress)) {
1245 		wsm_unlock_tx(priv);
1246 		goto done_put;
1247 	}
1248 
1249 	priv->join_pending = true;
1250 
1251 	/* Sanity check basic rates */
1252 	if (!join.basic_rate_set)
1253 		join.basic_rate_set = 7;
1254 
1255 	/* Sanity check beacon interval */
1256 	if (!priv->beacon_int)
1257 		priv->beacon_int = 1;
1258 
1259 	join.beacon_interval = priv->beacon_int;
1260 
1261 	/* BT Coex related changes */
1262 	if (priv->bt_present) {
1263 		if (((priv->conf_listen_interval * 100) %
1264 		     priv->beacon_int) == 0)
1265 			priv->listen_interval =
1266 				((priv->conf_listen_interval * 100) /
1267 				 priv->beacon_int);
1268 		else
1269 			priv->listen_interval =
1270 				((priv->conf_listen_interval * 100) /
1271 				 priv->beacon_int + 1);
1272 	}
1273 
1274 	if (priv->hw->conf.ps_dtim_period)
1275 		priv->join_dtim_period = priv->hw->conf.ps_dtim_period;
1276 	join.dtim_period = priv->join_dtim_period;
1277 
1278 	join.channel_number = priv->channel->hw_value;
1279 	join.band = (priv->channel->band == NL80211_BAND_5GHZ) ?
1280 		WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
1281 
1282 	memcpy(join.bssid, bssid, sizeof(join.bssid));
1283 
1284 	pr_debug("[STA] Join BSSID: %pM DTIM: %d, interval: %d\n",
1285 		 join.bssid,
1286 		 join.dtim_period, priv->beacon_int);
1287 
1288 	if (!priv->vif->cfg.ibss_joined) {
1289 		const u8 *ssidie;
1290 		rcu_read_lock();
1291 		ssidie = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1292 		if (ssidie) {
1293 			join.ssid_len = ssidie[1];
1294 			memcpy(join.ssid, &ssidie[2], join.ssid_len);
1295 		}
1296 		rcu_read_unlock();
1297 	}
1298 
1299 	if (priv->vif->p2p) {
1300 		join.flags |= WSM_JOIN_FLAGS_P2P_GO;
1301 		join.basic_rate_set =
1302 			cw1200_rate_mask_to_wsm(priv, 0xFF0);
1303 	}
1304 
1305 	/* Enable asynchronous join calls */
1306 	if (!priv->vif->cfg.ibss_joined) {
1307 		join.flags |= WSM_JOIN_FLAGS_FORCE;
1308 		join.flags |= WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND;
1309 	}
1310 
1311 	wsm_flush_tx(priv);
1312 
1313 	/* Stay Awake for Join and Auth Timeouts and a bit more */
1314 	cw1200_pm_stay_awake(&priv->pm_state,
1315 			     CW1200_JOIN_TIMEOUT + CW1200_AUTH_TIMEOUT);
1316 
1317 	cw1200_update_listening(priv, false);
1318 
1319 	/* Turn on Block ACKs */
1320 	wsm_set_block_ack_policy(priv, priv->ba_tx_tid_mask,
1321 				 priv->ba_rx_tid_mask);
1322 
1323 	/* Set up timeout */
1324 	if (join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND) {
1325 		priv->join_status = CW1200_JOIN_STATUS_JOINING;
1326 		queue_delayed_work(priv->workqueue,
1327 				   &priv->join_timeout,
1328 				   CW1200_JOIN_TIMEOUT);
1329 	}
1330 
1331 	/* 802.11w protected mgmt frames */
1332 	mgmt_policy.protectedMgmtEnable = 0;
1333 	mgmt_policy.unprotectedMgmtFramesAllowed = 1;
1334 	mgmt_policy.encryptionForAuthFrame = 1;
1335 	wsm_set_protected_mgmt_policy(priv, &mgmt_policy);
1336 
1337 	/* Perform actual join */
1338 	if (wsm_join(priv, &join)) {
1339 		pr_err("[STA] cw1200_join_work: wsm_join failed!\n");
1340 		cancel_delayed_work_sync(&priv->join_timeout);
1341 		cw1200_update_listening(priv, priv->listening);
1342 		/* Tx lock still held, unjoin will clear it. */
1343 		if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1344 			wsm_unlock_tx(priv);
1345 	} else {
1346 		if (!(join.flags & WSM_JOIN_FLAGS_FORCE_WITH_COMPLETE_IND))
1347 			cw1200_join_complete(priv); /* Will clear tx_lock */
1348 
1349 		/* Upload keys */
1350 		cw1200_upload_keys(priv);
1351 
1352 		/* Due to beacon filtering it is possible that the
1353 		 * AP's beacon is not known for the mac80211 stack.
1354 		 * Disable filtering temporary to make sure the stack
1355 		 * receives at least one
1356 		 */
1357 		priv->disable_beacon_filter = true;
1358 	}
1359 	cw1200_update_filtering(priv);
1360 
1361 done_put:
1362 	mutex_unlock(&priv->conf_mutex);
1363 	if (bss)
1364 		cfg80211_put_bss(priv->hw->wiphy, bss);
1365 }
1366 
cw1200_join_timeout(struct work_struct * work)1367 void cw1200_join_timeout(struct work_struct *work)
1368 {
1369 	struct cw1200_common *priv =
1370 		container_of(work, struct cw1200_common, join_timeout.work);
1371 	pr_debug("[WSM] Join timed out.\n");
1372 	wsm_lock_tx(priv);
1373 	if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1374 		wsm_unlock_tx(priv);
1375 }
1376 
cw1200_do_unjoin(struct cw1200_common * priv)1377 static void cw1200_do_unjoin(struct cw1200_common *priv)
1378 {
1379 	struct wsm_reset reset = {
1380 		.reset_statistics = true,
1381 	};
1382 
1383 	cancel_delayed_work_sync(&priv->join_timeout);
1384 
1385 	mutex_lock(&priv->conf_mutex);
1386 	priv->join_pending = false;
1387 
1388 	if (atomic_read(&priv->scan.in_progress)) {
1389 		if (priv->delayed_unjoin)
1390 			wiphy_dbg(priv->hw->wiphy, "Delayed unjoin is already scheduled.\n");
1391 		else
1392 			priv->delayed_unjoin = true;
1393 		goto done;
1394 	}
1395 
1396 	priv->delayed_link_loss = false;
1397 
1398 	if (!priv->join_status)
1399 		goto done;
1400 
1401 	if (priv->join_status == CW1200_JOIN_STATUS_AP)
1402 		goto done;
1403 
1404 	cancel_work_sync(&priv->update_filtering_work);
1405 	cancel_work_sync(&priv->set_beacon_wakeup_period_work);
1406 	priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1407 
1408 	/* Unjoin is a reset. */
1409 	wsm_flush_tx(priv);
1410 	wsm_keep_alive_period(priv, 0);
1411 	wsm_reset(priv, &reset);
1412 	wsm_set_output_power(priv, priv->output_power * 10);
1413 	priv->join_dtim_period = 0;
1414 	cw1200_setup_mac(priv);
1415 	cw1200_free_event_queue(priv);
1416 	cancel_work_sync(&priv->event_handler);
1417 	cw1200_update_listening(priv, priv->listening);
1418 	cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
1419 
1420 	/* Disable Block ACKs */
1421 	wsm_set_block_ack_policy(priv, 0, 0);
1422 
1423 	priv->disable_beacon_filter = false;
1424 	cw1200_update_filtering(priv);
1425 	memset(&priv->association_mode, 0,
1426 	       sizeof(priv->association_mode));
1427 	memset(&priv->bss_params, 0, sizeof(priv->bss_params));
1428 	priv->setbssparams_done = false;
1429 	memset(&priv->firmware_ps_mode, 0,
1430 	       sizeof(priv->firmware_ps_mode));
1431 
1432 	pr_debug("[STA] Unjoin completed.\n");
1433 
1434 done:
1435 	mutex_unlock(&priv->conf_mutex);
1436 }
1437 
cw1200_unjoin_work(struct work_struct * work)1438 void cw1200_unjoin_work(struct work_struct *work)
1439 {
1440 	struct cw1200_common *priv =
1441 		container_of(work, struct cw1200_common, unjoin_work);
1442 
1443 	cw1200_do_unjoin(priv);
1444 
1445 	/* Tell the stack we're dead */
1446 	ieee80211_connection_loss(priv->vif);
1447 
1448 	wsm_unlock_tx(priv);
1449 }
1450 
cw1200_enable_listening(struct cw1200_common * priv)1451 int cw1200_enable_listening(struct cw1200_common *priv)
1452 {
1453 	struct wsm_start start = {
1454 		.mode = WSM_START_MODE_P2P_DEV,
1455 		.band = WSM_PHY_BAND_2_4G,
1456 		.beacon_interval = 100,
1457 		.dtim_period = 1,
1458 		.probe_delay = 0,
1459 		.basic_rate_set = 0x0F,
1460 	};
1461 
1462 	if (priv->channel) {
1463 		start.band = priv->channel->band == NL80211_BAND_5GHZ ?
1464 			     WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
1465 		start.channel_number = priv->channel->hw_value;
1466 	} else {
1467 		start.band = WSM_PHY_BAND_2_4G;
1468 		start.channel_number = 1;
1469 	}
1470 
1471 	return wsm_start(priv, &start);
1472 }
1473 
cw1200_disable_listening(struct cw1200_common * priv)1474 int cw1200_disable_listening(struct cw1200_common *priv)
1475 {
1476 	int ret;
1477 	struct wsm_reset reset = {
1478 		.reset_statistics = true,
1479 	};
1480 	ret = wsm_reset(priv, &reset);
1481 	return ret;
1482 }
1483 
cw1200_update_listening(struct cw1200_common * priv,bool enabled)1484 void cw1200_update_listening(struct cw1200_common *priv, bool enabled)
1485 {
1486 	if (enabled) {
1487 		if (priv->join_status == CW1200_JOIN_STATUS_PASSIVE) {
1488 			if (!cw1200_enable_listening(priv))
1489 				priv->join_status = CW1200_JOIN_STATUS_MONITOR;
1490 			wsm_set_probe_responder(priv, true);
1491 		}
1492 	} else {
1493 		if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
1494 			if (!cw1200_disable_listening(priv))
1495 				priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
1496 			wsm_set_probe_responder(priv, false);
1497 		}
1498 	}
1499 }
1500 
cw1200_set_uapsd_param(struct cw1200_common * priv,const struct wsm_edca_params * arg)1501 int cw1200_set_uapsd_param(struct cw1200_common *priv,
1502 			   const struct wsm_edca_params *arg)
1503 {
1504 	int ret;
1505 	u16 uapsd_flags = 0;
1506 
1507 	/* Here's the mapping AC [queue, bit]
1508 	 *  VO [0,3], VI [1, 2], BE [2, 1], BK [3, 0]
1509 	 */
1510 
1511 	if (arg->uapsd_enable[0])
1512 		uapsd_flags |= 1 << 3;
1513 
1514 	if (arg->uapsd_enable[1])
1515 		uapsd_flags |= 1 << 2;
1516 
1517 	if (arg->uapsd_enable[2])
1518 		uapsd_flags |= 1 << 1;
1519 
1520 	if (arg->uapsd_enable[3])
1521 		uapsd_flags |= 1;
1522 
1523 	/* Currently pseudo U-APSD operation is not supported, so setting
1524 	 * MinAutoTriggerInterval, MaxAutoTriggerInterval and
1525 	 * AutoTriggerStep to 0
1526 	 */
1527 
1528 	priv->uapsd_info.uapsd_flags = cpu_to_le16(uapsd_flags);
1529 	priv->uapsd_info.min_auto_trigger_interval = 0;
1530 	priv->uapsd_info.max_auto_trigger_interval = 0;
1531 	priv->uapsd_info.auto_trigger_step = 0;
1532 
1533 	ret = wsm_set_uapsd_info(priv, &priv->uapsd_info);
1534 	return ret;
1535 }
1536 
1537 /* ******************************************************************** */
1538 /* AP API								*/
1539 
cw1200_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1540 int cw1200_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1541 		   struct ieee80211_sta *sta)
1542 {
1543 	struct cw1200_common *priv = hw->priv;
1544 	struct cw1200_sta_priv *sta_priv =
1545 			(struct cw1200_sta_priv *)&sta->drv_priv;
1546 	struct cw1200_link_entry *entry;
1547 	struct sk_buff *skb;
1548 
1549 	if (priv->mode != NL80211_IFTYPE_AP)
1550 		return 0;
1551 
1552 	sta_priv->link_id = cw1200_find_link_id(priv, sta->addr);
1553 	if (WARN_ON(!sta_priv->link_id)) {
1554 		wiphy_info(priv->hw->wiphy,
1555 			   "[AP] No more link IDs available.\n");
1556 		return -ENOENT;
1557 	}
1558 
1559 	entry = &priv->link_id_db[sta_priv->link_id - 1];
1560 	spin_lock_bh(&priv->ps_state_lock);
1561 	if ((sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK) ==
1562 					IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
1563 		priv->sta_asleep_mask |= BIT(sta_priv->link_id);
1564 	entry->status = CW1200_LINK_HARD;
1565 	while ((skb = skb_dequeue(&entry->rx_queue)))
1566 		ieee80211_rx_irqsafe(priv->hw, skb);
1567 	spin_unlock_bh(&priv->ps_state_lock);
1568 	return 0;
1569 }
1570 
cw1200_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1571 int cw1200_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1572 		      struct ieee80211_sta *sta)
1573 {
1574 	struct cw1200_common *priv = hw->priv;
1575 	struct cw1200_sta_priv *sta_priv =
1576 			(struct cw1200_sta_priv *)&sta->drv_priv;
1577 	struct cw1200_link_entry *entry;
1578 
1579 	if (priv->mode != NL80211_IFTYPE_AP || !sta_priv->link_id)
1580 		return 0;
1581 
1582 	entry = &priv->link_id_db[sta_priv->link_id - 1];
1583 	spin_lock_bh(&priv->ps_state_lock);
1584 	entry->status = CW1200_LINK_RESERVE;
1585 	entry->timestamp = jiffies;
1586 	wsm_lock_tx_async(priv);
1587 	if (queue_work(priv->workqueue, &priv->link_id_work) <= 0)
1588 		wsm_unlock_tx(priv);
1589 	spin_unlock_bh(&priv->ps_state_lock);
1590 	flush_workqueue(priv->workqueue);
1591 	return 0;
1592 }
1593 
__cw1200_sta_notify(struct ieee80211_hw * dev,struct ieee80211_vif * vif,enum sta_notify_cmd notify_cmd,int link_id)1594 static void __cw1200_sta_notify(struct ieee80211_hw *dev,
1595 				struct ieee80211_vif *vif,
1596 				enum sta_notify_cmd notify_cmd,
1597 				int link_id)
1598 {
1599 	struct cw1200_common *priv = dev->priv;
1600 	u32 bit, prev;
1601 
1602 	/* Zero link id means "for all link IDs" */
1603 	if (link_id)
1604 		bit = BIT(link_id);
1605 	else if (WARN_ON_ONCE(notify_cmd != STA_NOTIFY_AWAKE))
1606 		bit = 0;
1607 	else
1608 		bit = priv->link_id_map;
1609 	prev = priv->sta_asleep_mask & bit;
1610 
1611 	switch (notify_cmd) {
1612 	case STA_NOTIFY_SLEEP:
1613 		if (!prev) {
1614 			if (priv->buffered_multicasts &&
1615 			    !priv->sta_asleep_mask)
1616 				queue_work(priv->workqueue,
1617 					   &priv->multicast_start_work);
1618 			priv->sta_asleep_mask |= bit;
1619 		}
1620 		break;
1621 	case STA_NOTIFY_AWAKE:
1622 		if (prev) {
1623 			priv->sta_asleep_mask &= ~bit;
1624 			priv->pspoll_mask &= ~bit;
1625 			if (priv->tx_multicast && link_id &&
1626 			    !priv->sta_asleep_mask)
1627 				queue_work(priv->workqueue,
1628 					   &priv->multicast_stop_work);
1629 			cw1200_bh_wakeup(priv);
1630 		}
1631 		break;
1632 	}
1633 }
1634 
cw1200_sta_notify(struct ieee80211_hw * dev,struct ieee80211_vif * vif,enum sta_notify_cmd notify_cmd,struct ieee80211_sta * sta)1635 void cw1200_sta_notify(struct ieee80211_hw *dev,
1636 		       struct ieee80211_vif *vif,
1637 		       enum sta_notify_cmd notify_cmd,
1638 		       struct ieee80211_sta *sta)
1639 {
1640 	struct cw1200_common *priv = dev->priv;
1641 	struct cw1200_sta_priv *sta_priv =
1642 		(struct cw1200_sta_priv *)&sta->drv_priv;
1643 
1644 	spin_lock_bh(&priv->ps_state_lock);
1645 	__cw1200_sta_notify(dev, vif, notify_cmd, sta_priv->link_id);
1646 	spin_unlock_bh(&priv->ps_state_lock);
1647 }
1648 
cw1200_ps_notify(struct cw1200_common * priv,int link_id,bool ps)1649 static void cw1200_ps_notify(struct cw1200_common *priv,
1650 		      int link_id, bool ps)
1651 {
1652 	if (link_id > CW1200_MAX_STA_IN_AP_MODE)
1653 		return;
1654 
1655 	pr_debug("%s for LinkId: %d. STAs asleep: %.8X\n",
1656 		 ps ? "Stop" : "Start",
1657 		 link_id, priv->sta_asleep_mask);
1658 
1659 	__cw1200_sta_notify(priv->hw, priv->vif,
1660 			    ps ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE, link_id);
1661 }
1662 
cw1200_set_tim_impl(struct cw1200_common * priv,bool aid0_bit_set)1663 static int cw1200_set_tim_impl(struct cw1200_common *priv, bool aid0_bit_set)
1664 {
1665 	struct sk_buff *skb;
1666 	struct wsm_update_ie update_ie = {
1667 		.what = WSM_UPDATE_IE_BEACON,
1668 		.count = 1,
1669 	};
1670 	u16 tim_offset, tim_length;
1671 
1672 	pr_debug("[AP] mcast: %s.\n", aid0_bit_set ? "ena" : "dis");
1673 
1674 	skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
1675 				       &tim_offset, &tim_length, 0);
1676 	if (!skb) {
1677 		if (!__cw1200_flush(priv, true))
1678 			wsm_unlock_tx(priv);
1679 		return -ENOENT;
1680 	}
1681 
1682 	if (tim_offset && tim_length >= 6) {
1683 		/* Ignore DTIM count from mac80211:
1684 		 * firmware handles DTIM internally.
1685 		 */
1686 		skb->data[tim_offset + 2] = 0;
1687 
1688 		/* Set/reset aid0 bit */
1689 		if (aid0_bit_set)
1690 			skb->data[tim_offset + 4] |= 1;
1691 		else
1692 			skb->data[tim_offset + 4] &= ~1;
1693 	}
1694 
1695 	update_ie.ies = &skb->data[tim_offset];
1696 	update_ie.length = tim_length;
1697 	wsm_update_ie(priv, &update_ie);
1698 
1699 	dev_kfree_skb(skb);
1700 
1701 	return 0;
1702 }
1703 
cw1200_set_tim_work(struct work_struct * work)1704 void cw1200_set_tim_work(struct work_struct *work)
1705 {
1706 	struct cw1200_common *priv =
1707 		container_of(work, struct cw1200_common, set_tim_work);
1708 	(void)cw1200_set_tim_impl(priv, priv->aid0_bit_set);
1709 }
1710 
cw1200_set_tim(struct ieee80211_hw * dev,struct ieee80211_sta * sta,bool set)1711 int cw1200_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
1712 		   bool set)
1713 {
1714 	struct cw1200_common *priv = dev->priv;
1715 	queue_work(priv->workqueue, &priv->set_tim_work);
1716 	return 0;
1717 }
1718 
cw1200_set_cts_work(struct work_struct * work)1719 void cw1200_set_cts_work(struct work_struct *work)
1720 {
1721 	struct cw1200_common *priv =
1722 		container_of(work, struct cw1200_common, set_cts_work);
1723 
1724 	u8 erp_ie[3] = {WLAN_EID_ERP_INFO, 0x1, 0};
1725 	struct wsm_update_ie update_ie = {
1726 		.what = WSM_UPDATE_IE_BEACON,
1727 		.count = 1,
1728 		.ies = erp_ie,
1729 		.length = 3,
1730 	};
1731 	u32 erp_info;
1732 	__le32 use_cts_prot;
1733 	mutex_lock(&priv->conf_mutex);
1734 	erp_info = priv->erp_info;
1735 	mutex_unlock(&priv->conf_mutex);
1736 	use_cts_prot =
1737 		erp_info & WLAN_ERP_USE_PROTECTION ?
1738 		__cpu_to_le32(1) : 0;
1739 
1740 	erp_ie[ERP_INFO_BYTE_OFFSET] = erp_info;
1741 
1742 	pr_debug("[STA] ERP information 0x%x\n", erp_info);
1743 
1744 	wsm_write_mib(priv, WSM_MIB_ID_NON_ERP_PROTECTION,
1745 		      &use_cts_prot, sizeof(use_cts_prot));
1746 	wsm_update_ie(priv, &update_ie);
1747 
1748 	return;
1749 }
1750 
cw1200_set_btcoexinfo(struct cw1200_common * priv)1751 static int cw1200_set_btcoexinfo(struct cw1200_common *priv)
1752 {
1753 	struct wsm_override_internal_txrate arg;
1754 	int ret = 0;
1755 
1756 	if (priv->mode == NL80211_IFTYPE_STATION) {
1757 		/* Plumb PSPOLL and NULL template */
1758 		cw1200_upload_pspoll(priv);
1759 		cw1200_upload_null(priv);
1760 		cw1200_upload_qosnull(priv);
1761 	} else {
1762 		return 0;
1763 	}
1764 
1765 	memset(&arg, 0, sizeof(struct wsm_override_internal_txrate));
1766 
1767 	if (!priv->vif->p2p) {
1768 		/* STATION mode */
1769 		if (priv->bss_params.operational_rate_set & ~0xF) {
1770 			pr_debug("[STA] STA has ERP rates\n");
1771 			/* G or BG mode */
1772 			arg.internalTxRate = (__ffs(
1773 			priv->bss_params.operational_rate_set & ~0xF));
1774 		} else {
1775 			pr_debug("[STA] STA has non ERP rates\n");
1776 			/* B only mode */
1777 			arg.internalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
1778 		}
1779 		arg.nonErpInternalTxRate = (__ffs(le32_to_cpu(priv->association_mode.basic_rate_set)));
1780 	} else {
1781 		/* P2P mode */
1782 		arg.internalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
1783 		arg.nonErpInternalTxRate = (__ffs(priv->bss_params.operational_rate_set & ~0xF));
1784 	}
1785 
1786 	pr_debug("[STA] BTCOEX_INFO MODE %d, internalTxRate : %x, nonErpInternalTxRate: %x\n",
1787 		 priv->mode,
1788 		 arg.internalTxRate,
1789 		 arg.nonErpInternalTxRate);
1790 
1791 	ret = wsm_write_mib(priv, WSM_MIB_ID_OVERRIDE_INTERNAL_TX_RATE,
1792 			    &arg, sizeof(arg));
1793 
1794 	return ret;
1795 }
1796 
cw1200_bss_info_changed(struct ieee80211_hw * dev,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)1797 void cw1200_bss_info_changed(struct ieee80211_hw *dev,
1798 			     struct ieee80211_vif *vif,
1799 			     struct ieee80211_bss_conf *info,
1800 			     u64 changed)
1801 {
1802 	struct cw1200_common *priv = dev->priv;
1803 	bool do_join = false;
1804 
1805 	mutex_lock(&priv->conf_mutex);
1806 
1807 	pr_debug("BSS CHANGED:  %llx\n", changed);
1808 
1809 	/* TODO: BSS_CHANGED_QOS */
1810 	/* TODO: BSS_CHANGED_TXPOWER */
1811 
1812 	if (changed & BSS_CHANGED_ARP_FILTER) {
1813 		struct wsm_mib_arp_ipv4_filter filter = {0};
1814 		int i;
1815 
1816 		pr_debug("[STA] BSS_CHANGED_ARP_FILTER cnt: %d\n",
1817 			 vif->cfg.arp_addr_cnt);
1818 
1819 		/* Currently only one IP address is supported by firmware.
1820 		 * In case of more IPs arp filtering will be disabled.
1821 		 */
1822 		if (vif->cfg.arp_addr_cnt > 0 &&
1823 		    vif->cfg.arp_addr_cnt <= WSM_MAX_ARP_IP_ADDRTABLE_ENTRIES) {
1824 			for (i = 0; i < vif->cfg.arp_addr_cnt; i++) {
1825 				filter.ipv4addrs[i] = vif->cfg.arp_addr_list[i];
1826 				pr_debug("[STA] addr[%d]: 0x%X\n",
1827 					 i, filter.ipv4addrs[i]);
1828 			}
1829 			filter.enable = __cpu_to_le32(1);
1830 		}
1831 
1832 		pr_debug("[STA] arp ip filter enable: %d\n",
1833 			 __le32_to_cpu(filter.enable));
1834 
1835 		wsm_set_arp_ipv4_filter(priv, &filter);
1836 	}
1837 
1838 	if (changed &
1839 	    (BSS_CHANGED_BEACON |
1840 	     BSS_CHANGED_AP_PROBE_RESP |
1841 	     BSS_CHANGED_BSSID |
1842 	     BSS_CHANGED_SSID |
1843 	     BSS_CHANGED_IBSS)) {
1844 		pr_debug("BSS_CHANGED_BEACON\n");
1845 		priv->beacon_int = info->beacon_int;
1846 		cw1200_update_beaconing(priv);
1847 		cw1200_upload_beacon(priv);
1848 	}
1849 
1850 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
1851 		pr_debug("BSS_CHANGED_BEACON_ENABLED (%d)\n", info->enable_beacon);
1852 
1853 		if (priv->enable_beacon != info->enable_beacon) {
1854 			cw1200_enable_beaconing(priv, info->enable_beacon);
1855 			priv->enable_beacon = info->enable_beacon;
1856 		}
1857 	}
1858 
1859 	if (changed & BSS_CHANGED_BEACON_INT) {
1860 		pr_debug("CHANGED_BEACON_INT\n");
1861 		if (vif->cfg.ibss_joined)
1862 			do_join = true;
1863 		else if (priv->join_status == CW1200_JOIN_STATUS_AP)
1864 			cw1200_update_beaconing(priv);
1865 	}
1866 
1867 	/* assoc/disassoc, or maybe AID changed */
1868 	if (changed & BSS_CHANGED_ASSOC) {
1869 		wsm_lock_tx(priv);
1870 		priv->wep_default_key_id = -1;
1871 		wsm_unlock_tx(priv);
1872 	}
1873 
1874 	if (changed & BSS_CHANGED_BSSID) {
1875 		pr_debug("BSS_CHANGED_BSSID\n");
1876 		do_join = true;
1877 	}
1878 
1879 	if (changed &
1880 	    (BSS_CHANGED_ASSOC |
1881 	     BSS_CHANGED_BSSID |
1882 	     BSS_CHANGED_IBSS |
1883 	     BSS_CHANGED_BASIC_RATES |
1884 	     BSS_CHANGED_HT)) {
1885 		pr_debug("BSS_CHANGED_ASSOC\n");
1886 		if (vif->cfg.assoc) {
1887 			if (priv->join_status < CW1200_JOIN_STATUS_PRE_STA) {
1888 				ieee80211_connection_loss(vif);
1889 				mutex_unlock(&priv->conf_mutex);
1890 				return;
1891 			} else if (priv->join_status == CW1200_JOIN_STATUS_PRE_STA) {
1892 				priv->join_status = CW1200_JOIN_STATUS_STA;
1893 			}
1894 		} else {
1895 			do_join = true;
1896 		}
1897 
1898 		if (vif->cfg.assoc || vif->cfg.ibss_joined) {
1899 			struct ieee80211_sta *sta = NULL;
1900 			__le32 htprot = 0;
1901 
1902 			if (info->dtim_period)
1903 				priv->join_dtim_period = info->dtim_period;
1904 			priv->beacon_int = info->beacon_int;
1905 
1906 			rcu_read_lock();
1907 
1908 			if (info->bssid && !vif->cfg.ibss_joined)
1909 				sta = ieee80211_find_sta(vif, info->bssid);
1910 			if (sta) {
1911 				priv->ht_info.ht_cap = sta->deflink.ht_cap;
1912 				priv->bss_params.operational_rate_set =
1913 					cw1200_rate_mask_to_wsm(priv,
1914 						sta->deflink.supp_rates[priv->channel->band]);
1915 				priv->ht_info.channel_type = cfg80211_get_chandef_type(&dev->conf.chandef);
1916 				priv->ht_info.operation_mode = info->ht_operation_mode;
1917 			} else {
1918 				memset(&priv->ht_info, 0,
1919 				       sizeof(priv->ht_info));
1920 				priv->bss_params.operational_rate_set = -1;
1921 			}
1922 			rcu_read_unlock();
1923 
1924 			/* Non Greenfield stations present */
1925 			if (priv->ht_info.operation_mode &
1926 			    IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT)
1927 				htprot |= cpu_to_le32(WSM_NON_GREENFIELD_STA_PRESENT);
1928 
1929 			/* Set HT protection method */
1930 			htprot |= cpu_to_le32((priv->ht_info.operation_mode & IEEE80211_HT_OP_MODE_PROTECTION) << 2);
1931 
1932 			/* TODO:
1933 			 * STBC_param.dual_cts
1934 			 *  STBC_param.LSIG_TXOP_FILL
1935 			 */
1936 
1937 			wsm_write_mib(priv, WSM_MIB_ID_SET_HT_PROTECTION,
1938 				      &htprot, sizeof(htprot));
1939 
1940 			priv->association_mode.greenfield =
1941 				cw1200_ht_greenfield(&priv->ht_info);
1942 			priv->association_mode.flags =
1943 				WSM_ASSOCIATION_MODE_SNOOP_ASSOC_FRAMES |
1944 				WSM_ASSOCIATION_MODE_USE_PREAMBLE_TYPE |
1945 				WSM_ASSOCIATION_MODE_USE_HT_MODE |
1946 				WSM_ASSOCIATION_MODE_USE_BASIC_RATE_SET |
1947 				WSM_ASSOCIATION_MODE_USE_MPDU_START_SPACING;
1948 			priv->association_mode.preamble =
1949 				info->use_short_preamble ?
1950 				WSM_JOIN_PREAMBLE_SHORT :
1951 				WSM_JOIN_PREAMBLE_LONG;
1952 			priv->association_mode.basic_rate_set = __cpu_to_le32(
1953 				cw1200_rate_mask_to_wsm(priv,
1954 							info->basic_rates));
1955 			priv->association_mode.mpdu_start_spacing =
1956 				cw1200_ht_ampdu_density(&priv->ht_info);
1957 
1958 			cw1200_cqm_bssloss_sm(priv, 0, 0, 0);
1959 			cancel_work_sync(&priv->unjoin_work);
1960 
1961 			priv->bss_params.beacon_lost_count = priv->cqm_beacon_loss_count;
1962 			priv->bss_params.aid = vif->cfg.aid;
1963 
1964 			if (priv->join_dtim_period < 1)
1965 				priv->join_dtim_period = 1;
1966 
1967 			pr_debug("[STA] DTIM %d, interval: %d\n",
1968 				 priv->join_dtim_period, priv->beacon_int);
1969 			pr_debug("[STA] Preamble: %d, Greenfield: %d, Aid: %d, Rates: 0x%.8X, Basic: 0x%.8X\n",
1970 				 priv->association_mode.preamble,
1971 				 priv->association_mode.greenfield,
1972 				 priv->bss_params.aid,
1973 				 priv->bss_params.operational_rate_set,
1974 				 priv->association_mode.basic_rate_set);
1975 			wsm_set_association_mode(priv, &priv->association_mode);
1976 
1977 			if (!vif->cfg.ibss_joined) {
1978 				wsm_keep_alive_period(priv, 30 /* sec */);
1979 				wsm_set_bss_params(priv, &priv->bss_params);
1980 				priv->setbssparams_done = true;
1981 				cw1200_set_beacon_wakeup_period_work(&priv->set_beacon_wakeup_period_work);
1982 				cw1200_set_pm(priv, &priv->powersave_mode);
1983 			}
1984 			if (priv->vif->p2p) {
1985 				pr_debug("[STA] Setting p2p powersave configuration.\n");
1986 				wsm_set_p2p_ps_modeinfo(priv,
1987 							&priv->p2p_ps_modeinfo);
1988 			}
1989 			if (priv->bt_present)
1990 				cw1200_set_btcoexinfo(priv);
1991 		} else {
1992 			memset(&priv->association_mode, 0,
1993 			       sizeof(priv->association_mode));
1994 			memset(&priv->bss_params, 0, sizeof(priv->bss_params));
1995 		}
1996 	}
1997 
1998 	/* ERP Protection */
1999 	if (changed & (BSS_CHANGED_ASSOC |
2000 		       BSS_CHANGED_ERP_CTS_PROT |
2001 		       BSS_CHANGED_ERP_PREAMBLE)) {
2002 		u32 prev_erp_info = priv->erp_info;
2003 		if (info->use_cts_prot)
2004 			priv->erp_info |= WLAN_ERP_USE_PROTECTION;
2005 		else if (!(prev_erp_info & WLAN_ERP_NON_ERP_PRESENT))
2006 			priv->erp_info &= ~WLAN_ERP_USE_PROTECTION;
2007 
2008 		if (info->use_short_preamble)
2009 			priv->erp_info |= WLAN_ERP_BARKER_PREAMBLE;
2010 		else
2011 			priv->erp_info &= ~WLAN_ERP_BARKER_PREAMBLE;
2012 
2013 		pr_debug("[STA] ERP Protection: %x\n", priv->erp_info);
2014 
2015 		if (prev_erp_info != priv->erp_info)
2016 			queue_work(priv->workqueue, &priv->set_cts_work);
2017 	}
2018 
2019 	/* ERP Slottime */
2020 	if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_SLOT)) {
2021 		__le32 slot_time = info->use_short_slot ?
2022 			__cpu_to_le32(9) : __cpu_to_le32(20);
2023 		pr_debug("[STA] Slot time: %d us.\n",
2024 			 __le32_to_cpu(slot_time));
2025 		wsm_write_mib(priv, WSM_MIB_ID_DOT11_SLOT_TIME,
2026 			      &slot_time, sizeof(slot_time));
2027 	}
2028 
2029 	if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_CQM)) {
2030 		struct wsm_rcpi_rssi_threshold threshold = {
2031 			.rollingAverageCount = 8,
2032 		};
2033 		pr_debug("[CQM] RSSI threshold subscribe: %d +- %d\n",
2034 			 info->cqm_rssi_thold, info->cqm_rssi_hyst);
2035 		priv->cqm_rssi_thold = info->cqm_rssi_thold;
2036 		priv->cqm_rssi_hyst = info->cqm_rssi_hyst;
2037 
2038 		if (info->cqm_rssi_thold || info->cqm_rssi_hyst) {
2039 			/* RSSI subscription enabled */
2040 			/* TODO: It's not a correct way of setting threshold.
2041 			 * Upper and lower must be set equal here and adjusted
2042 			 * in callback. However current implementation is much
2043 			 * more relaible and stable.
2044 			 */
2045 
2046 			/* RSSI: signed Q8.0, RCPI: unsigned Q7.1
2047 			 * RSSI = RCPI / 2 - 110
2048 			 */
2049 			if (priv->cqm_use_rssi) {
2050 				threshold.upperThreshold =
2051 					info->cqm_rssi_thold + info->cqm_rssi_hyst;
2052 				threshold.lowerThreshold =
2053 					info->cqm_rssi_thold;
2054 				threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
2055 			} else {
2056 				threshold.upperThreshold = (info->cqm_rssi_thold + info->cqm_rssi_hyst + 110) * 2;
2057 				threshold.lowerThreshold = (info->cqm_rssi_thold + 110) * 2;
2058 			}
2059 			threshold.rssiRcpiMode |= WSM_RCPI_RSSI_THRESHOLD_ENABLE;
2060 		} else {
2061 			/* There is a bug in FW, see sta.c. We have to enable
2062 			 * dummy subscription to get correct RSSI values.
2063 			 */
2064 			threshold.rssiRcpiMode |=
2065 				WSM_RCPI_RSSI_THRESHOLD_ENABLE |
2066 				WSM_RCPI_RSSI_DONT_USE_UPPER |
2067 				WSM_RCPI_RSSI_DONT_USE_LOWER;
2068 			if (priv->cqm_use_rssi)
2069 				threshold.rssiRcpiMode |= WSM_RCPI_RSSI_USE_RSSI;
2070 		}
2071 		wsm_set_rcpi_rssi_threshold(priv, &threshold);
2072 	}
2073 	mutex_unlock(&priv->conf_mutex);
2074 
2075 	if (do_join) {
2076 		wsm_lock_tx(priv);
2077 		cw1200_do_join(priv); /* Will unlock it for us */
2078 	}
2079 }
2080 
cw1200_multicast_start_work(struct work_struct * work)2081 void cw1200_multicast_start_work(struct work_struct *work)
2082 {
2083 	struct cw1200_common *priv =
2084 		container_of(work, struct cw1200_common, multicast_start_work);
2085 	long tmo = priv->join_dtim_period *
2086 			(priv->beacon_int + 20) * HZ / 1024;
2087 
2088 	cancel_work_sync(&priv->multicast_stop_work);
2089 
2090 	if (!priv->aid0_bit_set) {
2091 		wsm_lock_tx(priv);
2092 		cw1200_set_tim_impl(priv, true);
2093 		priv->aid0_bit_set = true;
2094 		mod_timer(&priv->mcast_timeout, jiffies + tmo);
2095 		wsm_unlock_tx(priv);
2096 	}
2097 }
2098 
cw1200_multicast_stop_work(struct work_struct * work)2099 void cw1200_multicast_stop_work(struct work_struct *work)
2100 {
2101 	struct cw1200_common *priv =
2102 		container_of(work, struct cw1200_common, multicast_stop_work);
2103 
2104 	if (priv->aid0_bit_set) {
2105 		del_timer_sync(&priv->mcast_timeout);
2106 		wsm_lock_tx(priv);
2107 		priv->aid0_bit_set = false;
2108 		cw1200_set_tim_impl(priv, false);
2109 		wsm_unlock_tx(priv);
2110 	}
2111 }
2112 
cw1200_mcast_timeout(struct timer_list * t)2113 void cw1200_mcast_timeout(struct timer_list *t)
2114 {
2115 	struct cw1200_common *priv = from_timer(priv, t, mcast_timeout);
2116 
2117 	wiphy_warn(priv->hw->wiphy,
2118 		   "Multicast delivery timeout.\n");
2119 	spin_lock_bh(&priv->ps_state_lock);
2120 	priv->tx_multicast = priv->aid0_bit_set &&
2121 			priv->buffered_multicasts;
2122 	if (priv->tx_multicast)
2123 		cw1200_bh_wakeup(priv);
2124 	spin_unlock_bh(&priv->ps_state_lock);
2125 }
2126 
cw1200_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)2127 int cw1200_ampdu_action(struct ieee80211_hw *hw,
2128 			struct ieee80211_vif *vif,
2129 			struct ieee80211_ampdu_params *params)
2130 {
2131 	/* Aggregation is implemented fully in firmware,
2132 	 * including block ack negotiation. Do not allow
2133 	 * mac80211 stack to do anything: it interferes with
2134 	 * the firmware.
2135 	 */
2136 
2137 	/* Note that we still need this function stubbed. */
2138 	return -ENOTSUPP;
2139 }
2140 
2141 /* ******************************************************************** */
2142 /* WSM callback								*/
cw1200_suspend_resume(struct cw1200_common * priv,struct wsm_suspend_resume * arg)2143 void cw1200_suspend_resume(struct cw1200_common *priv,
2144 			  struct wsm_suspend_resume *arg)
2145 {
2146 	pr_debug("[AP] %s: %s\n",
2147 		 arg->stop ? "stop" : "start",
2148 		 arg->multicast ? "broadcast" : "unicast");
2149 
2150 	if (arg->multicast) {
2151 		bool cancel_tmo = false;
2152 		spin_lock_bh(&priv->ps_state_lock);
2153 		if (arg->stop) {
2154 			priv->tx_multicast = false;
2155 		} else {
2156 			/* Firmware sends this indication every DTIM if there
2157 			 * is a STA in powersave connected. There is no reason
2158 			 * to suspend, following wakeup will consume much more
2159 			 * power than it could be saved.
2160 			 */
2161 			cw1200_pm_stay_awake(&priv->pm_state,
2162 					     priv->join_dtim_period *
2163 					     (priv->beacon_int + 20) * HZ / 1024);
2164 			priv->tx_multicast = (priv->aid0_bit_set &&
2165 					      priv->buffered_multicasts);
2166 			if (priv->tx_multicast) {
2167 				cancel_tmo = true;
2168 				cw1200_bh_wakeup(priv);
2169 			}
2170 		}
2171 		spin_unlock_bh(&priv->ps_state_lock);
2172 		if (cancel_tmo)
2173 			del_timer_sync(&priv->mcast_timeout);
2174 	} else {
2175 		spin_lock_bh(&priv->ps_state_lock);
2176 		cw1200_ps_notify(priv, arg->link_id, arg->stop);
2177 		spin_unlock_bh(&priv->ps_state_lock);
2178 		if (!arg->stop)
2179 			cw1200_bh_wakeup(priv);
2180 	}
2181 	return;
2182 }
2183 
2184 /* ******************************************************************** */
2185 /* AP privates								*/
2186 
cw1200_upload_beacon(struct cw1200_common * priv)2187 static int cw1200_upload_beacon(struct cw1200_common *priv)
2188 {
2189 	int ret = 0;
2190 	struct ieee80211_mgmt *mgmt;
2191 	struct wsm_template_frame frame = {
2192 		.frame_type = WSM_FRAME_TYPE_BEACON,
2193 	};
2194 
2195 	u16 tim_offset;
2196 	u16 tim_len;
2197 
2198 	if (priv->mode == NL80211_IFTYPE_STATION ||
2199 	    priv->mode == NL80211_IFTYPE_MONITOR ||
2200 	    priv->mode == NL80211_IFTYPE_UNSPECIFIED)
2201 		goto done;
2202 
2203 	if (priv->vif->p2p)
2204 		frame.rate = WSM_TRANSMIT_RATE_6;
2205 
2206 	frame.skb = ieee80211_beacon_get_tim(priv->hw, priv->vif,
2207 					     &tim_offset, &tim_len, 0);
2208 	if (!frame.skb)
2209 		return -ENOMEM;
2210 
2211 	ret = wsm_set_template_frame(priv, &frame);
2212 
2213 	if (ret)
2214 		goto done;
2215 
2216 	/* TODO: Distill probe resp; remove TIM
2217 	 * and any other beacon-specific IEs
2218 	 */
2219 	mgmt = (void *)frame.skb->data;
2220 	mgmt->frame_control =
2221 		__cpu_to_le16(IEEE80211_FTYPE_MGMT |
2222 			      IEEE80211_STYPE_PROBE_RESP);
2223 
2224 	frame.frame_type = WSM_FRAME_TYPE_PROBE_RESPONSE;
2225 	if (priv->vif->p2p) {
2226 		ret = wsm_set_probe_responder(priv, true);
2227 	} else {
2228 		ret = wsm_set_template_frame(priv, &frame);
2229 		wsm_set_probe_responder(priv, false);
2230 	}
2231 
2232 done:
2233 	dev_kfree_skb(frame.skb);
2234 
2235 	return ret;
2236 }
2237 
cw1200_upload_pspoll(struct cw1200_common * priv)2238 static int cw1200_upload_pspoll(struct cw1200_common *priv)
2239 {
2240 	int ret = 0;
2241 	struct wsm_template_frame frame = {
2242 		.frame_type = WSM_FRAME_TYPE_PS_POLL,
2243 		.rate = 0xFF,
2244 	};
2245 
2246 
2247 	frame.skb = ieee80211_pspoll_get(priv->hw, priv->vif);
2248 	if (!frame.skb)
2249 		return -ENOMEM;
2250 
2251 	ret = wsm_set_template_frame(priv, &frame);
2252 
2253 	dev_kfree_skb(frame.skb);
2254 
2255 	return ret;
2256 }
2257 
cw1200_upload_null(struct cw1200_common * priv)2258 static int cw1200_upload_null(struct cw1200_common *priv)
2259 {
2260 	int ret = 0;
2261 	struct wsm_template_frame frame = {
2262 		.frame_type = WSM_FRAME_TYPE_NULL,
2263 		.rate = 0xFF,
2264 	};
2265 
2266 	frame.skb = ieee80211_nullfunc_get(priv->hw, priv->vif,-1, false);
2267 	if (!frame.skb)
2268 		return -ENOMEM;
2269 
2270 	ret = wsm_set_template_frame(priv, &frame);
2271 
2272 	dev_kfree_skb(frame.skb);
2273 
2274 	return ret;
2275 }
2276 
cw1200_upload_qosnull(struct cw1200_common * priv)2277 static int cw1200_upload_qosnull(struct cw1200_common *priv)
2278 {
2279 	/* TODO:  This needs to be implemented
2280 
2281 	struct wsm_template_frame frame = {
2282 		.frame_type = WSM_FRAME_TYPE_QOS_NULL,
2283 		.rate = 0xFF,
2284 	};
2285 
2286 	frame.skb = ieee80211_qosnullfunc_get(priv->hw, priv->vif);
2287 	if (!frame.skb)
2288 		return -ENOMEM;
2289 
2290 	ret = wsm_set_template_frame(priv, &frame);
2291 
2292 	dev_kfree_skb(frame.skb);
2293 
2294 	*/
2295 	return 0;
2296 }
2297 
cw1200_enable_beaconing(struct cw1200_common * priv,bool enable)2298 static int cw1200_enable_beaconing(struct cw1200_common *priv,
2299 				   bool enable)
2300 {
2301 	struct wsm_beacon_transmit transmit = {
2302 		.enable_beaconing = enable,
2303 	};
2304 
2305 	return wsm_beacon_transmit(priv, &transmit);
2306 }
2307 
cw1200_start_ap(struct cw1200_common * priv)2308 static int cw1200_start_ap(struct cw1200_common *priv)
2309 {
2310 	int ret;
2311 	struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
2312 	struct wsm_start start = {
2313 		.mode = priv->vif->p2p ?
2314 				WSM_START_MODE_P2P_GO : WSM_START_MODE_AP,
2315 		.band = (priv->channel->band == NL80211_BAND_5GHZ) ?
2316 				WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G,
2317 		.channel_number = priv->channel->hw_value,
2318 		.beacon_interval = conf->beacon_int,
2319 		.dtim_period = conf->dtim_period,
2320 		.preamble = conf->use_short_preamble ?
2321 				WSM_JOIN_PREAMBLE_SHORT :
2322 				WSM_JOIN_PREAMBLE_LONG,
2323 		.probe_delay = 100,
2324 		.basic_rate_set = cw1200_rate_mask_to_wsm(priv,
2325 				conf->basic_rates),
2326 	};
2327 	struct wsm_operational_mode mode = {
2328 		.power_mode = cw1200_power_mode,
2329 		.disable_more_flag_usage = true,
2330 	};
2331 
2332 	memset(start.ssid, 0, sizeof(start.ssid));
2333 	if (!conf->hidden_ssid) {
2334 		start.ssid_len = priv->vif->cfg.ssid_len;
2335 		memcpy(start.ssid, priv->vif->cfg.ssid, start.ssid_len);
2336 	}
2337 
2338 	priv->beacon_int = conf->beacon_int;
2339 	priv->join_dtim_period = conf->dtim_period;
2340 
2341 	memset(&priv->link_id_db, 0, sizeof(priv->link_id_db));
2342 
2343 	pr_debug("[AP] ch: %d(%d), bcn: %d(%d), brt: 0x%.8X, ssid: %.*s.\n",
2344 		 start.channel_number, start.band,
2345 		 start.beacon_interval, start.dtim_period,
2346 		 start.basic_rate_set,
2347 		 start.ssid_len, start.ssid);
2348 	ret = wsm_start(priv, &start);
2349 	if (!ret)
2350 		ret = cw1200_upload_keys(priv);
2351 	if (!ret && priv->vif->p2p) {
2352 		pr_debug("[AP] Setting p2p powersave configuration.\n");
2353 		wsm_set_p2p_ps_modeinfo(priv, &priv->p2p_ps_modeinfo);
2354 	}
2355 	if (!ret) {
2356 		wsm_set_block_ack_policy(priv, 0, 0);
2357 		priv->join_status = CW1200_JOIN_STATUS_AP;
2358 		cw1200_update_filtering(priv);
2359 	}
2360 	wsm_set_operational_mode(priv, &mode);
2361 	return ret;
2362 }
2363 
cw1200_update_beaconing(struct cw1200_common * priv)2364 static int cw1200_update_beaconing(struct cw1200_common *priv)
2365 {
2366 	struct ieee80211_bss_conf *conf = &priv->vif->bss_conf;
2367 	struct wsm_reset reset = {
2368 		.link_id = 0,
2369 		.reset_statistics = true,
2370 	};
2371 
2372 	if (priv->mode == NL80211_IFTYPE_AP) {
2373 		/* TODO: check if changed channel, band */
2374 		if (priv->join_status != CW1200_JOIN_STATUS_AP ||
2375 		    priv->beacon_int != conf->beacon_int) {
2376 			pr_debug("ap restarting\n");
2377 			wsm_lock_tx(priv);
2378 			if (priv->join_status != CW1200_JOIN_STATUS_PASSIVE)
2379 				wsm_reset(priv, &reset);
2380 			priv->join_status = CW1200_JOIN_STATUS_PASSIVE;
2381 			cw1200_start_ap(priv);
2382 			wsm_unlock_tx(priv);
2383 		} else
2384 			pr_debug("ap started join_status: %d\n",
2385 				 priv->join_status);
2386 	}
2387 	return 0;
2388 }
2389