1 /**
2  * Copyright (c) 2014 Redpine Signals Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/etherdevice.h>
18 #include "rsi_debugfs.h"
19 #include "rsi_mgmt.h"
20 #include "rsi_common.h"
21 
22 static const struct ieee80211_channel rsi_2ghz_channels[] = {
23 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2412,
24 	  .hw_value = 1 }, /* Channel 1 */
25 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2417,
26 	  .hw_value = 2 }, /* Channel 2 */
27 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2422,
28 	  .hw_value = 3 }, /* Channel 3 */
29 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2427,
30 	  .hw_value = 4 }, /* Channel 4 */
31 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2432,
32 	  .hw_value = 5 }, /* Channel 5 */
33 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2437,
34 	  .hw_value = 6 }, /* Channel 6 */
35 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2442,
36 	  .hw_value = 7 }, /* Channel 7 */
37 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2447,
38 	  .hw_value = 8 }, /* Channel 8 */
39 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2452,
40 	  .hw_value = 9 }, /* Channel 9 */
41 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2457,
42 	  .hw_value = 10 }, /* Channel 10 */
43 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2462,
44 	  .hw_value = 11 }, /* Channel 11 */
45 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2467,
46 	  .hw_value = 12 }, /* Channel 12 */
47 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2472,
48 	  .hw_value = 13 }, /* Channel 13 */
49 	{ .band = IEEE80211_BAND_2GHZ, .center_freq = 2484,
50 	  .hw_value = 14 }, /* Channel 14 */
51 };
52 
53 static const struct ieee80211_channel rsi_5ghz_channels[] = {
54 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5180,
55 	  .hw_value = 36,  }, /* Channel 36 */
56 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5200,
57 	  .hw_value = 40, }, /* Channel 40 */
58 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5220,
59 	  .hw_value = 44, }, /* Channel 44 */
60 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5240,
61 	  .hw_value = 48, }, /* Channel 48 */
62 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5260,
63 	  .hw_value = 52, }, /* Channel 52 */
64 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5280,
65 	  .hw_value = 56, }, /* Channel 56 */
66 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5300,
67 	  .hw_value = 60, }, /* Channel 60 */
68 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5320,
69 	  .hw_value = 64, }, /* Channel 64 */
70 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5500,
71 	  .hw_value = 100, }, /* Channel 100 */
72 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5520,
73 	  .hw_value = 104, }, /* Channel 104 */
74 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5540,
75 	  .hw_value = 108, }, /* Channel 108 */
76 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5560,
77 	  .hw_value = 112, }, /* Channel 112 */
78 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5580,
79 	  .hw_value = 116, }, /* Channel 116 */
80 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5600,
81 	  .hw_value = 120, }, /* Channel 120 */
82 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5620,
83 	  .hw_value = 124, }, /* Channel 124 */
84 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5640,
85 	  .hw_value = 128, }, /* Channel 128 */
86 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5660,
87 	  .hw_value = 132, }, /* Channel 132 */
88 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5680,
89 	  .hw_value = 136, }, /* Channel 136 */
90 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5700,
91 	  .hw_value = 140, }, /* Channel 140 */
92 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5745,
93 	  .hw_value = 149, }, /* Channel 149 */
94 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5765,
95 	  .hw_value = 153, }, /* Channel 153 */
96 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5785,
97 	  .hw_value = 157, }, /* Channel 157 */
98 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5805,
99 	  .hw_value = 161, }, /* Channel 161 */
100 	{ .band = IEEE80211_BAND_5GHZ, .center_freq = 5825,
101 	  .hw_value = 165, }, /* Channel 165 */
102 };
103 
104 struct ieee80211_rate rsi_rates[12] = {
105 	{ .bitrate = STD_RATE_01  * 5, .hw_value = RSI_RATE_1 },
106 	{ .bitrate = STD_RATE_02  * 5, .hw_value = RSI_RATE_2 },
107 	{ .bitrate = STD_RATE_5_5 * 5, .hw_value = RSI_RATE_5_5 },
108 	{ .bitrate = STD_RATE_11  * 5, .hw_value = RSI_RATE_11 },
109 	{ .bitrate = STD_RATE_06  * 5, .hw_value = RSI_RATE_6 },
110 	{ .bitrate = STD_RATE_09  * 5, .hw_value = RSI_RATE_9 },
111 	{ .bitrate = STD_RATE_12  * 5, .hw_value = RSI_RATE_12 },
112 	{ .bitrate = STD_RATE_18  * 5, .hw_value = RSI_RATE_18 },
113 	{ .bitrate = STD_RATE_24  * 5, .hw_value = RSI_RATE_24 },
114 	{ .bitrate = STD_RATE_36  * 5, .hw_value = RSI_RATE_36 },
115 	{ .bitrate = STD_RATE_48  * 5, .hw_value = RSI_RATE_48 },
116 	{ .bitrate = STD_RATE_54  * 5, .hw_value = RSI_RATE_54 },
117 };
118 
119 const u16 rsi_mcsrates[8] = {
120 	RSI_RATE_MCS0, RSI_RATE_MCS1, RSI_RATE_MCS2, RSI_RATE_MCS3,
121 	RSI_RATE_MCS4, RSI_RATE_MCS5, RSI_RATE_MCS6, RSI_RATE_MCS7
122 };
123 
124 /**
125  * rsi_is_cipher_wep() -  This function determines if the cipher is WEP or not.
126  * @common: Pointer to the driver private structure.
127  *
128  * Return: If cipher type is WEP, a value of 1 is returned, else 0.
129  */
130 
131 bool rsi_is_cipher_wep(struct rsi_common *common)
132 {
133 	if (((common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP104) ||
134 	     (common->secinfo.gtk_cipher == WLAN_CIPHER_SUITE_WEP40)) &&
135 	    (!common->secinfo.ptk_cipher))
136 		return true;
137 	else
138 		return false;
139 }
140 
141 /**
142  * rsi_register_rates_channels() - This function registers channels and rates.
143  * @adapter: Pointer to the adapter structure.
144  * @band: Operating band to be set.
145  *
146  * Return: None.
147  */
148 static void rsi_register_rates_channels(struct rsi_hw *adapter, int band)
149 {
150 	struct ieee80211_supported_band *sbands = &adapter->sbands[band];
151 	void *channels = NULL;
152 
153 	if (band == IEEE80211_BAND_2GHZ) {
154 		channels = kmalloc(sizeof(rsi_2ghz_channels), GFP_KERNEL);
155 		memcpy(channels,
156 		       rsi_2ghz_channels,
157 		       sizeof(rsi_2ghz_channels));
158 		sbands->band = IEEE80211_BAND_2GHZ;
159 		sbands->n_channels = ARRAY_SIZE(rsi_2ghz_channels);
160 		sbands->bitrates = rsi_rates;
161 		sbands->n_bitrates = ARRAY_SIZE(rsi_rates);
162 	} else {
163 		channels = kmalloc(sizeof(rsi_5ghz_channels), GFP_KERNEL);
164 		memcpy(channels,
165 		       rsi_5ghz_channels,
166 		       sizeof(rsi_5ghz_channels));
167 		sbands->band = IEEE80211_BAND_5GHZ;
168 		sbands->n_channels = ARRAY_SIZE(rsi_5ghz_channels);
169 		sbands->bitrates = &rsi_rates[4];
170 		sbands->n_bitrates = ARRAY_SIZE(rsi_rates) - 4;
171 	}
172 
173 	sbands->channels = channels;
174 
175 	memset(&sbands->ht_cap, 0, sizeof(struct ieee80211_sta_ht_cap));
176 	sbands->ht_cap.ht_supported = true;
177 	sbands->ht_cap.cap = (IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
178 			      IEEE80211_HT_CAP_SGI_20 |
179 			      IEEE80211_HT_CAP_SGI_40);
180 	sbands->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K;
181 	sbands->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
182 	sbands->ht_cap.mcs.rx_mask[0] = 0xff;
183 	sbands->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
184 	/* sbands->ht_cap.mcs.rx_highest = 0x82; */
185 }
186 
187 /**
188  * rsi_mac80211_detach() - This function is used to de-initialize the
189  *			   Mac80211 stack.
190  * @adapter: Pointer to the adapter structure.
191  *
192  * Return: None.
193  */
194 void rsi_mac80211_detach(struct rsi_hw *adapter)
195 {
196 	struct ieee80211_hw *hw = adapter->hw;
197 
198 	if (hw) {
199 		ieee80211_stop_queues(hw);
200 		ieee80211_unregister_hw(hw);
201 		ieee80211_free_hw(hw);
202 	}
203 
204 	rsi_remove_dbgfs(adapter);
205 }
206 EXPORT_SYMBOL_GPL(rsi_mac80211_detach);
207 
208 /**
209  * rsi_indicate_tx_status() - This function indicates the transmit status.
210  * @adapter: Pointer to the adapter structure.
211  * @skb: Pointer to the socket buffer structure.
212  * @status: Status
213  *
214  * Return: None.
215  */
216 void rsi_indicate_tx_status(struct rsi_hw *adapter,
217 			    struct sk_buff *skb,
218 			    int status)
219 {
220 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
221 
222 	memset(info->driver_data, 0, IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
223 
224 	if (!status)
225 		info->flags |= IEEE80211_TX_STAT_ACK;
226 
227 	ieee80211_tx_status_irqsafe(adapter->hw, skb);
228 }
229 
230 /**
231  * rsi_mac80211_tx() - This is the handler that 802.11 module calls for each
232  *		       transmitted frame.SKB contains the buffer starting
233  *		       from the IEEE 802.11 header.
234  * @hw: Pointer to the ieee80211_hw structure.
235  * @control: Pointer to the ieee80211_tx_control structure
236  * @skb: Pointer to the socket buffer structure.
237  *
238  * Return: None
239  */
240 static void rsi_mac80211_tx(struct ieee80211_hw *hw,
241 			    struct ieee80211_tx_control *control,
242 			    struct sk_buff *skb)
243 {
244 	struct rsi_hw *adapter = hw->priv;
245 	struct rsi_common *common = adapter->priv;
246 
247 	rsi_core_xmit(common, skb);
248 }
249 
250 /**
251  * rsi_mac80211_start() - This is first handler that 802.11 module calls, since
252  *			  the driver init is complete by then, just
253  *			  returns success.
254  * @hw: Pointer to the ieee80211_hw structure.
255  *
256  * Return: 0 as success.
257  */
258 static int rsi_mac80211_start(struct ieee80211_hw *hw)
259 {
260 	struct rsi_hw *adapter = hw->priv;
261 	struct rsi_common *common = adapter->priv;
262 
263 	mutex_lock(&common->mutex);
264 	common->iface_down = false;
265 	mutex_unlock(&common->mutex);
266 
267 	return 0;
268 }
269 
270 /**
271  * rsi_mac80211_stop() - This is the last handler that 802.11 module calls.
272  * @hw: Pointer to the ieee80211_hw structure.
273  *
274  * Return: None.
275  */
276 static void rsi_mac80211_stop(struct ieee80211_hw *hw)
277 {
278 	struct rsi_hw *adapter = hw->priv;
279 	struct rsi_common *common = adapter->priv;
280 
281 	mutex_lock(&common->mutex);
282 	common->iface_down = true;
283 	mutex_unlock(&common->mutex);
284 }
285 
286 /**
287  * rsi_mac80211_add_interface() - This function is called when a netdevice
288  *				  attached to the hardware is enabled.
289  * @hw: Pointer to the ieee80211_hw structure.
290  * @vif: Pointer to the ieee80211_vif structure.
291  *
292  * Return: ret: 0 on success, negative error code on failure.
293  */
294 static int rsi_mac80211_add_interface(struct ieee80211_hw *hw,
295 				      struct ieee80211_vif *vif)
296 {
297 	struct rsi_hw *adapter = hw->priv;
298 	struct rsi_common *common = adapter->priv;
299 	int ret = -EOPNOTSUPP;
300 
301 	mutex_lock(&common->mutex);
302 	switch (vif->type) {
303 	case NL80211_IFTYPE_STATION:
304 		if (!adapter->sc_nvifs) {
305 			++adapter->sc_nvifs;
306 			adapter->vifs[0] = vif;
307 			ret = rsi_set_vap_capabilities(common, STA_OPMODE);
308 		}
309 		break;
310 	default:
311 		rsi_dbg(ERR_ZONE,
312 			"%s: Interface type %d not supported\n", __func__,
313 			vif->type);
314 	}
315 	mutex_unlock(&common->mutex);
316 
317 	return ret;
318 }
319 
320 /**
321  * rsi_mac80211_remove_interface() - This function notifies driver that an
322  *				     interface is going down.
323  * @hw: Pointer to the ieee80211_hw structure.
324  * @vif: Pointer to the ieee80211_vif structure.
325  *
326  * Return: None.
327  */
328 static void rsi_mac80211_remove_interface(struct ieee80211_hw *hw,
329 					  struct ieee80211_vif *vif)
330 {
331 	struct rsi_hw *adapter = hw->priv;
332 	struct rsi_common *common = adapter->priv;
333 
334 	mutex_lock(&common->mutex);
335 	if (vif->type == NL80211_IFTYPE_STATION)
336 		adapter->sc_nvifs--;
337 
338 	if (!memcmp(adapter->vifs[0], vif, sizeof(struct ieee80211_vif)))
339 		adapter->vifs[0] = NULL;
340 	mutex_unlock(&common->mutex);
341 }
342 
343 /**
344  * rsi_channel_change() - This function is a performs the checks
345  *			  required for changing a channel and sets
346  *			  the channel accordingly.
347  * @hw: Pointer to the ieee80211_hw structure.
348  *
349  * Return: 0 on success, negative error code on failure.
350  */
351 static int rsi_channel_change(struct ieee80211_hw *hw)
352 {
353 	struct rsi_hw *adapter = hw->priv;
354 	struct rsi_common *common = adapter->priv;
355 	int status = -EOPNOTSUPP;
356 	struct ieee80211_channel *curchan = hw->conf.chandef.chan;
357 	u16 channel = curchan->hw_value;
358 	struct ieee80211_bss_conf *bss = &adapter->vifs[0]->bss_conf;
359 
360 	rsi_dbg(INFO_ZONE,
361 		"%s: Set channel: %d MHz type: %d channel_no %d\n",
362 		__func__, curchan->center_freq,
363 		curchan->flags, channel);
364 
365 	if (bss->assoc) {
366 		if (!common->hw_data_qs_blocked &&
367 		    (rsi_get_connected_channel(adapter) != channel)) {
368 			rsi_dbg(INFO_ZONE, "blk data q %d\n", channel);
369 			if (!rsi_send_block_unblock_frame(common, true))
370 				common->hw_data_qs_blocked = true;
371 		}
372 	}
373 
374 	status = rsi_band_check(common);
375 	if (!status)
376 		status = rsi_set_channel(adapter->priv, channel);
377 
378 	if (bss->assoc) {
379 		if (common->hw_data_qs_blocked &&
380 		    (rsi_get_connected_channel(adapter) == channel)) {
381 			rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
382 			if (!rsi_send_block_unblock_frame(common, false))
383 				common->hw_data_qs_blocked = false;
384 		}
385 	} else {
386 		if (common->hw_data_qs_blocked) {
387 			rsi_dbg(INFO_ZONE, "unblk data q %d\n", channel);
388 			if (!rsi_send_block_unblock_frame(common, false))
389 				common->hw_data_qs_blocked = false;
390 		}
391 	}
392 
393 	return status;
394 }
395 
396 /**
397  * rsi_mac80211_config() - This function is a handler for configuration
398  *			   requests. The stack calls this function to
399  *			   change hardware configuration, e.g., channel.
400  * @hw: Pointer to the ieee80211_hw structure.
401  * @changed: Changed flags set.
402  *
403  * Return: 0 on success, negative error code on failure.
404  */
405 static int rsi_mac80211_config(struct ieee80211_hw *hw,
406 			       u32 changed)
407 {
408 	struct rsi_hw *adapter = hw->priv;
409 	struct rsi_common *common = adapter->priv;
410 	int status = -EOPNOTSUPP;
411 
412 	mutex_lock(&common->mutex);
413 
414 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL)
415 		status = rsi_channel_change(hw);
416 
417 	mutex_unlock(&common->mutex);
418 
419 	return status;
420 }
421 
422 /**
423  * rsi_get_connected_channel() - This function is used to get the current
424  *				 connected channel number.
425  * @adapter: Pointer to the adapter structure.
426  *
427  * Return: Current connected AP's channel number is returned.
428  */
429 u16 rsi_get_connected_channel(struct rsi_hw *adapter)
430 {
431 	struct ieee80211_vif *vif = adapter->vifs[0];
432 	if (vif) {
433 		struct ieee80211_bss_conf *bss = &vif->bss_conf;
434 		struct ieee80211_channel *channel = bss->chandef.chan;
435 		return channel->hw_value;
436 	}
437 
438 	return 0;
439 }
440 
441 /**
442  * rsi_mac80211_bss_info_changed() - This function is a handler for config
443  *				     requests related to BSS parameters that
444  *				     may vary during BSS's lifespan.
445  * @hw: Pointer to the ieee80211_hw structure.
446  * @vif: Pointer to the ieee80211_vif structure.
447  * @bss_conf: Pointer to the ieee80211_bss_conf structure.
448  * @changed: Changed flags set.
449  *
450  * Return: None.
451  */
452 static void rsi_mac80211_bss_info_changed(struct ieee80211_hw *hw,
453 					  struct ieee80211_vif *vif,
454 					  struct ieee80211_bss_conf *bss_conf,
455 					  u32 changed)
456 {
457 	struct rsi_hw *adapter = hw->priv;
458 	struct rsi_common *common = adapter->priv;
459 
460 	mutex_lock(&common->mutex);
461 	if (changed & BSS_CHANGED_ASSOC) {
462 		rsi_dbg(INFO_ZONE, "%s: Changed Association status: %d\n",
463 			__func__, bss_conf->assoc);
464 		rsi_inform_bss_status(common,
465 				      bss_conf->assoc,
466 				      bss_conf->bssid,
467 				      bss_conf->qos,
468 				      bss_conf->aid);
469 	}
470 
471 	if (changed & BSS_CHANGED_CQM) {
472 		common->cqm_info.last_cqm_event_rssi = 0;
473 		common->cqm_info.rssi_thold = bss_conf->cqm_rssi_thold;
474 		common->cqm_info.rssi_hyst = bss_conf->cqm_rssi_hyst;
475 		rsi_dbg(INFO_ZONE, "RSSI throld & hysteresis are: %d %d\n",
476 			common->cqm_info.rssi_thold,
477 			common->cqm_info.rssi_hyst);
478 	}
479 	mutex_unlock(&common->mutex);
480 }
481 
482 /**
483  * rsi_mac80211_conf_filter() - This function configure the device's RX filter.
484  * @hw: Pointer to the ieee80211_hw structure.
485  * @changed: Changed flags set.
486  * @total_flags: Total initial flags set.
487  * @multicast: Multicast.
488  *
489  * Return: None.
490  */
491 static void rsi_mac80211_conf_filter(struct ieee80211_hw *hw,
492 				     u32 changed_flags,
493 				     u32 *total_flags,
494 				     u64 multicast)
495 {
496 	/* Not doing much here as of now */
497 	*total_flags &= RSI_SUPP_FILTERS;
498 }
499 
500 /**
501  * rsi_mac80211_conf_tx() - This function configures TX queue parameters
502  *			    (EDCF (aifs, cw_min, cw_max), bursting)
503  *			    for a hardware TX queue.
504  * @hw: Pointer to the ieee80211_hw structure
505  * @vif: Pointer to the ieee80211_vif structure.
506  * @queue: Queue number.
507  * @params: Pointer to ieee80211_tx_queue_params structure.
508  *
509  * Return: 0 on success, negative error code on failure.
510  */
511 static int rsi_mac80211_conf_tx(struct ieee80211_hw *hw,
512 				struct ieee80211_vif *vif, u16 queue,
513 				const struct ieee80211_tx_queue_params *params)
514 {
515 	struct rsi_hw *adapter = hw->priv;
516 	struct rsi_common *common = adapter->priv;
517 	u8 idx = 0;
518 
519 	if (queue >= IEEE80211_NUM_ACS)
520 		return 0;
521 
522 	rsi_dbg(INFO_ZONE,
523 		"%s: Conf queue %d, aifs: %d, cwmin: %d cwmax: %d, txop: %d\n",
524 		__func__, queue, params->aifs,
525 		params->cw_min, params->cw_max, params->txop);
526 
527 	mutex_lock(&common->mutex);
528 	/* Map into the way the f/w expects */
529 	switch (queue) {
530 	case IEEE80211_AC_VO:
531 		idx = VO_Q;
532 		break;
533 	case IEEE80211_AC_VI:
534 		idx = VI_Q;
535 		break;
536 	case IEEE80211_AC_BE:
537 		idx = BE_Q;
538 		break;
539 	case IEEE80211_AC_BK:
540 		idx = BK_Q;
541 		break;
542 	default:
543 		idx = BE_Q;
544 		break;
545 	}
546 
547 	memcpy(&common->edca_params[idx],
548 	       params,
549 	       sizeof(struct ieee80211_tx_queue_params));
550 	mutex_unlock(&common->mutex);
551 
552 	return 0;
553 }
554 
555 /**
556  * rsi_hal_key_config() - This function loads the keys into the firmware.
557  * @hw: Pointer to the ieee80211_hw structure.
558  * @vif: Pointer to the ieee80211_vif structure.
559  * @key: Pointer to the ieee80211_key_conf structure.
560  *
561  * Return: status: 0 on success, -1 on failure.
562  */
563 static int rsi_hal_key_config(struct ieee80211_hw *hw,
564 			      struct ieee80211_vif *vif,
565 			      struct ieee80211_key_conf *key)
566 {
567 	struct rsi_hw *adapter = hw->priv;
568 	int status;
569 	u8 key_type;
570 
571 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
572 		key_type = RSI_PAIRWISE_KEY;
573 	else
574 		key_type = RSI_GROUP_KEY;
575 
576 	rsi_dbg(ERR_ZONE, "%s: Cipher 0x%x key_type: %d key_len: %d\n",
577 		__func__, key->cipher, key_type, key->keylen);
578 
579 	if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
580 	    (key->cipher == WLAN_CIPHER_SUITE_WEP40)) {
581 		status = rsi_hal_load_key(adapter->priv,
582 					  key->key,
583 					  key->keylen,
584 					  RSI_PAIRWISE_KEY,
585 					  key->keyidx,
586 					  key->cipher);
587 		if (status)
588 			return status;
589 	}
590 	return rsi_hal_load_key(adapter->priv,
591 				key->key,
592 				key->keylen,
593 				key_type,
594 				key->keyidx,
595 				key->cipher);
596 }
597 
598 /**
599  * rsi_mac80211_set_key() - This function sets type of key to be loaded.
600  * @hw: Pointer to the ieee80211_hw structure.
601  * @cmd: enum set_key_cmd.
602  * @vif: Pointer to the ieee80211_vif structure.
603  * @sta: Pointer to the ieee80211_sta structure.
604  * @key: Pointer to the ieee80211_key_conf structure.
605  *
606  * Return: status: 0 on success, negative error code on failure.
607  */
608 static int rsi_mac80211_set_key(struct ieee80211_hw *hw,
609 				enum set_key_cmd cmd,
610 				struct ieee80211_vif *vif,
611 				struct ieee80211_sta *sta,
612 				struct ieee80211_key_conf *key)
613 {
614 	struct rsi_hw *adapter = hw->priv;
615 	struct rsi_common *common = adapter->priv;
616 	struct security_info *secinfo = &common->secinfo;
617 	int status;
618 
619 	mutex_lock(&common->mutex);
620 	switch (cmd) {
621 	case SET_KEY:
622 		secinfo->security_enable = true;
623 		status = rsi_hal_key_config(hw, vif, key);
624 		if (status) {
625 			mutex_unlock(&common->mutex);
626 			return status;
627 		}
628 
629 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
630 			secinfo->ptk_cipher = key->cipher;
631 		else
632 			secinfo->gtk_cipher = key->cipher;
633 
634 		key->hw_key_idx = key->keyidx;
635 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
636 
637 		rsi_dbg(ERR_ZONE, "%s: RSI set_key\n", __func__);
638 		break;
639 
640 	case DISABLE_KEY:
641 		secinfo->security_enable = false;
642 		rsi_dbg(ERR_ZONE, "%s: RSI del key\n", __func__);
643 		memset(key, 0, sizeof(struct ieee80211_key_conf));
644 		status = rsi_hal_key_config(hw, vif, key);
645 		break;
646 
647 	default:
648 		status = -EOPNOTSUPP;
649 		break;
650 	}
651 
652 	mutex_unlock(&common->mutex);
653 	return status;
654 }
655 
656 /**
657  * rsi_mac80211_ampdu_action() - This function selects the AMPDU action for
658  *				 the corresponding mlme_action flag and
659  *				 informs the f/w regarding this.
660  * @hw: Pointer to the ieee80211_hw structure.
661  * @vif: Pointer to the ieee80211_vif structure.
662  * @action: ieee80211_ampdu_mlme_action enum.
663  * @sta: Pointer to the ieee80211_sta structure.
664  * @tid: Traffic identifier.
665  * @ssn: Pointer to ssn value.
666  * @buf_size: Buffer size (for kernel version > 2.6.38).
667  * @amsdu: is AMSDU in AMPDU allowed
668  *
669  * Return: status: 0 on success, negative error code on failure.
670  */
671 static int rsi_mac80211_ampdu_action(struct ieee80211_hw *hw,
672 				     struct ieee80211_vif *vif,
673 				     enum ieee80211_ampdu_mlme_action action,
674 				     struct ieee80211_sta *sta,
675 				     unsigned short tid,
676 				     unsigned short *ssn,
677 				     unsigned char buf_size,
678 				     bool amsdu)
679 {
680 	int status = -EOPNOTSUPP;
681 	struct rsi_hw *adapter = hw->priv;
682 	struct rsi_common *common = adapter->priv;
683 	u16 seq_no = 0;
684 	u8 ii = 0;
685 
686 	for (ii = 0; ii < RSI_MAX_VIFS; ii++) {
687 		if (vif == adapter->vifs[ii])
688 			break;
689 	}
690 
691 	mutex_lock(&common->mutex);
692 	rsi_dbg(INFO_ZONE, "%s: AMPDU action %d called\n", __func__, action);
693 	if (ssn != NULL)
694 		seq_no = *ssn;
695 
696 	switch (action) {
697 	case IEEE80211_AMPDU_RX_START:
698 		status = rsi_send_aggregation_params_frame(common,
699 							   tid,
700 							   seq_no,
701 							   buf_size,
702 							   STA_RX_ADDBA_DONE);
703 		break;
704 
705 	case IEEE80211_AMPDU_RX_STOP:
706 		status = rsi_send_aggregation_params_frame(common,
707 							   tid,
708 							   0,
709 							   buf_size,
710 							   STA_RX_DELBA);
711 		break;
712 
713 	case IEEE80211_AMPDU_TX_START:
714 		common->vif_info[ii].seq_start = seq_no;
715 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
716 		status = 0;
717 		break;
718 
719 	case IEEE80211_AMPDU_TX_STOP_CONT:
720 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
721 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
722 		status = rsi_send_aggregation_params_frame(common,
723 							   tid,
724 							   seq_no,
725 							   buf_size,
726 							   STA_TX_DELBA);
727 		if (!status)
728 			ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
729 		break;
730 
731 	case IEEE80211_AMPDU_TX_OPERATIONAL:
732 		status = rsi_send_aggregation_params_frame(common,
733 							   tid,
734 							   common->vif_info[ii]
735 								.seq_start,
736 							   buf_size,
737 							   STA_TX_ADDBA_DONE);
738 		break;
739 
740 	default:
741 		rsi_dbg(ERR_ZONE, "%s: Uknown AMPDU action\n", __func__);
742 		break;
743 	}
744 
745 	mutex_unlock(&common->mutex);
746 	return status;
747 }
748 
749 /**
750  * rsi_mac80211_set_rts_threshold() - This function sets rts threshold value.
751  * @hw: Pointer to the ieee80211_hw structure.
752  * @value: Rts threshold value.
753  *
754  * Return: 0 on success.
755  */
756 static int rsi_mac80211_set_rts_threshold(struct ieee80211_hw *hw,
757 					  u32 value)
758 {
759 	struct rsi_hw *adapter = hw->priv;
760 	struct rsi_common *common = adapter->priv;
761 
762 	mutex_lock(&common->mutex);
763 	common->rts_threshold = value;
764 	mutex_unlock(&common->mutex);
765 
766 	return 0;
767 }
768 
769 /**
770  * rsi_mac80211_set_rate_mask() - This function sets bitrate_mask to be used.
771  * @hw: Pointer to the ieee80211_hw structure
772  * @vif: Pointer to the ieee80211_vif structure.
773  * @mask: Pointer to the cfg80211_bitrate_mask structure.
774  *
775  * Return: 0 on success.
776  */
777 static int rsi_mac80211_set_rate_mask(struct ieee80211_hw *hw,
778 				      struct ieee80211_vif *vif,
779 				      const struct cfg80211_bitrate_mask *mask)
780 {
781 	struct rsi_hw *adapter = hw->priv;
782 	struct rsi_common *common = adapter->priv;
783 	enum ieee80211_band band = hw->conf.chandef.chan->band;
784 
785 	mutex_lock(&common->mutex);
786 	common->fixedrate_mask[band] = 0;
787 
788 	if (mask->control[band].legacy == 0xfff) {
789 		common->fixedrate_mask[band] =
790 			(mask->control[band].ht_mcs[0] << 12);
791 	} else {
792 		common->fixedrate_mask[band] =
793 			mask->control[band].legacy;
794 	}
795 	mutex_unlock(&common->mutex);
796 
797 	return 0;
798 }
799 
800 /**
801  * rsi_perform_cqm() - This function performs cqm.
802  * @common: Pointer to the driver private structure.
803  * @bssid: pointer to the bssid.
804  * @rssi: RSSI value.
805  */
806 static void rsi_perform_cqm(struct rsi_common *common,
807 			    u8 *bssid,
808 			    s8 rssi)
809 {
810 	struct rsi_hw *adapter = common->priv;
811 	s8 last_event = common->cqm_info.last_cqm_event_rssi;
812 	int thold = common->cqm_info.rssi_thold;
813 	u32 hyst = common->cqm_info.rssi_hyst;
814 	enum nl80211_cqm_rssi_threshold_event event;
815 
816 	if (rssi < thold && (last_event == 0 || rssi < (last_event - hyst)))
817 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
818 	else if (rssi > thold &&
819 		 (last_event == 0 || rssi > (last_event + hyst)))
820 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
821 	else
822 		return;
823 
824 	common->cqm_info.last_cqm_event_rssi = rssi;
825 	rsi_dbg(INFO_ZONE, "CQM: Notifying event: %d\n", event);
826 	ieee80211_cqm_rssi_notify(adapter->vifs[0], event, GFP_KERNEL);
827 
828 	return;
829 }
830 
831 /**
832  * rsi_fill_rx_status() - This function fills rx status in
833  *			  ieee80211_rx_status structure.
834  * @hw: Pointer to the ieee80211_hw structure.
835  * @skb: Pointer to the socket buffer structure.
836  * @common: Pointer to the driver private structure.
837  * @rxs: Pointer to the ieee80211_rx_status structure.
838  *
839  * Return: None.
840  */
841 static void rsi_fill_rx_status(struct ieee80211_hw *hw,
842 			       struct sk_buff *skb,
843 			       struct rsi_common *common,
844 			       struct ieee80211_rx_status *rxs)
845 {
846 	struct ieee80211_bss_conf *bss = &common->priv->vifs[0]->bss_conf;
847 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
848 	struct skb_info *rx_params = (struct skb_info *)info->driver_data;
849 	struct ieee80211_hdr *hdr;
850 	char rssi = rx_params->rssi;
851 	u8 hdrlen = 0;
852 	u8 channel = rx_params->channel;
853 	s32 freq;
854 
855 	hdr = ((struct ieee80211_hdr *)(skb->data));
856 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
857 
858 	memset(info, 0, sizeof(struct ieee80211_tx_info));
859 
860 	rxs->signal = -(rssi);
861 
862 	rxs->band = common->band;
863 
864 	freq = ieee80211_channel_to_frequency(channel, rxs->band);
865 
866 	if (freq)
867 		rxs->freq = freq;
868 
869 	if (ieee80211_has_protected(hdr->frame_control)) {
870 		if (rsi_is_cipher_wep(common)) {
871 			memmove(skb->data + 4, skb->data, hdrlen);
872 			skb_pull(skb, 4);
873 		} else {
874 			memmove(skb->data + 8, skb->data, hdrlen);
875 			skb_pull(skb, 8);
876 			rxs->flag |= RX_FLAG_MMIC_STRIPPED;
877 		}
878 		rxs->flag |= RX_FLAG_DECRYPTED;
879 		rxs->flag |= RX_FLAG_IV_STRIPPED;
880 	}
881 
882 	/* CQM only for connected AP beacons, the RSSI is a weighted avg */
883 	if (bss->assoc && !(memcmp(bss->bssid, hdr->addr2, ETH_ALEN))) {
884 		if (ieee80211_is_beacon(hdr->frame_control))
885 			rsi_perform_cqm(common, hdr->addr2, rxs->signal);
886 	}
887 
888 	return;
889 }
890 
891 /**
892  * rsi_indicate_pkt_to_os() - This function sends recieved packet to mac80211.
893  * @common: Pointer to the driver private structure.
894  * @skb: Pointer to the socket buffer structure.
895  *
896  * Return: None.
897  */
898 void rsi_indicate_pkt_to_os(struct rsi_common *common,
899 			    struct sk_buff *skb)
900 {
901 	struct rsi_hw *adapter = common->priv;
902 	struct ieee80211_hw *hw = adapter->hw;
903 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
904 
905 	if ((common->iface_down) || (!adapter->sc_nvifs)) {
906 		dev_kfree_skb(skb);
907 		return;
908 	}
909 
910 	/* filling in the ieee80211_rx_status flags */
911 	rsi_fill_rx_status(hw, skb, common, rx_status);
912 
913 	ieee80211_rx_irqsafe(hw, skb);
914 }
915 
916 static void rsi_set_min_rate(struct ieee80211_hw *hw,
917 			     struct ieee80211_sta *sta,
918 			     struct rsi_common *common)
919 {
920 	u8 band = hw->conf.chandef.chan->band;
921 	u8 ii;
922 	u32 rate_bitmap;
923 	bool matched = false;
924 
925 	common->bitrate_mask[band] = sta->supp_rates[band];
926 
927 	rate_bitmap = (common->fixedrate_mask[band] & sta->supp_rates[band]);
928 
929 	if (rate_bitmap & 0xfff) {
930 		/* Find out the min rate */
931 		for (ii = 0; ii < ARRAY_SIZE(rsi_rates); ii++) {
932 			if (rate_bitmap & BIT(ii)) {
933 				common->min_rate = rsi_rates[ii].hw_value;
934 				matched = true;
935 				break;
936 			}
937 		}
938 	}
939 
940 	common->vif_info[0].is_ht = sta->ht_cap.ht_supported;
941 
942 	if ((common->vif_info[0].is_ht) && (rate_bitmap >> 12)) {
943 		for (ii = 0; ii < ARRAY_SIZE(rsi_mcsrates); ii++) {
944 			if ((rate_bitmap >> 12) & BIT(ii)) {
945 				common->min_rate = rsi_mcsrates[ii];
946 				matched = true;
947 				break;
948 			}
949 		}
950 	}
951 
952 	if (!matched)
953 		common->min_rate = 0xffff;
954 }
955 
956 /**
957  * rsi_mac80211_sta_add() - This function notifies driver about a peer getting
958  *			    connected.
959  * @hw: pointer to the ieee80211_hw structure.
960  * @vif: Pointer to the ieee80211_vif structure.
961  * @sta: Pointer to the ieee80211_sta structure.
962  *
963  * Return: 0 on success, -1 on failure.
964  */
965 static int rsi_mac80211_sta_add(struct ieee80211_hw *hw,
966 				struct ieee80211_vif *vif,
967 				struct ieee80211_sta *sta)
968 {
969 	struct rsi_hw *adapter = hw->priv;
970 	struct rsi_common *common = adapter->priv;
971 
972 	mutex_lock(&common->mutex);
973 
974 	rsi_set_min_rate(hw, sta, common);
975 
976 	if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ||
977 	    (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40)) {
978 		common->vif_info[0].sgi = true;
979 	}
980 
981 	if (sta->ht_cap.ht_supported)
982 		ieee80211_start_tx_ba_session(sta, 0, 0);
983 
984 	mutex_unlock(&common->mutex);
985 
986 	return 0;
987 }
988 
989 /**
990  * rsi_mac80211_sta_remove() - This function notifies driver about a peer
991  *			       getting disconnected.
992  * @hw: Pointer to the ieee80211_hw structure.
993  * @vif: Pointer to the ieee80211_vif structure.
994  * @sta: Pointer to the ieee80211_sta structure.
995  *
996  * Return: 0 on success, -1 on failure.
997  */
998 static int rsi_mac80211_sta_remove(struct ieee80211_hw *hw,
999 				   struct ieee80211_vif *vif,
1000 				   struct ieee80211_sta *sta)
1001 {
1002 	struct rsi_hw *adapter = hw->priv;
1003 	struct rsi_common *common = adapter->priv;
1004 
1005 	mutex_lock(&common->mutex);
1006 	/* Resetting all the fields to default values */
1007 	common->bitrate_mask[IEEE80211_BAND_2GHZ] = 0;
1008 	common->bitrate_mask[IEEE80211_BAND_5GHZ] = 0;
1009 	common->min_rate = 0xffff;
1010 	common->vif_info[0].is_ht = false;
1011 	common->vif_info[0].sgi = false;
1012 	common->vif_info[0].seq_start = 0;
1013 	common->secinfo.ptk_cipher = 0;
1014 	common->secinfo.gtk_cipher = 0;
1015 	mutex_unlock(&common->mutex);
1016 
1017 	return 0;
1018 }
1019 
1020 static struct ieee80211_ops mac80211_ops = {
1021 	.tx = rsi_mac80211_tx,
1022 	.start = rsi_mac80211_start,
1023 	.stop = rsi_mac80211_stop,
1024 	.add_interface = rsi_mac80211_add_interface,
1025 	.remove_interface = rsi_mac80211_remove_interface,
1026 	.config = rsi_mac80211_config,
1027 	.bss_info_changed = rsi_mac80211_bss_info_changed,
1028 	.conf_tx = rsi_mac80211_conf_tx,
1029 	.configure_filter = rsi_mac80211_conf_filter,
1030 	.set_key = rsi_mac80211_set_key,
1031 	.set_rts_threshold = rsi_mac80211_set_rts_threshold,
1032 	.set_bitrate_mask = rsi_mac80211_set_rate_mask,
1033 	.ampdu_action = rsi_mac80211_ampdu_action,
1034 	.sta_add = rsi_mac80211_sta_add,
1035 	.sta_remove = rsi_mac80211_sta_remove,
1036 };
1037 
1038 /**
1039  * rsi_mac80211_attach() - This function is used to initialize Mac80211 stack.
1040  * @common: Pointer to the driver private structure.
1041  *
1042  * Return: 0 on success, -1 on failure.
1043  */
1044 int rsi_mac80211_attach(struct rsi_common *common)
1045 {
1046 	int status = 0;
1047 	struct ieee80211_hw *hw = NULL;
1048 	struct wiphy *wiphy = NULL;
1049 	struct rsi_hw *adapter = common->priv;
1050 	u8 addr_mask[ETH_ALEN] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x3};
1051 
1052 	rsi_dbg(INIT_ZONE, "%s: Performing mac80211 attach\n", __func__);
1053 
1054 	hw = ieee80211_alloc_hw(sizeof(struct rsi_hw), &mac80211_ops);
1055 	if (!hw) {
1056 		rsi_dbg(ERR_ZONE, "%s: ieee80211 hw alloc failed\n", __func__);
1057 		return -ENOMEM;
1058 	}
1059 
1060 	wiphy = hw->wiphy;
1061 
1062 	SET_IEEE80211_DEV(hw, adapter->device);
1063 
1064 	hw->priv = adapter;
1065 	adapter->hw = hw;
1066 
1067 	ieee80211_hw_set(hw, SIGNAL_DBM);
1068 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
1069 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1070 
1071 	hw->queues = MAX_HW_QUEUES;
1072 	hw->extra_tx_headroom = RSI_NEEDED_HEADROOM;
1073 
1074 	hw->max_rates = 1;
1075 	hw->max_rate_tries = MAX_RETRIES;
1076 
1077 	hw->max_tx_aggregation_subframes = 6;
1078 	rsi_register_rates_channels(adapter, IEEE80211_BAND_2GHZ);
1079 	rsi_register_rates_channels(adapter, IEEE80211_BAND_5GHZ);
1080 	hw->rate_control_algorithm = "AARF";
1081 
1082 	SET_IEEE80211_PERM_ADDR(hw, common->mac_addr);
1083 	ether_addr_copy(hw->wiphy->addr_mask, addr_mask);
1084 
1085 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1086 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1087 	wiphy->retry_short = RETRY_SHORT;
1088 	wiphy->retry_long  = RETRY_LONG;
1089 	wiphy->frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1090 	wiphy->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1091 	wiphy->flags = 0;
1092 
1093 	wiphy->available_antennas_rx = 1;
1094 	wiphy->available_antennas_tx = 1;
1095 	wiphy->bands[IEEE80211_BAND_2GHZ] =
1096 		&adapter->sbands[IEEE80211_BAND_2GHZ];
1097 	wiphy->bands[IEEE80211_BAND_5GHZ] =
1098 		&adapter->sbands[IEEE80211_BAND_5GHZ];
1099 
1100 	status = ieee80211_register_hw(hw);
1101 	if (status)
1102 		return status;
1103 
1104 	return rsi_init_dbgfs(adapter);
1105 }
1106