157588c71SMiquel Raynal // SPDX-License-Identifier: GPL-2.0-only 257588c71SMiquel Raynal /* 357588c71SMiquel Raynal * IEEE 802.15.4 scanning management 457588c71SMiquel Raynal * 557588c71SMiquel Raynal * Copyright (C) 2021 Qorvo US, Inc 657588c71SMiquel Raynal * Authors: 757588c71SMiquel Raynal * - David Girault <david.girault@qorvo.com> 857588c71SMiquel Raynal * - Miquel Raynal <miquel.raynal@bootlin.com> 957588c71SMiquel Raynal */ 1057588c71SMiquel Raynal 1157588c71SMiquel Raynal #include <linux/module.h> 1257588c71SMiquel Raynal #include <linux/rtnetlink.h> 1357588c71SMiquel Raynal #include <net/mac802154.h> 1457588c71SMiquel Raynal 1557588c71SMiquel Raynal #include "ieee802154_i.h" 1657588c71SMiquel Raynal #include "driver-ops.h" 1757588c71SMiquel Raynal #include "../ieee802154/nl802154.h" 1857588c71SMiquel Raynal 193accf476SMiquel Raynal #define IEEE802154_BEACON_MHR_SZ 13 203accf476SMiquel Raynal #define IEEE802154_BEACON_PL_SZ 4 213accf476SMiquel Raynal #define IEEE802154_BEACON_SKB_SZ (IEEE802154_BEACON_MHR_SZ + \ 223accf476SMiquel Raynal IEEE802154_BEACON_PL_SZ) 233accf476SMiquel Raynal 2457588c71SMiquel Raynal /* mac802154_scan_cleanup_locked() must be called upon scan completion or abort. 2557588c71SMiquel Raynal * - Completions are asynchronous, not locked by the rtnl and decided by the 2657588c71SMiquel Raynal * scan worker. 2757588c71SMiquel Raynal * - Aborts are decided by userspace, and locked by the rtnl. 2857588c71SMiquel Raynal * 2957588c71SMiquel Raynal * Concurrent modifications to the PHY, the interfaces or the hardware is in 3057588c71SMiquel Raynal * general prevented by the rtnl. So in most cases we don't need additional 3157588c71SMiquel Raynal * protection. 3257588c71SMiquel Raynal * 3357588c71SMiquel Raynal * However, the scan worker get's triggered without anybody noticing and thus we 3457588c71SMiquel Raynal * must ensure the presence of the devices as well as data consistency: 3557588c71SMiquel Raynal * - The sub-interface and device driver module get both their reference 3657588c71SMiquel Raynal * counters incremented whenever we start a scan, so they cannot disappear 3757588c71SMiquel Raynal * during operation. 3857588c71SMiquel Raynal * - Data consistency is achieved by the use of rcu protected pointers. 3957588c71SMiquel Raynal */ 4057588c71SMiquel Raynal static int mac802154_scan_cleanup_locked(struct ieee802154_local *local, 4157588c71SMiquel Raynal struct ieee802154_sub_if_data *sdata, 4257588c71SMiquel Raynal bool aborted) 4357588c71SMiquel Raynal { 4457588c71SMiquel Raynal struct wpan_dev *wpan_dev = &sdata->wpan_dev; 4557588c71SMiquel Raynal struct wpan_phy *wpan_phy = local->phy; 4657588c71SMiquel Raynal struct cfg802154_scan_request *request; 4757588c71SMiquel Raynal u8 arg; 4857588c71SMiquel Raynal 4957588c71SMiquel Raynal /* Prevent any further use of the scan request */ 5057588c71SMiquel Raynal clear_bit(IEEE802154_IS_SCANNING, &local->ongoing); 5157588c71SMiquel Raynal cancel_delayed_work(&local->scan_work); 5257588c71SMiquel Raynal request = rcu_replace_pointer(local->scan_req, NULL, 1); 5357588c71SMiquel Raynal if (!request) 5457588c71SMiquel Raynal return 0; 5557588c71SMiquel Raynal kfree_rcu(request); 5657588c71SMiquel Raynal 5757588c71SMiquel Raynal /* Advertize first, while we know the devices cannot be removed */ 5857588c71SMiquel Raynal if (aborted) 5957588c71SMiquel Raynal arg = NL802154_SCAN_DONE_REASON_ABORTED; 6057588c71SMiquel Raynal else 6157588c71SMiquel Raynal arg = NL802154_SCAN_DONE_REASON_FINISHED; 6257588c71SMiquel Raynal nl802154_scan_done(wpan_phy, wpan_dev, arg); 6357588c71SMiquel Raynal 6457588c71SMiquel Raynal /* Cleanup software stack */ 6557588c71SMiquel Raynal ieee802154_mlme_op_post(local); 6657588c71SMiquel Raynal 6757588c71SMiquel Raynal /* Set the hardware back in its original state */ 6857588c71SMiquel Raynal drv_set_channel(local, wpan_phy->current_page, 6957588c71SMiquel Raynal wpan_phy->current_channel); 7057588c71SMiquel Raynal ieee802154_configure_durations(wpan_phy, wpan_phy->current_page, 7157588c71SMiquel Raynal wpan_phy->current_channel); 7257588c71SMiquel Raynal drv_stop(local); 7357588c71SMiquel Raynal synchronize_net(); 7457588c71SMiquel Raynal sdata->required_filtering = sdata->iface_default_filtering; 7557588c71SMiquel Raynal drv_start(local, sdata->required_filtering, &local->addr_filt); 7657588c71SMiquel Raynal 7757588c71SMiquel Raynal return 0; 7857588c71SMiquel Raynal } 7957588c71SMiquel Raynal 8057588c71SMiquel Raynal int mac802154_abort_scan_locked(struct ieee802154_local *local, 8157588c71SMiquel Raynal struct ieee802154_sub_if_data *sdata) 8257588c71SMiquel Raynal { 8357588c71SMiquel Raynal ASSERT_RTNL(); 8457588c71SMiquel Raynal 8557588c71SMiquel Raynal if (!mac802154_is_scanning(local)) 8657588c71SMiquel Raynal return -ESRCH; 8757588c71SMiquel Raynal 8857588c71SMiquel Raynal return mac802154_scan_cleanup_locked(local, sdata, true); 8957588c71SMiquel Raynal } 9057588c71SMiquel Raynal 9157588c71SMiquel Raynal static unsigned int mac802154_scan_get_channel_time(u8 duration_order, 9257588c71SMiquel Raynal u8 symbol_duration) 9357588c71SMiquel Raynal { 9457588c71SMiquel Raynal u64 base_super_frame_duration = (u64)symbol_duration * 9557588c71SMiquel Raynal IEEE802154_SUPERFRAME_PERIOD * IEEE802154_SLOT_PERIOD; 9657588c71SMiquel Raynal 9757588c71SMiquel Raynal return usecs_to_jiffies(base_super_frame_duration * 9857588c71SMiquel Raynal (BIT(duration_order) + 1)); 9957588c71SMiquel Raynal } 10057588c71SMiquel Raynal 10157588c71SMiquel Raynal static void mac802154_flush_queued_beacons(struct ieee802154_local *local) 10257588c71SMiquel Raynal { 10357588c71SMiquel Raynal struct cfg802154_mac_pkt *mac_pkt, *tmp; 10457588c71SMiquel Raynal 10557588c71SMiquel Raynal list_for_each_entry_safe(mac_pkt, tmp, &local->rx_beacon_list, node) { 10657588c71SMiquel Raynal list_del(&mac_pkt->node); 10757588c71SMiquel Raynal kfree_skb(mac_pkt->skb); 10857588c71SMiquel Raynal kfree(mac_pkt); 10957588c71SMiquel Raynal } 11057588c71SMiquel Raynal } 11157588c71SMiquel Raynal 11257588c71SMiquel Raynal static void 11357588c71SMiquel Raynal mac802154_scan_get_next_channel(struct ieee802154_local *local, 11457588c71SMiquel Raynal struct cfg802154_scan_request *scan_req, 11557588c71SMiquel Raynal u8 *channel) 11657588c71SMiquel Raynal { 11757588c71SMiquel Raynal (*channel)++; 11857588c71SMiquel Raynal *channel = find_next_bit((const unsigned long *)&scan_req->channels, 11957588c71SMiquel Raynal IEEE802154_MAX_CHANNEL + 1, 12057588c71SMiquel Raynal *channel); 12157588c71SMiquel Raynal } 12257588c71SMiquel Raynal 12357588c71SMiquel Raynal static int mac802154_scan_find_next_chan(struct ieee802154_local *local, 12457588c71SMiquel Raynal struct cfg802154_scan_request *scan_req, 12557588c71SMiquel Raynal u8 page, u8 *channel) 12657588c71SMiquel Raynal { 12757588c71SMiquel Raynal mac802154_scan_get_next_channel(local, scan_req, channel); 12857588c71SMiquel Raynal if (*channel > IEEE802154_MAX_CHANNEL) 12957588c71SMiquel Raynal return -EINVAL; 13057588c71SMiquel Raynal 13157588c71SMiquel Raynal return 0; 13257588c71SMiquel Raynal } 13357588c71SMiquel Raynal 13457588c71SMiquel Raynal void mac802154_scan_worker(struct work_struct *work) 13557588c71SMiquel Raynal { 13657588c71SMiquel Raynal struct ieee802154_local *local = 13757588c71SMiquel Raynal container_of(work, struct ieee802154_local, scan_work.work); 13857588c71SMiquel Raynal struct cfg802154_scan_request *scan_req; 13957588c71SMiquel Raynal struct ieee802154_sub_if_data *sdata; 14057588c71SMiquel Raynal unsigned int scan_duration = 0; 14157588c71SMiquel Raynal struct wpan_phy *wpan_phy; 14257588c71SMiquel Raynal u8 scan_req_duration; 14357588c71SMiquel Raynal u8 page, channel; 14457588c71SMiquel Raynal int ret; 14557588c71SMiquel Raynal 14657588c71SMiquel Raynal /* Ensure the device receiver is turned off when changing channels 14757588c71SMiquel Raynal * because there is no atomic way to change the channel and know on 14857588c71SMiquel Raynal * which one a beacon might have been received. 14957588c71SMiquel Raynal */ 15057588c71SMiquel Raynal drv_stop(local); 15157588c71SMiquel Raynal synchronize_net(); 15257588c71SMiquel Raynal mac802154_flush_queued_beacons(local); 15357588c71SMiquel Raynal 15457588c71SMiquel Raynal rcu_read_lock(); 15557588c71SMiquel Raynal scan_req = rcu_dereference(local->scan_req); 15657588c71SMiquel Raynal if (unlikely(!scan_req)) { 15757588c71SMiquel Raynal rcu_read_unlock(); 15857588c71SMiquel Raynal return; 15957588c71SMiquel Raynal } 16057588c71SMiquel Raynal 16157588c71SMiquel Raynal sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(scan_req->wpan_dev); 16257588c71SMiquel Raynal 16357588c71SMiquel Raynal /* Wait an arbitrary amount of time in case we cannot use the device */ 16457588c71SMiquel Raynal if (local->suspended || !ieee802154_sdata_running(sdata)) { 16557588c71SMiquel Raynal rcu_read_unlock(); 16657588c71SMiquel Raynal queue_delayed_work(local->mac_wq, &local->scan_work, 16757588c71SMiquel Raynal msecs_to_jiffies(1000)); 16857588c71SMiquel Raynal return; 16957588c71SMiquel Raynal } 17057588c71SMiquel Raynal 17157588c71SMiquel Raynal wpan_phy = scan_req->wpan_phy; 17257588c71SMiquel Raynal scan_req_duration = scan_req->duration; 17357588c71SMiquel Raynal 17457588c71SMiquel Raynal /* Look for the next valid chan */ 17557588c71SMiquel Raynal page = local->scan_page; 17657588c71SMiquel Raynal channel = local->scan_channel; 17757588c71SMiquel Raynal do { 17857588c71SMiquel Raynal ret = mac802154_scan_find_next_chan(local, scan_req, page, &channel); 17957588c71SMiquel Raynal if (ret) { 18057588c71SMiquel Raynal rcu_read_unlock(); 18157588c71SMiquel Raynal goto end_scan; 18257588c71SMiquel Raynal } 18357588c71SMiquel Raynal } while (!ieee802154_chan_is_valid(scan_req->wpan_phy, page, channel)); 18457588c71SMiquel Raynal 18557588c71SMiquel Raynal rcu_read_unlock(); 18657588c71SMiquel Raynal 18757588c71SMiquel Raynal /* Bypass the stack on purpose when changing the channel */ 18857588c71SMiquel Raynal rtnl_lock(); 18957588c71SMiquel Raynal ret = drv_set_channel(local, page, channel); 19057588c71SMiquel Raynal rtnl_unlock(); 19157588c71SMiquel Raynal if (ret) { 19257588c71SMiquel Raynal dev_err(&sdata->dev->dev, 19357588c71SMiquel Raynal "Channel change failure during scan, aborting (%d)\n", ret); 19457588c71SMiquel Raynal goto end_scan; 19557588c71SMiquel Raynal } 19657588c71SMiquel Raynal 19757588c71SMiquel Raynal local->scan_page = page; 19857588c71SMiquel Raynal local->scan_channel = channel; 19957588c71SMiquel Raynal 20057588c71SMiquel Raynal rtnl_lock(); 20157588c71SMiquel Raynal ret = drv_start(local, IEEE802154_FILTERING_3_SCAN, &local->addr_filt); 20257588c71SMiquel Raynal rtnl_unlock(); 20357588c71SMiquel Raynal if (ret) { 20457588c71SMiquel Raynal dev_err(&sdata->dev->dev, 20557588c71SMiquel Raynal "Restarting failure after channel change, aborting (%d)\n", ret); 20657588c71SMiquel Raynal goto end_scan; 20757588c71SMiquel Raynal } 20857588c71SMiquel Raynal 20957588c71SMiquel Raynal ieee802154_configure_durations(wpan_phy, page, channel); 21057588c71SMiquel Raynal scan_duration = mac802154_scan_get_channel_time(scan_req_duration, 21157588c71SMiquel Raynal wpan_phy->symbol_duration); 21257588c71SMiquel Raynal dev_dbg(&sdata->dev->dev, 21357588c71SMiquel Raynal "Scan page %u channel %u for %ums\n", 21457588c71SMiquel Raynal page, channel, jiffies_to_msecs(scan_duration)); 21557588c71SMiquel Raynal queue_delayed_work(local->mac_wq, &local->scan_work, scan_duration); 21657588c71SMiquel Raynal return; 21757588c71SMiquel Raynal 21857588c71SMiquel Raynal end_scan: 21957588c71SMiquel Raynal rtnl_lock(); 22057588c71SMiquel Raynal mac802154_scan_cleanup_locked(local, sdata, false); 22157588c71SMiquel Raynal rtnl_unlock(); 22257588c71SMiquel Raynal } 22357588c71SMiquel Raynal 22457588c71SMiquel Raynal int mac802154_trigger_scan_locked(struct ieee802154_sub_if_data *sdata, 22557588c71SMiquel Raynal struct cfg802154_scan_request *request) 22657588c71SMiquel Raynal { 22757588c71SMiquel Raynal struct ieee802154_local *local = sdata->local; 22857588c71SMiquel Raynal 22957588c71SMiquel Raynal ASSERT_RTNL(); 23057588c71SMiquel Raynal 23157588c71SMiquel Raynal if (mac802154_is_scanning(local)) 23257588c71SMiquel Raynal return -EBUSY; 23357588c71SMiquel Raynal 23457588c71SMiquel Raynal /* TODO: support other scanning type */ 23557588c71SMiquel Raynal if (request->type != NL802154_SCAN_PASSIVE) 23657588c71SMiquel Raynal return -EOPNOTSUPP; 23757588c71SMiquel Raynal 23857588c71SMiquel Raynal /* Store scanning parameters */ 23957588c71SMiquel Raynal rcu_assign_pointer(local->scan_req, request); 24057588c71SMiquel Raynal 24157588c71SMiquel Raynal /* Software scanning requires to set promiscuous mode, so we need to 24257588c71SMiquel Raynal * pause the Tx queue during the entire operation. 24357588c71SMiquel Raynal */ 24457588c71SMiquel Raynal ieee802154_mlme_op_pre(local); 24557588c71SMiquel Raynal 24657588c71SMiquel Raynal sdata->required_filtering = IEEE802154_FILTERING_3_SCAN; 24757588c71SMiquel Raynal local->scan_page = request->page; 24857588c71SMiquel Raynal local->scan_channel = -1; 24957588c71SMiquel Raynal set_bit(IEEE802154_IS_SCANNING, &local->ongoing); 25057588c71SMiquel Raynal 25157588c71SMiquel Raynal nl802154_scan_started(request->wpan_phy, request->wpan_dev); 25257588c71SMiquel Raynal 25357588c71SMiquel Raynal queue_delayed_work(local->mac_wq, &local->scan_work, 0); 25457588c71SMiquel Raynal 25557588c71SMiquel Raynal return 0; 25657588c71SMiquel Raynal } 25757588c71SMiquel Raynal 25857588c71SMiquel Raynal int mac802154_process_beacon(struct ieee802154_local *local, 25957588c71SMiquel Raynal struct sk_buff *skb, 26057588c71SMiquel Raynal u8 page, u8 channel) 26157588c71SMiquel Raynal { 26257588c71SMiquel Raynal struct ieee802154_beacon_hdr *bh = (void *)skb->data; 26357588c71SMiquel Raynal struct ieee802154_addr *src = &mac_cb(skb)->source; 26457588c71SMiquel Raynal struct cfg802154_scan_request *scan_req; 26557588c71SMiquel Raynal struct ieee802154_coord_desc desc; 26657588c71SMiquel Raynal 26757588c71SMiquel Raynal if (skb->len != sizeof(*bh)) 26857588c71SMiquel Raynal return -EINVAL; 26957588c71SMiquel Raynal 27057588c71SMiquel Raynal if (unlikely(src->mode == IEEE802154_ADDR_NONE)) 27157588c71SMiquel Raynal return -EINVAL; 27257588c71SMiquel Raynal 27357588c71SMiquel Raynal dev_dbg(&skb->dev->dev, 27457588c71SMiquel Raynal "BEACON received on page %u channel %u\n", 27557588c71SMiquel Raynal page, channel); 27657588c71SMiquel Raynal 27757588c71SMiquel Raynal memcpy(&desc.addr, src, sizeof(desc.addr)); 27857588c71SMiquel Raynal desc.page = page; 27957588c71SMiquel Raynal desc.channel = channel; 28057588c71SMiquel Raynal desc.link_quality = mac_cb(skb)->lqi; 28157588c71SMiquel Raynal desc.superframe_spec = get_unaligned_le16(skb->data); 28257588c71SMiquel Raynal desc.gts_permit = bh->gts_permit; 28357588c71SMiquel Raynal 28457588c71SMiquel Raynal trace_802154_scan_event(&desc); 28557588c71SMiquel Raynal 28657588c71SMiquel Raynal rcu_read_lock(); 28757588c71SMiquel Raynal scan_req = rcu_dereference(local->scan_req); 28857588c71SMiquel Raynal if (likely(scan_req)) 28957588c71SMiquel Raynal nl802154_scan_event(scan_req->wpan_phy, scan_req->wpan_dev, &desc); 29057588c71SMiquel Raynal rcu_read_unlock(); 29157588c71SMiquel Raynal 29257588c71SMiquel Raynal return 0; 29357588c71SMiquel Raynal } 2943accf476SMiquel Raynal 2953accf476SMiquel Raynal static int mac802154_transmit_beacon(struct ieee802154_local *local, 2963accf476SMiquel Raynal struct wpan_dev *wpan_dev) 2973accf476SMiquel Raynal { 2983accf476SMiquel Raynal struct cfg802154_beacon_request *beacon_req; 2993accf476SMiquel Raynal struct ieee802154_sub_if_data *sdata; 3003accf476SMiquel Raynal struct sk_buff *skb; 3013accf476SMiquel Raynal int ret; 3023accf476SMiquel Raynal 3033accf476SMiquel Raynal /* Update the sequence number */ 3043accf476SMiquel Raynal local->beacon.mhr.seq = atomic_inc_return(&wpan_dev->bsn) & 0xFF; 3053accf476SMiquel Raynal 3063accf476SMiquel Raynal skb = alloc_skb(IEEE802154_BEACON_SKB_SZ, GFP_KERNEL); 3073accf476SMiquel Raynal if (!skb) 3083accf476SMiquel Raynal return -ENOBUFS; 3093accf476SMiquel Raynal 3103accf476SMiquel Raynal rcu_read_lock(); 3113accf476SMiquel Raynal beacon_req = rcu_dereference(local->beacon_req); 3123accf476SMiquel Raynal if (unlikely(!beacon_req)) { 3133accf476SMiquel Raynal rcu_read_unlock(); 3143accf476SMiquel Raynal kfree_skb(skb); 3153accf476SMiquel Raynal return -EINVAL; 3163accf476SMiquel Raynal } 3173accf476SMiquel Raynal 3183accf476SMiquel Raynal sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev); 3193accf476SMiquel Raynal skb->dev = sdata->dev; 3203accf476SMiquel Raynal 3213accf476SMiquel Raynal rcu_read_unlock(); 3223accf476SMiquel Raynal 3233accf476SMiquel Raynal ret = ieee802154_beacon_push(skb, &local->beacon); 3243accf476SMiquel Raynal if (ret) { 3253accf476SMiquel Raynal kfree_skb(skb); 3263accf476SMiquel Raynal return ret; 3273accf476SMiquel Raynal } 3283accf476SMiquel Raynal 329*1375e3baSMiquel Raynal /* Using the MLME transmission helper for sending beacons is a bit 330*1375e3baSMiquel Raynal * overkill because we do not really care about the final outcome. 331*1375e3baSMiquel Raynal * 332*1375e3baSMiquel Raynal * Even though, going through the whole net stack with a regular 333*1375e3baSMiquel Raynal * dev_queue_xmit() is not relevant either because we want beacons to be 334*1375e3baSMiquel Raynal * sent "now" rather than go through the whole net stack scheduling 335*1375e3baSMiquel Raynal * (qdisc & co). 336*1375e3baSMiquel Raynal * 337*1375e3baSMiquel Raynal * Finally, using ieee802154_subif_start_xmit() would only be an option 338*1375e3baSMiquel Raynal * if we had a generic transmit helper which would acquire the 339*1375e3baSMiquel Raynal * HARD_TX_LOCK() to prevent buffer handling conflicts with regular 340*1375e3baSMiquel Raynal * packets. 341*1375e3baSMiquel Raynal * 342*1375e3baSMiquel Raynal * So for now we keep it simple and send beacons with our MLME helper, 343*1375e3baSMiquel Raynal * even if it stops the ieee802154 queue entirely during these 344*1375e3baSMiquel Raynal * transmissions, wich anyway does not have a huge impact on the 345*1375e3baSMiquel Raynal * performances given the current design of the stack. 346*1375e3baSMiquel Raynal */ 347*1375e3baSMiquel Raynal return ieee802154_mlme_tx(local, sdata, skb); 3483accf476SMiquel Raynal } 3493accf476SMiquel Raynal 3503accf476SMiquel Raynal void mac802154_beacon_worker(struct work_struct *work) 3513accf476SMiquel Raynal { 3523accf476SMiquel Raynal struct ieee802154_local *local = 3533accf476SMiquel Raynal container_of(work, struct ieee802154_local, beacon_work.work); 3543accf476SMiquel Raynal struct cfg802154_beacon_request *beacon_req; 3553accf476SMiquel Raynal struct ieee802154_sub_if_data *sdata; 3563accf476SMiquel Raynal struct wpan_dev *wpan_dev; 3573accf476SMiquel Raynal int ret; 3583accf476SMiquel Raynal 3593accf476SMiquel Raynal rcu_read_lock(); 3603accf476SMiquel Raynal beacon_req = rcu_dereference(local->beacon_req); 3613accf476SMiquel Raynal if (unlikely(!beacon_req)) { 3623accf476SMiquel Raynal rcu_read_unlock(); 3633accf476SMiquel Raynal return; 3643accf476SMiquel Raynal } 3653accf476SMiquel Raynal 3663accf476SMiquel Raynal sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev); 3673accf476SMiquel Raynal 3683accf476SMiquel Raynal /* Wait an arbitrary amount of time in case we cannot use the device */ 3693accf476SMiquel Raynal if (local->suspended || !ieee802154_sdata_running(sdata)) { 3703accf476SMiquel Raynal rcu_read_unlock(); 3713accf476SMiquel Raynal queue_delayed_work(local->mac_wq, &local->beacon_work, 3723accf476SMiquel Raynal msecs_to_jiffies(1000)); 3733accf476SMiquel Raynal return; 3743accf476SMiquel Raynal } 3753accf476SMiquel Raynal 3763accf476SMiquel Raynal wpan_dev = beacon_req->wpan_dev; 3773accf476SMiquel Raynal 3783accf476SMiquel Raynal rcu_read_unlock(); 3793accf476SMiquel Raynal 3803accf476SMiquel Raynal dev_dbg(&sdata->dev->dev, "Sending beacon\n"); 3813accf476SMiquel Raynal ret = mac802154_transmit_beacon(local, wpan_dev); 3823accf476SMiquel Raynal if (ret) 3833accf476SMiquel Raynal dev_err(&sdata->dev->dev, 3843accf476SMiquel Raynal "Beacon could not be transmitted (%d)\n", ret); 3853accf476SMiquel Raynal 3863accf476SMiquel Raynal if (local->beacon_interval >= 0) 3873accf476SMiquel Raynal queue_delayed_work(local->mac_wq, &local->beacon_work, 3883accf476SMiquel Raynal local->beacon_interval); 3893accf476SMiquel Raynal } 3903accf476SMiquel Raynal 3913accf476SMiquel Raynal int mac802154_stop_beacons_locked(struct ieee802154_local *local, 3923accf476SMiquel Raynal struct ieee802154_sub_if_data *sdata) 3933accf476SMiquel Raynal { 3943accf476SMiquel Raynal struct wpan_dev *wpan_dev = &sdata->wpan_dev; 3953accf476SMiquel Raynal struct cfg802154_beacon_request *request; 3963accf476SMiquel Raynal 3973accf476SMiquel Raynal ASSERT_RTNL(); 3983accf476SMiquel Raynal 3993accf476SMiquel Raynal if (!mac802154_is_beaconing(local)) 4003accf476SMiquel Raynal return -ESRCH; 4013accf476SMiquel Raynal 4023accf476SMiquel Raynal clear_bit(IEEE802154_IS_BEACONING, &local->ongoing); 4033accf476SMiquel Raynal cancel_delayed_work(&local->beacon_work); 4043accf476SMiquel Raynal request = rcu_replace_pointer(local->beacon_req, NULL, 1); 4053accf476SMiquel Raynal if (!request) 4063accf476SMiquel Raynal return 0; 4073accf476SMiquel Raynal kfree_rcu(request); 4083accf476SMiquel Raynal 4093accf476SMiquel Raynal nl802154_beaconing_done(wpan_dev); 4103accf476SMiquel Raynal 4113accf476SMiquel Raynal return 0; 4123accf476SMiquel Raynal } 4133accf476SMiquel Raynal 4143accf476SMiquel Raynal int mac802154_send_beacons_locked(struct ieee802154_sub_if_data *sdata, 4153accf476SMiquel Raynal struct cfg802154_beacon_request *request) 4163accf476SMiquel Raynal { 4173accf476SMiquel Raynal struct ieee802154_local *local = sdata->local; 4183accf476SMiquel Raynal 4193accf476SMiquel Raynal ASSERT_RTNL(); 4203accf476SMiquel Raynal 4213accf476SMiquel Raynal if (mac802154_is_beaconing(local)) 4223accf476SMiquel Raynal mac802154_stop_beacons_locked(local, sdata); 4233accf476SMiquel Raynal 4243accf476SMiquel Raynal /* Store beaconing parameters */ 4253accf476SMiquel Raynal rcu_assign_pointer(local->beacon_req, request); 4263accf476SMiquel Raynal 4273accf476SMiquel Raynal set_bit(IEEE802154_IS_BEACONING, &local->ongoing); 4283accf476SMiquel Raynal 4293accf476SMiquel Raynal memset(&local->beacon, 0, sizeof(local->beacon)); 4303accf476SMiquel Raynal local->beacon.mhr.fc.type = IEEE802154_FC_TYPE_BEACON; 4313accf476SMiquel Raynal local->beacon.mhr.fc.security_enabled = 0; 4323accf476SMiquel Raynal local->beacon.mhr.fc.frame_pending = 0; 4333accf476SMiquel Raynal local->beacon.mhr.fc.ack_request = 0; 4343accf476SMiquel Raynal local->beacon.mhr.fc.intra_pan = 0; 4353accf476SMiquel Raynal local->beacon.mhr.fc.dest_addr_mode = IEEE802154_NO_ADDRESSING; 4363accf476SMiquel Raynal local->beacon.mhr.fc.version = IEEE802154_2003_STD; 4373accf476SMiquel Raynal local->beacon.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING; 4383accf476SMiquel Raynal atomic_set(&request->wpan_dev->bsn, -1); 4393accf476SMiquel Raynal local->beacon.mhr.source.mode = IEEE802154_ADDR_LONG; 4408338304cSMiquel Raynal local->beacon.mhr.source.pan_id = request->wpan_dev->pan_id; 4418338304cSMiquel Raynal local->beacon.mhr.source.extended_addr = request->wpan_dev->extended_addr; 4423accf476SMiquel Raynal local->beacon.mac_pl.beacon_order = request->interval; 4433accf476SMiquel Raynal local->beacon.mac_pl.superframe_order = request->interval; 4443accf476SMiquel Raynal local->beacon.mac_pl.final_cap_slot = 0xf; 4453accf476SMiquel Raynal local->beacon.mac_pl.battery_life_ext = 0; 4463accf476SMiquel Raynal /* TODO: Fill this field depending on the coordinator capacity */ 4473accf476SMiquel Raynal local->beacon.mac_pl.pan_coordinator = 1; 4483accf476SMiquel Raynal local->beacon.mac_pl.assoc_permit = 1; 4493accf476SMiquel Raynal 4503accf476SMiquel Raynal /* Start the beacon work */ 4513accf476SMiquel Raynal local->beacon_interval = 4523accf476SMiquel Raynal mac802154_scan_get_channel_time(request->interval, 4533accf476SMiquel Raynal request->wpan_phy->symbol_duration); 4543accf476SMiquel Raynal queue_delayed_work(local->mac_wq, &local->beacon_work, 0); 4553accf476SMiquel Raynal 4563accf476SMiquel Raynal return 0; 4573accf476SMiquel Raynal } 458