xref: /dragonfly/sys/dev/netif/iwm/if_iwm_mac_ctxt.c (revision a42bad2d)
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 <sys/param.h>
109 #include <sys/bus.h>
110 #include <sys/endian.h>
111 #include <sys/firmware.h>
112 #include <sys/kernel.h>
113 #include <sys/malloc.h>
114 #include <sys/mbuf.h>
115 #include <sys/rman.h>
116 #include <sys/sysctl.h>
117 #include <sys/linker.h>
118 
119 #include <machine/endian.h>
120 
121 #include <bus/pci/pcivar.h>
122 #include <bus/pci/pcireg.h>
123 
124 #include <net/bpf.h>
125 
126 #include <net/if.h>
127 #include <net/if_var.h>
128 #include <net/if_arp.h>
129 #include <net/if_dl.h>
130 #include <net/if_media.h>
131 #include <net/if_types.h>
132 
133 #include <netinet/in.h>
134 #include <netinet/in_systm.h>
135 #include <netinet/if_ether.h>
136 #include <netinet/ip.h>
137 
138 #include <netproto/802_11/ieee80211_var.h>
139 #include <netproto/802_11/ieee80211_regdomain.h>
140 #include <netproto/802_11/ieee80211_ratectl.h>
141 #include <netproto/802_11/ieee80211_radiotap.h>
142 
143 #include "if_iwmreg.h"
144 #include "if_iwmvar.h"
145 #include "if_iwm_debug.h"
146 #include "if_iwm_util.h"
147 #include "if_iwm_mac_ctxt.h"
148 
149 const uint8_t iwm_mvm_ac_to_tx_fifo[] = {
150 	IWM_MVM_TX_FIFO_BE,
151 	IWM_MVM_TX_FIFO_BK,
152 	IWM_MVM_TX_FIFO_VI,
153 	IWM_MVM_TX_FIFO_VO,
154 };
155 
156 static void
157 iwm_mvm_ack_rates(struct iwm_softc *sc, int is2ghz,
158 	int *cck_rates, int *ofdm_rates, struct iwm_node *in)
159 {
160 	int lowest_present_ofdm = 100;
161 	int lowest_present_cck = 100;
162 	uint8_t cck = 0;
163 	uint8_t ofdm = 0;
164 	int i;
165 	struct ieee80211_rateset *rs = &in->in_ni.ni_rates;
166 
167 	if (is2ghz) {
168 		for (i = IWM_FIRST_CCK_RATE; i <= IWM_LAST_CCK_RATE; i++) {
169 			if ((iwm_ridx2rate(rs, i) & IEEE80211_RATE_BASIC) == 0)
170 				continue;
171 			cck |= (1 << i);
172 			if (lowest_present_cck > i)
173 				lowest_present_cck = i;
174 		}
175 	}
176 	for (i = IWM_FIRST_OFDM_RATE; i <= IWM_LAST_NON_HT_RATE; i++) {
177 		if ((iwm_ridx2rate(rs, i) & IEEE80211_RATE_BASIC) == 0)
178 			continue;
179 		ofdm |= (1 << (i - IWM_FIRST_OFDM_RATE));
180 		if (lowest_present_ofdm > i)
181 			lowest_present_ofdm = i;
182 	}
183 
184 	/*
185 	 * Now we've got the basic rates as bitmaps in the ofdm and cck
186 	 * variables. This isn't sufficient though, as there might not
187 	 * be all the right rates in the bitmap. E.g. if the only basic
188 	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
189 	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
190 	 *
191 	 *    [...] a STA responding to a received frame shall transmit
192 	 *    its Control Response frame [...] at the highest rate in the
193 	 *    BSSBasicRateSet parameter that is less than or equal to the
194 	 *    rate of the immediately previous frame in the frame exchange
195 	 *    sequence ([...]) and that is of the same modulation class
196 	 *    ([...]) as the received frame. If no rate contained in the
197 	 *    BSSBasicRateSet parameter meets these conditions, then the
198 	 *    control frame sent in response to a received frame shall be
199 	 *    transmitted at the highest mandatory rate of the PHY that is
200 	 *    less than or equal to the rate of the received frame, and
201 	 *    that is of the same modulation class as the received frame.
202 	 *
203 	 * As a consequence, we need to add all mandatory rates that are
204 	 * lower than all of the basic rates to these bitmaps.
205 	 */
206 
207 	if (IWM_RATE_24M_INDEX < lowest_present_ofdm)
208 		ofdm |= IWM_RATE_BIT_MSK(24) >> IWM_FIRST_OFDM_RATE;
209 	if (IWM_RATE_12M_INDEX < lowest_present_ofdm)
210 		ofdm |= IWM_RATE_BIT_MSK(12) >> IWM_FIRST_OFDM_RATE;
211 	/* 6M already there or needed so always add */
212 	ofdm |= IWM_RATE_BIT_MSK(6) >> IWM_FIRST_OFDM_RATE;
213 
214 	/*
215 	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
216 	 * Note, however:
217 	 *  - if no CCK rates are basic, it must be ERP since there must
218 	 *    be some basic rates at all, so they're OFDM => ERP PHY
219 	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
220 	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
221 	 *  - if 5.5M is basic, 1M and 2M are mandatory
222 	 *  - if 2M is basic, 1M is mandatory
223 	 *  - if 1M is basic, that's the only valid ACK rate.
224 	 * As a consequence, it's not as complicated as it sounds, just add
225 	 * any lower rates to the ACK rate bitmap.
226 	 */
227 	if (IWM_RATE_11M_INDEX < lowest_present_cck)
228 		cck |= IWM_RATE_BIT_MSK(11) >> IWM_FIRST_CCK_RATE;
229 	if (IWM_RATE_5M_INDEX < lowest_present_cck)
230 		cck |= IWM_RATE_BIT_MSK(5) >> IWM_FIRST_CCK_RATE;
231 	if (IWM_RATE_2M_INDEX < lowest_present_cck)
232 		cck |= IWM_RATE_BIT_MSK(2) >> IWM_FIRST_CCK_RATE;
233 	/* 1M already there or needed so always add */
234 	cck |= IWM_RATE_BIT_MSK(1) >> IWM_FIRST_CCK_RATE;
235 
236 	*cck_rates = cck;
237 	*ofdm_rates = ofdm;
238 }
239 
240 static void
241 iwm_mvm_mac_ctxt_cmd_common(struct iwm_softc *sc, struct iwm_node *in,
242 	struct iwm_mac_ctx_cmd *cmd, uint32_t action)
243 {
244 	struct ieee80211com *ic = &sc->sc_ic;
245 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
246 	struct ieee80211_node *ni = vap->iv_bss;
247 	struct iwm_vap *ivp = IWM_VAP(vap);
248 	int cck_ack_rates, ofdm_ack_rates;
249 	int i;
250 	int is2ghz;
251 
252 	/*
253 	 * id is the MAC address ID - something to do with MAC filtering.
254 	 * color - not sure.
255 	 *
256 	 * These are both functions of the vap, not of the node.
257 	 * So, for now, hard-code both to 0 (default).
258 	 */
259 	cmd->id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id,
260 	    ivp->color));
261 	cmd->action = htole32(action);
262 
263 	cmd->mac_type = htole32(IWM_FW_MAC_TYPE_BSS_STA);
264 
265 	/*
266 	 * The TSF ID is one of four TSF tracking resources in the firmware.
267 	 * Read the iwlwifi/mvm code for more details.
268 	 *
269 	 * For now, just hard-code it to TSF tracking ID 0; we only support
270 	 * a single STA mode VAP.
271 	 *
272 	 * It's per-vap, not per-node.
273 	 */
274 	cmd->tsf_id = htole32(IWM_DEFAULT_TSFID);
275 
276 	IEEE80211_ADDR_COPY(cmd->node_addr, vap->iv_myaddr);
277 
278 	/*
279 	 * XXX should we error out if in_assoc is 1 and ni == NULL?
280 	 */
281 #if 0
282 	if (in->in_assoc) {
283 		IEEE80211_ADDR_COPY(cmd->bssid_addr, ni->ni_bssid);
284 	} else {
285 		/* eth broadcast address */
286 		IEEE80211_ADDR_COPY(cmd->bssid_addr, ieee80211broadcastaddr);
287 	}
288 #else
289 	/*
290 	 * XXX This workaround makes the firmware behave more correctly once
291 	 *     we are associated, regularly giving us statistics notifications,
292 	 *     as well as signaling missed beacons to us.
293 	 *     Since we only call iwm_mvm_mac_ctxt_add() and
294 	 *     iwm_mvm_mac_ctxt_changed() when already authenticating or
295 	 *     associating, ni->ni_bssid should always make sense here.
296 	 */
297 	if (ivp->iv_auth) {
298 		IEEE80211_ADDR_COPY(cmd->bssid_addr, ni->ni_bssid);
299 	} else {
300 		/* XXX Or maybe all zeroes address? */
301 		IEEE80211_ADDR_COPY(cmd->bssid_addr, ieee80211broadcastaddr);
302 	}
303 #endif
304 
305 	/*
306 	 * Default to 2ghz if no node information is given.
307 	 */
308 	if (in && in->in_ni.ni_chan != IEEE80211_CHAN_ANYC) {
309 		is2ghz = !! IEEE80211_IS_CHAN_2GHZ(in->in_ni.ni_chan);
310 	} else {
311 		is2ghz = 1;
312 	}
313 	iwm_mvm_ack_rates(sc, is2ghz, &cck_ack_rates, &ofdm_ack_rates, in);
314 	cmd->cck_rates = htole32(cck_ack_rates);
315 	cmd->ofdm_rates = htole32(ofdm_ack_rates);
316 
317 	cmd->cck_short_preamble
318 	    = htole32((ic->ic_flags & IEEE80211_F_SHPREAMBLE)
319 	      ? IWM_MAC_FLG_SHORT_PREAMBLE : 0);
320 	cmd->short_slot
321 	    = htole32((ic->ic_flags & IEEE80211_F_SHSLOT)
322 	      ? IWM_MAC_FLG_SHORT_SLOT : 0);
323 
324 	for (i = 0; i < WME_NUM_AC; i++) {
325 		uint8_t txf = iwm_mvm_ac_to_tx_fifo[i];
326 
327 		cmd->ac[txf].cw_min = htole16(ivp->queue_params[i].cw_min);
328 		cmd->ac[txf].cw_max = htole16(ivp->queue_params[i].cw_max);
329 		cmd->ac[txf].edca_txop =
330 		    htole16(ivp->queue_params[i].edca_txop);
331 		cmd->ac[txf].aifsn = ivp->queue_params[i].aifsn;
332 		cmd->ac[txf].fifos_mask = (1 << txf);
333 	}
334 
335 	if (ivp->have_wme)
336 		cmd->qos_flags |= htole32(IWM_MAC_QOS_FLG_UPDATE_EDCA);
337 
338 	if (ic->ic_flags & IEEE80211_F_USEPROT)
339 		cmd->protection_flags |= htole32(IWM_MAC_PROT_FLG_TGG_PROTECT);
340 
341 	cmd->filter_flags = htole32(IWM_MAC_FILTER_ACCEPT_GRP);
342 }
343 
344 static int
345 iwm_mvm_mac_ctxt_send_cmd(struct iwm_softc *sc, struct iwm_mac_ctx_cmd *cmd)
346 {
347 	int ret = iwm_mvm_send_cmd_pdu(sc, IWM_MAC_CONTEXT_CMD, IWM_CMD_SYNC,
348 				       sizeof(*cmd), cmd);
349 	if (ret)
350 		device_printf(sc->sc_dev,
351 		    "%s: Failed to send MAC context (action:%d): %d\n",
352 		    __func__, le32toh(cmd->action), ret);
353 	return ret;
354 }
355 
356 /*
357  * Fill the specific data for mac context of type station or p2p client
358  */
359 static void
360 iwm_mvm_mac_ctxt_cmd_fill_sta(struct iwm_softc *sc, struct iwm_node *in,
361 	struct iwm_mac_data_sta *ctxt_sta, int force_assoc_off)
362 {
363 	struct ieee80211_node *ni = &in->in_ni;
364 	unsigned dtim_period, dtim_count;
365 	struct ieee80211com *ic = &sc->sc_ic;
366 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
367 
368 	/* will this work? */
369 	dtim_period = vap->iv_dtim_period;
370 	dtim_count = vap->iv_dtim_count;
371 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_BEACON | IWM_DEBUG_CMD,
372 	    "%s: force_assoc_off=%d\n", __func__, force_assoc_off);
373 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_BEACON | IWM_DEBUG_CMD,
374 	    "DTIM: period=%d count=%d\n", dtim_period, dtim_count);
375 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_BEACON | IWM_DEBUG_CMD,
376 	    "BEACON: tsf: %llu, ni_intval=%d\n",
377 	    (unsigned long long) le64toh(ni->ni_tstamp.tsf),
378 	    ni->ni_intval);
379 
380 	/* We need the dtim_period to set the MAC as associated */
381 	if (in->in_assoc && dtim_period && !force_assoc_off) {
382 		uint64_t tsf;
383 		uint32_t dtim_offs;
384 
385 		/*
386 		 * The DTIM count counts down, so when it is N that means N
387 		 * more beacon intervals happen until the DTIM TBTT. Therefore
388 		 * add this to the current time. If that ends up being in the
389 		 * future, the firmware will handle it.
390 		 *
391 		 * Also note that the system_timestamp (which we get here as
392 		 * "sync_device_ts") and TSF timestamp aren't at exactly the
393 		 * same offset in the frame -- the TSF is at the first symbol
394 		 * of the TSF, the system timestamp is at signal acquisition
395 		 * time. This means there's an offset between them of at most
396 		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
397 		 * 384us in the longest case), this is currently not relevant
398 		 * as the firmware wakes up around 2ms before the TBTT.
399 		 */
400 		dtim_offs = dtim_count * ni->ni_intval;
401 		/* convert TU to usecs */
402 		dtim_offs *= 1024;
403 
404 		/*
405 		 * net80211: TSF is in 802.11 order, so convert up to local
406 		 * ordering before we manipulate things.
407 		 */
408 		tsf = le64toh(ni->ni_tstamp.tsf);
409 
410 		ctxt_sta->dtim_tsf = htole64(tsf + dtim_offs);
411 		ctxt_sta->dtim_time = htole32(tsf + dtim_offs);
412 
413 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_BEACON | IWM_DEBUG_CMD,
414 		    "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
415 		    (long long)le64toh(ctxt_sta->dtim_tsf),
416 		    le32toh(ctxt_sta->dtim_time), dtim_offs);
417 
418 		ctxt_sta->is_assoc = htole32(1);
419 	} else {
420 		ctxt_sta->is_assoc = htole32(0);
421 	}
422 
423 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_CMD | IWM_DEBUG_BEACON,
424 	    "%s: ni_intval: %d, bi_reciprocal: %d, dtim_interval: %d, dtim_reciprocal: %d\n",
425 	    __func__,
426 	    ni->ni_intval,
427 	    iwm_mvm_reciprocal(ni->ni_intval),
428 	    ni->ni_intval * dtim_period,
429 	    iwm_mvm_reciprocal(ni->ni_intval * dtim_period));
430 
431 	ctxt_sta->bi = htole32(ni->ni_intval);
432 	ctxt_sta->bi_reciprocal = htole32(iwm_mvm_reciprocal(ni->ni_intval));
433 	ctxt_sta->dtim_interval = htole32(ni->ni_intval * dtim_period);
434 	ctxt_sta->dtim_reciprocal =
435 	    htole32(iwm_mvm_reciprocal(ni->ni_intval * dtim_period));
436 
437 	/* 10 = CONN_MAX_LISTEN_INTERVAL */
438 	ctxt_sta->listen_interval = htole32(10);
439 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_CMD | IWM_DEBUG_BEACON,
440 	    "%s: associd=%d\n", __func__, IEEE80211_AID(ni->ni_associd));
441 	ctxt_sta->assoc_id = htole32(IEEE80211_AID(ni->ni_associd));
442 }
443 
444 static int
445 iwm_mvm_mac_ctxt_cmd_station(struct iwm_softc *sc, struct ieee80211vap *vap,
446 	uint32_t action)
447 {
448 	struct ieee80211_node *ni = vap->iv_bss;
449 	struct iwm_node *in = IWM_NODE(ni);
450 	struct iwm_mac_ctx_cmd cmd = {};
451 
452 	IWM_DPRINTF(sc, IWM_DEBUG_RESET,
453 	    "%s: called; action=%d\n", __func__, action);
454 
455 	/* Fill the common data for all mac context types */
456 	iwm_mvm_mac_ctxt_cmd_common(sc, in, &cmd, action);
457 
458 	/* Allow beacons to pass through as long as we are not associated,or we
459 	 * do not have dtim period information */
460 	if (!in->in_assoc || !vap->iv_dtim_period)
461 		cmd.filter_flags |= htole32(IWM_MAC_FILTER_IN_BEACON);
462 	else
463 		cmd.filter_flags &= ~htole32(IWM_MAC_FILTER_IN_BEACON);
464 
465 	/* Fill the data specific for station mode */
466 	iwm_mvm_mac_ctxt_cmd_fill_sta(sc, in,
467 	    &cmd.sta, action == IWM_FW_CTXT_ACTION_ADD);
468 
469 	return iwm_mvm_mac_ctxt_send_cmd(sc, &cmd);
470 }
471 
472 static int
473 iwm_mvm_mac_ctx_send(struct iwm_softc *sc, struct ieee80211vap *vap,
474     uint32_t action)
475 {
476 	return iwm_mvm_mac_ctxt_cmd_station(sc, vap, action);
477 }
478 
479 int
480 iwm_mvm_mac_ctxt_add(struct iwm_softc *sc, struct ieee80211vap *vap)
481 {
482 	struct iwm_vap *iv = IWM_VAP(vap);
483 	int ret;
484 
485 	if (iv->is_uploaded != 0) {
486 		device_printf(sc->sc_dev, "%s: called; uploaded != 0\n",
487 		    __func__);
488 		return (EIO);
489 	}
490 
491 	ret = iwm_mvm_mac_ctx_send(sc, vap, IWM_FW_CTXT_ACTION_ADD);
492 	if (ret)
493 		return (ret);
494 	iv->is_uploaded = 1;
495 	return (0);
496 }
497 
498 int
499 iwm_mvm_mac_ctxt_changed(struct iwm_softc *sc, struct ieee80211vap *vap)
500 {
501 	struct iwm_vap *iv = IWM_VAP(vap);
502 
503 	if (iv->is_uploaded == 0) {
504 		device_printf(sc->sc_dev, "%s: called; uploaded = 0\n",
505 		    __func__);
506 		return (EIO);
507 	}
508 	return iwm_mvm_mac_ctx_send(sc, vap, IWM_FW_CTXT_ACTION_MODIFY);
509 }
510 
511 #if 0
512 static int
513 iwm_mvm_mac_ctxt_remove(struct iwm_softc *sc, struct iwm_node *in)
514 {
515 	struct iwm_mac_ctx_cmd cmd;
516 	int ret;
517 
518 	if (!in->in_uploaded) {
519 		device_printf(sc->sc_dev,
520 		    "attempt to remove !uploaded node %p", in);
521 		return EIO;
522 	}
523 
524 	memset(&cmd, 0, sizeof(cmd));
525 
526 	cmd.id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(IWM_DEFAULT_MACID,
527 	    IWM_DEFAULT_COLOR));
528 	cmd.action = htole32(IWM_FW_CTXT_ACTION_REMOVE);
529 
530 	ret = iwm_mvm_send_cmd_pdu(sc,
531 	    IWM_MAC_CONTEXT_CMD, IWM_CMD_SYNC, sizeof(cmd), &cmd);
532 	if (ret) {
533 		device_printf(sc->sc_dev,
534 		    "Failed to remove MAC context: %d\n", ret);
535 		return ret;
536 	}
537 	in->in_uploaded = 0;
538 
539 	return 0;
540 }
541 #endif
542