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