1// SPDX-License-Identifier: BSD-3-Clause-Clear
2/*
3 * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
4 */
5
6#include <net/mac80211.h>
7#include "core.h"
8#include "mac.h"
9#include "p2p.h"
10
11static void ath12k_p2p_noa_ie_fill(u8 *data, size_t len,
12				   const struct ath12k_wmi_p2p_noa_info *noa)
13{
14	struct ieee80211_p2p_noa_attr *noa_attr;
15	u8 ctwindow = le32_get_bits(noa->noa_attr, WMI_P2P_NOA_INFO_CTWIN_TU);
16	bool oppps = le32_get_bits(noa->noa_attr, WMI_P2P_NOA_INFO_OPP_PS);
17	__le16 *noa_attr_len;
18	u16 attr_len;
19	u8 noa_descriptors = le32_get_bits(noa->noa_attr,
20					   WMI_P2P_NOA_INFO_DESC_NUM);
21	int i;
22
23	/* P2P IE */
24	data[0] = WLAN_EID_VENDOR_SPECIFIC;
25	data[1] = len - 2;
26	data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
27	data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
28	data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
29	data[5] = WLAN_OUI_TYPE_WFA_P2P;
30
31	/* NOA ATTR */
32	data[6] = IEEE80211_P2P_ATTR_ABSENCE_NOTICE;
33	noa_attr_len = (__le16 *)&data[7]; /* 2 bytes */
34	noa_attr = (struct ieee80211_p2p_noa_attr *)&data[9];
35
36	noa_attr->index = le32_get_bits(noa->noa_attr,
37					WMI_P2P_NOA_INFO_INDEX);
38	noa_attr->oppps_ctwindow = ctwindow;
39	if (oppps)
40		noa_attr->oppps_ctwindow |= IEEE80211_P2P_OPPPS_ENABLE_BIT;
41
42	for (i = 0; i < noa_descriptors; i++) {
43		noa_attr->desc[i].count =
44			__le32_to_cpu(noa->descriptors[i].type_count);
45		noa_attr->desc[i].duration = noa->descriptors[i].duration;
46		noa_attr->desc[i].interval = noa->descriptors[i].interval;
47		noa_attr->desc[i].start_time = noa->descriptors[i].start_time;
48	}
49
50	attr_len = 2; /* index + oppps_ctwindow */
51	attr_len += noa_descriptors * sizeof(struct ieee80211_p2p_noa_desc);
52	*noa_attr_len = __cpu_to_le16(attr_len);
53}
54
55static size_t ath12k_p2p_noa_ie_len_compute(const struct ath12k_wmi_p2p_noa_info *noa)
56{
57	size_t len = 0;
58
59	if (!(le32_get_bits(noa->noa_attr, WMI_P2P_NOA_INFO_DESC_NUM)) &&
60	    !(le32_get_bits(noa->noa_attr, WMI_P2P_NOA_INFO_OPP_PS)))
61		return 0;
62
63	len += 1 + 1 + 4; /* EID + len + OUI */
64	len += 1 + 2; /* noa attr + attr len */
65	len += 1 + 1; /* index + oppps_ctwindow */
66	len += le32_get_bits(noa->noa_attr, WMI_P2P_NOA_INFO_DESC_NUM) *
67			sizeof(struct ieee80211_p2p_noa_desc);
68
69	return len;
70}
71
72static void ath12k_p2p_noa_ie_assign(struct ath12k_vif *arvif, void *ie,
73				     size_t len)
74{
75	struct ath12k *ar = arvif->ar;
76
77	lockdep_assert_held(&ar->data_lock);
78
79	kfree(arvif->u.ap.noa_data);
80
81	arvif->u.ap.noa_data = ie;
82	arvif->u.ap.noa_len = len;
83}
84
85static void __ath12k_p2p_noa_update(struct ath12k_vif *arvif,
86				    const struct ath12k_wmi_p2p_noa_info *noa)
87{
88	struct ath12k *ar = arvif->ar;
89	void *ie;
90	size_t len;
91
92	lockdep_assert_held(&ar->data_lock);
93
94	ath12k_p2p_noa_ie_assign(arvif, NULL, 0);
95
96	len = ath12k_p2p_noa_ie_len_compute(noa);
97	if (!len)
98		return;
99
100	ie = kmalloc(len, GFP_ATOMIC);
101	if (!ie)
102		return;
103
104	ath12k_p2p_noa_ie_fill(ie, len, noa);
105	ath12k_p2p_noa_ie_assign(arvif, ie, len);
106}
107
108void ath12k_p2p_noa_update(struct ath12k_vif *arvif,
109			   const struct ath12k_wmi_p2p_noa_info *noa)
110{
111	struct ath12k *ar = arvif->ar;
112
113	spin_lock_bh(&ar->data_lock);
114	__ath12k_p2p_noa_update(arvif, noa);
115	spin_unlock_bh(&ar->data_lock);
116}
117
118static void ath12k_p2p_noa_update_vdev_iter(void *data, u8 *mac,
119					    struct ieee80211_vif *vif)
120{
121	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
122	struct ath12k_p2p_noa_arg *arg = data;
123
124	if (arvif->vdev_id != arg->vdev_id)
125		return;
126
127	ath12k_p2p_noa_update(arvif, arg->noa);
128}
129
130void ath12k_p2p_noa_update_by_vdev_id(struct ath12k *ar, u32 vdev_id,
131				      const struct ath12k_wmi_p2p_noa_info *noa)
132{
133	struct ath12k_p2p_noa_arg arg = {
134		.vdev_id = vdev_id,
135		.noa = noa,
136	};
137
138	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
139						   IEEE80211_IFACE_ITER_NORMAL,
140						   ath12k_p2p_noa_update_vdev_iter,
141						   &arg);
142}
143