xref: /openbmc/linux/net/mac802154/tx.c (revision d6b6592ac6d11eab91e6758d224eac35f4122aca)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25b641ebeSalex.bluesman.smirnov@gmail.com /*
35b641ebeSalex.bluesman.smirnov@gmail.com  * Copyright 2007-2012 Siemens AG
45b641ebeSalex.bluesman.smirnov@gmail.com  *
55b641ebeSalex.bluesman.smirnov@gmail.com  * Written by:
65b641ebeSalex.bluesman.smirnov@gmail.com  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
75b641ebeSalex.bluesman.smirnov@gmail.com  * Sergey Lapin <slapin@ossfans.org>
85b641ebeSalex.bluesman.smirnov@gmail.com  * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
95b641ebeSalex.bluesman.smirnov@gmail.com  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
105b641ebeSalex.bluesman.smirnov@gmail.com  */
115b641ebeSalex.bluesman.smirnov@gmail.com 
125b641ebeSalex.bluesman.smirnov@gmail.com #include <linux/netdevice.h>
135b641ebeSalex.bluesman.smirnov@gmail.com #include <linux/if_arp.h>
145b641ebeSalex.bluesman.smirnov@gmail.com #include <linux/crc-ccitt.h>
15061ef8f9SAlexander Aring #include <asm/unaligned.h>
165b641ebeSalex.bluesman.smirnov@gmail.com 
176001d522SAlexander Aring #include <net/rtnetlink.h>
18b5992fe9SAlan Ott #include <net/ieee802154_netdev.h>
195b641ebeSalex.bluesman.smirnov@gmail.com #include <net/mac802154.h>
205ad60d36SAlexander Aring #include <net/cfg802154.h>
215b641ebeSalex.bluesman.smirnov@gmail.com 
220f1556bcSAlexander Aring #include "ieee802154_i.h"
2359cb300fSAlexander Aring #include "driver-ops.h"
245b641ebeSalex.bluesman.smirnov@gmail.com 
ieee802154_xmit_sync_worker(struct work_struct * work)25be8c6d86SMiquel Raynal void ieee802154_xmit_sync_worker(struct work_struct *work)
265b641ebeSalex.bluesman.smirnov@gmail.com {
27c22ff7b4SLennert Buytenhek 	struct ieee802154_local *local =
28983a974bSMiquel Raynal 		container_of(work, struct ieee802154_local, sync_tx_work);
29c22ff7b4SLennert Buytenhek 	struct sk_buff *skb = local->tx_skb;
30409c3b0cSAlexander Aring 	struct net_device *dev = skb->dev;
315b641ebeSalex.bluesman.smirnov@gmail.com 	int res;
325b641ebeSalex.bluesman.smirnov@gmail.com 
3359cb300fSAlexander Aring 	res = drv_xmit_sync(local, skb);
346001d522SAlexander Aring 	if (res)
356001d522SAlexander Aring 		goto err_tx;
366001d522SAlexander Aring 
37*cf3f2031SYunshui Jiang 	DEV_STATS_INC(dev, tx_packets);
38*cf3f2031SYunshui Jiang 	DEV_STATS_ADD(dev, tx_bytes, skb->len);
39409c3b0cSAlexander Aring 
400ff4628fSEric Dumazet 	ieee802154_xmit_complete(&local->hw, skb, false);
410ff4628fSEric Dumazet 
426001d522SAlexander Aring 	return;
436001d522SAlexander Aring 
446001d522SAlexander Aring err_tx:
45cdb66beaSAlexander Aring 	/* Restart the netif queue on each sub_if_data object. */
4620a19d1dSMiquel Raynal 	ieee802154_release_queue(local);
476c1c78d0SAlexander Aring 	if (atomic_dec_and_test(&local->phy->ongoing_txs))
48f0feb349SMiquel Raynal 		wake_up(&local->phy->sync_txq);
49cdb66beaSAlexander Aring 	kfree_skb(skb);
50409c3b0cSAlexander Aring 	netdev_dbg(dev, "transmission failed\n");
51cdb66beaSAlexander Aring }
525b641ebeSalex.bluesman.smirnov@gmail.com 
53dc67c6b3SAlexander Aring static netdev_tx_t
ieee802154_tx(struct ieee802154_local * local,struct sk_buff * skb)54e5e584fcSAlexander Aring ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
555b641ebeSalex.bluesman.smirnov@gmail.com {
56409c3b0cSAlexander Aring 	struct net_device *dev = skb->dev;
57ed0a5dceSAlexander Aring 	int ret;
585b641ebeSalex.bluesman.smirnov@gmail.com 
5990386a7eSAlexander Aring 	if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) {
60f9c52831SAlexander Aring 		struct sk_buff *nskb;
61f9c52831SAlexander Aring 		u16 crc;
624710d806SVarka Bhadram 
63f9c52831SAlexander Aring 		if (unlikely(skb_tailroom(skb) < IEEE802154_FCS_LEN)) {
64f9c52831SAlexander Aring 			nskb = skb_copy_expand(skb, 0, IEEE802154_FCS_LEN,
65f9c52831SAlexander Aring 					       GFP_ATOMIC);
66f9c52831SAlexander Aring 			if (likely(nskb)) {
67f9c52831SAlexander Aring 				consume_skb(skb);
68f9c52831SAlexander Aring 				skb = nskb;
69f9c52831SAlexander Aring 			} else {
70d08d951aSMiquel Raynal 				goto err_free_skb;
71f9c52831SAlexander Aring 			}
72f9c52831SAlexander Aring 		}
73f9c52831SAlexander Aring 
74f9c52831SAlexander Aring 		crc = crc_ccitt(0, skb->data, skb->len);
75061ef8f9SAlexander Aring 		put_unaligned_le16(crc, skb_put(skb, 2));
765b641ebeSalex.bluesman.smirnov@gmail.com 	}
775b641ebeSalex.bluesman.smirnov@gmail.com 
78b5992fe9SAlan Ott 	/* Stop the netif queue on each sub_if_data object. */
7920a19d1dSMiquel Raynal 	ieee802154_hold_queue(local);
80bde000aeSMiquel Raynal 	atomic_inc(&local->phy->ongoing_txs);
81b5992fe9SAlan Ott 
82983a974bSMiquel Raynal 	/* Drivers should preferably implement the async callback. In some rare
83983a974bSMiquel Raynal 	 * cases they only provide a sync callback which we will use as a
84983a974bSMiquel Raynal 	 * fallback.
85983a974bSMiquel Raynal 	 */
86ed0a5dceSAlexander Aring 	if (local->ops->xmit_async) {
870ff4628fSEric Dumazet 		unsigned int len = skb->len;
880ff4628fSEric Dumazet 
8959cb300fSAlexander Aring 		ret = drv_xmit_async(local, skb);
90d08d951aSMiquel Raynal 		if (ret)
91d08d951aSMiquel Raynal 			goto err_wake_netif_queue;
92409c3b0cSAlexander Aring 
93*cf3f2031SYunshui Jiang 		DEV_STATS_INC(dev, tx_packets);
94*cf3f2031SYunshui Jiang 		DEV_STATS_ADD(dev, tx_bytes, len);
95ed0a5dceSAlexander Aring 	} else {
96c22ff7b4SLennert Buytenhek 		local->tx_skb = skb;
97983a974bSMiquel Raynal 		queue_work(local->workqueue, &local->sync_tx_work);
98ed0a5dceSAlexander Aring 	}
995b641ebeSalex.bluesman.smirnov@gmail.com 
1005b641ebeSalex.bluesman.smirnov@gmail.com 	return NETDEV_TX_OK;
101f5588912SVarka Bhadram 
102d08d951aSMiquel Raynal err_wake_netif_queue:
10320a19d1dSMiquel Raynal 	ieee802154_release_queue(local);
1046c1c78d0SAlexander Aring 	if (atomic_dec_and_test(&local->phy->ongoing_txs))
105f0feb349SMiquel Raynal 		wake_up(&local->phy->sync_txq);
106d08d951aSMiquel Raynal err_free_skb:
107f5588912SVarka Bhadram 	kfree_skb(skb);
108f5588912SVarka Bhadram 	return NETDEV_TX_OK;
1095b641ebeSalex.bluesman.smirnov@gmail.com }
11050c6fb99SAlexander Aring 
ieee802154_sync_queue(struct ieee802154_local * local)111f0feb349SMiquel Raynal static int ieee802154_sync_queue(struct ieee802154_local *local)
112f0feb349SMiquel Raynal {
113f0feb349SMiquel Raynal 	int ret;
114f0feb349SMiquel Raynal 
115f0feb349SMiquel Raynal 	ieee802154_hold_queue(local);
116f0feb349SMiquel Raynal 	ieee802154_disable_queue(local);
117f0feb349SMiquel Raynal 	wait_event(local->phy->sync_txq, !atomic_read(&local->phy->ongoing_txs));
118f0feb349SMiquel Raynal 	ret = local->tx_result;
119f0feb349SMiquel Raynal 	ieee802154_release_queue(local);
120f0feb349SMiquel Raynal 
121f0feb349SMiquel Raynal 	return ret;
122f0feb349SMiquel Raynal }
123f0feb349SMiquel Raynal 
ieee802154_sync_and_hold_queue(struct ieee802154_local * local)124f0feb349SMiquel Raynal int ieee802154_sync_and_hold_queue(struct ieee802154_local *local)
125f0feb349SMiquel Raynal {
1262b13db13SMiquel Raynal 	int ret;
127f0feb349SMiquel Raynal 
1282b13db13SMiquel Raynal 	ieee802154_hold_queue(local);
1292b13db13SMiquel Raynal 	ret = ieee802154_sync_queue(local);
1302b13db13SMiquel Raynal 	set_bit(WPAN_PHY_FLAG_STATE_QUEUE_STOPPED, &local->phy->flags);
1312b13db13SMiquel Raynal 
1322b13db13SMiquel Raynal 	return ret;
133f0feb349SMiquel Raynal }
134f0feb349SMiquel Raynal 
ieee802154_mlme_op_pre(struct ieee802154_local * local)135ddd9ee7cSMiquel Raynal int ieee802154_mlme_op_pre(struct ieee802154_local *local)
136ddd9ee7cSMiquel Raynal {
137ddd9ee7cSMiquel Raynal 	return ieee802154_sync_and_hold_queue(local);
138ddd9ee7cSMiquel Raynal }
139ddd9ee7cSMiquel Raynal 
ieee802154_mlme_tx_locked(struct ieee802154_local * local,struct ieee802154_sub_if_data * sdata,struct sk_buff * skb)140dd180962SMiquel Raynal int ieee802154_mlme_tx_locked(struct ieee802154_local *local,
141dd180962SMiquel Raynal 			      struct ieee802154_sub_if_data *sdata,
142dd180962SMiquel Raynal 			      struct sk_buff *skb)
143dd180962SMiquel Raynal {
144dd180962SMiquel Raynal 	/* Avoid possible calls to ->ndo_stop() when we asynchronously perform
145dd180962SMiquel Raynal 	 * MLME transmissions.
146dd180962SMiquel Raynal 	 */
147dd180962SMiquel Raynal 	ASSERT_RTNL();
148dd180962SMiquel Raynal 
149dd180962SMiquel Raynal 	/* Ensure the device was not stopped, otherwise error out */
150dd180962SMiquel Raynal 	if (!local->open_count)
151dd180962SMiquel Raynal 		return -ENETDOWN;
152dd180962SMiquel Raynal 
153dd180962SMiquel Raynal 	/* Warn if the ieee802154 core thinks MLME frames can be sent while the
154dd180962SMiquel Raynal 	 * net interface expects this cannot happen.
155dd180962SMiquel Raynal 	 */
156dd180962SMiquel Raynal 	if (WARN_ON_ONCE(!netif_running(sdata->dev)))
157dd180962SMiquel Raynal 		return -ENETDOWN;
158dd180962SMiquel Raynal 
159dd180962SMiquel Raynal 	ieee802154_tx(local, skb);
160dd180962SMiquel Raynal 	return ieee802154_sync_queue(local);
161dd180962SMiquel Raynal }
162dd180962SMiquel Raynal 
ieee802154_mlme_tx(struct ieee802154_local * local,struct ieee802154_sub_if_data * sdata,struct sk_buff * skb)163fbdaa5baSMiquel Raynal int ieee802154_mlme_tx(struct ieee802154_local *local,
164fbdaa5baSMiquel Raynal 		       struct ieee802154_sub_if_data *sdata,
165fbdaa5baSMiquel Raynal 		       struct sk_buff *skb)
166ddd9ee7cSMiquel Raynal {
167ddd9ee7cSMiquel Raynal 	int ret;
168ddd9ee7cSMiquel Raynal 
169ddd9ee7cSMiquel Raynal 	rtnl_lock();
170dd180962SMiquel Raynal 	ret = ieee802154_mlme_tx_locked(local, sdata, skb);
171ddd9ee7cSMiquel Raynal 	rtnl_unlock();
172ddd9ee7cSMiquel Raynal 
173ddd9ee7cSMiquel Raynal 	return ret;
174ddd9ee7cSMiquel Raynal }
175ddd9ee7cSMiquel Raynal 
ieee802154_mlme_op_post(struct ieee802154_local * local)176ddd9ee7cSMiquel Raynal void ieee802154_mlme_op_post(struct ieee802154_local *local)
177ddd9ee7cSMiquel Raynal {
178ddd9ee7cSMiquel Raynal 	ieee802154_release_queue(local);
179ddd9ee7cSMiquel Raynal }
180ddd9ee7cSMiquel Raynal 
ieee802154_mlme_tx_one(struct ieee802154_local * local,struct ieee802154_sub_if_data * sdata,struct sk_buff * skb)181fbdaa5baSMiquel Raynal int ieee802154_mlme_tx_one(struct ieee802154_local *local,
182fbdaa5baSMiquel Raynal 			   struct ieee802154_sub_if_data *sdata,
183fbdaa5baSMiquel Raynal 			   struct sk_buff *skb)
184ddd9ee7cSMiquel Raynal {
185ddd9ee7cSMiquel Raynal 	int ret;
186ddd9ee7cSMiquel Raynal 
187ddd9ee7cSMiquel Raynal 	ieee802154_mlme_op_pre(local);
188fbdaa5baSMiquel Raynal 	ret = ieee802154_mlme_tx(local, sdata, skb);
189ddd9ee7cSMiquel Raynal 	ieee802154_mlme_op_post(local);
190ddd9ee7cSMiquel Raynal 
191ddd9ee7cSMiquel Raynal 	return ret;
192ddd9ee7cSMiquel Raynal }
193ddd9ee7cSMiquel Raynal 
ieee802154_mlme_tx_one_locked(struct ieee802154_local * local,struct ieee802154_sub_if_data * sdata,struct sk_buff * skb)194dd180962SMiquel Raynal int ieee802154_mlme_tx_one_locked(struct ieee802154_local *local,
195dd180962SMiquel Raynal 				  struct ieee802154_sub_if_data *sdata,
196dd180962SMiquel Raynal 				  struct sk_buff *skb)
197dd180962SMiquel Raynal {
198dd180962SMiquel Raynal 	int ret;
199dd180962SMiquel Raynal 
200dd180962SMiquel Raynal 	ieee802154_mlme_op_pre(local);
201dd180962SMiquel Raynal 	ret = ieee802154_mlme_tx_locked(local, sdata, skb);
202dd180962SMiquel Raynal 	ieee802154_mlme_op_post(local);
203dd180962SMiquel Raynal 
204dd180962SMiquel Raynal 	return ret;
205dd180962SMiquel Raynal }
206dd180962SMiquel Raynal 
ieee802154_queue_is_stopped(struct ieee802154_local * local)2072b13db13SMiquel Raynal static bool ieee802154_queue_is_stopped(struct ieee802154_local *local)
2082b13db13SMiquel Raynal {
2092b13db13SMiquel Raynal 	return test_bit(WPAN_PHY_FLAG_STATE_QUEUE_STOPPED, &local->phy->flags);
2102b13db13SMiquel Raynal }
2112b13db13SMiquel Raynal 
212226730e1SMiquel Raynal static netdev_tx_t
ieee802154_hot_tx(struct ieee802154_local * local,struct sk_buff * skb)213226730e1SMiquel Raynal ieee802154_hot_tx(struct ieee802154_local *local, struct sk_buff *skb)
214226730e1SMiquel Raynal {
2152b13db13SMiquel Raynal 	/* Warn if the net interface tries to transmit frames while the
2162b13db13SMiquel Raynal 	 * ieee802154 core assumes the queue is stopped.
2172b13db13SMiquel Raynal 	 */
2182b13db13SMiquel Raynal 	WARN_ON_ONCE(ieee802154_queue_is_stopped(local));
2192b13db13SMiquel Raynal 
220226730e1SMiquel Raynal 	return ieee802154_tx(local, skb);
221226730e1SMiquel Raynal }
222226730e1SMiquel Raynal 
223e5e584fcSAlexander Aring netdev_tx_t
ieee802154_monitor_start_xmit(struct sk_buff * skb,struct net_device * dev)224e5e584fcSAlexander Aring ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev)
22550c6fb99SAlexander Aring {
22650c6fb99SAlexander Aring 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
22750c6fb99SAlexander Aring 
22850c6fb99SAlexander Aring 	skb->skb_iif = dev->ifindex;
22950c6fb99SAlexander Aring 
230226730e1SMiquel Raynal 	return ieee802154_hot_tx(sdata->local, skb);
23150c6fb99SAlexander Aring }
23250c6fb99SAlexander Aring 
233e5e584fcSAlexander Aring netdev_tx_t
ieee802154_subif_start_xmit(struct sk_buff * skb,struct net_device * dev)234e5e584fcSAlexander Aring ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
23550c6fb99SAlexander Aring {
23650c6fb99SAlexander Aring 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
23750c6fb99SAlexander Aring 	int rc;
23850c6fb99SAlexander Aring 
239d58a2fa9SAlexander Aring 	/* TODO we should move it to wpan_dev_hard_header and dev_hard_header
240d58a2fa9SAlexander Aring 	 * functions. The reason is wireshark will show a mac header which is
241d58a2fa9SAlexander Aring 	 * with security fields but the payload is not encrypted.
242d58a2fa9SAlexander Aring 	 */
24350c6fb99SAlexander Aring 	rc = mac802154_llsec_encrypt(&sdata->sec, skb);
24450c6fb99SAlexander Aring 	if (rc) {
245cfa626cbSAlexander Aring 		netdev_warn(dev, "encryption failed: %i\n", rc);
24650c6fb99SAlexander Aring 		kfree_skb(skb);
24750c6fb99SAlexander Aring 		return NETDEV_TX_OK;
24850c6fb99SAlexander Aring 	}
24950c6fb99SAlexander Aring 
25050c6fb99SAlexander Aring 	skb->skb_iif = dev->ifindex;
25150c6fb99SAlexander Aring 
252226730e1SMiquel Raynal 	return ieee802154_hot_tx(sdata->local, skb);
25350c6fb99SAlexander Aring }
254