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