1/*
2 * NXP Wireless LAN device driver: station TX data handling
3 *
4 * Copyright 2011-2020 NXP
5 *
6 * This software file (the "File") is distributed by NXP
7 * 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
27/*
28 * This function fills the TxPD for tx packets.
29 *
30 * The Tx buffer received by this function should already have the
31 * header space allocated for TxPD.
32 *
33 * This function inserts the TxPD in between interface header and actual
34 * data and adjusts the buffer pointers accordingly.
35 *
36 * The following TxPD fields are set by this function, as required -
37 *      - BSS number
38 *      - Tx packet length and offset
39 *      - Priority
40 *      - Packet delay
41 *      - Priority specific Tx control
42 *      - Flags
43 */
44void *mwifiex_process_sta_txpd(struct mwifiex_private *priv,
45				struct sk_buff *skb)
46{
47	struct mwifiex_adapter *adapter = priv->adapter;
48	struct txpd *local_tx_pd;
49	struct mwifiex_txinfo *tx_info = MWIFIEX_SKB_TXCB(skb);
50	unsigned int pad;
51	u16 pkt_type, pkt_offset;
52	int hroom = adapter->intf_hdr_len;
53
54	if (!skb->len) {
55		mwifiex_dbg(adapter, ERROR,
56			    "Tx: bad packet length: %d\n", skb->len);
57		tx_info->status_code = -1;
58		return skb->data;
59	}
60
61	BUG_ON(skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN);
62
63	pkt_type = mwifiex_is_skb_mgmt_frame(skb) ? PKT_TYPE_MGMT : 0;
64
65	pad = ((void *)skb->data - (sizeof(*local_tx_pd) + hroom)-
66			 NULL) & (MWIFIEX_DMA_ALIGN_SZ - 1);
67	skb_push(skb, sizeof(*local_tx_pd) + pad);
68
69	local_tx_pd = (struct txpd *) skb->data;
70	memset(local_tx_pd, 0, sizeof(struct txpd));
71	local_tx_pd->bss_num = priv->bss_num;
72	local_tx_pd->bss_type = priv->bss_type;
73	local_tx_pd->tx_pkt_length = cpu_to_le16((u16)(skb->len -
74						       (sizeof(struct txpd) +
75							pad)));
76
77	local_tx_pd->priority = (u8) skb->priority;
78	local_tx_pd->pkt_delay_2ms =
79				mwifiex_wmm_compute_drv_pkt_delay(priv, skb);
80
81	if (tx_info->flags & MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS ||
82	    tx_info->flags & MWIFIEX_BUF_FLAG_ACTION_TX_STATUS) {
83		local_tx_pd->tx_token_id = tx_info->ack_frame_id;
84		local_tx_pd->flags |= MWIFIEX_TXPD_FLAGS_REQ_TX_STATUS;
85	}
86
87	if (local_tx_pd->priority <
88	    ARRAY_SIZE(priv->wmm.user_pri_pkt_tx_ctrl))
89		/*
90		 * Set the priority specific tx_control field, setting of 0 will
91		 *   cause the default value to be used later in this function
92		 */
93		local_tx_pd->tx_control =
94			cpu_to_le32(priv->wmm.user_pri_pkt_tx_ctrl[local_tx_pd->
95								   priority]);
96
97	if (adapter->pps_uapsd_mode) {
98		if (mwifiex_check_last_packet_indication(priv)) {
99			adapter->tx_lock_flag = true;
100			local_tx_pd->flags =
101				MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET;
102		}
103	}
104
105	if (tx_info->flags & MWIFIEX_BUF_FLAG_TDLS_PKT)
106		local_tx_pd->flags |= MWIFIEX_TXPD_FLAGS_TDLS_PACKET;
107
108	/* Offset of actual data */
109	pkt_offset = sizeof(struct txpd) + pad;
110	if (pkt_type == PKT_TYPE_MGMT) {
111		/* Set the packet type and add header for management frame */
112		local_tx_pd->tx_pkt_type = cpu_to_le16(pkt_type);
113		pkt_offset += MWIFIEX_MGMT_FRAME_HEADER_SIZE;
114	}
115
116	local_tx_pd->tx_pkt_offset = cpu_to_le16(pkt_offset);
117
118	/* make space for adapter->intf_hdr_len */
119	skb_push(skb, hroom);
120
121	if (!local_tx_pd->tx_control)
122		/* TxCtrl set by user or default */
123		local_tx_pd->tx_control = cpu_to_le32(priv->pkt_tx_ctrl);
124
125	return skb->data;
126}
127
128/*
129 * This function tells firmware to send a NULL data packet.
130 *
131 * The function creates a NULL data packet with TxPD and sends to the
132 * firmware for transmission, with highest priority setting.
133 */
134int mwifiex_send_null_packet(struct mwifiex_private *priv, u8 flags)
135{
136	struct mwifiex_adapter *adapter = priv->adapter;
137	struct txpd *local_tx_pd;
138	struct mwifiex_tx_param tx_param;
139/* sizeof(struct txpd) + Interface specific header */
140#define NULL_PACKET_HDR 64
141	u32 data_len = NULL_PACKET_HDR;
142	struct sk_buff *skb;
143	int ret;
144	struct mwifiex_txinfo *tx_info = NULL;
145
146	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
147		return -1;
148
149	if (!priv->media_connected)
150		return -1;
151
152	if (adapter->data_sent)
153		return -1;
154
155	if (adapter->if_ops.is_port_ready &&
156	    !adapter->if_ops.is_port_ready(priv))
157		return -1;
158
159	skb = dev_alloc_skb(data_len);
160	if (!skb)
161		return -1;
162
163	tx_info = MWIFIEX_SKB_TXCB(skb);
164	memset(tx_info, 0, sizeof(*tx_info));
165	tx_info->bss_num = priv->bss_num;
166	tx_info->bss_type = priv->bss_type;
167	tx_info->pkt_len = data_len -
168			(sizeof(struct txpd) + adapter->intf_hdr_len);
169	skb_reserve(skb, sizeof(struct txpd) + adapter->intf_hdr_len);
170	skb_push(skb, sizeof(struct txpd));
171
172	local_tx_pd = (struct txpd *) skb->data;
173	local_tx_pd->tx_control = cpu_to_le32(priv->pkt_tx_ctrl);
174	local_tx_pd->flags = flags;
175	local_tx_pd->priority = WMM_HIGHEST_PRIORITY;
176	local_tx_pd->tx_pkt_offset = cpu_to_le16(sizeof(struct txpd));
177	local_tx_pd->bss_num = priv->bss_num;
178	local_tx_pd->bss_type = priv->bss_type;
179
180	skb_push(skb, adapter->intf_hdr_len);
181	if (adapter->iface_type == MWIFIEX_USB) {
182		ret = adapter->if_ops.host_to_card(adapter, priv->usb_port,
183						   skb, NULL);
184	} else {
185		tx_param.next_pkt_len = 0;
186		ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_DATA,
187						   skb, &tx_param);
188	}
189	switch (ret) {
190	case -EBUSY:
191		dev_kfree_skb_any(skb);
192		mwifiex_dbg(adapter, ERROR,
193			    "%s: host_to_card failed: ret=%d\n",
194			    __func__, ret);
195		adapter->dbg.num_tx_host_to_card_failure++;
196		break;
197	case -1:
198		dev_kfree_skb_any(skb);
199		mwifiex_dbg(adapter, ERROR,
200			    "%s: host_to_card failed: ret=%d\n",
201			    __func__, ret);
202		adapter->dbg.num_tx_host_to_card_failure++;
203		break;
204	case 0:
205		dev_kfree_skb_any(skb);
206		mwifiex_dbg(adapter, DATA,
207			    "data: %s: host_to_card succeeded\n",
208			    __func__);
209		adapter->tx_lock_flag = true;
210		break;
211	case -EINPROGRESS:
212		adapter->tx_lock_flag = true;
213		break;
214	default:
215		break;
216	}
217
218	return ret;
219}
220
221/*
222 * This function checks if we need to send last packet indication.
223 */
224u8
225mwifiex_check_last_packet_indication(struct mwifiex_private *priv)
226{
227	struct mwifiex_adapter *adapter = priv->adapter;
228	u8 ret = false;
229
230	if (!adapter->sleep_period.period)
231		return ret;
232	if (mwifiex_wmm_lists_empty(adapter))
233			ret = true;
234
235	if (ret && !adapter->cmd_sent && !adapter->curr_cmd &&
236	    !is_command_pending(adapter)) {
237		adapter->delay_null_pkt = false;
238		ret = true;
239	} else {
240		ret = false;
241		adapter->delay_null_pkt = true;
242	}
243	return ret;
244}
245