tx.c 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2007-2012 Siemens AG
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Written by:
 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
 * Sergey Lapin <slapin@ossfans.org>
 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
 */

#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/crc-ccitt.h>

A
Alan Ott 已提交
24
#include <net/ieee802154_netdev.h>
25
#include <net/mac802154.h>
26
#include <net/cfg802154.h>
27

28
#include "ieee802154_i.h"
29 30 31 32 33 34 35

/* IEEE 802.15.4 transceivers can sleep during the xmit session, so process
 * packets through the workqueue.
 */
struct xmit_work {
	struct sk_buff *skb;
	struct work_struct work;
36
	struct ieee802154_local *local;
37 38 39 40 41 42 43
	u8 chan;
	u8 page;
};

static void mac802154_xmit_worker(struct work_struct *work)
{
	struct xmit_work *xw = container_of(work, struct xmit_work, work);
44
	struct ieee802154_sub_if_data *sdata;
45 46
	int res;

47 48 49 50
	mutex_lock(&xw->local->phy->pib_lock);
	if (xw->local->phy->current_channel != xw->chan ||
	    xw->local->phy->current_page != xw->page) {
		res = xw->local->ops->set_channel(&xw->local->hw,
51 52 53 54 55 56
						  xw->page,
						  xw->chan);
		if (res) {
			pr_debug("set_channel failed\n");
			goto out;
		}
57

58 59
		xw->local->phy->current_channel = xw->chan;
		xw->local->phy->current_page = xw->page;
60 61
	}

62
	res = xw->local->ops->xmit(&xw->local->hw, xw->skb);
63 64
	if (res)
		pr_debug("transmission failed\n");
65 66

out:
67
	mutex_unlock(&xw->local->phy->pib_lock);
68

A
Alan Ott 已提交
69 70
	/* Restart the netif queue on each sub_if_data object. */
	rcu_read_lock();
71
	list_for_each_entry_rcu(sdata, &xw->local->interfaces, list)
A
Alan Ott 已提交
72 73
		netif_wake_queue(sdata->dev);
	rcu_read_unlock();
74 75 76 77 78 79

	dev_kfree_skb(xw->skb);

	kfree(xw);
}

80 81
static netdev_tx_t mac802154_tx(struct ieee802154_local *local,
				struct sk_buff *skb, u8 page, u8 chan)
82 83
{
	struct xmit_work *work;
84
	struct ieee802154_sub_if_data *sdata;
85

86
	if (!(local->phy->channels_supported[page] & (1 << chan))) {
87
		WARN_ON(1);
V
Varka Bhadram 已提交
88
		goto err_tx;
89
	}
90

91
	mac802154_monitors_rx(local, skb);
92

93
	if (!(local->hw.flags & IEEE802154_HW_OMIT_CKSUM)) {
94 95
		u16 crc = crc_ccitt(0, skb->data, skb->len);
		u8 *data = skb_put(skb, 2);
96

97 98 99 100
		data[0] = crc & 0xff;
		data[1] = crc >> 8;
	}

101
	if (skb_cow_head(skb, local->hw.extra_tx_headroom))
V
Varka Bhadram 已提交
102
		goto err_tx;
103

V
Varka Bhadram 已提交
104
	work = kzalloc(sizeof(*work), GFP_ATOMIC);
A
Alan Ott 已提交
105 106
	if (!work) {
		kfree_skb(skb);
107
		return NETDEV_TX_BUSY;
A
Alan Ott 已提交
108
	}
109

A
Alan Ott 已提交
110 111
	/* Stop the netif queue on each sub_if_data object. */
	rcu_read_lock();
112
	list_for_each_entry_rcu(sdata, &local->interfaces, list)
A
Alan Ott 已提交
113 114 115
		netif_stop_queue(sdata->dev);
	rcu_read_unlock();

116 117
	INIT_WORK(&work->work, mac802154_xmit_worker);
	work->skb = skb;
118
	work->local = local;
119 120 121
	work->page = page;
	work->chan = chan;

122
	queue_work(local->workqueue, &work->work);
123 124

	return NETDEV_TX_OK;
V
Varka Bhadram 已提交
125 126 127 128

err_tx:
	kfree_skb(skb);
	return NETDEV_TX_OK;
129
}
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

netdev_tx_t mac802154_monitor_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
	u8 chan, page;

	/* FIXME: locking */
	chan = sdata->local->phy->current_channel;
	page = sdata->local->phy->current_page;

	if (chan == MAC802154_CHAN_NONE) /* not initialized */
		return NETDEV_TX_OK;

	if (WARN_ON(page >= WPAN_NUM_PAGES) ||
	    WARN_ON(chan >= WPAN_NUM_CHANNELS))
		return NETDEV_TX_OK;

	skb->skb_iif = dev->ifindex;
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;

	return mac802154_tx(sdata->local, skb, page, chan);
}

netdev_tx_t mac802154_wpan_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
	u8 chan, page;
	int rc;

	spin_lock_bh(&sdata->mib_lock);
	chan = sdata->chan;
	page = sdata->page;
	spin_unlock_bh(&sdata->mib_lock);

	if (chan == MAC802154_CHAN_NONE ||
	    page >= WPAN_NUM_PAGES ||
	    chan >= WPAN_NUM_CHANNELS) {
		kfree_skb(skb);
		return NETDEV_TX_OK;
	}

	rc = mac802154_llsec_encrypt(&sdata->sec, skb);
	if (rc) {
		pr_warn("encryption failed: %i\n", rc);
		kfree_skb(skb);
		return NETDEV_TX_OK;
	}

	skb->skb_iif = dev->ifindex;
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;

	return mac802154_tx(sdata->local, skb, page, chan);
}