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 21e2c3e6f5SMiquel Raynal #define IEEE802154_MAC_CMD_MHR_SZ 23 22e2c3e6f5SMiquel Raynal #define IEEE802154_MAC_CMD_PL_SZ 1 233accf476SMiquel Raynal #define IEEE802154_BEACON_SKB_SZ (IEEE802154_BEACON_MHR_SZ + \ 243accf476SMiquel Raynal IEEE802154_BEACON_PL_SZ) 25e2c3e6f5SMiquel Raynal #define IEEE802154_MAC_CMD_SKB_SZ (IEEE802154_MAC_CMD_MHR_SZ + \ 26e2c3e6f5SMiquel Raynal IEEE802154_MAC_CMD_PL_SZ) 273accf476SMiquel Raynal 2857588c71SMiquel Raynal /* mac802154_scan_cleanup_locked() must be called upon scan completion or abort. 2957588c71SMiquel Raynal * - Completions are asynchronous, not locked by the rtnl and decided by the 3057588c71SMiquel Raynal * scan worker. 3157588c71SMiquel Raynal * - Aborts are decided by userspace, and locked by the rtnl. 3257588c71SMiquel Raynal * 3357588c71SMiquel Raynal * Concurrent modifications to the PHY, the interfaces or the hardware is in 3457588c71SMiquel Raynal * general prevented by the rtnl. So in most cases we don't need additional 3557588c71SMiquel Raynal * protection. 3657588c71SMiquel Raynal * 3757588c71SMiquel Raynal * However, the scan worker get's triggered without anybody noticing and thus we 3857588c71SMiquel Raynal * must ensure the presence of the devices as well as data consistency: 3957588c71SMiquel Raynal * - The sub-interface and device driver module get both their reference 4057588c71SMiquel Raynal * counters incremented whenever we start a scan, so they cannot disappear 4157588c71SMiquel Raynal * during operation. 4257588c71SMiquel Raynal * - Data consistency is achieved by the use of rcu protected pointers. 4357588c71SMiquel Raynal */ 4457588c71SMiquel Raynal static int mac802154_scan_cleanup_locked(struct ieee802154_local *local, 4557588c71SMiquel Raynal struct ieee802154_sub_if_data *sdata, 4657588c71SMiquel Raynal bool aborted) 4757588c71SMiquel Raynal { 4857588c71SMiquel Raynal struct wpan_dev *wpan_dev = &sdata->wpan_dev; 4957588c71SMiquel Raynal struct wpan_phy *wpan_phy = local->phy; 5057588c71SMiquel Raynal struct cfg802154_scan_request *request; 5157588c71SMiquel Raynal u8 arg; 5257588c71SMiquel Raynal 5357588c71SMiquel Raynal /* Prevent any further use of the scan request */ 5457588c71SMiquel Raynal clear_bit(IEEE802154_IS_SCANNING, &local->ongoing); 5557588c71SMiquel Raynal cancel_delayed_work(&local->scan_work); 5657588c71SMiquel Raynal request = rcu_replace_pointer(local->scan_req, NULL, 1); 5757588c71SMiquel Raynal if (!request) 5857588c71SMiquel Raynal return 0; 5957588c71SMiquel Raynal kfree_rcu(request); 6057588c71SMiquel Raynal 6157588c71SMiquel Raynal /* Advertize first, while we know the devices cannot be removed */ 6257588c71SMiquel Raynal if (aborted) 6357588c71SMiquel Raynal arg = NL802154_SCAN_DONE_REASON_ABORTED; 6457588c71SMiquel Raynal else 6557588c71SMiquel Raynal arg = NL802154_SCAN_DONE_REASON_FINISHED; 6657588c71SMiquel Raynal nl802154_scan_done(wpan_phy, wpan_dev, arg); 6757588c71SMiquel Raynal 6857588c71SMiquel Raynal /* Cleanup software stack */ 6957588c71SMiquel Raynal ieee802154_mlme_op_post(local); 7057588c71SMiquel Raynal 7157588c71SMiquel Raynal /* Set the hardware back in its original state */ 7257588c71SMiquel Raynal drv_set_channel(local, wpan_phy->current_page, 7357588c71SMiquel Raynal wpan_phy->current_channel); 7457588c71SMiquel Raynal ieee802154_configure_durations(wpan_phy, wpan_phy->current_page, 7557588c71SMiquel Raynal wpan_phy->current_channel); 7657588c71SMiquel Raynal drv_stop(local); 7757588c71SMiquel Raynal synchronize_net(); 7857588c71SMiquel Raynal sdata->required_filtering = sdata->iface_default_filtering; 7957588c71SMiquel Raynal drv_start(local, sdata->required_filtering, &local->addr_filt); 8057588c71SMiquel Raynal 8157588c71SMiquel Raynal return 0; 8257588c71SMiquel Raynal } 8357588c71SMiquel Raynal 8457588c71SMiquel Raynal int mac802154_abort_scan_locked(struct ieee802154_local *local, 8557588c71SMiquel Raynal struct ieee802154_sub_if_data *sdata) 8657588c71SMiquel Raynal { 8757588c71SMiquel Raynal ASSERT_RTNL(); 8857588c71SMiquel Raynal 8957588c71SMiquel Raynal if (!mac802154_is_scanning(local)) 9057588c71SMiquel Raynal return -ESRCH; 9157588c71SMiquel Raynal 9257588c71SMiquel Raynal return mac802154_scan_cleanup_locked(local, sdata, true); 9357588c71SMiquel Raynal } 9457588c71SMiquel Raynal 9557588c71SMiquel Raynal static unsigned int mac802154_scan_get_channel_time(u8 duration_order, 9657588c71SMiquel Raynal u8 symbol_duration) 9757588c71SMiquel Raynal { 9857588c71SMiquel Raynal u64 base_super_frame_duration = (u64)symbol_duration * 9957588c71SMiquel Raynal IEEE802154_SUPERFRAME_PERIOD * IEEE802154_SLOT_PERIOD; 10057588c71SMiquel Raynal 10157588c71SMiquel Raynal return usecs_to_jiffies(base_super_frame_duration * 10257588c71SMiquel Raynal (BIT(duration_order) + 1)); 10357588c71SMiquel Raynal } 10457588c71SMiquel Raynal 10557588c71SMiquel Raynal static void mac802154_flush_queued_beacons(struct ieee802154_local *local) 10657588c71SMiquel Raynal { 10757588c71SMiquel Raynal struct cfg802154_mac_pkt *mac_pkt, *tmp; 10857588c71SMiquel Raynal 10957588c71SMiquel Raynal list_for_each_entry_safe(mac_pkt, tmp, &local->rx_beacon_list, node) { 11057588c71SMiquel Raynal list_del(&mac_pkt->node); 11157588c71SMiquel Raynal kfree_skb(mac_pkt->skb); 11257588c71SMiquel Raynal kfree(mac_pkt); 11357588c71SMiquel Raynal } 11457588c71SMiquel Raynal } 11557588c71SMiquel Raynal 11657588c71SMiquel Raynal static void 11757588c71SMiquel Raynal mac802154_scan_get_next_channel(struct ieee802154_local *local, 11857588c71SMiquel Raynal struct cfg802154_scan_request *scan_req, 11957588c71SMiquel Raynal u8 *channel) 12057588c71SMiquel Raynal { 12157588c71SMiquel Raynal (*channel)++; 12257588c71SMiquel Raynal *channel = find_next_bit((const unsigned long *)&scan_req->channels, 12357588c71SMiquel Raynal IEEE802154_MAX_CHANNEL + 1, 12457588c71SMiquel Raynal *channel); 12557588c71SMiquel Raynal } 12657588c71SMiquel Raynal 12757588c71SMiquel Raynal static int mac802154_scan_find_next_chan(struct ieee802154_local *local, 12857588c71SMiquel Raynal struct cfg802154_scan_request *scan_req, 12957588c71SMiquel Raynal u8 page, u8 *channel) 13057588c71SMiquel Raynal { 13157588c71SMiquel Raynal mac802154_scan_get_next_channel(local, scan_req, channel); 13257588c71SMiquel Raynal if (*channel > IEEE802154_MAX_CHANNEL) 13357588c71SMiquel Raynal return -EINVAL; 13457588c71SMiquel Raynal 13557588c71SMiquel Raynal return 0; 13657588c71SMiquel Raynal } 13757588c71SMiquel Raynal 138e2c3e6f5SMiquel Raynal static int mac802154_scan_prepare_beacon_req(struct ieee802154_local *local) 139e2c3e6f5SMiquel Raynal { 140e2c3e6f5SMiquel Raynal memset(&local->scan_beacon_req, 0, sizeof(local->scan_beacon_req)); 141e2c3e6f5SMiquel Raynal local->scan_beacon_req.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD; 142e2c3e6f5SMiquel Raynal local->scan_beacon_req.mhr.fc.dest_addr_mode = IEEE802154_SHORT_ADDRESSING; 143e2c3e6f5SMiquel Raynal local->scan_beacon_req.mhr.fc.version = IEEE802154_2003_STD; 144e2c3e6f5SMiquel Raynal local->scan_beacon_req.mhr.fc.source_addr_mode = IEEE802154_NO_ADDRESSING; 145e2c3e6f5SMiquel Raynal local->scan_beacon_req.mhr.dest.mode = IEEE802154_ADDR_SHORT; 146e2c3e6f5SMiquel Raynal local->scan_beacon_req.mhr.dest.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); 147e2c3e6f5SMiquel Raynal local->scan_beacon_req.mhr.dest.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); 148e2c3e6f5SMiquel Raynal local->scan_beacon_req.mac_pl.cmd_id = IEEE802154_CMD_BEACON_REQ; 149e2c3e6f5SMiquel Raynal 150e2c3e6f5SMiquel Raynal return 0; 151e2c3e6f5SMiquel Raynal } 152e2c3e6f5SMiquel Raynal 153e2c3e6f5SMiquel Raynal static int mac802154_transmit_beacon_req(struct ieee802154_local *local, 154e2c3e6f5SMiquel Raynal struct ieee802154_sub_if_data *sdata) 155e2c3e6f5SMiquel Raynal { 156e2c3e6f5SMiquel Raynal struct sk_buff *skb; 157e2c3e6f5SMiquel Raynal int ret; 158e2c3e6f5SMiquel Raynal 159e2c3e6f5SMiquel Raynal skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ, GFP_KERNEL); 160e2c3e6f5SMiquel Raynal if (!skb) 161e2c3e6f5SMiquel Raynal return -ENOBUFS; 162e2c3e6f5SMiquel Raynal 163e2c3e6f5SMiquel Raynal skb->dev = sdata->dev; 164e2c3e6f5SMiquel Raynal 165e2c3e6f5SMiquel Raynal ret = ieee802154_mac_cmd_push(skb, &local->scan_beacon_req, NULL, 0); 166e2c3e6f5SMiquel Raynal if (ret) { 167e2c3e6f5SMiquel Raynal kfree_skb(skb); 168e2c3e6f5SMiquel Raynal return ret; 169e2c3e6f5SMiquel Raynal } 170e2c3e6f5SMiquel Raynal 171e2c3e6f5SMiquel Raynal return ieee802154_mlme_tx(local, sdata, skb); 172e2c3e6f5SMiquel Raynal } 173e2c3e6f5SMiquel Raynal 17457588c71SMiquel Raynal void mac802154_scan_worker(struct work_struct *work) 17557588c71SMiquel Raynal { 17657588c71SMiquel Raynal struct ieee802154_local *local = 17757588c71SMiquel Raynal container_of(work, struct ieee802154_local, scan_work.work); 17857588c71SMiquel Raynal struct cfg802154_scan_request *scan_req; 17957588c71SMiquel Raynal struct ieee802154_sub_if_data *sdata; 18057588c71SMiquel Raynal unsigned int scan_duration = 0; 18157588c71SMiquel Raynal struct wpan_phy *wpan_phy; 18257588c71SMiquel Raynal u8 scan_req_duration; 18357588c71SMiquel Raynal u8 page, channel; 18457588c71SMiquel Raynal int ret; 18557588c71SMiquel Raynal 18657588c71SMiquel Raynal /* Ensure the device receiver is turned off when changing channels 18757588c71SMiquel Raynal * because there is no atomic way to change the channel and know on 18857588c71SMiquel Raynal * which one a beacon might have been received. 18957588c71SMiquel Raynal */ 19057588c71SMiquel Raynal drv_stop(local); 19157588c71SMiquel Raynal synchronize_net(); 19257588c71SMiquel Raynal mac802154_flush_queued_beacons(local); 19357588c71SMiquel Raynal 19457588c71SMiquel Raynal rcu_read_lock(); 19557588c71SMiquel Raynal scan_req = rcu_dereference(local->scan_req); 19657588c71SMiquel Raynal if (unlikely(!scan_req)) { 19757588c71SMiquel Raynal rcu_read_unlock(); 19857588c71SMiquel Raynal return; 19957588c71SMiquel Raynal } 20057588c71SMiquel Raynal 20157588c71SMiquel Raynal sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(scan_req->wpan_dev); 20257588c71SMiquel Raynal 20357588c71SMiquel Raynal /* Wait an arbitrary amount of time in case we cannot use the device */ 20457588c71SMiquel Raynal if (local->suspended || !ieee802154_sdata_running(sdata)) { 20557588c71SMiquel Raynal rcu_read_unlock(); 20657588c71SMiquel Raynal queue_delayed_work(local->mac_wq, &local->scan_work, 20757588c71SMiquel Raynal msecs_to_jiffies(1000)); 20857588c71SMiquel Raynal return; 20957588c71SMiquel Raynal } 21057588c71SMiquel Raynal 21157588c71SMiquel Raynal wpan_phy = scan_req->wpan_phy; 21257588c71SMiquel Raynal scan_req_duration = scan_req->duration; 21357588c71SMiquel Raynal 21457588c71SMiquel Raynal /* Look for the next valid chan */ 21557588c71SMiquel Raynal page = local->scan_page; 21657588c71SMiquel Raynal channel = local->scan_channel; 21757588c71SMiquel Raynal do { 21857588c71SMiquel Raynal ret = mac802154_scan_find_next_chan(local, scan_req, page, &channel); 21957588c71SMiquel Raynal if (ret) { 22057588c71SMiquel Raynal rcu_read_unlock(); 22157588c71SMiquel Raynal goto end_scan; 22257588c71SMiquel Raynal } 22357588c71SMiquel Raynal } while (!ieee802154_chan_is_valid(scan_req->wpan_phy, page, channel)); 22457588c71SMiquel Raynal 22557588c71SMiquel Raynal rcu_read_unlock(); 22657588c71SMiquel Raynal 22757588c71SMiquel Raynal /* Bypass the stack on purpose when changing the channel */ 22857588c71SMiquel Raynal rtnl_lock(); 22957588c71SMiquel Raynal ret = drv_set_channel(local, page, channel); 23057588c71SMiquel Raynal rtnl_unlock(); 23157588c71SMiquel Raynal if (ret) { 23257588c71SMiquel Raynal dev_err(&sdata->dev->dev, 23357588c71SMiquel Raynal "Channel change failure during scan, aborting (%d)\n", ret); 23457588c71SMiquel Raynal goto end_scan; 23557588c71SMiquel Raynal } 23657588c71SMiquel Raynal 23757588c71SMiquel Raynal local->scan_page = page; 23857588c71SMiquel Raynal local->scan_channel = channel; 23957588c71SMiquel Raynal 24057588c71SMiquel Raynal rtnl_lock(); 24157588c71SMiquel Raynal ret = drv_start(local, IEEE802154_FILTERING_3_SCAN, &local->addr_filt); 24257588c71SMiquel Raynal rtnl_unlock(); 24357588c71SMiquel Raynal if (ret) { 24457588c71SMiquel Raynal dev_err(&sdata->dev->dev, 24557588c71SMiquel Raynal "Restarting failure after channel change, aborting (%d)\n", ret); 24657588c71SMiquel Raynal goto end_scan; 24757588c71SMiquel Raynal } 24857588c71SMiquel Raynal 249e2c3e6f5SMiquel Raynal if (scan_req->type == NL802154_SCAN_ACTIVE) { 250e2c3e6f5SMiquel Raynal ret = mac802154_transmit_beacon_req(local, sdata); 251e2c3e6f5SMiquel Raynal if (ret) 252e2c3e6f5SMiquel Raynal dev_err(&sdata->dev->dev, 253e2c3e6f5SMiquel Raynal "Error when transmitting beacon request (%d)\n", ret); 254e2c3e6f5SMiquel Raynal } 255e2c3e6f5SMiquel Raynal 25657588c71SMiquel Raynal ieee802154_configure_durations(wpan_phy, page, channel); 25757588c71SMiquel Raynal scan_duration = mac802154_scan_get_channel_time(scan_req_duration, 25857588c71SMiquel Raynal wpan_phy->symbol_duration); 25957588c71SMiquel Raynal dev_dbg(&sdata->dev->dev, 26057588c71SMiquel Raynal "Scan page %u channel %u for %ums\n", 26157588c71SMiquel Raynal page, channel, jiffies_to_msecs(scan_duration)); 26257588c71SMiquel Raynal queue_delayed_work(local->mac_wq, &local->scan_work, scan_duration); 26357588c71SMiquel Raynal return; 26457588c71SMiquel Raynal 26557588c71SMiquel Raynal end_scan: 26657588c71SMiquel Raynal rtnl_lock(); 26757588c71SMiquel Raynal mac802154_scan_cleanup_locked(local, sdata, false); 26857588c71SMiquel Raynal rtnl_unlock(); 26957588c71SMiquel Raynal } 27057588c71SMiquel Raynal 27157588c71SMiquel Raynal int mac802154_trigger_scan_locked(struct ieee802154_sub_if_data *sdata, 27257588c71SMiquel Raynal struct cfg802154_scan_request *request) 27357588c71SMiquel Raynal { 27457588c71SMiquel Raynal struct ieee802154_local *local = sdata->local; 27557588c71SMiquel Raynal 27657588c71SMiquel Raynal ASSERT_RTNL(); 27757588c71SMiquel Raynal 27857588c71SMiquel Raynal if (mac802154_is_scanning(local)) 27957588c71SMiquel Raynal return -EBUSY; 28057588c71SMiquel Raynal 281e2c3e6f5SMiquel Raynal if (request->type != NL802154_SCAN_PASSIVE && 282e2c3e6f5SMiquel Raynal request->type != NL802154_SCAN_ACTIVE) 28357588c71SMiquel Raynal return -EOPNOTSUPP; 28457588c71SMiquel Raynal 28557588c71SMiquel Raynal /* Store scanning parameters */ 28657588c71SMiquel Raynal rcu_assign_pointer(local->scan_req, request); 28757588c71SMiquel Raynal 28857588c71SMiquel Raynal /* Software scanning requires to set promiscuous mode, so we need to 28957588c71SMiquel Raynal * pause the Tx queue during the entire operation. 29057588c71SMiquel Raynal */ 29157588c71SMiquel Raynal ieee802154_mlme_op_pre(local); 29257588c71SMiquel Raynal 29357588c71SMiquel Raynal sdata->required_filtering = IEEE802154_FILTERING_3_SCAN; 29457588c71SMiquel Raynal local->scan_page = request->page; 29557588c71SMiquel Raynal local->scan_channel = -1; 29657588c71SMiquel Raynal set_bit(IEEE802154_IS_SCANNING, &local->ongoing); 297e2c3e6f5SMiquel Raynal if (request->type == NL802154_SCAN_ACTIVE) 298e2c3e6f5SMiquel Raynal mac802154_scan_prepare_beacon_req(local); 29957588c71SMiquel Raynal 30057588c71SMiquel Raynal nl802154_scan_started(request->wpan_phy, request->wpan_dev); 30157588c71SMiquel Raynal 30257588c71SMiquel Raynal queue_delayed_work(local->mac_wq, &local->scan_work, 0); 30357588c71SMiquel Raynal 30457588c71SMiquel Raynal return 0; 30557588c71SMiquel Raynal } 30657588c71SMiquel Raynal 30757588c71SMiquel Raynal int mac802154_process_beacon(struct ieee802154_local *local, 30857588c71SMiquel Raynal struct sk_buff *skb, 30957588c71SMiquel Raynal u8 page, u8 channel) 31057588c71SMiquel Raynal { 31157588c71SMiquel Raynal struct ieee802154_beacon_hdr *bh = (void *)skb->data; 31257588c71SMiquel Raynal struct ieee802154_addr *src = &mac_cb(skb)->source; 31357588c71SMiquel Raynal struct cfg802154_scan_request *scan_req; 31457588c71SMiquel Raynal struct ieee802154_coord_desc desc; 31557588c71SMiquel Raynal 31657588c71SMiquel Raynal if (skb->len != sizeof(*bh)) 31757588c71SMiquel Raynal return -EINVAL; 31857588c71SMiquel Raynal 31957588c71SMiquel Raynal if (unlikely(src->mode == IEEE802154_ADDR_NONE)) 32057588c71SMiquel Raynal return -EINVAL; 32157588c71SMiquel Raynal 32257588c71SMiquel Raynal dev_dbg(&skb->dev->dev, 32357588c71SMiquel Raynal "BEACON received on page %u channel %u\n", 32457588c71SMiquel Raynal page, channel); 32557588c71SMiquel Raynal 32657588c71SMiquel Raynal memcpy(&desc.addr, src, sizeof(desc.addr)); 32757588c71SMiquel Raynal desc.page = page; 32857588c71SMiquel Raynal desc.channel = channel; 32957588c71SMiquel Raynal desc.link_quality = mac_cb(skb)->lqi; 33057588c71SMiquel Raynal desc.superframe_spec = get_unaligned_le16(skb->data); 33157588c71SMiquel Raynal desc.gts_permit = bh->gts_permit; 33257588c71SMiquel Raynal 33357588c71SMiquel Raynal trace_802154_scan_event(&desc); 33457588c71SMiquel Raynal 33557588c71SMiquel Raynal rcu_read_lock(); 33657588c71SMiquel Raynal scan_req = rcu_dereference(local->scan_req); 33757588c71SMiquel Raynal if (likely(scan_req)) 33857588c71SMiquel Raynal nl802154_scan_event(scan_req->wpan_phy, scan_req->wpan_dev, &desc); 33957588c71SMiquel Raynal rcu_read_unlock(); 34057588c71SMiquel Raynal 34157588c71SMiquel Raynal return 0; 34257588c71SMiquel Raynal } 3433accf476SMiquel Raynal 3443accf476SMiquel Raynal static int mac802154_transmit_beacon(struct ieee802154_local *local, 3453accf476SMiquel Raynal struct wpan_dev *wpan_dev) 3463accf476SMiquel Raynal { 3473accf476SMiquel Raynal struct cfg802154_beacon_request *beacon_req; 3483accf476SMiquel Raynal struct ieee802154_sub_if_data *sdata; 3493accf476SMiquel Raynal struct sk_buff *skb; 3503accf476SMiquel Raynal int ret; 3513accf476SMiquel Raynal 3523accf476SMiquel Raynal /* Update the sequence number */ 3533accf476SMiquel Raynal local->beacon.mhr.seq = atomic_inc_return(&wpan_dev->bsn) & 0xFF; 3543accf476SMiquel Raynal 3553accf476SMiquel Raynal skb = alloc_skb(IEEE802154_BEACON_SKB_SZ, GFP_KERNEL); 3563accf476SMiquel Raynal if (!skb) 3573accf476SMiquel Raynal return -ENOBUFS; 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 kfree_skb(skb); 3643accf476SMiquel Raynal return -EINVAL; 3653accf476SMiquel Raynal } 3663accf476SMiquel Raynal 3673accf476SMiquel Raynal sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev); 3683accf476SMiquel Raynal skb->dev = sdata->dev; 3693accf476SMiquel Raynal 3703accf476SMiquel Raynal rcu_read_unlock(); 3713accf476SMiquel Raynal 3723accf476SMiquel Raynal ret = ieee802154_beacon_push(skb, &local->beacon); 3733accf476SMiquel Raynal if (ret) { 3743accf476SMiquel Raynal kfree_skb(skb); 3753accf476SMiquel Raynal return ret; 3763accf476SMiquel Raynal } 3773accf476SMiquel Raynal 3781375e3baSMiquel Raynal /* Using the MLME transmission helper for sending beacons is a bit 3791375e3baSMiquel Raynal * overkill because we do not really care about the final outcome. 3801375e3baSMiquel Raynal * 3811375e3baSMiquel Raynal * Even though, going through the whole net stack with a regular 3821375e3baSMiquel Raynal * dev_queue_xmit() is not relevant either because we want beacons to be 3831375e3baSMiquel Raynal * sent "now" rather than go through the whole net stack scheduling 3841375e3baSMiquel Raynal * (qdisc & co). 3851375e3baSMiquel Raynal * 3861375e3baSMiquel Raynal * Finally, using ieee802154_subif_start_xmit() would only be an option 3871375e3baSMiquel Raynal * if we had a generic transmit helper which would acquire the 3881375e3baSMiquel Raynal * HARD_TX_LOCK() to prevent buffer handling conflicts with regular 3891375e3baSMiquel Raynal * packets. 3901375e3baSMiquel Raynal * 3911375e3baSMiquel Raynal * So for now we keep it simple and send beacons with our MLME helper, 3921375e3baSMiquel Raynal * even if it stops the ieee802154 queue entirely during these 3931375e3baSMiquel Raynal * transmissions, wich anyway does not have a huge impact on the 3941375e3baSMiquel Raynal * performances given the current design of the stack. 3951375e3baSMiquel Raynal */ 3961375e3baSMiquel Raynal return ieee802154_mlme_tx(local, sdata, skb); 3973accf476SMiquel Raynal } 3983accf476SMiquel Raynal 3993accf476SMiquel Raynal void mac802154_beacon_worker(struct work_struct *work) 4003accf476SMiquel Raynal { 4013accf476SMiquel Raynal struct ieee802154_local *local = 4023accf476SMiquel Raynal container_of(work, struct ieee802154_local, beacon_work.work); 4033accf476SMiquel Raynal struct cfg802154_beacon_request *beacon_req; 4043accf476SMiquel Raynal struct ieee802154_sub_if_data *sdata; 4053accf476SMiquel Raynal struct wpan_dev *wpan_dev; 406*d021d218SMiquel Raynal u8 interval; 4073accf476SMiquel Raynal int ret; 4083accf476SMiquel Raynal 4093accf476SMiquel Raynal rcu_read_lock(); 4103accf476SMiquel Raynal beacon_req = rcu_dereference(local->beacon_req); 4113accf476SMiquel Raynal if (unlikely(!beacon_req)) { 4123accf476SMiquel Raynal rcu_read_unlock(); 4133accf476SMiquel Raynal return; 4143accf476SMiquel Raynal } 4153accf476SMiquel Raynal 4163accf476SMiquel Raynal sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev); 4173accf476SMiquel Raynal 4183accf476SMiquel Raynal /* Wait an arbitrary amount of time in case we cannot use the device */ 4193accf476SMiquel Raynal if (local->suspended || !ieee802154_sdata_running(sdata)) { 4203accf476SMiquel Raynal rcu_read_unlock(); 4213accf476SMiquel Raynal queue_delayed_work(local->mac_wq, &local->beacon_work, 4223accf476SMiquel Raynal msecs_to_jiffies(1000)); 4233accf476SMiquel Raynal return; 4243accf476SMiquel Raynal } 4253accf476SMiquel Raynal 4263accf476SMiquel Raynal wpan_dev = beacon_req->wpan_dev; 427*d021d218SMiquel Raynal interval = beacon_req->interval; 4283accf476SMiquel Raynal 4293accf476SMiquel Raynal rcu_read_unlock(); 4303accf476SMiquel Raynal 4313accf476SMiquel Raynal dev_dbg(&sdata->dev->dev, "Sending beacon\n"); 4323accf476SMiquel Raynal ret = mac802154_transmit_beacon(local, wpan_dev); 4333accf476SMiquel Raynal if (ret) 4343accf476SMiquel Raynal dev_err(&sdata->dev->dev, 4353accf476SMiquel Raynal "Beacon could not be transmitted (%d)\n", ret); 4363accf476SMiquel Raynal 437*d021d218SMiquel Raynal if (interval < IEEE802154_ACTIVE_SCAN_DURATION) 4383accf476SMiquel Raynal queue_delayed_work(local->mac_wq, &local->beacon_work, 4393accf476SMiquel Raynal local->beacon_interval); 4403accf476SMiquel Raynal } 4413accf476SMiquel Raynal 4423accf476SMiquel Raynal int mac802154_stop_beacons_locked(struct ieee802154_local *local, 4433accf476SMiquel Raynal struct ieee802154_sub_if_data *sdata) 4443accf476SMiquel Raynal { 4453accf476SMiquel Raynal struct wpan_dev *wpan_dev = &sdata->wpan_dev; 4463accf476SMiquel Raynal struct cfg802154_beacon_request *request; 4473accf476SMiquel Raynal 4483accf476SMiquel Raynal ASSERT_RTNL(); 4493accf476SMiquel Raynal 4503accf476SMiquel Raynal if (!mac802154_is_beaconing(local)) 4513accf476SMiquel Raynal return -ESRCH; 4523accf476SMiquel Raynal 4533accf476SMiquel Raynal clear_bit(IEEE802154_IS_BEACONING, &local->ongoing); 4543accf476SMiquel Raynal cancel_delayed_work(&local->beacon_work); 4553accf476SMiquel Raynal request = rcu_replace_pointer(local->beacon_req, NULL, 1); 4563accf476SMiquel Raynal if (!request) 4573accf476SMiquel Raynal return 0; 4583accf476SMiquel Raynal kfree_rcu(request); 4593accf476SMiquel Raynal 4603accf476SMiquel Raynal nl802154_beaconing_done(wpan_dev); 4613accf476SMiquel Raynal 4623accf476SMiquel Raynal return 0; 4633accf476SMiquel Raynal } 4643accf476SMiquel Raynal 4653accf476SMiquel Raynal int mac802154_send_beacons_locked(struct ieee802154_sub_if_data *sdata, 4663accf476SMiquel Raynal struct cfg802154_beacon_request *request) 4673accf476SMiquel Raynal { 4683accf476SMiquel Raynal struct ieee802154_local *local = sdata->local; 4693accf476SMiquel Raynal 4703accf476SMiquel Raynal ASSERT_RTNL(); 4713accf476SMiquel Raynal 4723accf476SMiquel Raynal if (mac802154_is_beaconing(local)) 4733accf476SMiquel Raynal mac802154_stop_beacons_locked(local, sdata); 4743accf476SMiquel Raynal 4753accf476SMiquel Raynal /* Store beaconing parameters */ 4763accf476SMiquel Raynal rcu_assign_pointer(local->beacon_req, request); 4773accf476SMiquel Raynal 4783accf476SMiquel Raynal set_bit(IEEE802154_IS_BEACONING, &local->ongoing); 4793accf476SMiquel Raynal 4803accf476SMiquel Raynal memset(&local->beacon, 0, sizeof(local->beacon)); 4813accf476SMiquel Raynal local->beacon.mhr.fc.type = IEEE802154_FC_TYPE_BEACON; 4823accf476SMiquel Raynal local->beacon.mhr.fc.security_enabled = 0; 4833accf476SMiquel Raynal local->beacon.mhr.fc.frame_pending = 0; 4843accf476SMiquel Raynal local->beacon.mhr.fc.ack_request = 0; 4853accf476SMiquel Raynal local->beacon.mhr.fc.intra_pan = 0; 4863accf476SMiquel Raynal local->beacon.mhr.fc.dest_addr_mode = IEEE802154_NO_ADDRESSING; 4873accf476SMiquel Raynal local->beacon.mhr.fc.version = IEEE802154_2003_STD; 4883accf476SMiquel Raynal local->beacon.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING; 4893accf476SMiquel Raynal atomic_set(&request->wpan_dev->bsn, -1); 4903accf476SMiquel Raynal local->beacon.mhr.source.mode = IEEE802154_ADDR_LONG; 4918338304cSMiquel Raynal local->beacon.mhr.source.pan_id = request->wpan_dev->pan_id; 4928338304cSMiquel Raynal local->beacon.mhr.source.extended_addr = request->wpan_dev->extended_addr; 4933accf476SMiquel Raynal local->beacon.mac_pl.beacon_order = request->interval; 494*d021d218SMiquel Raynal if (request->interval <= IEEE802154_MAX_SCAN_DURATION) 4953accf476SMiquel Raynal local->beacon.mac_pl.superframe_order = request->interval; 4963accf476SMiquel Raynal local->beacon.mac_pl.final_cap_slot = 0xf; 4973accf476SMiquel Raynal local->beacon.mac_pl.battery_life_ext = 0; 498*d021d218SMiquel Raynal /* TODO: Fill this field with the coordinator situation in the network */ 4993accf476SMiquel Raynal local->beacon.mac_pl.pan_coordinator = 1; 5003accf476SMiquel Raynal local->beacon.mac_pl.assoc_permit = 1; 5013accf476SMiquel Raynal 502*d021d218SMiquel Raynal if (request->interval == IEEE802154_ACTIVE_SCAN_DURATION) 503*d021d218SMiquel Raynal return 0; 504*d021d218SMiquel Raynal 5053accf476SMiquel Raynal /* Start the beacon work */ 5063accf476SMiquel Raynal local->beacon_interval = 5073accf476SMiquel Raynal mac802154_scan_get_channel_time(request->interval, 5083accf476SMiquel Raynal request->wpan_phy->symbol_duration); 5093accf476SMiquel Raynal queue_delayed_work(local->mac_wq, &local->beacon_work, 0); 5103accf476SMiquel Raynal 5113accf476SMiquel Raynal return 0; 5123accf476SMiquel Raynal } 513