tx.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2007-2012 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * Written by:
  14. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  15. * Sergey Lapin <slapin@ossfans.org>
  16. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  17. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  18. */
  19. #include <linux/netdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/crc-ccitt.h>
  22. #include <asm/unaligned.h>
  23. #include <net/rtnetlink.h>
  24. #include <net/ieee802154_netdev.h>
  25. #include <net/mac802154.h>
  26. #include <net/cfg802154.h>
  27. #include "ieee802154_i.h"
  28. #include "driver-ops.h"
  29. /* IEEE 802.15.4 transceivers can sleep during the xmit session, so process
  30. * packets through the workqueue.
  31. */
  32. struct ieee802154_xmit_cb {
  33. struct sk_buff *skb;
  34. struct work_struct work;
  35. struct ieee802154_local *local;
  36. };
  37. static struct ieee802154_xmit_cb ieee802154_xmit_cb;
  38. static void ieee802154_xmit_worker(struct work_struct *work)
  39. {
  40. struct ieee802154_xmit_cb *cb =
  41. container_of(work, struct ieee802154_xmit_cb, work);
  42. struct ieee802154_local *local = cb->local;
  43. struct sk_buff *skb = cb->skb;
  44. struct net_device *dev = skb->dev;
  45. int res;
  46. rtnl_lock();
  47. /* check if ifdown occurred while schedule */
  48. if (!netif_running(dev))
  49. goto err_tx;
  50. res = drv_xmit_sync(local, skb);
  51. if (res)
  52. goto err_tx;
  53. ieee802154_xmit_complete(&local->hw, skb, false);
  54. dev->stats.tx_packets++;
  55. dev->stats.tx_bytes += skb->len;
  56. rtnl_unlock();
  57. return;
  58. err_tx:
  59. /* Restart the netif queue on each sub_if_data object. */
  60. ieee802154_wake_queue(&local->hw);
  61. rtnl_unlock();
  62. kfree_skb(skb);
  63. netdev_dbg(dev, "transmission failed\n");
  64. }
  65. static netdev_tx_t
  66. ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
  67. {
  68. struct net_device *dev = skb->dev;
  69. int ret;
  70. if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) {
  71. u16 crc = crc_ccitt(0, skb->data, skb->len);
  72. put_unaligned_le16(crc, skb_put(skb, 2));
  73. }
  74. if (skb_cow_head(skb, local->hw.extra_tx_headroom))
  75. goto err_tx;
  76. /* Stop the netif queue on each sub_if_data object. */
  77. ieee802154_stop_queue(&local->hw);
  78. /* async is priority, otherwise sync is fallback */
  79. if (local->ops->xmit_async) {
  80. ret = drv_xmit_async(local, skb);
  81. if (ret) {
  82. ieee802154_wake_queue(&local->hw);
  83. goto err_tx;
  84. }
  85. dev->stats.tx_packets++;
  86. dev->stats.tx_bytes += skb->len;
  87. } else {
  88. INIT_WORK(&ieee802154_xmit_cb.work, ieee802154_xmit_worker);
  89. ieee802154_xmit_cb.skb = skb;
  90. ieee802154_xmit_cb.local = local;
  91. queue_work(local->workqueue, &ieee802154_xmit_cb.work);
  92. }
  93. return NETDEV_TX_OK;
  94. err_tx:
  95. kfree_skb(skb);
  96. return NETDEV_TX_OK;
  97. }
  98. netdev_tx_t
  99. ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev)
  100. {
  101. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  102. skb->skb_iif = dev->ifindex;
  103. return ieee802154_tx(sdata->local, skb);
  104. }
  105. netdev_tx_t
  106. ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  107. {
  108. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  109. int rc;
  110. rc = mac802154_llsec_encrypt(&sdata->sec, skb);
  111. if (rc) {
  112. netdev_warn(dev, "encryption failed: %i\n", rc);
  113. kfree_skb(skb);
  114. return NETDEV_TX_OK;
  115. }
  116. skb->skb_iif = dev->ifindex;
  117. return ieee802154_tx(sdata->local, skb);
  118. }