1d4886179SRui Paulo /* $OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $ */
2d4886179SRui Paulo
3d4886179SRui Paulo /*
4d4886179SRui Paulo * Copyright (c) 2014 genua mbh <info@genua.de>
5d4886179SRui Paulo * Copyright (c) 2014 Fixup Software Ltd.
6d4886179SRui Paulo *
7d4886179SRui Paulo * Permission to use, copy, modify, and distribute this software for any
8d4886179SRui Paulo * purpose with or without fee is hereby granted, provided that the above
9d4886179SRui Paulo * copyright notice and this permission notice appear in all copies.
10d4886179SRui Paulo *
11d4886179SRui Paulo * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12d4886179SRui Paulo * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13d4886179SRui Paulo * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14d4886179SRui Paulo * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15d4886179SRui Paulo * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16d4886179SRui Paulo * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17d4886179SRui Paulo * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18d4886179SRui Paulo */
19d4886179SRui Paulo
20d4886179SRui Paulo /*-
21d4886179SRui Paulo * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22d4886179SRui Paulo * which were used as the reference documentation for this implementation.
23d4886179SRui Paulo *
24d4886179SRui Paulo * Driver version we are currently based off of is
25d4886179SRui Paulo * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26d4886179SRui Paulo *
27d4886179SRui Paulo ***********************************************************************
28d4886179SRui Paulo *
29d4886179SRui Paulo * This file is provided under a dual BSD/GPLv2 license. When using or
30d4886179SRui Paulo * redistributing this file, you may do so under either license.
31d4886179SRui Paulo *
32d4886179SRui Paulo * GPL LICENSE SUMMARY
33d4886179SRui Paulo *
34bdba6830SAdrian Chadd * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35d4886179SRui Paulo *
36d4886179SRui Paulo * This program is free software; you can redistribute it and/or modify
37d4886179SRui Paulo * it under the terms of version 2 of the GNU General Public License as
38d4886179SRui Paulo * published by the Free Software Foundation.
39d4886179SRui Paulo *
40d4886179SRui Paulo * This program is distributed in the hope that it will be useful, but
41d4886179SRui Paulo * WITHOUT ANY WARRANTY; without even the implied warranty of
42d4886179SRui Paulo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43d4886179SRui Paulo * General Public License for more details.
44d4886179SRui Paulo *
45d4886179SRui Paulo * You should have received a copy of the GNU General Public License
46d4886179SRui Paulo * along with this program; if not, write to the Free Software
47d4886179SRui Paulo * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48d4886179SRui Paulo * USA
49d4886179SRui Paulo *
50d4886179SRui Paulo * The full GNU General Public License is included in this distribution
51d4886179SRui Paulo * in the file called COPYING.
52d4886179SRui Paulo *
53d4886179SRui Paulo * Contact Information:
54bdba6830SAdrian Chadd * Intel Linux Wireless <linuxwifi@intel.com>
55d4886179SRui Paulo * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56d4886179SRui Paulo *
57d4886179SRui Paulo * BSD LICENSE
58d4886179SRui Paulo *
59bdba6830SAdrian Chadd * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
60d4886179SRui Paulo * All rights reserved.
61d4886179SRui Paulo *
62d4886179SRui Paulo * Redistribution and use in source and binary forms, with or without
63d4886179SRui Paulo * modification, are permitted provided that the following conditions
64d4886179SRui Paulo * are met:
65d4886179SRui Paulo *
66d4886179SRui Paulo * * Redistributions of source code must retain the above copyright
67d4886179SRui Paulo * notice, this list of conditions and the following disclaimer.
68d4886179SRui Paulo * * Redistributions in binary form must reproduce the above copyright
69d4886179SRui Paulo * notice, this list of conditions and the following disclaimer in
70d4886179SRui Paulo * the documentation and/or other materials provided with the
71d4886179SRui Paulo * distribution.
72d4886179SRui Paulo * * Neither the name Intel Corporation nor the names of its
73d4886179SRui Paulo * contributors may be used to endorse or promote products derived
74d4886179SRui Paulo * from this software without specific prior written permission.
75d4886179SRui Paulo *
76d4886179SRui Paulo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
77d4886179SRui Paulo * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
78d4886179SRui Paulo * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
79d4886179SRui Paulo * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
80d4886179SRui Paulo * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
81d4886179SRui Paulo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
82d4886179SRui Paulo * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
83d4886179SRui Paulo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
84d4886179SRui Paulo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
85d4886179SRui Paulo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
86d4886179SRui Paulo * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87d4886179SRui Paulo */
88d4886179SRui Paulo
89d4886179SRui Paulo #include <sys/cdefs.h>
90b789292fSAndriy Voskoboinyk #include "opt_wlan.h"
91616201d1SAdrian Chadd #include "opt_iwm.h"
92b789292fSAndriy Voskoboinyk
93d4886179SRui Paulo #include <sys/param.h>
94d4886179SRui Paulo #include <sys/bus.h>
95d4886179SRui Paulo #include <sys/conf.h>
96d4886179SRui Paulo #include <sys/endian.h>
97d4886179SRui Paulo #include <sys/firmware.h>
98d4886179SRui Paulo #include <sys/kernel.h>
99d4886179SRui Paulo #include <sys/malloc.h>
100d4886179SRui Paulo #include <sys/mbuf.h>
101d4886179SRui Paulo #include <sys/mutex.h>
102d4886179SRui Paulo #include <sys/module.h>
103d4886179SRui Paulo #include <sys/proc.h>
104d4886179SRui Paulo #include <sys/rman.h>
105d4886179SRui Paulo #include <sys/socket.h>
106d4886179SRui Paulo #include <sys/sockio.h>
107d4886179SRui Paulo #include <sys/sysctl.h>
108d4886179SRui Paulo #include <sys/linker.h>
109d4886179SRui Paulo
110d4886179SRui Paulo #include <machine/bus.h>
111d4886179SRui Paulo #include <machine/endian.h>
112d4886179SRui Paulo #include <machine/resource.h>
113d4886179SRui Paulo
114d4886179SRui Paulo #include <dev/pci/pcivar.h>
115d4886179SRui Paulo #include <dev/pci/pcireg.h>
116d4886179SRui Paulo
117d4886179SRui Paulo #include <net/bpf.h>
118d4886179SRui Paulo
119d4886179SRui Paulo #include <net/if.h>
120d4886179SRui Paulo #include <net/if_var.h>
121d4886179SRui Paulo #include <net/if_arp.h>
122d4886179SRui Paulo #include <net/if_dl.h>
123d4886179SRui Paulo #include <net/if_media.h>
124d4886179SRui Paulo #include <net/if_types.h>
125d4886179SRui Paulo
126d4886179SRui Paulo #include <netinet/in.h>
127d4886179SRui Paulo #include <netinet/in_systm.h>
128d4886179SRui Paulo #include <netinet/if_ether.h>
129d4886179SRui Paulo #include <netinet/ip.h>
130d4886179SRui Paulo
131d4886179SRui Paulo #include <net80211/ieee80211_var.h>
132d4886179SRui Paulo #include <net80211/ieee80211_regdomain.h>
133d4886179SRui Paulo #include <net80211/ieee80211_ratectl.h>
134d4886179SRui Paulo #include <net80211/ieee80211_radiotap.h>
135d4886179SRui Paulo
13649fdbf0aSRui Paulo #include <dev/iwm/if_iwmreg.h>
13749fdbf0aSRui Paulo #include <dev/iwm/if_iwmvar.h>
13849fdbf0aSRui Paulo #include <dev/iwm/if_iwm_debug.h>
13949fdbf0aSRui Paulo #include <dev/iwm/if_iwm_util.h>
14049fdbf0aSRui Paulo #include <dev/iwm/if_iwm_binding.h>
141cd684decSAdrian Chadd #include <dev/iwm/if_iwm_sf.h>
142d4886179SRui Paulo
143d4886179SRui Paulo /*
144d4886179SRui Paulo * BEGIN iwlwifi/mvm/binding.c
145d4886179SRui Paulo */
146d4886179SRui Paulo
147e7065dd1SMark Johnston struct iwm_iface_iterator_data {
148bdba6830SAdrian Chadd int idx;
149bdba6830SAdrian Chadd
150e7065dd1SMark Johnston struct iwm_phy_ctxt *phyctxt;
151bdba6830SAdrian Chadd
152bdba6830SAdrian Chadd uint16_t ids[IWM_MAX_MACS_IN_BINDING];
153bdba6830SAdrian Chadd int16_t colors[IWM_MAX_MACS_IN_BINDING];
154bdba6830SAdrian Chadd };
155bdba6830SAdrian Chadd
156bdba6830SAdrian Chadd static int
iwm_binding_cmd(struct iwm_softc * sc,uint32_t action,struct iwm_iface_iterator_data * data)157e7065dd1SMark Johnston iwm_binding_cmd(struct iwm_softc *sc, uint32_t action,
158e7065dd1SMark Johnston struct iwm_iface_iterator_data *data)
159d4886179SRui Paulo {
160*355c1513SAdrian Chadd struct iwm_binding_cmd_v1 cmd;
161e7065dd1SMark Johnston struct iwm_phy_ctxt *phyctxt = data->phyctxt;
162d4886179SRui Paulo int i, ret;
163d4886179SRui Paulo uint32_t status;
164d4886179SRui Paulo
165d4886179SRui Paulo memset(&cmd, 0, sizeof(cmd));
166d4886179SRui Paulo
167d4886179SRui Paulo cmd.id_and_color
168d4886179SRui Paulo = htole32(IWM_FW_CMD_ID_AND_COLOR(phyctxt->id, phyctxt->color));
169d4886179SRui Paulo cmd.action = htole32(action);
170d4886179SRui Paulo cmd.phy = htole32(IWM_FW_CMD_ID_AND_COLOR(phyctxt->id, phyctxt->color));
171d4886179SRui Paulo
172bdba6830SAdrian Chadd for (i = 0; i < IWM_MAX_MACS_IN_BINDING; i++)
173d4886179SRui Paulo cmd.macs[i] = htole32(IWM_FW_CTXT_INVALID);
174bdba6830SAdrian Chadd for (i = 0; i < data->idx; i++)
175bdba6830SAdrian Chadd cmd.macs[i] = htole32(IWM_FW_CMD_ID_AND_COLOR(data->ids[i],
176bdba6830SAdrian Chadd data->colors[i]));
177d4886179SRui Paulo
178d4886179SRui Paulo status = 0;
179e7065dd1SMark Johnston ret = iwm_send_cmd_pdu_status(sc, IWM_BINDING_CONTEXT_CMD,
180d4886179SRui Paulo sizeof(cmd), &cmd, &status);
181d4886179SRui Paulo if (ret) {
182bdba6830SAdrian Chadd device_printf(sc->sc_dev,
183bdba6830SAdrian Chadd "Failed to send binding (action:%d): %d\n", action, ret);
184d4886179SRui Paulo return ret;
185d4886179SRui Paulo }
186d4886179SRui Paulo
187d4886179SRui Paulo if (status) {
188bdba6830SAdrian Chadd device_printf(sc->sc_dev,
189bdba6830SAdrian Chadd "Binding command failed: %u\n", status);
190d4886179SRui Paulo ret = EIO;
191d4886179SRui Paulo }
192d4886179SRui Paulo
193d4886179SRui Paulo return ret;
194d4886179SRui Paulo }
195d4886179SRui Paulo
196bdba6830SAdrian Chadd static int
iwm_binding_update(struct iwm_softc * sc,struct iwm_vap * ivp,struct iwm_phy_ctxt * phyctxt,boolean_t add)197e7065dd1SMark Johnston iwm_binding_update(struct iwm_softc *sc, struct iwm_vap *ivp,
198e7065dd1SMark Johnston struct iwm_phy_ctxt *phyctxt, boolean_t add)
199d4886179SRui Paulo {
200e7065dd1SMark Johnston struct iwm_iface_iterator_data data = {
201bdba6830SAdrian Chadd .phyctxt = phyctxt,
202bdba6830SAdrian Chadd };
203bdba6830SAdrian Chadd uint32_t action;
204bdba6830SAdrian Chadd
205bdba6830SAdrian Chadd if (add)
206bdba6830SAdrian Chadd action = IWM_FW_CTXT_ACTION_ADD;
207bdba6830SAdrian Chadd else
208bdba6830SAdrian Chadd action = IWM_FW_CTXT_ACTION_REMOVE;
209bdba6830SAdrian Chadd
210bdba6830SAdrian Chadd if (add) {
211bdba6830SAdrian Chadd data.ids[0] = ivp->id;
212bdba6830SAdrian Chadd data.colors[0] = ivp->color;
213bdba6830SAdrian Chadd data.idx++;
214bdba6830SAdrian Chadd }
215bdba6830SAdrian Chadd
216e7065dd1SMark Johnston return iwm_binding_cmd(sc, action, &data);
217d4886179SRui Paulo }
218d4886179SRui Paulo
219d4886179SRui Paulo int
iwm_binding_add_vif(struct iwm_softc * sc,struct iwm_vap * ivp)220e7065dd1SMark Johnston iwm_binding_add_vif(struct iwm_softc *sc, struct iwm_vap *ivp)
221d4886179SRui Paulo {
222bdba6830SAdrian Chadd if (!ivp->phy_ctxt)
223bdba6830SAdrian Chadd return EINVAL;
224bdba6830SAdrian Chadd
225bdba6830SAdrian Chadd /*
226bdba6830SAdrian Chadd * Update SF - Disable if needed. if this fails, SF might still be on
227bdba6830SAdrian Chadd * while many macs are bound, which is forbidden - so fail the binding.
228bdba6830SAdrian Chadd */
229e7065dd1SMark Johnston if (iwm_sf_update(sc, &ivp->iv_vap, FALSE))
230bdba6830SAdrian Chadd return EINVAL;
231bdba6830SAdrian Chadd
232e7065dd1SMark Johnston return iwm_binding_update(sc, ivp, ivp->phy_ctxt, TRUE);
233bdba6830SAdrian Chadd }
234bdba6830SAdrian Chadd
235bdba6830SAdrian Chadd int
iwm_binding_remove_vif(struct iwm_softc * sc,struct iwm_vap * ivp)236e7065dd1SMark Johnston iwm_binding_remove_vif(struct iwm_softc *sc, struct iwm_vap *ivp)
237bdba6830SAdrian Chadd {
238bdba6830SAdrian Chadd int ret;
239bdba6830SAdrian Chadd
240bdba6830SAdrian Chadd if (!ivp->phy_ctxt)
241bdba6830SAdrian Chadd return EINVAL;
242bdba6830SAdrian Chadd
243e7065dd1SMark Johnston ret = iwm_binding_update(sc, ivp, ivp->phy_ctxt, FALSE);
244bdba6830SAdrian Chadd
245bdba6830SAdrian Chadd if (!ret) {
246e7065dd1SMark Johnston if (iwm_sf_update(sc, &ivp->iv_vap, TRUE))
247bdba6830SAdrian Chadd device_printf(sc->sc_dev,
248bdba6830SAdrian Chadd "Failed to update SF state\n");
249bdba6830SAdrian Chadd }
250bdba6830SAdrian Chadd
251bdba6830SAdrian Chadd return ret;
252d4886179SRui Paulo }
253