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 mutex_init(&local->iflist_mtx); 93 94 tasklet_setup(&local->tasklet, ieee802154_tasklet_handler); 95 96 skb_queue_head_init(&local->skb_queue); 97 98 INIT_WORK(&local->sync_tx_work, ieee802154_xmit_sync_worker); 99 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; 104 phy->supported.min_frame_retries = 0; 105 phy->supported.max_frame_retries = 7; 106 phy->supported.max_csma_backoffs = 5; 107 phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE; 108 109 /* always supported */ 110 phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE) | BIT(NL802154_IFTYPE_COORD); 111 112 return &local->hw; 113 } 114 EXPORT_SYMBOL(ieee802154_alloc_hw); 115 116 void ieee802154_configure_durations(struct wpan_phy *phy) 117 { 118 u32 duration = 0; 119 120 switch (phy->current_page) { 121 case 0: 122 if (BIT(phy->current_channel) & 0x1) 123 /* 868 MHz BPSK 802.15.4-2003: 20 ksym/s */ 124 duration = 50 * NSEC_PER_USEC; 125 else if (BIT(phy->current_channel) & 0x7FE) 126 /* 915 MHz BPSK 802.15.4-2003: 40 ksym/s */ 127 duration = 25 * NSEC_PER_USEC; 128 else if (BIT(phy->current_channel) & 0x7FFF800) 129 /* 2400 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */ 130 duration = 16 * NSEC_PER_USEC; 131 break; 132 case 2: 133 if (BIT(phy->current_channel) & 0x1) 134 /* 868 MHz O-QPSK 802.15.4-2006: 25 ksym/s */ 135 duration = 40 * NSEC_PER_USEC; 136 else if (BIT(phy->current_channel) & 0x7FE) 137 /* 915 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */ 138 duration = 16 * NSEC_PER_USEC; 139 break; 140 case 3: 141 if (BIT(phy->current_channel) & 0x3FFF) 142 /* 2.4 GHz CSS 802.15.4a-2007: 1/6 Msym/s */ 143 duration = 6 * NSEC_PER_USEC; 144 break; 145 default: 146 break; 147 } 148 149 if (!duration) { 150 pr_debug("Unknown PHY symbol duration\n"); 151 return; 152 } 153 154 phy->symbol_duration = duration; 155 phy->lifs_period = (IEEE802154_LIFS_PERIOD * phy->symbol_duration) / NSEC_PER_SEC; 156 phy->sifs_period = (IEEE802154_SIFS_PERIOD * phy->symbol_duration) / NSEC_PER_SEC; 157 } 158 EXPORT_SYMBOL(ieee802154_configure_durations); 159 160 void ieee802154_free_hw(struct ieee802154_hw *hw) 161 { 162 struct ieee802154_local *local = hw_to_local(hw); 163 164 BUG_ON(!list_empty(&local->interfaces)); 165 166 mutex_destroy(&local->iflist_mtx); 167 168 wpan_phy_free(local->phy); 169 } 170 EXPORT_SYMBOL(ieee802154_free_hw); 171 172 static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy) 173 { 174 /* TODO warn on empty symbol_duration 175 * Should be done when all drivers sets this value. 176 */ 177 178 wpan_phy->lifs_period = 179 (IEEE802154_LIFS_PERIOD * wpan_phy->symbol_duration) / 1000; 180 wpan_phy->sifs_period = 181 (IEEE802154_SIFS_PERIOD * wpan_phy->symbol_duration) / 1000; 182 } 183 184 int ieee802154_register_hw(struct ieee802154_hw *hw) 185 { 186 struct ieee802154_local *local = hw_to_local(hw); 187 struct net_device *dev; 188 int rc = -ENOSYS; 189 190 local->workqueue = 191 create_singlethread_workqueue(wpan_phy_name(local->phy)); 192 if (!local->workqueue) { 193 rc = -ENOMEM; 194 goto out; 195 } 196 197 hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 198 local->ifs_timer.function = ieee802154_xmit_ifs_timer; 199 200 wpan_phy_set_dev(local->phy, local->hw.parent); 201 202 ieee802154_setup_wpan_phy_pib(local->phy); 203 204 ieee802154_configure_durations(local->phy); 205 206 if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) { 207 local->phy->supported.min_csma_backoffs = 4; 208 local->phy->supported.max_csma_backoffs = 4; 209 local->phy->supported.min_maxbe = 5; 210 local->phy->supported.max_maxbe = 5; 211 local->phy->supported.min_minbe = 3; 212 local->phy->supported.max_minbe = 3; 213 } 214 215 if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) { 216 local->phy->supported.min_frame_retries = 3; 217 local->phy->supported.max_frame_retries = 3; 218 } 219 220 if (hw->flags & IEEE802154_HW_PROMISCUOUS) 221 local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR); 222 223 rc = wpan_phy_register(local->phy); 224 if (rc < 0) 225 goto out_wq; 226 227 rtnl_lock(); 228 229 dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM, 230 NL802154_IFTYPE_NODE, 231 cpu_to_le64(0x0000000000000000ULL)); 232 if (IS_ERR(dev)) { 233 rtnl_unlock(); 234 rc = PTR_ERR(dev); 235 goto out_phy; 236 } 237 238 rtnl_unlock(); 239 240 return 0; 241 242 out_phy: 243 wpan_phy_unregister(local->phy); 244 out_wq: 245 destroy_workqueue(local->workqueue); 246 out: 247 return rc; 248 } 249 EXPORT_SYMBOL(ieee802154_register_hw); 250 251 void ieee802154_unregister_hw(struct ieee802154_hw *hw) 252 { 253 struct ieee802154_local *local = hw_to_local(hw); 254 255 tasklet_kill(&local->tasklet); 256 flush_workqueue(local->workqueue); 257 258 rtnl_lock(); 259 260 ieee802154_remove_interfaces(local); 261 262 rtnl_unlock(); 263 264 destroy_workqueue(local->workqueue); 265 wpan_phy_unregister(local->phy); 266 } 267 EXPORT_SYMBOL(ieee802154_unregister_hw); 268 269 static int __init ieee802154_init(void) 270 { 271 return ieee802154_iface_init(); 272 } 273 274 static void __exit ieee802154_exit(void) 275 { 276 ieee802154_iface_exit(); 277 278 rcu_barrier(); 279 } 280 281 subsys_initcall(ieee802154_init); 282 module_exit(ieee802154_exit); 283 284 MODULE_DESCRIPTION("IEEE 802.15.4 subsystem"); 285 MODULE_LICENSE("GPL v2"); 286