mac802154: tx: remove xmit channel context switch
[cascardo/linux.git] / net / mac802154 / tx.c
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
20 #include <linux/netdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/crc-ccitt.h>
23
24 #include <net/ieee802154_netdev.h>
25 #include <net/mac802154.h>
26 #include <net/cfg802154.h>
27
28 #include "ieee802154_i.h"
29
30 /* IEEE 802.15.4 transceivers can sleep during the xmit session, so process
31  * packets through the workqueue.
32  */
33 struct wpan_xmit_cb {
34         struct sk_buff *skb;
35         struct work_struct work;
36         struct ieee802154_local *local;
37 };
38
39 static inline struct wpan_xmit_cb *wpan_xmit_cb(const struct sk_buff *skb)
40 {
41         BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct wpan_xmit_cb));
42
43         return (struct wpan_xmit_cb *)skb->cb;
44 }
45
46 static void mac802154_xmit_worker(struct work_struct *work)
47 {
48         struct wpan_xmit_cb *cb = container_of(work, struct wpan_xmit_cb, work);
49         struct ieee802154_local *local = cb->local;
50         struct ieee802154_sub_if_data *sdata;
51         struct sk_buff *skb = cb->skb;
52         int res;
53
54         res = local->ops->xmit(&local->hw, skb);
55         if (res)
56                 pr_debug("transmission failed\n");
57
58         /* Restart the netif queue on each sub_if_data object. */
59         rcu_read_lock();
60         list_for_each_entry_rcu(sdata, &local->interfaces, list)
61                 netif_wake_queue(sdata->dev);
62         rcu_read_unlock();
63
64         dev_kfree_skb(skb);
65 }
66
67 static netdev_tx_t
68 mac802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
69 {
70         struct ieee802154_sub_if_data *sdata;
71         struct wpan_xmit_cb *cb = wpan_xmit_cb(skb);
72
73         mac802154_monitors_rx(local, skb);
74
75         if (!(local->hw.flags & IEEE802154_HW_OMIT_CKSUM)) {
76                 u16 crc = crc_ccitt(0, skb->data, skb->len);
77                 u8 *data = skb_put(skb, 2);
78
79                 data[0] = crc & 0xff;
80                 data[1] = crc >> 8;
81         }
82
83         if (skb_cow_head(skb, local->hw.extra_tx_headroom))
84                 goto err_tx;
85
86         /* Stop the netif queue on each sub_if_data object. */
87         rcu_read_lock();
88         list_for_each_entry_rcu(sdata, &local->interfaces, list)
89                 netif_stop_queue(sdata->dev);
90         rcu_read_unlock();
91
92         INIT_WORK(&cb->work, mac802154_xmit_worker);
93         cb->skb = skb;
94         cb->local = local;
95
96         queue_work(local->workqueue, &cb->work);
97
98         return NETDEV_TX_OK;
99
100 err_tx:
101         kfree_skb(skb);
102         return NETDEV_TX_OK;
103 }
104
105 netdev_tx_t mac802154_monitor_xmit(struct sk_buff *skb, struct net_device *dev)
106 {
107         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
108
109         skb->skb_iif = dev->ifindex;
110         dev->stats.tx_packets++;
111         dev->stats.tx_bytes += skb->len;
112
113         return mac802154_tx(sdata->local, skb);
114 }
115
116 netdev_tx_t mac802154_wpan_xmit(struct sk_buff *skb, struct net_device *dev)
117 {
118         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
119         int rc;
120
121         rc = mac802154_llsec_encrypt(&sdata->sec, skb);
122         if (rc) {
123                 pr_warn("encryption failed: %i\n", rc);
124                 kfree_skb(skb);
125                 return NETDEV_TX_OK;
126         }
127
128         skb->skb_iif = dev->ifindex;
129         dev->stats.tx_packets++;
130         dev->stats.tx_bytes += skb->len;
131
132         return mac802154_tx(sdata->local, skb);
133 }