xref: /freebsd/sys/dev/iwm/if_iwm_sta.c (revision d6b92ffa)
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/cdefs.h>
76 __FBSDID("$FreeBSD$");
77 
78 #include "opt_wlan.h"
79 #include "opt_iwm.h"
80 
81 #include <sys/param.h>
82 #include <sys/bus.h>
83 #include <sys/conf.h>
84 #include <sys/endian.h>
85 #include <sys/firmware.h>
86 #include <sys/kernel.h>
87 #include <sys/malloc.h>
88 #include <sys/mbuf.h>
89 #include <sys/mutex.h>
90 #include <sys/module.h>
91 #include <sys/proc.h>
92 #include <sys/rman.h>
93 #include <sys/socket.h>
94 #include <sys/sockio.h>
95 #include <sys/sysctl.h>
96 #include <sys/linker.h>
97 
98 #include <machine/bus.h>
99 #include <machine/endian.h>
100 #include <machine/resource.h>
101 
102 #include <dev/pci/pcivar.h>
103 #include <dev/pci/pcireg.h>
104 
105 #include <net/bpf.h>
106 
107 #include <net/if.h>
108 #include <net/if_var.h>
109 #include <net/if_arp.h>
110 #include <net/if_dl.h>
111 #include <net/if_media.h>
112 #include <net/if_types.h>
113 
114 #include <netinet/in.h>
115 #include <netinet/in_systm.h>
116 #include <netinet/if_ether.h>
117 #include <netinet/ip.h>
118 
119 #include <net80211/ieee80211_var.h>
120 #include <net80211/ieee80211_regdomain.h>
121 #include <net80211/ieee80211_ratectl.h>
122 #include <net80211/ieee80211_radiotap.h>
123 
124 #include <dev/iwm/if_iwmreg.h>
125 #include <dev/iwm/if_iwmvar.h>
126 #include <dev/iwm/if_iwm_config.h>
127 #include <dev/iwm/if_iwm_debug.h>
128 #include <dev/iwm/if_iwm_constants.h>
129 #include <dev/iwm/if_iwm_util.h>
130 #include <dev/iwm/if_iwm_mac_ctxt.h>
131 #include <dev/iwm/if_iwm_sta.h>
132 
133 /*
134  * New version of ADD_STA_sta command added new fields at the end of the
135  * structure, so sending the size of the relevant API's structure is enough to
136  * support both API versions.
137  */
138 static inline int
139 iwm_mvm_add_sta_cmd_size(struct iwm_softc *sc)
140 {
141 #ifdef notyet
142 	return iwm_mvm_has_new_rx_api(mvm) ?
143 		sizeof(struct iwm_mvm_add_sta_cmd) :
144 		sizeof(struct iwm_mvm_add_sta_cmd_v7);
145 #else
146 	return sizeof(struct iwm_mvm_add_sta_cmd);
147 #endif
148 }
149 
150 /* send station add/update command to firmware */
151 int
152 iwm_mvm_sta_send_to_fw(struct iwm_softc *sc, struct iwm_node *in,
153 	boolean_t update)
154 {
155 	struct iwm_vap *ivp = IWM_VAP(in->in_ni.ni_vap);
156 	struct iwm_mvm_add_sta_cmd add_sta_cmd = {
157 		.sta_id = IWM_STATION_ID,
158 		.mac_id_n_color =
159 		    htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color)),
160 		.add_modify = update ? 1 : 0,
161 		.station_flags_msk = htole32(IWM_STA_FLG_FAT_EN_MSK |
162 					     IWM_STA_FLG_MIMO_EN_MSK),
163 		.tid_disable_tx = htole16(0xffff),
164 	};
165 	int ret;
166 	uint32_t status;
167 	uint32_t agg_size = 0, mpdu_dens = 0;
168 
169 	if (!update) {
170 		int ac;
171 		for (ac = 0; ac < WME_NUM_AC; ac++) {
172 			add_sta_cmd.tfd_queue_msk |=
173 			    htole32(1 << iwm_mvm_ac_to_tx_fifo[ac]);
174 		}
175 		IEEE80211_ADDR_COPY(&add_sta_cmd.addr, in->in_ni.ni_bssid);
176 	}
177 
178 	add_sta_cmd.station_flags |=
179 		htole32(agg_size << IWM_STA_FLG_MAX_AGG_SIZE_SHIFT);
180 	add_sta_cmd.station_flags |=
181 		htole32(mpdu_dens << IWM_STA_FLG_AGG_MPDU_DENS_SHIFT);
182 
183 	status = IWM_ADD_STA_SUCCESS;
184 	ret = iwm_mvm_send_cmd_pdu_status(sc, IWM_ADD_STA,
185 					  iwm_mvm_add_sta_cmd_size(sc),
186 					  &add_sta_cmd, &status);
187 	if (ret)
188 		return ret;
189 
190 	switch (status & IWM_ADD_STA_STATUS_MASK) {
191 	case IWM_ADD_STA_SUCCESS:
192 		IWM_DPRINTF(sc, IWM_DEBUG_NODE, "IWM_ADD_STA PASSED\n");
193 		break;
194 	default:
195 		ret = EIO;
196 		device_printf(sc->sc_dev, "IWM_ADD_STA failed\n");
197 		break;
198 	}
199 
200 	return ret;
201 }
202 
203 int
204 iwm_mvm_add_sta(struct iwm_softc *sc, struct iwm_node *in)
205 {
206 	return iwm_mvm_sta_send_to_fw(sc, in, FALSE);
207 }
208 
209 int
210 iwm_mvm_update_sta(struct iwm_softc *sc, struct iwm_node *in)
211 {
212 	return iwm_mvm_sta_send_to_fw(sc, in, TRUE);
213 }
214 
215 int
216 iwm_mvm_drain_sta(struct iwm_softc *sc, struct iwm_vap *ivp, boolean_t drain)
217 {
218 	struct iwm_mvm_add_sta_cmd cmd = {};
219 	int ret;
220 	uint32_t status;
221 
222 	cmd.mac_id_n_color =
223 	    htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color));
224 	cmd.sta_id = IWM_STATION_ID;
225 	cmd.add_modify = IWM_STA_MODE_MODIFY;
226 	cmd.station_flags = drain ? htole32(IWM_STA_FLG_DRAIN_FLOW) : 0;
227 	cmd.station_flags_msk = htole32(IWM_STA_FLG_DRAIN_FLOW);
228 
229 	status = IWM_ADD_STA_SUCCESS;
230 	ret = iwm_mvm_send_cmd_pdu_status(sc, IWM_ADD_STA,
231 					  iwm_mvm_add_sta_cmd_size(sc),
232 					  &cmd, &status);
233 	if (ret)
234 		return ret;
235 
236 	switch (status & IWM_ADD_STA_STATUS_MASK) {
237 	case IWM_ADD_STA_SUCCESS:
238 		IWM_DPRINTF(sc, IWM_DEBUG_NODE,
239 		    "Frames for staid %d will drained in fw\n", IWM_STATION_ID);
240 		break;
241 	default:
242 		ret = EIO;
243 		device_printf(sc->sc_dev,
244 		    "Couldn't drain frames for staid %d\n", IWM_STATION_ID);
245 		break;
246 	}
247 
248 	return ret;
249 }
250 
251 /*
252  * Remove a station from the FW table. Before sending the command to remove
253  * the station validate that the station is indeed known to the driver (sanity
254  * only).
255  */
256 static int
257 iwm_mvm_rm_sta_common(struct iwm_softc *sc)
258 {
259 	struct iwm_mvm_rm_sta_cmd rm_sta_cmd = {
260 		.sta_id = IWM_STATION_ID,
261 	};
262 	int ret;
263 
264 	ret = iwm_mvm_send_cmd_pdu(sc, IWM_REMOVE_STA, 0,
265 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
266 	if (ret) {
267 		device_printf(sc->sc_dev,
268 		    "Failed to remove station. Id=%d\n", IWM_STATION_ID);
269 		return ret;
270 	}
271 
272 	return 0;
273 }
274 
275 int
276 iwm_mvm_rm_sta(struct iwm_softc *sc, struct ieee80211vap *vap,
277 	boolean_t is_assoc)
278 {
279 	uint32_t tfd_queue_msk = 0;
280 	int ret;
281 	int ac;
282 
283 	ret = iwm_mvm_drain_sta(sc, IWM_VAP(vap), TRUE);
284 	if (ret)
285 		return ret;
286 	mbufq_drain(&sc->sc_snd); /* XXX needed ? */
287 	for (ac = 0; ac < WME_NUM_AC; ac++) {
288 		tfd_queue_msk |= htole32(1 << iwm_mvm_ac_to_tx_fifo[ac]);
289 	}
290 	ret = iwm_mvm_flush_tx_path(sc, tfd_queue_msk, 0);
291 	if (ret)
292 		return ret;
293 #ifdef notyet /* function not yet implemented */
294 	ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
295 					    mvm_sta->tfd_queue_msk);
296 	if (ret)
297 		return ret;
298 #endif
299 	ret = iwm_mvm_drain_sta(sc, IWM_VAP(vap), FALSE);
300 
301 	/* if we are associated - we can't remove the AP STA now */
302 	if (is_assoc)
303 		return ret;
304 
305 	/* XXX wait until STA is drained */
306 
307 	ret = iwm_mvm_rm_sta_common(sc);
308 
309 	return ret;
310 }
311 
312 int
313 iwm_mvm_rm_sta_id(struct iwm_softc *sc, struct ieee80211vap *vap)
314 {
315 	/* XXX wait until STA is drained */
316 
317 	return iwm_mvm_rm_sta_common(sc);
318 }
319 
320 static int
321 iwm_mvm_add_int_sta_common(struct iwm_softc *sc, struct iwm_int_sta *sta,
322 	const uint8_t *addr, uint16_t mac_id, uint16_t color)
323 {
324 	struct iwm_mvm_add_sta_cmd cmd;
325 	int ret;
326 	uint32_t status;
327 
328 	memset(&cmd, 0, sizeof(cmd));
329 	cmd.sta_id = sta->sta_id;
330 	cmd.mac_id_n_color = htole32(IWM_FW_CMD_ID_AND_COLOR(mac_id, color));
331 
332 	cmd.tfd_queue_msk = htole32(sta->tfd_queue_msk);
333 	cmd.tid_disable_tx = htole16(0xffff);
334 
335 	if (addr)
336 		IEEE80211_ADDR_COPY(cmd.addr, addr);
337 
338 	ret = iwm_mvm_send_cmd_pdu_status(sc, IWM_ADD_STA,
339 					  iwm_mvm_add_sta_cmd_size(sc),
340 					  &cmd, &status);
341 	if (ret)
342 		return ret;
343 
344 	switch (status & IWM_ADD_STA_STATUS_MASK) {
345 	case IWM_ADD_STA_SUCCESS:
346 		IWM_DPRINTF(sc, IWM_DEBUG_NODE, "Internal station added.\n");
347 		return 0;
348 	default:
349 		ret = EIO;
350 		device_printf(sc->sc_dev,
351 		    "Add internal station failed, status=0x%x\n", status);
352 		break;
353 	}
354 	return ret;
355 }
356 
357 int
358 iwm_mvm_add_aux_sta(struct iwm_softc *sc)
359 {
360 	int ret;
361 
362 	sc->sc_aux_sta.sta_id = IWM_AUX_STA_ID;
363 	sc->sc_aux_sta.tfd_queue_msk = (1 << IWM_MVM_AUX_QUEUE);
364 
365 	/* Map Aux queue to fifo - needs to happen before adding Aux station */
366 	ret = iwm_enable_txq(sc, 0, IWM_MVM_AUX_QUEUE, IWM_MVM_TX_FIFO_MCAST);
367 	if (ret)
368 		return ret;
369 
370 	ret = iwm_mvm_add_int_sta_common(sc, &sc->sc_aux_sta, NULL,
371 					 IWM_MAC_INDEX_AUX, 0);
372 
373 	if (ret) {
374 		memset(&sc->sc_aux_sta, 0, sizeof(sc->sc_aux_sta));
375 		sc->sc_aux_sta.sta_id = IWM_MVM_STATION_COUNT;
376 	}
377 	return ret;
378 }
379 
380 void iwm_mvm_del_aux_sta(struct iwm_softc *sc)
381 {
382 	memset(&sc->sc_aux_sta, 0, sizeof(sc->sc_aux_sta));
383 	sc->sc_aux_sta.sta_id = IWM_MVM_STATION_COUNT;
384 }
385