tx.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2007-2012 Siemens AG
  4. *
  5. * Written by:
  6. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  7. * Sergey Lapin <slapin@ossfans.org>
  8. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  9. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  10. */
  11. #include <linux/netdevice.h>
  12. #include <linux/if_arp.h>
  13. #include <linux/crc-ccitt.h>
  14. #include <linux/unaligned.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/ieee802154_netdev.h>
  17. #include <net/mac802154.h>
  18. #include <net/cfg802154.h>
  19. #include "ieee802154_i.h"
  20. #include "driver-ops.h"
  21. void ieee802154_xmit_sync_worker(struct work_struct *work)
  22. {
  23. struct ieee802154_local *local =
  24. container_of(work, struct ieee802154_local, sync_tx_work);
  25. struct sk_buff *skb = local->tx_skb;
  26. struct net_device *dev = skb->dev;
  27. int res;
  28. res = drv_xmit_sync(local, skb);
  29. if (res)
  30. goto err_tx;
  31. DEV_STATS_INC(dev, tx_packets);
  32. DEV_STATS_ADD(dev, tx_bytes, skb->len);
  33. ieee802154_xmit_complete(&local->hw, skb, false);
  34. return;
  35. err_tx:
  36. /* Restart the netif queue on each sub_if_data object. */
  37. ieee802154_release_queue(local);
  38. if (atomic_dec_and_test(&local->phy->ongoing_txs))
  39. wake_up(&local->phy->sync_txq);
  40. kfree_skb(skb);
  41. netdev_dbg(dev, "transmission failed\n");
  42. }
  43. static netdev_tx_t
  44. ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
  45. {
  46. struct net_device *dev = skb->dev;
  47. int ret;
  48. if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) {
  49. struct sk_buff *nskb;
  50. u16 crc;
  51. if (unlikely(skb_tailroom(skb) < IEEE802154_FCS_LEN)) {
  52. nskb = skb_copy_expand(skb, 0, IEEE802154_FCS_LEN,
  53. GFP_ATOMIC);
  54. if (likely(nskb)) {
  55. consume_skb(skb);
  56. skb = nskb;
  57. } else {
  58. goto err_free_skb;
  59. }
  60. }
  61. crc = crc_ccitt(0, skb->data, skb->len);
  62. put_unaligned_le16(crc, skb_put(skb, 2));
  63. }
  64. /* Stop the netif queue on each sub_if_data object. */
  65. ieee802154_hold_queue(local);
  66. atomic_inc(&local->phy->ongoing_txs);
  67. /* Drivers should preferably implement the async callback. In some rare
  68. * cases they only provide a sync callback which we will use as a
  69. * fallback.
  70. */
  71. if (local->ops->xmit_async) {
  72. unsigned int len = skb->len;
  73. ret = drv_xmit_async(local, skb);
  74. if (ret)
  75. goto err_wake_netif_queue;
  76. DEV_STATS_INC(dev, tx_packets);
  77. DEV_STATS_ADD(dev, tx_bytes, len);
  78. } else {
  79. local->tx_skb = skb;
  80. queue_work(local->workqueue, &local->sync_tx_work);
  81. }
  82. return NETDEV_TX_OK;
  83. err_wake_netif_queue:
  84. ieee802154_release_queue(local);
  85. if (atomic_dec_and_test(&local->phy->ongoing_txs))
  86. wake_up(&local->phy->sync_txq);
  87. err_free_skb:
  88. kfree_skb(skb);
  89. return NETDEV_TX_OK;
  90. }
  91. static int ieee802154_sync_queue(struct ieee802154_local *local)
  92. {
  93. int ret;
  94. ieee802154_hold_queue(local);
  95. ieee802154_disable_queue(local);
  96. wait_event(local->phy->sync_txq, !atomic_read(&local->phy->ongoing_txs));
  97. ret = local->tx_result;
  98. ieee802154_release_queue(local);
  99. return ret;
  100. }
  101. int ieee802154_sync_and_hold_queue(struct ieee802154_local *local)
  102. {
  103. int ret;
  104. ieee802154_hold_queue(local);
  105. ret = ieee802154_sync_queue(local);
  106. set_bit(WPAN_PHY_FLAG_STATE_QUEUE_STOPPED, &local->phy->flags);
  107. return ret;
  108. }
  109. int ieee802154_mlme_op_pre(struct ieee802154_local *local)
  110. {
  111. return ieee802154_sync_and_hold_queue(local);
  112. }
  113. int ieee802154_mlme_tx_locked(struct ieee802154_local *local,
  114. struct ieee802154_sub_if_data *sdata,
  115. struct sk_buff *skb)
  116. {
  117. /* Avoid possible calls to ->ndo_stop() when we asynchronously perform
  118. * MLME transmissions.
  119. */
  120. ASSERT_RTNL();
  121. /* Ensure the device was not stopped, otherwise error out */
  122. if (!local->open_count)
  123. return -ENETDOWN;
  124. /* Warn if the ieee802154 core thinks MLME frames can be sent while the
  125. * net interface expects this cannot happen.
  126. */
  127. if (WARN_ON_ONCE(!netif_running(sdata->dev)))
  128. return -ENETDOWN;
  129. ieee802154_tx(local, skb);
  130. return ieee802154_sync_queue(local);
  131. }
  132. int ieee802154_mlme_tx(struct ieee802154_local *local,
  133. struct ieee802154_sub_if_data *sdata,
  134. struct sk_buff *skb)
  135. {
  136. int ret;
  137. rtnl_lock();
  138. ret = ieee802154_mlme_tx_locked(local, sdata, skb);
  139. rtnl_unlock();
  140. return ret;
  141. }
  142. void ieee802154_mlme_op_post(struct ieee802154_local *local)
  143. {
  144. ieee802154_release_queue(local);
  145. }
  146. int ieee802154_mlme_tx_one_locked(struct ieee802154_local *local,
  147. struct ieee802154_sub_if_data *sdata,
  148. struct sk_buff *skb)
  149. {
  150. int ret;
  151. ieee802154_mlme_op_pre(local);
  152. ret = ieee802154_mlme_tx_locked(local, sdata, skb);
  153. ieee802154_mlme_op_post(local);
  154. return ret;
  155. }
  156. static bool ieee802154_queue_is_stopped(struct ieee802154_local *local)
  157. {
  158. return test_bit(WPAN_PHY_FLAG_STATE_QUEUE_STOPPED, &local->phy->flags);
  159. }
  160. static netdev_tx_t
  161. ieee802154_hot_tx(struct ieee802154_local *local, struct sk_buff *skb)
  162. {
  163. /* Warn if the net interface tries to transmit frames while the
  164. * ieee802154 core assumes the queue is stopped.
  165. */
  166. WARN_ON_ONCE(ieee802154_queue_is_stopped(local));
  167. return ieee802154_tx(local, skb);
  168. }
  169. netdev_tx_t
  170. ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev)
  171. {
  172. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  173. skb->skb_iif = dev->ifindex;
  174. return ieee802154_hot_tx(sdata->local, skb);
  175. }
  176. netdev_tx_t
  177. ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  178. {
  179. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  180. int rc;
  181. /* TODO we should move it to wpan_dev_hard_header and dev_hard_header
  182. * functions. The reason is wireshark will show a mac header which is
  183. * with security fields but the payload is not encrypted.
  184. */
  185. rc = mac802154_llsec_encrypt(&sdata->sec, skb);
  186. if (rc) {
  187. netdev_warn(dev, "encryption failed: %i\n", rc);
  188. kfree_skb(skb);
  189. return NETDEV_TX_OK;
  190. }
  191. skb->skb_iif = dev->ifindex;
  192. return ieee802154_hot_tx(sdata->local, skb);
  193. }