xref: /linux/net/mac802154/main.c (revision 86f86e6fdedc33c5b8d38b9208b350f8accf57ee)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2007-2012 Siemens AG
4  *
5  * Written by:
6  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 
13 #include <net/netlink.h>
14 #include <net/nl802154.h>
15 #include <net/mac802154.h>
16 #include <net/ieee802154_netdev.h>
17 #include <net/route.h>
18 #include <net/cfg802154.h>
19 
20 #include "ieee802154_i.h"
21 #include "cfg.h"
22 
23 static void ieee802154_tasklet_handler(struct tasklet_struct *t)
24 {
25 	struct ieee802154_local *local = from_tasklet(local, t, tasklet);
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;
35 			ieee802154_rx(local, skb);
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 
46 struct ieee802154_hw *
47 ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
48 {
49 	struct wpan_phy *phy;
50 	struct ieee802154_local *local;
51 	size_t priv_size;
52 
53 	if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
54 		    !ops->start || !ops->stop || !ops->set_channel))
55 		return NULL;
56 
57 	/* Ensure 32-byte alignment of our private data and hw private data.
58 	 * We use the wpan_phy priv data for both our ieee802154_local and for
59 	 * the driver's private data
60 	 *
61 	 * in memory it'll be like this:
62 	 *
63 	 * +-------------------------+
64 	 * | struct wpan_phy         |
65 	 * +-------------------------+
66 	 * | struct ieee802154_local |
67 	 * +-------------------------+
68 	 * | driver's private data   |
69 	 * +-------------------------+
70 	 *
71 	 * Due to ieee802154 layer isn't aware of driver and MAC structures,
72 	 * so lets align them here.
73 	 */
74 
75 	priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
76 
77 	phy = wpan_phy_new(&mac802154_config_ops, priv_size);
78 	if (!phy) {
79 		pr_err("failure to allocate master IEEE802.15.4 device\n");
80 		return NULL;
81 	}
82 
83 	phy->privid = mac802154_wpan_phy_privid;
84 
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;
90 
91 	INIT_LIST_HEAD(&local->interfaces);
92 	INIT_LIST_HEAD(&local->rx_beacon_list);
93 	INIT_LIST_HEAD(&local->rx_mac_cmd_list);
94 	spin_lock_init(&local->rx_lock);
95 	mutex_init(&local->iflist_mtx);
96 
97 	tasklet_setup(&local->tasklet, ieee802154_tasklet_handler);
98 
99 	skb_queue_head_init(&local->skb_queue);
100 
101 	INIT_WORK(&local->sync_tx_work, ieee802154_xmit_sync_worker);
102 	INIT_DELAYED_WORK(&local->scan_work, mac802154_scan_worker);
103 	INIT_WORK(&local->rx_beacon_work, mac802154_rx_beacon_worker);
104 	INIT_DELAYED_WORK(&local->beacon_work, mac802154_beacon_worker);
105 	INIT_WORK(&local->rx_mac_cmd_work, mac802154_rx_mac_cmd_worker);
106 
107 	init_completion(&local->assoc_done);
108 	spin_lock_init(&local->assoc_lock);
109 
110 	/* init supported flags with 802.15.4 default ranges */
111 	phy->supported.max_minbe = 8;
112 	phy->supported.min_maxbe = 3;
113 	phy->supported.max_maxbe = 8;
114 	phy->supported.min_frame_retries = 0;
115 	phy->supported.max_frame_retries = 7;
116 	phy->supported.max_csma_backoffs = 5;
117 	phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
118 
119 	/* always supported */
120 	phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE) | BIT(NL802154_IFTYPE_COORD);
121 
122 	return &local->hw;
123 }
124 EXPORT_SYMBOL(ieee802154_alloc_hw);
125 
126 void ieee802154_configure_durations(struct wpan_phy *phy,
127 				    unsigned int page, unsigned int channel)
128 {
129 	u32 duration = 0;
130 
131 	switch (page) {
132 	case 0:
133 		if (BIT(channel) & 0x1)
134 			/* 868 MHz BPSK 802.15.4-2003: 20 ksym/s */
135 			duration = 50 * NSEC_PER_USEC;
136 		else if (BIT(channel) & 0x7FE)
137 			/* 915 MHz BPSK	802.15.4-2003: 40 ksym/s */
138 			duration = 25 * NSEC_PER_USEC;
139 		else if (BIT(channel) & 0x7FFF800)
140 			/* 2400 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */
141 			duration = 16 * NSEC_PER_USEC;
142 		break;
143 	case 2:
144 		if (BIT(channel) & 0x1)
145 			/* 868 MHz O-QPSK 802.15.4-2006: 25 ksym/s */
146 			duration = 40 * NSEC_PER_USEC;
147 		else if (BIT(channel) & 0x7FE)
148 			/* 915 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */
149 			duration = 16 * NSEC_PER_USEC;
150 		break;
151 	case 3:
152 		if (BIT(channel) & 0x3FFF)
153 			/* 2.4 GHz CSS 802.15.4a-2007: 1/6 Msym/s */
154 			duration = 6 * NSEC_PER_USEC;
155 		break;
156 	default:
157 		break;
158 	}
159 
160 	if (!duration) {
161 		pr_debug("Unknown PHY symbol duration\n");
162 		return;
163 	}
164 
165 	phy->symbol_duration = duration;
166 	phy->lifs_period =
167 		(IEEE802154_LIFS_PERIOD * phy->symbol_duration) / NSEC_PER_USEC;
168 	phy->sifs_period =
169 		(IEEE802154_SIFS_PERIOD * phy->symbol_duration) / NSEC_PER_USEC;
170 }
171 EXPORT_SYMBOL(ieee802154_configure_durations);
172 
173 void ieee802154_free_hw(struct ieee802154_hw *hw)
174 {
175 	struct ieee802154_local *local = hw_to_local(hw);
176 
177 	BUG_ON(!list_empty(&local->interfaces));
178 
179 	mutex_destroy(&local->iflist_mtx);
180 
181 	wpan_phy_free(local->phy);
182 }
183 EXPORT_SYMBOL(ieee802154_free_hw);
184 
185 static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
186 {
187 	/* TODO warn on empty symbol_duration
188 	 * Should be done when all drivers sets this value.
189 	 */
190 
191 	wpan_phy->lifs_period =	(IEEE802154_LIFS_PERIOD *
192 				 wpan_phy->symbol_duration) / NSEC_PER_USEC;
193 	wpan_phy->sifs_period =	(IEEE802154_SIFS_PERIOD *
194 				 wpan_phy->symbol_duration) / NSEC_PER_USEC;
195 }
196 
197 int ieee802154_register_hw(struct ieee802154_hw *hw)
198 {
199 	struct ieee802154_local *local = hw_to_local(hw);
200 	char mac_wq_name[IFNAMSIZ + 10] = {};
201 	struct net_device *dev;
202 	int rc = -ENOSYS;
203 
204 	local->workqueue =
205 		create_singlethread_workqueue(wpan_phy_name(local->phy));
206 	if (!local->workqueue) {
207 		rc = -ENOMEM;
208 		goto out;
209 	}
210 
211 	snprintf(mac_wq_name, IFNAMSIZ + 10, "%s-mac-cmds", wpan_phy_name(local->phy));
212 	local->mac_wq =	create_singlethread_workqueue(mac_wq_name);
213 	if (!local->mac_wq) {
214 		rc = -ENOMEM;
215 		goto out_wq;
216 	}
217 
218 	hrtimer_setup(&local->ifs_timer, ieee802154_xmit_ifs_timer, CLOCK_MONOTONIC,
219 		      HRTIMER_MODE_REL);
220 
221 	wpan_phy_set_dev(local->phy, local->hw.parent);
222 
223 	ieee802154_setup_wpan_phy_pib(local->phy);
224 
225 	ieee802154_configure_durations(local->phy, local->phy->current_page,
226 				       local->phy->current_channel);
227 
228 	if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
229 		local->phy->supported.min_csma_backoffs = 4;
230 		local->phy->supported.max_csma_backoffs = 4;
231 		local->phy->supported.min_maxbe = 5;
232 		local->phy->supported.max_maxbe = 5;
233 		local->phy->supported.min_minbe = 3;
234 		local->phy->supported.max_minbe = 3;
235 	}
236 
237 	if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
238 		local->phy->supported.min_frame_retries = 3;
239 		local->phy->supported.max_frame_retries = 3;
240 	}
241 
242 	if (hw->flags & IEEE802154_HW_PROMISCUOUS)
243 		local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
244 
245 	rc = wpan_phy_register(local->phy);
246 	if (rc < 0)
247 		goto out_mac_wq;
248 
249 	rtnl_lock();
250 
251 	dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
252 				NL802154_IFTYPE_NODE,
253 				cpu_to_le64(0x0000000000000000ULL));
254 	if (IS_ERR(dev)) {
255 		rtnl_unlock();
256 		rc = PTR_ERR(dev);
257 		goto out_phy;
258 	}
259 
260 	rtnl_unlock();
261 
262 	return 0;
263 
264 out_phy:
265 	wpan_phy_unregister(local->phy);
266 out_mac_wq:
267 	destroy_workqueue(local->mac_wq);
268 out_wq:
269 	destroy_workqueue(local->workqueue);
270 out:
271 	return rc;
272 }
273 EXPORT_SYMBOL(ieee802154_register_hw);
274 
275 void ieee802154_unregister_hw(struct ieee802154_hw *hw)
276 {
277 	struct ieee802154_local *local = hw_to_local(hw);
278 
279 	tasklet_kill(&local->tasklet);
280 	flush_workqueue(local->workqueue);
281 
282 	rtnl_lock();
283 
284 	ieee802154_remove_interfaces(local);
285 
286 	rtnl_unlock();
287 
288 	destroy_workqueue(local->mac_wq);
289 	destroy_workqueue(local->workqueue);
290 	wpan_phy_unregister(local->phy);
291 }
292 EXPORT_SYMBOL(ieee802154_unregister_hw);
293 
294 static int __init ieee802154_init(void)
295 {
296 	return ieee802154_iface_init();
297 }
298 
299 static void __exit ieee802154_exit(void)
300 {
301 	ieee802154_iface_exit();
302 
303 	rcu_barrier();
304 }
305 
306 subsys_initcall(ieee802154_init);
307 module_exit(ieee802154_exit);
308 
309 MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
310 MODULE_LICENSE("GPL v2");
311