1 /*
2  * Marvell Wireless LAN device driver: 802.11n
3  *
4  * Copyright (C) 2011-2014, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19 
20 #include "decl.h"
21 #include "ioctl.h"
22 #include "util.h"
23 #include "fw.h"
24 #include "main.h"
25 #include "wmm.h"
26 #include "11n.h"
27 
28 /*
29  * Fills HT capability information field, AMPDU Parameters field, HT extended
30  * capability field, and supported MCS set fields.
31  *
32  * HT capability information field, AMPDU Parameters field, supported MCS set
33  * fields are retrieved from cfg80211 stack
34  *
35  * RD responder bit to set to clear in the extended capability header.
36  */
37 int mwifiex_fill_cap_info(struct mwifiex_private *priv, u8 radio_type,
38 			  struct ieee80211_ht_cap *ht_cap)
39 {
40 	uint16_t ht_ext_cap = le16_to_cpu(ht_cap->extended_ht_cap_info);
41 	struct ieee80211_supported_band *sband =
42 					priv->wdev.wiphy->bands[radio_type];
43 
44 	if (WARN_ON_ONCE(!sband)) {
45 		mwifiex_dbg(priv->adapter, ERROR, "Invalid radio type!\n");
46 		return -EINVAL;
47 	}
48 
49 	ht_cap->ampdu_params_info =
50 		(sband->ht_cap.ampdu_factor &
51 		 IEEE80211_HT_AMPDU_PARM_FACTOR) |
52 		((sband->ht_cap.ampdu_density <<
53 		 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT) &
54 		 IEEE80211_HT_AMPDU_PARM_DENSITY);
55 
56 	memcpy((u8 *)&ht_cap->mcs, &sband->ht_cap.mcs,
57 	       sizeof(sband->ht_cap.mcs));
58 
59 	if (priv->bss_mode == NL80211_IFTYPE_STATION ||
60 	    (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
61 	     (priv->adapter->sec_chan_offset !=
62 					IEEE80211_HT_PARAM_CHA_SEC_NONE)))
63 		/* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
64 		SETHT_MCS32(ht_cap->mcs.rx_mask);
65 
66 	/* Clear RD responder bit */
67 	ht_ext_cap &= ~IEEE80211_HT_EXT_CAP_RD_RESPONDER;
68 
69 	ht_cap->cap_info = cpu_to_le16(sband->ht_cap.cap);
70 	ht_cap->extended_ht_cap_info = cpu_to_le16(ht_ext_cap);
71 
72 	if (ISSUPP_BEAMFORMING(priv->adapter->hw_dot_11n_dev_cap))
73 		ht_cap->tx_BF_cap_info = cpu_to_le32(MWIFIEX_DEF_11N_TX_BF_CAP);
74 
75 	return 0;
76 }
77 
78 /*
79  * This function returns the pointer to an entry in BA Stream
80  * table which matches the requested BA status.
81  */
82 static struct mwifiex_tx_ba_stream_tbl *
83 mwifiex_get_ba_status(struct mwifiex_private *priv,
84 		      enum mwifiex_ba_status ba_status)
85 {
86 	struct mwifiex_tx_ba_stream_tbl *tx_ba_tsr_tbl;
87 	unsigned long flags;
88 
89 	spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
90 	list_for_each_entry(tx_ba_tsr_tbl, &priv->tx_ba_stream_tbl_ptr, list) {
91 		if (tx_ba_tsr_tbl->ba_status == ba_status) {
92 			spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock,
93 					       flags);
94 			return tx_ba_tsr_tbl;
95 		}
96 	}
97 	spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
98 	return NULL;
99 }
100 
101 /*
102  * This function handles the command response of delete a block
103  * ack request.
104  *
105  * The function checks the response success status and takes action
106  * accordingly (send an add BA request in case of success, or recreate
107  * the deleted stream in case of failure, if the add BA was also
108  * initiated by us).
109  */
110 int mwifiex_ret_11n_delba(struct mwifiex_private *priv,
111 			  struct host_cmd_ds_command *resp)
112 {
113 	int tid;
114 	struct mwifiex_tx_ba_stream_tbl *tx_ba_tbl;
115 	struct host_cmd_ds_11n_delba *del_ba = &resp->params.del_ba;
116 	uint16_t del_ba_param_set = le16_to_cpu(del_ba->del_ba_param_set);
117 
118 	tid = del_ba_param_set >> DELBA_TID_POS;
119 	if (del_ba->del_result == BA_RESULT_SUCCESS) {
120 		mwifiex_del_ba_tbl(priv, tid, del_ba->peer_mac_addr,
121 				   TYPE_DELBA_SENT,
122 				   INITIATOR_BIT(del_ba_param_set));
123 
124 		tx_ba_tbl = mwifiex_get_ba_status(priv, BA_SETUP_INPROGRESS);
125 		if (tx_ba_tbl)
126 			mwifiex_send_addba(priv, tx_ba_tbl->tid,
127 					   tx_ba_tbl->ra);
128 	} else { /*
129 		  * In case of failure, recreate the deleted stream in case
130 		  * we initiated the ADDBA
131 		  */
132 		if (!INITIATOR_BIT(del_ba_param_set))
133 			return 0;
134 
135 		mwifiex_create_ba_tbl(priv, del_ba->peer_mac_addr, tid,
136 				      BA_SETUP_INPROGRESS);
137 
138 		tx_ba_tbl = mwifiex_get_ba_status(priv, BA_SETUP_INPROGRESS);
139 
140 		if (tx_ba_tbl)
141 			mwifiex_del_ba_tbl(priv, tx_ba_tbl->tid, tx_ba_tbl->ra,
142 					   TYPE_DELBA_SENT, true);
143 	}
144 
145 	return 0;
146 }
147 
148 /*
149  * This function handles the command response of add a block
150  * ack request.
151  *
152  * Handling includes changing the header fields to CPU formats, checking
153  * the response success status and taking actions accordingly (delete the
154  * BA stream table in case of failure).
155  */
156 int mwifiex_ret_11n_addba_req(struct mwifiex_private *priv,
157 			      struct host_cmd_ds_command *resp)
158 {
159 	int tid, tid_down;
160 	struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &resp->params.add_ba_rsp;
161 	struct mwifiex_tx_ba_stream_tbl *tx_ba_tbl;
162 	struct mwifiex_ra_list_tbl *ra_list;
163 	u16 block_ack_param_set = le16_to_cpu(add_ba_rsp->block_ack_param_set);
164 
165 	add_ba_rsp->ssn = cpu_to_le16((le16_to_cpu(add_ba_rsp->ssn))
166 			& SSN_MASK);
167 
168 	tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
169 	       >> BLOCKACKPARAM_TID_POS;
170 
171 	tid_down = mwifiex_wmm_downgrade_tid(priv, tid);
172 	ra_list = mwifiex_wmm_get_ralist_node(priv, tid_down, add_ba_rsp->
173 		peer_mac_addr);
174 	if (le16_to_cpu(add_ba_rsp->status_code) != BA_RESULT_SUCCESS) {
175 		if (ra_list) {
176 			ra_list->ba_status = BA_SETUP_NONE;
177 			ra_list->amsdu_in_ampdu = false;
178 		}
179 		mwifiex_del_ba_tbl(priv, tid, add_ba_rsp->peer_mac_addr,
180 				   TYPE_DELBA_SENT, true);
181 		if (add_ba_rsp->add_rsp_result != BA_RESULT_TIMEOUT)
182 			priv->aggr_prio_tbl[tid].ampdu_ap =
183 				BA_STREAM_NOT_ALLOWED;
184 		return 0;
185 	}
186 
187 	tx_ba_tbl = mwifiex_get_ba_tbl(priv, tid, add_ba_rsp->peer_mac_addr);
188 	if (tx_ba_tbl) {
189 		mwifiex_dbg(priv->adapter, EVENT, "info: BA stream complete\n");
190 		tx_ba_tbl->ba_status = BA_SETUP_COMPLETE;
191 		if ((block_ack_param_set & BLOCKACKPARAM_AMSDU_SUPP_MASK) &&
192 		    priv->add_ba_param.tx_amsdu &&
193 		    (priv->aggr_prio_tbl[tid].amsdu != BA_STREAM_NOT_ALLOWED))
194 			tx_ba_tbl->amsdu = true;
195 		else
196 			tx_ba_tbl->amsdu = false;
197 		if (ra_list) {
198 			ra_list->amsdu_in_ampdu = tx_ba_tbl->amsdu;
199 			ra_list->ba_status = BA_SETUP_COMPLETE;
200 		}
201 	} else {
202 		mwifiex_dbg(priv->adapter, ERROR, "BA stream not created\n");
203 	}
204 
205 	return 0;
206 }
207 
208 /*
209  * This function prepares command of reconfigure Tx buffer.
210  *
211  * Preparation includes -
212  *      - Setting command ID, action and proper size
213  *      - Setting Tx buffer size (for SET only)
214  *      - Ensuring correct endian-ness
215  */
216 int mwifiex_cmd_recfg_tx_buf(struct mwifiex_private *priv,
217 			     struct host_cmd_ds_command *cmd, int cmd_action,
218 			     u16 *buf_size)
219 {
220 	struct host_cmd_ds_txbuf_cfg *tx_buf = &cmd->params.tx_buf;
221 	u16 action = (u16) cmd_action;
222 
223 	cmd->command = cpu_to_le16(HostCmd_CMD_RECONFIGURE_TX_BUFF);
224 	cmd->size =
225 		cpu_to_le16(sizeof(struct host_cmd_ds_txbuf_cfg) + S_DS_GEN);
226 	tx_buf->action = cpu_to_le16(action);
227 	switch (action) {
228 	case HostCmd_ACT_GEN_SET:
229 		mwifiex_dbg(priv->adapter, CMD,
230 			    "cmd: set tx_buf=%d\n", *buf_size);
231 		tx_buf->buff_size = cpu_to_le16(*buf_size);
232 		break;
233 	case HostCmd_ACT_GEN_GET:
234 	default:
235 		tx_buf->buff_size = 0;
236 		break;
237 	}
238 	return 0;
239 }
240 
241 /*
242  * This function prepares command of AMSDU aggregation control.
243  *
244  * Preparation includes -
245  *      - Setting command ID, action and proper size
246  *      - Setting AMSDU control parameters (for SET only)
247  *      - Ensuring correct endian-ness
248  */
249 int mwifiex_cmd_amsdu_aggr_ctrl(struct host_cmd_ds_command *cmd,
250 				int cmd_action,
251 				struct mwifiex_ds_11n_amsdu_aggr_ctrl *aa_ctrl)
252 {
253 	struct host_cmd_ds_amsdu_aggr_ctrl *amsdu_ctrl =
254 		&cmd->params.amsdu_aggr_ctrl;
255 	u16 action = (u16) cmd_action;
256 
257 	cmd->command = cpu_to_le16(HostCmd_CMD_AMSDU_AGGR_CTRL);
258 	cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_amsdu_aggr_ctrl)
259 				+ S_DS_GEN);
260 	amsdu_ctrl->action = cpu_to_le16(action);
261 	switch (action) {
262 	case HostCmd_ACT_GEN_SET:
263 		amsdu_ctrl->enable = cpu_to_le16(aa_ctrl->enable);
264 		amsdu_ctrl->curr_buf_size = 0;
265 		break;
266 	case HostCmd_ACT_GEN_GET:
267 	default:
268 		amsdu_ctrl->curr_buf_size = 0;
269 		break;
270 	}
271 	return 0;
272 }
273 
274 /*
275  * This function prepares 11n configuration command.
276  *
277  * Preparation includes -
278  *      - Setting command ID, action and proper size
279  *      - Setting HT Tx capability and HT Tx information fields
280  *      - Ensuring correct endian-ness
281  */
282 int mwifiex_cmd_11n_cfg(struct mwifiex_private *priv,
283 			struct host_cmd_ds_command *cmd, u16 cmd_action,
284 			struct mwifiex_ds_11n_tx_cfg *txcfg)
285 {
286 	struct host_cmd_ds_11n_cfg *htcfg = &cmd->params.htcfg;
287 
288 	cmd->command = cpu_to_le16(HostCmd_CMD_11N_CFG);
289 	cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_11n_cfg) + S_DS_GEN);
290 	htcfg->action = cpu_to_le16(cmd_action);
291 	htcfg->ht_tx_cap = cpu_to_le16(txcfg->tx_htcap);
292 	htcfg->ht_tx_info = cpu_to_le16(txcfg->tx_htinfo);
293 
294 	if (priv->adapter->is_hw_11ac_capable)
295 		htcfg->misc_config = cpu_to_le16(txcfg->misc_config);
296 
297 	return 0;
298 }
299 
300 /*
301  * This function appends an 11n TLV to a buffer.
302  *
303  * Buffer allocation is responsibility of the calling
304  * function. No size validation is made here.
305  *
306  * The function fills up the following sections, if applicable -
307  *      - HT capability IE
308  *      - HT information IE (with channel list)
309  *      - 20/40 BSS Coexistence IE
310  *      - HT Extended Capabilities IE
311  */
312 int
313 mwifiex_cmd_append_11n_tlv(struct mwifiex_private *priv,
314 			   struct mwifiex_bssdescriptor *bss_desc,
315 			   u8 **buffer)
316 {
317 	struct mwifiex_ie_types_htcap *ht_cap;
318 	struct mwifiex_ie_types_htinfo *ht_info;
319 	struct mwifiex_ie_types_chan_list_param_set *chan_list;
320 	struct mwifiex_ie_types_2040bssco *bss_co_2040;
321 	struct mwifiex_ie_types_extcap *ext_cap;
322 	int ret_len = 0;
323 	struct ieee80211_supported_band *sband;
324 	struct ieee_types_header *hdr;
325 	u8 radio_type;
326 
327 	if (!buffer || !*buffer)
328 		return ret_len;
329 
330 	radio_type = mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
331 	sband = priv->wdev.wiphy->bands[radio_type];
332 
333 	if (bss_desc->bcn_ht_cap) {
334 		ht_cap = (struct mwifiex_ie_types_htcap *) *buffer;
335 		memset(ht_cap, 0, sizeof(struct mwifiex_ie_types_htcap));
336 		ht_cap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
337 		ht_cap->header.len =
338 				cpu_to_le16(sizeof(struct ieee80211_ht_cap));
339 		memcpy((u8 *) ht_cap + sizeof(struct mwifiex_ie_types_header),
340 		       (u8 *)bss_desc->bcn_ht_cap,
341 		       le16_to_cpu(ht_cap->header.len));
342 
343 		mwifiex_fill_cap_info(priv, radio_type, &ht_cap->ht_cap);
344 		/* Update HT40 capability from current channel information */
345 		if (bss_desc->bcn_ht_oper) {
346 			u8 ht_param = bss_desc->bcn_ht_oper->ht_param;
347 			u8 radio =
348 			mwifiex_band_to_radio_type(bss_desc->bss_band);
349 			int freq =
350 			ieee80211_channel_to_frequency(bss_desc->channel,
351 						       radio);
352 			struct ieee80211_channel *chan =
353 			ieee80211_get_channel(priv->adapter->wiphy, freq);
354 
355 			switch (ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
356 			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
357 				if (chan->flags & IEEE80211_CHAN_NO_HT40PLUS) {
358 					ht_cap->ht_cap.cap_info &=
359 					cpu_to_le16
360 					(~IEEE80211_HT_CAP_SUP_WIDTH_20_40);
361 					ht_cap->ht_cap.cap_info &=
362 					cpu_to_le16(~IEEE80211_HT_CAP_SGI_40);
363 				}
364 				break;
365 			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
366 				if (chan->flags & IEEE80211_CHAN_NO_HT40MINUS) {
367 					ht_cap->ht_cap.cap_info &=
368 					cpu_to_le16
369 					(~IEEE80211_HT_CAP_SUP_WIDTH_20_40);
370 					ht_cap->ht_cap.cap_info &=
371 					cpu_to_le16(~IEEE80211_HT_CAP_SGI_40);
372 				}
373 				break;
374 			}
375 		}
376 
377 		*buffer += sizeof(struct mwifiex_ie_types_htcap);
378 		ret_len += sizeof(struct mwifiex_ie_types_htcap);
379 	}
380 
381 	if (bss_desc->bcn_ht_oper) {
382 		if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
383 			ht_info = (struct mwifiex_ie_types_htinfo *) *buffer;
384 			memset(ht_info, 0,
385 			       sizeof(struct mwifiex_ie_types_htinfo));
386 			ht_info->header.type =
387 					cpu_to_le16(WLAN_EID_HT_OPERATION);
388 			ht_info->header.len =
389 				cpu_to_le16(
390 					sizeof(struct ieee80211_ht_operation));
391 
392 			memcpy((u8 *) ht_info +
393 			       sizeof(struct mwifiex_ie_types_header),
394 			       (u8 *)bss_desc->bcn_ht_oper,
395 			       le16_to_cpu(ht_info->header.len));
396 
397 			if (!(sband->ht_cap.cap &
398 					IEEE80211_HT_CAP_SUP_WIDTH_20_40))
399 				ht_info->ht_oper.ht_param &=
400 					~(IEEE80211_HT_PARAM_CHAN_WIDTH_ANY |
401 					IEEE80211_HT_PARAM_CHA_SEC_OFFSET);
402 
403 			*buffer += sizeof(struct mwifiex_ie_types_htinfo);
404 			ret_len += sizeof(struct mwifiex_ie_types_htinfo);
405 		}
406 
407 		chan_list =
408 			(struct mwifiex_ie_types_chan_list_param_set *) *buffer;
409 		memset(chan_list, 0,
410 		       sizeof(struct mwifiex_ie_types_chan_list_param_set));
411 		chan_list->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
412 		chan_list->header.len = cpu_to_le16(
413 			sizeof(struct mwifiex_ie_types_chan_list_param_set) -
414 			sizeof(struct mwifiex_ie_types_header));
415 		chan_list->chan_scan_param[0].chan_number =
416 			bss_desc->bcn_ht_oper->primary_chan;
417 		chan_list->chan_scan_param[0].radio_type =
418 			mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
419 
420 		if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
421 		    bss_desc->bcn_ht_oper->ht_param &
422 		    IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)
423 			SET_SECONDARYCHAN(chan_list->chan_scan_param[0].
424 					  radio_type,
425 					  (bss_desc->bcn_ht_oper->ht_param &
426 					  IEEE80211_HT_PARAM_CHA_SEC_OFFSET));
427 
428 		*buffer += sizeof(struct mwifiex_ie_types_chan_list_param_set);
429 		ret_len += sizeof(struct mwifiex_ie_types_chan_list_param_set);
430 	}
431 
432 	if (bss_desc->bcn_bss_co_2040) {
433 		bss_co_2040 = (struct mwifiex_ie_types_2040bssco *) *buffer;
434 		memset(bss_co_2040, 0,
435 		       sizeof(struct mwifiex_ie_types_2040bssco));
436 		bss_co_2040->header.type = cpu_to_le16(WLAN_EID_BSS_COEX_2040);
437 		bss_co_2040->header.len =
438 		       cpu_to_le16(sizeof(bss_co_2040->bss_co_2040));
439 
440 		memcpy((u8 *) bss_co_2040 +
441 		       sizeof(struct mwifiex_ie_types_header),
442 		       bss_desc->bcn_bss_co_2040 +
443 		       sizeof(struct ieee_types_header),
444 		       le16_to_cpu(bss_co_2040->header.len));
445 
446 		*buffer += sizeof(struct mwifiex_ie_types_2040bssco);
447 		ret_len += sizeof(struct mwifiex_ie_types_2040bssco);
448 	}
449 
450 	if (bss_desc->bcn_ext_cap) {
451 		hdr = (void *)bss_desc->bcn_ext_cap;
452 		ext_cap = (struct mwifiex_ie_types_extcap *) *buffer;
453 		memset(ext_cap, 0, sizeof(struct mwifiex_ie_types_extcap));
454 		ext_cap->header.type = cpu_to_le16(WLAN_EID_EXT_CAPABILITY);
455 		ext_cap->header.len = cpu_to_le16(hdr->len);
456 
457 		memcpy((u8 *)ext_cap->ext_capab,
458 		       bss_desc->bcn_ext_cap + sizeof(struct ieee_types_header),
459 		       le16_to_cpu(ext_cap->header.len));
460 
461 		if (hdr->len > 3 &&
462 		    ext_cap->ext_capab[3] & WLAN_EXT_CAPA4_INTERWORKING_ENABLED)
463 			priv->hs2_enabled = true;
464 		else
465 			priv->hs2_enabled = false;
466 
467 		*buffer += sizeof(struct mwifiex_ie_types_extcap) + hdr->len;
468 		ret_len += sizeof(struct mwifiex_ie_types_extcap) + hdr->len;
469 	}
470 
471 	return ret_len;
472 }
473 
474 /*
475  * This function checks if the given pointer is valid entry of
476  * Tx BA Stream table.
477  */
478 static int mwifiex_is_tx_ba_stream_ptr_valid(struct mwifiex_private *priv,
479 				struct mwifiex_tx_ba_stream_tbl *tx_tbl_ptr)
480 {
481 	struct mwifiex_tx_ba_stream_tbl *tx_ba_tsr_tbl;
482 
483 	list_for_each_entry(tx_ba_tsr_tbl, &priv->tx_ba_stream_tbl_ptr, list) {
484 		if (tx_ba_tsr_tbl == tx_tbl_ptr)
485 			return true;
486 	}
487 
488 	return false;
489 }
490 
491 /*
492  * This function deletes the given entry in Tx BA Stream table.
493  *
494  * The function also performs a validity check on the supplied
495  * pointer before trying to delete.
496  */
497 void mwifiex_11n_delete_tx_ba_stream_tbl_entry(struct mwifiex_private *priv,
498 				struct mwifiex_tx_ba_stream_tbl *tx_ba_tsr_tbl)
499 {
500 	if (!tx_ba_tsr_tbl &&
501 	    mwifiex_is_tx_ba_stream_ptr_valid(priv, tx_ba_tsr_tbl))
502 		return;
503 
504 	mwifiex_dbg(priv->adapter, INFO,
505 		    "info: tx_ba_tsr_tbl %p\n", tx_ba_tsr_tbl);
506 
507 	list_del(&tx_ba_tsr_tbl->list);
508 
509 	kfree(tx_ba_tsr_tbl);
510 }
511 
512 /*
513  * This function deletes all the entries in Tx BA Stream table.
514  */
515 void mwifiex_11n_delete_all_tx_ba_stream_tbl(struct mwifiex_private *priv)
516 {
517 	int i;
518 	struct mwifiex_tx_ba_stream_tbl *del_tbl_ptr, *tmp_node;
519 	unsigned long flags;
520 
521 	spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
522 	list_for_each_entry_safe(del_tbl_ptr, tmp_node,
523 				 &priv->tx_ba_stream_tbl_ptr, list)
524 		mwifiex_11n_delete_tx_ba_stream_tbl_entry(priv, del_tbl_ptr);
525 	spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
526 
527 	INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
528 
529 	for (i = 0; i < MAX_NUM_TID; ++i)
530 		priv->aggr_prio_tbl[i].ampdu_ap =
531 			priv->aggr_prio_tbl[i].ampdu_user;
532 }
533 
534 /*
535  * This function returns the pointer to an entry in BA Stream
536  * table which matches the given RA/TID pair.
537  */
538 struct mwifiex_tx_ba_stream_tbl *
539 mwifiex_get_ba_tbl(struct mwifiex_private *priv, int tid, u8 *ra)
540 {
541 	struct mwifiex_tx_ba_stream_tbl *tx_ba_tsr_tbl;
542 	unsigned long flags;
543 
544 	spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
545 	list_for_each_entry(tx_ba_tsr_tbl, &priv->tx_ba_stream_tbl_ptr, list) {
546 		if (ether_addr_equal_unaligned(tx_ba_tsr_tbl->ra, ra) &&
547 		    tx_ba_tsr_tbl->tid == tid) {
548 			spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock,
549 					       flags);
550 			return tx_ba_tsr_tbl;
551 		}
552 	}
553 	spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
554 	return NULL;
555 }
556 
557 /*
558  * This function creates an entry in Tx BA stream table for the
559  * given RA/TID pair.
560  */
561 void mwifiex_create_ba_tbl(struct mwifiex_private *priv, u8 *ra, int tid,
562 			   enum mwifiex_ba_status ba_status)
563 {
564 	struct mwifiex_tx_ba_stream_tbl *new_node;
565 	struct mwifiex_ra_list_tbl *ra_list;
566 	unsigned long flags;
567 	int tid_down;
568 
569 	if (!mwifiex_get_ba_tbl(priv, tid, ra)) {
570 		new_node = kzalloc(sizeof(struct mwifiex_tx_ba_stream_tbl),
571 				   GFP_ATOMIC);
572 		if (!new_node)
573 			return;
574 
575 		tid_down = mwifiex_wmm_downgrade_tid(priv, tid);
576 		ra_list = mwifiex_wmm_get_ralist_node(priv, tid_down, ra);
577 		if (ra_list) {
578 			ra_list->ba_status = ba_status;
579 			ra_list->amsdu_in_ampdu = false;
580 		}
581 		INIT_LIST_HEAD(&new_node->list);
582 
583 		new_node->tid = tid;
584 		new_node->ba_status = ba_status;
585 		memcpy(new_node->ra, ra, ETH_ALEN);
586 
587 		spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
588 		list_add_tail(&new_node->list, &priv->tx_ba_stream_tbl_ptr);
589 		spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
590 	}
591 }
592 
593 /*
594  * This function sends an add BA request to the given TID/RA pair.
595  */
596 int mwifiex_send_addba(struct mwifiex_private *priv, int tid, u8 *peer_mac)
597 {
598 	struct host_cmd_ds_11n_addba_req add_ba_req;
599 	u32 tx_win_size = priv->add_ba_param.tx_win_size;
600 	static u8 dialog_tok;
601 	int ret;
602 	unsigned long flags;
603 	u16 block_ack_param_set;
604 
605 	mwifiex_dbg(priv->adapter, CMD, "cmd: %s: tid %d\n", __func__, tid);
606 
607 	memset(&add_ba_req, 0, sizeof(add_ba_req));
608 
609 	if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
610 	    ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
611 	    priv->adapter->is_hw_11ac_capable &&
612 	    memcmp(priv->cfg_bssid, peer_mac, ETH_ALEN)) {
613 		struct mwifiex_sta_node *sta_ptr;
614 
615 		spin_lock_irqsave(&priv->sta_list_spinlock, flags);
616 		sta_ptr = mwifiex_get_sta_entry(priv, peer_mac);
617 		if (!sta_ptr) {
618 			spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
619 			mwifiex_dbg(priv->adapter, ERROR,
620 				    "BA setup with unknown TDLS peer %pM!\n",
621 				    peer_mac);
622 			return -1;
623 		}
624 		if (sta_ptr->is_11ac_enabled)
625 			tx_win_size = MWIFIEX_11AC_STA_AMPDU_DEF_TXWINSIZE;
626 		spin_unlock_irqrestore(&priv->sta_list_spinlock, flags);
627 	}
628 
629 	block_ack_param_set = (u16)((tid << BLOCKACKPARAM_TID_POS) |
630 				    tx_win_size << BLOCKACKPARAM_WINSIZE_POS |
631 				    IMMEDIATE_BLOCK_ACK);
632 
633 	/* enable AMSDU inside AMPDU */
634 	if (priv->add_ba_param.tx_amsdu &&
635 	    (priv->aggr_prio_tbl[tid].amsdu != BA_STREAM_NOT_ALLOWED))
636 		block_ack_param_set |= BLOCKACKPARAM_AMSDU_SUPP_MASK;
637 
638 	add_ba_req.block_ack_param_set = cpu_to_le16(block_ack_param_set);
639 	add_ba_req.block_ack_tmo = cpu_to_le16((u16)priv->add_ba_param.timeout);
640 
641 	++dialog_tok;
642 
643 	if (dialog_tok == 0)
644 		dialog_tok = 1;
645 
646 	add_ba_req.dialog_token = dialog_tok;
647 	memcpy(&add_ba_req.peer_mac_addr, peer_mac, ETH_ALEN);
648 
649 	/* We don't wait for the response of this command */
650 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_11N_ADDBA_REQ,
651 			       0, 0, &add_ba_req, false);
652 
653 	return ret;
654 }
655 
656 /*
657  * This function sends a delete BA request to the given TID/RA pair.
658  */
659 int mwifiex_send_delba(struct mwifiex_private *priv, int tid, u8 *peer_mac,
660 		       int initiator)
661 {
662 	struct host_cmd_ds_11n_delba delba;
663 	int ret;
664 	uint16_t del_ba_param_set;
665 
666 	memset(&delba, 0, sizeof(delba));
667 	delba.del_ba_param_set = cpu_to_le16(tid << DELBA_TID_POS);
668 
669 	del_ba_param_set = le16_to_cpu(delba.del_ba_param_set);
670 	if (initiator)
671 		del_ba_param_set |= IEEE80211_DELBA_PARAM_INITIATOR_MASK;
672 	else
673 		del_ba_param_set &= ~IEEE80211_DELBA_PARAM_INITIATOR_MASK;
674 
675 	memcpy(&delba.peer_mac_addr, peer_mac, ETH_ALEN);
676 
677 	/* We don't wait for the response of this command */
678 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_11N_DELBA,
679 			       HostCmd_ACT_GEN_SET, 0, &delba, false);
680 
681 	return ret;
682 }
683 
684 /*
685  * This function sends delba to specific tid
686  */
687 void mwifiex_11n_delba(struct mwifiex_private *priv, int tid)
688 {
689 	struct mwifiex_rx_reorder_tbl *rx_reor_tbl_ptr;
690 	unsigned long flags;
691 
692 	spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
693 	list_for_each_entry(rx_reor_tbl_ptr, &priv->rx_reorder_tbl_ptr, list) {
694 		if (rx_reor_tbl_ptr->tid == tid) {
695 			dev_dbg(priv->adapter->dev,
696 				"Send delba to tid=%d, %pM\n",
697 				tid, rx_reor_tbl_ptr->ta);
698 			mwifiex_send_delba(priv, tid, rx_reor_tbl_ptr->ta, 0);
699 			goto exit;
700 		}
701 	}
702 exit:
703 	spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
704 }
705 
706 /*
707  * This function handles the command response of a delete BA request.
708  */
709 void mwifiex_11n_delete_ba_stream(struct mwifiex_private *priv, u8 *del_ba)
710 {
711 	struct host_cmd_ds_11n_delba *cmd_del_ba =
712 		(struct host_cmd_ds_11n_delba *) del_ba;
713 	uint16_t del_ba_param_set = le16_to_cpu(cmd_del_ba->del_ba_param_set);
714 	int tid;
715 
716 	tid = del_ba_param_set >> DELBA_TID_POS;
717 
718 	mwifiex_del_ba_tbl(priv, tid, cmd_del_ba->peer_mac_addr,
719 			   TYPE_DELBA_RECEIVE, INITIATOR_BIT(del_ba_param_set));
720 }
721 
722 /*
723  * This function retrieves the Rx reordering table.
724  */
725 int mwifiex_get_rx_reorder_tbl(struct mwifiex_private *priv,
726 			       struct mwifiex_ds_rx_reorder_tbl *buf)
727 {
728 	int i;
729 	struct mwifiex_ds_rx_reorder_tbl *rx_reo_tbl = buf;
730 	struct mwifiex_rx_reorder_tbl *rx_reorder_tbl_ptr;
731 	int count = 0;
732 	unsigned long flags;
733 
734 	spin_lock_irqsave(&priv->rx_reorder_tbl_lock, flags);
735 	list_for_each_entry(rx_reorder_tbl_ptr, &priv->rx_reorder_tbl_ptr,
736 			    list) {
737 		rx_reo_tbl->tid = (u16) rx_reorder_tbl_ptr->tid;
738 		memcpy(rx_reo_tbl->ta, rx_reorder_tbl_ptr->ta, ETH_ALEN);
739 		rx_reo_tbl->start_win = rx_reorder_tbl_ptr->start_win;
740 		rx_reo_tbl->win_size = rx_reorder_tbl_ptr->win_size;
741 		for (i = 0; i < rx_reorder_tbl_ptr->win_size; ++i) {
742 			if (rx_reorder_tbl_ptr->rx_reorder_ptr[i])
743 				rx_reo_tbl->buffer[i] = true;
744 			else
745 				rx_reo_tbl->buffer[i] = false;
746 		}
747 		rx_reo_tbl++;
748 		count++;
749 
750 		if (count >= MWIFIEX_MAX_RX_BASTREAM_SUPPORTED)
751 			break;
752 	}
753 	spin_unlock_irqrestore(&priv->rx_reorder_tbl_lock, flags);
754 
755 	return count;
756 }
757 
758 /*
759  * This function retrieves the Tx BA stream table.
760  */
761 int mwifiex_get_tx_ba_stream_tbl(struct mwifiex_private *priv,
762 				 struct mwifiex_ds_tx_ba_stream_tbl *buf)
763 {
764 	struct mwifiex_tx_ba_stream_tbl *tx_ba_tsr_tbl;
765 	struct mwifiex_ds_tx_ba_stream_tbl *rx_reo_tbl = buf;
766 	int count = 0;
767 	unsigned long flags;
768 
769 	spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
770 	list_for_each_entry(tx_ba_tsr_tbl, &priv->tx_ba_stream_tbl_ptr, list) {
771 		rx_reo_tbl->tid = (u16) tx_ba_tsr_tbl->tid;
772 		mwifiex_dbg(priv->adapter, DATA, "data: %s tid=%d\n",
773 			    __func__, rx_reo_tbl->tid);
774 		memcpy(rx_reo_tbl->ra, tx_ba_tsr_tbl->ra, ETH_ALEN);
775 		rx_reo_tbl->amsdu = tx_ba_tsr_tbl->amsdu;
776 		rx_reo_tbl++;
777 		count++;
778 		if (count >= MWIFIEX_MAX_TX_BASTREAM_SUPPORTED)
779 			break;
780 	}
781 	spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
782 
783 	return count;
784 }
785 
786 /*
787  * This function retrieves the entry for specific tx BA stream table by RA and
788  * deletes it.
789  */
790 void mwifiex_del_tx_ba_stream_tbl_by_ra(struct mwifiex_private *priv, u8 *ra)
791 {
792 	struct mwifiex_tx_ba_stream_tbl *tbl, *tmp;
793 	unsigned long flags;
794 
795 	if (!ra)
796 		return;
797 
798 	spin_lock_irqsave(&priv->tx_ba_stream_tbl_lock, flags);
799 	list_for_each_entry_safe(tbl, tmp, &priv->tx_ba_stream_tbl_ptr, list)
800 		if (!memcmp(tbl->ra, ra, ETH_ALEN))
801 			mwifiex_11n_delete_tx_ba_stream_tbl_entry(priv, tbl);
802 	spin_unlock_irqrestore(&priv->tx_ba_stream_tbl_lock, flags);
803 
804 	return;
805 }
806 
807 /* This function initializes the BlockACK setup information for given
808  * mwifiex_private structure.
809  */
810 void mwifiex_set_ba_params(struct mwifiex_private *priv)
811 {
812 	priv->add_ba_param.timeout = MWIFIEX_DEFAULT_BLOCK_ACK_TIMEOUT;
813 
814 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
815 		priv->add_ba_param.tx_win_size =
816 						MWIFIEX_UAP_AMPDU_DEF_TXWINSIZE;
817 		priv->add_ba_param.rx_win_size =
818 						MWIFIEX_UAP_AMPDU_DEF_RXWINSIZE;
819 	} else {
820 		priv->add_ba_param.tx_win_size =
821 						MWIFIEX_STA_AMPDU_DEF_TXWINSIZE;
822 		priv->add_ba_param.rx_win_size =
823 						MWIFIEX_STA_AMPDU_DEF_RXWINSIZE;
824 	}
825 
826 	priv->add_ba_param.tx_amsdu = true;
827 	priv->add_ba_param.rx_amsdu = true;
828 
829 	return;
830 }
831 
832 u8 mwifiex_get_sec_chan_offset(int chan)
833 {
834 	u8 sec_offset;
835 
836 	switch (chan) {
837 	case 36:
838 	case 44:
839 	case 52:
840 	case 60:
841 	case 100:
842 	case 108:
843 	case 116:
844 	case 124:
845 	case 132:
846 	case 140:
847 	case 149:
848 	case 157:
849 		sec_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
850 		break;
851 	case 40:
852 	case 48:
853 	case 56:
854 	case 64:
855 	case 104:
856 	case 112:
857 	case 120:
858 	case 128:
859 	case 136:
860 	case 144:
861 	case 153:
862 	case 161:
863 		sec_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
864 		break;
865 	case 165:
866 	default:
867 		sec_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
868 		break;
869 	}
870 
871 	return sec_offset;
872 }
873 
874 /* This function will send DELBA to entries in the priv's
875  * Tx BA stream table
876  */
877 static void
878 mwifiex_send_delba_txbastream_tbl(struct mwifiex_private *priv, u8 tid)
879 {
880 	struct mwifiex_adapter *adapter = priv->adapter;
881 	struct mwifiex_tx_ba_stream_tbl *tx_ba_stream_tbl_ptr;
882 
883 	list_for_each_entry(tx_ba_stream_tbl_ptr,
884 			    &priv->tx_ba_stream_tbl_ptr, list) {
885 		if (tx_ba_stream_tbl_ptr->ba_status == BA_SETUP_COMPLETE) {
886 			if (tid == tx_ba_stream_tbl_ptr->tid) {
887 				dev_dbg(adapter->dev,
888 					"Tx:Send delba to tid=%d, %pM\n", tid,
889 					tx_ba_stream_tbl_ptr->ra);
890 				mwifiex_send_delba(priv,
891 						   tx_ba_stream_tbl_ptr->tid,
892 						   tx_ba_stream_tbl_ptr->ra, 1);
893 				return;
894 			}
895 		}
896 	}
897 }
898 
899 /* This function updates all the tx_win_size
900  */
901 void mwifiex_update_ampdu_txwinsize(struct mwifiex_adapter *adapter)
902 {
903 	u8 i;
904 	u32 tx_win_size;
905 	struct mwifiex_private *priv;
906 
907 	for (i = 0; i < adapter->priv_num; i++) {
908 		if (!adapter->priv[i])
909 			continue;
910 		priv = adapter->priv[i];
911 		tx_win_size = priv->add_ba_param.tx_win_size;
912 
913 		if (priv->bss_type == MWIFIEX_BSS_TYPE_STA)
914 			priv->add_ba_param.tx_win_size =
915 				MWIFIEX_STA_AMPDU_DEF_TXWINSIZE;
916 
917 		if (priv->bss_type == MWIFIEX_BSS_TYPE_P2P)
918 			priv->add_ba_param.tx_win_size =
919 				MWIFIEX_STA_AMPDU_DEF_TXWINSIZE;
920 
921 		if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP)
922 			priv->add_ba_param.tx_win_size =
923 				MWIFIEX_UAP_AMPDU_DEF_TXWINSIZE;
924 
925 		if (adapter->coex_win_size) {
926 			if (adapter->coex_tx_win_size)
927 				priv->add_ba_param.tx_win_size =
928 					adapter->coex_tx_win_size;
929 		}
930 
931 		if (tx_win_size != priv->add_ba_param.tx_win_size) {
932 			if (!priv->media_connected)
933 				continue;
934 			for (i = 0; i < MAX_NUM_TID; i++)
935 				mwifiex_send_delba_txbastream_tbl(priv, i);
936 		}
937 	}
938 }
939