Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007-2012 Siemens AG |
| 4 | * |
| 5 | * Written by: |
| 6 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/netdevice.h> |
| 12 | |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 13 | #include <net/netlink.h> |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 14 | #include <net/nl802154.h> |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 15 | #include <net/mac802154.h> |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 16 | #include <net/ieee802154_netdev.h> |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 17 | #include <net/route.h> |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame] | 18 | #include <net/cfg802154.h> |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 19 | |
Alexander Aring | 0f1556b | 2014-10-25 09:41:00 +0200 | [diff] [blame] | 20 | #include "ieee802154_i.h" |
Alexander Aring | 1201cd2 | 2014-11-02 04:18:36 +0100 | [diff] [blame] | 21 | #include "cfg.h" |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 22 | |
Alexander Aring | c5c47e6 | 2014-10-27 17:13:30 +0100 | [diff] [blame] | 23 | static void ieee802154_tasklet_handler(unsigned long data) |
| 24 | { |
| 25 | struct ieee802154_local *local = (struct ieee802154_local *)data; |
| 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; |
Varka Bhadram | d10270c | 2015-07-07 10:50:43 +0530 | [diff] [blame] | 35 | ieee802154_rx(local, skb); |
Alexander Aring | c5c47e6 | 2014-10-27 17:13:30 +0100 | [diff] [blame] | 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 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 46 | struct ieee802154_hw * |
Alexander Aring | 1630186 | 2014-10-28 18:21:18 +0100 | [diff] [blame] | 47 | ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops) |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 48 | { |
| 49 | struct wpan_phy *phy; |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 50 | struct ieee802154_local *local; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 51 | size_t priv_size; |
| 52 | |
Varka Bhadram | 8f45182 | 2015-06-23 11:41:03 +0530 | [diff] [blame] | 53 | if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed || |
| 54 | !ops->start || !ops->stop || !ops->set_channel)) |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 55 | return NULL; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 56 | |
| 57 | /* Ensure 32-byte alignment of our private data and hw private data. |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 58 | * We use the wpan_phy priv data for both our ieee802154_local and for |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 59 | * the driver's private data |
| 60 | * |
| 61 | * in memory it'll be like this: |
| 62 | * |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 63 | * +-------------------------+ |
| 64 | * | struct wpan_phy | |
| 65 | * +-------------------------+ |
| 66 | * | struct ieee802154_local | |
| 67 | * +-------------------------+ |
| 68 | * | driver's private data | |
| 69 | * +-------------------------+ |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 70 | * |
| 71 | * Due to ieee802154 layer isn't aware of driver and MAC structures, |
Alexander Aring | 139f14a | 2014-10-25 05:25:08 +0200 | [diff] [blame] | 72 | * so lets align them here. |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 73 | */ |
| 74 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 75 | priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 76 | |
Alexander Aring | f601379 | 2014-11-09 08:36:47 +0100 | [diff] [blame] | 77 | phy = wpan_phy_new(&mac802154_config_ops, priv_size); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 78 | if (!phy) { |
Chen Weilong | 83a1a7c | 2013-10-30 15:28:07 +0800 | [diff] [blame] | 79 | pr_err("failure to allocate master IEEE802.15.4 device\n"); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | |
Alexander Aring | 6322d50 | 2014-11-12 03:36:51 +0100 | [diff] [blame] | 83 | phy->privid = mac802154_wpan_phy_privid; |
| 84 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 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; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 90 | |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 91 | INIT_LIST_HEAD(&local->interfaces); |
| 92 | mutex_init(&local->iflist_mtx); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 93 | |
Alexander Aring | c5c47e6 | 2014-10-27 17:13:30 +0100 | [diff] [blame] | 94 | tasklet_init(&local->tasklet, |
| 95 | ieee802154_tasklet_handler, |
| 96 | (unsigned long)local); |
| 97 | |
| 98 | skb_queue_head_init(&local->skb_queue); |
| 99 | |
Lennert Buytenhek | c22ff7b | 2015-07-21 17:44:47 +0300 | [diff] [blame] | 100 | INIT_WORK(&local->tx_work, ieee802154_xmit_worker); |
| 101 | |
Alexander Aring | fea3318 | 2015-05-17 21:44:43 +0200 | [diff] [blame] | 102 | /* init supported flags with 802.15.4 default ranges */ |
| 103 | phy->supported.max_minbe = 8; |
| 104 | phy->supported.min_maxbe = 3; |
| 105 | phy->supported.max_maxbe = 8; |
Alexander Aring | 89c7d78 | 2015-08-10 21:15:56 +0200 | [diff] [blame] | 106 | phy->supported.min_frame_retries = 0; |
Alexander Aring | fea3318 | 2015-05-17 21:44:43 +0200 | [diff] [blame] | 107 | phy->supported.max_frame_retries = 7; |
| 108 | phy->supported.max_csma_backoffs = 5; |
| 109 | phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE; |
| 110 | |
Alexander Aring | 6531868 | 2015-05-17 21:44:47 +0200 | [diff] [blame] | 111 | /* always supported */ |
| 112 | phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE); |
| 113 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 114 | return &local->hw; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 115 | } |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 116 | EXPORT_SYMBOL(ieee802154_alloc_hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 117 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 118 | void ieee802154_free_hw(struct ieee802154_hw *hw) |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 119 | { |
Alexander Aring | 6074136 | 2014-10-25 17:16:39 +0200 | [diff] [blame] | 120 | struct ieee802154_local *local = hw_to_local(hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 121 | |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 122 | BUG_ON(!list_empty(&local->interfaces)); |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 123 | |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 124 | mutex_destroy(&local->iflist_mtx); |
Konstantin Khlebnikov | 1e9f954 | 2012-12-14 01:03:03 +0000 | [diff] [blame] | 125 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 126 | wpan_phy_free(local->phy); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 127 | } |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 128 | EXPORT_SYMBOL(ieee802154_free_hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 129 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 130 | static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy) |
| 131 | { |
| 132 | /* TODO warn on empty symbol_duration |
| 133 | * Should be done when all drivers sets this value. |
| 134 | */ |
| 135 | |
| 136 | wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD * |
| 137 | wpan_phy->symbol_duration; |
| 138 | wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD * |
| 139 | wpan_phy->symbol_duration; |
| 140 | } |
| 141 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 142 | int ieee802154_register_hw(struct ieee802154_hw *hw) |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 143 | { |
Alexander Aring | 6074136 | 2014-10-25 17:16:39 +0200 | [diff] [blame] | 144 | struct ieee802154_local *local = hw_to_local(hw); |
Alexander Aring | e4962a1 | 2014-11-05 20:51:19 +0100 | [diff] [blame] | 145 | struct net_device *dev; |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 146 | int rc = -ENOSYS; |
| 147 | |
Alexander Aring | f773054 | 2014-10-25 17:16:41 +0200 | [diff] [blame] | 148 | local->workqueue = |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 149 | create_singlethread_workqueue(wpan_phy_name(local->phy)); |
Alexander Aring | f773054 | 2014-10-25 17:16:41 +0200 | [diff] [blame] | 150 | if (!local->workqueue) { |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 151 | rc = -ENOMEM; |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 152 | goto out; |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 153 | } |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 154 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 155 | hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 156 | local->ifs_timer.function = ieee802154_xmit_ifs_timer; |
| 157 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 158 | wpan_phy_set_dev(local->phy, local->hw.parent); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 159 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 160 | ieee802154_setup_wpan_phy_pib(local->phy); |
| 161 | |
Alexander Aring | fea3318 | 2015-05-17 21:44:43 +0200 | [diff] [blame] | 162 | if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) { |
| 163 | local->phy->supported.min_csma_backoffs = 4; |
| 164 | local->phy->supported.max_csma_backoffs = 4; |
| 165 | local->phy->supported.min_maxbe = 5; |
| 166 | local->phy->supported.max_maxbe = 5; |
| 167 | local->phy->supported.min_minbe = 3; |
| 168 | local->phy->supported.max_minbe = 3; |
| 169 | } |
| 170 | |
| 171 | if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) { |
Alexander Aring | 89c7d78 | 2015-08-10 21:15:56 +0200 | [diff] [blame] | 172 | local->phy->supported.min_frame_retries = 3; |
| 173 | local->phy->supported.max_frame_retries = 3; |
Alexander Aring | fea3318 | 2015-05-17 21:44:43 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Alexander Aring | 6531868 | 2015-05-17 21:44:47 +0200 | [diff] [blame] | 176 | if (hw->flags & IEEE802154_HW_PROMISCUOUS) |
| 177 | local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR); |
| 178 | |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 179 | rc = wpan_phy_register(local->phy); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 180 | if (rc < 0) |
| 181 | goto out_wq; |
| 182 | |
Alexander Aring | e4962a1 | 2014-11-05 20:51:19 +0100 | [diff] [blame] | 183 | rtnl_lock(); |
| 184 | |
Varka Bhadram | 5b4a1039 | 2015-04-30 17:44:57 +0200 | [diff] [blame] | 185 | dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM, |
| 186 | NL802154_IFTYPE_NODE, |
Alexander Aring | 0e57547 | 2014-11-17 08:20:52 +0100 | [diff] [blame] | 187 | cpu_to_le64(0x0000000000000000ULL)); |
Alexander Aring | e4962a1 | 2014-11-05 20:51:19 +0100 | [diff] [blame] | 188 | if (IS_ERR(dev)) { |
| 189 | rtnl_unlock(); |
| 190 | rc = PTR_ERR(dev); |
Alexander Aring | 2b4d413 | 2015-04-30 17:44:53 +0200 | [diff] [blame] | 191 | goto out_phy; |
Alexander Aring | e4962a1 | 2014-11-05 20:51:19 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | rtnl_unlock(); |
| 195 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 196 | return 0; |
| 197 | |
Alexander Aring | 2b4d413 | 2015-04-30 17:44:53 +0200 | [diff] [blame] | 198 | out_phy: |
| 199 | wpan_phy_unregister(local->phy); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 200 | out_wq: |
Alexander Aring | f773054 | 2014-10-25 17:16:41 +0200 | [diff] [blame] | 201 | destroy_workqueue(local->workqueue); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 202 | out: |
| 203 | return rc; |
| 204 | } |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 205 | EXPORT_SYMBOL(ieee802154_register_hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 206 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 207 | void ieee802154_unregister_hw(struct ieee802154_hw *hw) |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 208 | { |
Alexander Aring | 6074136 | 2014-10-25 17:16:39 +0200 | [diff] [blame] | 209 | struct ieee802154_local *local = hw_to_local(hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 210 | |
Alexander Aring | c5c47e6 | 2014-10-27 17:13:30 +0100 | [diff] [blame] | 211 | tasklet_kill(&local->tasklet); |
Alexander Aring | f773054 | 2014-10-25 17:16:41 +0200 | [diff] [blame] | 212 | flush_workqueue(local->workqueue); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 213 | |
| 214 | rtnl_lock(); |
| 215 | |
Alexander Aring | 592dfbfc | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 216 | ieee802154_remove_interfaces(local); |
alex.bluesman.smirnov@gmail.com | 62610ad | 2012-05-15 20:50:28 +0000 | [diff] [blame] | 217 | |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 218 | rtnl_unlock(); |
| 219 | |
Koen Zandberg | aef00c1 | 2016-02-10 11:49:38 +0100 | [diff] [blame] | 220 | destroy_workqueue(local->workqueue); |
Alexander Aring | a5e1ec5 | 2014-10-25 17:16:35 +0200 | [diff] [blame] | 221 | wpan_phy_unregister(local->phy); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 222 | } |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 223 | EXPORT_SYMBOL(ieee802154_unregister_hw); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 224 | |
Alexander Aring | be4fd8e | 2014-11-12 03:36:53 +0100 | [diff] [blame] | 225 | static int __init ieee802154_init(void) |
| 226 | { |
| 227 | return ieee802154_iface_init(); |
| 228 | } |
| 229 | |
| 230 | static void __exit ieee802154_exit(void) |
| 231 | { |
| 232 | ieee802154_iface_exit(); |
| 233 | |
| 234 | rcu_barrier(); |
| 235 | } |
| 236 | |
| 237 | subsys_initcall(ieee802154_init); |
| 238 | module_exit(ieee802154_exit); |
| 239 | |
Alexander Aring | 912f67a | 2014-11-12 03:36:52 +0100 | [diff] [blame] | 240 | MODULE_DESCRIPTION("IEEE 802.15.4 subsystem"); |
alex.bluesman.smirnov@gmail.com | 1010f54 | 2012-05-15 20:50:20 +0000 | [diff] [blame] | 241 | MODULE_LICENSE("GPL v2"); |