mac802154: tx: make worker information static
[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/rtnetlink.h>
25 #include <net/ieee802154_netdev.h>
26 #include <net/mac802154.h>
27 #include <net/cfg802154.h>
28
29 #include "ieee802154_i.h"
30
31 /* IEEE 802.15.4 transceivers can sleep during the xmit session, so process
32  * packets through the workqueue.
33  */
34 struct ieee802154_xmit_cb {
35         struct sk_buff *skb;
36         struct work_struct work;
37         struct ieee802154_local *local;
38 };
39
40 static struct ieee802154_xmit_cb ieee802154_xmit_cb;
41
42 static void ieee802154_xmit_worker(struct work_struct *work)
43 {
44         struct ieee802154_xmit_cb *cb =
45                 container_of(work, struct ieee802154_xmit_cb, work);
46         struct ieee802154_local *local = cb->local;
47         struct sk_buff *skb = cb->skb;
48         struct net_device *dev = skb->dev;
49         int res;
50
51         rtnl_lock();
52
53         /* check if ifdown occurred while schedule */
54         if (!netif_running(dev))
55                 goto err_tx;
56
57         res = local->ops->xmit_sync(&local->hw, skb);
58         if (res)
59                 goto err_tx;
60
61         ieee802154_xmit_complete(&local->hw, skb);
62
63         dev->stats.tx_packets++;
64         dev->stats.tx_bytes += skb->len;
65
66         rtnl_unlock();
67
68         return;
69
70 err_tx:
71         /* Restart the netif queue on each sub_if_data object. */
72         ieee802154_wake_queue(&local->hw);
73         rtnl_unlock();
74         kfree_skb(skb);
75         netdev_dbg(dev, "transmission failed\n");
76 }
77
78 static netdev_tx_t
79 ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
80 {
81         struct net_device *dev = skb->dev;
82         int ret;
83
84         mac802154_monitors_rx(local, skb);
85
86         if (!(local->hw.flags & IEEE802154_HW_OMIT_CKSUM)) {
87                 __le16 crc = cpu_to_le16(crc_ccitt(0, skb->data, skb->len));
88
89                 memcpy(skb_put(skb, 2), &crc, 2);
90         }
91
92         if (skb_cow_head(skb, local->hw.extra_tx_headroom))
93                 goto err_tx;
94
95         /* Stop the netif queue on each sub_if_data object. */
96         ieee802154_stop_queue(&local->hw);
97
98         /* async is priority, otherwise sync is fallback */
99         if (local->ops->xmit_async) {
100                 ret = local->ops->xmit_async(&local->hw, skb);
101                 if (ret) {
102                         ieee802154_wake_queue(&local->hw);
103                         goto err_tx;
104                 }
105
106                 dev->stats.tx_packets++;
107                 dev->stats.tx_bytes += skb->len;
108         } else {
109                 INIT_WORK(&ieee802154_xmit_cb.work, ieee802154_xmit_worker);
110                 ieee802154_xmit_cb.skb = skb;
111                 ieee802154_xmit_cb.local = local;
112
113                 queue_work(local->workqueue, &ieee802154_xmit_cb.work);
114         }
115
116         return NETDEV_TX_OK;
117
118 err_tx:
119         kfree_skb(skb);
120         return NETDEV_TX_OK;
121 }
122
123 netdev_tx_t
124 ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev)
125 {
126         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
127
128         skb->skb_iif = dev->ifindex;
129
130         return ieee802154_tx(sdata->local, skb);
131 }
132
133 netdev_tx_t
134 ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
135 {
136         struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
137         int rc;
138
139         rc = mac802154_llsec_encrypt(&sdata->sec, skb);
140         if (rc) {
141                 netdev_warn(dev, "encryption failed: %i\n", rc);
142                 kfree_skb(skb);
143                 return NETDEV_TX_OK;
144         }
145
146         skb->skb_iif = dev->ifindex;
147
148         return ieee802154_tx(sdata->local, skb);
149 }