xref: /freebsd/sys/dev/iwm/if_iwm_mac_ctxt.c (revision 7cc42f6d)
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) 2007 - 2013 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 <ilw@linux.intel.com>
55  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56  *
57  *
58  * BSD LICENSE
59  *
60  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61  * All rights reserved.
62  *
63  * Redistribution and use in source and binary forms, with or without
64  * modification, are permitted provided that the following conditions
65  * are met:
66  *
67  *  * Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer.
69  *  * Redistributions in binary form must reproduce the above copyright
70  *    notice, this list of conditions and the following disclaimer in
71  *    the documentation and/or other materials provided with the
72  *    distribution.
73  *  * Neither the name Intel Corporation nor the names of its
74  *    contributors may be used to endorse or promote products derived
75  *    from this software without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88  */
89 
90 /*-
91  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
92  *
93  * Permission to use, copy, modify, and distribute this software for any
94  * purpose with or without fee is hereby granted, provided that the above
95  * copyright notice and this permission notice appear in all copies.
96  *
97  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
100  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
101  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
102  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
103  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
104  */
105 #include <sys/cdefs.h>
106 __FBSDID("$FreeBSD$");
107 
108 #include "opt_wlan.h"
109 #include "opt_iwm.h"
110 
111 #include <sys/param.h>
112 #include <sys/bus.h>
113 #include <sys/conf.h>
114 #include <sys/endian.h>
115 #include <sys/firmware.h>
116 #include <sys/kernel.h>
117 #include <sys/malloc.h>
118 #include <sys/mbuf.h>
119 #include <sys/mutex.h>
120 #include <sys/module.h>
121 #include <sys/proc.h>
122 #include <sys/rman.h>
123 #include <sys/socket.h>
124 #include <sys/sockio.h>
125 #include <sys/sysctl.h>
126 #include <sys/linker.h>
127 
128 #include <machine/bus.h>
129 #include <machine/endian.h>
130 #include <machine/resource.h>
131 
132 #include <dev/pci/pcivar.h>
133 #include <dev/pci/pcireg.h>
134 
135 #include <net/bpf.h>
136 
137 #include <net/if.h>
138 #include <net/if_var.h>
139 #include <net/if_arp.h>
140 #include <net/if_dl.h>
141 #include <net/if_media.h>
142 #include <net/if_types.h>
143 
144 #include <netinet/in.h>
145 #include <netinet/in_systm.h>
146 #include <netinet/if_ether.h>
147 #include <netinet/ip.h>
148 
149 #include <net80211/ieee80211_var.h>
150 #include <net80211/ieee80211_regdomain.h>
151 #include <net80211/ieee80211_ratectl.h>
152 #include <net80211/ieee80211_radiotap.h>
153 
154 #include <dev/iwm/if_iwmreg.h>
155 #include <dev/iwm/if_iwmvar.h>
156 #include <dev/iwm/if_iwm_debug.h>
157 #include <dev/iwm/if_iwm_util.h>
158 #include <dev/iwm/if_iwm_mac_ctxt.h>
159 
160 /*
161  * BEGIN mvm/mac-ctxt.c
162  */
163 
164 const uint8_t iwm_ac_to_tx_fifo[] = {
165 	IWM_TX_FIFO_BE,
166 	IWM_TX_FIFO_BK,
167 	IWM_TX_FIFO_VI,
168 	IWM_TX_FIFO_VO,
169 };
170 
171 static void
172 iwm_ack_rates(struct iwm_softc *sc, int is2ghz,
173 	int *cck_rates, int *ofdm_rates, struct iwm_node *in)
174 {
175 	int lowest_present_ofdm = 100;
176 	int lowest_present_cck = 100;
177 	uint8_t cck = 0;
178 	uint8_t ofdm = 0;
179 	int i;
180 	struct ieee80211_rateset *rs = &in->in_ni.ni_rates;
181 
182 	if (is2ghz) {
183 		for (i = IWM_FIRST_CCK_RATE; i <= IWM_LAST_CCK_RATE; i++) {
184 			if ((iwm_ridx2rate(rs, i) & IEEE80211_RATE_BASIC) == 0)
185 				continue;
186 			cck |= (1 << i);
187 			if (lowest_present_cck > i)
188 				lowest_present_cck = i;
189 		}
190 	}
191 	for (i = IWM_FIRST_OFDM_RATE; i <= IWM_LAST_NON_HT_RATE; i++) {
192 		if ((iwm_ridx2rate(rs, i) & IEEE80211_RATE_BASIC) == 0)
193 			continue;
194 		ofdm |= (1 << (i - IWM_FIRST_OFDM_RATE));
195 		if (lowest_present_ofdm > i)
196 			lowest_present_ofdm = i;
197 	}
198 
199 	/*
200 	 * Now we've got the basic rates as bitmaps in the ofdm and cck
201 	 * variables. This isn't sufficient though, as there might not
202 	 * be all the right rates in the bitmap. E.g. if the only basic
203 	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
204 	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
205 	 *
206 	 *    [...] a STA responding to a received frame shall transmit
207 	 *    its Control Response frame [...] at the highest rate in the
208 	 *    BSSBasicRateSet parameter that is less than or equal to the
209 	 *    rate of the immediately previous frame in the frame exchange
210 	 *    sequence ([...]) and that is of the same modulation class
211 	 *    ([...]) as the received frame. If no rate contained in the
212 	 *    BSSBasicRateSet parameter meets these conditions, then the
213 	 *    control frame sent in response to a received frame shall be
214 	 *    transmitted at the highest mandatory rate of the PHY that is
215 	 *    less than or equal to the rate of the received frame, and
216 	 *    that is of the same modulation class as the received frame.
217 	 *
218 	 * As a consequence, we need to add all mandatory rates that are
219 	 * lower than all of the basic rates to these bitmaps.
220 	 */
221 
222 	if (IWM_RATE_24M_INDEX < lowest_present_ofdm)
223 		ofdm |= IWM_RATE_BIT_MSK(24) >> IWM_FIRST_OFDM_RATE;
224 	if (IWM_RATE_12M_INDEX < lowest_present_ofdm)
225 		ofdm |= IWM_RATE_BIT_MSK(12) >> IWM_FIRST_OFDM_RATE;
226 	/* 6M already there or needed so always add */
227 	ofdm |= IWM_RATE_BIT_MSK(6) >> IWM_FIRST_OFDM_RATE;
228 
229 	/*
230 	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
231 	 * Note, however:
232 	 *  - if no CCK rates are basic, it must be ERP since there must
233 	 *    be some basic rates at all, so they're OFDM => ERP PHY
234 	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
235 	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
236 	 *  - if 5.5M is basic, 1M and 2M are mandatory
237 	 *  - if 2M is basic, 1M is mandatory
238 	 *  - if 1M is basic, that's the only valid ACK rate.
239 	 * As a consequence, it's not as complicated as it sounds, just add
240 	 * any lower rates to the ACK rate bitmap.
241 	 */
242 	if (IWM_RATE_11M_INDEX < lowest_present_cck)
243 		cck |= IWM_RATE_BIT_MSK(11) >> IWM_FIRST_CCK_RATE;
244 	if (IWM_RATE_5M_INDEX < lowest_present_cck)
245 		cck |= IWM_RATE_BIT_MSK(5) >> IWM_FIRST_CCK_RATE;
246 	if (IWM_RATE_2M_INDEX < lowest_present_cck)
247 		cck |= IWM_RATE_BIT_MSK(2) >> IWM_FIRST_CCK_RATE;
248 	/* 1M already there or needed so always add */
249 	cck |= IWM_RATE_BIT_MSK(1) >> IWM_FIRST_CCK_RATE;
250 
251 	*cck_rates = cck;
252 	*ofdm_rates = ofdm;
253 }
254 
255 static void
256 iwm_mac_ctxt_cmd_common(struct iwm_softc *sc, struct iwm_node *in,
257 	struct iwm_mac_ctx_cmd *cmd, uint32_t action)
258 {
259 	struct ieee80211com *ic = &sc->sc_ic;
260 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
261 	struct ieee80211_node *ni = vap->iv_bss;
262 	struct iwm_vap *ivp = IWM_VAP(vap);
263 	int cck_ack_rates, ofdm_ack_rates;
264 	int i;
265 	int is2ghz;
266 
267 	/*
268 	 * id is the MAC address ID - something to do with MAC filtering.
269 	 * color - not sure.
270 	 *
271 	 * These are both functions of the vap, not of the node.
272 	 * So, for now, hard-code both to 0 (default).
273 	 */
274 	cmd->id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id,
275 	    ivp->color));
276 	cmd->action = htole32(action);
277 
278 	cmd->mac_type = htole32(IWM_FW_MAC_TYPE_BSS_STA);
279 
280 	/*
281 	 * The TSF ID is one of four TSF tracking resources in the firmware.
282 	 * Read the iwlwifi/mvm code for more details.
283 	 *
284 	 * For now, just hard-code it to TSF tracking ID 0; we only support
285 	 * a single STA mode VAP.
286 	 *
287 	 * It's per-vap, not per-node.
288 	 */
289 	cmd->tsf_id = htole32(IWM_DEFAULT_TSFID);
290 
291 	IEEE80211_ADDR_COPY(cmd->node_addr, vap->iv_myaddr);
292 
293 	/*
294 	 * XXX should we error out if in_assoc is 1 and ni == NULL?
295 	 */
296 #if 0
297 	if (in->in_assoc) {
298 		IEEE80211_ADDR_COPY(cmd->bssid_addr, ni->ni_bssid);
299 	} else {
300 		/* eth broadcast address */
301 		IEEE80211_ADDR_COPY(cmd->bssid_addr, ieee80211broadcastaddr);
302 	}
303 #else
304 	/*
305 	 * XXX This workaround makes the firmware behave more correctly once
306 	 *     we are associated, regularly giving us statistics notifications,
307 	 *     as well as signaling missed beacons to us.
308 	 *     Since we only call iwm_mac_ctxt_add() and
309 	 *     iwm_mac_ctxt_changed() when already authenticating or
310 	 *     associating, ni->ni_bssid should always make sense here.
311 	 */
312 	if (ivp->iv_auth) {
313 		IEEE80211_ADDR_COPY(cmd->bssid_addr, ni->ni_bssid);
314 	} else {
315 		/* XXX Or maybe all zeroes address? */
316 		IEEE80211_ADDR_COPY(cmd->bssid_addr, ieee80211broadcastaddr);
317 	}
318 #endif
319 
320 	/*
321 	 * Default to 2ghz if no node information is given.
322 	 */
323 	if (in && in->in_ni.ni_chan != IEEE80211_CHAN_ANYC) {
324 		is2ghz = !! IEEE80211_IS_CHAN_2GHZ(in->in_ni.ni_chan);
325 	} else {
326 		is2ghz = 1;
327 	}
328 	iwm_ack_rates(sc, is2ghz, &cck_ack_rates, &ofdm_ack_rates, in);
329 	cmd->cck_rates = htole32(cck_ack_rates);
330 	cmd->ofdm_rates = htole32(ofdm_ack_rates);
331 
332 	cmd->cck_short_preamble
333 	    = htole32((ic->ic_flags & IEEE80211_F_SHPREAMBLE)
334 	      ? IWM_MAC_FLG_SHORT_PREAMBLE : 0);
335 	cmd->short_slot
336 	    = htole32((ic->ic_flags & IEEE80211_F_SHSLOT)
337 	      ? IWM_MAC_FLG_SHORT_SLOT : 0);
338 
339 	/*
340 	 * XXX TODO: if we're doing QOS..
341 	 * cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA)
342 	 */
343 
344 	for (i = 0; i < WME_NUM_AC; i++) {
345 		uint8_t txf = iwm_ac_to_tx_fifo[i];
346 
347 		cmd->ac[txf].cw_min = htole16(ivp->queue_params[i].cw_min);
348 		cmd->ac[txf].cw_max = htole16(ivp->queue_params[i].cw_max);
349 		cmd->ac[txf].edca_txop =
350 		    htole16(ivp->queue_params[i].edca_txop);
351 		cmd->ac[txf].aifsn = ivp->queue_params[i].aifsn;
352 		cmd->ac[txf].fifos_mask = (1 << txf);
353 	}
354 
355 	if (ivp->have_wme)
356 		cmd->qos_flags |= htole32(IWM_MAC_QOS_FLG_UPDATE_EDCA);
357 
358 	if (ic->ic_flags & IEEE80211_F_USEPROT)
359 		cmd->protection_flags |= htole32(IWM_MAC_PROT_FLG_TGG_PROTECT);
360 
361 	cmd->filter_flags = htole32(IWM_MAC_FILTER_ACCEPT_GRP);
362 }
363 
364 static int
365 iwm_mac_ctxt_send_cmd(struct iwm_softc *sc, struct iwm_mac_ctx_cmd *cmd)
366 {
367 	int ret = iwm_send_cmd_pdu(sc, IWM_MAC_CONTEXT_CMD, IWM_CMD_SYNC,
368 				       sizeof(*cmd), cmd);
369 	if (ret)
370 		device_printf(sc->sc_dev,
371 		    "%s: Failed to send MAC context (action:%d): %d\n",
372 		    __func__, le32toh(cmd->action), ret);
373 	return ret;
374 }
375 
376 /*
377  * Fill the specific data for mac context of type station or p2p client
378  */
379 static void
380 iwm_mac_ctxt_cmd_fill_sta(struct iwm_softc *sc, struct iwm_node *in,
381 	struct iwm_mac_data_sta *ctxt_sta, int force_assoc_off)
382 {
383 	struct ieee80211_node *ni = &in->in_ni;
384 	unsigned dtim_period, dtim_count;
385 	struct ieee80211com *ic = &sc->sc_ic;
386 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
387 
388 	/* will this work? */
389 	dtim_period = vap->iv_dtim_period;
390 	dtim_count = vap->iv_dtim_count;
391 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_BEACON | IWM_DEBUG_CMD,
392 	    "%s: force_assoc_off=%d\n", __func__, force_assoc_off);
393 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_BEACON | IWM_DEBUG_CMD,
394 	    "DTIM: period=%d count=%d\n", dtim_period, dtim_count);
395 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_BEACON | IWM_DEBUG_CMD,
396 	    "BEACON: tsf: %llu, ni_intval=%d\n",
397 	    (unsigned long long) le64toh(ni->ni_tstamp.tsf),
398 	    ni->ni_intval);
399 
400 	/* We need the dtim_period to set the MAC as associated */
401 	if (in->in_assoc && dtim_period && !force_assoc_off) {
402 		uint64_t tsf;
403 		uint32_t dtim_offs;
404 
405 		/*
406 		 * The DTIM count counts down, so when it is N that means N
407 		 * more beacon intervals happen until the DTIM TBTT. Therefore
408 		 * add this to the current time. If that ends up being in the
409 		 * future, the firmware will handle it.
410 		 *
411 		 * Also note that the system_timestamp (which we get here as
412 		 * "sync_device_ts") and TSF timestamp aren't at exactly the
413 		 * same offset in the frame -- the TSF is at the first symbol
414 		 * of the TSF, the system timestamp is at signal acquisition
415 		 * time. This means there's an offset between them of at most
416 		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
417 		 * 384us in the longest case), this is currently not relevant
418 		 * as the firmware wakes up around 2ms before the TBTT.
419 		 */
420 		dtim_offs = dtim_count * ni->ni_intval;
421 		/* convert TU to usecs */
422 		dtim_offs *= 1024;
423 
424 		/*
425 		 * net80211: TSF is in 802.11 order, so convert up to local
426 		 * ordering before we manipulate things.
427 		 */
428 		tsf = le64toh(ni->ni_tstamp.tsf);
429 
430 		ctxt_sta->dtim_tsf = htole64(tsf + dtim_offs);
431 		ctxt_sta->dtim_time = htole32(tsf + dtim_offs);
432 
433 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_BEACON | IWM_DEBUG_CMD,
434 		    "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
435 		    (long long)le64toh(ctxt_sta->dtim_tsf),
436 		    le32toh(ctxt_sta->dtim_time), dtim_offs);
437 
438 		ctxt_sta->is_assoc = htole32(1);
439 	} else {
440 		ctxt_sta->is_assoc = htole32(0);
441 	}
442 
443 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_CMD | IWM_DEBUG_BEACON,
444 	    "%s: ni_intval: %d, bi_reciprocal: %d, dtim_interval: %d, dtim_reciprocal: %d\n",
445 	    __func__,
446 	    ni->ni_intval,
447 	    iwm_reciprocal(ni->ni_intval),
448 	    ni->ni_intval * dtim_period,
449 	    iwm_reciprocal(ni->ni_intval * dtim_period));
450 
451 	ctxt_sta->bi = htole32(ni->ni_intval);
452 	ctxt_sta->bi_reciprocal = htole32(iwm_reciprocal(ni->ni_intval));
453 	ctxt_sta->dtim_interval = htole32(ni->ni_intval * dtim_period);
454 	ctxt_sta->dtim_reciprocal =
455 	    htole32(iwm_reciprocal(ni->ni_intval * dtim_period));
456 
457 	/* 10 = CONN_MAX_LISTEN_INTERVAL */
458 	ctxt_sta->listen_interval = htole32(10);
459 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_CMD | IWM_DEBUG_BEACON,
460 	    "%s: associd=%d\n", __func__, IEEE80211_AID(ni->ni_associd));
461 	ctxt_sta->assoc_id = htole32(IEEE80211_AID(ni->ni_associd));
462 }
463 
464 static int
465 iwm_mac_ctxt_cmd_station(struct iwm_softc *sc, struct ieee80211vap *vap,
466 	uint32_t action)
467 {
468 	struct ieee80211_node *ni = vap->iv_bss;
469 	struct iwm_node *in = IWM_NODE(ni);
470 	struct iwm_mac_ctx_cmd cmd = {};
471 
472 	IWM_DPRINTF(sc, IWM_DEBUG_RESET,
473 	    "%s: called; action=%d\n", __func__, action);
474 
475 	/* Fill the common data for all mac context types */
476 	iwm_mac_ctxt_cmd_common(sc, in, &cmd, action);
477 
478 	/* Allow beacons to pass through as long as we are not associated,or we
479 	 * do not have dtim period information */
480 	if (!in->in_assoc || !vap->iv_dtim_period)
481 		cmd.filter_flags |= htole32(IWM_MAC_FILTER_IN_BEACON);
482 	else
483 		cmd.filter_flags &= ~htole32(IWM_MAC_FILTER_IN_BEACON);
484 
485 	/* Fill the data specific for station mode */
486 	iwm_mac_ctxt_cmd_fill_sta(sc, in,
487 	    &cmd.sta, action == IWM_FW_CTXT_ACTION_ADD);
488 
489 	return iwm_mac_ctxt_send_cmd(sc, &cmd);
490 }
491 
492 static int
493 iwm_mac_ctx_send(struct iwm_softc *sc, struct ieee80211vap *vap,
494     uint32_t action)
495 {
496 	return iwm_mac_ctxt_cmd_station(sc, vap, action);
497 }
498 
499 int
500 iwm_mac_ctxt_add(struct iwm_softc *sc, struct ieee80211vap *vap)
501 {
502 	struct iwm_vap *iv = IWM_VAP(vap);
503 	int ret;
504 
505 	if (iv->is_uploaded != 0) {
506 		device_printf(sc->sc_dev, "%s: called; uploaded != 0\n",
507 		    __func__);
508 		return (EIO);
509 	}
510 
511 	ret = iwm_mac_ctx_send(sc, vap, IWM_FW_CTXT_ACTION_ADD);
512 	if (ret)
513 		return (ret);
514 	iv->is_uploaded = 1;
515 	return (0);
516 }
517 
518 int
519 iwm_mac_ctxt_changed(struct iwm_softc *sc, struct ieee80211vap *vap)
520 {
521 	struct iwm_vap *iv = IWM_VAP(vap);
522 
523 	if (iv->is_uploaded == 0) {
524 		device_printf(sc->sc_dev, "%s: called; uploaded = 0\n",
525 		    __func__);
526 		return (EIO);
527 	}
528 	return iwm_mac_ctx_send(sc, vap, IWM_FW_CTXT_ACTION_MODIFY);
529 }
530 
531 #if 0
532 static int
533 iwm_mac_ctxt_remove(struct iwm_softc *sc, struct iwm_node *in)
534 {
535 	struct iwm_mac_ctx_cmd cmd;
536 	int ret;
537 
538 	if (!in->in_uploaded) {
539 		device_printf(sc->sc_dev,
540 		    "attempt to remove !uploaded node %p", in);
541 		return EIO;
542 	}
543 
544 	memset(&cmd, 0, sizeof(cmd));
545 
546 	cmd.id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(IWM_DEFAULT_MACID,
547 	    IWM_DEFAULT_COLOR));
548 	cmd.action = htole32(IWM_FW_CTXT_ACTION_REMOVE);
549 
550 	ret = iwm_send_cmd_pdu(sc,
551 	    IWM_MAC_CONTEXT_CMD, IWM_CMD_SYNC, sizeof(cmd), &cmd);
552 	if (ret) {
553 		device_printf(sc->sc_dev,
554 		    "Failed to remove MAC context: %d\n", ret);
555 		return ret;
556 	}
557 	in->in_uploaded = 0;
558 
559 	return 0;
560 }
561 #endif
562