1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license.  When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2012 - 2014, 2020 Intel Corporation. All rights reserved.
9 * Copyright(c) 2016 Intel Deutschland GmbH
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * The full GNU General Public License is included in this distribution
21 * in the file called COPYING.
22 *
23 * Contact Information:
24 *  Intel Linux Wireless <linuxwifi@intel.com>
25 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *
27 * BSD LICENSE
28 *
29 * Copyright(c) 2012 - 2014, 2020 Intel Corporation. All rights reserved.
30 * Copyright(c) 2016 Intel Deutschland GmbH
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 *
37 *  * Redistributions of source code must retain the above copyright
38 *    notice, this list of conditions and the following disclaimer.
39 *  * Redistributions in binary form must reproduce the above copyright
40 *    notice, this list of conditions and the following disclaimer in
41 *    the documentation and/or other materials provided with the
42 *    distribution.
43 *  * Neither the name Intel Corporation nor the names of its
44 *    contributors may be used to endorse or promote products derived
45 *    from this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 *
59 *****************************************************************************/
60
61#include <net/mac80211.h>
62#include "fw-api.h"
63#include "mvm.h"
64
65struct iwl_mvm_iface_iterator_data {
66	struct ieee80211_vif *ignore_vif;
67	int idx;
68
69	struct iwl_mvm_phy_ctxt *phyctxt;
70
71	u16 ids[MAX_MACS_IN_BINDING];
72	u16 colors[MAX_MACS_IN_BINDING];
73};
74
75static int iwl_mvm_binding_cmd(struct iwl_mvm *mvm, u32 action,
76			       struct iwl_mvm_iface_iterator_data *data)
77{
78	struct iwl_binding_cmd cmd;
79	struct iwl_mvm_phy_ctxt *phyctxt = data->phyctxt;
80	int i, ret;
81	u32 status;
82	int size;
83
84	memset(&cmd, 0, sizeof(cmd));
85
86	if (fw_has_capa(&mvm->fw->ucode_capa,
87			IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT)) {
88		size = sizeof(cmd);
89		cmd.lmac_id = cpu_to_le32(iwl_mvm_get_lmac_id(mvm->fw,
90							      phyctxt->channel->band));
91	} else {
92		size = IWL_BINDING_CMD_SIZE_V1;
93	}
94
95	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(phyctxt->id,
96							   phyctxt->color));
97	cmd.action = cpu_to_le32(action);
98	cmd.phy = cpu_to_le32(FW_CMD_ID_AND_COLOR(phyctxt->id,
99						  phyctxt->color));
100
101	for (i = 0; i < MAX_MACS_IN_BINDING; i++)
102		cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
103	for (i = 0; i < data->idx; i++)
104		cmd.macs[i] = cpu_to_le32(FW_CMD_ID_AND_COLOR(data->ids[i],
105							      data->colors[i]));
106
107	status = 0;
108	ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
109					  size, &cmd, &status);
110	if (ret) {
111		IWL_ERR(mvm, "Failed to send binding (action:%d): %d\n",
112			action, ret);
113		return ret;
114	}
115
116	if (status) {
117		IWL_ERR(mvm, "Binding command failed: %u\n", status);
118		ret = -EIO;
119	}
120
121	return ret;
122}
123
124static void iwl_mvm_iface_iterator(void *_data, u8 *mac,
125				   struct ieee80211_vif *vif)
126{
127	struct iwl_mvm_iface_iterator_data *data = _data;
128	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
129
130	if (vif == data->ignore_vif)
131		return;
132
133	if (mvmvif->phy_ctxt != data->phyctxt)
134		return;
135
136	if (WARN_ON_ONCE(data->idx >= MAX_MACS_IN_BINDING))
137		return;
138
139	data->ids[data->idx] = mvmvif->id;
140	data->colors[data->idx] = mvmvif->color;
141	data->idx++;
142}
143
144static int iwl_mvm_binding_update(struct iwl_mvm *mvm,
145				  struct ieee80211_vif *vif,
146				  struct iwl_mvm_phy_ctxt *phyctxt,
147				  bool add)
148{
149	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
150	struct iwl_mvm_iface_iterator_data data = {
151		.ignore_vif = vif,
152		.phyctxt = phyctxt,
153	};
154	u32 action = FW_CTXT_ACTION_MODIFY;
155
156	lockdep_assert_held(&mvm->mutex);
157
158	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
159						   IEEE80211_IFACE_ITER_NORMAL,
160						   iwl_mvm_iface_iterator,
161						   &data);
162
163	/*
164	 * If there are no other interfaces yet we
165	 * need to create a new binding.
166	 */
167	if (data.idx == 0) {
168		if (add)
169			action = FW_CTXT_ACTION_ADD;
170		else
171			action = FW_CTXT_ACTION_REMOVE;
172	}
173
174	if (add) {
175		if (WARN_ON_ONCE(data.idx >= MAX_MACS_IN_BINDING))
176			return -EINVAL;
177
178		data.ids[data.idx] = mvmvif->id;
179		data.colors[data.idx] = mvmvif->color;
180		data.idx++;
181	}
182
183	return iwl_mvm_binding_cmd(mvm, action, &data);
184}
185
186int iwl_mvm_binding_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
187{
188	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
189
190	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
191		return -EINVAL;
192
193	/*
194	 * Update SF - Disable if needed. if this fails, SF might still be on
195	 * while many macs are bound, which is forbidden - so fail the binding.
196	 */
197	if (iwl_mvm_sf_update(mvm, vif, false))
198		return -EINVAL;
199
200	return iwl_mvm_binding_update(mvm, vif, mvmvif->phy_ctxt, true);
201}
202
203int iwl_mvm_binding_remove_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
204{
205	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
206	int ret;
207
208	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
209		return -EINVAL;
210
211	ret = iwl_mvm_binding_update(mvm, vif, mvmvif->phy_ctxt, false);
212
213	if (!ret)
214		if (iwl_mvm_sf_update(mvm, vif, true))
215			IWL_ERR(mvm, "Failed to update SF state\n");
216
217	return ret;
218}
219