alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 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 | * |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 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 | |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/if_arp.h> |
| 22 | #include <linux/crc-ccitt.h> |
Alexander Aring | 061ef8f | 2014-10-27 17:13:28 +0100 | [diff] [blame] | 23 | #include <asm/unaligned.h> |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 24 | |
Alexander Aring | 6001d52 | 2014-10-26 09:37:09 +0100 | [diff] [blame] | 25 | #include <net/rtnetlink.h> |
Alan Ott | b5992fe | 2013-04-03 04:00:56 +0000 | [diff] [blame] | 26 | #include <net/ieee802154_netdev.h> |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 27 | #include <net/mac802154.h> |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame] | 28 | #include <net/cfg802154.h> |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 29 | |
Alexander Aring | 0f1556b | 2014-10-25 09:41:00 +0200 | [diff] [blame] | 30 | #include "ieee802154_i.h" |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 31 | #include "driver-ops.h" |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 32 | |
| 33 | /* IEEE 802.15.4 transceivers can sleep during the xmit session, so process |
| 34 | * packets through the workqueue. |
| 35 | */ |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 36 | struct ieee802154_xmit_cb { |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 37 | struct sk_buff *skb; |
| 38 | struct work_struct work; |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 39 | struct ieee802154_local *local; |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Alexander Aring | f81f466 | 2014-10-26 18:15:34 +0100 | [diff] [blame] | 42 | static struct ieee802154_xmit_cb ieee802154_xmit_cb; |
Alexander Aring | fe24371 | 2014-10-26 09:37:02 +0100 | [diff] [blame] | 43 | |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 44 | static void ieee802154_xmit_worker(struct work_struct *work) |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 45 | { |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 46 | struct ieee802154_xmit_cb *cb = |
| 47 | container_of(work, struct ieee802154_xmit_cb, work); |
Alexander Aring | e89e45f | 2014-10-26 09:37:03 +0100 | [diff] [blame] | 48 | struct ieee802154_local *local = cb->local; |
Alexander Aring | e89e45f | 2014-10-26 09:37:03 +0100 | [diff] [blame] | 49 | struct sk_buff *skb = cb->skb; |
Alexander Aring | 409c3b0 | 2014-10-26 09:37:12 +0100 | [diff] [blame] | 50 | struct net_device *dev = skb->dev; |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 51 | int res; |
| 52 | |
Alexander Aring | 6001d52 | 2014-10-26 09:37:09 +0100 | [diff] [blame] | 53 | rtnl_lock(); |
| 54 | |
| 55 | /* check if ifdown occurred while schedule */ |
Alexander Aring | 409c3b0 | 2014-10-26 09:37:12 +0100 | [diff] [blame] | 56 | if (!netif_running(dev)) |
Alexander Aring | 6001d52 | 2014-10-26 09:37:09 +0100 | [diff] [blame] | 57 | goto err_tx; |
| 58 | |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 59 | res = drv_xmit_sync(local, skb); |
Alexander Aring | 6001d52 | 2014-10-26 09:37:09 +0100 | [diff] [blame] | 60 | if (res) |
| 61 | goto err_tx; |
| 62 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 63 | ieee802154_xmit_complete(&local->hw, skb, false); |
Alexander Aring | 6001d52 | 2014-10-26 09:37:09 +0100 | [diff] [blame] | 64 | |
Alexander Aring | 409c3b0 | 2014-10-26 09:37:12 +0100 | [diff] [blame] | 65 | dev->stats.tx_packets++; |
| 66 | dev->stats.tx_bytes += skb->len; |
| 67 | |
Alexander Aring | 6001d52 | 2014-10-26 09:37:09 +0100 | [diff] [blame] | 68 | rtnl_unlock(); |
| 69 | |
| 70 | return; |
| 71 | |
| 72 | err_tx: |
| 73 | /* Restart the netif queue on each sub_if_data object. */ |
| 74 | ieee802154_wake_queue(&local->hw); |
| 75 | rtnl_unlock(); |
| 76 | kfree_skb(skb); |
Alexander Aring | 409c3b0 | 2014-10-26 09:37:12 +0100 | [diff] [blame] | 77 | netdev_dbg(dev, "transmission failed\n"); |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Aring | dc67c6b | 2014-10-26 09:37:04 +0100 | [diff] [blame] | 80 | static netdev_tx_t |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 81 | ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb) |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 82 | { |
Alexander Aring | 409c3b0 | 2014-10-26 09:37:12 +0100 | [diff] [blame] | 83 | struct net_device *dev = skb->dev; |
Alexander Aring | ed0a5dc | 2014-10-26 09:37:08 +0100 | [diff] [blame] | 84 | int ret; |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 85 | |
Alexander Aring | 90386a7 | 2014-10-29 21:34:34 +0100 | [diff] [blame] | 86 | if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) { |
Alexander Aring | 061ef8f | 2014-10-27 17:13:28 +0100 | [diff] [blame] | 87 | u16 crc = crc_ccitt(0, skb->data, skb->len); |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 88 | |
Alexander Aring | 061ef8f | 2014-10-27 17:13:28 +0100 | [diff] [blame] | 89 | put_unaligned_le16(crc, skb_put(skb, 2)); |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 92 | if (skb_cow_head(skb, local->hw.extra_tx_headroom)) |
Varka Bhadram | f558891 | 2014-08-11 13:25:10 +0200 | [diff] [blame] | 93 | goto err_tx; |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 94 | |
Alan Ott | b5992fe | 2013-04-03 04:00:56 +0000 | [diff] [blame] | 95 | /* Stop the netif queue on each sub_if_data object. */ |
Alexander Aring | 18d60a0 | 2014-10-26 09:37:06 +0100 | [diff] [blame] | 96 | ieee802154_stop_queue(&local->hw); |
Alan Ott | b5992fe | 2013-04-03 04:00:56 +0000 | [diff] [blame] | 97 | |
Alexander Aring | ed0a5dc | 2014-10-26 09:37:08 +0100 | [diff] [blame] | 98 | /* async is priority, otherwise sync is fallback */ |
| 99 | if (local->ops->xmit_async) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 100 | ret = drv_xmit_async(local, skb); |
Alexander Aring | ed0a5dc | 2014-10-26 09:37:08 +0100 | [diff] [blame] | 101 | if (ret) { |
| 102 | ieee802154_wake_queue(&local->hw); |
| 103 | goto err_tx; |
| 104 | } |
Alexander Aring | 409c3b0 | 2014-10-26 09:37:12 +0100 | [diff] [blame] | 105 | |
| 106 | dev->stats.tx_packets++; |
| 107 | dev->stats.tx_bytes += skb->len; |
Alexander Aring | ed0a5dc | 2014-10-26 09:37:08 +0100 | [diff] [blame] | 108 | } else { |
Alexander Aring | f81f466 | 2014-10-26 18:15:34 +0100 | [diff] [blame] | 109 | INIT_WORK(&ieee802154_xmit_cb.work, ieee802154_xmit_worker); |
| 110 | ieee802154_xmit_cb.skb = skb; |
| 111 | ieee802154_xmit_cb.local = local; |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 112 | |
Alexander Aring | f81f466 | 2014-10-26 18:15:34 +0100 | [diff] [blame] | 113 | queue_work(local->workqueue, &ieee802154_xmit_cb.work); |
Alexander Aring | ed0a5dc | 2014-10-26 09:37:08 +0100 | [diff] [blame] | 114 | } |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 115 | |
| 116 | return NETDEV_TX_OK; |
Varka Bhadram | f558891 | 2014-08-11 13:25:10 +0200 | [diff] [blame] | 117 | |
| 118 | err_tx: |
| 119 | kfree_skb(skb); |
| 120 | return NETDEV_TX_OK; |
alex.bluesman.smirnov@gmail.com | 5b641eb | 2012-05-15 20:50:22 +0000 | [diff] [blame] | 121 | } |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 122 | |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 123 | netdev_tx_t |
| 124 | ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 125 | { |
| 126 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 127 | |
| 128 | skb->skb_iif = dev->ifindex; |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 129 | |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 130 | return ieee802154_tx(sdata->local, skb); |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 133 | netdev_tx_t |
| 134 | ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 135 | { |
| 136 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 137 | int rc; |
| 138 | |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 139 | rc = mac802154_llsec_encrypt(&sdata->sec, skb); |
| 140 | if (rc) { |
Alexander Aring | cfa626c | 2014-10-26 09:37:10 +0100 | [diff] [blame] | 141 | netdev_warn(dev, "encryption failed: %i\n", rc); |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 142 | kfree_skb(skb); |
| 143 | return NETDEV_TX_OK; |
| 144 | } |
| 145 | |
| 146 | skb->skb_iif = dev->ifindex; |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 147 | |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 148 | return ieee802154_tx(sdata->local, skb); |
Alexander Aring | 50c6fb9 | 2014-10-26 09:37:01 +0100 | [diff] [blame] | 149 | } |