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