xref: /freebsd/sys/dev/iwm/if_iwm_phy_ctxt.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
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 #include "opt_wlan.h"
107 #include "opt_iwm.h"
108 
109 #include <sys/param.h>
110 #include <sys/bus.h>
111 #include <sys/conf.h>
112 #include <sys/endian.h>
113 #include <sys/firmware.h>
114 #include <sys/kernel.h>
115 #include <sys/malloc.h>
116 #include <sys/mbuf.h>
117 #include <sys/mutex.h>
118 #include <sys/module.h>
119 #include <sys/proc.h>
120 #include <sys/rman.h>
121 #include <sys/socket.h>
122 #include <sys/sockio.h>
123 #include <sys/sysctl.h>
124 #include <sys/linker.h>
125 
126 #include <machine/bus.h>
127 #include <machine/endian.h>
128 #include <machine/resource.h>
129 
130 #include <dev/pci/pcivar.h>
131 #include <dev/pci/pcireg.h>
132 
133 #include <net/bpf.h>
134 
135 #include <net/if.h>
136 #include <net/if_var.h>
137 #include <net/if_arp.h>
138 #include <net/if_dl.h>
139 #include <net/if_media.h>
140 #include <net/if_types.h>
141 
142 #include <netinet/in.h>
143 #include <netinet/in_systm.h>
144 #include <netinet/if_ether.h>
145 #include <netinet/ip.h>
146 
147 #include <net80211/ieee80211_var.h>
148 #include <net80211/ieee80211_regdomain.h>
149 #include <net80211/ieee80211_ratectl.h>
150 #include <net80211/ieee80211_radiotap.h>
151 
152 #include <dev/iwm/if_iwmreg.h>
153 #include <dev/iwm/if_iwmvar.h>
154 #include <dev/iwm/if_iwm_debug.h>
155 #include <dev/iwm/if_iwm_util.h>
156 #include <dev/iwm/if_iwm_phy_ctxt.h>
157 
158 /*
159  * BEGIN iwlwifi/mvm/phy-ctxt.c
160  */
161 
162 /*
163  * Construct the generic fields of the PHY context command
164  */
165 static void
166 iwm_phy_ctxt_cmd_hdr(struct iwm_softc *sc, struct iwm_phy_ctxt *ctxt,
167 	struct iwm_phy_context_cmd *cmd, uint32_t action, uint32_t apply_time)
168 {
169 	memset(cmd, 0, sizeof(struct iwm_phy_context_cmd));
170 
171 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_CMD,
172 	    "%s: id=%d, colour=%d, action=%d, apply_time=%d\n",
173 	    __func__,
174 	    ctxt->id,
175 	    ctxt->color,
176 	    action,
177 	    apply_time);
178 
179 	cmd->id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(ctxt->id,
180 	    ctxt->color));
181 	cmd->action = htole32(action);
182 	cmd->apply_time = htole32(apply_time);
183 }
184 
185 /*
186  * Add the phy configuration to the PHY context command
187  */
188 static void
189 iwm_phy_ctxt_cmd_data(struct iwm_softc *sc,
190 	struct iwm_phy_context_cmd *cmd, struct ieee80211_channel *chan,
191 	uint8_t chains_static, uint8_t chains_dynamic)
192 {
193 	struct ieee80211com *ic = &sc->sc_ic;
194 	uint8_t active_cnt, idle_cnt;
195 
196 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_CMD,
197 	    "%s: 2ghz=%d, channel=%d, chains static=0x%x, dynamic=0x%x, "
198 	    "rx_ant=0x%x, tx_ant=0x%x\n",
199 	    __func__,
200 	    !! IEEE80211_IS_CHAN_2GHZ(chan),
201 	    ieee80211_chan2ieee(ic, chan),
202 	    chains_static,
203 	    chains_dynamic,
204 	    iwm_get_valid_rx_ant(sc),
205 	    iwm_get_valid_tx_ant(sc));
206 
207 
208 	cmd->ci.band = IEEE80211_IS_CHAN_2GHZ(chan) ?
209 	    IWM_PHY_BAND_24 : IWM_PHY_BAND_5;
210 
211 	cmd->ci.channel = ieee80211_chan2ieee(ic, chan);
212 	cmd->ci.width = IWM_PHY_VHT_CHANNEL_MODE20;
213 	cmd->ci.ctrl_pos = IWM_PHY_VHT_CTRL_POS_1_BELOW;
214 
215 	/* Set rx the chains */
216 	idle_cnt = chains_static;
217 	active_cnt = chains_dynamic;
218 
219 	/* In scenarios where we only ever use a single-stream rates,
220 	 * i.e. legacy 11b/g/a associations, single-stream APs or even
221 	 * static SMPS, enable both chains to get diversity, improving
222 	 * the case where we're far enough from the AP that attenuation
223 	 * between the two antennas is sufficiently different to impact
224 	 * performance.
225 	 */
226 	if (active_cnt == 1 && iwm_rx_diversity_allowed(sc)) {
227 		idle_cnt = 2;
228 		active_cnt = 2;
229 	}
230 
231 	cmd->rxchain_info = htole32(iwm_get_valid_rx_ant(sc) <<
232 					IWM_PHY_RX_CHAIN_VALID_POS);
233 	cmd->rxchain_info |= htole32(idle_cnt << IWM_PHY_RX_CHAIN_CNT_POS);
234 	cmd->rxchain_info |= htole32(active_cnt <<
235 	    IWM_PHY_RX_CHAIN_MIMO_CNT_POS);
236 
237 	cmd->txchain_info = htole32(iwm_get_valid_tx_ant(sc));
238 }
239 
240 /*
241  * Send a command
242  * only if something in the configuration changed: in case that this is the
243  * first time that the phy configuration is applied or in case that the phy
244  * configuration changed from the previous apply.
245  */
246 static int
247 iwm_phy_ctxt_apply(struct iwm_softc *sc,
248 	struct iwm_phy_ctxt *ctxt,
249 	uint8_t chains_static, uint8_t chains_dynamic,
250 	uint32_t action, uint32_t apply_time)
251 {
252 	struct iwm_phy_context_cmd cmd;
253 	int ret;
254 
255 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_CMD,
256 	    "%s: called; channel=%p\n",
257 	    __func__,
258 	    ctxt->channel);
259 
260 	/* Set the command header fields */
261 	iwm_phy_ctxt_cmd_hdr(sc, ctxt, &cmd, action, apply_time);
262 
263 	/* Set the command data */
264 	iwm_phy_ctxt_cmd_data(sc, &cmd, ctxt->channel,
265 	    chains_static, chains_dynamic);
266 
267 	ret = iwm_send_cmd_pdu(sc, IWM_PHY_CONTEXT_CMD, IWM_CMD_SYNC,
268 	    sizeof(struct iwm_phy_context_cmd), &cmd);
269 	if (ret) {
270 		device_printf(sc->sc_dev,
271 		    "PHY ctxt cmd error. ret=%d\n", ret);
272 	}
273 	return ret;
274 }
275 
276 /*
277  * Send a command to add a PHY context based on the current HW configuration.
278  */
279 int
280 iwm_phy_ctxt_add(struct iwm_softc *sc, struct iwm_phy_ctxt *ctxt,
281 	struct ieee80211_channel *chan,
282 	uint8_t chains_static, uint8_t chains_dynamic)
283 {
284 	ctxt->channel = chan;
285 
286 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_CMD,
287 	    "%s: called; channel=%d\n",
288 	    __func__,
289 	    ieee80211_chan2ieee(&sc->sc_ic, chan));
290 
291 	return iwm_phy_ctxt_apply(sc, ctxt,
292 	    chains_static, chains_dynamic, IWM_FW_CTXT_ACTION_ADD, 0);
293 }
294 
295 /*
296  * Send a command to modify the PHY context based on the current HW
297  * configuration. Note that the function does not check that the configuration
298  * changed.
299  */
300 int
301 iwm_phy_ctxt_changed(struct iwm_softc *sc,
302 	struct iwm_phy_ctxt *ctxt, struct ieee80211_channel *chan,
303 	uint8_t chains_static, uint8_t chains_dynamic)
304 {
305 	ctxt->channel = chan;
306 
307 	IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_CMD,
308 	    "%s: called; channel=%d\n",
309 	    __func__,
310 	    ieee80211_chan2ieee(&sc->sc_ic, chan));
311 
312 	return iwm_phy_ctxt_apply(sc, ctxt,
313 	    chains_static, chains_dynamic, IWM_FW_CTXT_ACTION_MODIFY, 0);
314 }
315 
316 /*
317  * END iwlwifi/mvm/phy-ctxt.c
318  */
319