xref: /dflybsd-src/sys/dev/netif/iwm/if_iwm_binding.c (revision 4cbc7cf96a5a52309aab9d8db3e0335042767af2)
124a8d46aSMatthew Dillon /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
224a8d46aSMatthew Dillon 
324a8d46aSMatthew Dillon /*
424a8d46aSMatthew Dillon  * Copyright (c) 2014 genua mbh <info@genua.de>
524a8d46aSMatthew Dillon  * Copyright (c) 2014 Fixup Software Ltd.
624a8d46aSMatthew Dillon  *
724a8d46aSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
824a8d46aSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
924a8d46aSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
1024a8d46aSMatthew Dillon  *
1124a8d46aSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1224a8d46aSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1324a8d46aSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1424a8d46aSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1524a8d46aSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1624a8d46aSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1724a8d46aSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1824a8d46aSMatthew Dillon  */
1924a8d46aSMatthew Dillon 
2024a8d46aSMatthew Dillon /*-
2124a8d46aSMatthew Dillon  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
2224a8d46aSMatthew Dillon  * which were used as the reference documentation for this implementation.
2324a8d46aSMatthew Dillon  *
2424a8d46aSMatthew Dillon  * Driver version we are currently based off of is
2524a8d46aSMatthew Dillon  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
2624a8d46aSMatthew Dillon  *
2724a8d46aSMatthew Dillon  ***********************************************************************
2824a8d46aSMatthew Dillon  *
2924a8d46aSMatthew Dillon  * This file is provided under a dual BSD/GPLv2 license.  When using or
3024a8d46aSMatthew Dillon  * redistributing this file, you may do so under either license.
3124a8d46aSMatthew Dillon  *
3224a8d46aSMatthew Dillon  * GPL LICENSE SUMMARY
3324a8d46aSMatthew Dillon  *
34f16ef749SImre Vadász  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
3524a8d46aSMatthew Dillon  *
3624a8d46aSMatthew Dillon  * This program is free software; you can redistribute it and/or modify
3724a8d46aSMatthew Dillon  * it under the terms of version 2 of the GNU General Public License as
3824a8d46aSMatthew Dillon  * published by the Free Software Foundation.
3924a8d46aSMatthew Dillon  *
4024a8d46aSMatthew Dillon  * This program is distributed in the hope that it will be useful, but
4124a8d46aSMatthew Dillon  * WITHOUT ANY WARRANTY; without even the implied warranty of
4224a8d46aSMatthew Dillon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4324a8d46aSMatthew Dillon  * General Public License for more details.
4424a8d46aSMatthew Dillon  *
4524a8d46aSMatthew Dillon  * You should have received a copy of the GNU General Public License
4624a8d46aSMatthew Dillon  * along with this program; if not, write to the Free Software
4724a8d46aSMatthew Dillon  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
4824a8d46aSMatthew Dillon  * USA
4924a8d46aSMatthew Dillon  *
5024a8d46aSMatthew Dillon  * The full GNU General Public License is included in this distribution
5124a8d46aSMatthew Dillon  * in the file called COPYING.
5224a8d46aSMatthew Dillon  *
5324a8d46aSMatthew Dillon  * Contact Information:
54f16ef749SImre Vadász  *  Intel Linux Wireless <linuxwifi@intel.com>
5524a8d46aSMatthew Dillon  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
5624a8d46aSMatthew Dillon  *
5724a8d46aSMatthew Dillon  * BSD LICENSE
5824a8d46aSMatthew Dillon  *
59f16ef749SImre Vadász  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
6024a8d46aSMatthew Dillon  * All rights reserved.
6124a8d46aSMatthew Dillon  *
6224a8d46aSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
6324a8d46aSMatthew Dillon  * modification, are permitted provided that the following conditions
6424a8d46aSMatthew Dillon  * are met:
6524a8d46aSMatthew Dillon  *
6624a8d46aSMatthew Dillon  *  * Redistributions of source code must retain the above copyright
6724a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
6824a8d46aSMatthew Dillon  *  * Redistributions in binary form must reproduce the above copyright
6924a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
7024a8d46aSMatthew Dillon  *    the documentation and/or other materials provided with the
7124a8d46aSMatthew Dillon  *    distribution.
7224a8d46aSMatthew Dillon  *  * Neither the name Intel Corporation nor the names of its
7324a8d46aSMatthew Dillon  *    contributors may be used to endorse or promote products derived
7424a8d46aSMatthew Dillon  *    from this software without specific prior written permission.
7524a8d46aSMatthew Dillon  *
7624a8d46aSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7724a8d46aSMatthew Dillon  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7824a8d46aSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7924a8d46aSMatthew Dillon  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8024a8d46aSMatthew Dillon  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8124a8d46aSMatthew Dillon  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8224a8d46aSMatthew Dillon  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8324a8d46aSMatthew Dillon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8424a8d46aSMatthew Dillon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8524a8d46aSMatthew Dillon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8624a8d46aSMatthew Dillon  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8724a8d46aSMatthew Dillon  */
8824a8d46aSMatthew Dillon 
8924a8d46aSMatthew Dillon #include <sys/param.h>
9024a8d46aSMatthew Dillon #include <sys/bus.h>
916acbba79SMatthew Dillon #include <sys/conf.h>
9224a8d46aSMatthew Dillon #include <sys/endian.h>
9324a8d46aSMatthew Dillon #include <sys/firmware.h>
9424a8d46aSMatthew Dillon #include <sys/kernel.h>
9524a8d46aSMatthew Dillon #include <sys/malloc.h>
9624a8d46aSMatthew Dillon #include <sys/mbuf.h>
9724a8d46aSMatthew Dillon #include <sys/rman.h>
9824a8d46aSMatthew Dillon #include <sys/sysctl.h>
9924a8d46aSMatthew Dillon #include <sys/linker.h>
10024a8d46aSMatthew Dillon 
10124a8d46aSMatthew Dillon #include <machine/endian.h>
10224a8d46aSMatthew Dillon 
10345bc40b1SMatthew Dillon #include <bus/pci/pcivar.h>
10445bc40b1SMatthew Dillon #include <bus/pci/pcireg.h>
10524a8d46aSMatthew Dillon 
10624a8d46aSMatthew Dillon #include <net/bpf.h>
10724a8d46aSMatthew Dillon 
10824a8d46aSMatthew Dillon #include <net/if.h>
10924a8d46aSMatthew Dillon #include <net/if_var.h>
11024a8d46aSMatthew Dillon #include <net/if_arp.h>
11124a8d46aSMatthew Dillon #include <net/if_dl.h>
11224a8d46aSMatthew Dillon #include <net/if_media.h>
11324a8d46aSMatthew Dillon #include <net/if_types.h>
11424a8d46aSMatthew Dillon 
11524a8d46aSMatthew Dillon #include <netinet/in.h>
11624a8d46aSMatthew Dillon #include <netinet/in_systm.h>
11724a8d46aSMatthew Dillon #include <netinet/if_ether.h>
11824a8d46aSMatthew Dillon #include <netinet/ip.h>
11924a8d46aSMatthew Dillon 
12045bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
12145bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_regdomain.h>
12245bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_ratectl.h>
12345bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_radiotap.h>
12424a8d46aSMatthew Dillon 
12545bc40b1SMatthew Dillon #include "if_iwmreg.h"
12645bc40b1SMatthew Dillon #include "if_iwmvar.h"
12745bc40b1SMatthew Dillon #include "if_iwm_debug.h"
12845bc40b1SMatthew Dillon #include "if_iwm_util.h"
12945bc40b1SMatthew Dillon #include "if_iwm_binding.h"
1306acbba79SMatthew Dillon #include "if_iwm_sf.h"
13124a8d46aSMatthew Dillon 
1326acbba79SMatthew Dillon /*
1336acbba79SMatthew Dillon  * BEGIN iwlwifi/mvm/binding.c
1346acbba79SMatthew Dillon  */
1356acbba79SMatthew Dillon 
1366acbba79SMatthew Dillon struct iwm_iface_iterator_data {
137f16ef749SImre Vadász 	int idx;
138f16ef749SImre Vadász 
1396acbba79SMatthew Dillon 	struct iwm_phy_ctxt *phyctxt;
140f16ef749SImre Vadász 
141f16ef749SImre Vadász 	uint16_t ids[IWM_MAX_MACS_IN_BINDING];
142f16ef749SImre Vadász 	int16_t colors[IWM_MAX_MACS_IN_BINDING];
143f16ef749SImre Vadász };
144f16ef749SImre Vadász 
145f16ef749SImre Vadász static int
iwm_binding_cmd(struct iwm_softc * sc,uint32_t action,struct iwm_iface_iterator_data * data)1466acbba79SMatthew Dillon iwm_binding_cmd(struct iwm_softc *sc, uint32_t action,
1476acbba79SMatthew Dillon 	struct iwm_iface_iterator_data *data)
14824a8d46aSMatthew Dillon {
149*4cbc7cf9SMichael Neumann 	struct iwm_binding_cmd_v1 cmd;
1506acbba79SMatthew Dillon 	struct iwm_phy_ctxt *phyctxt = data->phyctxt;
15124a8d46aSMatthew Dillon 	int i, ret;
15224a8d46aSMatthew Dillon 	uint32_t status;
15324a8d46aSMatthew Dillon 
15424a8d46aSMatthew Dillon 	memset(&cmd, 0, sizeof(cmd));
15524a8d46aSMatthew Dillon 
15624a8d46aSMatthew Dillon 	cmd.id_and_color
15724a8d46aSMatthew Dillon 	    = htole32(IWM_FW_CMD_ID_AND_COLOR(phyctxt->id, phyctxt->color));
15824a8d46aSMatthew Dillon 	cmd.action = htole32(action);
15924a8d46aSMatthew Dillon 	cmd.phy = htole32(IWM_FW_CMD_ID_AND_COLOR(phyctxt->id, phyctxt->color));
16024a8d46aSMatthew Dillon 
161f16ef749SImre Vadász 	for (i = 0; i < IWM_MAX_MACS_IN_BINDING; i++)
16224a8d46aSMatthew Dillon 		cmd.macs[i] = htole32(IWM_FW_CTXT_INVALID);
163f16ef749SImre Vadász 	for (i = 0; i < data->idx; i++)
164f16ef749SImre Vadász 		cmd.macs[i] = htole32(IWM_FW_CMD_ID_AND_COLOR(data->ids[i],
165f16ef749SImre Vadász 							      data->colors[i]));
16624a8d46aSMatthew Dillon 
16724a8d46aSMatthew Dillon 	status = 0;
1686acbba79SMatthew Dillon 	ret = iwm_send_cmd_pdu_status(sc, IWM_BINDING_CONTEXT_CMD,
16924a8d46aSMatthew Dillon 	    sizeof(cmd), &cmd, &status);
17024a8d46aSMatthew Dillon 	if (ret) {
171f16ef749SImre Vadász 		device_printf(sc->sc_dev,
172f16ef749SImre Vadász 		    "Failed to send binding (action:%d): %d\n", action, ret);
17324a8d46aSMatthew Dillon 		return ret;
17424a8d46aSMatthew Dillon 	}
17524a8d46aSMatthew Dillon 
17624a8d46aSMatthew Dillon 	if (status) {
177f16ef749SImre Vadász 		device_printf(sc->sc_dev,
178f16ef749SImre Vadász 		    "Binding command failed: %u\n", status);
17924a8d46aSMatthew Dillon 		ret = EIO;
18024a8d46aSMatthew Dillon 	}
18124a8d46aSMatthew Dillon 
18224a8d46aSMatthew Dillon 	return ret;
18324a8d46aSMatthew Dillon }
18424a8d46aSMatthew Dillon 
185f16ef749SImre Vadász static int
iwm_binding_update(struct iwm_softc * sc,struct iwm_vap * ivp,struct iwm_phy_ctxt * phyctxt,boolean_t add)1866acbba79SMatthew Dillon iwm_binding_update(struct iwm_softc *sc, struct iwm_vap *ivp,
1876acbba79SMatthew Dillon 	struct iwm_phy_ctxt *phyctxt, boolean_t add)
18824a8d46aSMatthew Dillon {
1896acbba79SMatthew Dillon 	struct iwm_iface_iterator_data data = {
190f16ef749SImre Vadász 		.phyctxt = phyctxt,
191f16ef749SImre Vadász 	};
192f16ef749SImre Vadász 	uint32_t action;
193f16ef749SImre Vadász 
194f16ef749SImre Vadász 	if (add)
195f16ef749SImre Vadász 		action = IWM_FW_CTXT_ACTION_ADD;
196f16ef749SImre Vadász 	else
197f16ef749SImre Vadász 		action = IWM_FW_CTXT_ACTION_REMOVE;
198f16ef749SImre Vadász 
199f16ef749SImre Vadász 	if (add) {
200f16ef749SImre Vadász 		data.ids[0] = ivp->id;
201f16ef749SImre Vadász 		data.colors[0] = ivp->color;
202f16ef749SImre Vadász 		data.idx++;
203f16ef749SImre Vadász 	}
204f16ef749SImre Vadász 
2056acbba79SMatthew Dillon 	return iwm_binding_cmd(sc, action, &data);
20624a8d46aSMatthew Dillon }
20724a8d46aSMatthew Dillon 
20824a8d46aSMatthew Dillon int
iwm_binding_add_vif(struct iwm_softc * sc,struct iwm_vap * ivp)2096acbba79SMatthew Dillon iwm_binding_add_vif(struct iwm_softc *sc, struct iwm_vap *ivp)
21024a8d46aSMatthew Dillon {
211f16ef749SImre Vadász 	if (!ivp->phy_ctxt)
212f16ef749SImre Vadász 		return EINVAL;
213f16ef749SImre Vadász 
214f16ef749SImre Vadász 	/*
215f16ef749SImre Vadász 	 * Update SF - Disable if needed. if this fails, SF might still be on
216f16ef749SImre Vadász 	 * while many macs are bound, which is forbidden - so fail the binding.
217f16ef749SImre Vadász 	 */
2186acbba79SMatthew Dillon 	if (iwm_sf_update(sc, &ivp->iv_vap, FALSE))
219f16ef749SImre Vadász 		return EINVAL;
220f16ef749SImre Vadász 
2216acbba79SMatthew Dillon 	return iwm_binding_update(sc, ivp, ivp->phy_ctxt, TRUE);
222f16ef749SImre Vadász }
223f16ef749SImre Vadász 
224f16ef749SImre Vadász int
iwm_binding_remove_vif(struct iwm_softc * sc,struct iwm_vap * ivp)2256acbba79SMatthew Dillon iwm_binding_remove_vif(struct iwm_softc *sc, struct iwm_vap *ivp)
226f16ef749SImre Vadász {
227f16ef749SImre Vadász 	int ret;
228f16ef749SImre Vadász 
229f16ef749SImre Vadász 	if (!ivp->phy_ctxt)
230f16ef749SImre Vadász 		return EINVAL;
231f16ef749SImre Vadász 
2326acbba79SMatthew Dillon 	ret = iwm_binding_update(sc, ivp, ivp->phy_ctxt, FALSE);
233f16ef749SImre Vadász 
234f16ef749SImre Vadász 	if (!ret) {
2356acbba79SMatthew Dillon 		if (iwm_sf_update(sc, &ivp->iv_vap, TRUE))
236f16ef749SImre Vadász 			device_printf(sc->sc_dev,
237f16ef749SImre Vadász 			    "Failed to update SF state\n");
238f16ef749SImre Vadász 	}
239f16ef749SImre Vadász 
240f16ef749SImre Vadász 	return ret;
24124a8d46aSMatthew Dillon }
242