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