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