Commit | Line | Data |
---|---|---|
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 | 23 | static 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 | 46 | struct ieee802154_hw * |
16301861 | 47 | ieee802154_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 | 91 | INIT_LIST_HEAD(&local->interfaces); |
57588c71 | 92 | INIT_LIST_HEAD(&local->rx_beacon_list); |
d021d218 | 93 | INIT_LIST_HEAD(&local->rx_mac_cmd_list); |
d98be45b | 94 | mutex_init(&local->iflist_mtx); |
1010f540 | 95 | |
b5bd8b62 | 96 | tasklet_setup(&local->tasklet, ieee802154_tasklet_handler); |
c5c47e67 AA |
97 | |
98 | skb_queue_head_init(&local->skb_queue); | |
99 | ||
983a974b | 100 | INIT_WORK(&local->sync_tx_work, ieee802154_xmit_sync_worker); |
57588c71 MR |
101 | INIT_DELAYED_WORK(&local->scan_work, mac802154_scan_worker); |
102 | INIT_WORK(&local->rx_beacon_work, mac802154_rx_beacon_worker); | |
3accf476 | 103 | INIT_DELAYED_WORK(&local->beacon_work, mac802154_beacon_worker); |
d021d218 | 104 | INIT_WORK(&local->rx_mac_cmd_work, mac802154_rx_mac_cmd_worker); |
c22ff7b4 | 105 | |
fefd1980 MR |
106 | init_completion(&local->assoc_done); |
107 | ||
fea3318d AA |
108 | /* init supported flags with 802.15.4 default ranges */ |
109 | phy->supported.max_minbe = 8; | |
110 | phy->supported.min_maxbe = 3; | |
111 | phy->supported.max_maxbe = 8; | |
89c7d788 | 112 | phy->supported.min_frame_retries = 0; |
fea3318d AA |
113 | phy->supported.max_frame_retries = 7; |
114 | phy->supported.max_csma_backoffs = 5; | |
115 | phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE; | |
116 | ||
65318680 | 117 | /* always supported */ |
2622e785 | 118 | phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE) | BIT(NL802154_IFTYPE_COORD); |
65318680 | 119 | |
a5e1ec53 | 120 | return &local->hw; |
1010f540 | 121 | } |
5a504397 | 122 | EXPORT_SYMBOL(ieee802154_alloc_hw); |
1010f540 | 123 | |
5755cd4d MR |
124 | void ieee802154_configure_durations(struct wpan_phy *phy, |
125 | unsigned int page, unsigned int channel) | |
781830c8 MR |
126 | { |
127 | u32 duration = 0; | |
128 | ||
5755cd4d | 129 | switch (page) { |
781830c8 | 130 | case 0: |
5755cd4d | 131 | if (BIT(channel) & 0x1) |
781830c8 MR |
132 | /* 868 MHz BPSK 802.15.4-2003: 20 ksym/s */ |
133 | duration = 50 * NSEC_PER_USEC; | |
5755cd4d | 134 | else if (BIT(channel) & 0x7FE) |
781830c8 MR |
135 | /* 915 MHz BPSK 802.15.4-2003: 40 ksym/s */ |
136 | duration = 25 * NSEC_PER_USEC; | |
5755cd4d | 137 | else if (BIT(channel) & 0x7FFF800) |
781830c8 MR |
138 | /* 2400 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */ |
139 | duration = 16 * NSEC_PER_USEC; | |
140 | break; | |
141 | case 2: | |
5755cd4d | 142 | if (BIT(channel) & 0x1) |
781830c8 MR |
143 | /* 868 MHz O-QPSK 802.15.4-2006: 25 ksym/s */ |
144 | duration = 40 * NSEC_PER_USEC; | |
5755cd4d | 145 | else if (BIT(channel) & 0x7FE) |
781830c8 MR |
146 | /* 915 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */ |
147 | duration = 16 * NSEC_PER_USEC; | |
148 | break; | |
149 | case 3: | |
5755cd4d | 150 | if (BIT(channel) & 0x3FFF) |
781830c8 MR |
151 | /* 2.4 GHz CSS 802.15.4a-2007: 1/6 Msym/s */ |
152 | duration = 6 * NSEC_PER_USEC; | |
153 | break; | |
154 | default: | |
155 | break; | |
156 | } | |
157 | ||
158 | if (!duration) { | |
159 | pr_debug("Unknown PHY symbol duration\n"); | |
160 | return; | |
161 | } | |
162 | ||
163 | phy->symbol_duration = duration; | |
07aa3398 DA |
164 | phy->lifs_period = |
165 | (IEEE802154_LIFS_PERIOD * phy->symbol_duration) / NSEC_PER_USEC; | |
166 | phy->sifs_period = | |
167 | (IEEE802154_SIFS_PERIOD * phy->symbol_duration) / NSEC_PER_USEC; | |
781830c8 MR |
168 | } |
169 | EXPORT_SYMBOL(ieee802154_configure_durations); | |
170 | ||
5a504397 | 171 | void ieee802154_free_hw(struct ieee802154_hw *hw) |
1010f540 | 172 | { |
60741361 | 173 | struct ieee802154_local *local = hw_to_local(hw); |
1010f540 | 174 | |
d98be45b | 175 | BUG_ON(!list_empty(&local->interfaces)); |
62610ad2 | 176 | |
d98be45b | 177 | mutex_destroy(&local->iflist_mtx); |
1e9f9545 | 178 | |
a5e1ec53 | 179 | wpan_phy_free(local->phy); |
1010f540 | 180 | } |
5a504397 | 181 | EXPORT_SYMBOL(ieee802154_free_hw); |
1010f540 | 182 | |
61f2dcba AA |
183 | static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy) |
184 | { | |
185 | /* TODO warn on empty symbol_duration | |
186 | * Should be done when all drivers sets this value. | |
187 | */ | |
188 | ||
07aa3398 DA |
189 | wpan_phy->lifs_period = (IEEE802154_LIFS_PERIOD * |
190 | wpan_phy->symbol_duration) / NSEC_PER_USEC; | |
191 | wpan_phy->sifs_period = (IEEE802154_SIFS_PERIOD * | |
192 | wpan_phy->symbol_duration) / NSEC_PER_USEC; | |
61f2dcba AA |
193 | } |
194 | ||
5a504397 | 195 | int ieee802154_register_hw(struct ieee802154_hw *hw) |
1010f540 | 196 | { |
60741361 | 197 | struct ieee802154_local *local = hw_to_local(hw); |
57588c71 | 198 | char mac_wq_name[IFNAMSIZ + 10] = {}; |
e4962a14 | 199 | struct net_device *dev; |
640985ec AA |
200 | int rc = -ENOSYS; |
201 | ||
f7730542 | 202 | local->workqueue = |
a5e1ec53 | 203 | create_singlethread_workqueue(wpan_phy_name(local->phy)); |
f7730542 | 204 | if (!local->workqueue) { |
640985ec | 205 | rc = -ENOMEM; |
1010f540 | 206 | goto out; |
640985ec | 207 | } |
1010f540 | 208 | |
57588c71 MR |
209 | snprintf(mac_wq_name, IFNAMSIZ + 10, "%s-mac-cmds", wpan_phy_name(local->phy)); |
210 | local->mac_wq = create_singlethread_workqueue(mac_wq_name); | |
211 | if (!local->mac_wq) { | |
212 | rc = -ENOMEM; | |
213 | goto out_wq; | |
214 | } | |
215 | ||
61f2dcba AA |
216 | hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
217 | local->ifs_timer.function = ieee802154_xmit_ifs_timer; | |
218 | ||
a5e1ec53 | 219 | wpan_phy_set_dev(local->phy, local->hw.parent); |
1010f540 | 220 | |
61f2dcba AA |
221 | ieee802154_setup_wpan_phy_pib(local->phy); |
222 | ||
5755cd4d MR |
223 | ieee802154_configure_durations(local->phy, local->phy->current_page, |
224 | local->phy->current_channel); | |
781830c8 | 225 | |
fea3318d AA |
226 | if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) { |
227 | local->phy->supported.min_csma_backoffs = 4; | |
228 | local->phy->supported.max_csma_backoffs = 4; | |
229 | local->phy->supported.min_maxbe = 5; | |
230 | local->phy->supported.max_maxbe = 5; | |
231 | local->phy->supported.min_minbe = 3; | |
232 | local->phy->supported.max_minbe = 3; | |
233 | } | |
234 | ||
235 | if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) { | |
89c7d788 AA |
236 | local->phy->supported.min_frame_retries = 3; |
237 | local->phy->supported.max_frame_retries = 3; | |
fea3318d AA |
238 | } |
239 | ||
65318680 AA |
240 | if (hw->flags & IEEE802154_HW_PROMISCUOUS) |
241 | local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR); | |
242 | ||
a5e1ec53 | 243 | rc = wpan_phy_register(local->phy); |
1010f540 | 244 | if (rc < 0) |
57588c71 | 245 | goto out_mac_wq; |
1010f540 | 246 | |
e4962a14 AA |
247 | rtnl_lock(); |
248 | ||
5b4a1039 VB |
249 | dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM, |
250 | NL802154_IFTYPE_NODE, | |
0e57547e | 251 | cpu_to_le64(0x0000000000000000ULL)); |
e4962a14 AA |
252 | if (IS_ERR(dev)) { |
253 | rtnl_unlock(); | |
254 | rc = PTR_ERR(dev); | |
2b4d413c | 255 | goto out_phy; |
e4962a14 AA |
256 | } |
257 | ||
258 | rtnl_unlock(); | |
259 | ||
1010f540 | 260 | return 0; |
261 | ||
2b4d413c AA |
262 | out_phy: |
263 | wpan_phy_unregister(local->phy); | |
57588c71 MR |
264 | out_mac_wq: |
265 | destroy_workqueue(local->mac_wq); | |
1010f540 | 266 | out_wq: |
f7730542 | 267 | destroy_workqueue(local->workqueue); |
1010f540 | 268 | out: |
269 | return rc; | |
270 | } | |
5a504397 | 271 | EXPORT_SYMBOL(ieee802154_register_hw); |
1010f540 | 272 | |
5a504397 | 273 | void ieee802154_unregister_hw(struct ieee802154_hw *hw) |
1010f540 | 274 | { |
60741361 | 275 | struct ieee802154_local *local = hw_to_local(hw); |
1010f540 | 276 | |
c5c47e67 | 277 | tasklet_kill(&local->tasklet); |
f7730542 | 278 | flush_workqueue(local->workqueue); |
1010f540 | 279 | |
280 | rtnl_lock(); | |
281 | ||
592dfbfc | 282 | ieee802154_remove_interfaces(local); |
62610ad2 | 283 | |
1010f540 | 284 | rtnl_unlock(); |
285 | ||
57588c71 | 286 | destroy_workqueue(local->mac_wq); |
aef00c15 | 287 | destroy_workqueue(local->workqueue); |
a5e1ec53 | 288 | wpan_phy_unregister(local->phy); |
1010f540 | 289 | } |
5a504397 | 290 | EXPORT_SYMBOL(ieee802154_unregister_hw); |
1010f540 | 291 | |
be4fd8e5 AA |
292 | static int __init ieee802154_init(void) |
293 | { | |
294 | return ieee802154_iface_init(); | |
295 | } | |
296 | ||
297 | static void __exit ieee802154_exit(void) | |
298 | { | |
299 | ieee802154_iface_exit(); | |
300 | ||
301 | rcu_barrier(); | |
302 | } | |
303 | ||
304 | subsys_initcall(ieee802154_init); | |
305 | module_exit(ieee802154_exit); | |
306 | ||
912f67ae | 307 | MODULE_DESCRIPTION("IEEE 802.15.4 subsystem"); |
1010f540 | 308 | MODULE_LICENSE("GPL v2"); |