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_common.h"
21 #include "rsi_ps.h"
22 
23 static const struct ieee80211_channel rsi_2ghz_channels[] = {
24 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2412,
25 	  .hw_value = 1 }, /* Channel 1 */
26 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2417,
27 	  .hw_value = 2 }, /* Channel 2 */
28 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2422,
29 	  .hw_value = 3 }, /* Channel 3 */
30 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2427,
31 	  .hw_value = 4 }, /* Channel 4 */
32 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2432,
33 	  .hw_value = 5 }, /* Channel 5 */
34 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2437,
35 	  .hw_value = 6 }, /* Channel 6 */
36 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2442,
37 	  .hw_value = 7 }, /* Channel 7 */
38 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2447,
39 	  .hw_value = 8 }, /* Channel 8 */
40 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2452,
41 	  .hw_value = 9 }, /* Channel 9 */
42 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2457,
43 	  .hw_value = 10 }, /* Channel 10 */
44 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2462,
45 	  .hw_value = 11 }, /* Channel 11 */
46 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2467,
47 	  .hw_value = 12 }, /* Channel 12 */
48 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2472,
49 	  .hw_value = 13 }, /* Channel 13 */
50 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2484,
51 	  .hw_value = 14 }, /* Channel 14 */
52 };
53 
54 static const struct ieee80211_channel rsi_5ghz_channels[] = {
55 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5180,
56 	  .hw_value = 36,  }, /* Channel 36 */
57 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5200,
58 	  .hw_value = 40, }, /* Channel 40 */
59 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5220,
60 	  .hw_value = 44, }, /* Channel 44 */
61 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5240,
62 	  .hw_value = 48, }, /* Channel 48 */
63 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5260,
64 	  .hw_value = 52, }, /* Channel 52 */
65 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5280,
66 	  .hw_value = 56, }, /* Channel 56 */
67 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5300,
68 	  .hw_value = 60, }, /* Channel 60 */
69 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5320,
70 	  .hw_value = 64, }, /* Channel 64 */
71 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5500,
72 	  .hw_value = 100, }, /* Channel 100 */
73 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5520,
74 	  .hw_value = 104, }, /* Channel 104 */
75 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5540,
76 	  .hw_value = 108, }, /* Channel 108 */
77 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5560,
78 	  .hw_value = 112, }, /* Channel 112 */
79 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5580,
80 	  .hw_value = 116, }, /* Channel 116 */
81 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5600,
82 	  .hw_value = 120, }, /* Channel 120 */
83 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5620,
84 	  .hw_value = 124, }, /* Channel 124 */
85 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5640,
86 	  .hw_value = 128, }, /* Channel 128 */
87 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5660,
88 	  .hw_value = 132, }, /* Channel 132 */
89 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5680,
90 	  .hw_value = 136, }, /* Channel 136 */
91 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5700,
92 	  .hw_value = 140, }, /* Channel 140 */
93 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5745,
94 	  .hw_value = 149, }, /* Channel 149 */
95 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5765,
96 	  .hw_value = 153, }, /* Channel 153 */
97 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5785,
98 	  .hw_value = 157, }, /* Channel 157 */
99 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5805,
100 	  .hw_value = 161, }, /* Channel 161 */
101 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5825,
102 	  .hw_value = 165, }, /* Channel 165 */
103 };
104 
105 struct ieee80211_rate rsi_rates[12] = {
106 	{ .bitrate = STD_RATE_01  * 5, .hw_value = RSI_RATE_1 },
107 	{ .bitrate = STD_RATE_02  * 5, .hw_value = RSI_RATE_2 },
108 	{ .bitrate = STD_RATE_5_5 * 5, .hw_value = RSI_RATE_5_5 },
109 	{ .bitrate = STD_RATE_11  * 5, .hw_value = RSI_RATE_11 },
110 	{ .bitrate = STD_RATE_06  * 5, .hw_value = RSI_RATE_6 },
111 	{ .bitrate = STD_RATE_09  * 5, .hw_value = RSI_RATE_9 },
112 	{ .bitrate = STD_RATE_12  * 5, .hw_value = RSI_RATE_12 },
113 	{ .bitrate = STD_RATE_18  * 5, .hw_value = RSI_RATE_18 },
114 	{ .bitrate = STD_RATE_24  * 5, .hw_value = RSI_RATE_24 },
115 	{ .bitrate = STD_RATE_36  * 5, .hw_value = RSI_RATE_36 },
116 	{ .bitrate = STD_RATE_48  * 5, .hw_value = RSI_RATE_48 },
117 	{ .bitrate = STD_RATE_54  * 5, .hw_value = RSI_RATE_54 },
118 };
119 
120 const u16 rsi_mcsrates[8] = {
121 	RSI_RATE_MCS0, RSI_RATE_MCS1, RSI_RATE_MCS2, RSI_RATE_MCS3,
122 	RSI_RATE_MCS4, RSI_RATE_MCS5, RSI_RATE_MCS6, RSI_RATE_MCS7
123 };
124 
125 static const u32 rsi_max_ap_stas[16] = {
126 	32,	/* 1 - Wi-Fi alone */
127 	0,	/* 2 */
128 	0,	/* 3 */
129 	0,	/* 4 - BT EDR alone */
130 	4,	/* 5 - STA + BT EDR */
131 	32,	/* 6 - AP + BT EDR */
132 	0,	/* 7 */
133 	0,	/* 8 - BT LE alone */
134 	4,	/* 9 - STA + BE LE */
135 	0,	/* 10 */
136 	0,	/* 11 */
137 	0,	/* 12 */
138 	1,	/* 13 - STA + BT Dual */
139 	4,	/* 14 - AP + BT Dual */
140 };
141 
142 /**
143  * rsi_is_cipher_wep() -  This function determines if the cipher is WEP or not.
144  * @common: Pointer to the driver private structure.
145  *
146  * Return: If cipher type is WEP, a value of 1 is returned, else 0.
147  */
148 
149 bool rsi_is_cipher_wep(struct rsi_common *common)
150 {
151 	if (((common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP104) ||
152 	     (common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP40)) &&
153 	    (!common->secinfo.ptk_cipher))
154 		return true;
155 	else
156 		return false;
157 }
158 
159 /**
160  * rsi_register_rates_channels() - This function registers channels and rates.
161  * @adapter: Pointer to the adapter structure.
162  * @band: Operating band to be set.
163  *
164  * Return: None.
165  */
166 static void rsi_register_rates_channels(struct rsi_hw *adapter, int band)
167 {
168 	struct ieee80211_supported_band *sbands = &adapter->sbands[band];
169 	void *channels = NULL;
170 
171 	if (band == NL80211_BAND_2GHZ) {
172 		channels = kmalloc(sizeof(rsi_2ghz_channels), GFP_KERNEL);
173 		memcpy(channels,
174 		       rsi_2ghz_channels,
175 		       sizeof(rsi_2ghz_channels));
176 		sbands->band = NL80211_BAND_2GHZ;
177 		sbands->n_channels = ARRAY_SIZE(rsi_2ghz_channels);
178 		sbands->bitrates = rsi_rates;
179 		sbands->n_bitrates = ARRAY_SIZE(rsi_rates);
180 	} else {
181 		channels = kmalloc(sizeof(rsi_5ghz_channels), GFP_KERNEL);
182 		memcpy(channels,
183 		       rsi_5ghz_channels,
184 		       sizeof(rsi_5ghz_channels));
185 		sbands->band = NL80211_BAND_5GHZ;
186 		sbands->n_channels = ARRAY_SIZE(rsi_5ghz_channels);
187 		sbands->bitrates = &rsi_rates[4];
188 		sbands->n_bitrates = ARRAY_SIZE(rsi_rates) - 4;
189 	}
190 
191 	sbands->channels = channels;
192 
193 	memset(&sbands->ht_cap, 0, sizeof(struct ieee80211_sta_ht_cap));
194 	sbands->ht_cap.ht_supported = true;
195 	sbands->ht_cap.cap = (IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
196 			      IEEE80211_HT_CAP_SGI_20 |
197 			      IEEE80211_HT_CAP_SGI_40);
198 	sbands->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K;
199 	sbands->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
200 	sbands->ht_cap.mcs.rx_mask[0] = 0xff;
201 	sbands->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
202 	/* sbands->ht_cap.mcs.rx_highest = 0x82; */
203 }
204 
205 /**
206  * rsi_mac80211_detach() - This function is used to de-initialize the
207  *			   Mac80211 stack.
208  * @adapter: Pointer to the adapter structure.
209  *
210  * Return: None.
211  */
212 void rsi_mac80211_detach(struct rsi_hw *adapter)
213 {
214 	struct ieee80211_hw *hw = adapter->hw;
215 	enum nl80211_band band;
216 
217 	if (hw) {
218 		ieee80211_stop_queues(hw);
219 		ieee80211_unregister_hw(hw);
220 		ieee80211_free_hw(hw);
221 	}
222 
223 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
224 		struct ieee80211_supported_band *sband =
225 					&adapter->sbands[band];
226 
227 		kfree(sband->channels);
228 	}
229 
230 #ifdef CONFIG_RSI_DEBUGFS
231 	rsi_remove_dbgfs(adapter);
232 	kfree(adapter->dfsentry);
233 #endif
234 }
235 EXPORT_SYMBOL_GPL(rsi_mac80211_detach);
236 
237 /**
238  * rsi_indicate_tx_status() - This function indicates the transmit status.
239  * @adapter: Pointer to the adapter structure.
240  * @skb: Pointer to the socket buffer structure.
241  * @status: Status
242  *
243  * Return: None.
244  */
245 void rsi_indicate_tx_status(struct rsi_hw *adapter,
246 			    struct sk_buff *skb,
247 			    int status)
248 {
249 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
250 	struct skb_info *tx_params;
251 
252 	if (!adapter->hw) {
253 		rsi_dbg(ERR_ZONE, "##### No MAC #####\n");
254 		return;
255 	}
256 
257 	if (!status)
258 		info->flags |= IEEE80211_TX_STAT_ACK;
259 
260 	tx_params = (struct skb_info *)info->driver_data;
261 	skb_pull(skb, tx_params->internal_hdr_size);
262 	memset(info->driver_data, 0, IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
263 
264 	ieee80211_tx_status_irqsafe(adapter->hw, skb);
265 }
266 
267 /**
268  * rsi_mac80211_tx() - This is the handler that 802.11 module calls for each
269  *		       transmitted frame.SKB contains the buffer starting
270  *		       from the IEEE 802.11 header.
271  * @hw: Pointer to the ieee80211_hw structure.
272  * @control: Pointer to the ieee80211_tx_control structure
273  * @skb: Pointer to the socket buffer structure.
274  *
275  * Return: None
276  */
277 static void rsi_mac80211_tx(struct ieee80211_hw *hw,
278 			    struct ieee80211_tx_control *control,
279 			    struct sk_buff *skb)
280 {
281 	struct rsi_hw *adapter = hw->priv;
282 	struct rsi_common *common = adapter->priv;
283 
284 	rsi_core_xmit(common, skb);
285 }
286 
287 /**
288  * rsi_mac80211_start() - This is first handler that 802.11 module calls, since
289  *			  the driver init is complete by then, just
290  *			  returns success.
291  * @hw: Pointer to the ieee80211_hw structure.
292  *
293  * Return: 0 as success.
294  */
295 static int rsi_mac80211_start(struct ieee80211_hw *hw)
296 {
297 	struct rsi_hw *adapter = hw->priv;
298 	struct rsi_common *common = adapter->priv;
299 
300 	rsi_dbg(ERR_ZONE, "===> Interface UP <===\n");
301 	mutex_lock(&common->mutex);
302 	common->iface_down = false;
303 	wiphy_rfkill_start_polling(hw->wiphy);
304 	rsi_send_rx_filter_frame(common, 0);
305 	mutex_unlock(&common->mutex);
306 
307 	return 0;
308 }
309 
310 /**
311  * rsi_mac80211_stop() - This is the last handler that 802.11 module calls.
312  * @hw: Pointer to the ieee80211_hw structure.
313  *
314  * Return: None.
315  */
316 static void rsi_mac80211_stop(struct ieee80211_hw *hw)
317 {
318 	struct rsi_hw *adapter = hw->priv;
319 	struct rsi_common *common = adapter->priv;
320 
321 	rsi_dbg(ERR_ZONE, "===> Interface DOWN <===\n");
322 	mutex_lock(&common->mutex);
323 	common->iface_down = true;
324 	wiphy_rfkill_stop_polling(hw->wiphy);
325 
326 	/* Block all rx frames */
327 	rsi_send_rx_filter_frame(common, 0xffff);
328 
329 	mutex_unlock(&common->mutex);
330 }
331 
332 /**
333  * rsi_mac80211_add_interface() - This function is called when a netdevice
334  *				  attached to the hardware is enabled.
335  * @hw: Pointer to the ieee80211_hw structure.
336  * @vif: Pointer to the ieee80211_vif structure.
337  *
338  * Return: ret: 0 on success, negative error code on failure.
339  */
340 static int rsi_mac80211_add_interface(struct ieee80211_hw *hw,
341 				      struct ieee80211_vif *vif)
342 {
343 	struct rsi_hw *adapter = hw->priv;
344 	struct rsi_common *common = adapter->priv;
345 	enum opmode intf_mode;
346 	int ret = -EOPNOTSUPP;
347 
348 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
349 	mutex_lock(&common->mutex);
350 
351 	if (adapter->sc_nvifs > 1) {
352 		mutex_unlock(&common->mutex);
353 		return -EOPNOTSUPP;
354 	}
355 
356 	switch (vif->type) {
357 	case NL80211_IFTYPE_STATION:
358 		rsi_dbg(INFO_ZONE, "Station Mode");
359 		intf_mode = STA_OPMODE;
360 		break;
361 	case NL80211_IFTYPE_AP:
362 		rsi_dbg(INFO_ZONE, "AP Mode");
363 		intf_mode = AP_OPMODE;
364 		break;
365 	default:
366 		rsi_dbg(ERR_ZONE,
367 			"%s: Interface type %d not supported\n", __func__,
368 			vif->type);
369 		goto out;
370 	}
371 
372 	adapter->vifs[adapter->sc_nvifs++] = vif;
373 	ret = rsi_set_vap_capabilities(common, intf_mode, common->mac_addr,
374 				       0, VAP_ADD);
375 	if (ret) {
376 		rsi_dbg(ERR_ZONE, "Failed to set VAP capabilities\n");
377 		goto out;
378 	}
379 
380 	if (vif->type == NL80211_IFTYPE_AP) {
381 		int i;
382 
383 		rsi_send_rx_filter_frame(common, DISALLOW_BEACONS);
384 		common->min_rate = RSI_RATE_AUTO;
385 		for (i = 0; i < common->max_stations; i++)
386 			common->stations[i].sta = NULL;
387 	}
388 
389 out:
390 	mutex_unlock(&common->mutex);
391 
392 	return ret;
393 }
394 
395 /**
396  * rsi_mac80211_remove_interface() - This function notifies driver that an
397  *				     interface is going down.
398  * @hw: Pointer to the ieee80211_hw structure.
399  * @vif: Pointer to the ieee80211_vif structure.
400  *
401  * Return: None.
402  */
403 static void rsi_mac80211_remove_interface(struct ieee80211_hw *hw,
404 					  struct ieee80211_vif *vif)
405 {
406 	struct rsi_hw *adapter = hw->priv;
407 	struct rsi_common *common = adapter->priv;
408 	enum opmode opmode;
409 
410 	rsi_dbg(INFO_ZONE, "Remove Interface Called\n");
411 
412 	mutex_lock(&common->mutex);
413 
414 	if (adapter->sc_nvifs <= 0) {
415 		mutex_unlock(&common->mutex);
416 		return;
417 	}
418 
419 	switch (vif->type) {
420 	case NL80211_IFTYPE_STATION:
421 		opmode = STA_OPMODE;
422 		break;
423 	case NL80211_IFTYPE_AP:
424 		opmode = AP_OPMODE;
425 		break;
426 	default:
427 		mutex_unlock(&common->mutex);
428 		return;
429 	}
430 	rsi_set_vap_capabilities(common, opmode, vif->addr,
431 				 0, VAP_DELETE);
432 	adapter->sc_nvifs--;
433 
434 	if (!memcmp(adapter->vifs[0], vif, sizeof(struct ieee80211_vif)))
435 		adapter->vifs[0] = NULL;
436 	mutex_unlock(&common->mutex);
437 }
438 
439 /**
440  * rsi_channel_change() - This function is a performs the checks
441  *			  required for changing a channel and sets
442  *			  the channel accordingly.
443  * @hw: Pointer to the ieee80211_hw structure.
444  *
445  * Return: 0 on success, negative error code on failure.
446  */
447 static int rsi_channel_change(struct ieee80211_hw *hw)
448 {
449 	struct rsi_hw *adapter = hw->priv;
450 	struct rsi_common *common = adapter->priv;
451 	int status = -EOPNOTSUPP;
452 	struct ieee80211_channel *curchan = hw->conf.chandef.chan;
453 	u16 channel = curchan->hw_value;
454 	struct ieee80211_bss_conf *bss = &adapter->vifs[0]->bss_conf;
455 
456 	rsi_dbg(INFO_ZONE,
457 		"%s: Set channel: %d MHz type: %d channel_no %d\n",
458 		__func__, curchan->center_freq,
459 		curchan->flags, channel);
460 
461 	if (bss->assoc) {
462 		if (!common->hw_data_qs_blocked &&
463 		    (rsi_get_connected_channel(adapter) != channel)) {
464 			rsi_dbg(INFO_ZONE, "blk data q %d\n", channel);
465 			if (!rsi_send_block_unblock_frame(common, true))
466 				common->hw_data_qs_blocked = true;
467 		}
468 	}
469 
470 	status = rsi_band_check(common);
471 	if (!status)
472 		status = rsi_set_channel(adapter->priv, curchan);
473 
474 	if (bss->assoc) {
475 		if (common->hw_data_qs_blocked &&
476 		    (rsi_get_connected_channel(adapter) == channel)) {
477 			rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
478 			if (!rsi_send_block_unblock_frame(common, false))
479 				common->hw_data_qs_blocked = false;
480 		}
481 	} else {
482 		if (common->hw_data_qs_blocked) {
483 			rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
484 			if (!rsi_send_block_unblock_frame(common, false))
485 				common->hw_data_qs_blocked = false;
486 		}
487 	}
488 
489 	return status;
490 }
491 
492 /**
493  * rsi_config_power() - This function configures tx power to device
494  * @hw: Pointer to the ieee80211_hw structure.
495  *
496  * Return: 0 on success, negative error code on failure.
497  */
498 static int rsi_config_power(struct ieee80211_hw *hw)
499 {
500 	struct rsi_hw *adapter = hw->priv;
501 	struct rsi_common *common = adapter->priv;
502 	struct ieee80211_conf *conf = &hw->conf;
503 
504 	if (adapter->sc_nvifs <= 0) {
505 		rsi_dbg(ERR_ZONE, "%s: No virtual interface found\n", __func__);
506 		return -EINVAL;
507 	}
508 
509 	rsi_dbg(INFO_ZONE,
510 		"%s: Set tx power: %d dBM\n", __func__, conf->power_level);
511 
512 	if (conf->power_level == common->tx_power)
513 		return 0;
514 
515 	common->tx_power = conf->power_level;
516 
517 	return rsi_send_radio_params_update(common);
518 }
519 
520 /**
521  * rsi_mac80211_config() - This function is a handler for configuration
522  *			   requests. The stack calls this function to
523  *			   change hardware configuration, e.g., channel.
524  * @hw: Pointer to the ieee80211_hw structure.
525  * @changed: Changed flags set.
526  *
527  * Return: 0 on success, negative error code on failure.
528  */
529 static int rsi_mac80211_config(struct ieee80211_hw *hw,
530 			       u32 changed)
531 {
532 	struct rsi_hw *adapter = hw->priv;
533 	struct rsi_common *common = adapter->priv;
534 	struct ieee80211_vif *vif = adapter->vifs[0];
535 	struct ieee80211_conf *conf = &hw->conf;
536 	int status = -EOPNOTSUPP;
537 
538 	mutex_lock(&common->mutex);
539 
540 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
541 		status = rsi_channel_change(hw);
542 
543 	/* tx power */
544 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
545 		rsi_dbg(INFO_ZONE, "%s: Configuring Power\n", __func__);
546 		status = rsi_config_power(hw);
547 	}
548 
549 	/* Power save parameters */
550 	if ((changed & IEEE80211_CONF_CHANGE_PS) &&
551 	    (vif->type == NL80211_IFTYPE_STATION)) {
552 		unsigned long flags;
553 
554 		spin_lock_irqsave(&adapter->ps_lock, flags);
555 		if (conf->flags & IEEE80211_CONF_PS)
556 			rsi_enable_ps(adapter);
557 		else
558 			rsi_disable_ps(adapter);
559 		spin_unlock_irqrestore(&adapter->ps_lock, flags);
560 	}
561 
562 	/* RTS threshold */
563 	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
564 		rsi_dbg(INFO_ZONE, "RTS threshold\n");
565 		if ((common->rts_threshold) <= IEEE80211_MAX_RTS_THRESHOLD) {
566 			rsi_dbg(INFO_ZONE,
567 				"%s: Sending vap updates....\n", __func__);
568 			status = rsi_send_vap_dynamic_update(common);
569 		}
570 	}
571 	mutex_unlock(&common->mutex);
572 
573 	return status;
574 }
575 
576 /**
577  * rsi_get_connected_channel() - This function is used to get the current
578  *				 connected channel number.
579  * @adapter: Pointer to the adapter structure.
580  *
581  * Return: Current connected AP's channel number is returned.
582  */
583 u16 rsi_get_connected_channel(struct rsi_hw *adapter)
584 {
585 	struct ieee80211_vif *vif = adapter->vifs[0];
586 	if (vif) {
587 		struct ieee80211_bss_conf *bss = &vif->bss_conf;
588 		struct ieee80211_channel *channel = bss->chandef.chan;
589 		return channel->hw_value;
590 	}
591 
592 	return 0;
593 }
594 
595 /**
596  * rsi_mac80211_bss_info_changed() - This function is a handler for config
597  *				     requests related to BSS parameters that
598  *				     may vary during BSS's lifespan.
599  * @hw: Pointer to the ieee80211_hw structure.
600  * @vif: Pointer to the ieee80211_vif structure.
601  * @bss_conf: Pointer to the ieee80211_bss_conf structure.
602  * @changed: Changed flags set.
603  *
604  * Return: None.
605  */
606 static void rsi_mac80211_bss_info_changed(struct ieee80211_hw *hw,
607 					  struct ieee80211_vif *vif,
608 					  struct ieee80211_bss_conf *bss_conf,
609 					  u32 changed)
610 {
611 	struct rsi_hw *adapter = hw->priv;
612 	struct rsi_common *common = adapter->priv;
613 	struct ieee80211_bss_conf *bss = &vif->bss_conf;
614 	struct ieee80211_conf *conf = &hw->conf;
615 	u16 rx_filter_word = 0;
616 
617 	mutex_lock(&common->mutex);
618 	if (changed & BSS_CHANGED_ASSOC) {
619 		rsi_dbg(INFO_ZONE, "%s: Changed Association status: %d\n",
620 			__func__, bss_conf->assoc);
621 		if (bss_conf->assoc) {
622 			/* Send the RX filter frame */
623 			rx_filter_word = (ALLOW_DATA_ASSOC_PEER |
624 					  ALLOW_CTRL_ASSOC_PEER |
625 					  ALLOW_MGMT_ASSOC_PEER);
626 			rsi_send_rx_filter_frame(common, rx_filter_word);
627 		}
628 		rsi_inform_bss_status(common,
629 				      STA_OPMODE,
630 				      bss_conf->assoc,
631 				      bss_conf->bssid,
632 				      bss_conf->qos,
633 				      bss_conf->aid,
634 				      NULL, 0);
635 		adapter->ps_info.dtim_interval_duration = bss->dtim_period;
636 		adapter->ps_info.listen_interval = conf->listen_interval;
637 
638 	/* If U-APSD is updated, send ps parameters to firmware */
639 	if (bss->assoc) {
640 		if (common->uapsd_bitmap) {
641 			rsi_dbg(INFO_ZONE, "Configuring UAPSD\n");
642 			rsi_conf_uapsd(adapter);
643 		}
644 	} else {
645 		common->uapsd_bitmap = 0;
646 	}
647 	}
648 
649 	if (changed & BSS_CHANGED_CQM) {
650 		common->cqm_info.last_cqm_event_rssi = 0;
651 		common->cqm_info.rssi_thold = bss_conf->cqm_rssi_thold;
652 		common->cqm_info.rssi_hyst = bss_conf->cqm_rssi_hyst;
653 		rsi_dbg(INFO_ZONE, "RSSI throld & hysteresis are: %d %d\n",
654 			common->cqm_info.rssi_thold,
655 			common->cqm_info.rssi_hyst);
656 	}
657 
658 	if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
659 	    (vif->type == NL80211_IFTYPE_AP)) {
660 		if (bss->enable_beacon) {
661 			rsi_dbg(INFO_ZONE, "===> BEACON ENABLED <===\n");
662 			common->beacon_enabled = 1;
663 		} else {
664 			rsi_dbg(INFO_ZONE, "===> BEACON DISABLED <===\n");
665 			common->beacon_enabled = 0;
666 		}
667 	}
668 
669 	mutex_unlock(&common->mutex);
670 }
671 
672 /**
673  * rsi_mac80211_conf_filter() - This function configure the device's RX filter.
674  * @hw: Pointer to the ieee80211_hw structure.
675  * @changed: Changed flags set.
676  * @total_flags: Total initial flags set.
677  * @multicast: Multicast.
678  *
679  * Return: None.
680  */
681 static void rsi_mac80211_conf_filter(struct ieee80211_hw *hw,
682 				     u32 changed_flags,
683 				     u32 *total_flags,
684 				     u64 multicast)
685 {
686 	/* Not doing much here as of now */
687 	*total_flags &= RSI_SUPP_FILTERS;
688 }
689 
690 /**
691  * rsi_mac80211_conf_tx() - This function configures TX queue parameters
692  *			    (EDCF (aifs, cw_min, cw_max), bursting)
693  *			    for a hardware TX queue.
694  * @hw: Pointer to the ieee80211_hw structure
695  * @vif: Pointer to the ieee80211_vif structure.
696  * @queue: Queue number.
697  * @params: Pointer to ieee80211_tx_queue_params structure.
698  *
699  * Return: 0 on success, negative error code on failure.
700  */
701 static int rsi_mac80211_conf_tx(struct ieee80211_hw *hw,
702 				struct ieee80211_vif *vif, u16 queue,
703 				const struct ieee80211_tx_queue_params *params)
704 {
705 	struct rsi_hw *adapter = hw->priv;
706 	struct rsi_common *common = adapter->priv;
707 	u8 idx = 0;
708 
709 	if (queue >= IEEE80211_NUM_ACS)
710 		return 0;
711 
712 	rsi_dbg(INFO_ZONE,
713 		"%s: Conf queue %d, aifs: %d, cwmin: %d cwmax: %d, txop: %d\n",
714 		__func__, queue, params->aifs,
715 		params->cw_min, params->cw_max, params->txop);
716 
717 	mutex_lock(&common->mutex);
718 	/* Map into the way the f/w expects */
719 	switch (queue) {
720 	case IEEE80211_AC_VO:
721 		idx = VO_Q;
722 		break;
723 	case IEEE80211_AC_VI:
724 		idx = VI_Q;
725 		break;
726 	case IEEE80211_AC_BE:
727 		idx = BE_Q;
728 		break;
729 	case IEEE80211_AC_BK:
730 		idx = BK_Q;
731 		break;
732 	default:
733 		idx = BE_Q;
734 		break;
735 	}
736 
737 	memcpy(&common->edca_params[idx],
738 	       params,
739 	       sizeof(struct ieee80211_tx_queue_params));
740 
741 	if (params->uapsd)
742 		common->uapsd_bitmap |= idx;
743 	else
744 		common->uapsd_bitmap &= (~idx);
745 
746 	mutex_unlock(&common->mutex);
747 
748 	return 0;
749 }
750 
751 /**
752  * rsi_hal_key_config() - This function loads the keys into the firmware.
753  * @hw: Pointer to the ieee80211_hw structure.
754  * @vif: Pointer to the ieee80211_vif structure.
755  * @key: Pointer to the ieee80211_key_conf structure.
756  *
757  * Return: status: 0 on success, negative error codes on failure.
758  */
759 static int rsi_hal_key_config(struct ieee80211_hw *hw,
760 			      struct ieee80211_vif *vif,
761 			      struct ieee80211_key_conf *key,
762 			      struct ieee80211_sta *sta)
763 {
764 	struct rsi_hw *adapter = hw->priv;
765 	struct rsi_sta *rsta = NULL;
766 	int status;
767 	u8 key_type;
768 	s16 sta_id = 0;
769 
770 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
771 		key_type = RSI_PAIRWISE_KEY;
772 	else
773 		key_type = RSI_GROUP_KEY;
774 
775 	rsi_dbg(ERR_ZONE, "%s: Cipher 0x%x key_type: %d key_len: %d\n",
776 		__func__, key->cipher, key_type, key->keylen);
777 
778 	if (vif->type == NL80211_IFTYPE_AP) {
779 		if (sta) {
780 			rsta = rsi_find_sta(adapter->priv, sta->addr);
781 			if (rsta)
782 				sta_id = rsta->sta_id;
783 		}
784 		adapter->priv->key = key;
785 	} else {
786 		if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
787 		    (key->cipher == WLAN_CIPHER_SUITE_WEP40)) {
788 			status = rsi_hal_load_key(adapter->priv,
789 						  key->key,
790 						  key->keylen,
791 						  RSI_PAIRWISE_KEY,
792 						  key->keyidx,
793 						  key->cipher,
794 						  sta_id);
795 			if (status)
796 				return status;
797 		}
798 	}
799 
800 	return rsi_hal_load_key(adapter->priv,
801 				key->key,
802 				key->keylen,
803 				key_type,
804 				key->keyidx,
805 				key->cipher,
806 				sta_id);
807 }
808 
809 /**
810  * rsi_mac80211_set_key() - This function sets type of key to be loaded.
811  * @hw: Pointer to the ieee80211_hw structure.
812  * @cmd: enum set_key_cmd.
813  * @vif: Pointer to the ieee80211_vif structure.
814  * @sta: Pointer to the ieee80211_sta structure.
815  * @key: Pointer to the ieee80211_key_conf structure.
816  *
817  * Return: status: 0 on success, negative error code on failure.
818  */
819 static int rsi_mac80211_set_key(struct ieee80211_hw *hw,
820 				enum set_key_cmd cmd,
821 				struct ieee80211_vif *vif,
822 				struct ieee80211_sta *sta,
823 				struct ieee80211_key_conf *key)
824 {
825 	struct rsi_hw *adapter = hw->priv;
826 	struct rsi_common *common = adapter->priv;
827 	struct security_info *secinfo = &common->secinfo;
828 	int status;
829 
830 	mutex_lock(&common->mutex);
831 	switch (cmd) {
832 	case SET_KEY:
833 		secinfo->security_enable = true;
834 		status = rsi_hal_key_config(hw, vif, key, sta);
835 		if (status) {
836 			mutex_unlock(&common->mutex);
837 			return status;
838 		}
839 
840 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
841 			secinfo->ptk_cipher = key->cipher;
842 		else
843 			secinfo->gtk_cipher = key->cipher;
844 
845 		key->hw_key_idx = key->keyidx;
846 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
847 
848 		rsi_dbg(ERR_ZONE, "%s: RSI set_key\n", __func__);
849 		break;
850 
851 	case DISABLE_KEY:
852 		if (vif->type == NL80211_IFTYPE_STATION)
853 			secinfo->security_enable = false;
854 		rsi_dbg(ERR_ZONE, "%s: RSI del key\n", __func__);
855 		memset(key, 0, sizeof(struct ieee80211_key_conf));
856 		status = rsi_hal_key_config(hw, vif, key, sta);
857 		break;
858 
859 	default:
860 		status = -EOPNOTSUPP;
861 		break;
862 	}
863 
864 	mutex_unlock(&common->mutex);
865 	return status;
866 }
867 
868 /**
869  * rsi_mac80211_ampdu_action() - This function selects the AMPDU action for
870  *				 the corresponding mlme_action flag and
871  *				 informs the f/w regarding this.
872  * @hw: Pointer to the ieee80211_hw structure.
873  * @vif: Pointer to the ieee80211_vif structure.
874  * @params: Pointer to A-MPDU action parameters
875  *
876  * Return: status: 0 on success, negative error code on failure.
877  */
878 static int rsi_mac80211_ampdu_action(struct ieee80211_hw *hw,
879 				     struct ieee80211_vif *vif,
880 				     struct ieee80211_ampdu_params *params)
881 {
882 	int status = -EOPNOTSUPP;
883 	struct rsi_hw *adapter = hw->priv;
884 	struct rsi_common *common = adapter->priv;
885 	struct rsi_sta *rsta = NULL;
886 	u16 seq_no = 0, seq_start = 0;
887 	u8 ii = 0;
888 	struct ieee80211_sta *sta = params->sta;
889 	u8 sta_id = 0;
890 	enum ieee80211_ampdu_mlme_action action = params->action;
891 	u16 tid = params->tid;
892 	u16 *ssn = &params->ssn;
893 	u8 buf_size = params->buf_size;
894 
895 	for (ii = 0; ii < RSI_MAX_VIFS; ii++) {
896 		if (vif == adapter->vifs[ii])
897 			break;
898 	}
899 
900 	mutex_lock(&common->mutex);
901 
902 	if (ssn != NULL)
903 		seq_no = *ssn;
904 
905 	if (vif->type == NL80211_IFTYPE_AP) {
906 		rsta = rsi_find_sta(common, sta->addr);
907 		if (!rsta) {
908 			rsi_dbg(ERR_ZONE, "No station mapped\n");
909 			status = 0;
910 			goto unlock;
911 		}
912 		sta_id = rsta->sta_id;
913 	}
914 
915 	rsi_dbg(INFO_ZONE,
916 		"%s: AMPDU action tid=%d ssn=0x%x, buf_size=%d sta_id=%d\n",
917 		__func__, tid, seq_no, buf_size, sta_id);
918 
919 	switch (action) {
920 	case IEEE80211_AMPDU_RX_START:
921 		status = rsi_send_aggregation_params_frame(common,
922 							   tid,
923 							   seq_no,
924 							   buf_size,
925 							   STA_RX_ADDBA_DONE,
926 							   sta_id);
927 		break;
928 
929 	case IEEE80211_AMPDU_RX_STOP:
930 		status = rsi_send_aggregation_params_frame(common,
931 							   tid,
932 							   0,
933 							   buf_size,
934 							   STA_RX_DELBA,
935 							   sta_id);
936 		break;
937 
938 	case IEEE80211_AMPDU_TX_START:
939 		if (vif->type == NL80211_IFTYPE_STATION)
940 			common->vif_info[ii].seq_start = seq_no;
941 		else if (vif->type == NL80211_IFTYPE_AP)
942 			rsta->seq_start[tid] = seq_no;
943 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
944 		status = 0;
945 		break;
946 
947 	case IEEE80211_AMPDU_TX_STOP_CONT:
948 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
949 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
950 		status = rsi_send_aggregation_params_frame(common,
951 							   tid,
952 							   seq_no,
953 							   buf_size,
954 							   STA_TX_DELBA,
955 							   sta_id);
956 		if (!status)
957 			ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
958 		break;
959 
960 	case IEEE80211_AMPDU_TX_OPERATIONAL:
961 		if (vif->type == NL80211_IFTYPE_STATION)
962 			seq_start = common->vif_info[ii].seq_start;
963 		else if (vif->type == NL80211_IFTYPE_AP)
964 			seq_start = rsta->seq_start[tid];
965 		status = rsi_send_aggregation_params_frame(common,
966 							   tid,
967 							   seq_start,
968 							   buf_size,
969 							   STA_TX_ADDBA_DONE,
970 							   sta_id);
971 		break;
972 
973 	default:
974 		rsi_dbg(ERR_ZONE, "%s: Uknown AMPDU action\n", __func__);
975 		break;
976 	}
977 
978 unlock:
979 	mutex_unlock(&common->mutex);
980 	return status;
981 }
982 
983 /**
984  * rsi_mac80211_set_rts_threshold() - This function sets rts threshold value.
985  * @hw: Pointer to the ieee80211_hw structure.
986  * @value: Rts threshold value.
987  *
988  * Return: 0 on success.
989  */
990 static int rsi_mac80211_set_rts_threshold(struct ieee80211_hw *hw,
991 					  u32 value)
992 {
993 	struct rsi_hw *adapter = hw->priv;
994 	struct rsi_common *common = adapter->priv;
995 
996 	mutex_lock(&common->mutex);
997 	common->rts_threshold = value;
998 	mutex_unlock(&common->mutex);
999 
1000 	return 0;
1001 }
1002 
1003 /**
1004  * rsi_mac80211_set_rate_mask() - This function sets bitrate_mask to be used.
1005  * @hw: Pointer to the ieee80211_hw structure
1006  * @vif: Pointer to the ieee80211_vif structure.
1007  * @mask: Pointer to the cfg80211_bitrate_mask structure.
1008  *
1009  * Return: 0 on success.
1010  */
1011 static int rsi_mac80211_set_rate_mask(struct ieee80211_hw *hw,
1012 				      struct ieee80211_vif *vif,
1013 				      const struct cfg80211_bitrate_mask *mask)
1014 {
1015 	struct rsi_hw *adapter = hw->priv;
1016 	struct rsi_common *common = adapter->priv;
1017 	enum nl80211_band band = hw->conf.chandef.chan->band;
1018 
1019 	mutex_lock(&common->mutex);
1020 	common->fixedrate_mask[band] = 0;
1021 
1022 	if (mask->control[band].legacy == 0xfff) {
1023 		common->fixedrate_mask[band] =
1024 			(mask->control[band].ht_mcs[0] << 12);
1025 	} else {
1026 		common->fixedrate_mask[band] =
1027 			mask->control[band].legacy;
1028 	}
1029 	mutex_unlock(&common->mutex);
1030 
1031 	return 0;
1032 }
1033 
1034 /**
1035  * rsi_perform_cqm() - This function performs cqm.
1036  * @common: Pointer to the driver private structure.
1037  * @bssid: pointer to the bssid.
1038  * @rssi: RSSI value.
1039  */
1040 static void rsi_perform_cqm(struct rsi_common *common,
1041 			    u8 *bssid,
1042 			    s8 rssi)
1043 {
1044 	struct rsi_hw *adapter = common->priv;
1045 	s8 last_event = common->cqm_info.last_cqm_event_rssi;
1046 	int thold = common->cqm_info.rssi_thold;
1047 	u32 hyst = common->cqm_info.rssi_hyst;
1048 	enum nl80211_cqm_rssi_threshold_event event;
1049 
1050 	if (rssi < thold && (last_event == 0 || rssi < (last_event - hyst)))
1051 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
1052 	else if (rssi > thold &&
1053 		 (last_event == 0 || rssi > (last_event + hyst)))
1054 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
1055 	else
1056 		return;
1057 
1058 	common->cqm_info.last_cqm_event_rssi = rssi;
1059 	rsi_dbg(INFO_ZONE, "CQM: Notifying event: %d\n", event);
1060 	ieee80211_cqm_rssi_notify(adapter->vifs[0], event, rssi, GFP_KERNEL);
1061 
1062 	return;
1063 }
1064 
1065 /**
1066  * rsi_fill_rx_status() - This function fills rx status in
1067  *			  ieee80211_rx_status structure.
1068  * @hw: Pointer to the ieee80211_hw structure.
1069  * @skb: Pointer to the socket buffer structure.
1070  * @common: Pointer to the driver private structure.
1071  * @rxs: Pointer to the ieee80211_rx_status structure.
1072  *
1073  * Return: None.
1074  */
1075 static void rsi_fill_rx_status(struct ieee80211_hw *hw,
1076 			       struct sk_buff *skb,
1077 			       struct rsi_common *common,
1078 			       struct ieee80211_rx_status *rxs)
1079 {
1080 	struct ieee80211_bss_conf *bss = &common->priv->vifs[0]->bss_conf;
1081 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1082 	struct skb_info *rx_params = (struct skb_info *)info->driver_data;
1083 	struct ieee80211_hdr *hdr;
1084 	char rssi = rx_params->rssi;
1085 	u8 hdrlen = 0;
1086 	u8 channel = rx_params->channel;
1087 	s32 freq;
1088 
1089 	hdr = ((struct ieee80211_hdr *)(skb->data));
1090 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
1091 
1092 	memset(info, 0, sizeof(struct ieee80211_tx_info));
1093 
1094 	rxs->signal = -(rssi);
1095 
1096 	rxs->band = common->band;
1097 
1098 	freq = ieee80211_channel_to_frequency(channel, rxs->band);
1099 
1100 	if (freq)
1101 		rxs->freq = freq;
1102 
1103 	if (ieee80211_has_protected(hdr->frame_control)) {
1104 		if (rsi_is_cipher_wep(common)) {
1105 			memmove(skb->data + 4, skb->data, hdrlen);
1106 			skb_pull(skb, 4);
1107 		} else {
1108 			memmove(skb->data + 8, skb->data, hdrlen);
1109 			skb_pull(skb, 8);
1110 			rxs->flag |= RX_FLAG_MMIC_STRIPPED;
1111 		}
1112 		rxs->flag |= RX_FLAG_DECRYPTED;
1113 		rxs->flag |= RX_FLAG_IV_STRIPPED;
1114 	}
1115 
1116 	/* CQM only for connected AP beacons, the RSSI is a weighted avg */
1117 	if (bss->assoc && !(memcmp(bss->bssid, hdr->addr2, ETH_ALEN))) {
1118 		if (ieee80211_is_beacon(hdr->frame_control))
1119 			rsi_perform_cqm(common, hdr->addr2, rxs->signal);
1120 	}
1121 
1122 	return;
1123 }
1124 
1125 /**
1126  * rsi_indicate_pkt_to_os() - This function sends recieved packet to mac80211.
1127  * @common: Pointer to the driver private structure.
1128  * @skb: Pointer to the socket buffer structure.
1129  *
1130  * Return: None.
1131  */
1132 void rsi_indicate_pkt_to_os(struct rsi_common *common,
1133 			    struct sk_buff *skb)
1134 {
1135 	struct rsi_hw *adapter = common->priv;
1136 	struct ieee80211_hw *hw = adapter->hw;
1137 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1138 
1139 	if ((common->iface_down) || (!adapter->sc_nvifs)) {
1140 		dev_kfree_skb(skb);
1141 		return;
1142 	}
1143 
1144 	/* filling in the ieee80211_rx_status flags */
1145 	rsi_fill_rx_status(hw, skb, common, rx_status);
1146 
1147 	ieee80211_rx_irqsafe(hw, skb);
1148 }
1149 
1150 static void rsi_set_min_rate(struct ieee80211_hw *hw,
1151 			     struct ieee80211_sta *sta,
1152 			     struct rsi_common *common)
1153 {
1154 	u8 band = hw->conf.chandef.chan->band;
1155 	u8 ii;
1156 	u32 rate_bitmap;
1157 	bool matched = false;
1158 
1159 	common->bitrate_mask[band] = sta->supp_rates[band];
1160 
1161 	rate_bitmap = (common->fixedrate_mask[band] & sta->supp_rates[band]);
1162 
1163 	if (rate_bitmap & 0xfff) {
1164 		/* Find out the min rate */
1165 		for (ii = 0; ii < ARRAY_SIZE(rsi_rates); ii++) {
1166 			if (rate_bitmap & BIT(ii)) {
1167 				common->min_rate = rsi_rates[ii].hw_value;
1168 				matched = true;
1169 				break;
1170 			}
1171 		}
1172 	}
1173 
1174 	common->vif_info[0].is_ht = sta->ht_cap.ht_supported;
1175 
1176 	if ((common->vif_info[0].is_ht) && (rate_bitmap >> 12)) {
1177 		for (ii = 0; ii < ARRAY_SIZE(rsi_mcsrates); ii++) {
1178 			if ((rate_bitmap >> 12) & BIT(ii)) {
1179 				common->min_rate = rsi_mcsrates[ii];
1180 				matched = true;
1181 				break;
1182 			}
1183 		}
1184 	}
1185 
1186 	if (!matched)
1187 		common->min_rate = 0xffff;
1188 }
1189 
1190 /**
1191  * rsi_mac80211_sta_add() - This function notifies driver about a peer getting
1192  *			    connected.
1193  * @hw: pointer to the ieee80211_hw structure.
1194  * @vif: Pointer to the ieee80211_vif structure.
1195  * @sta: Pointer to the ieee80211_sta structure.
1196  *
1197  * Return: 0 on success, negative error codes on failure.
1198  */
1199 static int rsi_mac80211_sta_add(struct ieee80211_hw *hw,
1200 				struct ieee80211_vif *vif,
1201 				struct ieee80211_sta *sta)
1202 {
1203 	struct rsi_hw *adapter = hw->priv;
1204 	struct rsi_common *common = adapter->priv;
1205 	bool sta_exist = false;
1206 	struct rsi_sta *rsta;
1207 	int status = 0;
1208 
1209 	rsi_dbg(INFO_ZONE, "Station Add: %pM\n", sta->addr);
1210 
1211 	mutex_lock(&common->mutex);
1212 
1213 	if (vif->type == NL80211_IFTYPE_AP) {
1214 		u8 cnt;
1215 		int sta_idx = -1;
1216 		int free_index = -1;
1217 
1218 		/* Check if max stations reached */
1219 		if (common->num_stations >= common->max_stations) {
1220 			rsi_dbg(ERR_ZONE, "Reject: Max Stations exists\n");
1221 			status = -EOPNOTSUPP;
1222 			goto unlock;
1223 		}
1224 		for (cnt = 0; cnt < common->max_stations; cnt++) {
1225 			rsta = &common->stations[cnt];
1226 
1227 			if (!rsta->sta) {
1228 				if (free_index < 0)
1229 					free_index = cnt;
1230 				continue;
1231 			}
1232 			if (!memcmp(rsta->sta->addr, sta->addr, ETH_ALEN)) {
1233 				rsi_dbg(INFO_ZONE, "Station exists\n");
1234 				sta_idx = cnt;
1235 				sta_exist = true;
1236 				break;
1237 			}
1238 		}
1239 		if (!sta_exist) {
1240 			if (free_index >= 0)
1241 				sta_idx = free_index;
1242 		}
1243 		if (sta_idx < 0) {
1244 			rsi_dbg(ERR_ZONE,
1245 				"%s: Some problem reaching here...\n",
1246 				__func__);
1247 			status = -EINVAL;
1248 			goto unlock;
1249 		}
1250 		rsta = &common->stations[sta_idx];
1251 		rsta->sta = sta;
1252 		rsta->sta_id = sta_idx;
1253 		for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1254 			rsta->start_tx_aggr[cnt] = false;
1255 		for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1256 			rsta->seq_start[cnt] = 0;
1257 		if (!sta_exist) {
1258 			rsi_dbg(INFO_ZONE, "New Station\n");
1259 
1260 			/* Send peer notify to device */
1261 			rsi_dbg(INFO_ZONE, "Indicate bss status to device\n");
1262 			rsi_inform_bss_status(common, AP_OPMODE, 1, sta->addr,
1263 					      sta->wme, sta->aid, sta, sta_idx);
1264 
1265 			if (common->key) {
1266 				struct ieee80211_key_conf *key = common->key;
1267 
1268 				if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
1269 				    (key->cipher == WLAN_CIPHER_SUITE_WEP40))
1270 					rsi_hal_load_key(adapter->priv,
1271 							 key->key,
1272 							 key->keylen,
1273 							 RSI_PAIRWISE_KEY,
1274 							 key->keyidx,
1275 							 key->cipher,
1276 							 sta_idx);
1277 			}
1278 
1279 			common->num_stations++;
1280 		}
1281 	}
1282 
1283 	if (vif->type == NL80211_IFTYPE_STATION) {
1284 		rsi_set_min_rate(hw, sta, common);
1285 		if (sta->ht_cap.ht_supported) {
1286 			common->vif_info[0].is_ht = true;
1287 			common->bitrate_mask[NL80211_BAND_2GHZ] =
1288 					sta->supp_rates[NL80211_BAND_2GHZ];
1289 			if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ||
1290 			    (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
1291 				common->vif_info[0].sgi = true;
1292 			ieee80211_start_tx_ba_session(sta, 0, 0);
1293 		}
1294 	}
1295 
1296 unlock:
1297 	mutex_unlock(&common->mutex);
1298 
1299 	return status;
1300 }
1301 
1302 /**
1303  * rsi_mac80211_sta_remove() - This function notifies driver about a peer
1304  *			       getting disconnected.
1305  * @hw: Pointer to the ieee80211_hw structure.
1306  * @vif: Pointer to the ieee80211_vif structure.
1307  * @sta: Pointer to the ieee80211_sta structure.
1308  *
1309  * Return: 0 on success, negative error codes on failure.
1310  */
1311 static int rsi_mac80211_sta_remove(struct ieee80211_hw *hw,
1312 				   struct ieee80211_vif *vif,
1313 				   struct ieee80211_sta *sta)
1314 {
1315 	struct rsi_hw *adapter = hw->priv;
1316 	struct rsi_common *common = adapter->priv;
1317 	struct ieee80211_bss_conf *bss = &vif->bss_conf;
1318 	struct rsi_sta *rsta;
1319 
1320 	rsi_dbg(INFO_ZONE, "Station Remove: %pM\n", sta->addr);
1321 
1322 	mutex_lock(&common->mutex);
1323 
1324 	if (vif->type == NL80211_IFTYPE_AP) {
1325 		u8 sta_idx, cnt;
1326 
1327 		/* Send peer notify to device */
1328 		rsi_dbg(INFO_ZONE, "Indicate bss status to device\n");
1329 		for (sta_idx = 0; sta_idx < common->max_stations; sta_idx++) {
1330 			rsta = &common->stations[sta_idx];
1331 
1332 			if (!rsta->sta)
1333 				continue;
1334 			if (!memcmp(rsta->sta->addr, sta->addr, ETH_ALEN)) {
1335 				rsi_inform_bss_status(common, AP_OPMODE, 0,
1336 						      sta->addr, sta->wme,
1337 						      sta->aid, sta, sta_idx);
1338 				rsta->sta = NULL;
1339 				rsta->sta_id = -1;
1340 				for (cnt = 0; cnt < IEEE80211_NUM_TIDS; cnt++)
1341 					rsta->start_tx_aggr[cnt] = false;
1342 				if (common->num_stations > 0)
1343 					common->num_stations--;
1344 				break;
1345 			}
1346 		}
1347 		if (sta_idx >= common->max_stations)
1348 			rsi_dbg(ERR_ZONE, "%s: No station found\n", __func__);
1349 	}
1350 
1351 	if (vif->type == NL80211_IFTYPE_STATION) {
1352 		/* Resetting all the fields to default values */
1353 		memcpy((u8 *)bss->bssid, (u8 *)sta->addr, ETH_ALEN);
1354 		bss->qos = sta->wme;
1355 		common->bitrate_mask[NL80211_BAND_2GHZ] = 0;
1356 		common->bitrate_mask[NL80211_BAND_5GHZ] = 0;
1357 		common->min_rate = 0xffff;
1358 		common->vif_info[0].is_ht = false;
1359 		common->vif_info[0].sgi = false;
1360 		common->vif_info[0].seq_start = 0;
1361 		common->secinfo.ptk_cipher = 0;
1362 		common->secinfo.gtk_cipher = 0;
1363 		if (!common->iface_down)
1364 			rsi_send_rx_filter_frame(common, 0);
1365 	}
1366 	mutex_unlock(&common->mutex);
1367 
1368 	return 0;
1369 }
1370 
1371 /**
1372  * rsi_mac80211_set_antenna() - This function is used to configure
1373  *				tx and rx antennas.
1374  * @hw: Pointer to the ieee80211_hw structure.
1375  * @tx_ant: Bitmap for tx antenna
1376  * @rx_ant: Bitmap for rx antenna
1377  *
1378  * Return: 0 on success, Negative error code on failure.
1379  */
1380 static int rsi_mac80211_set_antenna(struct ieee80211_hw *hw,
1381 				    u32 tx_ant, u32 rx_ant)
1382 {
1383 	struct rsi_hw *adapter = hw->priv;
1384 	struct rsi_common *common = adapter->priv;
1385 	u8 antenna = 0;
1386 
1387 	if (tx_ant > 1 || rx_ant > 1) {
1388 		rsi_dbg(ERR_ZONE,
1389 			"Invalid antenna selection (tx: %d, rx:%d)\n",
1390 			tx_ant, rx_ant);
1391 		rsi_dbg(ERR_ZONE,
1392 			"Use 0 for int_ant, 1 for ext_ant\n");
1393 		return -EINVAL;
1394 	}
1395 
1396 	rsi_dbg(INFO_ZONE, "%s: Antenna map Tx %x Rx %d\n",
1397 			__func__, tx_ant, rx_ant);
1398 
1399 	mutex_lock(&common->mutex);
1400 
1401 	antenna = tx_ant ? ANTENNA_SEL_UFL : ANTENNA_SEL_INT;
1402 	if (common->ant_in_use != antenna)
1403 		if (rsi_set_antenna(common, antenna))
1404 			goto fail_set_antenna;
1405 
1406 	rsi_dbg(INFO_ZONE, "(%s) Antenna path configured successfully\n",
1407 		tx_ant ? "UFL" : "INT");
1408 
1409 	common->ant_in_use = antenna;
1410 
1411 	mutex_unlock(&common->mutex);
1412 
1413 	return 0;
1414 
1415 fail_set_antenna:
1416 	rsi_dbg(ERR_ZONE, "%s: Failed.\n", __func__);
1417 	mutex_unlock(&common->mutex);
1418 	return -EINVAL;
1419 }
1420 
1421 /**
1422  * rsi_mac80211_get_antenna() - This function is used to configure
1423  * 				tx and rx antennas.
1424  *
1425  * @hw: Pointer to the ieee80211_hw structure.
1426  * @tx_ant: Bitmap for tx antenna
1427  * @rx_ant: Bitmap for rx antenna
1428  *
1429  * Return: 0 on success, negative error codes on failure.
1430  */
1431 static int rsi_mac80211_get_antenna(struct ieee80211_hw *hw,
1432 				    u32 *tx_ant, u32 *rx_ant)
1433 {
1434 	struct rsi_hw *adapter = hw->priv;
1435 	struct rsi_common *common = adapter->priv;
1436 
1437 	mutex_lock(&common->mutex);
1438 
1439 	*tx_ant = (common->ant_in_use == ANTENNA_SEL_UFL) ? 1 : 0;
1440 	*rx_ant = 0;
1441 
1442 	mutex_unlock(&common->mutex);
1443 
1444 	return 0;
1445 }
1446 
1447 static int rsi_map_region_code(enum nl80211_dfs_regions region_code)
1448 {
1449 	switch (region_code) {
1450 	case NL80211_DFS_FCC:
1451 		return RSI_REGION_FCC;
1452 	case NL80211_DFS_ETSI:
1453 		return RSI_REGION_ETSI;
1454 	case NL80211_DFS_JP:
1455 		return RSI_REGION_TELEC;
1456 	case NL80211_DFS_UNSET:
1457 		return RSI_REGION_WORLD;
1458 	}
1459 	return RSI_REGION_WORLD;
1460 }
1461 
1462 static void rsi_reg_notify(struct wiphy *wiphy,
1463 			   struct regulatory_request *request)
1464 {
1465 	struct ieee80211_supported_band *sband;
1466 	struct ieee80211_channel *ch;
1467 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1468 	struct rsi_hw * adapter = hw->priv;
1469 	struct rsi_common *common = adapter->priv;
1470 	int i;
1471 
1472 	mutex_lock(&common->mutex);
1473 
1474 	rsi_dbg(INFO_ZONE, "country = %s dfs_region = %d\n",
1475 		request->alpha2, request->dfs_region);
1476 
1477 	if (common->num_supp_bands > 1) {
1478 		sband = wiphy->bands[NL80211_BAND_5GHZ];
1479 
1480 		for (i = 0; i < sband->n_channels; i++) {
1481 			ch = &sband->channels[i];
1482 			if (ch->flags & IEEE80211_CHAN_DISABLED)
1483 				continue;
1484 
1485 			if (ch->flags & IEEE80211_CHAN_RADAR)
1486 				ch->flags |= IEEE80211_CHAN_NO_IR;
1487 		}
1488 	}
1489 	adapter->dfs_region = rsi_map_region_code(request->dfs_region);
1490 	rsi_dbg(INFO_ZONE, "RSI region code = %d\n", adapter->dfs_region);
1491 
1492 	adapter->country[0] = request->alpha2[0];
1493 	adapter->country[1] = request->alpha2[1];
1494 
1495 	mutex_unlock(&common->mutex);
1496 }
1497 
1498 static void rsi_mac80211_rfkill_poll(struct ieee80211_hw *hw)
1499 {
1500 	struct rsi_hw *adapter = hw->priv;
1501 	struct rsi_common *common = adapter->priv;
1502 
1503 	mutex_lock(&common->mutex);
1504 	if (common->fsm_state != FSM_MAC_INIT_DONE)
1505 		wiphy_rfkill_set_hw_state(hw->wiphy, true);
1506 	else
1507 		wiphy_rfkill_set_hw_state(hw->wiphy, false);
1508 	mutex_unlock(&common->mutex);
1509 }
1510 
1511 static const struct ieee80211_ops mac80211_ops = {
1512 	.tx = rsi_mac80211_tx,
1513 	.start = rsi_mac80211_start,
1514 	.stop = rsi_mac80211_stop,
1515 	.add_interface = rsi_mac80211_add_interface,
1516 	.remove_interface = rsi_mac80211_remove_interface,
1517 	.config = rsi_mac80211_config,
1518 	.bss_info_changed = rsi_mac80211_bss_info_changed,
1519 	.conf_tx = rsi_mac80211_conf_tx,
1520 	.configure_filter = rsi_mac80211_conf_filter,
1521 	.set_key = rsi_mac80211_set_key,
1522 	.set_rts_threshold = rsi_mac80211_set_rts_threshold,
1523 	.set_bitrate_mask = rsi_mac80211_set_rate_mask,
1524 	.ampdu_action = rsi_mac80211_ampdu_action,
1525 	.sta_add = rsi_mac80211_sta_add,
1526 	.sta_remove = rsi_mac80211_sta_remove,
1527 	.set_antenna = rsi_mac80211_set_antenna,
1528 	.get_antenna = rsi_mac80211_get_antenna,
1529 	.rfkill_poll = rsi_mac80211_rfkill_poll,
1530 };
1531 
1532 /**
1533  * rsi_mac80211_attach() - This function is used to initialize Mac80211 stack.
1534  * @common: Pointer to the driver private structure.
1535  *
1536  * Return: 0 on success, negative error codes on failure.
1537  */
1538 int rsi_mac80211_attach(struct rsi_common *common)
1539 {
1540 	int status = 0;
1541 	struct ieee80211_hw *hw = NULL;
1542 	struct wiphy *wiphy = NULL;
1543 	struct rsi_hw *adapter = common->priv;
1544 	u8 addr_mask[ETH_ALEN] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x3};
1545 
1546 	rsi_dbg(INIT_ZONE, "%s: Performing mac80211 attach\n", __func__);
1547 
1548 	hw = ieee80211_alloc_hw(sizeof(struct rsi_hw), &mac80211_ops);
1549 	if (!hw) {
1550 		rsi_dbg(ERR_ZONE, "%s: ieee80211 hw alloc failed\n", __func__);
1551 		return -ENOMEM;
1552 	}
1553 
1554 	wiphy = hw->wiphy;
1555 
1556 	SET_IEEE80211_DEV(hw, adapter->device);
1557 
1558 	hw->priv = adapter;
1559 	adapter->hw = hw;
1560 
1561 	ieee80211_hw_set(hw, SIGNAL_DBM);
1562 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
1563 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1564 	ieee80211_hw_set(hw, SUPPORTS_PS);
1565 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
1566 
1567 	hw->queues = MAX_HW_QUEUES;
1568 	hw->extra_tx_headroom = RSI_NEEDED_HEADROOM;
1569 
1570 	hw->max_rates = 1;
1571 	hw->max_rate_tries = MAX_RETRIES;
1572 	hw->uapsd_queues = RSI_IEEE80211_UAPSD_QUEUES;
1573 	hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
1574 
1575 	hw->max_tx_aggregation_subframes = 6;
1576 	rsi_register_rates_channels(adapter, NL80211_BAND_2GHZ);
1577 	rsi_register_rates_channels(adapter, NL80211_BAND_5GHZ);
1578 	hw->rate_control_algorithm = "AARF";
1579 
1580 	SET_IEEE80211_PERM_ADDR(hw, common->mac_addr);
1581 	ether_addr_copy(hw->wiphy->addr_mask, addr_mask);
1582 
1583 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1584 				 BIT(NL80211_IFTYPE_AP);
1585 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1586 	wiphy->retry_short = RETRY_SHORT;
1587 	wiphy->retry_long  = RETRY_LONG;
1588 	wiphy->frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1589 	wiphy->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1590 	wiphy->flags = 0;
1591 
1592 	wiphy->available_antennas_rx = 1;
1593 	wiphy->available_antennas_tx = 1;
1594 	wiphy->bands[NL80211_BAND_2GHZ] =
1595 		&adapter->sbands[NL80211_BAND_2GHZ];
1596 	wiphy->bands[NL80211_BAND_5GHZ] =
1597 		&adapter->sbands[NL80211_BAND_5GHZ];
1598 
1599 	/* AP Parameters */
1600 	wiphy->max_ap_assoc_sta = rsi_max_ap_stas[common->oper_mode - 1];
1601 	common->max_stations = wiphy->max_ap_assoc_sta;
1602 	rsi_dbg(ERR_ZONE, "Max Stations Allowed = %d\n", common->max_stations);
1603 	hw->sta_data_size = sizeof(struct rsi_sta);
1604 	wiphy->flags = WIPHY_FLAG_REPORTS_OBSS;
1605 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
1606 	wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER;
1607 	wiphy->reg_notifier = rsi_reg_notify;
1608 
1609 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1610 
1611 	status = ieee80211_register_hw(hw);
1612 	if (status)
1613 		return status;
1614 
1615 	return rsi_init_dbgfs(adapter);
1616 }
1617