blob: c62e95695c7843947c8643cb268f95f2e64c3da9 [file] [log] [blame]
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +00001/*
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 *
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000013 * 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>
Alexander Aring061ef8f2014-10-27 17:13:28 +010023#include <asm/unaligned.h>
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000024
Alexander Aring6001d522014-10-26 09:37:09 +010025#include <net/rtnetlink.h>
Alan Ottb5992fe2013-04-03 04:00:56 +000026#include <net/ieee802154_netdev.h>
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000027#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020028#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000029
Alexander Aring0f1556b2014-10-25 09:41:00 +020030#include "ieee802154_i.h"
Alexander Aring59cb3002014-10-28 18:21:21 +010031#include "driver-ops.h"
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000032
33/* IEEE 802.15.4 transceivers can sleep during the xmit session, so process
34 * packets through the workqueue.
35 */
Alexander Aringe5e584f2014-10-26 09:37:13 +010036struct ieee802154_xmit_cb {
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000037 struct sk_buff *skb;
38 struct work_struct work;
Alexander Aringa5e1ec52014-10-25 17:16:35 +020039 struct ieee802154_local *local;
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000040};
41
Alexander Aringf81f4662014-10-26 18:15:34 +010042static struct ieee802154_xmit_cb ieee802154_xmit_cb;
Alexander Aringfe243712014-10-26 09:37:02 +010043
Alexander Aringe5e584f2014-10-26 09:37:13 +010044static void ieee802154_xmit_worker(struct work_struct *work)
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000045{
Alexander Aringe5e584f2014-10-26 09:37:13 +010046 struct ieee802154_xmit_cb *cb =
47 container_of(work, struct ieee802154_xmit_cb, work);
Alexander Aringe89e45f2014-10-26 09:37:03 +010048 struct ieee802154_local *local = cb->local;
Alexander Aringe89e45f2014-10-26 09:37:03 +010049 struct sk_buff *skb = cb->skb;
Alexander Aring409c3b02014-10-26 09:37:12 +010050 struct net_device *dev = skb->dev;
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000051 int res;
52
Alexander Aring6001d522014-10-26 09:37:09 +010053 rtnl_lock();
54
55 /* check if ifdown occurred while schedule */
Alexander Aring409c3b02014-10-26 09:37:12 +010056 if (!netif_running(dev))
Alexander Aring6001d522014-10-26 09:37:09 +010057 goto err_tx;
58
Alexander Aring59cb3002014-10-28 18:21:21 +010059 res = drv_xmit_sync(local, skb);
Alexander Aring6001d522014-10-26 09:37:09 +010060 if (res)
61 goto err_tx;
62
Alexander Aring61f2dcb2014-11-12 19:51:56 +010063 ieee802154_xmit_complete(&local->hw, skb, false);
Alexander Aring6001d522014-10-26 09:37:09 +010064
Alexander Aring409c3b02014-10-26 09:37:12 +010065 dev->stats.tx_packets++;
66 dev->stats.tx_bytes += skb->len;
67
Alexander Aring6001d522014-10-26 09:37:09 +010068 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);
Alexander Aring409c3b02014-10-26 09:37:12 +010077 netdev_dbg(dev, "transmission failed\n");
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000078}
79
Alexander Aringdc67c6b2014-10-26 09:37:04 +010080static netdev_tx_t
Alexander Aringe5e584f2014-10-26 09:37:13 +010081ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000082{
Alexander Aring409c3b02014-10-26 09:37:12 +010083 struct net_device *dev = skb->dev;
Alexander Aringed0a5dc2014-10-26 09:37:08 +010084 int ret;
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000085
Alexander Aring90386a72014-10-29 21:34:34 +010086 if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) {
Alexander Aring061ef8f2014-10-27 17:13:28 +010087 u16 crc = crc_ccitt(0, skb->data, skb->len);
Varka Bhadram4710d802014-07-02 09:01:09 +053088
Alexander Aring061ef8f2014-10-27 17:13:28 +010089 put_unaligned_le16(crc, skb_put(skb, 2));
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000090 }
91
Alexander Aringa5e1ec52014-10-25 17:16:35 +020092 if (skb_cow_head(skb, local->hw.extra_tx_headroom))
Varka Bhadramf5588912014-08-11 13:25:10 +020093 goto err_tx;
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000094
Alan Ottb5992fe2013-04-03 04:00:56 +000095 /* Stop the netif queue on each sub_if_data object. */
Alexander Aring18d60a02014-10-26 09:37:06 +010096 ieee802154_stop_queue(&local->hw);
Alan Ottb5992fe2013-04-03 04:00:56 +000097
Alexander Aringed0a5dc2014-10-26 09:37:08 +010098 /* async is priority, otherwise sync is fallback */
99 if (local->ops->xmit_async) {
Alexander Aring59cb3002014-10-28 18:21:21 +0100100 ret = drv_xmit_async(local, skb);
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100101 if (ret) {
102 ieee802154_wake_queue(&local->hw);
103 goto err_tx;
104 }
Alexander Aring409c3b02014-10-26 09:37:12 +0100105
106 dev->stats.tx_packets++;
107 dev->stats.tx_bytes += skb->len;
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100108 } else {
Alexander Aringf81f4662014-10-26 18:15:34 +0100109 INIT_WORK(&ieee802154_xmit_cb.work, ieee802154_xmit_worker);
110 ieee802154_xmit_cb.skb = skb;
111 ieee802154_xmit_cb.local = local;
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +0000112
Alexander Aringf81f4662014-10-26 18:15:34 +0100113 queue_work(local->workqueue, &ieee802154_xmit_cb.work);
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100114 }
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +0000115
116 return NETDEV_TX_OK;
Varka Bhadramf5588912014-08-11 13:25:10 +0200117
118err_tx:
119 kfree_skb(skb);
120 return NETDEV_TX_OK;
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +0000121}
Alexander Aring50c6fb92014-10-26 09:37:01 +0100122
Alexander Aringe5e584f2014-10-26 09:37:13 +0100123netdev_tx_t
124ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev)
Alexander Aring50c6fb92014-10-26 09:37:01 +0100125{
126 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100127
128 skb->skb_iif = dev->ifindex;
Alexander Aring50c6fb92014-10-26 09:37:01 +0100129
Alexander Aringe5e584f2014-10-26 09:37:13 +0100130 return ieee802154_tx(sdata->local, skb);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100131}
132
Alexander Aringe5e584f2014-10-26 09:37:13 +0100133netdev_tx_t
134ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
Alexander Aring50c6fb92014-10-26 09:37:01 +0100135{
136 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100137 int rc;
138
Alexander Aring50c6fb92014-10-26 09:37:01 +0100139 rc = mac802154_llsec_encrypt(&sdata->sec, skb);
140 if (rc) {
Alexander Aringcfa626c2014-10-26 09:37:10 +0100141 netdev_warn(dev, "encryption failed: %i\n", rc);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100142 kfree_skb(skb);
143 return NETDEV_TX_OK;
144 }
145
146 skb->skb_iif = dev->ifindex;
Alexander Aring50c6fb92014-10-26 09:37:01 +0100147
Alexander Aringe5e584f2014-10-26 09:37:13 +0100148 return ieee802154_tx(sdata->local, skb);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100149}