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