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