tx.c revision ece561919326236c7fb791a5e883f0eb76af029e
1/**
2  * This file contains the handling of TX in wlan driver.
3  */
4#include <linux/netdevice.h>
5
6#include "hostcmd.h"
7#include "radiotap.h"
8#include "decl.h"
9#include "defs.h"
10#include "dev.h"
11#include "wext.h"
12
13/**
14 *  @brief This function converts Tx/Rx rates from IEEE80211_RADIOTAP_RATE
15 *  units (500 Kb/s) into Marvell WLAN format (see Table 8 in Section 3.2.1)
16 *
17 *  @param rate    Input rate
18 *  @return      Output Rate (0 if invalid)
19 */
20static u32 convert_radiotap_rate_to_mv(u8 rate)
21{
22	switch (rate) {
23	case 2:		/*   1 Mbps */
24		return 0 | (1 << 4);
25	case 4:		/*   2 Mbps */
26		return 1 | (1 << 4);
27	case 11:		/* 5.5 Mbps */
28		return 2 | (1 << 4);
29	case 22:		/*  11 Mbps */
30		return 3 | (1 << 4);
31	case 12:		/*   6 Mbps */
32		return 4 | (1 << 4);
33	case 18:		/*   9 Mbps */
34		return 5 | (1 << 4);
35	case 24:		/*  12 Mbps */
36		return 6 | (1 << 4);
37	case 36:		/*  18 Mbps */
38		return 7 | (1 << 4);
39	case 48:		/*  24 Mbps */
40		return 8 | (1 << 4);
41	case 72:		/*  36 Mbps */
42		return 9 | (1 << 4);
43	case 96:		/*  48 Mbps */
44		return 10 | (1 << 4);
45	case 108:		/*  54 Mbps */
46		return 11 | (1 << 4);
47	}
48	return 0;
49}
50
51/**
52 *  @brief This function processes a single packet and sends
53 *  to IF layer
54 *
55 *  @param priv    A pointer to wlan_private structure
56 *  @param skb     A pointer to skb which includes TX packet
57 *  @return 	   0 or -1
58 */
59static int SendSinglePacket(wlan_private * priv, struct sk_buff *skb)
60{
61	wlan_adapter *adapter = priv->adapter;
62	int ret = 0;
63	struct txpd localtxpd;
64	struct txpd *plocaltxpd = &localtxpd;
65	u8 *p802x_hdr;
66	struct tx_radiotap_hdr *pradiotap_hdr;
67	u32 new_rate;
68	u8 *ptr = priv->adapter->tmptxbuf;
69
70	lbs_deb_enter(LBS_DEB_TX);
71
72	if (priv->adapter->surpriseremoved)
73		return -1;
74
75	if (!skb->len || (skb->len > MRVDRV_ETH_TX_PACKET_BUFFER_SIZE)) {
76		lbs_deb_tx("tx err: skb length %d 0 or > %zd\n",
77		       skb->len, MRVDRV_ETH_TX_PACKET_BUFFER_SIZE);
78		ret = -1;
79		goto done;
80	}
81
82	memset(plocaltxpd, 0, sizeof(struct txpd));
83
84	plocaltxpd->tx_packet_length = cpu_to_le16(skb->len);
85
86	/* offset of actual data */
87	plocaltxpd->tx_packet_location = cpu_to_le32(sizeof(struct txpd));
88
89	/* TxCtrl set by user or default */
90	plocaltxpd->tx_control = cpu_to_le32(adapter->pkttxctrl);
91
92	p802x_hdr = skb->data;
93	if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
94
95		/* locate radiotap header */
96		pradiotap_hdr = (struct tx_radiotap_hdr *)skb->data;
97
98		/* set txpd fields from the radiotap header */
99		new_rate = convert_radiotap_rate_to_mv(pradiotap_hdr->rate);
100		if (new_rate != 0) {
101			/* use new tx_control[4:0] */
102			new_rate |= (adapter->pkttxctrl & ~0x1f);
103			plocaltxpd->tx_control = cpu_to_le32(new_rate);
104		}
105
106		/* skip the radiotap header */
107		p802x_hdr += sizeof(struct tx_radiotap_hdr);
108		plocaltxpd->tx_packet_length =
109			cpu_to_le16(le16_to_cpu(plocaltxpd->tx_packet_length)
110				    - sizeof(struct tx_radiotap_hdr));
111
112	}
113	/* copy destination address from 802.3 or 802.11 header */
114	if (priv->adapter->linkmode == WLAN_LINKMODE_802_11)
115		memcpy(plocaltxpd->tx_dest_addr_high, p802x_hdr + 4, ETH_ALEN);
116	else
117		memcpy(plocaltxpd->tx_dest_addr_high, p802x_hdr, ETH_ALEN);
118
119	lbs_deb_hex(LBS_DEB_TX, "txpd", (u8 *) plocaltxpd, sizeof(struct txpd));
120
121	if (IS_MESH_FRAME(skb)) {
122		plocaltxpd->tx_control |= cpu_to_le32(TxPD_MESH_FRAME);
123	}
124
125	memcpy(ptr, plocaltxpd, sizeof(struct txpd));
126
127	ptr += sizeof(struct txpd);
128
129	lbs_deb_hex(LBS_DEB_TX, "Tx Data", (u8 *) p802x_hdr, le16_to_cpu(plocaltxpd->tx_packet_length));
130	memcpy(ptr, p802x_hdr, le16_to_cpu(plocaltxpd->tx_packet_length));
131	ret = priv->hw_host_to_card(priv, MVMS_DAT,
132				    priv->adapter->tmptxbuf,
133				    le16_to_cpu(plocaltxpd->tx_packet_length) +
134				    sizeof(struct txpd));
135
136	if (ret) {
137		lbs_deb_tx("tx err: hw_host_to_card returned 0x%X\n", ret);
138		goto done;
139	}
140
141	lbs_deb_tx("SendSinglePacket succeeds\n");
142
143done:
144	if (!ret) {
145		priv->stats.tx_packets++;
146		priv->stats.tx_bytes += skb->len;
147	} else {
148		priv->stats.tx_dropped++;
149		priv->stats.tx_errors++;
150	}
151
152	if (!ret && priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
153		/* Keep the skb to echo it back once Tx feedback is
154		   received from FW */
155		skb_orphan(skb);
156		/* stop processing outgoing pkts */
157		netif_stop_queue(priv->dev);
158		if (priv->mesh_dev)
159			netif_stop_queue(priv->mesh_dev);
160		/* freeze any packets already in our queues */
161		priv->adapter->TxLockFlag = 1;
162	} else {
163		dev_kfree_skb_any(skb);
164		priv->adapter->currenttxskb = NULL;
165	}
166
167	lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
168	return ret;
169}
170
171
172void libertas_tx_runqueue(wlan_private *priv)
173{
174	wlan_adapter *adapter = priv->adapter;
175	int i;
176
177	spin_lock(&adapter->txqueue_lock);
178	for (i = 0; i < adapter->tx_queue_idx; i++) {
179		struct sk_buff *skb = adapter->tx_queue_ps[i];
180		spin_unlock(&adapter->txqueue_lock);
181		SendSinglePacket(priv, skb);
182		spin_lock(&adapter->txqueue_lock);
183	}
184	adapter->tx_queue_idx = 0;
185	spin_unlock(&adapter->txqueue_lock);
186}
187
188static void wlan_tx_queue(wlan_private *priv, struct sk_buff *skb)
189{
190	wlan_adapter *adapter = priv->adapter;
191
192	spin_lock(&adapter->txqueue_lock);
193
194	WARN_ON(priv->adapter->tx_queue_idx >= NR_TX_QUEUE);
195	adapter->tx_queue_ps[adapter->tx_queue_idx++] = skb;
196	if (adapter->tx_queue_idx == NR_TX_QUEUE) {
197		netif_stop_queue(priv->dev);
198		if (priv->mesh_dev)
199			netif_stop_queue(priv->mesh_dev);
200	} else {
201		netif_start_queue(priv->dev);
202		if (priv->mesh_dev)
203			netif_start_queue(priv->mesh_dev);
204	}
205
206	spin_unlock(&adapter->txqueue_lock);
207}
208
209/**
210 *  @brief This function checks the conditions and sends packet to IF
211 *  layer if everything is ok.
212 *
213 *  @param priv    A pointer to wlan_private structure
214 *  @return 	   n/a
215 */
216int libertas_process_tx(wlan_private * priv, struct sk_buff *skb)
217{
218	int ret = -1;
219
220	lbs_deb_enter(LBS_DEB_TX);
221	lbs_deb_hex(LBS_DEB_TX, "TX Data", skb->data, min_t(unsigned int, skb->len, 100));
222
223	if (priv->dnld_sent) {
224		lbs_pr_alert( "TX error: dnld_sent = %d, not sending\n",
225		       priv->dnld_sent);
226		goto done;
227	}
228
229	if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
230	    (priv->adapter->psstate == PS_STATE_PRE_SLEEP)) {
231		wlan_tx_queue(priv, skb);
232		return ret;
233	}
234
235	priv->adapter->currenttxskb = skb;
236
237	ret = SendSinglePacket(priv, skb);
238done:
239	lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
240	return ret;
241}
242
243/**
244 *  @brief This function sends to the host the last transmitted packet,
245 *  filling the radiotap headers with transmission information.
246 *
247 *  @param priv     A pointer to wlan_private structure
248 *  @param status   A 32 bit value containing transmission status.
249 *
250 *  @returns void
251 */
252void libertas_send_tx_feedback(wlan_private * priv)
253{
254	wlan_adapter *adapter = priv->adapter;
255	struct tx_radiotap_hdr *radiotap_hdr;
256	u32 status = adapter->eventcause;
257	int txfail;
258	int try_count;
259
260	if (adapter->radiomode != WLAN_RADIOMODE_RADIOTAP ||
261	    adapter->currenttxskb == NULL)
262		return;
263
264	radiotap_hdr = (struct tx_radiotap_hdr *)adapter->currenttxskb->data;
265
266	txfail = (status >> 24);
267
268#if 0
269	/* The version of roofnet that we've tested does not use this yet
270	 * But it may be used in the future.
271	 */
272	if (txfail)
273		radiotap_hdr->flags &= IEEE80211_RADIOTAP_F_TX_FAIL;
274#endif
275	try_count = (status >> 16) & 0xff;
276	radiotap_hdr->data_retries = (try_count) ?
277	    (1 + adapter->txretrycount - try_count) : 0;
278	libertas_upload_rx_packet(priv, adapter->currenttxskb);
279	adapter->currenttxskb = NULL;
280	priv->adapter->TxLockFlag = 0;
281	if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
282		netif_wake_queue(priv->dev);
283		if (priv->mesh_dev)
284			netif_wake_queue(priv->mesh_dev);
285	}
286}
287EXPORT_SYMBOL_GPL(libertas_send_tx_feedback);
288