mac802154: Use netif flow control
[firefly-linux-kernel-4.4.55.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  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Written by:
18  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
19  * Sergey Lapin <slapin@ossfans.org>
20  * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
21  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
22  */
23
24 #include <linux/netdevice.h>
25 #include <linux/if_arp.h>
26 #include <linux/crc-ccitt.h>
27
28 #include <net/ieee802154_netdev.h>
29 #include <net/mac802154.h>
30 #include <net/wpan-phy.h>
31
32 #include "mac802154.h"
33
34 /* IEEE 802.15.4 transceivers can sleep during the xmit session, so process
35  * packets through the workqueue.
36  */
37 struct xmit_work {
38         struct sk_buff *skb;
39         struct work_struct work;
40         struct mac802154_priv *priv;
41         u8 chan;
42         u8 page;
43 };
44
45 static void mac802154_xmit_worker(struct work_struct *work)
46 {
47         struct xmit_work *xw = container_of(work, struct xmit_work, work);
48         struct mac802154_sub_if_data *sdata;
49         int res;
50
51         mutex_lock(&xw->priv->phy->pib_lock);
52         if (xw->priv->phy->current_channel != xw->chan ||
53             xw->priv->phy->current_page != xw->page) {
54                 res = xw->priv->ops->set_channel(&xw->priv->hw,
55                                                   xw->page,
56                                                   xw->chan);
57                 if (res) {
58                         pr_debug("set_channel failed\n");
59                         goto out;
60                 }
61         }
62
63         res = xw->priv->ops->xmit(&xw->priv->hw, xw->skb);
64         if (res)
65                 pr_debug("transmission failed\n");
66
67 out:
68         mutex_unlock(&xw->priv->phy->pib_lock);
69
70         /* Restart the netif queue on each sub_if_data object. */
71         rcu_read_lock();
72         list_for_each_entry_rcu(sdata, &xw->priv->slaves, list)
73                 netif_wake_queue(sdata->dev);
74         rcu_read_unlock();
75
76         dev_kfree_skb(xw->skb);
77
78         kfree(xw);
79 }
80
81 netdev_tx_t mac802154_tx(struct mac802154_priv *priv, struct sk_buff *skb,
82                          u8 page, u8 chan)
83 {
84         struct xmit_work *work;
85         struct mac802154_sub_if_data *sdata;
86
87         if (!(priv->phy->channels_supported[page] & (1 << chan))) {
88                 WARN_ON(1);
89                 kfree_skb(skb);
90                 return NETDEV_TX_OK;
91         }
92
93         mac802154_monitors_rx(mac802154_to_priv(&priv->hw), skb);
94
95         if (!(priv->hw.flags & IEEE802154_HW_OMIT_CKSUM)) {
96                 u16 crc = crc_ccitt(0, skb->data, skb->len);
97                 u8 *data = skb_put(skb, 2);
98                 data[0] = crc & 0xff;
99                 data[1] = crc >> 8;
100         }
101
102         if (skb_cow_head(skb, priv->hw.extra_tx_headroom)) {
103                 kfree_skb(skb);
104                 return NETDEV_TX_OK;
105         }
106
107         work = kzalloc(sizeof(struct xmit_work), GFP_ATOMIC);
108         if (!work) {
109                 kfree_skb(skb);
110                 return NETDEV_TX_BUSY;
111         }
112
113         /* Stop the netif queue on each sub_if_data object. */
114         rcu_read_lock();
115         list_for_each_entry_rcu(sdata, &priv->slaves, list)
116                 netif_stop_queue(sdata->dev);
117         rcu_read_unlock();
118
119         INIT_WORK(&work->work, mac802154_xmit_worker);
120         work->skb = skb;
121         work->priv = priv;
122         work->page = page;
123         work->chan = chan;
124
125         queue_work(priv->dev_workqueue, &work->work);
126
127         return NETDEV_TX_OK;
128 }