xref: /dragonfly/sys/dev/netif/iwm/if_iwm_sta.c (revision ec21d9fb)
1 /*-
2  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
3  * which were used as the reference documentation for this implementation.
4  *
5  * Driver version we are currently based off of is
6  * Linux 4.7.3 (tag id d7f6728f57e3ecbb7ef34eb7d9f564d514775d75)
7  *
8  ***********************************************************************
9  *
10  * This file is provided under a dual BSD/GPLv2 license.  When using or
11  * redistributing this file, you may do so under either license.
12  *
13  * GPL LICENSE SUMMARY
14  *
15  * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
16  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
17  * Copyright(c) 2016 Intel Deutschland GmbH
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of version 2 of the GNU General Public License as
21  * published by the Free Software Foundation.
22  *
23  * This program is distributed in the hope that it will be useful, but
24  * WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
31  * USA
32  *
33  * The full GNU General Public License is included in this distribution
34  * in the file called COPYING.
35  *
36  * Contact Information:
37  *  Intel Linux Wireless <linuxwifi@intel.com>
38  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
39  *
40  * BSD LICENSE
41  *
42  * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
43  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
44  * Copyright(c) 2016 Intel Deutschland GmbH
45  * All rights reserved.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  *
51  *  * Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  *  * Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in
55  *    the documentation and/or other materials provided with the
56  *    distribution.
57  *  * Neither the name Intel Corporation nor the names of its
58  *    contributors may be used to endorse or promote products derived
59  *    from this software without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
62  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
63  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
64  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
65  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
66  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
67  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
68  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
69  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
70  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
71  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72  *
73  *****************************************************************************/
74 
75 #include <sys/param.h>
76 #include <sys/bus.h>
77 #include <sys/conf.h>
78 #include <sys/endian.h>
79 #include <sys/firmware.h>
80 #include <sys/kernel.h>
81 #include <sys/malloc.h>
82 #include <sys/mbuf.h>
83 #include <sys/rman.h>
84 #include <sys/sysctl.h>
85 #include <sys/linker.h>
86 
87 #include <machine/endian.h>
88 
89 #include <bus/pci/pcivar.h>
90 #include <bus/pci/pcireg.h>
91 
92 #include <net/bpf.h>
93 
94 #include <net/if.h>
95 #include <net/if_var.h>
96 #include <net/if_arp.h>
97 #include <net/if_dl.h>
98 #include <net/if_media.h>
99 #include <net/if_types.h>
100 
101 #include <netinet/in.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/if_ether.h>
104 #include <netinet/ip.h>
105 
106 #include <netproto/802_11/ieee80211_var.h>
107 #include <netproto/802_11/ieee80211_regdomain.h>
108 #include <netproto/802_11/ieee80211_ratectl.h>
109 #include <netproto/802_11/ieee80211_radiotap.h>
110 
111 #include "if_iwmreg.h"
112 #include "if_iwmvar.h"
113 #include "if_iwm_config.h"
114 #include "if_iwm_debug.h"
115 #include "if_iwm_constants.h"
116 #include "if_iwm_util.h"
117 #include "if_iwm_mac_ctxt.h"
118 #include "if_iwm_sta.h"
119 
120 /*
121  * New version of ADD_STA_sta command added new fields at the end of the
122  * structure, so sending the size of the relevant API's structure is enough to
123  * support both API versions.
124  */
125 static inline int
126 iwm_add_sta_cmd_size(struct iwm_softc *sc)
127 {
128 	return sc->cfg->mqrx_supported ? sizeof(struct iwm_add_sta_cmd) :
129 	    sizeof(struct iwm_add_sta_cmd_v7);
130 }
131 
132 /* send station add/update command to firmware */
133 int
134 iwm_sta_send_to_fw(struct iwm_softc *sc, struct iwm_node *in,
135 	boolean_t update)
136 {
137 	struct iwm_vap *ivp = IWM_VAP(in->in_ni.ni_vap);
138 	struct iwm_add_sta_cmd add_sta_cmd = {
139 		.sta_id = IWM_STATION_ID,
140 		.mac_id_n_color =
141 		    htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color)),
142 		.add_modify = update ? 1 : 0,
143 		.station_flags_msk = htole32(IWM_STA_FLG_FAT_EN_MSK |
144 					     IWM_STA_FLG_MIMO_EN_MSK),
145 		.tid_disable_tx = htole16(0xffff),
146 	};
147 	int ret;
148 	uint32_t status;
149 	uint32_t agg_size = 0, mpdu_dens = 0;
150 
151 	if (!update) {
152 		int ac;
153 		for (ac = 0; ac < WME_NUM_AC; ac++) {
154 			add_sta_cmd.tfd_queue_msk |=
155 			    htole32(1 << iwm_ac_to_tx_fifo[ac]);
156 		}
157 		IEEE80211_ADDR_COPY(&add_sta_cmd.addr, in->in_ni.ni_bssid);
158 	}
159 
160 	add_sta_cmd.station_flags |=
161 		htole32(agg_size << IWM_STA_FLG_MAX_AGG_SIZE_SHIFT);
162 	add_sta_cmd.station_flags |=
163 		htole32(mpdu_dens << IWM_STA_FLG_AGG_MPDU_DENS_SHIFT);
164 
165 	status = IWM_ADD_STA_SUCCESS;
166 	ret = iwm_send_cmd_pdu_status(sc, IWM_ADD_STA,
167 					  iwm_add_sta_cmd_size(sc),
168 					  &add_sta_cmd, &status);
169 	if (ret)
170 		return ret;
171 
172 	switch (status & IWM_ADD_STA_STATUS_MASK) {
173 	case IWM_ADD_STA_SUCCESS:
174 		IWM_DPRINTF(sc, IWM_DEBUG_NODE, "IWM_ADD_STA PASSED\n");
175 		break;
176 	default:
177 		ret = EIO;
178 		device_printf(sc->sc_dev, "IWM_ADD_STA failed\n");
179 		break;
180 	}
181 
182 	return ret;
183 }
184 
185 int
186 iwm_add_sta(struct iwm_softc *sc, struct iwm_node *in)
187 {
188 	return iwm_sta_send_to_fw(sc, in, FALSE);
189 }
190 
191 int
192 iwm_update_sta(struct iwm_softc *sc, struct iwm_node *in)
193 {
194 	return iwm_sta_send_to_fw(sc, in, TRUE);
195 }
196 
197 int
198 iwm_drain_sta(struct iwm_softc *sc, struct iwm_vap *ivp, boolean_t drain)
199 {
200 	struct iwm_add_sta_cmd cmd = {};
201 	int ret;
202 	uint32_t status;
203 
204 	cmd.mac_id_n_color =
205 	    htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color));
206 	cmd.sta_id = IWM_STATION_ID;
207 	cmd.add_modify = IWM_STA_MODE_MODIFY;
208 	cmd.station_flags = drain ? htole32(IWM_STA_FLG_DRAIN_FLOW) : 0;
209 	cmd.station_flags_msk = htole32(IWM_STA_FLG_DRAIN_FLOW);
210 
211 	status = IWM_ADD_STA_SUCCESS;
212 	ret = iwm_send_cmd_pdu_status(sc, IWM_ADD_STA,
213 					  iwm_add_sta_cmd_size(sc),
214 					  &cmd, &status);
215 	if (ret)
216 		return ret;
217 
218 	switch (status & IWM_ADD_STA_STATUS_MASK) {
219 	case IWM_ADD_STA_SUCCESS:
220 		IWM_DPRINTF(sc, IWM_DEBUG_NODE,
221 		    "Frames for staid %d will drained in fw\n", IWM_STATION_ID);
222 		break;
223 	default:
224 		ret = EIO;
225 		device_printf(sc->sc_dev,
226 		    "Couldn't drain frames for staid %d\n", IWM_STATION_ID);
227 		break;
228 	}
229 
230 	return ret;
231 }
232 
233 /*
234  * Remove a station from the FW table. Before sending the command to remove
235  * the station validate that the station is indeed known to the driver (sanity
236  * only).
237  */
238 static int
239 iwm_rm_sta_common(struct iwm_softc *sc)
240 {
241 	struct iwm_rm_sta_cmd rm_sta_cmd = {
242 		.sta_id = IWM_STATION_ID,
243 	};
244 	int ret;
245 
246 	ret = iwm_send_cmd_pdu(sc, IWM_REMOVE_STA, 0,
247 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
248 	if (ret) {
249 		device_printf(sc->sc_dev,
250 		    "Failed to remove station. Id=%d\n", IWM_STATION_ID);
251 		return ret;
252 	}
253 
254 	return 0;
255 }
256 
257 int
258 iwm_rm_sta(struct iwm_softc *sc, struct ieee80211vap *vap,
259 	boolean_t is_assoc)
260 {
261 	uint32_t tfd_queue_msk = 0;
262 	int ret;
263 	int ac;
264 
265 	ret = iwm_drain_sta(sc, IWM_VAP(vap), TRUE);
266 	if (ret)
267 		return ret;
268 	for (ac = 0; ac < WME_NUM_AC; ac++) {
269 		tfd_queue_msk |= htole32(1 << iwm_ac_to_tx_fifo[ac]);
270 	}
271 	ret = iwm_flush_tx_path(sc, tfd_queue_msk, IWM_CMD_SYNC);
272 	if (ret)
273 		return ret;
274 #ifdef notyet /* function not yet implemented */
275 	ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
276 					    mvm_sta->tfd_queue_msk);
277 	if (ret)
278 		return ret;
279 #endif
280 	ret = iwm_drain_sta(sc, IWM_VAP(vap), FALSE);
281 
282 	/* if we are associated - we can't remove the AP STA now */
283 	if (is_assoc)
284 		return ret;
285 
286 	/* XXX wait until STA is drained */
287 
288 	ret = iwm_rm_sta_common(sc);
289 
290 	return ret;
291 }
292 
293 int
294 iwm_rm_sta_id(struct iwm_softc *sc, struct ieee80211vap *vap)
295 {
296 	/* XXX wait until STA is drained */
297 
298 	return iwm_rm_sta_common(sc);
299 }
300 
301 static int
302 iwm_add_int_sta_common(struct iwm_softc *sc, struct iwm_int_sta *sta,
303     const uint8_t *addr, uint16_t mac_id, uint16_t color)
304 {
305 	struct iwm_add_sta_cmd cmd;
306 	int ret;
307 	uint32_t status;
308 
309 	memset(&cmd, 0, sizeof(cmd));
310 	cmd.sta_id = sta->sta_id;
311 	cmd.mac_id_n_color = htole32(IWM_FW_CMD_ID_AND_COLOR(mac_id, color));
312 	if (sta->sta_id == IWM_AUX_STA_ID && sc->cfg->mqrx_supported)
313 		cmd.station_type = IWM_STA_AUX_ACTIVITY;
314 
315 	cmd.tfd_queue_msk = htole32(sta->tfd_queue_msk);
316 	cmd.tid_disable_tx = htole16(0xffff);
317 
318 	if (addr)
319 		IEEE80211_ADDR_COPY(cmd.addr, addr);
320 
321 	ret = iwm_send_cmd_pdu_status(sc, IWM_ADD_STA,
322 					  iwm_add_sta_cmd_size(sc),
323 					  &cmd, &status);
324 	if (ret)
325 		return ret;
326 
327 	switch (status & IWM_ADD_STA_STATUS_MASK) {
328 	case IWM_ADD_STA_SUCCESS:
329 		IWM_DPRINTF(sc, IWM_DEBUG_NODE, "Internal station added.\n");
330 		return 0;
331 	default:
332 		ret = EIO;
333 		device_printf(sc->sc_dev,
334 		    "Add internal station failed, status=0x%x\n", status);
335 		break;
336 	}
337 	return ret;
338 }
339 
340 int
341 iwm_add_aux_sta(struct iwm_softc *sc)
342 {
343 	int ret;
344 
345 	sc->sc_aux_sta.sta_id = IWM_AUX_STA_ID;
346 	sc->sc_aux_sta.tfd_queue_msk = (1 << IWM_AUX_QUEUE);
347 
348 	/* Map Aux queue to fifo - needs to happen before adding Aux station */
349 	ret = iwm_enable_txq(sc, IWM_AUX_STA_ID, IWM_AUX_QUEUE,
350 	    IWM_TX_FIFO_MCAST);
351 	if (ret)
352 		return ret;
353 
354 	ret = iwm_add_int_sta_common(sc, &sc->sc_aux_sta, NULL,
355 					 IWM_MAC_INDEX_AUX, 0);
356 
357 	if (ret) {
358 		memset(&sc->sc_aux_sta, 0, sizeof(sc->sc_aux_sta));
359 		sc->sc_aux_sta.sta_id = IWM_STATION_COUNT;
360 	}
361 	return ret;
362 }
363 
364 void iwm_del_aux_sta(struct iwm_softc *sc)
365 {
366 	memset(&sc->sc_aux_sta, 0, sizeof(sc->sc_aux_sta));
367 	sc->sc_aux_sta.sta_id = IWM_STATION_COUNT;
368 }
369