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