1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f988d640SKalle Valo /*
3f988d640SKalle Valo * This file contains the handling of TX in wlan driver.
4f988d640SKalle Valo */
5f988d640SKalle Valo #include <linux/hardirq.h>
6f988d640SKalle Valo #include <linux/netdevice.h>
7f988d640SKalle Valo #include <linux/etherdevice.h>
8f988d640SKalle Valo #include <linux/sched.h>
9f988d640SKalle Valo #include <linux/export.h>
10f988d640SKalle Valo #include <net/cfg80211.h>
11f988d640SKalle Valo
12f988d640SKalle Valo #include "host.h"
13f988d640SKalle Valo #include "radiotap.h"
14f988d640SKalle Valo #include "decl.h"
15f988d640SKalle Valo #include "defs.h"
16f988d640SKalle Valo #include "dev.h"
17f988d640SKalle Valo #include "mesh.h"
18f988d640SKalle Valo
19f988d640SKalle Valo /**
20f988d640SKalle Valo * convert_radiotap_rate_to_mv - converts Tx/Rx rates from IEEE80211_RADIOTAP_RATE
21f988d640SKalle Valo * units (500 Kb/s) into Marvell WLAN format (see Table 8 in Section 3.2.1)
22f988d640SKalle Valo *
23f988d640SKalle Valo * @rate: Input rate
24f988d640SKalle Valo * returns: Output Rate (0 if invalid)
25f988d640SKalle Valo */
convert_radiotap_rate_to_mv(u8 rate)26f988d640SKalle Valo static u32 convert_radiotap_rate_to_mv(u8 rate)
27f988d640SKalle Valo {
28f988d640SKalle Valo switch (rate) {
29f988d640SKalle Valo case 2: /* 1 Mbps */
30f988d640SKalle Valo return 0 | (1 << 4);
31f988d640SKalle Valo case 4: /* 2 Mbps */
32f988d640SKalle Valo return 1 | (1 << 4);
33f988d640SKalle Valo case 11: /* 5.5 Mbps */
34f988d640SKalle Valo return 2 | (1 << 4);
35f988d640SKalle Valo case 22: /* 11 Mbps */
36f988d640SKalle Valo return 3 | (1 << 4);
37f988d640SKalle Valo case 12: /* 6 Mbps */
38f988d640SKalle Valo return 4 | (1 << 4);
39f988d640SKalle Valo case 18: /* 9 Mbps */
40f988d640SKalle Valo return 5 | (1 << 4);
41f988d640SKalle Valo case 24: /* 12 Mbps */
42f988d640SKalle Valo return 6 | (1 << 4);
43f988d640SKalle Valo case 36: /* 18 Mbps */
44f988d640SKalle Valo return 7 | (1 << 4);
45f988d640SKalle Valo case 48: /* 24 Mbps */
46f988d640SKalle Valo return 8 | (1 << 4);
47f988d640SKalle Valo case 72: /* 36 Mbps */
48f988d640SKalle Valo return 9 | (1 << 4);
49f988d640SKalle Valo case 96: /* 48 Mbps */
50f988d640SKalle Valo return 10 | (1 << 4);
51f988d640SKalle Valo case 108: /* 54 Mbps */
52f988d640SKalle Valo return 11 | (1 << 4);
53f988d640SKalle Valo }
54f988d640SKalle Valo return 0;
55f988d640SKalle Valo }
56f988d640SKalle Valo
57f988d640SKalle Valo /**
58f988d640SKalle Valo * lbs_hard_start_xmit - checks the conditions and sends packet to IF
59f988d640SKalle Valo * layer if everything is ok
60f988d640SKalle Valo *
61f988d640SKalle Valo * @skb: A pointer to skb which includes TX packet
62f988d640SKalle Valo * @dev: A pointer to the &struct net_device
63f988d640SKalle Valo * returns: 0 or -1
64f988d640SKalle Valo */
lbs_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)65f988d640SKalle Valo netdev_tx_t lbs_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
66f988d640SKalle Valo {
67f988d640SKalle Valo unsigned long flags;
68f988d640SKalle Valo struct lbs_private *priv = dev->ml_priv;
69f988d640SKalle Valo struct txpd *txpd;
70f988d640SKalle Valo char *p802x_hdr;
71f988d640SKalle Valo uint16_t pkt_len;
72f988d640SKalle Valo netdev_tx_t ret = NETDEV_TX_OK;
73f988d640SKalle Valo
74f988d640SKalle Valo /* We need to protect against the queues being restarted before
75f988d640SKalle Valo we get round to stopping them */
76f988d640SKalle Valo spin_lock_irqsave(&priv->driver_lock, flags);
77f988d640SKalle Valo
78f988d640SKalle Valo if (priv->surpriseremoved)
79f988d640SKalle Valo goto free;
80f988d640SKalle Valo
81f988d640SKalle Valo if (!skb->len || (skb->len > MRVDRV_ETH_TX_PACKET_BUFFER_SIZE)) {
82f988d640SKalle Valo lbs_deb_tx("tx err: skb length %d 0 or > %zd\n",
83f988d640SKalle Valo skb->len, MRVDRV_ETH_TX_PACKET_BUFFER_SIZE);
84f988d640SKalle Valo /* We'll never manage to send this one; drop it and return 'OK' */
85f988d640SKalle Valo
86f988d640SKalle Valo dev->stats.tx_dropped++;
87f988d640SKalle Valo dev->stats.tx_errors++;
88f988d640SKalle Valo goto free;
89f988d640SKalle Valo }
90f988d640SKalle Valo
91f988d640SKalle Valo
92f988d640SKalle Valo netif_stop_queue(priv->dev);
93f988d640SKalle Valo if (priv->mesh_dev)
94f988d640SKalle Valo netif_stop_queue(priv->mesh_dev);
95f988d640SKalle Valo
96f988d640SKalle Valo if (priv->tx_pending_len) {
97f988d640SKalle Valo /* This can happen if packets come in on the mesh and eth
98f988d640SKalle Valo device simultaneously -- there's no mutual exclusion on
99f988d640SKalle Valo hard_start_xmit() calls between devices. */
100f988d640SKalle Valo lbs_deb_tx("Packet on %s while busy\n", dev->name);
101f988d640SKalle Valo ret = NETDEV_TX_BUSY;
102f988d640SKalle Valo goto unlock;
103f988d640SKalle Valo }
104f988d640SKalle Valo
105f988d640SKalle Valo priv->tx_pending_len = -1;
106f988d640SKalle Valo spin_unlock_irqrestore(&priv->driver_lock, flags);
107f988d640SKalle Valo
108f988d640SKalle Valo lbs_deb_hex(LBS_DEB_TX, "TX Data", skb->data, min_t(unsigned int, skb->len, 100));
109f988d640SKalle Valo
110f988d640SKalle Valo txpd = (void *)priv->tx_pending_buf;
111f988d640SKalle Valo memset(txpd, 0, sizeof(struct txpd));
112f988d640SKalle Valo
113f988d640SKalle Valo p802x_hdr = skb->data;
114f988d640SKalle Valo pkt_len = skb->len;
115f988d640SKalle Valo
116*5fd32ae0SKees Cook BUILD_BUG_ON(sizeof(txpd->tx_dest_addr) != ETH_ALEN);
117f988d640SKalle Valo if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) {
118f988d640SKalle Valo struct tx_radiotap_hdr *rtap_hdr = (void *)skb->data;
119f988d640SKalle Valo
120f988d640SKalle Valo /* set txpd fields from the radiotap header */
121f988d640SKalle Valo txpd->tx_control = cpu_to_le32(convert_radiotap_rate_to_mv(rtap_hdr->rate));
122f988d640SKalle Valo
123f988d640SKalle Valo /* skip the radiotap header */
124f988d640SKalle Valo p802x_hdr += sizeof(*rtap_hdr);
125f988d640SKalle Valo pkt_len -= sizeof(*rtap_hdr);
126f988d640SKalle Valo
127f988d640SKalle Valo /* copy destination address from 802.11 header */
128*5fd32ae0SKees Cook memcpy(&txpd->tx_dest_addr, p802x_hdr + 4, ETH_ALEN);
129f988d640SKalle Valo } else {
130f988d640SKalle Valo /* copy destination address from 802.3 header */
131*5fd32ae0SKees Cook memcpy(&txpd->tx_dest_addr, p802x_hdr, ETH_ALEN);
132f988d640SKalle Valo }
133f988d640SKalle Valo
134f988d640SKalle Valo txpd->tx_packet_length = cpu_to_le16(pkt_len);
135f988d640SKalle Valo txpd->tx_packet_location = cpu_to_le32(sizeof(struct txpd));
136f988d640SKalle Valo
137f988d640SKalle Valo lbs_mesh_set_txpd(priv, dev, txpd);
138f988d640SKalle Valo
139f988d640SKalle Valo lbs_deb_hex(LBS_DEB_TX, "txpd", (u8 *) &txpd, sizeof(struct txpd));
140f988d640SKalle Valo
141f988d640SKalle Valo lbs_deb_hex(LBS_DEB_TX, "Tx Data", (u8 *) p802x_hdr, le16_to_cpu(txpd->tx_packet_length));
142f988d640SKalle Valo
143f988d640SKalle Valo memcpy(&txpd[1], p802x_hdr, le16_to_cpu(txpd->tx_packet_length));
144f988d640SKalle Valo
145f988d640SKalle Valo spin_lock_irqsave(&priv->driver_lock, flags);
146f988d640SKalle Valo priv->tx_pending_len = pkt_len + sizeof(struct txpd);
147f988d640SKalle Valo
148f988d640SKalle Valo lbs_deb_tx("%s lined up packet\n", __func__);
149f988d640SKalle Valo
150f988d640SKalle Valo dev->stats.tx_packets++;
151f988d640SKalle Valo dev->stats.tx_bytes += skb->len;
152f988d640SKalle Valo
153f988d640SKalle Valo if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) {
154f988d640SKalle Valo /* Keep the skb to echo it back once Tx feedback is
155f988d640SKalle Valo received from FW */
156f988d640SKalle Valo skb_orphan(skb);
157f988d640SKalle Valo
158f988d640SKalle Valo /* Keep the skb around for when we get feedback */
159f988d640SKalle Valo priv->currenttxskb = skb;
160f988d640SKalle Valo } else {
161f988d640SKalle Valo free:
162f988d640SKalle Valo dev_kfree_skb_any(skb);
163f988d640SKalle Valo }
164f988d640SKalle Valo
165f988d640SKalle Valo unlock:
166f988d640SKalle Valo spin_unlock_irqrestore(&priv->driver_lock, flags);
167f988d640SKalle Valo wake_up(&priv->waitq);
168f988d640SKalle Valo
169f988d640SKalle Valo return ret;
170f988d640SKalle Valo }
171f988d640SKalle Valo
172f988d640SKalle Valo /**
173f988d640SKalle Valo * lbs_send_tx_feedback - sends to the host the last transmitted packet,
174f988d640SKalle Valo * filling the radiotap headers with transmission information.
175f988d640SKalle Valo *
176f988d640SKalle Valo * @priv: A pointer to &struct lbs_private structure
177f988d640SKalle Valo * @try_count: A 32-bit value containing transmission retry status.
178f988d640SKalle Valo *
179f988d640SKalle Valo * returns: void
180f988d640SKalle Valo */
lbs_send_tx_feedback(struct lbs_private * priv,u32 try_count)181f988d640SKalle Valo void lbs_send_tx_feedback(struct lbs_private *priv, u32 try_count)
182f988d640SKalle Valo {
183f988d640SKalle Valo struct tx_radiotap_hdr *radiotap_hdr;
184f988d640SKalle Valo
185f988d640SKalle Valo if (priv->wdev->iftype != NL80211_IFTYPE_MONITOR ||
186f988d640SKalle Valo priv->currenttxskb == NULL)
187f988d640SKalle Valo return;
188f988d640SKalle Valo
189f988d640SKalle Valo radiotap_hdr = (struct tx_radiotap_hdr *)priv->currenttxskb->data;
190f988d640SKalle Valo
191f988d640SKalle Valo radiotap_hdr->data_retries = try_count ?
192f988d640SKalle Valo (1 + priv->txretrycount - try_count) : 0;
193f988d640SKalle Valo
194f988d640SKalle Valo priv->currenttxskb->protocol = eth_type_trans(priv->currenttxskb,
195f988d640SKalle Valo priv->dev);
196f988d640SKalle Valo netif_rx(priv->currenttxskb);
197f988d640SKalle Valo
198f988d640SKalle Valo priv->currenttxskb = NULL;
199f988d640SKalle Valo
200f988d640SKalle Valo if (priv->connect_status == LBS_CONNECTED)
201f988d640SKalle Valo netif_wake_queue(priv->dev);
202f988d640SKalle Valo
203f988d640SKalle Valo if (priv->mesh_dev && netif_running(priv->mesh_dev))
204f988d640SKalle Valo netif_wake_queue(priv->mesh_dev);
205f988d640SKalle Valo }
206f988d640SKalle Valo EXPORT_SYMBOL_GPL(lbs_send_tx_feedback);
207