locking/lockdep: Remove the unnecessary trace saving
[linux-block.git] / net / mac802154 / main.c
CommitLineData
1802d0be 1// SPDX-License-Identifier: GPL-2.0-only
1010f540 2/*
3 * Copyright (C) 2007-2012 Siemens AG
4 *
5 * Written by:
6 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
1010f540 7 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/netdevice.h>
12
62610ad2 13#include <net/netlink.h>
944742a3 14#include <net/nl802154.h>
1010f540 15#include <net/mac802154.h>
b70ab2e8 16#include <net/ieee802154_netdev.h>
1010f540 17#include <net/route.h>
5ad60d36 18#include <net/cfg802154.h>
1010f540 19
0f1556bc 20#include "ieee802154_i.h"
1201cd22 21#include "cfg.h"
1010f540 22
b5bd8b62 23static void ieee802154_tasklet_handler(struct tasklet_struct *t)
c5c47e67 24{
b5bd8b62 25 struct ieee802154_local *local = from_tasklet(local, t, tasklet);
c5c47e67
AA
26 struct sk_buff *skb;
27
28 while ((skb = skb_dequeue(&local->skb_queue))) {
29 switch (skb->pkt_type) {
30 case IEEE802154_RX_MSG:
31 /* Clear skb->pkt_type in order to not confuse kernel
32 * netstack.
33 */
34 skb->pkt_type = 0;
d10270ce 35 ieee802154_rx(local, skb);
c5c47e67
AA
36 break;
37 default:
38 WARN(1, "mac802154: Packet is of unknown type %d\n",
39 skb->pkt_type);
40 kfree_skb(skb);
41 break;
42 }
43 }
44}
45
5a504397 46struct ieee802154_hw *
16301861 47ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
1010f540 48{
49 struct wpan_phy *phy;
a5e1ec53 50 struct ieee802154_local *local;
1010f540 51 size_t priv_size;
52
8f451829
VB
53 if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
54 !ops->start || !ops->stop || !ops->set_channel))
1010f540 55 return NULL;
1010f540 56
57 /* Ensure 32-byte alignment of our private data and hw private data.
a5e1ec53 58 * We use the wpan_phy priv data for both our ieee802154_local and for
1010f540 59 * the driver's private data
60 *
61 * in memory it'll be like this:
62 *
a5e1ec53
AA
63 * +-------------------------+
64 * | struct wpan_phy |
65 * +-------------------------+
66 * | struct ieee802154_local |
67 * +-------------------------+
68 * | driver's private data |
69 * +-------------------------+
1010f540 70 *
71 * Due to ieee802154 layer isn't aware of driver and MAC structures,
139f14ad 72 * so lets align them here.
1010f540 73 */
74
a5e1ec53 75 priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
1010f540 76
f601379f 77 phy = wpan_phy_new(&mac802154_config_ops, priv_size);
1010f540 78 if (!phy) {
83a1a7ce 79 pr_err("failure to allocate master IEEE802.15.4 device\n");
1010f540 80 return NULL;
81 }
82
6322d50d
AA
83 phy->privid = mac802154_wpan_phy_privid;
84
a5e1ec53
AA
85 local = wpan_phy_priv(phy);
86 local->phy = phy;
87 local->hw.phy = local->phy;
88 local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
89 local->ops = ops;
1010f540 90
d98be45b
AA
91 INIT_LIST_HEAD(&local->interfaces);
92 mutex_init(&local->iflist_mtx);
1010f540 93
b5bd8b62 94 tasklet_setup(&local->tasklet, ieee802154_tasklet_handler);
c5c47e67
AA
95
96 skb_queue_head_init(&local->skb_queue);
97
c22ff7b4
LB
98 INIT_WORK(&local->tx_work, ieee802154_xmit_worker);
99
fea3318d
AA
100 /* init supported flags with 802.15.4 default ranges */
101 phy->supported.max_minbe = 8;
102 phy->supported.min_maxbe = 3;
103 phy->supported.max_maxbe = 8;
89c7d788 104 phy->supported.min_frame_retries = 0;
fea3318d
AA
105 phy->supported.max_frame_retries = 7;
106 phy->supported.max_csma_backoffs = 5;
107 phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
108
65318680
AA
109 /* always supported */
110 phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE);
111
a5e1ec53 112 return &local->hw;
1010f540 113}
5a504397 114EXPORT_SYMBOL(ieee802154_alloc_hw);
1010f540 115
5a504397 116void ieee802154_free_hw(struct ieee802154_hw *hw)
1010f540 117{
60741361 118 struct ieee802154_local *local = hw_to_local(hw);
1010f540 119
d98be45b 120 BUG_ON(!list_empty(&local->interfaces));
62610ad2 121
d98be45b 122 mutex_destroy(&local->iflist_mtx);
1e9f9545 123
a5e1ec53 124 wpan_phy_free(local->phy);
1010f540 125}
5a504397 126EXPORT_SYMBOL(ieee802154_free_hw);
1010f540 127
61f2dcba
AA
128static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
129{
130 /* TODO warn on empty symbol_duration
131 * Should be done when all drivers sets this value.
132 */
133
134 wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD *
135 wpan_phy->symbol_duration;
136 wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD *
137 wpan_phy->symbol_duration;
138}
139
5a504397 140int ieee802154_register_hw(struct ieee802154_hw *hw)
1010f540 141{
60741361 142 struct ieee802154_local *local = hw_to_local(hw);
e4962a14 143 struct net_device *dev;
640985ec
AA
144 int rc = -ENOSYS;
145
f7730542 146 local->workqueue =
a5e1ec53 147 create_singlethread_workqueue(wpan_phy_name(local->phy));
f7730542 148 if (!local->workqueue) {
640985ec 149 rc = -ENOMEM;
1010f540 150 goto out;
640985ec 151 }
1010f540 152
61f2dcba
AA
153 hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
154 local->ifs_timer.function = ieee802154_xmit_ifs_timer;
155
a5e1ec53 156 wpan_phy_set_dev(local->phy, local->hw.parent);
1010f540 157
61f2dcba
AA
158 ieee802154_setup_wpan_phy_pib(local->phy);
159
fea3318d
AA
160 if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
161 local->phy->supported.min_csma_backoffs = 4;
162 local->phy->supported.max_csma_backoffs = 4;
163 local->phy->supported.min_maxbe = 5;
164 local->phy->supported.max_maxbe = 5;
165 local->phy->supported.min_minbe = 3;
166 local->phy->supported.max_minbe = 3;
167 }
168
169 if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
89c7d788
AA
170 local->phy->supported.min_frame_retries = 3;
171 local->phy->supported.max_frame_retries = 3;
fea3318d
AA
172 }
173
65318680
AA
174 if (hw->flags & IEEE802154_HW_PROMISCUOUS)
175 local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
176
a5e1ec53 177 rc = wpan_phy_register(local->phy);
1010f540 178 if (rc < 0)
179 goto out_wq;
180
e4962a14
AA
181 rtnl_lock();
182
5b4a1039
VB
183 dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
184 NL802154_IFTYPE_NODE,
0e57547e 185 cpu_to_le64(0x0000000000000000ULL));
e4962a14
AA
186 if (IS_ERR(dev)) {
187 rtnl_unlock();
188 rc = PTR_ERR(dev);
2b4d413c 189 goto out_phy;
e4962a14
AA
190 }
191
192 rtnl_unlock();
193
1010f540 194 return 0;
195
2b4d413c
AA
196out_phy:
197 wpan_phy_unregister(local->phy);
1010f540 198out_wq:
f7730542 199 destroy_workqueue(local->workqueue);
1010f540 200out:
201 return rc;
202}
5a504397 203EXPORT_SYMBOL(ieee802154_register_hw);
1010f540 204
5a504397 205void ieee802154_unregister_hw(struct ieee802154_hw *hw)
1010f540 206{
60741361 207 struct ieee802154_local *local = hw_to_local(hw);
1010f540 208
c5c47e67 209 tasklet_kill(&local->tasklet);
f7730542 210 flush_workqueue(local->workqueue);
1010f540 211
212 rtnl_lock();
213
592dfbfc 214 ieee802154_remove_interfaces(local);
62610ad2 215
1010f540 216 rtnl_unlock();
217
aef00c15 218 destroy_workqueue(local->workqueue);
a5e1ec53 219 wpan_phy_unregister(local->phy);
1010f540 220}
5a504397 221EXPORT_SYMBOL(ieee802154_unregister_hw);
1010f540 222
be4fd8e5
AA
223static int __init ieee802154_init(void)
224{
225 return ieee802154_iface_init();
226}
227
228static void __exit ieee802154_exit(void)
229{
230 ieee802154_iface_exit();
231
232 rcu_barrier();
233}
234
235subsys_initcall(ieee802154_init);
236module_exit(ieee802154_exit);
237
912f67ae 238MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
1010f540 239MODULE_LICENSE("GPL v2");