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