Merge tag 'for-linus-20150422' of git://git.infradead.org/linux-mtd
[linux-2.6-block.git] / net / mac802154 / tx.c
CommitLineData
5b641ebe 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 *
5b641ebe 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>
061ef8f9 23#include <asm/unaligned.h>
5b641ebe 24
6001d522 25#include <net/rtnetlink.h>
b5992fe9 26#include <net/ieee802154_netdev.h>
5b641ebe 27#include <net/mac802154.h>
5ad60d36 28#include <net/cfg802154.h>
5b641ebe 29
0f1556bc 30#include "ieee802154_i.h"
59cb300f 31#include "driver-ops.h"
5b641ebe 32
33/* IEEE 802.15.4 transceivers can sleep during the xmit session, so process
34 * packets through the workqueue.
35 */
e5e584fc 36struct ieee802154_xmit_cb {
5b641ebe 37 struct sk_buff *skb;
38 struct work_struct work;
a5e1ec53 39 struct ieee802154_local *local;
5b641ebe 40};
41
f81f466c 42static struct ieee802154_xmit_cb ieee802154_xmit_cb;
fe24371d 43
e5e584fc 44static void ieee802154_xmit_worker(struct work_struct *work)
5b641ebe 45{
e5e584fc
AA
46 struct ieee802154_xmit_cb *cb =
47 container_of(work, struct ieee802154_xmit_cb, work);
e89e45f2 48 struct ieee802154_local *local = cb->local;
e89e45f2 49 struct sk_buff *skb = cb->skb;
409c3b0c 50 struct net_device *dev = skb->dev;
5b641ebe 51 int res;
52
6001d522
AA
53 rtnl_lock();
54
55 /* check if ifdown occurred while schedule */
409c3b0c 56 if (!netif_running(dev))
6001d522
AA
57 goto err_tx;
58
59cb300f 59 res = drv_xmit_sync(local, skb);
6001d522
AA
60 if (res)
61 goto err_tx;
62
61f2dcba 63 ieee802154_xmit_complete(&local->hw, skb, false);
6001d522 64
409c3b0c
AA
65 dev->stats.tx_packets++;
66 dev->stats.tx_bytes += skb->len;
67
6001d522
AA
68 rtnl_unlock();
69
70 return;
71
72err_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);
409c3b0c 77 netdev_dbg(dev, "transmission failed\n");
5b641ebe 78}
79
dc67c6b3 80static netdev_tx_t
e5e584fc 81ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
5b641ebe 82{
409c3b0c 83 struct net_device *dev = skb->dev;
ed0a5dce 84 int ret;
5b641ebe 85
90386a7e 86 if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) {
061ef8f9 87 u16 crc = crc_ccitt(0, skb->data, skb->len);
4710d806 88
061ef8f9 89 put_unaligned_le16(crc, skb_put(skb, 2));
5b641ebe 90 }
91
a5e1ec53 92 if (skb_cow_head(skb, local->hw.extra_tx_headroom))
f5588912 93 goto err_tx;
5b641ebe 94
b5992fe9 95 /* Stop the netif queue on each sub_if_data object. */
18d60a0d 96 ieee802154_stop_queue(&local->hw);
b5992fe9 97
ed0a5dce
AA
98 /* async is priority, otherwise sync is fallback */
99 if (local->ops->xmit_async) {
59cb300f 100 ret = drv_xmit_async(local, skb);
ed0a5dce
AA
101 if (ret) {
102 ieee802154_wake_queue(&local->hw);
103 goto err_tx;
104 }
409c3b0c
AA
105
106 dev->stats.tx_packets++;
107 dev->stats.tx_bytes += skb->len;
ed0a5dce 108 } else {
f81f466c
AA
109 INIT_WORK(&ieee802154_xmit_cb.work, ieee802154_xmit_worker);
110 ieee802154_xmit_cb.skb = skb;
111 ieee802154_xmit_cb.local = local;
5b641ebe 112
f81f466c 113 queue_work(local->workqueue, &ieee802154_xmit_cb.work);
ed0a5dce 114 }
5b641ebe 115
116 return NETDEV_TX_OK;
f5588912
VB
117
118err_tx:
119 kfree_skb(skb);
120 return NETDEV_TX_OK;
5b641ebe 121}
50c6fb99 122
e5e584fc
AA
123netdev_tx_t
124ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev)
50c6fb99
AA
125{
126 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
50c6fb99
AA
127
128 skb->skb_iif = dev->ifindex;
50c6fb99 129
e5e584fc 130 return ieee802154_tx(sdata->local, skb);
50c6fb99
AA
131}
132
e5e584fc
AA
133netdev_tx_t
134ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
50c6fb99
AA
135{
136 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
50c6fb99
AA
137 int rc;
138
50c6fb99
AA
139 rc = mac802154_llsec_encrypt(&sdata->sec, skb);
140 if (rc) {
cfa626cb 141 netdev_warn(dev, "encryption failed: %i\n", rc);
50c6fb99
AA
142 kfree_skb(skb);
143 return NETDEV_TX_OK;
144 }
145
146 skb->skb_iif = dev->ifindex;
50c6fb99 147
e5e584fc 148 return ieee802154_tx(sdata->local, skb);
50c6fb99 149}