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