1 /**
2  * Copyright (c) 2014 Redpine Signals Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/etherdevice.h>
18 #include "rsi_debugfs.h"
19 #include "rsi_mgmt.h"
20 #include "rsi_sdio.h"
21 #include "rsi_common.h"
22 #include "rsi_ps.h"
23 
24 static const struct ieee80211_channel rsi_2ghz_channels[] = {
25 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2412,
26 	  .hw_value = 1 }, /* Channel 1 */
27 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2417,
28 	  .hw_value = 2 }, /* Channel 2 */
29 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2422,
30 	  .hw_value = 3 }, /* Channel 3 */
31 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2427,
32 	  .hw_value = 4 }, /* Channel 4 */
33 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2432,
34 	  .hw_value = 5 }, /* Channel 5 */
35 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2437,
36 	  .hw_value = 6 }, /* Channel 6 */
37 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2442,
38 	  .hw_value = 7 }, /* Channel 7 */
39 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2447,
40 	  .hw_value = 8 }, /* Channel 8 */
41 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2452,
42 	  .hw_value = 9 }, /* Channel 9 */
43 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2457,
44 	  .hw_value = 10 }, /* Channel 10 */
45 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2462,
46 	  .hw_value = 11 }, /* Channel 11 */
47 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2467,
48 	  .hw_value = 12 }, /* Channel 12 */
49 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2472,
50 	  .hw_value = 13 }, /* Channel 13 */
51 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2484,
52 	  .hw_value = 14 }, /* Channel 14 */
53 };
54 
55 static const struct ieee80211_channel rsi_5ghz_channels[] = {
56 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5180,
57 	  .hw_value = 36,  }, /* Channel 36 */
58 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5200,
59 	  .hw_value = 40, }, /* Channel 40 */
60 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5220,
61 	  .hw_value = 44, }, /* Channel 44 */
62 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5240,
63 	  .hw_value = 48, }, /* Channel 48 */
64 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5260,
65 	  .hw_value = 52, }, /* Channel 52 */
66 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5280,
67 	  .hw_value = 56, }, /* Channel 56 */
68 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5300,
69 	  .hw_value = 60, }, /* Channel 60 */
70 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5320,
71 	  .hw_value = 64, }, /* Channel 64 */
72 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5500,
73 	  .hw_value = 100, }, /* Channel 100 */
74 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5520,
75 	  .hw_value = 104, }, /* Channel 104 */
76 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5540,
77 	  .hw_value = 108, }, /* Channel 108 */
78 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5560,
79 	  .hw_value = 112, }, /* Channel 112 */
80 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5580,
81 	  .hw_value = 116, }, /* Channel 116 */
82 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5600,
83 	  .hw_value = 120, }, /* Channel 120 */
84 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5620,
85 	  .hw_value = 124, }, /* Channel 124 */
86 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5640,
87 	  .hw_value = 128, }, /* Channel 128 */
88 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5660,
89 	  .hw_value = 132, }, /* Channel 132 */
90 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5680,
91 	  .hw_value = 136, }, /* Channel 136 */
92 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5700,
93 	  .hw_value = 140, }, /* Channel 140 */
94 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5745,
95 	  .hw_value = 149, }, /* Channel 149 */
96 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5765,
97 	  .hw_value = 153, }, /* Channel 153 */
98 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5785,
99 	  .hw_value = 157, }, /* Channel 157 */
100 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5805,
101 	  .hw_value = 161, }, /* Channel 161 */
102 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5825,
103 	  .hw_value = 165, }, /* Channel 165 */
104 };
105 
106 struct ieee80211_rate rsi_rates[12] = {
107 	{ .bitrate = STD_RATE_01  * 5, .hw_value = RSI_RATE_1 },
108 	{ .bitrate = STD_RATE_02  * 5, .hw_value = RSI_RATE_2 },
109 	{ .bitrate = STD_RATE_5_5 * 5, .hw_value = RSI_RATE_5_5 },
110 	{ .bitrate = STD_RATE_11  * 5, .hw_value = RSI_RATE_11 },
111 	{ .bitrate = STD_RATE_06  * 5, .hw_value = RSI_RATE_6 },
112 	{ .bitrate = STD_RATE_09  * 5, .hw_value = RSI_RATE_9 },
113 	{ .bitrate = STD_RATE_12  * 5, .hw_value = RSI_RATE_12 },
114 	{ .bitrate = STD_RATE_18  * 5, .hw_value = RSI_RATE_18 },
115 	{ .bitrate = STD_RATE_24  * 5, .hw_value = RSI_RATE_24 },
116 	{ .bitrate = STD_RATE_36  * 5, .hw_value = RSI_RATE_36 },
117 	{ .bitrate = STD_RATE_48  * 5, .hw_value = RSI_RATE_48 },
118 	{ .bitrate = STD_RATE_54  * 5, .hw_value = RSI_RATE_54 },
119 };
120 
121 const u16 rsi_mcsrates[8] = {
122 	RSI_RATE_MCS0, RSI_RATE_MCS1, RSI_RATE_MCS2, RSI_RATE_MCS3,
123 	RSI_RATE_MCS4, RSI_RATE_MCS5, RSI_RATE_MCS6, RSI_RATE_MCS7
124 };
125 
126 static const u32 rsi_max_ap_stas[16] = {
127 	32,	/* 1 - Wi-Fi alone */
128 	0,	/* 2 */
129 	0,	/* 3 */
130 	0,	/* 4 - BT EDR alone */
131 	4,	/* 5 - STA + BT EDR */
132 	32,	/* 6 - AP + BT EDR */
133 	0,	/* 7 */
134 	0,	/* 8 - BT LE alone */
135 	4,	/* 9 - STA + BE LE */
136 	0,	/* 10 */
137 	0,	/* 11 */
138 	0,	/* 12 */
139 	1,	/* 13 - STA + BT Dual */
140 	4,	/* 14 - AP + BT Dual */
141 };
142 
143 static const struct ieee80211_iface_limit rsi_iface_limits[] = {
144 	{
145 		.max = 1,
146 		.types = BIT(NL80211_IFTYPE_STATION),
147 	},
148 	{
149 		.max = 1,
150 		.types = BIT(NL80211_IFTYPE_AP) |
151 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
152 			BIT(NL80211_IFTYPE_P2P_GO),
153 	},
154 	{
155 		.max = 1,
156 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
157 	},
158 };
159 
160 static const struct ieee80211_iface_combination rsi_iface_combinations[] = {
161 	{
162 		.num_different_channels = 1,
163 		.max_interfaces = 3,
164 		.limits = rsi_iface_limits,
165 		.n_limits = ARRAY_SIZE(rsi_iface_limits),
166 	},
167 };
168 
169 /**
170  * rsi_is_cipher_wep() -  This function determines if the cipher is WEP or not.
171  * @common: Pointer to the driver private structure.
172  *
173  * Return: If cipher type is WEP, a value of 1 is returned, else 0.
174  */
175 
176 bool rsi_is_cipher_wep(struct rsi_common *common)
177 {
178 	if (((common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP104) ||
179 	     (common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP40)) &&
180 	    (!common->secinfo.ptk_cipher))
181 		return true;
182 	else
183 		return false;
184 }
185 
186 /**
187  * rsi_register_rates_channels() - This function registers channels and rates.
188  * @adapter: Pointer to the adapter structure.
189  * @band: Operating band to be set.
190  *
191  * Return: None.
192  */
193 static void rsi_register_rates_channels(struct rsi_hw *adapter, int band)
194 {
195 	struct ieee80211_supported_band *sbands = &adapter->sbands[band];
196 	void *channels = NULL;
197 
198 	if (band == NL80211_BAND_2GHZ) {
199 		channels = kmalloc(sizeof(rsi_2ghz_channels), GFP_KERNEL);
200 		memcpy(channels,
201 		       rsi_2ghz_channels,
202 		       sizeof(rsi_2ghz_channels));
203 		sbands->band = NL80211_BAND_2GHZ;
204 		sbands->n_channels = ARRAY_SIZE(rsi_2ghz_channels);
205 		sbands->bitrates = rsi_rates;
206 		sbands->n_bitrates = ARRAY_SIZE(rsi_rates);
207 	} else {
208 		channels = kmalloc(sizeof(rsi_5ghz_channels), GFP_KERNEL);
209 		memcpy(channels,
210 		       rsi_5ghz_channels,
211 		       sizeof(rsi_5ghz_channels));
212 		sbands->band = NL80211_BAND_5GHZ;
213 		sbands->n_channels = ARRAY_SIZE(rsi_5ghz_channels);
214 		sbands->bitrates = &rsi_rates[4];
215 		sbands->n_bitrates = ARRAY_SIZE(rsi_rates) - 4;
216 	}
217 
218 	sbands->channels = channels;
219 
220 	memset(&sbands->ht_cap, 0, sizeof(struct ieee80211_sta_ht_cap));
221 	sbands->ht_cap.ht_supported = true;
222 	sbands->ht_cap.cap = (IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
223 			      IEEE80211_HT_CAP_SGI_20 |
224 			      IEEE80211_HT_CAP_SGI_40);
225 	sbands->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K;
226 	sbands->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
227 	sbands->ht_cap.mcs.rx_mask[0] = 0xff;
228 	sbands->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
229 	/* sbands->ht_cap.mcs.rx_highest = 0x82; */
230 }
231 
232 /**
233  * rsi_mac80211_detach() - This function is used to de-initialize the
234  *			   Mac80211 stack.
235  * @adapter: Pointer to the adapter structure.
236  *
237  * Return: None.
238  */
239 void rsi_mac80211_detach(struct rsi_hw *adapter)
240 {
241 	struct ieee80211_hw *hw = adapter->hw;
242 	enum nl80211_band band;
243 
244 	if (hw) {
245 		ieee80211_stop_queues(hw);
246 		ieee80211_unregister_hw(hw);
247 		ieee80211_free_hw(hw);
248 	}
249 
250 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
251 		struct ieee80211_supported_band *sband =
252 					&adapter->sbands[band];
253 
254 		kfree(sband->channels);
255 	}
256 
257 #ifdef CONFIG_RSI_DEBUGFS
258 	rsi_remove_dbgfs(adapter);
259 	kfree(adapter->dfsentry);
260 #endif
261 }
262 EXPORT_SYMBOL_GPL(rsi_mac80211_detach);
263 
264 /**
265  * rsi_indicate_tx_status() - This function indicates the transmit status.
266  * @adapter: Pointer to the adapter structure.
267  * @skb: Pointer to the socket buffer structure.
268  * @status: Status
269  *
270  * Return: None.
271  */
272 void rsi_indicate_tx_status(struct rsi_hw *adapter,
273 			    struct sk_buff *skb,
274 			    int status)
275 {
276 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
277 	struct skb_info *tx_params;
278 
279 	if (!adapter->hw) {
280 		rsi_dbg(ERR_ZONE, "##### No MAC #####\n");
281 		return;
282 	}
283 
284 	if (!status)
285 		info->flags |= IEEE80211_TX_STAT_ACK;
286 
287 	tx_params = (struct skb_info *)info->driver_data;
288 	skb_pull(skb, tx_params->internal_hdr_size);
289 	memset(info->driver_data, 0, IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
290 
291 	ieee80211_tx_status_irqsafe(adapter->hw, skb);
292 }
293 
294 /**
295  * rsi_mac80211_tx() - This is the handler that 802.11 module calls for each
296  *		       transmitted frame.SKB contains the buffer starting
297  *		       from the IEEE 802.11 header.
298  * @hw: Pointer to the ieee80211_hw structure.
299  * @control: Pointer to the ieee80211_tx_control structure
300  * @skb: Pointer to the socket buffer structure.
301  *
302  * Return: None
303  */
304 static void rsi_mac80211_tx(struct ieee80211_hw *hw,
305 			    struct ieee80211_tx_control *control,
306 			    struct sk_buff *skb)
307 {
308 	struct rsi_hw *adapter = hw->priv;
309 	struct rsi_common *common = adapter->priv;
310 
311 	rsi_core_xmit(common, skb);
312 }
313 
314 /**
315  * rsi_mac80211_start() - This is first handler that 802.11 module calls, since
316  *			  the driver init is complete by then, just
317  *			  returns success.
318  * @hw: Pointer to the ieee80211_hw structure.
319  *
320  * Return: 0 as success.
321  */
322 static int rsi_mac80211_start(struct ieee80211_hw *hw)
323 {
324 	struct rsi_hw *adapter = hw->priv;
325 	struct rsi_common *common = adapter->priv;
326 
327 	rsi_dbg(ERR_ZONE, "===> Interface UP <===\n");
328 	mutex_lock(&common->mutex);
329 	if (common->hibernate_resume) {
330 		common->reinit_hw = true;
331 		adapter->host_intf_ops->reinit_device(adapter);
332 		wait_for_completion(&adapter->priv->wlan_init_completion);
333 	}
334 	common->iface_down = false;
335 	wiphy_rfkill_start_polling(hw->wiphy);
336 	rsi_send_rx_filter_frame(common, 0);
337 	mutex_unlock(&common->mutex);
338 
339 	return 0;
340 }
341 
342 /**
343  * rsi_mac80211_stop() - This is the last handler that 802.11 module calls.
344  * @hw: Pointer to the ieee80211_hw structure.
345  *
346  * Return: None.
347  */
348 static void rsi_mac80211_stop(struct ieee80211_hw *hw)
349 {
350 	struct rsi_hw *adapter = hw->priv;
351 	struct rsi_common *common = adapter->priv;
352 
353 	rsi_dbg(ERR_ZONE, "===> Interface DOWN <===\n");
354 	mutex_lock(&common->mutex);
355 	common->iface_down = true;
356 	wiphy_rfkill_stop_polling(hw->wiphy);
357 
358 	/* Block all rx frames */
359 	rsi_send_rx_filter_frame(common, 0xffff);
360 
361 	mutex_unlock(&common->mutex);
362 }
363 
364 static int rsi_map_intf_mode(enum nl80211_iftype vif_type)
365 {
366 	switch (vif_type) {
367 	case NL80211_IFTYPE_STATION:
368 		return RSI_OPMODE_STA;
369 	case NL80211_IFTYPE_AP:
370 		return RSI_OPMODE_AP;
371 	case NL80211_IFTYPE_P2P_DEVICE:
372 		return RSI_OPMODE_P2P_CLIENT;
373 	case NL80211_IFTYPE_P2P_CLIENT:
374 		return RSI_OPMODE_P2P_CLIENT;
375 	case NL80211_IFTYPE_P2P_GO:
376 		return RSI_OPMODE_P2P_GO;
377 	default:
378 		return RSI_OPMODE_UNSUPPORTED;
379 	}
380 }
381 
382 /**
383  * rsi_mac80211_add_interface() - This function is called when a netdevice
384  *				  attached to the hardware is enabled.
385  * @hw: Pointer to the ieee80211_hw structure.
386  * @vif: Pointer to the ieee80211_vif structure.
387  *
388  * Return: ret: 0 on success, negative error code on failure.
389  */
390 static int rsi_mac80211_add_interface(struct ieee80211_hw *hw,
391 				      struct ieee80211_vif *vif)
392 {
393 	struct rsi_hw *adapter = hw->priv;
394 	struct rsi_common *common = adapter->priv;
395 	struct vif_priv *vif_info = (struct vif_priv *)vif->drv_priv;
396 	enum opmode intf_mode;
397 	enum vap_status vap_status;
398 	int vap_idx = -1, i;
399 
400 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
401 	mutex_lock(&common->mutex);
402 
403 	intf_mode = rsi_map_intf_mode(vif->type);
404 	if (intf_mode == RSI_OPMODE_UNSUPPORTED) {
405 		rsi_dbg(ERR_ZONE,
406 			"%s: Interface type %d not supported\n", __func__,
407 			vif->type);
408 		mutex_unlock(&common->mutex);
409 		return -EOPNOTSUPP;
410 	}
411 	if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) ||
412 	    (vif->type == NL80211_IFTYPE_P2P_CLIENT) ||
413 	    (vif->type == NL80211_IFTYPE_P2P_GO))
414 		common->p2p_enabled = true;
415 
416 	/* Get free vap index */
417 	for (i = 0; i < RSI_MAX_VIFS; i++) {
418 		if (!adapter->vifs[i]) {
419 			vap_idx = i;
420 			break;
421 		}
422 	}
423 	if (vap_idx < 0) {
424 		rsi_dbg(ERR_ZONE, "Reject: Max VAPs reached\n");
425 		mutex_unlock(&common->mutex);
426 		return -EOPNOTSUPP;
427 	}
428 	vif_info->vap_id = vap_idx;
429 	adapter->vifs[vap_idx] = vif;
430 	adapter->sc_nvifs++;
431 	vap_status = VAP_ADD;
432 
433 	if (rsi_set_vap_capabilities(common, intf_mode, vif->addr,
434 				     vif_info->vap_id, vap_status)) {
435 		rsi_dbg(ERR_ZONE, "Failed to set VAP capabilities\n");
436 		mutex_unlock(&common->mutex);
437 		return -EINVAL;
438 	}
439 
440 	if ((vif->type == NL80211_IFTYPE_AP) ||
441 	    (vif->type == NL80211_IFTYPE_P2P_GO)) {
442 		rsi_send_rx_filter_frame(common, DISALLOW_BEACONS);
443 		common->min_rate = RSI_RATE_AUTO;
444 		for (i = 0; i < common->max_stations; i++)
445 			common->stations[i].sta = NULL;
446 	}
447 
448 	mutex_unlock(&common->mutex);
449 
450 	return 0;
451 }
452 
453 /**
454  * rsi_mac80211_remove_interface() - This function notifies driver that an
455  *				     interface is going down.
456  * @hw: Pointer to the ieee80211_hw structure.
457  * @vif: Pointer to the ieee80211_vif structure.
458  *
459  * Return: None.
460  */
461 static void rsi_mac80211_remove_interface(struct ieee80211_hw *hw,
462 					  struct ieee80211_vif *vif)
463 {
464 	struct rsi_hw *adapter = hw->priv;
465 	struct rsi_common *common = adapter->priv;
466 	enum opmode opmode;
467 	int i;
468 
469 	rsi_dbg(INFO_ZONE, "Remove Interface Called\n");
470 
471 	mutex_lock(&common->mutex);
472 
473 	if (adapter->sc_nvifs <= 0) {
474 		mutex_unlock(&common->mutex);
475 		return;
476 	}
477 
478 	opmode = rsi_map_intf_mode(vif->type);
479 	if (opmode == RSI_OPMODE_UNSUPPORTED) {
480 		rsi_dbg(ERR_ZONE, "Opmode error : %d\n", opmode);
481 		mutex_unlock(&common->mutex);
482 		return;
483 	}
484 	for (i = 0; i < RSI_MAX_VIFS; i++) {
485 		if (!adapter->vifs[i])
486 			continue;
487 		if (vif == adapter->vifs[i]) {
488 			rsi_set_vap_capabilities(common, opmode, vif->addr,
489 						 i, VAP_DELETE);
490 			adapter->sc_nvifs--;
491 			adapter->vifs[i] = NULL;
492 		}
493 	}
494 	mutex_unlock(&common->mutex);
495 }
496 
497 /**
498  * rsi_channel_change() - This function is a performs the checks
499  *			  required for changing a channel and sets
500  *			  the channel accordingly.
501  * @hw: Pointer to the ieee80211_hw structure.
502  *
503  * Return: 0 on success, negative error code on failure.
504  */
505 static int rsi_channel_change(struct ieee80211_hw *hw)
506 {
507 	struct rsi_hw *adapter = hw->priv;
508 	struct rsi_common *common = adapter->priv;
509 	int status = -EOPNOTSUPP;
510 	struct ieee80211_channel *curchan = hw->conf.chandef.chan;
511 	u16 channel = curchan->hw_value;
512 	struct ieee80211_vif *vif;
513 	struct ieee80211_bss_conf *bss;
514 	bool assoc = false;
515 	int i;
516 
517 	rsi_dbg(INFO_ZONE,
518 		"%s: Set channel: %d MHz type: %d channel_no %d\n",
519 		__func__, curchan->center_freq,
520 		curchan->flags, channel);
521 
522 	for (i = 0; i < RSI_MAX_VIFS; i++) {
523 		vif = adapter->vifs[i];
524 		if (!vif)
525 			continue;
526 		if (vif->type == NL80211_IFTYPE_STATION) {
527 			bss = &vif->bss_conf;
528 			if (bss->assoc) {
529 				assoc = true;
530 				break;
531 			}
532 		}
533 	}
534 	if (assoc) {
535 		if (!common->hw_data_qs_blocked &&
536 		    (rsi_get_connected_channel(vif) != channel)) {
537 			rsi_dbg(INFO_ZONE, "blk data q %d\n", channel);
538 			if (!rsi_send_block_unblock_frame(common, true))
539 				common->hw_data_qs_blocked = true;
540 		}
541 	}
542 
543 	status = rsi_band_check(common, curchan);
544 	if (!status)
545 		status = rsi_set_channel(adapter->priv, curchan);
546 
547 	if (assoc) {
548 		if (common->hw_data_qs_blocked &&
549 		    (rsi_get_connected_channel(vif) == channel)) {
550 			rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
551 			if (!rsi_send_block_unblock_frame(common, false))
552 				common->hw_data_qs_blocked = false;
553 		}
554 	}
555 
556 	return status;
557 }
558 
559 /**
560  * rsi_config_power() - This function configures tx power to device
561  * @hw: Pointer to the ieee80211_hw structure.
562  *
563  * Return: 0 on success, negative error code on failure.
564  */
565 static int rsi_config_power(struct ieee80211_hw *hw)
566 {
567 	struct rsi_hw *adapter = hw->priv;
568 	struct rsi_common *common = adapter->priv;
569 	struct ieee80211_conf *conf = &hw->conf;
570 
571 	if (adapter->sc_nvifs <= 0) {
572 		rsi_dbg(ERR_ZONE, "%s: No virtual interface found\n", __func__);
573 		return -EINVAL;
574 	}
575 
576 	rsi_dbg(INFO_ZONE,
577 		"%s: Set tx power: %d dBM\n", __func__, conf->power_level);
578 
579 	if (conf->power_level == common->tx_power)
580 		return 0;
581 
582 	common->tx_power = conf->power_level;
583 
584 	return rsi_send_radio_params_update(common);
585 }
586 
587 /**
588  * rsi_mac80211_config() - This function is a handler for configuration
589  *			   requests. The stack calls this function to
590  *			   change hardware configuration, e.g., channel.
591  * @hw: Pointer to the ieee80211_hw structure.
592  * @changed: Changed flags set.
593  *
594  * Return: 0 on success, negative error code on failure.
595  */
596 static int rsi_mac80211_config(struct ieee80211_hw *hw,
597 			       u32 changed)
598 {
599 	struct rsi_hw *adapter = hw->priv;
600 	struct rsi_common *common = adapter->priv;
601 	struct ieee80211_conf *conf = &hw->conf;
602 	int status = -EOPNOTSUPP;
603 
604 	mutex_lock(&common->mutex);
605 
606 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
607 		status = rsi_channel_change(hw);
608 
609 	/* tx power */
610 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
611 		rsi_dbg(INFO_ZONE, "%s: Configuring Power\n", __func__);
612 		status = rsi_config_power(hw);
613 	}
614 
615 	/* Power save parameters */
616 	if (changed & IEEE80211_CONF_CHANGE_PS) {
617 		struct ieee80211_vif *vif, *sta_vif = NULL;
618 		unsigned long flags;
619 		int i, set_ps = 1;
620 
621 		for (i = 0; i < RSI_MAX_VIFS; i++) {
622 			vif = adapter->vifs[i];
623 			if (!vif)
624 				continue;
625 			/* Don't go to power save if AP vap exists */
626 			if ((vif->type == NL80211_IFTYPE_AP) ||
627 			    (vif->type == NL80211_IFTYPE_P2P_GO)) {
628 				set_ps = 0;
629 				break;
630 			}
631 			if ((vif->type == NL80211_IFTYPE_STATION ||
632 			     vif->type == NL80211_IFTYPE_P2P_CLIENT) &&
633 			    (!sta_vif || vif->bss_conf.assoc))
634 				sta_vif = vif;
635 		}
636 		if (set_ps && sta_vif) {
637 			spin_lock_irqsave(&adapter->ps_lock, flags);
638 			if (conf->flags & IEEE80211_CONF_PS)
639 				rsi_enable_ps(adapter, sta_vif);
640 			else
641 				rsi_disable_ps(adapter, sta_vif);
642 			spin_unlock_irqrestore(&adapter->ps_lock, flags);
643 		}
644 	}
645 
646 	/* RTS threshold */
647 	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
648 		rsi_dbg(INFO_ZONE, "RTS threshold\n");
649 		if ((common->rts_threshold) <= IEEE80211_MAX_RTS_THRESHOLD) {
650 			rsi_dbg(INFO_ZONE,
651 				"%s: Sending vap updates....\n", __func__);
652 			status = rsi_send_vap_dynamic_update(common);
653 		}
654 	}
655 	mutex_unlock(&common->mutex);
656 
657 	return status;
658 }
659 
660 /**
661  * rsi_get_connected_channel() - This function is used to get the current
662  *				 connected channel number.
663  * @adapter: Pointer to the adapter structure.
664  *
665  * Return: Current connected AP's channel number is returned.
666  */
667 u16 rsi_get_connected_channel(struct ieee80211_vif *vif)
668 {
669 	struct ieee80211_bss_conf *bss;
670 	struct ieee80211_channel *channel;
671 
672 	if (!vif)
673 		return 0;
674 
675 	bss = &vif->bss_conf;
676 	channel = bss->chandef.chan;
677 
678 	if (!channel)
679 		return 0;
680 
681 	return channel->hw_value;
682 }
683 
684 static void rsi_switch_channel(struct rsi_hw *adapter,
685 			       struct ieee80211_vif *vif)
686 {
687 	struct rsi_common *common = adapter->priv;
688 	struct ieee80211_channel *channel;
689 
690 	if (common->iface_down)
691 		return;
692 	if (!vif)
693 		return;
694 
695 	channel = vif->bss_conf.chandef.chan;
696 
697 	if (!channel)
698 		return;
699 
700 	rsi_band_check(common, channel);
701 	rsi_set_channel(common, channel);
702 	rsi_dbg(INFO_ZONE, "Switched to channel - %d\n", channel->hw_value);
703 }
704 
705 /**
706  * rsi_mac80211_bss_info_changed() - This function is a handler for config
707  *				     requests related to BSS parameters that
708  *				     may vary during BSS's lifespan.
709  * @hw: Pointer to the ieee80211_hw structure.
710  * @vif: Pointer to the ieee80211_vif structure.
711  * @bss_conf: Pointer to the ieee80211_bss_conf structure.
712  * @changed: Changed flags set.
713  *
714  * Return: None.
715  */
716 static void rsi_mac80211_bss_info_changed(struct ieee80211_hw *hw,
717 					  struct ieee80211_vif *vif,
718 					  struct ieee80211_bss_conf *bss_conf,
719 					  u32 changed)
720 {
721 	struct rsi_hw *adapter = hw->priv;
722 	struct rsi_common *common = adapter->priv;
723 	struct ieee80211_bss_conf *bss = &vif->bss_conf;
724 	struct ieee80211_conf *conf = &hw->conf;
725 	u16 rx_filter_word = 0;
726 
727 	mutex_lock(&common->mutex);
728 	if (changed & BSS_CHANGED_ASSOC) {
729 		rsi_dbg(INFO_ZONE, "%s: Changed Association status: %d\n",
730 			__func__, bss_conf->assoc);
731 		if (bss_conf->assoc) {
732 			/* Send the RX filter frame */
733 			rx_filter_word = (ALLOW_DATA_ASSOC_PEER |
734 					  ALLOW_CTRL_ASSOC_PEER |
735 					  ALLOW_MGMT_ASSOC_PEER);
736 			rsi_send_rx_filter_frame(common, rx_filter_word);
737 		}
738 		rsi_inform_bss_status(common,
739 				      RSI_OPMODE_STA,
740 				      bss_conf->assoc,
741 				      bss_conf->bssid,
742 				      bss_conf->qos,
743 				      bss_conf->aid,
744 				      NULL, 0,
745 				      bss_conf->assoc_capability, vif);
746 		adapter->ps_info.dtim_interval_duration = bss->dtim_period;
747 		adapter->ps_info.listen_interval = conf->listen_interval;
748 
749 	/* If U-APSD is updated, send ps parameters to firmware */
750 	if (bss->assoc) {
751 		if (common->uapsd_bitmap) {
752 			rsi_dbg(INFO_ZONE, "Configuring UAPSD\n");
753 			rsi_conf_uapsd(adapter, vif);
754 		}
755 	} else {
756 		common->uapsd_bitmap = 0;
757 	}
758 	}
759 
760 	if (changed & BSS_CHANGED_CQM) {
761 		common->cqm_info.last_cqm_event_rssi = 0;
762 		common->cqm_info.rssi_thold = bss_conf->cqm_rssi_thold;
763 		common->cqm_info.rssi_hyst = bss_conf->cqm_rssi_hyst;
764 		rsi_dbg(INFO_ZONE, "RSSI throld & hysteresis are: %d %d\n",
765 			common->cqm_info.rssi_thold,
766 			common->cqm_info.rssi_hyst);
767 	}
768 
769 	if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
770 	    ((vif->type == NL80211_IFTYPE_AP) ||
771 	     (vif->type == NL80211_IFTYPE_P2P_GO))) {
772 		if (bss->enable_beacon) {
773 			rsi_dbg(INFO_ZONE, "===> BEACON ENABLED <===\n");
774 			common->beacon_enabled = 1;
775 		} else {
776 			rsi_dbg(INFO_ZONE, "===> BEACON DISABLED <===\n");
777 			common->beacon_enabled = 0;
778 		}
779 	}
780 
781 	mutex_unlock(&common->mutex);
782 }
783 
784 /**
785  * rsi_mac80211_conf_filter() - This function configure the device's RX filter.
786  * @hw: Pointer to the ieee80211_hw structure.
787  * @changed: Changed flags set.
788  * @total_flags: Total initial flags set.
789  * @multicast: Multicast.
790  *
791  * Return: None.
792  */
793 static void rsi_mac80211_conf_filter(struct ieee80211_hw *hw,
794 				     u32 changed_flags,
795 				     u32 *total_flags,
796 				     u64 multicast)
797 {
798 	/* Not doing much here as of now */
799 	*total_flags &= RSI_SUPP_FILTERS;
800 }
801 
802 /**
803  * rsi_mac80211_conf_tx() - This function configures TX queue parameters
804  *			    (EDCF (aifs, cw_min, cw_max), bursting)
805  *			    for a hardware TX queue.
806  * @hw: Pointer to the ieee80211_hw structure
807  * @vif: Pointer to the ieee80211_vif structure.
808  * @queue: Queue number.
809  * @params: Pointer to ieee80211_tx_queue_params structure.
810  *
811  * Return: 0 on success, negative error code on failure.
812  */
813 static int rsi_mac80211_conf_tx(struct ieee80211_hw *hw,
814 				struct ieee80211_vif *vif, u16 queue,
815 				const struct ieee80211_tx_queue_params *params)
816 {
817 	struct rsi_hw *adapter = hw->priv;
818 	struct rsi_common *common = adapter->priv;
819 	u8 idx = 0;
820 
821 	if (queue >= IEEE80211_NUM_ACS)
822 		return 0;
823 
824 	rsi_dbg(INFO_ZONE,
825 		"%s: Conf queue %d, aifs: %d, cwmin: %d cwmax: %d, txop: %d\n",
826 		__func__, queue, params->aifs,
827 		params->cw_min, params->cw_max, params->txop);
828 
829 	mutex_lock(&common->mutex);
830 	/* Map into the way the f/w expects */
831 	switch (queue) {
832 	case IEEE80211_AC_VO:
833 		idx = VO_Q;
834 		break;
835 	case IEEE80211_AC_VI:
836 		idx = VI_Q;
837 		break;
838 	case IEEE80211_AC_BE:
839 		idx = BE_Q;
840 		break;
841 	case IEEE80211_AC_BK:
842 		idx = BK_Q;
843 		break;
844 	default:
845 		idx = BE_Q;
846 		break;
847 	}
848 
849 	memcpy(&common->edca_params[idx],
850 	       params,
851 	       sizeof(struct ieee80211_tx_queue_params));
852 
853 	if (params->uapsd)
854 		common->uapsd_bitmap |= idx;
855 	else
856 		common->uapsd_bitmap &= (~idx);
857 
858 	mutex_unlock(&common->mutex);
859 
860 	return 0;
861 }
862 
863 /**
864  * rsi_hal_key_config() - This function loads the keys into the firmware.
865  * @hw: Pointer to the ieee80211_hw structure.
866  * @vif: Pointer to the ieee80211_vif structure.
867  * @key: Pointer to the ieee80211_key_conf structure.
868  *
869  * Return: status: 0 on success, negative error codes on failure.
870  */
871 static int rsi_hal_key_config(struct ieee80211_hw *hw,
872 			      struct ieee80211_vif *vif,
873 			      struct ieee80211_key_conf *key,
874 			      struct ieee80211_sta *sta)
875 {
876 	struct rsi_hw *adapter = hw->priv;
877 	struct rsi_sta *rsta = NULL;
878 	int status;
879 	u8 key_type;
880 	s16 sta_id = 0;
881 
882 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
883 		key_type = RSI_PAIRWISE_KEY;
884 	else
885 		key_type = RSI_GROUP_KEY;
886 
887 	rsi_dbg(ERR_ZONE, "%s: Cipher 0x%x key_type: %d key_len: %d\n",
888 		__func__, key->cipher, key_type, key->keylen);
889 
890 	if ((vif->type == NL80211_IFTYPE_AP) ||
891 	    (vif->type == NL80211_IFTYPE_P2P_GO)) {
892 		if (sta) {
893 			rsta = rsi_find_sta(adapter->priv, sta->addr);
894 			if (rsta)
895 				sta_id = rsta->sta_id;
896 		}
897 		adapter->priv->key = key;
898 	} else {
899 		if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
900 		    (key->cipher == WLAN_CIPHER_SUITE_WEP40)) {
901 			status = rsi_hal_load_key(adapter->priv,
902 						  key->key,
903 						  key->keylen,
904 						  RSI_PAIRWISE_KEY,
905 						  key->keyidx,
906 						  key->cipher,
907 						  sta_id,
908 						  vif);
909 			if (status)
910 				return status;
911 		}
912 	}
913 
914 	status = rsi_hal_load_key(adapter->priv,
915 				  key->key,
916 				  key->keylen,
917 				  key_type,
918 				  key->keyidx,
919 				  key->cipher,
920 				  sta_id,
921 				  vif);
922 	if (status)
923 		return status;
924 
925 	if (vif->type == NL80211_IFTYPE_STATION && key->key &&
926 	    (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
927 	     key->cipher == WLAN_CIPHER_SUITE_WEP40)) {
928 		if (!rsi_send_block_unblock_frame(adapter->priv, false))
929 			adapter->priv->hw_data_qs_blocked = false;
930 	}
931 
932 	return 0;
933 }
934 
935 /**
936  * rsi_mac80211_set_key() - This function sets type of key to be loaded.
937  * @hw: Pointer to the ieee80211_hw structure.
938  * @cmd: enum set_key_cmd.
939  * @vif: Pointer to the ieee80211_vif structure.
940  * @sta: Pointer to the ieee80211_sta structure.
941  * @key: Pointer to the ieee80211_key_conf structure.
942  *
943  * Return: status: 0 on success, negative error code on failure.
944  */
945 static int rsi_mac80211_set_key(struct ieee80211_hw *hw,
946 				enum set_key_cmd cmd,
947 				struct ieee80211_vif *vif,
948 				struct ieee80211_sta *sta,
949 				struct ieee80211_key_conf *key)
950 {
951 	struct rsi_hw *adapter = hw->priv;
952 	struct rsi_common *common = adapter->priv;
953 	struct security_info *secinfo = &common->secinfo;
954 	int status;
955 
956 	mutex_lock(&common->mutex);
957 	switch (cmd) {
958 	case SET_KEY:
959 		secinfo->security_enable = true;
960 		status = rsi_hal_key_config(hw, vif, key, sta);
961 		if (status) {
962 			mutex_unlock(&common->mutex);
963 			return status;
964 		}
965 
966 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
967 			secinfo->ptk_cipher = key->cipher;
968 		else
969 			secinfo->gtk_cipher = key->cipher;
970 
971 		key->hw_key_idx = key->keyidx;
972 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
973 
974 		rsi_dbg(ERR_ZONE, "%s: RSI set_key\n", __func__);
975 		break;
976 
977 	case DISABLE_KEY:
978 		if (vif->type == NL80211_IFTYPE_STATION)
979 			secinfo->security_enable = false;
980 		rsi_dbg(ERR_ZONE, "%s: RSI del key\n", __func__);
981 		memset(key, 0, sizeof(struct ieee80211_key_conf));
982 		status = rsi_hal_key_config(hw, vif, key, sta);
983 		break;
984 
985 	default:
986 		status = -EOPNOTSUPP;
987 		break;
988 	}
989 
990 	mutex_unlock(&common->mutex);
991 	return status;
992 }
993 
994 /**
995  * rsi_mac80211_ampdu_action() - This function selects the AMPDU action for
996  *				 the corresponding mlme_action flag and
997  *				 informs the f/w regarding this.
998  * @hw: Pointer to the ieee80211_hw structure.
999  * @vif: Pointer to the ieee80211_vif structure.
1000  * @params: Pointer to A-MPDU action parameters
1001  *
1002  * Return: status: 0 on success, negative error code on failure.
1003  */
1004 static int rsi_mac80211_ampdu_action(struct ieee80211_hw *hw,
1005 				     struct ieee80211_vif *vif,
1006 				     struct ieee80211_ampdu_params *params)
1007 {
1008 	int status = -EOPNOTSUPP;
1009 	struct rsi_hw *adapter = hw->priv;
1010 	struct rsi_common *common = adapter->priv;
1011 	struct rsi_sta *rsta = NULL;
1012 	u16 seq_no = 0, seq_start = 0;
1013 	u8 ii = 0;
1014 	struct ieee80211_sta *sta = params->sta;
1015 	u8 sta_id = 0;
1016 	enum ieee80211_ampdu_mlme_action action = params->action;
1017 	u16 tid = params->tid;
1018 	u16 *ssn = &params->ssn;
1019 	u8 buf_size = params->buf_size;
1020 
1021 	for (ii = 0; ii < RSI_MAX_VIFS; ii++) {
1022 		if (vif == adapter->vifs[ii])
1023 			break;
1024 	}
1025 
1026 	mutex_lock(&common->mutex);
1027 
1028 	if (ssn != NULL)
1029 		seq_no = *ssn;
1030 
1031 	if ((vif->type == NL80211_IFTYPE_AP) ||
1032 	    (vif->type == NL80211_IFTYPE_P2P_GO)) {
1033 		rsta = rsi_find_sta(common, sta->addr);
1034 		if (!rsta) {
1035 			rsi_dbg(ERR_ZONE, "No station mapped\n");
1036 			status = 0;
1037 			goto unlock;
1038 		}
1039 		sta_id = rsta->sta_id;
1040 	}
1041 
1042 	rsi_dbg(INFO_ZONE,
1043 		"%s: AMPDU action tid=%d ssn=0x%x, buf_size=%d sta_id=%d\n",
1044 		__func__, tid, seq_no, buf_size, sta_id);
1045 
1046 	switch (action) {
1047 	case IEEE80211_AMPDU_RX_START:
1048 		status = rsi_send_aggregation_params_frame(common,
1049 							   tid,
1050 							   seq_no,
1051 							   buf_size,
1052 							   STA_RX_ADDBA_DONE,
1053 							   sta_id);
1054 		break;
1055 
1056 	case IEEE80211_AMPDU_RX_STOP:
1057 		status = rsi_send_aggregation_params_frame(common,
1058 							   tid,
1059 							   0,
1060 							   buf_size,
1061 							   STA_RX_DELBA,
1062 							   sta_id);
1063 		break;
1064 
1065 	case IEEE80211_AMPDU_TX_START:
1066 		if ((vif->type == NL80211_IFTYPE_STATION) ||
1067 		    (vif->type == NL80211_IFTYPE_P2P_CLIENT))
1068 			common->vif_info[ii].seq_start = seq_no;
1069 		else if ((vif->type == NL80211_IFTYPE_AP) ||
1070 			 (vif->type == NL80211_IFTYPE_P2P_GO))
1071 			rsta->seq_start[tid] = seq_no;
1072 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1073 		status = 0;
1074 		break;
1075 
1076 	case IEEE80211_AMPDU_TX_STOP_CONT:
1077 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1078 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1079 		status = rsi_send_aggregation_params_frame(common,
1080 							   tid,
1081 							   seq_no,
1082 							   buf_size,
1083 							   STA_TX_DELBA,
1084 							   sta_id);
1085 		if (!status)
1086 			ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1087 		break;
1088 
1089 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1090 		if ((vif->type == NL80211_IFTYPE_STATION) ||
1091 		    (vif->type == NL80211_IFTYPE_P2P_CLIENT))
1092 			seq_start = common->vif_info[ii].seq_start;
1093 		else if ((vif->type == NL80211_IFTYPE_AP) ||
1094 			 (vif->type == NL80211_IFTYPE_P2P_GO))
1095 			seq_start = rsta->seq_start[tid];
1096 		status = rsi_send_aggregation_params_frame(common,
1097 							   tid,
1098 							   seq_start,
1099 							   buf_size,
1100 							   STA_TX_ADDBA_DONE,
1101 							   sta_id);
1102 		break;
1103 
1104 	default:
1105 		rsi_dbg(ERR_ZONE, "%s: Uknown AMPDU action\n", __func__);
1106 		break;
1107 	}
1108 
1109 unlock:
1110 	mutex_unlock(&common->mutex);
1111 	return status;
1112 }
1113 
1114 /**
1115  * rsi_mac80211_set_rts_threshold() - This function sets rts threshold value.
1116  * @hw: Pointer to the ieee80211_hw structure.
1117  * @value: Rts threshold value.
1118  *
1119  * Return: 0 on success.
1120  */
1121 static int rsi_mac80211_set_rts_threshold(struct ieee80211_hw *hw,
1122 					  u32 value)
1123 {
1124 	struct rsi_hw *adapter = hw->priv;
1125 	struct rsi_common *common = adapter->priv;
1126 
1127 	mutex_lock(&common->mutex);
1128 	common->rts_threshold = value;
1129 	mutex_unlock(&common->mutex);
1130 
1131 	return 0;
1132 }
1133 
1134 /**
1135  * rsi_mac80211_set_rate_mask() - This function sets bitrate_mask to be used.
1136  * @hw: Pointer to the ieee80211_hw structure
1137  * @vif: Pointer to the ieee80211_vif structure.
1138  * @mask: Pointer to the cfg80211_bitrate_mask structure.
1139  *
1140  * Return: 0 on success.
1141  */
1142 static int rsi_mac80211_set_rate_mask(struct ieee80211_hw *hw,
1143 				      struct ieee80211_vif *vif,
1144 				      const struct cfg80211_bitrate_mask *mask)
1145 {
1146 	struct rsi_hw *adapter = hw->priv;
1147 	struct rsi_common *common = adapter->priv;
1148 	enum nl80211_band band = hw->conf.chandef.chan->band;
1149 
1150 	mutex_lock(&common->mutex);
1151 	common->fixedrate_mask[band] = 0;
1152 
1153 	if (mask->control[band].legacy == 0xfff) {
1154 		common->fixedrate_mask[band] =
1155 			(mask->control[band].ht_mcs[0] << 12);
1156 	} else {
1157 		common->fixedrate_mask[band] =
1158 			mask->control[band].legacy;
1159 	}
1160 	mutex_unlock(&common->mutex);
1161 
1162 	return 0;
1163 }
1164 
1165 /**
1166  * rsi_perform_cqm() - This function performs cqm.
1167  * @common: Pointer to the driver private structure.
1168  * @bssid: pointer to the bssid.
1169  * @rssi: RSSI value.
1170  */
1171 static void rsi_perform_cqm(struct rsi_common *common,
1172 			    u8 *bssid,
1173 			    s8 rssi,
1174 			    struct ieee80211_vif *vif)
1175 {
1176 	s8 last_event = common->cqm_info.last_cqm_event_rssi;
1177 	int thold = common->cqm_info.rssi_thold;
1178 	u32 hyst = common->cqm_info.rssi_hyst;
1179 	enum nl80211_cqm_rssi_threshold_event event;
1180 
1181 	if (rssi < thold && (last_event == 0 || rssi < (last_event - hyst)))
1182 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
1183 	else if (rssi > thold &&
1184 		 (last_event == 0 || rssi > (last_event + hyst)))
1185 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
1186 	else
1187 		return;
1188 
1189 	common->cqm_info.last_cqm_event_rssi = rssi;
1190 	rsi_dbg(INFO_ZONE, "CQM: Notifying event: %d\n", event);
1191 	ieee80211_cqm_rssi_notify(vif, event, rssi, GFP_KERNEL);
1192 
1193 	return;
1194 }
1195 
1196 /**
1197  * rsi_fill_rx_status() - This function fills rx status in
1198  *			  ieee80211_rx_status structure.
1199  * @hw: Pointer to the ieee80211_hw structure.
1200  * @skb: Pointer to the socket buffer structure.
1201  * @common: Pointer to the driver private structure.
1202  * @rxs: Pointer to the ieee80211_rx_status structure.
1203  *
1204  * Return: None.
1205  */
1206 static void rsi_fill_rx_status(struct ieee80211_hw *hw,
1207 			       struct sk_buff *skb,
1208 			       struct rsi_common *common,
1209 			       struct ieee80211_rx_status *rxs)
1210 {
1211 	struct rsi_hw *adapter = common->priv;
1212 	struct ieee80211_vif *vif;
1213 	struct ieee80211_bss_conf *bss = NULL;
1214 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1215 	struct skb_info *rx_params = (struct skb_info *)info->driver_data;
1216 	struct ieee80211_hdr *hdr;
1217 	char rssi = rx_params->rssi;
1218 	u8 hdrlen = 0;
1219 	u8 channel = rx_params->channel;
1220 	s32 freq;
1221 	int i;
1222 
1223 	hdr = ((struct ieee80211_hdr *)(skb->data));
1224 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
1225 
1226 	memset(info, 0, sizeof(struct ieee80211_tx_info));
1227 
1228 	rxs->signal = -(rssi);
1229 
1230 	rxs->band = common->band;
1231 
1232 	freq = ieee80211_channel_to_frequency(channel, rxs->band);
1233 
1234 	if (freq)
1235 		rxs->freq = freq;
1236 
1237 	if (ieee80211_has_protected(hdr->frame_control)) {
1238 		if (rsi_is_cipher_wep(common)) {
1239 			memmove(skb->data + 4, skb->data, hdrlen);
1240 			skb_pull(skb, 4);
1241 		} else {
1242 			memmove(skb->data + 8, skb->data, hdrlen);
1243 			skb_pull(skb, 8);
1244 			rxs->flag |= RX_FLAG_MMIC_STRIPPED;
1245 		}
1246 		rxs->flag |= RX_FLAG_DECRYPTED;
1247 		rxs->flag |= RX_FLAG_IV_STRIPPED;
1248 	}
1249 
1250 	for (i = 0; i < RSI_MAX_VIFS; i++) {
1251 		vif = adapter->vifs[i];
1252 		if (!vif)
1253 			continue;
1254 		if (vif->type == NL80211_IFTYPE_STATION) {
1255 			bss = &vif->bss_conf;
1256 			break;
1257 		}
1258 	}
1259 	if (!bss)
1260 		return;
1261 	/* CQM only for connected AP beacons, the RSSI is a weighted avg */
1262 	if (bss->assoc && !(memcmp(bss->bssid, hdr->addr2, ETH_ALEN))) {
1263 		if (ieee80211_is_beacon(hdr->frame_control))
1264 			rsi_perform_cqm(common, hdr->addr2, rxs->signal, vif);
1265 	}
1266 
1267 	return;
1268 }
1269 
1270 /**
1271  * rsi_indicate_pkt_to_os() - This function sends recieved packet to mac80211.
1272  * @common: Pointer to the driver private structure.
1273  * @skb: Pointer to the socket buffer structure.
1274  *
1275  * Return: None.
1276  */
1277 void rsi_indicate_pkt_to_os(struct rsi_common *common,
1278 			    struct sk_buff *skb)
1279 {
1280 	struct rsi_hw *adapter = common->priv;
1281 	struct ieee80211_hw *hw = adapter->hw;
1282 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1283 
1284 	if ((common->iface_down) || (!adapter->sc_nvifs)) {
1285 		dev_kfree_skb(skb);
1286 		return;
1287 	}
1288 
1289 	/* filling in the ieee80211_rx_status flags */
1290 	rsi_fill_rx_status(hw, skb, common, rx_status);
1291 
1292 	ieee80211_rx_irqsafe(hw, skb);
1293 }
1294 
1295 static void rsi_set_min_rate(struct ieee80211_hw *hw,
1296 			     struct ieee80211_sta *sta,
1297 			     struct rsi_common *common)
1298 {
1299 	u8 band = hw->conf.chandef.chan->band;
1300 	u8 ii;
1301 	u32 rate_bitmap;
1302 	bool matched = false;
1303 
1304 	common->bitrate_mask[band] = sta->supp_rates[band];
1305 
1306 	rate_bitmap = (common->fixedrate_mask[band] & sta->supp_rates[band]);
1307 
1308 	if (rate_bitmap & 0xfff) {
1309 		/* Find out the min rate */
1310 		for (ii = 0; ii < ARRAY_SIZE(rsi_rates); ii++) {
1311 			if (rate_bitmap & BIT(ii)) {
1312 				common->min_rate = rsi_rates[ii].hw_value;
1313 				matched = true;
1314 				break;
1315 			}
1316 		}
1317 	}
1318 
1319 	common->vif_info[0].is_ht = sta->ht_cap.ht_supported;
1320 
1321 	if ((common->vif_info[0].is_ht) && (rate_bitmap >> 12)) {
1322 		for (ii = 0; ii < ARRAY_SIZE(rsi_mcsrates); ii++) {
1323 			if ((rate_bitmap >> 12) & BIT(ii)) {
1324 				common->min_rate = rsi_mcsrates[ii];
1325 				matched = true;
1326 				break;
1327 			}
1328 		}
1329 	}
1330 
1331 	if (!matched)
1332 		common->min_rate = 0xffff;
1333 }
1334 
1335 /**
1336  * rsi_mac80211_sta_add() - This function notifies driver about a peer getting
1337  *			    connected.
1338  * @hw: pointer to the ieee80211_hw structure.
1339  * @vif: Pointer to the ieee80211_vif structure.
1340  * @sta: Pointer to the ieee80211_sta structure.
1341  *
1342  * Return: 0 on success, negative error codes on failure.
1343  */
1344 static int rsi_mac80211_sta_add(struct ieee80211_hw *hw,
1345 				struct ieee80211_vif *vif,
1346 				struct ieee80211_sta *sta)
1347 {
1348 	struct rsi_hw *adapter = hw->priv;
1349 	struct rsi_common *common = adapter->priv;
1350 	bool sta_exist = false;
1351 	struct rsi_sta *rsta;
1352 	int status = 0;
1353 
1354 	rsi_dbg(INFO_ZONE, "Station Add: %pM\n", sta->addr);
1355 
1356 	mutex_lock(&common->mutex);
1357 
1358 	if ((vif->type == NL80211_IFTYPE_AP) ||
1359 	    (vif->type == NL80211_IFTYPE_P2P_GO)) {
1360 		u8 cnt;
1361 		int sta_idx = -1;
1362 		int free_index = -1;
1363 
1364 		/* Check if max stations reached */
1365 		if (common->num_stations >= common->max_stations) {
1366 			rsi_dbg(ERR_ZONE, "Reject: Max Stations exists\n");
1367 			status = -EOPNOTSUPP;
1368 			goto unlock;
1369 		}
1370 		for (cnt = 0; cnt < common->max_stations; cnt++) {
1371 			rsta = &common->stations[cnt];
1372 
1373 			if (!rsta->sta) {
1374 				if (free_index < 0)
1375 					free_index = cnt;
1376 				continue;
1377 			}
1378 			if (!memcmp(rsta->sta->addr, sta->addr, ETH_ALEN)) {
1379 				rsi_dbg(INFO_ZONE, "Station exists\n");
1380 				sta_idx = cnt;
1381 				sta_exist = true;
1382 				break;
1383 			}
1384 		}
1385 		if (!sta_exist) {
1386 			if (free_index >= 0)
1387 				sta_idx = free_index;
1388 		}
1389 		if (sta_idx < 0) {
1390 			rsi_dbg(ERR_ZONE,
1391 				"%s: Some problem reaching here...\n",
1392 				__func__);
1393 			status = -EINVAL;
1394 			goto unlock;
1395 		}
1396 		rsta = &common->stations[sta_idx];
1397 		rsta->sta = sta;
1398 		rsta->sta_id = sta_idx;
1399 		for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1400 			rsta->start_tx_aggr[cnt] = false;
1401 		for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1402 			rsta->seq_start[cnt] = 0;
1403 		if (!sta_exist) {
1404 			rsi_dbg(INFO_ZONE, "New Station\n");
1405 
1406 			/* Send peer notify to device */
1407 			rsi_dbg(INFO_ZONE, "Indicate bss status to device\n");
1408 			rsi_inform_bss_status(common, RSI_OPMODE_AP, 1,
1409 					      sta->addr, sta->wme, sta->aid,
1410 					      sta, sta_idx, 0, vif);
1411 
1412 			if (common->key) {
1413 				struct ieee80211_key_conf *key = common->key;
1414 
1415 				if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
1416 				    (key->cipher == WLAN_CIPHER_SUITE_WEP40))
1417 					rsi_hal_load_key(adapter->priv,
1418 							 key->key,
1419 							 key->keylen,
1420 							 RSI_PAIRWISE_KEY,
1421 							 key->keyidx,
1422 							 key->cipher,
1423 							 sta_idx,
1424 							 vif);
1425 			}
1426 
1427 			common->num_stations++;
1428 		}
1429 	}
1430 
1431 	if ((vif->type == NL80211_IFTYPE_STATION) ||
1432 	    (vif->type == NL80211_IFTYPE_P2P_CLIENT)) {
1433 		rsi_set_min_rate(hw, sta, common);
1434 		if (sta->ht_cap.ht_supported) {
1435 			common->vif_info[0].is_ht = true;
1436 			common->bitrate_mask[NL80211_BAND_2GHZ] =
1437 					sta->supp_rates[NL80211_BAND_2GHZ];
1438 			if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ||
1439 			    (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
1440 				common->vif_info[0].sgi = true;
1441 			ieee80211_start_tx_ba_session(sta, 0, 0);
1442 		}
1443 	}
1444 
1445 unlock:
1446 	mutex_unlock(&common->mutex);
1447 
1448 	return status;
1449 }
1450 
1451 /**
1452  * rsi_mac80211_sta_remove() - This function notifies driver about a peer
1453  *			       getting disconnected.
1454  * @hw: Pointer to the ieee80211_hw structure.
1455  * @vif: Pointer to the ieee80211_vif structure.
1456  * @sta: Pointer to the ieee80211_sta structure.
1457  *
1458  * Return: 0 on success, negative error codes on failure.
1459  */
1460 static int rsi_mac80211_sta_remove(struct ieee80211_hw *hw,
1461 				   struct ieee80211_vif *vif,
1462 				   struct ieee80211_sta *sta)
1463 {
1464 	struct rsi_hw *adapter = hw->priv;
1465 	struct rsi_common *common = adapter->priv;
1466 	struct ieee80211_bss_conf *bss = &vif->bss_conf;
1467 	struct rsi_sta *rsta;
1468 
1469 	rsi_dbg(INFO_ZONE, "Station Remove: %pM\n", sta->addr);
1470 
1471 	mutex_lock(&common->mutex);
1472 
1473 	if ((vif->type == NL80211_IFTYPE_AP) ||
1474 	    (vif->type == NL80211_IFTYPE_P2P_GO)) {
1475 		u8 sta_idx, cnt;
1476 
1477 		/* Send peer notify to device */
1478 		rsi_dbg(INFO_ZONE, "Indicate bss status to device\n");
1479 		for (sta_idx = 0; sta_idx < common->max_stations; sta_idx++) {
1480 			rsta = &common->stations[sta_idx];
1481 
1482 			if (!rsta->sta)
1483 				continue;
1484 			if (!memcmp(rsta->sta->addr, sta->addr, ETH_ALEN)) {
1485 				rsi_inform_bss_status(common, RSI_OPMODE_AP, 0,
1486 						      sta->addr, sta->wme,
1487 						      sta->aid, sta, sta_idx,
1488 						      0, vif);
1489 				rsta->sta = NULL;
1490 				rsta->sta_id = -1;
1491 				for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1492 					rsta->start_tx_aggr[cnt] = false;
1493 				if (common->num_stations > 0)
1494 					common->num_stations--;
1495 				break;
1496 			}
1497 		}
1498 		if (sta_idx >= common->max_stations)
1499 			rsi_dbg(ERR_ZONE, "%s: No station found\n", __func__);
1500 	}
1501 
1502 	if ((vif->type == NL80211_IFTYPE_STATION) ||
1503 	    (vif->type == NL80211_IFTYPE_P2P_CLIENT)) {
1504 		/* Resetting all the fields to default values */
1505 		memcpy((u8 *)bss->bssid, (u8 *)sta->addr, ETH_ALEN);
1506 		bss->qos = sta->wme;
1507 		common->bitrate_mask[NL80211_BAND_2GHZ] = 0;
1508 		common->bitrate_mask[NL80211_BAND_5GHZ] = 0;
1509 		common->min_rate = 0xffff;
1510 		common->vif_info[0].is_ht = false;
1511 		common->vif_info[0].sgi = false;
1512 		common->vif_info[0].seq_start = 0;
1513 		common->secinfo.ptk_cipher = 0;
1514 		common->secinfo.gtk_cipher = 0;
1515 		if (!common->iface_down)
1516 			rsi_send_rx_filter_frame(common, 0);
1517 	}
1518 	mutex_unlock(&common->mutex);
1519 
1520 	return 0;
1521 }
1522 
1523 /**
1524  * rsi_mac80211_set_antenna() - This function is used to configure
1525  *				tx and rx antennas.
1526  * @hw: Pointer to the ieee80211_hw structure.
1527  * @tx_ant: Bitmap for tx antenna
1528  * @rx_ant: Bitmap for rx antenna
1529  *
1530  * Return: 0 on success, Negative error code on failure.
1531  */
1532 static int rsi_mac80211_set_antenna(struct ieee80211_hw *hw,
1533 				    u32 tx_ant, u32 rx_ant)
1534 {
1535 	struct rsi_hw *adapter = hw->priv;
1536 	struct rsi_common *common = adapter->priv;
1537 	u8 antenna = 0;
1538 
1539 	if (tx_ant > 1 || rx_ant > 1) {
1540 		rsi_dbg(ERR_ZONE,
1541 			"Invalid antenna selection (tx: %d, rx:%d)\n",
1542 			tx_ant, rx_ant);
1543 		rsi_dbg(ERR_ZONE,
1544 			"Use 0 for int_ant, 1 for ext_ant\n");
1545 		return -EINVAL;
1546 	}
1547 
1548 	rsi_dbg(INFO_ZONE, "%s: Antenna map Tx %x Rx %d\n",
1549 			__func__, tx_ant, rx_ant);
1550 
1551 	mutex_lock(&common->mutex);
1552 
1553 	antenna = tx_ant ? ANTENNA_SEL_UFL : ANTENNA_SEL_INT;
1554 	if (common->ant_in_use != antenna)
1555 		if (rsi_set_antenna(common, antenna))
1556 			goto fail_set_antenna;
1557 
1558 	rsi_dbg(INFO_ZONE, "(%s) Antenna path configured successfully\n",
1559 		tx_ant ? "UFL" : "INT");
1560 
1561 	common->ant_in_use = antenna;
1562 
1563 	mutex_unlock(&common->mutex);
1564 
1565 	return 0;
1566 
1567 fail_set_antenna:
1568 	rsi_dbg(ERR_ZONE, "%s: Failed.\n", __func__);
1569 	mutex_unlock(&common->mutex);
1570 	return -EINVAL;
1571 }
1572 
1573 /**
1574  * rsi_mac80211_get_antenna() - This function is used to configure
1575  * 				tx and rx antennas.
1576  *
1577  * @hw: Pointer to the ieee80211_hw structure.
1578  * @tx_ant: Bitmap for tx antenna
1579  * @rx_ant: Bitmap for rx antenna
1580  *
1581  * Return: 0 on success, negative error codes on failure.
1582  */
1583 static int rsi_mac80211_get_antenna(struct ieee80211_hw *hw,
1584 				    u32 *tx_ant, u32 *rx_ant)
1585 {
1586 	struct rsi_hw *adapter = hw->priv;
1587 	struct rsi_common *common = adapter->priv;
1588 
1589 	mutex_lock(&common->mutex);
1590 
1591 	*tx_ant = (common->ant_in_use == ANTENNA_SEL_UFL) ? 1 : 0;
1592 	*rx_ant = 0;
1593 
1594 	mutex_unlock(&common->mutex);
1595 
1596 	return 0;
1597 }
1598 
1599 static int rsi_map_region_code(enum nl80211_dfs_regions region_code)
1600 {
1601 	switch (region_code) {
1602 	case NL80211_DFS_FCC:
1603 		return RSI_REGION_FCC;
1604 	case NL80211_DFS_ETSI:
1605 		return RSI_REGION_ETSI;
1606 	case NL80211_DFS_JP:
1607 		return RSI_REGION_TELEC;
1608 	case NL80211_DFS_UNSET:
1609 		return RSI_REGION_WORLD;
1610 	}
1611 	return RSI_REGION_WORLD;
1612 }
1613 
1614 static void rsi_reg_notify(struct wiphy *wiphy,
1615 			   struct regulatory_request *request)
1616 {
1617 	struct ieee80211_supported_band *sband;
1618 	struct ieee80211_channel *ch;
1619 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1620 	struct rsi_hw * adapter = hw->priv;
1621 	struct rsi_common *common = adapter->priv;
1622 	int i;
1623 
1624 	mutex_lock(&common->mutex);
1625 
1626 	rsi_dbg(INFO_ZONE, "country = %s dfs_region = %d\n",
1627 		request->alpha2, request->dfs_region);
1628 
1629 	if (common->num_supp_bands > 1) {
1630 		sband = wiphy->bands[NL80211_BAND_5GHZ];
1631 
1632 		for (i = 0; i < sband->n_channels; i++) {
1633 			ch = &sband->channels[i];
1634 			if (ch->flags & IEEE80211_CHAN_DISABLED)
1635 				continue;
1636 
1637 			if (ch->flags & IEEE80211_CHAN_RADAR)
1638 				ch->flags |= IEEE80211_CHAN_NO_IR;
1639 		}
1640 	}
1641 	adapter->dfs_region = rsi_map_region_code(request->dfs_region);
1642 	rsi_dbg(INFO_ZONE, "RSI region code = %d\n", adapter->dfs_region);
1643 
1644 	adapter->country[0] = request->alpha2[0];
1645 	adapter->country[1] = request->alpha2[1];
1646 
1647 	mutex_unlock(&common->mutex);
1648 }
1649 
1650 static void rsi_mac80211_rfkill_poll(struct ieee80211_hw *hw)
1651 {
1652 	struct rsi_hw *adapter = hw->priv;
1653 	struct rsi_common *common = adapter->priv;
1654 
1655 	mutex_lock(&common->mutex);
1656 	if (common->fsm_state != FSM_MAC_INIT_DONE)
1657 		wiphy_rfkill_set_hw_state(hw->wiphy, true);
1658 	else
1659 		wiphy_rfkill_set_hw_state(hw->wiphy, false);
1660 	mutex_unlock(&common->mutex);
1661 }
1662 
1663 static void rsi_resume_conn_channel(struct rsi_common *common)
1664 {
1665 	struct rsi_hw *adapter = common->priv;
1666 	struct ieee80211_vif *vif;
1667 	int cnt;
1668 
1669 	for (cnt = 0; cnt < RSI_MAX_VIFS; cnt++) {
1670 		vif = adapter->vifs[cnt];
1671 		if (!vif)
1672 			continue;
1673 
1674 		if ((vif->type == NL80211_IFTYPE_AP) ||
1675 		    (vif->type == NL80211_IFTYPE_P2P_GO)) {
1676 			rsi_switch_channel(adapter, vif);
1677 			break;
1678 		}
1679 		if (((vif->type == NL80211_IFTYPE_STATION) ||
1680 		     (vif->type == NL80211_IFTYPE_P2P_CLIENT)) &&
1681 		    vif->bss_conf.assoc) {
1682 			rsi_switch_channel(adapter, vif);
1683 			break;
1684 		}
1685 	}
1686 }
1687 
1688 void rsi_roc_timeout(struct timer_list *t)
1689 {
1690 	struct rsi_common *common = from_timer(common, t, roc_timer);
1691 
1692 	rsi_dbg(INFO_ZONE, "Remain on channel expired\n");
1693 
1694 	mutex_lock(&common->mutex);
1695 	ieee80211_remain_on_channel_expired(common->priv->hw);
1696 
1697 	if (timer_pending(&common->roc_timer))
1698 		del_timer(&common->roc_timer);
1699 
1700 	rsi_resume_conn_channel(common);
1701 	mutex_unlock(&common->mutex);
1702 }
1703 
1704 static int rsi_mac80211_roc(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1705 			    struct ieee80211_channel *chan, int duration,
1706 			    enum ieee80211_roc_type type)
1707 {
1708 	struct rsi_hw *adapter = (struct rsi_hw *)hw->priv;
1709 	struct rsi_common *common = (struct rsi_common *)adapter->priv;
1710 	int status = 0;
1711 
1712 	rsi_dbg(INFO_ZONE, "***** Remain on channel *****\n");
1713 
1714 	mutex_lock(&common->mutex);
1715 	rsi_dbg(INFO_ZONE, "%s: channel: %d duration: %dms\n",
1716 		__func__, chan->hw_value, duration);
1717 
1718 	if (timer_pending(&common->roc_timer)) {
1719 		rsi_dbg(INFO_ZONE, "Stop on-going ROC\n");
1720 		del_timer(&common->roc_timer);
1721 	}
1722 	common->roc_timer.expires = msecs_to_jiffies(duration) + jiffies;
1723 	add_timer(&common->roc_timer);
1724 
1725 	/* Configure band */
1726 	if (rsi_band_check(common, chan)) {
1727 		rsi_dbg(ERR_ZONE, "Failed to set band\n");
1728 		status = -EINVAL;
1729 		goto out;
1730 	}
1731 
1732 	/* Configure channel */
1733 	if (rsi_set_channel(common, chan)) {
1734 		rsi_dbg(ERR_ZONE, "Failed to set the channel\n");
1735 		status = -EINVAL;
1736 		goto out;
1737 	}
1738 
1739 	common->roc_vif = vif;
1740 	ieee80211_ready_on_channel(hw);
1741 	rsi_dbg(INFO_ZONE, "%s: Ready on channel :%d\n",
1742 		__func__, chan->hw_value);
1743 
1744 out:
1745 	mutex_unlock(&common->mutex);
1746 
1747 	return status;
1748 }
1749 
1750 static int rsi_mac80211_cancel_roc(struct ieee80211_hw *hw)
1751 {
1752 	struct rsi_hw *adapter = hw->priv;
1753 	struct rsi_common *common = adapter->priv;
1754 
1755 	rsi_dbg(INFO_ZONE, "Cancel remain on channel\n");
1756 
1757 	mutex_lock(&common->mutex);
1758 	if (!timer_pending(&common->roc_timer)) {
1759 		mutex_unlock(&common->mutex);
1760 		return 0;
1761 	}
1762 
1763 	del_timer(&common->roc_timer);
1764 
1765 	rsi_resume_conn_channel(common);
1766 	mutex_unlock(&common->mutex);
1767 
1768 	return 0;
1769 }
1770 
1771 #ifdef CONFIG_PM
1772 static const struct wiphy_wowlan_support rsi_wowlan_support = {
1773 	.flags = WIPHY_WOWLAN_ANY |
1774 		 WIPHY_WOWLAN_MAGIC_PKT |
1775 		 WIPHY_WOWLAN_DISCONNECT |
1776 		 WIPHY_WOWLAN_GTK_REKEY_FAILURE  |
1777 		 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
1778 		 WIPHY_WOWLAN_EAP_IDENTITY_REQ   |
1779 		 WIPHY_WOWLAN_4WAY_HANDSHAKE,
1780 };
1781 
1782 static u16 rsi_wow_map_triggers(struct rsi_common *common,
1783 				struct cfg80211_wowlan *wowlan)
1784 {
1785 	u16 wow_triggers = 0;
1786 
1787 	rsi_dbg(INFO_ZONE, "Mapping wowlan triggers\n");
1788 
1789 	if (wowlan->any)
1790 		wow_triggers |= RSI_WOW_ANY;
1791 	if (wowlan->magic_pkt)
1792 		wow_triggers |= RSI_WOW_MAGIC_PKT;
1793 	if (wowlan->disconnect)
1794 		wow_triggers |= RSI_WOW_DISCONNECT;
1795 	if (wowlan->gtk_rekey_failure || wowlan->eap_identity_req ||
1796 	    wowlan->four_way_handshake)
1797 		wow_triggers |= RSI_WOW_GTK_REKEY;
1798 
1799 	return wow_triggers;
1800 }
1801 
1802 int rsi_config_wowlan(struct rsi_hw *adapter, struct cfg80211_wowlan *wowlan)
1803 {
1804 	struct rsi_common *common = adapter->priv;
1805 	u16 triggers = 0;
1806 	u16 rx_filter_word = 0;
1807 	struct ieee80211_bss_conf *bss = NULL;
1808 
1809 	rsi_dbg(INFO_ZONE, "Config WoWLAN to device\n");
1810 
1811 	if (!adapter->vifs[0])
1812 		return -EINVAL;
1813 
1814 	bss = &adapter->vifs[0]->bss_conf;
1815 
1816 	if (WARN_ON(!wowlan)) {
1817 		rsi_dbg(ERR_ZONE, "WoW triggers not enabled\n");
1818 		return -EINVAL;
1819 	}
1820 
1821 	common->wow_flags |= RSI_WOW_ENABLED;
1822 	triggers = rsi_wow_map_triggers(common, wowlan);
1823 	if (!triggers) {
1824 		rsi_dbg(ERR_ZONE, "%s:No valid WoW triggers\n", __func__);
1825 		return -EINVAL;
1826 	}
1827 	if (!bss->assoc) {
1828 		rsi_dbg(ERR_ZONE,
1829 			"Cannot configure WoWLAN (Station not connected)\n");
1830 		common->wow_flags |= RSI_WOW_NO_CONNECTION;
1831 		return 0;
1832 	}
1833 	rsi_dbg(INFO_ZONE, "TRIGGERS %x\n", triggers);
1834 	rsi_send_wowlan_request(common, triggers, 1);
1835 
1836 	/**
1837 	 * Increase the beacon_miss threshold & keep-alive timers in
1838 	 * vap_update frame
1839 	 */
1840 	rsi_send_vap_dynamic_update(common);
1841 
1842 	rx_filter_word = (ALLOW_DATA_ASSOC_PEER | DISALLOW_BEACONS);
1843 	rsi_send_rx_filter_frame(common, rx_filter_word);
1844 
1845 	return 0;
1846 }
1847 EXPORT_SYMBOL(rsi_config_wowlan);
1848 
1849 static int rsi_mac80211_suspend(struct ieee80211_hw *hw,
1850 				struct cfg80211_wowlan *wowlan)
1851 {
1852 	struct rsi_hw *adapter = hw->priv;
1853 	struct rsi_common *common = adapter->priv;
1854 
1855 	rsi_dbg(INFO_ZONE, "%s: mac80211 suspend\n", __func__);
1856 	mutex_lock(&common->mutex);
1857 	if (rsi_config_wowlan(adapter, wowlan)) {
1858 		rsi_dbg(ERR_ZONE, "Failed to configure WoWLAN\n");
1859 		mutex_unlock(&common->mutex);
1860 		return 1;
1861 	}
1862 	mutex_unlock(&common->mutex);
1863 
1864 	return 0;
1865 }
1866 
1867 static int rsi_mac80211_resume(struct ieee80211_hw *hw)
1868 {
1869 	u16 rx_filter_word = 0;
1870 	struct rsi_hw *adapter = hw->priv;
1871 	struct rsi_common *common = adapter->priv;
1872 
1873 	common->wow_flags = 0;
1874 
1875 	rsi_dbg(INFO_ZONE, "%s: mac80211 resume\n", __func__);
1876 
1877 	if (common->hibernate_resume)
1878 		return 0;
1879 
1880 	mutex_lock(&common->mutex);
1881 	rsi_send_wowlan_request(common, 0, 0);
1882 
1883 	rx_filter_word = (ALLOW_DATA_ASSOC_PEER | ALLOW_CTRL_ASSOC_PEER |
1884 			  ALLOW_MGMT_ASSOC_PEER);
1885 	rsi_send_rx_filter_frame(common, rx_filter_word);
1886 	mutex_unlock(&common->mutex);
1887 
1888 	return 0;
1889 }
1890 
1891 #endif
1892 
1893 static const struct ieee80211_ops mac80211_ops = {
1894 	.tx = rsi_mac80211_tx,
1895 	.start = rsi_mac80211_start,
1896 	.stop = rsi_mac80211_stop,
1897 	.add_interface = rsi_mac80211_add_interface,
1898 	.remove_interface = rsi_mac80211_remove_interface,
1899 	.config = rsi_mac80211_config,
1900 	.bss_info_changed = rsi_mac80211_bss_info_changed,
1901 	.conf_tx = rsi_mac80211_conf_tx,
1902 	.configure_filter = rsi_mac80211_conf_filter,
1903 	.set_key = rsi_mac80211_set_key,
1904 	.set_rts_threshold = rsi_mac80211_set_rts_threshold,
1905 	.set_bitrate_mask = rsi_mac80211_set_rate_mask,
1906 	.ampdu_action = rsi_mac80211_ampdu_action,
1907 	.sta_add = rsi_mac80211_sta_add,
1908 	.sta_remove = rsi_mac80211_sta_remove,
1909 	.set_antenna = rsi_mac80211_set_antenna,
1910 	.get_antenna = rsi_mac80211_get_antenna,
1911 	.rfkill_poll = rsi_mac80211_rfkill_poll,
1912 	.remain_on_channel = rsi_mac80211_roc,
1913 	.cancel_remain_on_channel = rsi_mac80211_cancel_roc,
1914 #ifdef CONFIG_PM
1915 	.suspend = rsi_mac80211_suspend,
1916 	.resume  = rsi_mac80211_resume,
1917 #endif
1918 };
1919 
1920 /**
1921  * rsi_mac80211_attach() - This function is used to initialize Mac80211 stack.
1922  * @common: Pointer to the driver private structure.
1923  *
1924  * Return: 0 on success, negative error codes on failure.
1925  */
1926 int rsi_mac80211_attach(struct rsi_common *common)
1927 {
1928 	int status = 0;
1929 	struct ieee80211_hw *hw = NULL;
1930 	struct wiphy *wiphy = NULL;
1931 	struct rsi_hw *adapter = common->priv;
1932 	u8 addr_mask[ETH_ALEN] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x3};
1933 
1934 	rsi_dbg(INIT_ZONE, "%s: Performing mac80211 attach\n", __func__);
1935 
1936 	hw = ieee80211_alloc_hw(sizeof(struct rsi_hw), &mac80211_ops);
1937 	if (!hw) {
1938 		rsi_dbg(ERR_ZONE, "%s: ieee80211 hw alloc failed\n", __func__);
1939 		return -ENOMEM;
1940 	}
1941 
1942 	wiphy = hw->wiphy;
1943 
1944 	SET_IEEE80211_DEV(hw, adapter->device);
1945 
1946 	hw->priv = adapter;
1947 	adapter->hw = hw;
1948 
1949 	ieee80211_hw_set(hw, SIGNAL_DBM);
1950 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
1951 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1952 	ieee80211_hw_set(hw, SUPPORTS_PS);
1953 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
1954 
1955 	hw->queues = MAX_HW_QUEUES;
1956 	hw->extra_tx_headroom = RSI_NEEDED_HEADROOM;
1957 
1958 	hw->max_rates = 1;
1959 	hw->max_rate_tries = MAX_RETRIES;
1960 	hw->uapsd_queues = RSI_IEEE80211_UAPSD_QUEUES;
1961 	hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
1962 
1963 	hw->max_tx_aggregation_subframes = RSI_MAX_TX_AGGR_FRMS;
1964 	hw->max_rx_aggregation_subframes = RSI_MAX_RX_AGGR_FRMS;
1965 	hw->rate_control_algorithm = "AARF";
1966 
1967 	SET_IEEE80211_PERM_ADDR(hw, common->mac_addr);
1968 	ether_addr_copy(hw->wiphy->addr_mask, addr_mask);
1969 
1970 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1971 				 BIT(NL80211_IFTYPE_AP) |
1972 				 BIT(NL80211_IFTYPE_P2P_DEVICE) |
1973 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
1974 				 BIT(NL80211_IFTYPE_P2P_GO);
1975 
1976 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1977 	wiphy->retry_short = RETRY_SHORT;
1978 	wiphy->retry_long  = RETRY_LONG;
1979 	wiphy->frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1980 	wiphy->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1981 	wiphy->flags = 0;
1982 
1983 	wiphy->available_antennas_rx = 1;
1984 	wiphy->available_antennas_tx = 1;
1985 
1986 	rsi_register_rates_channels(adapter, NL80211_BAND_2GHZ);
1987 	wiphy->bands[NL80211_BAND_2GHZ] =
1988 		&adapter->sbands[NL80211_BAND_2GHZ];
1989 	if (common->num_supp_bands > 1) {
1990 		rsi_register_rates_channels(adapter, NL80211_BAND_5GHZ);
1991 		wiphy->bands[NL80211_BAND_5GHZ] =
1992 			&adapter->sbands[NL80211_BAND_5GHZ];
1993 	}
1994 
1995 	/* AP Parameters */
1996 	wiphy->max_ap_assoc_sta = rsi_max_ap_stas[common->oper_mode - 1];
1997 	common->max_stations = wiphy->max_ap_assoc_sta;
1998 	rsi_dbg(ERR_ZONE, "Max Stations Allowed = %d\n", common->max_stations);
1999 	hw->sta_data_size = sizeof(struct rsi_sta);
2000 	wiphy->flags = WIPHY_FLAG_REPORTS_OBSS;
2001 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
2002 	wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER;
2003 	wiphy->reg_notifier = rsi_reg_notify;
2004 
2005 #ifdef CONFIG_PM
2006 	wiphy->wowlan = &rsi_wowlan_support;
2007 #endif
2008 
2009 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2010 
2011 	/* Wi-Fi direct parameters */
2012 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
2013 	wiphy->flags |= WIPHY_FLAG_OFFCHAN_TX;
2014 	wiphy->max_remain_on_channel_duration = 10000;
2015 	hw->max_listen_interval = 10;
2016 	wiphy->iface_combinations = rsi_iface_combinations;
2017 	wiphy->n_iface_combinations = ARRAY_SIZE(rsi_iface_combinations);
2018 
2019 	if (common->coex_mode > 1)
2020 		wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
2021 
2022 	status = ieee80211_register_hw(hw);
2023 	if (status)
2024 		return status;
2025 
2026 	return rsi_init_dbgfs(adapter);
2027 }
2028