xref: /openbmc/linux/net/mac802154/scan.c (revision 3accf4762734a69ebd03cba989249c78ac7dfc7e)
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 
19*3accf476SMiquel Raynal #define IEEE802154_BEACON_MHR_SZ 13
20*3accf476SMiquel Raynal #define IEEE802154_BEACON_PL_SZ 4
21*3accf476SMiquel Raynal #define IEEE802154_BEACON_SKB_SZ (IEEE802154_BEACON_MHR_SZ + \
22*3accf476SMiquel Raynal 				  IEEE802154_BEACON_PL_SZ)
23*3accf476SMiquel 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 }
294*3accf476SMiquel Raynal 
295*3accf476SMiquel Raynal static int mac802154_transmit_beacon(struct ieee802154_local *local,
296*3accf476SMiquel Raynal 				     struct wpan_dev *wpan_dev)
297*3accf476SMiquel Raynal {
298*3accf476SMiquel Raynal 	struct cfg802154_beacon_request *beacon_req;
299*3accf476SMiquel Raynal 	struct ieee802154_sub_if_data *sdata;
300*3accf476SMiquel Raynal 	struct sk_buff *skb;
301*3accf476SMiquel Raynal 	int ret;
302*3accf476SMiquel Raynal 
303*3accf476SMiquel Raynal 	/* Update the sequence number */
304*3accf476SMiquel Raynal 	local->beacon.mhr.seq = atomic_inc_return(&wpan_dev->bsn) & 0xFF;
305*3accf476SMiquel Raynal 
306*3accf476SMiquel Raynal 	skb = alloc_skb(IEEE802154_BEACON_SKB_SZ, GFP_KERNEL);
307*3accf476SMiquel Raynal 	if (!skb)
308*3accf476SMiquel Raynal 		return -ENOBUFS;
309*3accf476SMiquel Raynal 
310*3accf476SMiquel Raynal 	rcu_read_lock();
311*3accf476SMiquel Raynal 	beacon_req = rcu_dereference(local->beacon_req);
312*3accf476SMiquel Raynal 	if (unlikely(!beacon_req)) {
313*3accf476SMiquel Raynal 		rcu_read_unlock();
314*3accf476SMiquel Raynal 		kfree_skb(skb);
315*3accf476SMiquel Raynal 		return -EINVAL;
316*3accf476SMiquel Raynal 	}
317*3accf476SMiquel Raynal 
318*3accf476SMiquel Raynal 	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev);
319*3accf476SMiquel Raynal 	skb->dev = sdata->dev;
320*3accf476SMiquel Raynal 
321*3accf476SMiquel Raynal 	rcu_read_unlock();
322*3accf476SMiquel Raynal 
323*3accf476SMiquel Raynal 	ret = ieee802154_beacon_push(skb, &local->beacon);
324*3accf476SMiquel Raynal 	if (ret) {
325*3accf476SMiquel Raynal 		kfree_skb(skb);
326*3accf476SMiquel Raynal 		return ret;
327*3accf476SMiquel Raynal 	}
328*3accf476SMiquel Raynal 
329*3accf476SMiquel Raynal 	return ieee802154_subif_start_xmit(skb, sdata->dev);
330*3accf476SMiquel Raynal }
331*3accf476SMiquel Raynal 
332*3accf476SMiquel Raynal void mac802154_beacon_worker(struct work_struct *work)
333*3accf476SMiquel Raynal {
334*3accf476SMiquel Raynal 	struct ieee802154_local *local =
335*3accf476SMiquel Raynal 		container_of(work, struct ieee802154_local, beacon_work.work);
336*3accf476SMiquel Raynal 	struct cfg802154_beacon_request *beacon_req;
337*3accf476SMiquel Raynal 	struct ieee802154_sub_if_data *sdata;
338*3accf476SMiquel Raynal 	struct wpan_dev *wpan_dev;
339*3accf476SMiquel Raynal 	int ret;
340*3accf476SMiquel Raynal 
341*3accf476SMiquel Raynal 	rcu_read_lock();
342*3accf476SMiquel Raynal 	beacon_req = rcu_dereference(local->beacon_req);
343*3accf476SMiquel Raynal 	if (unlikely(!beacon_req)) {
344*3accf476SMiquel Raynal 		rcu_read_unlock();
345*3accf476SMiquel Raynal 		return;
346*3accf476SMiquel Raynal 	}
347*3accf476SMiquel Raynal 
348*3accf476SMiquel Raynal 	sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev);
349*3accf476SMiquel Raynal 
350*3accf476SMiquel Raynal 	/* Wait an arbitrary amount of time in case we cannot use the device */
351*3accf476SMiquel Raynal 	if (local->suspended || !ieee802154_sdata_running(sdata)) {
352*3accf476SMiquel Raynal 		rcu_read_unlock();
353*3accf476SMiquel Raynal 		queue_delayed_work(local->mac_wq, &local->beacon_work,
354*3accf476SMiquel Raynal 				   msecs_to_jiffies(1000));
355*3accf476SMiquel Raynal 		return;
356*3accf476SMiquel Raynal 	}
357*3accf476SMiquel Raynal 
358*3accf476SMiquel Raynal 	wpan_dev = beacon_req->wpan_dev;
359*3accf476SMiquel Raynal 
360*3accf476SMiquel Raynal 	rcu_read_unlock();
361*3accf476SMiquel Raynal 
362*3accf476SMiquel Raynal 	dev_dbg(&sdata->dev->dev, "Sending beacon\n");
363*3accf476SMiquel Raynal 	ret = mac802154_transmit_beacon(local, wpan_dev);
364*3accf476SMiquel Raynal 	if (ret)
365*3accf476SMiquel Raynal 		dev_err(&sdata->dev->dev,
366*3accf476SMiquel Raynal 			"Beacon could not be transmitted (%d)\n", ret);
367*3accf476SMiquel Raynal 
368*3accf476SMiquel Raynal 	if (local->beacon_interval >= 0)
369*3accf476SMiquel Raynal 		queue_delayed_work(local->mac_wq, &local->beacon_work,
370*3accf476SMiquel Raynal 				   local->beacon_interval);
371*3accf476SMiquel Raynal }
372*3accf476SMiquel Raynal 
373*3accf476SMiquel Raynal int mac802154_stop_beacons_locked(struct ieee802154_local *local,
374*3accf476SMiquel Raynal 				  struct ieee802154_sub_if_data *sdata)
375*3accf476SMiquel Raynal {
376*3accf476SMiquel Raynal 	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
377*3accf476SMiquel Raynal 	struct cfg802154_beacon_request *request;
378*3accf476SMiquel Raynal 
379*3accf476SMiquel Raynal 	ASSERT_RTNL();
380*3accf476SMiquel Raynal 
381*3accf476SMiquel Raynal 	if (!mac802154_is_beaconing(local))
382*3accf476SMiquel Raynal 		return -ESRCH;
383*3accf476SMiquel Raynal 
384*3accf476SMiquel Raynal 	clear_bit(IEEE802154_IS_BEACONING, &local->ongoing);
385*3accf476SMiquel Raynal 	cancel_delayed_work(&local->beacon_work);
386*3accf476SMiquel Raynal 	request = rcu_replace_pointer(local->beacon_req, NULL, 1);
387*3accf476SMiquel Raynal 	if (!request)
388*3accf476SMiquel Raynal 		return 0;
389*3accf476SMiquel Raynal 	kfree_rcu(request);
390*3accf476SMiquel Raynal 
391*3accf476SMiquel Raynal 	nl802154_beaconing_done(wpan_dev);
392*3accf476SMiquel Raynal 
393*3accf476SMiquel Raynal 	return 0;
394*3accf476SMiquel Raynal }
395*3accf476SMiquel Raynal 
396*3accf476SMiquel Raynal int mac802154_send_beacons_locked(struct ieee802154_sub_if_data *sdata,
397*3accf476SMiquel Raynal 				  struct cfg802154_beacon_request *request)
398*3accf476SMiquel Raynal {
399*3accf476SMiquel Raynal 	struct ieee802154_local *local = sdata->local;
400*3accf476SMiquel Raynal 
401*3accf476SMiquel Raynal 	ASSERT_RTNL();
402*3accf476SMiquel Raynal 
403*3accf476SMiquel Raynal 	if (mac802154_is_beaconing(local))
404*3accf476SMiquel Raynal 		mac802154_stop_beacons_locked(local, sdata);
405*3accf476SMiquel Raynal 
406*3accf476SMiquel Raynal 	/* Store beaconing parameters */
407*3accf476SMiquel Raynal 	rcu_assign_pointer(local->beacon_req, request);
408*3accf476SMiquel Raynal 
409*3accf476SMiquel Raynal 	set_bit(IEEE802154_IS_BEACONING, &local->ongoing);
410*3accf476SMiquel Raynal 
411*3accf476SMiquel Raynal 	memset(&local->beacon, 0, sizeof(local->beacon));
412*3accf476SMiquel Raynal 	local->beacon.mhr.fc.type = IEEE802154_FC_TYPE_BEACON;
413*3accf476SMiquel Raynal 	local->beacon.mhr.fc.security_enabled = 0;
414*3accf476SMiquel Raynal 	local->beacon.mhr.fc.frame_pending = 0;
415*3accf476SMiquel Raynal 	local->beacon.mhr.fc.ack_request = 0;
416*3accf476SMiquel Raynal 	local->beacon.mhr.fc.intra_pan = 0;
417*3accf476SMiquel Raynal 	local->beacon.mhr.fc.dest_addr_mode = IEEE802154_NO_ADDRESSING;
418*3accf476SMiquel Raynal 	local->beacon.mhr.fc.version = IEEE802154_2003_STD;
419*3accf476SMiquel Raynal 	local->beacon.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING;
420*3accf476SMiquel Raynal 	atomic_set(&request->wpan_dev->bsn, -1);
421*3accf476SMiquel Raynal 	local->beacon.mhr.source.mode = IEEE802154_ADDR_LONG;
422*3accf476SMiquel Raynal 	local->beacon.mhr.source.pan_id = cpu_to_le16(request->wpan_dev->pan_id);
423*3accf476SMiquel Raynal 	local->beacon.mhr.source.extended_addr = cpu_to_le64(request->wpan_dev->extended_addr);
424*3accf476SMiquel Raynal 	local->beacon.mac_pl.beacon_order = request->interval;
425*3accf476SMiquel Raynal 	local->beacon.mac_pl.superframe_order = request->interval;
426*3accf476SMiquel Raynal 	local->beacon.mac_pl.final_cap_slot = 0xf;
427*3accf476SMiquel Raynal 	local->beacon.mac_pl.battery_life_ext = 0;
428*3accf476SMiquel Raynal 	/* TODO: Fill this field depending on the coordinator capacity */
429*3accf476SMiquel Raynal 	local->beacon.mac_pl.pan_coordinator = 1;
430*3accf476SMiquel Raynal 	local->beacon.mac_pl.assoc_permit = 1;
431*3accf476SMiquel Raynal 
432*3accf476SMiquel Raynal 	/* Start the beacon work */
433*3accf476SMiquel Raynal 	local->beacon_interval =
434*3accf476SMiquel Raynal 		mac802154_scan_get_channel_time(request->interval,
435*3accf476SMiquel Raynal 						request->wpan_phy->symbol_duration);
436*3accf476SMiquel Raynal 	queue_delayed_work(local->mac_wq, &local->beacon_work, 0);
437*3accf476SMiquel Raynal 
438*3accf476SMiquel Raynal 	return 0;
439*3accf476SMiquel Raynal }
440