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_8K;
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_attach() - 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_mac80211_config() - This function is a handler for configuration
345  *			   requests. The stack calls this function to
346  *			   change hardware configuration, e.g., channel.
347  * @hw: Pointer to the ieee80211_hw structure.
348  * @changed: Changed flags set.
349  *
350  * Return: 0 on success, negative error code on failure.
351  */
352 static int rsi_mac80211_config(struct ieee80211_hw *hw,
353 			       u32 changed)
354 {
355 	struct rsi_hw *adapter = hw->priv;
356 	struct rsi_common *common = adapter->priv;
357 	int status = -EOPNOTSUPP;
358 
359 	mutex_lock(&common->mutex);
360 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
361 		struct ieee80211_channel *curchan = hw->conf.chandef.chan;
362 		u16 channel = curchan->hw_value;
363 
364 		rsi_dbg(INFO_ZONE,
365 			"%s: Set channel: %d MHz type: %d channel_no %d\n",
366 			__func__, curchan->center_freq,
367 			curchan->flags, channel);
368 		common->band = curchan->band;
369 		status = rsi_set_channel(adapter->priv, channel);
370 	}
371 	mutex_unlock(&common->mutex);
372 
373 	return status;
374 }
375 
376 /**
377  * rsi_get_connected_channel() - This function is used to get the current
378  *				 connected channel number.
379  * @adapter: Pointer to the adapter structure.
380  *
381  * Return: Current connected AP's channel number is returned.
382  */
383 u16 rsi_get_connected_channel(struct rsi_hw *adapter)
384 {
385 	struct ieee80211_vif *vif = adapter->vifs[0];
386 	if (vif) {
387 		struct ieee80211_bss_conf *bss = &vif->bss_conf;
388 		struct ieee80211_channel *channel = bss->chandef.chan;
389 		return channel->hw_value;
390 	}
391 
392 	return 0;
393 }
394 
395 /**
396  * rsi_mac80211_bss_info_changed() - This function is a handler for config
397  *				     requests related to BSS parameters that
398  *				     may vary during BSS's lifespan.
399  * @hw: Pointer to the ieee80211_hw structure.
400  * @vif: Pointer to the ieee80211_vif structure.
401  * @bss_conf: Pointer to the ieee80211_bss_conf structure.
402  * @changed: Changed flags set.
403  *
404  * Return: None.
405  */
406 static void rsi_mac80211_bss_info_changed(struct ieee80211_hw *hw,
407 					  struct ieee80211_vif *vif,
408 					  struct ieee80211_bss_conf *bss_conf,
409 					  u32 changed)
410 {
411 	struct rsi_hw *adapter = hw->priv;
412 	struct rsi_common *common = adapter->priv;
413 
414 	mutex_lock(&common->mutex);
415 	if (changed & BSS_CHANGED_ASSOC) {
416 		rsi_dbg(INFO_ZONE, "%s: Changed Association status: %d\n",
417 			__func__, bss_conf->assoc);
418 		rsi_inform_bss_status(common,
419 				      bss_conf->assoc,
420 				      bss_conf->bssid,
421 				      bss_conf->qos,
422 				      bss_conf->aid);
423 	}
424 	mutex_unlock(&common->mutex);
425 }
426 
427 /**
428  * rsi_mac80211_conf_filter() - This function configure the device's RX filter.
429  * @hw: Pointer to the ieee80211_hw structure.
430  * @changed: Changed flags set.
431  * @total_flags: Total initial flags set.
432  * @multicast: Multicast.
433  *
434  * Return: None.
435  */
436 static void rsi_mac80211_conf_filter(struct ieee80211_hw *hw,
437 				     u32 changed_flags,
438 				     u32 *total_flags,
439 				     u64 multicast)
440 {
441 	/* Not doing much here as of now */
442 	*total_flags &= RSI_SUPP_FILTERS;
443 }
444 
445 /**
446  * rsi_mac80211_conf_tx() - This function configures TX queue parameters
447  *			    (EDCF (aifs, cw_min, cw_max), bursting)
448  *			    for a hardware TX queue.
449  * @hw: Pointer to the ieee80211_hw structure
450  * @vif: Pointer to the ieee80211_vif structure.
451  * @queue: Queue number.
452  * @params: Pointer to ieee80211_tx_queue_params structure.
453  *
454  * Return: 0 on success, negative error code on failure.
455  */
456 static int rsi_mac80211_conf_tx(struct ieee80211_hw *hw,
457 				struct ieee80211_vif *vif, u16 queue,
458 				const struct ieee80211_tx_queue_params *params)
459 {
460 	struct rsi_hw *adapter = hw->priv;
461 	struct rsi_common *common = adapter->priv;
462 	u8 idx = 0;
463 
464 	if (queue >= IEEE80211_NUM_ACS)
465 		return 0;
466 
467 	rsi_dbg(INFO_ZONE,
468 		"%s: Conf queue %d, aifs: %d, cwmin: %d cwmax: %d, txop: %d\n",
469 		__func__, queue, params->aifs,
470 		params->cw_min, params->cw_max, params->txop);
471 
472 	mutex_lock(&common->mutex);
473 	/* Map into the way the f/w expects */
474 	switch (queue) {
475 	case IEEE80211_AC_VO:
476 		idx = VO_Q;
477 		break;
478 	case IEEE80211_AC_VI:
479 		idx = VI_Q;
480 		break;
481 	case IEEE80211_AC_BE:
482 		idx = BE_Q;
483 		break;
484 	case IEEE80211_AC_BK:
485 		idx = BK_Q;
486 		break;
487 	default:
488 		idx = BE_Q;
489 		break;
490 	}
491 
492 	memcpy(&common->edca_params[idx],
493 	       params,
494 	       sizeof(struct ieee80211_tx_queue_params));
495 	mutex_unlock(&common->mutex);
496 
497 	return 0;
498 }
499 
500 /**
501  * rsi_hal_key_config() - This function loads the keys into the firmware.
502  * @hw: Pointer to the ieee80211_hw structure.
503  * @vif: Pointer to the ieee80211_vif structure.
504  * @key: Pointer to the ieee80211_key_conf structure.
505  *
506  * Return: status: 0 on success, -1 on failure.
507  */
508 static int rsi_hal_key_config(struct ieee80211_hw *hw,
509 			      struct ieee80211_vif *vif,
510 			      struct ieee80211_key_conf *key)
511 {
512 	struct rsi_hw *adapter = hw->priv;
513 	int status;
514 	u8 key_type;
515 
516 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
517 		key_type = RSI_PAIRWISE_KEY;
518 	else
519 		key_type = RSI_GROUP_KEY;
520 
521 	rsi_dbg(ERR_ZONE, "%s: Cipher 0x%x key_type: %d key_len: %d\n",
522 		__func__, key->cipher, key_type, key->keylen);
523 
524 	if ((key->cipher == WLAN_CIPHER_SUITE_WEP104) ||
525 	    (key->cipher == WLAN_CIPHER_SUITE_WEP40)) {
526 		status = rsi_hal_load_key(adapter->priv,
527 					  key->key,
528 					  key->keylen,
529 					  RSI_PAIRWISE_KEY,
530 					  key->keyidx,
531 					  key->cipher);
532 		if (status)
533 			return status;
534 	}
535 	return rsi_hal_load_key(adapter->priv,
536 				key->key,
537 				key->keylen,
538 				key_type,
539 				key->keyidx,
540 				key->cipher);
541 }
542 
543 /**
544  * rsi_mac80211_set_key() - This function sets type of key to be loaded.
545  * @hw: Pointer to the ieee80211_hw structure.
546  * @cmd: enum set_key_cmd.
547  * @vif: Pointer to the ieee80211_vif structure.
548  * @sta: Pointer to the ieee80211_sta structure.
549  * @key: Pointer to the ieee80211_key_conf structure.
550  *
551  * Return: status: 0 on success, negative error code on failure.
552  */
553 static int rsi_mac80211_set_key(struct ieee80211_hw *hw,
554 				enum set_key_cmd cmd,
555 				struct ieee80211_vif *vif,
556 				struct ieee80211_sta *sta,
557 				struct ieee80211_key_conf *key)
558 {
559 	struct rsi_hw *adapter = hw->priv;
560 	struct rsi_common *common = adapter->priv;
561 	struct security_info *secinfo = &common->secinfo;
562 	int status;
563 
564 	mutex_lock(&common->mutex);
565 	switch (cmd) {
566 	case SET_KEY:
567 		secinfo->security_enable = true;
568 		status = rsi_hal_key_config(hw, vif, key);
569 		if (status) {
570 			mutex_unlock(&common->mutex);
571 			return status;
572 		}
573 
574 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
575 			secinfo->ptk_cipher = key->cipher;
576 		else
577 			secinfo->gtk_cipher = key->cipher;
578 
579 		key->hw_key_idx = key->keyidx;
580 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
581 
582 		rsi_dbg(ERR_ZONE, "%s: RSI set_key\n", __func__);
583 		break;
584 
585 	case DISABLE_KEY:
586 		secinfo->security_enable = false;
587 		rsi_dbg(ERR_ZONE, "%s: RSI del key\n", __func__);
588 		memset(key, 0, sizeof(struct ieee80211_key_conf));
589 		status = rsi_hal_key_config(hw, vif, key);
590 		break;
591 
592 	default:
593 		status = -EOPNOTSUPP;
594 		break;
595 	}
596 
597 	mutex_unlock(&common->mutex);
598 	return status;
599 }
600 
601 /**
602  * rsi_mac80211_ampdu_action() - This function selects the AMPDU action for
603  *				 the corresponding mlme_action flag and
604  *				 informs the f/w regarding this.
605  * @hw: Pointer to the ieee80211_hw structure.
606  * @vif: Pointer to the ieee80211_vif structure.
607  * @action: ieee80211_ampdu_mlme_action enum.
608  * @sta: Pointer to the ieee80211_sta structure.
609  * @tid: Traffic identifier.
610  * @ssn: Pointer to ssn value.
611  * @buf_size: Buffer size (for kernel version > 2.6.38).
612  *
613  * Return: status: 0 on success, negative error code on failure.
614  */
615 static int rsi_mac80211_ampdu_action(struct ieee80211_hw *hw,
616 				     struct ieee80211_vif *vif,
617 				     enum ieee80211_ampdu_mlme_action action,
618 				     struct ieee80211_sta *sta,
619 				     unsigned short tid,
620 				     unsigned short *ssn,
621 				     unsigned char buf_size)
622 {
623 	int status = -EOPNOTSUPP;
624 	struct rsi_hw *adapter = hw->priv;
625 	struct rsi_common *common = adapter->priv;
626 	u16 seq_no = 0;
627 	u8 ii = 0;
628 
629 	for (ii = 0; ii < RSI_MAX_VIFS; ii++) {
630 		if (vif == adapter->vifs[ii])
631 			break;
632 	}
633 
634 	mutex_lock(&common->mutex);
635 	rsi_dbg(INFO_ZONE, "%s: AMPDU action %d called\n", __func__, action);
636 	if (ssn != NULL)
637 		seq_no = *ssn;
638 
639 	switch (action) {
640 	case IEEE80211_AMPDU_RX_START:
641 		status = rsi_send_aggregation_params_frame(common,
642 							   tid,
643 							   seq_no,
644 							   buf_size,
645 							   STA_RX_ADDBA_DONE);
646 		break;
647 
648 	case IEEE80211_AMPDU_RX_STOP:
649 		status = rsi_send_aggregation_params_frame(common,
650 							   tid,
651 							   0,
652 							   buf_size,
653 							   STA_RX_DELBA);
654 		break;
655 
656 	case IEEE80211_AMPDU_TX_START:
657 		common->vif_info[ii].seq_start = seq_no;
658 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
659 		break;
660 
661 	case IEEE80211_AMPDU_TX_STOP_CONT:
662 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
663 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
664 		status = rsi_send_aggregation_params_frame(common,
665 							   tid,
666 							   seq_no,
667 							   buf_size,
668 							   STA_TX_DELBA);
669 		if (!status)
670 			ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
671 		break;
672 
673 	case IEEE80211_AMPDU_TX_OPERATIONAL:
674 		status = rsi_send_aggregation_params_frame(common,
675 							   tid,
676 							   common->vif_info[ii]
677 								.seq_start,
678 							   buf_size,
679 							   STA_TX_ADDBA_DONE);
680 		break;
681 
682 	default:
683 		rsi_dbg(ERR_ZONE, "%s: Uknown AMPDU action\n", __func__);
684 		break;
685 	}
686 
687 	mutex_unlock(&common->mutex);
688 	return status;
689 }
690 
691 /**
692  * rsi_mac80211_set_rts_threshold() - This function sets rts threshold value.
693  * @hw: Pointer to the ieee80211_hw structure.
694  * @value: Rts threshold value.
695  *
696  * Return: 0 on success.
697  */
698 static int rsi_mac80211_set_rts_threshold(struct ieee80211_hw *hw,
699 					  u32 value)
700 {
701 	struct rsi_hw *adapter = hw->priv;
702 	struct rsi_common *common = adapter->priv;
703 
704 	mutex_lock(&common->mutex);
705 	common->rts_threshold = value;
706 	mutex_unlock(&common->mutex);
707 
708 	return 0;
709 }
710 
711 /**
712  * rsi_mac80211_set_rate_mask() - This function sets bitrate_mask to be used.
713  * @hw: Pointer to the ieee80211_hw structure
714  * @vif: Pointer to the ieee80211_vif structure.
715  * @mask: Pointer to the cfg80211_bitrate_mask structure.
716  *
717  * Return: 0 on success.
718  */
719 static int rsi_mac80211_set_rate_mask(struct ieee80211_hw *hw,
720 				      struct ieee80211_vif *vif,
721 				      const struct cfg80211_bitrate_mask *mask)
722 {
723 	struct rsi_hw *adapter = hw->priv;
724 	struct rsi_common *common = adapter->priv;
725 
726 	mutex_lock(&common->mutex);
727 
728 	common->fixedrate_mask[IEEE80211_BAND_2GHZ] = 0;
729 
730 	if (mask->control[IEEE80211_BAND_2GHZ].legacy == 0xfff) {
731 		common->fixedrate_mask[IEEE80211_BAND_2GHZ] =
732 			(mask->control[IEEE80211_BAND_2GHZ].ht_mcs[0] << 12);
733 	} else {
734 		common->fixedrate_mask[IEEE80211_BAND_2GHZ] =
735 			mask->control[IEEE80211_BAND_2GHZ].legacy;
736 	}
737 	mutex_unlock(&common->mutex);
738 
739 	return 0;
740 }
741 
742 /**
743  * rsi_fill_rx_status() - This function fills rx status in
744  *			  ieee80211_rx_status structure.
745  * @hw: Pointer to the ieee80211_hw structure.
746  * @skb: Pointer to the socket buffer structure.
747  * @common: Pointer to the driver private structure.
748  * @rxs: Pointer to the ieee80211_rx_status structure.
749  *
750  * Return: None.
751  */
752 static void rsi_fill_rx_status(struct ieee80211_hw *hw,
753 			       struct sk_buff *skb,
754 			       struct rsi_common *common,
755 			       struct ieee80211_rx_status *rxs)
756 {
757 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
758 	struct skb_info *rx_params = (struct skb_info *)info->driver_data;
759 	struct ieee80211_hdr *hdr;
760 	char rssi = rx_params->rssi;
761 	u8 hdrlen = 0;
762 	u8 channel = rx_params->channel;
763 	s32 freq;
764 
765 	hdr = ((struct ieee80211_hdr *)(skb->data));
766 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
767 
768 	memset(info, 0, sizeof(struct ieee80211_tx_info));
769 
770 	rxs->signal = -(rssi);
771 
772 	if (channel <= 14)
773 		rxs->band = IEEE80211_BAND_2GHZ;
774 	else
775 		rxs->band = IEEE80211_BAND_5GHZ;
776 
777 	freq = ieee80211_channel_to_frequency(channel, rxs->band);
778 
779 	if (freq)
780 		rxs->freq = freq;
781 
782 	if (ieee80211_has_protected(hdr->frame_control)) {
783 		if (rsi_is_cipher_wep(common)) {
784 			memmove(skb->data + 4, skb->data, hdrlen);
785 			skb_pull(skb, 4);
786 		} else {
787 			memmove(skb->data + 8, skb->data, hdrlen);
788 			skb_pull(skb, 8);
789 			rxs->flag |= RX_FLAG_MMIC_STRIPPED;
790 		}
791 		rxs->flag |= RX_FLAG_DECRYPTED;
792 		rxs->flag |= RX_FLAG_IV_STRIPPED;
793 	}
794 }
795 
796 /**
797  * rsi_indicate_pkt_to_os() - This function sends recieved packet to mac80211.
798  * @common: Pointer to the driver private structure.
799  * @skb: Pointer to the socket buffer structure.
800  *
801  * Return: None.
802  */
803 void rsi_indicate_pkt_to_os(struct rsi_common *common,
804 			    struct sk_buff *skb)
805 {
806 	struct rsi_hw *adapter = common->priv;
807 	struct ieee80211_hw *hw = adapter->hw;
808 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
809 
810 	if ((common->iface_down) || (!adapter->sc_nvifs)) {
811 		dev_kfree_skb(skb);
812 		return;
813 	}
814 
815 	/* filling in the ieee80211_rx_status flags */
816 	rsi_fill_rx_status(hw, skb, common, rx_status);
817 
818 	ieee80211_rx_irqsafe(hw, skb);
819 }
820 
821 static void rsi_set_min_rate(struct ieee80211_hw *hw,
822 			     struct ieee80211_sta *sta,
823 			     struct rsi_common *common)
824 {
825 	u8 band = hw->conf.chandef.chan->band;
826 	u8 ii;
827 	u32 rate_bitmap;
828 	bool matched = false;
829 
830 	common->bitrate_mask[band] = sta->supp_rates[band];
831 
832 	rate_bitmap = (common->fixedrate_mask[band] & sta->supp_rates[band]);
833 
834 	if (rate_bitmap & 0xfff) {
835 		/* Find out the min rate */
836 		for (ii = 0; ii < ARRAY_SIZE(rsi_rates); ii++) {
837 			if (rate_bitmap & BIT(ii)) {
838 				common->min_rate = rsi_rates[ii].hw_value;
839 				matched = true;
840 				break;
841 			}
842 		}
843 	}
844 
845 	common->vif_info[0].is_ht = sta->ht_cap.ht_supported;
846 
847 	if ((common->vif_info[0].is_ht) && (rate_bitmap >> 12)) {
848 		for (ii = 0; ii < ARRAY_SIZE(rsi_mcsrates); ii++) {
849 			if ((rate_bitmap >> 12) & BIT(ii)) {
850 				common->min_rate = rsi_mcsrates[ii];
851 				matched = true;
852 				break;
853 			}
854 		}
855 	}
856 
857 	if (!matched)
858 		common->min_rate = 0xffff;
859 }
860 
861 /**
862  * rsi_mac80211_sta_add() - This function notifies driver about a peer getting
863  *			    connected.
864  * @hw: pointer to the ieee80211_hw structure.
865  * @vif: Pointer to the ieee80211_vif structure.
866  * @sta: Pointer to the ieee80211_sta structure.
867  *
868  * Return: 0 on success, -1 on failure.
869  */
870 static int rsi_mac80211_sta_add(struct ieee80211_hw *hw,
871 				struct ieee80211_vif *vif,
872 				struct ieee80211_sta *sta)
873 {
874 	struct rsi_hw *adapter = hw->priv;
875 	struct rsi_common *common = adapter->priv;
876 
877 	mutex_lock(&common->mutex);
878 
879 	rsi_set_min_rate(hw, sta, common);
880 
881 	if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ||
882 	    (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40)) {
883 		common->vif_info[0].sgi = true;
884 	}
885 
886 	if (sta->ht_cap.ht_supported)
887 		ieee80211_start_tx_ba_session(sta, 0, 0);
888 
889 	mutex_unlock(&common->mutex);
890 
891 	return 0;
892 }
893 
894 /**
895  * rsi_mac80211_sta_remove() - This function notifies driver about a peer
896  *			       getting disconnected.
897  * @hw: Pointer to the ieee80211_hw structure.
898  * @vif: Pointer to the ieee80211_vif structure.
899  * @sta: Pointer to the ieee80211_sta structure.
900  *
901  * Return: 0 on success, -1 on failure.
902  */
903 static int rsi_mac80211_sta_remove(struct ieee80211_hw *hw,
904 				   struct ieee80211_vif *vif,
905 				   struct ieee80211_sta *sta)
906 {
907 	struct rsi_hw *adapter = hw->priv;
908 	struct rsi_common *common = adapter->priv;
909 
910 	mutex_lock(&common->mutex);
911 	/* Resetting all the fields to default values */
912 	common->bitrate_mask[IEEE80211_BAND_2GHZ] = 0;
913 	common->bitrate_mask[IEEE80211_BAND_5GHZ] = 0;
914 	common->min_rate = 0xffff;
915 	common->vif_info[0].is_ht = false;
916 	common->vif_info[0].sgi = false;
917 	common->vif_info[0].seq_start = 0;
918 	common->secinfo.ptk_cipher = 0;
919 	common->secinfo.gtk_cipher = 0;
920 	mutex_unlock(&common->mutex);
921 
922 	return 0;
923 }
924 
925 static struct ieee80211_ops mac80211_ops = {
926 	.tx = rsi_mac80211_tx,
927 	.start = rsi_mac80211_start,
928 	.stop = rsi_mac80211_stop,
929 	.add_interface = rsi_mac80211_add_interface,
930 	.remove_interface = rsi_mac80211_remove_interface,
931 	.config = rsi_mac80211_config,
932 	.bss_info_changed = rsi_mac80211_bss_info_changed,
933 	.conf_tx = rsi_mac80211_conf_tx,
934 	.configure_filter = rsi_mac80211_conf_filter,
935 	.set_key = rsi_mac80211_set_key,
936 	.set_rts_threshold = rsi_mac80211_set_rts_threshold,
937 	.set_bitrate_mask = rsi_mac80211_set_rate_mask,
938 	.ampdu_action = rsi_mac80211_ampdu_action,
939 	.sta_add = rsi_mac80211_sta_add,
940 	.sta_remove = rsi_mac80211_sta_remove,
941 };
942 
943 /**
944  * rsi_mac80211_attach() - This function is used to initialize Mac80211 stack.
945  * @common: Pointer to the driver private structure.
946  *
947  * Return: 0 on success, -1 on failure.
948  */
949 int rsi_mac80211_attach(struct rsi_common *common)
950 {
951 	int status = 0;
952 	struct ieee80211_hw *hw = NULL;
953 	struct wiphy *wiphy = NULL;
954 	struct rsi_hw *adapter = common->priv;
955 	u8 addr_mask[ETH_ALEN] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x3};
956 
957 	rsi_dbg(INIT_ZONE, "%s: Performing mac80211 attach\n", __func__);
958 
959 	hw = ieee80211_alloc_hw(sizeof(struct rsi_hw), &mac80211_ops);
960 	if (!hw) {
961 		rsi_dbg(ERR_ZONE, "%s: ieee80211 hw alloc failed\n", __func__);
962 		return -ENOMEM;
963 	}
964 
965 	wiphy = hw->wiphy;
966 
967 	SET_IEEE80211_DEV(hw, adapter->device);
968 
969 	hw->priv = adapter;
970 	adapter->hw = hw;
971 
972 	hw->flags = IEEE80211_HW_SIGNAL_DBM |
973 		    IEEE80211_HW_HAS_RATE_CONTROL |
974 		    IEEE80211_HW_AMPDU_AGGREGATION |
975 		    0;
976 
977 	hw->queues = MAX_HW_QUEUES;
978 	hw->extra_tx_headroom = RSI_NEEDED_HEADROOM;
979 
980 	hw->max_rates = 1;
981 	hw->max_rate_tries = MAX_RETRIES;
982 
983 	hw->max_tx_aggregation_subframes = 6;
984 	rsi_register_rates_channels(adapter, IEEE80211_BAND_2GHZ);
985 	hw->rate_control_algorithm = "AARF";
986 
987 	SET_IEEE80211_PERM_ADDR(hw, common->mac_addr);
988 	ether_addr_copy(hw->wiphy->addr_mask, addr_mask);
989 
990 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
991 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
992 	wiphy->retry_short = RETRY_SHORT;
993 	wiphy->retry_long  = RETRY_LONG;
994 	wiphy->frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
995 	wiphy->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
996 	wiphy->flags = 0;
997 
998 	wiphy->available_antennas_rx = 1;
999 	wiphy->available_antennas_tx = 1;
1000 	wiphy->bands[IEEE80211_BAND_2GHZ] =
1001 		&adapter->sbands[IEEE80211_BAND_2GHZ];
1002 
1003 	status = ieee80211_register_hw(hw);
1004 	if (status)
1005 		return status;
1006 
1007 	return rsi_init_dbgfs(adapter);
1008 }
1009